linux命令之pstack[通俗易懂]

linux命令之pstack[通俗易懂]很多时候我们想知道在Linux下后台程序到底运行到哪里了,卡住了吗,出错了吗,最简单的我们会使用#psauxf|grep来查看后台程序的状态,可是如果想知道的更多,那就可以用到pstack这个命令了。首先举一个简单的例子(test.c)来引出这个命令 #include#include#includevoid*thread_proc(void*data)

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

很多时候我们想知道在Linux下后台程序到底运行到哪里了,卡住了吗,出错了吗,最简单的我们会使用
# ps auxf | grep <process_name>
来查看后台程序的状态,可是如果想知道的更多,那就可以用到pstack这个命令了。
首先举一个简单的例子(test.c)来引出这个命令 
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void *thread_proc(void *data)
{
    printf("a new thread creaded\n");
    while (1)
    {
        sleep(10);
    }

    return (void *)0;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, thread_proc, NULL);
    pthread_join(tid, NULL);
    return 0;
}
# gcc -g -Wall -Werror test.c -o test -pthread
# ./test  &
# ps auxf | grep test           // 得到进程号(pid)
# pstack $pid                    
结果如下
// ****begin**************************
Thread 2 (Thread 0x7f7c78fc2710 (LWP 3198)):

#0  0x0000003c0f2a6a8d in nanosleep () from /lib64/libc.so.6

#1  0x0000003c0f2a6900 in sleep () from /lib64/libc.so.6

#2  0x0000000000400624 in thread_proc ()

#3  0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0

#4  0x0000003c0f2e153d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f7c78fc4700 (LWP 3197)):

#0  0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0

#1  0x000000000040065a in main ()
// ****end**************************
简单的分析一下, Thread 1为主线程, 首先执行的是main函数,接着停在pthread_join一直等待另一个线程的结束;Thread 2为主线程create出来的线程,通过结果我们可以看到,首先执行的是clone, 执行
# man clone                 // 可以看到clone的详细介绍,这里可以理解为在main函数中创建多线程
然后调用sleep, 而最终调用的是nanosleep
所以通过pstack 就可以很容易的后台的进程现在在干什么,而且也可以用来分析卡住的进程卡在哪里了。
再来看看pstack究竟是什么
# which pstack
/usr/bin/pstack                    // 显示命令所在位置
# ll /usr/bin/pastck
lrwxrwxrwx. 1 root root 6 Aug  1 21:10 /usr/bin/pstack -> gstack                 // 原来pstack是一个链接文件
# vi /usr/bin/gstack            // 分析一下gstack, 原来pstack就是由gdb执行的shell脚本,gdb原来这么强大啊,下一篇文章就来分析一下gdb
 17 backtrace="bt"
 18 if test -d /proc/$1/task ; then
 19     # Newer kernel; has a task/ directory.
 20     if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
 21         backtrace="thread apply all bt"
 22     fi
 23 elif test -f /proc/$1/maps ; then
 24     # Older kernel; go by it loading libpthread.
 25     if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
 26         backtrace="thread apply all bt"
 27     fi
 28 fi
 38 # Run GDB, strip out unwanted noise.
 39 $GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 | 
 40 set width 0
 41 set height 0
 42 set pagination no
 43 $backtrace
 44 EOF
 45 /bin/sed -n \
 46     -e 's/^\((gdb) \)*//' \
 47     -e '/^#/p' \
 48     -e '/^Thread/p'
核心代码如上,这里先不做分析,到下一章gdb中一起分析。
文章出处    http://www.cnblogs.com/mumuxinfei/p/4366708.html

pstack检测死锁
既然pstack可以打印出该进程的所有线程的情况,那它自然就可以用来检测死锁了。首先下一个死锁的例子
# vi dead_lock.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

pthread_mutex_t mutex_1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1_proc(void *data)
{
    pthread_mutex_lock(&mutex_1);
    sleep(1);
    pthread_mutex_lock(&mutex_2);

    pthread_mutex_unlock(&mutex_2);
    pthread_mutex_unlock(&mutex_1);
    return (void *)0;
}

