加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

linux查找段错误的方法

(2013-01-05 20:06:43)
标签:

linux

段错误

it

分类: 技术学习园地

linux下程序段错误排查方法

写一个简单的源程序做为举例:
#include
#include

int main()
{
    int i = 0;
    char *p = NULL;
    i++;
    strcpy(p ,"123");
    printf("i = %d\n,p = %s\n", i, p);
    return 0;
}

1.最简单的排查方法是使用gdb调试程序。
前提条件:开始调试之前,必须用程序中的调试信息编译要调试的程序。编译时添加 -g。例如gcc -g main.c -o main
调试步骤:
1)使用gdb运行程序:gdb 可执行程序路径,例如 gdb ./main。会出现如下gdb的信息等。

wd@wd-Lenovo:~/forktest/segfault$ gdb ./main
GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
...
Reading symbols from /home/wd/forktest/segfault/main...done.

2)使用run命令,执行程序:
(gdb) run

3)提示如下信息。此处打印了出错的文件,函数名和行号
Starting program: /home/wd/forktest/segfault/main

Program received signal SIGSEGV, Segmentation fault.
0x0804840d in main () at main.c:9
       strcpy(p ,"123");

4)使用bt命令,察看函数桟
(gdb) bt
#0  0x0804840d in main () at main.c:9


2.使用gdb方式调试有时候并非很方便,因为只有使用gdb启动程序或者在程序运行过程中使用gdb去attach程序,才能体现gdb的好处。
此时的方法如下:
1)通过打印内核缓冲区的记录察看程序出错信息,查出信息如下:
wd@wd-Lenovo:~/forktest/segfault$ dmesg

main[9203]: segfault at 0 ip 0804840d sp bfad6100 error 6 in main[8048000+1000]

注释:这种信息一般都是由内存访问越界造成的,不管是用户态程序还是内核态程序访问越界都会出core, 并在系统日志里面输出一条这样的信息。这条信息的前面分别是访问越界的程序名,进程ID号,访问越界的地址以及当时进程堆栈地址等信息,比较有用的信息是最后的error number.将error number转化成二进制。
bit2: 值为1表示是用户态程序内存访问越界,值为0表示是内核态程序内存访问越界
bit1: 值为1表示是写操作导致内存访问越界,值为0表示是读操作导致内存访问越界
bit0: 值为1表示没有足够的权限访问非法地址的内容,值为0表示访问的非法地址根本没有对应的页面,也就是无效地址

2)反汇编可执行程序:使用objdump命令,对上面的例子反汇编后结果如下,可以将结果重定向

wd@wd-Lenovo:~/forktest/segfault$ objdump -d ./main > obj

./main:     file format elf32-i386


Disassembly of section .init:

080482b4 <_init>:
 80482b4:    53                       push   �x
 80482b5:    83 ec 08                 sub    $0x8,%esp
 80482b8:    e8 00 00 00 00           call   80482bd <_init+0x9>
 80482bd:    5b                       pop    �x
 80482be:    81 c3 37 1d 00 00        add    $0x1d37,�x
 80482c4:    8b 83 fc ff ff ff        mov    -0x4(�x),�x
 80482ca:    85 c0                    test   �x,�x
 80482cc:    74 05                    je     80482d3 <_init+0x1f>
 80482ce:    e8 3d 00 00 00           call   8048310 <__gmon_start__@plt>
 80482d3:    e8 e8 00 00 00           call   80483c0
 80482d8:    e8 e3 01 00 00           call   80484c0 <__do_global_ctors_aux>
 80482dd:    83 c4 08                 add    $0x8,%esp
 80482e0:    5b                       pop    �x
 80482e1:    c3                       ret    

Disassembly of section .plt:

080482f0 :
 80482f0:    ff 35 f8 9f 04 08        pushl  0x8049ff8
 80482f6:    ff 25 fc 9f 04 08        jmp    *0x8049ffc
 80482fc:    00 00                    add    %al,(�x)
    ...

08048300 :
 8048300:    ff 25 00 a0 04 08        jmp    *0x804a000
 8048306:    68 00 00 00 00           push   $0x0
 804830b:    e9 e0 ff ff ff           jmp    80482f0 <_init+0x3c>

