In GDB on MinGW, how do I make Ctrl-C stop the program?

后端 未结 8 1018
灰色年华
灰色年华 2021-02-05 11:50

I\'m on Windows, running GDB on an executable built under MinGW. The program has an infinite loop. I want to find it by hitting Ctrl + C. When I do, both t

相关标签:
8条回答
  • 2021-02-05 12:19

    To find the infinite loop, you could try stepping through the execution until you get to a sequence that repeats indefinitely.

    I'm not sure, but I think Ctrl-C should only stop the execution, not gdb itself...

    I think there is a "handle" command that you can use to control how the interrupt signal is handled.

    0 讨论(0)
  • 2021-02-05 12:19

    I had the same problem. What solved it for me was using gdb with cmd.exe and setting the following option in gdb.

    set new-console on
    

    Now I can use Ctrl + c to interrupt both gui and console programs.

    0 讨论(0)
  • 2021-02-05 12:27

    Which "shell" are you using? If you use the MSYS "rxvt" shell, the behavior is pretty much as you describe. Ctrl-C only works if you are running from a normal Windows command prompt.

    0 讨论(0)
  • 2021-02-05 12:32

    I've just hit the same problem.

    The workaround from the wiki is to run debugbreak with the pid of the debugged process, but ps doesn't show this pid, only pid of gdb. Maybe there is another way to obtain it.

    But there is simpler workaround. Just start the program normally (not in gdb), check pid from ps and start gdb with this pid as second argument. When gdb is attached the process stops and I can print backtrace.

    0 讨论(0)
  • 2021-02-05 12:33

    As Matthew Talbert pointed out, this happens when GDB built with the native MinGW toolchain is used inside MSYS/Cygwin. Starting GDB with winpty worked like a charm as it's a tool designed just for that. It also worked for cdb.exe.

    0 讨论(0)
  • 2021-02-05 12:38

    Here is a solution that works every time:

    When GDB starts use this regular expression to capture the inferior process id:

    "\[New Thread (\d+)\."
    

    Then use:

    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, PID);
    DebugBreakProcess(hProcess);
    CloseHandle(hProcess);
    

    Have a look on the following GDB initialization script, it is required for this to work with MinGW on Windows 7 and later:

    # =====================================
    #  GDB preload initialization commands
    # =====================================
    
    # Set Unicode Charset
    #set target-charset UCS-2
    #set host-charset UCS-2
    #set charset UCS-2
    #set target-wide-charset UCS-2
    
    # Set UTF-8 Charset
    set target-charset UTF-8
    set host-charset UTF-8
    set charset UTF-8
    set target-wide-charset UTF-8
    
    # Request async target mode
    set target-async 1
    
    # Do not complain about pending breakpoints
    set breakpoint pending on
    
    # Enable All-Stop for all Threads
    set non-stop off
    
    # Do not ask for confirmations
    set confirm off
    
    # Do not create new console for output/logging
    set new-console off
    
    # Turn-off paggination to allow integration with IDE
    set pagination off
    
    # Call Stack files (and anywhere else) should be absolute path
    set filename-display absolute
    
    # Enable Pretty Print in GDB Panel
    set print pretty on
    
    # Enable notification of completion for asynchronous execution commands.
    set exec-done-display on
    
    # Show Addresses in objects, required for integration with IDE
    set print address on
    
    # Enable Pretty Print for Arrays
    set print array on
    
    # Flatten objects, required for integration with IDE
    set print object off
    
    # Include static members, required for integration with IDE
    set print static-members on
    
    # Show demangled vtable, required for integration with IDE
    set print vtbl off
    set print demangle on
    set demangle-style gnu-v3
    
    # Print full eight-bit characters, required for integration with IDE
    set print sevenbit-strings off
    
    # Set path for obj files
    path $(TARGET_ROOT)/obj
    
    # Load gdb scripts for STL (string, vector, map, etc.)
    source $(PATH_SDK_DEBUGGER)/stl-views-1.0.3.gdb
    
    # List of source code files
    dir $(PATH_SDK_COMMON)
    dir $(PATH_SDK_FRAMEWORKS)
    dir $(PATH_SDK_INCLUDES)
    dir $(PROJECT_PATHS.NATIVE_COMMON)
    
    # Load the binary
    file $(TARGET_OUTPUT)
    
    0 讨论(0)
提交回复
热议问题