美文网首页首页投稿(暂停使用,暂停投稿)
gdb调试多线程 如何解死锁问题

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

作者: 小王同学加油 | 来源:发表于2016-05-07 12:41 被阅读1892次

基础_多线程

Q1 gdb调试多线程 如何解死锁问题?
A1

gdb基本用法

  • info threads(show all thread)
  • thread thread number (switch )
  • thread apply all break demo.cpp:42(all)

eg:
同一个功能A,创建N个线程
同一个功能B,创建M个线程
来抢夺和释放资源C,D

不清楚那个线程 有限占用或者释放资源

  • [ ] 产生问题1 跟踪那个线程ID

代码实现顺序实际执行顺序是不一致的,
一般无法通过查看代码快速定位
thread ID id用那个呀?

确认那个线程产生了锁

thread apply all break demo.cpp:19
thread apply all break demo.cpp:42

pthread_mutex_t mymutex
公共资源:两个线程同时抢占

p mymutex

    显示了当前占用的线程(上图)
  当前目前线程位于5882 但是被
  __owner = 5883锁住

* 1 Thread 0x7ffff7fe1780 (LWP 5882。 2 Thread 0x7ffff6d6d700 (LWP 5883)

thread ID找到啦 5882
总结:
pthread_mutex_t.__data.__owner is a TID. pthread_t (frompthread_self()).

pthread_mutex_t 结构体

typedef union { struct __pthread_mutex_s { int __lock; unsigned int __count; int __owner; #if __WORDSIZE == 64 unsigned int __nusers; #endif /* KIND must stay at this position in the structure to maintain binary compatibility. */ int __kind; #if __WORDSIZE == 64 int __spins; __pthread_list_t __list; # define __PTHREAD_MUTEX_HAVE_PREV 1 #else unsigned int __nusers; __extension__ union { int __spins; __pthread_slist_t __list; }; #endif } __data; char __size[__SIZEOF_PTHREAD_MUTEX_T]; long int __align; } pthread_mutex_t;

  • [ ] 产生问题2
    gdb默认调试当前主线程

thread apply all command 表示
all 所有线程中相应的行上设置断点
你发现一个问题
调试期间(next)不断的不同线程来回切换,
(如果谁发现不是麻烦告知)
线程是cpu调度的最小单位 因为分片原因
cpu不断在不同线程之间切换
注意不是进程进程可以理解为一个主线程

set scheduler-locking on 只调试当前线程

  • [ ] 产生问题3
    如果进程有fork 如何办?

If you need to debug the child process, after the start gdb:
(Gdb) set follow-fork-mode child off

查询正在调试的进程:info inferiors
切换调试的进程:inferior id

如何分析思路

  • 不用gdb:
    假如100个线程 此时10个线程因为资源问题产生了死锁
    gdb调试会影响业务

可通过日志或者其他方式打印超时锁
然后pstack +进程ID 查看堆栈信息

  • 用gdb
    1 通过gcore或者gdb -p方式 进入
    2 设置断点 thread apply all commd
    等待一段时间产生死锁
    3 p pthread_mutex_t
    确定目前那个线程占用
    至少2个 一个不会产生死锁 加锁顺序

  • 其他工具
    Valgrind 的 helgrind
    工具也可以检测死锁。
    用法:
    valgrind --tool=helgrind ./deadlock
    http://valgrind.org/docs/manual/hg-manual.html

给出了详细的例子和说明

#include <pthread.h>
int var = 0;
void* child_fn ( void* arg ) {
   var++; /* Unprotected relative to parent */ /* this is line 6 */
   return NULL;
}

int main ( void ) {
   pthread_t child;
   pthread_create(&child, NULL, child_fn, NULL);
   var++; /* Unprotected relative to child */ /* this is line 13 */
   pthread_join(child, NULL);
   return 0;
}

变量var没有加锁

 ==7066== Possible data race during read of size 4 at 0x601040 by thread #1
==7066== Locks held: none
==7066==    at 0x4006C1: main (lock.cpp:13)
==7066== 
==7066== This conflicts with a previous write of size 4 by thread #2
==7066== Locks held: none
==7066==    at 0x400691: child_fn(void*) (lock.cpp:6)
==7066==    by 0x4C3094E: mythread_wrapper (hg_intercepts.c:389)
==7066==    by 0x50B2DF4: start_thread (in /usr/lib64/libpthread-2.17.so)
==7066==    by 0x5BDD1AC: clone (in /usr/lib64/libc-2.17.so)
==7066==  Address 0x601040 is 0 bytes inside data symbol "var"

参考

http://www.cnblogs.com/zhuyp1015/p/3618863.html kill -11不可取 用gcore
http://blog.csdn.net/pbymw8iwm/article/details/7876797

pthread_mutex_t struct: What does lock stand for
http://stackoverflow.com/questions/23449508/pthread-mutex-t-struct-what-does-lock-stand-for

Understanding deadlock behavior with gdb
http://stackoverflow.com/questions/21017794/understanding-deadlock-behavior-with-gdb

Helgrind: a thread error detector
http://valgrind.org/docs/manual/hg-manual.html

相关文章

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

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

  • gdb thread

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

  • jstack命令:教你如何排查多线程问题

    这是之前的一个死锁案例: 一个多线程死锁案例,如何避免及解决死锁问题? 如程序中发生这样的死锁问题该如何排查呢?我...

  • GDB调试多线程

    我在最开始学gdb的时候是十分抗拒的,调试没有界面?不能用鼠标点点点?怎么看各种变量的值。。。但是又不得不用gdb...

  • iOS 安全配置

    一、关于GDB挂起问题 1.首先什么是GDB,GDB就是调试工具,在iOS中 xcode中的断点查看就是调试工具中...

  • 2. gdb的使用

    参考链接1. GDB调试2. gdb调试方法3. gdb调试示例 1. 说说 gdb gdb 是一款 UNIX 系...

  • 使用 gdb 调试死锁线程

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

  • 苏宁-大数据开发岗技术面

    1.介绍项目,重点介绍与大数据相关的项目、个人工作。 2.讲讲多线程。我说了死锁产生原因,问如何解决死锁等等。。。...

  • gdb调试多线程步骤

    1.ps -eo pid,lstart,cmd | grep proname找出进程的pid 2.gdb atta...

  • gcc常用命令

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

网友评论

    本文标题:gdb调试多线程 如何解死锁问题

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