一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点。如:
$gcc -g -Wall hello.c -o hello
$g++ -g -Wall hello.cpp -o hello
如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址。当你用-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。
启动GDB的方法有以下几种:
gdb <program>
program也就是你的执行文件,一般在当前目录下。
gdb <program>core
用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。
gdb <program><PID>
如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。
以上三种都是进入gdb环境和加载被调试程序同时进行的。也可以先进入gdb环境,在加载被调试程序,方法如下:
*在终端输入:gdb
*在gdb环境中:file <program>
这两步等价于:gdb <program>
GDB启动时,可以加上一些GDB的启动开关,详细的开关可以用gdb -help查看。我在下面只例举一些比较常用的参数:
-symbols <file>
-s <file>
从指定文件中读取符号表。
-se file
从指定文件中读取符号表信息,并把他用在可执行文件中。
-core <file>
-c <file>
调试时core dump的core文件。
-directory <directory>
-d <directory>
加入一个源文件的搜索路径。默认搜索路径是环境变量中PATH所定义的路径。
用gdb调试动态链接库大家都知道在 Linux 可以用 gdb 来调试应用程序,当然前提是用 gcc 编译程序时要加上 -g 参数。
我这篇文章里将讨论一下用 gdb 来调试动态链接库的问题。
首先,假设我们准备这样的一个动态链接库:
引用:
库名称是: ggg
动态链接库文件名是: libggg.so
头文件是: get.h
提供这样两个函数调用接口:
int get ()
int set (int a)
要生成这样一个动态链接库,我们首先编写这样一个头文件:
/************关于本文档********************************************
*filename: get.h
*********************************************************************/
int get ()
int set (int a)
然后准备这样一个生成动态链接库的源文件:
/************关于本文档********************************************
*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++ 编译器来生成动态链接库,编译命令如下:
引用:
g++ get.cpp -shared -g -DDEBUG -o libggg.so
这样我们就准备好了动态链接库了,下面我们编写一个应用程序来调用此动态链接库,源代码如下:
/************关于本文档********************************************
*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
}
编译此程序用下列命令,如果已经把上面生成的 libggg.so 放到了库文件搜索路径指定的文件目录,比如 /lib 或 /usr/lib 之类的,就用下面这条命令:
引用:
g++ pk.cpp -o app -Wall -g -lggg
否则就用下面这条命令:
引用:
g++ pk.cpp -o app -Wall -g -lggg -L`pwd`
下面我们就开始调试上面命令生成的 app 程序吧。如果已经把上面生成的 libggg.so 放到了库文件搜索路径指定的文件目录,比如 /lib 或 /usr/lib 之类的,调试就顺利完成,如下:
引用:
#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=0xbfArrayArray0504) at pk.cpp:7
7 int a = 100
(gdb) n /* 继续执行程序的下一行代码 */
8 int b = get ()
(gdb) n /* 程序执行到了我们断点所在的动态链接库了 */
get x=0
Array 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 }
Array 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=0xbfArrayArray0504) 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 /* 程序顺利执行结束 */
如果我们没有把动态链接库放到指定目录,比如/lib里面,调试就会失败,过程如下:
引用:
# 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
方法一、在/etc/ld.so.conf文件中添加路径,vi /etc/ld.so.conf添加下边内容
123
include ld.so.conf.d/*.conf /usr/cluster/.share/lib
方法二、在终端输入:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/cluster/.share/lib
方法三、修改/etc/profile文件
123
export MPI_HOME=/usr/cluster export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MPI_HOME/.share/lib
在终端执行source /etc/profile 使配置文件生效
程序运行时加载动态库失败的解决方法
错误提示如下:
error while loading shared libraries: libjson.so.0: cannot open shared object file: No such file or directory
原因一般有两个,一个是 *** 作系统中没有包含该共享库(lib*.so.* 文件)或者共享库版本不对。解决办法就是重新下载安装。
另外一个原因就是已经安装了该共享库,但是执行需要调用该共享库的程序的时候,程序按照默认共享库路径找不到该共享库文件。解决方法如下:
如果共享库文件安装到了 /lib 或 /usr/lib 目录下,那么执行一下 ldconfig 命令。
ldconfig命令的用途, 主要是在默认搜寻目录(b和/usrb)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.
如果共享库文件安装到了 /usr/local/lib (一般开源的共享库都会安装到该目录下)或者其它非 /lib 或 /usr/lib 目录下,那么在执行 ldconfig 命令前,还要把新的共享库目录加入到共享库配置文件 /etc/ld.so.conf 中,如下:
1234
# cat /etc/ld.so.confinclude ld.so.conf.d/*.conf# echo "/usr/local/lib" >>/etc/ld.so.conf# ldconfig
或者在 /etc/ld.so.conf.d/ 目录下新建任何以 .conf 为后缀的文件,在该文件中加入库文件所在的目录。然后执行 ldconfig 更新 /etc/ld.so.cache 文件。
如果共享库文件安装到了其他非 /lib 或 /usr/lib 目录下,但是又不想在 /etc/ld.so.conf 文件中加共享库路径(或者是没有权限加路径)。那可以 export 一个全局变量 LD_LIBRARY_PATH,然后运行程序的时候就会去找个目录中找共享库。
LD_LIBRARY_PATH的意思是告诉loader在哪些目录中可以找到共享库. 可以设置多个搜索目录, 这些目录之间用冒号分隔开. 比如安装了一个mysql到/usr/local/mysql目录下, 其中有一大堆库文件在/usr/local/mysql/lib下面, 则可以在.bashrc或.bash_profile或shell里加入以下语句即可:
export LD_LIBRARY_PATH=/usr/local/mysql/lib:$LD_LIBRARY_PATH
一般来讲这只是一种临时的解决方案, 在没有权限或临时需要的时候使用.
如果程序需要的库文件比系统目前存在的库文件版本低,可以做一个链接。比如:
12345
error while loading shared libraries: libncurses.so.4: cannot open sharedobject file: No such file or directoryls /usr/lib/libncu*/usr/lib/libncurses.a /usr/lib/libncurses.so.5/usr/lib/libncurses.so /usr/lib/libncurses.so.5.3
可见虽然没有libncurses.so.4,但有libncurses.so.5,是可以向下兼容的
建一个链接就好了
1
ln -s /usr/lib/libncurses.so.5.3 /usr/lib/libncurses.so.4
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)