Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

84 views
Skip to first unread message

Peter Slížik

unread,
Feb 15, 2023, 12:17:00 PM2/15/23
to
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

Weatherby,Gerard

unread,
Feb 15, 2023, 1:25:38 PM2/15/23
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>.”
--
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$>

Barry

unread,
Feb 16, 2023, 3:58:24 AM2/16/23
to


> On 15 Feb 2023, at 17:23, Peter Slížik <peter....@gmail.com> wrote:
>
> Hello,
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
>

Peter Slížik

unread,
Feb 16, 2023, 5:02:01 AM2/16/23
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
0 new messages