45

I know that bash -x script.sh will execute script printing each line before actual execution. How to make Perl and Python interpreters do the same?

Randall
  • 2,859
  • 1
  • 21
  • 24
Vi.
  • 37,014
  • 18
  • 93
  • 148

3 Answers3

49

Devel::Trace is the Perl analogue, the trace module is Python's.

msw
  • 42,753
  • 9
  • 87
  • 112
  • 25
    For clarification: `perl -d:Trace program.pl` and `python -m trace -t program.py` respectively. – Vi. May 21 '10 at 00:51
  • 2
    @Vi.+1 for the command-line options for Devel::Trace. FWIW: For enabling perl debugging under apache mod_perl see the following link:http://www.perlmonks.org/?node_id=412121. Haven't tried it but it might help someone else. – GuruM Aug 06 '12 at 14:56
12

python -m trace -t main.py

Test program:

main.py

from a import g
def f(i):
    g(i)
for i in range(3):
    f(i)

a.py

def g(i):
    print i

Output:

 --- modulename: main, funcname: <module>
main.py(1): from a import g
 --- modulename: a, funcname: <module>
a.py(1): def g(i):
main.py(2): def f(i):
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
0
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
1
main.py(4): for i in range(3):
main.py(5):     f(i)
 --- modulename: main, funcname: f
main.py(3):     g(i)
 --- modulename: a, funcname: g
a.py(2):     print i
2
main.py(4): for i in range(3):
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

Tested on Ubuntu 16.10, Python 2.7.12.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • This seems redundant to the other answers to the question. –  Feb 11 '17 at 17:33
  • 1
    @duskwuff thanks for feedback. It saves a click and finding that you need `-t`, and shows what you get on a minimal multi-file example. – Ciro Santilli OurBigBook.com Feb 11 '17 at 17:41
  • It's better to have the answer in the question rather than a linked document if it's practical, not just for convenience, also to avoid link rot. – Dannie Jun 22 '22 at 12:50
7

Devel::DumpTrace was released in 2011 and has more features than Devel::Trace, such as evaluating variable values in the trace output.

mob
  • 117,087
  • 18
  • 149
  • 283