pstack和dmesg

这篇博客介绍了如何利用gstack和dmesg这两个工具进行进程堆栈信息的查看和系统内核日志的检查。gstack是一个用于获取进程堆栈轨迹的shell脚本,而dmesg则用于打印或控制内核消息。通过gstack的源码分析,可以看出它依赖于gdb来获取多线程进程的堆栈信息,并且能根据系统特性选择合适的堆栈回溯方式。
摘要由CSDN通过智能技术生成

活着的时候用pstack,死了看dmesg,让我们看看pstack的样子吧:

[muten003@wknode ~]$ which pstack
/usr/bin/pstack
[muten003@wknode ~]$ file /usr/bin/pstack
/usr/bin/pstack: symbolic link to `gstack'
[muten003@wknode ~]$ which gstack
/usr/bin/gstack
[muten003@wknode ~]$ file /usr/bin/gstack
/usr/bin/gstack: POSIX shell script, ASCII text executable
[muten003@wknode ~]$ more /usr/bin/gstack
#!/bin/sh

if test $# -ne 1; then
    echo "Usage: `basename $0 .sh` <process-id>" 1>&2
    exit 1
fi

if test ! -r /proc/$1; then
    echo "Process $1 not found." 1>&2
    exit 1
fi

# GDB doesn't allow "thread apply all bt" when the process isn't
# threaded; need to peek at the process to determine if that or the
# simpler "bt" should be used.

backtrace="bt"
if test -d /proc/$1/task ; then
    # Newer kernel; has a task/ directory.
    if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
	backtrace="thread apply all bt"
    fi
elif test -f /proc/$1/maps ; then
    # Older kernel; go by it loading libpthread.
    if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
	backtrace="thread apply all bt"
    fi
fi

GDB=${GDB:-/usr/bin/gdb}

# Run GDB, strip out unwanted noise.
# --readnever is no longer used since .gdb_index is now in use.
$GDB --quiet -nx $GDBARGS /proc/$1/exe $1 <<EOF 2>&1 | 
set width 0
set height 0
set pagination no
$backtrace
EOF
/bin/sed -n \
    -e 's/^\((gdb) \)*//' \
    -e '/^#/p' \
    -e '/^Thread/p'
[muten003@wknode ~]$ which dmesg
/usr/bin/dmesg
[muten003@wknode ~]$ file /usr/bin/dmesg
/usr/bin/dmesg: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=8b0c53b5d6f76a923ef6b22a266719fd3ab0eb5b, stripped

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值