如何调试崩溃的程序

原链接: http://community.bwbot.org/topic/138

在程序开发中经常会遇到这样的问题,对于C或C++的程序有时程序崩溃不能获得有效的调试信息
Segmentation fault
Core dump
这样的程序如何进行调试呢?
我们可以利用gdb去调试崩溃程序。

首先开启core dump文件。在开启之后,当程序崩溃的时候操作系统会自动的把崩溃信息存储到core文件里面。
在终端输入

ulimit -c unlimited

这样就打开core dump 功能了。

下面是一个崩溃的实际例子

randoms@nowhere:~/ramdisk$ /home/randoms/Documents/ros/workspace/devel/lib/orb_slam2/mono /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/Examples/ROS/orb_slam2/Data/ORBvoc.bin /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/Examples/ROS/orb_slam2/Data/setting4.yaml /camera/image_raw:=/camera_node/image_raw /Pose2D:=/xqserial_server/Pose2D > orb.log 
mono: ../nptl/pthread_mutex_lock.c:350: __pthread_mutex_lock_full: Assertion `(-(e)) != 3 || !robust' failed.
Aborted (core dumped)
randoms@nowhere:~/ramdisk$ ls
2.bag  core  KeyFrameTrajectory.txt  orb.log

可以看到在程序崩溃后创建了一个core文件,在终端输入下面的指令开始调试

randoms@nowhere:~/ramdisk$ gdb /home/randoms/Documents/ros/workspace/devel/lib/orb_slam2/mono core

gdb的指令格式是 gdb EXE_FILE_PATH CORE_FILE_PATH

等待载入完成

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/randoms/Documents/ros/workspace/devel/lib/orb_slam2/mono...(no debugging symbols found)...done.
[New LWP 14392]
[New LWP 14370]
[New LWP 14365]
[New LWP 14367]
[New LWP 14371]
[New LWP 14388]
[New LWP 14376]
[New LWP 14391]
[New LWP 14407]
[New LWP 14406]
[New LWP 14408]
[New LWP 14377]
[New LWP 14411]
[New LWP 14410]
[New LWP 14369]
[New LWP 14387]
[New LWP 14409]
[New LWP 14412]
[New LWP 14390]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `/home/randoms/Documents/ros/workspace/devel/lib/orb_slam2/mono /home/randoms/Do'.
Program terminated with signal SIGABRT, Aborted.
#0  0x00007fe6ca6ecc37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.

gdb在终端中输入

bt

可以显示出崩溃时的堆栈信息

#0  0x00007fe6ca6ecc37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007fe6ca6f0028 in __GI_abort () at abort.c:89
#2  0x00007fe6ca6e5bf6 in __assert_fail_base (
    fmt=0x7fe6ca8363b8 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", 
    assertion=assertion@entry=0x7fe6c9bb6a25 "(-(e)) != 3 || !robust", 
    file=file@entry=0x7fe6c9bb6a08 "../nptl/pthread_mutex_lock.c", 
    line=line@entry=350, 
    function=function@entry=0x7fe6c9bb6b20 <__PRETTY_FUNCTION__.8695> "__pthread_mutex_lock_full") at assert.c:92
#3  0x00007fe6ca6e5ca2 in __GI___assert_fail (
    assertion=assertion@entry=0x7fe6c9bb6a25 "(-(e)) != 3 || !robust", 
    file=file@entry=0x7fe6c9bb6a08 "../nptl/pthread_mutex_lock.c", 
    line=line@entry=350, 
    function=function@entry=0x7fe6c9bb6b20 <__PRETTY_FUNCTION__.8695> "__pthread_mutex_lock_full") at assert.c:101
#4  0x00007fe6c9ba9ce1 in __pthread_mutex_lock_full (mutex=0x9643740)
    at ../nptl/pthread_mutex_lock.c:350
#5  0x00007fe6cb03403a in __gthread_mutex_lock (__mutex=0x9643740)
    at /usr/include/x86_64-linux-gnu/c++/4.8/bits/gthr-default.h:748
#6  lock (this=0x9643740) at /usr/include/c++/4.8/mutex:134
#7  lock (this=0x7fe694ed5a10) at /usr/include/c++/4.8/mutex:511
#8  unique_lock (__m=..., this=0x7fe694ed5a10)
---Type <return> to continue, or q <return> to quit---
    at /usr/include/c++/4.8/mutex:443
#9  ORB_SLAM2::MapPoint::isBad (this=0x96434c0)
    at /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/src/MapPoint.cc:272
#10 0x00007fe6cb03c957 in ORB_SLAM2::KeyFrame::RemoveBadPoints (this=0xa33f510)
    at /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/src/KeyFrame.cc:1155
#11 0x00007fe6caff8b5a in ORB_SLAM2::Tracking::GC (this=0x711d310)
    at /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/src/Tracking.cc:311
#12 0x00007fe6cb0baef2 in ORB_SLAM2::GC::Run (this=0x7157fe0)
    at /home/randoms/Documents/ros/workspace/src/ORB_SLAM2/src/GC.cc:37
#13 0x00007fe6cad42a60 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#14 0x00007fe6c9bac184 in start_thread (arg=0x7fe694ed6700)
    at pthread_create.c:312
#15 0x00007fe6ca7b037d in clone ()
    at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111

这样我们就可以定位到具体在哪一个语句崩溃的。可以看出这次的崩溃是一个锁的问题。在MapPoint.cc文件里面。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值