GDB:Command
lastmod 2024-10-23created 2021-11-0513 edits
A list of GDB commands.
Basic
Basic GDB usage is as follows.
- prog
- The program to debug.
- core
- A file created when a program terminates abnormally during execution due to an error such as a 'segmentation fault'; it stores the internal state of the system at that moment as is. If you pass this file as an argument, GDB shows which part of the source code the executable terminated abnormally at.
- procID
- To debug a program that is already running, pass the id of that process (PID). Note that if a file with the same name as the PID exists, gdb treats it as a core file.
Using with GCC
Compile with the -g option in GCC. Then pass the executable as the first argument to gdb.
Abbreviations
To use a short command in place of a long one, use the following.
| break | b |
| clear | cl |
| delete | d |
| run | r |
| kill | k |
| step | s |
| next | n |
| continue | c |
| until | u |
List of GDB command
Lists and describes GDB commands.
-
break [file:]function: Sets a breakpoint on function (in file).-
b file.cpp:linenumber: Sets a breakpoint at linenumber in file.cpp.
-
r, run [arglist]: Starts the program (with arglist, if specified). bt: backtrace: shows the program stack. print expr: Shows the value of an expression. c: Continues running the program (stopped at a breakpoint, etc.). next: Executes the next line (after stopping); skips over functions called on that line without stepping into them. step: Executes the next line (after stopping); steps into functions called on that line. until: If you get stuck in a loop while using the step command, use the until command. help [name]: Shows information about the GDB command name, or general information about using GDB. q, quit, Ctrl+d: Exits GDB. l, list: Prints source code.-
list 10 -
list [함수명] -
list -(prints the previous 10 lines) -
list [파일명]:[함수명] -
list [파일명]:10
set listsize 20: Changes the number of lines list prints. finish: When a function is very long, use the finish command to move to the point where it ends. return: To exit a function without executing its remaining part, use the return command.-
return 1234: Ends the function with an arbitrarily specified return value.
watch: Watchpoints are handy for checking changes in variable values and in code.-
watch [변수명]: Breaks when a value is written to the variable -
rwatch [변수명]: Breaks when the variable's value is read -
awatch [변수명]: Breaks on both reads and writes of the variable
info sources: Prints the list of all sources. cd [Directory]: Change Directory. Changes the working directory. file [Program]: Selects the file to debug. set args [Arguments]: Sets command-line arguments. show args: Shows the list of currently set command-line arguments. d: Deletes all breakpoints. info breakpoints: Shows breakpoints. cl [LINE | FUNC]: Deletes a breakpoint. k: Ends program execution. s: Executes the current line and stops; steps into functions on a call. n: Executes the current line and stops; on a function call, runs the function and proceeds to the next line. c: Continues until a breakpoint is hit. u: Exits the current loop. si: Executes the current instruction; steps into functions on a call. ni: Executes the current instruction; does not step into functions on a call. Using breakpoints
- watch [EXP]
- Sets a watchpoint for expression EXP. Stops program execution whenever the value of EXP changes.
- break [[file:]n|function]
- Sets a breakpoint at a specific line (n) or function (function). With no argument, sets a breakpoint at the current execution position. When the running program reaches the specified breakpoint, execution stops.
- clear [[file:]n|function]
- Removes the breakpoint set at a specific line (n) or function (function). With no argument, removes all breakpoints set at the current execution position.
- delete [N …]
- Removes some breakpoints or auto-display expressions. N is the number of the breakpoint to remove. With no argument, removes all breakpoints.
- disable [N …]
- Disables some breakpoints. N is the breakpoint to disable. With no argument, disables all breakpoints. A disabled breakpoint can be made to work again with enable.
- enable [N …]
- Enables some breakpoints. N is the breakpoint to enable.
- condition [N COND]
- Specifies that breakpoint N stops only when COND is true.
Examining data
GDB usage for examining data is as follows.
- whatis EXP
- Prints the data type of expression EXP.
- ptype TYPE
- Prints the definition of type TYPE.
- print [EXP]
- Prints the value of expression EXP. Variables usable in EXP are those in the selected frame or global variables.
- printf ["format string", arg1, arg2, arg3, ..., argn]
- Prints in printf format.
- display [EXP]
- Prints the value of expression EXP every time the program stops.
- delete display (or undisplay) [N …]
- Cancels some of the expressions printed each time the program stops. N specifies the number of the expression to cancel.
- With no argument, cancels all auto-display expressions.
- disable display [N …]
- Disables some of the expressions printed each time the program stops. N specifies the number of the expression to disable.
- With no argument, disables all auto-display expressions.
- enable display [N …]
- Enables some of the expressions printed each time the program stops. N specifies the number of the expression to enable.
- With no argument, enables all auto-display expressions.
Examining files
GDB usage for examining files is as follows.
- cd [DIR]
- Sets the working directory to DIR
- pwd
- Prints the working directory.
- core-file [FILE]
- Uses FILE as the core dump file.
- file [FILE]
- Uses FILE as the program to debug.
- list [[file:]n|function][, [file:]n|function]
- Prints 10 lines around the specified function or line. Two arguments separated by a comma indicate the start and end lines to print.
- With no argument, prints the 10 lines after the previously printed lines. list – prints the 10 lines before the previously printed lines.
- set listsize [n]
- Sets the number of lines shown by list to n (default 10 lines).
Running programs
GDB usage for running programs is as follows.
- kill
- Forcibly terminates the program being debugged.
- run [args]
- Starts running the program to debug. You can specify the arguments (args) the program needs.
- If args is not specified, the arguments given by the previous run or by the 'set args' command are used. I/O redirection (>, <, >>) can be used.
- set args [args]
- Sets the arguments passed to the program being debugged when it starts.
- next [n]
- In a stopped program, executes the next n (default=1) statements and stops. For a function, runs the whole function.
- step [n]
- In a stopped program, executes the next n (default=1) statements and stops. For a function, steps into it and executes one statement at a time.
- continue [n]
- Continues a program stopped by a signal (Ctrl-C), a breakpoint, etc. If n is specified, ignores the next n-1 breakpoints and stops at the nth breakpoint.
- until [[file:]n|function]
- Runs until the program reaches a line greater than the current line, or the specified line, address, or function.
- (Same arguments as the break command.) It also stops when leaving the current stack frame. When going through a loop, the first iteration behaves like the next command, but from the second iteration it does not stop until it leaves the loop.
Stack
The stack consists of stack frames. gdb numbers stack frames starting from 0 for the innermost (currently executing) frame. gdb always treats one frame as the selected frame, and variable values are viewed for the selected frame. In the commands below, you can also specify another frame by number or address.
- bt [[-]N]
- Prints a backtrace of all stack frames or the innermost N frames. When the argument is negative (-N), prints the outermost N frames.
- (You may also use backtrace or where instead of bt.)
- frame [N]
- Selects and prints a stack frame. N is the frame number (or frame address) to select. With no argument, prints the currently selected frame.
- up [N]
- Selects and prints the stack frame (caller) that called the currently selected frame. N says how many frames to move up.
- down [N]
- Selects and prints the stack frame (callee) called by the currently selected frame. N says how many frames to move down.
- return [rvalue]
- Makes the selected stack frame return to its caller. Execution continues in the frame above the currently selected one (the caller). rvalue specifies the return value.
- set variable [variable] = [exp]
- Replaces the value of [variable] with the result of [exp]. You can write just set instead of set variable.
Status queries
GDB usage for status queries is as follows.
- info
- A general-purpose command for showing various things about the program being debugged.
- info breakpoints [N]
- Shows the status of set breakpoints. N is a specific breakpoint number.
- With no argument, shows the status of all breakpoints.
- (You may also use info watchpoints.)
- info args
- Shows the argument variables of the current stack frame.
- info display
- Shows the expressions to print when the program stops.
Memory watch mode
What you're looking for is called a watchpoint.
- watch: gdb will break when a write occurs
- rwatch: gdb will break wnen a read occurs
- awatch: gdb will break in both cases
Usage:
-
watch foo: watch the value of variable foo. -
watch *(int*)0x12345678: watch the value pointed by an address, casted to whatever type you want. -
watch a*b + c/d: watch an arbitrarily complex expression, valid in the program's native language.
Multi process debugging
-
set follow-fork-mode child - With this setting, after fork() the child process is put under debugging and the parent keeps running. The default value of
follow-fork-modeisparent.
-
show follow-fork-mode - Prints the current
follow-fork-modestate.
-
set detach-on-fork off - If you need to debug both the parent and child processes, you can use the following command to control both.
-
catch exec - If you want to stop execution when a specific event such as fork/vfork/exec occurs, you can set a catchpoint.
GDB support tools
- help [class|command]
- Help: prints the list of command classes, shows the list of commands in the given class, or shows help for the given command.
- quit
- Exits gdb.
- make [args]
- Runs make.