这样便把core file的大小设置为了无限大,同时也可以使用数字来替代unlimited,对core file的上限值做更精确的设定。 生成的core file在哪里? core file生成的地方是在/proc/sys/kernel/core_pattern文件定义的。 改动到生成到自己定义的目录的方法是:
echo "pattern" > /proc/sys/kernel/core_pattern
并且只有超级用户可以修改这两个文件。 "pattern"类似我们C语言打印字符串的格式,相关标识如下: %%: 相当于%
%p: 相当于<pid>
%u: 相当于<uid>
%g: 相当于<gid>
%s: 相当于导致dump的信号的数字
%t: 相当于dump的时间
%h: 相当于hostname
%e: 相当于执行文件的名称 这时用如下命令设置生成的core file到系统/tmp目录下,并记录pid以及执行文件名 echo "/tmp/core-%e-%p" > /proc/sys/kernel/core_pattern 测试如下代码 ?
| 1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h>
int func(int *p)
{
*p = 0;
}
int main()
{
func(NULL);
return 0;
}
|
Segmentation fault (core dumped) <-----这里出现段错误并生成core文件了。 在/tmp目录下发现文件core-main-10815 如何查看进程挂在哪里了? 我们可以用 gdb main /tmp/core-main-10815 查看信息,发现能定位到函数了 Program terminated with signal 11, Segmentation fault.
#0 0x080483ba in func ()
如何定位到行?
在编译的时候开启-g调试开关就可以了 gcc -o main -g a.c gdb main /tmp/core-main-10815 最终看到的结果如下,好棒。 Program terminated with signal 11, Segmentation fault.#0 0x080483ba in func (p=0x0) at a.c:5
5 *p = 0; 总结一下,需要定位进程挂在哪一行我们只需要4个操作, ulimit -c unlimited echo "/tmp/core-%e-%p" > /proc/sys/kernel/core_pattern gcc -o main -g a.c gdb main /tmp/core-main-10815 就可以啦。 补充说明: 相关常用gdb命令 1,(gdb) backtrace /* 查看当前线程函数栈回溯 */ 以上面的例子为例 Program terminated with signal 11, Segmentation fault. #0 0x080483ba in func (p=0x0) at main.c:5 5 *p = 0; (gdb) backtrace #0 0x080483ba in func (p=0x0) at main.c:5 #1 0x080483d4 in main () at main.c:10 如果是多线程环境下(gdb) thread apply all backtrace /* 显示所有线程栈回溯 */ 2,(gdb) print [var] /* 查看变量值 */ (gdb) print p $1 = (int *) 0x0 (gdb) print &p $2 = (int **) 0xbf96d4d4 3,(gdb) x/FMT [Address] /* 根据格式查看地址指向的值 */ 其中 FMT is a repeat count followed by a format letter and a size letter. Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal), t(binary), f(float), a(address), i(instruction), c(char) and s(string). Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes). The specified number of objects of the specified size are printed according to the format. (gdb) x/d 0xbf96d4d4 0xbf96d4d4: 0 (gdb) x/c 0xbf96d4d4 0xbf96d4d4: 0 ‘\000‘ 另外能导致产生core file文件的信号有以下10种
SIGQUIT:终端退出符
SIGILL:非法硬件指令
SIGTRAP:平台相关的硬件错误,现在多用在实现调试时的断点
SIGBUS:与平台相关的硬件错误,一般是内存错误
SIGABRT:调用abort函数时产生此信号,进程异常终止
SIGFPE:算术异常
SIGSEGV:segment violation,无效内存引用
SIGXCPU:超过了cpu使用资源限制(setrlimit)
SIGXFSZ:超过了文件长度限制(setrlimit)
SIGSYS:无效的系统调用
转载:gdb结合coredump定位崩溃进程
标签: