Linux伊甸园首页

控制面板 自由新闻 自由软件 自由文档 自由论坛 自由商城 联系我们
我的收藏 推荐文章 会员登陆 最后更新 高级搜索 退出登陆
新闻动态
新手入门
技术前沿
系统管理
网络管理
使用经验
编程开发
系统安全
解决方案
硬件相关
Unix家族
数据库类
观点评论
人物介绍



Linuxeden.com-- Linuxeden 新闻 文档 资料 教程 Linux伊甸园 / 编程开发 / 如何在 Linux 下调试动态链接库
如何在 Linux 下调试动态链接库  找linux工作,招linux人才,到Linuxeden人才频道
2007-04-03    articleman       点击: 2289
如何在 Linux 下调试动态链接库

如何在 Linux 下调试动态链接库

引自:

http://linux.chinaunix.net/bbs/viewthread

.php?tid=857435

转贴 - 如何在 Linux 下调试动态链接库

大家都知道在 Linux 可以用 gdb 来调试应用

程序,当然前提是用 gcc 编译程序时要加上

-g 参数。
我这篇文章里将讨论一下用 gdb 来调试动态链

接库的问题。
首先,假设我们准备这样的一个动态链接库:

QUOTE:
库名称是: ggg
动态链接库文件名是: libggg.so
头文件是: get.h
提供这样两个函数调用接口:
    int get ();
    int set (int a);

要生成这样一个动态链接库,我们首先编写这

样一个头文件:

[Copy to clipboard]
CODE:
/************关于本文档

*****************************************

***
*filename: get.h
*purpose:  一个动态链接库头文件示例
*tided by: zhoulifa(zhoulifa@163.com) 周

立发 (http://zhoulifa.9999mb.com)
Linux 爱好者 Linux 知识传播者 SOHO 族 开

发者 最擅长 C 语言
*date time: 2006-11-15 21:11:54
*Note: 任何人可以任意复制代码并运用这些文

档,当然包括你的商业用途
* 但请遵循 GPL
*Hope:希望越来越多的人贡献自己的力量,为

科学技术发展出力
* 科技站在巨人的肩膀上进步更快!感谢有开

源前辈的贡献!
*感谢 vcclass@hotmail.com 提供原始代码,

我在他的基础上整理了此文
*****************************************

****************************/
int get ();
int set (int a);

然后准备这样一个生成动态链接库的源文件:

[Copy to clipboard]
CODE:
/************关于本文档

*****************************************

***
*filename:  get.cpp
*purpose: 一个动态链接库源文件示例
*tided by: zhoulifa(zhoulifa@163.com) 周

立发 (http://zhoulifa.9999mb.com)
Linux 爱好者 Linux 知识传播者 SOHO 族 开

发者 最擅长 C 语言
*date time:2006-11-15 21:11:54
*Note: 任何人可以任意复制代码并运用这些文

档,当然包括你的商业用途
* 但请遵循 GPL
*Hope:希望越来越多的人贡献自己的力量,为

科学技术发展出力
* 科技站在巨人的肩膀上进步更快!感谢有开

源前辈的贡献!
*感谢 vcclass@hotmail.com 提供原始代码,

我在他的基础上整理了此文
*****************************************

****************************/
#include <stdio.h>

#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++ 编译器来生成动态

链接库,编译命令如下:

QUOTE:
g++ get.cpp -shared -g -DDEBUG -o

libggg.so

这样我们就准备好了动态链接库了,下面我们

编写一个应用程序来调用此动态链接库,源代

码如下:

[Copy to clipboard]
CODE:
/************关于本文档

*****************************************

***
*filename: pk.cpp
*purpose: 一个调用动态链接库的示例
*tided by: zhoulifa(zhoulifa@163.com) 周

立发 (http://zhoulifa.9999mb.com)
Linux 爱好者 Linux 知识传播者 SOHO 族 开

发者 最擅长 C 语言
*date time:2006-11-15 21:11:54
*Note: 任何人可以任意复制代码并运用这些文

档,当然包括你的商业用途
* 但请遵循 GPL
*Hope:希望越来越多的人贡献自己的力量,为

科学技术发展出力
* 科技站在巨人的肩膀上进步更快!感谢有开

源前辈的贡献!
*感谢 vcclass@hotmail.com 提供原始代码,

我在他的基础上整理了此文
*****************************************

****************************/
#include <stdio.h>

#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;

}

编译此程序用下列命令,如果已经把上面生成

的 libggg.so 放到了库文件搜索路径指定的文

件目录,比如 /lib 或 /usr/lib 之类的,就

用下面这条命令:

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

否则就用下面这条命令:

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

下面我们就开始调试上面命令生成的 app 程序

吧。如果已经把上面生成的 libggg.so 放到了

库文件搜索路径指定的文件目录,比如 /lib

或 /usr/lib 之类的,调试就顺利完成,如下



QUOTE:
zhoulifa@linux#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 处设

置断点 */
Breakpoint 1 at 0x804853c: file pk.cpp,

line 7.
(gdb) b set      /* 这是在程序的 set 处设

置断点 */
Function "set" not defined.
Make breakpoint pending on future shared

library load? (y or [n]) y /* 这里必须选

择 y 调试程序才会跟踪到动态链接库内部去

*/
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     /* 继续执行程序的下一行代码

*/
8               int b = get ();
(gdb) n      /* 程序执行到了我们断点所在

的动态链接库了 */
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   /* 查看当前代码行周围的代码

,证明我们已经跟踪到动态链接库的源代码里

面了 */
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  /* 程序顺利执行结束 */
zhoulifa@linux#

如果我们没有把动态链接库放到指定目录,比

如/lib里面,调试就会失败,过程如下:

QUOTE:
zhoulifa@linux# 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  /* 虽然调试操作都一样,但程序

执行失败 */
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
zhoulifa@linux#

本次实验的环境是:
CPU:AMD Athlon(tm) 64 Processor 3000+
内存:512M
OS:Ubuntu GNU/Linux 6.06 dapper LTS
gcc:gcc 版本 4.0.3 (Ubuntu 4.0.3-

1ubuntu5)

此贴转自 周立发的 Linux 论坛

http://zhoulifa.9999mb.com/bbs
责任编辑: articleman
发表评论 查看评论 加入收藏 Email给朋友 打印本文
如果你想对该文章评分, 请先登陆, 如果你仍未注册,请点击注册链接注册成为本站会员.
平均得分 0, 共 0 人评分
1 2 3 4 5 6 7 8 9 10
Copyright © 2002 -2003 Linuxeden.com-- Linuxeden 新闻 文档 资料 教程 Linux伊甸园
All rights reserved.