Search code examples
python-2.7stack-traceintrospectionpdb

Log call stack in Python


I'm debugging huge web app which was not written by me. That is why i need a way to print every stack call somewhere (stdout or file).

This discussion did not help me. I was thinking about pdb but it requires interaction while i'm looking for automated solution. I have only one function call at the beginning of execution of my app and want to see all other calls it makes.

The behaviour should be somewhat similar to Unix tee command. Python interpreter should execute code and at the same time it should log all function calls from different modules.

Any suggestions?

Edited:

#!/usr/bin/env python
import openerp
if __name__ == "__main__":
    openerp.cli.main()

So from this point i'd like to log all function calls that main() does.


Solution

  • If you use the python trace module you can debug each funcion or line where that interpreter executes.

    The trace moudule can be called from the cli without modifying the program:

      #print each executed line:
      python -m trace --count -C . -t MypyFile.py
      #print only called functions:
      python -m trace --listfuncs MypyFile.py
    

    With -t each line is traced and with -l each function called.

    More info : https://docs.python.org/2/library/trace.html