• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 297,  comments - 15,  trackbacks - 0
            我們在用C/C++語言寫程序的時侯,內存管理的絕大部分工作都是需要我們來做的。實際上,內存管理是一個比較繁瑣的工作,無論你多高明,經驗多豐富,難 免會在此處犯些小錯誤,而通常這些錯誤又是那么的淺顯而易于消除。但是手工“除蟲”(debug),往往是效率低下且讓人厭煩的,本文將就"段錯誤"這個 內存訪問越界的錯誤談談如何快速定位這些"段錯誤"的語句。
            下面將就以下的一個存在段錯誤的程序介紹幾種調試方法:
                 1  dummy_function (void)
                 2  {
                 3          unsigned char *ptr = 0x00;
                 4          *ptr = 0x00;
                 5  }
                 6
                 7  int main (void)
                 8  {
                 9          dummy_function ();
                10
                11          return 0;
                12  }
            作為一個熟練的C/C++程序員,以上代碼的bug應該是很清楚的,因為它嘗試操作地址為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;
            (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文件。后來,憶起為了漸少系統上的拉圾文件的數量(本人有些潔癖,這也是我喜歡Gentoo的原因 之一),禁止了core文件的生成,查看了以下果真如此,將系統的core文件的大小限制在512K大小,再試:
            xiaosuo@gentux test $ ulimit -c
            0
            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
            core文件終于產生了,用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;
            哇,好歷害,還是一步就定位到了錯誤所在地,佩服一下Linux/Unix系統的此類設計。
            接著考慮下去,以前用windows系統下的ie的時侯,有時打開某些網頁,會出現“運行時錯誤”,這個時侯如果恰好你的機器上又裝有windows的編譯器的話,他會彈出來一個對話框,問你是否進行調試,如果你選擇是,編譯器將被打開,并進入調試狀態,開始調試。
            Linux下如何做到這些呢?我的大腦飛速地旋轉著,有了,讓它在SIGSEGV的handler中調用gdb,于是第三個方法又誕生了:
            3.段錯誤時啟動調試:
            #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
            怎么樣?是不是依舊很酷?
            以上方法都是在系統上有gdb的前提下進行的,如果沒有呢?其實glibc為我們提供了此類能夠dump棧內容的函數簇,詳見/usr/include/execinfo.h(這些函數都沒有提供man page,難怪我們找不到),另外你也可以通過gnu的手冊進行學習。
            4.利用backtrace和objdump進行分析:
            重寫的代碼如下:
            #include <execinfo.h>
            #include <stdio.h>
            #include <stdlib.h>
            #include <signal.h>

            /* A dummy function to make the backtrace more interesting. */
                    void
            dummy_function (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 frames.\n", size);

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

                    free (strings);

                    exit(0);
            }

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

                    return 0;
            }
            編譯運行結果如下:
            xiaosuo@gentux test $ gcc -g -rdynamic g.c
            xiaosuo@gentux test $ ./a.out
            Obtained 5 stack frames.
            ./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對應的代碼位置:
            xiaosuo@gentux test $ objdump -d a.out

             8048765:       e8 02 fe ff ff          call   804856c <signal@plt>
             804876a:       e8 25 ff ff ff          call   8048694 <dummy_function>
             804876f:       b8 00 00 00 00          mov    $0x0,%eax
             8048774:       c9                      leave
            我們還是找到了在哪個函數(dummy_function)中出錯的,信息已然不是很完整,不過有總比沒有好的啊!
            后記:
            本文給出了分析"段錯誤"的幾種方法,不要認為這是與孔乙己先生的"回"字四種寫法一樣的哦,因為每種方法都有其自身的適用范圍和適用環境,請酌情使用,或遵醫囑。
            from:
            http://www.cublog.cn/u/5251/showart.php?id=173718
            posted on 2010-08-21 19:06 chatler 閱讀(335) 評論(0)  編輯 收藏 引用 所屬分類: Linux_Coding
            <2010年5月>
            2526272829301
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

            常用鏈接

            留言簿(10)

            隨筆分類(307)

            隨筆檔案(297)

            algorithm

            Books_Free_Online

            C++

            database

            Linux

            Linux shell

            linux socket

            misce

            • cloudward
            • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

            network

            OSS

            • Google Android
            • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
            • os161 file list

            overall

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品免费网站网| 国产成人香蕉久久久久| 亚洲国产精品久久久久婷婷老年| 久久久久高潮综合影院| 日韩va亚洲va欧美va久久| 久久高潮一级毛片免费| 久久久高清免费视频| 久久精品国产亚洲av高清漫画| 777米奇久久最新地址| 久久精品国产精品亚洲人人| 色婷婷狠狠久久综合五月| 久久综合亚洲鲁鲁五月天| 精品久久久久国产免费| 久久亚洲精品国产精品婷婷| 亚洲成色WWW久久网站| 亚洲国产成人久久精品99| 久久噜噜电影你懂的| 久久精品国产亚洲av日韩| 国产精品九九久久免费视频 | 精品一久久香蕉国产线看播放 | 久久精品无码一区二区三区免费 | 91麻豆精品国产91久久久久久| 欧美成a人片免费看久久| 狠狠色丁香婷婷久久综合不卡| 人妻无码αv中文字幕久久| 久久经典免费视频| 国产免费福利体检区久久 | 国产午夜福利精品久久2021| 久久久久亚洲av成人网人人软件 | 国内精品综合久久久40p| 久久久久久精品成人免费图片| 一级做a爰片久久毛片人呢| 日产精品久久久久久久性色| 亚洲欧美久久久久9999| 久久久久久无码国产精品中文字幕| 精品久久久久久久| 青青草原综合久久大伊人精品| 久久久无码一区二区三区| 亚洲狠狠婷婷综合久久久久| 久久久久波多野结衣高潮| 久久国产亚洲精品|