Linux查看进程栈信息

今天在Linux上调试程序程序的时候发现有时候程序会莫名其妙的hang住,于是就想能不能找到当时程序有那些线程,都在做什么。找了一下linux命令,还真可以满足我的需求。下面看一个小例子。

先准备一段程序,为了简单起见这里使用python来写,其中创建了两个线程来执行各自的任务。

import threading
import time


def test1():
    while(True):
        time.sleep(1)
        print 'test1'

def test2():
    while(True):
        time.sleep(1)
        print 'test2'


t1 = threading.Thread(target=test1, args=())
t2 = threading.Thread(target=test2, args=())
t1.start()
t2.start()

time.sleep(12345)

然后运行这个程序

$ python test.py

先使用 “pstree -apl <pid>” 查看进程结构

$ pstree -apl 26855
python,26855 test.py
  |-{python},26858
  |-{python},26859

然后使用 “ps -Lf <pid>” 查看线程信息

$ ps -Lf 26855
UID        PID  PPID   LWP  C NLWP STIME TTY      STAT   TIME CMD
jhadmin  26855 25902 26855  0    3 15:15 pts/5    Sl+    0:00 python test.py
jhadmin  26855 25902 26858  0    3 15:15 pts/5    Sl+    0:00 python test.py
jhadmin  26855 25902 26859  0    3 15:15 pts/5    Sl+    0:00 python test.py

最后,可以使用 “pstack <pid>” 查看线程的详细信息,如下:

$ pstack 26855
Thread 3 (Thread 0x7f8a344f2700 (LWP 26858)):
#0  0x00007f8a3b5387a3 in select () from /lib64/libc.so.6
#1  0x00007f8a344f5070 in time_sleep () from /usr/lib64/python2.7/lib-dynload/timemodule.so
#2  0x00007f8a3c215af0 in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#3  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#4  0x00007f8a3c1a188d in function_call () from /lib64/libpython2.7.so.1.0
#5  0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#6  0x00007f8a3c2104fd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#7  0x00007f8a3c2154bd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#8  0x00007f8a3c2154bd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#9  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#10 0x00007f8a3c1a1798 in function_call () from /lib64/libpython2.7.so.1.0
#11 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#12 0x00007f8a3c18b8d5 in instancemethod_call () from /lib64/libpython2.7.so.1.0
#13 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#14 0x00007f8a3c20e6f7 in PyEval_CallObjectWithKeywords () from /lib64/libpython2.7.so.1.0
#15 0x00007f8a3c2465c2 in t_bootstrap () from /lib64/libpython2.7.so.1.0
#16 0x00007f8a3bf1ce25 in start_thread () from /lib64/libpthread.so.0
#17 0x00007f8a3b54134d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f8a33cf1700 (LWP 26859)):
#0  0x00007f8a3b5387a3 in select () from /lib64/libc.so.6
#1  0x00007f8a344f5070 in time_sleep () from /usr/lib64/python2.7/lib-dynload/timemodule.so
#2  0x00007f8a3c215af0 in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#3  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#4  0x00007f8a3c1a188d in function_call () from /lib64/libpython2.7.so.1.0                                                                                           
#5  0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0                                                                                           
#6  0x00007f8a3c2104fd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0                                                                                      
#7  0x00007f8a3c2154bd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0                                                                                      
#8  0x00007f8a3c2154bd in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0                                                                                      
#9  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#10 0x00007f8a3c1a1798 in function_call () from /lib64/libpython2.7.so.1.0
#11 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#12 0x00007f8a3c18b8d5 in instancemethod_call () from /lib64/libpython2.7.so.1.0
#13 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#14 0x00007f8a3c20e6f7 in PyEval_CallObjectWithKeywords () from /lib64/libpython2.7.so.1.0
#15 0x00007f8a3c2465c2 in t_bootstrap () from /lib64/libpython2.7.so.1.0
#16 0x00007f8a3bf1ce25 in start_thread () from /lib64/libpthread.so.0
#17 0x00007f8a3b54134d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f8a3c6f3740 (LWP 26855)):
#0  0x00007f8a3bf22a0b in do_futex_wait.constprop.1 () from /lib64/libpthread.so.0
#1  0x00007f8a3bf22a9f in __new_sem_wait_slow.constprop.0 () from /lib64/libpthread.so.0
#2  0x00007f8a3bf22b3b in sem_wait@@GLIBC_2.2.5 () from /lib64/libpthread.so.0
#3  0x00007f8a3c242535 in PyThread_acquire_lock () from /lib64/libpython2.7.so.1.0
#4  0x00007f8a3c2461c2 in lock_PyThread_acquire_lock () from /lib64/libpython2.7.so.1.0
#5  0x00007f8a3c215af0 in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#6  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#7  0x00007f8a3c21533c in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#8  0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#9  0x00007f8a3c21533c in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#10 0x00007f8a3c217e3d in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#11 0x00007f8a3c1a1798 in function_call () from /lib64/libpython2.7.so.1.0
#12 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#13 0x00007f8a3c18b8d5 in instancemethod_call () from /lib64/libpython2.7.so.1.0
#14 0x00007f8a3c17c8e3 in PyObject_Call () from /lib64/libpython2.7.so.1.0
#15 0x00007f8a3c17c9c5 in call_function_tail () from /lib64/libpython2.7.so.1.0
#16 0x00007f8a3c17ccfb in PyObject_CallMethod () from /lib64/libpython2.7.so.1.0
#17 0x00007f8a3c232f29 in Py_Finalize () from /lib64/libpython2.7.so.1.0
#18 0x00007f8a3c244325 in Py_Main () from /lib64/libpython2.7.so.1.0
#19 0x00007f8a3b46ac05 in __libc_start_main () from /lib64/libc.so.6
#20 0x000000000040071e in _start ()

