Trace Calls to Functions

DESCRIPTION:
Adds to or deletes from the list of functions that are to be traced.

USAGE:
trace(what, tracer=T, exit=<<see below>>, at=1, print=T)
untrace(what=<<see below>>)

OPTIONAL ARGUMENTS:
what:
character vector, giving the names of functions: trace adds the functions to the list of those whose calls are traced and untrace removes them from that list. This may be the unquoted name of a function if there is only one. This argument is required for trace, but not for untrace.
tracer:
a function, the name of a function, or a logical value. If TRUE, the standard tracing function, std.trace, is used, which prints a line giving the function call itself before fun is called.
exit:
either TRUE or a function to be used as the exit tracing function. If TRUE, then std.xtrace is used as the exit tracing function. By default, there is no exit tracing.
at:
a location inside the body of the traced function at which the tracing is to be inserted. At the moment, this must be a number. If the function body consists of a braced expression, tracing will be inserted before the corresponding subexpression.
print:
logical flag: if TRUE, the tracing code prints a message (e.g., "On entry") to identify the location of the trace, before calling the tracing function. Usually this is helpful, but you can set the option to FALSE if you want silent tracing.

VALUE:
what is returned invisibly.

SIDE EFFECTS:
trace creates, for each function named in what, a new version on the session database, and trace also updates .Tracelist.

untrace removes the altered functions from the session database. If there is no argument to untrace, all functions are removed from .Tracelist.


DETAILS:
Tracing is accomplished by putting a revised version of the functions into the session database. Each call to trace for a given function replaces any previous tracing for that function. An object called .Tracelist on the session database lists the functions that are currently being traced. The .Traceon object in the session database tells whether tracing is on or off (see trace.on).

You can perform: tprint(myfun) to see where the at argument will put the tracing. This prints the body of the function myfun, with each subexpression numbered.

Tracing will only be performed during the current session of S-PLUS. Each call to trace for a given function replaces any previous tracing for that function.

The inspect function now provides a more general interactive debugging environment, including browsing and tracing, together with most of the functionality of the debugger function.


WARNING:
ALWAYS untrace a function before attempting to modify a traced function. Because tracing inserts a modified version of the traced function in the session database, it is frustrating and confusing to attempt to edit the traced function while tracing is in effect. (If you forget to untrace a function before editing it, you will end up with the tracing being part of your permanent function; eventually, you will need to edit this out.)

SEE ALSO:
trace.on , debugger , dump.calls , inspect , traceback , browser .

EXAMPLES:
trace(parse) # record all calls to the parse function

trace(c("sum", "cumsum"), exit=T)

untrace() # remove tracing from all functions