中国IT论坛's Archiver

水墨山林 发表于 2010-6-28 17:09

内核转存

[list][font=SimSun][url=http://www.lazysa.com/2009/08/708.html][size=18pt][b][color=#0066cc]内核转储(core dump)介绍与分析[/color][/b][/size][/url][/font]
[font=SimSun][size=10pt][b]内核转储core dump[/b][/size][/font]
[font=SimSun][size=10pt][b]何谓 core?[/b][/size][/font]
[font=SimSun][size=10pt]在使用半导体作为内存的材料前,人类是利用线圈当作内存的材料(发明 者为王安),线圈就叫作 core ,用线圈做的内存就叫作 core memory。如今 ,半导体工业澎勃发展,已经没有人用 core memory 了,不过,在许多情况下, 人们还是把记忆体叫作 core 。 [/size][/font]
[font=SimSun][size=10pt][b]何谓 core dump?[/b][/size][/font]
[font=SimSun][size=10pt]我们在开发(或使用)一个程序时,最怕的就是程序莫明其妙地当掉。虽然系统没事,但我们下次仍可能遇到相同的问题。于是这时操作系统就会把程序当掉 时的内存内容 dump 出来(现在通常是写在一个叫 core 的 file 里面),让 我们或是 debugger 做为参考。这个动作就叫作 core dump。 [/size][/font]
[font=SimSun][size=10pt][b]为何会发生 core dump?[/b][/size][/font]
[font=SimSun][size=10pt]前面说过,在程序当掉时出错。在 C/C++语言中,最常发生错误的地方就是指针有问题。您可以利用 core 文件和 debugger 把错误找出来(要怎麽在 debugger 中使用 core 文件?man 一下 gdb 吧!)。 [/size][/font]
[font=SimSun][size=10pt][b]我可以把 core 文件删掉吗?[/b][/size][/font]
[font=SimSun][size=10pt]如果你不会、不能、不需要修改程序,那就放心地把它删除了吧! [/size][/font]
[font=SimSun][size=10pt][b]要怎麽才不会让 core 文件出现?[/b][/size][/font]
[font=SimSun][size=10pt]如果用的是tcsh的话, 以试著在 .tcshrc 里加一行:[/size][/font]
[font=SimSun][size=10pt]limit coredumpsize 0[/size][/font]
[font=SimSun][size=10pt]如果用的是bash的话, 在/etc/profile里加上(或者修改)一条:[/size][/font]
[font=SimSun][size=10pt]ulimit -c 0[/size][/font]
[font=SimSun][size=10pt][b]有哪些core dump?[/b][/size][/font]
[font=SimSun][size=10pt]常见的coredump有:[/size][/font]
[font=SimSun][size=10pt]netdump —- 网络崩溃引起的内核转储[/size][/font]
[font=SimSun][size=10pt]kdump —- 内核意外崩溃引起的内核转储[/size][/font]
[font=SimSun][size=10pt]diskdump —- 硬盘意外崩溃引起的内核转储[/size][/font]
[font=SimSun][size=10pt][b]一个实例, 可以让你看出 core 最好用的地方 [/b][/size][/font]
[img=15,15]file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif[/img]
[font=SimSun][size=10pt]gdb -c core, 进去后打 where, 就可以 show 出你是在程序哪一行当掉的, 还有在当掉时在哪个 function 里, 这个 function 是被哪个 function 所 call 的, 而这个 function 又是被哪个 function 所 call 的…. 一直到 main() [/size][/font]
[font=SimSun][size=10pt]由这个信息, 可以找出五六成的 bug…….. 屡试不爽 [/size][/font]
[font=SimSun][size=10pt]但, 先决条件, 当你在 compile 时必须把 debug information 的选项打开 不然, 就会出现一大堆你看不懂的东西,而不是你喜欢的源程序。 [/size][/font]

[/list]

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.