Common GDB Commands Overview

(Click the public account above to quickly follow)

English: Fsf, Translation: Linux China/robot527

linux.cn/article-8900-1.html

If you have good articles to submit, please click → here for details

Table of Contents

  • break — Set a breakpoint at the specified line or function, abbreviated as b

  • info breakpoints — Print a list of all non-deleted breakpoints, watchpoints, and catchpoints, abbreviated as i b

  • disable — Disable breakpoints, abbreviated as dis

  • enable — Enable breakpoints

  • clear — Clear breakpoints at the specified line or function

  • delete — Delete breakpoints, abbreviated as d

  • tbreak — Set a temporary breakpoint, parameters same as break, but will be automatically deleted after the program stops for the first time

  • watch — Set a watchpoint for an expression (or variable), which pauses program execution when the value of the expression (or variable) changes

  • step — Step through, if there is a function call, it will enter that function, abbreviated as s

  • reverse-step — Reverse step through, if there is a function call, it will enter that function

  • next — Step through, if there is a function call, it will not enter that function, abbreviated as n

  • reverse-next — Reverse step through, if there is a function call, it will not enter that function

  • return — Return the selected stack frame to its caller

  • finish — Execute until the selected stack frame returns, abbreviated as fin

  • until — Execute until reaching a certain line after the current line in the current stack frame (used to skip loops, recursive function calls), abbreviated as u

  • continue — Resume program execution, abbreviated as c

  • print — Print the value of expression EXP, abbreviated as p

  • x — View memory

  • display — Print the value of expression EXP each time the program stops (automatically displayed)

  • info display — Print the list of expressions set to display automatically earlier

  • disable display — Disable automatic display

  • enable display — Enable automatic display

  • undisplay — Remove automatic display items

  • help — Print a list of commands (look up help for commands with parameters), abbreviated as h

  • attach — Attach to a running process for debugging

  • run — Start the program to be debugged, abbreviated as r

  • backtrace — View information about the program call stack, abbreviated as bt

  • ptype — Print the definition of type TYPE

break

Use the break command (abbreviated b) to set a breakpoint.

Usage:

  • break without parameters sets a breakpoint at the next instruction executed in the selected stack frame.

  • break <function-name> sets a breakpoint at the entry of the function body; in C++, you can specify the function name using class::function or function(type, …).

  • break <line-number> sets a breakpoint at the beginning of the specified line in the current source file.

  • break -N break +N sets a breakpoint at the beginning of N lines before or after the current source line, where N is a positive integer.

  • break <filename:linenum> sets a breakpoint at line linenum in source file filename.

  • break <filename:function> sets a breakpoint at the entry of function function in source file filename.

  • break <address> sets a breakpoint at the address of the program instruction.

  • break … if <cond> sets a conditional breakpoint, … represents one of the above parameters (or no parameters), cond is the condition expression, and the program execution pauses only when cond is non-zero.

info breakpoints

View the list of breakpoints, watchpoints, and catchpoints.

Usage:

  • info breakpoints [list…]

  • info break [list…]

  • list… is used to specify several breakpoint numbers (can be omitted), which can be 2, 1-3, 2 5, etc.

disable

Disable some breakpoints. The parameters are space-separated breakpoint numbers. To disable all breakpoints, do not add parameters.

Disabled breakpoints are not forgotten but will not be effective until re-enabled.

Usage:

  • disable [breakpoints] [list…]

  • breakpoints is a subcommand of disable (optional), list… is as described in info breakpoints.

enable

Enable some breakpoints. Provide breakpoint numbers (separated by spaces) as parameters. If no parameters are given, all breakpoints are enabled.

Usage:

  • enable [breakpoints] [list…] enables specified breakpoints (or all defined breakpoints).

  • enable [breakpoints] once list… temporarily enables specified breakpoints. GDB will disable these breakpoints immediately after stopping your program.

  • enable [breakpoints] delete list… enables specified breakpoints once and then deletes them. Once your program stops, GDB will delete these breakpoints. Equivalent to break set with tbreak.

breakpoints as described in disable.

clear

Clear breakpoints at specified lines or functions. Parameters can be line numbers, function names, or * followed by an address.

Usage:

  • clear without parameters clears all breakpoints in the currently executing source line of the selected stack frame.

  • clear <function>, clear <filename:function> deletes any breakpoints set at the entry of the named function.

  • clear <linenum>, clear <filename:linenum> deletes any breakpoints set in the code at the specified line number in the specified file.

  • clear <address> clears breakpoints at the specified address of the program instruction.

