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, 2006 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
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)
55 (defvar gdb-speedbar-auto-raise)
58 ;; ======================================================================
59 ;; GUD commands must be visible in C buffers visited by GUD
62 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
63 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
68 (defcustom gud-key-prefix "\C-x\C-a"
69 "Prefix of all GUD commands valid in C buffers."
73 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
74 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
76 (defvar gud-marker-filter nil)
77 (put 'gud-marker-filter 'permanent-local t)
78 (defvar gud-find-file nil)
79 (put 'gud-find-file 'permanent-local t)
81 (defun gud-marker-filter (&rest args)
82 (apply gud-marker-filter args))
84 (defvar gud-minor-mode nil)
85 (put 'gud-minor-mode 'permanent-local t)
87 (defvar gud-keep-buffer nil)
89 (defun gud-symbol (sym &optional soft minor-mode)
90 "Return the symbol used for SYM in MINOR-MODE.
91 MINOR-MODE defaults to `gud-minor-mode.
92 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
93 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
94 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
95 (funcall (if soft 'intern-soft 'intern)
96 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
98 (defun gud-val (sym &optional minor-mode)
99 "Return the value of `gud-symbol' SYM. Default to nil."
100 (let ((sym (gud-symbol sym t minor-mode)))
101 (if (boundp sym) (symbol-value sym))))
103 (defvar gud-running nil
104 "Non-nil if debuggee is running.
105 Used to grey out relevant togolbar icons.")
107 ;; Use existing Info buffer, if possible.
108 (defun gud-goto-info ()
109 "Go to relevant Emacs info node."
111 (let ((same-window-regexps same-window-regexps)
112 (display-buffer-reuse-frames t))
116 (if (eq (window-buffer window) (get-buffer "*info*"))
118 (setq same-window-regexps nil)
119 (throw 'info-found nil))))
121 (select-frame (make-frame)))
122 (if (memq gud-minor-mode '(gdbmi gdba))
123 (info "(emacs)GDB Graphical Interface")
124 (info "(emacs)Debuggers"))))
126 (defun gud-tool-bar-item-visible-no-fringe ()
127 (not (or (eq (buffer-local-value 'major-mode (window-buffer)) 'speedbar-mode)
128 (and (memq gud-minor-mode '(gdbmi gdba))
129 (> (car (window-fringes)) 0)))))
131 (defun gud-stop-subjob ()
134 (buffer-local-value 'gud-target-name gud-comint-buffer) "emacs")
136 (comint-interrupt-subjob)))
138 (easy-mmode-defmap gud-menu-map
139 '(([help] "Info" . gud-goto-info)
140 ([tooltips] menu-item "Toggle GUD tooltips" gud-tooltip-mode
141 :enable (and (not emacs-basic-display)
143 (fboundp 'x-show-tip))
144 :button (:toggle . gud-tooltip-mode))
145 ([refresh] "Refresh" . gud-refresh)
146 ([run] menu-item "Run" gud-run
147 :enable (and (not gud-running)
148 (memq gud-minor-mode '(gdbmi gdb dbx jdb)))
149 :visible (not (eq gud-minor-mode 'gdba)))
150 ([go] menu-item "Run/Continue" gud-go
151 :visible (and (not gud-running)
152 (eq gud-minor-mode 'gdba)))
153 ([stop] menu-item "Stop" gud-stop-subjob
154 :visible (or (not (eq gud-minor-mode 'gdba))
156 (eq gud-minor-mode 'gdba))))
157 ([until] menu-item "Continue to selection" gud-until
158 :enable (and (not gud-running)
159 (memq gud-minor-mode '(gdbmi gdba gdb perldb)))
160 :visible (gud-tool-bar-item-visible-no-fringe))
161 ([remove] menu-item "Remove Breakpoint" gud-remove
162 :enable (not gud-running)
163 :visible (gud-tool-bar-item-visible-no-fringe))
164 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
165 :enable (memq gud-minor-mode
166 '(gdbmi gdba gdb sdb xdb bashdb)))
167 ([break] menu-item "Set Breakpoint" gud-break
168 :enable (not gud-running)
169 :visible (gud-tool-bar-item-visible-no-fringe))
170 ([up] menu-item "Up Stack" gud-up
171 :enable (and (not gud-running)
173 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
174 ([down] menu-item "Down Stack" gud-down
175 :enable (and (not gud-running)
177 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
178 ([pp] menu-item "Print the emacs s-expression" gud-pp
179 :enable (and (not gud-running)
181 :visible (and (string-equal
183 'gud-target-name gud-comint-buffer) "emacs")
184 (eq gud-minor-mode 'gdba)))
185 ([print*] menu-item "Print Dereference" gud-pstar
186 :enable (and (not gud-running)
187 (memq gud-minor-mode '(gdbmi gdba gdb))))
188 ([print] menu-item "Print Expression" gud-print
189 :enable (not gud-running))
190 ([watch] menu-item "Watch Expression" gud-watch
191 :enable (and (not gud-running)
192 (memq gud-minor-mode '(gdbmi gdba))))
193 ([finish] menu-item "Finish Function" gud-finish
194 :enable (and (not gud-running)
196 '(gdbmi gdba gdb xdb jdb pdb bashdb))))
197 ([stepi] menu-item "Step Instruction" gud-stepi
198 :enable (and (not gud-running)
199 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
200 ([nexti] menu-item "Next Instruction" gud-nexti
201 :enable (and (not gud-running)
202 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
203 ([step] menu-item "Step Line" gud-step
204 :enable (not gud-running))
205 ([next] menu-item "Next Line" gud-next
206 :enable (not gud-running))
207 ([cont] menu-item "Continue" gud-cont
208 :enable (not gud-running)
209 :visible (not (eq gud-minor-mode 'gdba))))
210 "Menu for `gud-mode'."
213 (easy-mmode-defmap gud-minor-mode-map
214 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
215 "Map used in visited files.")
217 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
218 (if m (setcdr m gud-minor-mode-map)
219 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
222 ;; Will inherit from comint-mode via define-derived-mode.
224 "`gud-mode' keymap.")
226 (defvar gud-tool-bar-map
227 (if (display-graphic-p)
228 (let ((map (make-sparse-keymap)))
229 (dolist (x '((gud-break . "gud/break")
230 (gud-remove . "gud/remove")
231 (gud-print . "gud/print")
232 (gud-pstar . "gud/pstar")
234 (gud-watch . "gud/watch")
235 (gud-run . "gud/run")
237 (gud-stop-subjob . "gud/stop")
238 ;; gud-s, gud-si etc. instead of gud-step,
239 ;; gud-stepi, to avoid file-name clashes on DOS
241 (gud-cont . "gud/cont")
242 (gud-until . "gud/until")
243 (gud-next . "gud/next")
244 (gud-step . "gud/step")
245 (gud-finish . "gud/finish")
246 (gud-nexti . "gud/nexti")
247 (gud-stepi . "gud/stepi")
249 (gud-down . "gud/down")
250 (gud-goto-info . "info"))
252 (tool-bar-local-item-from-menu
253 (car x) (cdr x) map gud-minor-mode-map)))))
255 (defun gud-file-name (f)
256 "Transform a relative file name to an absolute file name.
257 Uses `gud-<MINOR-MODE>-directories' to find the source files."
258 (if (file-exists-p f) (expand-file-name f)
259 (let ((directories (gud-val 'directories))
262 (let ((path (expand-file-name f (car directories))))
263 (if (file-exists-p path)
266 (setq directories (cdr directories)))
269 (defun gud-find-file (file)
270 ;; Don't get confused by double slashes in the name that comes from GDB.
271 (while (string-match "//+" file)
272 (setq file (replace-match "/" t t file)))
273 (let ((minor-mode gud-minor-mode)
274 (buf (funcall (or gud-find-file 'gud-file-name) file)))
276 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
278 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
279 (with-current-buffer buf
280 (set (make-local-variable 'gud-minor-mode) minor-mode)
281 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
282 (when (and gud-tooltip-mode
283 (memq gud-minor-mode '(gdbmi gdba)))
284 (make-local-variable 'gdb-define-alist)
285 (unless gdb-define-alist (gdb-create-define-alist))
286 (add-hook 'after-save-hook 'gdb-create-define-alist nil t))
287 (make-local-variable 'gud-keep-buffer))
290 ;; ======================================================================
291 ;; command definition
293 ;; This macro is used below to define some basic debugger interface commands.
294 ;; Of course you may use `gud-def' with any other debugger command, including
295 ;; user defined ones.
297 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
298 ;; which defines FUNC to send the command NAME to the debugger, gives
299 ;; it the docstring DOC, and binds that function to KEY in the GUD
300 ;; major mode. The function is also bound in the global keymap with the
303 (defmacro gud-def (func cmd key &optional doc)
304 "Define FUNC to be a command sending STR and bound to KEY, with
305 optional doc string DOC. Certain %-escapes in the string arguments
306 are interpreted specially if present. These are:
308 %f name (without directory) of current source file.
309 %F name (without directory or extension) of current source file.
310 %d directory of current source file.
311 %l number of current source line
312 %e text of the C lvalue or function-call expression surrounding point.
313 %a text of the hexadecimal address surrounding point
314 %p prefix argument to the command (if any) as a number
316 The `current' source file is the file of the current buffer (if
317 we're in a C file) or the source file current at the last break or
318 step (if we're in the GUD buffer).
319 The `current' line is that of the current buffer (if we're in a
320 source file) or the source line number at the last break or step (if
321 we're in the GUD buffer)."
324 ,@(if doc (list doc))
329 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
330 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
332 ;; Where gud-display-frame should put the debugging arrow; a cons of
333 ;; (filename . line-number). This is set by the marker-filter, which scans
334 ;; the debugger's output for indications of the current program counter.
335 (defvar gud-last-frame nil)
337 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
338 ;; the last frame, even if it's been called before and gud-last-frame has
340 (defvar gud-last-last-frame nil)
342 ;; All debugger-specific information is collected here.
343 ;; Here's how it works, in case you ever need to add a debugger to the mode.
345 ;; Each entry must define the following at startup:
348 ;; comint-prompt-regexp
349 ;; gud-<name>-massage-args
350 ;; gud-<name>-marker-filter
351 ;; gud-<name>-find-file
353 ;; The job of the massage-args method is to modify the given list of
354 ;; debugger arguments before running the debugger.
356 ;; The job of the marker-filter method is to detect file/line markers in
357 ;; strings and set the global gud-last-frame to indicate what display
358 ;; action (if any) should be triggered by the marker. Note that only
359 ;; whatever the method *returns* is displayed in the buffer; thus, you
360 ;; can filter the debugger's output, interpreting some and passing on
363 ;; The job of the find-file method is to visit and return the buffer indicated
364 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
367 ;; ======================================================================
368 ;; speedbar support functions and variables.
369 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
371 (defvar gud-last-speedbar-stackframe nil
372 "Description of the currently displayed GUD stack.
373 t means that there is no stack, and we are in display-file mode.")
375 (defvar gud-speedbar-key-map nil
376 "Keymap used when in the buffers display mode.")
378 (defun gud-speedbar-item-info ()
379 "Display the data type of the watch expression element."
380 (let ((var (nth (- (line-number-at-pos (point)) 2) gdb-var-list)))
382 (speedbar-message "%s" (nth 3 var)))))
384 (defun gud-install-speedbar-variables ()
385 "Install those variables used by speedbar to enhance gud/gdb."
386 (if gud-speedbar-key-map
388 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
390 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
391 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
392 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
393 (define-key gud-speedbar-key-map " " 'speedbar-toggle-line-expansion)
394 (define-key gud-speedbar-key-map "[" 'speedbar-expand-line-descendants)
395 (define-key gud-speedbar-key-map "]" 'speedbar-contract-line-descendants)
396 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)
397 (define-key gud-speedbar-key-map "p" 'gud-pp))
399 (speedbar-add-expansion-list '("GUD" gud-speedbar-menu-items
401 gud-expansion-speedbar-buttons))
404 'speedbar-mode-functions-list
405 '("GUD" (speedbar-item-info . gud-speedbar-item-info)
406 (speedbar-line-directory . ignore))))
408 (defvar gud-speedbar-menu-items
409 '(["Jump to stack frame" speedbar-edit-line
410 :visible (with-current-buffer gud-comint-buffer
411 (not (memq gud-minor-mode '(gdbmi gdba))))]
412 ["Edit value" speedbar-edit-line
413 :visible (with-current-buffer gud-comint-buffer
414 (memq gud-minor-mode '(gdbmi gdba)))]
415 ["Delete expression" gdb-var-delete
416 (with-current-buffer gud-comint-buffer
417 (memq gud-minor-mode '(gdbmi gdba)))]
418 ["Auto raise frame" gdb-speedbar-auto-raise
419 :style toggle :selected gdb-speedbar-auto-raise
420 :visible (with-current-buffer gud-comint-buffer
421 (memq gud-minor-mode '(gdbmi gdba)))])
422 "Additional menu items to add to the speedbar frame.")
424 ;; Make sure our special speedbar mode is loaded
425 (if (featurep 'speedbar)
426 (gud-install-speedbar-variables)
427 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
429 (defun gud-expansion-speedbar-buttons (directory zero)
430 "Wrapper for call to speedbar-add-expansion-list. DIRECTORY and
431 ZERO are not used, but are required by the caller."
432 (gud-speedbar-buttons gud-comint-buffer))
434 (defun gud-speedbar-buttons (buffer)
435 "Create a speedbar display based on the current state of GUD.
436 If the GUD BUFFER is not running a supported debugger, then turn
437 off the specialized speedbar mode. BUFFER is not used, but are
438 required by the caller."
439 (when (and gud-comint-buffer
440 ;; gud-comint-buffer might be killed
441 (buffer-name gud-comint-buffer))
442 (let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
443 (window (get-buffer-window (current-buffer) 0))
444 (p (window-point window)))
446 ((memq minor-mode '(gdbmi gdba))
447 (when (or gdb-var-changed
449 (goto-char (point-min))
450 (let ((case-fold-search t))
451 (looking-at "Watch Expressions:")))))
453 (insert "Watch Expressions:\n")
454 (if gdb-speedbar-auto-raise
455 (raise-frame speedbar-frame))
456 (let ((var-list gdb-var-list))
458 (let* (char (depth 0) (start 0)
459 (var (car var-list)) (varnum (nth 1 var)))
460 (while (string-match "\\." varnum start)
461 (setq depth (1+ depth)
462 start (1+ (match-beginning 0))))
463 (if (or (equal (nth 2 var) "0")
464 (and (equal (nth 2 var) "1")
465 (string-match "char \\*$" (nth 3 var))))
466 (speedbar-make-tag-line 'bracket ?? nil nil
467 (concat (car var) "\t" (nth 4 var))
470 (if gdb-show-changed-values
472 (changed 'font-lock-warning-face)
473 (out-of-scope 'shadow)
476 (if (and (cadr var-list)
477 (string-match (concat varnum "\\.")
478 (cadr (cadr var-list))))
481 (if (string-match "\\*$" (nth 3 var))
482 (speedbar-make-tag-line 'bracket char
483 'gdb-speedbar-expand-node varnum
484 (concat (car var) "\t"
489 gdb-show-changed-values)
490 'font-lock-warning-face
492 (speedbar-make-tag-line 'bracket char
493 'gdb-speedbar-expand-node varnum
494 (concat (car var) "\t" (nth 3 var))
495 nil nil nil depth))))
496 (setq var-list (cdr var-list))))
497 (setq gdb-var-changed nil)))
498 (t (unless (and (save-excursion
499 (goto-char (point-min))
500 (looking-at "Current Stack:"))
501 (equal gud-last-last-frame gud-last-speedbar-stackframe))
502 (let ((gud-frame-list
503 (cond ((eq minor-mode 'gdb)
504 (gud-gdb-get-stackframe buffer))
505 ;; Add more debuggers here!
506 (t (speedbar-remove-localized-speedbar-support buffer)
509 (if (not gud-frame-list)
510 (insert "No Stack frames\n")
511 (insert "Current Stack:\n"))
512 (dolist (frame gud-frame-list)
513 (insert (nth 1 frame) ":\n")
514 (if (= (length frame) 2)
516 (speedbar-insert-button (car frame)
517 'speedbar-directory-face
519 (speedbar-insert-button
522 'speedbar-highlight-face
523 (cond ((memq minor-mode '(gdbmi gdba gdb))
524 'gud-gdb-goto-stackframe)
525 (t (error "Should never be here")))
527 (setq gud-last-speedbar-stackframe gud-last-last-frame))))
528 (set-window-point window p))))
531 ;; ======================================================================
534 ;; History of argument lists passed to gdb.
535 (defvar gud-gdb-history nil)
537 (defcustom gud-gdb-command-name "gdb --annotate=3"
538 "Default command to execute an executable under the GDB debugger."
542 (defvar gud-gdb-marker-regexp
543 ;; This used to use path-separator instead of ":";
544 ;; however, we found that on both Windows 32 and MSDOS
545 ;; a colon is correct here.
546 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
547 "\\([0-9]*\\)" ":" ".*\n"))
549 ;; There's no guarantee that Emacs will hand the filter the entire
550 ;; marker at once; it could be broken up across several strings. We
551 ;; might even receive a big chunk with several markers in it. If we
552 ;; receive a chunk of text which looks like it might contain the
553 ;; beginning of a marker, we save it here between calls to the
555 (defvar gud-marker-acc "")
556 (make-variable-buffer-local 'gud-marker-acc)
558 (defun gud-gdb-marker-filter (string)
559 (setq gud-marker-acc (concat gud-marker-acc string))
562 ;; Process all the complete markers in this chunk.
563 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
566 ;; Extract the frame position from the marker.
567 gud-last-frame (cons (match-string 1 gud-marker-acc)
568 (string-to-number (match-string 2 gud-marker-acc)))
570 ;; Append any text before the marker to the output we're going
571 ;; to return - we don't include the marker in this text.
572 output (concat output
573 (substring gud-marker-acc 0 (match-beginning 0)))
575 ;; Set the accumulator to the remaining text.
576 gud-marker-acc (substring gud-marker-acc (match-end 0))))
578 ;; Check for annotations and change gud-minor-mode to 'gdba if
580 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
581 (let ((match (match-string 1 gud-marker-acc)))
583 ;; Pick up stopped annotation if attaching to process.
584 (if (string-equal match "stopped") (setq gdb-active-process t))
586 ;; Using annotations, switch to gud-gdba-marker-filter.
587 (when (string-equal match "prompt")
592 ;; Append any text before the marker to the output we're going
593 ;; to return - we don't include the marker in this text.
594 output (concat output
595 (substring gud-marker-acc 0 (match-beginning 0)))
597 ;; Set the accumulator to the remaining text.
599 gud-marker-acc (substring gud-marker-acc (match-end 0)))
601 ;; Pick up any errors that occur before first prompt annotation.
602 (if (string-equal match "error-begin")
603 (put-text-property 0 (length gud-marker-acc)
604 'face font-lock-warning-face
607 ;; Does the remaining text look like it might end with the
608 ;; beginning of another marker? If it does, then keep it in
609 ;; gud-marker-acc until we receive the rest of it. Since we
610 ;; know the full marker regexp above failed, it's pretty simple to
611 ;; test for marker starts.
612 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
614 ;; Everything before the potential marker start can be output.
615 (setq output (concat output (substring gud-marker-acc
616 0 (match-beginning 0))))
618 ;; Everything after, we save, to combine with later input.
620 (substring gud-marker-acc (match-beginning 0))))
622 (setq output (concat output gud-marker-acc)
627 (easy-mmode-defmap gud-minibuffer-local-map
628 '(("\C-i" . comint-dynamic-complete-filename))
629 "Keymap for minibuffer prompting of gud startup command."
630 :inherit minibuffer-local-map)
632 (defun gud-query-cmdline (minor-mode &optional init)
633 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
634 (cmd-name (gud-val 'command-name minor-mode)))
635 (unless (boundp hist-sym) (set hist-sym nil))
636 (read-from-minibuffer
637 (format "Run %s (like this): " minor-mode)
638 (or (car-safe (symbol-value hist-sym))
639 (concat (or cmd-name (symbol-name minor-mode))
643 (dolist (f (directory-files default-directory) file)
644 (if (and (file-executable-p f)
645 (not (file-directory-p f))
647 (file-newer-than-file-p f file)))
649 gud-minibuffer-local-map nil
652 (defvar gdb-first-prompt t)
654 (defvar gud-filter-pending-text nil
655 "Non-nil means this is text that has been saved for later in `gud-filter'.")
658 (defun gdb (command-line)
659 "Run gdb on program FILE in buffer *gud-FILE*.
660 The directory containing FILE becomes the initial working
661 directory and source-file directory for your debugger. By
662 default this command starts GDB using a graphical interface. See
663 `gdba' for more information.
665 To run GDB in text command mode, set `gud-gdb-command-name' to
666 \"gdb --fullname\" and include the pathname, if necessary."
667 (interactive (list (gud-query-cmdline 'gdb)))
669 (if (and gud-comint-buffer
670 (buffer-name gud-comint-buffer)
671 (with-current-buffer gud-comint-buffer (eq gud-minor-mode 'gdba)))
672 (error "Multiple debugging is only supported with \"gdb --fullname\""))
674 (gud-common-init command-line nil 'gud-gdb-marker-filter)
675 (set (make-local-variable 'gud-minor-mode) 'gdb)
677 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
678 (gud-def gud-tbreak "tbreak %f:%l" "\C-t"
679 "Set temporary breakpoint at current line.")
680 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
681 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
682 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
683 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
684 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
685 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
686 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
688 (progn (gud-call "tbreak %f:%l") (gud-call "jump %f:%l"))
689 "\C-j" "Set execution address to current line.")
691 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
692 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
693 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
694 (gud-def gud-pstar "print* %e" nil
695 "Evaluate C dereferenced pointer expression at point.")
697 ;; For debugging Emacs only.
698 (gud-def gud-pv "pv1 %e" "\C-v" "Print the value of the lisp variable.")
700 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
701 (gud-def gud-run "run" nil "Run the program.")
703 (local-set-key "\C-i" 'gud-gdb-complete-command)
704 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
705 (setq paragraph-start comint-prompt-regexp)
706 (setq gdb-first-prompt t)
707 (setq gud-filter-pending-text nil)
708 (run-hooks 'gdb-mode-hook))
710 ;; One of the nice features of GDB is its impressive support for
711 ;; context-sensitive command completion. We preserve that feature
712 ;; in the GUD buffer by using a GDB command designed just for Emacs.
714 ;; The completion process filter indicates when it is finished.
715 (defvar gud-gdb-fetch-lines-in-progress)
717 ;; Since output may arrive in fragments we accumulate partials strings here.
718 (defvar gud-gdb-fetch-lines-string)
720 ;; We need to know how much of the completion to chop off.
721 (defvar gud-gdb-fetch-lines-break)
723 ;; The completion list is constructed by the process filter.
724 (defvar gud-gdb-fetched-lines)
726 (defvar gud-comint-buffer nil)
728 (defun gud-gdb-complete-command ()
729 "Perform completion on the GDB command preceding point.
730 This is implemented using the GDB `complete' command which isn't
731 available with older versions of GDB."
734 (command (buffer-substring (comint-line-beginning-position) end))
736 ;; Find the word break. This match will always succeed.
737 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
738 (substring command (match-beginning 2))))
740 (gud-gdb-run-command-fetch-lines (concat "complete " command)
742 ;; From string-match above.
743 (match-beginning 2))))
744 ;; Protect against old versions of GDB.
746 (string-match "^Undefined command: \"complete\"" (car complete-list))
747 (error "This version of GDB doesn't support the `complete' command"))
748 ;; Sort the list like readline.
749 (setq complete-list (sort complete-list (function string-lessp)))
750 ;; Remove duplicates.
751 (let ((first complete-list)
752 (second (cdr complete-list)))
754 (if (string-equal (car first) (car second))
755 (setcdr first (setq second (cdr second)))
757 second (cdr second)))))
758 ;; Add a trailing single quote if there is a unique completion
759 ;; and it contains an odd number of unquoted single quotes.
760 (and (= (length complete-list) 1)
761 (let ((str (car complete-list))
764 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
765 (setq count (1+ count)
767 (and (= (mod count 2) 1)
768 (setq complete-list (list (concat str "'"))))))
769 ;; Let comint handle the rest.
770 (comint-dynamic-simple-complete command-word complete-list)))
772 ;; The completion process filter is installed temporarily to slurp the
773 ;; output of GDB up to the next prompt and build the completion list.
774 (defun gud-gdb-fetch-lines-filter (string filter)
775 "Filter used to read the list of lines output by a command.
776 STRING is the output to filter.
777 It is passed through FILTER before we look at it."
778 (setq string (funcall filter string))
779 (setq string (concat gud-gdb-fetch-lines-string string))
780 (while (string-match "\n" string)
781 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
782 gud-gdb-fetched-lines)
783 (setq string (substring string (match-end 0))))
784 (if (string-match comint-prompt-regexp string)
786 (setq gud-gdb-fetch-lines-in-progress nil)
789 (setq gud-gdb-fetch-lines-string string)
792 ;; gdb speedbar functions
794 (defun gud-gdb-goto-stackframe (text token indent)
795 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
796 (speedbar-with-attached-buffer
797 (gud-basic-call (concat "server frame " (nth 1 token)))
800 (defvar gud-gdb-fetched-stack-frame nil
801 "Stack frames we are fetching from GDB.")
803 ;(defun gud-gdb-get-scope-data (text token indent)
804 ; ;; checkdoc-params: (indent)
805 ; "Fetch data associated with a stack frame, and expand/contract it.
806 ;Data to do this is retrieved from TEXT and TOKEN."
807 ; (let ((args nil) (scope nil))
808 ; (gud-gdb-run-command-fetch-lines "info args")
810 ; (gud-gdb-run-command-fetch-lines "info local")
814 (defun gud-gdb-get-stackframe (buffer)
815 "Extract the current stack frame out of the GUD GDB BUFFER."
817 (fetched-stack-frame-list
818 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
819 (if (and (car fetched-stack-frame-list)
820 (string-match "No stack" (car fetched-stack-frame-list)))
821 ;; Go into some other mode???
823 (dolist (e fetched-stack-frame-list)
824 (let ((name nil) (num nil))
826 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
827 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
828 (if (not (string-match
829 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
832 (list (nth 0 (car newlst))
835 (match-string 2 e))))
836 (setq num (match-string 1 e)
837 name (match-string 2 e))
841 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
842 (list name num (match-string 1 e)
848 ;(defun gud-gdb-selected-frame-info (buffer)
849 ; "Learn GDB information for the currently selected stack frame in BUFFER."
852 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
853 "Run COMMAND, and return the list of lines it outputs.
854 BUFFER is the GUD buffer in which to run the command.
855 SKIP is the number of chars to skip on each lines, it defaults to 0."
856 (with-current-buffer buffer
858 (goto-char (point-max))
860 (not (looking-at comint-prompt-regexp)))
862 ;; Much of this copied from GDB complete, but I'm grabbing the stack
864 (let ((gud-gdb-fetch-lines-in-progress t)
865 (gud-gdb-fetched-lines nil)
866 (gud-gdb-fetch-lines-string nil)
867 (gud-gdb-fetch-lines-break (or skip 0))
869 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
870 ;; Issue the command to GDB.
871 (gud-basic-call command)
873 (while gud-gdb-fetch-lines-in-progress
874 (accept-process-output (get-buffer-process buffer)))
875 (nreverse gud-gdb-fetched-lines)))))
878 ;; ======================================================================
881 ;; History of argument lists passed to sdb.
882 (defvar gud-sdb-history nil)
884 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
885 "If nil, we're on a System V Release 4 and don't need the tags hack.")
887 (defvar gud-sdb-lastfile nil)
889 (defun gud-sdb-marker-filter (string)
891 (if gud-marker-acc (concat gud-marker-acc string) string))
893 ;; Process all complete markers in this chunk
896 ;; System V Release 3.2 uses this format
897 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
898 gud-marker-acc start)
900 (cons (match-string 3 gud-marker-acc)
901 (string-to-number (match-string 4 gud-marker-acc)))))
902 ;; System V Release 4.0 quite often clumps two lines together
903 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
904 gud-marker-acc start)
905 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
907 (cons gud-sdb-lastfile
908 (string-to-number (match-string 3 gud-marker-acc)))))
909 ;; System V Release 4.0
910 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
911 gud-marker-acc start)
912 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
913 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
914 gud-marker-acc start))
916 (cons gud-sdb-lastfile
917 (string-to-number (match-string 1 gud-marker-acc)))))
919 (setq gud-sdb-lastfile nil)))
920 (setq start (match-end 0)))
922 ;; Search for the last incomplete line in this chunk
923 (while (string-match "\n" gud-marker-acc start)
924 (setq start (match-end 0)))
926 ;; If we have an incomplete line, store it in gud-marker-acc.
927 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
930 (defun gud-sdb-find-file (f)
931 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
934 (defun sdb (command-line)
935 "Run sdb on program FILE in buffer *gud-FILE*.
936 The directory containing FILE becomes the initial working directory
937 and source-file directory for your debugger."
938 (interactive (list (gud-query-cmdline 'sdb)))
940 (if gud-sdb-needs-tags (require 'etags))
941 (if (and gud-sdb-needs-tags
942 (not (and (boundp 'tags-file-name)
943 (stringp tags-file-name)
944 (file-exists-p tags-file-name))))
945 (error "The sdb support requires a valid tags table to work"))
947 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
948 (set (make-local-variable 'gud-minor-mode) 'sdb)
950 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
951 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
952 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
953 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
954 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
955 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
956 (gud-def gud-cont "c" "\C-r" "Continue with display.")
957 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
959 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
960 (setq paragraph-start comint-prompt-regexp)
961 (run-hooks 'sdb-mode-hook)
964 ;; ======================================================================
967 ;; History of argument lists passed to dbx.
968 (defvar gud-dbx-history nil)
970 (defcustom gud-dbx-directories nil
971 "*A list of directories that dbx should search for source code.
972 If nil, only source files in the program directory
973 will be known to dbx.
975 The file names should be absolute, or relative to the directory
976 containing the executable being debugged."
977 :type '(choice (const :tag "Current Directory" nil)
982 (defun gud-dbx-massage-args (file args)
983 (nconc (let ((directories gud-dbx-directories)
986 (setq result (cons (car directories) (cons "-I" result)))
987 (setq directories (cdr directories)))
991 (defun gud-dbx-marker-filter (string)
992 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
995 ;; Process all complete markers in this chunk.
996 (while (or (string-match
997 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
998 gud-marker-acc start)
1000 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
1001 gud-marker-acc start))
1002 (setq gud-last-frame
1003 (cons (match-string 2 gud-marker-acc)
1004 (string-to-number (match-string 1 gud-marker-acc)))
1005 start (match-end 0)))
1007 ;; Search for the last incomplete line in this chunk
1008 (while (string-match "\n" gud-marker-acc start)
1009 (setq start (match-end 0)))
1011 ;; If the incomplete line APPEARS to begin with another marker, keep it
1012 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1013 ;; unnecessary concat during the next call.
1014 (setq gud-marker-acc
1015 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
1016 (substring gud-marker-acc (match-beginning 0))
1020 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
1021 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
1023 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
1024 ;; We haven't tested gud on this system:
1025 (string-match "^mips-[^-]*-riscos" system-configuration)
1026 ;; It's documented on OSF/1.3
1027 (string-match "^mips-[^-]*-osf1" system-configuration)
1028 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
1029 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
1031 (defvar gud-dbx-command-name
1032 (concat "dbx" (if gud-mips-p " -emacs")))
1034 ;; This is just like the gdb one except for the regexps since we need to cope
1035 ;; with an optional breakpoint number in [] before the ^Z^Z
1036 (defun gud-mipsdbx-marker-filter (string)
1037 (setq gud-marker-acc (concat gud-marker-acc string))
1040 ;; Process all the complete markers in this chunk.
1041 (while (string-match
1042 ;; This is like th gdb marker but with an optional
1043 ;; leading break point number like `[1] '
1044 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
1048 ;; Extract the frame position from the marker.
1050 (cons (match-string 1 gud-marker-acc)
1051 (string-to-number (match-string 2 gud-marker-acc)))
1053 ;; Append any text before the marker to the output we're going
1054 ;; to return - we don't include the marker in this text.
1055 output (concat output
1056 (substring gud-marker-acc 0 (match-beginning 0)))
1058 ;; Set the accumulator to the remaining text.
1059 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1061 ;; Does the remaining text look like it might end with the
1062 ;; beginning of another marker? If it does, then keep it in
1063 ;; gud-marker-acc until we receive the rest of it. Since we
1064 ;; know the full marker regexp above failed, it's pretty simple to
1065 ;; test for marker starts.
1066 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
1068 ;; Everything before the potential marker start can be output.
1069 (setq output (concat output (substring gud-marker-acc
1070 0 (match-beginning 0))))
1072 ;; Everything after, we save, to combine with later input.
1073 (setq gud-marker-acc
1074 (substring gud-marker-acc (match-beginning 0))))
1076 (setq output (concat output gud-marker-acc)
1081 ;; The dbx in IRIX is a pain. It doesn't print the file name when
1082 ;; stopping at a breakpoint (but you do get it from the `up' and
1083 ;; `down' commands...). The only way to extract the information seems
1084 ;; to be with a `file' command, although the current line number is
1085 ;; available in $curline. Thus we have to look for output which
1086 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
1087 ;; to output the information we want with a combination of the
1088 ;; `printf' and `file' commands as a pseudo marker which we can
1089 ;; recognise next time through the marker-filter. This would be like
1090 ;; the gdb marker but you can't get the file name without a newline...
1091 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
1092 ;; number rather than a line number etc. Maybe this could be made to
1093 ;; work by listing all the breakpoints and picking the one(s) with the
1094 ;; correct line number, but life's too short.
1095 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
1098 (and (string-match "^mips-[^-]*-irix" system-configuration)
1099 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
1100 "Non-nil to assume the interface appropriate for IRIX dbx.
1101 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
1102 a better solution in 6.1 upwards.")
1103 (defvar gud-dbx-use-stopformat-p
1104 (string-match "irix[6-9]\\.[1-9]" system-configuration)
1105 "Non-nil to use the dbx feature present at least from Irix 6.1
1106 whereby $stopformat=1 produces an output format compatiable with
1107 `gud-dbx-marker-filter'.")
1108 ;; [Irix dbx seems to be a moving target. The dbx output changed
1109 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
1110 ;; the output from `up' is no longer spotted by gud (and it's probably
1111 ;; not distinctive enough to try to match it -- use C-<, C->
1112 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
1113 ;; `long long'(why?!), so the printf stuff needed changing. The line
1114 ;; number was cast to `long' as a compromise between the new `long
1115 ;; long' and the original `int'. This is reported not to work in 6.2,
1116 ;; so it's changed back to int -- don't make your sources too long.
1117 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
1118 ;; whereby `set $stopformat=1' reportedly produces output compatible
1119 ;; with `gud-dbx-marker-filter', which we prefer.
1121 ;; The process filter is also somewhat
1122 ;; unreliable, sometimes not spotting the markers; I don't know
1123 ;; whether there's anything that can be done about that. It would be
1124 ;; much better if SGI could be persuaded to (re?)instate the MIPS
1125 ;; -emacs flag for gdb-like output (which ought to be possible as most
1126 ;; of the communication I've had over it has been from sgi.com).]
1128 ;; this filter is influenced by the xdb one rather than the gdb one
1129 (defun gud-irixdbx-marker-filter (string)
1130 (let (result (case-fold-search nil))
1131 (if (or (string-match comint-prompt-regexp string)
1132 (string-match ".*\012" string))
1133 (setq result (concat gud-marker-acc string)
1135 (setq gud-marker-acc (concat gud-marker-acc string)))
1138 ;; look for breakpoint or signal indication e.g.:
1139 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1140 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1141 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1143 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1145 ;; prod dbx into printing out the line number and file
1146 ;; name in a form we can grok as below
1147 (process-send-string (get-buffer-process gud-comint-buffer)
1148 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1149 ;; look for result of, say, "up" e.g.:
1150 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1151 ;; (this will also catch one of the lines printed by "where")
1153 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1155 (let ((file (match-string 1 result)))
1156 (if (file-exists-p file)
1157 (setq gud-last-frame
1158 (cons (match-string 1 result)
1159 (string-to-number (match-string 2 result))))))
1161 ((string-match ; kluged-up marker as above
1162 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1163 (let ((file (gud-file-name (match-string 2 result))))
1164 (if (and file (file-exists-p file))
1165 (setq gud-last-frame
1167 (string-to-number (match-string 1 result))))))
1168 (setq result (substring result 0 (match-beginning 0))))))
1171 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
1172 "Non-nil means to assume the interface approriate for DG/UX dbx.
1173 This was tested using R4.11.")
1175 ;; There are a couple of differences between DG's dbx output and normal
1176 ;; dbx output which make it nontrivial to integrate this into the
1177 ;; standard dbx-marker-filter (mainly, there are a different number of
1178 ;; backreferences). The markers look like:
1180 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1182 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1185 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1189 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1191 ;; from up/down/where.
1193 (defun gud-dguxdbx-marker-filter (string)
1194 (setq gud-marker-acc (if gud-marker-acc
1195 (concat gud-marker-acc string)
1197 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1198 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1200 ;; Process all complete markers in this chunk.
1201 (while (string-match re gud-marker-acc start)
1202 (setq gud-last-frame
1203 (cons (match-string 4 gud-marker-acc)
1204 (string-to-number (match-string 3 gud-marker-acc)))
1205 start (match-end 0)))
1207 ;; Search for the last incomplete line in this chunk
1208 (while (string-match "\n" gud-marker-acc start)
1209 (setq start (match-end 0)))
1211 ;; If the incomplete line APPEARS to begin with another marker, keep it
1212 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1213 ;; unnecessary concat during the next call.
1214 (setq gud-marker-acc
1215 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1216 (substring gud-marker-acc (match-beginning 0))
1221 (defun dbx (command-line)
1222 "Run dbx on program FILE in buffer *gud-FILE*.
1223 The directory containing FILE becomes the initial working directory
1224 and source-file directory for your debugger."
1225 (interactive (list (gud-query-cmdline 'dbx)))
1229 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1231 (gud-common-init command-line 'gud-dbx-massage-args
1232 'gud-irixdbx-marker-filter))
1234 (gud-common-init command-line 'gud-dbx-massage-args
1235 'gud-dguxdbx-marker-filter))
1237 (gud-common-init command-line 'gud-dbx-massage-args
1238 'gud-dbx-marker-filter)))
1240 (set (make-local-variable 'gud-minor-mode) 'dbx)
1244 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1245 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1246 (gud-def gud-break "stop at \"%f\":%l"
1247 "\C-b" "Set breakpoint at current line.")
1248 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1250 (gud-def gud-break "stop at \"%d%f\":%l"
1251 "\C-b" "Set breakpoint at current line.")
1252 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1253 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1254 "<" "Up (numeric arg) stack frames.")
1255 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1256 ">" "Down (numeric arg) stack frames.")
1257 ;; Make dbx give out the source location info that we need.
1258 (process-send-string (get-buffer-process gud-comint-buffer)
1259 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1261 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1262 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1263 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1264 "\C-b" "Set breakpoint at current line.")
1265 (if gud-dbx-use-stopformat-p
1266 (process-send-string (get-buffer-process gud-comint-buffer)
1267 "set $stopformat=1\n"))))
1269 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1270 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1271 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1272 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1273 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1274 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1275 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1276 (gud-def gud-run "run" nil "Run the program.")
1278 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1279 (setq paragraph-start comint-prompt-regexp)
1280 (run-hooks 'dbx-mode-hook)
1283 ;; ======================================================================
1284 ;; xdb (HP PARISC debugger) functions
1286 ;; History of argument lists passed to xdb.
1287 (defvar gud-xdb-history nil)
1289 (defcustom gud-xdb-directories nil
1290 "*A list of directories that xdb should search for source code.
1291 If nil, only source files in the program directory
1292 will be known to xdb.
1294 The file names should be absolute, or relative to the directory
1295 containing the executable being debugged."
1296 :type '(choice (const :tag "Current Directory" nil)
1301 (defun gud-xdb-massage-args (file args)
1302 (nconc (let ((directories gud-xdb-directories)
1305 (setq result (cons (car directories) (cons "-d" result)))
1306 (setq directories (cdr directories)))
1310 ;; xdb does not print the lines all at once, so we have to accumulate them
1311 (defun gud-xdb-marker-filter (string)
1313 (if (or (string-match comint-prompt-regexp string)
1314 (string-match ".*\012" string))
1315 (setq result (concat gud-marker-acc string)
1317 (setq gud-marker-acc (concat gud-marker-acc string)))
1319 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1321 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1323 (let ((line (string-to-number (match-string 2 result)))
1324 (file (gud-file-name (match-string 1 result))))
1326 (setq gud-last-frame (cons file line))))))
1330 (defun xdb (command-line)
1331 "Run xdb on program FILE in buffer *gud-FILE*.
1332 The directory containing FILE becomes the initial working directory
1333 and source-file directory for your debugger.
1335 You can set the variable `gud-xdb-directories' to a list of program source
1336 directories if your program contains sources from more than one directory."
1337 (interactive (list (gud-query-cmdline 'xdb)))
1339 (gud-common-init command-line 'gud-xdb-massage-args
1340 'gud-xdb-marker-filter)
1341 (set (make-local-variable 'gud-minor-mode) 'xdb)
1343 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1344 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1345 "Set temporary breakpoint at current line.")
1346 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1347 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1348 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1349 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1350 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1351 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1352 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1353 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1355 (setq comint-prompt-regexp "^>")
1356 (setq paragraph-start comint-prompt-regexp)
1357 (run-hooks 'xdb-mode-hook))
1359 ;; ======================================================================
1362 ;; History of argument lists passed to perldb.
1363 (defvar gud-perldb-history nil)
1365 (defun gud-perldb-massage-args (file args)
1366 "Convert a command line as would be typed normally to run perldb
1367 into one that invokes an Emacs-enabled debugging session.
1368 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1369 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1371 (let* ((new-args nil)
1373 (shift (lambda () (push (pop args) new-args))))
1375 ;; Pass all switches and -e scripts through.
1377 (string-match "^-" (car args))
1378 (not (equal "-" (car args)))
1379 (not (equal "--" (car args))))
1380 (when (equal "-e" (car args))
1381 ;; -e goes with the next arg, so shift one extra.
1383 ;; -e as the last arg is an error in Perl.
1384 (error "No code specified for -e"))
1390 (string-match "^-" (car args)))
1391 (error "Can't use stdin as the script to debug"))
1392 ;; This is the program name.
1395 ;; If -e specified, make sure there is a -- so -emacs is not taken
1397 (if (and args (equal "--" (car args)))
1399 (and seen-e (push "--" new-args)))
1401 (push "-emacs" new-args)
1405 (nreverse new-args)))
1407 ;; There's no guarantee that Emacs will hand the filter the entire
1408 ;; marker at once; it could be broken up across several strings. We
1409 ;; might even receive a big chunk with several markers in it. If we
1410 ;; receive a chunk of text which looks like it might contain the
1411 ;; beginning of a marker, we save it here between calls to the
1413 (defun gud-perldb-marker-filter (string)
1414 (setq gud-marker-acc (concat gud-marker-acc string))
1417 ;; Process all the complete markers in this chunk.
1418 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1422 ;; Extract the frame position from the marker.
1424 (cons (match-string 1 gud-marker-acc)
1425 (string-to-number (match-string 3 gud-marker-acc)))
1427 ;; Append any text before the marker to the output we're going
1428 ;; to return - we don't include the marker in this text.
1429 output (concat output
1430 (substring gud-marker-acc 0 (match-beginning 0)))
1432 ;; Set the accumulator to the remaining text.
1433 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1435 ;; Does the remaining text look like it might end with the
1436 ;; beginning of another marker? If it does, then keep it in
1437 ;; gud-marker-acc until we receive the rest of it. Since we
1438 ;; know the full marker regexp above failed, it's pretty simple to
1439 ;; test for marker starts.
1440 (if (string-match "\032.*\\'" gud-marker-acc)
1442 ;; Everything before the potential marker start can be output.
1443 (setq output (concat output (substring gud-marker-acc
1444 0 (match-beginning 0))))
1446 ;; Everything after, we save, to combine with later input.
1447 (setq gud-marker-acc
1448 (substring gud-marker-acc (match-beginning 0))))
1450 (setq output (concat output gud-marker-acc)
1455 (defcustom gud-perldb-command-name "perl -d"
1456 "Default command to execute a Perl script under debugger."
1461 (defun perldb (command-line)
1462 "Run perldb on program FILE in buffer *gud-FILE*.
1463 The directory containing FILE becomes the initial working directory
1464 and source-file directory for your debugger."
1466 (list (gud-query-cmdline 'perldb
1467 (concat (or (buffer-file-name) "-e 0") " "))))
1469 (gud-common-init command-line 'gud-perldb-massage-args
1470 'gud-perldb-marker-filter)
1471 (set (make-local-variable 'gud-minor-mode) 'perldb)
1473 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1474 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1475 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1476 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1477 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1478 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1479 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1480 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1481 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1482 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1485 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1486 (setq paragraph-start comint-prompt-regexp)
1487 (run-hooks 'perldb-mode-hook))
1489 ;; ======================================================================
1490 ;; pdb (Python debugger) functions
1492 ;; History of argument lists passed to pdb.
1493 (defvar gud-pdb-history nil)
1495 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1496 ;; Either file or function name may be omitted: "> <string>(0)?()"
1497 (defvar gud-pdb-marker-regexp
1498 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1499 (defvar gud-pdb-marker-regexp-file-group 1)
1500 (defvar gud-pdb-marker-regexp-line-group 2)
1501 (defvar gud-pdb-marker-regexp-fnname-group 3)
1503 (defvar gud-pdb-marker-regexp-start "^> ")
1505 ;; There's no guarantee that Emacs will hand the filter the entire
1506 ;; marker at once; it could be broken up across several strings. We
1507 ;; might even receive a big chunk with several markers in it. If we
1508 ;; receive a chunk of text which looks like it might contain the
1509 ;; beginning of a marker, we save it here between calls to the
1511 (defun gud-pdb-marker-filter (string)
1512 (setq gud-marker-acc (concat gud-marker-acc string))
1515 ;; Process all the complete markers in this chunk.
1516 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1519 ;; Extract the frame position from the marker.
1521 (let ((file (match-string gud-pdb-marker-regexp-file-group
1523 (line (string-to-number
1524 (match-string gud-pdb-marker-regexp-line-group
1526 (if (string-equal file "<string>")
1530 ;; Output everything instead of the below
1531 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1532 ;; ;; Append any text before the marker to the output we're going
1533 ;; ;; to return - we don't include the marker in this text.
1534 ;; output (concat output
1535 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1537 ;; Set the accumulator to the remaining text.
1538 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1540 ;; Does the remaining text look like it might end with the
1541 ;; beginning of another marker? If it does, then keep it in
1542 ;; gud-marker-acc until we receive the rest of it. Since we
1543 ;; know the full marker regexp above failed, it's pretty simple to
1544 ;; test for marker starts.
1545 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1547 ;; Everything before the potential marker start can be output.
1548 (setq output (concat output (substring gud-marker-acc
1549 0 (match-beginning 0))))
1551 ;; Everything after, we save, to combine with later input.
1552 (setq gud-marker-acc
1553 (substring gud-marker-acc (match-beginning 0))))
1555 (setq output (concat output gud-marker-acc)
1560 (defcustom gud-pdb-command-name "pdb"
1561 "File name for executing the Python debugger.
1562 This should be an executable on your path, or an absolute file name."
1567 (defun pdb (command-line)
1568 "Run pdb on program FILE in buffer `*gud-FILE*'.
1569 The directory containing FILE becomes the initial working directory
1570 and source-file directory for your debugger."
1572 (list (gud-query-cmdline 'pdb)))
1574 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1575 (set (make-local-variable 'gud-minor-mode) 'pdb)
1577 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1578 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1579 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1580 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1581 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1582 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1583 (gud-def gud-up "up" "<" "Up one stack frame.")
1584 (gud-def gud-down "down" ">" "Down one stack frame.")
1585 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1587 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1589 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1590 (setq comint-prompt-regexp "^(Pdb) *")
1591 (setq paragraph-start comint-prompt-regexp)
1592 (run-hooks 'pdb-mode-hook))
1594 ;; ======================================================================
1598 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1599 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1601 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1602 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1603 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1605 ;; INVOCATION NOTES:
1607 ;; You invoke jdb-mode with:
1611 ;; It responds with:
1613 ;; Run jdb (like this): jdb
1615 ;; type any jdb switches followed by the name of the class you'd like to debug.
1616 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1617 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1618 ;; See the known problems section below for restrictions when specifying jdb
1619 ;; command line switches (search forward for '-classpath').
1621 ;; You should see something like the following:
1623 ;; Current directory is ~/src/java/hello/
1624 ;; Initializing jdb...
1625 ;; 0xed2f6628:class(hello)
1628 ;; To set an initial breakpoint try:
1630 ;; > stop in hello.main
1631 ;; Breakpoint set in hello.main
1634 ;; To execute the program type:
1639 ;; Breakpoint hit: running ...
1640 ;; hello.main (hello:12)
1642 ;; Type M-n to step over the current line and M-s to step into it. That,
1643 ;; along with the JDB 'help' command should get you started. The 'quit'
1644 ;; JDB command will get out out of the debugger. There is some truly
1645 ;; pathetic JDB documentation available at:
1647 ;; http://java.sun.com/products/jdk/1.1/debugging/
1649 ;; KNOWN PROBLEMS AND FIXME's:
1651 ;; Not sure what happens with inner classes ... haven't tried them.
1653 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1655 ;; You must not put whitespace between "-classpath" and the path to
1656 ;; search for java classes even though it is required when invoking jdb
1657 ;; from the command line. See gud-jdb-massage-args for details.
1658 ;; The same applies for "-sourcepath".
1660 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1661 ;; refer to the documentation of `gud-jdb-use-classpath' and
1662 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1663 ;; on using the classpath for locating java source files.
1665 ;; If any of the source files in the directories listed in
1666 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1667 ;; every file ending in ".java" in these directories parses without error.
1669 ;; All the .java files in the directories in gud-jdb-directories are
1670 ;; syntactically analyzed each time gud jdb is invoked. It would be
1671 ;; nice to keep as much information as possible between runs. It would
1672 ;; be really nice to analyze the files only as neccessary (when the
1673 ;; source needs to be displayed.) I'm not sure to what extent the former
1674 ;; can be accomplished and I'm not sure the latter can be done at all
1675 ;; since I don't know of any general way to tell which .class files are
1676 ;; defined by which .java file without analyzing all the .java files.
1677 ;; If anyone knows why JavaSoft didn't put the source file names in
1678 ;; debuggable .class files please clue me in so I find something else
1679 ;; to be spiteful and bitter about.
1681 ;; ======================================================================
1682 ;; gud jdb variables and functions
1684 (defcustom gud-jdb-command-name "jdb"
1685 "Command that executes the Java debugger."
1689 (defcustom gud-jdb-use-classpath t
1690 "If non-nil, search for Java source files in classpath directories.
1691 The list of directories to search is the value of `gud-jdb-classpath'.
1692 The file pathname is obtained by converting the fully qualified
1693 class information output by jdb to a relative pathname and appending
1694 it to `gud-jdb-classpath' element by element until a match is found.
1696 This method has a significant jdb startup time reduction advantage
1697 since it does not require the scanning of all `gud-jdb-directories'
1698 and parsing all Java files for class information.
1700 Set to nil to use `gud-jdb-directories' to scan java sources for
1701 class information on jdb startup (original method)."
1705 (defvar gud-jdb-classpath nil
1706 "Java/jdb classpath directories list.
1707 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1708 list automatically using the following methods in sequence
1709 \(with subsequent successful steps overriding the results of previous
1712 1) Read the CLASSPATH environment variable,
1713 2) Read any \"-classpath\" argument used to run jdb,
1714 or detected in jdb output (e.g. if jdb is run by a script
1715 that echoes the actual jdb command before starting jdb)
1716 3) Send a \"classpath\" command to jdb and scan jdb output for
1717 classpath information if jdb is invoked with an \"-attach\" (to
1718 an already running VM) argument (This case typically does not
1719 have a \"-classpath\" command line argument - that is provided
1720 to the VM when it is started).
1722 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1723 those debuggers do not support the classpath command. Use 1) or 2).")
1725 (defvar gud-jdb-sourcepath nil
1726 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1727 This list is prepended to `gud-jdb-classpath' to form the complete
1728 list of directories searched for source files.")
1730 (defvar gud-marker-acc-max-length 4000
1731 "Maximum number of debugger output characters to keep.
1732 This variable limits the size of `gud-marker-acc' which holds
1733 the most recent debugger output history while searching for
1734 source file information.")
1736 (defvar gud-jdb-history nil
1737 "History of argument lists passed to jdb.")
1740 ;; List of Java source file directories.
1741 (defvar gud-jdb-directories (list ".")
1742 "*A list of directories that gud jdb should search for source code.
1743 The file names should be absolute, or relative to the current
1746 The set of .java files residing in the directories listed are
1747 syntactically analyzed to determine the classes they define and the
1748 packages in which these classes belong. In this way gud jdb maps the
1749 package-qualified class names output by the jdb debugger to the source
1750 file from which the class originated. This allows gud mode to keep
1751 the source code display in sync with the debugging session.")
1753 (defvar gud-jdb-source-files nil
1754 "List of the java source files for this debugging session.")
1756 ;; Association list of fully qualified class names (package + class name)
1757 ;; and their source files.
1758 (defvar gud-jdb-class-source-alist nil
1759 "Association list of fully qualified class names and source files.")
1761 ;; This is used to hold a source file during analysis.
1762 (defvar gud-jdb-analysis-buffer nil)
1764 (defvar gud-jdb-classpath-string nil
1765 "Holds temporary classpath values.")
1767 (defun gud-jdb-build-source-files-list (path extn)
1768 "Return a list of java source files (absolute paths).
1769 PATH gives the directories in which to search for files with
1770 extension EXTN. Normally EXTN is given as the regular expression
1772 (apply 'nconc (mapcar (lambda (d)
1773 (when (file-directory-p d)
1774 (directory-files d t extn nil)))
1777 ;; Move point past whitespace.
1778 (defun gud-jdb-skip-whitespace ()
1779 (skip-chars-forward " \n\r\t\014"))
1781 ;; Move point past a "// <eol>" type of comment.
1782 (defun gud-jdb-skip-single-line-comment ()
1785 ;; Move point past a "/* */" or "/** */" type of comment.
1786 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1790 (if (eq (following-char) ?*)
1794 (if (eq (following-char) ?/)
1797 (throw 'break nil)))))
1800 ;; Move point past any number of consecutive whitespace chars and/or comments.
1801 (defun gud-jdb-skip-whitespace-and-comments ()
1802 (gud-jdb-skip-whitespace)
1807 (gud-jdb-skip-single-line-comment)
1808 (gud-jdb-skip-whitespace))
1809 ((looking-at "/\\*")
1810 (gud-jdb-skip-traditional-or-documentation-comment)
1811 (gud-jdb-skip-whitespace))
1812 (t (throw 'done nil))))))
1814 ;; Move point past things that are id-like. The intent is to skip regular
1815 ;; id's, such as class or interface names as well as package and interface
1817 (defun gud-jdb-skip-id-ish-thing ()
1818 (skip-chars-forward "^ /\n\r\t\014,;{"))
1820 ;; Move point past a string literal.
1821 (defun gud-jdb-skip-string-literal ()
1824 ((eq (following-char) ?\\)
1826 ((eq (following-char) ?\042))))
1830 ;; Move point past a character literal.
1831 (defun gud-jdb-skip-character-literal ()
1835 (if (eq (following-char) ?\\)
1837 (not (eq (following-char) ?\')))
1841 ;; Move point past the following block. There may be (legal) cruft before
1842 ;; the block's opening brace. There must be a block or it's the end of life
1843 ;; in petticoat junction.
1844 (defun gud-jdb-skip-block ()
1846 ;; Find the begining of the block.
1848 (not (eq (following-char) ?{))
1850 ;; Skip any constructs that can harbor literal block delimiter
1851 ;; characters and/or the delimiters for the constructs themselves.
1854 (gud-jdb-skip-single-line-comment))
1855 ((looking-at "/\\*")
1856 (gud-jdb-skip-traditional-or-documentation-comment))
1857 ((eq (following-char) ?\042)
1858 (gud-jdb-skip-string-literal))
1859 ((eq (following-char) ?\')
1860 (gud-jdb-skip-character-literal))
1861 (t (forward-char))))
1863 ;; Now at the begining of the block.
1866 ;; Skip over the body of the block as well as the final brace.
1867 (let ((open-level 1))
1868 (while (not (eq open-level 0))
1871 (gud-jdb-skip-single-line-comment))
1872 ((looking-at "/\\*")
1873 (gud-jdb-skip-traditional-or-documentation-comment))
1874 ((eq (following-char) ?\042)
1875 (gud-jdb-skip-string-literal))
1876 ((eq (following-char) ?\')
1877 (gud-jdb-skip-character-literal))
1878 ((eq (following-char) ?{)
1879 (setq open-level (+ open-level 1))
1881 ((eq (following-char) ?})
1882 (setq open-level (- open-level 1))
1884 (t (forward-char))))))
1886 ;; Find the package and class definitions in Java source file FILE. Assumes
1887 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1888 ;; to hold the source during analysis.
1889 (defun gud-jdb-analyze-source (buf file)
1892 (insert-file-contents file nil nil nil t)
1897 (gud-jdb-skip-whitespace)
1901 ;; Any number of semi's following a block is legal. Move point
1902 ;; past them. Note that comments and whitespace may be
1903 ;; interspersed as well.
1904 ((eq (following-char) ?\073)
1907 ;; Move point past a single line comment.
1909 (gud-jdb-skip-single-line-comment))
1911 ;; Move point past a traditional or documentation comment.
1912 ((looking-at "/\\*")
1913 (gud-jdb-skip-traditional-or-documentation-comment))
1915 ;; Move point past a package statement, but save the PackageName.
1916 ((looking-at "package")
1918 (gud-jdb-skip-whitespace-and-comments)
1920 (gud-jdb-skip-id-ish-thing)
1921 (setq p (concat (buffer-substring s (point)) "."))
1922 (gud-jdb-skip-whitespace-and-comments)
1923 (if (eq (following-char) ?\073)
1926 ;; Move point past an import statement.
1927 ((looking-at "import")
1929 (gud-jdb-skip-whitespace-and-comments)
1930 (gud-jdb-skip-id-ish-thing)
1931 (gud-jdb-skip-whitespace-and-comments)
1932 (if (eq (following-char) ?\073)
1935 ;; Move point past the various kinds of ClassModifiers.
1936 ((looking-at "public")
1938 ((looking-at "abstract")
1940 ((looking-at "final")
1943 ;; Move point past a ClassDeclaraction, but save the class
1945 ((looking-at "class")
1947 (gud-jdb-skip-whitespace-and-comments)
1949 (gud-jdb-skip-id-ish-thing)
1951 l (nconc l (list (concat p (buffer-substring s (point)))))))
1952 (gud-jdb-skip-block))
1954 ;; Move point past an interface statement.
1955 ((looking-at "interface")
1957 (gud-jdb-skip-block))
1959 ;; Anything else means the input is invalid.
1961 (message "Error parsing file %s." file)
1962 (throw 'abort nil))))))
1965 (defun gud-jdb-build-class-source-alist-for-file (file)
1969 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1971 ;; Return an alist of fully qualified classes and the source files
1972 ;; holding their definitions. SOURCES holds a list of all the source
1973 ;; files to examine.
1974 (defun gud-jdb-build-class-source-alist (sources)
1975 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1980 'gud-jdb-build-class-source-alist-for-file
1982 (kill-buffer gud-jdb-analysis-buffer)
1983 (setq gud-jdb-analysis-buffer nil)))
1985 ;; Change what was given in the minibuffer to something that can be used to
1986 ;; invoke the debugger.
1987 (defun gud-jdb-massage-args (file args)
1988 ;; The jdb executable must have whitespace between "-classpath" and
1989 ;; its value while gud-common-init expects all switch values to
1990 ;; follow the switch keyword without intervening whitespace. We
1991 ;; require that when the user enters the "-classpath" switch in the
1992 ;; EMACS minibuffer that they do so without the intervening
1993 ;; whitespace. This function adds it back (it's called after
1994 ;; gud-common-init). There are more switches like this (for
1995 ;; instance "-host" and "-password") but I don't care about them
1998 (let (massaged-args user-error)
2000 (while (and args (not user-error))
2002 ((setq user-error (string-match "-classpath$" (car args))))
2003 ((setq user-error (string-match "-sourcepath$" (car args))))
2004 ((string-match "-classpath\\(.+\\)" (car args))
2006 (append massaged-args
2008 (setq gud-jdb-classpath-string
2009 (match-string 1 (car args)))))))
2010 ((string-match "-sourcepath\\(.+\\)" (car args))
2012 (append massaged-args
2014 (setq gud-jdb-sourcepath
2015 (match-string 1 (car args)))))))
2016 (t (setq massaged-args (append massaged-args (list (car args))))))
2017 (setq args (cdr args)))
2019 ;; By this point the current directory is all screwed up. Maybe we
2020 ;; could fix things and re-invoke gud-common-init, but for now I think
2021 ;; issueing the error is good enough.
2024 (kill-buffer (current-buffer))
2025 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
2028 ;; Search for an association with P, a fully qualified class name, in
2029 ;; gud-jdb-class-source-alist. The asssociation gives the fully
2030 ;; qualified file name of the source file which produced the class.
2031 (defun gud-jdb-find-source-file (p)
2032 (cdr (assoc p gud-jdb-class-source-alist)))
2034 ;; Note: Reset to this value every time a prompt is seen
2035 (defvar gud-jdb-lowest-stack-level 999)
2037 (defun gud-jdb-find-source-using-classpath (p)
2038 "Find source file corresponding to fully qualified class p.
2039 Convert p from jdb's output, converted to a pathname
2040 relative to a classpath directory."
2043 (;; Replace dots with slashes and append ".java" to generate file
2044 ;; name relative to classpath
2047 (mapconcat 'identity
2049 ;; Eliminate any subclass references in the class
2050 ;; name string. These start with a "$"
2052 (if (string-match "$.*" x)
2053 (replace-match "" t t x) p))
2057 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2060 (not (setq found-file
2062 (concat (car cplist) "/" filename)))))
2063 (setq cplist (cdr cplist)))
2064 (if found-file (concat (car cplist) "/" filename)))))
2066 (defun gud-jdb-find-source (string)
2067 "Alias for function used to locate source files.
2068 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
2069 during jdb initialization depending on the value of
2070 `gud-jdb-use-classpath'."
2073 (defun gud-jdb-parse-classpath-string (string)
2074 "Parse the classpath list and convert each item to an absolute pathname."
2075 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
2076 (replace-match "" nil nil s) s))
2077 (mapcar 'file-truename
2080 (concat "[ \t\n\r,\"" path-separator "]+")))))
2082 ;; See comentary for other debugger's marker filters - there you will find
2083 ;; important notes about STRING.
2084 (defun gud-jdb-marker-filter (string)
2086 ;; Build up the accumulator.
2087 (setq gud-marker-acc
2089 (concat gud-marker-acc string)
2092 ;; Look for classpath information until gud-jdb-classpath-string is found
2093 ;; (interactive, multiple settings of classpath from jdb
2094 ;; not supported/followed)
2095 (if (and gud-jdb-use-classpath
2096 (not gud-jdb-classpath-string)
2097 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
2098 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
2099 (setq gud-jdb-classpath
2100 (gud-jdb-parse-classpath-string
2101 (setq gud-jdb-classpath-string
2102 (match-string 1 gud-marker-acc)))))
2104 ;; We process STRING from left to right. Each time through the
2105 ;; following loop we process at most one marker. After we've found a
2106 ;; marker, delete gud-marker-acc up to and including the match
2108 ;; Process each complete marker in the input.
2111 ;; Do we see a marker?
2113 ;; jdb puts out a string of the following form when it
2114 ;; hits a breakpoint:
2116 ;; <fully-qualified-class><method> (<class>:<line-number>)
2118 ;; <fully-qualified-class>'s are composed of Java ID's
2119 ;; separated by periods. <method> and <class> are
2120 ;; also Java ID's. <method> begins with a period and
2121 ;; may contain less-than and greater-than (constructors,
2122 ;; for instance, are called <init> in the symbol table.)
2123 ;; Java ID's begin with a letter followed by letters
2124 ;; and/or digits. The set of letters includes underscore
2127 ;; The first group matches <fully-qualified-class>,
2128 ;; the second group matches <class> and the third group
2129 ;; matches <line-number>. We don't care about using
2130 ;; <method> so we don't "group" it.
2132 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2135 ;; The ".," in the last square-bracket are necessary because
2136 ;; of Sun's total disrespect for backwards compatibility in
2137 ;; reported line numbers from jdb - starting in 1.4.0 they
2138 ;; print line numbers using LOCALE, inserting a comma or a
2139 ;; period at the thousands positions (how ingenious!).
2141 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2142 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2145 ;; A good marker is one that:
2146 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2147 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2148 ;; since the last prompt
2149 ;; Figure out the line on which to position the debugging arrow.
2150 ;; Return the info as a cons of the form:
2152 ;; (<file-name> . <line-number>) .
2153 (if (if (match-beginning 1)
2155 (setq n (string-to-number (substring
2157 (1+ (match-beginning 1))
2158 (- (match-end 1) 2))))
2159 (if (< n gud-jdb-lowest-stack-level)
2160 (progn (setq gud-jdb-lowest-stack-level n) t)))
2162 (if (setq file-found
2163 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2164 (setq gud-last-frame
2168 ((numstr (match-string 4 gud-marker-acc)))
2169 (if (string-match "[.,]" numstr)
2170 (replace-match "" nil nil numstr)
2172 (message "Could not find source file.")))
2174 ;; Set the accumulator to the remaining text.
2175 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2177 (if (string-match comint-prompt-regexp gud-marker-acc)
2178 (setq gud-jdb-lowest-stack-level 999)))
2180 ;; Do not allow gud-marker-acc to grow without bound. If the source
2181 ;; file information is not within the last 3/4
2182 ;; gud-marker-acc-max-length characters, well,...
2183 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2184 (setq gud-marker-acc
2185 (substring gud-marker-acc
2186 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2188 ;; We don't filter any debugger output so just return what we were given.
2191 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2194 (defun jdb (command-line)
2195 "Run jdb with command line COMMAND-LINE in a buffer.
2196 The buffer is named \"*gud*\" if no initial class is given or
2197 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2198 switch is given, omit all whitespace between it and its value.
2200 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2201 information on how jdb accesses source files. Alternatively (if
2202 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2203 original source file access method.
2205 For general information about commands available to control jdb from
2206 gud, see `gud-mode'."
2208 (list (gud-query-cmdline 'jdb)))
2209 (setq gud-jdb-classpath nil)
2210 (setq gud-jdb-sourcepath nil)
2212 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2213 ;; if CLASSPATH is set.
2214 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
2215 (if gud-jdb-classpath-string
2216 (setq gud-jdb-classpath
2217 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2218 (setq gud-jdb-classpath-string nil) ; prepare for next
2220 (gud-common-init command-line 'gud-jdb-massage-args
2221 'gud-jdb-marker-filter)
2222 (set (make-local-variable 'gud-minor-mode) 'jdb)
2224 ;; If a -classpath option was provided, set gud-jdb-classpath
2225 (if gud-jdb-classpath-string
2226 (setq gud-jdb-classpath
2227 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2228 (setq gud-jdb-classpath-string nil) ; prepare for next
2229 ;; If a -sourcepath option was provided, parse it
2230 (if gud-jdb-sourcepath
2231 (setq gud-jdb-sourcepath
2232 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2234 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2235 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2236 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2237 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2238 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2239 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2240 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2241 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2242 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2244 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2245 (setq paragraph-start comint-prompt-regexp)
2246 (run-hooks 'jdb-mode-hook)
2248 (if gud-jdb-use-classpath
2249 ;; Get the classpath information from the debugger
2251 (if (string-match "-attach" command-line)
2252 (gud-call "classpath"))
2253 (fset 'gud-jdb-find-source
2254 'gud-jdb-find-source-using-classpath))
2256 ;; Else create and bind the class/source association list as well
2257 ;; as the source file list.
2258 (setq gud-jdb-class-source-alist
2259 (gud-jdb-build-class-source-alist
2260 (setq gud-jdb-source-files
2261 (gud-jdb-build-source-files-list gud-jdb-directories
2263 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2266 ;; ======================================================================
2268 ;; BASHDB support. See http://bashdb.sourceforge.net
2270 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2272 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2274 ;; INVOCATION NOTES:
2276 ;; You invoke bashdb-mode with:
2278 ;; M-x bashdb <enter>
2280 ;; It responds with:
2282 ;; Run bashdb (like this): bash
2285 ;; History of argument lists passed to bashdb.
2286 (defvar gud-bashdb-history nil)
2288 ;; Convert a command line as would be typed normally to run a script
2289 ;; into one that invokes an Emacs-enabled debugging session.
2290 ;; "--debugger" in inserted as the first switch.
2292 ;; There's no guarantee that Emacs will hand the filter the entire
2293 ;; marker at once; it could be broken up across several strings. We
2294 ;; might even receive a big chunk with several markers in it. If we
2295 ;; receive a chunk of text which looks like it might contain the
2296 ;; beginning of a marker, we save it here between calls to the
2298 (defun gud-bashdb-marker-filter (string)
2299 (setq gud-marker-acc (concat gud-marker-acc string))
2302 ;; Process all the complete markers in this chunk.
2303 ;; Format of line looks like this:
2304 ;; (/etc/init.d/ntp.init:16):
2305 ;; but we also allow DOS drive letters
2306 ;; (d:/etc/init.d/ntp.init:16):
2307 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2311 ;; Extract the frame position from the marker.
2313 (cons (match-string 2 gud-marker-acc)
2314 (string-to-number (match-string 4 gud-marker-acc)))
2316 ;; Append any text before the marker to the output we're going
2317 ;; to return - we don't include the marker in this text.
2318 output (concat output
2319 (substring gud-marker-acc 0 (match-beginning 0)))
2321 ;; Set the accumulator to the remaining text.
2322 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2324 ;; Does the remaining text look like it might end with the
2325 ;; beginning of another marker? If it does, then keep it in
2326 ;; gud-marker-acc until we receive the rest of it. Since we
2327 ;; know the full marker regexp above failed, it's pretty simple to
2328 ;; test for marker starts.
2329 (if (string-match "\032.*\\'" gud-marker-acc)
2331 ;; Everything before the potential marker start can be output.
2332 (setq output (concat output (substring gud-marker-acc
2333 0 (match-beginning 0))))
2335 ;; Everything after, we save, to combine with later input.
2336 (setq gud-marker-acc
2337 (substring gud-marker-acc (match-beginning 0))))
2339 (setq output (concat output gud-marker-acc)
2344 (defcustom gud-bashdb-command-name "bash --debugger"
2345 "File name for executing bash debugger."
2350 (defun bashdb (command-line)
2351 "Run bashdb on program FILE in buffer *gud-FILE*.
2352 The directory containing FILE becomes the initial working directory
2353 and source-file directory for your debugger."
2355 (list (read-from-minibuffer "Run bashdb (like this): "
2356 (if (consp gud-bashdb-history)
2357 (car gud-bashdb-history)
2358 (concat gud-bashdb-command-name
2360 gud-minibuffer-local-map nil
2361 '(gud-bashdb-history . 1))))
2363 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2365 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2367 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2368 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2369 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2370 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2371 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2372 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2373 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2374 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2375 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2376 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2379 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2381 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2382 (setq paragraph-start comint-prompt-regexp)
2383 (run-hooks 'bashdb-mode-hook)
2387 ;; End of debugger-specific information
2391 ;; When we send a command to the debugger via gud-call, it's annoying
2392 ;; to see the command and the new prompt inserted into the debugger's
2393 ;; buffer; we have other ways of knowing the command has completed.
2395 ;; If the buffer looks like this:
2396 ;; --------------------
2397 ;; (gdb) set args foo bar
2399 ;; --------------------
2400 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2401 ;; source file to set a breakpoint, we want the buffer to end up like
2403 ;; --------------------
2404 ;; (gdb) set args foo bar
2405 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2407 ;; --------------------
2408 ;; Essentially, the old prompt is deleted, and the command's output
2409 ;; and the new prompt take its place.
2411 ;; Not echoing the command is easy enough; you send it directly using
2412 ;; process-send-string, and it never enters the buffer. However,
2413 ;; getting rid of the old prompt is trickier; you don't want to do it
2414 ;; when you send the command, since that will result in an annoying
2415 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2416 ;; waits for a response from the debugger, and the new prompt is
2417 ;; inserted. Instead, we'll wait until we actually get some output
2418 ;; from the subprocess before we delete the prompt. If the command
2419 ;; produced no output other than a new prompt, that prompt will most
2420 ;; likely be in the first chunk of output received, so we will delete
2421 ;; the prompt and then replace it with an identical one. If the
2422 ;; command produces output, the prompt is moving anyway, so the
2423 ;; flicker won't be annoying.
2425 ;; So - when we want to delete the prompt upon receipt of the next
2426 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2427 ;; the start of the prompt; the process filter will notice this, and
2428 ;; delete all text between it and the process output marker. If
2429 ;; gud-delete-prompt-marker points nowhere, we leave the current
2431 (defvar gud-delete-prompt-marker nil)
2434 (put 'gud-mode 'mode-class 'special)
2436 (define-derived-mode gud-mode comint-mode "Debugger"
2437 "Major mode for interacting with an inferior debugger process.
2439 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2440 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2441 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2442 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2444 After startup, the following commands are available in both the GUD
2445 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2448 \\[gud-break] sets a breakpoint at the current file and line. In the
2449 GUD buffer, the current file and line are those of the last breakpoint or
2450 step. In a source buffer, they are the buffer's file and current line.
2452 \\[gud-remove] removes breakpoints on the current file and line.
2454 \\[gud-refresh] displays in the source window the last line referred to
2457 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2458 step-one-line (not entering function calls), and step-one-instruction
2459 and then update the source window with the current file and position.
2460 \\[gud-cont] continues execution.
2462 \\[gud-print] tries to find the largest C lvalue or function-call expression
2463 around point, and sends it to the debugger for value display.
2465 The above commands are common to all supported debuggers except xdb which
2466 does not support stepping instructions.
2468 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2469 except that the breakpoint is temporary; that is, it is removed when
2470 execution stops on it.
2472 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2473 frame. \\[gud-down] drops back down through one.
2475 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2476 the current function and stops.
2478 All the keystrokes above are accessible in the GUD buffer
2479 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2481 All pre-defined functions for which the concept make sense repeat
2482 themselves the appropriate number of times if you give a prefix
2485 You may use the `gud-def' macro in the initialization hook to define other
2488 Other commands for interacting with the debugger process are inherited from
2489 comint mode, which see."
2490 (setq mode-line-process '(":%s"))
2491 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2492 (set (make-local-variable 'gud-last-frame) nil)
2493 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2494 (make-local-variable 'comint-prompt-regexp)
2495 ;; Don't put repeated commands in command history many times.
2496 (set (make-local-variable 'comint-input-ignoredups) t)
2497 (make-local-variable 'paragraph-start)
2498 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2499 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2501 ;; Cause our buffers to be displayed, by default,
2502 ;; in the selected window.
2503 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2505 (defcustom gud-chdir-before-run t
2506 "Non-nil if GUD should `cd' to the debugged executable."
2510 (defvar gud-target-name "--unknown--"
2511 "The apparent name of the program being debugged in a gud buffer.")
2513 ;; Perform initializations common to all debuggers.
2514 ;; The first arg is the specified command line,
2515 ;; which starts with the program to debug.
2516 ;; The other three args specify the values to use
2517 ;; for local variables in the debugger buffer.
2518 (defun gud-common-init (command-line massage-args marker-filter
2519 &optional find-file)
2520 (let* ((words (split-string command-line))
2521 (program (car words))
2522 (dir default-directory)
2523 ;; Extract the file name from WORDS
2524 ;; and put t in its place.
2525 ;; Later on we will put the modified file name arg back there.
2526 (file-word (let ((w (cdr words)))
2527 (while (and w (= ?- (aref (car w) 0)))
2533 (and file-word (substitute-in-file-name file-word)))
2535 ;; If a directory was specified, expand the file name.
2536 ;; Otherwise, don't expand it, so GDB can use the PATH.
2537 ;; A file name without directory is literally valid
2538 ;; only if the file exists in ., and in that case,
2539 ;; omitting the expansion here has no visible effect.
2540 (file (and file-word
2541 (if (file-name-directory file-subst)
2542 (expand-file-name file-subst)
2544 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2545 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2546 (pop-to-buffer (concat "*gud" filepart "*"))
2547 (when (and existing-buffer (get-buffer-process existing-buffer))
2548 (error "This program is already running under gdb"))
2549 ;; Set the dir, in case the buffer already existed with a different dir.
2550 (setq default-directory dir)
2551 ;; Set default-directory to the file's directory.
2553 gud-chdir-before-run
2554 ;; Don't set default-directory if no directory was specified.
2555 ;; In that case, either the file is found in the current directory,
2556 ;; in which case this setq is a no-op,
2557 ;; or it is found by searching PATH,
2558 ;; in which case we don't know what directory it was found in.
2559 (file-name-directory file)
2560 (setq default-directory (file-name-directory file)))
2561 (or (bolp) (newline))
2562 (insert "Current directory is " default-directory "\n")
2563 ;; Put the substituted and expanded file name back in its place.
2565 (while (and w (not (eq (car w) t)))
2569 (apply 'make-comint (concat "gud" filepart) program nil
2570 (if massage-args (funcall massage-args file args) args))
2571 ;; Since comint clobbered the mode, we don't set it until now.
2573 (set (make-local-variable 'gud-target-name)
2574 (and file-word (file-name-nondirectory file))))
2575 (set (make-local-variable 'gud-marker-filter) marker-filter)
2576 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2577 (setq gud-running nil)
2578 (setq gud-last-last-frame nil)
2580 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2581 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2584 (defun gud-set-buffer ()
2585 (when (eq major-mode 'gud-mode)
2586 (setq gud-comint-buffer (current-buffer))))
2588 (defvar gud-filter-defer-flag nil
2589 "Non-nil means don't process anything from the debugger right now.
2590 It is saved for when this flag is not set.")
2592 ;; These functions are responsible for inserting output from your debugger
2593 ;; into the buffer. The hard work is done by the method that is
2594 ;; the value of gud-marker-filter.
2596 (defun gud-filter (proc string)
2597 ;; Here's where the actual buffer insertion is done
2598 (let (output process-window)
2599 (if (buffer-name (process-buffer proc))
2600 (if gud-filter-defer-flag
2601 ;; If we can't process any text now,
2602 ;; save it for later.
2603 (setq gud-filter-pending-text
2604 (concat (or gud-filter-pending-text "") string))
2606 ;; If we have to ask a question during the processing,
2607 ;; defer any additional text that comes from the debugger
2608 ;; during that time.
2609 (let ((gud-filter-defer-flag t))
2610 ;; Process now any text we previously saved up.
2611 (if gud-filter-pending-text
2612 (setq string (concat gud-filter-pending-text string)
2613 gud-filter-pending-text nil))
2615 (with-current-buffer (process-buffer proc)
2616 ;; If we have been so requested, delete the debugger prompt.
2619 (if (marker-buffer gud-delete-prompt-marker)
2620 (let ((inhibit-read-only t))
2621 (delete-region (process-mark proc)
2622 gud-delete-prompt-marker)
2623 (comint-update-fence)
2624 (set-marker gud-delete-prompt-marker nil)))
2625 ;; Save the process output, checking for source file markers.
2626 (setq output (gud-marker-filter string))
2627 ;; Check for a filename-and-line number.
2628 ;; Don't display the specified file
2629 ;; unless (1) point is at or after the position where output appears
2630 ;; and (2) this buffer is on the screen.
2631 (setq process-window
2633 (>= (point) (process-mark proc))
2634 (get-buffer-window (current-buffer)))))
2636 ;; Let the comint filter do the actual insertion.
2637 ;; That lets us inherit various comint features.
2638 (comint-output-filter proc output))
2640 ;; Put the arrow on the source line.
2641 ;; This must be outside of the save-excursion
2642 ;; in case the source file is our current buffer.
2644 (with-selected-window process-window
2645 (gud-display-frame))
2646 ;; We have to be in the proper buffer, (process-buffer proc),
2647 ;; but not in a save-excursion, because that would restore point.
2648 (with-current-buffer (process-buffer proc)
2649 (gud-display-frame))))
2651 ;; If we deferred text that arrived during this processing,
2653 (if gud-filter-pending-text
2654 (gud-filter proc ""))))))
2656 (defvar gud-minor-mode-type nil)
2657 (defvar gud-overlay-arrow-position nil)
2658 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2660 (defun gud-sentinel (proc msg)
2661 (cond ((null (buffer-name (process-buffer proc)))
2663 ;; Stop displaying an arrow in a source file.
2664 (setq gud-overlay-arrow-position nil)
2665 (set-process-buffer proc nil)
2666 (if (and (boundp 'speedbar-frame)
2667 (string-equal speedbar-initial-expansion-list-name "GUD"))
2668 (speedbar-change-initial-expansion-list
2669 speedbar-previously-used-expansion-list-name))
2670 (if (memq gud-minor-mode-type '(gdbmi gdba))
2673 ((memq (process-status proc) '(signal exit))
2674 ;; Stop displaying an arrow in a source file.
2675 (setq gud-overlay-arrow-position nil)
2676 (with-current-buffer gud-comint-buffer
2677 (if (memq gud-minor-mode-type '(gdbmi gdba))
2680 (let* ((obuf (current-buffer)))
2681 ;; save-excursion isn't the right thing if
2682 ;; process-buffer is current-buffer
2685 ;; Write something in *compilation* and hack its mode line,
2686 (set-buffer (process-buffer proc))
2687 ;; Fix the mode line.
2688 (setq mode-line-process
2690 (symbol-name (process-status proc))))
2691 (force-mode-line-update)
2693 (insert ?\n mode-name " " msg)
2695 (goto-char (point-max))
2696 (insert ?\n mode-name " " msg)))
2697 ;; If buffer and mode line will show that the process
2698 ;; is dead, we can delete it now. Otherwise it
2699 ;; will stay around until M-x list-processes.
2700 (delete-process proc))
2701 ;; Restore old buffer, but don't restore old point
2702 ;; if obuf is the gud buffer.
2703 (set-buffer obuf))))))
2705 (defun gud-kill-buffer-hook ()
2706 (setq gud-minor-mode-type gud-minor-mode)
2708 (kill-process (get-buffer-process (current-buffer)))
2712 (dolist (buffer (buffer-list))
2713 (unless (eq buffer gud-comint-buffer)
2714 (with-current-buffer buffer
2715 (when gud-minor-mode
2716 (setq gud-minor-mode nil)
2717 (kill-local-variable 'tool-bar-map))))))
2719 (defun gud-display-frame ()
2720 "Find and obey the last filename-and-line marker from the debugger.
2721 Obeying it means displaying in another window the specified file and line."
2723 (when gud-last-frame
2725 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2726 (setq gud-last-last-frame gud-last-frame
2727 gud-last-frame nil)))
2729 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2730 ;; and that its line LINE is visible.
2731 ;; Put the overlay-arrow on the line LINE in that buffer.
2732 ;; Most of the trickiness in here comes from wanting to preserve the current
2733 ;; region-restriction if that's possible. We use an explicit display-buffer
2734 ;; to get around the fact that this is called inside a save-excursion.
2736 (defun gud-display-line (true-file line)
2737 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2739 (with-current-buffer gud-comint-buffer
2740 (gud-find-file true-file)))
2741 (window (and buffer (or (get-buffer-window buffer)
2742 (display-buffer buffer))))
2744 (message "%s %s" (current-buffer) buffer)
2747 (with-current-buffer buffer
2748 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2750 (format "File %s changed on disk. Reread from disk? "
2753 (setq gud-keep-buffer t)))
2758 (or gud-overlay-arrow-position
2759 (setq gud-overlay-arrow-position (make-marker)))
2760 (set-marker gud-overlay-arrow-position (point) (current-buffer))
2761 ;; If they turned on hl-line, move the hl-line highlight to
2762 ;; the arrow's line.
2763 (when (featurep 'hl-line)
2765 (global-hl-line-mode
2766 (global-hl-line-highlight))
2767 ((and hl-line-mode hl-line-sticky-flag)
2768 (hl-line-highlight)))))
2769 (cond ((or (< pos (point-min)) (> pos (point-max)))
2772 (if window (set-window-point window gud-overlay-arrow-position))))))
2774 ;; The gud-call function must do the right thing whether its invoking
2775 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2776 ;; or a C buffer. In the former case, we want to supply data from
2777 ;; gud-last-frame. Here's how we do it:
2779 (defun gud-format-command (str arg)
2780 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2781 (frame (or gud-last-frame gud-last-last-frame))
2783 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2784 (let ((key (string-to-char (match-string 2 str)))
2788 (setq subst (file-name-nondirectory (if insource
2792 (setq subst (file-name-sans-extension
2793 (file-name-nondirectory (if insource
2797 (setq subst (file-name-directory (if insource
2801 (setq subst (int-to-string
2805 (+ (count-lines (point-min) (point))
2809 (setq subst (gud-find-expr)))
2811 (setq subst (gud-read-address)))
2821 (+ (count-lines (point-min) (point))
2825 (setq subst (if arg (int-to-string arg)))))
2826 (setq result (concat result (match-string 1 str) subst)))
2827 (setq str (substring str (match-end 2))))
2828 ;; There might be text left in STR when the loop ends.
2829 (concat result str)))
2831 (defun gud-read-address ()
2832 "Return a string containing the core-address found in the buffer at point."
2835 (let ((pt (point)) found begin)
2836 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2838 (found (forward-char 2)
2839 (buffer-substring found
2840 (progn (re-search-forward "[^0-9a-f]")
2843 (t (setq begin (progn (re-search-backward "[^0-9]")
2847 (re-search-forward "[^0-9]")
2849 (buffer-substring begin (point))))))))
2851 (defun gud-call (fmt &optional arg)
2852 (let ((msg (gud-format-command fmt arg)))
2853 (message "Command: %s" msg)
2855 (gud-basic-call msg)))
2857 (defun gud-basic-call (command)
2858 "Invoke the debugger COMMAND displaying source in other window."
2861 (let ((proc (get-buffer-process gud-comint-buffer)))
2862 (or proc (error "Current buffer has no process"))
2863 ;; Arrange for the current prompt to get deleted.
2865 (set-buffer gud-comint-buffer)
2868 (goto-char (process-mark proc))
2870 (if (looking-at comint-prompt-regexp)
2871 (set-marker gud-delete-prompt-marker (point)))
2872 (if (memq gud-minor-mode '(gdbmi gdba))
2873 (apply comint-input-sender (list proc command))
2874 (process-send-string proc (concat command "\n")))))))
2876 (defun gud-refresh (&optional arg)
2877 "Fix up a possibly garbled display, and redraw the arrow."
2879 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2883 ;; Code for parsing expressions out of C or Fortran code. The single entry
2884 ;; point is gud-find-expr, which tries to return an lvalue expression from
2887 (defvar gud-find-expr-function 'gud-find-c-expr)
2889 (defun gud-find-expr (&rest args)
2890 (apply gud-find-expr-function args))
2892 ;; The next eight functions are hacked from gdbsrc.el by
2893 ;; Debby Ayers <ayers@asc.slb.com>,
2894 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2896 (defun gud-find-c-expr ()
2897 "Returns the expr that surrounds point."
2901 (expr (gud-innermost-expr))
2902 (test-expr (gud-prev-expr)))
2903 (while (and test-expr (gud-expr-compound test-expr expr))
2904 (let ((prev-expr expr))
2905 (setq expr (cons (car test-expr) (cdr expr)))
2906 (goto-char (car expr))
2907 (setq test-expr (gud-prev-expr))
2908 ;; If we just pasted on the condition of an if or while,
2909 ;; throw it away again.
2910 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2911 '("if" "while" "for"))
2915 (setq test-expr (gud-next-expr))
2916 (while (gud-expr-compound expr test-expr)
2917 (setq expr (cons (car expr) (cdr test-expr)))
2918 (setq test-expr (gud-next-expr)))
2919 (buffer-substring (car expr) (cdr expr)))))
2921 (defun gud-innermost-expr ()
2922 "Returns the smallest expr that point is in; move point to beginning of it.
2923 The expr is represented as a cons cell, where the car specifies the point in
2924 the current buffer that marks the beginning of the expr and the cdr specifies
2925 the character after the end of the expr."
2926 (let ((p (point)) begin end)
2928 (setq begin (point))
2941 (defun gud-backward-sexp ()
2942 "Version of `backward-sexp' that catches errors."
2947 (defun gud-forward-sexp ()
2948 "Version of `forward-sexp' that catches errors."
2953 (defun gud-prev-expr ()
2954 "Returns the previous expr, point is set to beginning of that expr.
2955 The expr is represented as a cons cell, where the car specifies the point in
2956 the current buffer that marks the beginning of the expr and the cdr specifies
2957 the character after the end of the expr"
2958 (let ((begin) (end))
2960 (setq begin (point))
2966 (defun gud-next-expr ()
2967 "Returns the following expr, point is set to beginning of that expr.
2968 The expr is represented as a cons cell, where the car specifies the point in
2969 the current buffer that marks the beginning of the expr and the cdr specifies
2970 the character after the end of the expr."
2971 (let ((begin) (end))
2976 (setq begin (point))
2979 (defun gud-expr-compound-sep (span-start span-end)
2980 "Scan from SPAN-START to SPAN-END for punctuation characters.
2981 If `->' is found, return `?.'. If `.' is found, return `?.'.
2982 If any other punctuation is found, return `??'.
2983 If no punctuation is found, return `? '."
2986 (while (< span-start span-end)
2987 (setq syntax (char-syntax (char-after span-start)))
2990 ((= syntax ?.) (setq syntax (char-after span-start))
2992 ((= syntax ?.) (setq result ?.))
2993 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2995 (setq span-start (+ span-start 1)))
2996 (t (setq span-start span-end)
2997 (setq result ??)))))
2998 (setq span-start (+ span-start 1)))
3001 (defun gud-expr-compound (first second)
3002 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
3003 The two exprs are represented as a cons cells, where the car
3004 specifies the point in the current buffer that marks the beginning of the
3005 expr and the cdr specifies the character after the end of the expr.
3006 Link exprs of the form:
3013 (let ((span-start (cdr first))
3014 (span-end (car second))
3016 (setq syntax (gud-expr-compound-sep span-start span-end))
3018 ((= (car first) (car second)) nil)
3019 ((= (cdr first) (cdr second)) nil)
3022 (setq span-start (char-after (- span-start 1)))
3023 (setq span-end (char-after span-end))
3025 ((= span-start ?)) t)
3026 ((= span-start ?]) t)
3032 (defun gud-find-class (f line)
3033 "Find fully qualified class in file F at line LINE.
3034 This function uses the `gud-jdb-classpath' (and optional
3035 `gud-jdb-sourcepath') list(s) to derive a file
3036 pathname relative to its classpath directory. The values in
3037 `gud-jdb-classpath' are assumed to have been converted to absolute
3038 pathname standards using file-truename.
3039 If F is visited by a buffer and its mode is CC-mode(Java),
3040 syntactic information of LINE is used to find the enclosing (nested)
3041 class string which is appended to the top level
3042 class of the file (using s to separate nested class ids)."
3043 ;; Convert f to a standard representation and remove suffix
3044 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
3046 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
3047 (fbuffer (get-file-buffer f))
3048 syntax-symbol syntax-point class-found)
3049 (setq f (file-name-sans-extension (file-truename f)))
3050 ;; Syntax-symbol returns the symbol of the *first* element
3051 ;; in the syntactical analysis result list, syntax-point
3052 ;; returns the buffer position of same
3053 (fset 'syntax-symbol (lambda (x) (c-langelem-sym (car x))))
3054 (fset 'syntax-point (lambda (x) (c-langelem-pos (car x))))
3055 ;; Search through classpath list for an entry that is
3057 (while (and cplist (not class-found))
3058 (if (string-match (car cplist) f)
3060 (mapconcat 'identity
3062 (substring f (+ (match-end 0) 1))
3064 (setq cplist (cdr cplist)))
3065 ;; if f is visited by a java(cc-mode) buffer, walk up the
3066 ;; syntactic information chain and collect any 'inclass
3067 ;; symbols until 'topmost-intro is reached to find out if
3068 ;; point is within a nested class
3069 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
3071 (set-buffer fbuffer)
3072 (let ((nclass) (syntax))
3073 ;; While the c-syntactic information does not start
3074 ;; with the 'topmost-intro symbol, there may be
3075 ;; nested classes...
3076 (while (not (eq 'topmost-intro
3077 (syntax-symbol (c-guess-basic-syntax))))
3078 ;; Check if the current position c-syntactic
3079 ;; analysis has 'inclass
3080 (setq syntax (c-guess-basic-syntax))
3082 (and (not (eq 'inclass (syntax-symbol syntax)))
3084 (setq syntax (cdr syntax)))
3085 (if (eq 'inclass (syntax-symbol syntax))
3087 (goto-char (syntax-point syntax))
3088 ;; Now we're at the beginning of a class
3089 ;; definition. Find class name
3091 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
3093 (append (list (match-string-no-properties 1))
3095 (setq syntax (c-guess-basic-syntax))
3096 (while (and (not (syntax-point syntax)) (cdr syntax))
3097 (setq syntax (cdr syntax)))
3098 (goto-char (syntax-point syntax))
3100 (string-match (concat (car nclass) "$") class-found)
3102 (replace-match (mapconcat 'identity nclass "$")
3103 t t class-found)))))
3104 (if (not class-found)
3105 (message "gud-find-class: class for file %s not found!" f))
3107 ;; Not using classpath - try class/source association list
3108 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
3111 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
3115 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3116 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3117 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3119 (defvar gdb-script-mode-syntax-table
3120 (let ((st (make-syntax-table)))
3121 (modify-syntax-entry ?' "\"" st)
3122 (modify-syntax-entry ?# "<" st)
3123 (modify-syntax-entry ?\n ">" st)
3126 (defvar gdb-script-font-lock-keywords
3127 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
3128 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
3129 ("^\\s-*\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-keyword-face))))
3131 ;; FIXME: The keyword "end" associated with "document"
3132 ;; should have font-lock-keyword-face (currently font-lock-doc-face).
3133 (defvar gdb-script-font-lock-syntactic-keywords
3134 '(("^document\\s-.*\\(\n\\)" (1 "< b"))
3135 ;; It would be best to change the \n in front, but it's more difficult.
3136 ("^en\\(d\\)\\>" (1 "> b"))))
3138 (defun gdb-script-font-lock-syntactic-face (state)
3140 ((nth 3 state) font-lock-string-face)
3141 ((nth 7 state) font-lock-doc-face)
3142 (t font-lock-comment-face)))
3144 (defvar gdb-script-basic-indent 2)
3146 (defun gdb-script-skip-to-head ()
3147 "We're just in front of an `end' and we need to go to its head."
3148 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\|commands\\)\\>" nil 'move)
3150 (gdb-script-skip-to-head)))
3152 (defun gdb-script-calculate-indentation ()
3154 ((looking-at "end\\>")
3155 (gdb-script-skip-to-head)
3156 (current-indentation))
3157 ((looking-at "else\\>")
3158 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3160 (gdb-script-skip-to-head))
3161 (current-indentation))
3163 (forward-comment (- (point-max)))
3165 (skip-chars-forward " \t")
3166 (+ (current-indentation)
3167 (if (looking-at "\\(if\\|while\\|define\\|else\\|commands\\)\\>")
3168 gdb-script-basic-indent 0)))))
3170 (defun gdb-script-indent-line ()
3171 "Indent current line of GDB script."
3173 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3176 (skip-chars-forward " \t")
3177 (not (looking-at "end\\>"))))
3179 (let* ((savep (point))
3180 (indent (condition-case nil
3183 (skip-chars-forward " \t")
3184 (if (>= (point) savep) (setq savep nil))
3185 (max (gdb-script-calculate-indentation) 0))
3188 (save-excursion (indent-line-to indent))
3189 (indent-line-to indent)))))
3191 ;; Derived from cfengine.el.
3192 (defun gdb-script-beginning-of-defun ()
3193 "`beginning-of-defun' function for Gdb script mode.
3194 Treats actions as defuns."
3195 (unless (<= (current-column) (current-indentation))
3197 (if (re-search-backward "^define \\|^document " nil t)
3199 (goto-char (point-min)))
3202 ;; Derived from cfengine.el.
3203 (defun gdb-script-end-of-defun ()
3204 "`end-of-defun' function for Gdb script mode.
3205 Treats actions as defuns."
3207 (if (re-search-forward "^end" nil t)
3209 (goto-char (point-max)))
3213 (add-to-list 'auto-mode-alist '("/\\.gdbinit" . gdb-script-mode))
3216 (define-derived-mode gdb-script-mode nil "GDB-Script"
3217 "Major mode for editing GDB scripts"
3218 (set (make-local-variable 'comment-start) "#")
3219 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3220 (set (make-local-variable 'outline-regexp) "[ \t]")
3221 (set (make-local-variable 'imenu-generic-expression)
3222 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3223 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3224 (set (make-local-variable 'beginning-of-defun-function)
3225 #'gdb-script-beginning-of-defun)
3226 (set (make-local-variable 'end-of-defun-function)
3227 #'gdb-script-end-of-defun)
3228 (set (make-local-variable 'font-lock-defaults)
3229 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3230 (font-lock-syntactic-keywords
3231 . gdb-script-font-lock-syntactic-keywords)
3232 (font-lock-syntactic-face-function
3233 . gdb-script-font-lock-syntactic-face))))
3236 ;;; tooltips for GUD
3238 ;;; Customizable settings
3240 (define-minor-mode gud-tooltip-mode
3241 "Toggle the display of GUD tooltips."
3246 (if gud-tooltip-mode
3248 (add-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3249 (add-hook 'pre-command-hook 'tooltip-hide)
3250 (add-hook 'tooltip-hook 'gud-tooltip-tips)
3251 (define-key global-map [mouse-movement] 'gud-tooltip-mouse-motion))
3252 (unless tooltip-mode (remove-hook 'pre-command-hook 'tooltip-hide)
3253 (remove-hook 'change-major-mode-hook 'gud-tooltip-change-major-mode)
3254 (remove-hook 'tooltip-hook 'gud-tooltip-tips)
3255 (define-key global-map [mouse-movement] 'ignore)))
3256 (gud-tooltip-activate-mouse-motions-if-enabled)
3259 (buffer-name gud-comint-buffer); gud-comint-buffer might be killed
3260 (with-current-buffer gud-comint-buffer
3261 (memq gud-minor-mode '(gdbmi gdba))))
3262 (if gud-tooltip-mode
3264 (dolist (buffer (buffer-list))
3265 (unless (eq buffer gud-comint-buffer)
3266 (with-current-buffer buffer
3267 (when (and (memq gud-minor-mode '(gdbmi gdba))
3268 (not (string-match "\\`\\*.+\\*\\'"
3270 (make-local-variable 'gdb-define-alist)
3271 (gdb-create-define-alist)
3272 (add-hook 'after-save-hook
3273 'gdb-create-define-alist nil t))))))
3274 (kill-local-variable 'gdb-define-alist)
3275 (remove-hook 'after-save-hook 'gdb-create-define-alist t))))
3277 (defcustom gud-tooltip-modes '(gud-mode c-mode c++-mode fortran-mode)
3278 "List of modes for which to enable GUD tooltips."
3283 (defcustom gud-tooltip-display
3284 '((eq (tooltip-event-buffer gud-tooltip-event)
3285 (marker-buffer gud-overlay-arrow-position)))
3286 "List of forms determining where GUD tooltips are displayed.
3288 Forms in the list are combined with AND. The default is to display
3289 only tooltips in the buffer containing the overlay arrow."
3294 (defcustom gud-tooltip-echo-area nil
3295 "Use the echo area instead of frames for GUD tooltips."
3300 (define-obsolete-variable-alias 'tooltip-gud-modes
3301 'gud-tooltip-modes "22.1")
3302 (define-obsolete-variable-alias 'tooltip-gud-display
3303 'gud-tooltip-display "22.1")
3305 ;;; Reacting on mouse movements
3307 (defun gud-tooltip-change-major-mode ()
3308 "Function added to `change-major-mode-hook' when tooltip mode is on."
3309 (add-hook 'post-command-hook 'gud-tooltip-activate-mouse-motions-if-enabled))
3311 (defun gud-tooltip-activate-mouse-motions-if-enabled ()
3312 "Reconsider for all buffers whether mouse motion events are desired."
3313 (remove-hook 'post-command-hook
3314 'gud-tooltip-activate-mouse-motions-if-enabled)
3315 (dolist (buffer (buffer-list))
3318 (if (and gud-tooltip-mode
3319 (memq major-mode gud-tooltip-modes))
3320 (gud-tooltip-activate-mouse-motions t)
3321 (gud-tooltip-activate-mouse-motions nil)))))
3323 (defvar gud-tooltip-mouse-motions-active nil
3324 "Locally t in a buffer if tooltip processing of mouse motion is enabled.")
3326 (defun gud-tooltip-activate-mouse-motions (activatep)
3327 "Activate/deactivate mouse motion events for the current buffer.
3328 ACTIVATEP non-nil means activate mouse motion events."
3331 (make-local-variable 'gud-tooltip-mouse-motions-active)
3332 (setq gud-tooltip-mouse-motions-active t)
3333 (make-local-variable 'track-mouse)
3334 (setq track-mouse t))
3335 (when gud-tooltip-mouse-motions-active
3336 (kill-local-variable 'gud-tooltip-mouse-motions-active)
3337 (kill-local-variable 'track-mouse))))
3339 (defun gud-tooltip-mouse-motion (event)
3340 "Command handler for mouse movement events in `global-map'."
3343 (when (car (mouse-pixel-position))
3344 (setq tooltip-last-mouse-motion-event (copy-sequence event))
3345 (tooltip-start-delayed-tip)))
3349 (defvar gud-tooltip-original-filter nil
3350 "Process filter to restore after GUD output has been received.")
3352 (defvar gud-tooltip-dereference nil
3353 "Non-nil means print expressions with a `*' in front of them.
3354 For C this would dereference a pointer expression.")
3356 (defvar gud-tooltip-event nil
3357 "The mouse movement event that led to a tooltip display.
3358 This event can be examined by forms in GUD-TOOLTIP-DISPLAY.")
3360 (defun gud-tooltip-dereference ()
3361 "Toggle whether tooltips should show `* expr' or `expr'.
3362 With arg, dereference expr iff arg is positive."
3364 (setq gud-tooltip-dereference
3366 (not gud-tooltip-dereference)
3367 (> (prefix-numeric-value arg) 0)))
3368 (message "Dereferencing is now %s."
3369 (if gud-tooltip-dereference "on" "off")))
3371 (define-obsolete-function-alias 'tooltip-gud-toggle-dereference
3372 'gud-tooltip-dereference "22.1")
3374 ; This will only display data that comes in one chunk.
3375 ; Larger arrays (say 400 elements) are displayed in
3376 ; the tooltip incompletely and spill over into the gud buffer.
3377 ; Switching the process-filter creates timing problems and
3378 ; it may be difficult to do better. Using annotations as in
3379 ; gdb-ui.el gets round this problem.
3380 (defun gud-tooltip-process-output (process output)
3381 "Process debugger output and show it in a tooltip window."
3382 (set-process-filter process gud-tooltip-original-filter)
3383 (tooltip-show (tooltip-strip-prompt process output)
3384 (or gud-tooltip-echo-area tooltip-use-echo-area)))
3386 (defun gud-tooltip-print-command (expr)
3387 "Return a suitable command to print the expression EXPR."
3388 (case gud-minor-mode
3389 (gdba (concat "server print " expr))
3390 ((dbx gdbmi) (concat "print " expr))
3391 (xdb (concat "p " expr))
3392 (sdb (concat expr "/"))
3395 (defun gud-tooltip-tips (event)
3396 "Show tip for identifier or selection under the mouse.
3397 The mouse must either point at an identifier or inside a selected
3398 region for the tip window to be shown. If gud-tooltip-dereference is t,
3399 add a `*' in front of the printed expression. In the case of a C program
3400 controlled by GDB, show the associated #define directives when program is
3403 This function must return nil if it doesn't handle EVENT."
3405 (when (and (eventp event)
3408 (buffer-name gud-comint-buffer); might be killed
3409 (setq process (get-buffer-process gud-comint-buffer))
3410 (posn-point (event-end event))
3411 (or (and (eq gud-minor-mode 'gdba) (not gdb-active-process))
3412 (progn (setq gud-tooltip-event event)
3413 (eval (cons 'and gud-tooltip-display)))))
3414 (let ((expr (tooltip-expr-to-print event)))
3416 (if (and (eq gud-minor-mode 'gdba)
3417 (not gdb-active-process))
3419 (with-current-buffer
3420 (window-buffer (let ((mouse (mouse-position)))
3421 (window-at (cadr mouse)
3423 (let ((define-elt (assoc expr gdb-define-alist)))
3424 (unless (null define-elt)
3427 (or gud-tooltip-echo-area tooltip-use-echo-area))
3429 (when gud-tooltip-dereference
3430 (setq expr (concat "*" expr)))
3431 (let ((cmd (gud-tooltip-print-command expr)))
3432 (when (and gud-tooltip-mode (eq gud-minor-mode 'gdb))
3433 (gud-tooltip-mode -1)
3434 (message-box "Using GUD tooltips in this mode is unsafe\n\
3435 so they have been disabled."))
3436 (unless (null cmd) ; CMD can be nil if unknown debugger
3437 (if (memq gud-minor-mode '(gdba gdbmi))
3441 gdb-server-prefix "macro expand " expr "\n")
3442 `(lambda () (gdb-tooltip-print-1 ,expr))))
3444 (list (concat cmd "\n") 'gdb-tooltip-print)))
3445 (setq gud-tooltip-original-filter (process-filter process))
3446 (set-process-filter process 'gud-tooltip-process-output)
3447 (gud-basic-call cmd))
3452 ;;; arch-tag: 6d990948-df65-461a-be39-1c7fb83ac4c4
3453 ;;; gud.el ends here