|
|
Red Hat Enterprise Linux 9 Essentials Book now available.
Purchase a copy of Red Hat Enterprise Linux 9 (RHEL 9) Essentials Red Hat Enterprise Linux 9 Essentials Print and eBook (PDF) editions contain 34 chapters and 298 pages
|
This section will describe a basic execution of GDB, using the following simple program:
The following procedure illustrates the debugging process in its most basic form.
Procedure 5.1. Debugging a 'Hello World' Program
-
Compile hello.c into an executable with the debug flag set, as in:
gcc -g -o hello hello.c
Ensure that the resulting binary hello is in the same directory as hello.c .
-
Run gdb on the hello binary, i.e. gdb hello .
-
After several introductory comments, gdb will display the default GDB prompt:
(gdb)
-
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:
Note that the print targets hello[0] and *hello require the evaluation of an expression, as does, for example, *(hello + 1) :
-
Next, list the source:
The list reveals that the fprintf call is on line 8. Apply a breakpoint on that line and resume the code:
-
Finally, use the “next” command to step past the fprintf call, executing it:
The following sections describe more complex applications of GDB.
|
|