Skip to content

Instantly share code, notes, and snippets.

@aagontuk
Last active October 5, 2022 10:19
Show Gist options
  • Save aagontuk/26224a9b80bbd9d6b4dd692c772582df to your computer and use it in GitHub Desktop.
Save aagontuk/26224a9b80bbd9d6b4dd692c772582df to your computer and use it in GitHub Desktop.
GDB Cheatsheet
* Run the program
gdb$ r arg1 arg2 ... # Run program until it hits a breakpoint
* Environment variable
gdb$ set environment VAR=VALUE
gdb$ unset environment VAR
gdb$ show environmet # Show all environment variable
gdb$ show environment VAR # Show environment variable VAR
* Setting breakpoint
gdb$ b 3 # Set breakpoint in line 3 of the executable's source code
gdb$ b main # Set breakpoint at function main
gdb$ b *0x800415 # Set breakpoint at address 0x800415
gdb$ b foo.c:3 # Set breakpoint at line 3 in file foo.c
gdb$ c # Continue execution after a breakpoint is hit
gdb$ advance POINT # Continue execution until temporary breakpoint POINT is hit
* Source code
gdb$ list # To examine source
gdb$ list N # Display source from line N
gdb$ list func_name # Display source from func_name function
* Examine Variable
gdb$ p ADD/VAR/REG # Print content of address/variable/register in a human readable format
gdb$ disp VAR/ADD/REG # Display VAR/ADD/REG each time code is run
* Changing value of a variable
gdb$ set var x = 10 # Set value of x to 10
* Various info about the binary
gdb$ info target # exec section information
gdb$ maint info sections # Extra information about sections
gdb$ maint info sections .text .data # Print sections selectively
gdb$ maint info sections CODE # Print sections selectively
gdb$ info functions # Print function informations
gdb$ info variables # Print all global and static variable informations
gdb$ info registers # Print values of all the registers
* Stepping through code
gdb$ n # Execute next line but don't step into function
gdb$ s # Execute next line if its a function step into in and execute first line
gdb$ ni # Execute next instruction. If its a call instruction, step over.
gdb$ si # Execute next instruction. If its a call instruction, step into it.
gdb$ finish # Finish current function execution and go back to its caller
* Disassemble
gdb$ set disassembly-flavor intel # Change disassembled code to intel flavored assembly code
gdb$ disassemble FUNCTION_NAME # Disassemble a function named FUNCTION_NAME
gdb$ disassemble /s FUNCTION_NAME # Disassemble with source code attached
gdb$ disassemble /rs FUNCTION_NAME # With raw instructions like objdump -d
gdb$ disassemble /rs FILE::FUNCTION_NAME # Disassemble function in a specific file
* Examine Memory
gdb$ x/Nb ADDRESS/SYMBOL_NAME # Print N raw bytes from ADDRESS or symbol SYMBOL_NAME
gdb$ x/i $eip # Print current instructionj
* Shard library
gdb$ set exec-wrapper env 'LD_PRELOAD=./sharedlib.so' # Debug with LD_PRELOAD
gdb$ file /usr/bin/ls
gdb$ set stop-on-solib-events # Stop program on shared lib acitivity
* TUI Mode
gdb$ tui [enable | disable]
gdb$ layout [asm | src | regs | split]
gdb$ focus [asm | src | regs | cmd]
gdb$ info win
gdb$ winheight NAME_OF_WINDOW [+ | -]count # change window hight by count
gdb$ set trace-commands on
gdb$ set logging on
Resources:
https://web.archive.org/web/20200414104650/https://cs.brown.edu/courses/cs033/docs/guides/gdb.pdf
https://web.archive.org/web/20200304210826/http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html
https://www.youtube.com/watch?v=PorfLSr3DDI
https://web.archive.org/save/http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html # tui mode
https://www.techbeamers.com/how-to-use-gdb-top-debugging-tips/
http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html
http://truthbk.github.io/gdb-ld_preload-and-libc/
Code Browsing:
Cscope: https://courses.cs.washington.edu/courses/cse451/14wi/tutorials/tutorial_cscope.html
@aagontuk
Copy link
Author

aagontuk commented Oct 5, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment