Este contenido no está disponible en el idioma seleccionado.
4.3.2. Running GDB
This section will describe a basic execution of GDB, using the following simple program:
hello.c
The following procedure illustrates the debugging process in its most basic form.
Procedure 4.1. Debugging a 'Hello World' Program
- Compile hello.c into an executable with the debug flag set, as in:
gcc -g -o hello hello.cEnsure that the resulting binaryhellois in the same directory ashello.c. - Run
gdbon thehellobinary, that is,gdb hello. - After several introductory comments,
gdbwill display the default GDB prompt:(gdb)
(gdb)Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The variable
hellois global, so it can be seen even before themainprocedure starts:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that theprinttargetshello[0]and*hellorequire the evaluation of an expression, as does, for example,*(hello + 1):(gdb) p *(hello + 1) $4 = 101 'e'
(gdb) p *(hello + 1) $4 = 101 'e'Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Next, list the source:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Thelistreveals that thefprintfcall is on line 8. Apply a breakpoint on that line and resume the code:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Finally, use the
nextcommand to step past thefprintfcall, executing it:(gdb) n Hello, World! 9 return (0);
(gdb) n Hello, World! 9 return (0);Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The following sections describe more complex applications of GDB.