08048310 <__gmon_start__@plt>:
 8048310:    ff 25 04 a0 04 08        jmp    *0x804a004
 8048316:    68 08 00 00 00           push   $0x8
 804831b:    e9 d0 ff ff ff           jmp    80482f0 <_init+0x3c>

08048320 <__libc_start_main@plt>:
 8048320:    ff 25 08 a0 04 08        jmp    *0x804a008
 8048326:    68 10 00 00 00           push   $0x10
 804832b:    e9 c0 ff ff ff           jmp    80482f0 <_init+0x3c>

Disassembly of section .text:

08048330 <_start>:
 8048330:    31 ed                    xor    �p,�p
 8048332:    5e                       pop    %esi
 8048333:    89 e1                    mov    %esp,�x
 8048335:    83 e4 f0                 and    $0xfffffff0,%esp
 8048338:    50                       push   �x
 8048339:    54                       push   %esp
 804833a:    52                       push   �x
 804833b:    68 b0 84 04 08           push   $0x80484b0
 8048340:    68 40 84 04 08           push   $0x8048440
 8048345:    51                       push   �x
 8048346:    56                       push   %esi
 8048347:    68 e4 83 04 08           push   $0x80483e4
 804834c:    e8 cf ff ff ff           call   8048320 <__libc_start_main@plt>
 8048351:    f4                       hlt    
 8048352:    90                       nop
 8048353:    90                       nop
 8048354:    90                       nop
 8048355:    90                       nop
 8048356:    90                       nop
 8048357:    90                       nop
 8048358:    90                       nop
 8048359:    90                       nop
 804835a:    90                       nop
 804835b:    90                       nop
 804835c:    90                       nop
 804835d:    90                       nop
 804835e:    90                       nop
 804835f:    90                       nop

08048360 <__do_global_dtors_aux>:
 8048360:    55                       push   �p
 8048361:    89 e5                    mov    %esp,�p
 8048363:    53                       push   �x
 8048364:    83 ec 04                 sub    $0x4,%esp
 8048367:    80 3d 14 a0 04 08 00     cmpb   $0x0,0x804a014
 804836e:    75 3f                    jne    80483af <__do_global_dtors_aux+0x4f>
 8048370:    a1 18 a0 04 08           mov    0x804a018,�x
 8048375:    bb 20 9f 04 08           mov    $0x8049f20,�x
 804837a:    81 eb 1c 9f 04 08        sub    $0x8049f1c,�x
 8048380:    c1 fb 02                 sar    $0x2,�x
 8048383:    83 eb 01                 sub    $0x1,�x
 8048386:    39 d8                    cmp    �x,�x
 8048388:    73 1e                    jae    80483a8 <__do_global_dtors_aux+0x48>
 804838a:    8d b6 00 00 00 00        lea    0x0(%esi),%esi
 8048390:    83 c0 01                 add    $0x1,�x
 8048393:    a3 18 a0 04 08           mov    �x,0x804a018
 8048398:    ff 14 85 1c 9f 04 08     call   *0x8049f1c(,�x,4)
 804839f:    a1 18 a0 04 08           mov    0x804a018,�x
 80483a4:    39 d8                    cmp    �x,�x
 80483a6:    72 e8                    jb     8048390 <__do_global_dtors_aux+0x30>
 80483a8:    c6 05 14 a0 04 08 01     movb   $0x1,0x804a014
 80483af:    83 c4 04                 add    $0x4,%esp
 80483b2:    5b                       pop    �x
 80483b3:    5d                       pop    �p
 80483b4:    c3                       ret    
 80483b5:    8d 74 26 00              lea    0x0(%esi,%eiz,1),%esi
 80483b9:    8d bc 27 00 00 00 00     lea    0x0(�i,%eiz,1),�i

080483c0 :
 80483c0:    55                       push   �p
 80483c1:    89 e5                    mov    %esp,�p
 80483c3:    83 ec 18                 sub    $0x18,%esp
 80483c6:    a1 24 9f 04 08           mov    0x8049f24,�x
 80483cb:    85 c0                    test   �x,�x
 80483cd:    74 12                    je     80483e1
 80483cf:    b8 00 00 00 00           mov    $0x0,�x
 80483d4:    85 c0                    test   �x,�x
 80483d6:    74 09                    je     80483e1
 80483d8:    c7 04 24 24 9f 04 08     movl   $0x8049f24,(%esp)
 80483df:    ff d0                    call   *�x
 80483e1:    c9                       leave  
 80483e2:    c3                       ret    
 80483e3:    90                       nop

