1 # Utilities for Insight.
2 # Copyright (C) 1997, 1998, 1999, 2004 Red Hat
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License (GPL) as published by
6 # the Free Software Foundation; either version 2 of the License, or (at
7 # your option) any later version.
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.
15 # ----------------------------------------------------------------------
20 # keep_raised - keep a window raised
21 # sleep - wait a certain number of seconds and return
22 # toggle_debug_mode - turn debugging on and off
23 # freeze - make a window modal
24 # bp_exists - does a breakpoint exist on linespec?
26 # ----------------------------------------------------------------------
30 # A helper procedure to keep a window on top.
31 proc keep_raised {top} {
32 if {[winfo exists $top]} {
35 after 1000 [info level 0]
39 # sleep - wait a certain number of seconds then return
43 after [expr {1000 * $sec}] set __sleep_timer 1
48 # ------------------------------------------------------------------
49 # PROC: auto_step - automatically step through a program
50 # ------------------------------------------------------------------
56 set auto_step_id [after 2000 auto_step]
60 # ------------------------------------------------------------------
61 # PROC: auto_step_cancel - cancel auto-stepping
62 # ------------------------------------------------------------------
64 proc auto_step_cancel {} {
67 if {[info exists auto_step_id]} {
68 after cancel $auto_step_id
73 # ------------------------------------------------------------------
74 # PROC: tfind_cmd -- to execute a tfind command on the target
75 # ------------------------------------------------------------------
76 proc tfind_cmd {command} {
78 # need to call gdb_cmd because we want to ignore the output
79 set err [catch {gdb_cmd $command} msg]
80 if {$err || [regexp "Target failed to find requested trace frame" $msg]} {
81 tk_messageBox -icon error -title "GDB" -type ok \
91 # ------------------------------------------------------------------
92 # PROC: save_trace_command -- Saves the current trace settings to a file
93 # ------------------------------------------------------------------
94 proc save_trace_commands {} {
96 set out_file [tk_getSaveFile -title "Enter output file for trace commands"]
97 debug "Got outfile: $out_file"
98 if {$out_file != ""} {
99 gdb_cmd "save-tracepoints $out_file"
103 # ------------------------------------------------------------------
104 # PROC: do_test - invoke the test passed in
105 # This proc is provided for convenience. For any test
106 # that uses the console window (like the console window
107 # tests), the file cannot be sourced directly using the
108 # 'tk' command because it will block the console window
109 # until the file is done executing. This proc assures
110 # that the console window is free for input by wrapping
111 # the source call in an after callback.
112 # Users may also pass in the verbose and tests globals
113 # used by the testsuite.
114 # ------------------------------------------------------------------
115 proc do_test {{file {}} {verbose {}} {tests {}}} {
119 error "wrong \# args: should be: do_test file ?verbose? ?tests ...?"
122 if {$verbose != {}} {
123 set _test(verbose) $verbose
124 } elseif {![info exists _test(verbose)]} {
129 set _test(tests) $tests
132 set _test(interactive) 1
133 after 500 [list source $file]
136 # ------------------------------------------------------------------
137 # PROCEDURE: gdbtk_read_defs
138 # Reads in the defs file for the testsuite. This is usually
139 # the first procedure called by a test file. It returns
140 # 1 if it was successful and 0 if not (if run interactively
141 # from the console window) or exits (if running via dejagnu).
142 # ------------------------------------------------------------------
143 proc gdbtk_read_defs {} {
146 if {[info exists env(DEFS)]} {
147 set err [catch {source $env(DEFS)} errTxt]
149 set err [catch {source defs} errTxt]
153 if {$_test(interactive)} {
154 tk_messageBox -icon error -message "Cannot load defs file:\n$errTxt" -type ok
157 puts stderr "cannot load defs files: $errTxt\ntry setting DEFS"
165 # ------------------------------------------------------------------
166 # PROCEDURE: bp_exists
167 # Returns BPNUM if a breakpoint exists at LINESPEC or
168 # -1 if no breakpoint exists there
169 # ------------------------------------------------------------------
170 proc bp_exists {linespec} {
172 lassign $linespec foo function filename line_number addr pc_addr
174 set bps [gdb_get_breakpoint_list]
176 set bpinfo [gdb_get_breakpoint_info $bpnum]
177 lassign $bpinfo file func line pc type enabled disposition \
178 ignore_count commands cond thread hit_count user_specification
179 if {$filename == $file && $function == $func && $addr == $pc} {
188 # gridCGet - This provides the missing grid cget
191 proc gridCGet {slave option} {
192 set config_list [grid info $slave]
193 return [lindex $config_list [expr [lsearch $config_list $option] + 1]]
196 # ------------------------------------------------------------------
197 # PROC: get_disassembly_flavor - gets the current disassembly flavor.
198 # The set disassembly-flavor command is assumed to exist. This
199 # will error out if it does not.
200 # ------------------------------------------------------------------
201 proc get_disassembly_flavor {} {
202 if {[catch {gdb_cmd "show disassembly-flavor"} ret]} {
205 regexp {\"([^\"]*)\"\.} $ret dummy gdb_val
210 # ------------------------------------------------------------------
211 # PROC: list_disassembly_flavors - Lists the current disassembly flavors.
212 # Returns an empty list if the set disassembly-flavor is not supported.
213 # ------------------------------------------------------------------
214 proc list_disassembly_flavors {} {
215 catch {gdb_cmd "set disassembly-flavor"} ret_val
216 if {[regexp {Requires an argument\. Valid arguments are (.*)\.} \
217 $ret_val dummy list]} {
218 foreach elem [split $list ","] {
219 lappend vals [string trim $elem]
227 # ------------------------------------------------------------------
228 # PROC: init_disassembly_flavor - Synchs up gdb's internal disassembly
229 # flavor with the value in the preferences file.
230 # ------------------------------------------------------------------
231 proc init_disassembly_flavor {} {
232 set gdb_val [get_disassembly_flavor]
233 if {$gdb_val != ""} {
234 set def_val [pref get gdb/src/disassembly-flavor]
235 if {[string compare $def_val ""] != 0} {
236 if {[catch "gdb_cmd \"set disassembly-flavor $def_val\""]} {
237 pref set gdb/src/disassembly-flavor $gdb_val
240 pref set gdb/src/disassembly-flavor $gdb_val
245 # ------------------------------------------------------------------
246 # PROC: list_element_strcmp - to be used in lsort -command when the
247 # elements are themselves lists, and you always want to look at
249 # ------------------------------------------------------------------
250 proc list_element_strcmp {index first second} {
251 set theFirst [lindex $first $index]
252 set theSecond [lindex $second $index]
254 return [string compare $theFirst $theSecond]
257 # ------------------------------------------------------------------
258 # PROC: gdbtk_endian - returns BIG or LITTLE depending on target
260 # ------------------------------------------------------------------
262 proc gdbtk_endian {} {
263 if {[catch {gdb_cmd "show endian"} result]} {
266 if {[regexp {.*big endian} $result]} {
268 } elseif {[regexp {.*little endian} $result]} {
276 # ------------------------------------------------------------------
277 # PROC: set_bg_colors - set background and text background for
279 # ------------------------------------------------------------------
280 proc set_bg_colors {{num ""}} {
284 set ::gdb_bg_num $num
286 set ::Colors(textbg) [pref get gdb/bg/$::gdb_bg_num]
288 # calculate background as 80% of textbg
289 set ::Colors(bg) [recolor $::Colors(textbg) 80]
291 # calculate trough and activebackground as 90% of background
292 set dbg [recolor $::Colors(bg) 90]
294 r_setcolors . -background $::Colors(bg)
295 r_setcolors . -highlightbackground $::Colors(bg)
296 r_setcolors . -textbackground $::Colors(textbg)
297 r_setcolors . -troughcolor $dbg
298 r_setcolors . -activebackground $dbg
304 # ------------------------------------------------------------------
305 # PROC: r_setcolors - recursively set background and text background for
307 # ------------------------------------------------------------------
308 proc r_setcolors {w option color} {
309 debug "$w $option $color"
312 if {![catch {$w isa Balloon} result] && $result == "1"} {
315 catch {$w config $option $color}
317 foreach child [winfo children $w] {
318 r_setcolors $child $option $color
322 # ------------------------------------------------------------------
323 # PROC: recolor - returns a darker or lighter color
324 # ------------------------------------------------------------------
325 proc recolor {color percent} {
326 set c [winfo rgb . $color]
327 return [format #%02x%02x%02x [expr {($percent * [lindex $c 0]) / 25600}] \
328 [expr {($percent * [lindex $c 1]) / 25600}] [expr {($percent * [lindex $c 2]) / 25600}]]