常見的Linux下的段錯誤 及解決辦法


一、段錯誤
 
      所謂的段錯誤就是指訪問的內存超出了系統所給這個程序的內存空間,通常這個值是由gdtr來保存的,他是一個48位的寄存器,其中的32位是保存由它指向的gdt表,后13位保存相應於gdt的下標,最后3位包括了程序是否在內存中以及程序的在cpu中的運行級別,指向的gdt是由以64位為一個單位的表,在這張表中就保存着程序運行的代碼段以及數據段的起始地址以及與此相應的段限和頁面交換還有程序運行級別還有內存粒度等等的信息。一旦一個程序發生了越界訪問,cpu就會產生相應的異常保護,於是segmentation fault就出現了。
 
二、原因
在編程中以下幾類做法容易導致段錯誤,基本是是錯誤地使用指針引起的

1)訪問系統數據區,尤其是往 系統保護的內存地址寫數據最常見就是給一個指針以0地址
2)內存越界(數組越界,變量類型不一致等) 訪問到不屬於你的內存區域
3)其他

例如:

<1>定義了指針后記得初始化,在使用的時候記得判斷是否為NULL

<2>在使用數組的時候是否被初始化,數組下標是否越界,數組元素是否存在等

<3>在變量處理的時候變量的格式控制是否合理等
 
三、解決方法

我們在用C/C++語言寫程序的時侯,內存管理的絕大部分工作都是需要我們來做的。
如何快速定位這些"段錯誤"的語句
/* Dummy.c 
* 測試訪問地址為0的內存區域所發生的端錯誤
*/
#include<stdio.h>

void Dummy(void); 

int
main(void)
{
     Dummy(); 
        
     return 0;
}//main

void Dummy(void) 
{
     //訪問地址為0的內存區域
     unsigned char* pstr = 0x00;
     *pstr = 0x00;
}
嘗試操作地址為0的內存區域,而這個內存區域通常是不可訪問的禁區,出錯了。編譯運行:
$ ./a.out
段錯誤,如圖:
1.利用gdb逐步查找段錯誤:
需要一個帶有調試信息的可執行程序,加上“-g -rdynamic"的參數進行編譯,然后用gdb調試運行這個新編譯的程序,具體步驟如下:
$ gcc -g -rdynamic d.c
$ 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: /a.out

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

不用一步步調試就找到了出錯位置d.c文件的第4行。

發現進程是由於收到了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文件的生成,
將系統的core文件的大小限制在512K大小
$ ulimit -c 0
$ ulimit -c 1000
$ ulimit -c 1000
$ ./a.out
段錯誤 (core dumped)
$ ls
a.out    core    d.c    f.c    g.c    pango.c    test_iconv.c    test_regex.c
core文件終於產生了,gdb調試:
$ 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_ () at d.c:4
4                             *ptr = 0x00;
windows系統下的ie的,有時打開某些網頁,會出現“運行時錯誤”,這個時侯如果恰好你的機器上又裝有windows的編譯器的話,
它會彈出來一個對話框,問你是否進行調試,如果你選擇是,編譯器將被打開,並進入調試狀態,開始調試。
Linux下如何做到這些?讓它在SIGSEGV的handler中調用gdb,於是第三個方法又誕生了:
3.段錯誤時啟動調試:
 
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<signal.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)] = '\0')
    buf[strlen(buf) - 1] = '\0';
  snprintf(cmd, sizeof(cmd),
    "gdb %s %d", buf, getpid());
  system(cmd);
  exit(0);
}//dump

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

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

  return 0;
}//main
編譯運行效果如下:
$ gcc -g -rdynamic f.c
$ ./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_ () at f.c:31
#6 0x08048886 in main () at f.c:38
以上方法都是在系統上有gdb的前提下進行的,如果沒有呢?其實glibc提供了此類能夠dump棧內容的函數簇,
詳見/usr/include/execinfo.h(這些函數都沒有提供man page,難怪我們找不到),另外也可以通過gnu的手冊進行學習。
4.利用backtrace和objdump進行分析:
重寫的代碼如下:
#i nclude <execinfo.h>
#i nclude <stdio.h>
#i nclude <stdlib.h>
#i nclude <signal.h>


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

void dump(int signo)
{
void *array[10];
size_t size;
char **strings;
size_t i;

size = backtrace (array, 10);
strings = backtrace_symbols (array, size);

printf ("Obtained %zd stack s.\n", size);

for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);

free (strings);

exit(0);
}

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

return 0;
}

編譯運行結果如下:
$ gcc -g -rdynamic g.c
$ ./a.out
Obtained 5 stack s.
./a.out(dump+0x19) [0x80486c2]
[0xffffe420]
./a.out(main+0x35) [0x804876f]
/lib/libc.so.6(__libc_start_main+0xe6) [0xb7e02866]
./a.out [0x8048601]

用objdump反匯編程序,找到地址0x804876f對應的代碼位置:
$ objdump -d a.out
8048765: e8 02 fe ff ff call 804856c <signal@plt>
804876a: e8 25 ff ff ff call 8048694 <dummy_>
804876f: b8 00 00 00 00 mov $0x0,%eax
8048774: c9 leav
 
原文:http://tech.diannaodian.com/dw/lin/2012/0604/180621.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM