bcc icon indicating copy to clipboard operation
bcc copied to clipboard

How to trace all functions with USDT in Python?

Open nurdann opened this issue 1 year ago • 2 comments

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.

  1. How to trace all function invocations?
  2. 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.

nurdann avatar Aug 10 '24 00:08 nurdann

I think this is because print and open are builtin functions, when they are called, function_entry usdt won't be called.

ZhangYet avatar Aug 15 '24 02:08 ZhangYet

For __init__() can you tell which class it belongs to?

nurdann avatar Aug 16 '24 17:08 nurdann