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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言字符串分割

    C语言字符串分割在C语言中,内置的函数库中除了可以用strtok()来对字符串进行分割之外,还可以用sscannf()对字符串进行分割。sscanf()包含的头文件stdio.h原型intsscanf(constchar*str,constchar*format,…)实例:#include&lt;stdio.h&gt;intmain(){ charbuf[]="…

    2022年4月30日
    54
  • dpkg用法详解_dpkg -l

    dpkg用法详解_dpkg -ldpkg是一个Debian的一个命令行工具,它可以用来安装、删除、构建和管理Debian的软件包。下面是它的一些命令解释:1)安装软件命令行:dpkg-i示例:dpkg-iavg71flm_r28-1_i386.deb2)安装一个目录下面所有的软件包命令行:dpkg-R示例:dpkg-R/usr/local/src3)释放软件包,但是不进行配置命令

    2022年9月27日
    4
  • Java链表的基本使用

    Java链表的基本使用得到的

    2022年5月3日
    33
  • 推荐系统——LR模型「建议收藏」

    推荐系统——LR模型「建议收藏」LR模型是广义线性模型。LR模型(对数几率回归模型),虽然叫回归,但是其本质为分类。对数几率函数是一种sigmoid函数。线性模型有可解释性强、易于并行的优点。但是其难以表示非线性关系,所以模型的准确性可能不好。为了增强原始特征与拟合目标之间的非线性关系,通常需要对原始特征做一些非线性转换。常用的转换方法包括:连续特征离散化、特征之间的交叉等。离散化相当于把连续函数变成分段函数来增加非线性…

    2022年10月13日
    4
  • 前端性能的优化_概括介绍

    前端性能的优化_概括介绍之前有整理过一部分知识点,一直没有发布,因为都是有关CSS方面的零散内容;现在想想无论做什么都需要慢慢积累,所以还是决定将之前整理的相关内容验证之后慢慢分享给你们,现在看到感觉还挺有意思。好了废话不多说,直接上代码以及图例(为了让大家方便阅读,都有自己验证过程的一些图片作为分享)。1.前端性能优化点:1.4个层面与8个点。1.4个层面:1.网络层面2.构建层面3.浏览器渲染层面4.服务端层面2.8个点:1.资源的合并与压缩。2

    2025年7月21日
    5
  • webpack打包流程

    webpack打包流程webpack 是一个用于现代 JavaScript 应用程序的静态模块打包工具 当 webpack 处理应用程序时 它会在内部从一个或多个入口点构建一个依赖图 dependencygr 然后将你项目中所需的每一个模块组合成一个或多个 bundles 它们均为静态资源 用于展示你的内容 1 创建一个新项目 然后执行 npminit2 全局安装 webpack 以及 webpack cilnpminstal gwebpacknpmi gwebpac

    2025年8月27日
    1

发表回复

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

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