| 1 |
# Copyright (C) 2024-2025 Free Software Foundation, Inc. |
| 2 |
# Contributed by David Malcolm <[email protected]>. |
| 3 |
|
| 4 |
# This program is free software; you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation; either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with GCC; see the file COPYING3. If not see |
| 16 |
# <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
# Get the 1-based line for LINENUM from FILENAME as a string |
| 19 |
|
| 20 |
proc get_line { filename linenum } { |
| 21 |
set f [open $filename] |
| 22 |
set lines [split [read $f] \n] |
| 23 |
close $f |
| 24 |
return [lindex $lines [expr $linenum - 1] ] |
| 25 |
} |
| 26 |
|
| 27 |
# Print a backtrace of the Tcl interpreter's stack, showing |
| 28 |
# frames, levels, source file and line where available. |
| 29 |
# |
| 30 |
# This isn't used anywhere, but is occasionally very helpful |
| 31 |
# to use when debugging. |
| 32 |
|
| 33 |
proc print_stack_backtrace {} { |
| 34 |
set current_frame_level [info frame] |
| 35 |
puts "VVV START OF BACKTRACE VVV" |
| 36 |
for {set i [expr $current_frame_level - 1]} {$i > 0} {incr i -1} { |
| 37 |
set frame [info frame $i] |
| 38 |
if { [dict exists $frame "level"] } { |
| 39 |
set level_num [dict get $frame "level"] |
| 40 |
set relative_level_offset [expr 1 - $level_num] |
| 41 |
set level [info level $relative_level_offset] |
| 42 |
set procname [lindex $level 0] |
| 43 |
# TODO: args = rest of $level, but this can be very long |
| 44 |
} else { |
| 45 |
set procname "" |
| 46 |
} |
| 47 |
set suffix "" |
| 48 |
if { $procname != "" } { |
| 49 |
set suffix " in proc $procname" |
| 50 |
} |
| 51 |
if { [dict get $frame "type"] == "source" } { |
| 52 |
set fname [dict get $frame "file"] |
| 53 |
set line [dict get $frame "line"] |
| 54 |
puts " $fname:$line: frame $i$suffix" |
| 55 |
puts " $line | [get_line $fname $line]" |
| 56 |
} else { |
| 57 |
set type [dict get $frame "type"] |
| 58 |
puts " <$type>: frame $i$suffix" |
| 59 |
} |
| 60 |
} |
| 61 |
puts "^^^ END OF BACKTRACE ^^^" |
| 62 |
} |