TIP #348 Version 1.4: Substituted 'errorStack' / 'traceback'

This is not necessarily the current version of this TIP.


TIP:348
Title:Substituted 'errorStack' / 'traceback'
Version:$Revision: 1.4 $
Author:Alexandre Ferrieux <alexandre dot ferrieux at gmail dot com>
State:Draft
Type:Project
Tcl-Version:8.7
Vote:Pending
Created:Thursday, 26 February 2009
Keywords:Tcl, debugging

Abstract

This TIP proposes to add a ::tcl::errorStack variable giving a "substituted" traceback similar to Python's or gdb's ones.

Background

The ::errorInfo variable is a valuable tool for debugging; however, it yields mostly static information regarding each level of procedure call, as it only gives the static text (extracted from the source) at the call site. This leads to frustrating situations, like when an error occurs at a deep level of a recursive function, ::errorInfo repeatedly reporting "f $x [foo [bar]]" or similar un-substituted commands. In other languages, the traceback is more useful in that it contains the actual values passed as arguments.

Proposed Change

This TIP proposes to create a ::tcl::errorStack variable which is a list of lists, made of the [info level 0] lists of command-and-args at each proc level at the time of error unwinding.

Rationale

In a natural implementation, its construction is analogous to ::errorInfo's, which is built incrementally, one level at a time while popping back from the error site. The only differences are that (1) dynamic arglists (from [info level 0]) are stored, (2) the result is a true list built by (the equivalent of) lappend, and (3) the granularity is coarser than ::errorInfo's since there is just one element per proc level (and not for intervening while or foreach constructs) and no adornment like "... while executing ..." .

An associated boolean variable ::tcl::useErrorStack serves as an on/off switch, so that there is no overhead when not using the feature.

And even when enabled, the performance hit of maintaining ::errorStack is very small, since basically it amounts to growing a (usually short) list whose elements are already existing Tcl_Obj's (the [info level 0] lists). This list-growing is done carefully, reusing earlier Tcl_ListObjReplace optimizations for the in-place case, so that in routine use the allocated Tcl_Obj, intrep, and element array storage backing the list never change. This way, even code heavily relying on [catch] across dozens of proc levels won't noticeably suffer from the enabling of the feature.

Reference Implementation

The reference implementation can be found at https://sourceforge.net/tracker/?func=detail&aid=2868499&group_id=10894&atid=310894 .

An earlier, extremely slow, pure-Tcl proof of concept based on write traces on ::errorInfo can be found at http://wiki.tcl.tk/traceback .

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows