]> code.delx.au - gnu-emacs/blob - lisp/progmodes/gud.el
13eac8392a216bd655fd9c033cd939f0185525c7
[gnu-emacs] / lisp / progmodes / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
2
3 ;; Copyright (C) 1992-1996, 1998, 2000-2012 Free Software Foundation, Inc.
4
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: FSF
7 ;; Keywords: unix, tools
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>.
27 ;; It was later rewritten by rms. Some ideas were due to Masanobu. Grand
28 ;; Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com> Barry
29 ;; Warsaw <bwarsaw@cen.com> hacked the mode to use comint.el. Shane Hartman
30 ;; <shane@spr.com> added support for xdb (HPUX debugger). Rick Sladkey
31 ;; <jrs@world.std.com> wrote the GDB command completion code. Dave Love
32 ;; <d.love@dl.ac.uk> added the IRIX kluge, re-implemented the Mips-ish variant
33 ;; and added a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX
34 ;; kluge with the gud-xdb-directories hack producing gud-dbx-directories.
35 ;; Derek L. Davies <ddavies@world.std.com> added support for jdb (Java
36 ;; debugger.)
37
38 ;;; Code:
39
40 (require 'comint)
41
42 (defvar gdb-active-process)
43 (defvar gdb-define-alist)
44 (defvar gdb-macro-info)
45 (defvar gdb-show-changed-values)
46 (defvar gdb-source-window)
47 (defvar gdb-var-list)
48 (defvar gdb-speedbar-auto-raise)
49 (defvar gud-tooltip-mode)
50 (defvar hl-line-mode)
51 (defvar hl-line-sticky-flag)
52 (defvar tool-bar-map)
53
54
55 ;; ======================================================================
56 ;; GUD commands must be visible in C buffers visited by GUD
57
58 (defgroup gud nil
59 "The \"Grand Unified Debugger\" interface.
60 Supported debuggers include gdb, sdb, dbx, xdb, perldb,
61 pdb (Python), and jdb."
62 :group 'processes
63 :group 'tools)
64
65
66 (defcustom gud-key-prefix "\C-x\C-a"
67 "Prefix of all GUD commands valid in C buffers."
68 :type 'key-sequence
69 :group 'gud)
70
71 (global-set-key (vconcat gud-key-prefix "\C-l") 'gud-refresh)
72 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
73
74 (defvar gud-marker-filter nil)
75 (put 'gud-marker-filter 'permanent-local t)
76 (defvar gud-find-file nil)
77 (put 'gud-find-file 'permanent-local t)
78
79 (defun gud-marker-filter (&rest args)
80 (apply gud-marker-filter args))
81
82 (defvar gud-minor-mode nil)
83 (put 'gud-minor-mode 'permanent-local t)
84
85 (defvar gud-comint-buffer nil)
86
87 (defvar gud-keep-buffer nil)
88
89 (defun gud-symbol (sym &optional soft minor-mode)
90 "Return the symbol used for SYM in MINOR-MODE.
91 MINOR-MODE defaults to `gud-minor-mode'.
92 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
93 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
94 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
95 (funcall (if soft 'intern-soft 'intern)
96 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
97
98 (defun gud-val (sym &optional minor-mode)
99 "Return the value of `gud-symbol' SYM. Default to nil."
100 (let ((sym (gud-symbol sym t minor-mode)))
101 (if (boundp sym) (symbol-value sym))))
102
103 (defvar gud-running nil
104 "Non-nil if debugged program is running.
105 Used to gray out relevant toolbar icons.")
106
107 (defvar gud-target-name "--unknown--"
108 "The apparent name of the program being debugged in a gud buffer.")
109
110 ;; Use existing Info buffer, if possible.
111 (defun gud-goto-info ()
112 "Go to relevant Emacs info node."
113 (interactive)
114 (if (eq gud-minor-mode 'gdbmi)
115 (info-other-window "(emacs)GDB Graphical Interface")
116 (info-other-window "(emacs)Debuggers")))
117
118 (defun gud-tool-bar-item-visible-no-fringe ()
119 (not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
120 (eq (buffer-local-value 'major-mode (window-buffer)) 'gdb-memory-mode)
121 (and (eq gud-minor-mode 'gdbmi)
122 (> (car (window-fringes)) 0)))))
123
124 (declare-function gdb-gud-context-command "gdb-mi.el")
125
126 (defun gud-stop-subjob ()
127 (interactive)
128 (with-current-buffer gud-comint-buffer
129 (cond ((string-equal gud-target-name "emacs")
130 (comint-stop-subjob))
131 ((eq gud-minor-mode 'jdb)
132 (gud-call "suspend"))
133 ((eq gud-minor-mode 'gdbmi)
134 (gud-call (gdb-gud-context-command "-exec-interrupt")))
135 (t
136 (comint-interrupt-subjob)))))
137
138 (easy-mmode-defmap gud-menu-map
139 '(([help] "Info (debugger)" . gud-goto-info)
140 ([tooltips] menu-item "Show GUD tooltips" gud-tooltip-mode
141 :enable (and (not emacs-basic-display)
142 (display-graphic-p)
143 (fboundp 'x-show-tip))
144 :visible (memq gud-minor-mode
145 '(gdbmi dbx sdb xdb pdb))
146 :button (:toggle . gud-tooltip-mode))
147 ([refresh] "Refresh" . gud-refresh)
148 ([run] menu-item "Run" gud-run
149 :enable (not gud-running)
150 :visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
151 ([go] menu-item (if (bound-and-true-p gdb-active-process)
152 "Continue" "Run") gud-go
153 :visible (and (eq gud-minor-mode 'gdbmi)
154 (gdb-show-run-p)))
155 ([stop] menu-item "Stop" gud-stop-subjob
156 :visible (or (not (memq gud-minor-mode '(gdbmi pdb)))
157 (gdb-show-stop-p)))
158 ([until] menu-item "Continue to selection" gud-until
159 :enable (not gud-running)
160 :visible (and (memq gud-minor-mode '(gdbmi gdb perldb))
161 (gud-tool-bar-item-visible-no-fringe)))
162 ([remove] menu-item "Remove Breakpoint" gud-remove
163 :enable (not gud-running)
164 :visible (gud-tool-bar-item-visible-no-fringe))
165 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
166 :enable (not gud-running)
167 :visible (memq gud-minor-mode
168 '(gdbmi gdb sdb xdb)))
169 ([break] menu-item "Set Breakpoint" gud-break
170 :enable (not gud-running)
171 :visible (gud-tool-bar-item-visible-no-fringe))
172 ([up] menu-item "Up Stack" gud-up
173 :enable (not gud-running)
174 :visible (memq gud-minor-mode
175 '(gdbmi gdb dbx xdb jdb pdb)))
176 ([down] menu-item "Down Stack" gud-down
177 :enable (not gud-running)
178 :visible (memq gud-minor-mode
179 '(gdbmi gdb dbx xdb jdb pdb)))
180 ([pp] menu-item "Print S-expression" gud-pp
181 :enable (and (not gud-running)
182 (bound-and-true-p gdb-active-process))
183 :visible (and (string-equal
184 (buffer-local-value
185 'gud-target-name gud-comint-buffer) "emacs")
186 (eq gud-minor-mode 'gdbmi)))
187 ([print*] menu-item (if (eq gud-minor-mode 'jdb)
188 "Dump object"
189 "Print Dereference") gud-pstar
190 :enable (not gud-running)
191 :visible (memq gud-minor-mode '(gdbmi gdb jdb)))
192 ([print] menu-item "Print Expression" gud-print
193 :enable (not gud-running))
194 ([watch] menu-item "Watch Expression" gud-watch
195 :enable (not gud-running)
196 :visible (eq gud-minor-mode 'gdbmi))
197 ([finish] menu-item "Finish Function" gud-finish
198 :enable (not gud-running)
199 :visible (memq gud-minor-mode
200 '(gdbmi gdb xdb jdb pdb)))
201 ([stepi] menu-item "Step Instruction" gud-stepi
202 :enable (not gud-running)
203 :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
204 ([nexti] menu-item "Next Instruction" gud-nexti
205 :enable (not gud-running)
206 :visible (memq gud-minor-mode '(gdbmi gdb dbx)))
207 ([step] menu-item "Step Line" gud-step
208 :enable (not gud-running))
209 ([next] menu-item "Next Line" gud-next
210 :enable (not gud-running))
211 ([cont] menu-item "Continue" gud-cont
212 :enable (not gud-running)
213 :visible (not (eq gud-minor-mode 'gdbmi))))
214 "Menu for `gud-mode'."
215 :name "Gud")
216
217 (easy-mmode-defmap gud-minor-mode-map
218 (append
219 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
220 ;; Get tool bar like functionality from the menu bar on a text only
221 ;; terminal.
222 (unless window-system
223 `(([menu-bar down]
224 . (,(propertize "down" 'face 'font-lock-doc-face) . gud-down))
225 ([menu-bar up]
226 . (,(propertize "up" 'face 'font-lock-doc-face) . gud-up))
227 ([menu-bar finish]
228 . (,(propertize "finish" 'face 'font-lock-doc-face) . gud-finish))
229 ([menu-bar step]
230 . (,(propertize "step" 'face 'font-lock-doc-face) . gud-step))
231 ([menu-bar next]
232 . (,(propertize "next" 'face 'font-lock-doc-face) . gud-next))
233 ([menu-bar until] menu-item
234 ,(propertize "until" 'face 'font-lock-doc-face) gud-until
235 :visible (memq gud-minor-mode '(gdbmi gdb perldb)))
236 ([menu-bar cont] menu-item
237 ,(propertize "cont" 'face 'font-lock-doc-face) gud-cont
238 :visible (not (eq gud-minor-mode 'gdbmi)))
239 ([menu-bar run] menu-item
240 ,(propertize "run" 'face 'font-lock-doc-face) gud-run
241 :visible (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
242 ([menu-bar go] menu-item
243 ,(propertize " go " 'face 'font-lock-doc-face) gud-go
244 :visible (and (eq gud-minor-mode 'gdbmi)
245 (gdb-show-run-p)))
246 ([menu-bar stop] menu-item
247 ,(propertize "stop" 'face 'font-lock-doc-face) gud-stop-subjob
248 :visible (or (and (eq gud-minor-mode 'gdbmi)
249 (gdb-show-stop-p))
250 (not (eq gud-minor-mode 'gdbmi))))
251 ([menu-bar print]
252 . (,(propertize "print" 'face 'font-lock-doc-face) . gud-print))
253 ([menu-bar tools] . undefined)
254 ([menu-bar buffer] . undefined)
255 ([menu-bar options] . undefined)
256 ([menu-bar edit] . undefined)
257 ([menu-bar file] . undefined))))
258 "Map used in visited files.")
259
260 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
261 (if m (setcdr m gud-minor-mode-map)
262 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
263
264 (defvar gud-mode-map
265 ;; Will inherit from comint-mode via define-derived-mode.
266 (make-sparse-keymap)
267 "`gud-mode' keymap.")
268
269 (defvar gud-tool-bar-map
270 (let ((map (make-sparse-keymap)))
271 (dolist (x '((gud-break . "gud/break")
272 (gud-remove . "gud/remove")
273 (gud-print . "gud/print")
274 (gud-pstar . "gud/pstar")
275 (gud-pp . "gud/pp")
276 (gud-watch . "gud/watch")
277 (gud-run . "gud/run")
278 (gud-go . "gud/go")
279 (gud-stop-subjob . "gud/stop")
280 (gud-cont . "gud/cont")
281 (gud-until . "gud/until")
282 (gud-next . "gud/next")
283 (gud-step . "gud/step")
284 (gud-finish . "gud/finish")
285 (gud-nexti . "gud/nexti")
286 (gud-stepi . "gud/stepi")
287 (gud-up . "gud/up")
288 (gud-down . "gud/down")
289 (gud-goto-info . "info"))
290 map)
291 (tool-bar-local-item-from-menu
292 (car x) (cdr x) map gud-minor-mode-map))))
293
294 (defun gud-file-name (f)
295 "Transform a relative file name to an absolute file name.
296 Uses `gud-<MINOR-MODE>-directories' to find the source files."
297 ;; When `default-directory' is a remote file name, prepend its
298 ;; remote part to f, which is the local file name. Fortunately,
299 ;; `file-remote-p' returns exactly this remote file name part (or
300 ;; nil otherwise).
301 (setq f (concat (or (file-remote-p default-directory) "") f))
302 (if (file-exists-p f) (expand-file-name f)
303 (let ((directories (gud-val 'directories))
304 (result nil))
305 (while directories
306 (let ((path (expand-file-name f (car directories))))
307 (if (file-exists-p path)
308 (setq result path
309 directories nil)))
310 (setq directories (cdr directories)))
311 result)))
312
313 (declare-function gdb-create-define-alist "gdb-mi" ())
314
315 (defun gud-find-file (file)
316 ;; Don't get confused by double slashes in the name that comes from GDB.
317 (while (string-match "//+" file)
318 (setq file (replace-match "/" t t file)))
319 (let ((minor-mode gud-minor-mode)
320 (buf (funcall (or gud-find-file 'gud-file-name) file)))
321 (when (stringp buf)
322 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
323 (when buf
324 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
325 (with-current-buffer buf
326 (set (make-local-variable 'gud-minor-mode) minor-mode)
327 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
328 (when (and gud-tooltip-mode
329 (eq gud-minor-mode 'gdbmi))
330 (make-local-variable 'gdb-define-alist)
331 (unless gdb-define-alist (gdb-create-define-alist))
332 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
333 (make-local-variable 'gud-keep-buffer))
334 buf)))
335 \f
336 ;; ======================================================================
337 ;; command definition
338
339 ;; This macro is used below to define some basic debugger interface commands.
340 ;; Of course you may use `gud-def' with any other debugger command, including
341 ;; user defined ones.
342
343 ;; A macro call like (gud-def FUNC CMD KEY DOC) expands to a form
344 ;; which defines FUNC to send the command CMD to the debugger, gives
345 ;; it the docstring DOC, and binds that function to KEY in the GUD
346 ;; major mode. The function is also bound in the global keymap with the
347 ;; GUD prefix.
348
349 (defmacro gud-def (func cmd key &optional doc)
350 "Define FUNC to be a command sending CMD and bound to KEY, with
351 optional doc string DOC. Certain %-escapes in the string arguments
352 are interpreted specially if present. These are:
353
354 %f -- Name (without directory) of current source file.
355 %F -- Name (without directory or extension) of current source file.
356 %d -- Directory of current source file.
357 %l -- Number of current source line.
358 %e -- Text of the C lvalue or function-call expression surrounding point.
359 %a -- Text of the hexadecimal address surrounding point.
360 %p -- Prefix argument to the command (if any) as a number.
361 %c -- Fully qualified class name derived from the expression
362 surrounding point (jdb only).
363
364 The `current' source file is the file of the current buffer (if
365 we're in a C file) or the source file current at the last break or
366 step (if we're in the GUD buffer).
367 The `current' line is that of the current buffer (if we're in a
368 source file) or the source line number at the last break or step (if
369 we're in the GUD buffer)."
370 `(progn
371 (defalias ',func (lambda (arg)
372 ,@(if doc (list doc))
373 (interactive "p")
374 (if (not gud-running)
375 ,(if (stringp cmd)
376 `(gud-call ,cmd arg)
377 cmd))))
378 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
379 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
380
381 ;; Where gud-display-frame should put the debugging arrow; a cons of
382 ;; (filename . line-number). This is set by the marker-filter, which scans
383 ;; the debugger's output for indications of the current program counter.
384 (defvar gud-last-frame nil)
385
386 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
387 ;; the last frame, even if it's been called before and gud-last-frame has
388 ;; been set to nil.
389 (defvar gud-last-last-frame nil)
390
391 ;; All debugger-specific information is collected here.
392 ;; Here's how it works, in case you ever need to add a debugger to the mode.
393 ;;
394 ;; Each entry must define the following at startup:
395 ;;
396 ;;<name>
397 ;; comint-prompt-regexp
398 ;; gud-<name>-massage-args
399 ;; gud-<name>-marker-filter
400 ;; gud-<name>-find-file
401 ;;
402 ;; The job of the massage-args method is to modify the given list of
403 ;; debugger arguments before running the debugger.
404 ;;
405 ;; The job of the marker-filter method is to detect file/line markers in
406 ;; strings and set the global gud-last-frame to indicate what display
407 ;; action (if any) should be triggered by the marker. Note that only
408 ;; whatever the method *returns* is displayed in the buffer; thus, you
409 ;; can filter the debugger's output, interpreting some and passing on
410 ;; the rest.
411 ;;
412 ;; The job of the find-file method is to visit and return the buffer indicated
413 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
414 ;; something else.
415 \f
416 ;; ======================================================================
417 ;; speedbar support functions and variables.
418 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
419
420 (defvar gud-last-speedbar-stackframe nil
421 "Description of the currently displayed GUD stack.
422 The value t means that there is no stack, and we are in display-file mode.")
423
424 (defvar gud-speedbar-key-map nil
425 "Keymap used when in the buffers display mode.")
426
427 (defun gud-speedbar-item-info ()
428 "Display the data type of the watch expression element."
429 (let ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list)))
430 (if (nth 7 var)
431 (speedbar-message "%s: %s" (nth 7 var) (nth 3 var))
432 (speedbar-message "%s" (nth 3 var)))))
433
434 (defun gud-install-speedbar-variables ()
435 "Install those variables used by speedbar to enhance gud/gdb."
436 (if gud-speedbar-key-map
437 nil
438 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
439
440 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
441 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
442 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
443 (define-key gud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
444 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)
445 (define-key gud-speedbar-key-map "p" 'gud-pp))
446
447 (speedbar-add-expansion-list '("GUD" gud-speedbar-menu-items
448 gud-speedbar-key-map
449 gud-expansion-speedbar-buttons))
450
451 (add-to-list
452 'speedbar-mode-functions-list
453 '("GUD" (speedbar-item-info . gud-speedbar-item-info)
454 (speedbar-line-directory . ignore))))
455
456 (defvar gud-speedbar-menu-items
457 '(["Jump to stack frame" speedbar-edit-line
458 :visible (not (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
459 'gdbmi))]
460 ["Edit value" speedbar-edit-line
461 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
462 'gdbmi)]
463 ["Delete expression" gdb-var-delete
464 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
465 'gdbmi)]
466 ["Auto raise frame" gdb-speedbar-auto-raise
467 :style toggle :selected gdb-speedbar-auto-raise
468 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
469 'gdbmi)]
470 ("Output Format"
471 :visible (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
472 'gdbmi)
473 ["Binary" (gdb-var-set-format "binary") t]
474 ["Natural" (gdb-var-set-format "natural") t]
475 ["Hexadecimal" (gdb-var-set-format "hexadecimal") t]))
476 "Additional menu items to add to the speedbar frame.")
477
478 ;; Make sure our special speedbar mode is loaded
479 (if (featurep 'speedbar)
480 (gud-install-speedbar-variables)
481 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
482
483 (defun gud-expansion-speedbar-buttons (_directory _zero)
484 "Wrapper for call to `speedbar-add-expansion-list'.
485 DIRECTORY and ZERO are not used, but are required by the caller."
486 (gud-speedbar-buttons gud-comint-buffer))
487
488 (defun gud-speedbar-buttons (buffer)
489 "Create a speedbar display based on the current state of GUD.
490 If the GUD BUFFER is not running a supported debugger, then turn
491 off the specialized speedbar mode. BUFFER is not used, but is
492 required by the caller."
493 (when (and gud-comint-buffer
494 ;; gud-comint-buffer might be killed
495 (buffer-name gud-comint-buffer))
496 (let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
497 (window (get-buffer-window (current-buffer) 0))
498 (start (window-start window))
499 (p (window-point window)))
500 (cond
501 ((eq minor-mode 'gdbmi)
502 (erase-buffer)
503 (insert "Watch Expressions:\n")
504 (let ((var-list gdb-var-list) parent)
505 (while var-list
506 (let* (char (depth 0) (start 0) (var (car var-list))
507 (varnum (car var)) (expr (nth 1 var))
508 (type (if (nth 3 var) (nth 3 var) " "))
509 (value (nth 4 var)) (status (nth 5 var))
510 (has-more (nth 6 var)))
511 (put-text-property
512 0 (length expr) 'face font-lock-variable-name-face expr)
513 (put-text-property
514 0 (length type) 'face font-lock-type-face type)
515 (while (string-match "\\." varnum start)
516 (setq depth (1+ depth)
517 start (1+ (match-beginning 0))))
518 (if (eq depth 0) (setq parent nil))
519 (if (and (or (not has-more) (string-equal has-more "0"))
520 (or (equal (nth 2 var) "0")
521 (and (equal (nth 2 var) "1")
522 (string-match "char \\*$" type)) ))
523 (speedbar-make-tag-line
524 'bracket ?? nil nil
525 (concat expr "\t" value)
526 (if (or parent (eq status 'out-of-scope))
527 nil 'gdb-edit-value)
528 nil
529 (if gdb-show-changed-values
530 (or parent (pcase status
531 (`changed 'font-lock-warning-face)
532 (`out-of-scope 'shadow)
533 (_ t)))
534 t)
535 depth)
536 (if (eq status 'out-of-scope) (setq parent 'shadow))
537 (if (and (nth 1 var-list)
538 (string-match (concat varnum "\\.")
539 (car (nth 1 var-list))))
540 (setq char ?-)
541 (setq char ?+))
542 (if (string-match "\\*$\\|\\*&$" type)
543 (speedbar-make-tag-line
544 'bracket char
545 'gdb-speedbar-expand-node varnum
546 (concat expr "\t" type "\t" value)
547 (if (or parent (eq status 'out-of-scope))
548 nil 'gdb-edit-value)
549 nil
550 (if gdb-show-changed-values
551 (or parent (pcase status
552 (`changed 'font-lock-warning-face)
553 (`out-of-scope 'shadow)
554 (_ t)))
555 t)
556 depth)
557 (speedbar-make-tag-line
558 'bracket char
559 'gdb-speedbar-expand-node varnum
560 (concat expr "\t" type)
561 nil nil
562 (if (and (or parent status) gdb-show-changed-values)
563 'shadow t)
564 depth))))
565 (setq var-list (cdr var-list)))))
566 (t (unless (and (save-excursion
567 (goto-char (point-min))
568 (looking-at "Current Stack:"))
569 (equal gud-last-last-frame gud-last-speedbar-stackframe))
570 (let ((gud-frame-list
571 (cond ((eq minor-mode 'gdb)
572 (gud-gdb-get-stackframe buffer))
573 ;; Add more debuggers here!
574 (t (speedbar-remove-localized-speedbar-support buffer)
575 nil))))
576 (erase-buffer)
577 (if (not gud-frame-list)
578 (insert "No Stack frames\n")
579 (insert "Current Stack:\n"))
580 (dolist (frame gud-frame-list)
581 (insert (nth 1 frame) ":\n")
582 (if (= (length frame) 2)
583 (progn
584 (speedbar-insert-button (car frame)
585 'speedbar-directory-face
586 nil nil nil t))
587 (speedbar-insert-button
588 (car frame)
589 'speedbar-file-face
590 'speedbar-highlight-face
591 (cond ((memq minor-mode '(gdbmi gdb))
592 'gud-gdb-goto-stackframe)
593 (t (error "Should never be here")))
594 frame t))))
595 (setq gud-last-speedbar-stackframe gud-last-last-frame))))
596 (set-window-start window start)
597 (set-window-point window p))))
598
599 \f
600 ;; ======================================================================
601 ;; gdb functions
602
603 ;; History of argument lists passed to gdb.
604 (defvar gud-gdb-history nil)
605
606 (defcustom gud-gud-gdb-command-name "gdb --fullname"
607 "Default command to run an executable under GDB in text command mode.
608 The option \"--fullname\" must be included in this value."
609 :type 'string
610 :group 'gud)
611
612 (defvar gud-gdb-marker-regexp
613 ;; This used to use path-separator instead of ":";
614 ;; however, we found that on both Windows 32 and MSDOS
615 ;; a colon is correct here.
616 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
617 "\\([0-9]*\\)" ":" ".*\n"))
618
619 ;; There's no guarantee that Emacs will hand the filter the entire
620 ;; marker at once; it could be broken up across several strings. We
621 ;; might even receive a big chunk with several markers in it. If we
622 ;; receive a chunk of text which looks like it might contain the
623 ;; beginning of a marker, we save it here between calls to the
624 ;; filter.
625 (defvar gud-marker-acc "")
626 (make-variable-buffer-local 'gud-marker-acc)
627
628 (defun gud-gdb-marker-filter (string)
629 (setq gud-marker-acc (concat gud-marker-acc string))
630 (let ((output ""))
631
632 ;; Process all the complete markers in this chunk.
633 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
634 (setq
635
636 ;; Extract the frame position from the marker.
637 gud-last-frame (cons (match-string 1 gud-marker-acc)
638 (string-to-number (match-string 2 gud-marker-acc)))
639
640 ;; Append any text before the marker to the output we're going
641 ;; to return - we don't include the marker in this text.
642 output (concat output
643 (substring gud-marker-acc 0 (match-beginning 0)))
644
645 ;; Set the accumulator to the remaining text.
646 gud-marker-acc (substring gud-marker-acc (match-end 0))))
647
648 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
649 (setq
650 ;; Append any text before the marker to the output we're going
651 ;; to return - we don't include the marker in this text.
652 output (concat output
653 (substring gud-marker-acc 0 (match-beginning 0)))
654
655 ;; Set the accumulator to the remaining text.
656
657 gud-marker-acc (substring gud-marker-acc (match-end 0))))
658
659 ;; Does the remaining text look like it might end with the
660 ;; beginning of another marker? If it does, then keep it in
661 ;; gud-marker-acc until we receive the rest of it. Since we
662 ;; know the full marker regexp above failed, it's pretty simple to
663 ;; test for marker starts.
664 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
665 (progn
666 ;; Everything before the potential marker start can be output.
667 (setq output (concat output (substring gud-marker-acc
668 0 (match-beginning 0))))
669
670 ;; Everything after, we save, to combine with later input.
671 (setq gud-marker-acc
672 (substring gud-marker-acc (match-beginning 0))))
673
674 (setq output (concat output gud-marker-acc)
675 gud-marker-acc ""))
676
677 output))
678
679 (easy-mmode-defmap gud-minibuffer-local-map
680 '(("\C-i" . comint-dynamic-complete-filename))
681 "Keymap for minibuffer prompting of gud startup command."
682 :inherit minibuffer-local-map)
683
684 (defun gud-query-cmdline (minor-mode &optional init)
685 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
686 (cmd-name (gud-val 'command-name minor-mode)))
687 (unless (boundp hist-sym) (set hist-sym nil))
688 (read-from-minibuffer
689 (format "Run %s (like this): " minor-mode)
690 (or (car-safe (symbol-value hist-sym))
691 (concat (or cmd-name (symbol-name minor-mode))
692 " "
693 (or init
694 (let ((file nil))
695 (dolist (f (directory-files default-directory) file)
696 (if (and (file-executable-p f)
697 (not (file-directory-p f))
698 (or (not file)
699 (file-newer-than-file-p f file)))
700 (setq file f)))))))
701 gud-minibuffer-local-map nil
702 hist-sym)))
703
704 (defvar gdb-first-prompt t)
705
706 (defvar gud-filter-pending-text nil
707 "Non-nil means this is text that has been saved for later in `gud-filter'.")
708
709 ;; If in gdb mode, gdb-mi is loaded.
710 (declare-function gdb-restore-windows "gdb-mi" ())
711
712 ;; The old gdb command (text command mode). The new one is in gdb-mi.el.
713 ;;;###autoload
714 (defun gud-gdb (command-line)
715 "Run gdb on program FILE in buffer *gud-FILE*.
716 The directory containing FILE becomes the initial working
717 directory and source-file directory for your debugger."
718 (interactive (list (gud-query-cmdline 'gud-gdb)))
719
720 (when (and gud-comint-buffer
721 (buffer-name gud-comint-buffer)
722 (get-buffer-process gud-comint-buffer)
723 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdbmi)))
724 (gdb-restore-windows)
725 (error
726 "Multiple debugging requires restarting in text command mode"))
727
728 (gud-common-init command-line nil 'gud-gdb-marker-filter)
729 (set (make-local-variable 'gud-minor-mode) 'gdb)
730
731 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
732 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
733 "Set temporary breakpoint at current line.")
734 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
735 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
736 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
737 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
738 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
739 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
740 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
741 (gud-def gud-jump
742 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
743 "\C-j" "Set execution address to current line.")
744
745 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
746 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
747 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
748 (gud-def gud-pstar "print* %e" nil
749 "Evaluate C dereferenced pointer expression at point.")
750
751 ;; For debugging Emacs only.
752 (gud-def gud-pv "pv %e" "\C-v" "Print the value of the lisp variable.")
753
754 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
755 (gud-def gud-run "run" nil "Run the program.")
756
757 (add-hook 'completion-at-point-functions #'gud-gdb-completion-at-point
758 nil 'local)
759 (set (make-local-variable 'gud-gdb-completion-function) 'gud-gdb-completions)
760
761 (local-set-key "\C-i" 'completion-at-point)
762 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
763 (setq paragraph-start comint-prompt-regexp)
764 (setq gdb-first-prompt t)
765 (setq gud-running nil)
766 (setq gud-filter-pending-text nil)
767 (run-hooks 'gud-gdb-mode-hook))
768
769 ;; One of the nice features of GDB is its impressive support for
770 ;; context-sensitive command completion. We preserve that feature
771 ;; in the GUD buffer by using a GDB command designed just for Emacs.
772
773 (defvar gud-gdb-completion-function nil
774 "Completion function for GDB commands.
775 It receives two arguments: COMMAND, the prefix for which we seek
776 completion; and CONTEXT, the text before COMMAND on the line.
777 It should return a list of completion strings.")
778
779 ;; The completion process filter indicates when it is finished.
780 (defvar gud-gdb-fetch-lines-in-progress)
781
782 ;; Since output may arrive in fragments we accumulate partials strings here.
783 (defvar gud-gdb-fetch-lines-string)
784
785 ;; We need to know how much of the completion to chop off.
786 (defvar gud-gdb-fetch-lines-break)
787
788 ;; The completion list is constructed by the process filter.
789 (defvar gud-gdb-fetched-lines)
790
791 (defun gud-gdb-completions (context command)
792 "Completion table for GDB commands.
793 COMMAND is the prefix for which we seek completion.
794 CONTEXT is the text before COMMAND on the line."
795 (let* ((start (- (point) (field-beginning)))
796 (complete-list
797 (gud-gdb-run-command-fetch-lines (concat "complete " context command)
798 (current-buffer)
799 ;; From string-match above.
800 (length context))))
801 ;; `gud-gdb-run-command-fetch-lines' has some nasty side-effects on the
802 ;; buffer (via `gud-delete-prompt-marker'): it removes the prompt and then
803 ;; re-adds it later, thus messing up markers and overlays along the way.
804 ;; This is a problem for completion-in-region which uses an overlay to
805 ;; create a field.
806 ;; So we restore completion-in-region's field if needed.
807 ;; FIXME: change gud-gdb-run-command-fetch-lines so it doesn't modify the
808 ;; buffer at all.
809 (when (/= start (- (point) (field-beginning)))
810 (dolist (ol (overlays-at (1- (point))))
811 (when (eq (overlay-get ol 'field) 'completion)
812 (move-overlay ol (- (point) start) (overlay-end ol)))))
813 ;; Protect against old versions of GDB.
814 (and complete-list
815 (string-match "^Undefined command: \"complete\"" (car complete-list))
816 (error "This version of GDB doesn't support the `complete' command"))
817 (gud-gdb-completions-1 complete-list)))
818
819 ;; This function is also used by `gud-gdbmi-completions'.
820 (defun gud-gdb-completions-1 (complete-list)
821 ;; Sort the list like readline.
822 (setq complete-list (sort complete-list (function string-lessp)))
823 ;; Remove duplicates.
824 (let ((first complete-list)
825 (second (cdr complete-list)))
826 (while second
827 (if (string-equal (car first) (car second))
828 (setcdr first (setq second (cdr second)))
829 (setq first second
830 second (cdr second)))))
831 ;; Add a trailing single quote if there is a unique completion
832 ;; and it contains an odd number of unquoted single quotes.
833 (and (= (length complete-list) 1)
834 (let ((str (car complete-list))
835 (pos 0)
836 (count 0))
837 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
838 (setq count (1+ count)
839 pos (match-end 0)))
840 (and (= (mod count 2) 1)
841 (setq complete-list (list (concat str "'"))))))
842 complete-list)
843
844 (defun gud-gdb-completion-at-point ()
845 "Return the data to complete the GDB command before point."
846 (let ((end (point))
847 (start
848 (save-excursion
849 (skip-chars-backward "^ " (comint-line-beginning-position))
850 (point))))
851 (list start end
852 (completion-table-dynamic
853 (apply-partially gud-gdb-completion-function
854 (buffer-substring (comint-line-beginning-position)
855 start))))))
856
857 ;; (defun gud-gdb-complete-command ()
858 ;; "Perform completion on the GDB command preceding point.
859 ;; This is implemented using the GDB `complete' command which isn't
860 ;; available with older versions of GDB."
861 ;; (interactive)
862 ;; (apply #'completion-in-region (gud-gdb-completion-at-point)))
863
864 ;; The completion process filter is installed temporarily to slurp the
865 ;; output of GDB up to the next prompt and build the completion list.
866 (defun gud-gdb-fetch-lines-filter (string)
867 "Filter used to read the list of lines output by a command.
868 STRING is the output to filter.
869 It is passed through `gud-gdb-marker-filter' before we look at it."
870 (setq string (gud-gdb-marker-filter string))
871 (setq string (concat gud-gdb-fetch-lines-string string))
872 (while (string-match "\n" string)
873 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
874 gud-gdb-fetched-lines)
875 (setq string (substring string (match-end 0))))
876 (if (string-match comint-prompt-regexp string)
877 (progn
878 (setq gud-gdb-fetch-lines-in-progress nil)
879 string)
880 (progn
881 (setq gud-gdb-fetch-lines-string string)
882 "")))
883
884 ;; gdb speedbar functions
885
886 (defun gud-gdb-goto-stackframe (_text token _indent)
887 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
888 (speedbar-with-attached-buffer
889 (gud-basic-call (concat "server frame " (nth 1 token)))
890 (sit-for 1)))
891
892 (defvar gud-gdb-fetched-stack-frame nil
893 "Stack frames we are fetching from GDB.")
894
895 (defun gud-gdb-get-stackframe (buffer)
896 "Extract the current stack frame out of the GUD GDB BUFFER."
897 (let ((newlst nil)
898 (fetched-stack-frame-list
899 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
900 (if (and (car fetched-stack-frame-list)
901 (string-match "No stack" (car fetched-stack-frame-list)))
902 ;; Go into some other mode???
903 nil
904 (dolist (e fetched-stack-frame-list)
905 (let ((name nil) (num nil))
906 (if (not (or
907 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
908 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
909 (if (not (string-match
910 "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e))
911 nil
912 (setcar newlst
913 (list (nth 0 (car newlst))
914 (nth 1 (car newlst))
915 (match-string 1 e)
916 (match-string 2 e))))
917 (setq num (match-string 1 e)
918 name (match-string 2 e))
919 (setq newlst
920 (cons
921 (if (string-match
922 "at \\([-0-9a-zA-Z_/.]+\\):\\([0-9]+\\)$" e)
923 (list name num (match-string 1 e)
924 (match-string 2 e))
925 (list name num))
926 newlst)))))
927 (nreverse newlst))))
928
929 ;(defun gud-gdb-selected-frame-info (buffer)
930 ; "Learn GDB information for the currently selected stack frame in BUFFER."
931 ; )
932
933 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
934 "Run COMMAND, and return the list of lines it outputs.
935 BUFFER is the current buffer which may be the GUD buffer in which to run.
936 SKIP is the number of chars to skip on each line, it defaults to 0."
937 (with-current-buffer gud-comint-buffer
938 (unless (and (eq gud-comint-buffer buffer)
939 (save-excursion
940 (goto-char (point-max))
941 (forward-line 0)
942 (not (looking-at comint-prompt-regexp))))
943 (let ((gud-gdb-fetch-lines-in-progress t)
944 (gud-gdb-fetched-lines nil)
945 (gud-gdb-fetch-lines-string nil)
946 (gud-gdb-fetch-lines-break (or skip 0))
947 (gud-marker-filter #'gud-gdb-fetch-lines-filter))
948 ;; Issue the command to GDB.
949 (gud-basic-call command)
950 ;; Slurp the output.
951 (while gud-gdb-fetch-lines-in-progress
952 (accept-process-output (get-buffer-process gud-comint-buffer)))
953 (nreverse gud-gdb-fetched-lines)))))
954
955 \f
956 ;; ======================================================================
957 ;; sdb functions
958
959 ;; History of argument lists passed to sdb.
960 (defvar gud-sdb-history nil)
961
962 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
963 "If nil, we're on a System V Release 4 and don't need the tags hack.")
964
965 (defvar gud-sdb-lastfile nil)
966
967 (defun gud-sdb-marker-filter (string)
968 (setq gud-marker-acc
969 (if gud-marker-acc (concat gud-marker-acc string) string))
970 (let (start)
971 ;; Process all complete markers in this chunk
972 (while
973 (cond
974 ;; System V Release 3.2 uses this format
975 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
976 gud-marker-acc start)
977 (setq gud-last-frame
978 (cons (match-string 3 gud-marker-acc)
979 (string-to-number (match-string 4 gud-marker-acc)))))
980 ;; System V Release 4.0 quite often clumps two lines together
981 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
982 gud-marker-acc start)
983 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
984 (setq gud-last-frame
985 (cons gud-sdb-lastfile
986 (string-to-number (match-string 3 gud-marker-acc)))))
987 ;; System V Release 4.0
988 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
989 gud-marker-acc start)
990 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
991 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
992 gud-marker-acc start))
993 (setq gud-last-frame
994 (cons gud-sdb-lastfile
995 (string-to-number (match-string 1 gud-marker-acc)))))
996 (t
997 (setq gud-sdb-lastfile nil)))
998 (setq start (match-end 0)))
999
1000 ;; Search for the last incomplete line in this chunk
1001 (while (string-match "\n" gud-marker-acc start)
1002 (setq start (match-end 0)))
1003
1004 ;; If we have an incomplete line, store it in gud-marker-acc.
1005 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
1006 string)
1007
1008 (defun gud-sdb-find-file (f)
1009 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
1010
1011 ;;;###autoload
1012 (defun sdb (command-line)
1013 "Run sdb on program FILE in buffer *gud-FILE*.
1014 The directory containing FILE becomes the initial working directory
1015 and source-file directory for your debugger."
1016 (interactive (list (gud-query-cmdline 'sdb)))
1017
1018 (if gud-sdb-needs-tags (require 'etags))
1019 (if (and gud-sdb-needs-tags
1020 (not (and (boundp 'tags-file-name)
1021 (stringp tags-file-name)
1022 (file-exists-p tags-file-name))))
1023 (error "The sdb support requires a valid tags table to work"))
1024
1025 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
1026 (set (make-local-variable 'gud-minor-mode) 'sdb)
1027
1028 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
1029 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
1030 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
1031 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
1032 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
1033 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1034 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1035 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
1036
1037 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
1038 (setq paragraph-start comint-prompt-regexp)
1039 (run-hooks 'sdb-mode-hook)
1040 )
1041 \f
1042 ;; ======================================================================
1043 ;; dbx functions
1044
1045 ;; History of argument lists passed to dbx.
1046 (defvar gud-dbx-history nil)
1047
1048 (defcustom gud-dbx-directories nil
1049 "A list of directories that dbx should search for source code.
1050 If nil, only source files in the program directory
1051 will be known to dbx.
1052
1053 The file names should be absolute, or relative to the directory
1054 containing the executable being debugged."
1055 :type '(choice (const :tag "Current Directory" nil)
1056 (repeat :value ("")
1057 directory))
1058 :group 'gud)
1059
1060 (defun gud-dbx-massage-args (_file args)
1061 (nconc (let ((directories gud-dbx-directories)
1062 (result nil))
1063 (while directories
1064 (setq result (cons (car directories) (cons "-I" result)))
1065 (setq directories (cdr directories)))
1066 (nreverse result))
1067 args))
1068
1069 (defun gud-dbx-marker-filter (string)
1070 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
1071
1072 (let (start)
1073 ;; Process all complete markers in this chunk.
1074 (while (or (string-match
1075 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1076 gud-marker-acc start)
1077 (string-match
1078 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1079 gud-marker-acc start))
1080 (setq gud-last-frame
1081 (cons (match-string 2 gud-marker-acc)
1082 (string-to-number (match-string 1 gud-marker-acc)))
1083 start (match-end 0)))
1084
1085 ;; Search for the last incomplete line in this chunk
1086 (while (string-match "\n" gud-marker-acc start)
1087 (setq start (match-end 0)))
1088
1089 ;; If the incomplete line APPEARS to begin with another marker, keep it
1090 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1091 ;; unnecessary concat during the next call.
1092 (setq gud-marker-acc
1093 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
1094 (substring gud-marker-acc (match-beginning 0))
1095 nil)))
1096 string)
1097
1098 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
1099 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
1100 (defvar gud-mips-p
1101 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
1102 ;; We haven't tested gud on this system:
1103 (string-match "^mips-[^-]*-riscos" system-configuration)
1104 ;; It's documented on OSF/1.3
1105 (string-match "^mips-[^-]*-osf1" system-configuration)
1106 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
1107 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
1108
1109 (defvar gud-dbx-command-name
1110 (concat "dbx" (if gud-mips-p " -emacs")))
1111
1112 ;; This is just like the gdb one except for the regexps since we need to cope
1113 ;; with an optional breakpoint number in [] before the ^Z^Z
1114 (defun gud-mipsdbx-marker-filter (string)
1115 (setq gud-marker-acc (concat gud-marker-acc string))
1116 (let ((output ""))
1117
1118 ;; Process all the complete markers in this chunk.
1119 (while (string-match
1120 ;; This is like th gdb marker but with an optional
1121 ;; leading break point number like `[1] '
1122 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1123 gud-marker-acc)
1124 (setq
1125
1126 ;; Extract the frame position from the marker.
1127 gud-last-frame
1128 (cons (match-string 1 gud-marker-acc)
1129 (string-to-number (match-string 2 gud-marker-acc)))
1130
1131 ;; Append any text before the marker to the output we're going
1132 ;; to return - we don't include the marker in this text.
1133 output (concat output
1134 (substring gud-marker-acc 0 (match-beginning 0)))
1135
1136 ;; Set the accumulator to the remaining text.
1137 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1138
1139 ;; Does the remaining text look like it might end with the
1140 ;; beginning of another marker? If it does, then keep it in
1141 ;; gud-marker-acc until we receive the rest of it. Since we
1142 ;; know the full marker regexp above failed, it's pretty simple to
1143 ;; test for marker starts.
1144 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
1145 (progn
1146 ;; Everything before the potential marker start can be output.
1147 (setq output (concat output (substring gud-marker-acc
1148 0 (match-beginning 0))))
1149
1150 ;; Everything after, we save, to combine with later input.
1151 (setq gud-marker-acc
1152 (substring gud-marker-acc (match-beginning 0))))
1153
1154 (setq output (concat output gud-marker-acc)
1155 gud-marker-acc ""))
1156
1157 output))
1158
1159 ;; The dbx in IRIX is a pain. It doesn't print the file name when
1160 ;; stopping at a breakpoint (but you do get it from the `up' and
1161 ;; `down' commands...). The only way to extract the information seems
1162 ;; to be with a `file' command, although the current line number is
1163 ;; available in $curline. Thus we have to look for output which
1164 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
1165 ;; to output the information we want with a combination of the
1166 ;; `printf' and `file' commands as a pseudo marker which we can
1167 ;; recognize next time through the marker-filter. This would be like
1168 ;; the gdb marker but you can't get the file name without a newline...
1169 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
1170 ;; number rather than a line number etc. Maybe this could be made to
1171 ;; work by listing all the breakpoints and picking the one(s) with the
1172 ;; correct line number, but life's too short.
1173 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
1174
1175 (defvar gud-irix-p
1176 (and (string-match "^mips-[^-]*-irix" system-configuration)
1177 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
1178 "Non-nil to assume the interface appropriate for IRIX dbx.
1179 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
1180 a better solution in 6.1 upwards.")
1181 (defvar gud-dbx-use-stopformat-p
1182 (string-match "irix[6-9]\\.[1-9]" system-configuration)
1183 "Non-nil to use the dbx feature present at least from Irix 6.1
1184 whereby $stopformat=1 produces an output format compatible with
1185 `gud-dbx-marker-filter'.")
1186 ;; [Irix dbx seems to be a moving target. The dbx output changed
1187 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
1188 ;; the output from `up' is no longer spotted by gud (and it's probably
1189 ;; not distinctive enough to try to match it -- use C-<, C->
1190 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
1191 ;; `long long'(why?!), so the printf stuff needed changing. The line
1192 ;; number was cast to `long' as a compromise between the new `long
1193 ;; long' and the original `int'. This is reported not to work in 6.2,
1194 ;; so it's changed back to int -- don't make your sources too long.
1195 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
1196 ;; whereby `set $stopformat=1' reportedly produces output compatible
1197 ;; with `gud-dbx-marker-filter', which we prefer.
1198
1199 ;; The process filter is also somewhat
1200 ;; unreliable, sometimes not spotting the markers; I don't know
1201 ;; whether there's anything that can be done about that. It would be
1202 ;; much better if SGI could be persuaded to (re?)instate the MIPS
1203 ;; -emacs flag for gdb-like output (which ought to be possible as most
1204 ;; of the communication I've had over it has been from sgi.com).]
1205
1206 ;; this filter is influenced by the xdb one rather than the gdb one
1207 (defun gud-irixdbx-marker-filter (string)
1208 (let (result (case-fold-search nil))
1209 (if (or (string-match comint-prompt-regexp string)
1210 (string-match ".*\012" string))
1211 (setq result (concat gud-marker-acc string)
1212 gud-marker-acc "")
1213 (setq gud-marker-acc (concat gud-marker-acc string)))
1214 (if result
1215 (cond
1216 ;; look for breakpoint or signal indication e.g.:
1217 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1218 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1219 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1220 ((string-match
1221 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1222 result)
1223 ;; prod dbx into printing out the line number and file
1224 ;; name in a form we can grok as below
1225 (process-send-string (get-buffer-process gud-comint-buffer)
1226 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1227 ;; look for result of, say, "up" e.g.:
1228 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1229 ;; (this will also catch one of the lines printed by "where")
1230 ((string-match
1231 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1232 result)
1233 (let ((file (match-string 1 result)))
1234 (if (file-exists-p file)
1235 (setq gud-last-frame
1236 (cons (match-string 1 result)
1237 (string-to-number (match-string 2 result))))))
1238 result)
1239 ((string-match ; kluged-up marker as above
1240 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1241 (let ((file (gud-file-name (match-string 2 result))))
1242 (if (and file (file-exists-p file))
1243 (setq gud-last-frame
1244 (cons file
1245 (string-to-number (match-string 1 result))))))
1246 (setq result (substring result 0 (match-beginning 0))))))
1247 (or result "")))
1248
1249 ;; There are a couple of differences between DG's dbx output and normal
1250 ;; dbx output which make it nontrivial to integrate this into the
1251 ;; standard dbx-marker-filter (mainly, there are a different number of
1252 ;; backreferences). The markers look like:
1253 ;;
1254 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1255 ;;
1256 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1257 ;; number), and
1258 ;;
1259 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1260 ;;
1261 ;; from signals and
1262 ;;
1263 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1264 ;;
1265 ;; from up/down/where.
1266
1267 (defun gud-dguxdbx-marker-filter (string)
1268 (setq gud-marker-acc (if gud-marker-acc
1269 (concat gud-marker-acc string)
1270 string))
1271 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1272 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1273 start)
1274 ;; Process all complete markers in this chunk.
1275 (while (string-match re gud-marker-acc start)
1276 (setq gud-last-frame
1277 (cons (match-string 4 gud-marker-acc)
1278 (string-to-number (match-string 3 gud-marker-acc)))
1279 start (match-end 0)))
1280
1281 ;; Search for the last incomplete line in this chunk
1282 (while (string-match "\n" gud-marker-acc start)
1283 (setq start (match-end 0)))
1284
1285 ;; If the incomplete line APPEARS to begin with another marker, keep it
1286 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1287 ;; unnecessary concat during the next call.
1288 (setq gud-marker-acc
1289 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1290 (substring gud-marker-acc (match-beginning 0))
1291 nil)))
1292 string)
1293
1294 ;;;###autoload
1295 (defun dbx (command-line)
1296 "Run dbx on program FILE in buffer *gud-FILE*.
1297 The directory containing FILE becomes the initial working directory
1298 and source-file directory for your debugger."
1299 (interactive (list (gud-query-cmdline 'dbx)))
1300
1301 (cond
1302 (gud-mips-p
1303 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1304 (gud-irix-p
1305 (gud-common-init command-line 'gud-dbx-massage-args
1306 'gud-irixdbx-marker-filter))
1307 (t
1308 (gud-common-init command-line 'gud-dbx-massage-args
1309 'gud-dbx-marker-filter)))
1310
1311 (set (make-local-variable 'gud-minor-mode) 'dbx)
1312
1313 (cond
1314 (gud-mips-p
1315 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1316 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1317 (gud-def gud-break "stop at \"%f\":%l"
1318 "\C-b" "Set breakpoint at current line.")
1319 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1320 (gud-irix-p
1321 (gud-def gud-break "stop at \"%d%f\":%l"
1322 "\C-b" "Set breakpoint at current line.")
1323 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1324 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1325 "<" "Up (numeric arg) stack frames.")
1326 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1327 ">" "Down (numeric arg) stack frames.")
1328 ;; Make dbx give out the source location info that we need.
1329 (process-send-string (get-buffer-process gud-comint-buffer)
1330 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1331 (t
1332 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1333 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1334 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1335 "\C-b" "Set breakpoint at current line.")
1336 (if gud-dbx-use-stopformat-p
1337 (process-send-string (get-buffer-process gud-comint-buffer)
1338 "set $stopformat=1\n"))))
1339
1340 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1341 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1342 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1343 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1344 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1345 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1346 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1347 (gud-def gud-run "run" nil "Run the program.")
1348
1349 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1350 (setq paragraph-start comint-prompt-regexp)
1351 (run-hooks 'dbx-mode-hook)
1352 )
1353 \f
1354 ;; ======================================================================
1355 ;; xdb (HP PARISC debugger) functions
1356
1357 ;; History of argument lists passed to xdb.
1358 (defvar gud-xdb-history nil)
1359
1360 (defcustom gud-xdb-directories nil
1361 "A list of directories that xdb should search for source code.
1362 If nil, only source files in the program directory
1363 will be known to xdb.
1364
1365 The file names should be absolute, or relative to the directory
1366 containing the executable being debugged."
1367 :type '(choice (const :tag "Current Directory" nil)
1368 (repeat :value ("")
1369 directory))
1370 :group 'gud)
1371
1372 (defun gud-xdb-massage-args (_file args)
1373 (nconc (let ((directories gud-xdb-directories)
1374 (result nil))
1375 (while directories
1376 (setq result (cons (car directories) (cons "-d" result)))
1377 (setq directories (cdr directories)))
1378 (nreverse result))
1379 args))
1380
1381 ;; xdb does not print the lines all at once, so we have to accumulate them
1382 (defun gud-xdb-marker-filter (string)
1383 (let (result)
1384 (if (or (string-match comint-prompt-regexp string)
1385 (string-match ".*\012" string))
1386 (setq result (concat gud-marker-acc string)
1387 gud-marker-acc "")
1388 (setq gud-marker-acc (concat gud-marker-acc string)))
1389 (if result
1390 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1391 result)
1392 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1393 result))
1394 (let ((line (string-to-number (match-string 2 result)))
1395 (file (gud-file-name (match-string 1 result))))
1396 (if file
1397 (setq gud-last-frame (cons file line))))))
1398 (or result "")))
1399
1400 ;;;###autoload
1401 (defun xdb (command-line)
1402 "Run xdb on program FILE in buffer *gud-FILE*.
1403 The directory containing FILE becomes the initial working directory
1404 and source-file directory for your debugger.
1405
1406 You can set the variable `gud-xdb-directories' to a list of program source
1407 directories if your program contains sources from more than one directory."
1408 (interactive (list (gud-query-cmdline 'xdb)))
1409
1410 (gud-common-init command-line 'gud-xdb-massage-args
1411 'gud-xdb-marker-filter)
1412 (set (make-local-variable 'gud-minor-mode) 'xdb)
1413
1414 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1415 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1416 "Set temporary breakpoint at current line.")
1417 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1418 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1419 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1420 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1421 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1422 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1423 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1424 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1425
1426 (setq comint-prompt-regexp "^>")
1427 (setq paragraph-start comint-prompt-regexp)
1428 (run-hooks 'xdb-mode-hook))
1429 \f
1430 ;; ======================================================================
1431 ;; perldb functions
1432
1433 ;; History of argument lists passed to perldb.
1434 (defvar gud-perldb-history nil)
1435
1436 (defun gud-perldb-massage-args (_file args)
1437 "Convert a command line as would be typed normally to run perldb
1438 into one that invokes an Emacs-enabled debugging session.
1439 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1440 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1441 ;; arguments ?
1442 (let* ((new-args nil)
1443 (seen-e nil)
1444 (shift (lambda () (push (pop args) new-args))))
1445
1446 ;; Pass all switches and -e scripts through.
1447 (while (and args
1448 (string-match "^-" (car args))
1449 (not (equal "-" (car args)))
1450 (not (equal "--" (car args))))
1451 (when (equal "-e" (car args))
1452 ;; -e goes with the next arg, so shift one extra.
1453 (or (funcall shift)
1454 ;; -e as the last arg is an error in Perl.
1455 (error "No code specified for -e"))
1456 (setq seen-e t))
1457 (funcall shift))
1458
1459 (unless seen-e
1460 (if (or (not args)
1461 (string-match "^-" (car args)))
1462 (error "Can't use stdin as the script to debug"))
1463 ;; This is the program name.
1464 (funcall shift))
1465
1466 ;; If -e specified, make sure there is a -- so -emacs is not taken
1467 ;; as -e macs.
1468 (if (and args (equal "--" (car args)))
1469 (funcall shift)
1470 (and seen-e (push "--" new-args)))
1471
1472 (push "-emacs" new-args)
1473 (while args
1474 (funcall shift))
1475
1476 (nreverse new-args)))
1477
1478 ;; There's no guarantee that Emacs will hand the filter the entire
1479 ;; marker at once; it could be broken up across several strings. We
1480 ;; might even receive a big chunk with several markers in it. If we
1481 ;; receive a chunk of text which looks like it might contain the
1482 ;; beginning of a marker, we save it here between calls to the
1483 ;; filter.
1484 (defun gud-perldb-marker-filter (string)
1485 (setq gud-marker-acc (concat gud-marker-acc string))
1486 (let ((output ""))
1487
1488 ;; Process all the complete markers in this chunk.
1489 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1490 gud-marker-acc)
1491 (setq
1492
1493 ;; Extract the frame position from the marker.
1494 gud-last-frame
1495 (cons (match-string 1 gud-marker-acc)
1496 (string-to-number (match-string 3 gud-marker-acc)))
1497
1498 ;; Append any text before the marker to the output we're going
1499 ;; to return - we don't include the marker in this text.
1500 output (concat output
1501 (substring gud-marker-acc 0 (match-beginning 0)))
1502
1503 ;; Set the accumulator to the remaining text.
1504 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1505
1506 ;; Does the remaining text look like it might end with the
1507 ;; beginning of another marker? If it does, then keep it in
1508 ;; gud-marker-acc until we receive the rest of it. Since we
1509 ;; know the full marker regexp above failed, it's pretty simple to
1510 ;; test for marker starts.
1511 (if (string-match "\032.*\\'" gud-marker-acc)
1512 (progn
1513 ;; Everything before the potential marker start can be output.
1514 (setq output (concat output (substring gud-marker-acc
1515 0 (match-beginning 0))))
1516
1517 ;; Everything after, we save, to combine with later input.
1518 (setq gud-marker-acc
1519 (substring gud-marker-acc (match-beginning 0))))
1520
1521 (setq output (concat output gud-marker-acc)
1522 gud-marker-acc ""))
1523
1524 output))
1525
1526 (defcustom gud-perldb-command-name "perl -d"
1527 "Default command to execute a Perl script under debugger."
1528 :type 'string
1529 :group 'gud)
1530
1531 ;;;###autoload
1532 (defun perldb (command-line)
1533 "Run perldb on program FILE in buffer *gud-FILE*.
1534 The directory containing FILE becomes the initial working directory
1535 and source-file directory for your debugger."
1536 (interactive
1537 (list (gud-query-cmdline 'perldb
1538 (concat (or (buffer-file-name) "-e 0") " "))))
1539
1540 (gud-common-init command-line 'gud-perldb-massage-args
1541 'gud-perldb-marker-filter)
1542 (set (make-local-variable 'gud-minor-mode) 'perldb)
1543
1544 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1545 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1546 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1547 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1548 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1549 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1550 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1551 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1552 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1553 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1554
1555
1556 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1557 (setq paragraph-start comint-prompt-regexp)
1558 (run-hooks 'perldb-mode-hook))
1559 \f
1560 ;; ======================================================================
1561 ;; pdb (Python debugger) functions
1562
1563 ;; History of argument lists passed to pdb.
1564 (defvar gud-pdb-history nil)
1565
1566 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1567 ;; Either file or function name may be omitted: "> <string>(0)?()"
1568 (defvar gud-pdb-marker-regexp
1569 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\|<module>\\)()\\(->[^\n\r]*\\)?[\n\r]")
1570
1571 (defvar gud-pdb-marker-regexp-file-group 1)
1572 (defvar gud-pdb-marker-regexp-line-group 2)
1573 (defvar gud-pdb-marker-regexp-fnname-group 3)
1574
1575 (defvar gud-pdb-marker-regexp-start "^> ")
1576
1577 ;; There's no guarantee that Emacs will hand the filter the entire
1578 ;; marker at once; it could be broken up across several strings. We
1579 ;; might even receive a big chunk with several markers in it. If we
1580 ;; receive a chunk of text which looks like it might contain the
1581 ;; beginning of a marker, we save it here between calls to the
1582 ;; filter.
1583 (defun gud-pdb-marker-filter (string)
1584 (setq gud-marker-acc (concat gud-marker-acc string))
1585 (let ((output ""))
1586
1587 ;; Process all the complete markers in this chunk.
1588 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1589 (setq
1590
1591 ;; Extract the frame position from the marker.
1592 gud-last-frame
1593 (let ((file (match-string gud-pdb-marker-regexp-file-group
1594 gud-marker-acc))
1595 (line (string-to-number
1596 (match-string gud-pdb-marker-regexp-line-group
1597 gud-marker-acc))))
1598 (if (string-equal file "<string>")
1599 gud-last-frame
1600 (cons file line)))
1601
1602 ;; Output everything instead of the below
1603 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1604 ;; ;; Append any text before the marker to the output we're going
1605 ;; ;; to return - we don't include the marker in this text.
1606 ;; output (concat output
1607 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1608
1609 ;; Set the accumulator to the remaining text.
1610 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1611
1612 ;; Does the remaining text look like it might end with the
1613 ;; beginning of another marker? If it does, then keep it in
1614 ;; gud-marker-acc until we receive the rest of it. Since we
1615 ;; know the full marker regexp above failed, it's pretty simple to
1616 ;; test for marker starts.
1617 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1618 (progn
1619 ;; Everything before the potential marker start can be output.
1620 (setq output (concat output (substring gud-marker-acc
1621 0 (match-beginning 0))))
1622
1623 ;; Everything after, we save, to combine with later input.
1624 (setq gud-marker-acc
1625 (substring gud-marker-acc (match-beginning 0))))
1626
1627 (setq output (concat output gud-marker-acc)
1628 gud-marker-acc ""))
1629
1630 output))
1631
1632 (defcustom gud-pdb-command-name "pdb"
1633 "File name for executing the Python debugger.
1634 This should be an executable on your path, or an absolute file name."
1635 :type 'string
1636 :group 'gud)
1637
1638 ;;;###autoload
1639 (defun pdb (command-line)
1640 "Run pdb on program FILE in buffer `*gud-FILE*'.
1641 The directory containing FILE becomes the initial working directory
1642 and source-file directory for your debugger."
1643 (interactive
1644 (list (gud-query-cmdline 'pdb)))
1645
1646 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1647 (set (make-local-variable 'gud-minor-mode) 'pdb)
1648
1649 (gud-def gud-break "break %d%f:%l" "\C-b" "Set breakpoint at current line.")
1650 (gud-def gud-remove "clear %d%f:%l" "\C-d" "Remove breakpoint at current line")
1651 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1652 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1653 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1654 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1655 (gud-def gud-up "up" "<" "Up one stack frame.")
1656 (gud-def gud-down "down" ">" "Down one stack frame.")
1657 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1658 ;; Is this right?
1659 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1660
1661 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1662 (setq comint-prompt-regexp "^(Pdb) *")
1663 (setq paragraph-start comint-prompt-regexp)
1664 (run-hooks 'pdb-mode-hook))
1665 \f
1666 ;; ======================================================================
1667 ;;
1668 ;; JDB support.
1669 ;;
1670 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1671 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1672 ;;
1673 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1674 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1675 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1676 ;;
1677 ;; INVOCATION NOTES:
1678 ;;
1679 ;; You invoke jdb-mode with:
1680 ;;
1681 ;; M-x jdb <enter>
1682 ;;
1683 ;; It responds with:
1684 ;;
1685 ;; Run jdb (like this): jdb
1686 ;;
1687 ;; type any jdb switches followed by the name of the class you'd like to debug.
1688 ;; Supply a fully qualified classname (these don't have the ".class" extension)
1689 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1690 ;; See the known problems section below for restrictions when specifying jdb
1691 ;; command line switches (search forward for '-classpath').
1692 ;;
1693 ;; You should see something like the following:
1694 ;;
1695 ;; Current directory is ~/src/java/hello/
1696 ;; Initializing jdb...
1697 ;; 0xed2f6628:class(hello)
1698 ;; >
1699 ;;
1700 ;; To set an initial breakpoint try:
1701 ;;
1702 ;; > stop in hello.main
1703 ;; Breakpoint set in hello.main
1704 ;; >
1705 ;;
1706 ;; To execute the program type:
1707 ;;
1708 ;; > run
1709 ;; run hello
1710 ;;
1711 ;; Breakpoint hit: running ...
1712 ;; hello.main (hello:12)
1713 ;;
1714 ;; Type M-n to step over the current line and M-s to step into it. That,
1715 ;; along with the JDB 'help' command should get you started. The 'quit'
1716 ;; JDB command will get out out of the debugger. There is some truly
1717 ;; pathetic JDB documentation available at:
1718 ;;
1719 ;; http://java.sun.com/products/jdk/1.1/debugging/
1720 ;;
1721 ;; KNOWN PROBLEMS AND FIXME's:
1722 ;;
1723 ;; Not sure what happens with inner classes ... haven't tried them.
1724 ;;
1725 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1726 ;;
1727 ;; You must not put whitespace between "-classpath" and the path to
1728 ;; search for java classes even though it is required when invoking jdb
1729 ;; from the command line. See gud-jdb-massage-args for details.
1730 ;; The same applies for "-sourcepath".
1731 ;;
1732 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1733 ;; refer to the documentation of `gud-jdb-use-classpath' and
1734 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1735 ;; on using the classpath for locating java source files.
1736 ;;
1737 ;; If any of the source files in the directories listed in
1738 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1739 ;; every file ending in ".java" in these directories parses without error.
1740 ;;
1741 ;; All the .java files in the directories in gud-jdb-directories are
1742 ;; syntactically analyzed each time gud jdb is invoked. It would be
1743 ;; nice to keep as much information as possible between runs. It would
1744 ;; be really nice to analyze the files only as necessary (when the
1745 ;; source needs to be displayed.) I'm not sure to what extent the former
1746 ;; can be accomplished and I'm not sure the latter can be done at all
1747 ;; since I don't know of any general way to tell which .class files are
1748 ;; defined by which .java file without analyzing all the .java files.
1749 ;; If anyone knows why JavaSoft didn't put the source file names in
1750 ;; debuggable .class files please clue me in so I find something else
1751 ;; to be spiteful and bitter about.
1752 ;;
1753 ;; ======================================================================
1754 ;; gud jdb variables and functions
1755
1756 (defcustom gud-jdb-command-name "jdb"
1757 "Command that executes the Java debugger."
1758 :type 'string
1759 :group 'gud)
1760
1761 (defcustom gud-jdb-use-classpath t
1762 "If non-nil, search for Java source files in classpath directories.
1763 The list of directories to search is the value of `gud-jdb-classpath'.
1764 The file pathname is obtained by converting the fully qualified
1765 class information output by jdb to a relative pathname and appending
1766 it to `gud-jdb-classpath' element by element until a match is found.
1767
1768 This method has a significant jdb startup time reduction advantage
1769 since it does not require the scanning of all `gud-jdb-directories'
1770 and parsing all Java files for class information.
1771
1772 Set to nil to use `gud-jdb-directories' to scan java sources for
1773 class information on jdb startup (original method)."
1774 :type 'boolean
1775 :group 'gud)
1776
1777 (defvar gud-jdb-classpath nil
1778 "Java/jdb classpath directories list.
1779 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1780 list automatically using the following methods in sequence
1781 \(with subsequent successful steps overriding the results of previous
1782 steps):
1783
1784 1) Read the CLASSPATH environment variable,
1785 2) Read any \"-classpath\" argument used to run jdb,
1786 or detected in jdb output (e.g. if jdb is run by a script
1787 that echoes the actual jdb command before starting jdb),
1788 3) Send a \"classpath\" command to jdb and scan jdb output for
1789 classpath information if jdb is invoked with an \"-attach\" (to
1790 an already running VM) argument (This case typically does not
1791 have a \"-classpath\" command line argument - that is provided
1792 to the VM when it is started).
1793
1794 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1795 those debuggers do not support the classpath command. Use 1) or 2).")
1796
1797 (defvar gud-jdb-sourcepath nil
1798 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1799 This list is prepended to `gud-jdb-classpath' to form the complete
1800 list of directories searched for source files.")
1801
1802 (defvar gud-marker-acc-max-length 4000
1803 "Maximum number of debugger output characters to keep.
1804 This variable limits the size of `gud-marker-acc' which holds
1805 the most recent debugger output history while searching for
1806 source file information.")
1807
1808 (defvar gud-jdb-history nil
1809 "History of argument lists passed to jdb.")
1810
1811
1812 ;; List of Java source file directories.
1813 (defvar gud-jdb-directories (list ".")
1814 "A list of directories that gud jdb should search for source code.
1815 The file names should be absolute, or relative to the current
1816 directory.
1817
1818 The set of .java files residing in the directories listed are
1819 syntactically analyzed to determine the classes they define and the
1820 packages in which these classes belong. In this way gud jdb maps the
1821 package-qualified class names output by the jdb debugger to the source
1822 file from which the class originated. This allows gud mode to keep
1823 the source code display in sync with the debugging session.")
1824
1825 (defvar gud-jdb-source-files nil
1826 "List of the java source files for this debugging session.")
1827
1828 ;; Association list of fully qualified class names (package + class name)
1829 ;; and their source files.
1830 (defvar gud-jdb-class-source-alist nil
1831 "Association list of fully qualified class names and source files.")
1832
1833 ;; This is used to hold a source file during analysis.
1834 (defvar gud-jdb-analysis-buffer nil)
1835
1836 (defvar gud-jdb-classpath-string nil
1837 "Holds temporary classpath values.")
1838
1839 (defun gud-jdb-build-source-files-list (path extn)
1840 "Return a list of java source files (absolute paths).
1841 PATH gives the directories in which to search for files with
1842 extension EXTN. Normally EXTN is given as the regular expression
1843 \"\\.java$\" ."
1844 (apply 'nconc (mapcar (lambda (d)
1845 (when (file-directory-p d)
1846 (directory-files d t extn nil)))
1847 path)))
1848
1849 ;; Move point past whitespace.
1850 (defun gud-jdb-skip-whitespace ()
1851 (skip-chars-forward " \n\r\t\014"))
1852
1853 ;; Move point past a "// <eol>" type of comment.
1854 (defun gud-jdb-skip-single-line-comment ()
1855 (end-of-line))
1856
1857 ;; Move point past a "/* */" or "/** */" type of comment.
1858 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1859 (forward-char 2)
1860 (catch 'break
1861 (while (not (eobp))
1862 (if (eq (following-char) ?*)
1863 (progn
1864 (forward-char)
1865 (if (not (eobp))
1866 (if (eq (following-char) ?/)
1867 (progn
1868 (forward-char)
1869 (throw 'break nil)))))
1870 (forward-char)))))
1871
1872 ;; Move point past any number of consecutive whitespace chars and/or comments.
1873 (defun gud-jdb-skip-whitespace-and-comments ()
1874 (gud-jdb-skip-whitespace)
1875 (catch 'done
1876 (while t
1877 (cond
1878 ((looking-at "//")
1879 (gud-jdb-skip-single-line-comment)
1880 (gud-jdb-skip-whitespace))
1881 ((looking-at "/\\*")
1882 (gud-jdb-skip-traditional-or-documentation-comment)
1883 (gud-jdb-skip-whitespace))
1884 (t (throw 'done nil))))))
1885
1886 ;; Move point past things that are id-like. The intent is to skip regular
1887 ;; id's, such as class or interface names as well as package and interface
1888 ;; names.
1889 (defun gud-jdb-skip-id-ish-thing ()
1890 (skip-chars-forward "^ /\n\r\t\014,;{"))
1891
1892 ;; Move point past a string literal.
1893 (defun gud-jdb-skip-string-literal ()
1894 (forward-char)
1895 (while (not (cond
1896 ((eq (following-char) ?\\)
1897 (forward-char))
1898 ((eq (following-char) ?\042))))
1899 (forward-char))
1900 (forward-char))
1901
1902 ;; Move point past a character literal.
1903 (defun gud-jdb-skip-character-literal ()
1904 (forward-char)
1905 (while
1906 (progn
1907 (if (eq (following-char) ?\\)
1908 (forward-char 2))
1909 (not (eq (following-char) ?\')))
1910 (forward-char))
1911 (forward-char))
1912
1913 ;; Move point past the following block. There may be (legal) cruft before
1914 ;; the block's opening brace. There must be a block or it's the end of life
1915 ;; in petticoat junction.
1916 (defun gud-jdb-skip-block ()
1917
1918 ;; Find the beginning of the block.
1919 (while
1920 (not (eq (following-char) ?{))
1921
1922 ;; Skip any constructs that can harbor literal block delimiter
1923 ;; characters and/or the delimiters for the constructs themselves.
1924 (cond
1925 ((looking-at "//")
1926 (gud-jdb-skip-single-line-comment))
1927 ((looking-at "/\\*")
1928 (gud-jdb-skip-traditional-or-documentation-comment))
1929 ((eq (following-char) ?\042)
1930 (gud-jdb-skip-string-literal))
1931 ((eq (following-char) ?\')
1932 (gud-jdb-skip-character-literal))
1933 (t (forward-char))))
1934
1935 ;; Now at the beginning of the block.
1936 (forward-char)
1937
1938 ;; Skip over the body of the block as well as the final brace.
1939 (let ((open-level 1))
1940 (while (not (eq open-level 0))
1941 (cond
1942 ((looking-at "//")
1943 (gud-jdb-skip-single-line-comment))
1944 ((looking-at "/\\*")
1945 (gud-jdb-skip-traditional-or-documentation-comment))
1946 ((eq (following-char) ?\042)
1947 (gud-jdb-skip-string-literal))
1948 ((eq (following-char) ?\')
1949 (gud-jdb-skip-character-literal))
1950 ((eq (following-char) ?{)
1951 (setq open-level (+ open-level 1))
1952 (forward-char))
1953 ((eq (following-char) ?})
1954 (setq open-level (- open-level 1))
1955 (forward-char))
1956 (t (forward-char))))))
1957
1958 ;; Find the package and class definitions in Java source file FILE. Assumes
1959 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1960 ;; to hold the source during analysis.
1961 (defun gud-jdb-analyze-source (buf file)
1962 (let ((l nil))
1963 (set-buffer buf)
1964 (insert-file-contents file nil nil nil t)
1965 (goto-char 0)
1966 (catch 'abort
1967 (let ((p ""))
1968 (while (progn
1969 (gud-jdb-skip-whitespace)
1970 (not (eobp)))
1971 (cond
1972
1973 ;; Any number of semi's following a block is legal. Move point
1974 ;; past them. Note that comments and whitespace may be
1975 ;; interspersed as well.
1976 ((eq (following-char) ?\073)
1977 (forward-char))
1978
1979 ;; Move point past a single line comment.
1980 ((looking-at "//")
1981 (gud-jdb-skip-single-line-comment))
1982
1983 ;; Move point past a traditional or documentation comment.
1984 ((looking-at "/\\*")
1985 (gud-jdb-skip-traditional-or-documentation-comment))
1986
1987 ;; Move point past a package statement, but save the PackageName.
1988 ((looking-at "package")
1989 (forward-char 7)
1990 (gud-jdb-skip-whitespace-and-comments)
1991 (let ((s (point)))
1992 (gud-jdb-skip-id-ish-thing)
1993 (setq p (concat (buffer-substring s (point)) "."))
1994 (gud-jdb-skip-whitespace-and-comments)
1995 (if (eq (following-char) ?\073)
1996 (forward-char))))
1997
1998 ;; Move point past an import statement.
1999 ((looking-at "import")
2000 (forward-char 6)
2001 (gud-jdb-skip-whitespace-and-comments)
2002 (gud-jdb-skip-id-ish-thing)
2003 (gud-jdb-skip-whitespace-and-comments)
2004 (if (eq (following-char) ?\073)
2005 (forward-char)))
2006
2007 ;; Move point past the various kinds of ClassModifiers.
2008 ((looking-at "public")
2009 (forward-char 6))
2010 ((looking-at "abstract")
2011 (forward-char 8))
2012 ((looking-at "final")
2013 (forward-char 5))
2014
2015 ;; Move point past a ClassDeclaration, but save the class
2016 ;; Identifier.
2017 ((looking-at "class")
2018 (forward-char 5)
2019 (gud-jdb-skip-whitespace-and-comments)
2020 (let ((s (point)))
2021 (gud-jdb-skip-id-ish-thing)
2022 (setq
2023 l (nconc l (list (concat p (buffer-substring s (point)))))))
2024 (gud-jdb-skip-block))
2025
2026 ;; Move point past an interface statement.
2027 ((looking-at "interface")
2028 (forward-char 9)
2029 (gud-jdb-skip-block))
2030
2031 ;; Anything else means the input is invalid.
2032 (t
2033 (message "Error parsing file %s." file)
2034 (throw 'abort nil))))))
2035 l))
2036
2037 (defun gud-jdb-build-class-source-alist-for-file (file)
2038 (mapcar
2039 (lambda (c)
2040 (cons c file))
2041 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
2042
2043 ;; Return an alist of fully qualified classes and the source files
2044 ;; holding their definitions. SOURCES holds a list of all the source
2045 ;; files to examine.
2046 (defun gud-jdb-build-class-source-alist (sources)
2047 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
2048 (prog1
2049 (apply
2050 'nconc
2051 (mapcar
2052 'gud-jdb-build-class-source-alist-for-file
2053 sources))
2054 (kill-buffer gud-jdb-analysis-buffer)
2055 (setq gud-jdb-analysis-buffer nil)))
2056
2057 ;; Change what was given in the minibuffer to something that can be used to
2058 ;; invoke the debugger.
2059 (defun gud-jdb-massage-args (_file args)
2060 ;; The jdb executable must have whitespace between "-classpath" and
2061 ;; its value while gud-common-init expects all switch values to
2062 ;; follow the switch keyword without intervening whitespace. We
2063 ;; require that when the user enters the "-classpath" switch in the
2064 ;; EMACS minibuffer that they do so without the intervening
2065 ;; whitespace. This function adds it back (it's called after
2066 ;; gud-common-init). There are more switches like this (for
2067 ;; instance "-host" and "-password") but I don't care about them
2068 ;; yet.
2069 (if args
2070 (let (massaged-args user-error)
2071
2072 (while (and args (not user-error))
2073 (cond
2074 ((setq user-error (string-match "-classpath$" (car args))))
2075 ((setq user-error (string-match "-sourcepath$" (car args))))
2076 ((string-match "-classpath\\(.+\\)" (car args))
2077 (setq massaged-args
2078 (append massaged-args
2079 (list "-classpath"
2080 (setq gud-jdb-classpath-string
2081 (match-string 1 (car args)))))))
2082 ((string-match "-sourcepath\\(.+\\)" (car args))
2083 (setq massaged-args
2084 (append massaged-args
2085 (list "-sourcepath"
2086 (setq gud-jdb-sourcepath
2087 (match-string 1 (car args)))))))
2088 (t (setq massaged-args (append massaged-args (list (car args))))))
2089 (setq args (cdr args)))
2090
2091 ;; By this point the current directory is all screwed up. Maybe we
2092 ;; could fix things and re-invoke gud-common-init, but for now I think
2093 ;; issuing the error is good enough.
2094 (if user-error
2095 (progn
2096 (kill-buffer (current-buffer))
2097 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
2098 massaged-args)))
2099
2100 ;; Search for an association with P, a fully qualified class name, in
2101 ;; gud-jdb-class-source-alist. The association gives the fully
2102 ;; qualified file name of the source file which produced the class.
2103 (defun gud-jdb-find-source-file (p)
2104 (cdr (assoc p gud-jdb-class-source-alist)))
2105
2106 ;; Note: Reset to this value every time a prompt is seen
2107 (defvar gud-jdb-lowest-stack-level 999)
2108
2109 (defun gud-jdb-find-source-using-classpath (p)
2110 "Find source file corresponding to fully qualified class P.
2111 Convert P from jdb's output, converted to a pathname
2112 relative to a classpath directory."
2113 (save-match-data
2114 (let
2115 (;; Replace dots with slashes and append ".java" to generate file
2116 ;; name relative to classpath
2117 (filename
2118 (concat
2119 (mapconcat 'identity
2120 (split-string
2121 ;; Eliminate any subclass references in the class
2122 ;; name string. These start with a "$"
2123 ((lambda (x)
2124 (if (string-match "$.*" x)
2125 (replace-match "" t t x) p))
2126 p)
2127 "\\.") "/")
2128 ".java"))
2129 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2130 found-file)
2131 (while (and cplist
2132 (not (setq found-file
2133 (file-readable-p
2134 (concat (car cplist) "/" filename)))))
2135 (setq cplist (cdr cplist)))
2136 (if found-file (concat (car cplist) "/" filename)))))
2137
2138 (defun gud-jdb-find-source (_string)
2139 "Alias for function used to locate source files.
2140 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
2141 during jdb initialization depending on the value of
2142 `gud-jdb-use-classpath'."
2143 nil)
2144
2145 (defun gud-jdb-parse-classpath-string (string)
2146 "Parse the classpath list and convert each item to an absolute pathname."
2147 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
2148 (replace-match "" nil nil s) s))
2149 (mapcar 'file-truename
2150 (split-string
2151 string
2152 (concat "[ \t\n\r,\"" path-separator "]+")))))
2153
2154 ;; See commentary for other debugger's marker filters - there you will find
2155 ;; important notes about STRING.
2156 (defun gud-jdb-marker-filter (string)
2157
2158 ;; Build up the accumulator.
2159 (setq gud-marker-acc
2160 (if gud-marker-acc
2161 (concat gud-marker-acc string)
2162 string))
2163
2164 ;; Look for classpath information until gud-jdb-classpath-string is found
2165 ;; (interactive, multiple settings of classpath from jdb
2166 ;; not supported/followed)
2167 (if (and gud-jdb-use-classpath
2168 (not gud-jdb-classpath-string)
2169 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
2170 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
2171 (setq gud-jdb-classpath
2172 (gud-jdb-parse-classpath-string
2173 (setq gud-jdb-classpath-string
2174 (match-string 1 gud-marker-acc)))))
2175
2176 ;; We process STRING from left to right. Each time through the
2177 ;; following loop we process at most one marker. After we've found a
2178 ;; marker, delete gud-marker-acc up to and including the match
2179 (let (file-found)
2180 ;; Process each complete marker in the input.
2181 (while
2182
2183 ;; Do we see a marker?
2184 (string-match
2185 ;; jdb puts out a string of the following form when it
2186 ;; hits a breakpoint:
2187 ;;
2188 ;; <fully-qualified-class><method> (<class>:<line-number>)
2189 ;;
2190 ;; <fully-qualified-class>'s are composed of Java ID's
2191 ;; separated by periods. <method> and <class> are
2192 ;; also Java ID's. <method> begins with a period and
2193 ;; may contain less-than and greater-than (constructors,
2194 ;; for instance, are called <init> in the symbol table.)
2195 ;; Java ID's begin with a letter followed by letters
2196 ;; and/or digits. The set of letters includes underscore
2197 ;; and dollar sign.
2198 ;;
2199 ;; The first group matches <fully-qualified-class>,
2200 ;; the second group matches <class> and the third group
2201 ;; matches <line-number>. We don't care about using
2202 ;; <method> so we don't "group" it.
2203 ;;
2204 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2205 ;; ID's only.
2206 ;;
2207 ;; The ".," in the last square-bracket are necessary because
2208 ;; of Sun's total disrespect for backwards compatibility in
2209 ;; reported line numbers from jdb - starting in 1.4.0 they
2210 ;; print line numbers using LOCALE, inserting a comma or a
2211 ;; period at the thousands positions (how ingenious!).
2212
2213 "\\(\\[[0-9]+] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2214 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2215 gud-marker-acc)
2216
2217 ;; A good marker is one that:
2218 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2219 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2220 ;; since the last prompt
2221 ;; Figure out the line on which to position the debugging arrow.
2222 ;; Return the info as a cons of the form:
2223 ;;
2224 ;; (<file-name> . <line-number>) .
2225 (if (if (match-beginning 1)
2226 (let (n)
2227 (setq n (string-to-number (substring
2228 gud-marker-acc
2229 (1+ (match-beginning 1))
2230 (- (match-end 1) 2))))
2231 (if (< n gud-jdb-lowest-stack-level)
2232 (progn (setq gud-jdb-lowest-stack-level n) t)))
2233 t)
2234 (if (setq file-found
2235 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2236 (setq gud-last-frame
2237 (cons file-found
2238 (string-to-number
2239 (let
2240 ((numstr (match-string 4 gud-marker-acc)))
2241 (if (string-match "[.,]" numstr)
2242 (replace-match "" nil nil numstr)
2243 numstr)))))
2244 (message "Could not find source file.")))
2245
2246 ;; Set the accumulator to the remaining text.
2247 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2248
2249 (if (string-match comint-prompt-regexp gud-marker-acc)
2250 (setq gud-jdb-lowest-stack-level 999)))
2251
2252 ;; Do not allow gud-marker-acc to grow without bound. If the source
2253 ;; file information is not within the last 3/4
2254 ;; gud-marker-acc-max-length characters, well,...
2255 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2256 (setq gud-marker-acc
2257 (substring gud-marker-acc
2258 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2259
2260 ;; We don't filter any debugger output so just return what we were given.
2261 string)
2262
2263 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2264
2265 ;;;###autoload
2266 (defun jdb (command-line)
2267 "Run jdb with command line COMMAND-LINE in a buffer.
2268 The buffer is named \"*gud*\" if no initial class is given or
2269 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2270 switch is given, omit all whitespace between it and its value.
2271
2272 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2273 information on how jdb accesses source files. Alternatively (if
2274 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2275 original source file access method.
2276
2277 For general information about commands available to control jdb from
2278 gud, see `gud-mode'."
2279 (interactive
2280 (list (gud-query-cmdline 'jdb)))
2281 (setq gud-jdb-classpath nil)
2282 (setq gud-jdb-sourcepath nil)
2283
2284 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2285 ;; if CLASSPATH is set.
2286 (setq gud-jdb-classpath-string (or (getenv "CLASSPATH") "."))
2287 (if gud-jdb-classpath-string
2288 (setq gud-jdb-classpath
2289 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2290 (setq gud-jdb-classpath-string nil) ; prepare for next
2291
2292 (gud-common-init command-line 'gud-jdb-massage-args
2293 'gud-jdb-marker-filter)
2294 (set (make-local-variable 'gud-minor-mode) 'jdb)
2295
2296 ;; If a -classpath option was provided, set gud-jdb-classpath
2297 (if gud-jdb-classpath-string
2298 (setq gud-jdb-classpath
2299 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2300 (setq gud-jdb-classpath-string nil) ; prepare for next
2301 ;; If a -sourcepath option was provided, parse it
2302 (if gud-jdb-sourcepath
2303 (setq gud-jdb-sourcepath
2304 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2305
2306 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2307 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2308 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2309 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2310 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2311 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2312 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2313 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2314 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2315 (gud-def gud-print "print %e" "\C-p" "Print value of expression at point.")
2316 (gud-def gud-pstar "dump %e" nil "Print all object information at point.")
2317
2318 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2319 (setq paragraph-start comint-prompt-regexp)
2320 (run-hooks 'jdb-mode-hook)
2321
2322 (if gud-jdb-use-classpath
2323 ;; Get the classpath information from the debugger
2324 (progn
2325 (if (string-match "-attach" command-line)
2326 (gud-call "classpath"))
2327 (fset 'gud-jdb-find-source
2328 'gud-jdb-find-source-using-classpath))
2329
2330 ;; Else create and bind the class/source association list as well
2331 ;; as the source file list.
2332 (setq gud-jdb-class-source-alist
2333 (gud-jdb-build-class-source-alist
2334 (setq gud-jdb-source-files
2335 (gud-jdb-build-source-files-list gud-jdb-directories
2336 "\\.java$"))))
2337 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2338
2339 ;;
2340 ;; End of debugger-specific information
2341 ;;
2342
2343 \f
2344 ;; When we send a command to the debugger via gud-call, it's annoying
2345 ;; to see the command and the new prompt inserted into the debugger's
2346 ;; buffer; we have other ways of knowing the command has completed.
2347 ;;
2348 ;; If the buffer looks like this:
2349 ;; --------------------
2350 ;; (gdb) set args foo bar
2351 ;; (gdb) -!-
2352 ;; --------------------
2353 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2354 ;; source file to set a breakpoint, we want the buffer to end up like
2355 ;; this:
2356 ;; --------------------
2357 ;; (gdb) set args foo bar
2358 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2359 ;; (gdb) -!-
2360 ;; --------------------
2361 ;; Essentially, the old prompt is deleted, and the command's output
2362 ;; and the new prompt take its place.
2363 ;;
2364 ;; Not echoing the command is easy enough; you send it directly using
2365 ;; process-send-string, and it never enters the buffer. However,
2366 ;; getting rid of the old prompt is trickier; you don't want to do it
2367 ;; when you send the command, since that will result in an annoying
2368 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2369 ;; waits for a response from the debugger, and the new prompt is
2370 ;; inserted. Instead, we'll wait until we actually get some output
2371 ;; from the subprocess before we delete the prompt. If the command
2372 ;; produced no output other than a new prompt, that prompt will most
2373 ;; likely be in the first chunk of output received, so we will delete
2374 ;; the prompt and then replace it with an identical one. If the
2375 ;; command produces output, the prompt is moving anyway, so the
2376 ;; flicker won't be annoying.
2377 ;;
2378 ;; So - when we want to delete the prompt upon receipt of the next
2379 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2380 ;; the start of the prompt; the process filter will notice this, and
2381 ;; delete all text between it and the process output marker. If
2382 ;; gud-delete-prompt-marker points nowhere, we leave the current
2383 ;; prompt alone.
2384 (defvar gud-delete-prompt-marker nil)
2385
2386 \f
2387 (put 'gud-mode 'mode-class 'special)
2388
2389 (define-derived-mode gud-mode comint-mode "Debugger"
2390 "Major mode for interacting with an inferior debugger process.
2391
2392 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2393 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2394 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2395 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2396
2397 After startup, the following commands are available in both the GUD
2398 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2399 or step operation:
2400
2401 \\[gud-break] sets a breakpoint at the current file and line. In the
2402 GUD buffer, the current file and line are those of the last breakpoint or
2403 step. In a source buffer, they are the buffer's file and current line.
2404
2405 \\[gud-remove] removes breakpoints on the current file and line.
2406
2407 \\[gud-refresh] displays in the source window the last line referred to
2408 in the gud buffer.
2409
2410 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2411 step-one-line (not entering function calls), and step-one-instruction
2412 and then update the source window with the current file and position.
2413 \\[gud-cont] continues execution.
2414
2415 \\[gud-print] tries to find the largest C lvalue or function-call expression
2416 around point, and sends it to the debugger for value display.
2417
2418 The above commands are common to all supported debuggers except xdb which
2419 does not support stepping instructions.
2420
2421 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2422 except that the breakpoint is temporary; that is, it is removed when
2423 execution stops on it.
2424
2425 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2426 frame. \\[gud-down] drops back down through one.
2427
2428 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2429 the current function and stops.
2430
2431 All the keystrokes above are accessible in the GUD buffer
2432 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2433
2434 All pre-defined functions for which the concept make sense repeat
2435 themselves the appropriate number of times if you give a prefix
2436 argument.
2437
2438 You may use the `gud-def' macro in the initialization hook to define other
2439 commands.
2440
2441 Other commands for interacting with the debugger process are inherited from
2442 comint mode, which see."
2443 (setq mode-line-process '(":%s"))
2444 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2445 (set (make-local-variable 'gud-last-frame) nil)
2446 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2447 (make-local-variable 'comint-prompt-regexp)
2448 ;; Don't put repeated commands in command history many times.
2449 (set (make-local-variable 'comint-input-ignoredups) t)
2450 (make-local-variable 'paragraph-start)
2451 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2452 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2453
2454 (defcustom gud-chdir-before-run t
2455 "Non-nil if GUD should `cd' to the debugged executable."
2456 :group 'gud
2457 :type 'boolean)
2458
2459 (declare-function tramp-file-name-localname "tramp" (vec))
2460 (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
2461
2462 ;; Perform initializations common to all debuggers.
2463 ;; The first arg is the specified command line,
2464 ;; which starts with the program to debug.
2465 ;; The other three args specify the values to use
2466 ;; for local variables in the debugger buffer.
2467 (defun gud-common-init (command-line massage-args marker-filter
2468 &optional find-file)
2469 (let* ((words (split-string-and-unquote command-line))
2470 (program (car words))
2471 (dir default-directory)
2472 ;; Extract the file name from WORDS
2473 ;; and put t in its place.
2474 ;; Later on we will put the modified file name arg back there.
2475 (file-word (let ((w (cdr words)))
2476 (while (and w (= ?- (aref (car w) 0)))
2477 (setq w (cdr w)))
2478 (and w
2479 (prog1 (car w)
2480 (setcar w t)))))
2481 (file-subst
2482 (and file-word (substitute-in-file-name file-word)))
2483 (args (cdr words))
2484 ;; If a directory was specified, expand the file name.
2485 ;; Otherwise, don't expand it, so GDB can use the PATH.
2486 ;; A file name without directory is literally valid
2487 ;; only if the file exists in ., and in that case,
2488 ;; omitting the expansion here has no visible effect.
2489 (file (and file-word
2490 (if (file-name-directory file-subst)
2491 (expand-file-name file-subst)
2492 file-subst)))
2493 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2494 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2495 (switch-to-buffer (concat "*gud" filepart "*"))
2496 (when (and existing-buffer (get-buffer-process existing-buffer))
2497 (error "This program is already being debugged"))
2498 ;; Set the dir, in case the buffer already existed with a different dir.
2499 (setq default-directory dir)
2500 ;; Set default-directory to the file's directory.
2501 (and file-word
2502 gud-chdir-before-run
2503 ;; Don't set default-directory if no directory was specified.
2504 ;; In that case, either the file is found in the current directory,
2505 ;; in which case this setq is a no-op,
2506 ;; or it is found by searching PATH,
2507 ;; in which case we don't know what directory it was found in.
2508 (file-name-directory file)
2509 (setq default-directory (file-name-directory file)))
2510 (or (bolp) (newline))
2511 (insert "Current directory is " default-directory "\n")
2512 ;; Put the substituted and expanded file name back in its place.
2513 (let ((w args))
2514 (while (and w (not (eq (car w) t)))
2515 (setq w (cdr w)))
2516 (if w
2517 (setcar w
2518 (if (file-remote-p file)
2519 ;; Tramp has already been loaded if we are here.
2520 (setq file (tramp-file-name-localname
2521 (tramp-dissect-file-name file)))
2522 file))))
2523 (apply 'make-comint (concat "gud" filepart) program nil
2524 (if massage-args (funcall massage-args file args) args))
2525 ;; Since comint clobbered the mode, we don't set it until now.
2526 (gud-mode)
2527 (set (make-local-variable 'gud-target-name)
2528 (and file-word (file-name-nondirectory file))))
2529 (set (make-local-variable 'gud-marker-filter) marker-filter)
2530 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2531 (setq gud-last-last-frame nil)
2532
2533 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2534 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2535 (gud-set-buffer))
2536
2537 (defun gud-set-buffer ()
2538 (when (derived-mode-p 'gud-mode)
2539 (setq gud-comint-buffer (current-buffer))))
2540
2541 (defvar gud-filter-defer-flag nil
2542 "Non-nil means don't process anything from the debugger right now.
2543 It is saved for when this flag is not set.")
2544
2545 ;; These functions are responsible for inserting output from your debugger
2546 ;; into the buffer. The hard work is done by the method that is
2547 ;; the value of gud-marker-filter.
2548
2549 (defun gud-filter (proc string)
2550 ;; Here's where the actual buffer insertion is done
2551 (let (output process-window)
2552 (if (buffer-name (process-buffer proc))
2553 (if gud-filter-defer-flag
2554 ;; If we can't process any text now,
2555 ;; save it for later.
2556 (setq gud-filter-pending-text
2557 (concat (or gud-filter-pending-text "") string))
2558
2559 ;; If we have to ask a question during the processing,
2560 ;; defer any additional text that comes from the debugger
2561 ;; during that time.
2562 (let ((gud-filter-defer-flag t))
2563 ;; Process now any text we previously saved up.
2564 (if gud-filter-pending-text
2565 (setq string (concat gud-filter-pending-text string)
2566 gud-filter-pending-text nil))
2567
2568 (with-current-buffer (process-buffer proc)
2569 ;; If we have been so requested, delete the debugger prompt.
2570 (save-restriction
2571 (widen)
2572 (if (marker-buffer gud-delete-prompt-marker)
2573 (let ((inhibit-read-only t))
2574 (delete-region (process-mark proc)
2575 gud-delete-prompt-marker)
2576 (comint-update-fence)
2577 (set-marker gud-delete-prompt-marker nil)))
2578 ;; Save the process output, checking for source file markers.
2579 (setq output (gud-marker-filter string))
2580 ;; Check for a filename-and-line number.
2581 ;; Don't display the specified file
2582 ;; unless (1) point is at or after the position where output appears
2583 ;; and (2) this buffer is on the screen.
2584 (setq process-window
2585 (and gud-last-frame
2586 (>= (point) (process-mark proc))
2587 (get-buffer-window (current-buffer)))))
2588
2589 ;; Let the comint filter do the actual insertion.
2590 ;; That lets us inherit various comint features.
2591 (comint-output-filter proc output))
2592
2593 ;; Put the arrow on the source line.
2594 ;; This must be outside of the save-excursion
2595 ;; in case the source file is our current buffer.
2596 (if process-window
2597 (with-selected-window process-window
2598 (gud-display-frame))
2599 ;; We have to be in the proper buffer, (process-buffer proc),
2600 ;; but not in a save-excursion, because that would restore point.
2601 (with-current-buffer (process-buffer proc)
2602 (gud-display-frame))))
2603
2604 ;; If we deferred text that arrived during this processing,
2605 ;; handle it now.
2606 (if gud-filter-pending-text
2607 (gud-filter proc ""))))))
2608
2609 (defvar gud-minor-mode-type nil)
2610 (defvar gud-overlay-arrow-position nil)
2611 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2612
2613 (declare-function gdb-reset "gdb-mi" ())
2614
2615 (defun gud-sentinel (proc msg)
2616 (cond ((null (buffer-name (process-buffer proc)))
2617 ;; buffer killed
2618 ;; Stop displaying an arrow in a source file.
2619 (setq gud-overlay-arrow-position nil)
2620 (set-process-buffer proc nil)
2621 (if (and (boundp 'speedbar-frame)
2622 (string-equal speedbar-initial-expansion-list-name "GUD"))
2623 (speedbar-change-initial-expansion-list
2624 speedbar-previously-used-expansion-list-name))
2625 (if (eq gud-minor-mode-type 'gdbmi)
2626 (gdb-reset)
2627 (gud-reset)))
2628 ((memq (process-status proc) '(signal exit))
2629 ;; Stop displaying an arrow in a source file.
2630 (setq gud-overlay-arrow-position nil)
2631 (if (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2632 'gdbmi)
2633 (gdb-reset)
2634 (gud-reset))
2635 (let* ((obuf (current-buffer)))
2636 ;; save-excursion isn't the right thing if
2637 ;; process-buffer is current-buffer
2638 (unwind-protect
2639 (progn
2640 ;; Write something in the GUD buffer and hack its mode line,
2641 (set-buffer (process-buffer proc))
2642 ;; Fix the mode line.
2643 (setq mode-line-process
2644 (concat ":"
2645 (symbol-name (process-status proc))))
2646 (force-mode-line-update)
2647 (if (eobp)
2648 (insert ?\n mode-name " " msg)
2649 (save-excursion
2650 (goto-char (point-max))
2651 (insert ?\n mode-name " " msg)))
2652 ;; If buffer and mode line will show that the process
2653 ;; is dead, we can delete it now. Otherwise it
2654 ;; will stay around until M-x list-processes.
2655 (delete-process proc))
2656 ;; Restore old buffer, but don't restore old point
2657 ;; if obuf is the gud buffer.
2658 (set-buffer obuf))))))
2659
2660 (defun gud-kill-buffer-hook ()
2661 (setq gud-minor-mode-type gud-minor-mode)
2662 (condition-case nil
2663 (progn
2664 (kill-process (get-buffer-process (current-buffer)))
2665 (delete-process (get-process "gdb-inferior")))
2666 (error nil)))
2667
2668 (defun gud-reset ()
2669 (dolist (buffer (buffer-list))
2670 (unless (eq buffer gud-comint-buffer)
2671 (with-current-buffer buffer
2672 (when gud-minor-mode
2673 (setq gud-minor-mode nil)
2674 (kill-local-variable 'tool-bar-map))))))
2675
2676 (defun gud-display-frame ()
2677 "Find and obey the last filename-and-line marker from the debugger.
2678 Obeying it means displaying in another window the specified file and line."
2679 (interactive)
2680 (when gud-last-frame
2681 (gud-set-buffer)
2682 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2683 (setq gud-last-last-frame gud-last-frame
2684 gud-last-frame nil)))
2685
2686 (declare-function global-hl-line-highlight "hl-line" ())
2687 (declare-function hl-line-highlight "hl-line" ())
2688 (declare-function gdb-display-source-buffer "gdb-mi" (buffer))
2689
2690 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2691 ;; and that its line LINE is visible.
2692 ;; Put the overlay-arrow on the line LINE in that buffer.
2693 ;; Most of the trickiness in here comes from wanting to preserve the current
2694 ;; region-restriction if that's possible. We use an explicit display-buffer
2695 ;; to get around the fact that this is called inside a save-excursion.
2696
2697 (defun gud-display-line (true-file line)
2698 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2699 (buffer
2700 (with-current-buffer gud-comint-buffer
2701 (gud-find-file true-file)))
2702 (window (and buffer
2703 (or (get-buffer-window buffer)
2704 (display-buffer buffer))))
2705 (pos))
2706 (when buffer
2707 (with-current-buffer buffer
2708 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2709 (if (yes-or-no-p
2710 (format "File %s changed on disk. Reread from disk? "
2711 (buffer-name)))
2712 (revert-buffer t t)
2713 (setq gud-keep-buffer t)))
2714 (save-restriction
2715 (widen)
2716 (goto-char (point-min))
2717 (forward-line (1- line))
2718 (setq pos (point))
2719 (or gud-overlay-arrow-position
2720 (setq gud-overlay-arrow-position (make-marker)))
2721 (set-marker gud-overlay-arrow-position (point) (current-buffer))
2722 ;; If they turned on hl-line, move the hl-line highlight to
2723 ;; the arrow's line.
2724 (when (featurep 'hl-line)
2725 (cond
2726 (global-hl-line-mode
2727 (global-hl-line-highlight))
2728 ((and hl-line-mode hl-line-sticky-flag)
2729 (hl-line-highlight)))))
2730 (cond ((or (< pos (point-min)) (> pos (point-max)))
2731 (widen)
2732 (goto-char pos))))
2733 (when window
2734 (set-window-point window gud-overlay-arrow-position)
2735 (if (eq gud-minor-mode 'gdbmi)
2736 (setq gdb-source-window window))))))
2737
2738 ;; The gud-call function must do the right thing whether its invoking
2739 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2740 ;; or a C buffer. In the former case, we want to supply data from
2741 ;; gud-last-frame. Here's how we do it:
2742
2743 (defun gud-format-command (str arg)
2744 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2745 (frame (or gud-last-frame gud-last-last-frame))
2746 result)
2747 (while (and str
2748 (let ((case-fold-search nil))
2749 (string-match "\\([^%]*\\)%\\([adefFlpc]\\)" str)))
2750 (let ((key (string-to-char (match-string 2 str)))
2751 subst)
2752 (cond
2753 ((eq key ?f)
2754 (setq subst (file-name-nondirectory (if insource
2755 (buffer-file-name)
2756 (car frame)))))
2757 ((eq key ?F)
2758 (setq subst (file-name-base (if insource
2759 (buffer-file-name)
2760 (car frame)))))
2761 ((eq key ?d)
2762 (setq subst (file-name-directory (if insource
2763 (buffer-file-name)
2764 (car frame)))))
2765 ((eq key ?l)
2766 (setq subst (int-to-string
2767 (if insource
2768 (save-restriction
2769 (widen)
2770 (+ (count-lines (point-min) (point))
2771 (if (bolp) 1 0)))
2772 (cdr frame)))))
2773 ((eq key ?e)
2774 (setq subst (gud-find-expr)))
2775 ((eq key ?a)
2776 (setq subst (gud-read-address)))
2777 ((eq key ?c)
2778 (setq subst
2779 (gud-find-class
2780 (if insource
2781 (buffer-file-name)
2782 (car frame))
2783 (if insource
2784 (save-restriction
2785 (widen)
2786 (+ (count-lines (point-min) (point))
2787 (if (bolp) 1 0)))
2788 (cdr frame)))))
2789 ((eq key ?p)
2790 (setq subst (if arg (int-to-string arg)))))
2791 (setq result (concat result (match-string 1 str) subst)))
2792 (setq str (substring str (match-end 2))))
2793 ;; There might be text left in STR when the loop ends.
2794 (concat result str)))
2795
2796 (defun gud-read-address ()
2797 "Return a string containing the core-address found in the buffer at point."
2798 (save-match-data
2799 (save-excursion
2800 (let ((pt (point)) found begin)
2801 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2802 (cond
2803 (found (forward-char 2)
2804 (buffer-substring found
2805 (progn (re-search-forward "[^0-9a-f]")
2806 (forward-char -1)
2807 (point))))
2808 (t (setq begin (progn (re-search-backward "[^0-9]")
2809 (forward-char 1)
2810 (point)))
2811 (forward-char 1)
2812 (re-search-forward "[^0-9]")
2813 (forward-char -1)
2814 (buffer-substring begin (point))))))))
2815
2816 (defun gud-call (fmt &optional arg)
2817 (let ((msg (gud-format-command fmt arg)))
2818 (message "Command: %s" msg)
2819 (sit-for 0)
2820 (gud-basic-call msg)))
2821
2822 (defun gud-basic-call (command)
2823 "Invoke the debugger COMMAND displaying source in other window."
2824 (interactive)
2825 (gud-set-buffer)
2826 (let ((proc (get-buffer-process gud-comint-buffer)))
2827 (or proc (error "Current buffer has no process"))
2828 ;; Arrange for the current prompt to get deleted.
2829 (with-current-buffer gud-comint-buffer
2830 (save-excursion
2831 (save-restriction
2832 (widen)
2833 (if (marker-position gud-delete-prompt-marker)
2834 ;; We get here when printing an expression.
2835 (goto-char gud-delete-prompt-marker)
2836 (goto-char (process-mark proc))
2837 (forward-line 0))
2838 (if (looking-at comint-prompt-regexp)
2839 (set-marker gud-delete-prompt-marker (point)))
2840 (if (eq gud-minor-mode 'gdbmi)
2841 (apply comint-input-sender (list proc command))
2842 (process-send-string proc (concat command "\n"))))))))
2843
2844 (defun gud-refresh (&optional arg)
2845 "Fix up a possibly garbled display, and redraw the arrow."
2846 (interactive "P")
2847 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2848 (gud-display-frame)
2849 (recenter arg))
2850 \f
2851 ;; Code for parsing expressions out of C or Fortran code. The single entry
2852 ;; point is gud-find-expr, which tries to return an lvalue expression from
2853 ;; around point.
2854
2855 (defvar gud-find-expr-function 'gud-find-c-expr)
2856
2857 (defun gud-find-expr (&rest args)
2858 (let ((expr (if (and transient-mark-mode mark-active)
2859 (buffer-substring (region-beginning) (region-end))
2860 (apply gud-find-expr-function args))))
2861 (save-match-data
2862 (if (string-match "\n" expr)
2863 (error "Expression must not include a newline"))
2864 (with-current-buffer gud-comint-buffer
2865 (save-excursion
2866 (goto-char (process-mark (get-buffer-process gud-comint-buffer)))
2867 (forward-line 0)
2868 (when (looking-at comint-prompt-regexp)
2869 (set-marker gud-delete-prompt-marker (point))
2870 (set-marker-insertion-type gud-delete-prompt-marker t))
2871 (unless (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
2872 'jdb)
2873 (insert (concat expr " = "))))))
2874 expr))
2875
2876 ;; The next eight functions are hacked from gdbsrc.el by
2877 ;; Debby Ayers <ayers@asc.slb.com>,
2878 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2879
2880 (defun gud-find-c-expr ()
2881 "Returns the expr that surrounds point."
2882 (interactive)
2883 (save-excursion
2884 (let ((p (point))
2885 (expr (gud-innermost-expr))
2886 (test-expr (gud-prev-expr)))
2887 (while (and test-expr (gud-expr-compound test-expr expr))
2888 (let ((prev-expr expr))
2889 (setq expr (cons (car test-expr) (cdr expr)))
2890 (goto-char (car expr))
2891 (setq test-expr (gud-prev-expr))
2892 ;; If we just pasted on the condition of an if or while,
2893 ;; throw it away again.
2894 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2895 '("if" "while" "for"))
2896 (setq test-expr nil
2897 expr prev-expr))))
2898 (goto-char p)
2899 (setq test-expr (gud-next-expr))
2900 (while (gud-expr-compound expr test-expr)
2901 (setq expr (cons (car expr) (cdr test-expr)))
2902 (setq test-expr (gud-next-expr)))
2903 (buffer-substring (car expr) (cdr expr)))))
2904
2905 (defun gud-innermost-expr ()
2906 "Returns the smallest expr that point is in; move point to beginning of it.
2907 The expr is represented as a cons cell, where the car specifies the point in
2908 the current buffer that marks the beginning of the expr and the cdr specifies
2909 the character after the end of the expr."
2910 (let ((p (point)) begin end)
2911 (gud-backward-sexp)
2912 (setq begin (point))
2913 (gud-forward-sexp)
2914 (setq end (point))
2915 (if (>= p end)
2916 (progn
2917 (setq begin p)
2918 (goto-char p)
2919 (gud-forward-sexp)
2920 (setq end (point)))
2921 )
2922 (goto-char begin)
2923 (cons begin end)))
2924
2925 (defun gud-backward-sexp ()
2926 "Version of `backward-sexp' that catches errors."
2927 (condition-case nil
2928 (backward-sexp)
2929 (error t)))
2930
2931 (defun gud-forward-sexp ()
2932 "Version of `forward-sexp' that catches errors."
2933 (condition-case nil
2934 (forward-sexp)
2935 (error t)))
2936
2937 (defun gud-prev-expr ()
2938 "Returns the previous expr, point is set to beginning of that expr.
2939 The expr is represented as a cons cell, where the car specifies the point in
2940 the current buffer that marks the beginning of the expr and the cdr specifies
2941 the character after the end of the expr"
2942 (let ((begin) (end))
2943 (gud-backward-sexp)
2944 (setq begin (point))
2945 (gud-forward-sexp)
2946 (setq end (point))
2947 (goto-char begin)
2948 (cons begin end)))
2949
2950 (defun gud-next-expr ()
2951 "Returns the following expr, point is set to beginning of that expr.
2952 The expr is represented as a cons cell, where the car specifies the point in
2953 the current buffer that marks the beginning of the expr and the cdr specifies
2954 the character after the end of the expr."
2955 (let ((begin) (end))
2956 (gud-forward-sexp)
2957 (gud-forward-sexp)
2958 (setq end (point))
2959 (gud-backward-sexp)
2960 (setq begin (point))
2961 (cons begin end)))
2962
2963 (defun gud-expr-compound-sep (span-start span-end)
2964 "Scan from SPAN-START to SPAN-END for punctuation characters.
2965 If `->' is found, return `?.'. If `.' is found, return `?.'.
2966 If any other punctuation is found, return `??'.
2967 If no punctuation is found, return `? '."
2968 (let ((result ?\s)
2969 (syntax))
2970 (while (< span-start span-end)
2971 (setq syntax (char-syntax (char-after span-start)))
2972 (cond
2973 ((= syntax ?\s) t)
2974 ((= syntax ?.) (setq syntax (char-after span-start))
2975 (cond
2976 ((= syntax ?.) (setq result ?.))
2977 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2978 (setq result ?.)
2979 (setq span-start (+ span-start 1)))
2980 (t (setq span-start span-end)
2981 (setq result ??)))))
2982 (setq span-start (+ span-start 1)))
2983 result))
2984
2985 (defun gud-expr-compound (first second)
2986 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2987 The two exprs are represented as a cons cells, where the car
2988 specifies the point in the current buffer that marks the beginning of the
2989 expr and the cdr specifies the character after the end of the expr.
2990 Link exprs of the form:
2991 Expr -> Expr
2992 Expr . Expr
2993 Expr (Expr)
2994 Expr [Expr]
2995 (Expr) Expr
2996 [Expr] Expr"
2997 (let ((span-start (cdr first))
2998 (span-end (car second))
2999 (syntax))
3000 (setq syntax (gud-expr-compound-sep span-start span-end))
3001 (cond
3002 ((= (car first) (car second)) nil)
3003 ((= (cdr first) (cdr second)) nil)
3004 ((= syntax ?.) t)
3005 ((= syntax ?\s)
3006 (setq span-start (char-after (- span-start 1)))
3007 (setq span-end (char-after span-end))
3008 (cond
3009 ((= span-start ?)) t)
3010 ((= span-start ?]) t)
3011 ((= span-end ?() t)
3012 ((= span-end ?[) t)
3013 (t nil)))
3014 (t nil))))
3015
3016
3017 (declare-function c-langelem-sym "cc-defs" (langelem))
3018 (declare-function c-langelem-pos "cc-defs" (langelem))
3019
3020 (defun gud-find-class (f _line)
3021 "Find fully qualified class in file F at line LINE.
3022 This function uses the `gud-jdb-classpath' (and optional
3023 `gud-jdb-sourcepath') list(s) to derive a file
3024 pathname relative to its classpath directory. The values in
3025 `gud-jdb-classpath' are assumed to have been converted to absolute
3026 pathname standards using file-truename.
3027 If F is visited by a buffer and its mode is CC-mode(Java),
3028 syntactic information of LINE is used to find the enclosing (nested)
3029 class string which is appended to the top level
3030 class of the file (using s to separate nested class ids)."
3031 ;; Convert f to a standard representation and remove suffix
3032 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
3033 (save-match-data
3034 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
3035 (fbuffer (get-file-buffer f))
3036 class-found
3037 ;; Syntax-symbol returns the symbol of the *first* element
3038 ;; in the syntactical analysis result list, syntax-point
3039 ;; returns the buffer position of same
3040 (syntax-symbol (lambda (x) (c-langelem-sym (car x))))
3041 (syntax-point (lambda (x) (c-langelem-pos (car x)))))
3042 (setq f (file-name-sans-extension (file-truename f)))
3043 ;; Search through classpath list for an entry that is
3044 ;; contained in f
3045 (while (and cplist (not class-found))
3046 (if (string-match (car cplist) f)
3047 (setq class-found
3048 (mapconcat 'identity
3049 (split-string
3050 (substring f (+ (match-end 0) 1))
3051 "/") ".")))
3052 (setq cplist (cdr cplist)))
3053 ;; if f is visited by a java(cc-mode) buffer, walk up the
3054 ;; syntactic information chain and collect any 'inclass
3055 ;; symbols until 'topmost-intro is reached to find out if
3056 ;; point is within a nested class
3057 ;; FIXME: Yuck!!! cc-mode should provide a function instead.
3058 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
3059 (with-current-buffer fbuffer
3060 (let ((nclass) (syntax))
3061 ;; While the c-syntactic information does not start
3062 ;; with the 'topmost-intro symbol, there may be
3063 ;; nested classes...
3064 (while (not (eq 'topmost-intro
3065 (funcall syntax-symbol (c-guess-basic-syntax))))
3066 ;; Check if the current position c-syntactic
3067 ;; analysis has 'inclass
3068 (setq syntax (c-guess-basic-syntax))
3069 (while
3070 (and (not (eq 'inclass (funcall syntax-symbol syntax)))
3071 (cdr syntax))
3072 (setq syntax (cdr syntax)))
3073 (if (eq 'inclass (funcall syntax-symbol syntax))
3074 (progn
3075 (goto-char (funcall syntax-point syntax))
3076 ;; Now we're at the beginning of a class
3077 ;; definition. Find class name
3078 (looking-at
3079 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
3080 (setq nclass
3081 (append (list (match-string-no-properties 1))
3082 nclass)))
3083 (setq syntax (c-guess-basic-syntax))
3084 (while (and (not (funcall syntax-point syntax)) (cdr syntax))
3085 (setq syntax (cdr syntax)))
3086 (goto-char (funcall syntax-point syntax))
3087 ))
3088 (string-match (concat (car nclass) "$") class-found)
3089 (setq class-found
3090 (replace-match (mapconcat 'identity nclass "$")
3091 t t class-found)))))
3092 (if (not class-found)
3093 (message "gud-find-class: class for file %s not found!" f))
3094 class-found))
3095 ;; Not using classpath - try class/source association list
3096 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
3097 (if class-found
3098 (car class-found)
3099 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
3100 nil))))
3101
3102 \f
3103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3104 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3105 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3106
3107 (defvar gdb-script-mode-syntax-table
3108 (let ((st (make-syntax-table)))
3109 (modify-syntax-entry ?' "\"" st)
3110 (modify-syntax-entry ?# "<" st)
3111 (modify-syntax-entry ?\n ">" st)
3112 st))
3113
3114 (defvar gdb-script-font-lock-keywords
3115 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
3116 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
3117 ("^\\s-*\\(\\w\\(\\w\\|\\s_\\)*\\)" (1 font-lock-keyword-face))))
3118
3119 (defconst gdb-script-syntax-propertize-function
3120 (syntax-propertize-rules
3121 ("^document\\s-.*\\(\n\\)" (1 "< b"))
3122 ("^end\\(\\>\\)"
3123 (1 (ignore
3124 (when (and (> (match-beginning 0) (point-min))
3125 (eq 1 (nth 7 (save-excursion
3126 (syntax-ppss (1- (match-beginning 0)))))))
3127 ;; We change the \n in front, which is more difficult, but results
3128 ;; in better highlighting. If the doc is empty, the single \n is
3129 ;; both the beginning and the end of the docstring, which can't be
3130 ;; expressed in syntax-tables. Instead, we place the "> b" after
3131 ;; placing the "< b", so the start marker is overwritten by the
3132 ;; termination marker and in the end Emacs simply considers that
3133 ;; there's no docstring at all, which is fine.
3134 (put-text-property (1- (match-beginning 0)) (match-beginning 0)
3135 'syntax-table (eval-when-compile
3136 (string-to-syntax "> b")))
3137 ;; Make sure that rehighlighting the previous line won't erase our
3138 ;; syntax-table property and that modifying `end' will.
3139 (put-text-property (1- (match-beginning 0)) (match-end 0)
3140 'syntax-multiline t)))))))
3141
3142 (defun gdb-script-font-lock-syntactic-face (state)
3143 (cond
3144 ((nth 3 state) font-lock-string-face)
3145 ((nth 7 state) font-lock-doc-face)
3146 (t font-lock-comment-face)))
3147
3148 (defvar gdb-script-basic-indent 2)
3149
3150 (defun gdb-script-skip-to-head ()
3151 "We're just in front of an `end' and we need to go to its head."
3152 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\|commands\\)\\>" nil 'move)
3153 (match-end 2))
3154 (gdb-script-skip-to-head)))
3155
3156 (defun gdb-script-calculate-indentation ()
3157 (cond
3158 ((looking-at "end\\>")
3159 (gdb-script-skip-to-head)
3160 (current-indentation))
3161 ((looking-at "else\\>")
3162 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3163 (match-end 2))
3164 (gdb-script-skip-to-head))
3165 (current-indentation))
3166 (t
3167 (forward-comment (- (point-max)))
3168 (forward-line 0)
3169 (skip-chars-forward " \t")
3170 (+ (current-indentation)
3171 (if (looking-at "\\(if\\|while\\|define\\|else\\|commands\\)\\>")
3172 gdb-script-basic-indent 0)))))
3173
3174 (defun gdb-script-indent-line ()
3175 "Indent current line of GDB script."
3176 (interactive)
3177 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3178 (save-excursion
3179 (forward-line 0)
3180 (skip-chars-forward " \t")
3181 (not (looking-at "end\\>"))))
3182 'noindent
3183 (let* ((savep (point))
3184 (indent (condition-case nil
3185 (save-excursion
3186 (forward-line 0)
3187 (skip-chars-forward " \t")
3188 (if (>= (point) savep) (setq savep nil))
3189 (max (gdb-script-calculate-indentation) 0))
3190 (error 0))))
3191 (if savep
3192 (save-excursion (indent-line-to indent))
3193 (indent-line-to indent)))))
3194
3195 ;; Derived from cfengine.el.
3196 (defun gdb-script-beginning-of-defun ()
3197 "`beginning-of-defun' function for Gdb script mode.
3198 Treats actions as defuns."
3199 (unless (<= (current-column) (current-indentation))
3200 (end-of-line))
3201 (if (re-search-backward "^define \\|^document " nil t)
3202 (beginning-of-line)
3203 (goto-char (point-min)))
3204 t)
3205
3206 ;; Derived from cfengine.el.
3207 (defun gdb-script-end-of-defun ()
3208 "`end-of-defun' function for Gdb script mode.
3209 Treats actions as defuns."
3210 (end-of-line)
3211 (if (re-search-forward "^end" nil t)
3212 (beginning-of-line)
3213 (goto-char (point-max)))
3214 t)
3215
3216 ;;;###autoload
3217 (define-derived-mode gdb-script-mode prog-mode "GDB-Script"
3218 "Major mode for editing GDB scripts."
3219 (set (make-local-variable 'comment-start) "#")
3220 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3221 (set (make-local-variable 'outline-regexp) "[ \t]")
3222 (set (make-local-variable 'imenu-generic-expression)
3223 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3224 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3225 (set (make-local-variable 'beginning-of-defun-function)
3226 #'gdb-script-beginning-of-defun)
3227 (set (make-local-variable 'end-of-defun-function)
3228 #'gdb-script-end-of-defun)
3229 (set (make-local-variable 'font-lock-defaults)
3230 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3231 (font-lock-syntactic-face-function
3232 . gdb-script-font-lock-syntactic-face)))
3233 ;; Recognize docstrings.
3234 (set (make-local-variable 'syntax-propertize-function)
3235 gdb-script-syntax-propertize-function)
3236 (add-hook 'syntax-propertize-extend-region-functions
3237 #'syntax-propertize-multiline 'append 'local))
3238
3239 \f
3240 ;;; tooltips for GUD
3241
3242 ;;; Customizable settings
3243
3244 ;;;###autoload
3245 (define-minor-mode gud-tooltip-mode
3246 "Toggle the display of GUD tooltips.
3247 With a prefix argument ARG, enable the feature if ARG is
3248 positive, and disable it otherwise. If called from Lisp, enable
3249 it if ARG is omitted or nil."
3250 :global t
3251 :group 'gud
3252 :group 'tooltip
3253 (require 'tooltip)
3254 (if gud-tooltip-mode
3255 (progn
3256 (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3257 (add-hook 'pre-command-hook 'tooltip-hide)
3258 (add-hook 'tooltip-functions 'gud-tooltip-tips)
3259 (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
3260 (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
3261 (remove-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3262 (remove-hook 'tooltip-functions 'gud-tooltip-tips)
3263 (define-key global-map [mouse-movement] 'ignore)))
3264 (gud-tooltip-activate-mouse-motions-if-enabled)
3265 (if (and gud-comint-buffer
3266 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
3267 (eq (buffer-local-value 'gud-minor-mode gud-comint-buffer)
3268 'gdbmi))
3269 (if gud-tooltip-mode
3270 (progn
3271 (dolist (buffer (buffer-list))
3272 (unless (eq buffer gud-comint-buffer)
3273 (with-current-buffer buffer
3274 (when (and (eq gud-minor-mode 'gdbmi)
3275 (not (string-match "\\`\\*.+\\*\\'"
3276 (buffer-name))))
3277 (make-local-variable 'gdb-define-alist)
3278 (gdb-create-define-alist)
3279 (add-hook 'after-save-hook
3280 'gdb-create-define-alist nil t))))))
3281 (kill-local-variable 'gdb-define-alist)
3282 (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
3283
3284 (define-obsolete-variable-alias 'tooltip-gud-modes
3285 'gud-tooltip-modes "22.1")
3286
3287 (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode
3288 python-mode)
3289 "List of modes for which to enable GUD tooltips."
3290 :type 'sexp
3291 :group 'gud
3292 :group 'tooltip)
3293
3294 (define-obsolete-variable-alias 'tooltip-gud-display
3295 'gud-tooltip-display "22.1")
3296
3297 (defcustom gud-tooltip-display
3298 '((eq (tooltip-event-buffer gud-tooltip-event)
3299 (marker-buffer gud-overlay-arrow-position)))
3300 "List of forms determining where GUD tooltips are displayed.
3301
3302 Forms in the list are combined with AND. The default is to display
3303 only tooltips in the buffer containing the overlay arrow."
3304 :type 'sexp
3305 :group 'gud
3306 :group 'tooltip)
3307
3308 (defcustom gud-tooltip-echo-area nil
3309 "Use the echo area instead of frames for GUD tooltips."
3310 :type 'boolean
3311 :group 'gud
3312 :group 'tooltip)
3313
3314 ;;; Reacting on mouse movements
3315
3316 (defun gud-tooltip-change-major-mode ()
3317 "Function added to `change-major-mode-hook' when tooltip mode is on."
3318 (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
3319
3320 (defun gud-tooltip-activate-mouse-motions-if-enabled ()
3321 "Reconsider for all buffers whether mouse motion events are desired."
3322 (remove-hook 'post-command-hook
3323 'gud-tooltip-activate-mouse-motions-if-enabled)
3324 (dolist (buffer (buffer-list))
3325 (with-current-buffer buffer
3326 (if (and gud-tooltip-mode
3327 (memq major-mode gud-tooltip-modes))
3328 (gud-tooltip-activate-mouse-motions t)
3329 (gud-tooltip-activate-mouse-motions nil)))))
3330
3331 (defvar gud-tooltip-mouse-motions-active nil
3332 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
3333
3334 ;; We don't set track-mouse globally because this is a big redisplay
3335 ;; problem in buffers having a pre-command-hook or such installed,
3336 ;; which does a set-buffer, like the summary buffer of Gnus. Calling
3337 ;; set-buffer prevents redisplay optimizations, so every mouse motion
3338 ;; would be accompanied by a full redisplay.
3339
3340 (defun gud-tooltip-activate-mouse-motions (activatep)
3341 "Activate/deactivate mouse motion events for the current buffer.
3342 ACTIVATEP non-nil means activate mouse motion events."
3343 (if activatep
3344 (progn
3345 (set (make-local-variable 'gud-tooltip-mouse-motions-active) t)
3346 (set (make-local-variable 'track-mouse) t))
3347 (when gud-tooltip-mouse-motions-active
3348 (kill-local-variable 'gud-tooltip-mouse-motions-active)
3349 (kill-local-variable 'track-mouse))))
3350
3351 (defvar tooltip-last-mouse-motion-event)
3352 (declare-function tooltip-hide "tooltip" (&optional ignored-arg))
3353 (declare-function tooltip-start-delayed-tip "tooltip" ())
3354
3355 (defun gud-tooltip-mouse-motion (event)
3356 "Command handler for mouse movement events in `global-map'."
3357 (interactive "e")
3358 (tooltip-hide)
3359 (when (car (mouse-pixel-position))
3360 (setq tooltip-last-mouse-motion-event (copy-sequence event))
3361 (tooltip-start-delayed-tip)))
3362
3363 ;;; Tips for `gud'
3364
3365 (defvar gud-tooltip-original-filter nil
3366 "Process filter to restore after GUD output has been received.")
3367
3368 (defvar gud-tooltip-dereference nil
3369 "Non-nil means print expressions with a `*' in front of them.
3370 For C this would dereference a pointer expression.")
3371
3372 (defvar gud-tooltip-event nil
3373 "The mouse movement event that led to a tooltip display.
3374 This event can be examined by forms in `gud-tooltip-display'.")
3375
3376 (defun gud-tooltip-dereference (&optional arg)
3377 "Toggle whether tooltips should show `* expr' or `expr'.
3378 With arg, dereference expr if ARG is positive, otherwise do not dereference."
3379 (interactive "P")
3380 (setq gud-tooltip-dereference
3381 (if (null arg)
3382 (not gud-tooltip-dereference)
3383 (> (prefix-numeric-value arg) 0)))
3384 (message "Dereferencing is now %s."
3385 (if gud-tooltip-dereference "on" "off")))
3386
3387 (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
3388 'gud-tooltip-dereference "22.1")
3389 (defvar tooltip-use-echo-area)
3390 (declare-function tooltip-show "tooltip" (text &optional use-echo-area))
3391 (declare-function tooltip-strip-prompt "tooltip" (process output))
3392
3393 ; This will only display data that comes in one chunk.
3394 ; Larger arrays (say 400 elements) are displayed in
3395 ; the tooltip incompletely and spill over into the gud buffer.
3396 ; Switching the process-filter creates timing problems and
3397 ; it may be difficult to do better. Using GDB/MI as in
3398 ; gdb-mi.el gets round this problem.
3399 (defun gud-tooltip-process-output (process output)
3400 "Process debugger output and show it in a tooltip window."
3401 (set-process-filter process gud-tooltip-original-filter)
3402 (tooltip-show (tooltip-strip-prompt process output)
3403 (or gud-tooltip-echo-area tooltip-use-echo-area)))
3404
3405 (defun gud-tooltip-print-command (expr)
3406 "Return a suitable command to print the expression EXPR."
3407 (pcase gud-minor-mode
3408 (`gdbmi (concat "-data-evaluate-expression \"" expr "\""))
3409 (`dbx (concat "print " expr))
3410 ((or `xdb `pdb) (concat "p " expr))
3411 (`sdb (concat expr "/"))))
3412
3413 (declare-function gdb-input "gdb-mi" (command handler))
3414 (declare-function tooltip-expr-to-print "tooltip" (event))
3415 (declare-function tooltip-event-buffer "tooltip" (event))
3416
3417 (defun gud-tooltip-tips (event)
3418 "Show tip for identifier or selection under the mouse.
3419 The mouse must either point at an identifier or inside a selected
3420 region for the tip window to be shown. If `gud-tooltip-dereference' is t,
3421 add a `*' in front of the printed expression. In the case of a C program
3422 controlled by GDB, show the associated #define directives when program is
3423 not executing.
3424
3425 This function must return nil if it doesn't handle EVENT."
3426 (let (process)
3427 (when (and (eventp event)
3428 gud-tooltip-mode
3429 gud-comint-buffer
3430 (buffer-name gud-comint-buffer); might be killed
3431 (setq process (get-buffer-process gud-comint-buffer))
3432 (posn-point (event-end event))
3433 (or (and (eq gud-minor-mode 'gdbmi) (not gdb-active-process))
3434 (progn (setq gud-tooltip-event event)
3435 (eval (cons 'and gud-tooltip-display)))))
3436 (let ((expr (tooltip-expr-to-print event)))
3437 (when expr
3438 (if (and (eq gud-minor-mode 'gdbmi)
3439 (not gdb-active-process))
3440 (progn
3441 (with-current-buffer (tooltip-event-buffer event)
3442 (let ((define-elt (assoc expr gdb-define-alist)))
3443 (unless (null define-elt)
3444 (tooltip-show
3445 (cdr define-elt)
3446 (or gud-tooltip-echo-area tooltip-use-echo-area))
3447 expr))))
3448 (when gud-tooltip-dereference
3449 (setq expr (concat "*" expr)))
3450 (let ((cmd (gud-tooltip-print-command expr)))
3451 (when (and gud-tooltip-mode (eq gud-minor-mode 'gdb))
3452 (gud-tooltip-mode -1)
3453 ;; The blank before the newline is for MS-Windows,
3454 ;; whose emulation of message box removes newlines and
3455 ;; displays a single long line.
3456 (message-box "Using GUD tooltips in this mode is unsafe \n\
3457 so they have been disabled."))
3458 (unless (null cmd) ; CMD can be nil if unknown debugger
3459 (if (eq gud-minor-mode 'gdbmi)
3460 (if gdb-macro-info
3461 (gdb-input
3462 (concat
3463 "server macro expand " expr "\n")
3464 `(lambda () (gdb-tooltip-print-1 ,expr)))
3465 (gdb-input
3466 (concat cmd "\n")
3467 `(lambda () (gdb-tooltip-print ,expr))))
3468 (setq gud-tooltip-original-filter (process-filter process))
3469 (set-process-filter process 'gud-tooltip-process-output)
3470 (gud-basic-call cmd))
3471 expr))))))))
3472
3473 (provide 'gud)
3474
3475 ;;; gud.el ends here