python - 我可以使用 linux perf 获得 python 调用堆栈吗?

标签 python linux perf

例如,

    def test():
        print "test"

我使用了 perf record -g -p $pid,但结果只是关于 PyEval_EvalFrameEx。如何获取真名“test”或者如果不能使用 perf?

最佳答案

截至 2018 年,perf 根本不支持读取 Python 堆栈帧(参见 a 2014 Python mailinglist discussion)。

Python 3.6 支持Dtrace and Systemtap .

另一种方法是Pyflame, a stochastic profiler for Python通过 ptrace() 对 python 调用堆栈进行采样。与 Dtrace/Systemtap 相比,您不需要额外的权限,而且它也适用于在没有工具化支持的情况下编译的 Python 版本。

当您在 Pyflame 中使用 --threads 选项时,您会看到调用 C/C++ 扩展的 Python 行,尽管堆栈跟踪在最后一个 Python 帧处停止。但也许这对您的用例来说已经足够了。

编辑: Pyflame 是 abandoned 2019年底左右。一个 hacker news thread提到以下替代方案:

关于python - 我可以使用 linux perf 获得 python 调用堆栈吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26902991/

相关文章:

linux - 使用 top/ps 识别 perf 的 PID

python - "Fatal Python error: Couldn' t 使用 CherryPy 创建 autoTLSkey 映射

python - 装饰Flask MethodView派生类中的方法

python - 这段代码有什么问题?我收到一个名称错误

python - 如何使用/dev/ptmx创建虚拟串口?

linux - 在 VirtualBox guest 中运行 perf

python - 如何计算二维列表中不同条目的数量?

linux - valgrind : Opening several suppression files at once

linux - 如何将 windows 批处理文件转换为 linux shell 脚本?

android - 我应该使用哪个事件作为 perf 的 -e 选项来获取函数分支事件?