GDB Command Summary

1. Start

  1. gdb -q: Suppress unnecessary output.
  2. file a.out: Specify the debug file.
  3. gdb –args a.out a.txt: Specify arguments.
  4. set args a.txt: Specify arguments after starting gdb.
  5. run a.txt: Run with specified arguments.
  6. start a.txt: Start with specified arguments (break at main).
  7. cd $dir; path $dir: Specify environment variables.
  8. run > a.txt: Redirect output to a.txt.

2. Regular Breakpoints

  1. break $location: Location can be:
  • linenum
  • filename : linenum
  • +offset/-offset
  • function
  • filename : function
  • break $location if cond: Conditional breakpoint.
  • tbreak: Temporary breakpoint, invalid after stopping.
  • rbreak: Regular expression matching, e.g., rbreak rb_*
  • info breakpoint [n]: n is optional, for a specific breakpoint number.
  • info watchpoint [n]: n is optional, for a specific watchpoint number.
  • clear $location: Delete breakpoint at specified location. Location can be a line number or a function name. If it’s a function name, delete all breakpoints at the function entry.
  • delete [breakpoints] [num]: Delete all or specified numbered breakpoints.
  • disable [breakpoints] [num]: If num is not specified, disable all breakpoints.
  • enable [breakpoints] [num…]: Activate multiple breakpoints specified by num…, if num… is not set, activate all disabled breakpoints.
  • enable [breakpoints] once num: Temporarily activate multiple breakpoints by num…, but can only use them once, after which they return to disabled state.
  • enable [breakpoints] count num…: Temporarily activate multiple breakpoints by num…, which can be used count times, after which they enter disabled state.
  • enable [breakpoints] delete num…: Activate multiple breakpoints by num…, but can only use them once, after which they will be permanently deleted.
  • 3. Watchpoints

    1. watch cond: Monitor variable changes. The watch command means: the program will only stop when the monitored variable (expression) changes.

    • If the monitored variable (expression) is a local variable (expression), once the local variable (expression) goes out of scope, the monitoring operation will also go out of scope;
    • If monitoring a pointer variable (e.g., *p), then watch *p and watch p are different; the former monitors changes to the data pointed to by p, while the latter monitors whether the pointer p itself has changed;
    • These 3 monitoring commands can also be used to monitor changes to array element values; for example, for the array a[10], watch a means that as long as the data stored in the array a changes, the program will stop execution.
  • rwatch: Hardware watchpoint. The program will stop as soon as there is a read operation on the target variable (expression).

  • awatch: Hardware watchpoint. The program will stop as soon as there is a read or write operation on the target variable (expression).

  • info watchpoints:

  • set can-use-hw-watchpoints 0: Force the use of software watchpoints only.

  • 4. Catch Breakpoints

    1. catch $event: Monitor the occurrence of a certain event.
    • throw [exception]: The program throws an exception.
    • catch [exception]: The program catches an exception.
    • load/unload [regexp]: Load/unload dynamic libraries.
  • up: Return to the source code where the event was triggered.
  • tcatch: Temporary.
  • 5. Conditional Breakpoints

    1. break … if cond:
    2. watch expr if cond:
    3. condition $bnum expression: Add or modify the condition expression for the breakpoint with number bnum.
    • bnum: The number of the target breakpoint.
    • expression: The condition expression to be added or modified for the breakpoint. Conditional catch breakpoints cannot be generated directly; they can be transformed from a regular catch breakpoint by using info break to get the breakpoint number, then using condition to turn it into a catch breakpoint.
  • condition $bnum: Delete the condition expression of the breakpoint with number bnum, turning it into a regular unconditional breakpoint.
  • ignore $bnum count: Temporarily disable the target breakpoint, which will recover after count times.
  • 6. Step Debugging

    1. next $count: Step through count lines of code (do not enter functions).
    2. step $count: Step through count lines of code (enter functions).
    3. until: The until command without parameters can quickly run through the current loop body and stop outside the loop body. Note that the until command does not always perform this function; it will only do so when executed to the end of the loop body (the last line of code); otherwise, the until command functions the same as the next command, which steps through the program.
    4. until $location: Location is a line number. Stop after executing to the specified location.

    7. View Variable or Expression Values

    1. print num: num refers to the target variable or expression to view or modify.
    2. display expr: Like the print command, the display command is also used to view the value of a variable or expression during debugging; the difference is that when using the display command to view the value, GDB will automatically print it every time the program pauses (for example, during step execution), while the print command will not.
    3. display /fmt expr: There should be no space between the display command and /fmt. For example, with /x, it should be written as (gdb)display/x expr. /fmt:
    • /x prints integers in hexadecimal format.
    • /d prints integers in signed decimal format.
    • /u prints integers in unsigned decimal format.
    • /o prints integers in octal format.
    • /t prints integers in binary format.
    • /f prints the value of a variable or expression as a floating-point number.
    • /c prints the value of a variable or expression as a character.
  • info display:
  • (gdb) info displayAuto-display expressions now in effect:Num Enb Expression2:      y      /t result1:      y      num
    • num: The number of the variable or expression
    • enb: Whether enabled
    1. undisplay num/delete display num …: Delete from the auto-display list of variables or expressions.
    2. disable display num …: Disable variables or expressions in the list.
    3. enable display num …: Enable.

    8. Signals

    1. info signals: View the types of signals that GDB can handle.

    2. info signals $signal:

    3. handle $signal mode: Here, the signal parameter indicates the target signal to be set, which can usually be the full name of a signal (SIGINT) or its abbreviation (the part after ‘SIG’); if you want to specify all signals, you can use all.

      The mode parameter is used to clarify how GDB handles the target information, and its value can be one of the following:

    • nostop: When the signal occurs, GDB will not pause the program; it can continue executing, but will print a message indicating that the signal has occurred;
    • stop: When the signal occurs, GDB will pause program execution.
    • noprint: When the signal occurs, GDB will not print any messages;
    • print: When the signal occurs, GDB will print necessary messages;
    • nopass (or ignore): GDB captures the target signal but does not allow the program to handle it;
    • pass (or noignore): GDB allows the program to handle the target signal while capturing it.

    9. Stack Information

    1. frame $spec: This command can select the stack frame specified by the spec parameter as the current stack frame. The value of the spec parameter can be specified in three common ways:
    • By specifying the stack frame number. 0 corresponds to the current called function’s stack frame number, and the maximum numbered stack frame corresponds to the main() function;
    • By specifying the stack frame address. The stack frame address can be seen in the information printed by the info frame command.
    • By specifying the function’s name. Note that if it is a recursive function with multiple stack frames, this method specifies the one with the smallest number.
  • up n/down n:
  • info frame: This command will print the following information about the current stack frame:
    • The current stack frame number and the stack frame address;
    • The storage address of the function corresponding to the current stack frame, and the storage address of the code when the function was called;
    • The caller of the current function, and the corresponding stack frame address;
    • The programming language used for this stack frame;
    • The storage address and value of the function parameters;
    • The storage address of local variables in the function;
    • The register variables stored in the stack frame, such as the instruction register (represented as rip in a 64-bit environment, eip in a 32-bit environment), stack base pointer register (rbp in 64-bit, ebp in 32-bit), etc.
  • info args: View the values of the current function’s parameters.
  • info locals: View the values of local variables in the current function.
  • backtrace [-full] [n]: Where the parameters enclosed in [ ] are optional, their meanings are:
    • n: An integer value; when positive, it indicates printing the information of the innermost n stack frames; when negative, it indicates printing the information of the outermost n stack frames;
    • -full: Print local variable values along with stack frame information. This command is only used to print the information of all stack frames in the current thread.
  • thread apply all backtrace: Print the stack frame information of all threads.
  • 10. Search and Edit Source Code

    1. edit [location]: The default editor for GDB edit command is the ex compiler.
    2. edit [filename] : [location]:
    3. export EDITOR=/usr/bin/vim:
    4. search/reverse-search:

    11. Help

    1. help:
    2. help $command:
    3. $command :

    Leave a Comment