delete

Delete some breakpoints or automatic display expressions. Parameters are space-separated breakpoint numbers. To delete all breakpoints, do not add parameters.

Usage: delete [breakpoints] [list…]

tbreak

Set a temporary breakpoint. Parameters are the same as break.

Except that the breakpoint is temporary, everything else is the same as break, so it will be deleted when hit.

watch

Set a watchpoint for an expression.

Usage: watch [-l|-location] <expr> every time the value of an expression changes, the watchpoint will pause program execution.

If -l or -location is given, it will evaluate expr and watch the memory it points to. For example, watch *(int *)0x12345678 will observe a 4-byte area at the specified address (assuming int occupies 4 bytes).

step

Step through the program until reaching a different source line.

Usage: step [N] parameter N indicates executing N times (or until the program stops for another reason).

Warning: If the step command is used when control is in a function compiled without debugging information, execution will continue until control reaches a function with debugging information. Similarly, it will not enter a function compiled without debugging information.

To execute a function without debugging information, use the stepi command, see below.

reverse-step

Reverse step through the program until reaching the beginning of another source line.

Usage: reverse-step [N] parameter N indicates executing N times (or until the program stops for another reason).

next

Step through the program, finishing the subroutine call.

Usage: next [N]

Unlike step, if the current source line calls a subroutine, this command will not enter the subroutine, but will treat it as a single source line and continue execution.

reverse-next

Reverse step through the program, finishing the subroutine call.

Usage: reverse-next [N]

If the source line to be executed calls a subroutine, this command will not enter the subroutine, treating the call as a single instruction.

Parameter N indicates executing N times (or until the program stops for another reason).

return

You can use the return command to cancel the execution of the function call. If you give an expression parameter, its value is used as the return value of the function.

Usage: return <expression> returns the value of expression as the return value of the function and makes the function return directly.

finish

Execute until the selected stack frame returns.

Usage: finish after returning, the returned value will be printed and placed in the value history.

until

Execute until the program reaches the current line after the current line in the current stack frame (same parameters as the break command). This command is used to skip through a loop multiple times to avoid stepping through it.

Usage: until <location> or u <location> continue running the program until reaching the specified location or the current stack frame returns.

continue

Continue running the debugged program after a signal or breakpoint.

Usage: continue [N] if starting from a breakpoint, you can use the number N as a parameter, which means setting the ignore count for that breakpoint to N – 1 (so that the breakpoint will not interrupt before the Nth reach). If non-stop mode is enabled (use show non-stop to view), only the current thread continues, otherwise all threads in the program will continue.

print

Evaluate and print the value of expression EXP. Accessible variables are the lexical environment of the selected stack frame, as well as all variables in the global or entire file scope.

Usage:

  • print [expr] or print /f [expr] expr is an expression (in the source code language).

By default, the value of expr is printed in a format suitable for its data type; you can choose a different format by specifying /f, where f is a letter specifying the format; see output format.

If expr is omitted, GDB will display the last value again.

To print a struct variable in an indented format with one member per line, use the command set print pretty on; to cancel, use the command set print pretty off.

The command show print can be used to view all print settings.

x

Check memory.

Usage: x/nfu <addr> or x <addr> n, f, and u are optional parameters used to specify how to display memory and how to format. addr is the expression of the address to start displaying memory.

n is the repetition count (default is 1), specifying how many units (specified by u) of memory values to display.

f display format (initial default is x), display format is one of the formats used by print(‘x’, ‘d’, ‘u’, ‘o’, ‘t’, ‘a’, ‘c’, ‘f’, ‘s’), with i (machine instructions).

u unit size, b for single-byte, h for double-byte, w for four-byte, g for eight-byte.

For example:

x/3uh 0x54320 means starting from address 0x54320 display 3 memory values in unsigned decimal integer format, double-byte units.

x/16xb 0x7f95b7d18870 means starting from address 0x7f95b7d18870 display 16 memory values in hexadecimal integer format, single-byte units.

display

Print the value of expression EXP each time the program pauses.

Usage: display <expr>, display/fmt <expr> or display/fmt <addr> fmt is used to specify the display format. Similar to the /f in the print command.

