pstack竟然是一个shell脚本,核心是调用gdb的thread apply all bt查看进程的所有线程的堆栈,之后用sed正则展示线程堆栈信息。

/proc/pid/exe是一个指向可执行文件的软连接。

#!/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}
if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then
    readnever=--readnever
else
    readnever=
fi
# Run GDB, strip out unwanted noise.
$GDB --quiet $readnever -nx /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'

最新文章

  1. git&amp;&amp;github使用方法总结
  2. OpenJDK 编译-Linux环境
  3. Leetcode jump Game
  4. 什么是web service
  5. [ javascript canvas 插件演示 ] canvas 插件演示
  6. 项目忽然出现 The superclass &quot;javax.servlet.http.HttpServlet&quot; was not found on the Java Build Path 解决方法
  7. ASP.NET MVC 入门8、ModelState与数据验证
  8. an introduction to conditional random fields
  9. swift SDWebImage使用
  10. 疑难杂症rendering(对角线上的线)
  11. 你好,C++(21)只要天还没黑,就一直在工地干活-4.3.1 while循环:只要…就一直…
  12. 简单JS多级下拉框无刷新
  13. 多线程并发 synchronized对象锁的控制与优化
  14. [原创]浅谈如何使用gcc开发NT核心驱动程序
  15. Storm流计算之项目篇(Storm+Kafka+HBase+Highcharts+JQuery,含3个完整实际项目)
  16. 写作Openwrt固件
  17. Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
  18. urllib.parse
  19. SQL Server的Descending Indexes
  20. 金蝶K/3 固定置产相关SQL语句

热门文章

  1. UVa 11361 (计数 递推) Investigating Div-Sum Property
  2. UVa 10375 (唯一分解定理) Choose and divide
  3. 基于AJAX的长轮询(long-polling)方式实现简单的聊天室程序
  4. 【C#学习笔记】指针使用
  5. Oracle中job的使用详解
  6. 淘宝语音搜索的实现——html5
  7. JAVA深复制(深克隆)与浅复制(浅克隆)
  8. android 触摸事件、点击事件的区别
  9. Xamarin Mono Android Ios 安装、破解(4.12)
  10. MVC中的ActionResult