zoukankan      html  css  js  c++  java
  • linux Segmentation faults 段错误详解

    什么是段错误

    下面是来自 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 就出现了

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

    常见段错误举例

    这里列举一下常见的段错误例子。

    1 scanf 参数:把 &i 写为 i

    1. int i;
    2. scanf("%d", i);

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

    2 sprintf/printf 参数:%d/%c 写成 %s

    1. int i = 10;
    2. printf("%s", i);

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

    3 数组访问越界

    1. char test[1];
    2. printf("%c", test[1000000000]);

    注:也可能报告为 Bus Error,可能存在对未对齐的地址读或写。

    4 写只读内存

    1. char *ptr = "test";
    2. strcpy(ptr, "TEST");

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

    5 堆栈溢出

    1. void main()
    2. {
    3. main();
    4. }

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

    6 pthread_create() 失败后 pthread_join()

    1. #define THREAD_MAX_NUM
    2. pthread_t thread[THREAD_MAX_NUM];

    分析:用 pthread_create() 创建了各个线程,然后用 pthread_join() 来等待线程的结束。刚开始直接等待,在创建线程都成功时,pthread_join() 能顺利等到各个线程结束,但是一旦创建线程失败,用 pthread_join() 来等待那个本不存在的线程时自然会存在未知内存的情况,从而导致段错误的发生。解决办法是:在创建线程之前,先初始化线程数组,在等待线程结束时,判断线程是否为初始值,如果是的话,说明线程并没有创建成功,所以就不能等拉。

    7 小结

    综上所有例子,

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

    其他的就需要根据经验不断积累,更多例子会不断追加到上述列表中。

    另外,也务必掌握一些基本的分析和调试手段,即使在遇到新的这类问题时也知道如何应对。

    5 分析和调试手段

    分析方法除了最简便的 catchsegv 外,还有诸多办法,它们的应用场景各异。

    5.1 catchsegv 原理

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

    5.2 gdb 调试

    1. gdb ./segfault-scanf
    2. ...
    3. Reading symbols from ./segfault-scanf...done.
    4. (gdb) r
    5. Starting program: segfault-scanf
    6. 100
    7. Program received signal SIGSEGV, Segmentation fault.
    8. 0x00007ffff7a6b61a in _IO_vfscanf_internal (s=<optimized out>,
    9. format=<optimized out>, argptr=argptr@entry=0x7fffffffddc8,
    10. errp=errp@entry=0x0) at vfscanf.c:1857
    11. 1857 vfscanf.c: No such file or directory.
    12. (gdb) bt
    13. #0 0x00007ffff7a6b61a in _IO_vfscanf_internal (s=<optimized out>,
    14. format=<optimized out>, argptr=argptr@entry=0x7fffffffddc8,
    15. errp=errp@entry=0x0) at vfscanf.c:1857
    16. #1 0x00007ffff7a72399 in __isoc99_scanf (format=<optimized out>)
    17. at isoc99_scanf.c:37
    18. #2 0x0000000000400580 in main ()

    5.3 coredump 分析

    1. $ ulimit -c 1024
    2. $ gdb segfault-scanf ./core
    3. Reading symbols from segfault-scanf...done.
    4. [New LWP 16913]
    5. Core was generated by `./segfault-scanf'.
    6. Program terminated with signal SIGSEGV, Segmentation fault.
    7. #0 0x00007fd2d24ec61a in _IO_vfscanf_internal (s=<optimized out>,
    8. format=<optimized out>, argptr=argptr@entry=0x7fff14dfa668,
    9. errp=errp@entry=0x0) at vfscanf.c:1857
    10. 1857 vfscanf.c: No such file or directory.

    5.4 程序内捕获 SIGSEGV 信号并启动 gdb

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <signal.h>
    4. #include <string.h>
    5. void dump(int signo)
    6. {
    7. char buf[1024];
    8. char cmd[1024];
    9. FILE *fh;
    10. snprintf(buf, sizeof(buf), "/proc/%d/cmdline", getpid());
    11. if(!(fh = fopen(buf, "r")))
    12. exit(0);
    13. if(!fgets(buf, sizeof(buf), fh))
    14. exit(0);
    15. fclose(fh);
    16. if(buf[strlen(buf) - 1] == ' ')
    17. buf[strlen(buf) - 1] = '';
    18. snprintf(cmd, sizeof(cmd), "gdb %s %d", buf, getpid());
    19. system(cmd);
    20. exit(0);
    21. }
    22. int main(int argc, char *argv[])
    23. {
    24. int i;
    25. signal(SIGSEGV, &dump);
    26. scanf("%d ", i);
    27. return 0;
    28. }

    用法如下:

    1. $ gcc -g -rdynamic -o segfault-scanf segfault-scanf.c
    2. $ sudo ./segfault-scanf
    3. 100
    4. (gdb) bt
    5. #0 0x00007fb743e065cc in __libc_waitpid (pid=16988,
    6. stat_loc=stat_loc@entry=0x7fffb51d8fe0, options=options@entry=0)
    7. at ../sysdeps/unix/sysv/linux/waitpid.c:31
    8. #1 0x00007fb743d8b1d2 in do_system (line=<optimized out>)
    9. at ../sysdeps/posix/system.c:148
    10. #2 0x0000000000400ba1 in dump (signo=11) at segfault-scanf.c:21
    11. #3 <signal handler called>
    12. #4 0x00007fb743d9c61a in _IO_vfscanf_internal (s=<optimized out>,
    13. format=<optimized out>, argptr=argptr@entry=0x7fffb51da318,
    14. errp=errp@entry=0x0) at vfscanf.c:1857
    15. #5 0x00007fb743da3399 in __isoc99_scanf (format=<optimized out>)
    16. at isoc99_scanf.c:37
    17. #6 0x0000000000400bdd in main (argc=1, argv=0x7fffb51da508)
    18. at segfault-scanf.c:31

    5.5 程序内捕获 SIGSEGV 信号并调用 backtrace 获取回调

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <signal.h>
    4. #include <string.h>
    5. #include <execinfo.h>
    6. void dump(int signo)
    7. {
    8. void *array[10];
    9. size_t size;
    10. char **strings;
    11. size_t i;
    12. size = backtrace (array, 10);
    13. strings = backtrace_symbols (array, size);
    14. printf ("Obtained %zd stack frames. ", size);
    15. for (i = 0; i < size; i++)
    16. printf ("%s ", strings[i]);
    17. free (strings);
    18. exit(0);
    19. }
    20. int main(int argc, char *argv[])
    21. {
    22. int i;
    23. signal(SIGSEGV, &dump);
    24. scanf("%d ", i);
    25. return 0;
    26. }

    用法如下:

    1. $ gcc -rdynamic -o ./segfault-scanf segfault-scanf.c
    2. $ ./segfault-scanf
    3. 100
    4. Obtained 7 stack frames.
    5. ./segfault-scanf() [0x40077e]
    6. /lib/x86_64-linux-gnu/libc.so.6(+0x36c30) [0x7f249fa43c30]
    7. /lib/x86_64-linux-gnu/libc.so.6(_IO_vfscanf+0x303a) [0x7f249fa6461a]
    8. /lib/x86_64-linux-gnu/libc.so.6(__isoc99_scanf+0x109) [0x7f249fa6b399]
    9. ./segfault-scanf-call-backtrace() [0x400837]
    10. /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f249fa2eec5]
    11. ./segfault-scanf-call-backtrace() [0x400699]

    除此之外,还可以通过 dmesg 查看内核信息并通过 objdump 或者 addr2line 把 IP 地址转化为代码行,不过用法没有 catchsegv 来得简单。dmesg 获取的内核信息由 arch/x86/mm/fault.c: show_signal_msg() 打印。

    参考:

    Linux 段错误详解 - 泰晓科技 (tinylab.org)

    Linux环境下段错误的产生原因及调试方法小结_yuzeze的专栏-CSDN博客_catchsegv

  • 相关阅读:
    WIN10修改桌面路径到(其他盘)教程
    C#通过IMessageFilter捕获鼠标消息
    C# ICON和Image互转
    由于找不到 opencv_world320.dll,无法继续执行代
    HttpWebRequest.Connection的问题
    远程服务器环境调用本地JS启动摄像头功能遇见的问题
    nginx反向代理配置
    获取开源天气信息的API
    Java实现八种排序算法(代码详细解释)
    Java面试之前复习总结(二)
  • 原文地址:https://www.cnblogs.com/zhiminyu/p/14451062.html
Copyright © 2011-2022 走看看