080483e4 :
 80483e4:    55                       push   �p
 80483e5:    89 e5                    mov    %esp,�p
 80483e7:    83 e4 f0                 and    $0xfffffff0,%esp
 80483ea:    83 ec 20                 sub    $0x20,%esp
 80483ed:    c7 44 24 18 00 00 00     movl   $0x0,0x18(%esp)
 80483f4:    00
 80483f5:    c7 44 24 1c 00 00 00     movl   $0x0,0x1c(%esp)
 80483fc:    00
 80483fd:    83 44 24 18 01           addl   $0x1,0x18(%esp)
 8048402:    ba 10 85 04 08           mov    $0x8048510,�x
 8048407:    8b 44 24 1c              mov    0x1c(%esp),�x
 804840b:    8b 12                    mov    (�x),�x
 804840d:    89 10                    mov    �x,(�x)
 804840f:    b8 14 85 04 08           mov    $0x8048514,�x
 8048414:    8b 54 24 1c              mov    0x1c(%esp),�x
 8048418:    89 54 24 08              mov    �x,0x8(%esp)
 804841c:    8b 54 24 18              mov    0x18(%esp),�x
 8048420:    89 54 24 04              mov    �x,0x4(%esp)
 8048424:    89 04 24                 mov    �x,(%esp)
 8048427:    e8 d4 fe ff ff           call   8048300
 804842c:    b8 00 00 00 00           mov    $0x0,�x
 8048431:    c9                       leave  
 8048432:    c3                       ret    
 8048433:    90                       nop
 8048434:    90                       nop
 8048435:    90                       nop
 8048436:    90                       nop
 8048437:    90                       nop
 8048438:    90                       nop
 8048439:    90                       nop
 804843a:    90                       nop
 804843b:    90                       nop
 804843c:    90                       nop
 804843d:    90                       nop
 804843e:    90                       nop
 804843f:    90                       nop

08048440 <__libc_csu_init>:
 8048440:    55                       push   �p
 8048441:    57                       push   �i
 8048442:    56                       push   %esi
 8048443:    53                       push   �x
 8048444:    e8 69 00 00 00           call   80484b2 <__i686.get_pc_thunk.bx>
 8048449:    81 c3 ab 1b 00 00        add    $0x1bab,�x
 804844f:    83 ec 1c                 sub    $0x1c,%esp
 8048452:    8b 6c 24 30              mov    0x30(%esp),�p
 8048456:    8d bb 20 ff ff ff        lea    -0xe0(�x),�i
 804845c:    e8 53 fe ff ff           call   80482b4 <_init>
 8048461:    8d 83 20 ff ff ff        lea    -0xe0(�x),�x
 8048467:    29 c7                    sub    �x,�i
 8048469:    c1 ff 02                 sar    $0x2,�i
 804846c:    85 ff                    test   �i,�i
 804846e:    74 29                    je     8048499 <__libc_csu_init+0x59>
 8048470:    31 f6                    xor    %esi,%esi
 8048472:    8d b6 00 00 00 00        lea    0x0(%esi),%esi
 8048478:    8b 44 24 38              mov    0x38(%esp),�x
 804847c:    89 2c 24                 mov    �p,(%esp)
 804847f:    89 44 24 08              mov    �x,0x8(%esp)
 8048483:    8b 44 24 34              mov    0x34(%esp),�x
 8048487:    89 44 24 04              mov    �x,0x4(%esp)
 804848b:    ff 94 b3 20 ff ff ff     call   *-0xe0(�x,%esi,4)
 8048492:    83 c6 01                 add    $0x1,%esi
 8048495:    39 fe                    cmp    �i,%esi
 8048497:    75 df                    jne    8048478 <__libc_csu_init+0x38>
 8048499:    83 c4 1c                 add    $0x1c,%esp
 804849c:    5b                       pop    �x
 804849d:    5e                       pop    %esi
 804849e:    5f                       pop    �i
 804849f:    5d                       pop    �p
 80484a0:    c3                       ret    
 80484a1:    eb 0d                    jmp    80484b0 <__libc_csu_fini>
 80484a3:    90                       nop
 80484a4:    90                       nop
 80484a5:    90                       nop
 80484a6:    90                       nop
 80484a7:    90                       nop
 80484a8:    90                       nop
 80484a9:    90                       nop
 80484aa:    90                       nop
 80484ab:    90                       nop
 80484ac:    90                       nop
 80484ad:    90                       nop
 80484ae:    90                       nop
 80484af:    90                       nop

