0.绪
GDB(GNU Debugger)是类 Unix(如 Linux)操作系统下的一款开源的 C/C++ 程序调试器。
要使用gdb进行调试,需要在gcc/g++命令选项加“-g”,去掉调试信息使用“strip”命令。
例如:下面fork.c文件:
#include<stdio.h>
#include<unistd.h>
int main(){
if( ( fork()&& fork() ) || ( fork()&&fork() ) );
printf("fork\n");
//return 0;
}
编译:
gcc -g -o fork fork.c
调试:
gdb fork
bo@bo:/home/share/test$ gdb fork
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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://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, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from fork...done.
(gdb)
去掉调试信息:
strip fork
2.gdb命令
命令 | 缩写 | 说明 |
run | r | 运行程序 |
continue | c | 继续执行,到下一个断点处 |
next | n | 单步跟踪程序 |
step | s | 则进入函数 |
until | u | 运行程序直到退出循环体 |
until+行号 | 运行至某行 | |
finish | fi | 运行程序,直到当前函数完成返回 |
call | 调用程序中可见的函数,并传递“参数” | |
return | 结束当前调用函数并返回指定值 | |
jump | j | 前程序执行流跳转到指定行 |
p | 打印变量或寄存器值 | |
backtrace | bt | 当前运行的堆栈列表 |
break | b | 设置断点 |
delete | del | 删除断点 |
enable | 启用断点 | |
disable | 禁用断点 | |
watch | 监视变量 | |
list | l | 查看源代码 |
disassemble | dis | 查看汇编代码 |
info | 查看当前断点设置情况 | |
set args | 设置程序启动命令行参数 | |
show args | 查看设置的命令行参数 | |
layout | 分割窗口 |