Linux下的「Segmentation fault (core dumped)」


=Start=

缘由:

最近一段时间在Linux下用C语言做一些开发工作,但是由于实际经验不足,问题比较多。其中段错误就是让人非常头痛的一个问题。所以就借此机会系统学习了一下,这里对Linux环境下的段错误(Segmentation fault)做个小结,方便以后遇到类似问题好进行排查与解决。

正文:

参考解答:
什么是段错误?(what&why)

下面是来自 Answers.com 的定义:

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.

对照的中文解释:

所谓的段错误就是指访问的内存超出了系统给这个程序的内存空间,通常这个值是由 gdtr 来保存的,它是一个 48 位的寄存器,其中的 32 位是保存由它指向的 gdt 表,后 13 位保存相应于 gdt 的下标,最后 3 位包括了程序是否在内存中以及程序的在 cpu 中的运行级别,指向的 gdt 是由以 64 位为一个单位的表,在这张表中就保存着程序运行的代码段以及数据段的起始地址以及与此相应的段限和页面交换还有程序运行级别还有内存粒度等等的信息。一旦一个程序发生了越界访问,cpu 就会产生相应的异常保护,于是 segmentation fault 就出现了。

通过上面的解释,段错误应该就是访问了不可访问的内存,这个内存空间要么是不存在的,要么是受到系统保护的。

常见段错误举例

访问不存在的内存地址

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int *ptr = NULL;
    *ptr = 0;
}

分析:访问内存地址不存在(这里是NULL)的地方——引用空指针。

访问受系统保护的内存地址

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int *ptr = (int *)0;
    *ptr = 100;
}

分析:上面的那段代码试图往内存地址为0的地方进行写入。

尝试对只读内存进行写入

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    char *ptr = "test";
    *ptr = "TEST"; // 或 strcpy(ptr, "TEST");
}

分析:ptr 被定义成了 “test”,是一个只读的内存段,不能直接写入,要写入需要用 malloc 从堆中分配或者定义成一个字符串数组。

栈溢出

#include <stdio.h>
#include <stdlib.h>
void main()
{
    main();
}

分析:上面实际上是一个死循环的递归调用,会造成栈溢出。

printf数据类型不一致

#include <stdio.h>
int main() {
      int b = 10;
      printf("%s\n", b);
      return 0;
}

分析:打印字串时,实际上是打印某个地址开始的所有字符,而这里把整数作为参数传递过去,这个整数被当成了一个地址,然后 printf 从这个地址开始打印字符,直到某个位置上的值为 \0。如果这个整数代表的地址不存在或者不可访问,自然也是访问了不该访问的内存 —— segmentation fault。

 scanf忘记取地址符(&)

int i;
scanf("%d", i);

分析:i 被定义后,数值是不确定的,而 scanf 把 i 的值当作参数传入 scanf,而 scanf 则会把 i 当成了地址,把用户输入的内容存入该处。而该地址因为随机,可能根本就不存在或者不合法。

数组越界

#include <stdio.h>
int main() {
    char test[1];
    printf("%c", test[100000]); // 当100000变得很大时,可能会引发「Bus error」而不是「Segmentation fault」
    return 0;
}
如何调试段错误?(how)

