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