首页 技术 正文
技术 2022年11月15日
0 收藏 355 点赞 3,431 浏览 2051 个字

重点是查看进程的线程中,哪个线程占用cpu过高,然后用gdb附加到进程,调试线程,看是否有死循环或者死锁等问题,步骤如下: 
1 先用ps + grep找出该死的进程pid,比如 1706

2 top -H -p 1706,(top然后shift+H可以看出某个线程,左上角有提示:thread on 则为可查看线程)所有该进程的线程都列出来, 看看哪个线程pid占用最多,记下对应的线程号,如:1723

  1. gdb attach 到进程号码(1706)
  2. (仍然在gdb中) info threads 结果大致如下:
(gdb) info threads   8 Thread 0x7f9fa9366700 (LWP 1716)  0x0000003cec00b98e in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0   7 Thread 0x7f9fa8965700 (LWP 1720)  0x0000003cec00b98e in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0   6 Thread 0x7f9fa7f64700 (LWP 1721)  0x0000003cec00f4b5 in sigwait ()    from /lib64/libpthread.so.0   5 Thread 0x7f9fa7563700 (LWP 1722)  0x0000003cec00b98e in pthread_cond_timedwait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0   4 Thread 0x7f9fa6b62700 (LWP 1723)  0x0000003cec00b5bc in pthread_cond_wait@@GLIBC_2.3.2     () from /lib64/libpthread.so.0   3 Thread 0x7f9fa6161700 (LWP 1724)  0x0000003cebce9163 in epoll_wait ()    from /lib64/libc.so.6   2 Thread 0x7f9fa1159700 (LWP 1887)  0x0000003cebce9163 in epoll_wait ()    from /lib64/libc.so.6 * 1 Thread 0x7f9fa95ad820 (LWP 1706)  0x0000003cec00b5bc in pthread_cond_wait@@GLIBC_2.3.2     () from /lib64/libpthread.so.0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

找到线程号码对应的thread(LWP1723)即是我们刚刚记下的线程号

  1. (仍然在gdb中)thread 线程号码切换到线程(4)–这里在info threads显示出来的序号需要使用gdb能识别的线程序号,即执行:thread 4切换到我们刚刚记下的线程号:1723的对应线程,如下:
(gdb) thread 4 [Switching to thread 4 (Thread 0x7f9fa6b62700 (LWP 1723))]#0  0x0000003cec00b5bc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
  • 1
  • 2

6.(仍然在gdb中)bt 查看线程调用堆栈 
(gdb) bt

#0  0x0000003cec00b5bc in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0 #1  0x00007f9fa9f7144d in IceUtil::Cond::waitImpl (this=0x263f4c8,      mutex=...) at ../../include/IceUtil/Cond.h:215 #2  0x00007f9fa9f9a4b1 in IceUtil::Monitor::wait (this=0x263f4c8)     at ../../include/IceUtil/Monitor.h:152 #3  0x00007f9fa9fd7567 in IceInternal::EndpointHostResolver::run (this=0x263f480)     at EndpointI.cpp:161 #4  0x00007f9fa9b1b975 in startHook (arg=0x263f480) at Thread.cpp:413 #5  0x0000003cec0079d1 in start_thread () from /lib64/libpthread.so.0 #6  0x0000003cebce8b6d in clone () from /lib64/libc.so.6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

7.从上面输出的信息,基本上可以查看线程对应的代码断,是否有死循环等,如果是死锁的话,需要多次查看当前线程堆栈,或者查看全部线程的堆栈,总是会有某些个线程跟其他线程不一致,然后再对应到代码来进行定位解决

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,908
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297