找回密码
 注册
搜索
查看: 624|回复: 2

[讨论] linux 下segmentation fault 的调试

[复制链接]
发表于 2006-7-4 13:49:00 | 显示全部楼层 |阅读模式
首先介绍一下segmentation fault,主要就是编程的时候访问了非法的指针,比如空指针,下面是一段权威的描述,结合了操作系统对内存管理和进程的虚拟内存,大家可以看看哦
One of the most common problems when making software is errors like "Segmentation fault", also called SegFault. Here is what a SegFault is. Virtual memory in a computer can be created in 2 ways: pages or segments. Paging means that the memory is divided in pages of equal size, containing words of memory in it. Segmentation means that every process has a segment of memory of needed size, with gaps of empty memory blocks between the segments. The operating system knows the upper limit of every segment, and every segment begins at a virtual address 0. When a program accesses a memory block, it calls a virtual address that the Memory Management Unit (MMU) maps to a real address. If the operating system sees that the requested address doesn't match any valid address in the segment, it will send a signal to the process terminating it. SegFaults are the direct result of a memory error. The program has a bad pointer, a memory leak or any kind of error that makes it access the wrong memory address. To correct these errors you need to check pointers and arrays for errors.
 楼主| 发表于 2006-7-4 13:53:00 | 显示全部楼层
<P>下面给一个快速发现代码中指针非法使用引起错误的例子。</P><P><FONT face=Arial size=1>To debug segmentation fault, please following steps:<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1>&gt; ulimit –c unlimited<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1># run test image<p></p></FONT></P><P><FONT face=Arial size=1>&gt; ./a.out<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1># Segmenation fault happened. You will see the file “core.xxxx” (xxxx is pid) in the directory<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1>&gt; gdb a.out core.xxxx<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1>&gt; (gdb) bt<p></p></FONT></P><P><FONT face=Arial size=1><p> </p></FONT><FONT face=Arial size=1># Then you will see the segmentation fault point at the File xxxx.cpp : Line xxxx.<p></p></FONT></P>
点评回复

使用道具 举报

 楼主| 发表于 2006-7-4 14:05:00 | 显示全部楼层
<P><FONT face=Arial color=navy size=1>举个例子,我在fedora 下面运行这个代码的, 文件名叫做test.c</FONT></P><P><FONT face=Arial color=navy size=1>#include &lt;stdio.h&gt;<p></p></FONT></P><P><FONT face=Arial color=navy size=1><p> </p></FONT><FONT face=Arial color=navy size=1>int main()<p></p></FONT></P><P><FONT face=Arial color=navy size=1>{<p></p></FONT></P><P><FONT face=Arial color=navy size=1>   char *p = NULL;<p></p></FONT></P><P><FONT face=Arial color=navy size=1>   *p = 10;<p></p></FONT></P><P><FONT face=Arial color=navy size=1>}</FONT></P><P><FONT face=Arial color=navy size=1>1.编译一下 gcc -ggdb test.c</FONT></P><P><FONT face=Arial color=navy size=1>2.输入命令 ulimit -c unlimited</FONT></P><P><FONT face=Arial color=navy size=1>3.运行文件 ./a.out</FONT></P><P><FONT face=Arial color=navy size=1>发生segmentation falut,同时会生成一个文件core.xxxx(xxxx means pid)</FONT></P><P><FONT face=Arial color=navy size=1>4.gdb a.out core.xxxx</FONT></P><P><FONT face=Arial color=navy size=1>5.gdb &gt; bt</FONT></P><P><FONT face=Arial color=navy size=1>马上就会输出错误代码所在的文件和行数,同时还打印出这句错误的语句。</FONT></P>
点评回复

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-5-9 20:32 , Processed in 0.047055 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表