이 콘텐츠는 선택한 언어로 제공되지 않습니다.
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.c
Ensure that the resulting binaryhello
is in the same directory ashello.c
. - Run
gdb
on thehello
binary, that is,gdb hello
. - After several introductory comments,
gdb
will display the default GDB prompt:(gdb)
(gdb)
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The variable
hello
is global, so it can be seen even before themain
procedure starts:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note that theprint
targetshello[0]
and*hello
require 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 Thelist
reveals that thefprintf
call 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
next
command to step past thefprintf
call, 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.