30
votes

I am debugging a program using gdb. First I load my executable, then I continue to run the program. I sometimes want to interrupt execution of my program, so I do Ctrl + C.

My problem is that this closes both my program and gdb. How can I exit my program without exiting gdb?

5
That isn't supposed to happen; Ctrl-C is just supposed to interrupt (not terminate) your program, and it's definitely not supposed to terminate gdb. What environment are you using? - trojanfoe
I'm using Cygwin. Weird. - Randomblue

5 Answers

31
votes

Have you tried to use kill from inside gdb?

17
votes

Use ctrl-c to interrupt the application. Then run "signal SIGINT" from the GDB prompt, which will send this signal to your application, causing it to do the same things it would normally have done when you do ctrl-c from the command line.

7
votes

Looks like under Windows, you have to use Ctrl-Break not Ctrl-C. See this page.

Excerpt:

MS-Windows programs that call SetConsoleMode to switch off the special meaning of the `Ctrl-C' keystroke cannot be interrupted by typing C-c. For this reason, gdb on MS-Windows supports C- as an alternative interrupt key sequence, which can be used to interrupt the debuggee even if it ignores C-c.

3
votes

First run the program (not from inside gdb), then find its pid.

In another shell, run gdb --pid=<your program's pid>. This attaches gdb to a running program. It stops the program's execution, so issue c to continue.

Now quit your program, your gdb session will stay there.

3
votes

Like Manlio said, what you want in this case is kill, which will prompt you with a message that reads:

Kill the program being debugged? (y or n)

If you type help running you will see all the important commands you will need to know to do common things, such as stopping the execution of a current program. These should be all commands for the program you are running and not gdb.

shell