美文网首页
Linux段错误调试

Linux段错误调试

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

  • 原因
    1 . 访问系统数据区,尤其是往 系统保护的内存地址写数据
    最常见就是给一个指针以0地址
    2 . 内存越界(数组越界,变量类型不一致等) 访问到不属于你的内存区域

  • 解决方案
    下面将就以下的一个存在段错误的程序介绍几种调试方法:

dummy_function (void)
    {
              unsigned char *ptr = 0x00;
              *ptr = 0x00;
      }
      int main (void)
      {
              dummy_function ();

              return 0;
      }

因为它尝试操作地址为0的内存区域,而这个内存区域通常是不可访问的禁区,当然就会出错了。我们尝试编译运行它:
xiaosuo@gentux test $ ./a.out
段错误
果然不出所料,它出错并退出了。

1 . 利用gdb逐步查找段错误:
首先我们需要一个带有调试信息的可执行程序,所以我们加上“-g -rdynamic"的参数进行编译,然后用gdb调试运行这个新编译的程序,具体步骤如下:

xiaosuo@gentux test $ gcc -g -rdynamic d.c
xiaosuo@gentux test $ gdb ./a.out
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

(gdb) r
Starting program: /home/xiaosuo/test/a.out

Program received signal SIGSEGV, Segmentation fault.
0x08048524 in dummy_function () at d.c:4
4               *ptr = 0x00;

从这里我们还发现进程是由于收到了SIGSEGV信号而结束的。通过进一步的查阅文档(man 7 signal),我们知道SIGSEGV默认handler的动作是打印”段错误"的出错信息,并产生Core文件,由此我们又产生了方法二。
2 . 分析Core文件:
Core文件:The default action of certain signals is to cause a process to terminate and produce a core dump file, a disk file containing an image of the process's memory at the time of termination. A list of the signals which cause a process to dump core can be found in signal(7).
以 上资料摘自man page(man 5 core)

设置core文件大小为1000,再次尝试,core文件终于产生了

xiaosuo@gentux test $ ulimit -c 1000
xiaosuo@gentux test $ ulimit -c
1000
xiaosuo@gentux test $ ./a.out
段错误 (core dumped)
xiaosuo@gentux test $ ls
a.out  core  d.c  f.c  g.c  pango.c  test_iconv.c  test_regex.c

用gdb调试一下

xiaosuo@gentux test $ gdb ./a.out core
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".


warning: Can't read pathname for load map: 输入/输出错误.
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
#0  0x08048524 in dummy_function () at d.c:4
4               *ptr = 0x00;

3 . 在SIGSEGV的handler中调用gdb

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

void dump(int signo)
{
        char buf[1024];
        char cmd[1024];
        FILE *fh;

        snprintf(buf, sizeof(buf), "/proc/%d/cmdline", getpid());
        if(!(fh = fopen(buf, "r")))
                exit(0);
        if(!fgets(buf, sizeof(buf), fh))
                exit(0);
        fclose(fh);
        if(buf[strlen(buf) - 1] == '\n')
                buf[strlen(buf) - 1] = '\0';
        snprintf(cmd, sizeof(cmd), "gdb %s %d", buf, getpid());
        system(cmd);

        exit(0);
}

        void
dummy_function (void)
{
        unsigned char *ptr = 0x00;
        *ptr = 0x00;
}

        int
main (void)
{
        signal(SIGSEGV, &dump);
        dummy_function ();

        return 0;
}

编译运行效果如下:

xiaosuo@gentux test $ gcc -g -rdynamic f.c
xiaosuo@gentux test $ ./a.out
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

Attaching to program: /home/xiaosuo/test/a.out, process 9563
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
0xffffe410 in __kernel_vsyscall ()
(gdb) bt
#0  0xffffe410 in __kernel_vsyscall ()
#1  0xb7ee4b53 in waitpid () from /lib/libc.so.6
#2  0xb7e925c9 in strtold_l () from /lib/libc.so.6
#3  0x08048830 in dump (signo=11) at f.c:22
#4  <signal handler called>
#5  0x0804884c in dummy_function () at f.c:31
#6  0x08048886 in main () at f.c:38

相关文章

  • Linux段错误调试

    原理:段错误就是指访问的内存超出了系统所给这个程序的内存空间,通常这个值是由gdtr来保存的,他是一个48位的寄存...

  • 扣丁学堂Linux开发解析如何得到一个段错误的核心转储

    在现如今,大多数程序开发都会涉及到程序调试一个段错误,比如在Linux开发培训中,如何得到一个段错误的核心转储呢?...

  • 2020-09-19

    Linux学习2 gdb调试工具: 检查逻辑错误 基础指令:-g:使用该参数编译可以执行文件,得到调试表。 其他...

  • Android No target device found

    问题描述 Android Studio 3.2.1 Linux版本中进行真机调试,在运行app的时候出现错误:no...

  • python调试

    linux python调试技巧 Linux下Python基础调试 http://blog.163.com/liu...

  • linux c 段错误调查

    在linux c开发中经常会遇到各种问题,的确很令人头疼,下面总结一下我用到的调查方法: 1.段错误---gdb ...

  • 调试python的段错误方法

      最近搞漏洞挖掘的时候,发现python会出现Segmentation fault,研究下发现是调用C++的程序...

  • 解决java调试工具无法连接jvm

    1.现象在Linux服务器执行jstack pid查看java堆栈信息时提示错误,无法连接上jvm调试工具 2.原...

  • linux编程入门(七)-使用gdb调试程序

    程序开发离不开调试,可以断点调试,也可以打log调试,linux下断点调试c,c++程序用gdb。 断点调试虽然很...

  • PWN入门到放弃

    Linux下的pwn常用到的工具有: gdb:Linux调试中必要用到的 gdb-peda:gdb方便调试的工具,...

网友评论

      本文标题:Linux段错误调试

      本文链接:https://www.haomeiwen.com/subject/cwxecctx.html