GDB调试入门教程
- 创业
- 2025-09-09 13:45:02

GDB 调试入门教程 1. `sample.cpp`1.1. Compile and Run 2. GDB 调试3. GDB commandsReferences
GDB is a command line debugger. It is a good choice on Linux or WSL. On macOS, use LLDB instead.
1. sample.cpp (base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l total 4 -rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 22:52 sample.cpp (base) yongqiang@yongqiang:~/workspace/yongqiang$ #include <iostream> #include <vector> double Sum(const std::vector<double> &data) { double total = 0; for (size_t i = 0; i < data.size(); ++i) { total += data[i]; } return total; } int main() { std::vector<double> data; data.push_back(10); data.push_back(20); data.push_back(30); std::cout << "Sum(data) = " << Sum(data) << std::endl; return 0; } 1.1. Compile and Run (base) yongqiang@yongqiang:~/workspace/yongqiang$ uname -a Linux yongqiang 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux (base) yongqiang@yongqiang:~/workspace/yongqiang$Compile your program with the -g flag and start GDB in text user interface (TUI) mode.
(base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l total 4 -rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 22:52 sample.cpp (base) yongqiang@yongqiang:~/workspace/yongqiang$ (base) yongqiang@yongqiang:~/workspace/yongqiang$ g++ -g --std=c++17 sample.cpp -o sample (base) yongqiang@yongqiang:~/workspace/yongqiang$ ls -l total 144 -rwxr-xr-x 1 yongqiang yongqiang 142488 Feb 16 22:58 sample -rwxr-xr-x 1 yongqiang yongqiang 365 Feb 16 22:52 sample.cpp (base) yongqiang@yongqiang:~/workspace/yongqiang$ (base) yongqiang@yongqiang:~/workspace/yongqiang$ ./sample Sum(data) = 60 (base) yongqiang@yongqiang:~/workspace/yongqiang$ 2. GDB 调试Run program with GDB: $ gdb sample
Set a breakpoint on main function: (gdb) break main or (gdb) b main
As breakpoing was set on main function, program will stop at main function and wait for gdb command.
Run program: (gdb) run or (gdb) r
Print value of total: (gdb) print total or (gdb) p total
(base) yongqiang@yongqiang:~/workspace/yongqiang$ gdb sample GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http:// .gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http:// .gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from sample... (gdb) break main Breakpoint 1 at 0x1314: file sample.cpp, line 13. (gdb) run Starting program: /home/yongqiang/workspace/yongqiang/sample Breakpoint 1, main () at sample.cpp:13 13 int main() { (gdb) list 8 } 9 10 return total; 11 } 12 13 int main() { 14 std::vector<double> data; 15 data.push_back(10); 16 data.push_back(20); 17 data.push_back(30); (gdb) next 14 std::vector<double> data; (gdb) next 15 data.push_back(10); (gdb) next 16 data.push_back(20); (gdb) next 17 data.push_back(30); (gdb) break Sum Breakpoint 2 at 0x5555555552a9: file sample.cpp, line 4. (gdb) next 19 std::cout << "Sum(data) = " << Sum(data) << std::endl; (gdb) next Breakpoint 2, Sum (data=std::vector of length -840921094004155616, capacity -196785920380801129 = {...}) at sample.cpp:4 4 double Sum(const std::vector<double> &data) { (gdb) next 5 double total = 0; (gdb) print total $1 = 6.9533558072559594e-310 (gdb) next 6 for (size_t i = 0; i < data.size(); ++i) { (gdb) next 7 total += data[i]; (gdb) next 6 for (size_t i = 0; i < data.size(); ++i) { (gdb) next 7 total += data[i]; (gdb) next 6 for (size_t i = 0; i < data.size(); ++i) { (gdb) next 7 total += data[i]; (gdb) next 6 for (size_t i = 0; i < data.size(); ++i) { (gdb) next 10 return total; (gdb) print total $2 = 60 (gdb) next 11 } (gdb) next Sum(data) = 60 main () at sample.cpp:21 21 return 0; (gdb) clear No breakpoint at this line. (gdb) quit A debugging session is active. Inferior 1 [process 26162] will be killed. Quit anyway? (y or n) y (base) yongqiang@yongqiang:~/workspace/yongqiang$ 3. GDB commands CommandDescriptionrun or rExecutes the program from start to endbreak or bSets a breakpoint on a particular linedisableDisables a breakpointenableEnables a disabled breakpointnext or nExecutes the next line of code without diving into functions; Step over functionsstepGoes to the next instruction, diving into the function; Step into functionslist or lDisplays the codeprint or pDisplays the value of a variablequit or qExits out of GDBclearClears all breakpointscontinue or cContinue execution until the next breakpoint References[1] Yongqiang Cheng, yongqiang.blog.csdn.net/ [2] GDB Tutorial, .gdbtutorial /