GDB 调试入门教程
GDB 调试入门教程1. sample.cpp1.1. Compile and Run2. GDB 调试3. GDB commandsReferencesGDB is a command line debugger. It is a good choice on Linux or WSL.On macOS, use LLDB instead.1.sample.cpp(base)yongqiangyongqiang:~/workspace/yongqiang$ls-ltotal4-rwxr-xr-x1yongqiang yongqiang365Feb1622:52 sample.cpp(base)yongqiangyongqiang:~/workspace/yongqiang$#include iostream #include vector double Sum(const std::vectordouble data) { double total 0; for (size_t i 0; i data.size(); i) { total data[i]; } return total; } int main() { std::vectordouble 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)yongqiangyongqiang:~/workspace/yongqiang$uname-aLinux yongqiang5.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)yongqiangyongqiang:~/workspace/yongqiang$Compile your program with the-gflag and start GDB in text user interface (TUI) mode.(base)yongqiangyongqiang:~/workspace/yongqiang$ls-ltotal4-rwxr-xr-x1yongqiang yongqiang365Feb1622:52 sample.cpp(base)yongqiangyongqiang:~/workspace/yongqiang$(base)yongqiangyongqiang:~/workspace/yongqiang$ g-g--stdc17 sample.cpp-osample(base)yongqiangyongqiang:~/workspace/yongqiang$ls-ltotal144-rwxr-xr-x1yongqiang yongqiang142488Feb1622:58 sample -rwxr-xr-x1yongqiang yongqiang365Feb1622:52 sample.cpp(base)yongqiangyongqiang:~/workspace/yongqiang$(base)yongqiangyongqiang:~/workspace/yongqiang$ ./sample Sum(data)60(base)yongqiangyongqiang:~/workspace/yongqiang$2. GDB 调试Run program with GDB:$ gdb sampleSet a breakpoint onmainfunction:(gdb) break mainor(gdb) b mainAs breakpoing was set onmainfunction, program will stop atmainfunction and wait forgdbcommand.Set a breakpoint:(gdb) break line_number(gdb) break 5or(gdb) b line_number(gdb) b 5Run program:(gdb) runor(gdb) rPrint value oftotal:(gdb) print totalor(gdb) p totalDisplay the code:(gdb) listor(gdb) lView breakpoints:(gdb) info breakor(gdb) info bDisable breakpoints:(gdb) disable breakor(gdb) disable bRe-enable disabled breakpoints:(gdb) enable breakor(gdb) enable bChange variable values:(gdb) set total 99(base)yongqiangyongqiang:~/workspace/yongqiang$ gdb sample GNU gdb(Ubuntu9.2-0ubuntu1~20.04.2)9.2Copyright(C)2020Free Software Foundation, Inc. License GPLv3: GNU GPL version3or laterhttp://gnu.org/licenses/gpl.htmlThis isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Typeshow copyingandshow warrantyfordetails. This GDB was configured asx86_64-linux-gnu.Typeshow configurationforconfiguration details. For bug reporting instructions, please see:http://www.gnu.org/software/gdb/bugs/. Find the GDB manual and other documentation resources online at:http://www.gnu.org/software/gdb/documentation/. For help,typehelp.Typeapropos wordto searchforcommands related toword... Reading symbols from sample...(gdb)breakmain Breakpoint1at 0x1314:filesample.cpp, line13.(gdb)run Starting program: /home/yongqiang/workspace/yongqiang/sample Breakpoint1, main()at sample.cpp:1313intmain(){(gdb)list8}910returntotal;11}1213intmain(){14std::vectordoubledata;15data.push_back(10);16data.push_back(20);17data.push_back(30);(gdb)next14std::vectordoubledata;(gdb)next15data.push_back(10);(gdb)next16data.push_back(20);(gdb)next17data.push_back(30);(gdb)breakSum Breakpoint2at 0x5555555552a9:filesample.cpp, line4.(gdb)next19std::coutSum(data) Sum(data)std::endl;(gdb)next Breakpoint2, Sum(datastd::vector of length -840921094004155616, capacity-196785920380801129{...})at sample.cpp:44double Sum(const std::vectordoubledata){(gdb)next5double total0;(gdb)print total$16.9533558072559594e-310(gdb)next6for(size_t i0;idata.size();i){(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next10returntotal;(gdb)print total$260(gdb)next11}(gdb)next Sum(data)60main()at sample.cpp:2121return0;(gdb)clearNo breakpoint at this line.(gdb)quit A debugging session is active. Inferior1[process26162]will be killed. Quit anyway?(y or n)y(base)yongqiangyongqiang:~/workspace/yongqiang$(base)yongqiangyongqiang:~/workspace/yongqiang$ gdb sample GNU gdb(Ubuntu9.2-0ubuntu1~20.04.2)9.2Copyright(C)2020Free Software Foundation, Inc. License GPLv3: GNU GPL version3or laterhttp://gnu.org/licenses/gpl.htmlThis isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Typeshow copyingandshow warrantyfordetails. This GDB was configured asx86_64-linux-gnu.Typeshow configurationforconfiguration details. For bug reporting instructions, please see:http://www.gnu.org/software/gdb/bugs/. Find the GDB manual and other documentation resources online at:http://www.gnu.org/software/gdb/documentation/. For help,typehelp.Typeapropos wordto searchforcommands related toword... Reading symbols from sample...(gdb)breakSum Breakpoint1at 0x12a9:filesample.cpp, line4.(gdb)infobreakNum Type Disp Enb Address What1breakpoint keep y 0x00000000000012a9inSum(std::vectordouble, std::allocatordoubleconst)at sample.cpp:4(gdb)run Starting program: /home/yongqiang/workspace/yongqiang/sample Breakpoint1, Sum(datastd::vector of length -840921094004155616, capacity-196785920380801129{...})at sample.cpp:44double Sum(const std::vectordoubledata){(gdb)next5double total0;(gdb)next6for(size_t i0;idata.size();i){(gdb)print total$10(gdb)settotal100(gdb)print total$2100(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next7totaldata[i];(gdb)next6for(size_t i0;idata.size();i){(gdb)next10returntotal;(gdb)next11}(gdb)next Sum(data)160main()at sample.cpp:2121return0;(gdb)next14std::vectordoubledata;(gdb)next22}(gdb)next __libc_start_main(main0x555555555314main(),argc1,argv0x7fffffffd358,initoptimized out,finioptimized out,rtld_finioptimized out,stack_end0x7fffffffd348)at../csu/libc-start.c:342342../csu/libc-start.c: No suchfileor directory.(gdb)quit A debugging session is active. Inferior1[process128]will be killed. Quit anyway?(y or n)y(base)yongqiangyongqiang:~/workspace/yongqiang$3. GDB commandsCommandDescription(gdb) runor(gdb) rRun program(gdb) break line_number(gdb) break 5or(gdb) b line_number(gdb) b 5Set a breakpoint(gdb) disable breakor(gdb) disable bDisable breakpoints(gdb) enable breakor(gdb) enable bRe-enable disabled breakpoints(gdb) nextor(gdb) nExecutes the next line of code without diving into functions; Step over functions(gdb) stepGo to the next instruction; Step into functions(gdb) listor(gdb) lDisplay the code(gdb) print variable_nameor(gdb) p variable_nameDisplay the value of a variable(gdb) quitor(gdb) qExit out of GDB(gdb) clearClear all breakpoints(gdb) continueor(gdb) cContinue execution until the next breakpoint(base)yongqiangyongqiang:~/workspace/yongqiang$ gdb GNU gdb(Ubuntu9.2-0ubuntu1~20.04.2)9.2Copyright(C)2020Free Software Foundation, Inc. License GPLv3: GNU GPL version3or laterhttp://gnu.org/licenses/gpl.htmlThis isfreesoftware: you arefreeto change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Typeshow copyingandshow warrantyfordetails. This GDB was configured asx86_64-linux-gnu.Typeshow configurationforconfiguration details. For bug reporting instructions, please see:http://www.gnu.org/software/gdb/bugs/. Find the GDB manual and other documentation resources online at:http://www.gnu.org/software/gdb/documentation/. For help,typehelp.Typeapropos wordto searchforcommands related toword.(gdb)quit(base)yongqiangyongqiang:~/workspace/yongqiang$References[1] Yongqiang Cheng (程永强), https://yongqiang.blog.csdn.net/[2] GDB Tutorial, https://www.gdbtutorial.com/[3] GDB: The GNU Project Debugger, https://www.sourceware.org/gdb/

相关新闻