GDB debugging introduction

This article introduces the common commands and shortcuts using GDB.
Suppose we have a program named hello.c and complied using gcc -g -o hello hello.c.

commands

Begin gdb session: gdb hello

Set breakpoint: break #lineNumber, eg break 10

Hint: Find line number by using vim hello.c, press escape and input :set nu

Run: run, run with stdin: run < inputfile.txt

Start debugging: start

Show local variables: info locals

Watch a local variable: watch variableName

Continue debugging: continue

Step in: step, executed the next line without stepping in: next

Display a variable at each step: display variableName

shortcuts

In gdb session, press ctrl+x and then press a to show the source code. Press ctrl+x and then press 2 to show the corresponding assembly code.

gdb source code

You could also install cgdb to have better experiences. Use cgdb hello and press escape to scroll the source code and press i to exe command

Press ctrl+x and then press 2 one more time to show the CPU register group.
gdb cpu registry
Use info all-registers to show all registers.
Use tui reg float to show float registry group.