core dump 相关保存(转自http://blog.sina.com.cn/s/blog_602f87700100ew04.html)

1. 什么是Core:

Sam之前一直以为Core Dump中Core是 Linux Kernel的意思. 今天才发现在这里,Core是另一种意思:

在使用半导体作为内存的材料前,人类是利用线圈当作内存的材料(发明者为王安),线圈就叫作 core ,用线圈做的内存就叫作 core memory。如今 ,半导体工业澎勃发展,已经没有人用 core memory 了,不过,在许多情况下, 人们还是把记忆体叫作 core 。

 

2. 什么是Core Dump:

我们在开发(或使用)一个程序时,最怕的就是程序莫明其妙地当掉。虽然系统没事,但我们下次仍可能遇到相同的问题。于是这时操作系统就会把程序当掉 时的内存内容 dump 出来(现在通常是写在一个叫 core 的 file 里面),让 我们或是 debugger 做为参考。这个动作就叫作 core dump。

 

3. Core Dump时会生成何种文件:

Core Dump时,会生成诸如 core.进程号 的文件。

 

4. 为何有时程序Down了,却没生成 Core文件。

Linux下,有一些设置,标明了resources available to the shell and to processes。 可以使用

#ulimit -a 来看这些设置。 (ulimit是bash built-in Command)

              -a     All current limits are reported
              -c     The maximum size of core files created
              -d     The maximum size of a process钬檚 data segment
              -e     The maximum scheduling priority ("nice")
              -f     The maximum size of files written by the shell and its children
              -i     The maximum number of pending signals
              -l     The maximum size that may be locked into memory
              -m     The maximum resident set size (has no effect on Linux)
              -n     The maximum number of open file descriptors (most systems do not allow this value to be set)
              -p     The pipe size in 512-byte blocks (this may not be set)
              -q     The maximum number of bytes in POSIX message queues
              -r     The maximum real-time scheduling priority
              -s     The maximum stack size
              -t     The maximum amount of cpu time in seconds
              -u     The maximum number of processes available to a single user
              -v     The maximum amount of virtual memory available to the shell
              -x     The maximum number of file locks

 从这里可以看出,如果 -c是显示:core file size          (blocks, -c)

如果这个值为0,则无法生成core文件。所以可以使用:

#ulimit -c 1024  或者 #ulimit -c unlimited  来使能 core文件。

如果程序出错时生成Core 文件,则会显示Segmentation fault (core dumped)

 

5. Core Dump的核心转储文件目录和命名规则:
/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0

 

 

6. 如何使用Core文件:

在Linux下,使用:

#gdb -c core.pid program_name

就可以进入gdb模式。

 

输入where,就可以指出是在哪一行被Down掉,哪个function内,由谁调用等等。

(gdb) where

 

或者输入 bt。

(gdb) bt

 

7. 如何让一个正常的程序down:

#kill -s SIGSEGV pid

 

 

8. 察看Core文件输出在何处:

存放Coredump的目录即进程的当前目录,一般就是当初发出命令启动该进程时所在的目录。但如果是通过脚本启动,则脚本可能会修改当前目录,这时进程真正的当前目录就会与当初执行脚本所在目录不同。这时可以查看”/proc/<进程pid>/cwd“符号链接的目标来确定进程真正的当前目录地址。通过系统服务启动的进程也可通过这一方法查看。

 

9. 嵌入式设备下如何使用Core dump:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章