summaryrefslogtreecommitdiff
path: root/gdb/gdbtk/library/util.tcl
blob: 558dbbf601a05733a06ea1d38d2e11b0d09ed827 (plain)
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
# Utilities for GDBtk.
# Copyright 1997, 1998, 1999 Cygnus Solutions
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License (GPL) as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


# ----------------------------------------------------------------------
# Misc routines
#
#   PROCS:
#
#     keep_raised - keep a window raised
#     sleep - wait a certain number of seconds and return
#     toggle_debug_mode - turn debugging on and off
#     freeze - make a window modal
#     bp_exists - does a breakpoint exist on linespec?
#
# ----------------------------------------------------------------------
#


# A helper procedure to keep a window on top.
proc keep_raised {top} {
  if {[winfo exists $top]} {
    raise $top
    wm deiconify $top
    after 1000 [info level 0]
  }
}

# sleep - wait a certain number of seconds then return
proc sleep {sec} {
  global __sleep_timer
  set __sleep_timer 0
  after [expr {1000 * $sec}] set __sleep_timer 1
  vwait __sleep_timer
}


# ------------------------------------------------------------------
#  PROC:  auto_step - automatically step through a program
# ------------------------------------------------------------------

# FIXME FIXME
proc auto_step {} {
  global auto_step_id

  set auto_step_id [after 2000 auto_step]
  gdb_cmd next
}

# ------------------------------------------------------------------
#  PROC:  auto_step_cancel - cancel auto-stepping
# ------------------------------------------------------------------

proc auto_step_cancel {} {
  global auto_step_id

  if {[info exists auto_step_id]} {
    after cancel $auto_step_id
    unset auto_step_id
  }
}

# ------------------------------------------------------------------
#  PROC:  tfind_cmd -- to execute a tfind command on the target
# ------------------------------------------------------------------
proc tfind_cmd {command} {
  gdbtk_busy
  # need to call gdb_cmd because we want to ignore the output
  set err [catch {gdb_cmd $command} msg]
  if {$err || [regexp "Target failed to find requested trace frame" $msg]} {
    tk_messageBox -icon error -title "GDB" -type ok \
      -modal task -message $msg
    gdbtk_idle
    return
  } else {
    gdbtk_update
    gdbtk_idle
  }
}

# ------------------------------------------------------------------
#  PROC:  save_trace_command -- Saves the current trace settings to a file
# ------------------------------------------------------------------
proc save_trace_commands {} {
  
  set out_file [tk_getSaveFile -title "Enter output file for trace commands"]
  debug "Got outfile: $out_file"
  if {$out_file != ""} {
    gdb_cmd "save-tracepoints $out_file"
  }
}

# ------------------------------------------------------------------
#  PROC:  do_test - invoke the test passed in
#           This proc is provided for convenience. For any test
#           that uses the console window (like the console window
#           tests), the file cannot be sourced directly using the
#           'tk' command because it will block the console window
#           until the file is done executing. This proc assures
#           that the console window is free for input by wrapping
#           the source call in an after callback.
#           Users may also pass in the verbose and tests globals
#           used by the testsuite.
# ------------------------------------------------------------------
proc do_test {{file {}} {verbose {}} {tests {}}} {
  global _test

  if {$file == {}} {
    error "wrong \# args: should be: do_test file ?verbose? ?tests ...?"
  }

  if {$verbose != {}} {
    set _test(verbose) $verbose
  } elseif {![info exists _test(verbose)]} {
    set _test(verbose) 0
  }

  if {$tests != {}} {
    set _test(tests) $tests
  }

  set _test(interactive) 1
  after 500 [list source $file]
}

# ------------------------------------------------------------------
#  PROCEDURE:  gdbtk_read_defs
#        Reads in the defs file for the testsuite. This is usually
#        the first procedure called by a test file. It returns
#        1 if it was successful and 0 if not (if run interactively
#        from the console window) or exits (if running via dejagnu).
# ------------------------------------------------------------------
proc gdbtk_read_defs {} {
  global _test env

  if {[info exists env(DEFS)]} {
    set err [catch {source $env(DEFS)} errTxt]
  } else {
    set err [catch {source defs} errTxt]
  }

  if {$err} {
    if {$_test(interactive)} {
      tk_messageBox -icon error -message "Cannot load defs file:\n$errTxt" -type ok
      return 0
    } else {
      puts stdout "cannot load defs files: $errTxt\ntry setting DEFS"
      exit 1
    }
  }

  return 1
}

# ------------------------------------------------------------------
#  PROCEDURE:  bp_exists
#            Returns BPNUM if a breakpoint exists at LINESPEC or
#            -1 if no breakpoint exists there
# ------------------------------------------------------------------
proc bp_exists {linespec} {

  lassign $linespec foo function filename line_number addr pc_addr

  set bps [gdb_get_breakpoint_list]
  foreach bpnum $bps {
    set bpinfo [gdb_get_breakpoint_info $bpnum]
    lassign $bpinfo file func line pc type enabled disposition \
      ignore_count commands cond thread hit_count
    if {$filename == $file && $function == $func && $addr == $pc} {
      return $bpnum
    }
  }

  return -1
}


# Scrolled Listbox - this could be in libgui,
# but we'll probably just start using new iwidgets stuff 
# soon so keep it here temporarily.  This is based on
# code from Welch's book.

