段错误http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html
oop消息http://blog.csdn.net/heqiuya/article/details/7762482
段错误定义
A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. In short, a segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (e.g., attempts to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as Address or Bus errors. |
一、gdb用于单步调试,但是只适用于错误可以重现的情况
使用gdb test(所运行程序),然后输入run,程序会自动运行,直到出现错误停止。
调试时,如果出现如下状况
出现错误的函数没有正常显示出来,上网搜索,有人这样解释:
确定安装了libc-dbg包,要不然使用gdb时会报 warning: Can't read pathname for load map: Input/output error ,gdb显示的函数名会是??.
于是我安装了,确实好了,可是下一次调试时又出现这样的问题
二、core文件,记录程序异常时的信息,便于出错后进行分析
可是默认情况下,core文件大小被限制为0,所以发生错误时无法生成core文件,使用如下方式修改。
以普通用户登录,修改~/.bashrc,设置ulimit -c 1024,使用source ~/.bashrc使设置生效,新打开终端,使用ulimit -c查看,则为你设置的值,1024,因为~/.bashrc可以设置该用户打开的所有bash终端
所以,普通用户和root用户有各自的~/.bashrc文件。
shell和bashhttp://forum.ubuntu.org.cn/viewtopic.php?f=86&t=417913
shell、bash等配置文件的作用域http://blog.csdn.net/ithomer/article/details/6322892
ulimit的使用https://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/
source命令,使配置文件不需要重启生效http://blog.sina.com.cn/s/blog_5ac88b350100b9lg.html
好了,终于生成core文件了,使用gdb调试 gdb test(生成core文件的程序) core,
如上图,很轻易的找到错误所在,在ano()函数中的*ptr = 1;指令出错。
bt选项用于打印backtrace(函数栈的所有信息)
可是,如第一项提到的,可能出现如下错误,留待解决。。。。。
三、使用dump文件
首先,查看出错信息
出错地址为0,ip为080483c4(cs为段地址,ip为段内偏移量)
然后,生成dump文件
objdump -D -S test > dump
加入-S选项是为了在汇编语言中查看c源码
objdump命令使用http://blog.csdn.net/beyondioi/article/details/7796414
很轻易查找出,80483c4所在位置,这是错误出现在自己编写的函数中的情况,如果错误出现在库函数中,则麻烦的多,
如图所示,错误出现在im-fcitx.so中,这种情况暂时没有解决。。。。。。
四、举一个稍微复杂点的例子
如图,根据栈的信息,可以判断出错误出现在qqnumbereffective()函数中,具体位置为0x8048acf,但是我们并不知道这个地址是哪条指令,于是借助dump文件
可以很轻易的找到,8048a3f所对应的指令是return qq;
成功定位错误