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, 1993, 1994, 1995, 1996, 1998, 2000, 2001, 2002, 2003,
8 ;; 2004, 2005 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu> It was
30 ;; later rewritten by rms. Some ideas were due to Masanobu. Grand
31 ;; Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com> Barry
32 ;; Warsaw <bwarsaw@cen.com> hacked the mode to use comint.el. Shane Hartman
33 ;; <shane@spr.com> added support for xdb (HPUX debugger). Rick Sladkey
34 ;; <jrs@world.std.com> wrote the GDB command completion code. Dave Love
35 ;; <d.love@dl.ac.uk> added the IRIX kluge, re-implemented the Mips-ish variant
36 ;; and added a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX
37 ;; kluge with the gud-xdb-directories hack producing gud-dbx-directories.
38 ;; Derek L. Davies <ddavies@world.std.com> added support for jdb (Java
43 (eval-when-compile (require 'cl)) ; for case macro
48 (defvar gdb-active-process)
49 (defvar gdb-define-alist)
50 (defvar gdb-macro-info)
51 (defvar gdb-server-prefix)
52 (defvar gdb-show-changed-values)
53 (defvar gdb-var-changed)
57 ;; ======================================================================
58 ;; GUD commands must be visible in C buffers visited by GUD
61 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
62 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
67 (defcustom gud-key-prefix "\C-x\C-a"
68 "Prefix of all GUD commands valid in C buffers."
72 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
73 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
75 (defvar gud-marker-filter nil)
76 (put 'gud-marker-filter 'permanent-local t)
77 (defvar gud-find-file nil)
78 (put 'gud-find-file 'permanent-local t)
80 (defun gud-marker-filter (&rest args)
81 (apply gud-marker-filter args))
83 (defvar gud-minor-mode nil)
84 (put 'gud-minor-mode 'permanent-local t)
86 (defvar gud-keep-buffer nil)
88 (defun gud-symbol (sym &optional soft minor-mode)
89 "Return the symbol used for SYM in MINOR-MODE.
90 MINOR-MODE defaults to `gud-minor-mode.
91 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
92 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
93 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
94 (funcall (if soft 'intern-soft 'intern)
95 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
97 (defun gud-val (sym &optional minor-mode)
98 "Return the value of `gud-symbol' SYM. Default to nil."
99 (let ((sym (gud-symbol sym t minor-mode)))
100 (if (boundp sym) (symbol-value sym))))
102 (defvar gud-running nil
103 "Non-nil if debuggee is running.
104 Used to grey out relevant togolbar icons.")
106 ;; Use existing Info buffer, if possible.
107 (defun gud-goto-info ()
108 "Go to relevant Emacs info node."
110 (let ((same-window-regexps same-window-regexps)
111 (display-buffer-reuse-frames t))
115 (if (eq (window-buffer window) (get-buffer "*info*"))
117 (setq same-window-regexps nil)
118 (throw 'info-found nil))))
120 (select-frame (make-frame)))
121 (if (memq gud-minor-mode '(gdbmi gdba))
122 (info "(emacs)GDB Graphical Interface")
123 (info "(emacs)Debuggers"))))
125 (easy-mmode-defmap gud-menu-map
126 '(([help] "Info" . gud-goto-info)
127 ([tooltips] menu-item "Toggle GUD tooltips" gud-tooltip-mode
128 :enable (and (not emacs-basic-display)
130 (fboundp 'x-show-tip))
131 :button (:toggle . gud-tooltip-mode))
132 ([refresh] "Refresh" . gud-refresh)
133 ([run] menu-item "Run" gud-run
134 :enable (and (not gud-running)
135 (memq gud-minor-mode '(gdbmi gdba gdb dbx jdb))))
136 ([until] menu-item "Continue to selection" gud-until
137 :enable (and (not gud-running)
138 (memq gud-minor-mode '(gdbmi gdba gdb perldb))))
139 ([remove] menu-item "Remove Breakpoint" gud-remove
140 :enable (not gud-running)
141 :visible (not (and (memq gud-minor-mode '(gdbmi gdba))
143 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
144 :enable (memq gud-minor-mode '(gdbmi gdba gdb sdb xdb bashdb)))
145 ([break] menu-item "Set Breakpoint" gud-break
146 :enable (not gud-running)
147 :visible (not (and (memq gud-minor-mode '(gdbmi gdba))
149 ([up] menu-item "Up Stack" gud-up
150 :enable (and (not gud-running)
152 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
153 ([down] menu-item "Down Stack" gud-down
154 :enable (and (not gud-running)
156 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
157 ([print*] menu-item "Print Dereference" gud-pstar
158 :enable (and (not gud-running)
159 (memq gud-minor-mode '(gdbmi gdba gdb))))
160 ([print] menu-item "Print Expression" gud-print
161 :enable (not gud-running))
162 ([watch] menu-item "Watch Expression" gud-watch
163 :enable (and (not gud-running)
164 (memq gud-minor-mode '(gdbmi gdba))))
165 ([finish] menu-item "Finish Function" gud-finish
166 :enable (and (not gud-running)
168 '(gdbmi gdba gdb xdb jdb pdb bashdb))))
169 ([stepi] menu-item "Step Instruction" gud-stepi
170 :enable (and (not gud-running)
171 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
172 ([nexti] menu-item "Next Instruction" gud-nexti
173 :enable (and (not gud-running)
174 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
175 ([step] menu-item "Step Line" gud-step
176 :enable (not gud-running))
177 ([next] menu-item "Next Line" gud-next
178 :enable (not gud-running))
179 ([cont] menu-item "Continue" gud-cont
180 :enable (not gud-running)))
181 "Menu for `gud-mode'."
184 (easy-mmode-defmap gud-minor-mode-map
185 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
186 "Map used in visited files.")
188 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
189 (if m (setcdr m gud-minor-mode-map)
190 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
193 ;; Will inherit from comint-mode via define-derived-mode.
195 "`gud-mode' keymap.")
197 (defvar gud-tool-bar-map
198 (if (display-graphic-p)
199 (let ((map (make-sparse-keymap)))
200 (dolist (x '((gud-break . "gud-break")
201 (gud-remove . "gud-remove")
202 (gud-print . "gud-print")
203 (gud-pstar . "gud-pstar")
204 (gud-watch . "gud-watch")
205 (gud-cont . "gud-cont")
206 (gud-until . "gud-until")
207 (gud-finish . "gud-finish")
208 (gud-run . "gud-run")
209 ;; gud-s, gud-si etc. instead of gud-step,
210 ;; gud-stepi, to avoid file-name clashes on DOS
214 (gud-nexti . "gud-ni")
215 (gud-stepi . "gud-si")
217 (gud-down . "gud-down")
218 (gud-goto-info . "info"))
220 (tool-bar-local-item-from-menu
221 (car x) (cdr x) map gud-minor-mode-map)))))
223 (defun gud-file-name (f)
224 "Transform a relative file name to an absolute file name.
225 Uses `gud-<MINOR-MODE>-directories' to find the source files."
226 (if (file-exists-p f) (expand-file-name f)
227 (let ((directories (gud-val 'directories))
230 (let ((path (expand-file-name f (car directories))))
231 (if (file-exists-p path)
234 (setq directories (cdr directories)))
237 (defun gud-find-file (file)
238 ;; Don't get confused by double slashes in the name that comes from GDB.
239 (while (string-match "//+" file)
240 (setq file (replace-match "/" t t file)))
241 (let ((minor-mode gud-minor-mode)
242 (buf (funcall (or gud-find-file 'gud-file-name) file)))
244 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
246 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
247 (with-current-buffer buf
248 (set (make-local-variable 'gud-minor-mode) minor-mode)
249 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
250 (when (and gud-tooltip-mode
251 (memq gud-minor-mode '(gdbmi gdba)))
252 (make-local-variable 'gdb-define-alist)
253 (unless gdb-define-alist (gdb-create-define-alist))
254 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
255 (make-local-variable 'gud-keep-buffer))
258 ;; ======================================================================
259 ;; command definition
261 ;; This macro is used below to define some basic debugger interface commands.
262 ;; Of course you may use `gud-def' with any other debugger command, including
263 ;; user defined ones.
265 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
266 ;; which defines FUNC to send the command NAME to the debugger, gives
267 ;; it the docstring DOC, and binds that function to KEY in the GUD
268 ;; major mode. The function is also bound in the global keymap with the
271 (defmacro gud-def (func cmd key &optional doc)
272 "Define FUNC to be a command sending STR and bound to KEY, with
273 optional doc string DOC. Certain %-escapes in the string arguments
274 are interpreted specially if present. These are:
276 %f name (without directory) of current source file.
277 %F name (without directory or extension) of current source file.
278 %d directory of current source file.
279 %l number of current source line
280 %e text of the C lvalue or function-call expression surrounding point.
281 %a text of the hexadecimal address surrounding point
282 %p prefix argument to the command (if any) as a number
284 The `current' source file is the file of the current buffer (if
285 we're in a C file) or the source file current at the last break or
286 step (if we're in the GUD buffer).
287 The `current' line is that of the current buffer (if we're in a
288 source file) or the source line number at the last break or step (if
289 we're in the GUD buffer)."
292 ,@(if doc (list doc))
297 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
298 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
300 ;; Where gud-display-frame should put the debugging arrow; a cons of
301 ;; (filename . line-number). This is set by the marker-filter, which scans
302 ;; the debugger's output for indications of the current program counter.
303 (defvar gud-last-frame nil)
305 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
306 ;; the last frame, even if it's been called before and gud-last-frame has
308 (defvar gud-last-last-frame nil)
310 ;; All debugger-specific information is collected here.
311 ;; Here's how it works, in case you ever need to add a debugger to the mode.
313 ;; Each entry must define the following at startup:
316 ;; comint-prompt-regexp
317 ;; gud-<name>-massage-args
318 ;; gud-<name>-marker-filter
319 ;; gud-<name>-find-file
321 ;; The job of the massage-args method is to modify the given list of
322 ;; debugger arguments before running the debugger.
324 ;; The job of the marker-filter method is to detect file/line markers in
325 ;; strings and set the global gud-last-frame to indicate what display
326 ;; action (if any) should be triggered by the marker. Note that only
327 ;; whatever the method *returns* is displayed in the buffer; thus, you
328 ;; can filter the debugger's output, interpreting some and passing on
331 ;; The job of the find-file method is to visit and return the buffer indicated
332 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
335 ;; ======================================================================
336 ;; speedbar support functions and variables.
337 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
339 (defvar gud-last-speedbar-buffer nil
340 "The last GUD buffer used.")
342 (defvar gud-last-speedbar-stackframe nil
343 "Description of the currently displayed GUD stack.
344 t means that there is no stack, and we are in display-file mode.")
346 (defvar gud-speedbar-key-map nil
347 "Keymap used when in the buffers display mode.")
349 (defun gud-install-speedbar-variables ()
350 "Install those variables used by speedbar to enhance gud/gdb."
351 (if gud-speedbar-key-map
353 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
355 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
356 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
357 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
358 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)))
360 (defvar gud-speedbar-menu-items
361 '(["Jump to stack frame" speedbar-edit-line
362 :visible (with-current-buffer gud-comint-buffer
363 (not (memq gud-minor-mode '(gdbmi gdba))))]
364 ["Edit value" speedbar-edit-line
365 :visible (with-current-buffer gud-comint-buffer
366 (memq gud-minor-mode '(gdbmi gdba)))]
367 ["Delete expression" gdb-var-delete
368 (with-current-buffer gud-comint-buffer
369 (memq gud-minor-mode '(gdbmi gdba)))])
370 "Additional menu items to add to the speedbar frame.")
372 ;; Make sure our special speedbar mode is loaded
373 (if (featurep 'speedbar)
374 (gud-install-speedbar-variables)
375 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
377 (defun gud-speedbar-buttons (buffer)
378 "Create a speedbar display based on the current state of GUD.
379 If the GUD BUFFER is not running a supported debugger, then turn
380 off the specialized speedbar mode."
381 (let ((minor-mode (with-current-buffer buffer gud-minor-mode)))
383 ((memq minor-mode '(gdbmi gdba))
384 (when (or gdb-var-changed
386 (goto-char (point-min))
387 (let ((case-fold-search t))
388 (looking-at "Watch Expressions:")))))
390 (insert "Watch Expressions:\n")
391 (let ((var-list gdb-var-list))
393 (let* ((depth 0) (start 0) (char ?+)
394 (var (car var-list)) (varnum (nth 1 var)))
395 (while (string-match "\\." varnum start)
396 (setq depth (1+ depth)
397 start (1+ (match-beginning 0))))
398 (if (equal (nth 2 var) "0")
399 (speedbar-make-tag-line 'bracket ?? nil nil
400 (concat (car var) "\t" (nth 4 var))
404 gdb-show-changed-values)
405 'font-lock-warning-face
407 (if (and (cadr var-list)
408 (string-match varnum (cadr (cadr var-list))))
410 (speedbar-make-tag-line 'bracket char
411 'gdb-speedbar-expand-node varnum
412 (concat (car var) "\t" (nth 3 var))
414 (setq var-list (cdr var-list))))
415 (setq gdb-var-changed nil)))
416 (t (if (and (save-excursion
417 (goto-char (point-min))
418 (looking-at "Current Stack"))
419 (equal gud-last-last-frame gud-last-speedbar-stackframe))
421 (setq gud-last-speedbar-buffer buffer)
422 (let ((gud-frame-list
423 (cond ((eq minor-mode 'gdb)
424 (gud-gdb-get-stackframe buffer))
425 ;; Add more debuggers here!
426 (t (speedbar-remove-localized-speedbar-support buffer)
429 (if (not gud-frame-list)
430 (insert "No Stack frames\n")
431 (insert "Current Stack:\n"))
432 (dolist (frame gud-frame-list)
433 (insert (nth 1 frame) ":\n")
434 (if (= (length frame) 2)
436 ; (speedbar-insert-button "[?]"
437 ; 'speedbar-button-face
439 (speedbar-insert-button (car frame)
440 'speedbar-directory-face
442 ; (speedbar-insert-button "[+]"
443 ; 'speedbar-button-face
444 ; 'speedbar-highlight-face
445 ; 'gud-gdb-get-scope-data
447 (speedbar-insert-button (car frame)
449 'speedbar-highlight-face
450 (cond ((memq minor-mode '(gdbmi gdba gdb))
451 'gud-gdb-goto-stackframe)
452 (t (error "Should never be here")))
454 ; (let ((selected-frame
455 ; (cond ((eq ff 'gud-gdb-find-file)
456 ; (gud-gdb-selected-frame-info buffer))
457 ; (t (error "Should never be here"))))))
459 (setq gud-last-speedbar-stackframe gud-last-last-frame))))))
462 ;; ======================================================================
465 ;; History of argument lists passed to gdb.
466 (defvar gud-gdb-history nil)
468 (defcustom gud-gdb-command-name "gdb --annotate=3"
469 "Default command to execute an executable under the GDB debugger."
473 (defvar gud-gdb-marker-regexp
474 ;; This used to use path-separator instead of ":";
475 ;; however, we found that on both Windows 32 and MSDOS
476 ;; a colon is correct here.
477 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
478 "\\([0-9]*\\)" ":" ".*\n"))
480 ;; There's no guarantee that Emacs will hand the filter the entire
481 ;; marker at once; it could be broken up across several strings. We
482 ;; might even receive a big chunk with several markers in it. If we
483 ;; receive a chunk of text which looks like it might contain the
484 ;; beginning of a marker, we save it here between calls to the
486 (defvar gud-marker-acc "")
487 (make-variable-buffer-local 'gud-marker-acc)
489 (defun gud-gdb-marker-filter (string)
490 (setq gud-marker-acc (concat gud-marker-acc string))
493 ;; Process all the complete markers in this chunk.
494 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
497 ;; Extract the frame position from the marker.
498 gud-last-frame (cons (match-string 1 gud-marker-acc)
499 (string-to-number (match-string 2 gud-marker-acc)))
501 ;; Append any text before the marker to the output we're going
502 ;; to return - we don't include the marker in this text.
503 output (concat output
504 (substring gud-marker-acc 0 (match-beginning 0)))
506 ;; Set the accumulator to the remaining text.
507 gud-marker-acc (substring gud-marker-acc (match-end 0))))
509 ;; Check for annotations and change gud-minor-mode to 'gdba if
511 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
512 (let ((match (match-string 1 gud-marker-acc)))
513 (when (string-equal match "prompt")
518 ;; Append any text before the marker to the output we're going
519 ;; to return - we don't include the marker in this text.
520 output (concat output
521 (substring gud-marker-acc 0 (match-beginning 0)))
523 ;; Set the accumulator to the remaining text.
525 gud-marker-acc (substring gud-marker-acc (match-end 0)))
526 (if (string-equal match "error-begin")
527 (put-text-property 0 (length gud-marker-acc)
528 'face font-lock-warning-face
531 ;; Does the remaining text look like it might end with the
532 ;; beginning of another marker? If it does, then keep it in
533 ;; gud-marker-acc until we receive the rest of it. Since we
534 ;; know the full marker regexp above failed, it's pretty simple to
535 ;; test for marker starts.
536 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
538 ;; Everything before the potential marker start can be output.
539 (setq output (concat output (substring gud-marker-acc
540 0 (match-beginning 0))))
542 ;; Everything after, we save, to combine with later input.
544 (substring gud-marker-acc (match-beginning 0))))
546 (setq output (concat output gud-marker-acc)
551 (easy-mmode-defmap gud-minibuffer-local-map
552 '(("\C-i" . comint-dynamic-complete-filename))
553 "Keymap for minibuffer prompting of gud startup command."
554 :inherit minibuffer-local-map)
556 (defun gud-query-cmdline (minor-mode &optional init)
557 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
558 (cmd-name (gud-val 'command-name minor-mode)))
559 (unless (boundp hist-sym) (set hist-sym nil))
560 (read-from-minibuffer
561 (format "Run %s (like this): " minor-mode)
562 (or (car-safe (symbol-value hist-sym))
563 (concat (or cmd-name (symbol-name minor-mode))
567 (dolist (f (directory-files default-directory) file)
568 (if (and (file-executable-p f)
569 (not (file-directory-p f))
571 (file-newer-than-file-p f file)))
573 gud-minibuffer-local-map nil
576 (defvar gdb-first-prompt t)
578 (defvar gud-filter-pending-text nil
579 "Non-nil means this is text that has been saved for later in `gud-filter'.")
582 (defun gdb (command-line)
583 "Run gdb on program FILE in buffer *gud-FILE*.
584 The directory containing FILE becomes the initial working directory
585 and source-file directory for your debugger."
586 (interactive (list (gud-query-cmdline 'gdb)))
588 (gud-common-init command-line nil 'gud-gdb-marker-filter)
589 (set (make-local-variable 'gud-minor-mode) 'gdb)
591 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
592 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
593 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
594 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
595 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
596 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
597 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
598 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
599 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
601 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
602 "\C-j" "Set execution address to current line.")
604 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
605 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
606 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
607 (gud-def gud-pstar "print* %e" nil
608 "Evaluate C dereferenced pointer expression at point.")
609 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
610 (gud-def gud-run "run" nil "Run the program.")
612 (local-set-key "\C-i" 'gud-gdb-complete-command)
613 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
614 (setq paragraph-start comint-prompt-regexp)
615 (setq gdb-first-prompt t)
616 (setq gud-filter-pending-text nil)
617 (run-hooks 'gdb-mode-hook))
619 ;; One of the nice features of GDB is its impressive support for
620 ;; context-sensitive command completion. We preserve that feature
621 ;; in the GUD buffer by using a GDB command designed just for Emacs.
623 ;; The completion process filter indicates when it is finished.
624 (defvar gud-gdb-fetch-lines-in-progress)
626 ;; Since output may arrive in fragments we accumulate partials strings here.
627 (defvar gud-gdb-fetch-lines-string)
629 ;; We need to know how much of the completion to chop off.
630 (defvar gud-gdb-fetch-lines-break)
632 ;; The completion list is constructed by the process filter.
633 (defvar gud-gdb-fetched-lines)
635 (defvar gud-comint-buffer nil)
637 (defun gud-gdb-complete-command ()
638 "Perform completion on the GDB command preceding point.
639 This is implemented using the GDB `complete' command which isn't
640 available with older versions of GDB."
643 (command (buffer-substring (comint-line-beginning-position) end))
645 ;; Find the word break. This match will always succeed.
646 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
647 (substring command (match-beginning 2))))
649 (gud-gdb-run-command-fetch-lines (concat "complete " command)
651 ;; From string-match above.
652 (match-beginning 2))))
653 ;; Protect against old versions of GDB.
655 (string-match "^Undefined command: \"complete\"" (car complete-list))
656 (error "This version of GDB doesn't support the `complete' command"))
657 ;; Sort the list like readline.
658 (setq complete-list (sort complete-list (function string-lessp)))
659 ;; Remove duplicates.
660 (let ((first complete-list)
661 (second (cdr complete-list)))
663 (if (string-equal (car first) (car second))
664 (setcdr first (setq second (cdr second)))
666 second (cdr second)))))
667 ;; Add a trailing single quote if there is a unique completion
668 ;; and it contains an odd number of unquoted single quotes.
669 (and (= (length complete-list) 1)
670 (let ((str (car complete-list))
673 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
674 (setq count (1+ count)
676 (and (= (mod count 2) 1)
677 (setq complete-list (list (concat str "'"))))))
678 ;; Let comint handle the rest.
679 (comint-dynamic-simple-complete command-word complete-list)))
681 ;; The completion process filter is installed temporarily to slurp the
682 ;; output of GDB up to the next prompt and build the completion list.
683 (defun gud-gdb-fetch-lines-filter (string filter)
684 "Filter used to read the list of lines output by a command.
685 STRING is the output to filter.
686 It is passed through FILTER before we look at it."
687 (setq string (funcall filter string))
688 (setq string (concat gud-gdb-fetch-lines-string string))
689 (while (string-match "\n" string)
690 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
691 gud-gdb-fetched-lines)
692 (setq string (substring string (match-end 0))))
693 (if (string-match comint-prompt-regexp string)
695 (setq gud-gdb-fetch-lines-in-progress nil)
698 (setq gud-gdb-fetch-lines-string string)
701 ;; gdb speedbar functions
703 (defun gud-gdb-goto-stackframe (text token indent)
704 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
705 (speedbar-with-attached-buffer
706 (gud-basic-call (concat "server frame " (nth 1 token)))
709 (defvar gud-gdb-fetched-stack-frame nil
710 "Stack frames we are fetching from GDB.")
712 ;(defun gud-gdb-get-scope-data (text token indent)
713 ; ;; checkdoc-params: (indent)
714 ; "Fetch data associated with a stack frame, and expand/contract it.
715 ;Data to do this is retrieved from TEXT and TOKEN."
716 ; (let ((args nil) (scope nil))
717 ; (gud-gdb-run-command-fetch-lines "info args")
719 ; (gud-gdb-run-command-fetch-lines "info local")
723 (defun gud-gdb-get-stackframe (buffer)
724 "Extract the current stack frame out of the GUD GDB BUFFER."
726 (fetched-stack-frame-list
727 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
728 (if (and (car fetched-stack-frame-list)
729 (string-match "No stack" (car fetched-stack-frame-list)))
730 ;; Go into some other mode???
732 (dolist (e fetched-stack-frame-list)
733 (let ((name nil) (num nil))
735 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
736 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
737 (if (not (string-match
738 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
741 (list (nth 0 (car newlst))
744 (match-string 2 e))))
745 (setq num (match-string 1 e)
746 name (match-string 2 e))
750 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
751 (list name num (match-string 1 e)
757 ;(defun gud-gdb-selected-frame-info (buffer)
758 ; "Learn GDB information for the currently selected stack frame in BUFFER."
761 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
762 "Run COMMAND, and return the list of lines it outputs.
763 BUFFER is the GUD buffer in which to run the command.
764 SKIP is the number of chars to skip on each lines, it defaults to 0."
765 (with-current-buffer buffer
767 (goto-char (point-max))
769 (not (looking-at comint-prompt-regexp)))
771 ;; Much of this copied from GDB complete, but I'm grabbing the stack
773 (let ((gud-gdb-fetch-lines-in-progress t)
774 (gud-gdb-fetched-lines nil)
775 (gud-gdb-fetch-lines-string nil)
776 (gud-gdb-fetch-lines-break (or skip 0))
778 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
779 ;; Issue the command to GDB.
780 (gud-basic-call command)
782 (while gud-gdb-fetch-lines-in-progress
783 (accept-process-output (get-buffer-process buffer)))
784 (nreverse gud-gdb-fetched-lines)))))
787 ;; ======================================================================
790 ;; History of argument lists passed to sdb.
791 (defvar gud-sdb-history nil)
793 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
794 "If nil, we're on a System V Release 4 and don't need the tags hack.")
796 (defvar gud-sdb-lastfile nil)
798 (defun gud-sdb-marker-filter (string)
800 (if gud-marker-acc (concat gud-marker-acc string) string))
802 ;; Process all complete markers in this chunk
805 ;; System V Release 3.2 uses this format
806 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
807 gud-marker-acc start)
809 (cons (match-string 3 gud-marker-acc)
810 (string-to-number (match-string 4 gud-marker-acc)))))
811 ;; System V Release 4.0 quite often clumps two lines together
812 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
813 gud-marker-acc start)
814 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
816 (cons gud-sdb-lastfile
817 (string-to-number (match-string 3 gud-marker-acc)))))
818 ;; System V Release 4.0
819 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
820 gud-marker-acc start)
821 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
822 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
823 gud-marker-acc start))
825 (cons gud-sdb-lastfile
826 (string-to-number (match-string 1 gud-marker-acc)))))
828 (setq gud-sdb-lastfile nil)))
829 (setq start (match-end 0)))
831 ;; Search for the last incomplete line in this chunk
832 (while (string-match "\n" gud-marker-acc start)
833 (setq start (match-end 0)))
835 ;; If we have an incomplete line, store it in gud-marker-acc.
836 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
839 (defun gud-sdb-find-file (f)
840 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
843 (defun sdb (command-line)
844 "Run sdb on program FILE in buffer *gud-FILE*.
845 The directory containing FILE becomes the initial working directory
846 and source-file directory for your debugger."
847 (interactive (list (gud-query-cmdline 'sdb)))
849 (if gud-sdb-needs-tags (require 'etags))
850 (if (and gud-sdb-needs-tags
851 (not (and (boundp 'tags-file-name)
852 (stringp tags-file-name)
853 (file-exists-p tags-file-name))))
854 (error "The sdb support requires a valid tags table to work"))
856 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
857 (set (make-local-variable 'gud-minor-mode) 'sdb)
859 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
860 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
861 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
862 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
863 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
864 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
865 (gud-def gud-cont "c" "\C-r" "Continue with display.")
866 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
868 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
869 (setq paragraph-start comint-prompt-regexp)
870 (run-hooks 'sdb-mode-hook)
873 ;; ======================================================================
876 ;; History of argument lists passed to dbx.
877 (defvar gud-dbx-history nil)
879 (defcustom gud-dbx-directories nil
880 "*A list of directories that dbx should search for source code.
881 If nil, only source files in the program directory
882 will be known to dbx.
884 The file names should be absolute, or relative to the directory
885 containing the executable being debugged."
886 :type '(choice (const :tag "Current Directory" nil)
891 (defun gud-dbx-massage-args (file args)
892 (nconc (let ((directories gud-dbx-directories)
895 (setq result (cons (car directories) (cons "-I" result)))
896 (setq directories (cdr directories)))
900 (defun gud-dbx-marker-filter (string)
901 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
904 ;; Process all complete markers in this chunk.
905 (while (or (string-match
906 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
907 gud-marker-acc start)
909 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
910 gud-marker-acc start))
912 (cons (match-string 2 gud-marker-acc)
913 (string-to-number (match-string 1 gud-marker-acc)))
914 start (match-end 0)))
916 ;; Search for the last incomplete line in this chunk
917 (while (string-match "\n" gud-marker-acc start)
918 (setq start (match-end 0)))
920 ;; If the incomplete line APPEARS to begin with another marker, keep it
921 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
922 ;; unnecessary concat during the next call.
924 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
925 (substring gud-marker-acc (match-beginning 0))
929 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
930 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
932 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
933 ;; We haven't tested gud on this system:
934 (string-match "^mips-[^-]*-riscos" system-configuration)
935 ;; It's documented on OSF/1.3
936 (string-match "^mips-[^-]*-osf1" system-configuration)
937 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
938 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
940 (defvar gud-dbx-command-name
941 (concat "dbx" (if gud-mips-p " -emacs")))
943 ;; This is just like the gdb one except for the regexps since we need to cope
944 ;; with an optional breakpoint number in [] before the ^Z^Z
945 (defun gud-mipsdbx-marker-filter (string)
946 (setq gud-marker-acc (concat gud-marker-acc string))
949 ;; Process all the complete markers in this chunk.
951 ;; This is like th gdb marker but with an optional
952 ;; leading break point number like `[1] '
953 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
957 ;; Extract the frame position from the marker.
959 (cons (match-string 1 gud-marker-acc)
960 (string-to-number (match-string 2 gud-marker-acc)))
962 ;; Append any text before the marker to the output we're going
963 ;; to return - we don't include the marker in this text.
964 output (concat output
965 (substring gud-marker-acc 0 (match-beginning 0)))
967 ;; Set the accumulator to the remaining text.
968 gud-marker-acc (substring gud-marker-acc (match-end 0))))
970 ;; Does the remaining text look like it might end with the
971 ;; beginning of another marker? If it does, then keep it in
972 ;; gud-marker-acc until we receive the rest of it. Since we
973 ;; know the full marker regexp above failed, it's pretty simple to
974 ;; test for marker starts.
975 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
977 ;; Everything before the potential marker start can be output.
978 (setq output (concat output (substring gud-marker-acc
979 0 (match-beginning 0))))
981 ;; Everything after, we save, to combine with later input.
983 (substring gud-marker-acc (match-beginning 0))))
985 (setq output (concat output gud-marker-acc)
990 ;; The dbx in IRIX is a pain. It doesn't print the file name when
991 ;; stopping at a breakpoint (but you do get it from the `up' and
992 ;; `down' commands...). The only way to extract the information seems
993 ;; to be with a `file' command, although the current line number is
994 ;; available in $curline. Thus we have to look for output which
995 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
996 ;; to output the information we want with a combination of the
997 ;; `printf' and `file' commands as a pseudo marker which we can
998 ;; recognise next time through the marker-filter. This would be like
999 ;; the gdb marker but you can't get the file name without a newline...
1000 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
1001 ;; number rather than a line number etc. Maybe this could be made to
1002 ;; work by listing all the breakpoints and picking the one(s) with the
1003 ;; correct line number, but life's too short.
1004 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
1007 (and (string-match "^mips-[^-]*-irix" system-configuration)
1008 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
1009 "Non-nil to assume the interface appropriate for IRIX dbx.
1010 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
1011 a better solution in 6.1 upwards.")
1012 (defvar gud-dbx-use-stopformat-p
1013 (string-match "irix[6-9]\\.[1-9]" system-configuration)
1014 "Non-nil to use the dbx feature present at least from Irix 6.1
1015 whereby $stopformat=1 produces an output format compatiable with
1016 `gud-dbx-marker-filter'.")
1017 ;; [Irix dbx seems to be a moving target. The dbx output changed
1018 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
1019 ;; the output from `up' is no longer spotted by gud (and it's probably
1020 ;; not distinctive enough to try to match it -- use C-<, C->
1021 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
1022 ;; `long long'(why?!), so the printf stuff needed changing. The line
1023 ;; number was cast to `long' as a compromise between the new `long
1024 ;; long' and the original `int'. This is reported not to work in 6.2,
1025 ;; so it's changed back to int -- don't make your sources too long.
1026 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
1027 ;; whereby `set $stopformat=1' reportedly produces output compatible
1028 ;; with `gud-dbx-marker-filter', which we prefer.
1030 ;; The process filter is also somewhat
1031 ;; unreliable, sometimes not spotting the markers; I don't know
1032 ;; whether there's anything that can be done about that. It would be
1033 ;; much better if SGI could be persuaded to (re?)instate the MIPS
1034 ;; -emacs flag for gdb-like output (which ought to be possible as most
1035 ;; of the communication I've had over it has been from sgi.com).]
1037 ;; this filter is influenced by the xdb one rather than the gdb one
1038 (defun gud-irixdbx-marker-filter (string)
1039 (let (result (case-fold-search nil))
1040 (if (or (string-match comint-prompt-regexp string)
1041 (string-match ".*\012" string))
1042 (setq result (concat gud-marker-acc string)
1044 (setq gud-marker-acc (concat gud-marker-acc string)))
1047 ;; look for breakpoint or signal indication e.g.:
1048 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1049 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1050 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1052 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1054 ;; prod dbx into printing out the line number and file
1055 ;; name in a form we can grok as below
1056 (process-send-string (get-buffer-process gud-comint-buffer)
1057 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1058 ;; look for result of, say, "up" e.g.:
1059 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1060 ;; (this will also catch one of the lines printed by "where")
1062 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1064 (let ((file (match-string 1 result)))
1065 (if (file-exists-p file)
1066 (setq gud-last-frame
1067 (cons (match-string 1 result)
1068 (string-to-number (match-string 2 result))))))
1070 ((string-match ; kluged-up marker as above
1071 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1072 (let ((file (gud-file-name (match-string 2 result))))
1073 (if (and file (file-exists-p file))
1074 (setq gud-last-frame
1076 (string-to-number (match-string 1 result))))))
1077 (setq result (substring result 0 (match-beginning 0))))))
1080 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
1081 "Non-nil means to assume the interface approriate for DG/UX dbx.
1082 This was tested using R4.11.")
1084 ;; There are a couple of differences between DG's dbx output and normal
1085 ;; dbx output which make it nontrivial to integrate this into the
1086 ;; standard dbx-marker-filter (mainly, there are a different number of
1087 ;; backreferences). The markers look like:
1089 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1091 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1094 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1098 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1100 ;; from up/down/where.
1102 (defun gud-dguxdbx-marker-filter (string)
1103 (setq gud-marker-acc (if gud-marker-acc
1104 (concat gud-marker-acc string)
1106 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1107 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1109 ;; Process all complete markers in this chunk.
1110 (while (string-match re gud-marker-acc start)
1111 (setq gud-last-frame
1112 (cons (match-string 4 gud-marker-acc)
1113 (string-to-number (match-string 3 gud-marker-acc)))
1114 start (match-end 0)))
1116 ;; Search for the last incomplete line in this chunk
1117 (while (string-match "\n" gud-marker-acc start)
1118 (setq start (match-end 0)))
1120 ;; If the incomplete line APPEARS to begin with another marker, keep it
1121 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1122 ;; unnecessary concat during the next call.
1123 (setq gud-marker-acc
1124 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1125 (substring gud-marker-acc (match-beginning 0))
1130 (defun dbx (command-line)
1131 "Run dbx on program FILE in buffer *gud-FILE*.
1132 The directory containing FILE becomes the initial working directory
1133 and source-file directory for your debugger."
1134 (interactive (list (gud-query-cmdline 'dbx)))
1138 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1140 (gud-common-init command-line 'gud-dbx-massage-args
1141 'gud-irixdbx-marker-filter))
1143 (gud-common-init command-line 'gud-dbx-massage-args
1144 'gud-dguxdbx-marker-filter))
1146 (gud-common-init command-line 'gud-dbx-massage-args
1147 'gud-dbx-marker-filter)))
1149 (set (make-local-variable 'gud-minor-mode) 'dbx)
1153 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1154 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1155 (gud-def gud-break "stop at \"%f\":%l"
1156 "\C-b" "Set breakpoint at current line.")
1157 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1159 (gud-def gud-break "stop at \"%d%f\":%l"
1160 "\C-b" "Set breakpoint at current line.")
1161 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1162 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1163 "<" "Up (numeric arg) stack frames.")
1164 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1165 ">" "Down (numeric arg) stack frames.")
1166 ;; Make dbx give out the source location info that we need.
1167 (process-send-string (get-buffer-process gud-comint-buffer)
1168 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1170 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1171 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1172 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1173 "\C-b" "Set breakpoint at current line.")
1174 (if gud-dbx-use-stopformat-p
1175 (process-send-string (get-buffer-process gud-comint-buffer)
1176 "set $stopformat=1\n"))))
1178 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1179 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1180 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1181 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1182 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1183 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1184 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1185 (gud-def gud-run "run" nil "Run the program.")
1187 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1188 (setq paragraph-start comint-prompt-regexp)
1189 (run-hooks 'dbx-mode-hook)
1192 ;; ======================================================================
1193 ;; xdb (HP PARISC debugger) functions
1195 ;; History of argument lists passed to xdb.
1196 (defvar gud-xdb-history nil)
1198 (defcustom gud-xdb-directories nil
1199 "*A list of directories that xdb should search for source code.
1200 If nil, only source files in the program directory
1201 will be known to xdb.
1203 The file names should be absolute, or relative to the directory
1204 containing the executable being debugged."
1205 :type '(choice (const :tag "Current Directory" nil)
1210 (defun gud-xdb-massage-args (file args)
1211 (nconc (let ((directories gud-xdb-directories)
1214 (setq result (cons (car directories) (cons "-d" result)))
1215 (setq directories (cdr directories)))
1219 ;; xdb does not print the lines all at once, so we have to accumulate them
1220 (defun gud-xdb-marker-filter (string)
1222 (if (or (string-match comint-prompt-regexp string)
1223 (string-match ".*\012" string))
1224 (setq result (concat gud-marker-acc string)
1226 (setq gud-marker-acc (concat gud-marker-acc string)))
1228 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1230 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1232 (let ((line (string-to-number (match-string 2 result)))
1233 (file (gud-file-name (match-string 1 result))))
1235 (setq gud-last-frame (cons file line))))))
1239 (defun xdb (command-line)
1240 "Run xdb on program FILE in buffer *gud-FILE*.
1241 The directory containing FILE becomes the initial working directory
1242 and source-file directory for your debugger.
1244 You can set the variable `gud-xdb-directories' to a list of program source
1245 directories if your program contains sources from more than one directory."
1246 (interactive (list (gud-query-cmdline 'xdb)))
1248 (gud-common-init command-line 'gud-xdb-massage-args
1249 'gud-xdb-marker-filter)
1250 (set (make-local-variable 'gud-minor-mode) 'xdb)
1252 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1253 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1254 "Set temporary breakpoint at current line.")
1255 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1256 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1257 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1258 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1259 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1260 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1261 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1262 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1264 (setq comint-prompt-regexp "^>")
1265 (setq paragraph-start comint-prompt-regexp)
1266 (run-hooks 'xdb-mode-hook))
1268 ;; ======================================================================
1271 ;; History of argument lists passed to perldb.
1272 (defvar gud-perldb-history nil)
1274 (defun gud-perldb-massage-args (file args)
1275 "Convert a command line as would be typed normally to run perldb
1276 into one that invokes an Emacs-enabled debugging session.
1277 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1278 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1280 (let* ((new-args nil)
1282 (shift (lambda () (push (pop args) new-args))))
1284 ;; Pass all switches and -e scripts through.
1286 (string-match "^-" (car args))
1287 (not (equal "-" (car args)))
1288 (not (equal "--" (car args))))
1289 (when (equal "-e" (car args))
1290 ;; -e goes with the next arg, so shift one extra.
1292 ;; -e as the last arg is an error in Perl.
1293 (error "No code specified for -e"))
1299 (string-match "^-" (car args)))
1300 (error "Can't use stdin as the script to debug"))
1301 ;; This is the program name.
1304 ;; If -e specified, make sure there is a -- so -emacs is not taken
1306 (if (and args (equal "--" (car args)))
1308 (and seen-e (push "--" new-args)))
1310 (push "-emacs" new-args)
1314 (nreverse new-args)))
1316 ;; There's no guarantee that Emacs will hand the filter the entire
1317 ;; marker at once; it could be broken up across several strings. We
1318 ;; might even receive a big chunk with several markers in it. If we
1319 ;; receive a chunk of text which looks like it might contain the
1320 ;; beginning of a marker, we save it here between calls to the
1322 (defun gud-perldb-marker-filter (string)
1323 (setq gud-marker-acc (concat gud-marker-acc string))
1326 ;; Process all the complete markers in this chunk.
1327 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1331 ;; Extract the frame position from the marker.
1333 (cons (match-string 1 gud-marker-acc)
1334 (string-to-number (match-string 3 gud-marker-acc)))
1336 ;; Append any text before the marker to the output we're going
1337 ;; to return - we don't include the marker in this text.
1338 output (concat output
1339 (substring gud-marker-acc 0 (match-beginning 0)))
1341 ;; Set the accumulator to the remaining text.
1342 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1344 ;; Does the remaining text look like it might end with the
1345 ;; beginning of another marker? If it does, then keep it in
1346 ;; gud-marker-acc until we receive the rest of it. Since we
1347 ;; know the full marker regexp above failed, it's pretty simple to
1348 ;; test for marker starts.
1349 (if (string-match "\032.*\\'" gud-marker-acc)
1351 ;; Everything before the potential marker start can be output.
1352 (setq output (concat output (substring gud-marker-acc
1353 0 (match-beginning 0))))
1355 ;; Everything after, we save, to combine with later input.
1356 (setq gud-marker-acc
1357 (substring gud-marker-acc (match-beginning 0))))
1359 (setq output (concat output gud-marker-acc)
1364 (defcustom gud-perldb-command-name "perl -d"
1365 "Default command to execute a Perl script under debugger."
1370 (defun perldb (command-line)
1371 "Run perldb on program FILE in buffer *gud-FILE*.
1372 The directory containing FILE becomes the initial working directory
1373 and source-file directory for your debugger."
1375 (list (gud-query-cmdline 'perldb
1376 (concat (or (buffer-file-name) "-e 0") " "))))
1378 (gud-common-init command-line 'gud-perldb-massage-args
1379 'gud-perldb-marker-filter)
1380 (set (make-local-variable 'gud-minor-mode) 'perldb)
1382 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1383 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1384 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1385 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1386 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1387 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1388 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1389 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1390 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1391 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1394 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1395 (setq paragraph-start comint-prompt-regexp)
1396 (run-hooks 'perldb-mode-hook))
1398 ;; ======================================================================
1399 ;; pdb (Python debugger) functions
1401 ;; History of argument lists passed to pdb.
1402 (defvar gud-pdb-history nil)
1404 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1405 ;; Either file or function name may be omitted: "> <string>(0)?()"
1406 (defvar gud-pdb-marker-regexp
1407 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1408 (defvar gud-pdb-marker-regexp-file-group 1)
1409 (defvar gud-pdb-marker-regexp-line-group 2)
1410 (defvar gud-pdb-marker-regexp-fnname-group 3)
1412 (defvar gud-pdb-marker-regexp-start "^> ")
1414 ;; There's no guarantee that Emacs will hand the filter the entire
1415 ;; marker at once; it could be broken up across several strings. We
1416 ;; might even receive a big chunk with several markers in it. If we
1417 ;; receive a chunk of text which looks like it might contain the
1418 ;; beginning of a marker, we save it here between calls to the
1420 (defun gud-pdb-marker-filter (string)
1421 (setq gud-marker-acc (concat gud-marker-acc string))
1424 ;; Process all the complete markers in this chunk.
1425 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1428 ;; Extract the frame position from the marker.
1430 (let ((file (match-string gud-pdb-marker-regexp-file-group
1432 (line (string-to-number
1433 (match-string gud-pdb-marker-regexp-line-group
1435 (if (string-equal file "<string>")
1439 ;; Output everything instead of the below
1440 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1441 ;; ;; Append any text before the marker to the output we're going
1442 ;; ;; to return - we don't include the marker in this text.
1443 ;; output (concat output
1444 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1446 ;; Set the accumulator to the remaining text.
1447 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1449 ;; Does the remaining text look like it might end with the
1450 ;; beginning of another marker? If it does, then keep it in
1451 ;; gud-marker-acc until we receive the rest of it. Since we
1452 ;; know the full marker regexp above failed, it's pretty simple to
1453 ;; test for marker starts.
1454 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1456 ;; Everything before the potential marker start can be output.
1457 (setq output (concat output (substring gud-marker-acc
1458 0 (match-beginning 0))))
1460 ;; Everything after, we save, to combine with later input.
1461 (setq gud-marker-acc
1462 (substring gud-marker-acc (match-beginning 0))))
1464 (setq output (concat output gud-marker-acc)
1469 (defcustom gud-pdb-command-name "pdb"
1470 "File name for executing the Python debugger.
1471 This should be an executable on your path, or an absolute file name."
1476 (defun pdb (command-line)
1477 "Run pdb on program FILE in buffer `*gud-FILE*'.
1478 The directory containing FILE becomes the initial working directory
1479 and source-file directory for your debugger."
1481 (list (gud-query-cmdline 'pdb)))
1483 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1484 (set (make-local-variable 'gud-minor-mode) 'pdb)
1486 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1487 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1488 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1489 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1490 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1491 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1492 (gud-def gud-up "up" "<" "Up one stack frame.")
1493 (gud-def gud-down "down" ">" "Down one stack frame.")
1494 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1496 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1498 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1499 (setq comint-prompt-regexp "^(Pdb) *")
1500 (setq paragraph-start comint-prompt-regexp)
1501 (run-hooks 'pdb-mode-hook))
1503 ;; ======================================================================
1507 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1508 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1510 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1511 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1512 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1514 ;; INVOCATION NOTES:
1516 ;; You invoke jdb-mode with:
1520 ;; It responds with:
1522 ;; Run jdb (like this): jdb
1524 ;; type any jdb switches followed by the name of the class you'd like to debug.
1525 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1526 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1527 ;; See the known problems section below for restrictions when specifying jdb
1528 ;; command line switches (search forward for '-classpath').
1530 ;; You should see something like the following:
1532 ;; Current directory is ~/src/java/hello/
1533 ;; Initializing jdb...
1534 ;; 0xed2f6628:class(hello)
1537 ;; To set an initial breakpoint try:
1539 ;; > stop in hello.main
1540 ;; Breakpoint set in hello.main
1543 ;; To execute the program type:
1548 ;; Breakpoint hit: running ...
1549 ;; hello.main (hello:12)
1551 ;; Type M-n to step over the current line and M-s to step into it. That,
1552 ;; along with the JDB 'help' command should get you started. The 'quit'
1553 ;; JDB command will get out out of the debugger. There is some truly
1554 ;; pathetic JDB documentation available at:
1556 ;; http://java.sun.com/products/jdk/1.1/debugging/
1558 ;; KNOWN PROBLEMS AND FIXME's:
1560 ;; Not sure what happens with inner classes ... haven't tried them.
1562 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1564 ;; You must not put whitespace between "-classpath" and the path to
1565 ;; search for java classes even though it is required when invoking jdb
1566 ;; from the command line. See gud-jdb-massage-args for details.
1567 ;; The same applies for "-sourcepath".
1569 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1570 ;; refer to the documentation of `gud-jdb-use-classpath' and
1571 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1572 ;; on using the classpath for locating java source files.
1574 ;; If any of the source files in the directories listed in
1575 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1576 ;; every file ending in ".java" in these directories parses without error.
1578 ;; All the .java files in the directories in gud-jdb-directories are
1579 ;; syntactically analyzed each time gud jdb is invoked. It would be
1580 ;; nice to keep as much information as possible between runs. It would
1581 ;; be really nice to analyze the files only as neccessary (when the
1582 ;; source needs to be displayed.) I'm not sure to what extent the former
1583 ;; can be accomplished and I'm not sure the latter can be done at all
1584 ;; since I don't know of any general way to tell which .class files are
1585 ;; defined by which .java file without analyzing all the .java files.
1586 ;; If anyone knows why JavaSoft didn't put the source file names in
1587 ;; debuggable .class files please clue me in so I find something else
1588 ;; to be spiteful and bitter about.
1590 ;; ======================================================================
1591 ;; gud jdb variables and functions
1593 (defcustom gud-jdb-command-name "jdb"
1594 "Command that executes the Java debugger."
1598 (defcustom gud-jdb-use-classpath t
1599 "If non-nil, search for Java source files in classpath directories.
1600 The list of directories to search is the value of `gud-jdb-classpath'.
1601 The file pathname is obtained by converting the fully qualified
1602 class information output by jdb to a relative pathname and appending
1603 it to `gud-jdb-classpath' element by element until a match is found.
1605 This method has a significant jdb startup time reduction advantage
1606 since it does not require the scanning of all `gud-jdb-directories'
1607 and parsing all Java files for class information.
1609 Set to nil to use `gud-jdb-directories' to scan java sources for
1610 class information on jdb startup (original method)."
1614 (defvar gud-jdb-classpath nil
1615 "Java/jdb classpath directories list.
1616 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1617 list automatically using the following methods in sequence
1618 \(with subsequent successful steps overriding the results of previous
1621 1) Read the CLASSPATH environment variable,
1622 2) Read any \"-classpath\" argument used to run jdb,
1623 or detected in jdb output (e.g. if jdb is run by a script
1624 that echoes the actual jdb command before starting jdb)
1625 3) Send a \"classpath\" command to jdb and scan jdb output for
1626 classpath information if jdb is invoked with an \"-attach\" (to
1627 an already running VM) argument (This case typically does not
1628 have a \"-classpath\" command line argument - that is provided
1629 to the VM when it is started).
1631 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1632 those debuggers do not support the classpath command. Use 1) or 2).")
1634 (defvar gud-jdb-sourcepath nil
1635 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1636 This list is prepended to `gud-jdb-classpath' to form the complete
1637 list of directories searched for source files.")
1639 (defvar gud-marker-acc-max-length 4000
1640 "Maximum number of debugger output characters to keep.
1641 This variable limits the size of `gud-marker-acc' which holds
1642 the most recent debugger output history while searching for
1643 source file information.")
1645 (defvar gud-jdb-history nil
1646 "History of argument lists passed to jdb.")
1649 ;; List of Java source file directories.
1650 (defvar gud-jdb-directories (list ".")
1651 "*A list of directories that gud jdb should search for source code.
1652 The file names should be absolute, or relative to the current
1655 The set of .java files residing in the directories listed are
1656 syntactically analyzed to determine the classes they define and the
1657 packages in which these classes belong. In this way gud jdb maps the
1658 package-qualified class names output by the jdb debugger to the source
1659 file from which the class originated. This allows gud mode to keep
1660 the source code display in sync with the debugging session.")
1662 (defvar gud-jdb-source-files nil
1663 "List of the java source files for this debugging session.")
1665 ;; Association list of fully qualified class names (package + class name)
1666 ;; and their source files.
1667 (defvar gud-jdb-class-source-alist nil
1668 "Association list of fully qualified class names and source files.")
1670 ;; This is used to hold a source file during analysis.
1671 (defvar gud-jdb-analysis-buffer nil)
1673 (defvar gud-jdb-classpath-string nil
1674 "Holds temporary classpath values.")
1676 (defun gud-jdb-build-source-files-list (path extn)
1677 "Return a list of java source files (absolute paths).
1678 PATH gives the directories in which to search for files with
1679 extension EXTN. Normally EXTN is given as the regular expression
1681 (apply 'nconc (mapcar (lambda (d)
1682 (when (file-directory-p d)
1683 (directory-files d t extn nil)))
1686 ;; Move point past whitespace.
1687 (defun gud-jdb-skip-whitespace ()
1688 (skip-chars-forward " \n\r\t\014"))
1690 ;; Move point past a "// <eol>" type of comment.
1691 (defun gud-jdb-skip-single-line-comment ()
1694 ;; Move point past a "/* */" or "/** */" type of comment.
1695 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1699 (if (eq (following-char) ?*)
1703 (if (eq (following-char) ?/)
1706 (throw 'break nil)))))
1709 ;; Move point past any number of consecutive whitespace chars and/or comments.
1710 (defun gud-jdb-skip-whitespace-and-comments ()
1711 (gud-jdb-skip-whitespace)
1716 (gud-jdb-skip-single-line-comment)
1717 (gud-jdb-skip-whitespace))
1718 ((looking-at "/\\*")
1719 (gud-jdb-skip-traditional-or-documentation-comment)
1720 (gud-jdb-skip-whitespace))
1721 (t (throw 'done nil))))))
1723 ;; Move point past things that are id-like. The intent is to skip regular
1724 ;; id's, such as class or interface names as well as package and interface
1726 (defun gud-jdb-skip-id-ish-thing ()
1727 (skip-chars-forward "^ /\n\r\t\014,;{"))
1729 ;; Move point past a string literal.
1730 (defun gud-jdb-skip-string-literal ()
1733 ((eq (following-char) ?\\)
1735 ((eq (following-char) ?\042))))
1739 ;; Move point past a character literal.
1740 (defun gud-jdb-skip-character-literal ()
1744 (if (eq (following-char) ?\\)
1746 (not (eq (following-char) ?\')))
1750 ;; Move point past the following block. There may be (legal) cruft before
1751 ;; the block's opening brace. There must be a block or it's the end of life
1752 ;; in petticoat junction.
1753 (defun gud-jdb-skip-block ()
1755 ;; Find the begining of the block.
1757 (not (eq (following-char) ?{))
1759 ;; Skip any constructs that can harbor literal block delimiter
1760 ;; characters and/or the delimiters for the constructs themselves.
1763 (gud-jdb-skip-single-line-comment))
1764 ((looking-at "/\\*")
1765 (gud-jdb-skip-traditional-or-documentation-comment))
1766 ((eq (following-char) ?\042)
1767 (gud-jdb-skip-string-literal))
1768 ((eq (following-char) ?\')
1769 (gud-jdb-skip-character-literal))
1770 (t (forward-char))))
1772 ;; Now at the begining of the block.
1775 ;; Skip over the body of the block as well as the final brace.
1776 (let ((open-level 1))
1777 (while (not (eq open-level 0))
1780 (gud-jdb-skip-single-line-comment))
1781 ((looking-at "/\\*")
1782 (gud-jdb-skip-traditional-or-documentation-comment))
1783 ((eq (following-char) ?\042)
1784 (gud-jdb-skip-string-literal))
1785 ((eq (following-char) ?\')
1786 (gud-jdb-skip-character-literal))
1787 ((eq (following-char) ?{)
1788 (setq open-level (+ open-level 1))
1790 ((eq (following-char) ?})
1791 (setq open-level (- open-level 1))
1793 (t (forward-char))))))
1795 ;; Find the package and class definitions in Java source file FILE. Assumes
1796 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1797 ;; to hold the source during analysis.
1798 (defun gud-jdb-analyze-source (buf file)
1801 (insert-file-contents file nil nil nil t)
1806 (gud-jdb-skip-whitespace)
1810 ;; Any number of semi's following a block is legal. Move point
1811 ;; past them. Note that comments and whitespace may be
1812 ;; interspersed as well.
1813 ((eq (following-char) ?\073)
1816 ;; Move point past a single line comment.
1818 (gud-jdb-skip-single-line-comment))
1820 ;; Move point past a traditional or documentation comment.
1821 ((looking-at "/\\*")
1822 (gud-jdb-skip-traditional-or-documentation-comment))
1824 ;; Move point past a package statement, but save the PackageName.
1825 ((looking-at "package")
1827 (gud-jdb-skip-whitespace-and-comments)
1829 (gud-jdb-skip-id-ish-thing)
1830 (setq p (concat (buffer-substring s (point)) "."))
1831 (gud-jdb-skip-whitespace-and-comments)
1832 (if (eq (following-char) ?\073)
1835 ;; Move point past an import statement.
1836 ((looking-at "import")
1838 (gud-jdb-skip-whitespace-and-comments)
1839 (gud-jdb-skip-id-ish-thing)
1840 (gud-jdb-skip-whitespace-and-comments)
1841 (if (eq (following-char) ?\073)
1844 ;; Move point past the various kinds of ClassModifiers.
1845 ((looking-at "public")
1847 ((looking-at "abstract")
1849 ((looking-at "final")
1852 ;; Move point past a ClassDeclaraction, but save the class
1854 ((looking-at "class")
1856 (gud-jdb-skip-whitespace-and-comments)
1858 (gud-jdb-skip-id-ish-thing)
1860 l (nconc l (list (concat p (buffer-substring s (point)))))))
1861 (gud-jdb-skip-block))
1863 ;; Move point past an interface statement.
1864 ((looking-at "interface")
1866 (gud-jdb-skip-block))
1868 ;; Anything else means the input is invalid.
1870 (message "Error parsing file %s." file)
1871 (throw 'abort nil))))))
1874 (defun gud-jdb-build-class-source-alist-for-file (file)
1878 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1880 ;; Return an alist of fully qualified classes and the source files
1881 ;; holding their definitions. SOURCES holds a list of all the source
1882 ;; files to examine.
1883 (defun gud-jdb-build-class-source-alist (sources)
1884 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1889 'gud-jdb-build-class-source-alist-for-file
1891 (kill-buffer gud-jdb-analysis-buffer)
1892 (setq gud-jdb-analysis-buffer nil)))
1894 ;; Change what was given in the minibuffer to something that can be used to
1895 ;; invoke the debugger.
1896 (defun gud-jdb-massage-args (file args)
1897 ;; The jdb executable must have whitespace between "-classpath" and
1898 ;; its value while gud-common-init expects all switch values to
1899 ;; follow the switch keyword without intervening whitespace. We
1900 ;; require that when the user enters the "-classpath" switch in the
1901 ;; EMACS minibuffer that they do so without the intervening
1902 ;; whitespace. This function adds it back (it's called after
1903 ;; gud-common-init). There are more switches like this (for
1904 ;; instance "-host" and "-password") but I don't care about them
1907 (let (massaged-args user-error)
1909 (while (and args (not user-error))
1911 ((setq user-error (string-match "-classpath$" (car args))))
1912 ((setq user-error (string-match "-sourcepath$" (car args))))
1913 ((string-match "-classpath\\(.+\\)" (car args))
1915 (append massaged-args
1917 (setq gud-jdb-classpath-string
1918 (match-string 1 (car args)))))))
1919 ((string-match "-sourcepath\\(.+\\)" (car args))
1921 (append massaged-args
1923 (setq gud-jdb-sourcepath
1924 (match-string 1 (car args)))))))
1925 (t (setq massaged-args (append massaged-args (list (car args))))))
1926 (setq args (cdr args)))
1928 ;; By this point the current directory is all screwed up. Maybe we
1929 ;; could fix things and re-invoke gud-common-init, but for now I think
1930 ;; issueing the error is good enough.
1933 (kill-buffer (current-buffer))
1934 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
1937 ;; Search for an association with P, a fully qualified class name, in
1938 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1939 ;; qualified file name of the source file which produced the class.
1940 (defun gud-jdb-find-source-file (p)
1941 (cdr (assoc p gud-jdb-class-source-alist)))
1943 ;; Note: Reset to this value every time a prompt is seen
1944 (defvar gud-jdb-lowest-stack-level 999)
1946 (defun gud-jdb-find-source-using-classpath (p)
1947 "Find source file corresponding to fully qualified class p.
1948 Convert p from jdb's output, converted to a pathname
1949 relative to a classpath directory."
1952 (;; Replace dots with slashes and append ".java" to generate file
1953 ;; name relative to classpath
1956 (mapconcat 'identity
1958 ;; Eliminate any subclass references in the class
1959 ;; name string. These start with a "$"
1961 (if (string-match "$.*" x)
1962 (replace-match "" t t x) p))
1966 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
1969 (not (setq found-file
1971 (concat (car cplist) "/" filename)))))
1972 (setq cplist (cdr cplist)))
1973 (if found-file (concat (car cplist) "/" filename)))))
1975 (defun gud-jdb-find-source (string)
1976 "Alias for function used to locate source files.
1977 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
1978 during jdb initialization depending on the value of
1979 `gud-jdb-use-classpath'."
1982 (defun gud-jdb-parse-classpath-string (string)
1983 "Parse the classpath list and convert each item to an absolute pathname."
1984 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
1985 (replace-match "" nil nil s) s))
1986 (mapcar 'file-truename
1989 (concat "[ \t\n\r,\"" path-separator "]+")))))
1991 ;; See comentary for other debugger's marker filters - there you will find
1992 ;; important notes about STRING.
1993 (defun gud-jdb-marker-filter (string)
1995 ;; Build up the accumulator.
1996 (setq gud-marker-acc
1998 (concat gud-marker-acc string)
2001 ;; Look for classpath information until gud-jdb-classpath-string is found
2002 ;; (interactive, multiple settings of classpath from jdb
2003 ;; not supported/followed)
2004 (if (and gud-jdb-use-classpath
2005 (not gud-jdb-classpath-string)
2006 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
2007 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
2008 (setq gud-jdb-classpath
2009 (gud-jdb-parse-classpath-string
2010 (setq gud-jdb-classpath-string
2011 (match-string 1 gud-marker-acc)))))
2013 ;; We process STRING from left to right. Each time through the
2014 ;; following loop we process at most one marker. After we've found a
2015 ;; marker, delete gud-marker-acc up to and including the match
2017 ;; Process each complete marker in the input.
2020 ;; Do we see a marker?
2022 ;; jdb puts out a string of the following form when it
2023 ;; hits a breakpoint:
2025 ;; <fully-qualified-class><method> (<class>:<line-number>)
2027 ;; <fully-qualified-class>'s are composed of Java ID's
2028 ;; separated by periods. <method> and <class> are
2029 ;; also Java ID's. <method> begins with a period and
2030 ;; may contain less-than and greater-than (constructors,
2031 ;; for instance, are called <init> in the symbol table.)
2032 ;; Java ID's begin with a letter followed by letters
2033 ;; and/or digits. The set of letters includes underscore
2036 ;; The first group matches <fully-qualified-class>,
2037 ;; the second group matches <class> and the third group
2038 ;; matches <line-number>. We don't care about using
2039 ;; <method> so we don't "group" it.
2041 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2044 ;; The ".," in the last square-bracket are necessary because
2045 ;; of Sun's total disrespect for backwards compatibility in
2046 ;; reported line numbers from jdb - starting in 1.4.0 they
2047 ;; print line numbers using LOCALE, inserting a comma or a
2048 ;; period at the thousands positions (how ingenious!).
2050 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2051 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2054 ;; A good marker is one that:
2055 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2056 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2057 ;; since the last prompt
2058 ;; Figure out the line on which to position the debugging arrow.
2059 ;; Return the info as a cons of the form:
2061 ;; (<file-name> . <line-number>) .
2062 (if (if (match-beginning 1)
2064 (setq n (string-to-number (substring
2066 (1+ (match-beginning 1))
2067 (- (match-end 1) 2))))
2068 (if (< n gud-jdb-lowest-stack-level)
2069 (progn (setq gud-jdb-lowest-stack-level n) t)))
2071 (if (setq file-found
2072 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2073 (setq gud-last-frame
2077 ((numstr (match-string 4 gud-marker-acc)))
2078 (if (string-match "[.,]" numstr)
2079 (replace-match "" nil nil numstr)
2081 (message "Could not find source file.")))
2083 ;; Set the accumulator to the remaining text.
2084 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2086 (if (string-match comint-prompt-regexp gud-marker-acc)
2087 (setq gud-jdb-lowest-stack-level 999)))
2089 ;; Do not allow gud-marker-acc to grow without bound. If the source
2090 ;; file information is not within the last 3/4
2091 ;; gud-marker-acc-max-length characters, well,...
2092 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2093 (setq gud-marker-acc
2094 (substring gud-marker-acc
2095 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2097 ;; We don't filter any debugger output so just return what we were given.
2100 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2103 (defun jdb (command-line)
2104 "Run jdb with command line COMMAND-LINE in a buffer.
2105 The buffer is named \"*gud*\" if no initial class is given or
2106 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2107 switch is given, omit all whitespace between it and its value.
2109 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2110 information on how jdb accesses source files. Alternatively (if
2111 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2112 original source file access method.
2114 For general information about commands available to control jdb from
2115 gud, see `gud-mode'."
2117 (list (gud-query-cmdline 'jdb)))
2118 (setq gud-jdb-classpath nil)
2119 (setq gud-jdb-sourcepath nil)
2121 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2122 ;; if CLASSPATH is set.
2123 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
2124 (if gud-jdb-classpath-string
2125 (setq gud-jdb-classpath
2126 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2127 (setq gud-jdb-classpath-string nil) ; prepare for next
2129 (gud-common-init command-line 'gud-jdb-massage-args
2130 'gud-jdb-marker-filter)
2131 (set (make-local-variable 'gud-minor-mode) 'jdb)
2133 ;; If a -classpath option was provided, set gud-jdb-classpath
2134 (if gud-jdb-classpath-string
2135 (setq gud-jdb-classpath
2136 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2137 (setq gud-jdb-classpath-string nil) ; prepare for next
2138 ;; If a -sourcepath option was provided, parse it
2139 (if gud-jdb-sourcepath
2140 (setq gud-jdb-sourcepath
2141 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2143 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2144 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2145 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2146 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2147 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2148 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2149 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2150 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2151 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2153 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2154 (setq paragraph-start comint-prompt-regexp)
2155 (run-hooks 'jdb-mode-hook)
2157 (if gud-jdb-use-classpath
2158 ;; Get the classpath information from the debugger
2160 (if (string-match "-attach" command-line)
2161 (gud-call "classpath"))
2162 (fset 'gud-jdb-find-source
2163 'gud-jdb-find-source-using-classpath))
2165 ;; Else create and bind the class/source association list as well
2166 ;; as the source file list.
2167 (setq gud-jdb-class-source-alist
2168 (gud-jdb-build-class-source-alist
2169 (setq gud-jdb-source-files
2170 (gud-jdb-build-source-files-list gud-jdb-directories
2172 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2175 ;; ======================================================================
2177 ;; BASHDB support. See http://bashdb.sourceforge.net
2179 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2181 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2183 ;; INVOCATION NOTES:
2185 ;; You invoke bashdb-mode with:
2187 ;; M-x bashdb <enter>
2189 ;; It responds with:
2191 ;; Run bashdb (like this): bash
2194 ;; History of argument lists passed to bashdb.
2195 (defvar gud-bashdb-history nil)
2197 ;; Convert a command line as would be typed normally to run a script
2198 ;; into one that invokes an Emacs-enabled debugging session.
2199 ;; "--debugger" in inserted as the first switch.
2201 ;; There's no guarantee that Emacs will hand the filter the entire
2202 ;; marker at once; it could be broken up across several strings. We
2203 ;; might even receive a big chunk with several markers in it. If we
2204 ;; receive a chunk of text which looks like it might contain the
2205 ;; beginning of a marker, we save it here between calls to the
2207 (defun gud-bashdb-marker-filter (string)
2208 (setq gud-marker-acc (concat gud-marker-acc string))
2211 ;; Process all the complete markers in this chunk.
2212 ;; Format of line looks like this:
2213 ;; (/etc/init.d/ntp.init:16):
2214 ;; but we also allow DOS drive letters
2215 ;; (d:/etc/init.d/ntp.init:16):
2216 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2220 ;; Extract the frame position from the marker.
2222 (cons (match-string 2 gud-marker-acc)
2223 (string-to-number (match-string 4 gud-marker-acc)))
2225 ;; Append any text before the marker to the output we're going
2226 ;; to return - we don't include the marker in this text.
2227 output (concat output
2228 (substring gud-marker-acc 0 (match-beginning 0)))
2230 ;; Set the accumulator to the remaining text.
2231 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2233 ;; Does the remaining text look like it might end with the
2234 ;; beginning of another marker? If it does, then keep it in
2235 ;; gud-marker-acc until we receive the rest of it. Since we
2236 ;; know the full marker regexp above failed, it's pretty simple to
2237 ;; test for marker starts.
2238 (if (string-match "\032.*\\'" gud-marker-acc)
2240 ;; Everything before the potential marker start can be output.
2241 (setq output (concat output (substring gud-marker-acc
2242 0 (match-beginning 0))))
2244 ;; Everything after, we save, to combine with later input.
2245 (setq gud-marker-acc
2246 (substring gud-marker-acc (match-beginning 0))))
2248 (setq output (concat output gud-marker-acc)
2253 (defcustom gud-bashdb-command-name "bash --debugger"
2254 "File name for executing bash debugger."
2259 (defun bashdb (command-line)
2260 "Run bashdb on program FILE in buffer *gud-FILE*.
2261 The directory containing FILE becomes the initial working directory
2262 and source-file directory for your debugger."
2264 (list (read-from-minibuffer "Run bashdb (like this): "
2265 (if (consp gud-bashdb-history)
2266 (car gud-bashdb-history)
2267 (concat gud-bashdb-command-name
2269 gud-minibuffer-local-map nil
2270 '(gud-bashdb-history . 1))))
2272 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2274 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2276 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2277 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2278 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2279 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2280 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2281 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2282 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2283 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2284 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2285 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2288 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2290 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2291 (setq paragraph-start comint-prompt-regexp)
2292 (run-hooks 'bashdb-mode-hook)
2296 ;; End of debugger-specific information
2300 ;; When we send a command to the debugger via gud-call, it's annoying
2301 ;; to see the command and the new prompt inserted into the debugger's
2302 ;; buffer; we have other ways of knowing the command has completed.
2304 ;; If the buffer looks like this:
2305 ;; --------------------
2306 ;; (gdb) set args foo bar
2308 ;; --------------------
2309 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2310 ;; source file to set a breakpoint, we want the buffer to end up like
2312 ;; --------------------
2313 ;; (gdb) set args foo bar
2314 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2316 ;; --------------------
2317 ;; Essentially, the old prompt is deleted, and the command's output
2318 ;; and the new prompt take its place.
2320 ;; Not echoing the command is easy enough; you send it directly using
2321 ;; process-send-string, and it never enters the buffer. However,
2322 ;; getting rid of the old prompt is trickier; you don't want to do it
2323 ;; when you send the command, since that will result in an annoying
2324 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2325 ;; waits for a response from the debugger, and the new prompt is
2326 ;; inserted. Instead, we'll wait until we actually get some output
2327 ;; from the subprocess before we delete the prompt. If the command
2328 ;; produced no output other than a new prompt, that prompt will most
2329 ;; likely be in the first chunk of output received, so we will delete
2330 ;; the prompt and then replace it with an identical one. If the
2331 ;; command produces output, the prompt is moving anyway, so the
2332 ;; flicker won't be annoying.
2334 ;; So - when we want to delete the prompt upon receipt of the next
2335 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2336 ;; the start of the prompt; the process filter will notice this, and
2337 ;; delete all text between it and the process output marker. If
2338 ;; gud-delete-prompt-marker points nowhere, we leave the current
2340 (defvar gud-delete-prompt-marker nil)
2343 (put 'gud-mode 'mode-class 'special)
2345 (define-derived-mode gud-mode comint-mode "Debugger"
2346 "Major mode for interacting with an inferior debugger process.
2348 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2349 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2350 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2351 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2353 After startup, the following commands are available in both the GUD
2354 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2357 \\[gud-break] sets a breakpoint at the current file and line. In the
2358 GUD buffer, the current file and line are those of the last breakpoint or
2359 step. In a source buffer, they are the buffer's file and current line.
2361 \\[gud-remove] removes breakpoints on the current file and line.
2363 \\[gud-refresh] displays in the source window the last line referred to
2366 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2367 step-one-line (not entering function calls), and step-one-instruction
2368 and then update the source window with the current file and position.
2369 \\[gud-cont] continues execution.
2371 \\[gud-print] tries to find the largest C lvalue or function-call expression
2372 around point, and sends it to the debugger for value display.
2374 The above commands are common to all supported debuggers except xdb which
2375 does not support stepping instructions.
2377 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2378 except that the breakpoint is temporary; that is, it is removed when
2379 execution stops on it.
2381 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2382 frame. \\[gud-down] drops back down through one.
2384 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2385 the current function and stops.
2387 All the keystrokes above are accessible in the GUD buffer
2388 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2390 All pre-defined functions for which the concept make sense repeat
2391 themselves the appropriate number of times if you give a prefix
2394 You may use the `gud-def' macro in the initialization hook to define other
2397 Other commands for interacting with the debugger process are inherited from
2398 comint mode, which see."
2399 (setq mode-line-process '(":%s"))
2400 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2401 (set (make-local-variable 'gud-last-frame) nil)
2402 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2403 (make-local-variable 'comint-prompt-regexp)
2404 ;; Don't put repeated commands in command history many times.
2405 (set (make-local-variable 'comint-input-ignoredups) t)
2406 (make-local-variable 'paragraph-start)
2407 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2408 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2410 ;; Cause our buffers to be displayed, by default,
2411 ;; in the selected window.
2412 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2414 (defcustom gud-chdir-before-run t
2415 "Non-nil if GUD should `cd' to the debugged executable."
2419 (defvar gud-target-name "--unknown--"
2420 "The apparent name of the program being debugged in a gud buffer.")
2422 ;; Perform initializations common to all debuggers.
2423 ;; The first arg is the specified command line,
2424 ;; which starts with the program to debug.
2425 ;; The other three args specify the values to use
2426 ;; for local variables in the debugger buffer.
2427 (defun gud-common-init (command-line massage-args marker-filter
2428 &optional find-file)
2429 (let* ((words (split-string command-line))
2430 (program (car words))
2431 (dir default-directory)
2432 ;; Extract the file name from WORDS
2433 ;; and put t in its place.
2434 ;; Later on we will put the modified file name arg back there.
2435 (file-word (let ((w (cdr words)))
2436 (while (and w (= ?- (aref (car w) 0)))
2442 (and file-word (substitute-in-file-name file-word)))
2444 ;; If a directory was specified, expand the file name.
2445 ;; Otherwise, don't expand it, so GDB can use the PATH.
2446 ;; A file name without directory is literally valid
2447 ;; only if the file exists in ., and in that case,
2448 ;; omitting the expansion here has no visible effect.
2449 (file (and file-word
2450 (if (file-name-directory file-subst)
2451 (expand-file-name file-subst)
2453 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2454 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2455 (pop-to-buffer (concat "*gud" filepart "*"))
2456 (when (and existing-buffer (get-buffer-process existing-buffer))
2457 (error "This program is already running under gdb"))
2458 ;; Set the dir, in case the buffer already existed with a different dir.
2459 (setq default-directory dir)
2460 ;; Set default-directory to the file's directory.
2462 gud-chdir-before-run
2463 ;; Don't set default-directory if no directory was specified.
2464 ;; In that case, either the file is found in the current directory,
2465 ;; in which case this setq is a no-op,
2466 ;; or it is found by searching PATH,
2467 ;; in which case we don't know what directory it was found in.
2468 (file-name-directory file)
2469 (setq default-directory (file-name-directory file)))
2470 (or (bolp) (newline))
2471 (insert "Current directory is " default-directory "\n")
2472 ;; Put the substituted and expanded file name back in its place.
2474 (while (and w (not (eq (car w) t)))
2478 (apply 'make-comint (concat "gud" filepart) program nil
2479 (if massage-args (funcall massage-args file args) args))
2480 ;; Since comint clobbered the mode, we don't set it until now.
2482 (set (make-local-variable 'gud-target-name)
2483 (and file-word (file-name-nondirectory file))))
2484 (set (make-local-variable 'gud-marker-filter) marker-filter)
2485 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2486 (setq gud-running nil)
2487 (setq gud-last-last-frame nil)
2489 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2490 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2493 (defun gud-set-buffer ()
2494 (when (eq major-mode 'gud-mode)
2495 (setq gud-comint-buffer (current-buffer))))
2497 (defvar gud-filter-defer-flag nil
2498 "Non-nil means don't process anything from the debugger right now.
2499 It is saved for when this flag is not set.")
2501 ;; These functions are responsible for inserting output from your debugger
2502 ;; into the buffer. The hard work is done by the method that is
2503 ;; the value of gud-marker-filter.
2505 (defun gud-filter (proc string)
2506 ;; Here's where the actual buffer insertion is done
2507 (let (output process-window)
2508 (if (buffer-name (process-buffer proc))
2509 (if gud-filter-defer-flag
2510 ;; If we can't process any text now,
2511 ;; save it for later.
2512 (setq gud-filter-pending-text
2513 (concat (or gud-filter-pending-text "") string))
2515 ;; If we have to ask a question during the processing,
2516 ;; defer any additional text that comes from the debugger
2517 ;; during that time.
2518 (let ((gud-filter-defer-flag t))
2519 ;; Process now any text we previously saved up.
2520 (if gud-filter-pending-text
2521 (setq string (concat gud-filter-pending-text string)
2522 gud-filter-pending-text nil))
2524 (with-current-buffer (process-buffer proc)
2525 ;; If we have been so requested, delete the debugger prompt.
2528 (if (marker-buffer gud-delete-prompt-marker)
2530 (delete-region (process-mark proc)
2531 gud-delete-prompt-marker)
2532 (set-marker gud-delete-prompt-marker nil)))
2533 ;; Save the process output, checking for source file markers.
2534 (setq output (gud-marker-filter string))
2535 ;; Check for a filename-and-line number.
2536 ;; Don't display the specified file
2537 ;; unless (1) point is at or after the position where output appears
2538 ;; and (2) this buffer is on the screen.
2539 (setq process-window
2541 (>= (point) (process-mark proc))
2542 (get-buffer-window (current-buffer)))))
2544 ;; Let the comint filter do the actual insertion.
2545 ;; That lets us inherit various comint features.
2546 (comint-output-filter proc output))
2548 ;; Put the arrow on the source line.
2549 ;; This must be outside of the save-excursion
2550 ;; in case the source file is our current buffer.
2552 (with-selected-window process-window
2553 (gud-display-frame))
2554 ;; We have to be in the proper buffer, (process-buffer proc),
2555 ;; but not in a save-excursion, because that would restore point.
2556 (with-current-buffer (process-buffer proc)
2557 (gud-display-frame))))
2559 ;; If we deferred text that arrived during this processing,
2561 (if gud-filter-pending-text
2562 (gud-filter proc ""))))))
2564 (defvar gud-minor-mode-type nil)
2565 (defvar gud-overlay-arrow-position nil)
2566 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2568 (defun gud-sentinel (proc msg)
2569 (cond ((null (buffer-name (process-buffer proc)))
2571 ;; Stop displaying an arrow in a source file.
2572 (setq gud-overlay-arrow-position nil)
2573 (set-process-buffer proc nil)
2574 (if (memq gud-minor-mode-type '(gdbmi gdba))
2577 ((memq (process-status proc) '(signal exit))
2578 ;; Stop displaying an arrow in a source file.
2579 (setq gud-overlay-arrow-position nil)
2580 (with-current-buffer gud-comint-buffer
2581 (if (memq gud-minor-mode-type '(gdbmi gdba))
2584 (let* ((obuf (current-buffer)))
2585 ;; save-excursion isn't the right thing if
2586 ;; process-buffer is current-buffer
2589 ;; Write something in *compilation* and hack its mode line,
2590 (set-buffer (process-buffer proc))
2591 ;; Fix the mode line.
2592 (setq mode-line-process
2594 (symbol-name (process-status proc))))
2595 (force-mode-line-update)
2597 (insert ?\n mode-name " " msg)
2599 (goto-char (point-max))
2600 (insert ?\n mode-name " " msg)))
2601 ;; If buffer and mode line will show that the process
2602 ;; is dead, we can delete it now. Otherwise it
2603 ;; will stay around until M-x list-processes.
2604 (delete-process proc))
2605 ;; Restore old buffer, but don't restore old point
2606 ;; if obuf is the gud buffer.
2607 (set-buffer obuf))))))
2609 (defun gud-kill-buffer-hook ()
2610 (setq gud-minor-mode-type gud-minor-mode)
2612 (kill-process (get-buffer-process (current-buffer)))
2616 (dolist (buffer (buffer-list))
2617 (unless (eq buffer gud-comint-buffer)
2618 (with-current-buffer buffer
2619 (when gud-minor-mode
2620 (setq gud-minor-mode nil)
2621 (kill-local-variable 'tool-bar-map))))))
2623 (defun gud-display-frame ()
2624 "Find and obey the last filename-and-line marker from the debugger.
2625 Obeying it means displaying in another window the specified file and line."
2627 (when gud-last-frame
2629 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2630 (setq gud-last-last-frame gud-last-frame
2631 gud-last-frame nil)))
2633 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2634 ;; and that its line LINE is visible.
2635 ;; Put the overlay-arrow on the line LINE in that buffer.
2636 ;; Most of the trickiness in here comes from wanting to preserve the current
2637 ;; region-restriction if that's possible. We use an explicit display-buffer
2638 ;; to get around the fact that this is called inside a save-excursion.
2640 (defun gud-display-line (true-file line)
2641 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2643 (with-current-buffer gud-comint-buffer
2644 (gud-find-file true-file)))
2645 (window (and buffer (or (get-buffer-window buffer)
2646 (display-buffer buffer))))
2650 (with-current-buffer buffer
2651 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2653 (format "File %s changed on disk. Reread from disk? "
2656 (setq gud-keep-buffer t)))
2661 (or gud-overlay-arrow-position
2662 (setq gud-overlay-arrow-position (make-marker)))
2663 (set-marker gud-overlay-arrow-position (point) (current-buffer)))
2664 (cond ((or (< pos (point-min)) (> pos (point-max)))
2667 (if window (set-window-point window gud-overlay-arrow-position))))))
2669 ;; The gud-call function must do the right thing whether its invoking
2670 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2671 ;; or a C buffer. In the former case, we want to supply data from
2672 ;; gud-last-frame. Here's how we do it:
2674 (defun gud-format-command (str arg)
2675 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2676 (frame (or gud-last-frame gud-last-last-frame))
2678 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2679 (let ((key (string-to-char (match-string 2 str)))
2683 (setq subst (file-name-nondirectory (if insource
2687 (setq subst (file-name-sans-extension
2688 (file-name-nondirectory (if insource
2692 (setq subst (file-name-directory (if insource
2696 (setq subst (int-to-string
2700 (+ (count-lines (point-min) (point))
2704 (setq subst (gud-find-expr)))
2706 (setq subst (gud-read-address)))
2716 (+ (count-lines (point-min) (point))
2720 (setq subst (if arg (int-to-string arg)))))
2721 (setq result (concat result (match-string 1 str) subst)))
2722 (setq str (substring str (match-end 2))))
2723 ;; There might be text left in STR when the loop ends.
2724 (concat result str)))
2726 (defun gud-read-address ()
2727 "Return a string containing the core-address found in the buffer at point."
2730 (let ((pt (point)) found begin)
2731 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2733 (found (forward-char 2)
2734 (buffer-substring found
2735 (progn (re-search-forward "[^0-9a-f]")
2738 (t (setq begin (progn (re-search-backward "[^0-9]")
2742 (re-search-forward "[^0-9]")
2744 (buffer-substring begin (point))))))))
2746 (defun gud-call (fmt &optional arg)
2747 (let ((msg (gud-format-command fmt arg)))
2748 (message "Command: %s" msg)
2750 (gud-basic-call msg)))
2752 (defun gud-basic-call (command)
2753 "Invoke the debugger COMMAND displaying source in other window."
2756 (let ((proc (get-buffer-process gud-comint-buffer)))
2757 (or proc (error "Current buffer has no process"))
2758 ;; Arrange for the current prompt to get deleted.
2760 (set-buffer gud-comint-buffer)
2763 (goto-char (process-mark proc))
2765 (if (looking-at comint-prompt-regexp)
2766 (set-marker gud-delete-prompt-marker (point)))
2767 (if (memq gud-minor-mode '(gdbmi gdba))
2768 (apply comint-input-sender (list proc command))
2769 (process-send-string proc (concat command "\n")))))))
2771 (defun gud-refresh (&optional arg)
2772 "Fix up a possibly garbled display, and redraw the arrow."
2774 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2778 ;; Code for parsing expressions out of C or Fortran code. The single entry
2779 ;; point is gud-find-expr, which tries to return an lvalue expression from
2782 (defvar gud-find-expr-function 'gud-find-c-expr)
2784 (defun gud-find-expr (&rest args)
2785 (apply gud-find-expr-function args))
2787 ;; The next eight functions are hacked from gdbsrc.el by
2788 ;; Debby Ayers <ayers@asc.slb.com>,
2789 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2791 (defun gud-find-c-expr ()
2792 "Returns the expr that surrounds point."
2796 (expr (gud-innermost-expr))
2797 (test-expr (gud-prev-expr)))
2798 (while (and test-expr (gud-expr-compound test-expr expr))
2799 (let ((prev-expr expr))
2800 (setq expr (cons (car test-expr) (cdr expr)))
2801 (goto-char (car expr))
2802 (setq test-expr (gud-prev-expr))
2803 ;; If we just pasted on the condition of an if or while,
2804 ;; throw it away again.
2805 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2806 '("if" "while" "for"))
2810 (setq test-expr (gud-next-expr))
2811 (while (gud-expr-compound expr test-expr)
2812 (setq expr (cons (car expr) (cdr test-expr)))
2813 (setq test-expr (gud-next-expr)))
2814 (buffer-substring (car expr) (cdr expr)))))
2816 (defun gud-innermost-expr ()
2817 "Returns the smallest expr that point is in; move point to beginning of it.
2818 The expr is represented as a cons cell, where the car specifies the point in
2819 the current buffer that marks the beginning of the expr and the cdr specifies
2820 the character after the end of the expr."
2821 (let ((p (point)) begin end)
2823 (setq begin (point))
2836 (defun gud-backward-sexp ()
2837 "Version of `backward-sexp' that catches errors."
2842 (defun gud-forward-sexp ()
2843 "Version of `forward-sexp' that catches errors."
2848 (defun gud-prev-expr ()
2849 "Returns the previous expr, point is set to beginning of that expr.
2850 The expr is represented as a cons cell, where the car specifies the point in
2851 the current buffer that marks the beginning of the expr and the cdr specifies
2852 the character after the end of the expr"
2853 (let ((begin) (end))
2855 (setq begin (point))
2861 (defun gud-next-expr ()
2862 "Returns the following expr, point is set to beginning of that expr.
2863 The expr is represented as a cons cell, where the car specifies the point in
2864 the current buffer that marks the beginning of the expr and the cdr specifies
2865 the character after the end of the expr."
2866 (let ((begin) (end))
2871 (setq begin (point))
2874 (defun gud-expr-compound-sep (span-start span-end)
2875 "Scan from SPAN-START to SPAN-END for punctuation characters.
2876 If `->' is found, return `?.'. If `.' is found, return `?.'.
2877 If any other punctuation is found, return `??'.
2878 If no punctuation is found, return `? '."
2881 (while (< span-start span-end)
2882 (setq syntax (char-syntax (char-after span-start)))
2885 ((= syntax ?.) (setq syntax (char-after span-start))
2887 ((= syntax ?.) (setq result ?.))
2888 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2890 (setq span-start (+ span-start 1)))
2891 (t (setq span-start span-end)
2892 (setq result ??)))))
2893 (setq span-start (+ span-start 1)))
2896 (defun gud-expr-compound (first second)
2897 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2898 The two exprs are represented as a cons cells, where the car
2899 specifies the point in the current buffer that marks the beginning of the
2900 expr and the cdr specifies the character after the end of the expr.
2901 Link exprs of the form:
2908 (let ((span-start (cdr first))
2909 (span-end (car second))
2911 (setq syntax (gud-expr-compound-sep span-start span-end))
2913 ((= (car first) (car second)) nil)
2914 ((= (cdr first) (cdr second)) nil)
2917 (setq span-start (char-after (- span-start 1)))
2918 (setq span-end (char-after span-end))
2920 ((= span-start ?)) t)
2921 ((= span-start ?]) t)
2927 (defun gud-find-class (f line)
2928 "Find fully qualified class in file F at line LINE.
2929 This function uses the `gud-jdb-classpath' (and optional
2930 `gud-jdb-sourcepath') list(s) to derive a file
2931 pathname relative to its classpath directory. The values in
2932 `gud-jdb-classpath' are assumed to have been converted to absolute
2933 pathname standards using file-truename.
2934 If F is visited by a buffer and its mode is CC-mode(Java),
2935 syntactic information of LINE is used to find the enclosing (nested)
2936 class string which is appended to the top level
2937 class of the file (using s to separate nested class ids)."
2938 ;; Convert f to a standard representation and remove suffix
2939 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
2941 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2942 (fbuffer (get-file-buffer f))
2943 syntax-symbol syntax-point class-found)
2944 (setq f (file-name-sans-extension (file-truename f)))
2945 ;; Syntax-symbol returns the symbol of the *first* element
2946 ;; in the syntactical analysis result list, syntax-point
2947 ;; returns the buffer position of same
2948 (fset 'syntax-symbol (lambda (x) (c-langelem-sym (car x))))
2949 (fset 'syntax-point (lambda (x) (c-langelem-pos (car x))))
2950 ;; Search through classpath list for an entry that is
2952 (while (and cplist (not class-found))
2953 (if (string-match (car cplist) f)
2955 (mapconcat 'identity
2957 (substring f (+ (match-end 0) 1))
2959 (setq cplist (cdr cplist)))
2960 ;; if f is visited by a java(cc-mode) buffer, walk up the
2961 ;; syntactic information chain and collect any 'inclass
2962 ;; symbols until 'topmost-intro is reached to find out if
2963 ;; point is within a nested class
2964 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
2966 (set-buffer fbuffer)
2967 (let ((nclass) (syntax))
2968 ;; While the c-syntactic information does not start
2969 ;; with the 'topmost-intro symbol, there may be
2970 ;; nested classes...
2971 (while (not (eq 'topmost-intro
2972 (syntax-symbol (c-guess-basic-syntax))))
2973 ;; Check if the current position c-syntactic
2974 ;; analysis has 'inclass
2975 (setq syntax (c-guess-basic-syntax))
2977 (and (not (eq 'inclass (syntax-symbol syntax)))
2979 (setq syntax (cdr syntax)))
2980 (if (eq 'inclass (syntax-symbol syntax))
2982 (goto-char (syntax-point syntax))
2983 ;; Now we're at the beginning of a class
2984 ;; definition. Find class name
2986 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
2988 (append (list (match-string-no-properties 1))
2990 (setq syntax (c-guess-basic-syntax))
2991 (while (and (not (syntax-point syntax)) (cdr syntax))
2992 (setq syntax (cdr syntax)))
2993 (goto-char (syntax-point syntax))
2995 (string-match (concat (car nclass) "$") class-found)
2997 (replace-match (mapconcat 'identity nclass "$")
2998 t t class-found)))))
2999 (if (not class-found)
3000 (message "gud-find-class: class for file %s not found!" f))
3002 ;; Not using classpath - try class/source association list
3003 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
3006 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
3010 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3011 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3014 (defvar gdb-script-mode-syntax-table
3015 (let ((st (make-syntax-table)))
3016 (modify-syntax-entry ?' "\"" st)
3017 (modify-syntax-entry ?# "<" st)
3018 (modify-syntax-entry ?\n ">" st)
3021 (defvar gdb-script-font-lock-keywords
3022 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
3023 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
3024 ("^\\s-*\\([a-z]+\\)" (1 font-lock-keyword-face))))
3026 (defvar gdb-script-font-lock-syntactic-keywords
3027 '(("^document\\s-.*\\(\n\\)" (1 "< b"))
3028 ;; It would be best to change the \n in front, but it's more difficult.
3029 ("^en\\(d\\)\\>" (1 "> b"))))
3031 (defun gdb-script-font-lock-syntactic-face (state)
3033 ((nth 3 state) font-lock-string-face)
3034 ((nth 7 state) font-lock-doc-face)
3035 (t font-lock-comment-face)))
3037 (defvar gdb-script-basic-indent 2)
3039 (defun gdb-script-skip-to-head ()
3040 "We're just in front of an `end' and we need to go to its head."
3041 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\)\\>" nil 'move)
3043 (gdb-script-skip-to-head)))
3045 (defun gdb-script-calculate-indentation ()
3047 ((looking-at "end\\>")
3048 (gdb-script-skip-to-head)
3049 (current-indentation))
3050 ((looking-at "else\\>")
3051 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3053 (gdb-script-skip-to-head))
3054 (current-indentation))
3056 (forward-comment (- (point-max)))
3058 (skip-chars-forward " \t")
3059 (+ (current-indentation)
3060 (if (looking-at "\\(if\\|while\\|define\\|else\\)\\>")
3061 gdb-script-basic-indent 0)))))
3063 (defun gdb-script-indent-line ()
3064 "Indent current line of GDB script."
3066 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3069 (skip-chars-forward " \t")
3070 (not (looking-at "end\\>"))))
3072 (let* ((savep (point))
3073 (indent (condition-case nil
3076 (skip-chars-forward " \t")
3077 (if (>= (point) savep) (setq savep nil))
3078 (max (gdb-script-calculate-indentation) 0))
3081 (save-excursion (indent-line-to indent))
3082 (indent-line-to indent)))))
3084 ;; Derived from cfengine.el.
3085 (defun gdb-script-beginning-of-defun ()
3086 "`beginning-of-defun' function for Gdb script mode.
3087 Treats actions as defuns."
3088 (unless (<= (current-column) (current-indentation))
3090 (if (re-search-backward "^define \\|^document " nil t)
3092 (goto-char (point-min)))
3095 ;; Derived from cfengine.el.
3096 (defun gdb-script-end-of-defun ()
3097 "`end-of-defun' function for Gdb script mode.
3098 Treats actions as defuns."
3100 (if (re-search-forward "^end" nil t)
3102 (goto-char (point-max)))
3106 (add-to-list 'auto-mode-alist '("/\\.gdbinit" . gdb-script-mode))
3109 (define-derived-mode gdb-script-mode nil "GDB-Script"
3110 "Major mode for editing GDB scripts"
3111 (set (make-local-variable 'comment-start) "#")
3112 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3113 (set (make-local-variable 'outline-regexp) "[ \t]")
3114 (set (make-local-variable 'imenu-generic-expression)
3115 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3116 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3117 (set (make-local-variable 'beginning-of-defun-function)
3118 #'gdb-script-beginning-of-defun)
3119 (set (make-local-variable 'end-of-defun-function)
3120 #'gdb-script-end-of-defun)
3121 (set (make-local-variable 'font-lock-defaults)
3122 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3123 (font-lock-syntactic-keywords
3124 . gdb-script-font-lock-syntactic-keywords)
3125 (font-lock-syntactic-face-function
3126 . gdb-script-font-lock-syntactic-face))))
3129 ;;; tooltips for GUD
3131 ;;; Customizable settings
3132 (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode)
3133 "List of modes for which to enable GUD tips."
3138 (defcustom gud-tooltip-display
3139 '((eq (tooltip-event-buffer gud-tooltip-event)
3140 (marker-buffer gud-overlay-arrow-position)))
3141 "List of forms determining where GUD tooltips are displayed.
3143 Forms in the list are combined with AND. The default is to display
3144 only tooltips in the buffer containing the overlay arrow."
3146 :tag "GUD buffers predicate"
3149 (defcustom gud-tooltip-echo-area nil
3150 "Use the echo area instead of frames for GUD tooltips."
3152 :tag "Use echo area"
3155 (define-obsolete-variable-alias 'tooltip-gud-modes
3156 'gud-tooltip-modes "22.1")
3157 (define-obsolete-variable-alias 'tooltip-gud-display
3158 'gud-tooltip-display "22.1")
3160 ;;; Reacting on mouse movements
3162 (defun gud-tooltip-change-major-mode ()
3163 "Function added to `change-major-mode-hook' when tooltip mode is on."
3164 (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
3166 (defun gud-tooltip-activate-mouse-motions-if-enabled ()
3167 "Reconsider for all buffers whether mouse motion events are desired."
3168 (remove-hook 'post-command-hook
3169 'gud-tooltip-activate-mouse-motions-if-enabled)
3170 (dolist (buffer (buffer-list))
3173 (if (and gud-tooltip-mode
3174 (memq major-mode gud-tooltip-modes))
3175 (gud-tooltip-activate-mouse-motions t)
3176 (gud-tooltip-activate-mouse-motions nil)))))
3178 (defvar gud-tooltip-mouse-motions-active nil
3179 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
3181 (defun gud-tooltip-activate-mouse-motions (activatep)
3182 "Activate/deactivate mouse motion events for the current buffer.
3183 ACTIVATEP non-nil means activate mouse motion events."
3186 (make-local-variable 'gud-tooltip-mouse-motions-active)
3187 (setq gud-tooltip-mouse-motions-active t)
3188 (make-local-variable 'track-mouse)
3189 (setq track-mouse t))
3190 (when gud-tooltip-mouse-motions-active
3191 (kill-local-variable 'gud-tooltip-mouse-motions-active)
3192 (kill-local-variable 'track-mouse))))
3194 (defun gud-tooltip-mouse-motion (event)
3195 "Command handler for mouse movement events in `global-map'."
3198 (when (car (mouse-pixel-position))
3199 (setq tooltip-last-mouse-motion-event (copy-sequence event))
3200 (tooltip-start-delayed-tip)))
3204 (defvar gud-tooltip-original-filter nil
3205 "Process filter to restore after GUD output has been received.")
3207 (defvar gud-tooltip-dereference nil
3208 "Non-nil means print expressions with a `*' in front of them.
3209 For C this would dereference a pointer expression.")
3211 (defvar gud-tooltip-event nil
3212 "The mouse movement event that led to a tooltip display.
3213 This event can be examined by forms in GUD-TOOLTIP-DISPLAY.")
3215 (defun toggle-gud-tooltip-dereference ()
3216 "Toggle whether tooltips should show `* expr' or `expr'."
3218 (setq gud-tooltip-dereference (not gud-tooltip-dereference))
3219 (when (interactive-p)
3220 (message "Dereferencing is now %s."
3221 (if gud-tooltip-dereference "on" "off"))))
3223 (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
3224 'toggle-gud-tooltip-dereference "22.1")
3227 (define-minor-mode gud-tooltip-mode
3228 "Toggle the display of GUD tooltips."
3232 (if gud-tooltip-mode
3234 (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3235 (add-hook 'pre-command-hook 'tooltip-hide)
3236 (add-hook 'tooltip-hook 'gud-tooltip-tips)
3237 (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
3238 (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
3239 (remove-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3240 (remove-hook 'tooltip-hook 'gud-tooltip-tips)
3241 (define-key global-map [mouse-movement] 'ignore)))
3242 (gud-tooltip-activate-mouse-motions-if-enabled)
3245 (buffer-name gud-comint-buffer); gud-comint-buffer might be kille
3246 (with-current-buffer gud-comint-buffer
3247 (memq gud-minor-mode '(gdbmi gdba))))
3248 (if gud-tooltip-mode
3250 (dolist (buffer (buffer-list))
3251 (unless (eq buffer gud-comint-buffer)
3252 (with-current-buffer buffer
3253 (when (and (memq gud-minor-mode '(gdbmi gdba))
3254 (not (string-match "\\`\\*.+\\*\\'"
3256 (make-local-variable 'gdb-define-alist)
3257 (gdb-create-define-alist)
3258 (add-hook 'after-save-hook
3259 'gdb-create-define-alist nil t))))))
3260 (kill-local-variable 'gdb-define-alist)
3261 (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
3263 ; This will only display data that comes in one chunk.
3264 ; Larger arrays (say 400 elements) are displayed in
3265 ; the tooltip incompletely and spill over into the gud buffer.
3266 ; Switching the process-filter creates timing problems and
3267 ; it may be difficult to do better. Using annotations as in
3268 ; gdb-ui.el gets round this problem.
3269 (defun gud-tooltip-process-output (process output)
3270 "Process debugger output and show it in a tooltip window."
3271 (set-process-filter process gud-tooltip-original-filter)
3272 (tooltip-show (tooltip-strip-prompt process output)
3273 (or gud-tooltip-echo-area tooltip-use-echo-area)))
3275 (defun gud-tooltip-print-command (expr)
3276 "Return a suitable command to print the expression EXPR.
3277 If GUD-TOOLTIP-DEREFERENCE is t, also prepend a `*' to EXPR."
3278 (when gud-tooltip-dereference
3279 (setq expr (concat "*" expr)))
3280 (case gud-minor-mode
3281 (gdba (concat "server print " expr))
3282 ((dbx gdbmi) (concat "print " expr))
3283 (xdb (concat "p " expr))
3284 (sdb (concat expr "/"))
3287 (defun gud-tooltip-tips (event)
3288 "Show tip for identifier or selection under the mouse.
3289 The mouse must either point at an identifier or inside a selected
3290 region for the tip window to be shown. If gud-tooltip-dereference is t,
3291 add a `*' in front of the printed expression. In the case of a C program
3292 controlled by GDB, show the associated #define directives when program is
3295 This function must return nil if it doesn't handle EVENT."
3297 (when (and (eventp event)
3299 (boundp 'gud-comint-buffer)
3301 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
3302 (setq process (get-buffer-process gud-comint-buffer))
3303 (posn-point (event-end event))
3304 (or (and (eq gud-minor-mode 'gdba) (not gdb-active-process))
3305 (progn (setq gud-tooltip-event event)
3306 (eval (cons 'and gud-tooltip-display)))))
3307 (let ((expr (tooltip-expr-to-print event)))
3309 (if (and (eq gud-minor-mode 'gdba)
3310 (not gdb-active-process))
3312 (with-current-buffer
3313 (window-buffer (let ((mouse (mouse-position)))
3314 (window-at (cadr mouse)
3316 (let ((define-elt (assoc expr gdb-define-alist)))
3317 (unless (null define-elt)
3320 (or gud-tooltip-echo-area tooltip-use-echo-area))
3322 (let ((cmd (gud-tooltip-print-command expr)))
3323 (when (and gud-tooltip-mode (eq gud-minor-mode 'gdb))
3324 (gud-tooltip-mode -1)
3325 (message-box "Using GUD tooltips in this mode is unsafe\n\
3326 so they have been disabled."))
3327 (unless (null cmd) ; CMD can be nil if unknown debugger
3328 (if (memq gud-minor-mode '(gdba gdbmi))
3332 gdb-server-prefix "macro expand " expr "\n")
3333 `(lambda () (gdb-tooltip-print-1 ,expr))))
3335 (list (concat cmd "\n") 'gdb-tooltip-print)))
3336 (setq gud-tooltip-original-filter (process-filter process))
3337 (set-process-filter process 'gud-tooltip-process-output)
3338 (gud-basic-call cmd))
3343 ;;; arch-tag: 6d990948-df65-461a-be39-1c7fb83ac4c4
3344 ;;; gud.el ends here