proc CygScrolledListbox { win args } {
  frame $win
  # Create listbox attached to scrollbars, pass thru $args
  eval {listbox $win.list -yscrollcommand [list $win.sy set]} $args
  scrollbar $win.sy -orient vertical -command [list $win.list yview]
  
  # Create padding based on the scrollbar width and border
  set pad [expr [$win.sy cget -width] + 2* \
	     ([$win.sy cget -bd] + \
		[$win.sy cget -highlightthickness])]

  frame $win.pad -width $pad -height $pad
  pack $win.sy -side right -fill y
  pack $win.list -side left -fill both -expand true
  return $win.list
}

# gridCGet - This provides the missing grid cget
# command.

proc gridCGet {slave option} {
  set config_list [grid info $slave]
  return [lindex $config_list [expr [lsearch $config_list $option] + 1]] 
}

# ------------------------------------------------------------------
# PROC: find_iwidgets_library - Find the IWidgets library.
#
# This is a little bit of bogosity which is necessary so we
# can find the iwidgets libraries if we are not installed:
# The problem is that the iwidgets are really weird.  The init file is 
# in the build tree, but all the library files are in the source tree...
#
# ------------------------------------------------------------------
proc find_iwidgets_library {} {

  set IwidgetsOK 1

  if {[catch {package require Iwidgets 3.0} errMsg]} {

    # OK, we are not installed or this would have succeeded...
    # Lets try to do it by hand:
    set IwidgetsOK 0

    set iwidgetsSrcDir [glob -nocomplain [file join \
					    [file dirname [file dirname $::tcl_library]] \
					    itcl iwidgets*]]
    
    # Canonicalize the executable's directory name.  It turns out that on Solaris, 
    # info nameofexecutable returns /foo/bar/real_dir/./gdb when gdb is launched from
    # another gdb session, so we have to fix this up.

    set exec_name [info nameofexecutable]
    set curdir [pwd] 
    if {[string compare [file type $exec_name] "link"] == 0} {
      set exec_name [file readlink $exec_name]
      if {[string compare [file pathtype $exec_name] "relative"] == 0} {
	set exec_name [file join [pwd] $exec_name]
      }
    }
    
    cd [file dirname $exec_name]
    set exec_name [pwd]
    cd $curdir

    set iwidgetsBuildDir [glob -nocomplain [file join \
					      [file dirname $exec_name] \
					      itcl iwidgets*]]
    
    if {[llength $iwidgetsSrcDir] == 1 && [llength $iwidgetsBuildDir] == 1} {
      # The lindex is necessary because the path may have spaces in it...
      set initFile [file join [lindex $iwidgetsBuildDir 0] \
		      $::tcl_platform(platform) iwidgets.tcl]
      set libDir [file join [lindex $iwidgetsSrcDir 0] generic]
      if {[file exists $initFile] && [file isdirectory $libDir]} {
	if {![catch {source $initFile} err]} {
	  # Now fix up the stuff the Iwidgets init file got wrong...
	  set libPos [lsearch $::auto_path [file join $::iwidgets::library scripts]]
	  if {$libPos >= 0} {
	    set auto_path [lreplace $::auto_path $libPos $libPos $libDir]
	  } else {
	    lappend ::auto_path $libDir
	  }
	  set ::iwidgets::library $libDir
	  set IwidgetsOK 1
	} else {
	  append errMsg "\nError in iwidgets.tcl file: $err"
	}
      }
    } else {
      append errMsg "\nCould not find in-place versions of the Iwidgets files\n"
      append errMsg "Looked at: [file join [file dirname\
                     [file dirname $::tcl_library]] itcl iwidgets*]\n"
      append errMsg "and: [file join [file dirname \
                    [info nameofexecutable]] itcl iwidgets*]\n"
    }

  }
  return $IwidgetsOK
}

# ------------------------------------------------------------------
#  PROC:  get_disassembly_flavor - gets the current disassembly flavor.
#         The set disassembly-flavor command is assumed to exist.  This
#         will error out if it does not.
# ------------------------------------------------------------------
proc get_disassembly_flavor {} {
  if {[catch {gdb_cmd "show disassembly-flavor"} ret]} {
    return ""
  } else {
    regexp {\"([^\"]*)\"\.} $ret dummy gdb_val
    return $gdb_val
  }
}
 
# ------------------------------------------------------------------
#  PROC:  list_disassembly_flavors - Lists the current disassembly flavors.
#         Returns an empty list if the set disassembly-flavor is not supported.
# ------------------------------------------------------------------
proc list_disassembly_flavors {} {
  catch {gdb_cmd "set disassembly-flavor"} ret_val
  if {[regexp {Requires an argument\. Valid arguments are (.*)\.} \
	 $ret_val dummy list]} {
    foreach elem  [split $list ","] {
      lappend vals [string trim $elem]
    }
    return [lsort $vals]
  } else {
    return {}
  }    
}

# ------------------------------------------------------------------
#  PROC:  init_disassembly_flavor - Synchs up gdb's internal disassembly
#         flavor with the value in the preferences file.
# ------------------------------------------------------------------
proc init_disassembly_flavor {} { 
  set gdb_val [get_disassembly_flavor]
  if {$gdb_val != ""} {
    set def_val [pref get gdb/src/disassembly-flavor]
    if {[string compare $def_val ""] != 0} {
      if {[catch "gdb_cmd \"set disassembly-flavor $def_val\""]} {
	pref set gdb/src/disassembly-flavor $gdb_val
      }
    } else {
      pref set gdb/src/disassembly-flavor $gdb_val
    }
  }
}

# ------------------------------------------------------------------
#  PROC:  list_element_strcmp - to be used in lsort -command when the
#         elements are themselves lists, and you always want to look at
#         a particular item.
# ------------------------------------------------------------------
proc list_element_strcmp {index first second} {
  set theFirst [lindex $first $index]
  set theSecond [lindex $second $index]

  return [string compare $theFirst $theSecond]
}