例如,
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/