如何让程序crash时生成coredump文件并用gdb调试

How to generate coredump file and debug it in gdb

1. ulimit -a 可以查看当前所有的limit
2. ulimit -c用来设置coredump。比如:ulimit -c unlimited,就设置不限制生成的coredump文件的大小。也可以ulimit -c 1024,这样就设置了coredump文件大小不超过1M。
3. 运行程序crash之后,core文件生成。此时:

gdb <program> <corefile>

或者直接运行gdb,然后在gdb的命令行中输入:core <corefile>也可以。

gdb默认就会打印出crash时候的函数调用和原因,比如:

#0 0xb7ca729b in strlen () from /lib/tls/i686/cmov/libc.so.6

这表示strlen函数调用出现了问题。

然后执行命令bt,打印函数调用栈:

(gdb) bt
#0 0xb7ca729b in strlen () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7e028ce in g_strdup () from /usr/lib/libglib-2.0.so.0
#2 0xb7fe1180 in ?? () from /usr/lib/libgobject-2.0.so.0
#3 0xb7fbed20 in g_object_set_valist () from /usr/lib/libgobject-2.0.so.0
#4 0xb7fbf266 in g_object_set () from /usr/lib/libgobject-2.0.so.0
#5 0x0804c889 in gst_pipeline_func (data=0xbfdcd644) at tester.c:1092
#6 0xb7e0c02f in ?? () from /usr/lib/libglib-2.0.so.0
#7 0xb7d9550f in start_thread () from /lib/tls/i686/cmov/libpthread.so.0
#8 0xb7d117ee in clone () from /lib/tls/i686/cmov/libc.so.6

这很清楚了,从下往上就是函数调用栈。问题显然出在了g_object_set这里,这个函数是在gst_pipeline_func函数中被调用的。

所以使用core文件对于debug程序有很大帮助的。
原文地址:https://www.cnblogs.com/super119/p/1996132.html