void *thread2_proc(void *data)
{
    pthread_mutex_lock(&mutex_2);
    sleep(1);
    pthread_mutex_lock(&mutex_1);

    pthread_mutex_unlock(&mutex_1);
    pthread_mutex_unlock(&mutex_2);
    return (void *)0;
}

int main()
{
    pthread_t tid1, tid2;
    pthread_create(&tid1, NULL, thread1_proc, NULL);
    pthread_create(&tid2, NULL, thread2_proc, NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    return 0;
}
# gcc -g -Wall -Werror dead_lock.c -pthread -o test
# ./test            // 则进程死锁一直卡住了
# pstack $pid 
Thread 3 (Thread 0x7f0489d8e710 (LWP 3616)):

#0  0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0

#1  0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0

#2  0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0

#3  0x000000000040068e in thread1_proc ()

#4  0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0

#5  0x0000003c0f2e153d in clone () from /lib64/libc.so.6

Thread 2 (Thread 0x7f048938d710 (LWP 3617)):

#0  0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0

#1  0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0

#2  0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0

#3  0x00000000004006d3 in thread2_proc ()

#4  0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0

#5  0x0000003c0f2e153d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f0489d90700 (LWP 3615)):

#0  0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0

#1  0x000000000040073d in main ()
可以看到Thread 2和Thread 3都在等待锁,就是等待别人释放自己想要锁的那把锁, 但是并不能看出来是否是死锁,继续使用gdb分析
# gdb -p $pid
# info thread        // 打印所有的线程信息
  3 Thread 0x7f645eb23710 (LWP 3687)  0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0

  2 Thread 0x7f645e122710 (LWP 3688)  0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0

* 1 Thread 0x7f645eb25700 (LWP 3686)  0x0000003c0f60803d in pthread_join () from /lib64/libpthread.so.0
*表示gdb锁定的线程,切换到第二个线程去查看
# thread 2          // 切换到第2个线程, 可以看到线程id 为 0x7f645e122710, 而LWP指定的值是gdb用来唯一标示该进程中线程的,便于调试的时候追踪
[Switching to thread 2 (Thread 0x7f645e122710 (LWP 3688))]#0  0x0000003c0f60e034 in __lll_lock_wait ()  
# bt                   // bt 可以打印函数堆栈,却无法看到函数参数,
#0  0x0000003c0f60e034 in __lll_lock_wait () from /lib64/libpthread.so.0

#1  0x0000003c0f609345 in _L_lock_870 () from /lib64/libpthread.so.0

#2  0x0000003c0f609217 in pthread_mutex_lock () from /lib64/libpthread.so.0

#3  0x00000000004006d3 in thread2_proc (data=0x0) at dead_lock.c:23

#4  0x0000003c0f6077e1 in start_thread () from /lib64/libpthread.so.0

#5  0x0000003c0f2e153d in clone () from /lib64/libc.so.6
# frame 3       // 打印第三帧信息(#2).每次函数调用都会有压栈的过程,而frame则记录栈中的帧信息,
#3  0x00000000004006d3 in thread2_proc (data=0x0) at dead_lock.c:23

23     pthread_mutex_lock(&mutex_1);
# p mutext_1  // 打印mutex_1的值 ,  __owner表示gdb中标示线程的值,即LWP
$1 = {__data = {__lock = 2, __count = 0,
__owner = 3687, __nusers = 1, __kind = 0, __spins = 0, __list = {__prev = 0x0,

      __next = 0x0}}, __size = “\002\000\000\000\000\000\000\000g\016\000\000\001”, ‘\000’ <repeats 26 times>, __align = 2}


# thread 3
# frame 3
# p mutex_2

$2 = {__data = {__lock = 2, __count = 0,
__owner = 3688, __nusers = 1, __kind = 0, __spins = 0, __list = {__prev = 0x0,

      __next = 0x0}}, __size = “\002\000\000\000\000\000\000\000h\016\000\000\001”, ‘\000’ <repeats 26 times>, __align = 2}

分析可以知道死锁了, 因为LWP(3688)在等待LWP(3687)所拥有的mutex_1, 而同时LWP(3688)又在等待LWP(3688)所拥有的mutex_2, 死锁。
如果每个线程在去取锁的时候都打印一条日志记录自己取到了哪个锁,或者正打算去取哪个锁,这样如果程序卡住的话可以通过查询日志看到是否有死锁,添加代码如下
#define pthread_mutex_lock(arg1, arg2) \
do { \
   printf("the thread %s try to  get the lock %s\n", arg1, #arg2);  \
   pthread_mutex_lock(arg2);   \
   printf("the thread %s get the lock %s\n", arg1, #arg2);  \
}while(0);
修改部分如下
pthread_mutex_lock("thread1", &mutex_1);   // thread2部分相同
sleep(1);
pthread_mutex_lock("thread1", &mutex_2);
...
重新编译后执行
# ./test
the thread thread2 try to  get the lock &mutex_2

the thread thread2 get the lock &mutex_2

the thread thread1 try to  get the lock &mutex_1

the thread thread1 get the lock &mutex_1

the thread thread2 try to  get the lock &mutex_1

the thread thread1 try to  get the lock &mutex_2
然后就可以分析得出死锁的结论。
参考博文:   http://www.cnblogs.com/mumuxinfei/p/4365697.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193818.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Swift3创建数组

    Swift3创建数组数组是由一组类型相同的元素构成的有序数据集合。数组中的集合元素是有序的,而且可以重复出现。1 数组创建在Swift语言中,数组的类型格式为:Array或[ElementType]其中Array中的ElementType表示数组的类型,是泛型写法。[ElementType]是一种简写方式。两者表示的功能是一样的,我们更偏向于使用简写形式,本书里所有数组类型都是使用简写形式。下

    2022年5月27日
    44
  • RS485接口定义

    RS485接口定义

    1.英式标识为 TDA(-) 、TDB(+) 、RDA(-) 、RDB(+) 、GND 
    2.美式标识为 Y 、Z 、 A 、 B 、 GND  
    3.中式标识为 TXD(+)/A 、TXD(-)/B 、RXD(-) 、RXD(+)、GND  
       rs485两线一般定义为:  
                 “A, B”或”Date+,Date-”  
       即常说的:”485+,485-”  
       rs485四线一般定

    2022年5月27日
    73
  • 可视化篇:效果图_可视化建模

    可视化篇:效果图_可视化建模写在最前在做可视化的时候,理解自己做的每个图形展示的意义,是多么的至关重要每做一张图的时候,我都在想,该如何阐述图形背后的故事下面是一些效果图,每张图,都只为更好地反应数据背后的那段故事。由于图片最大只能2M,所以调小后有的看起来不是很舒服,多多见谅所使用的工具主要是:R语言,Echart,D3.js如何实现在别的文章有详细说明。2016年的广州春运广州南站载客

    2022年8月31日
    5
  • vc 识别移动硬盘 U盘,本地硬盘[通俗易懂]

    vc 识别移动硬盘 U盘,本地硬盘[通俗易懂]vc 识别移动硬盘 U盘,本地硬盘

    2022年4月20日
    42
  • 此工作站和主域间的信任关系失败 又一解决办法_域与主机失去信任关系

    此工作站和主域间的信任关系失败 又一解决办法_域与主机失去信任关系在服务器的日志上,这个错误应该大家都不陌生了,错误的特征,我给大致描述一下:在域中总是会有计算机由于某种原因,导致计算机账户的密码无法和lsasecret同步系统会在计算机登陆到域的时候,提示已经丢失域的信任关系。日志大致如下:EventID:5SourceNETLOGONTypeErrorDescriptionThesessionsetupfromthecomputer…

    2022年10月19日
    4
  • 深入浅出谈开窗函数(一)

    深入浅出谈开窗函数(一)

    2021年12月5日
    48

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号