summaryrefslogtreecommitdiff
path: root/gdb/gdbtk/library/session.tcl
blob: 158ffd245be0c08a6c8fea8f6dddcad7ec4630c1 (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
# Local preferences functions for GDBtk.
# Copyright 2000, 2001, 2002 Red Hat, Inc.
#
# 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.

namespace eval Session {
  namespace export save load notice_file_change delete list_names

  # An internal function for canonicalizing path names.  This probably
  # should use `realpath', but that is more work.  So for now we neglect
  # the possibility of symlinks.
  proc _exe_name {path} {
    global tcl_platform

    # Get real directory.
    if {[string compare $tcl_platform(platform) "windows"] == 0} {
      set path [ide_cygwin_path to_win32 $path]
    }
    set save [pwd]
    cd [file dirname $path]
    set dir [pwd]
    cd $save
    return [file join $dir [file tail $path]]
  }

  # An internal function used when saving sessions.  Returns a string
  # that can be used to recreate all pertinent breakpoint state.
  proc _serialize_bps {} {
    set result {}

    # HACK.  When debugging gdb with itself in the build
    # directory, there is a ".gdbinit" file that will set
    # breakpoints on internal_error() and info_command().
    # If we then save and set them, they will accumulate.
    # Possible fixes are to modify GDB so we can tell which 
    # breakpoints were set from .gdbinit, or modify 
    # _recreate_bps to record which breakpoints were
    # set before it was called.  For now, we simply detect the
    # most common case and fix it.
    set basename [string tolower [file tail $::gdb_exe_name]]
    if {[string match "gdb*" $basename] 
	|| [string match "insight*" $basename]} {
      set debugging_gdb 1
    } else {
      set debugging_gdb 0
    }
    
    foreach bp_num [gdb_get_breakpoint_list] {
      lassign [gdb_get_breakpoint_info $bp_num] file function line_number \
	address type enabled disposition ignore_count command_list \
	condition thread hit_count user_specification

      # These breakpoints are set when debugging GDB with itself.
      # Ignore them so they don't accumulate. They get set again
      # by .gdbinit anyway. 
      if {$debugging_gdb} {
	if {$function == "internal_error" || $function == "info_command"} {
	  continue
	}
      }
      
      switch -glob -- $type {
	"breakpoint" -
	"hw breakpoint" {
	  if {$disposition == "delete"} {
	    set cmd tbreak
	  } else {
	    set cmd break
	  }

	  append cmd " "
	  if {$user_specification != ""} {
	    append cmd "$user_specification"
	  } elseif {$file != ""} {
	    # BpWin::bp_store uses file tail here, but I think that is
	    # wrong.
	    append cmd "$file:$line_number"
	  } else {
	    append cmd "*$address"
	  }
	}
	"watchpoint" -
	"hw watchpoint" {
	  set cmd watch
	  if {$user_specification != ""} {
	    append cmd " $user_specification"
	  } else {
	    # There's nothing sensible to do.
	    continue
	  }
	}

	"catch*" {
	  # FIXME: Don't know what to do.
	  continue
	}

	default {
	  # Can't serialize anything other than those listed above.
	  continue
	}
      }

      lappend result [list $cmd $enabled $condition $command_list]
    }
    
    return $result
  }

  # An internal function used when loading sessions.  It takes a
  # breakpoint string and recreates all the breakpoints.
  proc _recreate_bps {specs} {  
    foreach spec $specs {
      lassign $spec create enabled condition commands

      # Create the breakpoint
      if {[catch {gdb_cmd $create} txt]} {
	dbug W $txt
      }

      # Below we use `\$bpnum'.  This means we don't have to figure out
      # the number of the breakpoint when doing further manipulations.

      if {! $enabled} {
	gdb_cmd "disable \$bpnum"
      }

      if {$condition != ""} {
	gdb_cmd "cond \$bpnum $condition"
      }

      if {[llength $commands]} {
	lappend commands end
	eval gdb_run_readline_command_no_output [list "commands \$bpnum"] \
	  $commands
      }
    }
  }

  #
  # This procedure decides what makes up a gdb `session'.  Roughly a
  # session is whatever the user found useful when debugging a certain
  # executable.
  #
  # Eventually we should expand this procedure to know how to save
  # window placement and contents.  That requires more work.
  #
  proc save {} {
    global gdb_exe_name gdb_target_name
    global gdb_current_directory gdb_source_path

    # gdb sessions are named after the executable.
    set name [_exe_name $gdb_exe_name]
    set key gdb/session/$name

    # We fill a hash and then use that to set the actual preferences.

    # Always set the exe. name in case we later decide to change the
    # interpretation of the session key.  Use the full path to the
    # executable.
    set values(executable) $name

    # Some simple state the user wants.
    set values(args) [gdb_get_inferior_args]
    set values(dirs) $gdb_source_path
    set values(pwd) $gdb_current_directory
    set values(target) $gdb_target_name
    set values(target_cmd) $::gdb_target_cmd

    # these prefs need to be made session-dependent
    set values(run_attach) [pref get gdb/src/run_attach]
    set values(run_load) [pref get gdb/src/run_load]
    set values(run_run) [pref get gdb/src/run_run]
    set values(run_cont) [pref get gdb/src/run_cont]
    
    # Breakpoints.
    set values(breakpoints) [_serialize_bps]

    # Recompute list of recent sessions.  Trim to no more than 5 sessions.
    set recent [concat [list $name] \
		  [lremove [pref getd gdb/recent-projects] $name]]
    if {[llength $recent] > 5} then {
      set recent [lreplace $recent 5 end]
    }
    pref setd gdb/recent-projects $recent

    foreach k [array names values] {
      pref setd $key/$k $values($k)
    }
    pref setd $key/all-keys [array names values]
  }

  #
  # Load a session saved with Session::save.  NAME is the pretty name of
  # the session, as returned by Session::list_names.
  #
  proc load {name} {
    # gdb sessions are named after the executable.
    set key gdb/session/$name

    # Fetch all keys for this session into an array.
    foreach k [pref getd $key/all-keys] {
      set values($k) [pref getd $key/$k]
    }

    if {[info exists values(executable)]} {
      gdb_clear_file
      set_exe_name $values(executable)
      set_exe
    }
  }

  #
  # This is called from file_changed_hook.  It does all the work of
  # loading a session, if one exists with the same name as the current
  # executable.
  #
  proc notice_file_change {} {
    global gdb_exe_name gdb_target_name

    debug "noticed file change event for $gdb_exe_name"

    # gdb sessions are named after the executable.
    set name [_exe_name $gdb_exe_name]
    set key gdb/session/$name

    # Fetch all keys for this session into an array.
    foreach k [pref getd $key/all-keys] {
      set values($k) [pref getd $key/$k]
    }

    # reset these back to their defaults
    pref set gdb/src/run_attach          0
    pref set gdb/src/run_load            0
    pref set gdb/src/run_run             1
    pref set gdb/src/run_cont            0

    if {! [info exists values(executable)] || $values(executable) != $name} {
      # No such session.
      return
    }

    debug "reloading session for $gdb_exe_name"

    if {[info exists values(dirs)]} {
      # FIXME: short-circuit confirmation.
      gdb_cmd "directory"
      gdb_cmd "directory $values(dirs)"
    }

    if {[info exists values(pwd)]} {
      catch {gdb_cmd "cd $values(pwd)"}
    }

    if {[info exists values(args)]} {
      gdb_set_inferior_args $values(args)
    }

    if {[info exists values(breakpoints)]} {
      _recreate_bps $values(breakpoints)
    }

    if {[info exists values(target)]} {
      debug "Restoring Target: $values(target)"
      set gdb_target_name $values(target)
      debug "Restoring Target_Cmd: $values(target_cmd)"
      set ::gdb_target_cmd $values(target_cmd)
      set_baud
    }
    
    if {[info exists values(run_attach)]} {
      pref set gdb/src/run_attach $values(run_attach)
      pref set gdb/src/run_load $values(run_load)
      pref set gdb/src/run_run $values(run_run)
      pref set gdb/src/run_cont $values(run_cont)
    } 
  }

  #
  # Delete a session.  NAME is the internal name of the session.
  #
  proc delete {name} {
    # FIXME: we can't yet fully define this because the libgui
    # preference code doesn't supply a delete method.
    set recent [lremove [pref getd gdb/recent-projects] $name]
    pref setd gdb/recent-projects $recent
  }

  #
  # Return a list of all known sessions.  This returns the `pretty name'
  # of the session -- something suitable for a menu.
  #
  proc list_names {} {
    set newlist {}
    set result {}
    foreach name [pref getd gdb/recent-projects] {
      set exe [pref getd gdb/session/$name/executable]
      # Take this opportunity to prune the list.
      if {[file exists $exe]} then {
	lappend newlist $name
	lappend result $exe
      } else {
	# FIXME: if we could delete keys we would delete all keys
	# associated with NAME now.
      }
    }
    pref setd gdb/recent-projects $newlist
    return $result
  }
}