ubuntu 调试 core 文件
2015-10-16 14:59
393 查看
直接:
需要在 segment fault 的时候产生 core 文件 以便调试:ulimit -c unlimited // 然后 cd 到执行程序所在的目录, 然后再使用gdb调试: gdb my_program core // 列出调用栈,查看出错函数 bt
如果出现 segmentation fault 但是没有生成 core 文件,检查以下参数:
cat /proc/sys/kernel/core_uses_pid cat /proc/sys/fs/suid_dumpable cat /proc/sys/kernel/core_pattern
或者编译的代码加了 -NDEBUG 选项。
注意,对于在
/ect/rc.local中写的开机自动启程序,或者 deamon 程序,
/etc/sysctl.conf中的参数并不会对其生效,需要在其启动之前加入:
ulimit -c unlimited
或者慢慢看:
Description
Core dumps are often used to diagnose or debug errors in Linux or UNIX programs. Core dumps can serve as useful debugging aids for sys admins to find out why Application like Lighttpd, Apache, PHP-CGI or any other program crashed. Many vendors and open source project author requests a core file to troubleshoot a program. A core file is generated when an application program abnormally terminates due to bug, operating system security protection schema, or program simply try to write beyond the area of memory it has allocated, and so on. This article explains how to turn on core file support and track down bugs in programs.Turn On Core File Creation Support
By default most Linux distributions turn off core file creation (at least this is true for RHEL, CentOS, Fedora and Suse Linux). You need to use the ulimit command to configure core files.See The Current Core File Limits
Type the following command:
# ulimit -c
Sample outputs:
0
The output 0 (zero) means core file is not created.
Change Core File Limits
In this example, set the size limit of core files to 75000 bytes:
# ulimit -c 75000
HowTo: Enable Core File Dumps For Application Crashes And Segmentation Faults
Edit /etc/profile file and find line that read as follows to make persistent configuration:
ulimit -S -c 0 > /dev/null 2>&1
Update it as follows:
ulimit -c unlimited >/dev/null 2>&1
Save and close the file. Edit /etc/sysctl.conf, enter:
# sudo vi /etc/sysctl.conf
Append the following lines:
kernel.core_uses_pid = 1 kernel.core_pattern = /tmp/core-%e-%s-%u-%g-%p-%t #或者简单点,希望在程序的当前目录生成core文件,写成 = core 就行了。 fs.suid_dumpable = 2 # 2 dump setuid program, 1 dump all program, no security.
Save and close the file. Where,
kernel.core_uses_pid = 1: Appends the coring processes PID to the core file name.
fs.suid_dumpable = 2: Make sure you get core dumps for setuid programs.
kernel.core_pattern = /tmp/core-%e-%s-%u-%g-%p-%t: When the application terminates abnormally, a core file should appear in the /tmp. The kernel.core_pattern sysctl controls exact location of core file. You can define the core file name with the following template whih can contain % specifiers which are substituted by the following values when a core file is created:
%% - A single % character %p - PID of dumped process %u - real UID of dumped process %g - real GID of dumped process %s - number of signal causing dump %t - time of dump (seconds since 0:00h, 1 Jan 1970) %h - hostname (same as ’nodename’ returned by uname(2)) %e - executable filename
关于
fs.suid_dumpable的值的解释:
This value can be used to query and set the core dump mode for setuid
or otherwise protected/tainted binaries. The modes are
0 - (default) - traditional behaviour. Any process which has changed
privilege levels or is execute only will not be dumped
1 - (debug) - all processes dump core when possible. The core dump is
owned by the current user and no security is applied. This is
intended for system debugging situations only.
2 - (suidsafe) - any binary which normally not be dumped is dumped
readable by root only. This allows the end user to remove
such a dump but not access it directly. For security reasons
core dumps in this mode will not overwrite one another or
other files. This mode is appropriate when adminstrators are
attempting to debug problems in a normal environment.
Finally, enable debugging for all apps, enter (Redhat and friends specific):
# echo "DAEMON_COREFILE_LIMIT='unlimited'" >> /etc/sysconfig/init
Reload the settings in
/etc/sysctl.confby running the following command:
# sudo sysctl -p
How Do I Enable Core Dumping For Specific Deamon?
To enable core dumping for specific deamons, add the following line in the
/etc/sysconfig/daemon-filefile. In this example, edit
/etc/init.d/lighttpedand add line as follows:
DAEMON_COREFILE_LIMIT='unlimited'
Please note that
DAEMON_COREFILE_LIMITis Redhat specific, for all other distro add configuration as follows:
ulimit -c unlimited >/dev/null 2>&1 echo /tmp/core-%e-%s-%u-%g-%p-%t > /proc/sys/kernel/core_pattern
Save and close the file. Restart / reload lighttpd:
# /etc/init.d/lighttpd restart # su - lighttpd $ ulimit -c
Sample outputs:
unlimited
Now, you can send core files to vendor or software writes.
How Do I Read Core Files?
You need use the gdb command as follows:
$ gdb /path/to/application /path/to/corefile (gdb) bt (gdb) bt full (gdb) info threads (gdb) thread apply all bt (gdb) thread apply all bt full
尊重原作,转载自: http://www.cyberciti.biz/tips/linux-core-dumps.html
相关文章推荐
- 基于jQuery实现的菜单切换效果
- ajax post提交的方式
- iOS开发UI篇—CAlayer层的属性
- JAVA设计模式(6) —<结构型>适配器模式(Adapter)
- c语言:统计整数二进制表示中1的个数(汉明重量)
- JS不弹出网页文件下载本地
- Activity的生命周期理解
- 枚举类型
- 统计文件字数,并按出现次数打印结果
- 设计模式六大原则(4):接口隔离原则
- 实例展示elasticsearch集群生态,分片以及水平扩展.
- Android Bitmap和Canvas
- Spring3.0核心组件的源码简单分析
- 用ATL开发复合控件
- C++STL学习(5)容器map和multimap
- 设计模式六大原则(3):依赖倒置原则
- hdfs访问异常
- 大数网:即将召开的华为首届开发者大会说什么?
- oracle运行失败的类型
- 文本框只能输入数字和小数点,且小数点后只允许两位数字