gdb定位死锁位置

用g++进行编译时候要加上-g标签g++ -std=c++11 -g 2.cpp -o 2 -pthread

方法一:运行程序->找到pid->使用pid进入gdb调试

  • 运行程序,发现程序锁死,使用ps找到程序的pid:ps -aux | grep ./app 其中./app为程序名
  • 执行gdb -p pid进入gdb调试界面
  • 执行i threads打印当前所有线程
(gdb) i threads
  Id   Target Id         Frame 
  1    Thread 0x7f9082d44740 (LWP 7940) "2" 0x00007f908239198d in pthread_join (threadid=140258629605120, thread_return=0x0) at pthread_join.c:90
* 2    Thread 0x7f9081cb5700 (LWP 7941) "2" __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
  3    Thread 0x7f90814b4700 (LWP 7942) "2" __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
  • 执行thread 2进入线程2,执行bt打印堆栈信息,可以定位到第五行in do_work_1 () at 2.cpp:15
(gdb) bt
#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1  0x00007f9082392dbd in __GI___pthread_mutex_lock (mutex=0x605320 <_mutex2>) at ../nptl/pthread_mutex_lock.c:80
#2  0x0000000000401047 in __gthread_mutex_lock (__mutex=0x605320 <_mutex2>) at /usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:748
#3  0x000000000040173e in std::mutex::lock (this=0x605320 <_mutex2>) at /usr/include/c++/5/mutex:135
#4  0x00000000004017e4 in std::lock_guard<std::mutex>::lock_guard (this=0x7f9081cb4e50, __m=...) at /usr/include/c++/5/mutex:386
#5  0x0000000000401191 in do_work_1 () at 2.cpp:15
#6  0x0000000000402d69 in std::_Bind_simple<int (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0x1449c48) at /usr/include/c++/5/functional:1531
#7  0x0000000000402cc2 in std::_Bind_simple<int (*())()>::operator()() (this=0x1449c48) at /usr/include/c++/5/functional:1520
#8  0x0000000000402c52 in std::thread::_Impl<std::_Bind_simple<int (*())()> >::_M_run() (this=0x1449c30) at /usr/include/c++/5/thread:115
#9  0x00007f9082874c80 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x00007f90823906ba in start_thread (arg=0x7f9081cb5700) at pthread_create.c:333

方法二:直接使用gdb打开程序->run运行->出现死锁->ctrl+c关闭,之后同上

  • 载入程序sudo gdb ./2
  • 运行run
  • ctrl+c关闭程序,之后同上。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值