1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
|
# -----------------------------------------------------------------------------
# NAME:
# ::debug
#
# DESC:
# This namespace implements general-purpose debugging functions
# to display information as a program runs. In addition, it
# includes profiling (derived from Sage 1.1) and tracing. For
# output it can write to files, stdout, or use a debug output
# window.
#
# NOTES:
# Output of profiler is compatible with sageview.
#
# -----------------------------------------------------------------------------
package provide debug 1.0
namespace eval ::debug {
namespace export debug dbug
variable VERSION 1.1
variable absolute
variable stack ""
variable outfile "trace.out"
variable watch 0
variable watchstart 0
variable debugwin ""
variable tracedVars
variable logfile ""
variable initialized 0
variable stoptrace 0
variable tracing 0
variable profiling 0
variable level 0
# here's where we'll store our collected profile data
namespace eval data {
variable entries
}
proc logfile {file} {
variable logfile
if {$logfile != "" && $logfile != "stdout" && $logfile != "stderr"} {
catch {close $logfile}
}
if {$file == ""} {
set logfile ""
} elseif {$file == "stdout" || $file == "stderr"} {
set logfile $file
} else {
set logfile [open $file w+]
fconfigure $logfile -buffering line -blocking 0
}
}
# ----------------------------------------------------------------------------
# NAME: debug::trace_var
# SYNOPSIS: debug::trace_var {varName mode}
# DESC: Sets up variable trace. When the trace is activated,
# debugging messages will be displayed.
# ARGS: varName - the variable name
# mode - one of more of the following letters
# r - read
# w - write
# u - unset
# -----------------------------------------------------------------------------
proc trace_var {varName mode} {
variable tracedVars
lappend tracedVars [list $varName $mode]
uplevel \#0 trace variable $varName $mode ::debug::touched_by
}
# ----------------------------------------------------------------------------
# NAME: debug::remove_trace
# SYNOPSIS: debug::remove_trace {var mode}
# DESC: Removes a trace set up with "trace_var".
# ----------------------------------------------------------------------------
proc remove_trace {var mode} {
uplevel \#0 trace vdelete $var $mode ::debug::touched_by
}
# ----------------------------------------------------------------------------
# NAME: debug::remove_all_traces
# SYNOPSIS: debug::remove_all_traces
# DESC: Removes all traces set up with "trace_var".
# ----------------------------------------------------------------------------
proc remove_all_traces {} {
variable tracedVars
if {[info exists tracedVars]} {
foreach {elem} $tracedVars {
eval remove_trace $elem
}
unset tracedVars
}
}
# ----------------------------------------------------------------------------
# NAME: debug::touched_by
# SYNOPSIS: debug::touched_by {v a m}
# DESC: Trace function used by trace_var. Currently writes standard
# debugging messages or priority "W".
# ARGS: v - variable
# a - array element or ""
# m - mode
# ----------------------------------------------------------------------------
proc touched_by {v a m} {
if {$a==""} {
upvar $v foo
dbug W "Variable $v touched in mode $m"
} else {
dbug W "Variable ${v}($a) touched in mode $m"
upvar $v($a) foo
}
dbug W "New value: $foo"
show_call_stack 2
}
# ----------------------------------------------------------------------------
# NAME: debug::show_call_stack
# SYNOPSIS: debug::show_call_stack {{start_decr 0}}
# DESC: Function used by trace_var to print stack trace. Currently
# writes standard debugging messages or priority "W".
# ARGS: start_decr - how many levels to go up to start trace
# ----------------------------------------------------------------------------
proc show_call_stack {{start_decr 0}} {
set depth [expr {[info level] - $start_decr}]
if {$depth == 0} {
dbug W "Called at global scope"
} else {
dbug W "Stack Trace follows:"
for {set i $depth} {$i > 0} {incr i -1} {
dbug W "Level $i: [info level $i]"
}
}
}
# ----------------------------------------------------------------------------
# NAME: debug::createData
# SYNOPSIS: createData { name }
# DESC: Basically creates a data structure for storing profiling
# information about a function.
# ARGS: name - unique (full) function name
# -----------------------------------------------------------------------------
proc createData {name} {
lappend data::entries $name
namespace eval data::$name {
variable totaltimes 0
variable activetime 0
variable proccounts 0
variable timers 0
variable timerstart 0
variable nest 0
}
}
proc debugwin {obj} {
variable debugwin
set debugwin $obj
}
# -----------------------------------------------------------------------------
# NAME: debug::debug
#
# SYNOPSIS: debug { {msg ""} }
#
# DESC: Writes a message to the proper output. The priority of the
# message is assumed to be "I" (informational). This function
# is provided for compatibility with the previous debug function.
# For higher priority messages, use dbug.
#
# ARGS: msg - Message to be displayed.
# -----------------------------------------------------------------------------
proc debug {{msg ""}} {
set cls [string trimleft [uplevel namespace current] :]
if {$cls == ""} {
set cls "global"
}
set i [expr {[info level] - 1}]
if {$i > 0} {
set func [lindex [info level $i] 0]
set i [string first "::" $func]
if {$i != -1} {
# itcl proc has class prepended to func
# strip it off because we already have class in $cls
set func [string range $func [expr {$i+2}] end]
}
} else {
set func ""
}
::debug::_putdebug I $cls $func $msg
}
# -----------------------------------------------------------------------------
# NAME: debug::dbug
#
# SYNOPSIS: dbug { level msg }
#
# DESC: Writes a message to the proper output. Unlike debug, this
# function take a priority level.
#
# ARGS: msg - Message to be displayed.
# level - One of the following:
# "I" - Informational only
# "W" - Warning
# "E" - Error
# "X" - Fatal Error
# -----------------------------------------------------------------------------
proc dbug {level msg} {
set cls [string trimleft [uplevel namespace current] :]
if {$cls == ""} {
set cls "global"
}
set i [expr {[info level] - 1}]
if {$i > 0} {
set func [lindex [info level $i] 0]
} else {
set func ""
}
::debug::_putdebug $level $cls $func $msg
}
# -----------------------------------------------------------------------------
# NAME: debug::_putdebug
#
# SYNOPSIS: _putdebug { level cls func msg }
#
# DESC: Writes a message to the proper output. Will write to a debug
# window if one is defined. Otherwise will write to stdout.
#
# ARGS: msg - Message to be displayed.
# cls - name of calling itcl class or "global"
# func - name of calling function
# level - One of the following:
# "I" - Informational only
# "W" - Warning
# "E" - Error
# "X" - Fatal Error
# -----------------------------------------------------------------------------
proc _putdebug {lev cls func msg} {
variable debugwin
variable logfile
if {$debugwin != ""} {
$debugwin puts $lev $cls $func $msg
}
if {$logfile == "stdout"} {
if {$func != ""} { append cls ::$func }
puts $logfile "$lev: ($cls) $msg"
} elseif {$logfile != ""} {
puts $logfile [concat [list $lev] [list $cls] [list $func] [list $msg]]
}
}
proc _puttrace {enter lev func {ar ""}} {
variable debugwin
variable logfile
variable stoptrace
variable tracing
if {!$tracing} { return }
set func [string trimleft $func :]
if {$func == "DebugWin::put_trace" || $func == "DebugWin::_buildwin"} {
if {$enter} {
incr stoptrace
} else {
incr stoptrace -1
}
}
if {$stoptrace == 0} {
incr stoptrace
# strip off leading function name
set ar [lrange $ar 1 end]
if {$debugwin != ""} {
$debugwin put_trace $enter $lev $func $ar
}
if {$logfile != ""} {
puts $logfile [concat {T} [list $enter] [list $lev] [list $func] \
[list $ar]]
}
incr stoptrace -1
}
}
# -----------------------------------------------------------------------------
# NAME: debug::init
# SYNOPSIS: init
# DESC: Installs hooks in all procs and methods to enable profiling
# and tracing.
# NOTES: Installing these hooks slows loading of the program. Running
# with the hooks installed will cause significant slowdown of
# program execution.
# -----------------------------------------------------------------------------
proc init {} {
variable VERSION
variable absolute
variable initialized
# create the arrays for the .global. level
createData .global.
# start the absolute timer
set absolute [clock clicks]
# rename waits, exit, and all the ways of declaring functions
rename ::vwait ::original_vwait
interp alias {} ::vwait {} [namespace current]::sagevwait
createData .wait.
rename ::tkwait ::original_tkwait
interp alias {} ::tkwait {} [namespace current]::sagetkwait
rename ::exit ::original_exit
interp alias {} ::exit {} [namespace current]::sageexit
rename ::proc ::original_proc
interp alias {} ::proc {} [namespace current]::sageproc
rename ::itcl::parser::method ::original_method
interp alias {} ::itcl::parser::method {} [namespace current]::sagemethod
rename ::itcl::parser::proc ::original_itclproc
interp alias {} ::itcl::parser::proc {} [namespace current]::sageitclproc
rename ::body ::original_itclbody
interp alias {} ::body {} [namespace current]::sageitclbody
# redefine core procs
# foreach p [uplevel \#0 info procs] {
# set args ""
# set default ""
# # get the list of args (some could be defaulted)
# foreach arg [info args $p] {
# if { [info default $p $arg default] } {
# lappend args [list $arg $default]
# } else {
# lappend args $arg
# }
# }
# uplevel \#0 proc [list $p] [list $args] [list [info body $p]]
#}
set initialized 1
resetWatch 0
procEntry .global.
startWatch
}
# -----------------------------------------------------------------------------
# NAME: ::debug::trace_start
# SYNOPSIS: ::debug::trace_start
# DESC: Starts logging of function trace information.
# -----------------------------------------------------------------------------
proc trace_start {} {
variable tracing
set tracing 1
}
# -----------------------------------------------------------------------------
# NAME: ::debug::trace_stop
# SYNOPSIS: ::debug::trace_stop
# DESC: Stops logging of function trace information.
# -----------------------------------------------------------------------------
proc trace_stop {} {
variable tracing
set tracing 0
}
# -----------------------------------------------------------------------------
# NAME: debug::sagetkwait
# SYNOPSIS: sagetkwait {args}
# DESC: A wrapper function around tkwait so we know how much time the
# program is spending in the wait state.
# ARGS: args - args to pass to tkwait
# ----------------------------------------------------------------------------
proc sagetkwait {args} {
# simulate going into the .wait. proc
stopWatch
procEntry .wait.
startWatch
uplevel ::original_tkwait $args
# simulate the exiting of this proc
stopWatch
procExit .wait.
startWatch
}
# ----------------------------------------------------------------------------
# NAME: debug::sagevwait
# SYNOPSIS: sagevwait {args}
# DESC: A wrapper function around vwait so we know how much time the
# program is spending in the wait state.
# ARGS: args - args to pass to vwait
# ----------------------------------------------------------------------------
proc sagevwait {args} {
# simulate going into the .wait. proc
stopWatch
procEntry .wait.
startWatch
uplevel ::original_vwait $args
# simulate the exiting of this proc
stopWatch
procExit .wait.
startWatch
}
# -----------------------------------------------------------------------------
# NAME: debug::sageexit
# SYNOPSIS: sageexit {{value 0}}
# DESC: A wrapper function around exit so we can turn off profiling
# and tracing before exiting.
# ARGS: value - value to pass to exit
# -----------------------------------------------------------------------------
proc sageexit {{value 0}} {
variable program_name GDBtk
variable program_args ""
variable absolute
# stop the stopwatch
stopWatch
set totaltime [getWatch]
# stop the absolute timer
set stop [clock clicks]
# unwind the stack and turn off everyone's timers
stackUnwind
# disengage the proc callbacks
::original_proc procEntry {name} {}
::original_proc procExit {name args} {}
::original_proc methodEntry {name} {}
::original_proc methodExit {name args} {}
set absolute [expr {$stop - $absolute}]
# get the sage overhead time
set sagetime [expr {$absolute - $totaltime}]
# save the data
variable outfile
variable VERSION
set f [open $outfile w]
puts $f "set VERSION {$VERSION}"
puts $f "set program_name {$program_name}"
puts $f "set program_args {$program_args}"
puts $f "set absolute $absolute"
puts $f "set sagetime $sagetime"
puts $f "set totaltime $totaltime"
foreach procname $data::entries {
set totaltimes($procname) [set data::${procname}::totaltimes]
set proccounts($procname) [set data::${procname}::proccounts]
set timers($procname) [set data::${procname}::timers]
}
puts $f "array set totaltimes {[array get totaltimes]}"
puts $f "array set proccounts {[array get proccounts]}"
puts $f "array set timers {[array get timers]}"
close $f
original_exit $value
}
proc sageproc {name args body} {
# stop the watch
stopWatch
# update the name to include the namespace if it doesn't have one already
if {[string range $name 0 1] != "::"} {
# get the namespace this proc is being defined in
set ns [uplevel namespace current]
if { $ns == "::" } {
set ns ""
}
set name ${ns}::$name
}
createData $name
# create the callbacks for proc entry and exit
set ns [namespace current]
set extra "${ns}::stopWatch;"
append extra "set __.__ {};trace variable __.__ u {${ns}::stopWatch;${ns}::procExit $name;${ns}::startWatch};"
append extra "[namespace current]::procEntry $name;"
append extra "[namespace current]::startWatch;"
set args [list $args]
set body [list [concat $extra $body]]
startWatch
# define the proc with our extra stuff snuck in
uplevel ::original_proc $name $args $body
}
proc sageitclbody {name args body} {
# stop the watch
stopWatch
if {$name == "iwidgets::Scrolledwidget::_scrollWidget"} {
# Hack. This causes too many problems for the scrolled debug window
# so just don't include it in the profile functions.
uplevel ::original_itclbody $name [list $args] [list $body]
return
}
set fullname $name
# update the name to include the namespace if it doesn't have one already
if {[string range $name 0 1] != "::"} {
# get the namespace this proc is being defined in
set ns [uplevel namespace current]
if { $ns == "::" } {
set ns ""
}
set fullname ${ns}::$name
}
createData $fullname
# create the callbacks for proc entry and exit
set ns [namespace current]
set extra "${ns}::stopWatch;"
append extra "set __.__ {};trace variable __.__ u {${ns}::stopWatch;${ns}::procExit $fullname;${ns}::startWatch};"
append extra "[namespace current]::procEntry $fullname;"
append extra "[namespace current]::startWatch;"
set args [list $args]
set body [list [concat $extra $body]]
startWatch
# define the proc with our extra stuff snuck in
uplevel ::original_itclbody $name $args $body
}
proc sageitclproc {name args} {
# stop the watch
stopWatch
set body [lindex $args 1]
set args [lindex $args 0]
if {$body == ""} {
set args [list $args]
set args [concat $args $body]
} else {
# create the callbacks for proc entry and exit
set ns [namespace current]
set extra "${ns}::stopWatch;"
append extra "set __.__ {};trace variable __.__ u {${ns}::stopWatch;${ns}::methodExit $name;${ns}::startWatch};"
append extra "[namespace current]::methodEntry $name;"
append extra "[namespace current]::startWatch;"
set args [list $args [concat $extra $body]]
}
startWatch
uplevel ::original_itclproc $name $args
}
proc sagemethod {name args} {
# stop the watch
stopWatch
set body [lindex $args 1]
set args [lindex $args 0]
if {[string index $body 0] == "@" || $body == ""} {
set args [list $args]
set args [concat $args $body]
} else {
# create the callbacks for proc entry and exit
set ns [namespace current]
set extra "${ns}::stopWatch;"
append extra "set __.__ {};trace variable __.__ u {${ns}::stopWatch;${ns}::methodExit $name;${ns}::startWatch};"
append extra "[namespace current]::methodEntry $name;"
append extra "[namespace current]::startWatch;"
set args [list $args [concat $extra $body]]
}
startWatch
uplevel ::original_method $name $args
}
proc push {v} {
variable stack
variable level
lappend stack $v
incr level
}
proc pop {} {
variable stack
variable level
set v [lindex $stack end]
set stack [lreplace $stack end end]
incr level -1
return $v
}
proc look {} {
variable stack
return [lindex $stack end]
}
proc stackUnwind {} {
# Now unwind all the stacked procs by calling procExit on each.
# It is OK to use procExit on methods because the full name
# was pushed on the stack
while { [set procname [look]] != "" } {
procExit $procname
}
}
# we need args because this is part of a trace callback
proc startWatch {args} {
variable watchstart
set watchstart [clock clicks]
}
proc resetWatch {value} {
variable watch
set watch $value
}
proc stopWatch {} {
variable watch
variable watchstart
set watch [expr {$watch + ([clock clicks] - $watchstart)}]
return $watch
}
proc getWatch {} {
variable watch
return $watch
}
proc startTimer {v} {
if { $v != "" } {
set data::${v}::timerstart [getWatch]
}
}
proc stopTimer {v} {
if { $v == "" } return
set stop [getWatch]
set data::${v}::timers [expr {[set data::${v}::timers] + ($stop - [set data::${v}::timerstart])}]
}
proc procEntry {procname} {
variable level
_puttrace 1 $level $procname [uplevel info level [uplevel info level]]
set time [getWatch]
# stop the timer of the caller
set caller [look]
stopTimer $caller
incr data::${procname}::proccounts
if { [set data::${procname}::nest] == 0 } {
set data::${procname}::activetime $time
}
incr data::${procname}::nest
# push this proc on the stack
push $procname
# start the timer for this
startTimer $procname
}
proc methodEntry {procname} {
variable level
set time [getWatch]
# stop the timer of the caller
set caller [look]
stopTimer $caller
# get the namespace this method is in
set ns [uplevel namespace current]
if { $ns == "::" } {
set ns ""
}
set name ${ns}::$procname
_puttrace 1 $level $name [uplevel info level [uplevel info level]]
if {![info exists data::${name}::proccounts]} {
createData $name
}
incr data::${name}::proccounts
if { [set data::${name}::nest] == 0 } {
set data::${name}::activetime $time
}
incr data::${name}::nest
# push this proc on the stack
push $name
# start the timer for this
startTimer $name
}
# we need the args because this is called from a vartrace handler
proc procExit {procname args} {
variable level
set time [getWatch]
# stop the timer of the proc
stopTimer [pop]
_puttrace 0 $level $procname
set r [incr data::${procname}::nest -1]
if { $r == 0 } {
set data::${procname}::totaltimes \
[expr {[set data::${procname}::totaltimes] \
+ ($time - [set data::${procname}::activetime])}]
}
# now restart the timer of the caller
startTimer [look]
}
proc methodExit {procname args} {
variable level
set time [getWatch]
# stop the timer of the proc
stopTimer [pop]
# get the namespace this method is in
set ns [uplevel namespace current]
if { $ns == "::" } {
set ns ""
}
set procname ${ns}::$procname
_puttrace 0 $level $procname
set r [incr data::${procname}::nest -1]
if { $r == 0 } {
set data::${procname}::totaltimes \
[expr {[set data::${procname}::totaltimes] \
+ ($time - [set data::${procname}::activetime])}]
}
# now restart the timer of the caller
startTimer [look]
}
}
|