1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992,93,94,95,96,1998,2000,02,03,04 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38 ;; <ddavies@world.std.com> added support for jdb (Java debugger.)
42 (eval-when-compile (require 'cl)) ; for case macro
47 ;; ======================================================================
48 ;; GUD commands must be visible in C buffers visited by GUD
51 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
52 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
57 (defcustom gud-key-prefix "\C-x\C-a"
58 "Prefix of all GUD commands valid in C buffers."
62 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
63 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
65 (defvar gud-marker-filter nil)
66 (put 'gud-marker-filter 'permanent-local t)
67 (defvar gud-find-file nil)
68 (put 'gud-find-file 'permanent-local t)
70 (defun gud-marker-filter (&rest args)
71 (apply gud-marker-filter args))
73 (defvar gud-minor-mode nil)
74 (put 'gud-minor-mode 'permanent-local t)
76 (defvar gud-keep-buffer nil)
78 (defun gud-symbol (sym &optional soft minor-mode)
79 "Return the symbol used for SYM in MINOR-MODE.
80 MINOR-MODE defaults to `gud-minor-mode.
81 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
82 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
83 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
84 (funcall (if soft 'intern-soft 'intern)
85 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
87 (defun gud-val (sym &optional minor-mode)
88 "Return the value of `gud-symbol' SYM. Default to nil."
89 (let ((sym (gud-symbol sym t minor-mode)))
90 (if (boundp sym) (symbol-value sym))))
92 (defvar gud-running nil
93 "Non-nil if debuggee is running.
94 Used to grey out relevant togolbar icons.")
96 ;; Use existing Info buffer, if possible.
97 (defun gud-goto-info ()
98 "Go to relevant Emacs info node."
100 (let ((same-window-regexps same-window-regexps)
101 (display-buffer-reuse-frames t))
105 (if (eq (window-buffer window) (get-buffer "*info*"))
107 (setq same-window-regexps nil)
108 (throw 'info-found nil))))
111 (select-frame (make-frame)))
112 (if (memq gud-minor-mode '(gdbmi gdba))
113 (Info-goto-node "(emacs)GDB Graphical Interface")
114 (Info-goto-node "(emacs)Debuggers"))))
116 (easy-mmode-defmap gud-menu-map
117 '(([help] "Info" . gud-goto-info)
118 ([tooltips] menu-item "Toggle GUD tooltips" gud-tooltip-mode
119 :enable (and (not emacs-basic-display)
121 (fboundp 'x-show-tip))
122 :button (:toggle . gud-tooltip-mode))
123 ([refresh] "Refresh" . gud-refresh)
124 ([run] menu-item "Run" gud-run
125 :enable (and (not gud-running)
126 (memq gud-minor-mode '(gdbmi gdba gdb dbx jdb))))
127 ([until] menu-item "Continue to selection" gud-until
128 :enable (and (not gud-running)
129 (memq gud-minor-mode '(gdbmi gdba gdb perldb))))
130 ([remove] menu-item "Remove Breakpoint" gud-remove
131 :enable (not gud-running))
132 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
133 :enable (memq gud-minor-mode '(gdbmi gdba gdb sdb xdb bashdb)))
134 ([break] menu-item "Set Breakpoint" gud-break
135 :enable (not gud-running))
136 ([up] menu-item "Up Stack" gud-up
137 :enable (and (not gud-running)
139 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
140 ([down] menu-item "Down Stack" gud-down
141 :enable (and (not gud-running)
143 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
144 ([print] menu-item "Print Expression" gud-print
145 :enable (not gud-running))
146 ([watch] menu-item "Watch Expression" gud-watch
147 :enable (and (not gud-running)
148 (memq gud-minor-mode '(gdbmi gdba))))
149 ([finish] menu-item "Finish Function" gud-finish
150 :enable (and (not gud-running)
152 '(gdbmi gdba gdb xdb jdb pdb bashdb))))
153 ([stepi] menu-item "Step Instruction" gud-stepi
154 :enable (and (not gud-running)
155 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
156 ([nexti] menu-item "Next Instruction" gud-nexti
157 :enable (and (not gud-running)
158 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
159 ([step] menu-item "Step Line" gud-step
160 :enable (not gud-running))
161 ([next] menu-item "Next Line" gud-next
162 :enable (not gud-running))
163 ([cont] menu-item "Continue" gud-cont
164 :enable (not gud-running)))
165 "Menu for `gud-mode'."
168 (easy-mmode-defmap gud-minor-mode-map
169 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
170 "Map used in visited files.")
172 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
173 (if m (setcdr m gud-minor-mode-map)
174 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
177 ;; Will inherit from comint-mode via define-derived-mode.
179 "`gud-mode' keymap.")
181 (defvar gud-tool-bar-map
182 (if (display-graphic-p)
183 (let ((map (make-sparse-keymap)))
184 (dolist (x '((gud-break . "gud-break")
185 (gud-remove . "gud-remove")
186 (gud-print . "gud-print")
187 (gud-watch . "gud-watch")
188 (gud-run . "gud-run")
189 (gud-until . "gud-until")
190 (gud-cont . "gud-cont")
191 ;; gud-s, gud-si etc. instead of gud-step,
192 ;; gud-stepi, to avoid file-name clashes on DOS
196 (gud-finish . "gud-finish")
197 (gud-stepi . "gud-si")
198 (gud-nexti . "gud-ni")
200 (gud-down . "gud-down")
201 (gud-goto-info . "info"))
203 (tool-bar-local-item-from-menu
204 (car x) (cdr x) map gud-minor-mode-map)))))
206 (defun gud-file-name (f)
207 "Transform a relative file name to an absolute file name.
208 Uses `gud-<MINOR-MODE>-directories' to find the source files."
209 (if (file-exists-p f) (expand-file-name f)
210 (let ((directories (gud-val 'directories))
213 (let ((path (expand-file-name f (car directories))))
214 (if (file-exists-p path)
217 (setq directories (cdr directories)))
220 (defun gud-find-file (file)
221 ;; Don't get confused by double slashes in the name that comes from GDB.
222 (while (string-match "//+" file)
223 (setq file (replace-match "/" t t file)))
224 (let ((minor-mode gud-minor-mode)
225 (buf (funcall (or gud-find-file 'gud-file-name) file)))
227 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
229 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
230 (with-current-buffer buf
231 (set (make-local-variable 'gud-minor-mode) minor-mode)
232 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
233 (when (and gud-tooltip-mode
234 (memq gud-minor-mode '(gdbmi gdba)))
235 (make-local-variable 'gdb-define-alist)
236 (unless gdb-define-alist (gdb-create-define-alist))
237 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
238 (make-local-variable 'gud-keep-buffer))
241 ;; ======================================================================
242 ;; command definition
244 ;; This macro is used below to define some basic debugger interface commands.
245 ;; Of course you may use `gud-def' with any other debugger command, including
246 ;; user defined ones.
248 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
249 ;; which defines FUNC to send the command NAME to the debugger, gives
250 ;; it the docstring DOC, and binds that function to KEY in the GUD
251 ;; major mode. The function is also bound in the global keymap with the
254 (defmacro gud-def (func cmd key &optional doc)
255 "Define FUNC to be a command sending STR and bound to KEY, with
256 optional doc string DOC. Certain %-escapes in the string arguments
257 are interpreted specially if present. These are:
259 %f name (without directory) of current source file.
260 %F name (without directory or extension) of current source file.
261 %d directory of current source file.
262 %l number of current source line
263 %e text of the C lvalue or function-call expression surrounding point.
264 %a text of the hexadecimal address surrounding point
265 %p prefix argument to the command (if any) as a number
267 The `current' source file is the file of the current buffer (if
268 we're in a C file) or the source file current at the last break or
269 step (if we're in the GUD buffer).
270 The `current' line is that of the current buffer (if we're in a
271 source file) or the source line number at the last break or step (if
272 we're in the GUD buffer)."
275 ,@(if doc (list doc))
280 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
281 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
283 ;; Where gud-display-frame should put the debugging arrow; a cons of
284 ;; (filename . line-number). This is set by the marker-filter, which scans
285 ;; the debugger's output for indications of the current program counter.
286 (defvar gud-last-frame nil)
288 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
289 ;; the last frame, even if it's been called before and gud-last-frame has
291 (defvar gud-last-last-frame nil)
293 ;; All debugger-specific information is collected here.
294 ;; Here's how it works, in case you ever need to add a debugger to the mode.
296 ;; Each entry must define the following at startup:
299 ;; comint-prompt-regexp
300 ;; gud-<name>-massage-args
301 ;; gud-<name>-marker-filter
302 ;; gud-<name>-find-file
304 ;; The job of the massage-args method is to modify the given list of
305 ;; debugger arguments before running the debugger.
307 ;; The job of the marker-filter method is to detect file/line markers in
308 ;; strings and set the global gud-last-frame to indicate what display
309 ;; action (if any) should be triggered by the marker. Note that only
310 ;; whatever the method *returns* is displayed in the buffer; thus, you
311 ;; can filter the debugger's output, interpreting some and passing on
314 ;; The job of the find-file method is to visit and return the buffer indicated
315 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
318 ;; ======================================================================
319 ;; speedbar support functions and variables.
320 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
322 (defvar gud-last-speedbar-buffer nil
323 "The last GUD buffer used.")
325 (defvar gud-last-speedbar-stackframe nil
326 "Description of the currently displayed GUD stack.
327 t means that there is no stack, and we are in display-file mode.")
329 (defvar gud-speedbar-key-map nil
330 "Keymap used when in the buffers display mode.")
332 (defun gud-install-speedbar-variables ()
333 "Install those variables used by speedbar to enhance gud/gdb."
334 (if gud-speedbar-key-map
336 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
338 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
339 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
340 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
341 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)))
344 (defvar gud-speedbar-menu-items
345 ;; Note to self. Add expand, and turn off items when not available.
346 '(["Jump to stack frame" speedbar-edit-line
347 (with-current-buffer gud-comint-buffer
348 (not (memq gud-minor-mode '(gdbmi gdba))))]
349 ["Edit value" speedbar-edit-line
350 (with-current-buffer gud-comint-buffer
351 (not (memq gud-minor-mode '(gdbmi gdba))))]
352 ["Delete expression" gdb-var-delete
353 (with-current-buffer gud-comint-buffer
354 (not (memq gud-minor-mode '(gdbmi gdba))))])
355 "Additional menu items to add to the speedbar frame.")
357 ;; Make sure our special speedbar mode is loaded
358 (if (featurep 'speedbar)
359 (gud-install-speedbar-variables)
360 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
362 (defun gud-speedbar-buttons (buffer)
363 "Create a speedbar display based on the current state of GUD.
364 If the GUD BUFFER is not running a supported debugger, then turn
365 off the specialized speedbar mode."
366 (let ((minor-mode (with-current-buffer buffer gud-minor-mode)))
368 ((memq minor-mode '(gdbmi gdba))
369 (when (or gdb-var-changed
371 (goto-char (point-min))
372 (let ((case-fold-search t))
373 (looking-at "Watch Expressions:")))))
375 (insert "Watch Expressions:\n")
376 (let ((var-list gdb-var-list))
378 (let* ((depth 0) (start 0) (char ?+)
379 (var (car var-list)) (varnum (nth 1 var)))
380 (while (string-match "\\." varnum start)
381 (setq depth (1+ depth)
382 start (1+ (match-beginning 0))))
383 (if (equal (nth 2 var) "0")
384 (speedbar-make-tag-line 'bracket ?? nil nil
385 (concat (car var) "\t" (nth 4 var))
389 gdb-show-changed-values)
390 'font-lock-warning-face
392 (if (and (cadr var-list)
393 (string-match varnum (cadr (cadr var-list))))
395 (speedbar-make-tag-line 'bracket char
396 'gdb-speedbar-expand-node varnum
397 (concat (car var) "\t" (nth 3 var))
399 (setq var-list (cdr var-list))))
400 (setq gdb-var-changed nil)))
401 (t (if (and (save-excursion
402 (goto-char (point-min))
403 (looking-at "Current Stack"))
404 (equal gud-last-last-frame gud-last-speedbar-stackframe))
406 (setq gud-last-speedbar-buffer buffer)
407 (let ((gud-frame-list
408 (cond ((eq minor-mode 'gdb)
409 (gud-gdb-get-stackframe buffer))
410 ;; Add more debuggers here!
411 (t (speedbar-remove-localized-speedbar-support buffer)
414 (if (not gud-frame-list)
415 (insert "No Stack frames\n")
416 (insert "Current Stack:\n"))
417 (dolist (frame gud-frame-list)
418 (insert (nth 1 frame) ":\n")
419 (if (= (length frame) 2)
421 ; (speedbar-insert-button "[?]"
422 ; 'speedbar-button-face
424 (speedbar-insert-button (car frame)
425 'speedbar-directory-face
427 ; (speedbar-insert-button "[+]"
428 ; 'speedbar-button-face
429 ; 'speedbar-highlight-face
430 ; 'gud-gdb-get-scope-data
432 (speedbar-insert-button (car frame)
434 'speedbar-highlight-face
435 (cond ((memq minor-mode '(gdbmi gdba gdb))
436 'gud-gdb-goto-stackframe)
437 (t (error "Should never be here")))
439 ; (let ((selected-frame
440 ; (cond ((eq ff 'gud-gdb-find-file)
441 ; (gud-gdb-selected-frame-info buffer))
442 ; (t (error "Should never be here"))))))
444 (setq gud-last-speedbar-stackframe gud-last-last-frame))))))
447 ;; ======================================================================
450 ;; History of argument lists passed to gdb.
451 (defvar gud-gdb-history nil)
453 (defcustom gud-gdb-command-name "gdb --annotate=3"
454 "Default command to execute an executable under the GDB debugger."
458 (defvar gud-gdb-marker-regexp
459 ;; This used to use path-separator instead of ":";
460 ;; however, we found that on both Windows 32 and MSDOS
461 ;; a colon is correct here.
462 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
463 "\\([0-9]*\\)" ":" ".*\n"))
465 ;; There's no guarantee that Emacs will hand the filter the entire
466 ;; marker at once; it could be broken up across several strings. We
467 ;; might even receive a big chunk with several markers in it. If we
468 ;; receive a chunk of text which looks like it might contain the
469 ;; beginning of a marker, we save it here between calls to the
471 (defvar gud-marker-acc "")
472 (make-variable-buffer-local 'gud-marker-acc)
474 (defun gud-gdb-marker-filter (string)
475 (setq gud-marker-acc (concat gud-marker-acc string))
478 ;; Process all the complete markers in this chunk.
479 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
482 ;; Extract the frame position from the marker.
483 gud-last-frame (cons (match-string 1 gud-marker-acc)
484 (string-to-number (match-string 2 gud-marker-acc)))
486 ;; Append any text before the marker to the output we're going
487 ;; to return - we don't include the marker in this text.
488 output (concat output
489 (substring gud-marker-acc 0 (match-beginning 0)))
491 ;; Set the accumulator to the remaining text.
492 gud-marker-acc (substring gud-marker-acc (match-end 0))))
494 ;; Check for annotations and change gud-minor-mode to 'gdba if
496 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
497 (when (string-equal (match-string 1 gud-marker-acc) "prompt")
502 ;; Append any text before the marker to the output we're going
503 ;; to return - we don't include the marker in this text.
504 output (concat output
505 (substring gud-marker-acc 0 (match-beginning 0)))
507 ;; Set the accumulator to the remaining text.
508 gud-marker-acc (substring gud-marker-acc (match-end 0))))
510 ;; Does the remaining text look like it might end with the
511 ;; beginning of another marker? If it does, then keep it in
512 ;; gud-marker-acc until we receive the rest of it. Since we
513 ;; know the full marker regexp above failed, it's pretty simple to
514 ;; test for marker starts.
515 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
517 ;; Everything before the potential marker start can be output.
518 (setq output (concat output (substring gud-marker-acc
519 0 (match-beginning 0))))
521 ;; Everything after, we save, to combine with later input.
523 (substring gud-marker-acc (match-beginning 0))))
525 (setq output (concat output gud-marker-acc)
530 (easy-mmode-defmap gud-minibuffer-local-map
531 '(("\C-i" . comint-dynamic-complete-filename))
532 "Keymap for minibuffer prompting of gud startup command."
533 :inherit minibuffer-local-map)
535 (defun gud-query-cmdline (minor-mode &optional init)
536 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
537 (cmd-name (gud-val 'command-name minor-mode)))
538 (unless (boundp hist-sym) (set hist-sym nil))
539 (read-from-minibuffer
540 (format "Run %s (like this): " minor-mode)
541 (or (car-safe (symbol-value hist-sym))
542 (concat (or cmd-name (symbol-name minor-mode))
546 (dolist (f (directory-files default-directory) file)
547 (if (and (file-executable-p f)
548 (not (file-directory-p f))
550 (file-newer-than-file-p f file)))
552 gud-minibuffer-local-map nil
555 (defvar gdb-first-prompt t)
557 (defvar gud-filter-pending-text nil
558 "Non-nil means this is text that has been saved for later in `gud-filter'.")
561 (defun gdb (command-line)
562 "Run gdb on program FILE in buffer *gud-FILE*.
563 The directory containing FILE becomes the initial working directory
564 and source-file directory for your debugger."
565 (interactive (list (gud-query-cmdline 'gdb)))
567 (gud-common-init command-line nil 'gud-gdb-marker-filter)
568 (set (make-local-variable 'gud-minor-mode) 'gdb)
570 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
571 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
572 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
573 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
574 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
575 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
576 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
577 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
578 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
579 (gud-def gud-jump "tbreak %f:%l\njump %f:%l" "\C-j" "Relocate execution address to line at point in source buffer.")
581 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
582 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
583 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
584 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
585 (gud-def gud-run "run" nil "Run the program.")
587 (local-set-key "\C-i" 'gud-gdb-complete-command)
588 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
589 (setq paragraph-start comint-prompt-regexp)
590 (setq gdb-first-prompt t)
591 (setq gud-filter-pending-text nil)
592 (run-hooks 'gdb-mode-hook))
594 ;; One of the nice features of GDB is its impressive support for
595 ;; context-sensitive command completion. We preserve that feature
596 ;; in the GUD buffer by using a GDB command designed just for Emacs.
598 ;; The completion process filter indicates when it is finished.
599 (defvar gud-gdb-fetch-lines-in-progress)
601 ;; Since output may arrive in fragments we accumulate partials strings here.
602 (defvar gud-gdb-fetch-lines-string)
604 ;; We need to know how much of the completion to chop off.
605 (defvar gud-gdb-fetch-lines-break)
607 ;; The completion list is constructed by the process filter.
608 (defvar gud-gdb-fetched-lines)
610 (defvar gud-comint-buffer nil)
612 (defun gud-gdb-complete-command ()
613 "Perform completion on the GDB command preceding point.
614 This is implemented using the GDB `complete' command which isn't
615 available with older versions of GDB."
618 (command (buffer-substring (comint-line-beginning-position) end))
620 ;; Find the word break. This match will always succeed.
621 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
622 (substring command (match-beginning 2))))
624 (gud-gdb-run-command-fetch-lines (concat "complete " command)
626 ;; From string-match above.
627 (match-beginning 2))))
628 ;; Protect against old versions of GDB.
630 (string-match "^Undefined command: \"complete\"" (car complete-list))
631 (error "This version of GDB doesn't support the `complete' command"))
632 ;; Sort the list like readline.
633 (setq complete-list (sort complete-list (function string-lessp)))
634 ;; Remove duplicates.
635 (let ((first complete-list)
636 (second (cdr complete-list)))
638 (if (string-equal (car first) (car second))
639 (setcdr first (setq second (cdr second)))
641 second (cdr second)))))
642 ;; Add a trailing single quote if there is a unique completion
643 ;; and it contains an odd number of unquoted single quotes.
644 (and (= (length complete-list) 1)
645 (let ((str (car complete-list))
648 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
649 (setq count (1+ count)
651 (and (= (mod count 2) 1)
652 (setq complete-list (list (concat str "'"))))))
653 ;; Let comint handle the rest.
654 (comint-dynamic-simple-complete command-word complete-list)))
656 ;; The completion process filter is installed temporarily to slurp the
657 ;; output of GDB up to the next prompt and build the completion list.
658 (defun gud-gdb-fetch-lines-filter (string filter)
659 "Filter used to read the list of lines output by a command.
660 STRING is the output to filter.
661 It is passed through FILTER before we look at it."
662 (setq string (funcall filter string))
663 (setq string (concat gud-gdb-fetch-lines-string string))
664 (while (string-match "\n" string)
665 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
666 gud-gdb-fetched-lines)
667 (setq string (substring string (match-end 0))))
668 (if (string-match comint-prompt-regexp string)
670 (setq gud-gdb-fetch-lines-in-progress nil)
673 (setq gud-gdb-fetch-lines-string string)
676 ;; gdb speedbar functions
678 (defun gud-gdb-goto-stackframe (text token indent)
679 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
680 (speedbar-with-attached-buffer
681 (gud-basic-call (concat "server frame " (nth 1 token)))
684 (defvar gud-gdb-fetched-stack-frame nil
685 "Stack frames we are fetching from GDB.")
687 ;(defun gud-gdb-get-scope-data (text token indent)
688 ; ;; checkdoc-params: (indent)
689 ; "Fetch data associated with a stack frame, and expand/contract it.
690 ;Data to do this is retrieved from TEXT and TOKEN."
691 ; (let ((args nil) (scope nil))
692 ; (gud-gdb-run-command-fetch-lines "info args")
694 ; (gud-gdb-run-command-fetch-lines "info local")
698 (defun gud-gdb-get-stackframe (buffer)
699 "Extract the current stack frame out of the GUD GDB BUFFER."
701 (fetched-stack-frame-list
702 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
703 (if (and (car fetched-stack-frame-list)
704 (string-match "No stack" (car fetched-stack-frame-list)))
705 ;; Go into some other mode???
707 (dolist (e fetched-stack-frame-list)
708 (let ((name nil) (num nil))
710 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
711 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
712 (if (not (string-match
713 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
716 (list (nth 0 (car newlst))
719 (match-string 2 e))))
720 (setq num (match-string 1 e)
721 name (match-string 2 e))
725 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
726 (list name num (match-string 1 e)
732 ;(defun gud-gdb-selected-frame-info (buffer)
733 ; "Learn GDB information for the currently selected stack frame in BUFFER."
736 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
737 "Run COMMAND, and return the list of lines it outputs.
738 BUFFER is the GUD buffer in which to run the command.
739 SKIP is the number of chars to skip on each lines, it defaults to 0."
740 (with-current-buffer buffer
742 (goto-char (point-max))
744 (not (looking-at comint-prompt-regexp)))
746 ;; Much of this copied from GDB complete, but I'm grabbing the stack
748 (let ((gud-gdb-fetch-lines-in-progress t)
749 (gud-gdb-fetched-lines nil)
750 (gud-gdb-fetch-lines-string nil)
751 (gud-gdb-fetch-lines-break (or skip 0))
753 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
754 ;; Issue the command to GDB.
755 (gud-basic-call command)
757 (while gud-gdb-fetch-lines-in-progress
758 (accept-process-output (get-buffer-process buffer)))
759 (nreverse gud-gdb-fetched-lines)))))
762 ;; ======================================================================
765 ;; History of argument lists passed to sdb.
766 (defvar gud-sdb-history nil)
768 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
769 "If nil, we're on a System V Release 4 and don't need the tags hack.")
771 (defvar gud-sdb-lastfile nil)
773 (defun gud-sdb-marker-filter (string)
775 (if gud-marker-acc (concat gud-marker-acc string) string))
777 ;; Process all complete markers in this chunk
780 ;; System V Release 3.2 uses this format
781 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
782 gud-marker-acc start)
784 (cons (match-string 3 gud-marker-acc)
785 (string-to-number (match-string 4 gud-marker-acc)))))
786 ;; System V Release 4.0 quite often clumps two lines together
787 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
788 gud-marker-acc start)
789 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
791 (cons gud-sdb-lastfile
792 (string-to-number (match-string 3 gud-marker-acc)))))
793 ;; System V Release 4.0
794 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
795 gud-marker-acc start)
796 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
797 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
798 gud-marker-acc start))
800 (cons gud-sdb-lastfile
801 (string-to-number (match-string 1 gud-marker-acc)))))
803 (setq gud-sdb-lastfile nil)))
804 (setq start (match-end 0)))
806 ;; Search for the last incomplete line in this chunk
807 (while (string-match "\n" gud-marker-acc start)
808 (setq start (match-end 0)))
810 ;; If we have an incomplete line, store it in gud-marker-acc.
811 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
814 (defun gud-sdb-find-file (f)
815 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
818 (defun sdb (command-line)
819 "Run sdb on program FILE in buffer *gud-FILE*.
820 The directory containing FILE becomes the initial working directory
821 and source-file directory for your debugger."
822 (interactive (list (gud-query-cmdline 'sdb)))
824 (if (and gud-sdb-needs-tags
825 (not (and (boundp 'tags-file-name)
826 (stringp tags-file-name)
827 (file-exists-p tags-file-name))))
828 (error "The sdb support requires a valid tags table to work"))
830 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
831 (set (make-local-variable 'gud-minor-mode) 'sdb)
833 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
834 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
835 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
836 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
837 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
838 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
839 (gud-def gud-cont "c" "\C-r" "Continue with display.")
840 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
842 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
843 (setq paragraph-start comint-prompt-regexp)
844 (run-hooks 'sdb-mode-hook)
847 ;; ======================================================================
850 ;; History of argument lists passed to dbx.
851 (defvar gud-dbx-history nil)
853 (defcustom gud-dbx-directories nil
854 "*A list of directories that dbx should search for source code.
855 If nil, only source files in the program directory
856 will be known to dbx.
858 The file names should be absolute, or relative to the directory
859 containing the executable being debugged."
860 :type '(choice (const :tag "Current Directory" nil)
865 (defun gud-dbx-massage-args (file args)
866 (nconc (let ((directories gud-dbx-directories)
869 (setq result (cons (car directories) (cons "-I" result)))
870 (setq directories (cdr directories)))
874 (defun gud-dbx-marker-filter (string)
875 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
878 ;; Process all complete markers in this chunk.
879 (while (or (string-match
880 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
881 gud-marker-acc start)
883 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
884 gud-marker-acc start))
886 (cons (match-string 2 gud-marker-acc)
887 (string-to-number (match-string 1 gud-marker-acc)))
888 start (match-end 0)))
890 ;; Search for the last incomplete line in this chunk
891 (while (string-match "\n" gud-marker-acc start)
892 (setq start (match-end 0)))
894 ;; If the incomplete line APPEARS to begin with another marker, keep it
895 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
896 ;; unnecessary concat during the next call.
898 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
899 (substring gud-marker-acc (match-beginning 0))
903 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
904 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
906 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
907 ;; We haven't tested gud on this system:
908 (string-match "^mips-[^-]*-riscos" system-configuration)
909 ;; It's documented on OSF/1.3
910 (string-match "^mips-[^-]*-osf1" system-configuration)
911 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
912 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
914 (defvar gud-dbx-command-name
915 (concat "dbx" (if gud-mips-p " -emacs")))
917 ;; This is just like the gdb one except for the regexps since we need to cope
918 ;; with an optional breakpoint number in [] before the ^Z^Z
919 (defun gud-mipsdbx-marker-filter (string)
920 (setq gud-marker-acc (concat gud-marker-acc string))
923 ;; Process all the complete markers in this chunk.
925 ;; This is like th gdb marker but with an optional
926 ;; leading break point number like `[1] '
927 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
931 ;; Extract the frame position from the marker.
933 (cons (match-string 1 gud-marker-acc)
934 (string-to-number (match-string 2 gud-marker-acc)))
936 ;; Append any text before the marker to the output we're going
937 ;; to return - we don't include the marker in this text.
938 output (concat output
939 (substring gud-marker-acc 0 (match-beginning 0)))
941 ;; Set the accumulator to the remaining text.
942 gud-marker-acc (substring gud-marker-acc (match-end 0))))
944 ;; Does the remaining text look like it might end with the
945 ;; beginning of another marker? If it does, then keep it in
946 ;; gud-marker-acc until we receive the rest of it. Since we
947 ;; know the full marker regexp above failed, it's pretty simple to
948 ;; test for marker starts.
949 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
951 ;; Everything before the potential marker start can be output.
952 (setq output (concat output (substring gud-marker-acc
953 0 (match-beginning 0))))
955 ;; Everything after, we save, to combine with later input.
957 (substring gud-marker-acc (match-beginning 0))))
959 (setq output (concat output gud-marker-acc)
964 ;; The dbx in IRIX is a pain. It doesn't print the file name when
965 ;; stopping at a breakpoint (but you do get it from the `up' and
966 ;; `down' commands...). The only way to extract the information seems
967 ;; to be with a `file' command, although the current line number is
968 ;; available in $curline. Thus we have to look for output which
969 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
970 ;; to output the information we want with a combination of the
971 ;; `printf' and `file' commands as a pseudo marker which we can
972 ;; recognise next time through the marker-filter. This would be like
973 ;; the gdb marker but you can't get the file name without a newline...
974 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
975 ;; number rather than a line number etc. Maybe this could be made to
976 ;; work by listing all the breakpoints and picking the one(s) with the
977 ;; correct line number, but life's too short.
978 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
981 (and (string-match "^mips-[^-]*-irix" system-configuration)
982 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
983 "Non-nil to assume the interface appropriate for IRIX dbx.
984 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
985 a better solution in 6.1 upwards.")
986 (defvar gud-dbx-use-stopformat-p
987 (string-match "irix[6-9]\\.[1-9]" system-configuration)
988 "Non-nil to use the dbx feature present at least from Irix 6.1
989 whereby $stopformat=1 produces an output format compatiable with
990 `gud-dbx-marker-filter'.")
991 ;; [Irix dbx seems to be a moving target. The dbx output changed
992 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
993 ;; the output from `up' is no longer spotted by gud (and it's probably
994 ;; not distinctive enough to try to match it -- use C-<, C->
995 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
996 ;; `long long'(why?!), so the printf stuff needed changing. The line
997 ;; number was cast to `long' as a compromise between the new `long
998 ;; long' and the original `int'. This is reported not to work in 6.2,
999 ;; so it's changed back to int -- don't make your sources too long.
1000 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
1001 ;; whereby `set $stopformat=1' reportedly produces output compatible
1002 ;; with `gud-dbx-marker-filter', which we prefer.
1004 ;; The process filter is also somewhat
1005 ;; unreliable, sometimes not spotting the markers; I don't know
1006 ;; whether there's anything that can be done about that. It would be
1007 ;; much better if SGI could be persuaded to (re?)instate the MIPS
1008 ;; -emacs flag for gdb-like output (which ought to be possible as most
1009 ;; of the communication I've had over it has been from sgi.com).]
1011 ;; this filter is influenced by the xdb one rather than the gdb one
1012 (defun gud-irixdbx-marker-filter (string)
1013 (let (result (case-fold-search nil))
1014 (if (or (string-match comint-prompt-regexp string)
1015 (string-match ".*\012" string))
1016 (setq result (concat gud-marker-acc string)
1018 (setq gud-marker-acc (concat gud-marker-acc string)))
1021 ;; look for breakpoint or signal indication e.g.:
1022 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1023 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1024 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1026 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1028 ;; prod dbx into printing out the line number and file
1029 ;; name in a form we can grok as below
1030 (process-send-string (get-buffer-process gud-comint-buffer)
1031 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1032 ;; look for result of, say, "up" e.g.:
1033 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1034 ;; (this will also catch one of the lines printed by "where")
1036 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1038 (let ((file (match-string 1 result)))
1039 (if (file-exists-p file)
1040 (setq gud-last-frame
1041 (cons (match-string 1 result)
1042 (string-to-number (match-string 2 result))))))
1044 ((string-match ; kluged-up marker as above
1045 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1046 (let ((file (gud-file-name (match-string 2 result))))
1047 (if (and file (file-exists-p file))
1048 (setq gud-last-frame
1050 (string-to-number (match-string 1 result))))))
1051 (setq result (substring result 0 (match-beginning 0))))))
1054 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
1055 "Non-nil means to assume the interface approriate for DG/UX dbx.
1056 This was tested using R4.11.")
1058 ;; There are a couple of differences between DG's dbx output and normal
1059 ;; dbx output which make it nontrivial to integrate this into the
1060 ;; standard dbx-marker-filter (mainly, there are a different number of
1061 ;; backreferences). The markers look like:
1063 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1065 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1068 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1072 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1074 ;; from up/down/where.
1076 (defun gud-dguxdbx-marker-filter (string)
1077 (setq gud-marker-acc (if gud-marker-acc
1078 (concat gud-marker-acc string)
1080 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1081 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1083 ;; Process all complete markers in this chunk.
1084 (while (string-match re gud-marker-acc start)
1085 (setq gud-last-frame
1086 (cons (match-string 4 gud-marker-acc)
1087 (string-to-number (match-string 3 gud-marker-acc)))
1088 start (match-end 0)))
1090 ;; Search for the last incomplete line in this chunk
1091 (while (string-match "\n" gud-marker-acc start)
1092 (setq start (match-end 0)))
1094 ;; If the incomplete line APPEARS to begin with another marker, keep it
1095 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1096 ;; unnecessary concat during the next call.
1097 (setq gud-marker-acc
1098 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1099 (substring gud-marker-acc (match-beginning 0))
1104 (defun dbx (command-line)
1105 "Run dbx on program FILE in buffer *gud-FILE*.
1106 The directory containing FILE becomes the initial working directory
1107 and source-file directory for your debugger."
1108 (interactive (list (gud-query-cmdline 'dbx)))
1112 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1114 (gud-common-init command-line 'gud-dbx-massage-args
1115 'gud-irixdbx-marker-filter))
1117 (gud-common-init command-line 'gud-dbx-massage-args
1118 'gud-dguxdbx-marker-filter))
1120 (gud-common-init command-line 'gud-dbx-massage-args
1121 'gud-dbx-marker-filter)))
1123 (set (make-local-variable 'gud-minor-mode) 'dbx)
1127 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1128 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1129 (gud-def gud-break "stop at \"%f\":%l"
1130 "\C-b" "Set breakpoint at current line.")
1131 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1133 (gud-def gud-break "stop at \"%d%f\":%l"
1134 "\C-b" "Set breakpoint at current line.")
1135 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1136 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1137 "<" "Up (numeric arg) stack frames.")
1138 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1139 ">" "Down (numeric arg) stack frames.")
1140 ;; Make dbx give out the source location info that we need.
1141 (process-send-string (get-buffer-process gud-comint-buffer)
1142 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1144 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1145 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1146 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1147 "\C-b" "Set breakpoint at current line.")
1148 (if gud-dbx-use-stopformat-p
1149 (process-send-string (get-buffer-process gud-comint-buffer)
1150 "set $stopformat=1\n"))))
1152 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1153 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1154 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1155 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1156 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1157 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1158 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1159 (gud-def gud-run "run" nil "Run the program.")
1161 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1162 (setq paragraph-start comint-prompt-regexp)
1163 (run-hooks 'dbx-mode-hook)
1166 ;; ======================================================================
1167 ;; xdb (HP PARISC debugger) functions
1169 ;; History of argument lists passed to xdb.
1170 (defvar gud-xdb-history nil)
1172 (defcustom gud-xdb-directories nil
1173 "*A list of directories that xdb should search for source code.
1174 If nil, only source files in the program directory
1175 will be known to xdb.
1177 The file names should be absolute, or relative to the directory
1178 containing the executable being debugged."
1179 :type '(choice (const :tag "Current Directory" nil)
1184 (defun gud-xdb-massage-args (file args)
1185 (nconc (let ((directories gud-xdb-directories)
1188 (setq result (cons (car directories) (cons "-d" result)))
1189 (setq directories (cdr directories)))
1193 ;; xdb does not print the lines all at once, so we have to accumulate them
1194 (defun gud-xdb-marker-filter (string)
1196 (if (or (string-match comint-prompt-regexp string)
1197 (string-match ".*\012" string))
1198 (setq result (concat gud-marker-acc string)
1200 (setq gud-marker-acc (concat gud-marker-acc string)))
1202 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1204 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1206 (let ((line (string-to-number (match-string 2 result)))
1207 (file (gud-file-name (match-string 1 result))))
1209 (setq gud-last-frame (cons file line))))))
1213 (defun xdb (command-line)
1214 "Run xdb on program FILE in buffer *gud-FILE*.
1215 The directory containing FILE becomes the initial working directory
1216 and source-file directory for your debugger.
1218 You can set the variable 'gud-xdb-directories' to a list of program source
1219 directories if your program contains sources from more than one directory."
1220 (interactive (list (gud-query-cmdline 'xdb)))
1222 (gud-common-init command-line 'gud-xdb-massage-args
1223 'gud-xdb-marker-filter)
1224 (set (make-local-variable 'gud-minor-mode) 'xdb)
1226 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1227 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1228 "Set temporary breakpoint at current line.")
1229 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1230 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1231 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1232 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1233 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1234 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1235 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1236 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1238 (setq comint-prompt-regexp "^>")
1239 (setq paragraph-start comint-prompt-regexp)
1240 (run-hooks 'xdb-mode-hook))
1242 ;; ======================================================================
1245 ;; History of argument lists passed to perldb.
1246 (defvar gud-perldb-history nil)
1248 (defun gud-perldb-massage-args (file args)
1249 "Convert a command line as would be typed normally to run perldb
1250 into one that invokes an Emacs-enabled debugging session.
1251 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1252 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1254 (let* ((new-args nil)
1256 (shift (lambda () (push (pop args) new-args))))
1258 ;; Pass all switches and -e scripts through.
1260 (string-match "^-" (car args))
1261 (not (equal "-" (car args)))
1262 (not (equal "--" (car args))))
1263 (when (equal "-e" (car args))
1264 ;; -e goes with the next arg, so shift one extra.
1266 ;; -e as the last arg is an error in Perl.
1267 (error "No code specified for -e"))
1273 (string-match "^-" (car args)))
1274 (error "Can't use stdin as the script to debug"))
1275 ;; This is the program name.
1278 ;; If -e specified, make sure there is a -- so -emacs is not taken
1280 (if (and args (equal "--" (car args)))
1282 (and seen-e (push "--" new-args)))
1284 (push "-emacs" new-args)
1288 (nreverse new-args)))
1290 ;; There's no guarantee that Emacs will hand the filter the entire
1291 ;; marker at once; it could be broken up across several strings. We
1292 ;; might even receive a big chunk with several markers in it. If we
1293 ;; receive a chunk of text which looks like it might contain the
1294 ;; beginning of a marker, we save it here between calls to the
1296 (defun gud-perldb-marker-filter (string)
1297 (setq gud-marker-acc (concat gud-marker-acc string))
1300 ;; Process all the complete markers in this chunk.
1301 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1305 ;; Extract the frame position from the marker.
1307 (cons (match-string 1 gud-marker-acc)
1308 (string-to-number (match-string 3 gud-marker-acc)))
1310 ;; Append any text before the marker to the output we're going
1311 ;; to return - we don't include the marker in this text.
1312 output (concat output
1313 (substring gud-marker-acc 0 (match-beginning 0)))
1315 ;; Set the accumulator to the remaining text.
1316 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1318 ;; Does the remaining text look like it might end with the
1319 ;; beginning of another marker? If it does, then keep it in
1320 ;; gud-marker-acc until we receive the rest of it. Since we
1321 ;; know the full marker regexp above failed, it's pretty simple to
1322 ;; test for marker starts.
1323 (if (string-match "\032.*\\'" gud-marker-acc)
1325 ;; Everything before the potential marker start can be output.
1326 (setq output (concat output (substring gud-marker-acc
1327 0 (match-beginning 0))))
1329 ;; Everything after, we save, to combine with later input.
1330 (setq gud-marker-acc
1331 (substring gud-marker-acc (match-beginning 0))))
1333 (setq output (concat output gud-marker-acc)
1338 (defcustom gud-perldb-command-name "perl -d"
1339 "Default command to execute a Perl script under debugger."
1344 (defun perldb (command-line)
1345 "Run perldb on program FILE in buffer *gud-FILE*.
1346 The directory containing FILE becomes the initial working directory
1347 and source-file directory for your debugger."
1349 (list (gud-query-cmdline 'perldb
1350 (concat (or (buffer-file-name) "-e 0") " "))))
1352 (gud-common-init command-line 'gud-perldb-massage-args
1353 'gud-perldb-marker-filter)
1354 (set (make-local-variable 'gud-minor-mode) 'perldb)
1356 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1357 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1358 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1359 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1360 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1361 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1362 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1363 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1364 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1365 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1368 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1369 (setq paragraph-start comint-prompt-regexp)
1370 (run-hooks 'perldb-mode-hook))
1372 ;; ======================================================================
1373 ;; pdb (Python debugger) functions
1375 ;; History of argument lists passed to pdb.
1376 (defvar gud-pdb-history nil)
1378 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1379 ;; Either file or function name may be omitted: "> <string>(0)?()"
1380 (defvar gud-pdb-marker-regexp
1381 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1382 (defvar gud-pdb-marker-regexp-file-group 1)
1383 (defvar gud-pdb-marker-regexp-line-group 2)
1384 (defvar gud-pdb-marker-regexp-fnname-group 3)
1386 (defvar gud-pdb-marker-regexp-start "^> ")
1388 ;; There's no guarantee that Emacs will hand the filter the entire
1389 ;; marker at once; it could be broken up across several strings. We
1390 ;; might even receive a big chunk with several markers in it. If we
1391 ;; receive a chunk of text which looks like it might contain the
1392 ;; beginning of a marker, we save it here between calls to the
1394 (defun gud-pdb-marker-filter (string)
1395 (setq gud-marker-acc (concat gud-marker-acc string))
1398 ;; Process all the complete markers in this chunk.
1399 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1402 ;; Extract the frame position from the marker.
1404 (let ((file (match-string gud-pdb-marker-regexp-file-group
1406 (line (string-to-number
1407 (match-string gud-pdb-marker-regexp-line-group
1409 (if (string-equal file "<string>")
1413 ;; Output everything instead of the below
1414 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1415 ;; ;; Append any text before the marker to the output we're going
1416 ;; ;; to return - we don't include the marker in this text.
1417 ;; output (concat output
1418 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1420 ;; Set the accumulator to the remaining text.
1421 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1423 ;; Does the remaining text look like it might end with the
1424 ;; beginning of another marker? If it does, then keep it in
1425 ;; gud-marker-acc until we receive the rest of it. Since we
1426 ;; know the full marker regexp above failed, it's pretty simple to
1427 ;; test for marker starts.
1428 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1430 ;; Everything before the potential marker start can be output.
1431 (setq output (concat output (substring gud-marker-acc
1432 0 (match-beginning 0))))
1434 ;; Everything after, we save, to combine with later input.
1435 (setq gud-marker-acc
1436 (substring gud-marker-acc (match-beginning 0))))
1438 (setq output (concat output gud-marker-acc)
1443 (defcustom gud-pdb-command-name "pdb"
1444 "File name for executing the Python debugger.
1445 This should be an executable on your path, or an absolute file name."
1450 (defun pdb (command-line)
1451 "Run pdb on program FILE in buffer `*gud-FILE*'.
1452 The directory containing FILE becomes the initial working directory
1453 and source-file directory for your debugger."
1455 (list (gud-query-cmdline 'pdb)))
1457 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1458 (set (make-local-variable 'gud-minor-mode) 'pdb)
1460 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1461 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1462 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1463 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1464 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1465 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1466 (gud-def gud-up "up" "<" "Up one stack frame.")
1467 (gud-def gud-down "down" ">" "Down one stack frame.")
1468 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1470 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1472 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1473 (setq comint-prompt-regexp "^(Pdb) *")
1474 (setq paragraph-start comint-prompt-regexp)
1475 (run-hooks 'pdb-mode-hook))
1477 ;; ======================================================================
1481 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1482 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1484 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1485 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1486 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1488 ;; INVOCATION NOTES:
1490 ;; You invoke jdb-mode with:
1494 ;; It responds with:
1496 ;; Run jdb (like this): jdb
1498 ;; type any jdb switches followed by the name of the class you'd like to debug.
1499 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1500 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1501 ;; See the known problems section below for restrictions when specifying jdb
1502 ;; command line switches (search forward for '-classpath').
1504 ;; You should see something like the following:
1506 ;; Current directory is ~/src/java/hello/
1507 ;; Initializing jdb...
1508 ;; 0xed2f6628:class(hello)
1511 ;; To set an initial breakpoint try:
1513 ;; > stop in hello.main
1514 ;; Breakpoint set in hello.main
1517 ;; To execute the program type:
1522 ;; Breakpoint hit: running ...
1523 ;; hello.main (hello:12)
1525 ;; Type M-n to step over the current line and M-s to step into it. That,
1526 ;; along with the JDB 'help' command should get you started. The 'quit'
1527 ;; JDB command will get out out of the debugger. There is some truly
1528 ;; pathetic JDB documentation available at:
1530 ;; http://java.sun.com/products/jdk/1.1/debugging/
1532 ;; KNOWN PROBLEMS AND FIXME's:
1534 ;; Not sure what happens with inner classes ... haven't tried them.
1536 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1538 ;; You must not put whitespace between "-classpath" and the path to
1539 ;; search for java classes even though it is required when invoking jdb
1540 ;; from the command line. See gud-jdb-massage-args for details.
1541 ;; The same applies for "-sourcepath".
1543 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1544 ;; refer to the documentation of `gud-jdb-use-classpath' and
1545 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1546 ;; on using the classpath for locating java source files.
1548 ;; If any of the source files in the directories listed in
1549 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1550 ;; every file ending in ".java" in these directories parses without error.
1552 ;; All the .java files in the directories in gud-jdb-directories are
1553 ;; syntactically analyzed each time gud jdb is invoked. It would be
1554 ;; nice to keep as much information as possible between runs. It would
1555 ;; be really nice to analyze the files only as neccessary (when the
1556 ;; source needs to be displayed.) I'm not sure to what extent the former
1557 ;; can be accomplished and I'm not sure the latter can be done at all
1558 ;; since I don't know of any general way to tell which .class files are
1559 ;; defined by which .java file without analyzing all the .java files.
1560 ;; If anyone knows why JavaSoft didn't put the source file names in
1561 ;; debuggable .class files please clue me in so I find something else
1562 ;; to be spiteful and bitter about.
1564 ;; ======================================================================
1565 ;; gud jdb variables and functions
1567 (defcustom gud-jdb-command-name "jdb"
1568 "Command that executes the Java debugger."
1572 (defcustom gud-jdb-use-classpath t
1573 "If non-nil, search for Java source files in classpath directories.
1574 The list of directories to search is the value of `gud-jdb-classpath'.
1575 The file pathname is obtained by converting the fully qualified
1576 class information output by jdb to a relative pathname and appending
1577 it to `gud-jdb-classpath' element by element until a match is found.
1579 This method has a significant jdb startup time reduction advantage
1580 since it does not require the scanning of all `gud-jdb-directories'
1581 and parsing all Java files for class information.
1583 Set to nil to use `gud-jdb-directories' to scan java sources for
1584 class information on jdb startup (original method)."
1588 (defvar gud-jdb-classpath nil
1589 "Java/jdb classpath directories list.
1590 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1591 list automatically using the following methods in sequence
1592 \(with subsequent successful steps overriding the results of previous
1595 1) Read the CLASSPATH environment variable,
1596 2) Read any \"-classpath\" argument used to run jdb,
1597 or detected in jdb output (e.g. if jdb is run by a script
1598 that echoes the actual jdb command before starting jdb)
1599 3) Send a \"classpath\" command to jdb and scan jdb output for
1600 classpath information if jdb is invoked with an \"-attach\" (to
1601 an already running VM) argument (This case typically does not
1602 have a \"-classpath\" command line argument - that is provided
1603 to the VM when it is started).
1605 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1606 those debuggers do not support the classpath command. Use 1) or 2).")
1608 (defvar gud-jdb-sourcepath nil
1609 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1610 This list is prepended to `gud-jdb-classpath' to form the complete
1611 list of directories searched for source files.")
1613 (defvar gud-marker-acc-max-length 4000
1614 "Maximum number of debugger output characters to keep.
1615 This variable limits the size of `gud-marker-acc' which holds
1616 the most recent debugger output history while searching for
1617 source file information.")
1619 (defvar gud-jdb-history nil
1620 "History of argument lists passed to jdb.")
1623 ;; List of Java source file directories.
1624 (defvar gud-jdb-directories (list ".")
1625 "*A list of directories that gud jdb should search for source code.
1626 The file names should be absolute, or relative to the current
1629 The set of .java files residing in the directories listed are
1630 syntactically analyzed to determine the classes they define and the
1631 packages in which these classes belong. In this way gud jdb maps the
1632 package-qualified class names output by the jdb debugger to the source
1633 file from which the class originated. This allows gud mode to keep
1634 the source code display in sync with the debugging session.")
1636 (defvar gud-jdb-source-files nil
1637 "List of the java source files for this debugging session.")
1639 ;; Association list of fully qualified class names (package + class name)
1640 ;; and their source files.
1641 (defvar gud-jdb-class-source-alist nil
1642 "Association list of fully qualified class names and source files.")
1644 ;; This is used to hold a source file during analysis.
1645 (defvar gud-jdb-analysis-buffer nil)
1647 (defvar gud-jdb-classpath-string nil
1648 "Holds temporary classpath values.")
1650 (defun gud-jdb-build-source-files-list (path extn)
1651 "Return a list of java source files (absolute paths).
1652 PATH gives the directories in which to search for files with
1653 extension EXTN. Normally EXTN is given as the regular expression
1655 (apply 'nconc (mapcar (lambda (d)
1656 (when (file-directory-p d)
1657 (directory-files d t extn nil)))
1660 ;; Move point past whitespace.
1661 (defun gud-jdb-skip-whitespace ()
1662 (skip-chars-forward " \n\r\t\014"))
1664 ;; Move point past a "// <eol>" type of comment.
1665 (defun gud-jdb-skip-single-line-comment ()
1668 ;; Move point past a "/* */" or "/** */" type of comment.
1669 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1673 (if (eq (following-char) ?*)
1677 (if (eq (following-char) ?/)
1680 (throw 'break nil)))))
1683 ;; Move point past any number of consecutive whitespace chars and/or comments.
1684 (defun gud-jdb-skip-whitespace-and-comments ()
1685 (gud-jdb-skip-whitespace)
1690 (gud-jdb-skip-single-line-comment)
1691 (gud-jdb-skip-whitespace))
1692 ((looking-at "/\\*")
1693 (gud-jdb-skip-traditional-or-documentation-comment)
1694 (gud-jdb-skip-whitespace))
1695 (t (throw 'done nil))))))
1697 ;; Move point past things that are id-like. The intent is to skip regular
1698 ;; id's, such as class or interface names as well as package and interface
1700 (defun gud-jdb-skip-id-ish-thing ()
1701 (skip-chars-forward "^ /\n\r\t\014,;{"))
1703 ;; Move point past a string literal.
1704 (defun gud-jdb-skip-string-literal ()
1707 ((eq (following-char) ?\\)
1709 ((eq (following-char) ?\042))))
1713 ;; Move point past a character literal.
1714 (defun gud-jdb-skip-character-literal ()
1718 (if (eq (following-char) ?\\)
1720 (not (eq (following-char) ?\')))
1724 ;; Move point past the following block. There may be (legal) cruft before
1725 ;; the block's opening brace. There must be a block or it's the end of life
1726 ;; in petticoat junction.
1727 (defun gud-jdb-skip-block ()
1729 ;; Find the begining of the block.
1731 (not (eq (following-char) ?{))
1733 ;; Skip any constructs that can harbor literal block delimiter
1734 ;; characters and/or the delimiters for the constructs themselves.
1737 (gud-jdb-skip-single-line-comment))
1738 ((looking-at "/\\*")
1739 (gud-jdb-skip-traditional-or-documentation-comment))
1740 ((eq (following-char) ?\042)
1741 (gud-jdb-skip-string-literal))
1742 ((eq (following-char) ?\')
1743 (gud-jdb-skip-character-literal))
1744 (t (forward-char))))
1746 ;; Now at the begining of the block.
1749 ;; Skip over the body of the block as well as the final brace.
1750 (let ((open-level 1))
1751 (while (not (eq open-level 0))
1754 (gud-jdb-skip-single-line-comment))
1755 ((looking-at "/\\*")
1756 (gud-jdb-skip-traditional-or-documentation-comment))
1757 ((eq (following-char) ?\042)
1758 (gud-jdb-skip-string-literal))
1759 ((eq (following-char) ?\')
1760 (gud-jdb-skip-character-literal))
1761 ((eq (following-char) ?{)
1762 (setq open-level (+ open-level 1))
1764 ((eq (following-char) ?})
1765 (setq open-level (- open-level 1))
1767 (t (forward-char))))))
1769 ;; Find the package and class definitions in Java source file FILE. Assumes
1770 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1771 ;; to hold the source during analysis.
1772 (defun gud-jdb-analyze-source (buf file)
1775 (insert-file-contents file nil nil nil t)
1780 (gud-jdb-skip-whitespace)
1784 ;; Any number of semi's following a block is legal. Move point
1785 ;; past them. Note that comments and whitespace may be
1786 ;; interspersed as well.
1787 ((eq (following-char) ?\073)
1790 ;; Move point past a single line comment.
1792 (gud-jdb-skip-single-line-comment))
1794 ;; Move point past a traditional or documentation comment.
1795 ((looking-at "/\\*")
1796 (gud-jdb-skip-traditional-or-documentation-comment))
1798 ;; Move point past a package statement, but save the PackageName.
1799 ((looking-at "package")
1801 (gud-jdb-skip-whitespace-and-comments)
1803 (gud-jdb-skip-id-ish-thing)
1804 (setq p (concat (buffer-substring s (point)) "."))
1805 (gud-jdb-skip-whitespace-and-comments)
1806 (if (eq (following-char) ?\073)
1809 ;; Move point past an import statement.
1810 ((looking-at "import")
1812 (gud-jdb-skip-whitespace-and-comments)
1813 (gud-jdb-skip-id-ish-thing)
1814 (gud-jdb-skip-whitespace-and-comments)
1815 (if (eq (following-char) ?\073)
1818 ;; Move point past the various kinds of ClassModifiers.
1819 ((looking-at "public")
1821 ((looking-at "abstract")
1823 ((looking-at "final")
1826 ;; Move point past a ClassDeclaraction, but save the class
1828 ((looking-at "class")
1830 (gud-jdb-skip-whitespace-and-comments)
1832 (gud-jdb-skip-id-ish-thing)
1834 l (nconc l (list (concat p (buffer-substring s (point)))))))
1835 (gud-jdb-skip-block))
1837 ;; Move point past an interface statement.
1838 ((looking-at "interface")
1840 (gud-jdb-skip-block))
1842 ;; Anything else means the input is invalid.
1844 (message (format "Error parsing file %s." file))
1845 (throw 'abort nil))))))
1848 (defun gud-jdb-build-class-source-alist-for-file (file)
1852 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1854 ;; Return an alist of fully qualified classes and the source files
1855 ;; holding their definitions. SOURCES holds a list of all the source
1856 ;; files to examine.
1857 (defun gud-jdb-build-class-source-alist (sources)
1858 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1863 'gud-jdb-build-class-source-alist-for-file
1865 (kill-buffer gud-jdb-analysis-buffer)
1866 (setq gud-jdb-analysis-buffer nil)))
1868 ;; Change what was given in the minibuffer to something that can be used to
1869 ;; invoke the debugger.
1870 (defun gud-jdb-massage-args (file args)
1871 ;; The jdb executable must have whitespace between "-classpath" and
1872 ;; its value while gud-common-init expects all switch values to
1873 ;; follow the switch keyword without intervening whitespace. We
1874 ;; require that when the user enters the "-classpath" switch in the
1875 ;; EMACS minibuffer that they do so without the intervening
1876 ;; whitespace. This function adds it back (it's called after
1877 ;; gud-common-init). There are more switches like this (for
1878 ;; instance "-host" and "-password") but I don't care about them
1881 (let (massaged-args user-error)
1883 (while (and args (not user-error))
1885 ((setq user-error (string-match "-classpath$" (car args))))
1886 ((setq user-error (string-match "-sourcepath$" (car args))))
1887 ((string-match "-classpath\\(.+\\)" (car args))
1889 (append massaged-args
1891 (setq gud-jdb-classpath-string
1892 (match-string 1 (car args)))))))
1893 ((string-match "-sourcepath\\(.+\\)" (car args))
1895 (append massaged-args
1897 (setq gud-jdb-sourcepath
1898 (match-string 1 (car args)))))))
1899 (t (setq massaged-args (append massaged-args (list (car args))))))
1900 (setq args (cdr args)))
1902 ;; By this point the current directory is all screwed up. Maybe we
1903 ;; could fix things and re-invoke gud-common-init, but for now I think
1904 ;; issueing the error is good enough.
1907 (kill-buffer (current-buffer))
1908 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
1911 ;; Search for an association with P, a fully qualified class name, in
1912 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1913 ;; qualified file name of the source file which produced the class.
1914 (defun gud-jdb-find-source-file (p)
1915 (cdr (assoc p gud-jdb-class-source-alist)))
1917 ;; Note: Reset to this value every time a prompt is seen
1918 (defvar gud-jdb-lowest-stack-level 999)
1920 (defun gud-jdb-find-source-using-classpath (p)
1921 "Find source file corresponding to fully qualified class p.
1922 Convert p from jdb's output, converted to a pathname
1923 relative to a classpath directory."
1926 (;; Replace dots with slashes and append ".java" to generate file
1927 ;; name relative to classpath
1930 (mapconcat 'identity
1932 ;; Eliminate any subclass references in the class
1933 ;; name string. These start with a "$"
1935 (if (string-match "$.*" x)
1936 (replace-match "" t t x) p))
1940 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
1943 (not (setq found-file
1945 (concat (car cplist) "/" filename)))))
1946 (setq cplist (cdr cplist)))
1947 (if found-file (concat (car cplist) "/" filename)))))
1949 (defun gud-jdb-find-source (string)
1950 "Alias for function used to locate source files.
1951 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
1952 during jdb initialization depending on the value of
1953 `gud-jdb-use-classpath'."
1956 (defun gud-jdb-parse-classpath-string (string)
1957 "Parse the classpath list and convert each item to an absolute pathname."
1958 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
1959 (replace-match "" nil nil s) s))
1960 (mapcar 'file-truename
1963 (concat "[ \t\n\r,\"" path-separator "]+")))))
1965 ;; See comentary for other debugger's marker filters - there you will find
1966 ;; important notes about STRING.
1967 (defun gud-jdb-marker-filter (string)
1969 ;; Build up the accumulator.
1970 (setq gud-marker-acc
1972 (concat gud-marker-acc string)
1975 ;; Look for classpath information until gud-jdb-classpath-string is found
1976 ;; (interactive, multiple settings of classpath from jdb
1977 ;; not supported/followed)
1978 (if (and gud-jdb-use-classpath
1979 (not gud-jdb-classpath-string)
1980 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
1981 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
1982 (setq gud-jdb-classpath
1983 (gud-jdb-parse-classpath-string
1984 (setq gud-jdb-classpath-string
1985 (match-string 1 gud-marker-acc)))))
1987 ;; We process STRING from left to right. Each time through the
1988 ;; following loop we process at most one marker. After we've found a
1989 ;; marker, delete gud-marker-acc up to and including the match
1991 ;; Process each complete marker in the input.
1994 ;; Do we see a marker?
1996 ;; jdb puts out a string of the following form when it
1997 ;; hits a breakpoint:
1999 ;; <fully-qualified-class><method> (<class>:<line-number>)
2001 ;; <fully-qualified-class>'s are composed of Java ID's
2002 ;; separated by periods. <method> and <class> are
2003 ;; also Java ID's. <method> begins with a period and
2004 ;; may contain less-than and greater-than (constructors,
2005 ;; for instance, are called <init> in the symbol table.)
2006 ;; Java ID's begin with a letter followed by letters
2007 ;; and/or digits. The set of letters includes underscore
2010 ;; The first group matches <fully-qualified-class>,
2011 ;; the second group matches <class> and the third group
2012 ;; matches <line-number>. We don't care about using
2013 ;; <method> so we don't "group" it.
2015 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2018 ;; The ".," in the last square-bracket are necessary because
2019 ;; of Sun's total disrespect for backwards compatibility in
2020 ;; reported line numbers from jdb - starting in 1.4.0 they
2021 ;; print line numbers using LOCALE, inserting a comma or a
2022 ;; period at the thousands positions (how ingenious!).
2024 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2025 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2028 ;; A good marker is one that:
2029 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2030 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2031 ;; since the last prompt
2032 ;; Figure out the line on which to position the debugging arrow.
2033 ;; Return the info as a cons of the form:
2035 ;; (<file-name> . <line-number>) .
2036 (if (if (match-beginning 1)
2038 (setq n (string-to-number (substring
2040 (1+ (match-beginning 1))
2041 (- (match-end 1) 2))))
2042 (if (< n gud-jdb-lowest-stack-level)
2043 (progn (setq gud-jdb-lowest-stack-level n) t)))
2045 (if (setq file-found
2046 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2047 (setq gud-last-frame
2051 ((numstr (match-string 4 gud-marker-acc)))
2052 (if (string-match "[.,]" numstr)
2053 (replace-match "" nil nil numstr)
2055 (message "Could not find source file.")))
2057 ;; Set the accumulator to the remaining text.
2058 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2060 (if (string-match comint-prompt-regexp gud-marker-acc)
2061 (setq gud-jdb-lowest-stack-level 999)))
2063 ;; Do not allow gud-marker-acc to grow without bound. If the source
2064 ;; file information is not within the last 3/4
2065 ;; gud-marker-acc-max-length characters, well,...
2066 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2067 (setq gud-marker-acc
2068 (substring gud-marker-acc
2069 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2071 ;; We don't filter any debugger output so just return what we were given.
2074 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2077 (defun jdb (command-line)
2078 "Run jdb with command line COMMAND-LINE in a buffer.
2079 The buffer is named \"*gud*\" if no initial class is given or
2080 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2081 switch is given, omit all whitespace between it and its value.
2083 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2084 information on how jdb accesses source files. Alternatively (if
2085 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2086 original source file access method.
2088 For general information about commands available to control jdb from
2089 gud, see `gud-mode'."
2091 (list (gud-query-cmdline 'jdb)))
2092 (setq gud-jdb-classpath nil)
2093 (setq gud-jdb-sourcepath nil)
2095 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2096 ;; if CLASSPATH is set.
2097 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
2098 (if gud-jdb-classpath-string
2099 (setq gud-jdb-classpath
2100 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2101 (setq gud-jdb-classpath-string nil) ; prepare for next
2103 (gud-common-init command-line 'gud-jdb-massage-args
2104 'gud-jdb-marker-filter)
2105 (set (make-local-variable 'gud-minor-mode) 'jdb)
2107 ;; If a -classpath option was provided, set gud-jdb-classpath
2108 (if gud-jdb-classpath-string
2109 (setq gud-jdb-classpath
2110 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2111 (setq gud-jdb-classpath-string nil) ; prepare for next
2112 ;; If a -sourcepath option was provided, parse it
2113 (if gud-jdb-sourcepath
2114 (setq gud-jdb-sourcepath
2115 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2117 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2118 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2119 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2120 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2121 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2122 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2123 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2124 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2125 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2127 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2128 (setq paragraph-start comint-prompt-regexp)
2129 (run-hooks 'jdb-mode-hook)
2131 (if gud-jdb-use-classpath
2132 ;; Get the classpath information from the debugger
2134 (if (string-match "-attach" command-line)
2135 (gud-call "classpath"))
2136 (fset 'gud-jdb-find-source
2137 'gud-jdb-find-source-using-classpath))
2139 ;; Else create and bind the class/source association list as well
2140 ;; as the source file list.
2141 (setq gud-jdb-class-source-alist
2142 (gud-jdb-build-class-source-alist
2143 (setq gud-jdb-source-files
2144 (gud-jdb-build-source-files-list gud-jdb-directories
2146 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2149 ;; ======================================================================
2151 ;; BASHDB support. See http://bashdb.sourceforge.net
2153 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2155 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2157 ;; INVOCATION NOTES:
2159 ;; You invoke bashdb-mode with:
2161 ;; M-x bashdb <enter>
2163 ;; It responds with:
2165 ;; Run bashdb (like this): bash
2168 ;; History of argument lists passed to bashdb.
2169 (defvar gud-bashdb-history nil)
2171 ;; Convert a command line as would be typed normally to run a script
2172 ;; into one that invokes an Emacs-enabled debugging session.
2173 ;; "--debugger" in inserted as the first switch.
2175 ;; There's no guarantee that Emacs will hand the filter the entire
2176 ;; marker at once; it could be broken up across several strings. We
2177 ;; might even receive a big chunk with several markers in it. If we
2178 ;; receive a chunk of text which looks like it might contain the
2179 ;; beginning of a marker, we save it here between calls to the
2181 (defun gud-bashdb-marker-filter (string)
2182 (setq gud-marker-acc (concat gud-marker-acc string))
2185 ;; Process all the complete markers in this chunk.
2186 ;; Format of line looks like this:
2187 ;; (/etc/init.d/ntp.init:16):
2188 ;; but we also allow DOS drive letters
2189 ;; (d:/etc/init.d/ntp.init:16):
2190 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2194 ;; Extract the frame position from the marker.
2196 (cons (match-string 2 gud-marker-acc)
2197 (string-to-number (match-string 4 gud-marker-acc)))
2199 ;; Append any text before the marker to the output we're going
2200 ;; to return - we don't include the marker in this text.
2201 output (concat output
2202 (substring gud-marker-acc 0 (match-beginning 0)))
2204 ;; Set the accumulator to the remaining text.
2205 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2207 ;; Does the remaining text look like it might end with the
2208 ;; beginning of another marker? If it does, then keep it in
2209 ;; gud-marker-acc until we receive the rest of it. Since we
2210 ;; know the full marker regexp above failed, it's pretty simple to
2211 ;; test for marker starts.
2212 (if (string-match "\032.*\\'" gud-marker-acc)
2214 ;; Everything before the potential marker start can be output.
2215 (setq output (concat output (substring gud-marker-acc
2216 0 (match-beginning 0))))
2218 ;; Everything after, we save, to combine with later input.
2219 (setq gud-marker-acc
2220 (substring gud-marker-acc (match-beginning 0))))
2222 (setq output (concat output gud-marker-acc)
2227 (defcustom gud-bashdb-command-name "bash --debugger"
2228 "File name for executing bash debugger."
2233 (defun bashdb (command-line)
2234 "Run bashdb on program FILE in buffer *gud-FILE*.
2235 The directory containing FILE becomes the initial working directory
2236 and source-file directory for your debugger."
2238 (list (read-from-minibuffer "Run bashdb (like this): "
2239 (if (consp gud-bashdb-history)
2240 (car gud-bashdb-history)
2241 (concat gud-bashdb-command-name
2243 gud-minibuffer-local-map nil
2244 '(gud-bashdb-history . 1))))
2246 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2248 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2250 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2251 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2252 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2253 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2254 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2255 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2256 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2257 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2258 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2259 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2262 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2264 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2265 (setq paragraph-start comint-prompt-regexp)
2266 (run-hooks 'bashdb-mode-hook)
2270 ;; End of debugger-specific information
2274 ;; When we send a command to the debugger via gud-call, it's annoying
2275 ;; to see the command and the new prompt inserted into the debugger's
2276 ;; buffer; we have other ways of knowing the command has completed.
2278 ;; If the buffer looks like this:
2279 ;; --------------------
2280 ;; (gdb) set args foo bar
2282 ;; --------------------
2283 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2284 ;; source file to set a breakpoint, we want the buffer to end up like
2286 ;; --------------------
2287 ;; (gdb) set args foo bar
2288 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2290 ;; --------------------
2291 ;; Essentially, the old prompt is deleted, and the command's output
2292 ;; and the new prompt take its place.
2294 ;; Not echoing the command is easy enough; you send it directly using
2295 ;; process-send-string, and it never enters the buffer. However,
2296 ;; getting rid of the old prompt is trickier; you don't want to do it
2297 ;; when you send the command, since that will result in an annoying
2298 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2299 ;; waits for a response from the debugger, and the new prompt is
2300 ;; inserted. Instead, we'll wait until we actually get some output
2301 ;; from the subprocess before we delete the prompt. If the command
2302 ;; produced no output other than a new prompt, that prompt will most
2303 ;; likely be in the first chunk of output received, so we will delete
2304 ;; the prompt and then replace it with an identical one. If the
2305 ;; command produces output, the prompt is moving anyway, so the
2306 ;; flicker won't be annoying.
2308 ;; So - when we want to delete the prompt upon receipt of the next
2309 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2310 ;; the start of the prompt; the process filter will notice this, and
2311 ;; delete all text between it and the process output marker. If
2312 ;; gud-delete-prompt-marker points nowhere, we leave the current
2314 (defvar gud-delete-prompt-marker nil)
2317 (put 'gud-mode 'mode-class 'special)
2319 (define-derived-mode gud-mode comint-mode "Debugger"
2320 "Major mode for interacting with an inferior debugger process.
2322 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2323 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2324 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2325 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2327 After startup, the following commands are available in both the GUD
2328 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2331 \\[gud-break] sets a breakpoint at the current file and line. In the
2332 GUD buffer, the current file and line are those of the last breakpoint or
2333 step. In a source buffer, they are the buffer's file and current line.
2335 \\[gud-remove] removes breakpoints on the current file and line.
2337 \\[gud-refresh] displays in the source window the last line referred to
2340 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2341 step-one-line (not entering function calls), and step-one-instruction
2342 and then update the source window with the current file and position.
2343 \\[gud-cont] continues execution.
2345 \\[gud-print] tries to find the largest C lvalue or function-call expression
2346 around point, and sends it to the debugger for value display.
2348 The above commands are common to all supported debuggers except xdb which
2349 does not support stepping instructions.
2351 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2352 except that the breakpoint is temporary; that is, it is removed when
2353 execution stops on it.
2355 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2356 frame. \\[gud-down] drops back down through one.
2358 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2359 the current function and stops.
2361 All the keystrokes above are accessible in the GUD buffer
2362 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2364 All pre-defined functions for which the concept make sense repeat
2365 themselves the appropriate number of times if you give a prefix
2368 You may use the `gud-def' macro in the initialization hook to define other
2371 Other commands for interacting with the debugger process are inherited from
2372 comint mode, which see."
2373 (setq mode-line-process '(":%s"))
2374 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2375 (set (make-local-variable 'gud-last-frame) nil)
2376 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2377 (make-local-variable 'comint-prompt-regexp)
2378 ;; Don't put repeated commands in command history many times.
2379 (set (make-local-variable 'comint-input-ignoredups) t)
2380 (make-local-variable 'paragraph-start)
2381 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2382 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2384 ;; Cause our buffers to be displayed, by default,
2385 ;; in the selected window.
2386 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2388 (defcustom gud-chdir-before-run t
2389 "Non-nil if GUD should `cd' to the debugged executable."
2393 (defvar gud-target-name "--unknown--"
2394 "The apparent name of the program being debugged in a gud buffer.")
2396 ;; Perform initializations common to all debuggers.
2397 ;; The first arg is the specified command line,
2398 ;; which starts with the program to debug.
2399 ;; The other three args specify the values to use
2400 ;; for local variables in the debugger buffer.
2401 (defun gud-common-init (command-line massage-args marker-filter
2402 &optional find-file)
2403 (let* ((words (split-string command-line))
2404 (program (car words))
2405 (dir default-directory)
2406 ;; Extract the file name from WORDS
2407 ;; and put t in its place.
2408 ;; Later on we will put the modified file name arg back there.
2409 (file-word (let ((w (cdr words)))
2410 (while (and w (= ?- (aref (car w) 0)))
2416 (and file-word (substitute-in-file-name file-word)))
2418 ;; If a directory was specified, expand the file name.
2419 ;; Otherwise, don't expand it, so GDB can use the PATH.
2420 ;; A file name without directory is literally valid
2421 ;; only if the file exists in ., and in that case,
2422 ;; omitting the expansion here has no visible effect.
2423 (file (and file-word
2424 (if (file-name-directory file-subst)
2425 (expand-file-name file-subst)
2427 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2428 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2429 (pop-to-buffer (concat "*gud" filepart "*"))
2430 (when (and existing-buffer (get-buffer-process existing-buffer))
2431 (error "This program is already running under gdb"))
2432 ;; Set the dir, in case the buffer already existed with a different dir.
2433 (setq default-directory dir)
2434 ;; Set default-directory to the file's directory.
2436 gud-chdir-before-run
2437 ;; Don't set default-directory if no directory was specified.
2438 ;; In that case, either the file is found in the current directory,
2439 ;; in which case this setq is a no-op,
2440 ;; or it is found by searching PATH,
2441 ;; in which case we don't know what directory it was found in.
2442 (file-name-directory file)
2443 (setq default-directory (file-name-directory file)))
2444 (or (bolp) (newline))
2445 (insert "Current directory is " default-directory "\n")
2446 ;; Put the substituted and expanded file name back in its place.
2448 (while (and w (not (eq (car w) t)))
2452 (apply 'make-comint (concat "gud" filepart) program nil
2453 (if massage-args (funcall massage-args file args) args))
2454 ;; Since comint clobbered the mode, we don't set it until now.
2456 (set (make-local-variable 'gud-target-name)
2457 (and file-word (file-name-nondirectory file))))
2458 (set (make-local-variable 'gud-marker-filter) marker-filter)
2459 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2460 (setq gud-running nil)
2461 (setq gud-last-last-frame nil)
2463 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2464 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2467 (defun gud-set-buffer ()
2468 (when (eq major-mode 'gud-mode)
2469 (setq gud-comint-buffer (current-buffer))))
2471 (defvar gud-filter-defer-flag nil
2472 "Non-nil means don't process anything from the debugger right now.
2473 It is saved for when this flag is not set.")
2475 ;; These functions are responsible for inserting output from your debugger
2476 ;; into the buffer. The hard work is done by the method that is
2477 ;; the value of gud-marker-filter.
2479 (defun gud-filter (proc string)
2480 ;; Here's where the actual buffer insertion is done
2481 (let (output process-window)
2482 (if (buffer-name (process-buffer proc))
2483 (if gud-filter-defer-flag
2484 ;; If we can't process any text now,
2485 ;; save it for later.
2486 (setq gud-filter-pending-text
2487 (concat (or gud-filter-pending-text "") string))
2489 ;; If we have to ask a question during the processing,
2490 ;; defer any additional text that comes from the debugger
2491 ;; during that time.
2492 (let ((gud-filter-defer-flag t))
2493 ;; Process now any text we previously saved up.
2494 (if gud-filter-pending-text
2495 (setq string (concat gud-filter-pending-text string)
2496 gud-filter-pending-text nil))
2498 (with-current-buffer (process-buffer proc)
2499 ;; If we have been so requested, delete the debugger prompt.
2502 (if (marker-buffer gud-delete-prompt-marker)
2504 (delete-region (process-mark proc)
2505 gud-delete-prompt-marker)
2506 (set-marker gud-delete-prompt-marker nil)))
2507 ;; Save the process output, checking for source file markers.
2508 (setq output (gud-marker-filter string))
2509 ;; Check for a filename-and-line number.
2510 ;; Don't display the specified file
2511 ;; unless (1) point is at or after the position where output appears
2512 ;; and (2) this buffer is on the screen.
2513 (setq process-window
2515 (>= (point) (process-mark proc))
2516 (get-buffer-window (current-buffer)))))
2518 ;; Let the comint filter do the actual insertion.
2519 ;; That lets us inherit various comint features.
2520 (comint-output-filter proc output))
2522 ;; Put the arrow on the source line.
2523 ;; This must be outside of the save-excursion
2524 ;; in case the source file is our current buffer.
2526 (save-selected-window
2527 (select-window process-window)
2528 (gud-display-frame))
2529 ;; We have to be in the proper buffer, (process-buffer proc),
2530 ;; but not in a save-excursion, because that would restore point.
2531 (let ((old-buf (current-buffer)))
2532 (set-buffer (process-buffer proc))
2535 (set-buffer old-buf)))))
2537 ;; If we deferred text that arrived during this processing,
2539 (if gud-filter-pending-text
2540 (gud-filter proc ""))))))
2542 (defvar gud-minor-mode-type nil)
2543 (defvar gud-overlay-arrow-position nil)
2544 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2546 (defun gud-sentinel (proc msg)
2547 (cond ((null (buffer-name (process-buffer proc)))
2549 ;; Stop displaying an arrow in a source file.
2550 (setq gud-overlay-arrow-position nil)
2551 (set-process-buffer proc nil)
2552 (if (memq gud-minor-mode-type '(gdbmi gdba))
2555 ((memq (process-status proc) '(signal exit))
2556 ;; Stop displaying an arrow in a source file.
2557 (setq gud-overlay-arrow-position nil)
2558 (with-current-buffer gud-comint-buffer
2559 (if (memq gud-minor-mode-type '(gdbmi gdba))
2562 (let* ((obuf (current-buffer)))
2563 ;; save-excursion isn't the right thing if
2564 ;; process-buffer is current-buffer
2567 ;; Write something in *compilation* and hack its mode line,
2568 (set-buffer (process-buffer proc))
2569 ;; Fix the mode line.
2570 (setq mode-line-process
2572 (symbol-name (process-status proc))))
2573 (force-mode-line-update)
2575 (insert ?\n mode-name " " msg)
2577 (goto-char (point-max))
2578 (insert ?\n mode-name " " msg)))
2579 ;; If buffer and mode line will show that the process
2580 ;; is dead, we can delete it now. Otherwise it
2581 ;; will stay around until M-x list-processes.
2582 (delete-process proc))
2583 ;; Restore old buffer, but don't restore old point
2584 ;; if obuf is the gud buffer.
2585 (set-buffer obuf))))))
2587 (defun gud-kill-buffer-hook ()
2588 (setq gud-minor-mode-type gud-minor-mode)
2590 (kill-process (get-buffer-process gud-comint-buffer))
2594 (dolist (buffer (buffer-list))
2595 (unless (eq buffer gud-comint-buffer)
2596 (with-current-buffer buffer
2597 (when gud-minor-mode
2598 (setq gud-minor-mode nil)
2599 (kill-local-variable 'tool-bar-map))))))
2601 (defun gud-display-frame ()
2602 "Find and obey the last filename-and-line marker from the debugger.
2603 Obeying it means displaying in another window the specified file and line."
2605 (when gud-last-frame
2607 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2608 (setq gud-last-last-frame gud-last-frame
2609 gud-last-frame nil)))
2611 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2612 ;; and that its line LINE is visible.
2613 ;; Put the overlay-arrow on the line LINE in that buffer.
2614 ;; Most of the trickiness in here comes from wanting to preserve the current
2615 ;; region-restriction if that's possible. We use an explicit display-buffer
2616 ;; to get around the fact that this is called inside a save-excursion.
2618 (defun gud-display-line (true-file line)
2619 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2621 (with-current-buffer gud-comint-buffer
2622 (gud-find-file true-file)))
2623 (window (and buffer (or (get-buffer-window buffer)
2624 (display-buffer buffer))))
2628 (with-current-buffer buffer
2629 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2631 (format "File %s changed on disk. Reread from disk? "
2634 (setq gud-keep-buffer t)))
2639 (or gud-overlay-arrow-position
2640 (setq gud-overlay-arrow-position (make-marker)))
2641 (set-marker gud-overlay-arrow-position (point) (current-buffer)))
2642 (cond ((or (< pos (point-min)) (> pos (point-max)))
2645 (if window (set-window-point window gud-overlay-arrow-position))))))
2647 ;; The gud-call function must do the right thing whether its invoking
2648 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2649 ;; or a C buffer. In the former case, we want to supply data from
2650 ;; gud-last-frame. Here's how we do it:
2652 (defun gud-format-command (str arg)
2653 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2654 (frame (or gud-last-frame gud-last-last-frame))
2656 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2657 (let ((key (string-to-char (match-string 2 str)))
2661 (setq subst (file-name-nondirectory (if insource
2665 (setq subst (file-name-sans-extension
2666 (file-name-nondirectory (if insource
2670 (setq subst (file-name-directory (if insource
2674 (setq subst (int-to-string
2678 (+ (count-lines (point-min) (point))
2682 (setq subst (gud-find-expr)))
2684 (setq subst (gud-read-address)))
2694 (+ (count-lines (point-min) (point))
2698 (setq subst (if arg (int-to-string arg)))))
2699 (setq result (concat result (match-string 1 str) subst)))
2700 (setq str (substring str (match-end 2))))
2701 ;; There might be text left in STR when the loop ends.
2702 (concat result str)))
2704 (defun gud-read-address ()
2705 "Return a string containing the core-address found in the buffer at point."
2708 (let ((pt (point)) found begin)
2709 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2711 (found (forward-char 2)
2712 (buffer-substring found
2713 (progn (re-search-forward "[^0-9a-f]")
2716 (t (setq begin (progn (re-search-backward "[^0-9]")
2720 (re-search-forward "[^0-9]")
2722 (buffer-substring begin (point))))))))
2724 (defun gud-call (fmt &optional arg)
2725 (let ((msg (gud-format-command fmt arg)))
2726 (message "Command: %s" msg)
2728 (gud-basic-call msg)))
2730 (defun gud-basic-call (command)
2731 "Invoke the debugger COMMAND displaying source in other window."
2734 (let ((proc (get-buffer-process gud-comint-buffer)))
2735 (or proc (error "Current buffer has no process"))
2736 ;; Arrange for the current prompt to get deleted.
2738 (set-buffer gud-comint-buffer)
2741 (goto-char (process-mark proc))
2743 (if (looking-at comint-prompt-regexp)
2744 (set-marker gud-delete-prompt-marker (point)))
2745 (if (memq gud-minor-mode '(gdbmi gdba))
2746 (apply comint-input-sender (list proc command))
2747 (process-send-string proc (concat command "\n")))))))
2749 (defun gud-refresh (&optional arg)
2750 "Fix up a possibly garbled display, and redraw the arrow."
2752 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2756 ;; Code for parsing expressions out of C or Fortran code. The single entry
2757 ;; point is gud-find-expr, which tries to return an lvalue expression from
2760 (defvar gud-find-expr-function 'gud-find-c-expr)
2762 (defun gud-find-expr (&rest args)
2763 (apply gud-find-expr-function args))
2765 ;; The next eight functions are hacked from gdbsrc.el by
2766 ;; Debby Ayers <ayers@asc.slb.com>,
2767 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2769 (defun gud-find-c-expr ()
2770 "Returns the expr that surrounds point."
2774 (expr (gud-innermost-expr))
2775 (test-expr (gud-prev-expr)))
2776 (while (and test-expr (gud-expr-compound test-expr expr))
2777 (let ((prev-expr expr))
2778 (setq expr (cons (car test-expr) (cdr expr)))
2779 (goto-char (car expr))
2780 (setq test-expr (gud-prev-expr))
2781 ;; If we just pasted on the condition of an if or while,
2782 ;; throw it away again.
2783 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2784 '("if" "while" "for"))
2788 (setq test-expr (gud-next-expr))
2789 (while (gud-expr-compound expr test-expr)
2790 (setq expr (cons (car expr) (cdr test-expr)))
2791 (setq test-expr (gud-next-expr)))
2792 (buffer-substring (car expr) (cdr expr)))))
2794 (defun gud-innermost-expr ()
2795 "Returns the smallest expr that point is in; move point to beginning of it.
2796 The expr is represented as a cons cell, where the car specifies the point in
2797 the current buffer that marks the beginning of the expr and the cdr specifies
2798 the character after the end of the expr."
2799 (let ((p (point)) begin end)
2801 (setq begin (point))
2814 (defun gud-backward-sexp ()
2815 "Version of `backward-sexp' that catches errors."
2820 (defun gud-forward-sexp ()
2821 "Version of `forward-sexp' that catches errors."
2826 (defun gud-prev-expr ()
2827 "Returns the previous expr, point is set to beginning of that expr.
2828 The expr is represented as a cons cell, where the car specifies the point in
2829 the current buffer that marks the beginning of the expr and the cdr specifies
2830 the character after the end of the expr"
2831 (let ((begin) (end))
2833 (setq begin (point))
2839 (defun gud-next-expr ()
2840 "Returns the following expr, point is set to beginning of that expr.
2841 The expr is represented as a cons cell, where the car specifies the point in
2842 the current buffer that marks the beginning of the expr and the cdr specifies
2843 the character after the end of the expr."
2844 (let ((begin) (end))
2849 (setq begin (point))
2852 (defun gud-expr-compound-sep (span-start span-end)
2853 "Scan from SPAN-START to SPAN-END for punctuation characters.
2854 If `->' is found, return `?.'. If `.' is found, return `?.'.
2855 If any other punctuation is found, return `??'.
2856 If no punctuation is found, return `? '."
2859 (while (< span-start span-end)
2860 (setq syntax (char-syntax (char-after span-start)))
2863 ((= syntax ?.) (setq syntax (char-after span-start))
2865 ((= syntax ?.) (setq result ?.))
2866 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2868 (setq span-start (+ span-start 1)))
2869 (t (setq span-start span-end)
2870 (setq result ??)))))
2871 (setq span-start (+ span-start 1)))
2874 (defun gud-expr-compound (first second)
2875 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2876 The two exprs are represented as a cons cells, where the car
2877 specifies the point in the current buffer that marks the beginning of the
2878 expr and the cdr specifies the character after the end of the expr.
2879 Link exprs of the form:
2886 (let ((span-start (cdr first))
2887 (span-end (car second))
2889 (setq syntax (gud-expr-compound-sep span-start span-end))
2891 ((= (car first) (car second)) nil)
2892 ((= (cdr first) (cdr second)) nil)
2895 (setq span-start (char-after (- span-start 1)))
2896 (setq span-end (char-after span-end))
2898 ((= span-start ?)) t)
2899 ((= span-start ?]) t)
2905 (defun gud-find-class (f line)
2906 "Find fully qualified class in file F at line LINE.
2907 This function uses the `gud-jdb-classpath' (and optional
2908 `gud-jdb-sourcepath') list(s) to derive a file
2909 pathname relative to its classpath directory. The values in
2910 `gud-jdb-classpath' are assumed to have been converted to absolute
2911 pathname standards using file-truename.
2912 If F is visited by a buffer and its mode is CC-mode(Java),
2913 syntactic information of LINE is used to find the enclosing (nested)
2914 class string which is appended to the top level
2915 class of the file (using s to separate nested class ids)."
2916 ;; Convert f to a standard representation and remove suffix
2917 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
2919 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2920 (fbuffer (get-file-buffer f))
2921 syntax-symbol syntax-point class-found)
2922 (setq f (file-name-sans-extension (file-truename f)))
2923 ;; Syntax-symbol returns the symbol of the *first* element
2924 ;; in the syntactical analysis result list, syntax-point
2925 ;; returns the buffer position of same
2926 (fset 'syntax-symbol (lambda (x) (c-langelem-sym (car x))))
2927 (fset 'syntax-point (lambda (x) (c-langelem-pos (car x))))
2928 ;; Search through classpath list for an entry that is
2930 (while (and cplist (not class-found))
2931 (if (string-match (car cplist) f)
2933 (mapconcat 'identity
2935 (substring f (+ (match-end 0) 1))
2937 (setq cplist (cdr cplist)))
2938 ;; if f is visited by a java(cc-mode) buffer, walk up the
2939 ;; syntactic information chain and collect any 'inclass
2940 ;; symbols until 'topmost-intro is reached to find out if
2941 ;; point is within a nested class
2942 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
2944 (set-buffer fbuffer)
2945 (let ((nclass) (syntax))
2946 ;; While the c-syntactic information does not start
2947 ;; with the 'topmost-intro symbol, there may be
2948 ;; nested classes...
2949 (while (not (eq 'topmost-intro
2950 (syntax-symbol (c-guess-basic-syntax))))
2951 ;; Check if the current position c-syntactic
2952 ;; analysis has 'inclass
2953 (setq syntax (c-guess-basic-syntax))
2955 (and (not (eq 'inclass (syntax-symbol syntax)))
2957 (setq syntax (cdr syntax)))
2958 (if (eq 'inclass (syntax-symbol syntax))
2960 (goto-char (syntax-point syntax))
2961 ;; Now we're at the beginning of a class
2962 ;; definition. Find class name
2964 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
2966 (append (list (match-string-no-properties 1))
2968 (setq syntax (c-guess-basic-syntax))
2969 (while (and (not (syntax-point syntax)) (cdr syntax))
2970 (setq syntax (cdr syntax)))
2971 (goto-char (syntax-point syntax))
2973 (string-match (concat (car nclass) "$") class-found)
2975 (replace-match (mapconcat 'identity nclass "$")
2976 t t class-found)))))
2977 (if (not class-found)
2978 (message "gud-find-class: class for file %s not found!" f))
2980 ;; Not using classpath - try class/source association list
2981 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
2984 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
2988 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2989 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2990 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2992 (defvar gdb-script-mode-syntax-table
2993 (let ((st (make-syntax-table)))
2994 (modify-syntax-entry ?' "\"" st)
2995 (modify-syntax-entry ?# "<" st)
2996 (modify-syntax-entry ?\n ">" st)
2999 (defvar gdb-script-font-lock-keywords
3000 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
3001 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
3002 ("^\\s-*\\([a-z]+\\)" (1 font-lock-keyword-face))))
3004 (defvar gdb-script-font-lock-syntactic-keywords
3005 '(("^document\\s-.*\\(\n\\)" (1 "< b"))
3006 ;; It would be best to change the \n in front, but it's more difficult.
3007 ("^en\\(d\\)\\>" (1 "> b"))))
3009 (defun gdb-script-font-lock-syntactic-face (state)
3011 ((nth 3 state) font-lock-string-face)
3012 ((nth 7 state) font-lock-doc-face)
3013 (t font-lock-comment-face)))
3015 (defvar gdb-script-basic-indent 2)
3017 (defun gdb-script-skip-to-head ()
3018 "We're just in front of an `end' and we need to go to its head."
3019 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\)\\>" nil 'move)
3021 (gdb-script-skip-to-head)))
3023 (defun gdb-script-calculate-indentation ()
3025 ((looking-at "end\\>")
3026 (gdb-script-skip-to-head)
3027 (current-indentation))
3028 ((looking-at "else\\>")
3029 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3031 (gdb-script-skip-to-head))
3032 (current-indentation))
3034 (forward-comment (- (point-max)))
3036 (skip-chars-forward " \t")
3037 (+ (current-indentation)
3038 (if (looking-at "\\(if\\|while\\|define\\|else\\)\\>")
3039 gdb-script-basic-indent 0)))))
3041 (defun gdb-script-indent-line ()
3042 "Indent current line of GDB script."
3044 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3047 (skip-chars-forward " \t")
3048 (not (looking-at "end\\>"))))
3050 (let* ((savep (point))
3051 (indent (condition-case nil
3054 (skip-chars-forward " \t")
3055 (if (>= (point) savep) (setq savep nil))
3056 (max (gdb-script-calculate-indentation) 0))
3059 (save-excursion (indent-line-to indent))
3060 (indent-line-to indent)))))
3062 ;; Derived from cfengine.el.
3063 (defun gdb-script-beginning-of-defun ()
3064 "`beginning-of-defun' function for Gdb script mode.
3065 Treats actions as defuns."
3066 (unless (<= (current-column) (current-indentation))
3068 (if (re-search-backward "^define \\|^document " nil t)
3070 (goto-char (point-min)))
3073 ;; Derived from cfengine.el.
3074 (defun gdb-script-end-of-defun ()
3075 "`end-of-defun' function for Gdb script mode.
3076 Treats actions as defuns."
3078 (if (re-search-forward "^end" nil t)
3080 (goto-char (point-max)))
3084 (add-to-list 'auto-mode-alist '("/\\.gdbinit" . gdb-script-mode))
3087 (define-derived-mode gdb-script-mode nil "GDB-Script"
3088 "Major mode for editing GDB scripts"
3089 (set (make-local-variable 'comment-start) "#")
3090 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3091 (set (make-local-variable 'outline-regexp) "[ \t]")
3092 (set (make-local-variable 'imenu-generic-expression)
3093 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3094 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3095 (set (make-local-variable 'beginning-of-defun-function)
3096 #'gdb-script-beginning-of-defun)
3097 (set (make-local-variable 'end-of-defun-function)
3098 #'gdb-script-end-of-defun)
3099 (set (make-local-variable 'font-lock-defaults)
3100 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3101 (font-lock-syntactic-keywords
3102 . gdb-script-font-lock-syntactic-keywords)
3103 (font-lock-syntactic-face-function
3104 . gdb-script-font-lock-syntactic-face))))
3107 ;;; tooltips for GUD
3109 ;;; Customizable settings
3110 (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode)
3111 "List of modes for which to enable GUD tips."
3116 (defcustom gud-tooltip-display
3117 '((eq (tooltip-event-buffer gud-tooltip-event)
3118 (marker-buffer gud-overlay-arrow-position)))
3119 "List of forms determining where GUD tooltips are displayed.
3121 Forms in the list are combined with AND. The default is to display
3122 only tooltips in the buffer containing the overlay arrow."
3124 :tag "GUD buffers predicate"
3127 (defcustom gud-tooltip-echo-area nil
3128 "Use the echo area instead of frames for GUD tooltips."
3130 :tag "Use echo area"
3133 (define-obsolete-variable-alias 'tooltip-gud-modes
3134 'gud-tooltip-modes "22.1")
3135 (define-obsolete-variable-alias 'tooltip-gud-display
3136 'gud-tooltip-display "22.1")
3137 (define-obsolete-variable-alias 'tooltip-use-echo-area
3138 'gud-tooltip-echo-area "22.1")
3140 ;;; Reacting on mouse movements
3142 (defun gud-tooltip-change-major-mode ()
3143 "Function added to `change-major-mode-hook' when tooltip mode is on."
3144 (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
3146 (defun gud-tooltip-activate-mouse-motions-if-enabled ()
3147 "Reconsider for all buffers whether mouse motion events are desired."
3148 (remove-hook 'post-command-hook
3149 'gud-tooltip-activate-mouse-motions-if-enabled)
3150 (dolist (buffer (buffer-list))
3153 (if (and gud-tooltip-mode
3154 (memq major-mode gud-tooltip-modes))
3155 (gud-tooltip-activate-mouse-motions t)
3156 (gud-tooltip-activate-mouse-motions nil)))))
3158 (defvar gud-tooltip-mouse-motions-active nil
3159 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
3161 (defun gud-tooltip-activate-mouse-motions (activatep)
3162 "Activate/deactivate mouse motion events for the current buffer.
3163 ACTIVATEP non-nil means activate mouse motion events."
3166 (make-local-variable 'gud-tooltip-mouse-motions-active)
3167 (setq gud-tooltip-mouse-motions-active t)
3168 (make-local-variable 'track-mouse)
3169 (setq track-mouse t))
3170 (when gud-tooltip-mouse-motions-active
3171 (kill-local-variable 'gud-tooltip-mouse-motions-active)
3172 (kill-local-variable 'track-mouse))))
3174 (defun gud-tooltip-mouse-motion (event)
3175 "Command handler for mouse movement events in `global-map'."
3178 (when (car (mouse-pixel-position))
3179 (setq tooltip-last-mouse-motion-event (copy-sequence event))
3180 (tooltip-start-delayed-tip)))
3184 (defvar gud-tooltip-original-filter nil
3185 "Process filter to restore after GUD output has been received.")
3187 (defvar gud-tooltip-dereference nil
3188 "Non-nil means print expressions with a `*' in front of them.
3189 For C this would dereference a pointer expression.")
3191 (defvar gud-tooltip-event nil
3192 "The mouse movement event that led to a tooltip display.
3193 This event can be examined by forms in GUD-TOOLTIP-DISPLAY.")
3195 (defun gud-tooltip-toggle-dereference ()
3196 "Toggle whether tooltips should show `* expr' or `expr'."
3198 (setq gud-tooltip-dereference (not gud-tooltip-dereference))
3199 (when (interactive-p)
3200 (message "Dereferencing is now %s."
3201 (if gud-tooltip-dereference "on" "off"))))
3203 (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
3204 'gud-tooltip-toggle-dereference "22.1")
3206 (define-minor-mode gud-tooltip-mode
3207 "Toggle the display of GUD tooltips."
3210 (if gud-tooltip-mode
3212 (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3213 (add-hook 'pre-command-hook 'tooltip-hide)
3214 (add-hook 'tooltip-hook 'gud-tooltip-tips)
3215 (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
3216 (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
3217 (remove-hook 'change-major-mode-hook 'tooltip-change-major-mode)
3218 (remove-hook 'tooltip-hook 'gud-tooltip-tips)
3219 (define-key global-map [mouse-movement] 'ignore)))
3220 (gud-tooltip-activate-mouse-motions-if-enabled)
3221 (if (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba))
3222 (if gud-tooltip-mode
3224 (dolist (buffer (buffer-list))
3225 (unless (eq buffer gud-comint-buffer)
3226 (with-current-buffer buffer
3227 (when (and (memq gud-minor-mode '(gdbmi gdba))
3228 (not (string-match "\\`\\*.+\\*\\'"
3230 (make-local-variable 'gdb-define-alist)
3231 (gdb-create-define-alist)
3232 (add-hook 'after-save-hook
3233 'gdb-create-define-alist nil t))))))
3234 (kill-local-variable 'gdb-define-alist)
3235 (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
3237 ; This will only display data that comes in one chunk.
3238 ; Larger arrays (say 400 elements) are displayed in
3239 ; the tootip incompletely and spill over into the gud buffer.
3240 ; Switching the process-filter creates timing problems and
3241 ; it may be difficult to do better. Using annotations as in
3242 ; gdb-ui.el gets round this problem.
3243 (defun gud-tooltip-process-output (process output)
3244 "Process debugger output and show it in a tooltip window."
3245 (set-process-filter process gud-tooltip-original-filter)
3246 (tooltip-show (tooltip-strip-prompt process output)
3247 gud-tooltip-echo-area))
3249 (defun gud-tooltip-print-command (expr)
3250 "Return a suitable command to print the expression EXPR.
3251 If GUD-TOOLTIP-DEREFERENCE is t, also prepend a `*' to EXPR."
3252 (when gud-tooltip-dereference
3253 (setq expr (concat "*" expr)))
3254 (case gud-minor-mode
3255 ((gdb gdba) (concat "server print " expr))
3256 (dbx (concat "print " expr))
3257 (xdb (concat "p " expr))
3258 (sdb (concat expr "/"))
3261 (defun gud-tooltip-tips (event)
3262 "Show tip for identifier or selection under the mouse.
3263 The mouse must either point at an identifier or inside a selected
3264 region for the tip window to be shown. If gud-tooltip-dereference is t,
3265 add a `*' in front of the printed expression. In the case of a C program
3266 controlled by GDB, show the associated #define directives when program is
3269 This function must return nil if it doesn't handle EVENT."
3271 (when (and (eventp event)
3273 (boundp 'gud-comint-buffer)
3275 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
3276 (setq process (get-buffer-process gud-comint-buffer))
3277 (posn-point (event-end event))
3278 (or (and (eq gud-minor-mode 'gdba) (not gdb-active-process))
3279 (progn (setq gud-tooltip-event event)
3280 (eval (cons 'and gud-tooltip-display)))))
3281 (let ((expr (tooltip-expr-to-print event)))
3283 (if (and (eq gud-minor-mode 'gdba)
3284 (not gdb-active-process))
3286 (with-current-buffer
3287 (window-buffer (let ((mouse (mouse-position)))
3288 (window-at (cadr mouse)
3290 (let ((define-elt (assoc expr gdb-define-alist)))
3291 (unless (null define-elt)
3292 (tooltip-show (cdr define-elt))
3294 (let ((cmd (gud-tooltip-print-command expr)))
3295 (unless (null cmd) ; CMD can be nil if unknown debugger
3296 (if (eq gud-minor-mode 'gdba)
3298 (list (concat cmd "\n") 'gdb-tooltip-print))
3299 (setq gud-tooltip-original-filter (process-filter process))
3300 (set-process-filter process 'gud-tooltip-process-output)
3301 (gud-basic-call cmd))
3306 ;;; arch-tag: 6d990948-df65-461a-be39-1c7fb83ac4c4
3307 ;;; gud.el ends here