(rmail-mmdf-delim1, rmail-mmdf-delim2): Doc fixes.
[emacs.git] / lisp / gud.el
blob2842ce09ab14b2687d3bd06e592db101c74f204b
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,2002 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>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38 ;; <ddavies@world.std.com> added support for jdb (Java debugger.)
40 ;;; Code:
42 (require 'comint)
43 (require 'etags)
45 ;; ======================================================================
46 ;; GUD commands must be visible in C buffers visited by GUD
48 (defgroup gud nil
49 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
50 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
51 :group 'unix
52 :group 'tools)
55 (defcustom gud-key-prefix "\C-x\C-a"
56 "Prefix of all GUD commands valid in C buffers."
57 :type 'string
58 :group 'gud)
60 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
61 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
63 (defvar gud-marker-filter nil)
64 (put 'gud-marker-filter 'permanent-local t)
65 (defvar gud-find-file nil)
66 (put 'gud-find-file 'permanent-local t)
68 (defun gud-marker-filter (&rest args)
69 (apply gud-marker-filter args))
71 (defvar gud-minor-mode nil)
72 (put 'gud-minor-mode 'permanent-local t)
74 (defun gud-symbol (sym &optional soft minor-mode)
75 "Return the symbol used for SYM in MINOR-MODE.
76 MINOR-MODE defaults to `gud-minor-mode.
77 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
78 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
79 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
80 (funcall (if soft 'intern-soft 'intern)
81 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
83 (defun gud-val (sym &optional minor-mode)
84 "Return the value of `gud-symbol' SYM. Default to nil."
85 (let ((sym (gud-symbol sym t minor-mode)))
86 (if (boundp sym) (symbol-value sym))))
88 (defvar gud-running nil
89 "Non-nil if debuggee is running.
90 Used to grey out relevant toolbar icons.")
92 (easy-mmode-defmap gud-menu-map
93 '(([refresh] "Refresh" . gud-refresh)
94 ([run] menu-item "Run" gud-run
95 :enable (and (not gud-running)
96 (memq gud-minor-mode '(gdba gdb jdb))))
97 ([goto] menu-item "Continue to selection" gud-goto
98 :enable (and (not gud-running)
99 (memq gud-minor-mode '(gdba gdb))))
100 ([remove] menu-item "Remove Breakpoint" gud-remove
101 :enable (not gud-running))
102 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
103 :enable (memq gud-minor-mode '(gdba gdb sdb xdb bashdb)))
104 ([break] menu-item "Set Breakpoint" gud-break
105 :enable (not gud-running))
106 ([up] menu-item "Up Stack" gud-up
107 :enable (and (not gud-running)
108 (memq gud-minor-mode
109 '(gdba gdb dbx xdb jdb pdb bashdb))))
110 ([down] menu-item "Down Stack" gud-down
111 :enable (and (not gud-running)
112 (memq gud-minor-mode
113 '(gdba gdb dbx xdb jdb pdb bashdb))))
114 ([print] menu-item "Print Expression" gud-print
115 :enable (not gud-running))
116 ([display] menu-item "Display Expression" gud-display
117 :enable (and (not gud-running)
118 (eq gud-minor-mode 'gdba)))
119 ([finish] menu-item "Finish Function" gud-finish
120 :enable (and (not gud-running)
121 (memq gud-minor-mode
122 '(gdba gdb xdb jdb pdb bashdb))))
123 ([stepi] "Step Instruction" . gud-stepi)
124 ([step] menu-item "Step Line" gud-step
125 :enable (not gud-running))
126 ([next] menu-item "Next Line" gud-next
127 :enable (not gud-running))
128 ([cont] menu-item "Continue" gud-cont
129 :enable (not gud-running)))
130 "Menu for `gud-mode'."
131 :name "Gud")
133 (easy-mmode-defmap gud-minor-mode-map
134 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
135 "Map used in visited files.")
137 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
138 (if m (setcdr m gud-minor-mode-map)
139 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
141 (defvar gud-mode-map
142 ;; Will inherit from comint-mode via define-derived-mode.
143 (make-sparse-keymap)
144 "`gud-mode' keymap.")
146 (defvar gud-tool-bar-map
147 (if (display-graphic-p)
148 (let ((map (make-sparse-keymap)))
149 (dolist (x '((gud-break . "gud-break")
150 (gud-remove . "gud-remove")
151 (gud-print . "gud-print")
152 (gud-display . "gud-display")
153 (gud-run . "gud-run")
154 (gud-goto . "gud-goto")
155 (gud-cont . "gud-cont")
156 (gud-step . "gud-step")
157 (gud-next . "gud-next")
158 (gud-finish . "gud-finish")
159 (gud-up . "gud-up")
160 (gud-down . "gud-down"))
161 map)
162 (tool-bar-local-item-from-menu
163 (car x) (cdr x) map gud-minor-mode-map)))))
165 (defun gud-file-name (f)
166 "Transform a relative file name to an absolute file name.
167 Uses `gud-<MINOR-MODE>-directories' to find the source files."
168 (if (file-exists-p f) (expand-file-name f)
169 (let ((directories (gud-val 'directories))
170 (result nil))
171 (while directories
172 (let ((path (expand-file-name f (car directories))))
173 (if (file-exists-p path)
174 (setq result path
175 directories nil)))
176 (setq directories (cdr directories)))
177 result)))
179 (defun gud-find-file (file)
180 ;; Don't get confused by double slashes in the name that comes from GDB.
181 (while (string-match "//+" file)
182 (setq file (replace-match "/" t t file)))
183 (let ((minor-mode gud-minor-mode)
184 (buf (funcall (or gud-find-file 'gud-file-name) file)))
185 (when (stringp buf)
186 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
187 (when buf
188 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
189 (with-current-buffer buf
190 (set (make-local-variable 'gud-minor-mode) minor-mode)
191 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map))
192 buf)))
194 ;; ======================================================================
195 ;; command definition
197 ;; This macro is used below to define some basic debugger interface commands.
198 ;; Of course you may use `gud-def' with any other debugger command, including
199 ;; user defined ones.
201 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
202 ;; which defines FUNC to send the command NAME to the debugger, gives
203 ;; it the docstring DOC, and binds that function to KEY in the GUD
204 ;; major mode. The function is also bound in the global keymap with the
205 ;; GUD prefix.
207 (defmacro gud-def (func cmd key &optional doc)
208 "Define FUNC to be a command sending STR and bound to KEY, with
209 optional doc string DOC. Certain %-escapes in the string arguments
210 are interpreted specially if present. These are:
212 %f name (without directory) of current source file.
213 %F name (without directory or extension) of current source file.
214 %d directory of current source file.
215 %l number of current source line
216 %e text of the C lvalue or function-call expression surrounding point.
217 %a text of the hexadecimal address surrounding point
218 %p prefix argument to the command (if any) as a number
220 The `current' source file is the file of the current buffer (if
221 we're in a C file) or the source file current at the last break or
222 step (if we're in the GUD buffer).
223 The `current' line is that of the current buffer (if we're in a
224 source file) or the source line number at the last break or step (if
225 we're in the GUD buffer)."
226 `(progn
227 (defun ,func (arg)
228 ,@(if doc (list doc))
229 (interactive "p")
230 ,(if (stringp cmd)
231 `(gud-call ,cmd arg)
232 cmd))
233 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
234 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
236 ;; Where gud-display-frame should put the debugging arrow; a cons of
237 ;; (filename . line-number). This is set by the marker-filter, which scans
238 ;; the debugger's output for indications of the current program counter.
239 (defvar gud-last-frame nil)
241 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
242 ;; the last frame, even if it's been called before and gud-last-frame has
243 ;; been set to nil.
244 (defvar gud-last-last-frame nil)
246 ;; All debugger-specific information is collected here.
247 ;; Here's how it works, in case you ever need to add a debugger to the mode.
249 ;; Each entry must define the following at startup:
251 ;;<name>
252 ;; comint-prompt-regexp
253 ;; gud-<name>-massage-args
254 ;; gud-<name>-marker-filter
255 ;; gud-<name>-find-file
257 ;; The job of the massage-args method is to modify the given list of
258 ;; debugger arguments before running the debugger.
260 ;; The job of the marker-filter method is to detect file/line markers in
261 ;; strings and set the global gud-last-frame to indicate what display
262 ;; action (if any) should be triggered by the marker. Note that only
263 ;; whatever the method *returns* is displayed in the buffer; thus, you
264 ;; can filter the debugger's output, interpreting some and passing on
265 ;; the rest.
267 ;; The job of the find-file method is to visit and return the buffer indicated
268 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
269 ;; something else.
271 ;; ======================================================================
272 ;; speedbar support functions and variables.
273 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
275 (defvar gud-last-speedbar-buffer nil
276 "The last GUD buffer used.")
278 (defvar gud-last-speedbar-stackframe nil
279 "Description of the currently displayed GUD stack.
280 t means that there is no stack, and we are in display-file mode.")
282 (defvar gud-speedbar-key-map nil
283 "Keymap used when in the buffers display mode.")
285 (defun gud-install-speedbar-variables ()
286 "Install those variables used by speedbar to enhance gud/gdb."
287 (if gud-speedbar-key-map
289 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
291 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
292 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
293 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)))
295 (defvar gud-speedbar-menu-items
296 ;; Note to self. Add expand, and turn off items when not available.
297 '(["Jump to stack frame" speedbar-edit-line t])
298 "Additional menu items to add to the speedbar frame.")
300 ;; Make sure our special speedbar mode is loaded
301 (if (featurep 'speedbar)
302 (gud-install-speedbar-variables)
303 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
305 (defun gud-speedbar-buttons (buffer)
306 "Create a speedbar display based on the current state of GUD.
307 If the GUD BUFFER is not running a supported debugger, then turn
308 off the specialized speedbar mode."
309 (if (and (save-excursion (goto-char (point-min))
310 (looking-at "Current Stack"))
311 (equal gud-last-last-frame gud-last-speedbar-stackframe))
313 (setq gud-last-speedbar-buffer buffer)
314 (let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
315 (frames
316 (cond ((memq minor-mode '(gdba gdb))
317 (gud-gdb-get-stackframe buffer))
318 ;; Add more debuggers here!
320 (speedbar-remove-localized-speedbar-support buffer)
321 nil))))
322 (erase-buffer)
323 (if (not frames)
324 (insert "No Stack frames\n")
325 (insert "Current Stack:\n"))
326 (while frames
327 (insert (nth 1 (car frames)) ":\n")
328 (if (= (length (car frames)) 2)
329 (progn
330 ; (speedbar-insert-button "[?]"
331 ; 'speedbar-button-face
332 ; nil nil nil t)
333 (speedbar-insert-button (car (car frames))
334 'speedbar-directory-face
335 nil nil nil t))
336 ; (speedbar-insert-button "[+]"
337 ; 'speedbar-button-face
338 ; 'speedbar-highlight-face
339 ; 'gud-gdb-get-scope-data
340 ; (car frames) t)
341 (speedbar-insert-button (car (car frames))
342 'speedbar-file-face
343 'speedbar-highlight-face
344 (cond ((memq minor-mode '(gdba gdb))
345 'gud-gdb-goto-stackframe)
346 (t (error "Should never be here")))
347 (car frames) t))
348 (setq frames (cdr frames)))
349 ; (let ((selected-frame
350 ; (cond ((eq ff 'gud-gdb-find-file)
351 ; (gud-gdb-selected-frame-info buffer))
352 ; (t (error "Should never be here"))))))
354 (setq gud-last-speedbar-stackframe gud-last-last-frame)))
357 ;; ======================================================================
358 ;; gdb functions
360 ;; History of argument lists passed to gdb.
361 (defvar gud-gdb-history nil)
363 (defcustom gud-gdb-command-name "gdb --fullname"
364 "Default command to execute an executable under the GDB debugger."
365 :type 'string
366 :group 'gud)
368 (defvar gud-gdb-marker-regexp
369 ;; This used to use path-separator instead of ":";
370 ;; however, we found that on both Windows 32 and MSDOS
371 ;; a colon is correct here.
372 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
373 "\\([0-9]*\\)" ":" ".*\n"))
375 ;; There's no guarantee that Emacs will hand the filter the entire
376 ;; marker at once; it could be broken up across several strings. We
377 ;; might even receive a big chunk with several markers in it. If we
378 ;; receive a chunk of text which looks like it might contain the
379 ;; beginning of a marker, we save it here between calls to the
380 ;; filter.
381 (defvar gud-marker-acc "")
382 (make-variable-buffer-local 'gud-marker-acc)
384 (defun gud-gdb-marker-filter (string)
385 (setq gud-marker-acc (concat gud-marker-acc string))
386 (let ((output ""))
388 ;; Process all the complete markers in this chunk.
389 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
390 (setq
392 ;; Extract the frame position from the marker.
393 gud-last-frame (cons (match-string 1 gud-marker-acc)
394 (string-to-int (match-string 2 gud-marker-acc)))
396 ;; Append any text before the marker to the output we're going
397 ;; to return - we don't include the marker in this text.
398 output (concat output
399 (substring gud-marker-acc 0 (match-beginning 0)))
401 ;; Set the accumulator to the remaining text.
402 gud-marker-acc (substring gud-marker-acc (match-end 0))))
404 ;; Does the remaining text look like it might end with the
405 ;; beginning of another marker? If it does, then keep it in
406 ;; gud-marker-acc until we receive the rest of it. Since we
407 ;; know the full marker regexp above failed, it's pretty simple to
408 ;; test for marker starts.
409 (if (string-match "\032.*\\'" gud-marker-acc)
410 (progn
411 ;; Everything before the potential marker start can be output.
412 (setq output (concat output (substring gud-marker-acc
413 0 (match-beginning 0))))
415 ;; Everything after, we save, to combine with later input.
416 (setq gud-marker-acc
417 (substring gud-marker-acc (match-beginning 0))))
419 (setq output (concat output gud-marker-acc)
420 gud-marker-acc ""))
422 output))
424 (easy-mmode-defmap gud-minibuffer-local-map
425 '(("\C-i" . comint-dynamic-complete-filename))
426 "Keymap for minibuffer prompting of gud startup command."
427 :inherit minibuffer-local-map)
429 (defun gud-query-cmdline (minor-mode &optional init)
430 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
431 (cmd-name (gud-val 'command-name minor-mode)))
432 (unless (boundp hist-sym) (set hist-sym nil))
433 (read-from-minibuffer
434 (format "Run %s (like this): " minor-mode)
435 (or (car-safe (symbol-value hist-sym))
436 (concat (or cmd-name (symbol-name minor-mode))
438 (or init
439 (let ((file nil))
440 (dolist (f (directory-files default-directory) file)
441 (if (and (file-executable-p f)
442 (not (file-directory-p f))
443 (or (not file)
444 (file-newer-than-file-p f file)))
445 (setq file f)))))))
446 gud-minibuffer-local-map nil
447 hist-sym)))
449 ;;;###autoload
450 (defun gdb (command-line)
451 "Run gdb on program FILE in buffer *gud-FILE*.
452 The directory containing FILE becomes the initial working directory
453 and source-file directory for your debugger."
454 (interactive (list (gud-query-cmdline 'gdb)))
456 (gud-common-init command-line nil 'gud-gdb-marker-filter)
457 (set (make-local-variable 'gud-minor-mode) 'gdb)
459 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
460 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
461 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
462 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
463 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
464 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
465 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
466 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
467 (gud-def gud-jump "tbreak %f:%l\njump %f:%l" "\C-j" "Relocate execution address to line at point in source buffer.")
469 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
470 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
471 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
472 (gud-def gud-goto "until %l" "\C-u" "Continue up to current line.")
473 (gud-def gud-run "run" nil "Run the program.")
475 (local-set-key "\C-i" 'gud-gdb-complete-command)
476 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
477 (setq paragraph-start comint-prompt-regexp)
478 (run-hooks 'gdb-mode-hook)
481 ;; One of the nice features of GDB is its impressive support for
482 ;; context-sensitive command completion. We preserve that feature
483 ;; in the GUD buffer by using a GDB command designed just for Emacs.
485 ;; The completion process filter indicates when it is finished.
486 (defvar gud-gdb-fetch-lines-in-progress)
488 ;; Since output may arrive in fragments we accumulate partials strings here.
489 (defvar gud-gdb-fetch-lines-string)
491 ;; We need to know how much of the completion to chop off.
492 (defvar gud-gdb-fetch-lines-break)
494 ;; The completion list is constructed by the process filter.
495 (defvar gud-gdb-fetched-lines)
497 (defvar gud-comint-buffer nil)
499 (defun gud-gdb-complete-command ()
500 "Perform completion on the GDB command preceding point.
501 This is implemented using the GDB `complete' command which isn't
502 available with older versions of GDB."
503 (interactive)
504 (let* ((end (point))
505 (command (buffer-substring (comint-line-beginning-position) end))
506 (command-word
507 ;; Find the word break. This match will always succeed.
508 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
509 (substring command (match-beginning 2))))
510 (complete-list
511 (gud-gdb-run-command-fetch-lines (concat "complete " command)
512 (current-buffer)
513 ;; From string-match above.
514 (match-beginning 2))))
515 ;; Protect against old versions of GDB.
516 (and complete-list
517 (string-match "^Undefined command: \"complete\"" (car complete-list))
518 (error "This version of GDB doesn't support the `complete' command"))
519 ;; Sort the list like readline.
520 (setq complete-list (sort complete-list (function string-lessp)))
521 ;; Remove duplicates.
522 (let ((first complete-list)
523 (second (cdr complete-list)))
524 (while second
525 (if (string-equal (car first) (car second))
526 (setcdr first (setq second (cdr second)))
527 (setq first second
528 second (cdr second)))))
529 ;; Add a trailing single quote if there is a unique completion
530 ;; and it contains an odd number of unquoted single quotes.
531 (and (= (length complete-list) 1)
532 (let ((str (car complete-list))
533 (pos 0)
534 (count 0))
535 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
536 (setq count (1+ count)
537 pos (match-end 0)))
538 (and (= (mod count 2) 1)
539 (setq complete-list (list (concat str "'"))))))
540 ;; Let comint handle the rest.
541 (comint-dynamic-simple-complete command-word complete-list)))
543 ;; The completion process filter is installed temporarily to slurp the
544 ;; output of GDB up to the next prompt and build the completion list.
545 (defun gud-gdb-fetch-lines-filter (string filter)
546 "Filter used to read the list of lines output by a command.
547 STRING is the output to filter.
548 It is passed through FILTER before we look at it."
549 (setq string (funcall filter string))
550 (setq string (concat gud-gdb-fetch-lines-string string))
551 (while (string-match "\n" string)
552 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
553 gud-gdb-fetched-lines)
554 (setq string (substring string (match-end 0))))
555 (if (string-match comint-prompt-regexp string)
556 (progn
557 (setq gud-gdb-fetch-lines-in-progress nil)
558 string)
559 (progn
560 (setq gud-gdb-fetch-lines-string string)
561 "")))
563 ;; gdb speedbar functions
565 (defun gud-gdb-goto-stackframe (text token indent)
566 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
567 (speedbar-with-attached-buffer
568 (gud-basic-call (concat "frame " (nth 1 token)))
569 (sit-for 1)))
571 (defvar gud-gdb-fetched-stack-frame nil
572 "Stack frames we are fetching from GDB.")
574 ;(defun gud-gdb-get-scope-data (text token indent)
575 ; ;; checkdoc-params: (indent)
576 ; "Fetch data associated with a stack frame, and expand/contract it.
577 ;Data to do this is retrieved from TEXT and TOKEN."
578 ; (let ((args nil) (scope nil))
579 ; (gud-gdb-run-command-fetch-lines "info args")
581 ; (gud-gdb-run-command-fetch-lines "info local")
583 ; ))
585 (defun gud-gdb-get-stackframe (buffer)
586 "Extract the current stack frame out of the GUD GDB BUFFER."
587 (let ((newlst nil)
588 (fetched-stack-frame-list
589 (gud-gdb-run-command-fetch-lines "backtrace" buffer)))
590 (if (and (car fetched-stack-frame-list)
591 (string-match "No stack" (car fetched-stack-frame-list)))
592 ;; Go into some other mode???
594 (dolist (e fetched-stack-frame-list)
595 (let ((name nil) (num nil))
596 (if (not (or
597 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
598 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
599 (if (not (string-match
600 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
602 (setcar newlst
603 (list (nth 0 (car newlst))
604 (nth 1 (car newlst))
605 (match-string 1 e)
606 (match-string 2 e))))
607 (setq num (match-string 1 e)
608 name (match-string 2 e))
609 (setq newlst
610 (cons
611 (if (string-match
612 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
613 (list name num (match-string 1 e)
614 (match-string 2 e))
615 (list name num))
616 newlst)))))
617 (nreverse newlst))))
619 ;(defun gud-gdb-selected-frame-info (buffer)
620 ; "Learn GDB information for the currently selected stack frame in BUFFER."
623 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
624 "Run COMMAND, and return the list of lines it outputs.
625 BUFFER is the GUD buffer in which to run the command.
626 SKIP is the number of chars to skip on each lines, it defaults to 0."
627 (save-excursion
628 (set-buffer buffer)
629 (if (save-excursion
630 (goto-char (point-max))
631 (forward-line 0)
632 (not (looking-at comint-prompt-regexp)))
634 ;; Much of this copied from GDB complete, but I'm grabbing the stack
635 ;; frame instead.
636 (let ((gud-gdb-fetch-lines-in-progress t)
637 (gud-gdb-fetched-lines nil)
638 (gud-gdb-fetch-lines-string nil)
639 (gud-gdb-fetch-lines-break (or skip 0))
640 (gud-marker-filter
641 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
642 ;; Issue the command to GDB.
643 (gud-basic-call command)
644 ;; Slurp the output.
645 (while gud-gdb-fetch-lines-in-progress
646 (accept-process-output (get-buffer-process buffer)))
647 (nreverse gud-gdb-fetched-lines)))))
650 ;; ======================================================================
651 ;; sdb functions
653 ;; History of argument lists passed to sdb.
654 (defvar gud-sdb-history nil)
656 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
657 "If nil, we're on a System V Release 4 and don't need the tags hack.")
659 (defvar gud-sdb-lastfile nil)
661 (defun gud-sdb-marker-filter (string)
662 (setq gud-marker-acc
663 (if gud-marker-acc (concat gud-marker-acc string) string))
664 (let (start)
665 ;; Process all complete markers in this chunk
666 (while
667 (cond
668 ;; System V Release 3.2 uses this format
669 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
670 gud-marker-acc start)
671 (setq gud-last-frame
672 (cons (match-string 3 gud-marker-acc)
673 (string-to-int (match-string 4 gud-marker-acc)))))
674 ;; System V Release 4.0 quite often clumps two lines together
675 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
676 gud-marker-acc start)
677 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
678 (setq gud-last-frame
679 (cons gud-sdb-lastfile
680 (string-to-int (match-string 3 gud-marker-acc)))))
681 ;; System V Release 4.0
682 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
683 gud-marker-acc start)
684 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
685 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
686 gud-marker-acc start))
687 (setq gud-last-frame
688 (cons gud-sdb-lastfile
689 (string-to-int (match-string 1 gud-marker-acc)))))
691 (setq gud-sdb-lastfile nil)))
692 (setq start (match-end 0)))
694 ;; Search for the last incomplete line in this chunk
695 (while (string-match "\n" gud-marker-acc start)
696 (setq start (match-end 0)))
698 ;; If we have an incomplete line, store it in gud-marker-acc.
699 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
700 string)
702 (defun gud-sdb-find-file (f)
703 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
705 ;;;###autoload
706 (defun sdb (command-line)
707 "Run sdb on program FILE in buffer *gud-FILE*.
708 The directory containing FILE becomes the initial working directory
709 and source-file directory for your debugger."
710 (interactive (list (gud-query-cmdline 'sdb)))
712 (if (and gud-sdb-needs-tags
713 (not (and (boundp 'tags-file-name)
714 (stringp tags-file-name)
715 (file-exists-p tags-file-name))))
716 (error "The sdb support requires a valid tags table to work"))
718 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
719 (set (make-local-variable 'gud-minor-mode) 'sdb)
721 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
722 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
723 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
724 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
725 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
726 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
727 (gud-def gud-cont "c" "\C-r" "Continue with display.")
728 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
730 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
731 (setq paragraph-start comint-prompt-regexp)
732 (run-hooks 'sdb-mode-hook)
735 ;; ======================================================================
736 ;; dbx functions
738 ;; History of argument lists passed to dbx.
739 (defvar gud-dbx-history nil)
741 (defcustom gud-dbx-directories nil
742 "*A list of directories that dbx should search for source code.
743 If nil, only source files in the program directory
744 will be known to dbx.
746 The file names should be absolute, or relative to the directory
747 containing the executable being debugged."
748 :type '(choice (const :tag "Current Directory" nil)
749 (repeat :value ("")
750 directory))
751 :group 'gud)
753 (defun gud-dbx-massage-args (file args)
754 (nconc (let ((directories gud-dbx-directories)
755 (result nil))
756 (while directories
757 (setq result (cons (car directories) (cons "-I" result)))
758 (setq directories (cdr directories)))
759 (nreverse result))
760 args))
762 (defun gud-dbx-marker-filter (string)
763 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
765 (let (start)
766 ;; Process all complete markers in this chunk.
767 (while (or (string-match
768 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
769 gud-marker-acc start)
770 (string-match
771 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
772 gud-marker-acc start))
773 (setq gud-last-frame
774 (cons (match-string 2 gud-marker-acc)
775 (string-to-int (match-string 1 gud-marker-acc)))
776 start (match-end 0)))
778 ;; Search for the last incomplete line in this chunk
779 (while (string-match "\n" gud-marker-acc start)
780 (setq start (match-end 0)))
782 ;; If the incomplete line APPEARS to begin with another marker, keep it
783 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
784 ;; unnecessary concat during the next call.
785 (setq gud-marker-acc
786 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
787 (substring gud-marker-acc (match-beginning 0))
788 nil)))
789 string)
791 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
792 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
793 (defvar gud-mips-p
794 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
795 ;; We haven't tested gud on this system:
796 (string-match "^mips-[^-]*-riscos" system-configuration)
797 ;; It's documented on OSF/1.3
798 (string-match "^mips-[^-]*-osf1" system-configuration)
799 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
800 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
802 (defvar gud-dbx-command-name
803 (concat "dbx" (if gud-mips-p " -emacs")))
805 ;; This is just like the gdb one except for the regexps since we need to cope
806 ;; with an optional breakpoint number in [] before the ^Z^Z
807 (defun gud-mipsdbx-marker-filter (string)
808 (setq gud-marker-acc (concat gud-marker-acc string))
809 (let ((output ""))
811 ;; Process all the complete markers in this chunk.
812 (while (string-match
813 ;; This is like th gdb marker but with an optional
814 ;; leading break point number like `[1] '
815 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
816 gud-marker-acc)
817 (setq
819 ;; Extract the frame position from the marker.
820 gud-last-frame
821 (cons (match-string 1 gud-marker-acc)
822 (string-to-int (match-string 2 gud-marker-acc)))
824 ;; Append any text before the marker to the output we're going
825 ;; to return - we don't include the marker in this text.
826 output (concat output
827 (substring gud-marker-acc 0 (match-beginning 0)))
829 ;; Set the accumulator to the remaining text.
830 gud-marker-acc (substring gud-marker-acc (match-end 0))))
832 ;; Does the remaining text look like it might end with the
833 ;; beginning of another marker? If it does, then keep it in
834 ;; gud-marker-acc until we receive the rest of it. Since we
835 ;; know the full marker regexp above failed, it's pretty simple to
836 ;; test for marker starts.
837 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
838 (progn
839 ;; Everything before the potential marker start can be output.
840 (setq output (concat output (substring gud-marker-acc
841 0 (match-beginning 0))))
843 ;; Everything after, we save, to combine with later input.
844 (setq gud-marker-acc
845 (substring gud-marker-acc (match-beginning 0))))
847 (setq output (concat output gud-marker-acc)
848 gud-marker-acc ""))
850 output))
852 ;; The dbx in IRIX is a pain. It doesn't print the file name when
853 ;; stopping at a breakpoint (but you do get it from the `up' and
854 ;; `down' commands...). The only way to extract the information seems
855 ;; to be with a `file' command, although the current line number is
856 ;; available in $curline. Thus we have to look for output which
857 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
858 ;; to output the information we want with a combination of the
859 ;; `printf' and `file' commands as a pseudo marker which we can
860 ;; recognise next time through the marker-filter. This would be like
861 ;; the gdb marker but you can't get the file name without a newline...
862 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
863 ;; number rather than a line number etc. Maybe this could be made to
864 ;; work by listing all the breakpoints and picking the one(s) with the
865 ;; correct line number, but life's too short.
866 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
868 (defvar gud-irix-p
869 (and (string-match "^mips-[^-]*-irix" system-configuration)
870 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
871 "Non-nil to assume the interface appropriate for IRIX dbx.
872 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
873 a better solution in 6.1 upwards.")
874 (defvar gud-dbx-use-stopformat-p
875 (string-match "irix[6-9]\\.[1-9]" system-configuration)
876 "Non-nil to use the dbx feature present at least from Irix 6.1
877 whereby $stopformat=1 produces an output format compatiable with
878 `gud-dbx-marker-filter'.")
879 ;; [Irix dbx seems to be a moving target. The dbx output changed
880 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
881 ;; the output from `up' is no longer spotted by gud (and it's probably
882 ;; not distinctive enough to try to match it -- use C-<, C->
883 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
884 ;; `long long'(why?!), so the printf stuff needed changing. The line
885 ;; number was cast to `long' as a compromise between the new `long
886 ;; long' and the original `int'. This is reported not to work in 6.2,
887 ;; so it's changed back to int -- don't make your sources too long.
888 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
889 ;; whereby `set $stopformat=1' reportedly produces output compatible
890 ;; with `gud-dbx-marker-filter', which we prefer.
892 ;; The process filter is also somewhat
893 ;; unreliable, sometimes not spotting the markers; I don't know
894 ;; whether there's anything that can be done about that. It would be
895 ;; much better if SGI could be persuaded to (re?)instate the MIPS
896 ;; -emacs flag for gdb-like output (which ought to be possible as most
897 ;; of the communication I've had over it has been from sgi.com).]
899 ;; this filter is influenced by the xdb one rather than the gdb one
900 (defun gud-irixdbx-marker-filter (string)
901 (let (result (case-fold-search nil))
902 (if (or (string-match comint-prompt-regexp string)
903 (string-match ".*\012" string))
904 (setq result (concat gud-marker-acc string)
905 gud-marker-acc "")
906 (setq gud-marker-acc (concat gud-marker-acc string)))
907 (if result
908 (cond
909 ;; look for breakpoint or signal indication e.g.:
910 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
911 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
912 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
913 ((string-match
914 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
915 result)
916 ;; prod dbx into printing out the line number and file
917 ;; name in a form we can grok as below
918 (process-send-string (get-buffer-process gud-comint-buffer)
919 "printf \"\032\032%1d:\",(int)$curline;file\n"))
920 ;; look for result of, say, "up" e.g.:
921 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
922 ;; (this will also catch one of the lines printed by "where")
923 ((string-match
924 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
925 result)
926 (let ((file (match-string 1 result)))
927 (if (file-exists-p file)
928 (setq gud-last-frame
929 (cons (match-string 1 result)
930 (string-to-int (match-string 2 result))))))
931 result)
932 ((string-match ; kluged-up marker as above
933 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
934 (let ((file (gud-file-name (match-string 2 result))))
935 (if (and file (file-exists-p file))
936 (setq gud-last-frame
937 (cons file
938 (string-to-int (match-string 1 result))))))
939 (setq result (substring result 0 (match-beginning 0))))))
940 (or result "")))
942 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
943 "Non-nil means to assume the interface approriate for DG/UX dbx.
944 This was tested using R4.11.")
946 ;; There are a couple of differences between DG's dbx output and normal
947 ;; dbx output which make it nontrivial to integrate this into the
948 ;; standard dbx-marker-filter (mainly, there are a different number of
949 ;; backreferences). The markers look like:
951 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
953 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
954 ;; number), and
956 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
958 ;; from signals and
960 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
962 ;; from up/down/where.
964 (defun gud-dguxdbx-marker-filter (string)
965 (setq gud-marker-acc (if gud-marker-acc
966 (concat gud-marker-acc string)
967 string))
968 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
969 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
970 start)
971 ;; Process all complete markers in this chunk.
972 (while (string-match re gud-marker-acc start)
973 (setq gud-last-frame
974 (cons (match-string 4 gud-marker-acc)
975 (string-to-int (match-string 3 gud-marker-acc)))
976 start (match-end 0)))
978 ;; Search for the last incomplete line in this chunk
979 (while (string-match "\n" gud-marker-acc start)
980 (setq start (match-end 0)))
982 ;; If the incomplete line APPEARS to begin with another marker, keep it
983 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
984 ;; unnecessary concat during the next call.
985 (setq gud-marker-acc
986 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
987 (substring gud-marker-acc (match-beginning 0))
988 nil)))
989 string)
991 ;;;###autoload
992 (defun dbx (command-line)
993 "Run dbx on program FILE in buffer *gud-FILE*.
994 The directory containing FILE becomes the initial working directory
995 and source-file directory for your debugger."
996 (interactive (list (gud-query-cmdline 'dbx)))
998 (cond
999 (gud-mips-p
1000 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1001 (gud-irix-p
1002 (gud-common-init command-line 'gud-dbx-massage-args
1003 'gud-irixdbx-marker-filter))
1004 (gud-dgux-p
1005 (gud-common-init command-line 'gud-dbx-massage-args
1006 'gud-dguxdbx-marker-filter))
1008 (gud-common-init command-line 'gud-dbx-massage-args
1009 'gud-dbx-marker-filter)))
1011 (set (make-local-variable 'gud-minor-mode) 'dbx)
1013 (cond
1014 (gud-mips-p
1015 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1016 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1017 (gud-def gud-break "stop at \"%f\":%l"
1018 "\C-b" "Set breakpoint at current line.")
1019 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1020 (gud-irix-p
1021 (gud-def gud-break "stop at \"%d%f\":%l"
1022 "\C-b" "Set breakpoint at current line.")
1023 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1024 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1025 "<" "Up (numeric arg) stack frames.")
1026 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1027 ">" "Down (numeric arg) stack frames.")
1028 ;; Make dbx give out the source location info that we need.
1029 (process-send-string (get-buffer-process gud-comint-buffer)
1030 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1032 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1033 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1034 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1035 "\C-b" "Set breakpoint at current line.")
1036 (if gud-dbx-use-stopformat-p
1037 (process-send-string (get-buffer-process gud-comint-buffer)
1038 "set $stopformat=1\n"))))
1040 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1041 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1042 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1043 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1044 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1045 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1047 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1048 (setq paragraph-start comint-prompt-regexp)
1049 (run-hooks 'dbx-mode-hook)
1052 ;; ======================================================================
1053 ;; xdb (HP PARISC debugger) functions
1055 ;; History of argument lists passed to xdb.
1056 (defvar gud-xdb-history nil)
1058 (defcustom gud-xdb-directories nil
1059 "*A list of directories that xdb should search for source code.
1060 If nil, only source files in the program directory
1061 will be known to xdb.
1063 The file names should be absolute, or relative to the directory
1064 containing the executable being debugged."
1065 :type '(choice (const :tag "Current Directory" nil)
1066 (repeat :value ("")
1067 directory))
1068 :group 'gud)
1070 (defun gud-xdb-massage-args (file args)
1071 (nconc (let ((directories gud-xdb-directories)
1072 (result nil))
1073 (while directories
1074 (setq result (cons (car directories) (cons "-d" result)))
1075 (setq directories (cdr directories)))
1076 (nreverse result))
1077 args))
1079 ;; xdb does not print the lines all at once, so we have to accumulate them
1080 (defun gud-xdb-marker-filter (string)
1081 (let (result)
1082 (if (or (string-match comint-prompt-regexp string)
1083 (string-match ".*\012" string))
1084 (setq result (concat gud-marker-acc string)
1085 gud-marker-acc "")
1086 (setq gud-marker-acc (concat gud-marker-acc string)))
1087 (if result
1088 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1089 result)
1090 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1091 result))
1092 (let ((line (string-to-int (match-string 2 result)))
1093 (file (gud-file-name (match-string 1 result))))
1094 (if file
1095 (setq gud-last-frame (cons file line))))))
1096 (or result "")))
1098 ;;;###autoload
1099 (defun xdb (command-line)
1100 "Run xdb on program FILE in buffer *gud-FILE*.
1101 The directory containing FILE becomes the initial working directory
1102 and source-file directory for your debugger.
1104 You can set the variable 'gud-xdb-directories' to a list of program source
1105 directories if your program contains sources from more than one directory."
1106 (interactive (list (gud-query-cmdline 'xdb)))
1108 (gud-common-init command-line 'gud-xdb-massage-args
1109 'gud-xdb-marker-filter)
1110 (set (make-local-variable 'gud-minor-mode) 'xdb)
1112 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1113 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1114 "Set temporary breakpoint at current line.")
1115 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1116 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1117 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1118 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1119 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1120 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1121 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1122 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1124 (setq comint-prompt-regexp "^>")
1125 (setq paragraph-start comint-prompt-regexp)
1126 (run-hooks 'xdb-mode-hook))
1128 ;; ======================================================================
1129 ;; perldb functions
1131 ;; History of argument lists passed to perldb.
1132 (defvar gud-perldb-history nil)
1134 (defun gud-perldb-massage-args (file args)
1135 "Convert a command line as would be typed normally to run perldb
1136 into one that invokes an Emacs-enabled debugging session.
1137 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1138 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1139 ;; arguments ?
1140 (let* ((new-args nil)
1141 (seen-e nil)
1142 (shift (lambda () (push (pop args) new-args))))
1144 ;; Pass all switches and -e scripts through.
1145 (while (and args
1146 (string-match "^-" (car args))
1147 (not (equal "-" (car args)))
1148 (not (equal "--" (car args))))
1149 (when (equal "-e" (car args))
1150 ;; -e goes with the next arg, so shift one extra.
1151 (or (funcall shift)
1152 ;; -e as the last arg is an error in Perl.
1153 (error "No code specified for -e"))
1154 (setq seen-e t))
1155 (funcall shift))
1157 (unless seen-e
1158 (if (or (not args)
1159 (string-match "^-" (car args)))
1160 (error "Can't use stdin as the script to debug"))
1161 ;; This is the program name.
1162 (funcall shift))
1164 ;; If -e specified, make sure there is a -- so -emacs is not taken
1165 ;; as -e macs.
1166 (if (and args (equal "--" (car args)))
1167 (funcall shift)
1168 (and seen-e (push "--" new-args)))
1170 (push "-emacs" new-args)
1171 (while args
1172 (funcall shift))
1174 (nreverse new-args)))
1176 ;; There's no guarantee that Emacs will hand the filter the entire
1177 ;; marker at once; it could be broken up across several strings. We
1178 ;; might even receive a big chunk with several markers in it. If we
1179 ;; receive a chunk of text which looks like it might contain the
1180 ;; beginning of a marker, we save it here between calls to the
1181 ;; filter.
1182 (defun gud-perldb-marker-filter (string)
1183 (setq gud-marker-acc (concat gud-marker-acc string))
1184 (let ((output ""))
1186 ;; Process all the complete markers in this chunk.
1187 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1188 gud-marker-acc)
1189 (setq
1191 ;; Extract the frame position from the marker.
1192 gud-last-frame
1193 (cons (match-string 1 gud-marker-acc)
1194 (string-to-int (match-string 3 gud-marker-acc)))
1196 ;; Append any text before the marker to the output we're going
1197 ;; to return - we don't include the marker in this text.
1198 output (concat output
1199 (substring gud-marker-acc 0 (match-beginning 0)))
1201 ;; Set the accumulator to the remaining text.
1202 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1204 ;; Does the remaining text look like it might end with the
1205 ;; beginning of another marker? If it does, then keep it in
1206 ;; gud-marker-acc until we receive the rest of it. Since we
1207 ;; know the full marker regexp above failed, it's pretty simple to
1208 ;; test for marker starts.
1209 (if (string-match "\032.*\\'" gud-marker-acc)
1210 (progn
1211 ;; Everything before the potential marker start can be output.
1212 (setq output (concat output (substring gud-marker-acc
1213 0 (match-beginning 0))))
1215 ;; Everything after, we save, to combine with later input.
1216 (setq gud-marker-acc
1217 (substring gud-marker-acc (match-beginning 0))))
1219 (setq output (concat output gud-marker-acc)
1220 gud-marker-acc ""))
1222 output))
1224 (defcustom gud-perldb-command-name "perl -d"
1225 "Default command to execute a Perl script under debugger."
1226 :type 'string
1227 :group 'gud)
1229 ;;;###autoload
1230 (defun perldb (command-line)
1231 "Run perldb on program FILE in buffer *gud-FILE*.
1232 The directory containing FILE becomes the initial working directory
1233 and source-file directory for your debugger."
1234 (interactive
1235 (list (gud-query-cmdline 'perldb
1236 (concat (or (buffer-file-name) "-e 0") " "))))
1238 (gud-common-init command-line 'gud-perldb-massage-args
1239 'gud-perldb-marker-filter)
1240 (set (make-local-variable 'gud-minor-mode) 'perldb)
1242 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1243 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1244 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1245 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1246 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1247 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1248 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1249 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1250 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1252 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1253 (setq paragraph-start comint-prompt-regexp)
1254 (run-hooks 'perldb-mode-hook))
1256 ;; ======================================================================
1257 ;; pdb (Python debugger) functions
1259 ;; History of argument lists passed to pdb.
1260 (defvar gud-pdb-history nil)
1262 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1263 ;; Either file or function name may be omitted: "> <string>(0)?()"
1264 (defvar gud-pdb-marker-regexp
1265 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1266 (defvar gud-pdb-marker-regexp-file-group 1)
1267 (defvar gud-pdb-marker-regexp-line-group 2)
1268 (defvar gud-pdb-marker-regexp-fnname-group 3)
1270 (defvar gud-pdb-marker-regexp-start "^> ")
1272 ;; There's no guarantee that Emacs will hand the filter the entire
1273 ;; marker at once; it could be broken up across several strings. We
1274 ;; might even receive a big chunk with several markers in it. If we
1275 ;; receive a chunk of text which looks like it might contain the
1276 ;; beginning of a marker, we save it here between calls to the
1277 ;; filter.
1278 (defun gud-pdb-marker-filter (string)
1279 (setq gud-marker-acc (concat gud-marker-acc string))
1280 (let ((output ""))
1282 ;; Process all the complete markers in this chunk.
1283 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1284 (setq
1286 ;; Extract the frame position from the marker.
1287 gud-last-frame
1288 (let ((file (match-string gud-pdb-marker-regexp-file-group
1289 gud-marker-acc))
1290 (line (string-to-int
1291 (match-string gud-pdb-marker-regexp-line-group
1292 gud-marker-acc))))
1293 (if (string-equal file "<string>")
1294 gud-last-frame
1295 (cons file line)))
1297 ;; Output everything instead of the below
1298 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1299 ;; ;; Append any text before the marker to the output we're going
1300 ;; ;; to return - we don't include the marker in this text.
1301 ;; output (concat output
1302 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1304 ;; Set the accumulator to the remaining text.
1305 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1307 ;; Does the remaining text look like it might end with the
1308 ;; beginning of another marker? If it does, then keep it in
1309 ;; gud-marker-acc until we receive the rest of it. Since we
1310 ;; know the full marker regexp above failed, it's pretty simple to
1311 ;; test for marker starts.
1312 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1313 (progn
1314 ;; Everything before the potential marker start can be output.
1315 (setq output (concat output (substring gud-marker-acc
1316 0 (match-beginning 0))))
1318 ;; Everything after, we save, to combine with later input.
1319 (setq gud-marker-acc
1320 (substring gud-marker-acc (match-beginning 0))))
1322 (setq output (concat output gud-marker-acc)
1323 gud-marker-acc ""))
1325 output))
1327 (defcustom gud-pdb-command-name "pdb"
1328 "File name for executing the Python debugger.
1329 This should be an executable on your path, or an absolute file name."
1330 :type 'string
1331 :group 'gud)
1333 ;;;###autoload
1334 (defun pdb (command-line)
1335 "Run pdb on program FILE in buffer `*gud-FILE*'.
1336 The directory containing FILE becomes the initial working directory
1337 and source-file directory for your debugger."
1338 (interactive
1339 (list (gud-query-cmdline 'pdb)))
1341 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1342 (set (make-local-variable 'gud-minor-mode) 'pdb)
1344 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1345 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1346 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1347 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1348 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1349 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1350 (gud-def gud-up "up" "<" "Up one stack frame.")
1351 (gud-def gud-down "down" ">" "Down one stack frame.")
1352 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1353 ;; Is this right?
1354 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1356 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1357 (setq comint-prompt-regexp "^(Pdb) *")
1358 (setq paragraph-start comint-prompt-regexp)
1359 (run-hooks 'pdb-mode-hook))
1361 ;; ======================================================================
1363 ;; JDB support.
1365 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1366 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1368 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1369 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1370 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1372 ;; INVOCATION NOTES:
1374 ;; You invoke jdb-mode with:
1376 ;; M-x jdb <enter>
1378 ;; It responds with:
1380 ;; Run jdb (like this): jdb
1382 ;; type any jdb switches followed by the name of the class you'd like to debug.
1383 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1384 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1385 ;; See the known problems section below for restrictions when specifying jdb
1386 ;; command line switches (search forward for '-classpath').
1388 ;; You should see something like the following:
1390 ;; Current directory is ~/src/java/hello/
1391 ;; Initializing jdb...
1392 ;; 0xed2f6628:class(hello)
1393 ;; >
1395 ;; To set an initial breakpoint try:
1397 ;; > stop in hello.main
1398 ;; Breakpoint set in hello.main
1399 ;; >
1401 ;; To execute the program type:
1403 ;; > run
1404 ;; run hello
1406 ;; Breakpoint hit: running ...
1407 ;; hello.main (hello:12)
1409 ;; Type M-n to step over the current line and M-s to step into it. That,
1410 ;; along with the JDB 'help' command should get you started. The 'quit'
1411 ;; JDB command will get out out of the debugger. There is some truly
1412 ;; pathetic JDB documentation available at:
1414 ;; http://java.sun.com/products/jdk/1.1/debugging/
1416 ;; KNOWN PROBLEMS AND FIXME's:
1418 ;; Not sure what happens with inner classes ... haven't tried them.
1420 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1422 ;; You must not put whitespace between "-classpath" and the path to
1423 ;; search for java classes even though it is required when invoking jdb
1424 ;; from the command line. See gud-jdb-massage-args for details.
1425 ;; The same applies for "-sourcepath".
1427 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1428 ;; refer to the documentation of `gud-jdb-use-classpath' and
1429 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1430 ;; on using the classpath for locating java source files.
1432 ;; If any of the source files in the directories listed in
1433 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1434 ;; every file ending in ".java" in these directories parses without error.
1436 ;; All the .java files in the directories in gud-jdb-directories are
1437 ;; syntactically analyzed each time gud jdb is invoked. It would be
1438 ;; nice to keep as much information as possible between runs. It would
1439 ;; be really nice to analyze the files only as neccessary (when the
1440 ;; source needs to be displayed.) I'm not sure to what extent the former
1441 ;; can be accomplished and I'm not sure the latter can be done at all
1442 ;; since I don't know of any general way to tell which .class files are
1443 ;; defined by which .java file without analyzing all the .java files.
1444 ;; If anyone knows why JavaSoft didn't put the source file names in
1445 ;; debuggable .class files please clue me in so I find something else
1446 ;; to be spiteful and bitter about.
1448 ;; ======================================================================
1449 ;; gud jdb variables and functions
1451 (defcustom gud-jdb-command-name "jdb"
1452 "Command that executes the Java debugger."
1453 :type 'string
1454 :group 'gud)
1456 (defcustom gud-jdb-use-classpath t
1457 "If non-nil, search for Java source files in classpath directories.
1458 The list of directories to search is the value of `gud-jdb-classpath'.
1459 The file pathname is obtained by converting the fully qualified
1460 class information output by jdb to a relative pathname and appending
1461 it to `gud-jdb-classpath' element by element until a match is found.
1463 This method has a significant jdb startup time reduction advantage
1464 since it does not require the scanning of all `gud-jdb-directories'
1465 and parsing all Java files for class information.
1467 Set to nil to use `gud-jdb-directories' to scan java sources for
1468 class information on jdb startup (original method)."
1469 :type 'boolean
1470 :group 'gud)
1472 (defvar gud-jdb-classpath nil
1473 "Java/jdb classpath directories list.
1474 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1475 list automatically using the following methods in sequence
1476 \(with subsequent successful steps overriding the results of previous
1477 steps):
1479 1) Read the CLASSPATH environment variable,
1480 2) Read any \"-classpath\" argument used to run jdb,
1481 or detected in jdb output (e.g. if jdb is run by a script
1482 that echoes the actual jdb command before starting jdb)
1483 3) Send a \"classpath\" command to jdb and scan jdb output for
1484 classpath information if jdb is invoked with an \"-attach\" (to
1485 an already running VM) argument (This case typically does not
1486 have a \"-classpath\" command line argument - that is provided
1487 to the VM when it is started).
1489 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1490 those debuggers do not support the classpath command. Use 1) or 2).")
1492 (defvar gud-jdb-sourcepath nil
1493 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1494 This list is prepended to `gud-jdb-classpath' to form the complete
1495 list of directories searched for source files.")
1497 (defvar gud-marker-acc-max-length 4000
1498 "Maximum number of debugger output characters to keep.
1499 This variable limits the size of `gud-marker-acc' which holds
1500 the most recent debugger output history while searching for
1501 source file information.")
1503 (defvar gud-jdb-history nil
1504 "History of argument lists passed to jdb.")
1507 ;; List of Java source file directories.
1508 (defvar gud-jdb-directories (list ".")
1509 "*A list of directories that gud jdb should search for source code.
1510 The file names should be absolute, or relative to the current
1511 directory.
1513 The set of .java files residing in the directories listed are
1514 syntactically analyzed to determine the classes they define and the
1515 packages in which these classes belong. In this way gud jdb maps the
1516 package-qualified class names output by the jdb debugger to the source
1517 file from which the class originated. This allows gud mode to keep
1518 the source code display in sync with the debugging session.")
1520 (defvar gud-jdb-source-files nil
1521 "List of the java source files for this debugging session.")
1523 ;; Association list of fully qualified class names (package + class name)
1524 ;; and their source files.
1525 (defvar gud-jdb-class-source-alist nil
1526 "Association list of fully qualified class names and source files.")
1528 ;; This is used to hold a source file during analysis.
1529 (defvar gud-jdb-analysis-buffer nil)
1531 (defvar gud-jdb-classpath-string nil
1532 "Holds temporary classpath values.")
1534 (defun gud-jdb-build-source-files-list (path extn)
1535 "Return a list of java source files (absolute paths).
1536 PATH gives the directories in which to search for files with
1537 extension EXTN. Normally EXTN is given as the regular expression
1538 \"\\.java$\" ."
1539 (apply 'nconc (mapcar (lambda (d)
1540 (when (file-directory-p d)
1541 (directory-files d t extn nil)))
1542 path)))
1544 ;; Move point past whitespace.
1545 (defun gud-jdb-skip-whitespace ()
1546 (skip-chars-forward " \n\r\t\014"))
1548 ;; Move point past a "// <eol>" type of comment.
1549 (defun gud-jdb-skip-single-line-comment ()
1550 (end-of-line))
1552 ;; Move point past a "/* */" or "/** */" type of comment.
1553 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1554 (forward-char 2)
1555 (catch 'break
1556 (while (not (eobp))
1557 (if (eq (following-char) ?*)
1558 (progn
1559 (forward-char)
1560 (if (not (eobp))
1561 (if (eq (following-char) ?/)
1562 (progn
1563 (forward-char)
1564 (throw 'break nil)))))
1565 (forward-char)))))
1567 ;; Move point past any number of consecutive whitespace chars and/or comments.
1568 (defun gud-jdb-skip-whitespace-and-comments ()
1569 (gud-jdb-skip-whitespace)
1570 (catch 'done
1571 (while t
1572 (cond
1573 ((looking-at "//")
1574 (gud-jdb-skip-single-line-comment)
1575 (gud-jdb-skip-whitespace))
1576 ((looking-at "/\\*")
1577 (gud-jdb-skip-traditional-or-documentation-comment)
1578 (gud-jdb-skip-whitespace))
1579 (t (throw 'done nil))))))
1581 ;; Move point past things that are id-like. The intent is to skip regular
1582 ;; id's, such as class or interface names as well as package and interface
1583 ;; names.
1584 (defun gud-jdb-skip-id-ish-thing ()
1585 (skip-chars-forward "^ /\n\r\t\014,;{"))
1587 ;; Move point past a string literal.
1588 (defun gud-jdb-skip-string-literal ()
1589 (forward-char)
1590 (while (not (cond
1591 ((eq (following-char) ?\\)
1592 (forward-char))
1593 ((eq (following-char) ?\042))))
1594 (forward-char))
1595 (forward-char))
1597 ;; Move point past a character literal.
1598 (defun gud-jdb-skip-character-literal ()
1599 (forward-char)
1600 (while
1601 (progn
1602 (if (eq (following-char) ?\\)
1603 (forward-char 2))
1604 (not (eq (following-char) ?\')))
1605 (forward-char))
1606 (forward-char))
1608 ;; Move point past the following block. There may be (legal) cruft before
1609 ;; the block's opening brace. There must be a block or it's the end of life
1610 ;; in petticoat junction.
1611 (defun gud-jdb-skip-block ()
1613 ;; Find the begining of the block.
1614 (while
1615 (not (eq (following-char) ?{))
1617 ;; Skip any constructs that can harbor literal block delimiter
1618 ;; characters and/or the delimiters for the constructs themselves.
1619 (cond
1620 ((looking-at "//")
1621 (gud-jdb-skip-single-line-comment))
1622 ((looking-at "/\\*")
1623 (gud-jdb-skip-traditional-or-documentation-comment))
1624 ((eq (following-char) ?\042)
1625 (gud-jdb-skip-string-literal))
1626 ((eq (following-char) ?\')
1627 (gud-jdb-skip-character-literal))
1628 (t (forward-char))))
1630 ;; Now at the begining of the block.
1631 (forward-char)
1633 ;; Skip over the body of the block as well as the final brace.
1634 (let ((open-level 1))
1635 (while (not (eq open-level 0))
1636 (cond
1637 ((looking-at "//")
1638 (gud-jdb-skip-single-line-comment))
1639 ((looking-at "/\\*")
1640 (gud-jdb-skip-traditional-or-documentation-comment))
1641 ((eq (following-char) ?\042)
1642 (gud-jdb-skip-string-literal))
1643 ((eq (following-char) ?\')
1644 (gud-jdb-skip-character-literal))
1645 ((eq (following-char) ?{)
1646 (setq open-level (+ open-level 1))
1647 (forward-char))
1648 ((eq (following-char) ?})
1649 (setq open-level (- open-level 1))
1650 (forward-char))
1651 (t (forward-char))))))
1653 ;; Find the package and class definitions in Java source file FILE. Assumes
1654 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1655 ;; to hold the source during analysis.
1656 (defun gud-jdb-analyze-source (buf file)
1657 (let ((l nil))
1658 (set-buffer buf)
1659 (insert-file-contents file nil nil nil t)
1660 (goto-char 0)
1661 (catch 'abort
1662 (let ((p ""))
1663 (while (progn
1664 (gud-jdb-skip-whitespace)
1665 (not (eobp)))
1666 (cond
1668 ;; Any number of semi's following a block is legal. Move point
1669 ;; past them. Note that comments and whitespace may be
1670 ;; interspersed as well.
1671 ((eq (following-char) ?\073)
1672 (forward-char))
1674 ;; Move point past a single line comment.
1675 ((looking-at "//")
1676 (gud-jdb-skip-single-line-comment))
1678 ;; Move point past a traditional or documentation comment.
1679 ((looking-at "/\\*")
1680 (gud-jdb-skip-traditional-or-documentation-comment))
1682 ;; Move point past a package statement, but save the PackageName.
1683 ((looking-at "package")
1684 (forward-char 7)
1685 (gud-jdb-skip-whitespace-and-comments)
1686 (let ((s (point)))
1687 (gud-jdb-skip-id-ish-thing)
1688 (setq p (concat (buffer-substring s (point)) "."))
1689 (gud-jdb-skip-whitespace-and-comments)
1690 (if (eq (following-char) ?\073)
1691 (forward-char))))
1693 ;; Move point past an import statement.
1694 ((looking-at "import")
1695 (forward-char 6)
1696 (gud-jdb-skip-whitespace-and-comments)
1697 (gud-jdb-skip-id-ish-thing)
1698 (gud-jdb-skip-whitespace-and-comments)
1699 (if (eq (following-char) ?\073)
1700 (forward-char)))
1702 ;; Move point past the various kinds of ClassModifiers.
1703 ((looking-at "public")
1704 (forward-char 6))
1705 ((looking-at "abstract")
1706 (forward-char 8))
1707 ((looking-at "final")
1708 (forward-char 5))
1710 ;; Move point past a ClassDeclaraction, but save the class
1711 ;; Identifier.
1712 ((looking-at "class")
1713 (forward-char 5)
1714 (gud-jdb-skip-whitespace-and-comments)
1715 (let ((s (point)))
1716 (gud-jdb-skip-id-ish-thing)
1717 (setq
1718 l (nconc l (list (concat p (buffer-substring s (point)))))))
1719 (gud-jdb-skip-block))
1721 ;; Move point past an interface statement.
1722 ((looking-at "interface")
1723 (forward-char 9)
1724 (gud-jdb-skip-block))
1726 ;; Anything else means the input is invalid.
1728 (message (format "Error parsing file %s." file))
1729 (throw 'abort nil))))))
1732 (defun gud-jdb-build-class-source-alist-for-file (file)
1733 (mapcar
1734 (lambda (c)
1735 (cons c file))
1736 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1738 ;; Return an alist of fully qualified classes and the source files
1739 ;; holding their definitions. SOURCES holds a list of all the source
1740 ;; files to examine.
1741 (defun gud-jdb-build-class-source-alist (sources)
1742 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1743 (prog1
1744 (apply
1745 'nconc
1746 (mapcar
1747 'gud-jdb-build-class-source-alist-for-file
1748 sources))
1749 (kill-buffer gud-jdb-analysis-buffer)
1750 (setq gud-jdb-analysis-buffer nil)))
1752 ;; Change what was given in the minibuffer to something that can be used to
1753 ;; invoke the debugger.
1754 (defun gud-jdb-massage-args (file args)
1755 ;; The jdb executable must have whitespace between "-classpath" and
1756 ;; its value while gud-common-init expects all switch values to
1757 ;; follow the switch keyword without intervening whitespace. We
1758 ;; require that when the user enters the "-classpath" switch in the
1759 ;; EMACS minibuffer that they do so without the intervening
1760 ;; whitespace. This function adds it back (it's called after
1761 ;; gud-common-init). There are more switches like this (for
1762 ;; instance "-host" and "-password") but I don't care about them
1763 ;; yet.
1764 (if args
1765 (let (massaged-args user-error)
1767 (while (and args (not user-error))
1768 (cond
1769 ((setq user-error (string-match "-classpath$" (car args))))
1770 ((setq user-error (string-match "-sourcepath$" (car args))))
1771 ((string-match "-classpath\\(.+\\)" (car args))
1772 (setq massaged-args
1773 (append massaged-args
1774 (list "-classpath"
1775 (setq gud-jdb-classpath-string
1776 (match-string 1 (car args)))))))
1777 ((string-match "-sourcepath\\(.+\\)" (car args))
1778 (setq massaged-args
1779 (append massaged-args
1780 (list "-sourcepath"
1781 (setq gud-jdb-sourcepath
1782 (match-string 1 (car args)))))))
1783 (t (setq massaged-args (append massaged-args (list (car args))))))
1784 (setq args (cdr args)))
1786 ;; By this point the current directory is all screwed up. Maybe we
1787 ;; could fix things and re-invoke gud-common-init, but for now I think
1788 ;; issueing the error is good enough.
1789 (if user-error
1790 (progn
1791 (kill-buffer (current-buffer))
1792 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
1793 massaged-args)))
1795 ;; Search for an association with P, a fully qualified class name, in
1796 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1797 ;; qualified file name of the source file which produced the class.
1798 (defun gud-jdb-find-source-file (p)
1799 (cdr (assoc p gud-jdb-class-source-alist)))
1801 ;; Note: Reset to this value every time a prompt is seen
1802 (defvar gud-jdb-lowest-stack-level 999)
1804 (defun gud-jdb-find-source-using-classpath (p)
1805 "Find source file corresponding to fully qualified class p.
1806 Convert p from jdb's output, converted to a pathname
1807 relative to a classpath directory."
1808 (save-match-data
1809 (let
1810 (;; Replace dots with slashes and append ".java" to generate file
1811 ;; name relative to classpath
1812 (filename
1813 (concat
1814 (mapconcat 'identity
1815 (split-string
1816 ;; Eliminate any subclass references in the class
1817 ;; name string. These start with a "$"
1818 ((lambda (x)
1819 (if (string-match "$.*" x)
1820 (replace-match "" t t x) p))
1822 "\\.") "/")
1823 ".java"))
1824 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
1825 found-file)
1826 (while (and cplist
1827 (not (setq found-file
1828 (file-readable-p
1829 (concat (car cplist) "/" filename)))))
1830 (setq cplist (cdr cplist)))
1831 (if found-file (concat (car cplist) "/" filename)))))
1833 (defun gud-jdb-find-source (string)
1834 "Alias for function used to locate source files.
1835 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
1836 during jdb initialization depending on the value of
1837 `gud-jdb-use-classpath'."
1838 nil)
1840 (defun gud-jdb-parse-classpath-string (string)
1841 "Parse the classpath list and convert each item to an absolute pathname."
1842 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
1843 (replace-match "" nil nil s) s))
1844 (mapcar 'file-truename
1845 (split-string
1846 string
1847 (concat "[ \t\n\r,\"" path-separator "]+")))))
1849 ;; See comentary for other debugger's marker filters - there you will find
1850 ;; important notes about STRING.
1851 (defun gud-jdb-marker-filter (string)
1853 ;; Build up the accumulator.
1854 (setq gud-marker-acc
1855 (if gud-marker-acc
1856 (concat gud-marker-acc string)
1857 string))
1859 ;; Look for classpath information until gud-jdb-classpath-string is found
1860 ;; (interactive, multiple settings of classpath from jdb
1861 ;; not supported/followed)
1862 (if (and gud-jdb-use-classpath
1863 (not gud-jdb-classpath-string)
1864 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
1865 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
1866 (setq gud-jdb-classpath
1867 (gud-jdb-parse-classpath-string
1868 (setq gud-jdb-classpath-string
1869 (match-string 1 gud-marker-acc)))))
1871 ;; We process STRING from left to right. Each time through the
1872 ;; following loop we process at most one marker. After we've found a
1873 ;; marker, delete gud-marker-acc up to and including the match
1874 (let (file-found)
1875 ;; Process each complete marker in the input.
1876 (while
1878 ;; Do we see a marker?
1879 (string-match
1880 ;; jdb puts out a string of the following form when it
1881 ;; hits a breakpoint:
1883 ;; <fully-qualified-class><method> (<class>:<line-number>)
1885 ;; <fully-qualified-class>'s are composed of Java ID's
1886 ;; separated by periods. <method> and <class> are
1887 ;; also Java ID's. <method> begins with a period and
1888 ;; may contain less-than and greater-than (constructors,
1889 ;; for instance, are called <init> in the symbol table.)
1890 ;; Java ID's begin with a letter followed by letters
1891 ;; and/or digits. The set of letters includes underscore
1892 ;; and dollar sign.
1894 ;; The first group matches <fully-qualified-class>,
1895 ;; the second group matches <class> and the third group
1896 ;; matches <line-number>. We don't care about using
1897 ;; <method> so we don't "group" it.
1899 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1900 ;; ID's only.
1902 ;; The "," in the last square-bracket is necessary because of
1903 ;; Sun's total disrespect for backwards compatibility in
1904 ;; reported line numbers from jdb - starting in 1.4.0 they
1905 ;; introduced a comma at the thousands position (how
1906 ;; ingenious!)
1908 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
1909 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9,]+\\)"
1910 gud-marker-acc)
1912 ;; A good marker is one that:
1913 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
1914 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
1915 ;; since the last prompt
1916 ;; Figure out the line on which to position the debugging arrow.
1917 ;; Return the info as a cons of the form:
1919 ;; (<file-name> . <line-number>) .
1920 (if (if (match-beginning 1)
1921 (let (n)
1922 (setq n (string-to-int (substring
1923 gud-marker-acc
1924 (1+ (match-beginning 1))
1925 (- (match-end 1) 2))))
1926 (if (< n gud-jdb-lowest-stack-level)
1927 (progn (setq gud-jdb-lowest-stack-level n) t)))
1929 (if (setq file-found
1930 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
1931 (setq gud-last-frame
1932 (cons file-found
1933 (string-to-int
1934 (let
1935 ((numstr (match-string 4 gud-marker-acc)))
1936 (if (string-match "," numstr)
1937 (replace-match "" nil nil numstr)
1938 numstr)))))
1939 (message "Could not find source file.")))
1941 ;; Set the accumulator to the remaining text.
1942 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
1944 (if (string-match comint-prompt-regexp gud-marker-acc)
1945 (setq gud-jdb-lowest-stack-level 999)))
1947 ;; Do not allow gud-marker-acc to grow without bound. If the source
1948 ;; file information is not within the last 3/4
1949 ;; gud-marker-acc-max-length characters, well,...
1950 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
1951 (setq gud-marker-acc
1952 (substring gud-marker-acc
1953 (- (/ (* gud-marker-acc-max-length 3) 4)))))
1955 ;; We don't filter any debugger output so just return what we were given.
1956 string)
1958 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1960 ;;;###autoload
1961 (defun jdb (command-line)
1962 "Run jdb with command line COMMAND-LINE in a buffer.
1963 The buffer is named \"*gud*\" if no initial class is given or
1964 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
1965 switch is given, omit all whitespace between it and its value.
1967 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
1968 information on how jdb accesses source files. Alternatively (if
1969 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
1970 original source file access method.
1972 For general information about commands available to control jdb from
1973 gud, see `gud-mode'."
1974 (interactive
1975 (list (gud-query-cmdline 'jdb)))
1976 (setq gud-jdb-classpath nil)
1977 (setq gud-jdb-sourcepath nil)
1979 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
1980 ;; if CLASSPATH is set.
1981 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
1982 (if gud-jdb-classpath-string
1983 (setq gud-jdb-classpath
1984 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
1985 (setq gud-jdb-classpath-string nil) ; prepare for next
1987 (gud-common-init command-line 'gud-jdb-massage-args
1988 'gud-jdb-marker-filter)
1989 (set (make-local-variable 'gud-minor-mode) 'jdb)
1991 ;; If a -classpath option was provided, set gud-jdb-classpath
1992 (if gud-jdb-classpath-string
1993 (setq gud-jdb-classpath
1994 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
1995 (setq gud-jdb-classpath-string nil) ; prepare for next
1996 ;; If a -sourcepath option was provided, parse it
1997 (if gud-jdb-sourcepath
1998 (setq gud-jdb-sourcepath
1999 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2001 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2002 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2003 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2004 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2005 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2006 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2007 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2008 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2009 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2011 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2012 (setq paragraph-start comint-prompt-regexp)
2013 (run-hooks 'jdb-mode-hook)
2015 (if gud-jdb-use-classpath
2016 ;; Get the classpath information from the debugger
2017 (progn
2018 (if (string-match "-attach" command-line)
2019 (gud-call "classpath"))
2020 (fset 'gud-jdb-find-source
2021 'gud-jdb-find-source-using-classpath))
2023 ;; Else create and bind the class/source association list as well
2024 ;; as the source file list.
2025 (setq gud-jdb-class-source-alist
2026 (gud-jdb-build-class-source-alist
2027 (setq gud-jdb-source-files
2028 (gud-jdb-build-source-files-list gud-jdb-directories
2029 "\\.java$"))))
2030 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2033 ;; ======================================================================
2035 ;; BASHDB support. See http://bashdb.sourceforge.net
2037 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2039 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2041 ;; INVOCATION NOTES:
2043 ;; You invoke bashdb-mode with:
2045 ;; M-x bashdb <enter>
2047 ;; It responds with:
2049 ;; Run bashdb (like this): bash
2052 ;; History of argument lists passed to bashdb.
2053 (defvar gud-bashdb-history nil)
2055 ;; Convert a command line as would be typed normally to run a script
2056 ;; into one that invokes an Emacs-enabled debugging session.
2057 ;; "--debugger" in inserted as the first switch.
2059 ;; There's no guarantee that Emacs will hand the filter the entire
2060 ;; marker at once; it could be broken up across several strings. We
2061 ;; might even receive a big chunk with several markers in it. If we
2062 ;; receive a chunk of text which looks like it might contain the
2063 ;; beginning of a marker, we save it here between calls to the
2064 ;; filter.
2065 (defun gud-bashdb-marker-filter (string)
2066 (setq gud-marker-acc (concat gud-marker-acc string))
2067 (let ((output ""))
2069 ;; Process all the complete markers in this chunk.
2070 ;; Format of line looks like this:
2071 ;; (/etc/init.d/ntp.init:16):
2072 ;; but we also allow DOS drive letters
2073 ;; (d:/etc/init.d/ntp.init:16):
2074 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2075 gud-marker-acc)
2076 (setq
2078 ;; Extract the frame position from the marker.
2079 gud-last-frame
2080 (cons (match-string 2 gud-marker-acc)
2081 (string-to-int (match-string 4 gud-marker-acc)))
2083 ;; Append any text before the marker to the output we're going
2084 ;; to return - we don't include the marker in this text.
2085 output (concat output
2086 (substring gud-marker-acc 0 (match-beginning 0)))
2088 ;; Set the accumulator to the remaining text.
2089 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2091 ;; Does the remaining text look like it might end with the
2092 ;; beginning of another marker? If it does, then keep it in
2093 ;; gud-marker-acc until we receive the rest of it. Since we
2094 ;; know the full marker regexp above failed, it's pretty simple to
2095 ;; test for marker starts.
2096 (if (string-match "\032.*\\'" gud-marker-acc)
2097 (progn
2098 ;; Everything before the potential marker start can be output.
2099 (setq output (concat output (substring gud-marker-acc
2100 0 (match-beginning 0))))
2102 ;; Everything after, we save, to combine with later input.
2103 (setq gud-marker-acc
2104 (substring gud-marker-acc (match-beginning 0))))
2106 (setq output (concat output gud-marker-acc)
2107 gud-marker-acc ""))
2109 output))
2111 (defcustom gud-bashdb-command-name "bash --debugger"
2112 "File name for executing bash debugger."
2113 :type 'string
2114 :group 'gud)
2116 ;;;###autoload
2117 (defun bashdb (command-line)
2118 "Run bashdb on program FILE in buffer *gud-FILE*.
2119 The directory containing FILE becomes the initial working directory
2120 and source-file directory for your debugger."
2121 (interactive
2122 (list (read-from-minibuffer "Run bashdb (like this): "
2123 (if (consp gud-bashdb-history)
2124 (car gud-bashdb-history)
2125 (concat gud-bashdb-command-name
2126 " "))
2127 gud-minibuffer-local-map nil
2128 '(gud-bashdb-history . 1))))
2130 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2132 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2134 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2135 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2136 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2137 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2138 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2139 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2140 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2141 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2142 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2143 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2145 ;; Is this right?
2146 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2148 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2149 (setq paragraph-start comint-prompt-regexp)
2150 (run-hooks 'bashdb-mode-hook)
2154 ;; End of debugger-specific information
2158 ;; When we send a command to the debugger via gud-call, it's annoying
2159 ;; to see the command and the new prompt inserted into the debugger's
2160 ;; buffer; we have other ways of knowing the command has completed.
2162 ;; If the buffer looks like this:
2163 ;; --------------------
2164 ;; (gdb) set args foo bar
2165 ;; (gdb) -!-
2166 ;; --------------------
2167 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2168 ;; source file to set a breakpoint, we want the buffer to end up like
2169 ;; this:
2170 ;; --------------------
2171 ;; (gdb) set args foo bar
2172 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2173 ;; (gdb) -!-
2174 ;; --------------------
2175 ;; Essentially, the old prompt is deleted, and the command's output
2176 ;; and the new prompt take its place.
2178 ;; Not echoing the command is easy enough; you send it directly using
2179 ;; process-send-string, and it never enters the buffer. However,
2180 ;; getting rid of the old prompt is trickier; you don't want to do it
2181 ;; when you send the command, since that will result in an annoying
2182 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2183 ;; waits for a response from the debugger, and the new prompt is
2184 ;; inserted. Instead, we'll wait until we actually get some output
2185 ;; from the subprocess before we delete the prompt. If the command
2186 ;; produced no output other than a new prompt, that prompt will most
2187 ;; likely be in the first chunk of output received, so we will delete
2188 ;; the prompt and then replace it with an identical one. If the
2189 ;; command produces output, the prompt is moving anyway, so the
2190 ;; flicker won't be annoying.
2192 ;; So - when we want to delete the prompt upon receipt of the next
2193 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2194 ;; the start of the prompt; the process filter will notice this, and
2195 ;; delete all text between it and the process output marker. If
2196 ;; gud-delete-prompt-marker points nowhere, we leave the current
2197 ;; prompt alone.
2198 (defvar gud-delete-prompt-marker nil)
2201 (put 'gud-mode 'mode-class 'special)
2203 (define-derived-mode gud-mode comint-mode "Debugger"
2204 "Major mode for interacting with an inferior debugger process.
2206 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2207 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2208 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2209 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2211 After startup, the following commands are available in both the GUD
2212 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2213 or step operation:
2215 \\[gud-break] sets a breakpoint at the current file and line. In the
2216 GUD buffer, the current file and line are those of the last breakpoint or
2217 step. In a source buffer, they are the buffer's file and current line.
2219 \\[gud-remove] removes breakpoints on the current file and line.
2221 \\[gud-refresh] displays in the source window the last line referred to
2222 in the gud buffer.
2224 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2225 step-one-line (not entering function calls), and step-one-instruction
2226 and then update the source window with the current file and position.
2227 \\[gud-cont] continues execution.
2229 \\[gud-print] tries to find the largest C lvalue or function-call expression
2230 around point, and sends it to the debugger for value display.
2232 The above commands are common to all supported debuggers except xdb which
2233 does not support stepping instructions.
2235 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2236 except that the breakpoint is temporary; that is, it is removed when
2237 execution stops on it.
2239 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2240 frame. \\[gud-down] drops back down through one.
2242 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2243 the current function and stops.
2245 All the keystrokes above are accessible in the GUD buffer
2246 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2248 All pre-defined functions for which the concept make sense repeat
2249 themselves the appropriate number of times if you give a prefix
2250 argument.
2252 You may use the `gud-def' macro in the initialization hook to define other
2253 commands.
2255 Other commands for interacting with the debugger process are inherited from
2256 comint mode, which see."
2257 (setq mode-line-process '(":%s"))
2258 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2259 (set (make-local-variable 'gud-last-frame) nil)
2260 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2261 (make-local-variable 'comint-prompt-regexp)
2262 ;; Don't put repeated commands in command history many times.
2263 (set (make-local-variable 'comint-input-ignoredups) t)
2264 (make-local-variable 'paragraph-start)
2265 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker)))
2267 ;; Cause our buffers to be displayed, by default,
2268 ;; in the selected window.
2269 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2271 (defcustom gud-chdir-before-run t
2272 "Non-nil if GUD should `cd' to the debugged executable."
2273 :group 'gud
2274 :type 'boolean)
2276 (defvar gud-target-name "--unknown--"
2277 "The apparent name of the program being debugged in a gud buffer.")
2279 ;; Perform initializations common to all debuggers.
2280 ;; The first arg is the specified command line,
2281 ;; which starts with the program to debug.
2282 ;; The other three args specify the values to use
2283 ;; for local variables in the debugger buffer.
2284 (defun gud-common-init (command-line massage-args marker-filter
2285 &optional find-file)
2286 (let* ((words (split-string command-line))
2287 (program (car words))
2288 (dir default-directory)
2289 ;; Extract the file name from WORDS
2290 ;; and put t in its place.
2291 ;; Later on we will put the modified file name arg back there.
2292 (file-word (let ((w (cdr words)))
2293 (while (and w (= ?- (aref (car w) 0)))
2294 (setq w (cdr w)))
2295 (and w
2296 (prog1 (car w)
2297 (setcar w t)))))
2298 (file-subst
2299 (and file-word (substitute-in-file-name file-word)))
2300 (args (cdr words))
2301 ;; If a directory was specified, expand the file name.
2302 ;; Otherwise, don't expand it, so GDB can use the PATH.
2303 ;; A file name without directory is literally valid
2304 ;; only if the file exists in ., and in that case,
2305 ;; omitting the expansion here has no visible effect.
2306 (file (and file-word
2307 (if (file-name-directory file-subst)
2308 (expand-file-name file-subst)
2309 file-subst)))
2310 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2311 (pop-to-buffer (concat "*gud" filepart "*"))
2312 ;; Set the dir, in case the buffer already existed with a different dir.
2313 (setq default-directory dir)
2314 ;; Set default-directory to the file's directory.
2315 (and file-word
2316 gud-chdir-before-run
2317 ;; Don't set default-directory if no directory was specified.
2318 ;; In that case, either the file is found in the current directory,
2319 ;; in which case this setq is a no-op,
2320 ;; or it is found by searching PATH,
2321 ;; in which case we don't know what directory it was found in.
2322 (file-name-directory file)
2323 (setq default-directory (file-name-directory file)))
2324 (or (bolp) (newline))
2325 (insert "Current directory is " default-directory "\n")
2326 ;; Put the substituted and expanded file name back in its place.
2327 (let ((w args))
2328 (while (and w (not (eq (car w) t)))
2329 (setq w (cdr w)))
2330 (if w
2331 (setcar w file)))
2332 (apply 'make-comint (concat "gud" filepart) program nil
2333 (if massage-args (funcall massage-args file args) args))
2334 ;; Since comint clobbered the mode, we don't set it until now.
2335 (gud-mode)
2336 (set (make-local-variable 'gud-target-name)
2337 (and file-word (file-name-nondirectory file))))
2338 (set (make-local-variable 'gud-marker-filter) marker-filter)
2339 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2340 (setq gud-running nil)
2341 (setq gud-last-last-frame nil)
2343 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2344 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2345 (gud-set-buffer))
2347 (defun gud-set-buffer ()
2348 (when (eq major-mode 'gud-mode)
2349 (setq gud-comint-buffer (current-buffer))))
2351 (defvar gud-filter-defer-flag nil
2352 "Non-nil means don't process anything from the debugger right now.
2353 It is saved for when this flag is not set.")
2355 (defvar gud-filter-pending-text nil
2356 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2358 ;; These functions are responsible for inserting output from your debugger
2359 ;; into the buffer. The hard work is done by the method that is
2360 ;; the value of gud-marker-filter.
2362 (defun gud-filter (proc string)
2363 ;; Here's where the actual buffer insertion is done
2364 (let (output process-window)
2365 (if (buffer-name (process-buffer proc))
2366 (if gud-filter-defer-flag
2367 ;; If we can't process any text now,
2368 ;; save it for later.
2369 (setq gud-filter-pending-text
2370 (concat (or gud-filter-pending-text "") string))
2372 ;; If we have to ask a question during the processing,
2373 ;; defer any additional text that comes from the debugger
2374 ;; during that time.
2375 (let ((gud-filter-defer-flag t))
2376 ;; Process now any text we previously saved up.
2377 (if gud-filter-pending-text
2378 (setq string (concat gud-filter-pending-text string)
2379 gud-filter-pending-text nil))
2381 (with-current-buffer (process-buffer proc)
2382 ;; If we have been so requested, delete the debugger prompt.
2383 (save-restriction
2384 (widen)
2385 (if (marker-buffer gud-delete-prompt-marker)
2386 (progn
2387 (delete-region (process-mark proc)
2388 gud-delete-prompt-marker)
2389 (set-marker gud-delete-prompt-marker nil)))
2390 ;; Save the process output, checking for source file markers.
2391 (setq output (gud-marker-filter string))
2392 ;; Check for a filename-and-line number.
2393 ;; Don't display the specified file
2394 ;; unless (1) point is at or after the position where output appears
2395 ;; and (2) this buffer is on the screen.
2396 (setq process-window
2397 (and gud-last-frame
2398 (>= (point) (process-mark proc))
2399 (get-buffer-window (current-buffer)))))
2401 ;; Let the comint filter do the actual insertion.
2402 ;; That lets us inherit various comint features.
2403 (comint-output-filter proc output))
2405 ;; Put the arrow on the source line.
2406 ;; This must be outside of the save-excursion
2407 ;; in case the source file is our current buffer.
2408 (if process-window
2409 (save-selected-window
2410 (select-window process-window)
2411 (gud-display-frame))
2412 ;; We have to be in the proper buffer, (process-buffer proc),
2413 ;; but not in a save-excursion, because that would restore point.
2414 (let ((old-buf (current-buffer)))
2415 (set-buffer (process-buffer proc))
2416 (unwind-protect
2417 (gud-display-frame)
2418 (set-buffer old-buf)))))
2420 ;; If we deferred text that arrived during this processing,
2421 ;; handle it now.
2422 (if gud-filter-pending-text
2423 (gud-filter proc ""))))))
2425 (defun gud-sentinel (proc msg)
2426 (cond ((null (buffer-name (process-buffer proc)))
2427 ;; buffer killed
2428 ;; Stop displaying an arrow in a source file.
2429 (setq overlay-arrow-position nil)
2430 (set-process-buffer proc nil))
2431 ((memq (process-status proc) '(signal exit))
2432 ;; Stop displaying an arrow in a source file.
2433 (setq overlay-arrow-position nil)
2434 (let* ((obuf (current-buffer)))
2435 ;; save-excursion isn't the right thing if
2436 ;; process-buffer is current-buffer
2437 (unwind-protect
2438 (progn
2439 ;; Write something in *compilation* and hack its mode line,
2440 (set-buffer (process-buffer proc))
2441 ;; Fix the mode line.
2442 (setq mode-line-process
2443 (concat ":"
2444 (symbol-name (process-status proc))))
2445 (force-mode-line-update)
2446 (if (eobp)
2447 (insert ?\n mode-name " " msg)
2448 (save-excursion
2449 (goto-char (point-max))
2450 (insert ?\n mode-name " " msg)))
2451 ;; If buffer and mode line will show that the process
2452 ;; is dead, we can delete it now. Otherwise it
2453 ;; will stay around until M-x list-processes.
2454 (delete-process proc))
2455 ;; Restore old buffer, but don't restore old point
2456 ;; if obuf is the gud buffer.
2457 (set-buffer obuf))))))
2459 (defun gud-display-frame ()
2460 "Find and obey the last filename-and-line marker from the debugger.
2461 Obeying it means displaying in another window the specified file and line."
2462 (interactive)
2463 (when gud-last-frame
2464 (gud-set-buffer)
2465 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2466 (setq gud-last-last-frame gud-last-frame
2467 gud-last-frame nil)))
2469 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2470 ;; and that its line LINE is visible.
2471 ;; Put the overlay-arrow on the line LINE in that buffer.
2472 ;; Most of the trickiness in here comes from wanting to preserve the current
2473 ;; region-restriction if that's possible. We use an explicit display-buffer
2474 ;; to get around the fact that this is called inside a save-excursion.
2476 (defun gud-display-line (true-file line)
2477 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2478 (buffer
2479 (save-excursion
2480 (or (eq (current-buffer) gud-comint-buffer)
2481 (set-buffer gud-comint-buffer))
2482 (gud-find-file true-file)))
2483 (window (and buffer (or (get-buffer-window buffer)
2484 (if (eq gud-minor-mode 'gdba)
2485 (gdb-display-source-buffer buffer)
2486 (display-buffer buffer)))))
2487 (pos))
2488 (if buffer
2489 (progn
2490 (save-excursion
2491 (set-buffer buffer)
2492 (save-restriction
2493 (widen)
2494 (goto-line line)
2495 (setq pos (point))
2496 (setq overlay-arrow-string "=>")
2497 (or overlay-arrow-position
2498 (setq overlay-arrow-position (make-marker)))
2499 (set-marker overlay-arrow-position (point) (current-buffer)))
2500 (cond ((or (< pos (point-min)) (> pos (point-max)))
2501 (widen)
2502 (goto-char pos))))
2503 (set-window-point window overlay-arrow-position)))))
2505 ;; The gud-call function must do the right thing whether its invoking
2506 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2507 ;; or a C buffer. In the former case, we want to supply data from
2508 ;; gud-last-frame. Here's how we do it:
2510 (defun gud-format-command (str arg)
2511 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2512 (frame (or gud-last-frame gud-last-last-frame))
2513 result)
2514 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2515 (let ((key (string-to-char (match-string 2 str)))
2516 subst)
2517 (cond
2518 ((eq key ?f)
2519 (setq subst (file-name-nondirectory (if insource
2520 (buffer-file-name)
2521 (car frame)))))
2522 ((eq key ?F)
2523 (setq subst (file-name-sans-extension
2524 (file-name-nondirectory (if insource
2525 (buffer-file-name)
2526 (car frame))))))
2527 ((eq key ?d)
2528 (setq subst (file-name-directory (if insource
2529 (buffer-file-name)
2530 (car frame)))))
2531 ((eq key ?l)
2532 (setq subst (int-to-string
2533 (if insource
2534 (save-restriction
2535 (widen)
2536 (+ (count-lines (point-min) (point))
2537 (if (bolp) 1 0)))
2538 (cdr frame)))))
2539 ((eq key ?e)
2540 (setq subst (gud-find-c-expr)))
2541 ((eq key ?a)
2542 (setq subst (gud-read-address)))
2543 ((eq key ?c)
2544 (setq subst
2545 (gud-find-class
2546 (if insource
2547 (buffer-file-name)
2548 (car frame))
2549 (if insource
2550 (save-restriction
2551 (widen)
2552 (+ (count-lines (point-min) (point))
2553 (if (bolp) 1 0)))
2554 (cdr frame)))))
2555 ((eq key ?p)
2556 (setq subst (if arg (int-to-string arg)))))
2557 (setq result (concat result (match-string 1 str) subst)))
2558 (setq str (substring str (match-end 2))))
2559 ;; There might be text left in STR when the loop ends.
2560 (concat result str)))
2562 (defun gud-read-address ()
2563 "Return a string containing the core-address found in the buffer at point."
2564 (save-match-data
2565 (save-excursion
2566 (let ((pt (point)) found begin)
2567 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2568 (cond
2569 (found (forward-char 2)
2570 (buffer-substring found
2571 (progn (re-search-forward "[^0-9a-f]")
2572 (forward-char -1)
2573 (point))))
2574 (t (setq begin (progn (re-search-backward "[^0-9]")
2575 (forward-char 1)
2576 (point)))
2577 (forward-char 1)
2578 (re-search-forward "[^0-9]")
2579 (forward-char -1)
2580 (buffer-substring begin (point))))))))
2582 (defun gud-call (fmt &optional arg)
2583 (let ((msg (gud-format-command fmt arg)))
2584 (message "Command: %s" msg)
2585 (sit-for 0)
2586 (gud-basic-call msg)))
2588 (defun gud-basic-call (command)
2589 "Invoke the debugger COMMAND displaying source in other window."
2590 (interactive)
2591 (gud-set-buffer)
2592 (let ((proc (get-buffer-process gud-comint-buffer)))
2593 (or proc (error "Current buffer has no process"))
2594 ;; Arrange for the current prompt to get deleted.
2595 (save-excursion
2596 (set-buffer gud-comint-buffer)
2597 (save-restriction
2598 (widen)
2599 (goto-char (process-mark proc))
2600 (forward-line 0)
2601 (if (looking-at comint-prompt-regexp)
2602 (set-marker gud-delete-prompt-marker (point)))
2603 (if (eq gud-minor-mode 'gdba)
2604 (apply comint-input-sender (list proc command))
2605 (process-send-string proc (concat command "\n")))))))
2607 (defun gud-refresh (&optional arg)
2608 "Fix up a possibly garbled display, and redraw the arrow."
2609 (interactive "P")
2610 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2611 (gud-display-frame)
2612 (recenter arg))
2614 ;; Code for parsing expressions out of C code. The single entry point is
2615 ;; find-c-expr, which tries to return an lvalue expression from around point.
2617 ;; The rest of this file is a hacked version of gdbsrc.el by
2618 ;; Debby Ayers <ayers@asc.slb.com>,
2619 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2621 (defun gud-find-c-expr ()
2622 "Returns the C expr that surrounds point."
2623 (interactive)
2624 (save-excursion
2625 (let (p expr test-expr)
2626 (setq p (point))
2627 (setq expr (gud-innermost-expr))
2628 (setq test-expr (gud-prev-expr))
2629 (while (and test-expr (gud-expr-compound test-expr expr))
2630 (let ((prev-expr expr))
2631 (setq expr (cons (car test-expr) (cdr expr)))
2632 (goto-char (car expr))
2633 (setq test-expr (gud-prev-expr))
2634 ;; If we just pasted on the condition of an if or while,
2635 ;; throw it away again.
2636 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2637 '("if" "while" "for"))
2638 (setq test-expr nil
2639 expr prev-expr))))
2640 (goto-char p)
2641 (setq test-expr (gud-next-expr))
2642 (while (gud-expr-compound expr test-expr)
2643 (setq expr (cons (car expr) (cdr test-expr)))
2644 (setq test-expr (gud-next-expr)))
2645 (buffer-substring (car expr) (cdr expr)))))
2647 (defun gud-innermost-expr ()
2648 "Returns the smallest expr that point is in; move point to beginning of it.
2649 The expr is represented as a cons cell, where the car specifies the point in
2650 the current buffer that marks the beginning of the expr and the cdr specifies
2651 the character after the end of the expr."
2652 (let ((p (point)) begin end)
2653 (gud-backward-sexp)
2654 (setq begin (point))
2655 (gud-forward-sexp)
2656 (setq end (point))
2657 (if (>= p end)
2658 (progn
2659 (setq begin p)
2660 (goto-char p)
2661 (gud-forward-sexp)
2662 (setq end (point)))
2664 (goto-char begin)
2665 (cons begin end)))
2667 (defun gud-backward-sexp ()
2668 "Version of `backward-sexp' that catches errors."
2669 (condition-case nil
2670 (backward-sexp)
2671 (error t)))
2673 (defun gud-forward-sexp ()
2674 "Version of `forward-sexp' that catches errors."
2675 (condition-case nil
2676 (forward-sexp)
2677 (error t)))
2679 (defun gud-prev-expr ()
2680 "Returns the previous expr, point is set to beginning of that expr.
2681 The expr is represented as a cons cell, where the car specifies the point in
2682 the current buffer that marks the beginning of the expr and the cdr specifies
2683 the character after the end of the expr"
2684 (let ((begin) (end))
2685 (gud-backward-sexp)
2686 (setq begin (point))
2687 (gud-forward-sexp)
2688 (setq end (point))
2689 (goto-char begin)
2690 (cons begin end)))
2692 (defun gud-next-expr ()
2693 "Returns the following expr, point is set to beginning of that expr.
2694 The expr is represented as a cons cell, where the car specifies the point in
2695 the current buffer that marks the beginning of the expr and the cdr specifies
2696 the character after the end of the expr."
2697 (let ((begin) (end))
2698 (gud-forward-sexp)
2699 (gud-forward-sexp)
2700 (setq end (point))
2701 (gud-backward-sexp)
2702 (setq begin (point))
2703 (cons begin end)))
2705 (defun gud-expr-compound-sep (span-start span-end)
2706 "Scan from SPAN-START to SPAN-END for punctuation characters.
2707 If `->' is found, return `?.'. If `.' is found, return `?.'.
2708 If any other punctuation is found, return `??'.
2709 If no punctuation is found, return `? '."
2710 (let ((result ?\ )
2711 (syntax))
2712 (while (< span-start span-end)
2713 (setq syntax (char-syntax (char-after span-start)))
2714 (cond
2715 ((= syntax ?\ ) t)
2716 ((= syntax ?.) (setq syntax (char-after span-start))
2717 (cond
2718 ((= syntax ?.) (setq result ?.))
2719 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2720 (setq result ?.)
2721 (setq span-start (+ span-start 1)))
2722 (t (setq span-start span-end)
2723 (setq result ??)))))
2724 (setq span-start (+ span-start 1)))
2725 result))
2727 (defun gud-expr-compound (first second)
2728 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2729 The two exprs are represented as a cons cells, where the car
2730 specifies the point in the current buffer that marks the beginning of the
2731 expr and the cdr specifies the character after the end of the expr.
2732 Link exprs of the form:
2733 Expr -> Expr
2734 Expr . Expr
2735 Expr (Expr)
2736 Expr [Expr]
2737 (Expr) Expr
2738 [Expr] Expr"
2739 (let ((span-start (cdr first))
2740 (span-end (car second))
2741 (syntax))
2742 (setq syntax (gud-expr-compound-sep span-start span-end))
2743 (cond
2744 ((= (car first) (car second)) nil)
2745 ((= (cdr first) (cdr second)) nil)
2746 ((= syntax ?.) t)
2747 ((= syntax ?\ )
2748 (setq span-start (char-after (- span-start 1)))
2749 (setq span-end (char-after span-end))
2750 (cond
2751 ((= span-start ?)) t)
2752 ((= span-start ?]) t)
2753 ((= span-end ?() t)
2754 ((= span-end ?[) t)
2755 (t nil)))
2756 (t nil))))
2758 (defun gud-find-class (f line)
2759 "Find fully qualified class in file F at line LINE.
2760 This function uses the `gud-jdb-classpath' (and optional
2761 `gud-jdb-sourcepath') list(s) to derive a file
2762 pathname relative to its classpath directory. The values in
2763 `gud-jdb-classpath' are assumed to have been converted to absolute
2764 pathname standards using file-truename.
2765 If F is visited by a buffer and its mode is CC-mode(Java),
2766 syntactic information of LINE is used to find the enclosing (nested)
2767 class string which is appended to the top level
2768 class of the file (using s to separate nested class ids)."
2769 ;; Convert f to a standard representation and remove suffix
2770 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
2771 (save-match-data
2772 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2773 (fbuffer (get-file-buffer f))
2774 class-found)
2775 (setq f (file-name-sans-extension (file-truename f)))
2776 ;; Search through classpath list for an entry that is
2777 ;; contained in f
2778 (while (and cplist (not class-found))
2779 (if (string-match (car cplist) f)
2780 (setq class-found
2781 (mapconcat 'identity
2782 (split-string
2783 (substring f (+ (match-end 0) 1))
2784 "/") ".")))
2785 (setq cplist (cdr cplist)))
2786 ;; if f is visited by a java(cc-mode) buffer, walk up the
2787 ;; syntactic information chain and collect any 'inclass
2788 ;; symbols until 'topmost-intro is reached to find out if
2789 ;; point is within a nested class
2790 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
2791 (save-excursion
2792 (set-buffer fbuffer)
2793 (let ((nclass) (syntax)
2794 (pos (point)))
2795 ;; While the c-syntactic information does not start
2796 ;; with the 'topmost-intro symbol, there may be
2797 ;; nested classes...
2798 (while (not (eq 'topmost-intro
2799 (car (car (c-guess-basic-syntax)))))
2800 ;; Check if the current position c-syntactic
2801 ;; analysis has 'inclass
2802 (setq syntax (c-guess-basic-syntax))
2803 (while
2804 (and (not (eq 'inclass (car (car syntax))))
2805 (cdr syntax))
2806 (setq syntax (cdr syntax)))
2807 (if (eq 'inclass (car (car syntax)))
2808 (progn
2809 (goto-char (cdr (car syntax)))
2810 ;; Now we're at the beginning of a class
2811 ;; definition. Find class name
2812 (looking-at
2813 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
2814 (setq nclass
2815 (append (list (match-string-no-properties 1))
2816 nclass)))
2817 (setq syntax (c-guess-basic-syntax))
2818 (while (and (not (cdr (car syntax))) (cdr syntax))
2819 (setq syntax (cdr syntax)))
2820 (goto-char (cdr (car syntax)))
2822 (string-match (concat (car nclass) "$") class-found)
2823 (setq class-found
2824 (replace-match (mapconcat 'identity nclass "$")
2825 t t class-found)))))
2826 (if (not class-found)
2827 (message "gud-find-class: class for file %s not found!" f))
2828 class-found))
2829 ;; Not using classpath - try class/source association list
2830 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
2831 (if class-found
2832 (car class-found)
2833 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
2834 nil))))
2836 (provide 'gud)
2838 ;;; gud.el ends here