• <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>

            Networking /C++/Linux

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              11 Posts :: 14 Stories :: 1 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(4)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            用gdb調(diào)試動態(tài)鏈接庫

                大家都知道在 Linux 可以用 gdb 來調(diào)試應(yīng)用程序,當(dāng)然前提是用 gcc 編譯程序時要加上 -g 參數(shù)。

                我這篇文章里將討論一下用 gdb 來調(diào)試動態(tài)鏈接庫的問題。

                首先,假設(shè)我們準備這樣的一個動態(tài)鏈接庫:

                引用:

                庫名稱是: ggg

                動態(tài)鏈接庫文件名是: libggg.so

                頭文件是: get.h

                提供這樣兩個函數(shù)調(diào)用接口:

                 int get ();

                 int set (int a);

                

                要生成這樣一個動態(tài)鏈接庫,我們首先編寫這樣一個頭文件:

                /************關(guān)于本文檔********************************************

                *filename: get.h

                *********************************************************************/

                int get ();

                int set (int a);

                然后準備這樣一個生成動態(tài)鏈接庫的源文件:

                /************關(guān)于本文檔********************************************

                *filename: get.cpp

                *********************************************************************/

                #include

                #include "get.h"

                static int x=0;

                int get ()

                {

                 printf ("get x=%d\n", x);

                 return x;

                }

                int set (int a)

                {

                 printf ("set a=%d\n", a);

                 x = a;

                 return x;

                }

                然后我們用 GNU 的 C/C++ 編譯器來生成動態(tài)鏈接庫,編譯命令如下:

                引用:

                g++ get.cpp -shared -g -DDEBUG -o libggg.so

                這樣我們就準備好了動態(tài)鏈接庫了,下面我們編寫一個應(yīng)用程序來調(diào)用此動態(tài)鏈接庫,源代碼如下:

                /************關(guān)于本文檔********************************************

                *filename: pk.cpp

                *********************************************************************/

                #include

                #include "get.h"

                int main (int argc, char** argv)

                {

                 int a = 100;

                 int b = get ();

                 int c = set (a);

                 int d = get ();

                 printf ("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

                 return 0;

                }

                編譯此程序用下列命令,如果已經(jīng)把上面生成的 libggg.so 放到了庫文件搜索路徑指定的文件目錄,比如 /lib 或 /usr/lib 之類的,就用下面這條命令:

                引用:

                g++ pk.cpp -o app -Wall -g -lggg

                否則就用下面這條命令:

                引用:

                g++ pk.cpp -o app -Wall -g -lggg -L`pwd`

                下面我們就開始調(diào)試上面命令生成的 app 程序吧。如果已經(jīng)把上面生成的 libggg.so 放到了庫文件搜索路徑指定的文件目錄,比如 /lib 或 /usr/lib 之類的,調(diào)試就順利完成,如下:

                引用:

                #gdb ./app

                GNU gdb 6.4-debian

                Copyright 2005 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 "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

                (gdb) b main /* 這是在程序的 main 處設(shè)置斷點 */

                Breakpoint 1 at 0x804853c: file pk.cpp, line 7.

                (gdb) b set /* 這是在程序的 set 處設(shè)置斷點 */

                Function "set" not defined.

                Make breakpoint pending on future shared library load? (y or [n]) y /* 這里必須選擇 y 調(diào)試程序才會跟蹤到動態(tài)鏈接庫內(nèi)部去 */

                Breakpoint 2 (set) pending.

                (gdb) run /* 開始運行我們的程序,直到遇見斷點時暫停 */

                Starting program: /data/example/c/app

                Breakpoint 3 at 0xb7f665f8: file get.cpp, line 11.

                Pending breakpoint "set" resolved

                Breakpoint 1, main (argc=1, argv=0xbf990504) at pk.cpp:7

                7 int a = 100;

                (gdb) n /* 繼續(xù)執(zhí)行程序的下一行代碼 */

                8 int b = get ();

                (gdb) n /* 程序執(zhí)行到了我們斷點所在的動態(tài)鏈接庫了 */

                get x=0

                9 int c = set (a);

                (gdb) n

                Breakpoint 3, set (a=100) at get.cpp:11

                11 printf ("set a=%d\n", a);

                (gdb) list /* 查看當(dāng)前代碼行周圍的代碼,證明我們已經(jīng)跟蹤到動態(tài)鏈接庫的源代碼里面了 */

                6 printf ("get x=%d\n", x);

                7 return x;

                8 }

                9 int set (int a)

                10 {

                11 printf ("set a=%d\n", a);

                12 x = a;

                13 return x;

                14 }

                (gdb) n

                set a=100

                12 x = a;

                (gdb) n

                13 return x;

                (gdb) n

                14 }

                (gdb) n

                main (argc=1, argv=0xbf990504) at pk.cpp:10

                10 int d = get ();

                (gdb) n

                get x=100

                11 printf ("a=%d,b=%d,c=%d,d=%d\n",a,b,c,d);

                (gdb) n

                a=100,b=0,c=100,d=100

                12 return 0;

                (gdb) c

                Continuing.

                Program exited normally.

                (gdb) quit /* 程序順利執(zhí)行結(jié)束 */

                如果我們沒有把動態(tài)鏈接庫放到指定目錄,比如/lib里面,調(diào)試就會失敗,過程如下:

                引用:

                # gdb ./app

                GNU gdb 6.4-debian

                Copyright 2005 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 "i486-linux-gnu"...Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1".

                (gdb) b main

                Breakpoint 1 at 0x804853c: file pk.cpp, line 7.

                (gdb) b set

                Function "set" not defined.

                Make breakpoint pending on future shared library load? (y or [n]) y

                Breakpoint 2 (set) pending.

                (gdb) run /* 雖然調(diào)試操作都一樣,但程序執(zhí)行失敗 */

                Starting program: /data/example/c/app

                /data/example/c/app: error while loading shared libraries: libggg.so: cannot open shared object file: No such file or directory

                Program exited with code 0177.

                (gdb) quit
            http://os.chinaunix.net/a2007/0830/976/000000976152.shtml

            posted on 2011-12-15 10:56 likun 閱讀(465) 評論(0)  編輯 收藏 引用 所屬分類: DEBUG
            久久这里只精品国产99热| 久久精品成人| 国产亚洲婷婷香蕉久久精品| 久久精品国产一区| 一级做a爰片久久毛片看看| 99精品久久久久久久婷婷| 久久香蕉国产线看观看乱码| 中文字幕无码久久久| 精品久久久久久中文字幕| 亚洲精品97久久中文字幕无码 | 久久夜色精品国产亚洲| 久久精品亚洲AV久久久无码| 成人国内精品久久久久影院| 欧美久久久久久| 久久久精品人妻无码专区不卡| 亚洲狠狠婷婷综合久久久久 | 国产精品久久久久…| 久久久久久久91精品免费观看| 久久美女网站免费| 99久久精品午夜一区二区| 久久精品国产亚洲av麻豆图片| 国产精品99久久久久久猫咪| 99久久超碰中文字幕伊人| 亚洲精品无码成人片久久| 久久婷婷色综合一区二区| 欧美与黑人午夜性猛交久久久| 久久美女网站免费| 伊人久久大香线蕉精品| 中文精品久久久久国产网址 | 超级97碰碰碰碰久久久久最新 | 国内精品久久久久影院薰衣草 | 亚洲色欲久久久久综合网| 久久激情亚洲精品无码?V| 国产精品欧美亚洲韩国日本久久| 免费观看成人久久网免费观看| 日韩欧美亚洲综合久久影院d3| 精品久久久久久久久中文字幕| 一本大道久久a久久精品综合| 欧美久久综合性欧美| 久久国产免费直播| 国产免费久久精品99re丫y|