inux GDB调试--启动程序进行调试:GDB 程序名
linux GDB调试--对正在运行的程序进行调试:启动GDB后,attach 进程ID
linux GDB调试--对Coredumped文件进行调试:GDB 程序名 Coredumped文件名
调试准备
1、简单的问题程序,数组加法
#include <stdio.h> int main (void) { int count = 0; int addn[10]; int i; for ( i = 0; i <= 10; i++){ addn[i] = i; count += addn[i]; } printf("count = %d \n", count); return 0; }
!!!注意程序中存在数组越界
2、makefile文件
cc = gcc target = main obj = add.o $(target):$(obj) $(cc) -g $(obj) -Wall -o $(target) -lpthread -lrt add.o : add.c $(cc) -c add.c -g -lpthread .PHONY:clean $(RM) *.o $(target)
3、设置ulimit 使得编译产生core文件
- 查看ulimit设置,命令ulimit -c
一般默认为0,表示不生成core文件
- 使用ulimit -c unlimited来设置无限大,则任意情况下都会产生core文件
- 编译运行后产生core文件
调试过程
1、GDB进入core文件调试
- 命令gdb 编译的可执行文件名 Coredumped文件名
2、查看堆栈信息
- 命令 where 或者 breaktrace 或者 bt (bt等价于breaktrace)
- 根据堆栈信息或者gdb进入时罗列的信息查看Coredumped位置
这个便是提示数组越界的信息
如果是程序挂住,gdb会具体显示Coredumped的位置,挂在什么文件什么位置
3、退出GDB
- 命令 q或者quit(q等价于quit)
4、根据查找的问题根源修改源代码