For example,
def test(): print "test"
I used perf record -g -p $pid
, but the result was just all about PyEval_EvalFrameEx
. How can I get the real name "test" or if can not by using perf?
Description. This perf script option is used to process perf script data using perf's built-in Python interpreter. It reads and processes the input file and displays the results of the trace analysis implemented in the given Python script, if any.
The Python interpreter uses a call stack to run a Python program. When a function is called in Python, a new frame is pushed onto the call stack for its local execution, and every time a function call returns, its frame is popped off the call stack.
perf_events provides a command line tool, perf, and subcommands for various profiling activities. This is a single interface for the different instrumentation frameworks that provide the various events. The perf command alone will list the subcommands; here is perf version 4.10 (for the Linux 4.10 kernel):
As of 2018, perf
simply doesn't have support for reading the Python stack frames (cf. a 2014 Python mailinglist discussion).
Python 3.6 has some support for Dtrace and Systemtap.
An alternative to this is Pyflame, a stochastic profiler for Python that samples python call stacks via ptrace()
. In contrast to Dtrace/Systemtap you don't need extra permissions and it also works with Python versions that are compiled without instrumentalization support.
When you use the --threads
option with Pyflame you see Python lines that call into C/C++ extensions, although the stack-trace stops at the last Python frame. But perhaps this is sufficient for your use case.
Edit: Pyflame was abandoned in the end of 2019 or so. A hacker news thread mentions the following alternatives:
You won't be able to do this with perf, that's specifically built to interface the Linux process model, decode those stack frames, etc. It's doing what it's supposed to by telling you that it was executing the function PyEval_EvalFrameEx. It would have to be extended with python-specific information to be able to actually decode Python's frame information, which isn't going to happen. Unfortunately I haven't found a really good way to debug both Python and C/C++ modules easily. It's generally pdb for one and gdb for the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With