[Tutor] trace / profile function calls with inputs

Alan Gauld alan.gauld at btinternet.com
Mon Feb 10 02:04:12 CET 2014


On 08/02/14 16:36, Richard Cziva wrote:

> I am trying to print out every function that is being called while my
> Python program is running (own functions and library calls too). I can
> not modify the Python programs I am trying to profile.

I don't know of any way of doing this exactly as you want it.

> def foo(x):
>    y = math.cos(x)
>    z = 1 + 1
>    time.sleep(y+1)
>    return x * 50
>
> And it calls the function:
>
> print foo(100)
>
> I would like to retrieve an execution trace that shows me each function
> called with the value or hash of its arguments. According to the
> example, I am looking for a technique to extract something similar:
>
> foo(100)
> math.cos(100)
> time.sleep(0.87)

You say functions. But what about the functions used to implement 
operators like +,-,*,+= etc?

For classes they are methods. Should it list all of those calls
to? If so what about the operations on built in classes?
The x * 50 above for example? What about operations on literals?
z = 1+1 for instance?

If you know all the functions you could use a debugger and just
watch all functions and log the output.

How are you planning on analyzing the results? Sometimes a more 
intelligent and considered approach is better than just logging
everything...

HTH,
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list