Monday, March 05, 2012

GDB Basics

Not gonna try to do a GDB tutorial, but rather just post what I learned in trying to debug a C program that I had to modify. Some simple stuff:

break - set breakpoint at the start of that method
bt - print stack trace at current point
p - print (contents of) variable
l(ist) [nn] - print code around current point, or around line nn
run - start running program
c - continue (running freely)
n - next, step over
s - step into
quit - go home, all done

That'll get you pretty far, but the code I was modifying was using fork() to daemonize itself, and gdb was quitting as the initial process quit. So I used:

set follow-fork-mode

to get it to debug the child. Except, the child was forking as well, and I went the wrong way into that child. So I did the following:

1: set follow-fork-mode child
2: break forkChildProcesses
3: run
4: set follow-fork-mode parent
5: break 520
6: c

(1) set the follow mode, (2) set a breakpoint in the daemon, (3) ran to that breakpoint, (4) set the follow mode back, so I would stay in the daemon and not go into the daemon's children, (5) set a new breakpoint at line 520, where I thought the problem started, and (6) continued execution.

And yeah, run starts the program running, while c continues it. Got confused the first time I tried to run a program I'd already started, and it tried to confirm that I wanted to restart. Oops.

0 Comments:

Post a Comment

<< Home