1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992,93,94,95,96,1998,2000,02,03,04 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38 ;; <ddavies@world.std.com> added support for jdb (Java debugger.)
45 ;; ======================================================================
46 ;; GUD commands must be visible in C buffers visited by GUD
49 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
50 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
55 (defcustom gud-key-prefix "\C-x\C-a"
56 "Prefix of all GUD commands valid in C buffers."
60 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
61 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
63 (defvar gud-marker-filter nil)
64 (put 'gud-marker-filter 'permanent-local t)
65 (defvar gud-find-file nil)
66 (put 'gud-find-file 'permanent-local t)
68 (defun gud-marker-filter (&rest args)
69 (apply gud-marker-filter args))
71 (defvar gud-minor-mode nil)
72 (put 'gud-minor-mode 'permanent-local t)
74 (defvar gud-keep-buffer nil)
76 (defun gud-symbol (sym &optional soft minor-mode)
77 "Return the symbol used for SYM in MINOR-MODE.
78 MINOR-MODE defaults to `gud-minor-mode.
79 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
80 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
81 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
82 (funcall (if soft 'intern-soft 'intern)
83 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
85 (defun gud-val (sym &optional minor-mode)
86 "Return the value of `gud-symbol' SYM. Default to nil."
87 (let ((sym (gud-symbol sym t minor-mode)))
88 (if (boundp sym) (symbol-value sym))))
90 (defvar gud-running nil
91 "Non-nil if debuggee is running.
92 Used to grey out relevant togolbar icons.")
94 ;; Use existing Info buffer, if possible.
95 (defun gud-goto-info ()
96 "Go to relevant Emacs info node."
98 (let ((same-window-regexps same-window-regexps)
99 (display-buffer-reuse-frames t))
103 (if (eq (window-buffer window) (get-buffer "*info*"))
105 (setq same-window-regexps nil)
106 (throw 'info-found nil))))
109 (select-frame (make-frame)))
110 (if (memq gud-minor-mode '(gdbmi gdba))
111 (Info-goto-node "(emacs)GDB Graphical Interface")
112 (Info-goto-node "(emacs)Debuggers"))))
114 (easy-mmode-defmap gud-menu-map
115 '(([help] "Info" . gud-goto-info)
116 ([refresh] "Refresh" . gud-refresh)
117 ([run] menu-item "Run" gud-run
118 :enable (and (not gud-running)
119 (memq gud-minor-mode '(gdbmi gdba gdb dbx jdb))))
120 ([until] menu-item "Continue to selection" gud-until
121 :enable (and (not gud-running)
122 (memq gud-minor-mode '(gdbmi gdba gdb perldb))))
123 ([remove] menu-item "Remove Breakpoint" gud-remove
124 :enable (not gud-running))
125 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
126 :enable (memq gud-minor-mode '(gdbmi gdba gdb sdb xdb bashdb)))
127 ([break] menu-item "Set Breakpoint" gud-break
128 :enable (not gud-running))
129 ([up] menu-item "Up Stack" gud-up
130 :enable (and (not gud-running)
132 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
133 ([down] menu-item "Down Stack" gud-down
134 :enable (and (not gud-running)
136 '(gdbmi gdba gdb dbx xdb jdb pdb bashdb))))
137 ([print] menu-item "Print Expression" gud-print
138 :enable (not gud-running))
139 ([watch] menu-item "Watch Expression" gud-watch
140 :enable (and (not gud-running)
141 (memq gud-minor-mode '(gdbmi gdba))))
142 ([finish] menu-item "Finish Function" gud-finish
143 :enable (and (not gud-running)
145 '(gdbmi gdba gdb xdb jdb pdb bashdb))))
146 ([stepi] menu-item "Step Instruction" gud-stepi
147 :enable (and (not gud-running)
148 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
149 ([nexti] menu-item "Next Instruction" gud-nexti
150 :enable (and (not gud-running)
151 (memq gud-minor-mode '(gdbmi gdba gdb dbx))))
152 ([step] menu-item "Step Line" gud-step
153 :enable (not gud-running))
154 ([next] menu-item "Next Line" gud-next
155 :enable (not gud-running))
156 ([cont] menu-item "Continue" gud-cont
157 :enable (not gud-running)))
158 "Menu for `gud-mode'."
161 (easy-mmode-defmap gud-minor-mode-map
162 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
163 "Map used in visited files.")
165 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
166 (if m (setcdr m gud-minor-mode-map)
167 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
170 ;; Will inherit from comint-mode via define-derived-mode.
172 "`gud-mode' keymap.")
174 (defvar gud-tool-bar-map
175 (if (display-graphic-p)
176 (let ((map (make-sparse-keymap)))
177 (dolist (x '((gud-break . "gud-break")
178 (gud-remove . "gud-remove")
179 (gud-print . "gud-print")
180 (gud-watch . "gud-watch")
181 (gud-run . "gud-run")
182 (gud-until . "gud-until")
183 (gud-cont . "gud-cont")
184 ;; gud-s, gud-si etc. instead of gud-step,
185 ;; gud-stepi, to avoid file-name clashes on DOS
189 (gud-finish . "gud-finish")
190 (gud-stepi . "gud-si")
191 (gud-nexti . "gud-ni")
193 (gud-down . "gud-down")
194 (gud-goto-info . "info"))
196 (tool-bar-local-item-from-menu
197 (car x) (cdr x) map gud-minor-mode-map)))))
199 (defun gud-file-name (f)
200 "Transform a relative file name to an absolute file name.
201 Uses `gud-<MINOR-MODE>-directories' to find the source files."
202 (if (file-exists-p f) (expand-file-name f)
203 (let ((directories (gud-val 'directories))
206 (let ((path (expand-file-name f (car directories))))
207 (if (file-exists-p path)
210 (setq directories (cdr directories)))
213 (defun gud-find-file (file)
214 ;; Don't get confused by double slashes in the name that comes from GDB.
215 (while (string-match "//+" file)
216 (setq file (replace-match "/" t t file)))
217 (let ((minor-mode gud-minor-mode)
218 (buf (funcall (or gud-find-file 'gud-file-name) file)))
220 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
222 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
223 (with-current-buffer buf
224 (set (make-local-variable 'gud-minor-mode) minor-mode)
225 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
226 (make-local-variable 'gud-keep-buffer))
229 ;; ======================================================================
230 ;; command definition
232 ;; This macro is used below to define some basic debugger interface commands.
233 ;; Of course you may use `gud-def' with any other debugger command, including
234 ;; user defined ones.
236 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
237 ;; which defines FUNC to send the command NAME to the debugger, gives
238 ;; it the docstring DOC, and binds that function to KEY in the GUD
239 ;; major mode. The function is also bound in the global keymap with the
242 (defmacro gud-def (func cmd key &optional doc)
243 "Define FUNC to be a command sending STR and bound to KEY, with
244 optional doc string DOC. Certain %-escapes in the string arguments
245 are interpreted specially if present. These are:
247 %f name (without directory) of current source file.
248 %F name (without directory or extension) of current source file.
249 %d directory of current source file.
250 %l number of current source line
251 %e text of the C lvalue or function-call expression surrounding point.
252 %a text of the hexadecimal address surrounding point
253 %p prefix argument to the command (if any) as a number
255 The `current' source file is the file of the current buffer (if
256 we're in a C file) or the source file current at the last break or
257 step (if we're in the GUD buffer).
258 The `current' line is that of the current buffer (if we're in a
259 source file) or the source line number at the last break or step (if
260 we're in the GUD buffer)."
263 ,@(if doc (list doc))
268 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
269 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
271 ;; Where gud-display-frame should put the debugging arrow; a cons of
272 ;; (filename . line-number). This is set by the marker-filter, which scans
273 ;; the debugger's output for indications of the current program counter.
274 (defvar gud-last-frame nil)
276 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
277 ;; the last frame, even if it's been called before and gud-last-frame has
279 (defvar gud-last-last-frame nil)
281 ;; All debugger-specific information is collected here.
282 ;; Here's how it works, in case you ever need to add a debugger to the mode.
284 ;; Each entry must define the following at startup:
287 ;; comint-prompt-regexp
288 ;; gud-<name>-massage-args
289 ;; gud-<name>-marker-filter
290 ;; gud-<name>-find-file
292 ;; The job of the massage-args method is to modify the given list of
293 ;; debugger arguments before running the debugger.
295 ;; The job of the marker-filter method is to detect file/line markers in
296 ;; strings and set the global gud-last-frame to indicate what display
297 ;; action (if any) should be triggered by the marker. Note that only
298 ;; whatever the method *returns* is displayed in the buffer; thus, you
299 ;; can filter the debugger's output, interpreting some and passing on
302 ;; The job of the find-file method is to visit and return the buffer indicated
303 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
306 ;; ======================================================================
307 ;; speedbar support functions and variables.
308 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
310 (defvar gud-last-speedbar-buffer nil
311 "The last GUD buffer used.")
313 (defvar gud-last-speedbar-stackframe nil
314 "Description of the currently displayed GUD stack.
315 t means that there is no stack, and we are in display-file mode.")
317 (defvar gud-speedbar-key-map nil
318 "Keymap used when in the buffers display mode.")
320 (defun gud-install-speedbar-variables ()
321 "Install those variables used by speedbar to enhance gud/gdb."
322 (if gud-speedbar-key-map
324 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
326 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
327 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
328 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)
329 (define-key gud-speedbar-key-map "D" 'gdb-var-delete)))
332 (defvar gud-speedbar-menu-items
333 ;; Note to self. Add expand, and turn off items when not available.
334 '(["Jump to stack frame" speedbar-edit-line
335 (with-current-buffer gud-comint-buffer
336 (not (memq gud-minor-mode '(gdbmi gdba))))]
337 ["Edit value" speedbar-edit-line
338 (with-current-buffer gud-comint-buffer
339 (not (memq gud-minor-mode '(gdbmi gdba))))]
340 ["Delete expression" gdb-var-delete
341 (with-current-buffer gud-comint-buffer
342 (not (memq gud-minor-mode '(gdbmi gdba))))])
343 "Additional menu items to add to the speedbar frame.")
345 ;; Make sure our special speedbar mode is loaded
346 (if (featurep 'speedbar)
347 (gud-install-speedbar-variables)
348 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
350 (defun gud-speedbar-buttons (buffer)
351 "Create a speedbar display based on the current state of GUD.
352 If the GUD BUFFER is not running a supported debugger, then turn
353 off the specialized speedbar mode."
354 (let ((minor-mode (with-current-buffer buffer gud-minor-mode)))
356 ((memq minor-mode '(gdbmi gdba))
357 (when (or gdb-var-changed
359 (goto-char (point-min))
360 (let ((case-fold-search t))
361 (looking-at "Watch Expressions:")))))
363 (insert "Watch Expressions:\n")
364 (let ((var-list gdb-var-list))
366 (let* ((depth 0) (start 0) (char ?+)
367 (var (car var-list)) (varnum (nth 1 var)))
368 (while (string-match "\\." varnum start)
369 (setq depth (1+ depth)
370 start (1+ (match-beginning 0))))
371 (if (equal (nth 2 var) "0")
372 (speedbar-make-tag-line 'bracket ?? nil nil
373 (concat (car var) "\t" (nth 4 var))
377 gdb-show-changed-values)
378 'font-lock-warning-face
380 (if (and (cadr var-list)
381 (string-match varnum (cadr (cadr var-list))))
383 (speedbar-make-tag-line 'bracket char
384 'gdb-speedbar-expand-node varnum
385 (concat (car var) "\t" (nth 3 var))
387 (setq var-list (cdr var-list))))
388 (setq gdb-var-changed nil)))
389 (t (if (and (save-excursion
390 (goto-char (point-min))
391 (looking-at "Current Stack"))
392 (equal gud-last-last-frame gud-last-speedbar-stackframe))
394 (setq gud-last-speedbar-buffer buffer)
395 (let ((gud-frame-list
396 (cond ((eq minor-mode 'gdb)
397 (gud-gdb-get-stackframe buffer))
398 ;; Add more debuggers here!
399 (t (speedbar-remove-localized-speedbar-support buffer)
402 (if (not gud-frame-list)
403 (insert "No Stack frames\n")
404 (insert "Current Stack:\n"))
405 (dolist (frame gud-frame-list)
406 (insert (nth 1 frame) ":\n")
407 (if (= (length frame) 2)
409 ; (speedbar-insert-button "[?]"
410 ; 'speedbar-button-face
412 (speedbar-insert-button (car frame)
413 'speedbar-directory-face
415 ; (speedbar-insert-button "[+]"
416 ; 'speedbar-button-face
417 ; 'speedbar-highlight-face
418 ; 'gud-gdb-get-scope-data
420 (speedbar-insert-button (car frame)
422 'speedbar-highlight-face
423 (cond ((memq minor-mode '(gdbmi gdba gdb))
424 'gud-gdb-goto-stackframe)
425 (t (error "Should never be here")))
427 ; (let ((selected-frame
428 ; (cond ((eq ff 'gud-gdb-find-file)
429 ; (gud-gdb-selected-frame-info buffer))
430 ; (t (error "Should never be here"))))))
432 (setq gud-last-speedbar-stackframe gud-last-last-frame))))))
435 ;; ======================================================================
438 ;; History of argument lists passed to gdb.
439 (defvar gud-gdb-history nil)
441 (defcustom gud-gdb-command-name "gdb --annotate=3"
442 "Default command to execute an executable under the GDB debugger."
446 (defvar gud-gdb-marker-regexp
447 ;; This used to use path-separator instead of ":";
448 ;; however, we found that on both Windows 32 and MSDOS
449 ;; a colon is correct here.
450 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
451 "\\([0-9]*\\)" ":" ".*\n"))
453 ;; There's no guarantee that Emacs will hand the filter the entire
454 ;; marker at once; it could be broken up across several strings. We
455 ;; might even receive a big chunk with several markers in it. If we
456 ;; receive a chunk of text which looks like it might contain the
457 ;; beginning of a marker, we save it here between calls to the
459 (defvar gud-marker-acc "")
460 (make-variable-buffer-local 'gud-marker-acc)
462 (defun gud-gdb-marker-filter (string)
463 (setq gud-marker-acc (concat gud-marker-acc string))
466 ;; Process all the complete markers in this chunk.
467 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
470 ;; Extract the frame position from the marker.
471 gud-last-frame (cons (match-string 1 gud-marker-acc)
472 (string-to-int (match-string 2 gud-marker-acc)))
474 ;; Append any text before the marker to the output we're going
475 ;; to return - we don't include the marker in this text.
476 output (concat output
477 (substring gud-marker-acc 0 (match-beginning 0)))
479 ;; Set the accumulator to the remaining text.
480 gud-marker-acc (substring gud-marker-acc (match-end 0))))
482 ;; Check for annotations and change gud-minor-mode to 'gdba if
484 (while (string-match "\n\032\032\\(.*\\)\n" gud-marker-acc)
485 (when (string-equal (match-string 1 gud-marker-acc) "prompt")
490 ;; Append any text before the marker to the output we're going
491 ;; to return - we don't include the marker in this text.
492 output (concat output
493 (substring gud-marker-acc 0 (match-beginning 0)))
495 ;; Set the accumulator to the remaining text.
496 gud-marker-acc (substring gud-marker-acc (match-end 0))))
498 ;; Does the remaining text look like it might end with the
499 ;; beginning of another marker? If it does, then keep it in
500 ;; gud-marker-acc until we receive the rest of it. Since we
501 ;; know the full marker regexp above failed, it's pretty simple to
502 ;; test for marker starts.
503 (if (string-match "\n\\(\032.*\\)?\\'" gud-marker-acc)
505 ;; Everything before the potential marker start can be output.
506 (setq output (concat output (substring gud-marker-acc
507 0 (match-beginning 0))))
509 ;; Everything after, we save, to combine with later input.
511 (substring gud-marker-acc (match-beginning 0))))
513 (setq output (concat output gud-marker-acc)
518 (easy-mmode-defmap gud-minibuffer-local-map
519 '(("\C-i" . comint-dynamic-complete-filename))
520 "Keymap for minibuffer prompting of gud startup command."
521 :inherit minibuffer-local-map)
523 (defun gud-query-cmdline (minor-mode &optional init)
524 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
525 (cmd-name (gud-val 'command-name minor-mode)))
526 (unless (boundp hist-sym) (set hist-sym nil))
527 (read-from-minibuffer
528 (format "Run %s (like this): " minor-mode)
529 (or (car-safe (symbol-value hist-sym))
530 (concat (or cmd-name (symbol-name minor-mode))
534 (dolist (f (directory-files default-directory) file)
535 (if (and (file-executable-p f)
536 (not (file-directory-p f))
538 (file-newer-than-file-p f file)))
540 gud-minibuffer-local-map nil
543 (defvar gdb-first-prompt t)
545 (defvar gud-filter-pending-text nil
546 "Non-nil means this is text that has been saved for later in `gud-filter'.")
549 (defun gdb (command-line)
550 "Run gdb on program FILE in buffer *gud-FILE*.
551 The directory containing FILE becomes the initial working directory
552 and source-file directory for your debugger."
553 (interactive (list (gud-query-cmdline 'gdb)))
555 (gud-common-init command-line nil 'gud-gdb-marker-filter)
556 (set (make-local-variable 'gud-minor-mode) 'gdb)
558 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
559 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
560 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
561 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
562 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
563 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
564 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
565 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
566 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
567 (gud-def gud-jump "tbreak %f:%l\njump %f:%l" "\C-j" "Relocate execution address to line at point in source buffer.")
569 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
570 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
571 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
572 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
573 (gud-def gud-run "run" nil "Run the program.")
575 (local-set-key "\C-i" 'gud-gdb-complete-command)
576 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
577 (setq paragraph-start comint-prompt-regexp)
578 (setq gdb-first-prompt t)
579 (setq gud-filter-pending-text nil)
580 (run-hooks 'gdb-mode-hook))
582 ;; One of the nice features of GDB is its impressive support for
583 ;; context-sensitive command completion. We preserve that feature
584 ;; in the GUD buffer by using a GDB command designed just for Emacs.
586 ;; The completion process filter indicates when it is finished.
587 (defvar gud-gdb-fetch-lines-in-progress)
589 ;; Since output may arrive in fragments we accumulate partials strings here.
590 (defvar gud-gdb-fetch-lines-string)
592 ;; We need to know how much of the completion to chop off.
593 (defvar gud-gdb-fetch-lines-break)
595 ;; The completion list is constructed by the process filter.
596 (defvar gud-gdb-fetched-lines)
598 (defvar gud-comint-buffer nil)
600 (defun gud-gdb-complete-command ()
601 "Perform completion on the GDB command preceding point.
602 This is implemented using the GDB `complete' command which isn't
603 available with older versions of GDB."
606 (command (buffer-substring (comint-line-beginning-position) end))
608 ;; Find the word break. This match will always succeed.
609 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
610 (substring command (match-beginning 2))))
612 (gud-gdb-run-command-fetch-lines (concat "complete " command)
614 ;; From string-match above.
615 (match-beginning 2))))
616 ;; Protect against old versions of GDB.
618 (string-match "^Undefined command: \"complete\"" (car complete-list))
619 (error "This version of GDB doesn't support the `complete' command"))
620 ;; Sort the list like readline.
621 (setq complete-list (sort complete-list (function string-lessp)))
622 ;; Remove duplicates.
623 (let ((first complete-list)
624 (second (cdr complete-list)))
626 (if (string-equal (car first) (car second))
627 (setcdr first (setq second (cdr second)))
629 second (cdr second)))))
630 ;; Add a trailing single quote if there is a unique completion
631 ;; and it contains an odd number of unquoted single quotes.
632 (and (= (length complete-list) 1)
633 (let ((str (car complete-list))
636 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
637 (setq count (1+ count)
639 (and (= (mod count 2) 1)
640 (setq complete-list (list (concat str "'"))))))
641 ;; Let comint handle the rest.
642 (comint-dynamic-simple-complete command-word complete-list)))
644 ;; The completion process filter is installed temporarily to slurp the
645 ;; output of GDB up to the next prompt and build the completion list.
646 (defun gud-gdb-fetch-lines-filter (string filter)
647 "Filter used to read the list of lines output by a command.
648 STRING is the output to filter.
649 It is passed through FILTER before we look at it."
650 (setq string (funcall filter string))
651 (setq string (concat gud-gdb-fetch-lines-string string))
652 (while (string-match "\n" string)
653 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
654 gud-gdb-fetched-lines)
655 (setq string (substring string (match-end 0))))
656 (if (string-match comint-prompt-regexp string)
658 (setq gud-gdb-fetch-lines-in-progress nil)
661 (setq gud-gdb-fetch-lines-string string)
664 ;; gdb speedbar functions
666 (defun gud-gdb-goto-stackframe (text token indent)
667 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
668 (speedbar-with-attached-buffer
669 (gud-basic-call (concat "server frame " (nth 1 token)))
672 (defvar gud-gdb-fetched-stack-frame nil
673 "Stack frames we are fetching from GDB.")
675 ;(defun gud-gdb-get-scope-data (text token indent)
676 ; ;; checkdoc-params: (indent)
677 ; "Fetch data associated with a stack frame, and expand/contract it.
678 ;Data to do this is retrieved from TEXT and TOKEN."
679 ; (let ((args nil) (scope nil))
680 ; (gud-gdb-run-command-fetch-lines "info args")
682 ; (gud-gdb-run-command-fetch-lines "info local")
686 (defun gud-gdb-get-stackframe (buffer)
687 "Extract the current stack frame out of the GUD GDB BUFFER."
689 (fetched-stack-frame-list
690 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
691 (if (and (car fetched-stack-frame-list)
692 (string-match "No stack" (car fetched-stack-frame-list)))
693 ;; Go into some other mode???
695 (dolist (e fetched-stack-frame-list)
696 (let ((name nil) (num nil))
698 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
699 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
700 (if (not (string-match
701 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
704 (list (nth 0 (car newlst))
707 (match-string 2 e))))
708 (setq num (match-string 1 e)
709 name (match-string 2 e))
713 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
714 (list name num (match-string 1 e)
720 ;(defun gud-gdb-selected-frame-info (buffer)
721 ; "Learn GDB information for the currently selected stack frame in BUFFER."
724 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
725 "Run COMMAND, and return the list of lines it outputs.
726 BUFFER is the GUD buffer in which to run the command.
727 SKIP is the number of chars to skip on each lines, it defaults to 0."
728 (with-current-buffer buffer
730 (goto-char (point-max))
732 (not (looking-at comint-prompt-regexp)))
734 ;; Much of this copied from GDB complete, but I'm grabbing the stack
736 (let ((gud-gdb-fetch-lines-in-progress t)
737 (gud-gdb-fetched-lines nil)
738 (gud-gdb-fetch-lines-string nil)
739 (gud-gdb-fetch-lines-break (or skip 0))
741 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
742 ;; Issue the command to GDB.
743 (gud-basic-call command)
745 (while gud-gdb-fetch-lines-in-progress
746 (accept-process-output (get-buffer-process buffer)))
747 (nreverse gud-gdb-fetched-lines)))))
750 ;; ======================================================================
753 ;; History of argument lists passed to sdb.
754 (defvar gud-sdb-history nil)
756 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
757 "If nil, we're on a System V Release 4 and don't need the tags hack.")
759 (defvar gud-sdb-lastfile nil)
761 (defun gud-sdb-marker-filter (string)
763 (if gud-marker-acc (concat gud-marker-acc string) string))
765 ;; Process all complete markers in this chunk
768 ;; System V Release 3.2 uses this format
769 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
770 gud-marker-acc start)
772 (cons (match-string 3 gud-marker-acc)
773 (string-to-int (match-string 4 gud-marker-acc)))))
774 ;; System V Release 4.0 quite often clumps two lines together
775 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
776 gud-marker-acc start)
777 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
779 (cons gud-sdb-lastfile
780 (string-to-int (match-string 3 gud-marker-acc)))))
781 ;; System V Release 4.0
782 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
783 gud-marker-acc start)
784 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
785 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
786 gud-marker-acc start))
788 (cons gud-sdb-lastfile
789 (string-to-int (match-string 1 gud-marker-acc)))))
791 (setq gud-sdb-lastfile nil)))
792 (setq start (match-end 0)))
794 ;; Search for the last incomplete line in this chunk
795 (while (string-match "\n" gud-marker-acc start)
796 (setq start (match-end 0)))
798 ;; If we have an incomplete line, store it in gud-marker-acc.
799 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
802 (defun gud-sdb-find-file (f)
803 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
806 (defun sdb (command-line)
807 "Run sdb on program FILE in buffer *gud-FILE*.
808 The directory containing FILE becomes the initial working directory
809 and source-file directory for your debugger."
810 (interactive (list (gud-query-cmdline 'sdb)))
812 (if (and gud-sdb-needs-tags
813 (not (and (boundp 'tags-file-name)
814 (stringp tags-file-name)
815 (file-exists-p tags-file-name))))
816 (error "The sdb support requires a valid tags table to work"))
818 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
819 (set (make-local-variable 'gud-minor-mode) 'sdb)
821 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
822 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
823 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
824 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
825 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
826 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
827 (gud-def gud-cont "c" "\C-r" "Continue with display.")
828 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
830 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
831 (setq paragraph-start comint-prompt-regexp)
832 (run-hooks 'sdb-mode-hook)
835 ;; ======================================================================
838 ;; History of argument lists passed to dbx.
839 (defvar gud-dbx-history nil)
841 (defcustom gud-dbx-directories nil
842 "*A list of directories that dbx should search for source code.
843 If nil, only source files in the program directory
844 will be known to dbx.
846 The file names should be absolute, or relative to the directory
847 containing the executable being debugged."
848 :type '(choice (const :tag "Current Directory" nil)
853 (defun gud-dbx-massage-args (file args)
854 (nconc (let ((directories gud-dbx-directories)
857 (setq result (cons (car directories) (cons "-I" result)))
858 (setq directories (cdr directories)))
862 (defun gud-dbx-marker-filter (string)
863 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
866 ;; Process all complete markers in this chunk.
867 (while (or (string-match
868 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
869 gud-marker-acc start)
871 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
872 gud-marker-acc start))
874 (cons (match-string 2 gud-marker-acc)
875 (string-to-int (match-string 1 gud-marker-acc)))
876 start (match-end 0)))
878 ;; Search for the last incomplete line in this chunk
879 (while (string-match "\n" gud-marker-acc start)
880 (setq start (match-end 0)))
882 ;; If the incomplete line APPEARS to begin with another marker, keep it
883 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
884 ;; unnecessary concat during the next call.
886 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
887 (substring gud-marker-acc (match-beginning 0))
891 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
892 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
894 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
895 ;; We haven't tested gud on this system:
896 (string-match "^mips-[^-]*-riscos" system-configuration)
897 ;; It's documented on OSF/1.3
898 (string-match "^mips-[^-]*-osf1" system-configuration)
899 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
900 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
902 (defvar gud-dbx-command-name
903 (concat "dbx" (if gud-mips-p " -emacs")))
905 ;; This is just like the gdb one except for the regexps since we need to cope
906 ;; with an optional breakpoint number in [] before the ^Z^Z
907 (defun gud-mipsdbx-marker-filter (string)
908 (setq gud-marker-acc (concat gud-marker-acc string))
911 ;; Process all the complete markers in this chunk.
913 ;; This is like th gdb marker but with an optional
914 ;; leading break point number like `[1] '
915 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
919 ;; Extract the frame position from the marker.
921 (cons (match-string 1 gud-marker-acc)
922 (string-to-int (match-string 2 gud-marker-acc)))
924 ;; Append any text before the marker to the output we're going
925 ;; to return - we don't include the marker in this text.
926 output (concat output
927 (substring gud-marker-acc 0 (match-beginning 0)))
929 ;; Set the accumulator to the remaining text.
930 gud-marker-acc (substring gud-marker-acc (match-end 0))))
932 ;; Does the remaining text look like it might end with the
933 ;; beginning of another marker? If it does, then keep it in
934 ;; gud-marker-acc until we receive the rest of it. Since we
935 ;; know the full marker regexp above failed, it's pretty simple to
936 ;; test for marker starts.
937 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
939 ;; Everything before the potential marker start can be output.
940 (setq output (concat output (substring gud-marker-acc
941 0 (match-beginning 0))))
943 ;; Everything after, we save, to combine with later input.
945 (substring gud-marker-acc (match-beginning 0))))
947 (setq output (concat output gud-marker-acc)
952 ;; The dbx in IRIX is a pain. It doesn't print the file name when
953 ;; stopping at a breakpoint (but you do get it from the `up' and
954 ;; `down' commands...). The only way to extract the information seems
955 ;; to be with a `file' command, although the current line number is
956 ;; available in $curline. Thus we have to look for output which
957 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
958 ;; to output the information we want with a combination of the
959 ;; `printf' and `file' commands as a pseudo marker which we can
960 ;; recognise next time through the marker-filter. This would be like
961 ;; the gdb marker but you can't get the file name without a newline...
962 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
963 ;; number rather than a line number etc. Maybe this could be made to
964 ;; work by listing all the breakpoints and picking the one(s) with the
965 ;; correct line number, but life's too short.
966 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
969 (and (string-match "^mips-[^-]*-irix" system-configuration)
970 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
971 "Non-nil to assume the interface appropriate for IRIX dbx.
972 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
973 a better solution in 6.1 upwards.")
974 (defvar gud-dbx-use-stopformat-p
975 (string-match "irix[6-9]\\.[1-9]" system-configuration)
976 "Non-nil to use the dbx feature present at least from Irix 6.1
977 whereby $stopformat=1 produces an output format compatiable with
978 `gud-dbx-marker-filter'.")
979 ;; [Irix dbx seems to be a moving target. The dbx output changed
980 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
981 ;; the output from `up' is no longer spotted by gud (and it's probably
982 ;; not distinctive enough to try to match it -- use C-<, C->
983 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
984 ;; `long long'(why?!), so the printf stuff needed changing. The line
985 ;; number was cast to `long' as a compromise between the new `long
986 ;; long' and the original `int'. This is reported not to work in 6.2,
987 ;; so it's changed back to int -- don't make your sources too long.
988 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
989 ;; whereby `set $stopformat=1' reportedly produces output compatible
990 ;; with `gud-dbx-marker-filter', which we prefer.
992 ;; The process filter is also somewhat
993 ;; unreliable, sometimes not spotting the markers; I don't know
994 ;; whether there's anything that can be done about that. It would be
995 ;; much better if SGI could be persuaded to (re?)instate the MIPS
996 ;; -emacs flag for gdb-like output (which ought to be possible as most
997 ;; of the communication I've had over it has been from sgi.com).]
999 ;; this filter is influenced by the xdb one rather than the gdb one
1000 (defun gud-irixdbx-marker-filter (string)
1001 (let (result (case-fold-search nil))
1002 (if (or (string-match comint-prompt-regexp string)
1003 (string-match ".*\012" string))
1004 (setq result (concat gud-marker-acc string)
1006 (setq gud-marker-acc (concat gud-marker-acc string)))
1009 ;; look for breakpoint or signal indication e.g.:
1010 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
1011 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
1012 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
1014 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
1016 ;; prod dbx into printing out the line number and file
1017 ;; name in a form we can grok as below
1018 (process-send-string (get-buffer-process gud-comint-buffer)
1019 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1020 ;; look for result of, say, "up" e.g.:
1021 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
1022 ;; (this will also catch one of the lines printed by "where")
1024 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
1026 (let ((file (match-string 1 result)))
1027 (if (file-exists-p file)
1028 (setq gud-last-frame
1029 (cons (match-string 1 result)
1030 (string-to-int (match-string 2 result))))))
1032 ((string-match ; kluged-up marker as above
1033 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
1034 (let ((file (gud-file-name (match-string 2 result))))
1035 (if (and file (file-exists-p file))
1036 (setq gud-last-frame
1038 (string-to-int (match-string 1 result))))))
1039 (setq result (substring result 0 (match-beginning 0))))))
1042 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
1043 "Non-nil means to assume the interface approriate for DG/UX dbx.
1044 This was tested using R4.11.")
1046 ;; There are a couple of differences between DG's dbx output and normal
1047 ;; dbx output which make it nontrivial to integrate this into the
1048 ;; standard dbx-marker-filter (mainly, there are a different number of
1049 ;; backreferences). The markers look like:
1051 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
1053 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1056 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1060 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1062 ;; from up/down/where.
1064 (defun gud-dguxdbx-marker-filter (string)
1065 (setq gud-marker-acc (if gud-marker-acc
1066 (concat gud-marker-acc string)
1068 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1069 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1071 ;; Process all complete markers in this chunk.
1072 (while (string-match re gud-marker-acc start)
1073 (setq gud-last-frame
1074 (cons (match-string 4 gud-marker-acc)
1075 (string-to-int (match-string 3 gud-marker-acc)))
1076 start (match-end 0)))
1078 ;; Search for the last incomplete line in this chunk
1079 (while (string-match "\n" gud-marker-acc start)
1080 (setq start (match-end 0)))
1082 ;; If the incomplete line APPEARS to begin with another marker, keep it
1083 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1084 ;; unnecessary concat during the next call.
1085 (setq gud-marker-acc
1086 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1087 (substring gud-marker-acc (match-beginning 0))
1092 (defun dbx (command-line)
1093 "Run dbx on program FILE in buffer *gud-FILE*.
1094 The directory containing FILE becomes the initial working directory
1095 and source-file directory for your debugger."
1096 (interactive (list (gud-query-cmdline 'dbx)))
1100 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1102 (gud-common-init command-line 'gud-dbx-massage-args
1103 'gud-irixdbx-marker-filter))
1105 (gud-common-init command-line 'gud-dbx-massage-args
1106 'gud-dguxdbx-marker-filter))
1108 (gud-common-init command-line 'gud-dbx-massage-args
1109 'gud-dbx-marker-filter)))
1111 (set (make-local-variable 'gud-minor-mode) 'dbx)
1115 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1116 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1117 (gud-def gud-break "stop at \"%f\":%l"
1118 "\C-b" "Set breakpoint at current line.")
1119 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1121 (gud-def gud-break "stop at \"%d%f\":%l"
1122 "\C-b" "Set breakpoint at current line.")
1123 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1124 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1125 "<" "Up (numeric arg) stack frames.")
1126 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1127 ">" "Down (numeric arg) stack frames.")
1128 ;; Make dbx give out the source location info that we need.
1129 (process-send-string (get-buffer-process gud-comint-buffer)
1130 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1132 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1133 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1134 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1135 "\C-b" "Set breakpoint at current line.")
1136 (if gud-dbx-use-stopformat-p
1137 (process-send-string (get-buffer-process gud-comint-buffer)
1138 "set $stopformat=1\n"))))
1140 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1141 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1142 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1143 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1144 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1145 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1146 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1147 (gud-def gud-run "run" nil "Run the program.")
1149 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1150 (setq paragraph-start comint-prompt-regexp)
1151 (run-hooks 'dbx-mode-hook)
1154 ;; ======================================================================
1155 ;; xdb (HP PARISC debugger) functions
1157 ;; History of argument lists passed to xdb.
1158 (defvar gud-xdb-history nil)
1160 (defcustom gud-xdb-directories nil
1161 "*A list of directories that xdb should search for source code.
1162 If nil, only source files in the program directory
1163 will be known to xdb.
1165 The file names should be absolute, or relative to the directory
1166 containing the executable being debugged."
1167 :type '(choice (const :tag "Current Directory" nil)
1172 (defun gud-xdb-massage-args (file args)
1173 (nconc (let ((directories gud-xdb-directories)
1176 (setq result (cons (car directories) (cons "-d" result)))
1177 (setq directories (cdr directories)))
1181 ;; xdb does not print the lines all at once, so we have to accumulate them
1182 (defun gud-xdb-marker-filter (string)
1184 (if (or (string-match comint-prompt-regexp string)
1185 (string-match ".*\012" string))
1186 (setq result (concat gud-marker-acc string)
1188 (setq gud-marker-acc (concat gud-marker-acc string)))
1190 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1192 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1194 (let ((line (string-to-int (match-string 2 result)))
1195 (file (gud-file-name (match-string 1 result))))
1197 (setq gud-last-frame (cons file line))))))
1201 (defun xdb (command-line)
1202 "Run xdb on program FILE in buffer *gud-FILE*.
1203 The directory containing FILE becomes the initial working directory
1204 and source-file directory for your debugger.
1206 You can set the variable 'gud-xdb-directories' to a list of program source
1207 directories if your program contains sources from more than one directory."
1208 (interactive (list (gud-query-cmdline 'xdb)))
1210 (gud-common-init command-line 'gud-xdb-massage-args
1211 'gud-xdb-marker-filter)
1212 (set (make-local-variable 'gud-minor-mode) 'xdb)
1214 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1215 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1216 "Set temporary breakpoint at current line.")
1217 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1218 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1219 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1220 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1221 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1222 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1223 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1224 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1226 (setq comint-prompt-regexp "^>")
1227 (setq paragraph-start comint-prompt-regexp)
1228 (run-hooks 'xdb-mode-hook))
1230 ;; ======================================================================
1233 ;; History of argument lists passed to perldb.
1234 (defvar gud-perldb-history nil)
1236 (defun gud-perldb-massage-args (file args)
1237 "Convert a command line as would be typed normally to run perldb
1238 into one that invokes an Emacs-enabled debugging session.
1239 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1240 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1242 (let* ((new-args nil)
1244 (shift (lambda () (push (pop args) new-args))))
1246 ;; Pass all switches and -e scripts through.
1248 (string-match "^-" (car args))
1249 (not (equal "-" (car args)))
1250 (not (equal "--" (car args))))
1251 (when (equal "-e" (car args))
1252 ;; -e goes with the next arg, so shift one extra.
1254 ;; -e as the last arg is an error in Perl.
1255 (error "No code specified for -e"))
1261 (string-match "^-" (car args)))
1262 (error "Can't use stdin as the script to debug"))
1263 ;; This is the program name.
1266 ;; If -e specified, make sure there is a -- so -emacs is not taken
1268 (if (and args (equal "--" (car args)))
1270 (and seen-e (push "--" new-args)))
1272 (push "-emacs" new-args)
1276 (nreverse new-args)))
1278 ;; There's no guarantee that Emacs will hand the filter the entire
1279 ;; marker at once; it could be broken up across several strings. We
1280 ;; might even receive a big chunk with several markers in it. If we
1281 ;; receive a chunk of text which looks like it might contain the
1282 ;; beginning of a marker, we save it here between calls to the
1284 (defun gud-perldb-marker-filter (string)
1285 (setq gud-marker-acc (concat gud-marker-acc string))
1288 ;; Process all the complete markers in this chunk.
1289 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1293 ;; Extract the frame position from the marker.
1295 (cons (match-string 1 gud-marker-acc)
1296 (string-to-int (match-string 3 gud-marker-acc)))
1298 ;; Append any text before the marker to the output we're going
1299 ;; to return - we don't include the marker in this text.
1300 output (concat output
1301 (substring gud-marker-acc 0 (match-beginning 0)))
1303 ;; Set the accumulator to the remaining text.
1304 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1306 ;; Does the remaining text look like it might end with the
1307 ;; beginning of another marker? If it does, then keep it in
1308 ;; gud-marker-acc until we receive the rest of it. Since we
1309 ;; know the full marker regexp above failed, it's pretty simple to
1310 ;; test for marker starts.
1311 (if (string-match "\032.*\\'" gud-marker-acc)
1313 ;; Everything before the potential marker start can be output.
1314 (setq output (concat output (substring gud-marker-acc
1315 0 (match-beginning 0))))
1317 ;; Everything after, we save, to combine with later input.
1318 (setq gud-marker-acc
1319 (substring gud-marker-acc (match-beginning 0))))
1321 (setq output (concat output gud-marker-acc)
1326 (defcustom gud-perldb-command-name "perl -d"
1327 "Default command to execute a Perl script under debugger."
1332 (defun perldb (command-line)
1333 "Run perldb on program FILE in buffer *gud-FILE*.
1334 The directory containing FILE becomes the initial working directory
1335 and source-file directory for your debugger."
1337 (list (gud-query-cmdline 'perldb
1338 (concat (or (buffer-file-name) "-e 0") " "))))
1340 (gud-common-init command-line 'gud-perldb-massage-args
1341 'gud-perldb-marker-filter)
1342 (set (make-local-variable 'gud-minor-mode) 'perldb)
1344 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1345 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1346 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1347 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1348 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1349 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1350 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1351 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1352 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1353 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1356 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1357 (setq paragraph-start comint-prompt-regexp)
1358 (run-hooks 'perldb-mode-hook))
1360 ;; ======================================================================
1361 ;; pdb (Python debugger) functions
1363 ;; History of argument lists passed to pdb.
1364 (defvar gud-pdb-history nil)
1366 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1367 ;; Either file or function name may be omitted: "> <string>(0)?()"
1368 (defvar gud-pdb-marker-regexp
1369 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1370 (defvar gud-pdb-marker-regexp-file-group 1)
1371 (defvar gud-pdb-marker-regexp-line-group 2)
1372 (defvar gud-pdb-marker-regexp-fnname-group 3)
1374 (defvar gud-pdb-marker-regexp-start "^> ")
1376 ;; There's no guarantee that Emacs will hand the filter the entire
1377 ;; marker at once; it could be broken up across several strings. We
1378 ;; might even receive a big chunk with several markers in it. If we
1379 ;; receive a chunk of text which looks like it might contain the
1380 ;; beginning of a marker, we save it here between calls to the
1382 (defun gud-pdb-marker-filter (string)
1383 (setq gud-marker-acc (concat gud-marker-acc string))
1386 ;; Process all the complete markers in this chunk.
1387 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1390 ;; Extract the frame position from the marker.
1392 (let ((file (match-string gud-pdb-marker-regexp-file-group
1394 (line (string-to-int
1395 (match-string gud-pdb-marker-regexp-line-group
1397 (if (string-equal file "<string>")
1401 ;; Output everything instead of the below
1402 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1403 ;; ;; Append any text before the marker to the output we're going
1404 ;; ;; to return - we don't include the marker in this text.
1405 ;; output (concat output
1406 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1408 ;; Set the accumulator to the remaining text.
1409 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1411 ;; Does the remaining text look like it might end with the
1412 ;; beginning of another marker? If it does, then keep it in
1413 ;; gud-marker-acc until we receive the rest of it. Since we
1414 ;; know the full marker regexp above failed, it's pretty simple to
1415 ;; test for marker starts.
1416 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1418 ;; Everything before the potential marker start can be output.
1419 (setq output (concat output (substring gud-marker-acc
1420 0 (match-beginning 0))))
1422 ;; Everything after, we save, to combine with later input.
1423 (setq gud-marker-acc
1424 (substring gud-marker-acc (match-beginning 0))))
1426 (setq output (concat output gud-marker-acc)
1431 (defcustom gud-pdb-command-name "pdb"
1432 "File name for executing the Python debugger.
1433 This should be an executable on your path, or an absolute file name."
1438 (defun pdb (command-line)
1439 "Run pdb on program FILE in buffer `*gud-FILE*'.
1440 The directory containing FILE becomes the initial working directory
1441 and source-file directory for your debugger."
1443 (list (gud-query-cmdline 'pdb)))
1445 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1446 (set (make-local-variable 'gud-minor-mode) 'pdb)
1448 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1449 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1450 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1451 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1452 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1453 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1454 (gud-def gud-up "up" "<" "Up one stack frame.")
1455 (gud-def gud-down "down" ">" "Down one stack frame.")
1456 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1458 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1460 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1461 (setq comint-prompt-regexp "^(Pdb) *")
1462 (setq paragraph-start comint-prompt-regexp)
1463 (run-hooks 'pdb-mode-hook))
1465 ;; ======================================================================
1469 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1470 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1472 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1473 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1474 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1476 ;; INVOCATION NOTES:
1478 ;; You invoke jdb-mode with:
1482 ;; It responds with:
1484 ;; Run jdb (like this): jdb
1486 ;; type any jdb switches followed by the name of the class you'd like to debug.
1487 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1488 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1489 ;; See the known problems section below for restrictions when specifying jdb
1490 ;; command line switches (search forward for '-classpath').
1492 ;; You should see something like the following:
1494 ;; Current directory is ~/src/java/hello/
1495 ;; Initializing jdb...
1496 ;; 0xed2f6628:class(hello)
1499 ;; To set an initial breakpoint try:
1501 ;; > stop in hello.main
1502 ;; Breakpoint set in hello.main
1505 ;; To execute the program type:
1510 ;; Breakpoint hit: running ...
1511 ;; hello.main (hello:12)
1513 ;; Type M-n to step over the current line and M-s to step into it. That,
1514 ;; along with the JDB 'help' command should get you started. The 'quit'
1515 ;; JDB command will get out out of the debugger. There is some truly
1516 ;; pathetic JDB documentation available at:
1518 ;; http://java.sun.com/products/jdk/1.1/debugging/
1520 ;; KNOWN PROBLEMS AND FIXME's:
1522 ;; Not sure what happens with inner classes ... haven't tried them.
1524 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1526 ;; You must not put whitespace between "-classpath" and the path to
1527 ;; search for java classes even though it is required when invoking jdb
1528 ;; from the command line. See gud-jdb-massage-args for details.
1529 ;; The same applies for "-sourcepath".
1531 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1532 ;; refer to the documentation of `gud-jdb-use-classpath' and
1533 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1534 ;; on using the classpath for locating java source files.
1536 ;; If any of the source files in the directories listed in
1537 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1538 ;; every file ending in ".java" in these directories parses without error.
1540 ;; All the .java files in the directories in gud-jdb-directories are
1541 ;; syntactically analyzed each time gud jdb is invoked. It would be
1542 ;; nice to keep as much information as possible between runs. It would
1543 ;; be really nice to analyze the files only as neccessary (when the
1544 ;; source needs to be displayed.) I'm not sure to what extent the former
1545 ;; can be accomplished and I'm not sure the latter can be done at all
1546 ;; since I don't know of any general way to tell which .class files are
1547 ;; defined by which .java file without analyzing all the .java files.
1548 ;; If anyone knows why JavaSoft didn't put the source file names in
1549 ;; debuggable .class files please clue me in so I find something else
1550 ;; to be spiteful and bitter about.
1552 ;; ======================================================================
1553 ;; gud jdb variables and functions
1555 (defcustom gud-jdb-command-name "jdb"
1556 "Command that executes the Java debugger."
1560 (defcustom gud-jdb-use-classpath t
1561 "If non-nil, search for Java source files in classpath directories.
1562 The list of directories to search is the value of `gud-jdb-classpath'.
1563 The file pathname is obtained by converting the fully qualified
1564 class information output by jdb to a relative pathname and appending
1565 it to `gud-jdb-classpath' element by element until a match is found.
1567 This method has a significant jdb startup time reduction advantage
1568 since it does not require the scanning of all `gud-jdb-directories'
1569 and parsing all Java files for class information.
1571 Set to nil to use `gud-jdb-directories' to scan java sources for
1572 class information on jdb startup (original method)."
1576 (defvar gud-jdb-classpath nil
1577 "Java/jdb classpath directories list.
1578 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1579 list automatically using the following methods in sequence
1580 \(with subsequent successful steps overriding the results of previous
1583 1) Read the CLASSPATH environment variable,
1584 2) Read any \"-classpath\" argument used to run jdb,
1585 or detected in jdb output (e.g. if jdb is run by a script
1586 that echoes the actual jdb command before starting jdb)
1587 3) Send a \"classpath\" command to jdb and scan jdb output for
1588 classpath information if jdb is invoked with an \"-attach\" (to
1589 an already running VM) argument (This case typically does not
1590 have a \"-classpath\" command line argument - that is provided
1591 to the VM when it is started).
1593 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1594 those debuggers do not support the classpath command. Use 1) or 2).")
1596 (defvar gud-jdb-sourcepath nil
1597 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1598 This list is prepended to `gud-jdb-classpath' to form the complete
1599 list of directories searched for source files.")
1601 (defvar gud-marker-acc-max-length 4000
1602 "Maximum number of debugger output characters to keep.
1603 This variable limits the size of `gud-marker-acc' which holds
1604 the most recent debugger output history while searching for
1605 source file information.")
1607 (defvar gud-jdb-history nil
1608 "History of argument lists passed to jdb.")
1611 ;; List of Java source file directories.
1612 (defvar gud-jdb-directories (list ".")
1613 "*A list of directories that gud jdb should search for source code.
1614 The file names should be absolute, or relative to the current
1617 The set of .java files residing in the directories listed are
1618 syntactically analyzed to determine the classes they define and the
1619 packages in which these classes belong. In this way gud jdb maps the
1620 package-qualified class names output by the jdb debugger to the source
1621 file from which the class originated. This allows gud mode to keep
1622 the source code display in sync with the debugging session.")
1624 (defvar gud-jdb-source-files nil
1625 "List of the java source files for this debugging session.")
1627 ;; Association list of fully qualified class names (package + class name)
1628 ;; and their source files.
1629 (defvar gud-jdb-class-source-alist nil
1630 "Association list of fully qualified class names and source files.")
1632 ;; This is used to hold a source file during analysis.
1633 (defvar gud-jdb-analysis-buffer nil)
1635 (defvar gud-jdb-classpath-string nil
1636 "Holds temporary classpath values.")
1638 (defun gud-jdb-build-source-files-list (path extn)
1639 "Return a list of java source files (absolute paths).
1640 PATH gives the directories in which to search for files with
1641 extension EXTN. Normally EXTN is given as the regular expression
1643 (apply 'nconc (mapcar (lambda (d)
1644 (when (file-directory-p d)
1645 (directory-files d t extn nil)))
1648 ;; Move point past whitespace.
1649 (defun gud-jdb-skip-whitespace ()
1650 (skip-chars-forward " \n\r\t\014"))
1652 ;; Move point past a "// <eol>" type of comment.
1653 (defun gud-jdb-skip-single-line-comment ()
1656 ;; Move point past a "/* */" or "/** */" type of comment.
1657 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1661 (if (eq (following-char) ?*)
1665 (if (eq (following-char) ?/)
1668 (throw 'break nil)))))
1671 ;; Move point past any number of consecutive whitespace chars and/or comments.
1672 (defun gud-jdb-skip-whitespace-and-comments ()
1673 (gud-jdb-skip-whitespace)
1678 (gud-jdb-skip-single-line-comment)
1679 (gud-jdb-skip-whitespace))
1680 ((looking-at "/\\*")
1681 (gud-jdb-skip-traditional-or-documentation-comment)
1682 (gud-jdb-skip-whitespace))
1683 (t (throw 'done nil))))))
1685 ;; Move point past things that are id-like. The intent is to skip regular
1686 ;; id's, such as class or interface names as well as package and interface
1688 (defun gud-jdb-skip-id-ish-thing ()
1689 (skip-chars-forward "^ /\n\r\t\014,;{"))
1691 ;; Move point past a string literal.
1692 (defun gud-jdb-skip-string-literal ()
1695 ((eq (following-char) ?\\)
1697 ((eq (following-char) ?\042))))
1701 ;; Move point past a character literal.
1702 (defun gud-jdb-skip-character-literal ()
1706 (if (eq (following-char) ?\\)
1708 (not (eq (following-char) ?\')))
1712 ;; Move point past the following block. There may be (legal) cruft before
1713 ;; the block's opening brace. There must be a block or it's the end of life
1714 ;; in petticoat junction.
1715 (defun gud-jdb-skip-block ()
1717 ;; Find the begining of the block.
1719 (not (eq (following-char) ?{))
1721 ;; Skip any constructs that can harbor literal block delimiter
1722 ;; characters and/or the delimiters for the constructs themselves.
1725 (gud-jdb-skip-single-line-comment))
1726 ((looking-at "/\\*")
1727 (gud-jdb-skip-traditional-or-documentation-comment))
1728 ((eq (following-char) ?\042)
1729 (gud-jdb-skip-string-literal))
1730 ((eq (following-char) ?\')
1731 (gud-jdb-skip-character-literal))
1732 (t (forward-char))))
1734 ;; Now at the begining of the block.
1737 ;; Skip over the body of the block as well as the final brace.
1738 (let ((open-level 1))
1739 (while (not (eq open-level 0))
1742 (gud-jdb-skip-single-line-comment))
1743 ((looking-at "/\\*")
1744 (gud-jdb-skip-traditional-or-documentation-comment))
1745 ((eq (following-char) ?\042)
1746 (gud-jdb-skip-string-literal))
1747 ((eq (following-char) ?\')
1748 (gud-jdb-skip-character-literal))
1749 ((eq (following-char) ?{)
1750 (setq open-level (+ open-level 1))
1752 ((eq (following-char) ?})
1753 (setq open-level (- open-level 1))
1755 (t (forward-char))))))
1757 ;; Find the package and class definitions in Java source file FILE. Assumes
1758 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1759 ;; to hold the source during analysis.
1760 (defun gud-jdb-analyze-source (buf file)
1763 (insert-file-contents file nil nil nil t)
1768 (gud-jdb-skip-whitespace)
1772 ;; Any number of semi's following a block is legal. Move point
1773 ;; past them. Note that comments and whitespace may be
1774 ;; interspersed as well.
1775 ((eq (following-char) ?\073)
1778 ;; Move point past a single line comment.
1780 (gud-jdb-skip-single-line-comment))
1782 ;; Move point past a traditional or documentation comment.
1783 ((looking-at "/\\*")
1784 (gud-jdb-skip-traditional-or-documentation-comment))
1786 ;; Move point past a package statement, but save the PackageName.
1787 ((looking-at "package")
1789 (gud-jdb-skip-whitespace-and-comments)
1791 (gud-jdb-skip-id-ish-thing)
1792 (setq p (concat (buffer-substring s (point)) "."))
1793 (gud-jdb-skip-whitespace-and-comments)
1794 (if (eq (following-char) ?\073)
1797 ;; Move point past an import statement.
1798 ((looking-at "import")
1800 (gud-jdb-skip-whitespace-and-comments)
1801 (gud-jdb-skip-id-ish-thing)
1802 (gud-jdb-skip-whitespace-and-comments)
1803 (if (eq (following-char) ?\073)
1806 ;; Move point past the various kinds of ClassModifiers.
1807 ((looking-at "public")
1809 ((looking-at "abstract")
1811 ((looking-at "final")
1814 ;; Move point past a ClassDeclaraction, but save the class
1816 ((looking-at "class")
1818 (gud-jdb-skip-whitespace-and-comments)
1820 (gud-jdb-skip-id-ish-thing)
1822 l (nconc l (list (concat p (buffer-substring s (point)))))))
1823 (gud-jdb-skip-block))
1825 ;; Move point past an interface statement.
1826 ((looking-at "interface")
1828 (gud-jdb-skip-block))
1830 ;; Anything else means the input is invalid.
1832 (message (format "Error parsing file %s." file))
1833 (throw 'abort nil))))))
1836 (defun gud-jdb-build-class-source-alist-for-file (file)
1840 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1842 ;; Return an alist of fully qualified classes and the source files
1843 ;; holding their definitions. SOURCES holds a list of all the source
1844 ;; files to examine.
1845 (defun gud-jdb-build-class-source-alist (sources)
1846 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1851 'gud-jdb-build-class-source-alist-for-file
1853 (kill-buffer gud-jdb-analysis-buffer)
1854 (setq gud-jdb-analysis-buffer nil)))
1856 ;; Change what was given in the minibuffer to something that can be used to
1857 ;; invoke the debugger.
1858 (defun gud-jdb-massage-args (file args)
1859 ;; The jdb executable must have whitespace between "-classpath" and
1860 ;; its value while gud-common-init expects all switch values to
1861 ;; follow the switch keyword without intervening whitespace. We
1862 ;; require that when the user enters the "-classpath" switch in the
1863 ;; EMACS minibuffer that they do so without the intervening
1864 ;; whitespace. This function adds it back (it's called after
1865 ;; gud-common-init). There are more switches like this (for
1866 ;; instance "-host" and "-password") but I don't care about them
1869 (let (massaged-args user-error)
1871 (while (and args (not user-error))
1873 ((setq user-error (string-match "-classpath$" (car args))))
1874 ((setq user-error (string-match "-sourcepath$" (car args))))
1875 ((string-match "-classpath\\(.+\\)" (car args))
1877 (append massaged-args
1879 (setq gud-jdb-classpath-string
1880 (match-string 1 (car args)))))))
1881 ((string-match "-sourcepath\\(.+\\)" (car args))
1883 (append massaged-args
1885 (setq gud-jdb-sourcepath
1886 (match-string 1 (car args)))))))
1887 (t (setq massaged-args (append massaged-args (list (car args))))))
1888 (setq args (cdr args)))
1890 ;; By this point the current directory is all screwed up. Maybe we
1891 ;; could fix things and re-invoke gud-common-init, but for now I think
1892 ;; issueing the error is good enough.
1895 (kill-buffer (current-buffer))
1896 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
1899 ;; Search for an association with P, a fully qualified class name, in
1900 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1901 ;; qualified file name of the source file which produced the class.
1902 (defun gud-jdb-find-source-file (p)
1903 (cdr (assoc p gud-jdb-class-source-alist)))
1905 ;; Note: Reset to this value every time a prompt is seen
1906 (defvar gud-jdb-lowest-stack-level 999)
1908 (defun gud-jdb-find-source-using-classpath (p)
1909 "Find source file corresponding to fully qualified class p.
1910 Convert p from jdb's output, converted to a pathname
1911 relative to a classpath directory."
1914 (;; Replace dots with slashes and append ".java" to generate file
1915 ;; name relative to classpath
1918 (mapconcat 'identity
1920 ;; Eliminate any subclass references in the class
1921 ;; name string. These start with a "$"
1923 (if (string-match "$.*" x)
1924 (replace-match "" t t x) p))
1928 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
1931 (not (setq found-file
1933 (concat (car cplist) "/" filename)))))
1934 (setq cplist (cdr cplist)))
1935 (if found-file (concat (car cplist) "/" filename)))))
1937 (defun gud-jdb-find-source (string)
1938 "Alias for function used to locate source files.
1939 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
1940 during jdb initialization depending on the value of
1941 `gud-jdb-use-classpath'."
1944 (defun gud-jdb-parse-classpath-string (string)
1945 "Parse the classpath list and convert each item to an absolute pathname."
1946 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
1947 (replace-match "" nil nil s) s))
1948 (mapcar 'file-truename
1951 (concat "[ \t\n\r,\"" path-separator "]+")))))
1953 ;; See comentary for other debugger's marker filters - there you will find
1954 ;; important notes about STRING.
1955 (defun gud-jdb-marker-filter (string)
1957 ;; Build up the accumulator.
1958 (setq gud-marker-acc
1960 (concat gud-marker-acc string)
1963 ;; Look for classpath information until gud-jdb-classpath-string is found
1964 ;; (interactive, multiple settings of classpath from jdb
1965 ;; not supported/followed)
1966 (if (and gud-jdb-use-classpath
1967 (not gud-jdb-classpath-string)
1968 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
1969 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
1970 (setq gud-jdb-classpath
1971 (gud-jdb-parse-classpath-string
1972 (setq gud-jdb-classpath-string
1973 (match-string 1 gud-marker-acc)))))
1975 ;; We process STRING from left to right. Each time through the
1976 ;; following loop we process at most one marker. After we've found a
1977 ;; marker, delete gud-marker-acc up to and including the match
1979 ;; Process each complete marker in the input.
1982 ;; Do we see a marker?
1984 ;; jdb puts out a string of the following form when it
1985 ;; hits a breakpoint:
1987 ;; <fully-qualified-class><method> (<class>:<line-number>)
1989 ;; <fully-qualified-class>'s are composed of Java ID's
1990 ;; separated by periods. <method> and <class> are
1991 ;; also Java ID's. <method> begins with a period and
1992 ;; may contain less-than and greater-than (constructors,
1993 ;; for instance, are called <init> in the symbol table.)
1994 ;; Java ID's begin with a letter followed by letters
1995 ;; and/or digits. The set of letters includes underscore
1998 ;; The first group matches <fully-qualified-class>,
1999 ;; the second group matches <class> and the third group
2000 ;; matches <line-number>. We don't care about using
2001 ;; <method> so we don't "group" it.
2003 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
2006 ;; The ".," in the last square-bracket are necessary because
2007 ;; of Sun's total disrespect for backwards compatibility in
2008 ;; reported line numbers from jdb - starting in 1.4.0 they
2009 ;; print line numbers using LOCALE, inserting a comma or a
2010 ;; period at the thousands positions (how ingenious!).
2012 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
2013 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9.,]+\\)"
2016 ;; A good marker is one that:
2017 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
2018 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
2019 ;; since the last prompt
2020 ;; Figure out the line on which to position the debugging arrow.
2021 ;; Return the info as a cons of the form:
2023 ;; (<file-name> . <line-number>) .
2024 (if (if (match-beginning 1)
2026 (setq n (string-to-int (substring
2028 (1+ (match-beginning 1))
2029 (- (match-end 1) 2))))
2030 (if (< n gud-jdb-lowest-stack-level)
2031 (progn (setq gud-jdb-lowest-stack-level n) t)))
2033 (if (setq file-found
2034 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
2035 (setq gud-last-frame
2039 ((numstr (match-string 4 gud-marker-acc)))
2040 (if (string-match "[.,]" numstr)
2041 (replace-match "" nil nil numstr)
2043 (message "Could not find source file.")))
2045 ;; Set the accumulator to the remaining text.
2046 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
2048 (if (string-match comint-prompt-regexp gud-marker-acc)
2049 (setq gud-jdb-lowest-stack-level 999)))
2051 ;; Do not allow gud-marker-acc to grow without bound. If the source
2052 ;; file information is not within the last 3/4
2053 ;; gud-marker-acc-max-length characters, well,...
2054 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2055 (setq gud-marker-acc
2056 (substring gud-marker-acc
2057 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2059 ;; We don't filter any debugger output so just return what we were given.
2062 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2065 (defun jdb (command-line)
2066 "Run jdb with command line COMMAND-LINE in a buffer.
2067 The buffer is named \"*gud*\" if no initial class is given or
2068 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2069 switch is given, omit all whitespace between it and its value.
2071 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2072 information on how jdb accesses source files. Alternatively (if
2073 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2074 original source file access method.
2076 For general information about commands available to control jdb from
2077 gud, see `gud-mode'."
2079 (list (gud-query-cmdline 'jdb)))
2080 (setq gud-jdb-classpath nil)
2081 (setq gud-jdb-sourcepath nil)
2083 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2084 ;; if CLASSPATH is set.
2085 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
2086 (if gud-jdb-classpath-string
2087 (setq gud-jdb-classpath
2088 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2089 (setq gud-jdb-classpath-string nil) ; prepare for next
2091 (gud-common-init command-line 'gud-jdb-massage-args
2092 'gud-jdb-marker-filter)
2093 (set (make-local-variable 'gud-minor-mode) 'jdb)
2095 ;; If a -classpath option was provided, set gud-jdb-classpath
2096 (if gud-jdb-classpath-string
2097 (setq gud-jdb-classpath
2098 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2099 (setq gud-jdb-classpath-string nil) ; prepare for next
2100 ;; If a -sourcepath option was provided, parse it
2101 (if gud-jdb-sourcepath
2102 (setq gud-jdb-sourcepath
2103 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2105 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2106 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2107 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2108 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2109 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2110 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2111 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2112 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2113 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2115 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2116 (setq paragraph-start comint-prompt-regexp)
2117 (run-hooks 'jdb-mode-hook)
2119 (if gud-jdb-use-classpath
2120 ;; Get the classpath information from the debugger
2122 (if (string-match "-attach" command-line)
2123 (gud-call "classpath"))
2124 (fset 'gud-jdb-find-source
2125 'gud-jdb-find-source-using-classpath))
2127 ;; Else create and bind the class/source association list as well
2128 ;; as the source file list.
2129 (setq gud-jdb-class-source-alist
2130 (gud-jdb-build-class-source-alist
2131 (setq gud-jdb-source-files
2132 (gud-jdb-build-source-files-list gud-jdb-directories
2134 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2137 ;; ======================================================================
2139 ;; BASHDB support. See http://bashdb.sourceforge.net
2141 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2143 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2145 ;; INVOCATION NOTES:
2147 ;; You invoke bashdb-mode with:
2149 ;; M-x bashdb <enter>
2151 ;; It responds with:
2153 ;; Run bashdb (like this): bash
2156 ;; History of argument lists passed to bashdb.
2157 (defvar gud-bashdb-history nil)
2159 ;; Convert a command line as would be typed normally to run a script
2160 ;; into one that invokes an Emacs-enabled debugging session.
2161 ;; "--debugger" in inserted as the first switch.
2163 ;; There's no guarantee that Emacs will hand the filter the entire
2164 ;; marker at once; it could be broken up across several strings. We
2165 ;; might even receive a big chunk with several markers in it. If we
2166 ;; receive a chunk of text which looks like it might contain the
2167 ;; beginning of a marker, we save it here between calls to the
2169 (defun gud-bashdb-marker-filter (string)
2170 (setq gud-marker-acc (concat gud-marker-acc string))
2173 ;; Process all the complete markers in this chunk.
2174 ;; Format of line looks like this:
2175 ;; (/etc/init.d/ntp.init:16):
2176 ;; but we also allow DOS drive letters
2177 ;; (d:/etc/init.d/ntp.init:16):
2178 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2182 ;; Extract the frame position from the marker.
2184 (cons (match-string 2 gud-marker-acc)
2185 (string-to-int (match-string 4 gud-marker-acc)))
2187 ;; Append any text before the marker to the output we're going
2188 ;; to return - we don't include the marker in this text.
2189 output (concat output
2190 (substring gud-marker-acc 0 (match-beginning 0)))
2192 ;; Set the accumulator to the remaining text.
2193 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2195 ;; Does the remaining text look like it might end with the
2196 ;; beginning of another marker? If it does, then keep it in
2197 ;; gud-marker-acc until we receive the rest of it. Since we
2198 ;; know the full marker regexp above failed, it's pretty simple to
2199 ;; test for marker starts.
2200 (if (string-match "\032.*\\'" gud-marker-acc)
2202 ;; Everything before the potential marker start can be output.
2203 (setq output (concat output (substring gud-marker-acc
2204 0 (match-beginning 0))))
2206 ;; Everything after, we save, to combine with later input.
2207 (setq gud-marker-acc
2208 (substring gud-marker-acc (match-beginning 0))))
2210 (setq output (concat output gud-marker-acc)
2215 (defcustom gud-bashdb-command-name "bash --debugger"
2216 "File name for executing bash debugger."
2221 (defun bashdb (command-line)
2222 "Run bashdb on program FILE in buffer *gud-FILE*.
2223 The directory containing FILE becomes the initial working directory
2224 and source-file directory for your debugger."
2226 (list (read-from-minibuffer "Run bashdb (like this): "
2227 (if (consp gud-bashdb-history)
2228 (car gud-bashdb-history)
2229 (concat gud-bashdb-command-name
2231 gud-minibuffer-local-map nil
2232 '(gud-bashdb-history . 1))))
2234 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2236 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2238 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2239 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2240 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2241 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2242 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2243 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2244 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2245 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2246 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2247 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2250 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2252 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2253 (setq paragraph-start comint-prompt-regexp)
2254 (run-hooks 'bashdb-mode-hook)
2258 ;; End of debugger-specific information
2262 ;; When we send a command to the debugger via gud-call, it's annoying
2263 ;; to see the command and the new prompt inserted into the debugger's
2264 ;; buffer; we have other ways of knowing the command has completed.
2266 ;; If the buffer looks like this:
2267 ;; --------------------
2268 ;; (gdb) set args foo bar
2270 ;; --------------------
2271 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2272 ;; source file to set a breakpoint, we want the buffer to end up like
2274 ;; --------------------
2275 ;; (gdb) set args foo bar
2276 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2278 ;; --------------------
2279 ;; Essentially, the old prompt is deleted, and the command's output
2280 ;; and the new prompt take its place.
2282 ;; Not echoing the command is easy enough; you send it directly using
2283 ;; process-send-string, and it never enters the buffer. However,
2284 ;; getting rid of the old prompt is trickier; you don't want to do it
2285 ;; when you send the command, since that will result in an annoying
2286 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2287 ;; waits for a response from the debugger, and the new prompt is
2288 ;; inserted. Instead, we'll wait until we actually get some output
2289 ;; from the subprocess before we delete the prompt. If the command
2290 ;; produced no output other than a new prompt, that prompt will most
2291 ;; likely be in the first chunk of output received, so we will delete
2292 ;; the prompt and then replace it with an identical one. If the
2293 ;; command produces output, the prompt is moving anyway, so the
2294 ;; flicker won't be annoying.
2296 ;; So - when we want to delete the prompt upon receipt of the next
2297 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2298 ;; the start of the prompt; the process filter will notice this, and
2299 ;; delete all text between it and the process output marker. If
2300 ;; gud-delete-prompt-marker points nowhere, we leave the current
2302 (defvar gud-delete-prompt-marker nil)
2305 (put 'gud-mode 'mode-class 'special)
2307 (define-derived-mode gud-mode comint-mode "Debugger"
2308 "Major mode for interacting with an inferior debugger process.
2310 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2311 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2312 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2313 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2315 After startup, the following commands are available in both the GUD
2316 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2319 \\[gud-break] sets a breakpoint at the current file and line. In the
2320 GUD buffer, the current file and line are those of the last breakpoint or
2321 step. In a source buffer, they are the buffer's file and current line.
2323 \\[gud-remove] removes breakpoints on the current file and line.
2325 \\[gud-refresh] displays in the source window the last line referred to
2328 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2329 step-one-line (not entering function calls), and step-one-instruction
2330 and then update the source window with the current file and position.
2331 \\[gud-cont] continues execution.
2333 \\[gud-print] tries to find the largest C lvalue or function-call expression
2334 around point, and sends it to the debugger for value display.
2336 The above commands are common to all supported debuggers except xdb which
2337 does not support stepping instructions.
2339 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2340 except that the breakpoint is temporary; that is, it is removed when
2341 execution stops on it.
2343 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2344 frame. \\[gud-down] drops back down through one.
2346 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2347 the current function and stops.
2349 All the keystrokes above are accessible in the GUD buffer
2350 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2352 All pre-defined functions for which the concept make sense repeat
2353 themselves the appropriate number of times if you give a prefix
2356 You may use the `gud-def' macro in the initialization hook to define other
2359 Other commands for interacting with the debugger process are inherited from
2360 comint mode, which see."
2361 (setq mode-line-process '(":%s"))
2362 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2363 (set (make-local-variable 'gud-last-frame) nil)
2364 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2365 (make-local-variable 'comint-prompt-regexp)
2366 ;; Don't put repeated commands in command history many times.
2367 (set (make-local-variable 'comint-input-ignoredups) t)
2368 (make-local-variable 'paragraph-start)
2369 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker))
2370 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook nil t))
2372 ;; Cause our buffers to be displayed, by default,
2373 ;; in the selected window.
2374 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2376 (defcustom gud-chdir-before-run t
2377 "Non-nil if GUD should `cd' to the debugged executable."
2381 (defvar gud-target-name "--unknown--"
2382 "The apparent name of the program being debugged in a gud buffer.")
2384 ;; Perform initializations common to all debuggers.
2385 ;; The first arg is the specified command line,
2386 ;; which starts with the program to debug.
2387 ;; The other three args specify the values to use
2388 ;; for local variables in the debugger buffer.
2389 (defun gud-common-init (command-line massage-args marker-filter
2390 &optional find-file)
2391 (let* ((words (split-string command-line))
2392 (program (car words))
2393 (dir default-directory)
2394 ;; Extract the file name from WORDS
2395 ;; and put t in its place.
2396 ;; Later on we will put the modified file name arg back there.
2397 (file-word (let ((w (cdr words)))
2398 (while (and w (= ?- (aref (car w) 0)))
2404 (and file-word (substitute-in-file-name file-word)))
2406 ;; If a directory was specified, expand the file name.
2407 ;; Otherwise, don't expand it, so GDB can use the PATH.
2408 ;; A file name without directory is literally valid
2409 ;; only if the file exists in ., and in that case,
2410 ;; omitting the expansion here has no visible effect.
2411 (file (and file-word
2412 (if (file-name-directory file-subst)
2413 (expand-file-name file-subst)
2415 (filepart (and file-word (concat "-" (file-name-nondirectory file))))
2416 (existing-buffer (get-buffer (concat "*gud" filepart "*"))))
2417 (pop-to-buffer (concat "*gud" filepart "*"))
2418 (when (and existing-buffer (get-buffer-process existing-buffer))
2419 (error "This program is already running under gdb"))
2420 ;; Set the dir, in case the buffer already existed with a different dir.
2421 (setq default-directory dir)
2422 ;; Set default-directory to the file's directory.
2424 gud-chdir-before-run
2425 ;; Don't set default-directory if no directory was specified.
2426 ;; In that case, either the file is found in the current directory,
2427 ;; in which case this setq is a no-op,
2428 ;; or it is found by searching PATH,
2429 ;; in which case we don't know what directory it was found in.
2430 (file-name-directory file)
2431 (setq default-directory (file-name-directory file)))
2432 (or (bolp) (newline))
2433 (insert "Current directory is " default-directory "\n")
2434 ;; Put the substituted and expanded file name back in its place.
2436 (while (and w (not (eq (car w) t)))
2440 (apply 'make-comint (concat "gud" filepart) program nil
2441 (if massage-args (funcall massage-args file args) args))
2442 ;; Since comint clobbered the mode, we don't set it until now.
2444 (set (make-local-variable 'gud-target-name)
2445 (and file-word (file-name-nondirectory file))))
2446 (set (make-local-variable 'gud-marker-filter) marker-filter)
2447 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2448 (setq gud-running nil)
2449 (setq gud-last-last-frame nil)
2451 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2452 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2455 (defun gud-set-buffer ()
2456 (when (eq major-mode 'gud-mode)
2457 (setq gud-comint-buffer (current-buffer))))
2459 (defvar gud-filter-defer-flag nil
2460 "Non-nil means don't process anything from the debugger right now.
2461 It is saved for when this flag is not set.")
2463 ;; These functions are responsible for inserting output from your debugger
2464 ;; into the buffer. The hard work is done by the method that is
2465 ;; the value of gud-marker-filter.
2467 (defun gud-filter (proc string)
2468 ;; Here's where the actual buffer insertion is done
2469 (let (output process-window)
2470 (if (buffer-name (process-buffer proc))
2471 (if gud-filter-defer-flag
2472 ;; If we can't process any text now,
2473 ;; save it for later.
2474 (setq gud-filter-pending-text
2475 (concat (or gud-filter-pending-text "") string))
2477 ;; If we have to ask a question during the processing,
2478 ;; defer any additional text that comes from the debugger
2479 ;; during that time.
2480 (let ((gud-filter-defer-flag t))
2481 ;; Process now any text we previously saved up.
2482 (if gud-filter-pending-text
2483 (setq string (concat gud-filter-pending-text string)
2484 gud-filter-pending-text nil))
2486 (with-current-buffer (process-buffer proc)
2487 ;; If we have been so requested, delete the debugger prompt.
2490 (if (marker-buffer gud-delete-prompt-marker)
2492 (delete-region (process-mark proc)
2493 gud-delete-prompt-marker)
2494 (set-marker gud-delete-prompt-marker nil)))
2495 ;; Save the process output, checking for source file markers.
2496 (setq output (gud-marker-filter string))
2497 ;; Check for a filename-and-line number.
2498 ;; Don't display the specified file
2499 ;; unless (1) point is at or after the position where output appears
2500 ;; and (2) this buffer is on the screen.
2501 (setq process-window
2503 (>= (point) (process-mark proc))
2504 (get-buffer-window (current-buffer)))))
2506 ;; Let the comint filter do the actual insertion.
2507 ;; That lets us inherit various comint features.
2508 (comint-output-filter proc output))
2510 ;; Put the arrow on the source line.
2511 ;; This must be outside of the save-excursion
2512 ;; in case the source file is our current buffer.
2514 (save-selected-window
2515 (select-window process-window)
2516 (gud-display-frame))
2517 ;; We have to be in the proper buffer, (process-buffer proc),
2518 ;; but not in a save-excursion, because that would restore point.
2519 (let ((old-buf (current-buffer)))
2520 (set-buffer (process-buffer proc))
2523 (set-buffer old-buf)))))
2525 ;; If we deferred text that arrived during this processing,
2527 (if gud-filter-pending-text
2528 (gud-filter proc ""))))))
2530 (defvar gud-minor-mode-type nil)
2531 (defvar gud-overlay-arrow-position nil)
2532 (add-to-list 'overlay-arrow-variable-list 'gud-overlay-arrow-position)
2534 (defun gud-sentinel (proc msg)
2535 (cond ((null (buffer-name (process-buffer proc)))
2537 ;; Stop displaying an arrow in a source file.
2538 (setq gud-overlay-arrow-position nil)
2539 (set-process-buffer proc nil)
2540 (if (memq gud-minor-mode-type '(gdbmi gdba))
2543 ((memq (process-status proc) '(signal exit))
2544 ;; Stop displaying an arrow in a source file.
2545 (setq gud-overlay-arrow-position nil)
2546 (with-current-buffer gud-comint-buffer
2547 (if (memq gud-minor-mode-type '(gdbmi gdba))
2550 (let* ((obuf (current-buffer)))
2551 ;; save-excursion isn't the right thing if
2552 ;; process-buffer is current-buffer
2555 ;; Write something in *compilation* and hack its mode line,
2556 (set-buffer (process-buffer proc))
2557 ;; Fix the mode line.
2558 (setq mode-line-process
2560 (symbol-name (process-status proc))))
2561 (force-mode-line-update)
2563 (insert ?\n mode-name " " msg)
2565 (goto-char (point-max))
2566 (insert ?\n mode-name " " msg)))
2567 ;; If buffer and mode line will show that the process
2568 ;; is dead, we can delete it now. Otherwise it
2569 ;; will stay around until M-x list-processes.
2570 (delete-process proc))
2571 ;; Restore old buffer, but don't restore old point
2572 ;; if obuf is the gud buffer.
2573 (set-buffer obuf))))))
2575 (defun gud-kill-buffer-hook ()
2576 (setq gud-minor-mode-type gud-minor-mode)
2578 (kill-process (get-buffer-process gud-comint-buffer))
2582 (dolist (buffer (buffer-list))
2583 (unless (eq buffer gud-comint-buffer)
2584 (with-current-buffer buffer
2585 (when gud-minor-mode
2586 (setq gud-minor-mode nil)
2587 (kill-local-variable 'tool-bar-map))))))
2589 (defun gud-display-frame ()
2590 "Find and obey the last filename-and-line marker from the debugger.
2591 Obeying it means displaying in another window the specified file and line."
2593 (when gud-last-frame
2595 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2596 (setq gud-last-last-frame gud-last-frame
2597 gud-last-frame nil)))
2599 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2600 ;; and that its line LINE is visible.
2601 ;; Put the overlay-arrow on the line LINE in that buffer.
2602 ;; Most of the trickiness in here comes from wanting to preserve the current
2603 ;; region-restriction if that's possible. We use an explicit display-buffer
2604 ;; to get around the fact that this is called inside a save-excursion.
2606 (defun gud-display-line (true-file line)
2607 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2609 (with-current-buffer gud-comint-buffer
2610 (gud-find-file true-file)))
2611 (window (and buffer (or (get-buffer-window buffer)
2612 (display-buffer buffer))))
2616 (with-current-buffer buffer
2617 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2619 (format "File %s changed on disk. Reread from disk? "
2622 (setq gud-keep-buffer t)))
2627 (or gud-overlay-arrow-position
2628 (setq gud-overlay-arrow-position (make-marker)))
2629 (set-marker gud-overlay-arrow-position (point) (current-buffer)))
2630 (cond ((or (< pos (point-min)) (> pos (point-max)))
2633 (if window (set-window-point window gud-overlay-arrow-position))))))
2635 ;; The gud-call function must do the right thing whether its invoking
2636 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2637 ;; or a C buffer. In the former case, we want to supply data from
2638 ;; gud-last-frame. Here's how we do it:
2640 (defun gud-format-command (str arg)
2641 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2642 (frame (or gud-last-frame gud-last-last-frame))
2644 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2645 (let ((key (string-to-char (match-string 2 str)))
2649 (setq subst (file-name-nondirectory (if insource
2653 (setq subst (file-name-sans-extension
2654 (file-name-nondirectory (if insource
2658 (setq subst (file-name-directory (if insource
2662 (setq subst (int-to-string
2666 (+ (count-lines (point-min) (point))
2670 (setq subst (gud-find-expr)))
2672 (setq subst (gud-read-address)))
2682 (+ (count-lines (point-min) (point))
2686 (setq subst (if arg (int-to-string arg)))))
2687 (setq result (concat result (match-string 1 str) subst)))
2688 (setq str (substring str (match-end 2))))
2689 ;; There might be text left in STR when the loop ends.
2690 (concat result str)))
2692 (defun gud-read-address ()
2693 "Return a string containing the core-address found in the buffer at point."
2696 (let ((pt (point)) found begin)
2697 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2699 (found (forward-char 2)
2700 (buffer-substring found
2701 (progn (re-search-forward "[^0-9a-f]")
2704 (t (setq begin (progn (re-search-backward "[^0-9]")
2708 (re-search-forward "[^0-9]")
2710 (buffer-substring begin (point))))))))
2712 (defun gud-call (fmt &optional arg)
2713 (let ((msg (gud-format-command fmt arg)))
2714 (message "Command: %s" msg)
2716 (gud-basic-call msg)))
2718 (defun gud-basic-call (command)
2719 "Invoke the debugger COMMAND displaying source in other window."
2722 (let ((proc (get-buffer-process gud-comint-buffer)))
2723 (or proc (error "Current buffer has no process"))
2724 ;; Arrange for the current prompt to get deleted.
2726 (set-buffer gud-comint-buffer)
2729 (goto-char (process-mark proc))
2731 (if (looking-at comint-prompt-regexp)
2732 (set-marker gud-delete-prompt-marker (point)))
2733 (if (memq gud-minor-mode '(gdbmi gdba))
2734 (apply comint-input-sender (list proc command))
2735 (process-send-string proc (concat command "\n")))))))
2737 (defun gud-refresh (&optional arg)
2738 "Fix up a possibly garbled display, and redraw the arrow."
2740 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2744 ;; Code for parsing expressions out of C or Fortran code. The single entry
2745 ;; point is gud-find-expr, which tries to return an lvalue expression from
2748 (defvar gud-find-expr-function 'gud-find-c-expr)
2750 (defun gud-find-expr (&rest args)
2751 (apply gud-find-expr-function args))
2753 ;; The next eight functions are hacked from gdbsrc.el by
2754 ;; Debby Ayers <ayers@asc.slb.com>,
2755 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2757 (defun gud-find-c-expr ()
2758 "Returns the expr that surrounds point."
2762 (expr (gud-innermost-expr))
2763 (test-expr (gud-prev-expr)))
2764 (while (and test-expr (gud-expr-compound test-expr expr))
2765 (let ((prev-expr expr))
2766 (setq expr (cons (car test-expr) (cdr expr)))
2767 (goto-char (car expr))
2768 (setq test-expr (gud-prev-expr))
2769 ;; If we just pasted on the condition of an if or while,
2770 ;; throw it away again.
2771 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2772 '("if" "while" "for"))
2776 (setq test-expr (gud-next-expr))
2777 (while (gud-expr-compound expr test-expr)
2778 (setq expr (cons (car expr) (cdr test-expr)))
2779 (setq test-expr (gud-next-expr)))
2780 (buffer-substring (car expr) (cdr expr)))))
2782 (defun gud-innermost-expr ()
2783 "Returns the smallest expr that point is in; move point to beginning of it.
2784 The expr is represented as a cons cell, where the car specifies the point in
2785 the current buffer that marks the beginning of the expr and the cdr specifies
2786 the character after the end of the expr."
2787 (let ((p (point)) begin end)
2789 (setq begin (point))
2802 (defun gud-backward-sexp ()
2803 "Version of `backward-sexp' that catches errors."
2808 (defun gud-forward-sexp ()
2809 "Version of `forward-sexp' that catches errors."
2814 (defun gud-prev-expr ()
2815 "Returns the previous expr, point is set to beginning of that expr.
2816 The expr is represented as a cons cell, where the car specifies the point in
2817 the current buffer that marks the beginning of the expr and the cdr specifies
2818 the character after the end of the expr"
2819 (let ((begin) (end))
2821 (setq begin (point))
2827 (defun gud-next-expr ()
2828 "Returns the following expr, point is set to beginning of that expr.
2829 The expr is represented as a cons cell, where the car specifies the point in
2830 the current buffer that marks the beginning of the expr and the cdr specifies
2831 the character after the end of the expr."
2832 (let ((begin) (end))
2837 (setq begin (point))
2840 (defun gud-expr-compound-sep (span-start span-end)
2841 "Scan from SPAN-START to SPAN-END for punctuation characters.
2842 If `->' is found, return `?.'. If `.' is found, return `?.'.
2843 If any other punctuation is found, return `??'.
2844 If no punctuation is found, return `? '."
2847 (while (< span-start span-end)
2848 (setq syntax (char-syntax (char-after span-start)))
2851 ((= syntax ?.) (setq syntax (char-after span-start))
2853 ((= syntax ?.) (setq result ?.))
2854 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2856 (setq span-start (+ span-start 1)))
2857 (t (setq span-start span-end)
2858 (setq result ??)))))
2859 (setq span-start (+ span-start 1)))
2862 (defun gud-expr-compound (first second)
2863 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2864 The two exprs are represented as a cons cells, where the car
2865 specifies the point in the current buffer that marks the beginning of the
2866 expr and the cdr specifies the character after the end of the expr.
2867 Link exprs of the form:
2874 (let ((span-start (cdr first))
2875 (span-end (car second))
2877 (setq syntax (gud-expr-compound-sep span-start span-end))
2879 ((= (car first) (car second)) nil)
2880 ((= (cdr first) (cdr second)) nil)
2883 (setq span-start (char-after (- span-start 1)))
2884 (setq span-end (char-after span-end))
2886 ((= span-start ?)) t)
2887 ((= span-start ?]) t)
2893 (defun gud-find-class (f line)
2894 "Find fully qualified class in file F at line LINE.
2895 This function uses the `gud-jdb-classpath' (and optional
2896 `gud-jdb-sourcepath') list(s) to derive a file
2897 pathname relative to its classpath directory. The values in
2898 `gud-jdb-classpath' are assumed to have been converted to absolute
2899 pathname standards using file-truename.
2900 If F is visited by a buffer and its mode is CC-mode(Java),
2901 syntactic information of LINE is used to find the enclosing (nested)
2902 class string which is appended to the top level
2903 class of the file (using s to separate nested class ids)."
2904 ;; Convert f to a standard representation and remove suffix
2905 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
2907 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2908 (fbuffer (get-file-buffer f))
2909 syntax-symbol syntax-point class-found)
2910 (setq f (file-name-sans-extension (file-truename f)))
2911 ;; Syntax-symbol returns the symbol of the *first* element
2912 ;; in the syntactical analysis result list, syntax-point
2913 ;; returns the buffer position of same
2914 (fset 'syntax-symbol (lambda (x) (c-langelem-sym (car x))))
2915 (fset 'syntax-point (lambda (x) (c-langelem-pos (car x))))
2916 ;; Search through classpath list for an entry that is
2918 (while (and cplist (not class-found))
2919 (if (string-match (car cplist) f)
2921 (mapconcat 'identity
2923 (substring f (+ (match-end 0) 1))
2925 (setq cplist (cdr cplist)))
2926 ;; if f is visited by a java(cc-mode) buffer, walk up the
2927 ;; syntactic information chain and collect any 'inclass
2928 ;; symbols until 'topmost-intro is reached to find out if
2929 ;; point is within a nested class
2930 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
2932 (set-buffer fbuffer)
2933 (let ((nclass) (syntax))
2934 ;; While the c-syntactic information does not start
2935 ;; with the 'topmost-intro symbol, there may be
2936 ;; nested classes...
2937 (while (not (eq 'topmost-intro
2938 (syntax-symbol (c-guess-basic-syntax))))
2939 ;; Check if the current position c-syntactic
2940 ;; analysis has 'inclass
2941 (setq syntax (c-guess-basic-syntax))
2943 (and (not (eq 'inclass (syntax-symbol syntax)))
2945 (setq syntax (cdr syntax)))
2946 (if (eq 'inclass (syntax-symbol syntax))
2948 (goto-char (syntax-point syntax))
2949 ;; Now we're at the beginning of a class
2950 ;; definition. Find class name
2952 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
2954 (append (list (match-string-no-properties 1))
2956 (setq syntax (c-guess-basic-syntax))
2957 (while (and (not (syntax-point syntax)) (cdr syntax))
2958 (setq syntax (cdr syntax)))
2959 (goto-char (syntax-point syntax))
2961 (string-match (concat (car nclass) "$") class-found)
2963 (replace-match (mapconcat 'identity nclass "$")
2964 t t class-found)))))
2965 (if (not class-found)
2966 (message "gud-find-class: class for file %s not found!" f))
2968 ;; Not using classpath - try class/source association list
2969 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
2972 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
2975 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2976 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2977 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2979 (defvar gdb-script-mode-syntax-table
2980 (let ((st (make-syntax-table)))
2981 (modify-syntax-entry ?' "\"" st)
2982 (modify-syntax-entry ?# "<" st)
2983 (modify-syntax-entry ?\n ">" st)
2986 (defvar gdb-script-font-lock-keywords
2987 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
2988 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
2989 ("^\\s-*\\([a-z]+\\)" (1 font-lock-keyword-face))))
2991 (defvar gdb-script-font-lock-syntactic-keywords
2992 '(("^document\\s-.*\\(\n\\)" (1 "< b"))
2993 ;; It would be best to change the \n in front, but it's more difficult.
2994 ("^en\\(d\\)\\>" (1 "> b"))))
2996 (defun gdb-script-font-lock-syntactic-face (state)
2998 ((nth 3 state) font-lock-string-face)
2999 ((nth 7 state) font-lock-doc-face)
3000 (t font-lock-comment-face)))
3002 (defvar gdb-script-basic-indent 2)
3004 (defun gdb-script-skip-to-head ()
3005 "We're just in front of an `end' and we need to go to its head."
3006 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\)\\>" nil 'move)
3008 (gdb-script-skip-to-head)))
3010 (defun gdb-script-calculate-indentation ()
3012 ((looking-at "end\\>")
3013 (gdb-script-skip-to-head)
3014 (current-indentation))
3015 ((looking-at "else\\>")
3016 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
3018 (gdb-script-skip-to-head))
3019 (current-indentation))
3021 (forward-comment (- (point-max)))
3023 (skip-chars-forward " \t")
3024 (+ (current-indentation)
3025 (if (looking-at "\\(if\\|while\\|define\\|else\\)\\>")
3026 gdb-script-basic-indent 0)))))
3028 (defun gdb-script-indent-line ()
3029 "Indent current line of GDB script."
3031 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
3034 (skip-chars-forward " \t")
3035 (not (looking-at "end\\>"))))
3037 (let* ((savep (point))
3038 (indent (condition-case nil
3041 (skip-chars-forward " \t")
3042 (if (>= (point) savep) (setq savep nil))
3043 (max (gdb-script-calculate-indentation) 0))
3046 (save-excursion (indent-line-to indent))
3047 (indent-line-to indent)))))
3049 ;; Derived from cfengine.el.
3050 (defun gdb-script-beginning-of-defun ()
3051 "`beginning-of-defun' function for Gdb script mode.
3052 Treats actions as defuns."
3053 (unless (<= (current-column) (current-indentation))
3055 (if (re-search-backward "^define \\|^document " nil t)
3057 (goto-char (point-min)))
3060 ;; Derived from cfengine.el.
3061 (defun gdb-script-end-of-defun ()
3062 "`end-of-defun' function for Gdb script mode.
3063 Treats actions as defuns."
3065 (if (re-search-forward "^end" nil t)
3067 (goto-char (point-max)))
3071 (add-to-list 'auto-mode-alist '("/\\.gdbinit" . gdb-script-mode))
3074 (define-derived-mode gdb-script-mode nil "GDB-Script"
3075 "Major mode for editing GDB scripts"
3076 (set (make-local-variable 'comment-start) "#")
3077 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3078 (set (make-local-variable 'outline-regexp) "[ \t]")
3079 (set (make-local-variable 'imenu-generic-expression)
3080 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3081 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3082 (set (make-local-variable 'beginning-of-defun-function)
3083 #'gdb-script-beginning-of-defun)
3084 (set (make-local-variable 'end-of-defun-function)
3085 #'gdb-script-end-of-defun)
3086 (set (make-local-variable 'font-lock-defaults)
3087 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3088 (font-lock-syntactic-keywords
3089 . gdb-script-font-lock-syntactic-keywords)
3090 (font-lock-syntactic-face-function
3091 . gdb-script-font-lock-syntactic-face))))
3095 ;;; arch-tag: 6d990948-df65-461a-be39-1c7fb83ac4c4
3096 ;;; gud.el ends here