-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlgud.el
1324 lines (1171 loc) · 48.7 KB
/
mlgud.el
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
;;; mlgud.el --- parts of gud.el for matlab-shell -*- lexical-binding: t -*-
;; This contains parts of gud.el prefixed with matlab and modified to support `matlab-shell'. gud
;; does not support multiple debuggers. For matlab-shell, we'd need to be able to debug MATLAB in
;; `matlab-shell', while in another buffer uses `gud-gdb' or `gdb' from gud.el to debug C++ code.
;; Emacs 24 gud.el info:
;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
;; Copyright (C) 1992-1996, 1998, 2000-2015 Free Software Foundation,
;; Inc.
;; Author: Eric S. Raymond <[email protected]>
;; Maintainer: [email protected]
;; Keywords: unix, tools
;; 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; 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:
(require 'comint)
(defvar gdb-active-process)
(defvar gdb-define-alist)
(defvar gdb-macro-info)
(defvar gdb-show-changed-values)
(defvar gdb-source-window)
(defvar gdb-var-list)
(defvar hl-line-mode)
(defvar hl-line-sticky-flag)
;; ======================================================================
;; MLGUD commands must be visible in C buffers visited by MLGUD
(defgroup mlgud nil
"The \"Grand Unified Debugger\" interface.
Supported debuggers include gdb, sdb, dbx, xdb, perldb,
pdb (Python), and jdb."
:group 'processes
:group 'tools)
(defvar mlgud-marker-filter nil)
(put 'mlgud-marker-filter 'permanent-local t)
(defvar mlgud-find-file nil)
(put 'mlgud-find-file 'permanent-local t)
(defun mlgud-marker-filter (&rest args)
"Marker filter ARGS."
(apply mlgud-marker-filter args))
(defvar mlgud-minor-mode nil)
(put 'mlgud-minor-mode 'permanent-local t)
(defvar mlgud-comint-buffer nil)
(defvar mlgud-keep-buffer nil)
(defun mlgud-symbol (sym &optional soft minor-mode)
"Return the symbol used for SYM in MINOR-MODE.
MINOR-MODE defaults to `mlgud-minor-mode'.
The symbol returned is `mlgud-<MINOR-MODE>-<SYM>'.
If SOFT is non-nil, returns nil if the symbol doesn't already exist."
(unless (or minor-mode mlgud-minor-mode) (error "Internal error"))
(funcall (if soft 'intern-soft 'intern)
(format "mlgud-%s-%s" (or minor-mode mlgud-minor-mode) sym)))
(defun mlgud-val (sym &optional minor-mode)
"Return the value of `mlgud-symbol' SYM. Default to nil.
MINOR-MODE."
(let ((sym (mlgud-symbol sym t minor-mode)))
(if (boundp sym) (symbol-value sym))))
(defvar mlgud-running nil
"Non-nil if debugged program is running.
Used to gray out relevant toolbar icons.")
(defvar mlgud-target-name "--unknown--"
"The apparent name of the program being debugged in a mlgud buffer.")
;; Use existing Info buffer, if possible.
(defun mlgud-goto-info ()
"Go to relevant Emacs info node."
(interactive)
(if (eq mlgud-minor-mode 'gdbmi)
(info-other-window "(emacs)GDB Graphical Interface")
(info-other-window "(emacs)Debuggers")))
(defun mlgud-tool-bar-item-visible-no-fringe ()
"Tool bar fringe."
(not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
(eq (buffer-local-value 'major-mode (window-buffer)) 'gdb-memory-mode)
(and (eq mlgud-minor-mode 'gdbmi)
(> (car (window-fringes)) 0)))))
(declare-function gdb-gud-context-command "gdb-mi.el")
(defun mlgud-stop-subjob ()
"Stop."
(interactive)
(with-current-buffer mlgud-comint-buffer
(cond ((string-equal mlgud-target-name "emacs")
(comint-stop-subjob))
((eq mlgud-minor-mode 'jdb)
(mlgud-call "suspend"))
((eq mlgud-minor-mode 'gdbmi)
(mlgud-call (gdb-gud-context-command "-exec-interrupt")))
(t
(comint-interrupt-subjob)))))
(defvar mlgud-tool-bar-map
(let ((map (make-sparse-keymap)))
(dolist (x '((mlgud-break . "gud/break")
(mlgud-remove . "gud/remove")
(mlgud-print . "gud/print")
(mlgud-pstar . "gud/pstar")
(mlgud-pp . "gud/pp")
(mlgud-watch . "gud/watch")
(mlgud-run . "gud/run")
(mlgud-go . "gud/go")
(mlgud-stop-subjob . "gud/stop")
(mlgud-cont . "gud/cont")
(mlgud-until . "gud/until")
(mlgud-next . "gud/next")
(mlgud-step . "gud/step")
(mlgud-finish . "gud/finish")
(mlgud-nexti . "gud/nexti")
(mlgud-stepi . "gud/stepi")
(mlgud-up . "gud/up")
(mlgud-down . "gud/down")
(mlgud-goto-info . "info"))
map)
(tool-bar-local-item-from-menu
(car x) (cdr x) map))))
(defun mlgud-file-name (f)
"Transform a relative file name to an absolute file name, F.
Uses `mlgud-<MINOR-MODE>-directories' to find the source files."
;; When `default-directory' is a remote file name, prepend its
;; remote part to f, which is the local file name. Fortunately,
;; `file-remote-p' returns exactly this remote file name part (or
;; nil otherwise).
(setq f (concat (or (file-remote-p default-directory) "") f))
(if (file-exists-p f) (expand-file-name f)
(let ((directories (mlgud-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)))
(declare-function gdb-create-define-alist "gdb-mi" ())
(defvar mlgud-tooltip-mode) ;; defined below
(defun mlgud-find-file (file)
"Find FILE for mlgud."
;; 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 mlgud-minor-mode)
(buf (funcall (or mlgud-find-file 'mlgud-file-name) file)))
(when (stringp buf)
(setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
(when buf
;; Copy `mlgud-minor-mode' to the found buffer to turn on the menu.
(with-current-buffer buf
(setq-local mlgud-minor-mode minor-mode)
(if (boundp 'tool-bar-map) ; not --without-x
(setq-local tool-bar-map mlgud-tool-bar-map))
(when (and mlgud-tooltip-mode
(eq mlgud-minor-mode 'gdbmi))
(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 'mlgud-keep-buffer))
buf)))
;; ======================================================================
;; command definition
;; This macro is used below to define some basic debugger interface commands.
;; Of course you may use `mlgud-def' with any other debugger command, including
;; user defined ones.
;; A macro call like (mlgud-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 MLGUD
;; major mode. The function is also bound in the global keymap with the
;; MLGUD prefix.
(defmacro mlgud-def (func cmd &optional doc)
"Define FUNC to be a command sending CMD.
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.
%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 MLGUD 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 MLGUD buffer)."
`(progn
(defalias ',func (lambda (arg)
,@(if doc (list doc))
(interactive "p")
(if (not mlgud-running)
,(if (stringp cmd)
`(mlgud-call ,cmd arg)
cmd))))
))
;; Where mlgud-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 mlgud-last-frame nil)
;; Used by mlgud-refresh, which should cause mlgud-display-frame to redisplay
;; the last frame, even if it's been called before and mlgud-last-frame has
;; been set to nil.
(defvar mlgud-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
;; mlgud-<name>-massage-args
;; mlgud-<name>-marker-filter
;; mlgud-<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 mlgud-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 mlgud-tag-frame. This may be a file name, a tag name, or
;; something else.
;; ======================================================================
;; speedbar support functions and variables.
(eval-when-compile (require 'dframe)) ; for dframe-with-attached-buffer
(defvar mlgud-last-speedbar-stackframe nil
"Description of the currently displayed MLGUD stack.
The value t means that there is no stack, and we are in display-file mode.")
(defvar mlgud-speedbar-key-map nil
"Keymap used when in the buffers display mode.")
;; At runtime, will be pulled in as a require of speedbar.
(declare-function dframe-message "dframe" (fmt &rest args))
(defun mlgud-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 7 var)
(dframe-message "%s: %s" (nth 7 var) (nth 3 var))
(dframe-message "%s" (nth 3 var)))))
(declare-function speedbar-make-specialized-keymap "speedbar" ())
(declare-function speedbar-add-expansion-list "speedbar" (new-list))
(defvar speedbar-mode-functions-list)
(defun mlgud-install-speedbar-variables ()
"Install those variables used by speedbar to enhance mlgud/gdb."
(unless mlgud-speedbar-key-map
(setq mlgud-speedbar-key-map (speedbar-make-specialized-keymap))
(define-key mlgud-speedbar-key-map "j" 'speedbar-edit-line)
(define-key mlgud-speedbar-key-map "e" 'speedbar-edit-line)
(define-key mlgud-speedbar-key-map "\C-m" 'speedbar-edit-line)
(define-key mlgud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
(define-key mlgud-speedbar-key-map "D" 'gdb-var-delete)
(define-key mlgud-speedbar-key-map "p" 'mlgud-pp))
(speedbar-add-expansion-list '("mlMLGUD" mlgud-speedbar-menu-items
mlgud-speedbar-key-map
mlgud-expansion-speedbar-buttons))
(add-to-list
'speedbar-mode-functions-list
'("mlMLGUD" (speedbar-item-info . mlgud-speedbar-item-info)
(speedbar-line-directory . ignore))))
(defvar mlgud-speedbar-menu-items
'(["Jump to stack frame" speedbar-edit-line
:visible (not (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi))]
["Edit value" speedbar-edit-line
:visible (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi)]
["Delete expression" gdb-var-delete
:visible (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi)]
["Auto raise frame" gdb-speedbar-auto-raise
:style toggle :selected gdb-speedbar-auto-raise
:visible (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi)]
("Output Format"
:visible (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi)
["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)
(mlgud-install-speedbar-variables)
(with-eval-after-load "speedbar" (mlgud-install-speedbar-variables)))
(defun mlgud-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."
(mlgud-speedbar-buttons mlgud-comint-buffer))
(declare-function speedbar-make-tag-line "speedbar"
(type char func data tag tfunc tdata tface depth))
(declare-function speedbar-remove-localized-speedbar-support "speedbar"
(buffer))
(declare-function speedbar-insert-button "speedbar"
(text face mouse function &optional token prevline))
(defun mlgud-speedbar-buttons (buffer)
"Create a speedbar display based on the current state of MLGUD.
If the MLGUD 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 mlgud-comint-buffer
;; mlgud-comint-buffer might be killed
(buffer-name mlgud-comint-buffer))
(let* ((minor-mode (with-current-buffer buffer mlgud-minor-mode))
(window (get-buffer-window (current-buffer) 0))
(start (window-start window))
(p (window-point window)))
(cond
((eq minor-mode 'gdbmi)
(erase-buffer)
(insert "Watch Expressions:\n")
(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))
(has-more (nth 6 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 (and (or (not has-more) (string-equal has-more "0"))
(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 (pcase status
(`changed 'font-lock-warning-face)
(`out-of-scope 'shadow)
(_ 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 (pcase status
(`changed 'font-lock-warning-face)
(`out-of-scope 'shadow)
(_ 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 mlgud-last-last-frame mlgud-last-speedbar-stackframe))
(let ((mlgud-frame-list
(cond ;; Add more debuggers here!
(t (speedbar-remove-localized-speedbar-support buffer)
nil))))
(erase-buffer)
(if (not mlgud-frame-list)
(insert "No Stack frames\n")
(insert "Current Stack:\n"))
(dolist (frame mlgud-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 gdb))
'mlgud-gdb-goto-stackframe)
(t (error "Should never be here")))
frame t))))
(setq mlgud-last-speedbar-stackframe mlgud-last-last-frame))))
(set-window-start window start)
(set-window-point window p))))
;; When we send a command to the debugger via mlgud-call, it's annoying
;; to see the command and the new prompt inserted into the debugger's
;; buffer; we have other ways of knowing the command has completed.
;;
;; If the buffer looks like this:
;; --------------------
;; (gdb) set args foo bar
;; (gdb) -!-
;; --------------------
;; (the -!- marks the location of point), and we type `C-x SPC' in a
;; source file to set a breakpoint, we want the buffer to end up like
;; this:
;; --------------------
;; (gdb) set args foo bar
;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
;; (gdb) -!-
;; --------------------
;; Essentially, the old prompt is deleted, and the command's output
;; and the new prompt take its place.
;;
;; Not echoing the command is easy enough; you send it directly using
;; process-send-string, and it never enters the buffer. However,
;; getting rid of the old prompt is trickier; you don't want to do it
;; when you send the command, since that will result in an annoying
;; flicker as the prompt is deleted, redisplay occurs while Emacs
;; waits for a response from the debugger, and the new prompt is
;; inserted. Instead, we'll wait until we actually get some output
;; from the subprocess before we delete the prompt. If the command
;; produced no output other than a new prompt, that prompt will most
;; likely be in the first chunk of output received, so we will delete
;; the prompt and then replace it with an identical one. If the
;; command produces output, the prompt is moving anyway, so the
;; flicker won't be annoying.
;;
;; So - when we want to delete the prompt upon receipt of the next
;; chunk of debugger output, we position mlgud-delete-prompt-marker at
;; the start of the prompt; the process filter will notice this, and
;; delete all text between it and the process output marker. If
;; mlgud-delete-prompt-marker points nowhere, we leave the current
;; prompt alone.
(defvar mlgud-delete-prompt-marker nil)
(put 'mlgud-mode 'mode-class 'special)
(define-derived-mode mlgud-mode comint-mode "Debugger"
"Major mode for interacting with an inferior debugger process.
You start it up with one of the commands \\[gdb], \\[sdb], \\[dbx],
\\[perldb], \\[xdb], or \\[jdb]. Each entry point finishes by executing a
hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
`perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
After startup, the following commands are available in both the MLGUD
interaction buffer and any source buffer MLGUD visits due to a breakpoint stop
or step operation:
\\[mlgud-break] sets a breakpoint at the current file and line. In the
MLGUD buffer, the current file and line are those of the last breakpoint or
step. In a source buffer, they are the buffer's file and current line.
\\[mlgud-remove] removes breakpoints on the current file and line.
\\[mlgud-refresh] displays in the source window the last line referred to
in the mlgud buffer.
\\[mlgud-step], \\[mlgud-next], and \\[mlgud-stepi] do a step-one-line,
step-one-line (not entering function calls), and step-one-instruction
and then update the source window with the current file and position.
\\[mlgud-cont] continues execution.
\\[mlgud-print] tries to find the largest C lvalue or function-call expression
around point, and sends it to the debugger for value display.
The above commands are common to all supported debuggers except xdb which
does not support stepping instructions.
Under gdb, sdb and xdb, \\[mlgud-tbreak] behaves exactly like \\[mlgud-break],
except that the breakpoint is temporary; that is, it is removed when
execution stops on it.
Under gdb, dbx, and xdb, \\[mlgud-up] pops up through an enclosing stack
frame. \\[mlgud-down] drops back down through one.
If you are using gdb or xdb, \\[mlgud-finish] runs execution to the return from
the current function and stops.
All the keystrokes above are accessible in the MLGUD buffer
with the prefix Control-c, and in all buffers through the
prefix Control-x Control-a.
All pre-defined functions for which the concept make sense repeat
themselves the appropriate number of times if you give a prefix
argument.
You may use the `mlgud-def' macro in the initialization hook to define other
commands.
Other commands for interacting with the debugger process are inherited from
comint mode, which see."
(setq mode-line-process '(":%s"))
(define-key (current-local-map) "\C-c\C-l" 'mlgud-refresh)
(set (make-local-variable 'mlgud-last-frame) nil)
(if (boundp 'tool-bar-map) ; not --without-x
(setq-local tool-bar-map mlgud-tool-bar-map))
(make-local-variable 'comint-prompt-regexp)
;; Don't put repeated commands in command history many times.
(set (make-local-variable 'comint-input-ignoredups) t)
(make-local-variable 'paragraph-start)
(set (make-local-variable 'mlgud-delete-prompt-marker) (make-marker))
(add-hook 'kill-buffer-hook 'mlgud-kill-buffer-hook nil t))
(defun mlgud-set-buffer ()
"Set buffer for mlgud."
(when (derived-mode-p 'mlgud-mode)
(setq mlgud-comint-buffer (current-buffer))))
(defvar mlgud-filter-defer-flag nil
"Non-nil means don't process anything from the debugger right now.
It is saved for when this flag is not set.")
;; These functions are responsible for inserting output from your debugger
;; into the buffer. The hard work is done by the method that is
;; the value of mlgud-marker-filter.
(defvar mlgud-filter-pending-text nil
"Non-nil means this is text that has been saved for later in `mlgud-filter'.")
(defun mlgud-filter (proc string)
"Filter PROC STRING."
;; Here's where the actual buffer insertion is done
(let (output process-window)
(if (buffer-name (process-buffer proc))
(if mlgud-filter-defer-flag
;; If we can't process any text now,
;; save it for later.
(setq mlgud-filter-pending-text
(concat (or mlgud-filter-pending-text "") string))
;; If we have to ask a question during the processing,
;; defer any additional text that comes from the debugger
;; during that time.
(let ((mlgud-filter-defer-flag t))
;; Process now any text we previously saved up.
(if mlgud-filter-pending-text
(setq string (concat mlgud-filter-pending-text string)
mlgud-filter-pending-text nil))
(with-current-buffer (process-buffer proc)
;; If we have been so requested, delete the debugger prompt.
(save-restriction
(widen)
(if (marker-buffer mlgud-delete-prompt-marker)
(let ((inhibit-read-only t))
(delete-region (process-mark proc)
mlgud-delete-prompt-marker)
(comint-update-fence)
(set-marker mlgud-delete-prompt-marker nil)))
;; Save the process output, checking for source file markers.
(setq output (mlgud-marker-filter string))
;; Check for a filename-and-line number.
;; Don't display the specified file
;; unless (1) point is at or after the position where output appears
;; and (2) this buffer is on the screen.
(setq process-window
(and mlgud-last-frame
(>= (point) (process-mark proc))
(get-buffer-window (current-buffer)))))
;; Let the comint filter do the actual insertion.
;; That lets us inherit various comint features.
(comint-output-filter proc output))
;; Put the arrow on the source line.
;; This must be outside of the save-excursion
;; in case the source file is our current buffer.
(if process-window
(with-selected-window process-window
(mlgud-display-frame))
;; We have to be in the proper buffer, (process-buffer proc),
;; but not in a save-excursion, because that would restore point.
(with-current-buffer (process-buffer proc)
(mlgud-display-frame))))
;; If we deferred text that arrived during this processing,
;; handle it now.
(if mlgud-filter-pending-text
(mlgud-filter proc ""))))))
(defvar mlgud-minor-mode-type nil)
(defvar mlgud-overlay-arrow-position nil)
(add-to-list 'overlay-arrow-variable-list 'mlgud-overlay-arrow-position)
(declare-function gdb-reset "gdb-mi" ())
(declare-function speedbar-change-initial-expansion-list "speedbar" (new))
(defvar speedbar-previously-used-expansion-list-name)
(defun mlgud-sentinel (proc msg)
"Sentinel PROC MSG for mlgud."
(cond ((null (buffer-name (process-buffer proc)))
;; buffer killed
;; Stop displaying an arrow in a source file.
(setq mlgud-overlay-arrow-position nil)
(set-process-buffer proc nil)
(if (and (boundp 'speedbar-initial-expansion-list-name)
(string-equal speedbar-initial-expansion-list-name "mlMLGUD"))
(speedbar-change-initial-expansion-list
speedbar-previously-used-expansion-list-name))
(if (eq mlgud-minor-mode-type 'gdbmi)
(gdb-reset)
(mlgud-reset)))
((memq (process-status proc) '(signal exit))
;; Stop displaying an arrow in a source file.
(setq mlgud-overlay-arrow-position nil)
(if (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'gdbmi)
(gdb-reset)
(mlgud-reset))
(let* ((obuf (current-buffer)))
;; save-excursion isn't the right thing if
;; process-buffer is current-buffer
(unwind-protect
(progn
;; Write something in the MLGUD buffer and hack its mode line,
(set-buffer (process-buffer proc))
;; Fix the mode line.
(setq mode-line-process
(concat ":"
(symbol-name (process-status proc))))
(force-mode-line-update)
(if (eobp)
(insert ?\n mode-name " " msg)
(save-excursion
(goto-char (point-max))
(insert ?\n mode-name " " msg)))
;; If buffer and mode line will show that the process
;; is dead, we can delete it now. Otherwise it
;; will stay around until M-x list-processes.
(delete-process proc))
;; Restore old buffer, but don't restore old point
;; if obuf is the mlgud buffer.
(set-buffer obuf))))))
(defun mlgud-kill-buffer-hook ()
"Kill buffer hook for mlgud."
(setq mlgud-minor-mode-type mlgud-minor-mode)
(condition-case nil
(progn
(kill-process (get-buffer-process (current-buffer)))
(delete-process (get-process "gdb-inferior")))
(error nil)))
(defun mlgud-reset ()
"Reset mlgud."
(dolist (buffer (buffer-list))
(unless (eq buffer mlgud-comint-buffer)
(with-current-buffer buffer
(when mlgud-minor-mode
(setq mlgud-minor-mode nil)
(kill-local-variable 'tool-bar-map))))))
(defun mlgud-display-frame ()
"Find and obey the last filename-and-line marker from the debugger.
Obeying it means displaying in another window the specified file and line."
(interactive)
(when mlgud-last-frame
(mlgud-set-buffer)
(mlgud-display-line (car mlgud-last-frame) (cdr mlgud-last-frame))
(setq mlgud-last-last-frame mlgud-last-frame
mlgud-last-frame nil)))
(declare-function global-hl-line-highlight "hl-line" ())
(declare-function hl-line-highlight "hl-line" ())
(declare-function gdb-display-source-buffer "gdb-mi" (buffer))
;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
;; and that its line LINE is visible.
;; Put the overlay-arrow on the line LINE in that buffer.
;; Most of the trickiness in here comes from wanting to preserve the current
;; region-restriction if that's possible. We use an explicit display-buffer
;; to get around the fact that this is called inside a save-excursion.
(defun mlgud-display-line (true-file line)
"Display line for mlgud TRUE-FILE and LINE."
(let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
(buffer
(with-current-buffer mlgud-comint-buffer
(mlgud-find-file true-file)))
(window (and buffer
(or (get-buffer-window buffer)
(display-buffer buffer))))
(pos))
(when buffer
(with-current-buffer buffer
(unless (or (verify-visited-file-modtime buffer) mlgud-keep-buffer)
(if (yes-or-no-p
(format "File %s changed on disk. Reread from disk? "
(buffer-name)))
(revert-buffer t t)
(setq mlgud-keep-buffer t)))
(save-restriction
(widen)
(goto-char (point-min))
(forward-line (1- line))
(setq pos (point))
(or mlgud-overlay-arrow-position
(setq mlgud-overlay-arrow-position (make-marker)))
(set-marker mlgud-overlay-arrow-position (point) (current-buffer))
;; If they turned on hl-line, move the hl-line highlight to
;; the arrow's line.
(when (featurep 'hl-line)
(cond
(global-hl-line-mode
(global-hl-line-highlight))
((and hl-line-mode hl-line-sticky-flag)
(hl-line-highlight)))))
(cond ((or (< pos (point-min)) (> pos (point-max)))
(widen)
(goto-char pos))))
(when window
(set-window-point window mlgud-overlay-arrow-position)
(if (eq mlgud-minor-mode 'gdbmi)
(setq gdb-source-window window))))))
;; The mlgud-call function must do the right thing whether its invoking
;; keystroke is from the MLGUD buffer itself (via major-mode binding)
;; or a C buffer. In the former case, we want to supply data from
;; mlgud-last-frame. Here's how we do it:
(defun mlgud-format-command (str arg)
"Format command STR ARG."
(let ((insource (not (eq (current-buffer) mlgud-comint-buffer)))
(frame (or mlgud-last-frame mlgud-last-last-frame))
result)
(while (and str
(let ((case-fold-search nil))
(string-match "\\([^%]*\\)%\\([adefFlpc]\\)" str)))
(let ((key (string-to-char (match-string 2 str)))
subst)
(cond
((eq key ?f)
(setq subst (file-name-nondirectory (if insource
(buffer-file-name)
(car frame)))))
((eq key ?F)
(setq subst (file-name-base (if insource
(buffer-file-name)
(car frame)))))
((eq key ?d)
(setq subst (file-name-directory (if insource
(buffer-file-name)
(car frame)))))
((eq key ?l)
(setq subst (int-to-string
(if insource
(save-restriction
(widen)
(+ (count-lines (point-min) (point))
(if (bolp) 1 0)))
(cdr frame)))))
((eq key ?e)
(setq subst (mlgud-find-expr)))
((eq key ?a)
(setq subst (mlgud-read-address)))
((eq key ?p)
(setq subst (if arg (int-to-string arg)))))
(setq result (concat result (match-string 1 str) subst)))
(setq str (substring str (match-end 2))))
;; There might be text left in STR when the loop ends.
(concat result str)))
(defun mlgud-read-address ()
"Return a string containing the core-address found in the buffer at point."
(save-match-data
(save-excursion
(let ((pt (point)) found begin)
(setq found (if (search-backward "0x" (- pt 7) t) (point)))
(cond
(found (forward-char 2)
(buffer-substring found
(progn (re-search-forward "[^0-9a-f]")
(forward-char -1)
(point))))
(t (setq begin (progn (re-search-backward "[^0-9]")
(forward-char 1)
(point)))
(forward-char 1)
(re-search-forward "[^0-9]")
(forward-char -1)
(buffer-substring begin (point))))))))
(defun mlgud-call (fmt &optional arg)
"Call FMT ARG."
(let ((msg (mlgud-format-command fmt arg)))
(message "Command: %s" msg)
(sit-for 0)
(mlgud-basic-call msg)))
(defun mlgud-basic-call (command)
"Invoke the debugger COMMAND displaying source in other window."
(interactive)
(mlgud-set-buffer)
(let ((proc (get-buffer-process mlgud-comint-buffer)))
(or proc (error "Current buffer has no process"))
;; Arrange for the current prompt to get deleted.
(with-current-buffer mlgud-comint-buffer
(save-excursion
(save-restriction
(widen)
(if (marker-position mlgud-delete-prompt-marker)
;; We get here when printing an expression.
(goto-char mlgud-delete-prompt-marker)
(goto-char (process-mark proc))
(forward-line 0))
(if (looking-at comint-prompt-regexp)
(set-marker mlgud-delete-prompt-marker (point)))
(if (eq mlgud-minor-mode 'gdbmi)
(apply comint-input-sender (list proc command))
(process-send-string proc (concat command "\n"))))))))
(defun mlgud-refresh (&optional arg)
"Fix up a possibly garbled display, and redraw the arrow, ARG optional."
(interactive "P")
(or mlgud-last-frame (setq mlgud-last-frame mlgud-last-last-frame))
(mlgud-display-frame)
(recenter arg))
;; Code for parsing expressions out of C or Fortran code. The single entry
;; point is mlgud-find-expr, which tries to return an lvalue expression from
;; around point.
(defvar mlgud-find-expr-function 'mlgud-find-c-expr)
(defun mlgud-find-expr (&rest args)
"Find expression using ARGS."
(let ((expr (if (and transient-mark-mode mark-active)
(buffer-substring (region-beginning) (region-end))
(apply mlgud-find-expr-function args))))
(save-match-data
(if (string-match "\n" expr)
(error "Expression must not include a newline"))
(with-current-buffer mlgud-comint-buffer
(save-excursion
(goto-char (process-mark (get-buffer-process mlgud-comint-buffer)))
(forward-line 0)
(when (looking-at comint-prompt-regexp)
(set-marker mlgud-delete-prompt-marker (point))
(set-marker-insertion-type mlgud-delete-prompt-marker t))
(unless (eq (buffer-local-value 'mlgud-minor-mode mlgud-comint-buffer)
'jdb)
(insert (concat expr " = "))))))
expr))
;; The next eight functions are hacked from gdbsrc.el by
;; Debby Ayers <[email protected]>,
;; Rich Schaefer <[email protected]> Schlumberger, Austin, Tx.
(defun mlgud-find-c-expr ()
"Return the expr that surrounds point."
(interactive)
(save-excursion
(let ((p (point))
(expr (mlgud-innermost-expr))
(test-expr (mlgud-prev-expr)))
(while (and test-expr (mlgud-expr-compound test-expr expr))
(let ((prev-expr expr))
(setq expr (cons (car test-expr) (cdr expr)))
(goto-char (car expr))
(setq test-expr (mlgud-prev-expr))
;; If we just pasted on the condition of an if or while,
;; throw it away again.
(if (member (buffer-substring (car test-expr) (cdr test-expr))
'("if" "while" "for"))
(setq test-expr nil
expr prev-expr))))
(goto-char p)
(setq test-expr (mlgud-next-expr))
(while (mlgud-expr-compound expr test-expr)
(setq expr (cons (car expr) (cdr test-expr)))
(setq test-expr (mlgud-next-expr)))
(buffer-substring (car expr) (cdr expr)))))
(defun mlgud-innermost-expr ()
"Return the smallest expr that point is in; move point to beginning of it.
The expr is represented as a cons cell, where the car specifies the point in
the current buffer that marks the beginning of the expr and the cdr specifies
the character after the end of the expr."
(let ((p (point)) begin end)
(mlgud-backward-sexp)
(setq begin (point))
(mlgud-forward-sexp)
(setq end (point))
(if (>= p end)
(progn
(setq begin p)
(goto-char p)
(mlgud-forward-sexp)
(setq end (point)))
)
(goto-char begin)
(cons begin end)))
(defun mlgud-backward-sexp ()
"Version of `backward-sexp' to catch errors."
(condition-case nil
(backward-sexp)
(error t)))
(defun mlgud-forward-sexp ()
"Version of `forward-sexp' to catch errors."
(condition-case nil
(forward-sexp)
(error t)))