Back to Blog

GDB Debugging in Linux

#Linux#Assembly#DebuggingTools#File#Compiler

This article is written for programmer friends who primarily work on Windows operating systems but need to develop cross-platform software, as well as programming enthusiasts.

GDB is a powerful, command-line based program debugging tool released by the GNU open-source organization, running on UNIX/LINUX operating systems.

While GDB has many commands, we only need to master about ten of them to largely complete daily basic program debugging tasks.

| Command | Explanation | Example | |---|---|---| | file <filename> | Loads the executable program file to be debugged. Since GDB is usually executed in the same directory as the program being debugged, the filename does not need a path. | (gdb) file gdb-sample | | r | Short for Run, executes the program being debugged. If no breakpoints were set previously, the entire program will execute; if breakpoints exist, the program will pause at the first available breakpoint. | (gdb) r | | c | Short for Continue, resumes execution of the program being debugged until the next breakpoint or the end of the program. | (gdb) c | | b <line number> b <function name> b *<function name> b *<code address> d [number] | b: Short for Breakpoint, sets a breakpoint. Breakpoint locations can be specified using "line number", "function name", "execution address", etc. Adding a "*" symbol before a function name sets the breakpoint at the "prolog code generated by the compiler". If you are not familiar with assembly, you can ignore this usage. d: Short for Delete breakpoint, deletes a specific breakpoint by its number, or deletes all breakpoints. Breakpoint numbers increment starting from 1. | (gdb) b 8 (gdb) b main (gdb) b *main (gdb) b *0x804835c (gdb) d | | s, n | s: Executes one line of source code; if there is a function call in this line, it steps into that function. n: Executes one line of source code; function calls within this line are also executed (stepped over). s is equivalent to "Step Into" in other debuggers. n is equivalent to "Step Over" in other debuggers. These two commands can only be used if source code debugging information is available (compiled with the "-g" parameter in GCC). | (gdb) s (gdb) n | | si, ni | The si command is similar to s, and ni is similar to n. The difference is that these two commands (si/ni) operate on assembly instructions, while s/n operate on source code. | (gdb) si `(gdb) ni