-
Notifications
You must be signed in to change notification settings - Fork 1
/
gud.el.orig
3581 lines (3186 loc) · 135 KB
/
gud.el.orig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
;; Author: Eric S. Raymond <[email protected]>
;; Maintainer: FSF
;; Keywords: unix, tools
;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003,
;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; The ancestral gdb.el was by W. Schelter <[email protected]> It was
;; later rewritten by rms. Some ideas were due to Masanobu. Grand
;; Unification (sdb/dbx support) by Eric S. Raymond <[email protected]> Barry
;; Warsaw <[email protected]> hacked the mode to use comint.el. Shane Hartman
;; <[email protected]> added support for xdb (HPUX debugger). Rick Sladkey
;; <[email protected]> wrote the GDB command completion code. Dave Love
;; <[email protected]> added the IRIX kluge, re-implemented the Mips-ish variant
;; and added a menu. Brian D. Carlstrom <[email protected]> combined the IRIX
;; kluge with the gud-xdb-directories hack producing gud-dbx-directories.
;; Derek L. Davies <[email protected]> added support for jdb (Java
;; debugger.)
;;; Code:
(eval-when-compile (require 'cl)) ; for case macro
(require 'comint)
(defvar gdb-active-process)
(defvar gdb-define-alist)
(defvar gdb-macro-info)
(defvar gdb-server-prefix)
(defvar gdb-show-changed-values)
(defvar gdb-var-list)
(defvar gdb-speedbar-auto-raise)
(defvar tool-bar-map)
;; ======================================================================
;; GUD commands must be visible in C buffers visited by GUD
(defgroup gud nil
"Grand Unified Debugger mode for gdb and other debuggers under Emacs.
Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python) and jdb."
:group 'unix
:group 'tools)
(defcustom gud-key-prefix "\C-x\C-a"
"Prefix of all GUD commands valid in C buffers."
:type 'string
:group 'gud)
(global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
(define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
(defvar gud-marker-filter nil)
(put 'gud-marker-filter 'permanent-local t)
(defvar gud-find-file nil)
(put 'gud-find-file 'permanent-local t)
(defun gud-marker-filter (&rest args)
(apply gud-marker-filter args))
(defvar gud-minor-mode nil)
(put 'gud-minor-mode 'permanent-local t)
(defvar gud-comint-buffer nil)
(defvar gud-keep-buffer nil)
(defun gud-symbol (sym &optional soft minor-mode)
"Return the symbol used for SYM in MINOR-MODE.
MINOR-MODE defaults to `gud-minor-mode'.
The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
If SOFT is non-nil, returns nil if the symbol doesn't already exist."
(unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
(funcall (if soft 'intern-soft 'intern)
(format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
(defun gud-val (sym &optional minor-mode)
"Return the value of `gud-symbol' SYM. Default to nil."
(let ((sym (gud-symbol sym t minor-mode)))
(if (boundp sym) (symbol-value sym))))
(defvar gud-running nil
"Non-nil if debugged program is running.
Used to grey out relevant toolbar icons.")
(defvar gdb-ready nil)
;; Use existing Info buffer, if possible.
(defun gud-goto-info ()
"Go to relevant Emacs info node."
(interactive)
(let ((same-window-regexps same-window-regexps)
(display-buffer-reuse-frames t))
(catch 'info-found
(walk-windows
'(lambda (window)
(if (eq (window-buffer window) (get-buffer "*info*"))
(progn
(setq same-window-regexps nil)
(throw 'info-found nil))))
nil 0)
(select-frame (make-frame)))
(if (memq gud-minor-mode '(gdbmi gdba))
(info "(emacs)GDB Graphical Interface")
(info "(emacs)Debuggers"))))
(defun gud-tool-bar-item-visible-no-fringe ()
(not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
(and (memq gud-minor-mode '(gdbmi gdba))
(> (car (window-fringes)) 0)))))
(defun gud-stop-subjob ()
(interactive)
(with-current-buffer gud-comint-buffer
(if (string-equal gud-target-name "emacs")
(comint-stop-subjob)
(comint-interrupt-subjob))))
(easy-mmode-defmap gud-menu-map
'(([help] "Info" . gud-goto-info)
([tooltips] menu-item "Show GUD tooltips" gud-tooltip-mode
:enable (and (not emacs-basic-display)
(display-graphic-p)
(fboundp 'x-show-tip))
:visible (memq gud-minor-mode
'(lldb gdbmi gdba dbx sdb xdb pdb))
:button (:toggle . gud-tooltip-mode))
([refresh] "Refresh" . gud-refresh)
([run] menu-item "Run" gud-run
:enable (not gud-running)
:visible (memq gud-minor-mode '(lldb gdbmi gdb dbx jdb)))
([go] menu-item (if gdb-active-process "Continue" "Run") gud-go
:visible (and (not gud-running)
(eq gud-minor-mode 'gdba)))
([stop] menu-item "Stop" gud-stop-subjob
:visible (or (not (memq gud-minor-mode '(gdba pdb)))
(and gud-running
(eq gud-minor-mode 'gdba))))
([until] menu-item "Continue to selection" gud-until
:enable (not gud-running)
:visible (and (memq gud-minor-mode '(gdbmi gdba gdb perldb))
(gud-tool-bar-item-visible-no-fringe)))
([remove] menu-item "Remove Breakpoint" gud-remove
:enable (not gud-running)
:visible (gud-tool-bar-item-visible-no-fringe))
([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
:enable (not gud-running)
:visible (memq gud-minor-mode
'(lldb gdbmi gdba gdb sdb xdb)))
([break] menu-item "Set Breakpoint" gud-break
:enable (not gud-running)
:visible (gud-tool-bar-item-visible-no-fringe))
([up] menu-item "Up Stack" gud-up
:enable (not gud-running)
:visible (memq gud-minor-mode
'(lldb gdbmi gdba gdb dbx xdb jdb pdb)))
([down] menu-item "Down Stack" gud-down
:enable (not gud-running)
:visible (memq gud-minor-mode
'(lldb gdbmi gdba gdb dbx xdb jdb pdb)))
([pp] menu-item "Print S-expression" gud-pp
:enable (and (not gud-running)
gdb-active-process)
:visible (and (string-equal
(buffer-local-value
'gud-target-name gud-comint-buffer) "emacs")
(eq gud-minor-mode 'gdba)))
([print*] menu-item "Print Dereference" gud-pstar
:enable (not gud-running)
:visible (memq gud-minor-mode '(lldb gdbmi gdba gdb)))
([print] menu-item "Print Expression" gud-print
:enable (not gud-running))
([watch] menu-item "Watch Expression" gud-watch
:enable (not gud-running)
:visible (memq gud-minor-mode '(gdbmi gdba)))
([finish] menu-item "Finish Function" gud-finish
:enable (not gud-running)
:visible (memq gud-minor-mode
'(lldb gdbmi gdba gdb xdb jdb pdb)))
([stepi] menu-item "Step Instruction" gud-stepi
:enable (not gud-running)
:visible (memq gud-minor-mode '(lldb gdbmi gdba gdb dbx)))
([nexti] menu-item "Next Instruction" gud-nexti
:enable (not gud-running)
:visible (memq gud-minor-mode '(lldb gdbmi gdba gdb dbx)))
([step] menu-item "Step Line" gud-step
:enable (not gud-running))
([next] menu-item "Next Line" gud-next
:enable (not gud-running))
([cont] menu-item "Continue" gud-cont
:enable (not gud-running)
:visible (not (eq gud-minor-mode 'gdba))))
"Menu for `gud-mode'."
:name "Gud")
(easy-mmode-defmap gud-minor-mode-map
(append
`(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
;; Get tool bar like functionality from the menu bar on a text only
;; terminal.
(unless window-system
`(([menu-bar down]
. (,(propertize "down" 'face 'font-lock-doc-face) . gud-down))
([menu-bar up]
. (,(propertize "up" 'face 'font-lock-doc-face) . gud-up))
([menu-bar finish]
. (,(propertize "finish" 'face 'font-lock-doc-face) . gud-finish))
([menu-bar step]
. (,(propertize "step" 'face 'font-lock-doc-face) . gud-step))
([menu-bar next]
. (,(propertize "next" 'face 'font-lock-doc-face) . gud-next))
([menu-bar until] menu-item
,(propertize "until" 'face 'font-lock-doc-face) gud-until
:visible (memq gud-minor-mode '(gdbmi gdba gdb perldb)))
([menu-bar cont] menu-item
,(propertize "cont" 'face 'font-lock-doc-face) gud-cont
:visible (not (eq gud-minor-mode 'gdba)))
([menu-bar run] menu-item
,(propertize "run" 'face 'font-lock-doc-face) gud-run
:visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
([menu-bar go] menu-item
,(propertize " go " 'face 'font-lock-doc-face) gud-go
:visible (and (not gud-running)
(eq gud-minor-mode 'gdba)))
([menu-bar stop] menu-item
,(propertize "stop" 'face 'font-lock-doc-face) gud-stop-subjob
:visible (or gud-running
(not (eq gud-minor-mode 'gdba))))
([menu-bar print]
. (,(propertize "print" 'face 'font-lock-doc-face) . gud-print))
([menu-bar tools] . undefined)
([menu-bar buffer] . undefined)
([menu-bar options] . undefined)
([menu-bar edit] . undefined)
([menu-bar file] . undefined))))
"Map used in visited files.")
(let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
(if m (setcdr m gud-minor-mode-map)
(push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
(defvar gud-mode-map
;; Will inherit from comint-mode via define-derived-mode.
(make-sparse-keymap)
"`gud-mode' keymap.")
(defvar gud-tool-bar-map
(if (display-graphic-p)
(let ((map (make-sparse-keymap)))
(dolist (x '((gud-break . "gud/break")
(gud-remove . "gud/remove")
(gud-print . "gud/print")
(gud-pstar . "gud/pstar")
(gud-pp . "gud/pp")
(gud-watch . "gud/watch")
(gud-run . "gud/run")
(gud-go . "gud/go")
(gud-stop-subjob . "gud/stop")
(gud-cont . "gud/cont")
(gud-until . "gud/until")
(gud-next . "gud/next")
(gud-step . "gud/step")
(gud-finish . "gud/finish")
(gud-nexti . "gud/nexti")
(gud-stepi . "gud/stepi")
(gud-up . "gud/up")
(gud-down . "gud/down")
(gud-goto-info . "info"))
map)
(tool-bar-local-item-from-menu
(car x) (cdr x) map gud-minor-mode-map)))))
(defun gud-file-name (f)
"Transform a relative file name to an absolute file name.
Uses `gud-<MINOR-MODE>-directories' to find the source files."
(if (file-exists-p f) (expand-file-name f)
(let ((directories (gud-val 'directories))
(result nil))
(while directories
(let ((path (expand-file-name f (car directories))))
(if (file-exists-p path)
(setq result path
directories nil)))
(setq directories (cdr directories)))
result)))
(defun gud-find-file (file)
;; Don't get confused by double slashes in the name that comes from GDB.
(while (string-match "//+" file)
(setq file (replace-match "/" t t file)))
(let ((minor-mode gud-minor-mode)
(buf (funcall (or gud-find-file 'gud-file-name) file)))
(when (stringp buf)
(setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
(when buf
;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
(with-current-buffer buf
(set (make-local-variable 'gud-minor-mode) minor-mode)
(set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
(when (and gud-tooltip-mode
(memq gud-minor-mode '(gdbmi gdba)))
(make-local-variable 'gdb-define-alist)
(unless gdb-define-alist (gdb-create-define-alist))
(add-hook 'after-save-hook 'gdb-create-define-alist nil t))
(make-local-variable 'gud-keep-buffer))
buf)))
;; ======================================================================
;; command definition
;; This macro is used below to define some basic debugger interface commands.
;; Of course you may use `gud-def' with any other debugger command, including
;; user defined ones.
;; A macro call like (gud-def FUNC CMD KEY DOC) expands to a form
;; which defines FUNC to send the command CMD to the debugger, gives
;; it the docstring DOC, and binds that function to KEY in the GUD
;; major mode. The function is also bound in the global keymap with the
;; GUD prefix.
(defmacro gud-def (func cmd key &optional doc)
"Define FUNC to be a command sending CMD and bound to KEY, with
optional doc string DOC. Certain %-escapes in the string arguments
are interpreted specially if present. These are:
%f -- Name (without directory) of current source file.
%F -- Name (without directory or extension) of current source file.
%d -- Directory of current source file.
%l -- Number of current source line.
%e -- Text of the C lvalue or function-call expression surrounding point.
%a -- Text of the hexadecimal address surrounding point.
%b -- Text of the most recently created breakpoint id.
%p -- Prefix argument to the command (if any) as a number.
%c -- Fully qualified class name derived from the expression
surrounding point (jdb only).
The `current' source file is the file of the current buffer (if
we're in a C file) or the source file current at the last break or
step (if we're in the GUD buffer).
The `current' line is that of the current buffer (if we're in a
source file) or the source line number at the last break or step (if
we're in the GUD buffer)."
`(progn
(defun ,func (arg)
,@(if doc (list doc))
(interactive "p")
(if (not gud-running)
,(if (stringp cmd)
`(gud-call ,cmd arg)
cmd)))
,(if key `(local-set-key ,(concat "\C-c" key) ',func))
,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
;; Where gud-display-frame should put the debugging arrow; a cons of
;; (filename . line-number). This is set by the marker-filter, which scans
;; the debugger's output for indications of the current program counter.
(defvar gud-last-frame nil)
;; Used by gud-refresh, which should cause gud-display-frame to redisplay
;; the last frame, even if it's been called before and gud-last-frame has
;; been set to nil.
(defvar gud-last-last-frame nil)
;; All debugger-specific information is collected here.
;; Here's how it works, in case you ever need to add a debugger to the mode.
;;
;; Each entry must define the following at startup:
;;
;;<name>
;; comint-prompt-regexp
;; gud-<name>-massage-args
;; gud-<name>-marker-filter
;; gud-<name>-find-file
;;
;; The job of the massage-args method is to modify the given list of
;; debugger arguments before running the debugger.
;;
;; The job of the marker-filter method is to detect file/line markers in
;; strings and set the global gud-last-frame to indicate what display
;; action (if any) should be triggered by the marker. Note that only
;; whatever the method *returns* is displayed in the buffer; thus, you
;; can filter the debugger's output, interpreting some and passing on
;; the rest.
;;
;; The job of the find-file method is to visit and return the buffer indicated
;; by the car of gud-tag-frame. This may be a file name, a tag name, or
;; something else.
;; ======================================================================
;; speedbar support functions and variables.
(eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
(defvar gud-last-speedbar-stackframe nil
"Description of the currently displayed GUD stack.
The value t means that there is no stack, and we are in display-file mode.")
(defvar gud-speedbar-key-map nil
"Keymap used when in the buffers display mode.")
(defun gud-speedbar-item-info ()
"Display the data type of the watch expression element."
(let ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list)))
(if (nth 6 var)
(speedbar-message "%s: %s" (nth 6 var) (nth 3 var))
(speedbar-message "%s" (nth 3 var)))))
(defun gud-install-speedbar-variables ()
"Install those variables used by speedbar to enhance gud/gdb."
(if gud-speedbar-key-map
nil
(setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
(define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
(define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
(define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
(define-key gud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
(define-key gud-speedbar-key-map "D" 'gdb-var-delete)
(define-key gud-speedbar-key-map "p" 'gud-pp))
(speedbar-add-expansion-list '("GUD" gud-speedbar-menu-items
gud-speedbar-key-map
gud-expansion-speedbar-buttons))
(add-to-list
'speedbar-mode-functions-list
'("GUD" (speedbar-item-info . gud-speedbar-item-info)
(speedbar-line-directory . ignore))))
(defvar gud-speedbar-menu-items
'(["Jump to stack frame" speedbar-edit-line
:visible (not (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
'(gdbmi gdba)))]
["Edit value" speedbar-edit-line
:visible (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
'(gdbmi gdba))]
["Delete expression" gdb-var-delete
:visible (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
'(gdbmi gdba))]
["Auto raise frame" gdb-speedbar-auto-raise
:style toggle :selected gdb-speedbar-auto-raise
:visible (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
'(gdbmi gdba))]
("Output Format"
:visible (memq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
'(gdbmi gdba))
["Binary" (gdb-var-set-format "binary") t]
["Natural" (gdb-var-set-format "natural") t]
["Hexadecimal" (gdb-var-set-format "hexadecimal") t]))
"Additional menu items to add to the speedbar frame.")
;; Make sure our special speedbar mode is loaded
(if (featurep 'speedbar)
(gud-install-speedbar-variables)
(add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
(defun gud-expansion-speedbar-buttons (directory zero)
"Wrapper for call to `speedbar-add-expansion-list'.
DIRECTORY and ZERO are not used, but are required by the caller."
(gud-speedbar-buttons gud-comint-buffer))
(defun gud-speedbar-buttons (buffer)
"Create a speedbar display based on the current state of GUD.
If the GUD BUFFER is not running a supported debugger, then turn
off the specialized speedbar mode. BUFFER is not used, but is
required by the caller."
(when (and gud-comint-buffer
;; gud-comint-buffer might be killed
(buffer-name gud-comint-buffer))
(let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
(window (get-buffer-window (current-buffer) 0))
(start (window-start window))
(p (window-point window)))
(cond
((memq minor-mode '(gdbmi gdba))
(erase-buffer)
(insert "Watch Expressions:\n")
(if gdb-speedbar-auto-raise
(raise-frame speedbar-frame))
(let ((var-list gdb-var-list) parent)
(while var-list
(let* (char (depth 0) (start 0) (var (car var-list))
(varnum (car var)) (expr (nth 1 var))
(type (if (nth 3 var) (nth 3 var) " "))
(value (nth 4 var)) (status (nth 5 var)))
(put-text-property
0 (length expr) 'face font-lock-variable-name-face expr)
(put-text-property
0 (length type) 'face font-lock-type-face type)
(while (string-match "\\." varnum start)
(setq depth (1+ depth)
start (1+ (match-beginning 0))))
(if (eq depth 0) (setq parent nil))
(if (or (equal (nth 2 var) "0")
(and (equal (nth 2 var) "1")
(string-match "char \\*$" type)))
(speedbar-make-tag-line
'bracket ?? nil nil
(concat expr "\t" value)
(if (or parent (eq status 'out-of-scope))
nil 'gdb-edit-value)
nil
(if gdb-show-changed-values
(or parent (case status
(changed 'font-lock-warning-face)
(out-of-scope 'shadow)
(t t)))
t)
depth)
(if (eq status 'out-of-scope) (setq parent 'shadow))
(if (and (nth 1 var-list)
(string-match (concat varnum "\\.")
(car (nth 1 var-list))))
(setq char ?-)
(setq char ?+))
(if (string-match "\\*$\\|\\*&$" type)
(speedbar-make-tag-line
'bracket char
'gdb-speedbar-expand-node varnum
(concat expr "\t" type "\t" value)
(if (or parent (eq status 'out-of-scope))
nil 'gdb-edit-value)
nil
(if gdb-show-changed-values
(or parent (case status
(changed 'font-lock-warning-face)
(out-of-scope 'shadow)
(t t)))
t)
depth)
(speedbar-make-tag-line
'bracket char
'gdb-speedbar-expand-node varnum
(concat expr "\t" type)
nil nil
(if (and (or parent status) gdb-show-changed-values)
'shadow t)
depth))))
(setq var-list (cdr var-list)))))
(t (unless (and (save-excursion
(goto-char (point-min))
(looking-at "Current Stack:"))
(equal gud-last-last-frame gud-last-speedbar-stackframe))
(let ((gud-frame-list
(cond ((eq minor-mode 'gdb)
(gud-gdb-get-stackframe buffer))
;; Add more debuggers here!
(t (speedbar-remove-localized-speedbar-support buffer)
nil))))
(erase-buffer)
(if (not gud-frame-list)
(insert "No Stack frames\n")
(insert "Current Stack:\n"))
(dolist (frame gud-frame-list)
(insert (nth 1 frame) ":\n")
(if (= (length frame) 2)
(progn
(speedbar-insert-button (car frame)
'speedbar-directory-face
nil nil nil t))
(speedbar-insert-button
(car frame)
'speedbar-file-face
'speedbar-highlight-face
(cond ((memq minor-mode '(gdbmi gdba gdb))
'gud-gdb-goto-stackframe)
(t (error "Should never be here")))
frame t))))
(setq gud-last-speedbar-stackframe gud-last-last-frame))))
(set-window-start window start)
(set-window-point window p))))
;; ======================================================================
;; gdb functions
;; History of argument lists passed to gdb.
(defvar gud-gdb-history nil)
(defcustom gud-gud-gdb-command-name "gdb --fullname"
"Default command to run an executable under GDB in text command mode.
The option \"--fullname\" must be included in this value."
:type 'string
:group 'gud)
(defvar gud-gdb-marker-regexp
;; This used to use path-separator instead of ":";
;; however, we found that on both Windows 32 and MSDOS
;; a colon is correct here.
(concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
"\\([0-9]*\\)" ":" ".*\n"))
;; There's no guarantee that Emacs will hand the filter the entire
;; marker at once; it could be broken up across several strings. We
;; might even receive a big chunk with several markers in it. If we
;; receive a chunk of text which looks like it might contain the
;; beginning of a marker, we save it here between calls to the
;; filter.
(defvar gud-marker-acc "")
(make-variable-buffer-local 'gud-marker-acc)
(defun gud-gdb-marker-filter (string)
(setq gud-marker-acc (concat gud-marker-acc string))
(let ((output ""))
;; Process all the complete markers in this chunk.
(while (string-match gud-gdb-marker-regexp gud-marker-acc)
(setq
;; Extract the frame position from the marker.
gud-last-frame (cons (match-string 1 gud-marker-acc)
(string-to-number (match-string 2 gud-marker-acc)))
;; Append any text before the marker to the output we're going
;; to return - we don't include the marker in this text.
output (concat output
(substring gud-marker-acc 0 (match-beginning 0)))
;; Set the accumulator to the remaining text.
gud-marker-acc (substring gud-marker-acc (match-end 0))))
;; Check for annotations and change gud-minor-mode to 'gdba if
;; they are found.
(while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
(let ((match (match-string 1 gud-marker-acc)))
(setq
;; Append any text before the marker to the output we're going
;; to return - we don't include the marker in this text.
output (concat output
(substring gud-marker-acc 0 (match-beginning 0)))
;; Set the accumulator to the remaining text.
gud-marker-acc (substring gud-marker-acc (match-end 0)))))
;; Does the remaining text look like it might end with the
;; beginning of another marker? If it does, then keep it in
;; gud-marker-acc until we receive the rest of it. Since we
;; know the full marker regexp above failed, it's pretty simple to
;; test for marker starts.
(if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
(progn
;; Everything before the potential marker start can be output.
(setq output (concat output (substring gud-marker-acc
0 (match-beginning 0))))
;; Everything after, we save, to combine with later input.
(setq gud-marker-acc
(substring gud-marker-acc (match-beginning 0))))
(setq output (concat output gud-marker-acc)
gud-marker-acc ""))
output))
(easy-mmode-defmap gud-minibuffer-local-map
'(("\C-i" . comint-dynamic-complete-filename))
"Keymap for minibuffer prompting of gud startup command."
:inherit minibuffer-local-map)
(defun gud-query-cmdline (minor-mode &optional init)
(let* ((hist-sym (gud-symbol 'history nil minor-mode))
(cmd-name (gud-val 'command-name minor-mode)))
(unless (boundp hist-sym) (set hist-sym nil))
(read-from-minibuffer
(format "Run %s (like this): " minor-mode)
(or (car-safe (symbol-value hist-sym))
(concat (or cmd-name (symbol-name minor-mode))
" "
(or init
(let ((file nil))
(dolist (f (directory-files default-directory) file)
(if (and (file-executable-p f)
(not (file-directory-p f))
(or (not file)
(file-newer-than-file-p f file)))
(setq file f)))))))
gud-minibuffer-local-map nil
hist-sym)))
(defvar gdb-first-prompt t)
(defvar gud-filter-pending-text nil
"Non-nil means this is text that has been saved for later in `gud-filter'.")
;; The old gdb command (text command mode). The new one is in gdb-ui.el.
;;;###autoload
(defun gud-gdb (command-line)
"Run gdb on program FILE in buffer *gud-FILE*.
The directory containing FILE becomes the initial working
directory and source-file directory for your debugger."
(interactive (list (gud-query-cmdline 'gud-gdb)))
(when (and gud-comint-buffer
(buffer-name gud-comint-buffer)
(get-buffer-process gud-comint-buffer)
(with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
(gdb-restore-windows)
(error
"Multiple debugging requires restarting in text command mode"))
(gud-common-init command-line nil 'gud-gdb-marker-filter)
(set (make-local-variable 'gud-minor-mode) 'gdb)
(gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
(gud-def gud-tbreak "tbreak %f:%l" "\C-t"
"Set temporary breakpoint at current line.")
(gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
(gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
(gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
(gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
(gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
(gud-def gud-cont "cont" "\C-r" "Continue with display.")
(gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
(gud-def gud-jump
(progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
"\C-j" "Set execution address to current line.")
(gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
(gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
(gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
(gud-def gud-pstar "print* %e" nil
"Evaluate C dereferenced pointer expression at point.")
;; For debugging Emacs only.
(gud-def gud-pv "pv1 %e" "\C-v" "Print the value of the lisp variable.")
(gud-def gud-until "until %l" "\C-u" "Continue to current line.")
(gud-def gud-run "run" nil "Run the program.")
(local-set-key "\C-i" 'gud-gdb-complete-command)
(setq comint-prompt-regexp "^(.*gdb[+]?) *")
(setq paragraph-start comint-prompt-regexp)
(setq gdb-first-prompt t)
(setq gud-running nil)
(setq gdb-ready nil)
(setq gud-filter-pending-text nil)
(run-hooks 'gud-gdb-mode-hook))
;; One of the nice features of GDB is its impressive support for
;; context-sensitive command completion. We preserve that feature
;; in the GUD buffer by using a GDB command designed just for Emacs.
;; The completion process filter indicates when it is finished.
(defvar gud-gdb-fetch-lines-in-progress)
;; Since output may arrive in fragments we accumulate partials strings here.
(defvar gud-gdb-fetch-lines-string)
;; We need to know how much of the completion to chop off.
(defvar gud-gdb-fetch-lines-break)
;; The completion list is constructed by the process filter.
(defvar gud-gdb-fetched-lines)
(defun gud-gdb-complete-command (&optional command a b)
"Perform completion on the GDB command preceding point.
This is implemented using the GDB `complete' command which isn't
available with older versions of GDB."
(interactive)
(if command
;; Used by gud-watch in mini-buffer.
(setq command (concat "p " command))
;; Used in GUD buffer.
(let ((end (point)))
(setq command (buffer-substring (comint-line-beginning-position) end))))
(let* ((command-word
;; Find the word break. This match will always succeed.
(and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
(substring command (match-beginning 2))))
(complete-list
(gud-gdb-run-command-fetch-lines (concat "complete " command)
(current-buffer)
;; From string-match above.
(match-beginning 2))))
;; Protect against old versions of GDB.
(and complete-list
(string-match "^Undefined command: \"complete\"" (car complete-list))
(error "This version of GDB doesn't support the `complete' command"))
;; Sort the list like readline.
(setq complete-list (sort complete-list (function string-lessp)))
;; Remove duplicates.
(let ((first complete-list)
(second (cdr complete-list)))
(while second
(if (string-equal (car first) (car second))
(setcdr first (setq second (cdr second)))
(setq first second
second (cdr second)))))
;; Add a trailing single quote if there is a unique completion
;; and it contains an odd number of unquoted single quotes.
(and (= (length complete-list) 1)
(let ((str (car complete-list))
(pos 0)
(count 0))
(while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
(setq count (1+ count)
pos (match-end 0)))
(and (= (mod count 2) 1)
(setq complete-list (list (concat str "'"))))))
;; Let comint handle the rest.
(comint-dynamic-simple-complete command-word complete-list)))
;; The completion process filter is installed temporarily to slurp the
;; output of GDB up to the next prompt and build the completion list.
(defun gud-gdb-fetch-lines-filter (string filter)
"Filter used to read the list of lines output by a command.
STRING is the output to filter.
It is passed through FILTER before we look at it."
(setq string (funcall filter string))
(setq string (concat gud-gdb-fetch-lines-string string))
(while (string-match "\n" string)
(push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
gud-gdb-fetched-lines)
(setq string (substring string (match-end 0))))
(if (string-match comint-prompt-regexp string)
(progn
(setq gud-gdb-fetch-lines-in-progress nil)
string)
(progn
(setq gud-gdb-fetch-lines-string string)
"")))
;; gdb speedbar functions
(defun gud-gdb-goto-stackframe (text token indent)
"Goto the stackframe described by TEXT, TOKEN, and INDENT."
(speedbar-with-attached-buffer
(gud-basic-call (concat "server frame " (nth 1 token)))
(sit-for 1)))
(defvar gud-gdb-fetched-stack-frame nil
"Stack frames we are fetching from GDB.")
;(defun gud-gdb-get-scope-data (text token indent)
; ;; checkdoc-params: (indent)
; "Fetch data associated with a stack frame, and expand/contract it.
;Data to do this is retrieved from TEXT and TOKEN."
; (let ((args nil) (scope nil))
; (gud-gdb-run-command-fetch-lines "info args")
;
; (gud-gdb-run-command-fetch-lines "info local")
;
; ))
(defun gud-gdb-get-stackframe (buffer)
"Extract the current stack frame out of the GUD GDB BUFFER."
(let ((newlst nil)
(fetched-stack-frame-list
(gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
(if (and (car fetched-stack-frame-list)
(string-match "No stack" (car fetched-stack-frame-list)))
;; Go into some other mode???
nil
(dolist (e fetched-stack-frame-list)
(let ((name nil) (num nil))
(if (not (or
(string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
(string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
(if (not (string-match
"at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
nil
(setcar newlst
(list (nth 0 (car newlst))
(nth 1 (car newlst))
(match-string 1 e)
(match-string 2 e))))
(setq num (match-string 1 e)
name (match-string 2 e))
(setq newlst
(cons
(if (string-match
"at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
(list name num (match-string 1 e)
(match-string 2 e))
(list name num))
newlst)))))
(nreverse newlst))))
;(defun gud-gdb-selected-frame-info (buffer)
; "Learn GDB information for the currently selected stack frame in BUFFER."
; )
(defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
"Run COMMAND, and return the list of lines it outputs.
BUFFER is the current buffer which may be the GUD buffer in which to run.
SKIP is the number of chars to skip on each line, it defaults to 0."
(with-current-buffer gud-comint-buffer
(if (and (eq gud-comint-buffer buffer)
(save-excursion
(goto-char (point-max))
(forward-line 0)
(not (looking-at comint-prompt-regexp))))
nil
;; Much of this copied from GDB complete, but I'm grabbing the stack
;; frame instead.
(let ((gud-gdb-fetch-lines-in-progress t)
(gud-gdb-fetched-lines nil)
(gud-gdb-fetch-lines-string nil)
(gud-gdb-fetch-lines-break (or skip 0))
(gud-marker-filter
`(lambda (string)
(gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
;; Issue the command to GDB.
(gud-basic-call command)
;; Slurp the output.
(while gud-gdb-fetch-lines-in-progress
(accept-process-output (get-buffer-process gud-comint-buffer)))
(nreverse gud-gdb-fetched-lines)))))
;; ======================================================================
;; lldb functions
;; History of argument lists passed to lldb.
(defvar gud-lldb-history nil)
;; Keeps track of breakpoint created. In the following case, the id is "1".
;; It is used to implement temporary breakpoint.
;; (lldb) b main.c:39
;; breakpoint set --file 'main.c' --line 39
;; Breakpoint created: 1: file ='main.c', line = 39, locations = 1
(defvar gud-breakpoint-id nil)
(defun lldb-extract-breakpoint-id (string)
;; Search for "Breakpoint created: \\([^:\n]*\\):" pattern.
;(message "gud-marker-acc string is: |%s|" string)
(if (string-match "Breakpoint created: \\([^:\n]*\\):" string)
(progn
(setq gud-breakpoint-id (match-string 1 string))
(message "breakpoint id: %s" gud-breakpoint-id)))
)
(defun gud-lldb-marker-filter (string)
(setq gud-marker-acc
(if gud-marker-acc (concat gud-marker-acc string) string))
(lldb-extract-breakpoint-id gud-marker-acc)
(let (start)
;; Process all complete markers in this chunk
(while (or
;; (lldb) r
;; Process 15408 launched: '/Volumes/data/lldb/svn/trunk/test/conditional_break/a.out' (x86_64)
;; (lldb) Process 15408 stopped
;; * thread #1: tid = 0x2e03, 0x0000000100000de8 a.out`c + 7 at main.c:39, stop reason = breakpoint 1.1, queue = com.apple.main-thread
(string-match " at \\([^:\n]*\\):\\([0-9]*\\), stop reason = .*\n"
gud-marker-acc start)
;; (lldb) frame select -r 1
;; frame #1: 0x0000000100000e09 a.out`main + 25 at main.c:44
(string-match "^frame.* at \\([^:\n]*\\):\\([0-9]*\\)\n"
gud-marker-acc start))
;(message "gud-marker-acc matches our pattern....")
(setq gud-last-frame
(cons (match-string 1 gud-marker-acc)
(string-to-number (match-string 2 gud-marker-acc)))
start (match-end 0)))
;; Search for the last incomplete line in this chunk
(while (string-match "\n" gud-marker-acc start)
(setq start (match-end 0)))
;; If we have an incomplete line, store it in gud-marker-acc.
(setq gud-marker-acc (substring gud-marker-acc (or start 0))))
string)
;; Keeps track of whether the Python lldb_oneshot_break function definition has
;; been exec'ed.
(defvar lldb-oneshot-break-defined nil)
;;;###autoload
(defun lldb (command-line)
"Run lldb on program FILE in buffer *gud-FILE*.
The directory containing FILE becomes the initial working directory
and source-file directory for your debugger."
(interactive (list (gud-query-cmdline 'lldb)))
(gud-common-init command-line nil 'gud-lldb-marker-filter)
(set (make-local-variable 'gud-minor-mode) 'lldb)
(setq lldb-oneshot-break-defined nil)