This page looks best with JavaScript enabled

GDB debugging survival commands

 ·  🎃 kr0m

Every C programmer worth their salt will eventually have to debug their code either through prints or with a proper debugger. In this article, I will list the survival commands used in GDB.

The list is very brief and simple as it is a list of basic commands:

  • si: Step ASM

  • s: Step C entering functions

  • n: Step C without entering functions

  • print VAR_NAME: Displays the value of a variable

    • We can indicate the format: p/d will print the value in decimal, by default it does it in hexadecimal, we can see the supported formats in this link .
  • list: Displays the C source code

  • b N_LINE/FUNCTION_NAME: Sets a breakpoint

  • b *0x7c00: Debug memory position, in this case the BootLoader

  • info breakpoints: Lists the breakpoints

  • info functions: Lists of functions

  • watch VAR_NAME: Triggers a breakpoint when the value of the variable changes

  • info watchpoints: Lists the watchpoints

  • del N_BREAK: Deletes the indicated breakpoint

  • x/Nx MEM_ADDRESS: Displays the memory contents of the address, the s format is very useful when dealing with strings

  • print &VAR_NAME: Memory address of a variable

  • print $REGISTER: Displays the value of a register

  • print /x N: Displays the value of N in hexadecimal

  • info variables: Displays static or global variables

  • info locals: Displays the value of all existing variables in the stack of the current function

  • info frame: Displays information about the stack

  • info registers REGISTER: Displays the value of all registers or those we indicate as a parameter

  • bt: Displays the functions where the program flow has passed until reaching the current point

  • disassemble FUNCTION_NAME: View the source code of a function in ASM

Something very useful is to define custom commands, for example, if we set a breakpoint in a loop where a variable is modified, we can define the following command to monitor the variable in each iteration of the loop:

define next_and_print
n
print VAR_NAME
end

From here, we will have at our disposal a command called “next_and_print” that will execute the next code step and print the value of VAR_NAME.

I personally recommend installing a plugin like peda/gef since it will make data visualization easier: GDB-gef

If you liked the article, you can treat me to a RedBull here