Le débugger gdb

Pour utiliser gdb, le programme doit avoir été compilé avec l'option -g.
Pour lancer gdb, utiliser la commande gdb suivie du nom du programme ou utiliser la commande ESC-x gdb  sous emacs.
Le manuel complet est disponible en ligne. On peut aussi se procurer une carte de références refCard.ps.

Quelques commandes de base de gdb :
Voici un exemple de session gdb sous emacs, Le programme testé est fact (main.c, fact.h, fact.c, Makefile)

Current directory is ~/Enseignement/Licence/InitProg/2004/src/divers/3/
GNU gdb 5.3-20030128 (Apple version gdb-309) (Thu Dec  4 15:41:30 GMT 2003)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "powerpc-apple-darwin".
Reading symbols for shared libraries .. done
(gdb) run 6
Starting program: /Users/baudon/Enseignement/Licence/InitProg/2004/src/divers/3/fact 6
Reading symbols for shared libraries . done
720

Program exited normally.
(gdb) l
6 static void
7       message_usage(char *nom_de_la_commande)
8       {
9           fprintf(stderr, "Usage: %s n\n", nom_de_la_commande);
10      }
11     
12      int
13      main(int argc, char *argv[])
14      {
15        if (argc != 2)
(gdb) l main.c:12,22
12        int
13      main(int argc, char *argv[])
14      {
15        if (argc != 2)
16          {
17            message_usage(argv[0]);
18            return EXIT_FAILURE;
19          }
20        else
21          {
22            printf("%d\n", fact(atoi(argv[1])));
(gdb) 
23              return EXIT_SUCCESS;
24          }
25      }
(gdb) l fact
1 #include "fact.h"
2      
3       int
4       fact(n)
5       {
6         if (n == 0)
7           return 1;
8         else
9           return n * fact(n - 1);
10      }
(gdb) b main
Breakpoint 1 at 0x1d1c: file main.c, line 15.
(gdb) run
Starting program: /Users/baudon/Enseignement/Licence/InitProg/2004/src/divers/3/fact 6

Breakpoint 1, main (argc=2, argv=0xbffffc7c) at main.c:15
(gdb) p argc
$1 = 2
(gdb) p argv[1]
$2 = 0xbffffd77 "6"
(gdb) p atoi(argv[1])
Unable to call function at 0x9000c600: no return type information available.
To call this function anyway, you can cast the return type explicitly (e.g. 'print (float) fabs (3.0)')
(gdb) p (int) atoi(argv[1])
$3 = 6
(gdb) b fact
Breakpoint 2 at 0x1c58: file fact.c, line 6.
(gdb) cont
Continuing.

Breakpoint 2, fact (n=6) at fact.c:6
(gdb) display n
1: n = 6
(gdb) cont
Continuing.

Breakpoint 2, fact (n=5) at fact.c:6
1: n = 5
(gdb) 
Continuing.

Breakpoint 2, fact (n=4) at fact.c:6
1: n = 4
(gdb)
Continuing.

Breakpoint 2, fact (n=3) at fact.c:6
1: n = 3
(gdb)
Continuing.

Breakpoint 2, fact (n=2) at fact.c:6
1: n = 2
(gdb)
Continuing.

Breakpoint 2, fact (n=1) at fact.c:6
1: n = 1
(gdb)
Continuing.

Breakpoint 2, fact (n=0) at fact.c:6
1: n = 0
(gdb)
Continuing.
720

Program exited normally.
(gdb) del
Delete all breakpoints? (y or n) y
(gdb) b fact if n == 2
Breakpoint 3 at 0x1c58: file fact.c, line 6.
(gdb) run 6
Starting program: /Users/baudon/Enseignement/Licence/InitProg/2004/src/divers/3/fact 6

Breakpoint 3, fact (n=2) at fact.c:6
1: n = 2
(gdb) bt
#0  fact (n=2) at fact.c:6
#1  0x00001c80 in fact (n=3) at fact.c:9
#2  0x00001c80 in fact (n=4) at fact.c:9
#3  0x00001c80 in fact (n=5) at fact.c:9
#4  0x00001c80 in fact (n=6) at fact.c:9
#5  0x00001d5c in main (argc=2, argv=0xbffffc7c) at main.c:22
(gdb) up
#1  0x00001c80 in fact (n=3) at fact.c:9
(gdb) up
#2  0x00001c80 in fact (n=4) at fact.c:9
(gdb) p n
$4 = 4
(gdb) do
#1  0x00001c80 in fact (n=3) at fact.c:9
(gdb)
#0  fact (n=2) at fact.c:6
(gdb)
Bottom (i.e., innermost) frame selected; you cannot go down.
(gdb) cont
Continuing.
720

Program exited normally.
(gdb)