Fix gcc -Wall warnings. See ChangeLog for details.
[deliverable/binutils-gdb.git] / gdb / gdbtk.tcl
CommitLineData
4604b34c 1# GDB GUI setup for GDB, the GNU debugger.
1a57cd09 2# Copyright 1994, 1995, 1996
4604b34c
SG
3# Free Software Foundation, Inc.
4
5# Written by Stu Grossman <grossman@cygnus.com> of Cygnus Support.
6
7# This file is part of GDB.
8
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
4e327047 21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
754e5da2
SG
22
23set cfile Blank
006e71e9 24set wins($cfile) .src.text
754e5da2 25set current_label {}
8532893d 26set cfunc NIL
86db943c 27set line_numbers 1
546b8ca7 28set breakpoint_file(-1) {[garbage]}
280c564c 29set disassemble_with_source nosource
5bac2b50 30set gdb_prompt "(gdb) "
86db943c 31
3d9f68c0
FF
32# Hint: The following can be toggled from a tclsh window after
33# using the gdbtk "tk tclsh" command to open the window.
9e9cf822
SS
34set debug_interface 0
35
006e71e9
SG
36#option add *Foreground Black
37#option add *Background White
38#option add *Font -*-*-medium-r-normal--18-*-*-*-m-*-*-1
754e5da2
SG
39
40proc echo string {puts stdout $string}
41
4e327047
TT
42# Assign elements from LIST to variables named in ARGS. FIXME replace
43# with TclX version someday.
44proc lassign {list args} {
45 set len [expr {[llength $args] - 1}]
46 while {$len >= 0} {
47 upvar [lindex $args $len] local
48 set local [lindex $list $len]
49 decr len
50 }
51}
52
53#
54# Local procedure:
55#
56# decr (var val) - compliment to incr
57#
58# Description:
59#
60#
61proc decr {var {val 1}} {
62 upvar $var num
63 set num [expr {$num - $val}]
64 return $num
65}
66
67#
68# Center a window on the screen.
69#
3d9f68c0 70proc center_window {toplevel} {
4e327047
TT
71 # Withdraw and update, to ensure geometry computations are finished.
72 wm withdraw $toplevel
73 update idletasks
74
75 set x [expr {[winfo screenwidth $toplevel] / 2
76 - [winfo reqwidth $toplevel] / 2
77 - [winfo vrootx $toplevel]}]
78 set y [expr {[winfo screenheight $toplevel] / 2
79 - [winfo reqheight $toplevel] / 2
80 - [winfo vrooty $toplevel]}]
81 wm geometry $toplevel +${x}+${y}
82 wm deiconify $toplevel
83}
84
85#
86# Rearrange the bindtags so the widget comes after the class. I was
87# always for Ousterhout putting the class bindings first, but no...
88#
89proc bind_widget_after_class {widget} {
90 set class [winfo class $widget]
91 set newList {}
92 foreach tag [bindtags $widget] {
93 if {$tag == $widget} {
94 # Nothing.
95 } {
96 lappend newList $tag
97 if {$tag == $class} {
98 lappend newList $widget
99 }
100 }
101 }
102 bindtags $widget $newList
103}
104
f0b0d915
TT
105#
106# Make sure line number $LINE is visible in the text widget. But be
107# more clever than the "see" command: if LINE is not currently
108# displayed, arrange for LINE to be centered. There are cases in
109# which this does not work, so as a last resort we revert to "see".
110#
111# This is inefficient, but probably not slow enough to actually
112# notice.
113#
114proc ensure_line_visible {text line} {
115 set pixHeight [winfo height $text]
116 # Compute height of widget in lines. This fails if a line is wider
117 # than the screen. FIXME.
118 set topLine [lindex [split [$text index @0,0] .] 0]
119 set botLine [lindex [split [$text index @0,${pixHeight}] .] 0]
120
121 if {$line > $topLine && $line < $botLine} then {
122 # Onscreen, and not on the very edge.
123 return
124 }
125
126 set newTop [expr {$line - ($botLine - $topLine)}]
127 if {$newTop < 0} then {
128 set newTop 0
129 }
130 $text yview moveto $newTop
131
132 # In case the above failed.
133 $text see ${line}.0
134}
135
4e327047
TT
136if {[info exists env(EDITOR)]} then {
137 set editor $env(EDITOR)
138} else {
139 set editor emacs
8532893d
SG
140}
141
142# GDB callbacks
143#
144# These functions are called by GDB (from C code) to do various things in
145# TK-land. All start with the prefix `gdbtk_tcl_' to make them easy to find.
146#
147
148#
149# GDB Callback:
150#
151# gdbtk_tcl_fputs (text) - Output text to the command window
152#
153# Description:
154#
155# GDB calls this to output TEXT to the GDB command window. The text is
156# placed at the end of the text widget. Note that output may not occur,
157# due to buffering. Use gdbtk_tcl_flush to cause an immediate update.
158#
159
754e5da2 160proc gdbtk_tcl_fputs {arg} {
4e327047
TT
161 .cmd.text insert end "$arg"
162 .cmd.text see end
8532893d
SG
163}
164
86db943c 165proc gdbtk_tcl_fputs_error {arg} {
4e327047
TT
166 .cmd.text insert end "$arg"
167 .cmd.text see end
86db943c
SG
168}
169
8532893d
SG
170#
171# GDB Callback:
172#
173# gdbtk_tcl_flush () - Flush output to the command window
174#
175# Description:
176#
177# GDB calls this to force all buffered text to the GDB command window.
178#
179
180proc gdbtk_tcl_flush {} {
4e327047
TT
181 .cmd.text see end
182 update idletasks
754e5da2
SG
183}
184
8532893d
SG
185#
186# GDB Callback:
187#
188# gdbtk_tcl_query (message) - Create a yes/no query dialog box
189#
190# Description:
191#
192# GDB calls this to create a yes/no dialog box containing MESSAGE. GDB
193# is hung while the dialog box is active (ie: no commands will work),
194# however windows can still be refreshed in case of damage or exposure.
195#
754e5da2
SG
196
197proc gdbtk_tcl_query {message} {
4e327047
TT
198 # FIXME We really want a Help button here. But Tk's brain-damaged
199 # modal dialogs won't really allow it. Should have async dialog
200 # here.
201 set result [tk_dialog .query "gdb : query" "$message" questhead 0 Yes No]
202 return [expr {!$result}]
203}
754e5da2 204
8532893d
SG
205#
206# GDB Callback:
207#
208# gdbtk_start_variable_annotation (args ...) -
209#
210# Description:
211#
212# Not yet implemented.
213#
754e5da2 214
4e327047
TT
215proc gdbtk_tcl_start_variable_annotation {valaddr ref_type stor_cl
216 cum_expr field type_cast} {
217 echo "gdbtk_tcl_start_variable_annotation $valaddr $ref_type $stor_cl $cum_expr $field $type_cast"
754e5da2
SG
218}
219
8532893d
SG
220#
221# GDB Callback:
222#
223# gdbtk_end_variable_annotation (args ...) -
224#
225# Description:
226#
227# Not yet implemented.
228#
229
754e5da2
SG
230proc gdbtk_tcl_end_variable_annotation {} {
231 echo gdbtk_tcl_end_variable_annotation
232}
233
8532893d
SG
234#
235# GDB Callback:
236#
237# gdbtk_tcl_breakpoint (action bpnum file line) - Notify the TK
238# interface of changes to breakpoints.
239#
240# Description:
241#
242# GDB calls this to notify TK of changes to breakpoints. ACTION is one
243# of:
244# create - Notify of breakpoint creation
245# delete - Notify of breakpoint deletion
6131622e 246# modify - Notify of breakpoint modification
8532893d
SG
247#
248
6131622e
SG
249# file line pc type enabled disposition silent ignore_count commands cond_string thread hit_count
250
251proc gdbtk_tcl_breakpoint {action bpnum} {
252 set bpinfo [gdb_get_breakpoint_info $bpnum]
253 set file [lindex $bpinfo 0]
254 set line [lindex $bpinfo 1]
255 set pc [lindex $bpinfo 2]
256 set enable [lindex $bpinfo 4]
257
258 if {$action == "modify"} {
f61f41d9 259 if {$enable == "1"} {
6131622e
SG
260 set action enable
261 } else {
262 set action disable
263 }
264 }
265
8532893d 266 ${action}_breakpoint $bpnum $file $line $pc
754e5da2
SG
267}
268
41756e56
FF
269#
270# GDB Callback:
271#
272# gdbtk_tcl_readline_begin (message) - Notify Tk to open an interaction
273# window and start gathering user input
274#
275# Description:
276#
277# GDB calls this to notify TK that it needs to open an interaction
278# window, displaying the given message, and be prepared to accept
279# calls to gdbtk_tcl_readline to gather user input.
280
281proc gdbtk_tcl_readline_begin {message} {
282 global readline_text
283
284 # If another readline window already exists, just bring it to the front.
285 if {[winfo exists .rl]} {raise .rl ; return}
286
287 # Create top level frame with scrollbar and text widget.
288 toplevel .rl
289 wm title .rl "Interaction Window"
290 wm iconname .rl "Input"
291 message .rl.msg -text $message -aspect 7500 -justify left
292 text .rl.text -width 80 -height 20 -setgrid true -cursor hand2 \
293 -yscrollcommand {.rl.scroll set}
294 scrollbar .rl.scroll -command {.rl.text yview}
295 pack .rl.msg -side top -fill x
296 pack .rl.scroll -side right -fill y
297 pack .rl.text -side left -fill both -expand true
298
299 # When the user presses return, get the text from the command start mark to the
300 # current insert point, stash it in the readline text variable, and update the
301 # command start mark to the current insert point
302 bind .rl.text <Return> {
303 set readline_text [.rl.text get cmdstart {end - 1 char}]
304 .rl.text mark set cmdstart insert
305 }
3f8eefba
FF
306 bind .rl.text <BackSpace> {
307 if [%W compare insert > cmdstart] {
308 %W delete {insert - 1 char} insert
309 } else {
310 bell
311 }
312 break
313 }
314 bind .rl.text <Any-Key> {
315 if [%W compare insert < cmdstart] {
316 %W mark set insert end
317 }
318 }
319 bind .rl.text <Control-u> {
320 %W delete cmdstart "insert lineend"
321 %W see insert
322 }
41756e56
FF
323 bindtags .rl.text {.rl.text Text all}
324}
325
326#
327# GDB Callback:
328#
329# gdbtk_tcl_readline (prompt) - Get one user input line
330#
331# Description:
332#
333# GDB calls this to get one line of input from the user interaction
334# window, using "prompt" as the command line prompt.
335
336proc gdbtk_tcl_readline {prompt} {
337 global readline_text
338
339 .rl.text insert end $prompt
340 .rl.text mark set cmdstart insert
341 .rl.text mark gravity cmdstart left
342 .rl.text see insert
343
344 # Make this window the current one for input.
345 focus .rl.text
346 grab .rl
347 tkwait variable readline_text
348 grab release .rl
349 return $readline_text
350}
351
352#
353# GDB Callback:
354#
355# gdbtk_tcl_readline_end - Terminate a user interaction
356#
357# Description:
358#
359# GDB calls this when it is done getting interactive user input.
360# Destroy the interaction window.
361
362proc gdbtk_tcl_readline_end {} {
363 if {[winfo exists .rl]} { destroy .rl }
364}
365
6131622e
SG
366proc create_breakpoints_window {} {
367 global bpframe_lasty
368
4e327047 369 if {[winfo exists .breakpoints]} {raise .breakpoints ; return}
6131622e
SG
370
371 build_framework .breakpoints "Breakpoints" ""
372
373# First, delete all the old view menu entries
374
375 .breakpoints.menubar.view.menu delete 0 last
376
377# Get rid of label
378
379 destroy .breakpoints.label
380
381# Replace text with a canvas and fix the scrollbars
382
383 destroy .breakpoints.text
6131622e
SG
384 scrollbar .breakpoints.scrollx -orient horizontal \
385 -command {.breakpoints.c xview} -relief sunken
4e327047
TT
386 canvas .breakpoints.c -relief sunken -bd 2 \
387 -cursor hand2 \
388 -yscrollcommand {.breakpoints.scroll set} \
389 -xscrollcommand {.breakpoints.scrollx set}
390 .breakpoints.scroll configure -command {.breakpoints.c yview}
6131622e
SG
391
392 pack .breakpoints.scrollx -side bottom -fill x -in .breakpoints.info
393 pack .breakpoints.c -side left -expand yes -fill both \
394 -in .breakpoints.info
395
396 set bpframe_lasty 0
397
398# Create a frame for each breakpoint
399
400 foreach bpnum [gdb_get_breakpoint_list] {
401 add_breakpoint_frame $bpnum
402 }
403}
404
405# Create a frame for bpnum in the .breakpoints canvas
406
3d9f68c0 407proc add_breakpoint_frame {bpnum} {
4e327047
TT
408 global bpframe_lasty
409 global enabled
410 global disposition
411
412 if {![winfo exists .breakpoints]} return
413
414 set bpinfo [gdb_get_breakpoint_info $bpnum]
415
416 lassign $bpinfo file line pc type enabled($bpnum) disposition($bpnum) \
417 silent ignore_count commands cond thread hit_count
418
419 set f .breakpoints.c.$bpnum
420
421 if {![winfo exists $f]} {
422 frame $f -relief sunken -bd 2
423
424 label $f.id -text "#$bpnum $file:$line ($pc)" \
425 -relief flat -bd 2 -anchor w
426 frame $f.hit_count
427 label $f.hit_count.label -text "Hit count:" -relief flat \
428 -bd 2 -anchor w -width 11
429 label $f.hit_count.val -text $hit_count -relief flat \
430 -bd 2 -anchor w
431 checkbutton $f.hit_count.enabled -text Enabled \
432 -variable enabled($bpnum) -anchor w -relief flat
433
434 pack $f.hit_count.label $f.hit_count.val -side left
435 pack $f.hit_count.enabled -side right
436
437 frame $f.thread
438 label $f.thread.label -text "Thread: " -relief flat -bd 2 \
439 -width 11 -anchor w
440 entry $f.thread.entry -bd 2 -relief sunken -width 10
441 $f.thread.entry insert end $thread
442 pack $f.thread.label -side left
443 pack $f.thread.entry -side left -fill x
444
445 frame $f.cond
446 label $f.cond.label -text "Condition: " -relief flat -bd 2 \
447 -width 11 -anchor w
448 entry $f.cond.entry -bd 2 -relief sunken
449 $f.cond.entry insert end $cond
450 pack $f.cond.label -side left
451 pack $f.cond.entry -side left -fill x -expand yes
452
453 frame $f.ignore_count
454 label $f.ignore_count.label -text "Ignore count: " \
455 -relief flat -bd 2 -width 11 -anchor w
456 entry $f.ignore_count.entry -bd 2 -relief sunken -width 10
457 $f.ignore_count.entry insert end $ignore_count
458 pack $f.ignore_count.label -side left
459 pack $f.ignore_count.entry -side left -fill x
460
461 frame $f.disps
462
463 label $f.disps.label -text "Disposition: " -relief flat -bd 2 \
464 -anchor w -width 11
465
466 radiobutton $f.disps.delete -text Delete \
467 -variable disposition($bpnum) -anchor w -relief flat \
468 -command "gdb_cmd \"delete break $bpnum\"" \
469 -value delete
470
471 radiobutton $f.disps.disable -text Disable \
472 -variable disposition($bpnum) -anchor w -relief flat \
473 -command "gdb_cmd \"disable break $bpnum\"" \
474 -value disable
475
476 radiobutton $f.disps.donttouch -text "Leave alone" \
477 -variable disposition($bpnum) -anchor w -relief flat \
478 -command "gdb_cmd \"enable break $bpnum\"" \
479 -value donttouch
480
481 pack $f.disps.label $f.disps.delete $f.disps.disable \
482 $f.disps.donttouch -side left -anchor w
483 text $f.commands -relief sunken -bd 2 -setgrid true \
484 -cursor hand2 -height 3 -width 30
485
486 foreach line $commands {
487 $f.commands insert end "${line}\n"
488 }
6131622e 489
4e327047
TT
490 pack $f.id -side top -anchor nw -fill x
491 pack $f.hit_count $f.cond $f.thread $f.ignore_count $f.disps \
492 $f.commands -side top -fill x -anchor nw
493 }
6131622e 494
4e327047
TT
495 set tag [.breakpoints.c create window 0 $bpframe_lasty -window $f -anchor nw]
496 update
497 set bbox [.breakpoints.c bbox $tag]
6131622e 498
4e327047 499 set bpframe_lasty [lindex $bbox 3]
f1b64caa 500
4e327047 501 .breakpoints.c configure -width [lindex $bbox 2]
6131622e
SG
502}
503
504# Delete a breakpoint frame
505
3d9f68c0 506proc delete_breakpoint_frame {bpnum} {
6131622e
SG
507 global bpframe_lasty
508
4e327047 509 if {![winfo exists .breakpoints]} return
6131622e
SG
510
511# First, clear the canvas
512
513 .breakpoints.c delete all
514
515# Now, repopulate it with all but the doomed breakpoint
516
517 set bpframe_lasty 0
518 foreach bp [gdb_get_breakpoint_list] {
519 if {$bp != $bpnum} {
520 add_breakpoint_frame $bp
521 }
522 }
523}
524
335129a9 525proc asm_win_name {funcname} {
546b8ca7
SG
526 if {$funcname == "*None*"} {return .asm.text}
527
335129a9
SG
528 regsub -all {\.} $funcname _ temp
529
530 return .asm.func_${temp}
531}
532
8532893d
SG
533#
534# Local procedure:
535#
536# create_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
537#
538# Description:
539#
540# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
541# land of breakpoint creation. This consists of recording the file and
542# line number in the breakpoint_file and breakpoint_line arrays. Also,
543# if there is already a window associated with FILE, it is updated with
544# a breakpoint tag.
545#
546
547proc create_breakpoint {bpnum file line pc} {
754e5da2
SG
548 global wins
549 global breakpoint_file
550 global breakpoint_line
8532893d 551 global pos_to_breakpoint
335129a9 552 global pos_to_bpcount
8532893d
SG
553 global cfunc
554 global pclist
754e5da2
SG
555
556# Record breakpoint locations
557
558 set breakpoint_file($bpnum) $file
559 set breakpoint_line($bpnum) $line
8532893d 560 set pos_to_breakpoint($file:$line) $bpnum
4e327047 561 if {![info exists pos_to_bpcount($file:$line)]} {
335129a9
SG
562 set pos_to_bpcount($file:$line) 0
563 }
564 incr pos_to_bpcount($file:$line)
565 set pos_to_breakpoint($pc) $bpnum
4e327047 566 if {![info exists pos_to_bpcount($pc)]} {
335129a9
SG
567 set pos_to_bpcount($pc) 0
568 }
569 incr pos_to_bpcount($pc)
754e5da2 570
8532893d 571# If there's a window for this file, update it
754e5da2 572
4e327047 573 if {[info exists wins($file)]} {
754e5da2
SG
574 insert_breakpoint_tag $wins($file) $line
575 }
8532893d
SG
576
577# If there's an assembly window, update that too
578
335129a9 579 set win [asm_win_name $cfunc]
4e327047 580 if {[winfo exists $win]} {
637b1661 581 insert_breakpoint_tag $win [pc_to_line $pclist($cfunc) $pc]
8532893d 582 }
6131622e
SG
583
584# Update the breakpoints window
585
586 add_breakpoint_frame $bpnum
754e5da2
SG
587}
588
8532893d
SG
589#
590# Local procedure:
591#
592# delete_breakpoint (bpnum file line pc) - Delete breakpoint info from TK land
593#
594# Description:
595#
596# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
597# land of breakpoint destruction. This consists of removing the file and
598# line number from the breakpoint_file and breakpoint_line arrays. Also,
599# if there is already a window associated with FILE, the tags are removed
600# from it.
601#
602
603proc delete_breakpoint {bpnum file line pc} {
754e5da2
SG
604 global wins
605 global breakpoint_file
606 global breakpoint_line
8532893d 607 global pos_to_breakpoint
335129a9
SG
608 global pos_to_bpcount
609 global cfunc pclist
754e5da2 610
8532893d 611# Save line number and file for later
754e5da2
SG
612
613 set line $breakpoint_line($bpnum)
614
8532893d
SG
615 set file $breakpoint_file($bpnum)
616
754e5da2
SG
617# Reset breakpoint annotation info
618
335129a9 619 if {$pos_to_bpcount($file:$line) > 0} {
637b1661 620 decr pos_to_bpcount($file:$line)
335129a9
SG
621
622 if {$pos_to_bpcount($file:$line) == 0} {
637b1661
SG
623 catch "unset pos_to_breakpoint($file:$line)"
624
335129a9
SG
625 unset breakpoint_file($bpnum)
626 unset breakpoint_line($bpnum)
754e5da2 627
8532893d 628# If there's a window for this file, update it
754e5da2 629
4e327047 630 if {[info exists wins($file)]} {
335129a9
SG
631 delete_breakpoint_tag $wins($file) $line
632 }
633 }
634 }
635
636# If there's an assembly window, update that too
637
638 if {$pos_to_bpcount($pc) > 0} {
637b1661 639 decr pos_to_bpcount($pc)
335129a9
SG
640
641 if {$pos_to_bpcount($pc) == 0} {
637b1661
SG
642 catch "unset pos_to_breakpoint($pc)"
643
335129a9 644 set win [asm_win_name $cfunc]
4e327047 645 if {[winfo exists $win]} {
637b1661 646 delete_breakpoint_tag $win [pc_to_line $pclist($cfunc) $pc]
335129a9
SG
647 }
648 }
754e5da2 649 }
6131622e
SG
650
651 delete_breakpoint_frame $bpnum
754e5da2
SG
652}
653
8532893d
SG
654#
655# Local procedure:
656#
657# enable_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
658#
659# Description:
660#
661# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
662# land of a breakpoint being enabled. This consists of unstippling the
663# specified breakpoint indicator.
664#
665
666proc enable_breakpoint {bpnum file line pc} {
667 global wins
335129a9 668 global cfunc pclist
f61f41d9 669 global enabled
335129a9 670
4e327047 671 if {[info exists wins($file)]} {
335129a9
SG
672 $wins($file) tag configure $line -fgstipple {}
673 }
754e5da2 674
335129a9
SG
675# If there's an assembly window, update that too
676
677 set win [asm_win_name $cfunc]
4e327047 678 if {[winfo exists $win]} {
637b1661 679 $win tag configure [pc_to_line $pclist($cfunc) $pc] -fgstipple {}
335129a9 680 }
f61f41d9
MT
681
682# If there's a breakpoint window, update that too
683
4e327047 684 if {[winfo exists .breakpoints]} {
f61f41d9
MT
685 set enabled($bpnum) 1
686 }
754e5da2
SG
687}
688
8532893d
SG
689#
690# Local procedure:
691#
692# disable_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
693#
694# Description:
695#
696# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
697# land of a breakpoint being disabled. This consists of stippling the
698# specified breakpoint indicator.
699#
700
701proc disable_breakpoint {bpnum file line pc} {
702 global wins
335129a9 703 global cfunc pclist
f61f41d9 704 global enabled
335129a9 705
4e327047 706 if {[info exists wins($file)]} {
335129a9
SG
707 $wins($file) tag configure $line -fgstipple gray50
708 }
754e5da2 709
335129a9
SG
710# If there's an assembly window, update that too
711
712 set win [asm_win_name $cfunc]
4e327047 713 if {[winfo exists $win]} {
637b1661 714 $win tag configure [pc_to_line $pclist($cfunc) $pc] -fgstipple gray50
335129a9 715 }
f61f41d9
MT
716
717# If there's a breakpoint window, update that too
718
4e327047 719 if {[winfo exists .breakpoints]} {
f61f41d9
MT
720 set enabled($bpnum) 0
721 }
8532893d
SG
722}
723
724#
725# Local procedure:
726#
727# insert_breakpoint_tag (win line) - Insert a breakpoint tag in WIN.
728#
729# Description:
730#
731# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to insert a
732# breakpoint tag into window WIN at line LINE.
733#
754e5da2 734
8532893d
SG
735proc insert_breakpoint_tag {win line} {
736 $win configure -state normal
737 $win delete $line.0
738 $win insert $line.0 "B"
9e9cf822 739 $win tag add margin $line.0 $line.8
8532893d
SG
740
741 $win configure -state disabled
742}
743
744#
745# Local procedure:
746#
747# delete_breakpoint_tag (win line) - Remove a breakpoint tag from WIN.
748#
749# Description:
750#
751# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to remove a
752# breakpoint tag from window WIN at line LINE.
753#
754
755proc delete_breakpoint_tag {win line} {
756 $win configure -state normal
757 $win delete $line.0
746d1df4
SG
758 if {[string range $win 0 3] == ".src"} then {
759 $win insert $line.0 "\xa4"
760 } else {
761 $win insert $line.0 " "
762 }
9e9cf822 763 $win tag add margin $line.0 $line.8
8532893d
SG
764 $win configure -state disabled
765}
754e5da2 766
479f0f18 767proc gdbtk_tcl_busy {} {
fda6fadc
SS
768 if {[winfo exists .cmd]} {
769 .cmd.text configure -state disabled
770 }
4e327047 771 if {[winfo exists .src]} {
6131622e
SG
772 .src.start configure -state disabled
773 .src.stop configure -state normal
774 .src.step configure -state disabled
775 .src.next configure -state disabled
776 .src.continue configure -state disabled
777 .src.finish configure -state disabled
778 .src.up configure -state disabled
779 .src.down configure -state disabled
780 .src.bottom configure -state disabled
86db943c 781 }
4e327047 782 if {[winfo exists .asm]} {
6131622e
SG
783 .asm.stepi configure -state disabled
784 .asm.nexti configure -state disabled
785 .asm.continue configure -state disabled
786 .asm.finish configure -state disabled
787 .asm.up configure -state disabled
788 .asm.down configure -state disabled
789 .asm.bottom configure -state disabled
86db943c 790 }
6131622e 791 return
479f0f18
SG
792}
793
794proc gdbtk_tcl_idle {} {
fda6fadc
SS
795 if {[winfo exists .cmd]} {
796 .cmd.text configure -state normal
797 }
4e327047 798 if {[winfo exists .src]} {
6131622e
SG
799 .src.start configure -state normal
800 .src.stop configure -state disabled
801 .src.step configure -state normal
802 .src.next configure -state normal
803 .src.continue configure -state normal
804 .src.finish configure -state normal
805 .src.up configure -state normal
806 .src.down configure -state normal
807 .src.bottom configure -state normal
86db943c 808 }
4e327047 809 if {[winfo exists .asm]} {
6131622e
SG
810 .asm.stepi configure -state normal
811 .asm.nexti configure -state normal
812 .asm.continue configure -state normal
813 .asm.finish configure -state normal
814 .asm.up configure -state normal
815 .asm.down configure -state normal
816 .asm.bottom configure -state normal
86db943c 817 }
6131622e 818 return
479f0f18
SG
819}
820
637b1661
SG
821#
822# Local procedure:
823#
824# pc_to_line (pclist pc) - convert PC to a line number.
825#
826# Description:
827#
828# Convert PC to a line number from PCLIST. If exact line isn't found,
829# we return the first line that starts before PC.
830#
831proc pc_to_line {pclist pc} {
832 set line [lsearch -exact $pclist $pc]
833
834 if {$line >= 1} { return $line }
835
836 set line 1
837 foreach linepc [lrange $pclist 1 end] {
838 if {$pc < $linepc} { decr line ; return $line }
839 incr line
840 }
4e327047 841 return [expr {$line - 1}]
637b1661
SG
842}
843
8532893d
SG
844#
845# Menu:
846#
847# file popup menu - Define the file popup menu.
848#
849# Description:
850#
851# This menu just contains a bunch of buttons that do various things to
852# the line under the cursor.
853#
854# Items:
855#
856# Edit - Run the editor (specified by the environment variable EDITOR) on
857# this file, at the current line.
858# Breakpoint - Set a breakpoint at the current line. This just shoves
859# a `break' command at GDB with the appropriate file and line
860# number. Eventually, GDB calls us back (at gdbtk_tcl_breakpoint)
861# to notify us of where the breakpoint needs to show up.
862#
863
4e327047 864menu .file_popup -cursor hand2 -tearoff 0
8532893d
SG
865.file_popup add command -label "Not yet set" -state disabled
866.file_popup add separator
4e327047
TT
867.file_popup add command -label "Edit" \
868 -command {exec $editor +$selected_line $selected_file &}
869.file_popup add command -label "Set breakpoint" \
870 -command {gdb_cmd "break $selected_file:$selected_line"}
8532893d 871
6131622e
SG
872# Use this procedure to get the GDB core to execute the string `cmd'. This is
873# a wrapper around gdb_cmd, which will catch errors, and send output to the
874# command window. It will also cause all of the other windows to be updated.
875
876proc interactive_cmd {cmd} {
877 catch {gdb_cmd "$cmd"} result
878 .cmd.text insert end $result
4e327047 879 .cmd.text see end
6131622e
SG
880 update_ptr
881}
882
8532893d
SG
883#
884# Bindings:
885#
886# file popup menu - Define the file popup menu bindings.
887#
888# Description:
889#
4e327047
TT
890# This defines the binding for the file popup menu. It simply
891# unhighlights the line under the cursor.
8532893d
SG
892#
893
894bind .file_popup <Any-ButtonRelease-1> {
4e327047
TT
895 global selected_win
896 # Unhighlight the selected line
897 $selected_win tag delete breaktag
754e5da2
SG
898}
899
8532893d
SG
900#
901# Local procedure:
902#
9e9cf822 903# listing_window_popup (win x y xrel yrel) - Handle popups for listing window
8532893d
SG
904#
905# Description:
906#
9e9cf822
SS
907# This procedure is invoked by holding down button 2 (usually) in the
908# listing window. The action taken depends upon where the button was
909# pressed. If it was in the left margin (the breakpoint column), it
910# sets or clears a breakpoint. In the main text area, it will pop up a
911# menu.
8532893d
SG
912#
913
9e9cf822 914proc listing_window_popup {win x y xrel yrel} {
754e5da2
SG
915 global wins
916 global win_to_file
917 global file_to_debug_file
918 global highlight
919 global selected_line
920 global selected_file
921 global selected_win
9e9cf822 922 global pos_to_breakpoint
754e5da2 923
754e5da2
SG
924# Map TK window name back to file name.
925
926 set file $win_to_file($win)
927
9e9cf822 928 set pos [split [$win index @$xrel,$yrel] .]
754e5da2
SG
929
930# Record selected file and line for menu button actions
931
932 set selected_file $file_to_debug_file($file)
9e9cf822
SS
933 set selected_line [lindex $pos 0]
934 set selected_col [lindex $pos 1]
754e5da2
SG
935 set selected_win $win
936
754e5da2
SG
937# Post the menu near the pointer, (and grab it)
938
8532893d 939 .file_popup entryconfigure 0 -label "$selected_file:$selected_line"
9e9cf822
SS
940
941 tk_popup .file_popup $x $y
754e5da2
SG
942}
943
8532893d
SG
944#
945# Local procedure:
946#
9e9cf822 947# toggle_breakpoint (win x y xrel yrel) - Handle clicks on breakdots
8532893d
SG
948#
949# Description:
950#
9e9cf822 951# This procedure sets or clears breakpoints where the button clicked.
8532893d
SG
952#
953
9e9cf822 954proc toggle_breakpoint {win x y xrel yrel} {
8532893d
SG
955 global wins
956 global win_to_file
957 global file_to_debug_file
958 global highlight
959 global selected_line
960 global selected_file
961 global selected_win
962 global pos_to_breakpoint
963
964# Map TK window name back to file name.
965
966 set file $win_to_file($win)
967
968 set pos [split [$win index @$xrel,$yrel] .]
969
9e9cf822 970# Record selected file and line
8532893d
SG
971
972 set selected_file $file_to_debug_file($file)
973 set selected_line [lindex $pos 0]
974 set selected_col [lindex $pos 1]
975 set selected_win $win
976
977# If we're in the margin, then toggle the breakpoint
978
9e9cf822
SS
979 if {$selected_col < 8} { # this is alway true actually
980 set pos_break $selected_file:$selected_line
981 set pos $file:$selected_line
982 set tmp pos_to_breakpoint($pos)
983 if {[info exists $tmp]} {
984 set bpnum [set $tmp]
985 gdb_cmd "delete $bpnum"
986 } else {
987 gdb_cmd "break $pos_break"
988 }
989 return
8532893d 990 }
8532893d
SG
991}
992
993#
994# Local procedure:
995#
996# asm_window_button_1 (win x y xrel yrel) - Handle button 1 in asm window
997#
998# Description:
999#
1000# This procedure is invoked as a result of holding down button 1 in the
1001# assembly window. The action taken depends upon where the button was
1002# pressed. If it was in the left margin (the breakpoint column), it
1003# sets or clears a breakpoint. In the main text area, it will pop up a
1004# menu.
1005#
1006
1007proc asm_window_button_1 {win x y xrel yrel} {
1008 global wins
1009 global win_to_file
1010 global file_to_debug_file
1011 global highlight
1012 global selected_line
1013 global selected_file
1014 global selected_win
1015 global pos_to_breakpoint
1016 global pclist
1017 global cfunc
1018
1019 set pos [split [$win index @$xrel,$yrel] .]
1020
1021# Record selected file and line for menu button actions
1022
1023 set selected_line [lindex $pos 0]
1024 set selected_col [lindex $pos 1]
1025 set selected_win $win
1026
1027# Figure out the PC
1028
1029 set pc [lindex $pclist($cfunc) $selected_line]
1030
1031# If we're in the margin, then toggle the breakpoint
1032
746d1df4 1033 if {$selected_col < 11} {
8532893d 1034 set tmp pos_to_breakpoint($pc)
4e327047 1035 if {[info exists $tmp]} {
8532893d
SG
1036 set bpnum [set $tmp]
1037 gdb_cmd "delete $bpnum"
1038 } else {
1039 gdb_cmd "break *$pc"
1040 }
1041 return
1042 }
1043
1044# Post the menu near the pointer, (and grab it)
1045
1046# .file_popup entryconfigure 0 -label "$selected_file:$selected_line"
1047# .file_popup post [expr $x-[winfo width .file_popup]/2] [expr $y-10]
1048# grab .file_popup
1049}
1050
1051#
1052# Local procedure:
1053#
e12533e3 1054# do_nothing - Does absolutely nothing.
8532893d
SG
1055#
1056# Description:
1057#
1058# This procedure does nothing. It is used as a placeholder to allow
1059# the disabling of bindings that would normally be inherited from the
1060# parent widget. I can't think of any other way to do this.
1061#
1062
754e5da2
SG
1063proc do_nothing {} {}
1064
479f0f18
SG
1065#
1066# Local procedure:
1067#
e12533e3
SS
1068# not_implemented_yet - warn that a feature is unavailable
1069#
1070# Description:
1071#
1072# This procedure warns that something doesn't actually work yet.
1073#
1074
1075proc not_implemented_yet {message} {
c4a5c37c
SS
1076 tk_dialog .unimpl "gdb : unimpl" \
1077 "$message: not implemented in the interface yet" \
4e327047 1078 warning 0 "OK"
e12533e3
SS
1079}
1080
1081##
1082# Local procedure:
1083#
6131622e 1084# create_expr_window - Create expression display window
479f0f18
SG
1085#
1086# Description:
1087#
1088# Create the expression display window.
1089#
1090
4e327047
TT
1091# Set delete_expr_num, and set -state of Delete button.
1092proc expr_update_button {num} {
1093 global delete_expr_num
1094 set delete_expr_num $num
1095 if {$num > 0} then {
1096 set state normal
1097 } else {
1098 set state disabled
1099 }
1100 .expr.buts.delete configure -state $state
1101}
09722039 1102
4e327047
TT
1103proc add_expr {expr} {
1104 global expr_update_list
1105 global expr_num
09722039 1106
4e327047 1107 incr expr_num
09722039 1108
4e327047
TT
1109 set e .expr.exprs
1110 set f e$expr_num
09722039 1111
4e327047
TT
1112 checkbutton $e.updates.$f -text "" -relief flat \
1113 -variable expr_update_list($expr_num)
1114 text $e.expressions.$f -width 20 -height 1
1115 $e.expressions.$f insert 0.0 $expr
1116 bind $e.expressions.$f <1> "update_expr $expr_num"
1117 text $e.values.$f -width 20 -height 1
09722039 1118
4e327047
TT
1119 # Set up some bindings.
1120 foreach frame {updates expressions values} {
1121 bind $e.$frame.$f <FocusIn> "expr_update_button $expr_num"
1122 bind $e.$frame.$f <FocusOut> "expr_update_button 0"
1123 }
09722039 1124
4e327047 1125 update_expr $expr_num
09722039 1126
4e327047
TT
1127 pack $e.updates.$f -side top
1128 pack $e.expressions.$f -side top -expand yes -fill x
1129 pack $e.values.$f -side top -expand yes -fill x
09722039
SG
1130}
1131
09722039 1132proc delete_expr {} {
4e327047 1133 global delete_expr_num
efd14e45
FF
1134 global expr_update_list
1135
4e327047
TT
1136 if {$delete_expr_num > 0} then {
1137 set e .expr.exprs
1138 set f e${delete_expr_num}
09722039 1139
4e327047 1140 destroy $e.updates.$f $e.expressions.$f $e.values.$f
efd14e45 1141 unset expr_update_list($delete_expr_num)
4e327047 1142 }
09722039
SG
1143}
1144
1145proc update_expr {expr_num} {
4e327047 1146 global expr_update_list
09722039 1147
4e327047
TT
1148 set e .expr.exprs
1149 set f e${expr_num}
09722039 1150
4e327047
TT
1151 set expr [$e.expressions.$f get 0.0 end]
1152 $e.values.$f delete 0.0 end
1153 if {! [catch {gdb_eval $expr} val]} {
1154 $e.values.$f insert 0.0 $val
1155 } {
1156 # FIXME consider flashing widget here.
1157 }
280c564c
SG
1158}
1159
1160proc update_exprs {} {
1161 global expr_update_list
09722039 1162
280c564c 1163 foreach expr_num [array names expr_update_list] {
4e327047 1164 if {$expr_update_list($expr_num)} {
280c564c
SG
1165 update_expr $expr_num
1166 }
1167 }
09722039
SG
1168}
1169
6131622e 1170proc create_expr_window {} {
efd14e45
FF
1171 global expr_num
1172 global delete_expr_num
1173 global expr_update_list
280c564c 1174
4e327047 1175 if {[winfo exists .expr]} {raise .expr ; return}
280c564c 1176
efd14e45
FF
1177 # All the state about individual expressions is stored in the
1178 # expression window widgets, so when it is deleted, the
1179 # previous values of the expression global variables become
1180 # invalid. Reset to a known initial state.
1181 set expr_num 0
1182 set delete_expr_num 0
1183 catch {unset expr_update_list}
1184 set expr_update_list(0) 0
1185
479f0f18 1186 toplevel .expr
4e327047
TT
1187 wm title .expr "GDB Expressions"
1188 wm iconname .expr "Expressions"
09722039 1189
4e327047
TT
1190 frame .expr.entryframe -borderwidth 2 -relief raised
1191 label .expr.entryframe.entrylab -text "Expression: "
1192 entry .expr.entryframe.entry -borderwidth 2 -relief sunken
1193 bind .expr.entryframe.entry <Return> {
1194 add_expr [.expr.entryframe.entry get]
1195 .expr.entryframe.entry delete 0 end
1196 }
09722039 1197
4e327047
TT
1198 pack .expr.entryframe.entrylab -side left
1199 pack .expr.entryframe.entry -side left -fill x -expand yes
09722039 1200
4e327047 1201 frame .expr.buts -borderwidth 2 -relief raised
09722039 1202
4e327047
TT
1203 button .expr.buts.delete -text Delete -command delete_expr \
1204 -state disabled
09722039 1205
4e327047
TT
1206 button .expr.buts.close -text Close -command {destroy .expr}
1207 button .expr.buts.help -text Help -state disabled
09722039 1208
4e327047
TT
1209 pack .expr.buts.delete -side left
1210 pack .expr.buts.help .expr.buts.close -side right
09722039
SG
1211
1212 pack .expr.buts -side bottom -fill x
1213 pack .expr.entryframe -side bottom -fill x
1214
4e327047
TT
1215 frame .expr.exprs -borderwidth 2 -relief raised
1216
1217 # Use three subframes so columns will line up. Easier than
1218 # dealing with BLT for a table geometry manager. Someday Tk
1219 # will have one, use it then. FIXME this messes up keyboard
1220 # traversal.
1221 frame .expr.exprs.updates -borderwidth 0 -relief flat
1222 frame .expr.exprs.expressions -borderwidth 0 -relief flat
1223 frame .expr.exprs.values -borderwidth 0 -relief flat
09722039 1224
4e327047
TT
1225 label .expr.exprs.updates.label -text Update
1226 pack .expr.exprs.updates.label -side top -anchor w
1227 label .expr.exprs.expressions.label -text Expression
1228 pack .expr.exprs.expressions.label -side top -anchor w
1229 label .expr.exprs.values.label -text Value
1230 pack .expr.exprs.values.label -side top -anchor w
09722039 1231
4e327047
TT
1232 pack .expr.exprs.updates -side left
1233 pack .expr.exprs.values .expr.exprs.expressions \
1234 -side right -expand 1 -fill x
09722039 1235
4e327047 1236 pack .expr.exprs -side top -fill both -expand 1 -anchor w
479f0f18
SG
1237}
1238
1239#
1240# Local procedure:
1241#
1242# display_expression (expression) - Display EXPRESSION in display window
1243#
1244# Description:
1245#
e12533e3 1246# Display EXPRESSION and its value in the expression display window.
479f0f18
SG
1247#
1248
1249proc display_expression {expression} {
6131622e 1250 create_expr_window
479f0f18 1251
09722039 1252 add_expr $expression
479f0f18
SG
1253}
1254
8532893d
SG
1255#
1256# Local procedure:
1257#
1258# create_file_win (filename) - Create a win for FILENAME.
1259#
1260# Return value:
1261#
1262# The new text widget.
1263#
1264# Description:
1265#
1266# This procedure creates a text widget for FILENAME. It returns the
1267# newly created widget. First, a text widget is created, and given basic
1268# configuration info. Second, all the bindings are setup. Third, the
1269# file FILENAME is read into the text widget. Fourth, margins and line
1270# numbers are added.
1271#
1272
746d1df4 1273proc create_file_win {filename debug_file} {
754e5da2
SG
1274 global breakpoint_file
1275 global breakpoint_line
86db943c 1276 global line_numbers
9e9cf822 1277 global debug_interface
754e5da2 1278
8532893d
SG
1279# Replace all the dirty characters in $filename with clean ones, and generate
1280# a unique name for the text widget.
1281
746d1df4 1282 regsub -all {\.} $filename {} temp
006e71e9 1283 set win .src.text$temp
8532893d 1284
637b1661
SG
1285# Open the file, and read it into the text widget
1286
4e327047 1287 if {[catch "open $filename" fh]} {
746d1df4
SG
1288# File can't be read. Put error message into .src.nofile window and return.
1289
1290 catch {destroy .src.nofile}
6131622e 1291 text .src.nofile -height 25 -width 88 -relief sunken \
4e327047 1292 -borderwidth 2 -yscrollcommand ".src.scroll set" \
746d1df4
SG
1293 -setgrid true -cursor hand2
1294 .src.nofile insert 0.0 $fh
1295 .src.nofile configure -state disabled
1296 bind .src.nofile <1> do_nothing
1297 bind .src.nofile <B1-Motion> do_nothing
1298 return .src.nofile
637b1661
SG
1299 }
1300
8532893d
SG
1301# Actually create and do basic configuration on the text widget.
1302
6131622e 1303 text $win -height 25 -width 88 -relief sunken -borderwidth 2 \
4e327047 1304 -yscrollcommand ".src.scroll set" -setgrid true -cursor hand2
8532893d
SG
1305
1306# Setup all the bindings
1307
754e5da2 1308 bind $win <Enter> {focus %W}
479f0f18 1309 bind $win <1> do_nothing
754e5da2 1310 bind $win <B1-Motion> do_nothing
479f0f18 1311
f1b64caa
SG
1312 bind $win <Key-Alt_R> do_nothing
1313 bind $win <Key-Alt_L> do_nothing
1314 bind $win <Key-Prior> "$win yview {@0,0 - 10 lines}"
1315 bind $win <Key-Next> "$win yview {@0,0 + 10 lines}"
1316 bind $win <Key-Up> "$win yview {@0,0 - 1 lines}"
1317 bind $win <Key-Down> "$win yview {@0,0 + 1 lines}"
1318 bind $win <Key-Home> {update_listing [gdb_loc]}
4e327047 1319 bind $win <Key-End> "$win see end"
f1b64caa 1320
6131622e
SG
1321 bind $win n {interactive_cmd next}
1322 bind $win s {interactive_cmd step}
1323 bind $win c {interactive_cmd continue}
1324 bind $win f {interactive_cmd finish}
1325 bind $win u {interactive_cmd up}
1326 bind $win d {interactive_cmd down}
8532893d 1327
9e9cf822
SS
1328 if $debug_interface {
1329 bind $win <Control-C> {
1330 puts stdout burp
1331 }
1332 }
1333
754e5da2
SG
1334 $win delete 0.0 end
1335 $win insert 0.0 [read $fh]
1336 close $fh
8532893d 1337
86db943c 1338# Add margins (for annotations) and a line number to each line (if requested)
8532893d 1339
754e5da2
SG
1340 set numlines [$win index end]
1341 set numlines [lindex [split $numlines .] 0]
4e327047 1342 if {$line_numbers} {
86db943c
SG
1343 for {set i 1} {$i <= $numlines} {incr i} {
1344 $win insert $i.0 [format " %4d " $i]
1345 $win tag add source $i.8 "$i.0 lineend"
1346 }
1347 } else {
1348 for {set i 1} {$i <= $numlines} {incr i} {
1349 $win insert $i.0 " "
1350 $win tag add source $i.8 "$i.0 lineend"
1351 }
1352 }
479f0f18 1353
746d1df4
SG
1354# Add the breakdots
1355
1356 foreach i [gdb_sourcelines $debug_file] {
1357 $win delete $i.0
1358 $win insert $i.0 "\xa4"
1359 $win tag add margin $i.0 $i.8
1360 }
1361
9e9cf822
SS
1362 # A debugging trick to highlight sensitive regions.
1363 if $debug_interface {
1364 $win tag bind source <Enter> {
1365 %W tag configure source -background yellow
1366 }
1367 $win tag bind source <Leave> {
1368 %W tag configure source -background green
1369 }
1370 $win tag bind margin <Enter> {
1371 %W tag configure margin -background red
1372 }
1373 $win tag bind margin <Leave> {
1374 %W tag configure margin -background skyblue
1375 }
1376 }
1377
1378 $win tag bind margin <1> {
1379 toggle_breakpoint %W %X %Y %x %y
1380 }
1381
479f0f18
SG
1382 $win tag bind source <1> {
1383 %W mark set anchor "@%x,%y wordstart"
1384 set last [%W index "@%x,%y wordend"]
1385 %W tag remove sel 0.0 anchor
1386 %W tag remove sel $last end
1387 %W tag add sel anchor $last
1388 }
1389# $win tag bind source <Double-Button-1> {
1390# %W mark set anchor "@%x,%y wordstart"
1391# set last [%W index "@%x,%y wordend"]
1392# %W tag remove sel 0.0 anchor
1393# %W tag remove sel $last end
1394# %W tag add sel anchor $last
1395# echo "Selected [selection get]"
1396# }
1397 $win tag bind source <B1-Motion> {
1398 %W tag remove sel 0.0 anchor
1399 %W tag remove sel $last end
1400 %W tag add sel anchor @%x,%y
754e5da2 1401 }
f0b0d915
TT
1402 $win tag bind sel <1> break
1403 $win tag bind sel <Double-Button-1> {
9e9cf822
SS
1404 display_expression [selection get]
1405 break
f0b0d915
TT
1406 }
1407 $win tag bind sel <B1-Motion> break
1408 $win tag lower sel
479f0f18 1409
9e9cf822
SS
1410 $win tag bind source <2> {
1411 listing_window_popup %W %X %Y %x %y
1412 }
1413
f0b0d915
TT
1414 # Make these bindings do nothing on the text window -- they
1415 # are completely handled by the tag bindings above.
1416 bind $win <1> break
1417 bind $win <B1-Motion> break
1418 bind $win <Double-Button-1> break
754e5da2 1419
8532893d
SG
1420# Scan though the breakpoint data base and install any destined for this file
1421
754e5da2
SG
1422 foreach bpnum [array names breakpoint_file] {
1423 if {$breakpoint_file($bpnum) == $filename} {
1424 insert_breakpoint_tag $win $breakpoint_line($bpnum)
1425 }
1426 }
1427
8532893d
SG
1428# Disable the text widget to prevent user modifications
1429
754e5da2
SG
1430 $win configure -state disabled
1431 return $win
1432}
1433
8532893d
SG
1434#
1435# Local procedure:
1436#
637b1661 1437# create_asm_win (funcname pc) - Create an assembly win for FUNCNAME.
8532893d
SG
1438#
1439# Return value:
1440#
1441# The new text widget.
1442#
1443# Description:
1444#
1445# This procedure creates a text widget for FUNCNAME. It returns the
1446# newly created widget. First, a text widget is created, and given basic
1447# configuration info. Second, all the bindings are setup. Third, the
1448# function FUNCNAME is read into the text widget.
1449#
1450
637b1661 1451proc create_asm_win {funcname pc} {
8532893d
SG
1452 global breakpoint_file
1453 global breakpoint_line
8532893d 1454 global pclist
280c564c 1455 global disassemble_with_source
8532893d
SG
1456
1457# Replace all the dirty characters in $filename with clean ones, and generate
1458# a unique name for the text widget.
1459
335129a9 1460 set win [asm_win_name $funcname]
8532893d
SG
1461
1462# Actually create and do basic configuration on the text widget.
1463
6131622e 1464 text $win -height 25 -width 80 -relief sunken -borderwidth 2 \
4e327047 1465 -setgrid true -cursor hand2 -yscrollcommand ".asm.scroll set"
8532893d
SG
1466
1467# Setup all the bindings
1468
1469 bind $win <Enter> {focus %W}
f0b0d915
TT
1470 bind $win <1> {asm_window_button_1 %W %X %Y %x %y; break}
1471 bind $win <B1-Motion> break
1472 bind $win <Double-Button-1> break
f1b64caa
SG
1473
1474 bind $win <Key-Alt_R> do_nothing
1475 bind $win <Key-Alt_L> do_nothing
f1b64caa 1476
6131622e
SG
1477 bind $win n {interactive_cmd nexti}
1478 bind $win s {interactive_cmd stepi}
1479 bind $win c {interactive_cmd continue}
1480 bind $win f {interactive_cmd finish}
1481 bind $win u {interactive_cmd up}
1482 bind $win d {interactive_cmd down}
8532893d
SG
1483
1484# Disassemble the code, and read it into the new text widget
1485
6131622e 1486 $win insert end [gdb_disassemble $disassemble_with_source $pc]
8532893d
SG
1487
1488 set numlines [$win index end]
1489 set numlines [lindex [split $numlines .] 0]
637b1661 1490 decr numlines
8532893d
SG
1491
1492# Delete the first and last lines, cuz these contain useless info
1493
09722039
SG
1494# $win delete 1.0 2.0
1495# $win delete {end - 1 lines} end
1496# decr numlines 2
8532893d
SG
1497
1498# Add margins (for annotations) and note the PC for each line
1499
637b1661 1500 catch "unset pclist($funcname)"
335129a9 1501 lappend pclist($funcname) Unused
8532893d
SG
1502 for {set i 1} {$i <= $numlines} {incr i} {
1503 scan [$win get $i.0 "$i.0 lineend"] "%s " pc
1504 lappend pclist($funcname) $pc
1505 $win insert $i.0 " "
1506 }
1507
8532893d
SG
1508# Scan though the breakpoint data base and install any destined for this file
1509
1510# foreach bpnum [array names breakpoint_file] {
1511# if {$breakpoint_file($bpnum) == $filename} {
1512# insert_breakpoint_tag $win $breakpoint_line($bpnum)
1513# }
1514# }
1515
1516# Disable the text widget to prevent user modifications
1517
1518 $win configure -state disabled
1519 return $win
1520}
1521
8532893d
SG
1522#
1523# Local procedure:
1524#
1525# update_listing (linespec) - Update the listing window according to
1526# LINESPEC.
1527#
1528# Description:
1529#
1530# This procedure is called from various places to update the listing
1531# window based on LINESPEC. It is usually invoked with the result of
1532# gdb_loc.
1533#
1534# It will move the cursor, and scroll the text widget if necessary.
1535# Also, it will switch to another text widget if necessary, and update
1536# the label widget too.
1537#
1538# LINESPEC is a list of the form:
1539#
1540# { DEBUG_FILE FUNCNAME FILENAME LINE }, where:
1541#
1542# DEBUG_FILE - is the abbreviated form of the file name. This is usually
1543# the file name string given to the cc command. This is
1544# primarily needed for breakpoint commands, and when an
1545# abbreviated for of the filename is desired.
1546# FUNCNAME - is the name of the function.
1547# FILENAME - is the fully qualified (absolute) file name. It is usually
1548# the same as $PWD/$DEBUG_FILE, where PWD is the working dir
1549# at the time the cc command was given. This is used to
1550# actually locate the file to be displayed.
1551# LINE - The line number to be displayed.
1552#
1553# Usually, this procedure will just move the cursor one line down to the
1554# next line to be executed. However, if the cursor moves out of range
1555# or into another file, it will scroll the text widget so that the line
1556# of interest is in the middle of the viewable portion of the widget.
1557#
1558
754e5da2
SG
1559proc update_listing {linespec} {
1560 global pointers
754e5da2
SG
1561 global wins cfile
1562 global current_label
1563 global win_to_file
1564 global file_to_debug_file
746d1df4 1565 global .src.label
754e5da2 1566
8532893d
SG
1567# Rip the linespec apart
1568
4e327047 1569 lassign $linespec debug_file funcname filename line
754e5da2 1570
8532893d
SG
1571# Sometimes there's no source file for this location
1572
754e5da2
SG
1573 if {$filename == ""} {set filename Blank}
1574
8532893d
SG
1575# If we want to switch files, we need to unpack the current text widget, and
1576# stick in the new one.
1577
754e5da2
SG
1578 if {$filename != $cfile} then {
1579 pack forget $wins($cfile)
1580 set cfile $filename
8532893d
SG
1581
1582# Create a text widget for this file if necessary
1583
4e327047 1584 if {![info exists wins($cfile)]} then {
746d1df4
SG
1585 set wins($cfile) [create_file_win $cfile $debug_file]
1586 if {$wins($cfile) != ".src.nofile"} {
637b1661
SG
1587 set win_to_file($wins($cfile)) $cfile
1588 set file_to_debug_file($cfile) $debug_file
1589 set pointers($cfile) 1.1
1590 }
754e5da2
SG
1591 }
1592
8532893d
SG
1593# Pack the text widget into the listing widget, and scroll to the right place
1594
746d1df4
SG
1595 pack $wins($cfile) -side left -expand yes -in .src.info \
1596 -fill both -after .src.scroll
1597
1598# Make the scrollbar point at the new text widget
1599
1600 .src.scroll configure -command "$wins($cfile) yview"
1601
f0b0d915
TT
1602 # $wins($cfile) see "${line}.0 linestart"
1603 ensure_line_visible $wins($cfile) $line
754e5da2
SG
1604 }
1605
8532893d
SG
1606# Update the label widget in case the filename or function name has changed
1607
754e5da2
SG
1608 if {$current_label != "$filename.$funcname"} then {
1609 set tail [expr [string last / $filename] + 1]
746d1df4
SG
1610 set .src.label "[string range $filename $tail end] : ${funcname}()"
1611# .src.label configure -text "[string range $filename $tail end] : ${funcname}()"
754e5da2
SG
1612 set current_label $filename.$funcname
1613 }
1614
8532893d
SG
1615# Update the pointer, scrolling the text widget if necessary to keep the
1616# pointer in an acceptable part of the screen.
1617
4e327047 1618 if {[info exists pointers($cfile)]} then {
754e5da2
SG
1619 $wins($cfile) configure -state normal
1620 set pointer_pos $pointers($cfile)
1621 $wins($cfile) configure -state normal
746d1df4
SG
1622 $wins($cfile) delete $pointer_pos "$pointer_pos + 2 char"
1623 $wins($cfile) insert $pointer_pos " "
754e5da2
SG
1624
1625 set pointer_pos [$wins($cfile) index $line.1]
1626 set pointers($cfile) $pointer_pos
1627
746d1df4
SG
1628 $wins($cfile) delete $pointer_pos "$pointer_pos + 2 char"
1629 $wins($cfile) insert $pointer_pos "->"
f0b0d915 1630 ensure_line_visible $wins($cfile) $line
754e5da2
SG
1631 $wins($cfile) configure -state disabled
1632 }
1633}
1634
8532893d
SG
1635#
1636# Local procedure:
1637#
746d1df4 1638# create_asm_window - Open up the assembly window.
8532893d
SG
1639#
1640# Description:
1641#
1642# Create an assembly window if it doesn't exist.
1643#
1644
746d1df4 1645proc create_asm_window {} {
8532893d
SG
1646 global cfunc
1647
4e327047 1648 if {[winfo exists .asm]} {raise .asm ; return}
280c564c
SG
1649
1650 set cfunc *None*
1651 set win [asm_win_name $cfunc]
335129a9 1652
280c564c 1653 build_framework .asm Assembly "*NIL*"
006e71e9 1654
09722039
SG
1655# First, delete all the old menu entries
1656
280c564c 1657 .asm.menubar.view.menu delete 0 last
09722039 1658
4e327047 1659 .asm.text configure -yscrollcommand ".asm.scroll set"
8532893d 1660
280c564c
SG
1661 frame .asm.row1
1662 frame .asm.row2
8532893d 1663
280c564c 1664 button .asm.stepi -width 6 -text Stepi \
6131622e 1665 -command {interactive_cmd stepi}
280c564c 1666 button .asm.nexti -width 6 -text Nexti \
6131622e 1667 -command {interactive_cmd nexti}
280c564c 1668 button .asm.continue -width 6 -text Cont \
6131622e 1669 -command {interactive_cmd continue}
280c564c 1670 button .asm.finish -width 6 -text Finish \
6131622e
SG
1671 -command {interactive_cmd finish}
1672 button .asm.up -width 6 -text Up -command {interactive_cmd up}
280c564c 1673 button .asm.down -width 6 -text Down \
6131622e 1674 -command {interactive_cmd down}
280c564c 1675 button .asm.bottom -width 6 -text Bottom \
6131622e 1676 -command {interactive_cmd {frame 0}}
8532893d 1677
280c564c
SG
1678 pack .asm.stepi .asm.continue .asm.up .asm.bottom -side left -padx 3 -pady 5 -in .asm.row1
1679 pack .asm.nexti .asm.finish .asm.down -side left -padx 3 -pady 5 -in .asm.row2
006e71e9 1680
280c564c 1681 pack .asm.row2 .asm.row1 -side bottom -anchor w -before .asm.info
8532893d 1682
280c564c 1683 update
006e71e9 1684
280c564c 1685 update_assembly [gdb_loc]
09722039
SG
1686
1687# We do this update_assembly to get the proper value of disassemble-from-exec.
1688
1689# exec file menu item
280c564c
SG
1690 .asm.menubar.view.menu add radiobutton -label "Exec file" \
1691 -variable disassemble-from-exec -value 1
09722039 1692# target memory menu item
280c564c
SG
1693 .asm.menubar.view.menu add radiobutton -label "Target memory" \
1694 -variable disassemble-from-exec -value 0
1695
1696# Disassemble with source
1697 .asm.menubar.view.menu add checkbutton -label "Source" \
1698 -variable disassemble_with_source -onvalue source \
1699 -offvalue nosource -command {
1700 foreach asm [info command .asm.func_*] {
1701 destroy $asm
1702 }
1703 set cfunc NIL
1704 update_assembly [gdb_loc]
1705 }
8532893d
SG
1706}
1707
746d1df4 1708proc reg_config_menu {} {
746d1df4
SG
1709 catch {destroy .reg.config}
1710 toplevel .reg.config
1711 wm geometry .reg.config +300+300
1712 wm title .reg.config "Register configuration"
1713 wm iconname .reg.config "Reg config"
1714 set regnames [gdb_regnames]
1715 set num_regs [llength $regnames]
1716
86db943c
SG
1717 frame .reg.config.buts
1718
1719 button .reg.config.done -text " Done " -command "
1720 recompute_reg_display_list $num_regs
1721 populate_reg_window
1722 update_registers all
1723 destroy .reg.config "
1724
1725 button .reg.config.update -text Update -command "
1726 recompute_reg_display_list $num_regs
1727 populate_reg_window
1728 update_registers all "
1729
1730 pack .reg.config.buts -side bottom -fill x
746d1df4 1731
86db943c
SG
1732 pack .reg.config.done -side left -fill x -expand yes -in .reg.config.buts
1733 pack .reg.config.update -side right -fill x -expand yes -in .reg.config.buts
746d1df4
SG
1734
1735# Since there can be lots of registers, we build the window with no more than
1736# 32 rows, and as many columns as needed.
1737
1738# First, figure out how many columns we need and create that many column frame
1739# widgets
1740
1741 set ncols [expr ($num_regs + 31) / 32]
1742
1743 for {set col 0} {$col < $ncols} {incr col} {
1744 frame .reg.config.col$col
1745 pack .reg.config.col$col -side left -anchor n
1746 }
1747
1748# Now, create the checkbutton widgets and pack them in the appropriate columns
1749
1750 set col 0
1751 set row 0
1752 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
1753 set regname [lindex $regnames $regnum]
1754 checkbutton .reg.config.col$col.$row -text $regname -pady 0 \
86db943c 1755 -variable regena($regnum) -relief flat -anchor w -bd 1
746d1df4
SG
1756
1757 pack .reg.config.col$col.$row -side top -fill both
1758
1759 incr row
1760 if {$row >= 32} {
1761 incr col
1762 set row 0
1763 }
1764 }
1765}
1766
335129a9
SG
1767#
1768# Local procedure:
1769#
746d1df4 1770# create_registers_window - Open up the register display window.
335129a9
SG
1771#
1772# Description:
1773#
1774# Create the register display window, with automatic updates.
1775#
1776
746d1df4 1777proc create_registers_window {} {
ab5c0a12
FF
1778 global reg_format_natural
1779 global reg_format_decimal
1780 global reg_format_hex
1781 global reg_format_octal
1782 global reg_format_raw
1783 global reg_format_binary
1784 global reg_format_unsigned
746d1df4 1785
3d9f68c0 1786 # If we already have a register window, just use that one.
746d1df4 1787
3d9f68c0 1788 if {[winfo exists .reg]} {raise .reg ; return}
746d1df4 1789
3d9f68c0 1790 # Create an initial register display list consisting of all registers
746d1df4 1791
3d9f68c0 1792 init_reg_info
746d1df4 1793
3d9f68c0 1794 build_framework .reg Registers
746d1df4 1795
3d9f68c0 1796 # First, delete all the old menu entries
86db943c 1797
3d9f68c0 1798 .reg.menubar.view.menu delete 0 last
746d1df4 1799
3d9f68c0 1800 # Natural menu item
ab5c0a12 1801 .reg.menubar.view.menu add checkbutton -label $reg_format_natural(label) \
3d9f68c0
FF
1802 -variable reg_format_natural(enable) -onvalue on -offvalue off \
1803 -command {update_registers redraw}
746d1df4 1804
3d9f68c0 1805 # Decimal menu item
ab5c0a12 1806 .reg.menubar.view.menu add checkbutton -label $reg_format_decimal(label) \
3d9f68c0
FF
1807 -variable reg_format_decimal(enable) -onvalue on -offvalue off \
1808 -command {update_registers redraw}
746d1df4 1809
3d9f68c0 1810 # Hex menu item
ab5c0a12 1811 .reg.menubar.view.menu add checkbutton -label $reg_format_hex(label) \
3d9f68c0
FF
1812 -variable reg_format_hex(enable) -onvalue on -offvalue off \
1813 -command {update_registers redraw}
746d1df4 1814
3d9f68c0 1815 # Octal menu item
ab5c0a12 1816 .reg.menubar.view.menu add checkbutton -label $reg_format_octal(label) \
3d9f68c0
FF
1817 -variable reg_format_octal(enable) -onvalue on -offvalue off \
1818 -command {update_registers redraw}
746d1df4 1819
3d9f68c0 1820 # Binary menu item
ab5c0a12 1821 .reg.menubar.view.menu add checkbutton -label $reg_format_binary(label) \
3d9f68c0
FF
1822 -variable reg_format_binary(enable) -onvalue on -offvalue off \
1823 -command {update_registers redraw}
86db943c 1824
3d9f68c0 1825 # Unsigned menu item
ab5c0a12 1826 .reg.menubar.view.menu add checkbutton -label $reg_format_unsigned(label) \
3d9f68c0
FF
1827 -variable reg_format_unsigned(enable) -onvalue on -offvalue off \
1828 -command {update_registers redraw}
746d1df4 1829
3d9f68c0 1830 # Raw menu item
ab5c0a12 1831 .reg.menubar.view.menu add checkbutton -label $reg_format_raw(label) \
3d9f68c0
FF
1832 -variable reg_format_raw(enable) -onvalue on -offvalue off \
1833 -command {update_registers redraw}
746d1df4 1834
3d9f68c0
FF
1835 # Config menu item
1836 .reg.menubar.view.menu add separator
746d1df4 1837
3d9f68c0
FF
1838 .reg.menubar.view.menu add command -label Config \
1839 -command { reg_config_menu }
1840
1841 destroy .reg.label
1842
1843 # Install the reg names
1844
1845 populate_reg_window
1846 update_registers all
1847}
1848
1849proc init_reg_info {} {
1850 global reg_format_natural
1851 global reg_format_decimal
1852 global reg_format_hex
1853 global reg_format_octal
1854 global reg_format_raw
1855 global reg_format_binary
1856 global reg_format_unsigned
1857 global long_size
1858 global double_size
1859
1860 if {![info exists reg_format_hex]} {
1861 global reg_display_list
1862 global changed_reg_list
1863 global regena
1864
1865 set long_size [lindex [gdb_cmd {p sizeof(long)}] 2]
1866 set double_size [lindex [gdb_cmd {p sizeof(double)}] 2]
1867
1868 # The natural format may print floats or doubles as floating point,
1869 # which typically takes more room that printing ints on the same
1870 # machine. We assume that if longs are 8 bytes that this is
1871 # probably a 64 bit machine. (FIXME)
1872 set reg_format_natural(label) Natural
1873 set reg_format_natural(enable) on
1874 set reg_format_natural(format) {}
1875 if {$long_size == 8} then {
1876 set reg_format_natural(width) 25
1877 } else {
1878 set reg_format_natural(width) 16
1879 }
1880
1881 set reg_format_decimal(label) Decimal
1882 set reg_format_decimal(enable) off
1883 set reg_format_decimal(format) d
1884 if {$long_size == 8} then {
1885 set reg_format_decimal(width) 21
1886 } else {
1887 set reg_format_decimal(width) 12
1888 }
1889
1890 set reg_format_hex(label) Hex
1891 set reg_format_hex(enable) off
1892 set reg_format_hex(format) x
1893 set reg_format_hex(width) [expr $long_size * 2 + 3]
1894
1895 set reg_format_octal(label) Octal
1896 set reg_format_octal(enable) off
1897 set reg_format_octal(format) o
1898 set reg_format_octal(width) [expr $long_size * 8 / 3 + 3]
1899
1900 set reg_format_raw(label) Raw
1901 set reg_format_raw(enable) off
1902 set reg_format_raw(format) r
1903 set reg_format_raw(width) [expr $double_size * 2 + 3]
1904
1905 set reg_format_binary(label) Binary
1906 set reg_format_binary(enable) off
1907 set reg_format_binary(format) t
1908 set reg_format_binary(width) [expr $long_size * 8 + 1]
1909
1910 set reg_format_unsigned(label) Unsigned
1911 set reg_format_unsigned(enable) off
1912 set reg_format_unsigned(format) u
1913 if {$long_size == 8} then {
1914 set reg_format_unsigned(width) 21
1915 } else {
1916 set reg_format_unsigned(width) 11
1917 }
1918
1919 set num_regs [llength [gdb_regnames]]
1920 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
1921 set regena($regnum) 1
1922 }
1923 recompute_reg_display_list $num_regs
1924 #set changed_reg_list $reg_display_list
1925 set changed_reg_list {}
1926 }
746d1df4
SG
1927}
1928
cb3313c1 1929# Convert regena into a list of the enabled $regnums
746d1df4
SG
1930
1931proc recompute_reg_display_list {num_regs} {
1932 global reg_display_list
cb3313c1
SG
1933 global regmap
1934 global regena
746d1df4
SG
1935
1936 catch {unset reg_display_list}
3d9f68c0 1937 set reg_display_list {}
cb3313c1 1938
3d9f68c0 1939 set line 2
746d1df4 1940 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
746d1df4 1941
cb3313c1 1942 if {[set regena($regnum)] != 0} {
746d1df4 1943 lappend reg_display_list $regnum
cb3313c1
SG
1944 set regmap($regnum) $line
1945 incr line
746d1df4
SG
1946 }
1947 }
1948}
1949
1950# Fill out the register window with the names of the regs specified in
1951# reg_display_list.
1952
1953proc populate_reg_window {} {
3d9f68c0
FF
1954 global reg_format_natural
1955 global reg_format_decimal
1956 global reg_format_hex
1957 global reg_format_octal
1958 global reg_format_raw
1959 global reg_format_binary
1960 global reg_format_unsigned
1961 global max_regname_width
1962 global reg_display_list
1963
1964 set win .reg.text
1965 $win configure -state normal
1966
1967 # Clear the entire widget and insert a blank line at the top where
1968 # the column labels will appear.
1969 $win delete 0.0 end
1970 $win insert end "\n"
1971
1972 if {[llength $reg_display_list] > 0} {
746d1df4 1973 set regnames [eval gdb_regnames $reg_display_list]
3d9f68c0
FF
1974 } else {
1975 set regnames {}
1976 }
746d1df4 1977
3d9f68c0 1978 # Figure out the longest register name
335129a9 1979
3d9f68c0 1980 set max_regname_width 0
746d1df4 1981
3d9f68c0
FF
1982 foreach reg $regnames {
1983 set len [string length $reg]
1984 if {$len > $max_regname_width} {set max_regname_width $len}
1985 }
746d1df4 1986
3d9f68c0 1987 set width [expr $max_regname_width + 15]
746d1df4 1988
4149b5f4 1989 set height [expr [llength $regnames] + 1]
335129a9 1990
3d9f68c0 1991 if {$height > 60} {set height 60}
746d1df4 1992
3d9f68c0
FF
1993 $win configure -height $height -width $width
1994 foreach reg $regnames {
1995 $win insert end [format "%-*s\n" $width ${reg}]
1996 }
746d1df4 1997
3d9f68c0
FF
1998 #Delete the blank line left at end by last insertion.
1999 if {[llength $regnames] > 0} {
2000 $win delete {end - 1 char} end
2001 }
2002 $win yview 0
2003 $win configure -state disabled
335129a9
SG
2004}
2005
2006#
2007# Local procedure:
2008#
2009# update_registers - Update the registers window.
2010#
2011# Description:
2012#
3d9f68c0
FF
2013# This procedure updates the registers window according to the value of
2014# the "which" arg.
335129a9
SG
2015#
2016
746d1df4 2017proc update_registers {which} {
3d9f68c0
FF
2018 global max_regname_width
2019 global reg_format_natural
2020 global reg_format_decimal
2021 global reg_format_hex
2022 global reg_format_octal
2023 global reg_format_binary
2024 global reg_format_unsigned
2025 global reg_format_raw
2026 global reg_display_list
2027 global changed_reg_list
2028 global highlight
2029 global regmap
2030
2031 # margin is the column where we start printing values
2032 set margin [expr $max_regname_width + 1]
2033 set win .reg.text
2034 $win configure -state normal
2035
2036 if {$which == "all" || $which == "redraw"} {
2037 set display_list $reg_display_list
2038 $win delete 1.0 1.end
2039 $win insert 1.0 [format "%*s" $max_regname_width " "]
2040 foreach format {natural decimal unsigned hex octal raw binary } {
2041 set field (enable)
2042 set var reg_format_$format$field
2043 if {[set $var] == "on"} {
2044 set field (label)
2045 set var reg_format_$format$field
2046 set label [set $var]
2047 set field (width)
2048 set var reg_format_$format$field
2049 set var [format "%*s" [set $var] $label]
2050 $win insert 1.end $var
2051 }
746d1df4 2052 }
3d9f68c0
FF
2053 } else {
2054 # Unhighlight the old values
746d1df4 2055 foreach regnum $changed_reg_list {
3d9f68c0 2056 $win tag delete $win.$regnum
746d1df4 2057 }
746d1df4 2058 set changed_reg_list [eval gdb_changed_register_list $reg_display_list]
3d9f68c0
FF
2059 set display_list $changed_reg_list
2060 }
2061 foreach regnum $display_list {
2062 set lineindex $regmap($regnum)
2063 $win delete $lineindex.$margin "$lineindex.0 lineend"
2064 foreach format {natural decimal unsigned hex octal raw binary } {
2065 set field (enable)
2066 set var reg_format_$format$field
2067 if {[set $var] == "on"} {
2068 set field (format)
2069 set var reg_format_$format$field
2070 set regval [gdb_fetch_registers [set $var] $regnum]
2071 set field (width)
2072 set var reg_format_$format$field
2073 set regval [format "%*s" [set $var] $regval]
2074 $win insert $lineindex.end $regval
2075 }
2076 }
2077 }
2078 # Now, highlight the changed values of the interesting registers
2079 if {$which != "all"} {
746d1df4 2080 foreach regnum $changed_reg_list {
3d9f68c0
FF
2081 set lineindex $regmap($regnum)
2082 $win tag add $win.$regnum $lineindex.0 "$lineindex.0 lineend"
2083 eval $win tag configure $win.$regnum $highlight
746d1df4 2084 }
3d9f68c0
FF
2085 }
2086 set winwidth $margin
2087 foreach format {natural decimal unsigned hex octal raw binary} {
2088 set field (enable)
2089 set var reg_format_$format$field
2090 if {[set $var] == "on"} {
2091 set field (width)
2092 set var reg_format_$format$field
2093 set winwidth [expr $winwidth + [set $var]]
2094 }
2095 }
2096 $win configure -width $winwidth
2097 $win configure -state disabled
335129a9
SG
2098}
2099
8532893d
SG
2100#
2101# Local procedure:
2102#
2103# update_assembly - Update the assembly window.
2104#
2105# Description:
2106#
2107# This procedure updates the assembly window.
2108#
2109
2110proc update_assembly {linespec} {
2111 global asm_pointers
8532893d
SG
2112 global wins cfunc
2113 global current_label
2114 global win_to_file
2115 global file_to_debug_file
2116 global current_asm_label
2117 global pclist
746d1df4 2118 global .asm.label
8532893d
SG
2119
2120# Rip the linespec apart
2121
4e327047 2122 lassign $linespec debug_file funcname filename line pc
8532893d 2123
335129a9 2124 set win [asm_win_name $cfunc]
8532893d
SG
2125
2126# Sometimes there's no source file for this location
2127
2128 if {$filename == ""} {set filename Blank}
2129
2130# If we want to switch funcs, we need to unpack the current text widget, and
2131# stick in the new one.
2132
637b1661 2133 if {$funcname != $cfunc } {
546b8ca7 2134 set oldwin $win
8532893d
SG
2135 set cfunc $funcname
2136
335129a9 2137 set win [asm_win_name $cfunc]
8532893d
SG
2138
2139# Create a text widget for this func if necessary
2140
637b1661
SG
2141 if {![winfo exists $win]} {
2142 create_asm_win $cfunc $pc
8532893d
SG
2143 set asm_pointers($cfunc) 1.1
2144 set current_asm_label NIL
2145 }
2146
2147# Pack the text widget, and scroll to the right place
2148
546b8ca7 2149 pack forget $oldwin
8532893d 2150 pack $win -side left -expand yes -fill both \
006e71e9 2151 -after .asm.scroll
746d1df4 2152 .asm.scroll configure -command "$win yview"
637b1661 2153 set line [pc_to_line $pclist($cfunc) $pc]
f0b0d915 2154 ensure_line_visible $win $line
0af608b8 2155 update
8532893d
SG
2156 }
2157
2158# Update the label widget in case the filename or function name has changed
2159
335129a9 2160 if {$current_asm_label != "$pc $funcname"} then {
746d1df4 2161 set .asm.label "$pc $funcname"
335129a9 2162 set current_asm_label "$pc $funcname"
8532893d
SG
2163 }
2164
2165# Update the pointer, scrolling the text widget if necessary to keep the
2166# pointer in an acceptable part of the screen.
2167
4e327047 2168 if {[info exists asm_pointers($cfunc)]} then {
8532893d
SG
2169 $win configure -state normal
2170 set pointer_pos $asm_pointers($cfunc)
2171 $win configure -state normal
746d1df4
SG
2172 $win delete $pointer_pos "$pointer_pos + 2 char"
2173 $win insert $pointer_pos " "
8532893d
SG
2174
2175# Map the PC back to a line in the window
2176
637b1661 2177 set line [pc_to_line $pclist($cfunc) $pc]
8532893d
SG
2178
2179 if {$line == -1} {
2180 echo "Can't find PC $pc"
2181 return
2182 }
2183
8532893d
SG
2184 set pointer_pos [$win index $line.1]
2185 set asm_pointers($cfunc) $pointer_pos
2186
746d1df4
SG
2187 $win delete $pointer_pos "$pointer_pos + 2 char"
2188 $win insert $pointer_pos "->"
f0b0d915 2189 ensure_line_visible $win $line
8532893d
SG
2190 $win configure -state disabled
2191 }
2192}
2193
006e71e9
SG
2194#
2195# Local procedure:
2196#
2197# update_ptr - Update the listing window.
2198#
2199# Description:
2200#
2201# This routine will update the listing window using the result of
2202# gdb_loc.
2203#
2204
8532893d
SG
2205proc update_ptr {} {
2206 update_listing [gdb_loc]
4e327047 2207 if {[winfo exists .asm]} {
8532893d
SG
2208 update_assembly [gdb_loc]
2209 }
4e327047 2210 if {[winfo exists .reg]} {
746d1df4 2211 update_registers changed
335129a9 2212 }
4e327047 2213 if {[winfo exists .expr]} {
280c564c
SG
2214 update_exprs
2215 }
4e327047 2216 if {[winfo exists .autocmd]} {
6131622e
SG
2217 update_autocmd
2218 }
8532893d
SG
2219}
2220
006e71e9 2221# Make toplevel window disappear
754e5da2 2222
006e71e9 2223wm withdraw .
754e5da2 2224
754e5da2 2225proc files_command {} {
4e327047
TT
2226 toplevel .files_window
2227
2228 wm minsize .files_window 1 1
2229 # wm overrideredirect .files_window true
76e641bd 2230 listbox .files_window.list -width 30 -height 20 -setgrid true \
4e327047
TT
2231 -yscrollcommand {.files_window.scroll set} -relief sunken \
2232 -borderwidth 2
2233 scrollbar .files_window.scroll -orient vertical \
2234 -command {.files_window.list yview} -relief sunken
2235 button .files_window.close -text Close -command {destroy .files_window}
2236 .files_window.list configure -selectmode single
2237
f4e769dc
TT
2238 # Get the file list from GDB, sort it, and insert into the widget.
2239 eval .files_window.list insert 0 [lsort [gdb_listfiles]]
c81a3fa9 2240
4e327047
TT
2241 pack .files_window.close -side bottom -fill x -expand no -anchor s
2242 pack .files_window.scroll -side right -fill both
2243 pack .files_window.list -side left -fill both -expand yes
396bf873 2244 bind .files_window.list <ButtonRelease-1> {
4e327047
TT
2245 set file [%W get [%W curselection]]
2246 gdb_cmd "list $file:1,0"
2247 update_listing [gdb_loc $file:1]
2248 destroy .files_window
2249 }
396bf873
FF
2250 # We must execute the listbox binding first, because it
2251 # references the widget that will be destroyed by the widget
2252 # binding for Button-Release-1. Otherwise we try to use
2253 # .files_window.list after the .files_window is destroyed.
2254 bind_widget_after_class .files_window.list
754e5da2
SG
2255}
2256
2257button .files -text Files -command files_command
2258
4604b34c 2259proc apply_filespec {label default command} {
a5a6e3bd 2260 set filename [tk_getOpenFile -title $label -initialfile $default]
4604b34c 2261 if {$filename != ""} {
4e327047 2262 if {[catch {gdb_cmd "$command $filename"} retval]} {
4604b34c 2263 tk_dialog .filespec_error "gdb : $label error" \
4e327047
TT
2264 "Error in command \"$command $filename\"" error \
2265 0 Dismiss
4604b34c
SG
2266 return
2267 }
2268 update_ptr
2269 }
2270}
2271
4e327047
TT
2272# Run editor.
2273proc run_editor {editor file} {
2274 # FIXME should use index of line in middle of window, not line at
2275 # top.
2276 global wins
2277 set lineNo [lindex [split [$wins($file) index @0,0] .] 0]
2278 exec $editor +$lineNo $file
2279}
754e5da2 2280
4e327047 2281# Setup command window
006e71e9 2282proc build_framework {win {title GDBtk} {label {}}} {
746d1df4 2283 global ${win}.label
006e71e9
SG
2284
2285 toplevel ${win}
04576ab6 2286 wm title ${win} $title
006e71e9
SG
2287 wm minsize ${win} 1 1
2288
2289 frame ${win}.menubar
2290
2291 menubutton ${win}.menubar.file -padx 12 -text File \
2292 -menu ${win}.menubar.file.menu -underline 0
2293
2294 menu ${win}.menubar.file.menu
e12533e3 2295 ${win}.menubar.file.menu add command -label File... \
4604b34c 2296 -command {apply_filespec File a.out file}
e12533e3 2297 ${win}.menubar.file.menu add command -label Target... \
c4a5c37c 2298 -command { not_implemented_yet "target" }
006e71e9 2299 ${win}.menubar.file.menu add command -label Edit \
4e327047 2300 -command {run_editor $editor $cfile}
e12533e3
SS
2301 ${win}.menubar.file.menu add separator
2302 ${win}.menubar.file.menu add command -label "Exec File..." \
4604b34c 2303 -command {apply_filespec {Exec File} a.out exec-file}
e12533e3 2304 ${win}.menubar.file.menu add command -label "Symbol File..." \
4604b34c 2305 -command {apply_filespec {Symbol File} a.out symbol-file}
e12533e3
SS
2306 ${win}.menubar.file.menu add command -label "Add Symbol File..." \
2307 -command { not_implemented_yet "menu item, add symbol file" }
2308 ${win}.menubar.file.menu add command -label "Core File..." \
4604b34c
SG
2309 -command {apply_filespec {Core File} core core-file}
2310
e12533e3 2311 ${win}.menubar.file.menu add separator
006e71e9
SG
2312 ${win}.menubar.file.menu add command -label Close \
2313 -command "destroy ${win}"
e12533e3 2314 ${win}.menubar.file.menu add separator
006e71e9 2315 ${win}.menubar.file.menu add command -label Quit \
6131622e 2316 -command {interactive_cmd quit}
006e71e9 2317
c4a5c37c
SS
2318 menubutton ${win}.menubar.commands -padx 12 -text Commands \
2319 -menu ${win}.menubar.commands.menu -underline 0
2320
2321 menu ${win}.menubar.commands.menu
2322 ${win}.menubar.commands.menu add command -label Run \
6131622e 2323 -command {interactive_cmd run}
c4a5c37c 2324 ${win}.menubar.commands.menu add command -label Step \
6131622e 2325 -command {interactive_cmd step}
c4a5c37c 2326 ${win}.menubar.commands.menu add command -label Next \
6131622e 2327 -command {interactive_cmd next}
c4a5c37c 2328 ${win}.menubar.commands.menu add command -label Continue \
6131622e 2329 -command {interactive_cmd continue}
c4a5c37c
SS
2330 ${win}.menubar.commands.menu add separator
2331 ${win}.menubar.commands.menu add command -label Stepi \
6131622e 2332 -command {interactive_cmd stepi}
c4a5c37c 2333 ${win}.menubar.commands.menu add command -label Nexti \
6131622e 2334 -command {interactive_cmd nexti}
c4a5c37c 2335
09722039 2336 menubutton ${win}.menubar.view -padx 12 -text Options \
006e71e9
SG
2337 -menu ${win}.menubar.view.menu -underline 0
2338
2339 menu ${win}.menubar.view.menu
c4a5c37c
SS
2340 ${win}.menubar.view.menu add command -label Hex \
2341 -command {echo Hex}
006e71e9
SG
2342 ${win}.menubar.view.menu add command -label Decimal \
2343 -command {echo Decimal}
c4a5c37c
SS
2344 ${win}.menubar.view.menu add command -label Octal \
2345 -command {echo Octal}
006e71e9
SG
2346
2347 menubutton ${win}.menubar.window -padx 12 -text Window \
2348 -menu ${win}.menubar.window.menu -underline 0
2349
2350 menu ${win}.menubar.window.menu
006e71e9 2351 ${win}.menubar.window.menu add command -label Command \
280c564c 2352 -command create_command_window
c4a5c37c
SS
2353 ${win}.menubar.window.menu add separator
2354 ${win}.menubar.window.menu add command -label Source \
6131622e 2355 -command create_source_window
006e71e9 2356 ${win}.menubar.window.menu add command -label Assembly \
6131622e 2357 -command create_asm_window
c4a5c37c
SS
2358 ${win}.menubar.window.menu add separator
2359 ${win}.menubar.window.menu add command -label Registers \
6131622e 2360 -command create_registers_window
09722039 2361 ${win}.menubar.window.menu add command -label Expressions \
6131622e
SG
2362 -command create_expr_window
2363 ${win}.menubar.window.menu add command -label "Auto Command" \
2364 -command create_autocmd_window
f1b64caa
SG
2365 ${win}.menubar.window.menu add command -label Breakpoints \
2366 -command create_breakpoints_window
09722039 2367
280c564c
SG
2368# ${win}.menubar.window.menu add separator
2369# ${win}.menubar.window.menu add command -label Files \
2370# -command { not_implemented_yet "files window" }
006e71e9
SG
2371
2372 menubutton ${win}.menubar.help -padx 12 -text Help \
2373 -menu ${win}.menubar.help.menu -underline 0
2374
2375 menu ${win}.menubar.help.menu
2376 ${win}.menubar.help.menu add command -label "with GDBtk" \
2377 -command {echo "with GDBtk"}
2378 ${win}.menubar.help.menu add command -label "with this window" \
2379 -command {echo "with this window"}
c981300c
SG
2380 ${win}.menubar.help.menu add command -label "Report bug" \
2381 -command {exec send-pr}
006e71e9 2382
c4a5c37c 2383 pack ${win}.menubar.file \
c4a5c37c
SS
2384 ${win}.menubar.view \
2385 ${win}.menubar.window -side left
2386 pack ${win}.menubar.help -side right
006e71e9
SG
2387
2388 frame ${win}.info
6131622e 2389 text ${win}.text -height 25 -width 80 -relief sunken -borderwidth 2 \
006e71e9
SG
2390 -setgrid true -cursor hand2 -yscrollcommand "${win}.scroll set"
2391
746d1df4 2392 set ${win}.label $label
6131622e 2393 label ${win}.label -textvariable ${win}.label -borderwidth 2 -relief sunken
754e5da2 2394
6131622e
SG
2395 scrollbar ${win}.scroll -orient vertical -command "${win}.text yview" \
2396 -relief sunken
006e71e9 2397
f1b64caa
SG
2398 bind $win <Key-Alt_R> do_nothing
2399 bind $win <Key-Alt_L> do_nothing
f1b64caa 2400
006e71e9
SG
2401 pack ${win}.label -side bottom -fill x -in ${win}.info
2402 pack ${win}.scroll -side right -fill y -in ${win}.info
2403 pack ${win}.text -side left -expand yes -fill both -in ${win}.info
2404
2405 pack ${win}.menubar -side top -fill x
2406 pack ${win}.info -side top -fill both -expand yes
2407}
2408
746d1df4
SG
2409proc create_source_window {} {
2410 global wins
2411 global cfile
2412
4e327047 2413 if {[winfo exists .src]} {raise .src ; return}
280c564c 2414
746d1df4
SG
2415 build_framework .src Source "*No file*"
2416
86db943c
SG
2417# First, delete all the old view menu entries
2418
2419 .src.menubar.view.menu delete 0 last
2420
546b8ca7
SG
2421# Source file selection
2422 .src.menubar.view.menu add command -label "Select source file" \
2423 -command files_command
2424
86db943c
SG
2425# Line numbers enable/disable menu item
2426 .src.menubar.view.menu add checkbutton -variable line_numbers \
2427 -label "Line numbers" -onvalue 1 -offvalue 0 -command {
2428 foreach source [array names wins] {
2429 if {$source == "Blank"} continue
2430 destroy $wins($source)
2431 unset wins($source)
2432 }
2433 set cfile Blank
2434 update_listing [gdb_loc]
2435 }
2436
746d1df4
SG
2437 frame .src.row1
2438 frame .src.row2
2439
2440 button .src.start -width 6 -text Start -command \
6131622e
SG
2441 {interactive_cmd {break main}
2442 interactive_cmd {enable delete $bpnum}
2443 interactive_cmd run }
746d1df4
SG
2444 button .src.stop -width 6 -text Stop -fg red -activeforeground red \
2445 -state disabled -command gdb_stop
2446 button .src.step -width 6 -text Step \
6131622e 2447 -command {interactive_cmd step}
746d1df4 2448 button .src.next -width 6 -text Next \
6131622e 2449 -command {interactive_cmd next}
746d1df4 2450 button .src.continue -width 6 -text Cont \
6131622e 2451 -command {interactive_cmd continue}
746d1df4 2452 button .src.finish -width 6 -text Finish \
6131622e 2453 -command {interactive_cmd finish}
86db943c 2454 button .src.up -width 6 -text Up \
6131622e 2455 -command {interactive_cmd up}
746d1df4 2456 button .src.down -width 6 -text Down \
6131622e 2457 -command {interactive_cmd down}
746d1df4 2458 button .src.bottom -width 6 -text Bottom \
6131622e 2459 -command {interactive_cmd {frame 0}}
746d1df4
SG
2460
2461 pack .src.start .src.step .src.continue .src.up .src.bottom \
2462 -side left -padx 3 -pady 5 -in .src.row1
2463 pack .src.stop .src.next .src.finish .src.down -side left -padx 3 \
2464 -pady 5 -in .src.row2
2465
86db943c 2466 pack .src.row2 .src.row1 -side bottom -anchor w -before .src.info
746d1df4
SG
2467
2468 $wins($cfile) insert 0.0 " This page intentionally left blank."
2469 $wins($cfile) configure -width 88 -state disabled \
4e327047 2470 -yscrollcommand ".src.scroll set"
746d1df4 2471}
754e5da2 2472
6131622e
SG
2473proc update_autocmd {} {
2474 global .autocmd.label
2475 global accumulate_output
2476
2477 catch {gdb_cmd "${.autocmd.label}"} result
4e327047 2478 if {!$accumulate_output} { .autocmd.text delete 0.0 end }
6131622e 2479 .autocmd.text insert end $result
4e327047 2480 .autocmd.text see end
6131622e
SG
2481}
2482
2483proc create_autocmd_window {} {
4e327047 2484 global .autocmd.label
6131622e 2485
4e327047 2486 if {[winfo exists .autocmd]} {raise .autocmd ; return}
6131622e 2487
4e327047 2488 build_framework .autocmd "Auto Command" ""
6131622e 2489
4e327047 2490 # First, delete all the old view menu entries
6131622e 2491
4e327047 2492 .autocmd.menubar.view.menu delete 0 last
6131622e 2493
4e327047 2494 # Accumulate output option
6131622e 2495
4e327047
TT
2496 .autocmd.menubar.view.menu add checkbutton \
2497 -variable accumulate_output \
2498 -label "Accumulate output" -onvalue 1 -offvalue 0
6131622e 2499
4e327047 2500 # Now, create entry widget with label
6131622e 2501
4e327047 2502 frame .autocmd.entryframe
6131622e 2503
4e327047
TT
2504 entry .autocmd.entry -borderwidth 2 -relief sunken
2505 bind .autocmd.entry <Key-Return> {
2506 set .autocmd.label [.autocmd.entry get]
2507 .autocmd.entry delete 0 end
2508 }
6131622e 2509
4e327047 2510 label .autocmd.entrylab -text "Command: "
6131622e 2511
4e327047
TT
2512 pack .autocmd.entrylab -in .autocmd.entryframe -side left
2513 pack .autocmd.entry -in .autocmd.entryframe -side left -fill x -expand yes
6131622e 2514
4e327047 2515 pack .autocmd.entryframe -side bottom -fill x -before .autocmd.info
6131622e
SG
2516}
2517
f1b64caa
SG
2518# Return the longest common prefix in SLIST. Can be empty string.
2519
2520proc find_lcp slist {
2521# Handle trivial cases where list is empty or length 1
2522 if {[llength $slist] <= 1} {return [lindex $slist 0]}
2523
2524 set prefix [lindex $slist 0]
2525 set prefixlast [expr [string length $prefix] - 1]
2526
2527 foreach str [lrange $slist 1 end] {
2528 set test_str [string range $str 0 $prefixlast]
2529 while {[string compare $test_str $prefix] != 0} {
2530 decr prefixlast
2531 set prefix [string range $prefix 0 $prefixlast]
2532 set test_str [string range $str 0 $prefixlast]
2533 }
2534 if {$prefixlast < 0} break
2535 }
2536 return $prefix
2537}
2538
2539# Look through COMPLETIONS to generate the suffix needed to do command
2540# completion on CMD.
2541
2542proc find_completion {cmd completions} {
2543# Get longest common prefix
2544 set lcp [find_lcp $completions]
2545 set cmd_len [string length $cmd]
2546# Return suffix beyond end of cmd
2547 return [string range $lcp $cmd_len end]
2548}
2549
746d1df4 2550proc create_command_window {} {
754e5da2 2551 global command_line
f1b64caa 2552 global saw_tab
5bac2b50 2553 global gdb_prompt
754e5da2 2554
f1b64caa 2555 set saw_tab 0
4e327047 2556 if {[winfo exists .cmd]} {raise .cmd ; return}
280c564c 2557
746d1df4
SG
2558 build_framework .cmd Command "* Command Buffer *"
2559
4e327047
TT
2560 # Put focus on command area.
2561 focus .cmd.text
2562
754e5da2 2563 set command_line {}
746d1df4
SG
2564
2565 gdb_cmd {set language c}
2566 gdb_cmd {set height 0}
2567 gdb_cmd {set width 0}
2568
b8f3d4c6
SS
2569 bind .cmd.text <Control-c> gdb_stop
2570
4e327047
TT
2571 # Tk uses the Motifism that Delete means delete forward. I
2572 # hate this, and I'm not gonna take it any more.
2573 set bsBinding [bind Text <BackSpace>]
2574 bind .cmd.text <Delete> "delete_char %W ; $bsBinding; break"
b8f3d4c6 2575 bind .cmd.text <BackSpace> {
81ae689a 2576 if {([%W cget -state] == "disabled")} { break }
b8f3d4c6
SS
2577 delete_char %W
2578 }
2579 bind .cmd.text <Control-u> {
2580 if {([%W cget -state] == "disabled")} { break }
2581 delete_line %W
2582 break
2583 }
746d1df4 2584 bind .cmd.text <Any-Key> {
b8f3d4c6 2585 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2586 set saw_tab 0
2587 %W insert end %A
2588 %W see end
2589 append command_line %A
2590 break
2591 }
746d1df4 2592 bind .cmd.text <Key-Return> {
b8f3d4c6 2593 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2594 set saw_tab 0
2595 %W insert end \n
2596 interactive_cmd $command_line
2597
2598 # %W see end
2599 # catch "gdb_cmd [list $command_line]" result
2600 # %W insert end $result
2601 set command_line {}
2602 # update_ptr
5bac2b50 2603 %W insert end "$gdb_prompt"
4e327047
TT
2604 %W see end
2605 break
2606 }
4604b34c 2607 bind .cmd.text <Button-2> {
4e327047
TT
2608 %W insert end [selection get]
2609 %W see end
2610 append command_line [selection get]
2611 break
4604b34c 2612 }
f0b0d915
TT
2613 bind .cmd.text <B2-Motion> break
2614 bind .cmd.text <ButtonRelease-2> break
f1b64caa 2615 bind .cmd.text <Key-Tab> {
b8f3d4c6 2616 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2617 set choices [gdb_cmd "complete $command_line"]
2618 set choices [string trimright $choices \n]
2619 set choices [split $choices \n]
2620
2621 # Just do completion if this is the first tab
2622 if {!$saw_tab} {
2623 set saw_tab 1
2624 set completion [find_completion $command_line $choices]
2625 append command_line $completion
2626 # Here is where the completion is actually done. If there
2627 # is one match, complete the command and print a space.
2628 # If two or more matches, complete the command and beep.
2629 # If no match, just beep.
2630 switch [llength $choices] {
2631 0 {}
2632 1 {
2633 %W insert end "$completion "
2634 append command_line " "
2635 return
2636 }
746d1df4 2637
4e327047
TT
2638 default {
2639 %W insert end $completion
2640 }
2641 }
2642 bell
2643 %W see end
2644 } else {
2645 # User hit another consecutive tab. List the choices.
2646 # Note that at this point, choices may contain commands
2647 # with spaces. We have to lop off everything before (and
2648 # including) the last space so that the completion list
2649 # only shows the possibilities for the last token.
2650 set choices [lsort $choices]
2651 if {[regexp ".* " $command_line prefix]} {
2652 regsub -all $prefix $choices {} choices
2653 }
5bac2b50 2654 %W insert end "\n[join $choices { }]\n$gdb_prompt$command_line"
4e327047
TT
2655 %W see end
2656 }
2657 break
754e5da2 2658 }
4e327047 2659}
754e5da2 2660
81ae689a
FF
2661# Trim one character off the command line. The argument is ignored.
2662
4e327047
TT
2663proc delete_char {win} {
2664 global command_line
2665 set tmp [expr [string length $command_line] - 2]
2666 set command_line [string range $command_line 0 $tmp]
2667}
2668
81ae689a
FF
2669# FIXME: This should actually check that the first characters of the current
2670# line match the gdb prompt, since the user can move the insertion point
2671# anywhere. It should also check that the insertion point is in the last
2672# line of the text widget.
2673
4e327047 2674proc delete_line {win} {
81ae689a
FF
2675 global command_line
2676 global gdb_prompt
4e327047 2677
81ae689a
FF
2678 set tmp [string length $gdb_prompt]
2679 $win delete "insert linestart + $tmp chars" "insert lineend"
2680 $win see insert
2681 set command_line {}
754e5da2
SG
2682}
2683
4e327047
TT
2684#
2685# Create a copyright window and center it on the screen. Arrange for
2686# it to disappear when the user clicks it, or after a suitable period
2687# of time.
2688#
2689proc create_copyright_window {} {
2690 toplevel .c
2691 message .c.m -text [gdb_cmd {show version}] -aspect 500 -relief raised
2692 pack .c.m
a5cffdc4 2693
4e327047 2694 bind .c.m <1> {destroy .c}
f0b0d915 2695 bind .c <Leave> {destroy .c}
50140ab2
FF
2696 # "suitable period" currently means "30 seconds".
2697 after 30000 {
4e327047
TT
2698 if {[winfo exists .c]} then {
2699 destroy .c
2700 }
2701 }
746d1df4 2702
4e327047
TT
2703 wm transient .c .
2704 center_window .c
a5cffdc4 2705}
746d1df4 2706
954a4a2a
FF
2707# Begin support primarily for debugging the tcl/tk portion of gdbtk. You can
2708# start gdbtk, and then issue the command "tk tclsh" and a window will pop up
2709# giving you direct access to the tcl interpreter. With this, it is very easy
2710# to examine the values of global variables, directly invoke routines that are
2711# part of the gdbtk interface, replace existing proc's with new ones, etc.
2712# This code was inspired from example 11-3 in Brent Welch's "Practical
2713# Programming in Tcl and Tk"
2714
2715set tcl_prompt "tcl> "
2716
2717# Get the current command that user has typed, from cmdstart to end of text
2718# widget. Evaluate it, insert result back into text widget, issue a new
2719# prompt, update text widget and update command start mark.
2720
2721proc evaluate_tcl_command { twidget } {
2722 global tcl_prompt
2723
2724 set command [$twidget get cmdstart end]
2725 if [info complete $command] {
2726 set err [catch {uplevel #0 $command} result]
2727 $twidget insert insert \n$result\n
2728 $twidget insert insert $tcl_prompt
2729 $twidget see insert
2730 $twidget mark set cmdstart insert
2731 return
2732 }
2733}
2734
2735# Create the evaluation window and set up the keybindings to evaluate the
2736# last single line entered by the user. FIXME: allow multiple lines?
2737
2738proc tclsh {} {
2739 global tcl_prompt
2740
5bac2b50
FF
2741 # If another evaluation window already exists, just bring it to the front.
2742 if {[winfo exists .eval]} {raise .eval ; return}
2743
954a4a2a
FF
2744 # Create top level frame with scrollbar and text widget.
2745 toplevel .eval
2746 wm title .eval "Tcl Evaluation"
2747 wm iconname .eval "Tcl"
2748 text .eval.text -width 80 -height 20 -setgrid true -cursor hand2 \
2749 -yscrollcommand {.eval.scroll set}
2750 scrollbar .eval.scroll -command {.eval.text yview}
41756e56
FF
2751 pack .eval.scroll -side right -fill y
2752 pack .eval.text -side left -fill both -expand true
954a4a2a
FF
2753
2754 # Insert the tcl_prompt and initialize the cmdstart mark
2755 .eval.text insert insert $tcl_prompt
2756 .eval.text mark set cmdstart insert
2757 .eval.text mark gravity cmdstart left
2758
2759 # Make this window the current one for input.
2760 focus .eval.text
2761
2762 # Keybindings that limit input and evaluate things
2763 bind .eval.text <Return> { evaluate_tcl_command .eval.text ; break }
3f8eefba
FF
2764 bind .eval.text <BackSpace> {
2765 if [%W compare insert > cmdstart] {
2766 %W delete {insert - 1 char} insert
2767 } else {
2768 bell
2769 }
2770 break
2771 }
954a4a2a
FF
2772 bind .eval.text <Any-Key> {
2773 if [%W compare insert < cmdstart] {
2774 %W mark set insert end
2775 }
2776 }
3f8eefba
FF
2777 bind .eval.text <Control-u> {
2778 %W delete cmdstart "insert lineend"
2779 %W see insert
2780 }
954a4a2a
FF
2781 bindtags .eval.text {.eval.text Text all}
2782}
2783
5bac2b50
FF
2784# This proc is executed just prior to falling into the Tk main event loop.
2785proc gdbtk_tcl_preloop {} {
2786 global gdb_prompt
2787 .cmd.text insert end "$gdb_prompt"
2788 .cmd.text see end
2789 update
2790}
2791
4e327047
TT
2792# FIXME need to handle mono here. In Tk4 that is more complicated.
2793set highlight "-background red2 -borderwidth 2 -relief sunken"
09722039 2794
4e327047
TT
2795# Setup the initial windows
2796create_source_window
2797create_command_window
09722039 2798
4e327047
TT
2799# Make this last so user actually sees it.
2800create_copyright_window
2801# Refresh.
6131622e 2802update
09722039 2803
4e327047
TT
2804if {[file exists ~/.gdbtkinit]} {
2805 source ~/.gdbtkinit
6bd7d9fa 2806}
This page took 0.294555 seconds and 4 git commands to generate.