46

How do I stop a GDB execution without a breakpoint?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
anand
  • 617
  • 1
  • 5
  • 6

5 Answers5

36

Just use a regular interrupt Ctrl-c will work just fine. GDB just forwards the SIGINT to the debugging process which then dies. GDB will catch the non-standard exit and break the process there, so you can still examine all the threads, their stacks and current values of variables. This works fine, though you would be better off using break points. The only time I find myself doing this is, if I think I've gotten into some sort of infinite loop.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
luke
  • 14,518
  • 4
  • 46
  • 57
  • I tried ^C but it didn't work I am not able to type anything on the prompt.Also the program is multi threaded UI, could that be the issue ? – anand Jan 07 '09 at 19:25
  • 1
    Ctrl-C woks as I want, but I don't reproduce this on GDB 7.7.1: the process did not seem to get the Ctrl-C and die. It only got stopped by GDB, not killed. Hitting `c` again or `detach` makes it continue from where it stopped. – Ciro Santilli OurBigBook.com May 08 '15 at 10:45
  • 2
    `^c` doesn't work for me because my application catches it and dies gracefully. Any other suggestions? – lmat - Reinstate Monica Mar 18 '18 at 10:02
  • 1
    @LimitedAtonement it doesn't work if the application is calling `sigwait()` - cf. https://stackoverflow.com/a/6442197/427158 for a workaround. – maxschlepzig Aug 24 '19 at 07:06
14

GUI applications don't react to ^C and ^Break the way console applications do. Since these days most non-trivial projects tend to be GUI applications or libraries primarily used in GUI applications, you have two options:

  1. Send SIGSTOP to the application from a separate terminal. This is cumbersome.

  2. If you press ^C or ^Break on the GDB prompt, GDB will terminate but the application will remain running. You can then run GDB again to attach to it using the -p command-line switch. This loses debugger state.

In both cases, you might find this helpful: tasklist | grepProcessName| sed -e 's/ProcessName*\([0-9]*\).*/gdbModuleName-pid=\1/' > rungdb.sh You can modify this for use in shell scripts, makefiles or to send a signal instead of attaching GDB.

info threads will help you figure out which thread you want to look at. Then use threadThreadNumber to switch to it.

legoscia
  • 39,593
  • 22
  • 116
  • 167
8

Start a shell, find the process ID using ps and send it SIGSTOP or SIGINT by using the kill command (e.g. kill -INT pid).

zvrba
  • 24,186
  • 3
  • 55
  • 65
7

Just type BREAK without any arguments.

Break, when called without any arguments, break sets a breakpoint at the next instruction to be executed in the selected stack frame

Chris Allwein
  • 2,498
  • 1
  • 20
  • 24
  • 2
    In the context of gdb, running, stopping, stepping. This was exactly the kind of "stopping" I needed, killing the app was explained in thee gdb documentation. +1 – hobb Dec 11 '17 at 14:36
4

Ctrl + Z seems to work for me (but only in some cases - I'm not sure why).

kralyk
  • 4,249
  • 1
  • 32
  • 34
  • yeah, `Ctrl + C` **didn't** work for me too, so I used `Ctrl + Z` and it worked. – asn May 17 '20 at 15:43