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