I had to refresh my memory over the last two week on how to use the debugging application called 'gdb'. I have needed to step through both Pound and Dovecot's delivery applications to help me nail down a couple of tough problems. Debugging Dovecot's delivery application is very simple, so let's walk through it really quickly.
First, understand how the application is normally executed; normally the MTA will pipe in the email content to the deliver process, but we can fake it by using simple re-direction.
>deliver -d kevin@boebel.us < email.txt
But we want to step through the code, so we need to run it through gdb.
>gdb deliver
Before we issue the 'run' command, we need to set a breakpoint, so let's say we are trying to debug this portion of code in deliver.c (lines 540-546)
env_tz = getenv("TZ");
home = getenv("HOME");
env_clean();
if (env_tz != NULL)
env_put(t_strconcat("TZ=", env_tz, NULL));
if (home != NULL)
env_put(t_strconcat("HOME=", home, NULL));
(gdb) break deliver.c:540
Breakpoint 1 at 0x8056e72: file deliver.c, line 540.
Ok, the breakpoint is set, let's run the program, giving it the correct command line arguments:
(gdb) run -d kevin@boebel.us < email.txt
Starting program: /var/home/kminnick/dovecot/dovecot-lda-0.1/src/deliver -d kevin@boebel.us < email.txt
Breakpoint 1, main (argc=3, argv=0xbfffb3b4) at deliver.c:540
540 env_tz = getenv("TZ");
Cool, the program is now getting ready to execute this line of code, let's run some commands to get the value and type of 'home' variable both before and after the line is executed.
First, we need to skip this line of code and get to the next one:
(gdb) n
541 home = getenv("HOME");
Now we can examine the variable:
(gdb) pt home
type = const char *
(gdb) p home
$4 = 0x3f1cd8 "ä[\023"
As you can see, 'pt' prints the type of the variable, and 'p' prints the actual value. The variable is currently pointing to some garbage in memory. Let's execute the line of code and find the value of the variable after it's execution:
(gdb) n
542 env_clean();
(gdb) p home
$5 = 0xbfffdf44 "/root"
This was a very simple example of how to use gdb to debug your applications, next time we will discuss a more complex example on how to debug a program that uses forks and threads. I'm sure you can't wait... :)
Wednesday, June 14, 2006
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment