幼儿园老师的职业病:让程序崩溃时产生coredump
来源:百度文库 编辑:中财网 时间:2024/05/19 23:33:07
在 Windows 下我们已经习惯了用 Windbg 之类的工具调试 dump 文件,从而分析并排除程序运行时错误。在 Linux 下我们同样可以完成类似的工作 —— Core Dump。
我们先看看相关的设置。
$ ulimit -acore file size (blocks, -c) 0data seg size (kbytes, -d) unlimitedscheduling priority (-e) 20file size (blocks, -f) unlimitedpending signals (-i) 16382max locked memory (kbytes, -l) 64max memory size (kbytes, -m) unlimitedopen files (-n) 1024pipe size (512 bytes, -p) 8POSIX message queues (bytes, -q) 819200real-time priority (-r) 0stack size (kbytes, -s) 8192cpu time (seconds, -t) unlimitedmax user processes (-u) unlimitedvirtual memory (kbytes, -v) unlimitedfile locks (-x) unlimited
"core file size (blocks, -c) 0" 意味着在程序崩溃时不会生成 core dump 文件,我们需要修改一下设置。如果你和我一样懒得修改配置文件,那么就输入下面这样命令吧。
$ sudo sh -c "ulimit -c unlimited; ./test" # test 是可执行文件名。
等等…… 我们还是先准备个测试目标。
#include#include void test(){ char* s = "abc"; *s = 'x';}int main(int argc, char** argv){ test(); return (EXIT_SUCCESS);}
很显然,我们在 test() 里面写了一个不该写的东东,这无疑会很严重。生成可执行文件后,执行上面的命令。
$ sudo sh -c "ulimit -c unlimited; ./test"Segmentation fault (core dumped)
$ ls -ltotal 96-rw------- 1 root root 167936 2010-01-06 13:30 core-rwxr-xr-x 1 yuhen yuhen 9166 2010-01-06 13:16 test
这个 core 文件就是被系统 dump 出来的,我们分析目标就是它了。
$ sudo gdb test coreGNU gdb (GDB) 7.0-ubuntuCopyright (C) 2009 Free Software Foundation, Inc.Reading symbols from .../dist/Debug/test...done.warning: Can't read pathname for load map: Input/output error.Reading symbols from /lib/tls/i686/cmov/libpthread.so.0... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/tls/i686/cmov/libpthread.so.0Reading symbols from /lib/tls/i686/cmov/libc.so.6... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/tls/i686/cmov/libc.so.6Reading symbols from /lib/ld-linux.so.2... ...done.(no debugging symbols found)...done.Loaded symbols for /lib/ld-linux.so.2Core was generated by `./test'.Program terminated with signal 11, Segmentation fault.#0 0x080483f4 in test () at main.c:16warning: Source file is more recent than executable.16 *s = 'x';
最后这几行提示已经告诉我们错误的原因和代码位置,接下来如何调试就是 gdb 的技巧了,可以先输入 "where" 看看调用堆栈。
(gdb) where#0 0x080483f4 in test () at main.c:16#1 0x08048401 in main (argc=1, argv=0xbfd53e44) at main.c:22
(gdb) p s$1 = 0x80484d0 "abc"
(gdb) info filesSymbols from ".../dist/Debug/test".Local core dump file:Local exec file: `.../dist/Debug/test', file type elf32-i386. Entry point: 0x8048330 0x08048134 - 0x08048147 is .interp ... ... 0x08048330 - 0x080484ac is .text 0x080484ac - 0x080484c8 is .fini 0x080484c8 - 0x080484d4 is .rodata
很显然 "abc" 属于 .rodata,严禁调戏。
----------------
附:如果你调试的是 Release (-O2) 版本,而且删除(strip)了符号表,那还是老老实实数汇编代码吧。可见用 Debug 版本试运行是很重要滴!!!
关于程序崩溃
怎样让系统崩溃?
有没有好的程序, 让机子自动在某一时刻崩溃
如何让程序感染到另个程序,让另个程序的窗体上产生新的功能控件
怎么才能让系统崩溃?
为什么玩模拟人生经常程序崩溃?
为何一切换输入法程序就崩溃?
紫光输入法打字造成程序崩溃
紫光输入法打字造成程序崩溃
弟弟让整个家庭快崩溃了
有没有办法让操作系统定时崩溃
如果让一切崩溃,这一秒后便是尽头
网通公司让我崩溃了!
这垃圾病毒让我崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
弟弟让整个家庭快崩溃了
IE产生错误程序