27

I'm pretty much using GDB for the first time. I run

$ gdb

then I'm running

attach <mypid>

then I see that my process is stuck (which is probably ok). Now I want it to continue running, so I run

continue

and my process continues running but from here I'm stuck if I want again to watch my current stack trace etc. I couldn't get out of continuing... I tried Ctrl-D etc. but nothing worked for me... (was just a guess).

Melebius
  • 6,183
  • 4
  • 39
  • 52
Jas
  • 14,493
  • 27
  • 97
  • 148

4 Answers4

27

You should interrupt the process that is attached by gdb. Do not interrupt gdb itself. Interrupt the process by either ctrl-c in the terminal in which the process was started or send the process the SIGINT by kill -2 procid. With procid the id of the process being attached.

Lopje
  • 294
  • 3
  • 3
  • 2
    Shame there isn't a way to do this from the host when using gdbserver/client. – barkside Mar 04 '16 at 15:54
  • 1
    Not wanting to state the obvious, but it'll only work if the program doesn't handle SIGINT. If there's a sigaddset frenzy in main() or some init()... ;) – vesperto Dec 21 '16 at 17:42
  • This is hardly the best answer. You should not assume that the problem doesn't handle interruptions. The best answer should state how to gain interactivity with gdb (within gdb): "Control+C in the gdb process should bring you back to the command prompt." – corporateAbaper Jun 28 '19 at 16:09
9

Control+C in the gdb process should bring you back to the command prompt.

Tyler McHenry
  • 74,820
  • 18
  • 121
  • 166
  • ctrl-c does not work for me have a look please: ' bash-3.00# gdb GNU gdb 6.8 Copyright (C) 2008 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "i386-pc-solaris2.10". (gdb) attach 4305 Attaching to process 4305 Retry #1: Retry #2: ' – Jas Aug 14 '10 at 13:09
  • Retry #3: Retry #4: [New LWP 1] 0xfef298b5 in ?? () (gdb) info frame Stack level 0, frame at 0x80477c8: eip = 0xfef298b5; saved eip 0xfef1b5d2 Arglist at 0x80477c0, args: Locals at 0x80477c0, Previous frame's sp is 0x80477c8 Saved registers: eip at 0x80477c4 (gdb) print The history is empty. (gdb) continue Continuing. ^D ^C quit ^C^C^C ^C – Jas Aug 14 '10 at 13:10
  • how can i format this plz? i had this in separate lines how do i have it also in comments what i pasted in separate lines please? – Jas Aug 14 '10 at 13:15
  • You can't format (much) in comments. Edit it into your question. – Tyler McHenry Aug 14 '10 at 15:40
3

Here's a short GDB tutorial, and here's a full GDB manual.

The point of debugging is to inspect interesting/suspicious parts of the program. Breakpoints allow you to stop execution at some source location, and watchpoints allow you to stop when interesting data changes.

Simple examples:

(gdb) break my_function
(gdb) cont

This will insert a breakpoint at the beginning of my_function, so when execution of the program enters the function the program will be suspended and you get GDB prompt back, and be able to inspect program's state. Or you can step through the code.

(gdb) watch my_var
(gdb) cont

Same, but now the program will be stopped at whatever location that modifies the value of my_var.

Shameless plug - here's a link to my GDB presentation at NYC BSD User Group. Hope this helps.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • but i just wanted to pick a point in time of my own and at that point to examine the stack trace... and afterwards to continue the program, i didn't want breakpoints... in my case I want to examine the stack trace of the program at a time of my own. – Jas Aug 16 '10 at 06:48
  • OK, what does `info signal` says about `SIGINT`? – Nikolai Fetissov Aug 16 '10 at 17:34
  • @Jas what possible use is it to randomly interrupt a running program to examine the stack trace? All you're going to find is that the code that is currently on the stack is the code that is running. Usually this is a loop waiting for user input. Ho hum. – theMayer Feb 16 '18 at 16:44
0

interrupt

gdb> help interrupt
Interrupt the execution of the debugged program.
If non-stop mode is enabled, interrupt only the current thread,
otherwise all the threads in the program are stopped.  To 
interrupt all running threads in non-stop mode, use the -a option.

interrupt cmd also send SIGINT to debugged process.

gdb> info thread
Cannot execute this command while the target is running.
Use the "interrupt" command to stop the target
and then try again.

gdb> interrupt
[New Thread 27138.27266]
[New Thread 27138.27267]
[New Thread 27138.27268]
[New Thread 27138.27269]
[New Thread 27138.27270]

Thread 1 "loader" received signal SIGINT, Interrupt.
0x0000007fb7c02e90 in nanosleep () from target:/system/lib64/libc.so
yurenchen
  • 1,897
  • 19
  • 17