For formats i or s, or including unit size or unit count, add the expression addr as the memory address to check each time the program stops.

info display

Print the list of automatically displayed expressions, each with an item number, but does not show its value.

Includes disabled expressions and expressions that cannot be displayed immediately (currently unavailable automatic variables).

undisplay

Cancel the automatic display of certain expressions when the program pauses. The parameter is the number of the expression (use info display to query the number). Omitting parameters cancels all automatically displayed expressions.

delete display has the same effect as this command.

disable display

Disable the automatic display of certain expressions when the program pauses. Disabled display items will not be printed automatically but will not be forgotten. They may be enabled again later.

The parameter is the number of the expression (use info display to query the number). Omitting parameters disables all automatically displayed expressions.

enable display

Enable the automatic display of certain expressions when the program pauses.

The parameter is the number of the expression to re-display (use info display to query the number). Omitting parameters enables all automatically displayed expressions.

help

Print a list of commands.

You can use help without parameters (abbreviated as h) to display a brief list of command category names.

Using help <class> you can get a list of commands in that class. Using help <command> shows how to use that command.

attach

Attach to a process or file outside of GDB. This command can take a process ID or device file as a parameter.

For process ID, you must have permission to send signals to the process and must have a valid uid equal to that of the debugger.

Usage: attach <process-id> GDB will pause the process as the first thing after scheduling debugging of the specified process.

Whether the process attached by the attach command or the process started by the run command, you can use GDB commands to inspect and modify the attached process.

run

Start the program to be debugged.

You can directly specify parameters or use set args to set the parameters needed for startup.

For example: run arg1 arg2 … is equivalent to

set args arg1 arg2

run

Input and output redirection using >, < or >> is also allowed.

backtrace

Print overall stack frame information.

  • bt prints overall stack frame information, one stack frame per line.

  • bt n is similar but only prints the innermost n stack frames.

  • bt -n is similar but only prints the outermost n stack frames.

  • bt full n is similar to bt n but also prints the values of local variables.

where and info stack (abbreviated info s) are aliases for backtrace. Call stack information is similar to the following:

(gdb)where

#0 vconn_stream_run (vconn=0x99e5e38) at lib/vconn-stream.c:232

#1 0x080ed68a in vconn_run (vconn=0x99e5e38) at lib/vconn.c:276

#2 0x080dc6c8 in rconn_run (rc=0x99dbbe0) at lib/rconn.c:513

#3 0x08077b83 in ofconn_run (ofconn=0x99e8070, handle_openflow=0x805e274 <handle_openflow>) at ofproto/connmgr.c:1234

#4 0x08075f92 in connmgr_run (mgr=0x99dc878, handle_openflow=0x805e274 <handle_openflow>) at ofproto/connmgr.c:286

#5 0x08057d58 in ofproto_run (p=0x99d9ba0) at ofproto/ofproto.c:1159

#6 0x0804f96b in bridge_run () at vswitchd/bridge.c:2248

#7 0x08054168 in main (argc=4, argv=0xbf8333e4) at vswitchd/ovs-vswitchd.c:125

ptype

Print the definition of type TYPE.

Usage: ptype[/FLAGS] TYPE-NAME | EXPRESSION

Parameters can be type names defined by typedef, or struct STRUCT-TAG or class CLASS-NAME or union UNION-TAG or enum ENUM-TAG.

Look up that name based on the lexical context of the selected stack frame.

Similar commands are whatis, the difference being that whatis does not expand types defined by typedef, while ptype does expand them, as illustrated below:

/* Type declarations and variable definitions */

typedefdoublereal_t;

structcomplex{

real_t real;

doubleimag;

};

typedefstructcomplex complex_t;

complex_t var;

real_t *real_pointer_var;

These two commands yield the following output:

(gdb)whatis var

type = complex_t

(gdb)ptype var

type = structcomplex{

real_t real;

doubleimag;

}

(gdb)whatis complex_t

type = structcomplex

(gdb)whatis structcomplex

type = structcomplex

(gdb)ptype structcomplex

type = structcomplex{

real_t real;

doubleimag;

}

(gdb)whatis real_pointer_var

type = real_t *

(gdb)ptype real_pointer_var

type = double *

Did you gain something from this article? Please share it with more people.

Follow ‘Linux Enthusiasts’ to enhance your Linux skillsCommon GDB Commands Overview

Leave a Comment