linux抓取堆栈信息

方法1 :pstack pid

内部原理和 方法2相同,pstack 为 方法2的一个 脚本,信息没有比方法2 详细,但阻塞程序时间很短,几乎可以考虑不受影响。

方法2:gdb -p pid

thread apply all bt

通过gdb命令获取所有当前,线程堆栈信息,此方法会阻塞进程的当前运行,退出gdb 进程继续运行。

方法3:gcore -p pid

获取运行进程的当前的core文件,然后通过gdb 分析core文件。

内部使用gdb 命令 generate-core-file 生成core文件



早上发现一个进程没有相应, 试图gdb之.

(gdb) attach 30721
Attaching to program: /data0/s/bin/s, process 30721
ptrace: Operation not permitted.

晕倒, 俺已经是sudo权限了. 后来看到一篇博客说:当他已经在 gdb 某个进程时, 试图再利用 gcore 来手动产生一个 core 文件而不得(可以在gdb下用 generate-core-file 完成), 我有所启发, 莫非还有别人在gdb 这个进程.

ps 一下, 发现我的猜想是对的, 我进一步猜想, strace 也应该会失败.

0> strace -p 30721
attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted

果然如此, 两者都用到了 ptrace 这个系统调用. man ptrace 可以查看详情:

The ptrace system call provides a means by which a parent process may observe and control the execution of another pro-cess, and examine and change its core image and registers. It is primarily used to implement breakpoint debugging and system call tracing.

看linux内核详解的话, 应该能给出更深层次的解释, 我现在还留在意会阶段.


你可能感兴趣的:(linux抓取堆栈信息)