【原创】pstack 执行解析

简介:

首先,确认 pstack 只是一个 shell 脚本。  
[root@Betty ~]# cat `which pstack`
#!/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'
[root@Betty ~]#
其次,找个目标进程用于分析执行过程。  
[root@Betty ~]# ps aux|grep redis
root      1879  0.0  0.4 145624 15692 ?        Tsl  May03   0:58 /usr/local/bin/redis-server *:6379              
root     24960  0.0  0.0 103256   856 pts/1    S+   16:44   0:00 grep redis
[root@Betty ~]#
接着查看 pstack 脚本执行过程中个变量的值。  
[root@Betty ~]# sh -x ./pstack 1879
+ test 1 -ne 1
+ test '!' -r /proc/1879
+ backtrace=bt
+ test -d /proc/1879/task
++ /bin/ls /proc/1879/task
++ /usr/bin/wc -l
+ test 3 -gt 1
+ backtrace='thread apply all bt'
+ GDB=/usr/bin/gdb
+ /usr/bin/gdb -nx --quiet --batch --readnever
+ readnever=--readnever
+ /usr/bin/gdb --quiet --readnever -nx /proc/1879/exe 1879
+ /bin/sed -n -e 's/^\((gdb) \)*//' -e '/^#/p' -e '/^Thread/p'
Thread 3 (Thread 0x7f53e67ff700 (LWP 1884)):
#0  0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x000000000045d735 in bioProcessBackgroundJobs ()
#2  0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3  0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f53e5dfe700 (LWP 1885)):
#0  0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x000000000045d735 in bioProcessBackgroundJobs ()
#2  0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3  0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f53ed0c9720 (LWP 1879)):
#0  0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
#1  0x0000000000419ece in aeProcessEvents ()
#2  0x000000000041a26b in aeMain ()
#3  0x00000000004236ad in main ()
[root@Betty ~]#
最后,手动执行 gdb 查看对应的输出。  
[root@Betty ~]# /usr/bin/gdb --quiet --readnever -nx /proc/1879/exe 1879
Reading symbols from /proc/1879/exe...(no debugging symbols found)...done.
Attaching to program: /proc/1879/exe, process 1879
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 1885]
[New LWP 1884]
[Thread debugging using libthread_db enabled]
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64
(gdb) set width 0
(gdb) set height 0
(gdb) set pagination no
(gdb) thread apply all bt

Thread 3 (Thread 0x7f53e67ff700 (LWP 1884)):
#0  0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x000000000045d735 in bioProcessBackgroundJobs ()
#2  0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3  0x0000003224ce893d in clone () from /lib64/libc.so.6

Thread 2 (Thread 0x7f53e5dfe700 (LWP 1885)):
#0  0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x000000000045d735 in bioProcessBackgroundJobs ()
#2  0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3  0x0000003224ce893d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f53ed0c9720 (LWP 1879)):
#0  0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
#1  0x0000000000419ece in aeProcessEvents ()
#2  0x000000000041a26b in aeMain ()
#3  0x00000000004236ad in main ()
(gdb)
恩恩,又学到了新知识~~  

目录
相关文章
|
安全 Linux Shell
docker运行centos提示Operation not permitted
通过上述步骤,可以有效排查和解决在Docker中运行CentOS容器时遇到的"Operation not permitted"错误。这些措施涵盖了从权限配置、安全策略到容器运行参数的各个方面,确保在不同环境和使用场景下都能顺利运行容器。如果你需要进一步优化和管理你的Docker环境
1710 3
|
1天前
|
人工智能 安全 测试技术
|
3天前
|
云安全 人工智能 安全
阿里云 Agentic SOC 位居 IDC MarketScape安全运营智能体2026领导者类别
以 Agentic AI 重构安全运营闭环,阿里云云安全在产品能力与市场份额
1160 3
|
3天前
|
数据采集 机器学习/深度学习 人工智能
田间杂草定位与检测4200张YOLO智慧农业数据集分享
本数据集含4200张真实农田图像,YOLO格式,单类别(杂草)高质量标注,覆盖多作物、多光照、多生长阶段等复杂场景,专为智慧农业杂草检测与智能除草设备研发设计,支持YOLOv5/v8/v10等主流模型训练。
351 93
|
4天前
|
缓存 UED 开发者
Codex109天重置23次,明天还要再送一次
Codex近109天完成23次额度重置,7月14日将迎来第24次。Tibo高频响应用户反馈:优化GPT-5.6高消耗问题、补发失效福利、调整重置时间——形成“反馈→回应→修复→补偿”正向闭环,彰显以用户为中心的产品哲学。(239字)
575 11
|
7天前
|
存储 人工智能 JSON
Qwen 本地部署搭配 ComfyUI 生成 AI 漫剧完整实操指南(小白零基础可落地,零成本无限生成+角色一致性天花板)
2026全网最优本地漫剧流水线:零成本、离线运行、角色统一、低配(8G显卡)可跑。融合Qwen本地大模型+ComfyUI双引擎,实现剧本生成→分镜绘图→动态成片全自动,隐私安全、无审核限流,新手30分钟上手,日更无忧。(239字)
|
8天前
|
人工智能 缓存 JSON
刚刚 GPT-5.6 发布,吊打 Claude 5 和 Grok 4.5?一手实测来啦!
GPT-5.6 刚发布,跑分号称超越 Fable 5,真的假的?附一手实测,让 3 个最强 AI 编程模型 GPT-5.6 Sol、Claude Fable 5、Grok 4.5 在 Cursor 里同时一把梭开发网页游戏,看看最新模型到底谁更能打。
364 0

热门文章

最新文章