Chapter 5. Debugging
56
Ensure that the resulting binary hello is in the same directory as hello.c.
2. Run gdb on the hello binary, i.e. gdb hello.
3. After several introductory comments, gdb will display the default GDB prompt:
(gdb)
4. Some things can be done even before execution is started. The variable hello is global, so it can
be seen even before the main procedure starts:
gdb) p hello
$1 = "Hello, World!"
(gdb) p hello[0]
$2 = 72 'H'
(gdb) p *hello
$3 = 72 'H'
(gdb)
Note that the print targets hello[0] and *hello require the evaluation of an expression, as
does, for example, *(hello + 1):
(gdb) p *(hello + 1)
$4 = 101 'e'
5. Next, list the source:
(gdb) l
1 #include <stdio.h>
2
3 char hello[] = { "Hello, World!" };
4
5 int
6 main()
7 {
8 fprintf (stdout, "%s\n", hello);
9 return (0);
10 }
The list reveals that the fprintf call is on line 8. Apply a breakpoint on that line and resume
the code:
(gdb) br 8
Breakpoint 1 at 0x80483ed: file hello.c, line 8.
(gdb) r
Starting program: /home/moller/tinkering/gdb-manual/hello
Breakpoint 1, main () at hello.c:8
8 fprintf (stdout, "%s\n", hello);
6. Finally, use the “next” command to step past the fprintf call, executing it:
Commentaires sur ces manuels