C114门户论坛百科APPEN| 举报 切换到宽版

亚星游戏官网

 找回密码
 注册

只需一步,快速开始

短信验证,便捷登录

搜索
查看: 3344|回复: 0

[通信技术与资料] lnux Segmentation Fault的GDB调试方法 [复制链接]

军衔等级:

亚星游戏官网-yaxin222  新兵

注册:2005-10-29
发表于 2010-10-12 05:19:57 |显示全部楼层
这种方法需要用到Linux提供的core dump机制:当程序中出现内存操作错误时,会发生崩溃并产生核心文件(core文件)。使用GDB可以对产生的核心文件进行分析,找出程序是在什么时候崩溃的和在崩溃之前程序都做了些什么。
首先,你的Segmentation Fault错误必须要能重现(废话)。

然后,依参照下面的步骤来操作:

1)无论你是用Makefile来编译,还是直接在命令行手工输入命令来编译,都应该加上
-g
选项。

2)一般来说,在默认情况下,在程序崩溃时,core文件是不生成的(很多Linux发行版在默认时禁止生成核心文件)。所以,你必须修改这个默认选项,在命令行实行:


ulimit -c unlimited
表示不限制生成的core文件的大小。

3)运行你的程序,不管用什么方法,使之重现Segmentation Fault错误。

4)这时,你会发现在你程序同一目录下,生成了一个文件名为
core.***
的文件,即核心文件。例如,core.15667这样的文件。

5)用GDB调试它。假设你的可实行程序名为test,则在命令行实行:

gdb test core.15667

然后可能会显示出一堆信息:
GNU gdb 6.6
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 "i586-suse-linux"...
Using host libthread_db library "/lib/libthread_db.so.1".
warning: core file may not match specified executable file.
warning: Can't read pathname for load map: Input/output error.
Reading symbols from /usr/lib/libstdc++.so.6...done.
Loaded symbols for /usr/lib/libstdc++.so.6
Reading symbols from /lib/libm.so.6...done.
Loaded symbols for /lib/libm.so.6
Reading symbols from /lib/libgcc_s.so.1...done.
Loaded symbols for /lib/libgcc_s.so.1
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 `/home/YHang/cscf/make32/../../pplat/tools/sip_diam_mml_suse'.
Program terminated with signal 11, Segmentation fault.
#0
0xb7e072dd in opendir () from /lib/libc.so.6
然后大家输入并实行命令
bt
(gdb) bt
就会得到类似于下面的信息:
(gdb) bt
#0
0xb7e072dd in opendir () from /lib/libc.so.6
#1
0x08048d6f in getFilesName (dir=0x0) at sip_diam_mml.cpp:25
#2
0x0804900d in main () at sip_diam_mml.cpp:257
问题即可定位

举报本楼

您需要登录后才可以回帖 登录 | 注册 |

手机版|C114 ( 沪ICP备12002291号-1 )|联系大家 |网站地图  

GMT+8, 2024-9-25 09:41 , Processed in 0.400205 second(s), 15 queries , Gzip On.

Copyright © 1999-2023 C114 All Rights Reserved

Discuz Licensed

回顶部
XML 地图 | Sitemap 地图