GDB (API)
/home/stan/gdb/src/gdb/gdbtk/library/util.tcl
Go to the documentation of this file.
00001 # Utilities for Insight.
00002 # Copyright (C) 1997, 1998, 1999, 2004 Red Hat
00003 #
00004 # This program is free software; you can redistribute it and/or modify it
00005 # under the terms of the GNU General Public License (GPL) as published by
00006 # the Free Software Foundation; either version 2 of the License, or (at
00007 # your option) any later version.
00008 #
00009 # This program is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 # GNU General Public License for more details.
00013 
00014 
00015 # ----------------------------------------------------------------------
00016 # Misc routines
00017 #
00018 #   PROCS:
00019 #
00020 #     keep_raised - keep a window raised
00021 #     sleep - wait a certain number of seconds and return
00022 #     toggle_debug_mode - turn debugging on and off
00023 #     freeze - make a window modal
00024 #     bp_exists - does a breakpoint exist on linespec?
00025 #
00026 # ----------------------------------------------------------------------
00027 #
00028 
00029 
00030 # A helper procedure to keep a window on top.
00031 proc keep_raised {top} {
00032   if {[winfo exists $top]} {
00033     raise $top
00034     wm deiconify $top
00035     after 1000 [info level 0]
00036   }
00037 }
00038 
00039 # sleep - wait a certain number of seconds then return
00040 proc sleep {sec} {
00041   global __sleep_timer
00042   set __sleep_timer 0
00043   after [expr {1000 * $sec}] set __sleep_timer 1
00044   vwait __sleep_timer
00045 }
00046 
00047 
00048 # ------------------------------------------------------------------
00049 #  PROC:  auto_step - automatically step through a program
00050 # ------------------------------------------------------------------
00051 
00052 # FIXME FIXME
00053 proc auto_step {} {
00054   global auto_step_id
00055 
00056   set auto_step_id [after 2000 auto_step]
00057   gdb_cmd next
00058 }
00059 
00060 # ------------------------------------------------------------------
00061 #  PROC:  auto_step_cancel - cancel auto-stepping
00062 # ------------------------------------------------------------------
00063 
00064 proc auto_step_cancel {} {
00065   global auto_step_id
00066 
00067   if {[info exists auto_step_id]} {
00068     after cancel $auto_step_id
00069     unset auto_step_id
00070   }
00071 }
00072 
00073 # ------------------------------------------------------------------
00074 #  PROC:  tfind_cmd -- to execute a tfind command on the target
00075 # ------------------------------------------------------------------
00076 proc tfind_cmd {command} {
00077   gdbtk_busy
00078   # need to call gdb_cmd because we want to ignore the output
00079   set err [catch {gdb_cmd $command} msg]
00080   if {$err || [regexp "Target failed to find requested trace frame" $msg]} {
00081     tk_messageBox -icon error -title "GDB" -type ok \
00082       -message $msg
00083     gdbtk_idle
00084     return
00085   } else {
00086     gdbtk_update
00087     gdbtk_idle
00088   }
00089 }
00090 
00091 # ------------------------------------------------------------------
00092 #  PROC:  save_trace_command -- Saves the current trace settings to a file
00093 # ------------------------------------------------------------------
00094 proc save_trace_commands {} {
00095   
00096   set out_file [tk_getSaveFile -title "Enter output file for trace commands"]
00097   debug "Got outfile: $out_file"
00098   if {$out_file != ""} {
00099     gdb_cmd "save-tracepoints $out_file"
00100   }
00101 }
00102 
00103 # ------------------------------------------------------------------
00104 #  PROC:  do_test - invoke the test passed in
00105 #           This proc is provided for convenience. For any test
00106 #           that uses the console window (like the console window
00107 #           tests), the file cannot be sourced directly using the
00108 #           'tk' command because it will block the console window
00109 #           until the file is done executing. This proc assures
00110 #           that the console window is free for input by wrapping
00111 #           the source call in an after callback.
00112 #           Users may also pass in the verbose and tests globals
00113 #           used by the testsuite.
00114 # ------------------------------------------------------------------
00115 proc do_test {{file {}} {verbose {}} {tests {}}} {
00116   global _test
00117 
00118   if {$file == {}} {
00119     error "wrong \# args: should be: do_test file ?verbose? ?tests ...?"
00120   }
00121 
00122   if {$verbose != {}} {
00123     set _test(verbose) $verbose
00124   } elseif {![info exists _test(verbose)]} {
00125     set _test(verbose) 0
00126   }
00127 
00128   if {$tests != {}} {
00129     set _test(tests) $tests
00130   }
00131 
00132   set _test(interactive) 1
00133   after 500 [list source $file]
00134 }
00135 
00136 # ------------------------------------------------------------------
00137 #  PROCEDURE:  gdbtk_read_defs
00138 #        Reads in the defs file for the testsuite. This is usually
00139 #        the first procedure called by a test file. It returns
00140 #        1 if it was successful and 0 if not (if run interactively
00141 #        from the console window) or exits (if running via dejagnu).
00142 # ------------------------------------------------------------------
00143 proc gdbtk_read_defs {} {
00144   global _test env
00145 
00146   if {[info exists env(DEFS)]} {
00147     set err [catch {source $env(DEFS)} errTxt]
00148   } else {
00149     set err [catch {source defs} errTxt]
00150   }
00151 
00152   if {$err} {
00153     if {$_test(interactive)} {
00154       tk_messageBox -icon error -message "Cannot load defs file:\n$errTxt" -type ok
00155       return 0
00156     } else {
00157       puts stderr "cannot load defs files: $errTxt\ntry setting DEFS"
00158       exit 1
00159     }
00160   }
00161 
00162   return 1
00163 }
00164 
00165 # ------------------------------------------------------------------
00166 #  PROCEDURE:  bp_exists
00167 #            Returns BPNUM if a breakpoint exists at LINESPEC or
00168 #            -1 if no breakpoint exists there
00169 # ------------------------------------------------------------------
00170 proc bp_exists {linespec} {
00171 
00172   lassign $linespec foo function filename line_number addr pc_addr
00173 
00174   set bps [gdb_get_breakpoint_list]
00175   foreach bpnum $bps {
00176     set bpinfo [gdb_get_breakpoint_info $bpnum]
00177     lassign $bpinfo file func line pc type enabled disposition \
00178       ignore_count commands cond thread hit_count user_specification
00179     if {$filename == $file && $function == $func && $addr == $pc} {
00180       return $bpnum
00181     }
00182   }
00183 
00184   return -1
00185 }
00186 
00187 
00188 # gridCGet - This provides the missing grid cget
00189 # command.
00190 
00191 proc gridCGet {slave option} {
00192   set config_list [grid info $slave]
00193   return [lindex $config_list [expr [lsearch $config_list $option] + 1]] 
00194 }
00195 
00196 # ------------------------------------------------------------------
00197 #  PROC:  get_disassembly_flavor - gets the current disassembly flavor.
00198 #         The set disassembly-flavor command is assumed to exist.  This
00199 #         will error out if it does not.
00200 # ------------------------------------------------------------------
00201 proc get_disassembly_flavor {} {
00202   if {[catch {gdb_cmd "show disassembly-flavor"} ret]} {
00203     return ""
00204   } else {
00205     regexp {\"([^\"]*)\"\.} $ret dummy gdb_val
00206     return $gdb_val
00207   }
00208 }
00209  
00210 # ------------------------------------------------------------------
00211 #  PROC:  list_disassembly_flavors - Lists the current disassembly flavors.
00212 #         Returns an empty list if the set disassembly-flavor is not supported.
00213 # ------------------------------------------------------------------
00214 proc list_disassembly_flavors {} {
00215   catch {gdb_cmd "set disassembly-flavor"} ret_val
00216   if {[regexp {Requires an argument\. Valid arguments are (.*)\.} \
00217    $ret_val dummy list]} {
00218     foreach elem  [split $list ","] {
00219       lappend vals [string trim $elem]
00220     }
00221     return [lsort $vals]
00222   } else {
00223     return {}
00224   }    
00225 }
00226 
00227 # ------------------------------------------------------------------
00228 #  PROC:  init_disassembly_flavor - Synchs up gdb's internal disassembly
00229 #         flavor with the value in the preferences file.
00230 # ------------------------------------------------------------------
00231 proc init_disassembly_flavor {} { 
00232   set gdb_val [get_disassembly_flavor]
00233   if {$gdb_val != ""} {
00234     set def_val [pref get gdb/src/disassembly-flavor]
00235     if {[string compare $def_val ""] != 0} {
00236       if {[catch "gdb_cmd \"set disassembly-flavor $def_val\""]} {
00237   pref set gdb/src/disassembly-flavor $gdb_val
00238       }
00239     } else {
00240       pref set gdb/src/disassembly-flavor $gdb_val
00241     }
00242   }
00243 }
00244 
00245 # ------------------------------------------------------------------
00246 #  PROC:  list_element_strcmp - to be used in lsort -command when the
00247 #         elements are themselves lists, and you always want to look at
00248 #         a particular item.
00249 # ------------------------------------------------------------------
00250 proc list_element_strcmp {index first second} {
00251   set theFirst [lindex $first $index]
00252   set theSecond [lindex $second $index]
00253 
00254   return [string compare $theFirst $theSecond]
00255 }
00256 
00257 # ------------------------------------------------------------------
00258 #  PROC:  gdbtk_endian - returns BIG or LITTLE depending on target
00259 #                        endianess
00260 # ------------------------------------------------------------------
00261 
00262 proc gdbtk_endian {} {
00263   if {[catch {gdb_cmd "show endian"} result]} {
00264     return "UNKNOWN"
00265   }
00266   if {[regexp {.*big endian} $result]} {
00267     set result "BIG"
00268   } elseif {[regexp {.*little endian} $result]} {
00269     set result "LITTLE"
00270   } else {
00271     set result "UNKNOWN"
00272   }
00273   return $result
00274 }
00275 
00276 # ------------------------------------------------------------------
00277 #  PROC:  set_bg_colors - set background and text background for
00278 #                        all windows.
00279 # ------------------------------------------------------------------
00280 proc set_bg_colors {{num ""}} {
00281   debug $num
00282 
00283   if {$num != ""} {
00284     set ::gdb_bg_num $num
00285   }
00286   set ::Colors(textbg) [pref get gdb/bg/$::gdb_bg_num]
00287 
00288   # calculate background as 80% of textbg
00289   set ::Colors(bg) [recolor $::Colors(textbg) 80]
00290 
00291   # calculate trough and activebackground as 90% of background
00292   set dbg [recolor $::Colors(bg) 90]
00293 
00294   r_setcolors . -background $::Colors(bg)
00295   r_setcolors . -highlightbackground $::Colors(bg)
00296   r_setcolors . -textbackground $::Colors(textbg)
00297   r_setcolors . -troughcolor $dbg
00298   r_setcolors . -activebackground $dbg
00299 
00300   pref_set_option_db 1
00301   ManagedWin::restart
00302 }
00303 
00304 # ------------------------------------------------------------------
00305 #  PROC:  r_setcolors - recursively set background and text background for
00306 #                        all windows.
00307 # ------------------------------------------------------------------
00308 proc r_setcolors {w option color} {
00309   debug "$w $option $color"
00310 
00311   # exception(s)
00312   if {![catch {$w isa Balloon} result] && $result == "1"} {
00313     return
00314   }
00315   catch {$w config $option $color}
00316   
00317   foreach child [winfo children $w] {
00318     r_setcolors $child $option $color
00319   }
00320 }
00321 
00322 # ------------------------------------------------------------------
00323 #  PROC:  recolor - returns a darker or lighter color
00324 # ------------------------------------------------------------------
00325 proc recolor {color percent} {
00326   set c [winfo rgb . $color]
00327   return [format #%02x%02x%02x [expr {($percent * [lindex $c 0]) / 25600}]  \
00328       [expr {($percent * [lindex $c 1]) / 25600}] [expr {($percent * [lindex $c 2]) / 25600}]]
00329 }
00330 
00331 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines