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