LINUX调试

来源:互联网 发布:pp助手官网下载mac版 编辑:程序博客网 时间:2024/06/05 19:25

在linux调试时经常碰到堆栈错误,我们可以使用core文件对问题进行定位,

使用命令ulimit -c 1024设置core文件最大值;

设置后使用ulimit -c 查看设置的值是否生效没有生效显示为0;设置成功为1024;

不同系统生成的core文件命名不同,centos为core.pid,ubuntu为core等

生成后使用命令gdb  profilename core;

就会进入到gdb命令中,使用bt命令查看堆栈调用信息。


具体命令使用如下例子:

dalek@ubuntu:~/test/core$ vim tst.c



#include <stdio.h>
#include <stdlib.h>


struct tt 
{
int i ;
int j;
};
int main()
{


int i = 0;
struct tt t;
memset(&t, 0, sizeof(struct tt));


struct tt *p = &t;


p = NULL;
p->i = 0;




return 0;
}


dalek@ubuntu:~/test/core$ gcc tst.c 
tst.c: In function 鈥榤ain鈥


                             tst.c:14:1: warning: incompatible implicit declaration of built-in function 鈥榤emset鈥[enabled by default]
ls^Hdalek@ubuntu:~/test/core$ 
dalek@ubuntu:~/test/core$ ls
a.out  tst.c
dalek@ubuntu:~/test/core$ ulimit -c
0
dalek@ubuntu:~/test/core$ ulimit -c 1024
dalek@ubuntu:~/test/core$ ulimit -c
1024
dalek@ubuntu:~/test/core$ ./a.out 
Segmentation fault (core dumped)
dalek@ubuntu:~/test/core$ ls 
a.out  core  tst.c
dalek@ubuntu:~/test/core$ ll
ll: command not found
dalek@ubuntu:~/test/core$ ls -l
total 208
-rwxrwxr-x 1 dalek dalek   7121 Jun 28 05:37 a.out
-rw-r----- 1 dalek dalek 200704 Jun 28 05:37 core
-rw-rw-r-- 1 dalek dalek    198 Jun 28 05:36 tst.c
dalek@ubuntu:~/test/core$ gdb -c core 
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>.
[New LWP 11299]
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483e1 in ?? ()
(gdb) bt
#0  0x080483e1 in ?? ()
#1  0x00000000 in ?? ()
(gdb) q
dalek@ubuntu:~/test/core$ gdb a.out core 
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/dalek/test/core/a.out...(no debugging symbols found)...done.
[New LWP 11299]


warning: Can't read pathname for load map: Input/output error.
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483e1 in main ()
(gdb) bt
#0  0x080483e1 in main ()
(gdb) bt
#0  0x080483e1 in main ()
(gdb) q
dalek@ubuntu:~/test/core$ gdb a.out -c core 
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 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 "i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/dalek/test/core/a.out...(no debugging symbols found)...done.
[New LWP 11299]


warning: Can't read pathname for load map: Input/output error.
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x080483e1 in main ()
(gdb) 

原创粉丝点击