这里多说一句,如果要看java程序的栈信息,可以使用 "kill -3 <pid>" 来查看,比如:

$ nohub java Test > test.out &

$ kill -3 <pid>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏QQ音乐技术团队的专栏

ContentProvider 引发闪退之谜

CP 在跨进程调用的场景中,作为数据提供的进程称作 Server 进程,请求数据的进程称作 Client 进程。当我们享受它在跨进程场景下带来的便利时,可能未曾...

9180
来自专栏GIS讲堂

lzugis——Arcgis Server for JavaScript API之自定义InfoWindow

各位看到这个标题不要嫌烦,因为本人最近一直在研究相关的问题,所以相关文章也只能是这些,同时希望看过我的文章的朋友,我的文章能够给你帮助。

1104
来自专栏林冠宏的技术文章

js 调用百度地图,并且定位用户地址,显示省市区街,经纬度

网上的一些百度地图例子,基本上没有连套的 定位 例子。下面我分享一套我自己弄的,废话不多说,看代码,里面有注释! 1 <!DOCTYPE html> 2...

8019
来自专栏海天一树

Kaggle-Digit Recognizer kNN解决方案

一、题目 https://www.kaggle.com/c/digit-recognizer 二、kNN算法 请参考https://www.jianshu.co...

33510
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2649
来自专栏JavaEdge

CSS 全解析实战(八)-Bootstrap1 Bootstrap介绍2 Bootstrap基本用法3 Bootstrap JS组件

1042
来自专栏运维前线

Dell服务器常用管理命令总结

Dell服务器常用管理命令总结 准备新版本的 MegaCli-8.07.14-1.noarch.rpm 会把程序安装在/opt下,可以自定义安装目录,例如: ...

2377
来自专栏用户画像

绩效考核系统 数据库 sql server 代码

891
来自专栏DeveWork

几款更换WordPress 后台UI 的插件推荐

WordPress 3.8出来了,最大的改变就是全新扁平化后台UI。如果你对这个后台UI 不怎么感冒甚至有些反感(尼玛怎么那么丑啊?!)不妨用下面的插件来尽可能...

3477
来自专栏IMWeb前端团队

Zone.js 简介 & 抛砖引玉

本文作者:IMWeb 杨文坚 原文出处:IMWeb社区 未经同意,禁止转载 Zone.js是angular团队参照NodeJS的Domain,Dart...

2256

扫码关注云+社区

领取腾讯云代金券