GDB: Launching
Launching GDB |
|
Start GDB ready to launch and debug programfile |
gdb --args program arg1 arg2
|
Start GDB as above but supplying command line arguments to the target process. |
|
Attach GDB to a running target process. |
Selecting the Start of Debugging |
|
Run the debuggee and break at main() (if it exists). |
|
Attach GDB to a running target process. |
(gdb) attach --waitfor process-name
|
(Mac OS X only) Wait for a process to launch and immediately attach to it. |
Adding a shim |
gdb$ set exec-wrapper env 'LD_PRELOAD=libfoo.so'
|
The dynamic library file libfoo.so will be loaded into the address space of the debuggee. |
Logging |
gdb$ set logging file filename
|
The default logfile is gdb.txt but you can use this to change it. |
gdb$ set logging overwrite off
|
The default is on, which overwrites the existing log file. |
|
Turns on logging. |
|
With logging on, this will add a comment to the logfile. |
GDB: Execution
Displaying the Call Stack |
|
Show the list of stack frames (BackTrace). |
|
Show the list of stack frames with the local variables of each. |
|
Show saved stack pointer, call address, etc. for the selected stack frame. |
|
Select stack frame number number (and crashed GDB 6.3.50 on OS X). |
Controlling Execution |
|
Step-into (one or count instruction forward). |
|
Step-over (one or count instruction, stepping over function calls). |
|
Immediately return from the current function, optionally setting the return value. |
|
Stop after finishing execution of the current function. |
|
Any time GDB is stopped, this will continue normal execution. |
GDB: Environment
|
Display the debuggee's current environment variables. |
gdb$ set env varname=value
|
Set an environment variable. |
|
Delete an environment variable. |
|
Display the command-line arguments of the debuggee process. |
|
Set the command-line arguments to the debuggee process. |
|
Run shell commands (useful commands may include "ps -e", etc.) |
|
These two commands can can show or change the working directory of GDB (useful for logging, etc.). |
|
|
GDB: Breakpoints
Managing Breakpoints |
gdb$ set breakpoint pending on
|
Bypasses the warning about breakpoints in modules that aren't loaded yet. |
|
Sets a breakpoint at function if ("pending" off) or when ("pending on") a symbol by that name exists. |
|
Sets a breakpoint at address 0x00001234. |
gdb$ break 0x00001234 if symbol==somevalue*
|
This is an example of the conditional breakpoint syntax. |
|
Stop when the syscall name is called. Omit name to stop on every syscall. Instead of name, you can also specify a syscall by number. |
|
(not in Mac OS X) Stop when the debuggee loads any dynamic library. Also: catch unload. |
|
List all breakpoints and watchpoints. |
gdb$ clear [breakpointid]
|
Deletes one or all existing breakpoints. Without this cheat sheet, the user would be forced to guess what is being cleared. |
gdb$ disable [breakpointid]
|
Disables one or all breakpoints. |
|
Managing Watchpoints (Data Breakpoints) |
gdb$ watch *0x12345678 [mask 0xffffff00]
|
Break on any change to the 24 most significant bits of a 32-bit value at address 0x12345678. |
|
Like watch, but also stops on any write or read accesses to the given address. |
|
Like watch, but only stops on read accesses. |
GDB: Concurrency
Multithreaded Debugging |
|
List the threads of the target process. |
|
Attach GDB to the thread threadID. |
|
Only the debugged thread is halted in GDB, the rest continue to run non-stop (unless they are blocking on the thread being debugged). |
gdb$ set scheduler-locking on
|
Only the debugged thread will run when the debuggee is resumed. |
gdb$ set scheduler-locking step
|
Only the debugged thread will step when being step-debugged. |
gdb$ show scheduler-locking
|
Display the current setting value. |
|
Multiprocess Debugging |
gdb$ set follow-fork-mode child
|
GDB will detach at a fork() and attach to the new process. |
gdb$ set follow-fork-mode parent
|
(Default) GDB will not detach at a fork(). |
gdb$ show follow-fork-mode
|
Display the current setting value. |
gdb$ set follow-exec-mode new
|
GDB will detach at an exec() and attach to the new process. |
gdb$ set follow-exec-mode same
|
(Default) GDB will not detach at an exec(). |
gdb$ show follow-exec-mode
|
Display the current setting value. |
gdb$ set detach-on-fork off
|
GDB will not detach at a fork() and will also attach to the child process (both will be debugged). |
gdb$ show detach-on-fork
|
Display the current setting value. |
|
List all processes under GDB's control. (On Mac OS X: info files
) |
|
|
GDB: Memory
Memory Images |
|
Debug program using a memory dump file, imagefile. |
|
(not in Mac OS X) Dump the debuggee process memory to disk. |
Reading Disassembly and Memory |
gdb$ set disassembly-flavor intel
|
Use the modern syntax for x86-64 assembly. This is not the default. |
gdb$ set disassemble-next-line on
|
Disassemble the next instruction every time GDB stops. You want to turn this on. |
|
Disassemble (eXamine) the first 4 instructions at address 0x00001234. |
|
Disassemble the first 32 instructions starting at the current instruction ($RIP on x86-64). |
|
Same command, but attempting to disassemble both forward and backward from the current instruction. |
gdb$ info address symbolname
|
Display the address in memory of a given symbol, specified by name. |
gdb$ info symbol 0x00001234
|
Displays the symbol name (if any), executable segment, and executable module associated with the given address. |
|
Display one null-terminated string at address 0x00001234. |
|
Display 8 heXadecimal Bytes of memory starting at address 0x00001234. |
|
Display the value of the regular CPU registers. |
|
Display the value of all CPU registers including floating-point and vector registers. Does not include special Machine Specific Registers (MSRs). |
gdb$ find start_address, distance, value [, another_value, ...]
|
(not in Mac OS X) Search memory for a value, given a starting point and a search distance/offset. |
|
Display info about all of the executable modules of the debuggee (name, load address, file path, etc.). |
|
Display all of the function symbols available and their associated addresses. |
|
Display all of the variable symbols available and their associated addresses. |
GDB: Advanced
Anti-Anti Debugging |
gdb$ handle signal [keywords...]
|
(Untested) might bypass exception-based anti-debugging |
gdb$ catch syscall ptrace
|
(Untested) Use this breakpoint to return 0 (set $rax = 0; continue), should bypass ptrace() checking by the debuggee. |
|
Created By
Metadata
Favourited By
Comments
victor 12:19 17 Feb 15
This is really great and pretty complete. Thanks for sharing! I am printing this and putting on my desk
Add a Comment
Related Cheat Sheets