令人惊奇的gdb和pstack
2024-09-13 03:33:23
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'
最新文章
- git&;&;github使用方法总结
- OpenJDK 编译-Linux环境
- Leetcode jump Game
- 什么是web service
- [ javascript canvas 插件演示 ] canvas 插件演示
- 项目忽然出现 The superclass ";javax.servlet.http.HttpServlet"; was not found on the Java Build Path 解决方法
- ASP.NET MVC 入门8、ModelState与数据验证
- an introduction to conditional random fields
- swift SDWebImage使用
- 疑难杂症rendering(对角线上的线)
- 你好,C++(21)只要天还没黑,就一直在工地干活-4.3.1 while循环:只要…就一直…
- 简单JS多级下拉框无刷新
- 多线程并发 synchronized对象锁的控制与优化
- [原创]浅谈如何使用gcc开发NT核心驱动程序
- Storm流计算之项目篇(Storm+Kafka+HBase+Highcharts+JQuery,含3个完整实际项目)
- 写作Openwrt固件
- Tomcat Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
- urllib.parse
- SQL Server的Descending Indexes
- 金蝶K/3 固定置产相关SQL语句
热门文章
- UVa 11361 (计数 递推) Investigating Div-Sum Property
- UVa 10375 (唯一分解定理) Choose and divide
- 基于AJAX的长轮询(long-polling)方式实现简单的聊天室程序
- 【C#学习笔记】指针使用
- Oracle中job的使用详解
- 淘宝语音搜索的实现——html5
- JAVA深复制(深克隆)与浅复制(浅克隆)
- android 触摸事件、点击事件的区别
- Xamarin Mono Android Ios 安装、破解(4.12)
- MVC中的ActionResult