1. 使用printf/fprintf输出信息(要注意可能由printf引入的coredump问题

这个是看似最简单但往往很多情况下十分有效的调试方式,也许可以说是程序员用的最多的调试方式。简单来说,就是在程序的重要代码附近加上像printf这类输出信息,这样可以跟踪并打印出段错误在代码中可能出现的位置。

为了方便使用这种方法,可以使用条件编译指令#ifdef DEBUG和#endif把printf函数包起来。这样在程序编译时,如果加上-DDEBUG参数就能查看调试信息;否则不加该参数就不会显示调试信息。

2. 使用gcc和gdb

$ gcc -g segfault_1.c
$ ./a.out
Segmentation fault (core dumped)
$ gdb ./a.out
(gdb) run
(gdb) bt
(gdb) where
(gdb) list
(gdb) quit

1、仅当能确定程序一定会发生段错误的情况下使用。
2、当程序的源码可以获得的情况下,使用`-g`参数编译程序。
3、一般用于测试阶段,生产环境下gdb会有副作用:使程序运行减慢,运行不够稳定,等等。
4、即使在测试阶段,如果程序过于复杂,gdb也不能处理。

3. 使用core文件和gdb

$ ./a.out
Segmentation fault (core dumped)
$ gdb ./a.out core.xxxx
(gdb) bt
(gdb) where
(gdb) list
(gdb) quit

1、适合于在实际生成环境下调试程序的段错误(即在不用重新发生段错误的情况下重现段错误)。
2、打开core文件的大小限制(设置为非0值)。
3、当程序很复杂,core文件相当大时,该方法不可用。

1、在一些Linux版本下,默认是不产生core文件的,首先可以查看一下系统core文件的大小限制:
$ ulimit -c
0
2、可以看到默认设置情况下,本机Linux环境下发生段错误时不会自动生成core文件,下面设置下core文件的大小限制(单位为KB):
$ ulimit -c 1024
$ ulimit -c
1024
3、修改 /proc/sys/kernel/core_pattern 文件,此文件用于控制Core文件产生的文件名和所在位置,默认情况下,此文件内容只有一行内容"core"

4. 使用objdump

dmesg
objdump -d

5.1 使用catchsegv

catchsegv命令专门用来扑获段错误,它通过动态加载器(ld-linux.so)的预加载机制(PRELOAD)把一个事先写好的库(/lib/libSegFault.so)加载上,用于捕捉断错误的出错信息。

5.2 使用signal(SIGSEGV, handler)

产生段错误时会产生一个SIGSEGV信号。因此我们可以添加SIGSEGV的处理函数在程序发生段错误退出之前做一些事情,我们甚至可以使用backtrace(3)函数来保存当前栈内容。

如何避免段错误?

综上:

  1. 定义了指针后记得初始化,在使用时记得判断是否为 NULL
  2. 在使用数组时记得初始化,使用时要检查数组下标是否越界,数组元素是否存在等
  3. 在变量处理时变量的格式控制是否合理等
  4. 在free指针之前要记得进行判断

其他的就需要根据经验不断积累;另外,也务必掌握一些基本的分析和调试手段,即使在遇到新的这类问题时也知道如何应对。

参考链接:

Linux环境下段错误的产生原因及调试方法小结 #全面细致
http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html

Segmentation Fault错误原因总结
http://silencewt.github.io/2015/05/11/Segmentation-Fault%E9%94%99%E8%AF%AF%E5%8E%9F%E5%9B%A0%E6%80%BB%E7%BB%93/

segmentation fault
https://stackoverflow.com/questions/tagged/segmentation-fault

段错误(Segmentation fault)产生的原因以及调试方法
http://www.wtango.com/%E6%AE%B5%E9%94%99%E8%AF%AFsegmentation-fault%E4%BA%A7%E7%94%9F%E7%9A%84%E5%8E%9F%E5%9B%A0%E4%BB%A5%E5%8F%8A%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95/

Linux 段错误详解 #超级给力
http://tinylab.org/explore-linux-segmentation-fault/

Segmentation Fault in Linux 原因与避免
http://www.cnblogs.com/no7dw/archive/2013/02/20/2918372.html

https://en.wikipedia.org/wiki/Segmentation_fault

Linux上Core Dump文件的形成和分析
http://baidutech.blog.51cto.com/4114344/904419

段错误调试神器 – Core Dump详解
http://www.embeddedlinux.org.cn/html/jishuzixun/201307/08-2594.html

深入探索Linux coredump调试技巧
http://blog.csdn.net/forever_feng/article/details/4676420

=END=


《 “Linux下的「Segmentation fault (core dumped)」” 》 有 9 条评论

  1. memcpy滥用导致的「segmentation fault」
    https://stackoverflow.com/questions/8703948/memcpy-function-in-c
    `
    int main()
    {
    char *t = “Working on RedHat Linux”;
    char *s;

    s = malloc (8000 * sizeof(char));
    memcpy(s, t, 7000);
    // memcpy(s, t, strlen(t) + 1); // 这样就没有问题
    printf(“s = %s\nt = %s\n”,s,t);
    free(s);
    }
    `
    以上代码出现段错误的主要原因在于 指针t 指向的区域大小小于 7000 字节,但是 memcpy() 还是会去读取 指针t 指向字符串内容之后的内容(可能是不可读区域),所以引发段错误。
    建议在使用 memcpy() 时,源和目的都必须是可读的,同时最好也指定一个长度,防止越界。

回复 a-z 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注