Tcl Source Code

View Ticket
Login
Ticket UUID: afa8c9f5bbde5127175f4a9f118106aed36f6773
Title: Error in an execution trace reports line 1 and loses the traced command's frame in errorInfo
Type: Bug Created on: 2026-10-17 10:51:12
Submitter: serhiy.storchaka Assigned to: jan.nijtmans
Subsystem: 46. Traces Severity: Minor
Priority: 5 Medium Last modified: 2026-11-27 15:26:47
Status: Pending Closed by: nobody
Resolution: Fixed Closed on:
Version: 8.6.18, 9.0.5, 9.1.0
Description:

When an execution trace (enter or leave) raises an error, the errorInfo has the wrong line number and loses the frame of the traced command. File x.tcl:

proc p args {}
trace add execution p leave {error boom;#}

p

[catch {source x.tcl} m o; puts [dict get $o -errorinfo]] in 8.6.1 and later (also 9.0.5 and main):

boom
    while executing
"error boom"
    (leave trace on "p")
    (file "x.tcl" line 1)
    invoked from within
"source x.tcl"

8.5.19 says (file "x.tcl" line 4), with invoked from within "p" before it (8.6.0 also says line 4, but drops the error message of the trace, see [2438181fff]). The same happens with enter traces, in proc bodies ((procedure "q" line 1)) and in [eval] bodies. Variable traces are not affected ((write trace on "v"), invoked from within "set v 1", correct line).

Cause: the fix for [010f4162ef] ([f0e808cc64], 8.6.1) appends (enter/leave trace on "...") in TEOV_RunEnterTraces() / TEOV_RunLeaveTraces() in generic/tclBasic.c and then sets ERR_ALREADY_LOGGED. That flag makes the caller skip TclLogCommandInfo(), which is where the line number of the failing command is computed and the invoked from within frame is added, so the line stays 1.

A fix would need to keep that logging in the caller while still avoiding the doubled -errorstack level from [010f4162ef].

User Comments:
serhiy.storchaka added on 2026-10-17 12:10:54:

Proposed fix in branch trace-error-keep-frame: the trace code sets a new flag ERR_STACK_LOGGED instead of ERR_ALREADY_LOGGED, so TclLogCommandInfo() still adds the frame and line number to errorInfo and only skips the errorstack level (the doubled level of [010f4162ef] does not come back, error-4.8 passes). Tests trace-bug-afa8c9f5bb-1 and -2.


jan.nijtmans added on 2026-11-27 15:26:47:

Merged to [f158fb9ffa|trunk]