记录一次gdb debug经历
问题描述
今天在写代码时,运行时奔溃了。segment fault,而且是在程序退出main()函数后,才报的。
唯一的信息是:Segmentation fault (core dumped)
简直是一头雾水。
查看core文件
系统默认是不会生成core文件的,ulimit -c unlimited把core文件设为无限大。
使用gdb查看core文件
gdb ./example/sudoku_batch_test core
提示如下:
Program terminated with signal SIGSEGV, Segmentation fault.
#0 __GI___libc_free (mem=0x313030303030300a) at malloc.c:2951
2951 malloc.c: No such file or directory.
(gdb)
可以确定崩溃发生在malloc.c中。但是提示没有malloc.c的源码。
首先安装glibc的符号表,命令如下:
sudo apt-get install libc6-dbg
再来是安装glibc的源文件,命令如下:
sudo apt-get source libc6-dev
安装完毕后在当前目录下会多出一个glibc-2.23文件夹,该文件夹包含了glibc的源码。
源码准备就绪后,接着上面,在gdb命令提示符下输入:
directory glibc-2.23/malloc/将glibc-2.23/malloc/设为gdb源码搜索目录。结果如下:
warning: core file may not match specified executable file.
[New LWP 24491]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./example/sudoku_batch_test ../example/test1000 127.0.0.1 1'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 __GI___libc_free (mem=0x313030303030300a) at malloc.c:2951
2951 malloc.c: No such file or directory.
(gdb) directory glibc-2.23/malloc/
Source directories searched: /root/work/melon/build/glibc-2.23/malloc:$cdir:$cwd
(gdb)
现在我们就可以在gdb中查看崩溃处的源码了,执行list:
(gdb) l
warning: Source file is more recent than executable.
2946 if (mem == 0) /* free(0) has no effect */
2947 return;
2948
2949 p = mem2chunk (mem);
2950
2951 if (chunk_is_mmapped (p)) /* release mmapped memory. */
2952 {
2953 /* see if the dynamic brk/mmap threshold needs adjusting */
2954 if (!mp_.no_dyn_threshold
2955 && p->size > mp_.mmap_threshold
(gdb)
虽然知道了崩溃发生在2951行,但是貌似没有更多有效的信息。这时我想到了是不是可以看下函数的调用栈,或许会有信息。
接着执行backtrace(或者bt):
(gdb) bt
#0 __GI___libc_free (mem=0x313030303030300a) at malloc.c:2951
#1 0x000000000048bc9d in melon::Coroutine::~Coroutine (this=0x1fc9120, __in_chrg=<optimized out>)
at /root/work/melon/src/Coroutine.cpp:56
#2 0x000000000048d099 in std::_Sp_counted_ptr<melon::Coroutine*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (
this=0x1fc8190) at /usr/include/c++/5/bits/shared_ptr_base.h:374
#3 0x00000000004630f1 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x1fc8190)
at /usr/include/c++/5/bits/shared_ptr_base.h:150
#4 0x0000000000461f32 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x7f07f4ff1770,
__in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr_base.h:659
#5 0x00000000004749ed in std::__shared_ptr<melon::Coroutine, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (
this=0x7f07f4ff1768, __in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr_base.h:925
#6 0x0000000000474a39 in std::shared_ptr<melon::Coroutine>::~shared_ptr (this=0x7f07f4ff1768,
__in_chrg=<optimized out>) at /usr/include/c++/5/bits/shared_ptr.h:93
#7 0x00007f07f40915ff in __GI___call_tls_dtors () at cxa_thread_atexit_impl.c:155
#8 0x00007f07f4090f27 in __run_exit_handlers (status=0, listp=0x7f07f441b5f8 <__exit_funcs>,
run_list_atexit=run_list_atexit@entry=true) at exit.c:40
#9 0x00007f07f4091045 in __GI_exit (status=<optimized out>) at exit.c:104
#10 0x00007f07f4077837 in __libc_start_main (main=0x45f1c4 <main(int, char**)>, argc=4, argv=0x7ffcfb2ab218,
init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7ffcfb2ab208)
at ../csu/libc-start.c:325
#11 0x000000000045ec89 in _start ()
这下问题找到了,首先在线程结束或者程序运行结束会调用__GI___call_tls_dtors函数来析构线程本地存储。我确实用了thread_local关键字修饰Coroutine::Ptr变量。
从#1 0x000000000048bc9d in melon::Coroutine::~Coroutine可知在melon::Coroutine类的析构函数中调用了free()导致奔溃。
这下问题基本明确了,我在Coroutine析构函数中会释放stack_这个指针,
53 Coroutine::~Coroutine() {
54 LOG_DEBUG << "destroy coroutine:" << name_;
55 if (stack_) {
56 free(stack_);
57 }
58 }
有两个构造函数,其中一个如下:
39 Coroutine::Coroutine()
40 :c_id_(++t_coroutine_id),
41 name_("Main-" + std::to_string(c_id_)),
42 cb_(nullptr),
43 state_(CoroutineState::INIT) {
44
45 if (getcontext(&context_)) {
46 LOG_ERROR << "getcontext: errno=" << errno
47 << " error string:" << strerror(errno);
58 }
59 }
因为大意犯了个非常低级的错误,这个构造函数没有正确初始化statck_指针,将statck_初始化为nullptr后,问题就解决了。
update:2019-10-31
其实不用这么麻烦,gdb有个where命令,能直接打印出函数栈信息。
总结
遇到这类问题,一般用gdb查看core文件都能定位到崩溃的位置,如果不是直接引发的,可以查看函数调用栈,一般都能找到问题原因。
记录一次gdb debug经历的更多相关文章
- Linux GDB Debug
http://blog.jobbole.com/107925/ gdb 调试入门,大牛写的高质量指南 http://blog.jobbole.com/107759/ gdb是the GNU Debug ...
- 记录一次OOM排查经历(一)
一.经历概要 程序里有个跑数据的job,这个job的主要功能是往数据库写假数据. 既需要跑历史数据(传给job的日期是过去的时间),也需要能够上线后,实时跑(十秒钟触发一次,传入触发时的当前时间). ...
- Linux Kernel 0.12 启动简介,调试记录(Ubuntu1804, Bochs, gdb)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...
- DEBUG经历
在两年有余的学习生活中,我不仅在课堂上学到了很多东西,我也在一次次的错误中得到了宝贵的经验和教训.Bug和debug,构成了我生活中不可或缺的一部分. 我在编程中犯过的错误很多,无法一一阐述,再次说一 ...
- 记录一次ceph recovery经历
一次ceph recovery经历 背景 这是一个測试环境. 该环境中是cephfs 一共12个节点, 2个client.2个mds.8个osd mds: 2颗CPU,每一个4核.一共是8核. 128 ...
- 记一次难忘的排错debug经历(找了5天左右)(涉及内存覆盖)
strcpy和memcpy都没有处理内存覆盖问题. 函数描述 The memcpy function copies count bytes of src to dest. If the source ...
- 小记录:flask的DEBUG开关
请求站点的如下位置: http://www.ahfu.com/ahfuzhang/?debugger=yes&cmd=resource&f=style.css 居然正常范围了CSS文件 ...
- 记录一次无聊的(经历了Nodejs -> Shell -> C)的探索问题过程
提出问题 在运行项目的服务器的git是1.8.3.1版本的时候,pm2 deploy 项目,服务器fetch不到最新的一次commit. 对于这个问题,在pm2的github也有issues讨论.然后 ...
- gdb debug
http://www.cnblogs.com/life2refuel/p/5396538.html
随机推荐
- 04.Django基础四之模板系统
一 语法 模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二 变量 在Django的模板语言中按此语法使用:{ ...
- Spring入门教程
Spring新手入门教程,配套下面这两个大神的课程就可以了. 一个是Spring视频教程. 一个是Spring博客教程. https://www.imooc.com/learn/196 http:// ...
- AMD vs. CommonJS?
js开发者对js模块加载的尝试和创新从来都没有停止过,尤其是当nodejs的出现后,模块化加载的必要性更加凸显.本文不讨论如何在nodejs环境来模块化加载(创造者已经利用commonJS机制解决), ...
- 使用express框架创建服务器
上一篇写创建第一个node服务器是基于原始方法写的,express框架的作用就是省掉那些原始代码,直接调用相关的方法就可以了,开发起来简单方便. 一.package.json的配置 首先要进行pack ...
- asp.net core IdentityServer4 实现 resource owner password credentials(密码凭证)
前言 OAuth 2.0默认四种授权模式(GrantType) 授权码模式(authorization_code) 简化模式(implicit) 密码模式(resource owner passwor ...
- 使用Storm进行词频统计
词频统计 1.需求:读取指定目录的数据,并且实现单词计数功能 2.实现方案: Spout用于读取指定文件夹(目录),读取文件,将文件的每一行发射到Bolt SplitBolt用于接收Spout发射过来 ...
- cp -rf 操作时依然会提示覆盖
在linux上经常会使用cp -rf命令,该命令就是强制覆盖指定目录下的文件,但有时使用该命令依然会提示是否覆盖,cp命令的参数有如下一些: 参数说明: -a:此选项通常在复制目录时使用,它保留链接. ...
- Python celery和Redis入门安装使用(排难帖)
1.redis安装 下载地址 https://github.com/MicrosoftArchive/redis/releases,选择Redis-x64-3.2.100.msi5.8 MB下载就好了 ...
- 踩坑踩坑之Flask+ uWSGI + Tensorflow的Web服务部署
一.简介 作为算法开发人员,在算法模块完成后,拟部署Web服务以对外提供服务,从而将算法模型落地应用.本文针对首次基于Flask + uWSGI + Tensorflow + Nginx部署Web服务 ...
- Vue三步完成跨域请求
三步完成跨域请求 ①main.js中: Vue.prototype.HOME = '/api'; ② config/index.js中: module.exports = { dev: { // Pa ...