美文网首页
使用 gdb 调试死锁线程

使用 gdb 调试死锁线程

作者: 404d67ac8c12 | 来源:发表于2019-02-03 17:45 被阅读0次

死锁调试预备

  • -g 参数
  • attach
  • info threads
  • thread + number 切换对应线程

testlock.cpp

  1 #include <iostream>
  2 #include <thread>
  3 #include <mutex>
  4 #include <unistd.h>
  5 
  6 std::mutex gMutex;
  7 
  8 void Test1()
  9 {   
 10     gMutex.lock();
 11     gMutex.lock();
 12 }
 13 
 14 void Test2()
 15 {   
 16     while (true) {
 17         sleep(1);
 18     }
 19 }
 20 
 21 int main()
 22 {   
 23     std::thread t1(Test1);
 24     std::thread t2(Test2);
 25     
 26     t1.join();
 27     t2.join();
 28     return 0;
 29 }

编译运行
编译:$ g++ -std=c++11 -g ./testlock.cpp -pthread
运行:$ ./a.out
程序挂起,然后使用 gdb 进行调试。

gdb 调试

# 查找进程 id
$ ps -ef |grep a.out
test 18951 18823  0 17:26 pts/7 00:00:00  ./a.out

# 使用 root 权限 进入 attach
$ gdb a.out 18951
(gdb) info threads
  Id   Target Id         Frame 
* 1    Thread 0x7f4545f6a740 (LWP 18951) "a.out" 0x00007f45455a798d in pthread_join (threadid=139935485835008, thread_return=0x0)
    at pthread_join.c:90
  2    Thread 0x7f4544ecb700 (LWP 18952) "a.out" __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
  3    Thread 0x7f45446ca700 (LWP 18953) "a.out" 0x00007f45452a130d in nanosleep () at ../sysdeps/unix/syscall-template.S:84

# 调到第二个线程
(gdb)t 2
[Switching to thread 2 (Thread 0x7f4544ecb700 (LWP 18952))]
#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
135     ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: 没有那个文件或目录.

# 执行 bt
(gdb) bt
#0  __lll_lock_wait () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1  0x00007f45455a8dbd in __GI___pthread_mutex_lock (mutex=0x6041a0 <gMutex>) at ../nptl/pthread_mutex_lock.c:80
#2  0x0000000000400e67 in __gthread_mutex_lock (__mutex=0x6041a0 <gMutex>) at /usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:748
#3  0x0000000000401224 in std::mutex::lock (this=0x6041a0 <gMutex>) at /usr/include/c++/5/mutex:135
#4  0x0000000000400f0f in Test1 () at ./testlock.cpp:11
#5  0x0000000000402525 in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0x1b77c48)
    at /usr/include/c++/5/functional:1531
#6  0x000000000040247e in std::_Bind_simple<void (*())()>::operator()() (this=0x1b77c48) at /usr/include/c++/5/functional:1520
#7  0x000000000040240e in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0x1b77c30) at /usr/include/c++/5/thread:115
#8  0x00007f4545a8ac80 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007f45455a66ba in start_thread (arg=0x7f4544ecb700) at pthread_create.c:333
#10 0x00007f45452dc3dd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109

可以看到第 4 项 #4 0x0000000000400f0f in Test1 () at ./testlock.cpp:11 程序卡在了文件的 11 行。

相关文章

  • gdb调试多线程 如何解死锁问题

    基础_多线程 Q1 gdb调试多线程 如何解死锁问题?A1 gdb基本用法 info threads(show a...

  • 使用 gdb 调试死锁线程

    死锁调试预备 -g 参数 attach info threads thread + number 切换对应线程 t...

  • gdb调试

    gdb及调试排查 gdb使用 抓所有线程 thread apply all b squid_srv_do_writ...

  • gdb thread

    先介绍一下GDB多线程调试的基本命令。 info threads显示当前可调试的所有线程,每个线程会有一个GDB为...

  • gcc常用命令

    gdb相关 gcc加-g才能使用gdb调试gdb -tui a.out打开调试界面run/stop/continu...

  • gdb

    gdb调试工具使用

  • Xcode debug Hotspot(一)——创建Xcode项

    概述 前面安装gdb调试hotspot里面,我记录了自己安装gdb调试hotspot的过程。后来我发现,使用gdb...

  • 使用 GDB 调试 Android 应用

    GNU 工程调试器(GDB)是一个常用的 Unix 调试器。本文详述使用 gdb 调试 Android 应用和进程...

  • GDB 配置

    摘要:调试器 GDB 的配置 GDB 配置 使用 GDB 扩展来配置 GDB 事实上我还是觉得原生的 GDB 就...

  • 一、调试-使用gdb调试linuxC程序

    gdb是linux操作系统特有的调试工具,可以完成一般IDE提供的所有调试功能。使用gdb调试程序之前,必须使用g...

网友评论

      本文标题:使用 gdb 调试死锁线程

      本文链接:https://www.haomeiwen.com/subject/qsoysqtx.html