Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312336
  • 博文数量: 71
  • 博客积分: 1450
  • 博客等级: 上尉
  • 技术积分: 762
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-14 13:31
文章分类

全部博文(71)

文章存档

2014年(3)

2013年(8)

2011年(9)

2010年(38)

2009年(13)

我的朋友

分类: LINUX

2010-08-20 15:04:06

比如我们想要调试程序,可能有些系统默认是限制产生core dump文件的,而我们知道,core dump文件是我们调试程序的一个很有用的帮助文件,根据core dump文件提供的信息,我们可以比较快地定位到程序的bug。
如果用命令:
ulimit -c
看到的是0,则说明系统限制了产生core dump,我们可以设定一个文件大小以产生core dump文件。比如:
ulimit -c 1024
将设置允许产生的core dump文件最大为1024K
当然我们也可以设置为
ulimit -c unlimited
表示不限制产生的core dump文件的大小。

#arm-linux-gdb -c core ./exec_file
gdb命令:

对远程调试来说,你需要告诉 GDB 目标库所在的地方,这样它才能载入正确的拷贝--不然,它可能试图载入宿主机的库。GDB 有两个指定目标库查找目录的变量。

set solib-absolute-prefix path

If this variable is set, path will be used as a prefix for any absolute shared library paths; many runtime loaders store the absolute paths to the shared library in the target program's memory. If you use `solib-absolute-prefix' to find shared libraries, they need to be laid out in the same way that they are on the target, with e.g. a `/usr/lib' hierarchy under path. You can set the default value of `solib-absolute-prefix' by using the configure-time `--with-sysroot' option.

如果这一变量被设置,path 将被作为任何绝对路径共享库的前缀;许多运行时的载入器存储目标程序内存中共享库的绝对路径。如果你使用“solib-absolute-prefix” 查找共享库,它们应该和目标上有一样的路径结构,比如path下的一个“/usr/lib”的层次。

show solib-absolute-prefix

Display the current shared library prefix.

显示当前共享库前缀的配置。

set solib-search-path path

If this variable is set, path is a colon-separated list of directories to search for shared libraries. `solib-search-path' is used after `solib-absolute-prefix' fails to locate the library, or if the path to the library is relative instead of absolute. If you want to use `solib-search-path' instead of `solib-absolute-prefix', be sure to set `solib-absolute-prefix' to a nonexistant directory to prevent GDB from finding your host's libraries.

如果这一变量被设置,其中path 是冒号分割的用来搜索共享库的目录的列表。“solib-search-path”在“solib-absolute-prefix”定位库失败后使用, 或者库的路径是相对的,而不是绝对的。如果你想使用“solib-search-path”代替“solib-absolute-prefix”,确定 “solib-absolute-prefix”设置的是不存在的目录,以阻止 GDB 查找宿主机上的库。

show solib-search-path

Display the current shared library search path.

显示当前共享库查找路径的配置。




补充说明:

1.core文件的生成开关和大小限制
1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。
2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。若ulimit -c unlimited,则表示core文件的大小不受限制。ulimit -c 0关闭该功能。
PS: ulimit使用方法见
http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/


2.core文件的名称和生成路径
core文件生成路径:输入可执行文件运行命令的同一路径下。
若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
echo "1" > /proc/sys/kernel/core_uses_pid

2)proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。
可通过以下命令修改此文件:
echo "/corefile/core-%e-%p-%t" > core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为core-命令名-pid-时间戳
以下是参数列表:
    %p - insert pid into filename 添加pid
    %u - insert current uid into filename 添加当前uid
    %g - insert current gid into filename 添加当前gid
    %s - insert signal that caused the coredump into the filename 添加导致产生core的信号
    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间
    %h - insert hostname where the coredump happened into filename 添加主机名
    %e - insert coredumping executable name into filename 添加命令名


3.core文件的查看
core文件需要使用gdb来查看。
gdb ./a.out
core-file core.xxxx
使用bt命令即可看到程序出错的地方。
以下两种命令方式具有相同的效果,但是在有些环境下不生效,所以推荐使用上面的命令。
1)gdb -core=core.xxxx
file ./a.out
bt
2)gdb -c core.xxxx
file ./a.out
bt


4.开发板上使用core文件调试
如果开发板的操作系统也是linux,core调试方法依然适用。如果开发板上不支持gdb,可将开发板的环境(依赖库)、可执行文件和core文件拷贝到PC的linux下。
在PC上调试开发板上产生的core文件,需要使用交叉编译器自带的gdb,并且需要在gdb中指定solib-absolute-prefix和 solib-search-path两个变量以保证gdb能够找到可执行程序的依赖库路径。有一种建立配置文件的方法,不需要每次启动gdb都配置以上变 量,即:在待运行gdb的路径下建立.gdbinit。
配置文件内容:
set solib-absolute-prefix YOUR_CROSS_COMPILE_PATH
set solib-search-path YOUR_CROSS_COMPILE_PATH
set solib-search-path YOUR_DEVELOPER_TOOLS_LIB_PATH
handle SIG32 nostop noprint pass


阅读(3271) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~