段错误

段错误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.

Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.

On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.


一、gdb用于单步调试,但是只适用于错误可以重现的情况


使用gdb test(所运行程序),然后输入run,程序会自动运行,直到出现错误停止。

调试时,如果出现如下状况

出现错误的函数没有正常显示出来,上网搜索,有人这样解释:

确定安装了libc-dbg包,要不然使用gdb时会报 warning: Can't read pathname for load map: Input/output error ,gdb显示的函数名会是??.

于是我安装了,确实好了,可是下一次调试时又出现这样的问题

gdb使用http://wiki.ubuntu.org.cn/index.php?title=%E7%94%A8GDB%E8%B0%83%E8%AF%95%E7%A8%8B%E5%BA%8F&variant=zh-hans

二、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;

成功定位错误

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值