linux进阶24——GDB(十):gdb调试死锁

1. 一个例子

2个线程,加锁后轮流输出数据,其中1个线程,误将pthread_mutex_unlock(),写成pthread_mutex_lock()代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
int g_tickets = 100;
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
 
void* thread_proc1(void* arg)
{
        while (1)
        {
                pthread_mutex_lock(&g_mutex);
                if (g_tickets > 0)
                        printf("thread 1 sell tickets:%d\n", g_tickets--);
                else
                {
                        //pthread_mutex_unlock(&g_mutex);
                        pthread_mutex_lock(&g_mutex);
                        break;
                }
                //pthread_mutex_unlock(&g_mutex);
                pthread_mutex_lock(&g_mutex);
        }
 
        return (void*)1;
}
 
void* thread_proc2(void* arg)
{
        while (1)
        {
                pthread_mutex_lock(&g_mutex);
                if (g_tickets > 0)
                        printf("thread 2 sell tickets:%d\n", g_tickets--);
                else
                {
                        pthread_mutex_unlock(&g_mutex);
                        break;
                }
                pthread_mutex_unlock(&g_mutex);
        }
 
        pthread_exit((void*)2);
}
 
int main(int argc, char*argv[])
{
        pthread_t tid1, tid2;
        void *ret1, *ret2;
 
        pthread_create(&tid1, NULL, thread_proc1, NULL);
        pthread_create(&tid2, NULL, thread_proc2, NULL);
 
        pthread_join(tid1, &ret1);
        pthread_join(tid2, &ret2);
 
        printf("ret1:%d\n",(int)ret1);
        printf("ret2:%d\n",(int)ret2);
 
        return 0;
}

2. 编译、运行

[root@localhost day9]# gcc -g lock.c -lpthread -o lock
lock.c: 在函数‘main’中:
lock.c:57:28: 警告:将一个指针转换为大小不同的整数 [-Wpointer-to-int-cast]
   57 |         printf("ret1:%d\n",(int)ret1);
      |                            ^
lock.c:58:28: 警告:将一个指针转换为大小不同的整数 [-Wpointer-to-int-cast]
   58 |         printf("ret2:%d\n",(int)ret2);
      |                            ^
[root@localhost day9]# ls
lock  lock.c
[root@localhost day9]# ./lock
thread 1 sell tickets:100

程序打印出第一行后不再进行任何打印。

3. 调试---方法1

[root@localhost ~]# ps aux | grep lock
root        97  0.0  0.0      0     0 ?        S<   20:27   0:00 [kblockd]
root       521  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root       751  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/s]
root       818  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root      1855  0.0  0.0  22900   388 pts/0    Sl+  21:51   0:00 ./lock
root      1881  0.0  0.0 112828   984 pts/1    S+   21:52   0:00 grep --color=auto lock
[root@localhost ~]# pstree -p 1855
lock(1855)─┬─{lock}(1856)
           └─{lock}(1857)
[root@localhost ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 1856    <------附加进程
Attaching to process 1856
Reading symbols from /home/gdb/day9/lock...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
(gdb) bt  <-------显示堆栈信息
#0  0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007f6a9a4cfe9b in _L_lock_883 () from /lib64/libpthread.so.0
#2  0x00007f6a9a4cfd68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x000000000040073a in thread_proc1 (arg=0x0) at lock.c:22
#4  0x00007f6a9a4cdea5 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6a9a1f69fd in clone () from /lib64/libc.so.6
(gdb) 

或者

[root@localhost ~]# ps aux | grep lock
root        97  0.0  0.0      0     0 ?        S<   20:27   0:00 [kblockd]
root       521  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root       751  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/s]
root       818  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root      1855  0.0  0.0  22900   396 pts/0    Sl+  21:51   0:00 ./lock
root      1893  0.0  0.0 112828   980 pts/1    S+   21:55   0:00 grep --color=auto lock
[root@localhost ~]# gdb 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 1855
Attaching to process 1855
Reading symbols from /home/gdb/day9/lock...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 1857]
[New LWP 1856]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f6a9a4cf017 in pthread_join () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
(gdb) thread apply all bt     <----------------查看所有线程堆栈

Thread 3 (Thread 0x7f6a9a0f7700 (LWP 1856)):
#0  0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007f6a9a4cfe9b in _L_lock_883 () from /lib64/libpthread.so.0
#2  0x00007f6a9a4cfd68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x000000000040073a in thread_proc1 (arg=0x0) at lock.c:22
#4  0x00007f6a9a4cdea5 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6a9a1f69fd in clone () from /lib64/libc.so.6

Thread 2 (Thread 0x7f6a998f6700 (LWP 1857)):
#0  0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007f6a9a4cfe9b in _L_lock_883 () from /lib64/libpthread.so.0
#2  0x00007f6a9a4cfd68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x0000000000400759 in thread_proc2 (arg=0x0) at lock.c:32
#4  0x00007f6a9a4cdea5 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6a9a1f69fd in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f6a9a8f6740 (LWP 1855)):
#0  0x00007f6a9a4cf017 in pthread_join () from /lib64/libpthread.so.0
#1  0x00000000004007fe in main (argc=1, argv=0x7ffd48398348) at lock.c:54
(gdb) 

4. 调试---方法2

查看进程号

[root@localhost ~]# ps aux | grep lock
root        97  0.0  0.0      0     0 ?        S<   20:27   0:00 [kblockd]
root       521  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root       751  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/s]
root       818  0.0  0.0      0     0 ?        S<   20:27   0:00 [xfs-eofblocks/d]
root      1855  0.0  0.0  22900   396 pts/0    Sl+  21:51   0:00 ./lock
root      1901  0.0  0.0 112828   984 pts/1    S+   21:57   0:00 grep --color=auto lock
[root@localhost ~]# 

gdb调试定位bug的位置

1)gdb test 进程号      启动gdb attach 进程

2)info threads           显示所有线程信息

3)thread 2                 调到第2个线程

4)bt                           查看第2个线程的堆栈,即可可以看到线程死锁的地方
 

[root@localhost ~]# gdb lock 1855
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
lock: 没有那个文件或目录.
Attaching to process 1855
Reading symbols from /home/gdb/day9/lock...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 1857]
[New LWP 1856]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007f6a9a4cf017 in pthread_join () from /lib64/libpthread.so.0
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
(gdb) info threads 
  Id   Target Id         Frame 
  3    Thread 0x7f6a9a0f7700 (LWP 1856) "lock" 0x00007f6a9a4d454d in __lll_lock_wait ()
   from /lib64/libpthread.so.0
  2    Thread 0x7f6a998f6700 (LWP 1857) "lock" 0x00007f6a9a4d454d in __lll_lock_wait ()
   from /lib64/libpthread.so.0
* 1    Thread 0x7f6a9a8f6740 (LWP 1855) "lock" 0x00007f6a9a4cf017 in pthread_join ()
   from /lib64/libpthread.so.0
(gdb) thread 2
[Switching to thread 2 (Thread 0x7f6a998f6700 (LWP 1857))]
#0  0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
(gdb) bt
#0  0x00007f6a9a4d454d in __lll_lock_wait () from /lib64/libpthread.so.0
#1  0x00007f6a9a4cfe9b in _L_lock_883 () from /lib64/libpthread.so.0
#2  0x00007f6a9a4cfd68 in pthread_mutex_lock () from /lib64/libpthread.so.0
#3  0x0000000000400759 in thread_proc2 (arg=0x0) at lock.c:32
#4  0x00007f6a9a4cdea5 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f6a9a1f69fd in clone () from /lib64/libc.so.6
(gdb) 
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值