常用命令(common commands)
- 编译待调试源文件(compile the source files for debug)
public@public:~$ gcc source.c -g -Wall -o binaryfile.out // for *.c files
- 开启调试模式(begin debug)
public@public:~$ gdb binaryfile.out
or
public@public:~$ gdb
public@public:~$ file binaryfile.out
- 打断点(breaking point)
(gdb) break 10 // breaking point in line 10
or
(gdb) break functionname // breaking point in the function
(gdb) break [where] if n==6 // breaking point in where when n equal to 6
- 查看断点信息(show breaking point)
(gdb) info breakpoints
you will see the info like this:
Num Type Disp Enb Address What
1 breakpoint keep n 0x080484c8 in func at test.c:6
2 breakpoint keep y 0x080484d1 in func at test.c:7
stop only if n==6
3 breakpoint keep y 0x080484c1 in func at test.c:5
- 取消断点(disable breaking point)
(gdb) disable breakingpointnumber
like this:
(gdb) disable 1 // disable the breaking point is 1
- 恢复断点(enable breaking point)
(gdb) enable breakingpointnumber
like this:
(gdb) enable 1 // enable the breaking point is 1
- 删除断点(delete the breaking point)
(gdb) clear breakingpointnumber // delete breaking point number
like this:
(gdb) clear 1 // delete the breaking point is 1
(gdb) clear functionname // delete the breaking point of this function
- 继续运行至下一断点(running to the next breaking point)
(gdb) continue
- 打印信息(print the info)
(gdb) print var
(gdb) print n
- 执行下一条语句,只执行一条,不进入函数内部(execute the next statement)
(gdb) next
- 步进语句,会进入函数内部(step the next statement)
(gdb) step
- 显示源文件内容(show source file)
(gdb) list beginlinenumber,endlinenumber
or
(gdb) list filename:beginlinenumber,endlinenumber
like this:
(gdb) list 5,10 // show the 5 to 10 line
- 设置参数(set arguments)
进入调试程序后:
(gdb) set args arguments_values
- 退出(quit the gdb)
(gdb) quit
调试多线程
显示线程信息
info threads // show the thread where is running if the symbol * front the line
切换线程
thread THREAD_ID
让所有线程执行同一命令或者让指定线程执行同一命令
thread apply THREAD_ID1 THREAD_ID2 backtrace // show thread1 thread2 stack information
thread apply all backtrace // show everyone thread stack information
本文介绍使用GDB进行程序调试的基本步骤及常用命令,包括编译带调试信息的源文件、设置断点、查看断点信息、继续运行至下一断点等操作。
75

被折叠的 条评论
为什么被折叠?



