bcc
bcc copied to clipboard
How to trace all functions with USDT in Python?
I have the following Python code
# sample.py
from time import sleep
def foo():
...
def main():
while True:
with open("sample.py") as f:
content = f.read()
print("fooo")
foo()
sleep(3)
main()
Running the script
$ python3 sample.py > /dev/null &
[1] 270606
And run uflow.py
$ sudo python3 ~/bcc/tools/lib/uflow.py -l python 270606
Tracing method calls in python process 270606... Ctrl-C to quit.
CPU PID TID TIME(us) METHOD
12 270606 270606 12.130 -> /usr/lib/python3.10/codecs.py.__init__
12 270606 270606 12.131 -> /usr/lib/python3.10/codecs.py.__init__
12 270606 270606 12.131 <- /usr/lib/python3.10/codecs.py.__init__
12 270606 270606 12.131 <- /usr/lib/python3.10/codecs.py.__init__
12 270606 270606 12.131 -> /usr/lib/python3.10/codecs.py.decode
12 270606 270606 12.131 <- /usr/lib/python3.10/codecs.py.decode
12 270606 270606 12.131 -> /home/foo/Downloads/sample.py.foo
12 270606 270606 12.131 <- /home/foo/Downloads/sample.py.foo
The only function I see is the one defined in the script. I would expect that calls for open() and print() to show up.
- How to trace all function invocations?
- Can you build stack trace for specific functions? From the above sample output, it's not clear which class is calling
__init__in/usr/lib/python3.10/codecs.py.
I think this is because print and open are builtin functions, when they are called, function_entry usdt won't be called.
For __init__() can you tell which class it belongs to?