linux gdb 调试

来源:互联网 发布:2017部落冲突兵种数据 编辑:程序博客网 时间:2024/05/15 01:01

1.查看系统信息

# uname -a

Linux (none) 3.0.8 #20 Thu Mar 5 00:55:14 PST 2015 armv5tejl GNU/Linux


根文件系统配置必需加载 gdb 模块:

goke根文件系统配置加载 gdb模块:

通过文件系统配置文件rootfs.maksoftware/linux/rootfs/rootfs.mak)可以选择性安装根文
件系统所需要的组件

如果需要安装gdb,则做如下设置:
############################################################################
# --gdb
############################################################################
GDB_7_5_1_SUPPORT := 1
GDB_7_5_1_DEPEND := ZLIB_1_2_7_SUPPORT NCURSES_5_9_SUPPORT
这样在安装根文件系统的时候就会将 gdb 以及它所依赖的 zlibncurses 组件一并装入新建的根文件系统。

2.查看系统限制参数信息

# ulimit -a
time(seconds)        unlimited
file(blocks)         unlimited
data(kb)             unlimited
stack(kb)            8192
coredump(blocks)     unlimited
memory(kb)           unlimited
locked memory(kb)    64
process              219
nofiles              1024
vmemory(kb)          unlimited
locks                unlimited


修改coredump生成文件大小的指令:ulimit -c 1024(-c是ubuntu上显示的,操作coredump对应的代号)

ulimit -c unlimited


3.在宿主目录下修改.bashrc配置文件中添加 ulimit -c unlimited ,修改完.bashrc文件后运行. .bashrc让文件修改生效


4.写错误程序并编译

#include <stdio.h>

void testfun()
{
        int *f = NULL;
        *f = 2;
}

int main()
{
        printf("hello word!\n");
        testfun();
        return 0;
}


编译执行:

$ gcc -o test -g gdbtest.c

$ ./test

$ ls -l core 

-rw------- 1 zhancj zhancj 155648 2015-09-16 10:51 core


5.gdb调试

$ gdb -q test core
Reading symbols from /home/zhancj/unix_program/gdbtest/test...done.
[New Thread 1812]


warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/tls/i686/cmov/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/tls/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483f4 in testfun () at gdbtest.c:6
6 *f = 2;
(gdb) where //哪里出错
#0  0x080483f4 in testfun () at gdbtest.c:6

#1  0x08048416 in main () at gdbtest.c:12

(gdb) file ./test //GDB读取执行文件符号信息
Reading symbols from /home/zhancj/unix_program/gdbtest/test...done.
(gdb) bt //backtrace查看调用栈出错的地方
#0  0x080483f4 in testfun () at gdbtest.c:6
#1  0x08048416 in main () at gdbtest.c:12

(gdb) l//查看程序执行错误的地方
2
3 void testfun()
4 {
5 int *f = NULL;
6 *f = 2;
7 }
8
9 int main()
10 {
11 printf("hello word!\n");//下一行执行代码,出现错误

(gdb) b testfun//在testfun函数调用时设置断点
Breakpoint 1 at 0x80483ea: file gdbtest.c, line 5.
(gdb) run //运行
Starting program: /home/zhancj/unix_program/gdbtest/test 
hello word!


Breakpoint 1, testfun () at gdbtest.c:5
5 int *f = NULL;
(gdb) n //单步
6 *f = 2;
(gdb) n//单步


Program received signal SIGSEGV, Segmentation fault.
0x080483f4 in testfun () at gdbtest.c:6
6 *f = 2;
(gdb) n//单步


Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) q //退出调试
/build/buildd/gdb-7.1/gdb/inferior.c:353: internal-error: find_inferior_pid: Assertion `pid != 0' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Quit this debugging session? (y or n) y
/build/buildd/gdb-7.1/gdb/inferior.c:353: internal-error: find_inferior_pid: Assertion `pid != 0' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Create a core file of GDB? (y or n) y
Aborted (core dumped)



编译文件时需要注意:

1.编译项加-g:

$(CC) -g -c device.c -o $(OBJSPATH)/device.o $(CFlLAGS)


2.关闭arm-goke-linux-uclibcgnueabi-strip debug/ec_ipc_goke

# IPCAM
arm-goke-linux-uclibcgnueabi-g++ $(CFLAGS) $(OBJSPATH)/*.o -o $(OBJSPATH)/ec_ipc_goke $(LDFLAGS)
# arm-goke-linux-uclibcgnueabi-strip debug/ec_ipc_goke




0 0