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