gdb - 在 MinGW 上的 GDB 中,如何让 Ctrl-C 停止程序?

标签 gdb mingw

我在 Windows 上,在 MinGW 下构建的可执行文件上运行 GDB。该程序有一个无限循环。我想通过按 Ctrl + C 找到它。当我这样做时,程序和 GDB 都会退出。关于这个主题的所有帮助似乎都假设我使用的是 Linux。

最佳答案

这是因为 GDB 没有正确处理 GUI(非控制台)程序的 Ctrl + C 事件。

您可以在 Workaround for GDB Ctrl-C Interrupt 中找到解决方法

<小时/>

更新:鉴于 mingw.org 域已过期,以下是从网络文件中挽救的代码:

If you find yourself trying to interrupt a program being debugged by GDB with Ctrl-C and failing then this little program will allow you to issue the break from another session. Just run it passing the Windows pid of the process being debugged and GDB will regain control. [To compile, use gcc -o debugbreak -mno-cygwin -mthreads debugbreak.c ]

/* BEGIN debugbreak.c */

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#if _WIN32_WINNT < 0x0501
#error Must target Windows NT 5.0.1 or later for DebugBreakProcess
#endif

#include <Windows.h>
#include <stddef.h>
#include <stdlib.h>

/* Compile with this line:

    gcc -o debugbreak -mno-cygwin -mthreads debugbreak.c

*/

static char errbuffer[256];

static const char *geterrstr(DWORD errcode)
{
    size_t skip = 0;
    DWORD chars;
    chars = FormatMessage(
        FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, errcode, 0, errbuffer, sizeof(errbuffer)-1, 0);
    errbuffer[sizeof(errbuffer)-1] = 0;
    if (chars) {
        while (errbuffer[chars-1] == '\r' || errbuffer[chars-1] == '\n') {
            errbuffer[--chars] = 0;
        }
    }
    if (chars && errbuffer[chars-1] == '.') errbuffer[--chars] = 0;
    if (chars >= 2 && errbuffer[0] == '%' && errbuffer[1] >= '0'
        && errbuffer[1] <= '9')
    {
        skip = 2;
        while (chars > skip && errbuffer[skip] == ' ') ++skip;
        if (chars >= skip+2 && errbuffer[skip] == 'i'
            && errbuffer[skip+1] == 's')
        {
            skip += 2;
            while (chars > skip && errbuffer[skip] == ' ') ++skip;
        }
    }
    if (chars > skip && errbuffer[skip] >= 'A' && errbuffer[skip] <= 'Z') {
        errbuffer[skip] += 'a' - 'A';
    }
    return errbuffer+skip;
}

int main(int argc, char *argv[])
{
    HANDLE proc;
    unsigned proc_id = 0;
    BOOL break_result;

    if (argc != 2) {
        printf("Usage: debugbreak process_id_number\n");
        return 1;
    }
    proc_id = (unsigned) strtol(argv[1], NULL, 0);
    if (proc_id == 0) {
        printf("Invalid process id %u\n", proc_id);
        return 1;
    }
    proc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)proc_id);
    if (proc == NULL) {
        DWORD lastError = GetLastError();
        printf("Failed to open process %u\n", proc_id);
        printf("Error code is %lu (%s)\n", (unsigned long)lastError,
            geterrstr(lastError));
        return 1;
    }
    break_result = DebugBreakProcess(proc);
    if (!break_result) {
        DWORD lastError = GetLastError();
        printf("Failed to debug break process %u\n", proc_id);
        printf("Error code is %lu (%s)\n", (unsigned long)lastError,
            geterrstr(lastError));
        CloseHandle(proc);
        return 1;
    }
    printf("DebugBreak sent successfully to process id %u\n", proc_id);
    CloseHandle(proc);
    return 0;
}

/* END debugbreak.c */

With thanks to Kyle McKay for posting the code to the cygwin mailing list http://cygwin.com/ml/cygwin/2006-06/msg00321.html

关于gdb - 在 MinGW 上的 GDB 中,如何让 Ctrl-C 停止程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/711086/

相关文章:

c - 为什么 gdb 在从函数返回时停在与 "i b"显示不同的行?

c++ - 在 mingw c++ 中获取链接错误

linux - 为什么不带 "not stripped, with debug_info"选项的可执行文件file命令在编译时报 "-g"?

c - 函数调用时指针更改大小(使用 gdb 检查)

c++ - 将 libxbee 与 MinGw 和 eclipse 结合使用

c++ - Netbeans下无DLL依赖Mingw

c - Windows 上的 poll() c 函数

qt - mingw 发出无数关于忽略 "dll import"属性的警告

linux - GDB 不会中断动态加载的 .so 文件?

c - 在 Cypress FX3 中初始化 gdb 调试器时出现以下错误