Linux Segmentation fault (coredump)调试手段

简介

以下内容来自维基百科:Segmentation fault

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 (for example, attempting to write to a read-only location, or to overwrite part of the operating system).

The term “segmentation” has various uses in computing; in the context of “segmentation fault”, a term used since the 1950s,[citation needed] it refers to the address space of a program.[6] With memory protection, only the program’s own address space is readable, and of this, only the stack and the read/write portion of the data segment of a program are writable, while read-only data and the code segment are not writable. Thus attempting to read outside of the program’s address space, or writing to a read-only segment of the address space, results in a segmentation fault, hence the name.


On systems using hardware memory segmentation to provide virtual memory, a segmentation fault occurs when the hardware detects an attempt to refer to a non-existent segment, or to refer to a location outside the bounds of a segment, or to refer to a location in a fashion not allowed by the permissions granted for that segment. On systems using only paging, an invalid page fault generally leads to a segmentation fault, and segmentation faults and page faults are both faults raised by the virtual memory management system. Segmentation faults can also occur independently of page faults: illegal access to a valid page is a segmentation fault, but not an invalid page fault, and segmentation faults can occur in the middle of a page (hence no page fault), for example in a buffer overflow that stays within a page but illegally overwrites memory.


At the hardware level, the fault is initially raised by the memory management unit (MMU) on illegal access (if the referenced memory exists), as part of its memory protection feature, or an invalid page fault (if the referenced memory does not exist). If the problem is not an invalid logical address but instead an invalid physical address, a bus error is raised instead, though these are not always distinguished.


At the operating system level, this fault is caught and a signal is passed on to the offending process, activating the process’s handler for that signal. Different operating systems have different signal names to indicate that a segmentation fault has occurred. On Unix-like operating systems, a signal called SIGSEGV (abbreviated from segmentation violation) is sent to the offending process. On Microsoft Windows, the offending process receives a STATUS_ACCESS_VIOLATION exception.

gdb

很强大的功能,值得专门来一篇。gnu 提供的官方文档如下:
GDB: The GNU Project Debugger
特别想说明的是,从 gdb 6.1 开始,支持以 TUI(Terminal User Interface)支持交互模式开启。

gdb test -tui              #程序为test

addr2line

addr2line 可以将 ip 所指的地址转换为源码行号。
推荐使用下面方式打印源代码函数:

addr2line -C -f -e <YourPrograme> <lineNumber>  #其中YourPrograme也可以是库文件

dmesg

dmesg 用来打印或控制内核环形缓冲区。

nm

nm命令可以列出二进制文件中的符号表,包括符号地址、符号类型、符号名等,这样可以帮助定位在哪里发生了段错误。

objdump

查看二进制文件的内部信息,包含反汇编。

readelf

直接读取 Linux 平台的 ELF(Executable Linkable Format) 文件内容。如符号表、字符串表、段名字、重定位等。

可能引起的cause

发生Segmentation 的条件以及它们如何表现出来是特定于硬件和操作系统的:不同的硬件在给定的条件下会引发不同的故障,不同的操作系统会将这些故障转换为传递给进程的不同信号。 直接原因是内存访问冲突,而根本原因通常是某种软件错误。 确定根本原因——调试错误——在某些情况下可能很简单,其中程序会始终导致分段错误(例如,取消引用空指针),而在其他情况下,错误可能难以重现并取决于内存分配 在每次运行时(例如,取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值