gdb 死锁线程
2015-07-02 09:21
405 查看
1.调试文件 lock.c
[cpp] view
plaincopy
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *work_thread(void *arg)
{
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, 0);
usleep(1000*1000);
fprintf(stderr, "timeout we will start dead lock\n");
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex);
}
void *alive_thread(void *arg)
{
while (1)
{
usleep(1000*1000);
}
}
int main()
{
pthread_t alive_pid;
pthread_create(&alive_pid, 0, alive_thread, 0);
pthread_t dead_pid;
pthread_create(&dead_pid, 0, work_thread, 0);
void *ret = NULL;
pthread_join(dead_pid, &ret);
void *ret2 = NULL;
pthread_join(alive_pid, &ret2);
return 0;
}
2.编译运行 lock.c
[root@localhost ~]# gcc -g lock.c -pthread
[root@localhost ~]# ./a.out
timeout we will start dead lock
(程序挂起)
3.查找进程id
[root@localhost ~]# ps -e | grep a.out
12826 pts/3 00:00:00 a.out //进程id为12826
4.启动gdb attach 进程
[root@localhost ~]# gdb a.out 12826
GNU gdb (GDB) CentOS (7.0.1-45.el5.centos)
Copyright (C) 2009 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 "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...done.
Attaching to program: /root/a.out, process 12826
Reading symbols from /lib/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0xb7524b90 (LWP 12828)]
[New Thread 0xb7f25b90 (LWP 12827)]
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00502402 in __kernel_vsyscall ()
(gdb) info threads //显示所有线程信息
3 Thread 0xb7f25b90 (LWP 12827) 0x00502402 in __kernel_vsyscall ()
2 Thread 0xb7524b90 (LWP 12828) 0x00502402 in __kernel_vsyscall ()
* 1 Thread 0xb7f266c0 (LWP 12826) 0x00502402 in __kernel_vsyscall ()
(gdb) thread 2 //跳到第2个线程
[Switching to thread 2 (Thread 0xb7524b90 (LWP 12828))]#0 0x00502402 in __kernel_vsyscall ()
(gdb) bt //查看线程2的堆栈,可以发现该线程堵塞在lock.c第17行
#0 0x00502402 in __kernel_vsyscall ()
#1 0x0072e839 in __lll_lock_wait () from /lib/libpthread.so.0
#2 0x00729e9f in _L_lock_885 () from /lib/libpthread.so.0
#3 0x00729d66 in pthread_mutex_lock () from /lib/libpthread.so.0
#4 0x080485b4 in work_thread (arg=0x0) at lock.c:17
#5 0x00727912 in start_thread () from /lib/libpthread.so.0
#6 0x0066660e in clone () from /lib/libc.so.6
(gdb)
参考自 http://blog.csdn.net/openxmpp/article/details/8615000
[cpp] view
plaincopy
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void *work_thread(void *arg)
{
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, 0);
usleep(1000*1000);
fprintf(stderr, "timeout we will start dead lock\n");
pthread_mutex_lock(&mutex);
pthread_mutex_lock(&mutex);
}
void *alive_thread(void *arg)
{
while (1)
{
usleep(1000*1000);
}
}
int main()
{
pthread_t alive_pid;
pthread_create(&alive_pid, 0, alive_thread, 0);
pthread_t dead_pid;
pthread_create(&dead_pid, 0, work_thread, 0);
void *ret = NULL;
pthread_join(dead_pid, &ret);
void *ret2 = NULL;
pthread_join(alive_pid, &ret2);
return 0;
}
2.编译运行 lock.c
[root@localhost ~]# gcc -g lock.c -pthread
[root@localhost ~]# ./a.out
timeout we will start dead lock
(程序挂起)
3.查找进程id
[root@localhost ~]# ps -e | grep a.out
12826 pts/3 00:00:00 a.out //进程id为12826
4.启动gdb attach 进程
[root@localhost ~]# gdb a.out 12826
GNU gdb (GDB) CentOS (7.0.1-45.el5.centos)
Copyright (C) 2009 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 "i386-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/a.out...done.
Attaching to program: /root/a.out, process 12826
Reading symbols from /lib/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
[New Thread 0xb7524b90 (LWP 12828)]
[New Thread 0xb7f25b90 (LWP 12827)]
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.2
0x00502402 in __kernel_vsyscall ()
(gdb) info threads //显示所有线程信息
3 Thread 0xb7f25b90 (LWP 12827) 0x00502402 in __kernel_vsyscall ()
2 Thread 0xb7524b90 (LWP 12828) 0x00502402 in __kernel_vsyscall ()
* 1 Thread 0xb7f266c0 (LWP 12826) 0x00502402 in __kernel_vsyscall ()
(gdb) thread 2 //跳到第2个线程
[Switching to thread 2 (Thread 0xb7524b90 (LWP 12828))]#0 0x00502402 in __kernel_vsyscall ()
(gdb) bt //查看线程2的堆栈,可以发现该线程堵塞在lock.c第17行
#0 0x00502402 in __kernel_vsyscall ()
#1 0x0072e839 in __lll_lock_wait () from /lib/libpthread.so.0
#2 0x00729e9f in _L_lock_885 () from /lib/libpthread.so.0
#3 0x00729d66 in pthread_mutex_lock () from /lib/libpthread.so.0
#4 0x080485b4 in work_thread (arg=0x0) at lock.c:17
#5 0x00727912 in start_thread () from /lib/libpthread.so.0
#6 0x0066660e in clone () from /lib/libc.so.6
(gdb)
参考自 http://blog.csdn.net/openxmpp/article/details/8615000
相关文章推荐
- JAVA的用途是什么?如果学好JAVA能干什么【转】
- 【C语言】模拟实现库函数strcat函数
- enumerateObjectsUsingBlock、enumerateObjectsWithOptions、enumerateObjectsAtIndexes、makeObjectsPerfor使用
- acdream 1222 Quantization Problem [dp]
- Factorial
- PHP 开放JSON格式接口实例
- iOS高级开发, 开机启动, 无限后台运行, 监听进程
- Log4j详细使用教程
- linux fork()函数详解
- 【C语言】模拟实现库函数srtcpy函数
- Invert Binary Tree
- Cocos2d-x 3.0 红孩儿私人义务教育 - 第一章 熟人Cocos2d-x 3.0project
- JQ 回顶部
- CentOS用户管理及分组管理(三)
- Android开发设计模式之——单例模式
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools
- ubuntu 文件及子文件夹的权限的查看及修改
- 【C语言】实现一个函数求字符串长度(不能创建第三方变量)
- 《编程导论(Java)·3.2.4 循环语句》
- Linux设备管理文件系统,mdev,热插拔