Wolfram Computation Meets Knowledge

TraceInteractive

Contributed by: Richard Hennigan (Wolfram Research)

Traces an evaluation with an interface that allows interactive debugging

ResourceFunction["TraceInteractive"][expr]

evaluates expr in an interactive interface where each step of the evaluation can be inspected.

ResourceFunction["TraceInteractive"][expr,patt]

sets patt as a breakpoint for entering subsessions.

Details and Options

ResourceFunction["TraceInteractive"] has the following options:
CreateWindow True create a new window for tracing
NotebookClose False close the window when finished

Examples

Basic Examples

Retrieve the ResourceFunction:

In[1]:=
ResourceFunction["TraceInteractive"]
Out[1]=

Trace an evaluation and stop whenever addition occurs:

In[2]:=
ResourceFunction["TraceInteractive"][# + 2 # & /@ Range[3], _Plus]
Out[2]=

The buttons at the top of the window perform the following actions:

Return: Finish the evaluation with no further interruptions and return the result.

Continue: Resume evaluation until the next breakpoint is encountered.

Next: Proceed to the next expression in the trace.

Step: Same as “Next”, except includes atomic expressions.

Additional breakpoints can be added during evaluation by clicking the "+" button to the right of the breakpoints:

To remove breakpoints, just click on them in the list.

Now pressing "Continue" will stop on multiplication as well:

The "Return" button will complete the evaluation with no further interruptions:

The colored numbers on the left of each cell correspond to the current trace level of the evaluation:

In[3]:=
factorial[0] = 1;
factorial[x_Integer?Positive] := factorial[x - 1]*x;
ResourceFunction["TraceInteractive"][factorial[4], _factorial]
Out[5]=

Options

Use the current notebook instead of creating a new one:

In[6]:=
ResourceFunction["TraceInteractive"][# + 1 & /@ Range[3], _Plus, CreateWindow -> False]
In[7]:=
1 + 1
In[8]:=
2 + 1
In[9]:=
3 + 1
Out[9]=

Automatically close the trace notebook when evaluation is complete (result is returned to the current notebook):

In[10]:=
ResourceFunction["TraceInteractive"][# + 1 & /@ Range[3], _Plus, NotebookClose -> True]
Out[10]=

Applications

Enter a subsession when an error occurs to investigate the current Stack:

In[11]:=
bytes = ByteArray[{56, 67, 58, 120, 156, 45, 200, 177, 14, 130, 48, 16, 6, 96, 67, 162, 29, 116, 209, 39, 96, 240, 77, 136, 50, 233, 2,
    139, 155, 135, 253, 75, 47, 148, 59, 210, 131, 196, 199, 183, 131,
    219, 151, 47, 84, 182, 111, 34, 62, 83, 168, 236, 210, 232, 188, 232, 38, 254, 246, 93, 50, 204, 88, 165, 172, 123, 20, 210, 136, 194, 227, 159, 79, 154, 97, 174, 133, 32, 83, 234, 14, 44, 43, 114,
    238, 78, 47, 221, 234, 33, 235, 132, 154, 215, 176, 179, 115, 155,
    116, 160, 244, 246, 218, 71, 244, 145, 101, 52, 119, 189, 19, 39, 248, 31, 134, 178, 41, 81}];
BinaryDeserialize[bytes]
Out[12]=
In[13]:=
ResourceFunction["TraceInteractive"][BinaryDeserialize[bytes], HoldPattern[General::interr]]
Out[13]=

Resource History