080484b0 <__libc_csu_fini>:
 80484b0:    f3 c3                    repz ret

080484b2 <__i686.get_pc_thunk.bx>:
 80484b2:    8b 1c 24                 mov    (%esp),�x
 80484b5:    c3                       ret    
 80484b6:    90                       nop
 80484b7:    90                       nop
 80484b8:    90                       nop
 80484b9:    90                       nop
 80484ba:    90                       nop
 80484bb:    90                       nop
 80484bc:    90                       nop
 80484bd:    90                       nop
 80484be:    90                       nop
 80484bf:    90                       nop

080484c0 <__do_global_ctors_aux>:
 80484c0:    55                       push   �p
 80484c1:    89 e5                    mov    %esp,�p
 80484c3:    53                       push   �x
 80484c4:    83 ec 04                 sub    $0x4,%esp
 80484c7:    a1 14 9f 04 08           mov    0x8049f14,�x
 80484cc:    83 f8 ff                 cmp    $0xffffffff,�x
 80484cf:    74 13                    je     80484e4 <__do_global_ctors_aux+0x24>
 80484d1:    bb 14 9f 04 08           mov    $0x8049f14,�x
 80484d6:    66 90                    xchg   %ax,%ax
 80484d8:    83 eb 04                 sub    $0x4,�x
 80484db:    ff d0                    call   *�x
 80484dd:    8b 03                    mov    (�x),�x
 80484df:    83 f8 ff                 cmp    $0xffffffff,�x
 80484e2:    75 f4                    jne    80484d8 <__do_global_ctors_aux+0x18>
 80484e4:    83 c4 04                 add    $0x4,%esp
 80484e7:    5b                       pop    �x
 80484e8:    5d                       pop    �p
 80484e9:    c3                       ret    
 80484ea:    90                       nop
 80484eb:    90                       nop

Disassembly of section .fini:

080484ec <_fini>:
 80484ec:    53                       push   �x
 80484ed:    83 ec 08                 sub    $0x8,%esp
 80484f0:    e8 00 00 00 00           call   80484f5 <_fini+0x9>
 80484f5:    5b                       pop    �x
 80484f6:    81 c3 ff 1a 00 00        add    $0x1aff,�x
 80484fc:    e8 5f fe ff ff           call   8048360 <__do_global_dtors_aux>
 8048501:    83 c4 08                 add    $0x8,%esp
 8048504:    5b                       pop    �x
 8048505:    c3                       ret    

3) 在obj文件中查找804840d进行定位。


3.在程序出错时打印出函数的调用堆栈。
调用系统函数:backtrace()、backtrace_symbols()和backtrace_symbols_fd()。修改后的main.c如下
#include <signal.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

void sigdump(int s)
{
    void *array[10];

    size_t size;
    char **strings;
    size_t i;

    size = backtrace (array, 10);
    strings = backtrace_symbols (array, size);
    printf ("Obtained %zd stack frames.\n", size);
    for (i = 0; i < size; i++)
        printf ("%s\n", strings[i]);
    free (strings);
    exit(0);
}

int main()
{
    int i = 0;
    char *p = NULL;
    signal(SIGSEGV, (__sighandler_t)sigdump);
    i++;
    strcpy(p ,"123");
    printf("i = %d\n,p = %s\n", i, p);
    return 0;

}

执行结果如下:
wd@wd-Lenovo:~/forktest/segfault$ ./main
Obtained 5 stack frames.
./main() [0x804853d]
[0xb77a4400]
./main() [0x80485e5]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3) [0xb76054d3]
./main() [0x8048491]

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有