Mailing List Archive

Python: How to use the 'trace' module programmatically?
Hello,

I'm trying to analyze complex Python code. For some specific reasons, I
decided to use tracing instead of a debugger.

The first thing I tried was:

python -m trace -t /path/to/file.py

The output of this command turned out to be completely useless. The reason
is that there was a thread running in the background, doing some work
every *0.1
s* and this generated thousands of lines of tracing information. The useful
information (a reaction to my interaction with app GUI) scrolled away in a
blink.

For this reason, I decided to limit the scope of tracing. I did the
following.

The original code:

def caller():
print("I'm the caller.")
callee()
def callee():
print("Callee here.")

Code modified for tracing:

import trace

tracer = trace.Tracer(
count=0,
trace=1,
)
def outer():
print("I'm the caller.")
tracer.runfunc(inner)
def inner():
print("Callee here.")

Now I launched the program and the tracer did not generate any output. I
was hoping that this would provide complete tracing information, but only
for the limited scope of inner().

No success with tracer.run() either.

What I was able to do, when I set count=1, I was able to catch the coverage
data with tracer.results() and write them to a file. But the tracing
information was not generated even in this case.

Am I doing anything wrong?

Peter
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python: How to use the 'trace' module programmatically? [ In reply to ]
Have you tried the filter options?

?These options may be repeated multiple times.
--ignore-module=<mod>
Ignore each of the given module names and its submodules (if it is a package). The argument can be a list of names separated by a comma.
--ignore-dir=<dir>
Ignore all modules and packages in the named directory and subdirectories. The argument can be a list of directories separated by os.pathsep<https://docs.python.org/3/library/os.html#os.pathsep>.?



From: Python-list <python-list-bounces+gweatherby=uchc.edu@python.org> on behalf of Peter Sl??ik <peter.slizik@gmail.com>
Date: Wednesday, February 15, 2023 at 12:22 PM
To: python-list@python.org <python-list@python.org>
Subject: Python: How to use the 'trace' module programmatically?
*** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

Hello,

I'm trying to analyze complex Python code. For some specific reasons, I
decided to use tracing instead of a debugger.

The first thing I tried was:

python -m trace -t /path/to/file.py

The output of this command turned out to be completely useless. The reason
is that there was a thread running in the background, doing some work
every *0.1
s* and this generated thousands of lines of tracing information. The useful
information (a reaction to my interaction with app GUI) scrolled away in a
blink.

For this reason, I decided to limit the scope of tracing. I did the
following.

The original code:

def caller():
print("I'm the caller.")
callee()
def callee():
print("Callee here.")

Code modified for tracing:

import trace

tracer = trace.Tracer(
count=0,
trace=1,
)
def outer():
print("I'm the caller.")
tracer.runfunc(inner)
def inner():
print("Callee here.")

Now I launched the program and the tracer did not generate any output. I
was hoping that this would provide complete tracing information, but only
for the limited scope of inner().

No success with tracer.run() either.

What I was able to do, when I set count=1, I was able to catch the coverage
data with tracer.results() and write them to a file. But the tracing
information was not generated even in this case.

Am I doing anything wrong?

Peter
--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!m_WsH0rFVMLw4RMkVvcu-ZoClOMWVTsdB8E99Qy5Sq7ZZF1iBw5_NpLvorEe3_hYvy2kdDwe2obDr1E2ZjFCM3Of$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!m_WsH0rFVMLw4RMkVvcu-ZoClOMWVTsdB8E99Qy5Sq7ZZF1iBw5_NpLvorEe3_hYvy2kdDwe2obDr1E2ZjFCM3Of$>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python: How to use the 'trace' module programmatically? [ In reply to ]
> On 15 Feb 2023, at 17:23, Peter Slížik <peter.slizik@gmail.com> wrote:
>
> ?Hello,
>
> I'm trying to analyze complex Python code. For some specific reasons, I
> decided to use tracing instead of a debugger.
>
> The first thing I tried was:
>
> python -m trace -t /path/to/file.py
>
> The output of this command turned out to be completely useless. The reason
> is that there was a thread running in the background, doing some work
> every *0.1
> s* and this generated thousands of lines of tracing information. The useful
> information (a reaction to my interaction with app GUI) scrolled away in a
> blink.
>
> For this reason, I decided to limit the scope of tracing. I did the
> following.
>
> The original code:
>
> def caller():
> print("I'm the caller.")
> callee()
> def callee():
> print("Callee here.")
>
> Code modified for tracing:
>
> import trace
>
> tracer = trace.Tracer(
> count=0,
> trace=1,
> )
> def outer():
> print("I'm the caller.")
> tracer.runfunc(inner)


The docs show that you need to do either add the outfile to trace
or generate and write the report after runfunc returns.

I have not tested this, just read the docs out of curiosity
Here https://docs.python.org/3/library/trace.html

Barry

> def inner():
> print("Callee here.")
>
> Now I launched the program and the tracer did not generate any output. I
> was hoping that this would provide complete tracing information, but only
> for the limited scope of inner().
>
> No success with tracer.run() either.
>
> What I was able to do, when I set count=1, I was able to catch the coverage
> data with tracer.results() and write them to a file. But the tracing
> information was not generated even in this case.
>
> Am I doing anything wrong?
>
> Peter
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Python: How to use the 'trace' module programmatically? [ In reply to ]
Gerard - I did not use the filtering options. Thank you for bringing them
to my attention.

Barry - thank you for the insight.

Now the tracing works as expected. I'm not sure why it didn't work
before... Maybe the program redirected stdout?

Thank you guys,
Peter

On Thu, Feb 16, 2023 at 9:56 AM Barry <barry@barrys-emacs.org> wrote:

>
>
> On 15 Feb 2023, at 17:23, Peter Slížik <peter.slizik@gmail.com> wrote:
>
> ?Hello,
>
> I'm trying to analyze complex Python code. For some specific reasons, I
> decided to use tracing instead of a debugger.
>
> The first thing I tried was:
>
> python -m trace -t /path/to/file.py
>
> The output of this command turned out to be completely useless. The reason
> is that there was a thread running in the background, doing some work
> every *0.1
> s* and this generated thousands of lines of tracing information. The useful
> information (a reaction to my interaction with app GUI) scrolled away in a
> blink.
>
> For this reason, I decided to limit the scope of tracing. I did the
> following.
>
> The original code:
>
> def caller():
> print("I'm the caller.")
> callee()
> def callee():
> print("Callee here.")
>
> Code modified for tracing:
>
> import trace
>
> tracer = trace.Tracer(
> count=0,
> trace=1,
> )
> def outer():
> print("I'm the caller.")
> tracer.runfunc(inner)
>
>
>
> The docs show that you need to do either add the outfile to trace
> or generate and write the report after runfunc returns.
>
> I have not tested this, just read the docs out of curiosity
> Here https://docs.python.org/3/library/trace.html
>
> Barry
>
> def inner():
> print("Callee here.")
>
> Now I launched the program and the tracer did not generate any output. I
> was hoping that this would provide complete tracing information, but only
> for the limited scope of inner().
>
> No success with tracer.run() either.
>
> What I was able to do, when I set count=1, I was able to catch the coverage
> data with tracer.results() and write them to a file. But the tracing
> information was not generated even in this case.
>
> Am I doing anything wrong?
>
> Peter
> --
> https://mail.python.org/mailman/listinfo/python-list
>
>
--
https://mail.python.org/mailman/listinfo/python-list