1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992,93,94,95,96,1998,2000,02,2003 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38 ;; <ddavies@world.std.com> added support for jdb (Java debugger.)
45 ;; ======================================================================
46 ;; GUD commands must be visible in C buffers visited by GUD
49 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
50 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
55 (defcustom gud-key-prefix "\C-x\C-a"
56 "Prefix of all GUD commands valid in C buffers."
60 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
61 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
63 (defvar gud-marker-filter nil)
64 (put 'gud-marker-filter 'permanent-local t)
65 (defvar gud-find-file nil)
66 (put 'gud-find-file 'permanent-local t)
68 (defun gud-marker-filter (&rest args)
69 (apply gud-marker-filter args))
71 (defvar gud-minor-mode nil)
72 (put 'gud-minor-mode 'permanent-local t)
74 (defvar gud-keep-buffer nil)
76 (defun gud-symbol (sym &optional soft minor-mode)
77 "Return the symbol used for SYM in MINOR-MODE.
78 MINOR-MODE defaults to `gud-minor-mode.
79 The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
80 If SOFT is non-nil, returns nil if the symbol doesn't already exist."
81 (unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
82 (funcall (if soft 'intern-soft 'intern)
83 (format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
85 (defun gud-val (sym &optional minor-mode)
86 "Return the value of `gud-symbol' SYM. Default to nil."
87 (let ((sym (gud-symbol sym t minor-mode)))
88 (if (boundp sym) (symbol-value sym))))
90 (defvar gud-running nil
91 "Non-nil if debuggee is running.
92 Used to grey out relevant toolbar icons.")
94 (easy-mmode-defmap gud-menu-map
95 '(([refresh] "Refresh" . gud-refresh)
96 ([run] menu-item "Run" gud-run
97 :enable (and (not gud-running)
98 (memq gud-minor-mode '(gdba gdb dbx jdb))))
99 ([until] menu-item "Continue to selection" gud-until
100 :enable (and (not gud-running)
101 (memq gud-minor-mode '(gdba gdb perldb))))
102 ([remove] menu-item "Remove Breakpoint" gud-remove
103 :enable (not gud-running))
104 ([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
105 :enable (memq gud-minor-mode '(gdba gdb sdb xdb bashdb)))
106 ([break] menu-item "Set Breakpoint" gud-break
107 :enable (not gud-running))
108 ([up] menu-item "Up Stack" gud-up
109 :enable (and (not gud-running)
111 '(gdba gdb dbx xdb jdb pdb bashdb))))
112 ([down] menu-item "Down Stack" gud-down
113 :enable (and (not gud-running)
115 '(gdba gdb dbx xdb jdb pdb bashdb))))
116 ([print] menu-item "Print Expression" gud-print
117 :enable (not gud-running))
118 ([watch] menu-item "Watch Expression" gud-watch
119 :enable (and (not gud-running)
120 (eq gud-minor-mode 'gdba)))
121 ([finish] menu-item "Finish Function" gud-finish
122 :enable (and (not gud-running)
124 '(gdba gdb xdb jdb pdb bashdb))))
125 ([stepi] menu-item "Step Instruction" gud-stepi
126 :enable (and (not gud-running)
129 ([nexti] menu-item "Next Instruction" gud-nexti
130 :enable (and (not gud-running)
133 ([step] menu-item "Step Line" gud-step
134 :enable (not gud-running))
135 ([next] menu-item "Next Line" gud-next
136 :enable (not gud-running))
137 ([cont] menu-item "Continue" gud-cont
138 :enable (not gud-running)))
139 "Menu for `gud-mode'."
142 (easy-mmode-defmap gud-minor-mode-map
143 `(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
144 "Map used in visited files.")
146 (let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
147 (if m (setcdr m gud-minor-mode-map)
148 (push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
151 ;; Will inherit from comint-mode via define-derived-mode.
153 "`gud-mode' keymap.")
155 (defvar gud-tool-bar-map
156 (if (display-graphic-p)
157 (let ((map (make-sparse-keymap)))
158 (dolist (x '((gud-break . "gud-break")
159 (gud-remove . "gud-remove")
160 (gud-print . "gud-print")
161 (gud-watch . "gud-watch")
162 (gud-run . "gud-run")
163 (gud-until . "gud-until")
164 (gud-cont . "gud-cont")
165 (gud-step . "gud-step")
166 (gud-next . "gud-next")
167 (gud-finish . "gud-finish")
168 (gud-stepi . "gud-stepi")
169 (gud-nexti . "gud-nexti")
171 (gud-down . "gud-down"))
173 (tool-bar-local-item-from-menu
174 (car x) (cdr x) map gud-minor-mode-map)))))
176 (defun gud-file-name (f)
177 "Transform a relative file name to an absolute file name.
178 Uses `gud-<MINOR-MODE>-directories' to find the source files."
179 (if (file-exists-p f) (expand-file-name f)
180 (let ((directories (gud-val 'directories))
183 (let ((path (expand-file-name f (car directories))))
184 (if (file-exists-p path)
187 (setq directories (cdr directories)))
190 (defun gud-find-file (file)
191 ;; Don't get confused by double slashes in the name that comes from GDB.
192 (while (string-match "//+" file)
193 (setq file (replace-match "/" t t file)))
194 (let ((minor-mode gud-minor-mode)
195 (buf (funcall (or gud-find-file 'gud-file-name) file)))
197 (setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
199 ;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
200 (with-current-buffer buf
201 (set (make-local-variable 'gud-minor-mode) minor-mode)
202 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
203 (make-local-variable 'gud-keep-buffer))
206 ;; ======================================================================
207 ;; command definition
209 ;; This macro is used below to define some basic debugger interface commands.
210 ;; Of course you may use `gud-def' with any other debugger command, including
211 ;; user defined ones.
213 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
214 ;; which defines FUNC to send the command NAME to the debugger, gives
215 ;; it the docstring DOC, and binds that function to KEY in the GUD
216 ;; major mode. The function is also bound in the global keymap with the
219 (defmacro gud-def (func cmd key &optional doc)
220 "Define FUNC to be a command sending STR and bound to KEY, with
221 optional doc string DOC. Certain %-escapes in the string arguments
222 are interpreted specially if present. These are:
224 %f name (without directory) of current source file.
225 %F name (without directory or extension) of current source file.
226 %d directory of current source file.
227 %l number of current source line
228 %e text of the C lvalue or function-call expression surrounding point.
229 %a text of the hexadecimal address surrounding point
230 %p prefix argument to the command (if any) as a number
232 The `current' source file is the file of the current buffer (if
233 we're in a C file) or the source file current at the last break or
234 step (if we're in the GUD buffer).
235 The `current' line is that of the current buffer (if we're in a
236 source file) or the source line number at the last break or step (if
237 we're in the GUD buffer)."
240 ,@(if doc (list doc))
245 ,(if key `(local-set-key ,(concat "\C-c" key) ',func))
246 ,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
248 ;; Where gud-display-frame should put the debugging arrow; a cons of
249 ;; (filename . line-number). This is set by the marker-filter, which scans
250 ;; the debugger's output for indications of the current program counter.
251 (defvar gud-last-frame nil)
253 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
254 ;; the last frame, even if it's been called before and gud-last-frame has
256 (defvar gud-last-last-frame nil)
258 ;; All debugger-specific information is collected here.
259 ;; Here's how it works, in case you ever need to add a debugger to the mode.
261 ;; Each entry must define the following at startup:
264 ;; comint-prompt-regexp
265 ;; gud-<name>-massage-args
266 ;; gud-<name>-marker-filter
267 ;; gud-<name>-find-file
269 ;; The job of the massage-args method is to modify the given list of
270 ;; debugger arguments before running the debugger.
272 ;; The job of the marker-filter method is to detect file/line markers in
273 ;; strings and set the global gud-last-frame to indicate what display
274 ;; action (if any) should be triggered by the marker. Note that only
275 ;; whatever the method *returns* is displayed in the buffer; thus, you
276 ;; can filter the debugger's output, interpreting some and passing on
279 ;; The job of the find-file method is to visit and return the buffer indicated
280 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
283 ;; ======================================================================
284 ;; speedbar support functions and variables.
285 (eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
287 (defvar gud-last-speedbar-buffer nil
288 "The last GUD buffer used.")
290 (defvar gud-last-speedbar-stackframe nil
291 "Description of the currently displayed GUD stack.
292 t means that there is no stack, and we are in display-file mode.")
294 (defvar gud-speedbar-key-map nil
295 "Keymap used when in the buffers display mode.")
297 (defun gud-install-speedbar-variables ()
298 "Install those variables used by speedbar to enhance gud/gdb."
299 (if gud-speedbar-key-map
301 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
303 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
304 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
305 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)))
307 (defvar gud-speedbar-menu-items
308 ;; Note to self. Add expand, and turn off items when not available.
309 '(["Jump to stack frame" speedbar-edit-line t])
310 "Additional menu items to add to the speedbar frame.")
312 ;; Make sure our special speedbar mode is loaded
313 (if (featurep 'speedbar)
314 (gud-install-speedbar-variables)
315 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
317 (defun gud-speedbar-buttons (buffer)
318 "Create a speedbar display based on the current state of GUD.
319 If the GUD BUFFER is not running a supported debugger, then turn
320 off the specialized speedbar mode."
321 (let ((minor-mode (with-current-buffer buffer gud-minor-mode)))
323 ((eq minor-mode 'gdba)
324 (when (or gdb-var-changed
326 (goto-char (point-min))
327 (let ((case-fold-search t))
328 (looking-at "Watch Expressions:")))))
330 (insert "Watch Expressions:\n")
331 (let ((var-list gdb-var-list))
333 (let* ((depth 0) (start 0) (char ?+)
334 (var (car var-list)) (varnum (nth 1 var)))
335 (while (string-match "\\." varnum start)
336 (setq depth (1+ depth)
337 start (1+ (match-beginning 0))))
338 (if (equal (nth 2 var) "0")
339 (speedbar-make-tag-line 'bracket ?? nil nil
340 (concat (car var) "\t" (nth 4 var))
344 gdb-show-changed-values)
345 'font-lock-warning-face
347 (if (and (cadr var-list)
348 (string-match varnum (cadr (cadr var-list))))
350 (speedbar-make-tag-line 'bracket char
351 'gdb-speedbar-expand-node varnum
352 (concat (car var) "\t" (nth 3 var))
355 (setq var-list (cdr var-list))))
356 (setq gdb-var-changed nil)))
357 (t (if (and (save-excursion
358 (goto-char (point-min))
359 (looking-at "Current Stack"))
360 (equal gud-last-last-frame gud-last-speedbar-stackframe))
362 (setq gud-last-speedbar-buffer buffer)
363 (let ((gud-frame-list
364 (cond ((eq minor-mode 'gdb)
365 (gud-gdb-get-stackframe buffer))
366 ;; Add more debuggers here!
367 (t (speedbar-remove-localized-speedbar-support buffer)
370 (if (not gud-frame-list)
371 (insert "No Stack frames\n")
372 (insert "Current Stack:\n"))
373 (dolist (frame gud-frame-list)
374 (insert (nth 1 frame) ":\n")
375 (if (= (length frame) 2)
377 ; (speedbar-insert-button "[?]"
378 ; 'speedbar-button-face
380 (speedbar-insert-button (car frame)
381 'speedbar-directory-face
383 ; (speedbar-insert-button "[+]"
384 ; 'speedbar-button-face
385 ; 'speedbar-highlight-face
386 ; 'gud-gdb-get-scope-data
388 (speedbar-insert-button (car frame)
390 'speedbar-highlight-face
391 (cond ((memq minor-mode '(gdba gdb))
392 'gud-gdb-goto-stackframe)
393 (t (error "Should never be here")))
395 ; (let ((selected-frame
396 ; (cond ((eq ff 'gud-gdb-find-file)
397 ; (gud-gdb-selected-frame-info buffer))
398 ; (t (error "Should never be here"))))))
400 (setq gud-last-speedbar-stackframe gud-last-last-frame))))))
403 ;; ======================================================================
406 ;; History of argument lists passed to gdb.
407 (defvar gud-gdb-history nil)
409 (defcustom gud-gdb-command-name "gdb --fullname"
410 "Default command to execute an executable under the GDB debugger."
414 (defvar gud-gdb-marker-regexp
415 ;; This used to use path-separator instead of ":";
416 ;; however, we found that on both Windows 32 and MSDOS
417 ;; a colon is correct here.
418 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
419 "\\([0-9]*\\)" ":" ".*\n"))
421 ;; There's no guarantee that Emacs will hand the filter the entire
422 ;; marker at once; it could be broken up across several strings. We
423 ;; might even receive a big chunk with several markers in it. If we
424 ;; receive a chunk of text which looks like it might contain the
425 ;; beginning of a marker, we save it here between calls to the
427 (defvar gud-marker-acc "")
428 (make-variable-buffer-local 'gud-marker-acc)
430 (defun gud-gdb-marker-filter (string)
431 (setq gud-marker-acc (concat gud-marker-acc string))
434 ;; Process all the complete markers in this chunk.
435 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
438 ;; Extract the frame position from the marker.
439 gud-last-frame (cons (match-string 1 gud-marker-acc)
440 (string-to-int (match-string 2 gud-marker-acc)))
442 ;; Append any text before the marker to the output we're going
443 ;; to return - we don't include the marker in this text.
444 output (concat output
445 (substring gud-marker-acc 0 (match-beginning 0)))
447 ;; Set the accumulator to the remaining text.
448 gud-marker-acc (substring gud-marker-acc (match-end 0))))
450 ;; Does the remaining text look like it might end with the
451 ;; beginning of another marker? If it does, then keep it in
452 ;; gud-marker-acc until we receive the rest of it. Since we
453 ;; know the full marker regexp above failed, it's pretty simple to
454 ;; test for marker starts.
455 (if (string-match "\032.*\\'" gud-marker-acc)
457 ;; Everything before the potential marker start can be output.
458 (setq output (concat output (substring gud-marker-acc
459 0 (match-beginning 0))))
461 ;; Everything after, we save, to combine with later input.
463 (substring gud-marker-acc (match-beginning 0))))
465 (setq output (concat output gud-marker-acc)
470 (easy-mmode-defmap gud-minibuffer-local-map
471 '(("\C-i" . comint-dynamic-complete-filename))
472 "Keymap for minibuffer prompting of gud startup command."
473 :inherit minibuffer-local-map)
475 (defun gud-query-cmdline (minor-mode &optional init)
476 (let* ((hist-sym (gud-symbol 'history nil minor-mode))
477 (cmd-name (gud-val 'command-name minor-mode)))
478 (unless (boundp hist-sym) (set hist-sym nil))
479 (read-from-minibuffer
480 (format "Run %s (like this): " minor-mode)
481 (or (car-safe (symbol-value hist-sym))
482 (concat (or cmd-name (symbol-name minor-mode))
486 (dolist (f (directory-files default-directory) file)
487 (if (and (file-executable-p f)
488 (not (file-directory-p f))
490 (file-newer-than-file-p f file)))
492 gud-minibuffer-local-map nil
496 (defun gdb (command-line)
497 "Run gdb on program FILE in buffer *gud-FILE*.
498 The directory containing FILE becomes the initial working directory
499 and source-file directory for your debugger."
500 (interactive (list (gud-query-cmdline 'gdb)))
502 (gud-common-init command-line nil 'gud-gdb-marker-filter)
503 (set (make-local-variable 'gud-minor-mode) 'gdb)
505 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
506 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
507 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
508 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
509 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
510 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
511 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
512 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
513 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
514 (gud-def gud-jump "tbreak %f:%l\njump %f:%l" "\C-j" "Relocate execution address to line at point in source buffer.")
516 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
517 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
518 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
519 (gud-def gud-until "until %l" "\C-u" "Continue to current line.")
520 (gud-def gud-run "run" nil "Run the program.")
522 (local-set-key "\C-i" 'gud-gdb-complete-command)
523 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
524 (setq paragraph-start comint-prompt-regexp)
525 (run-hooks 'gdb-mode-hook)
528 ;; One of the nice features of GDB is its impressive support for
529 ;; context-sensitive command completion. We preserve that feature
530 ;; in the GUD buffer by using a GDB command designed just for Emacs.
532 ;; The completion process filter indicates when it is finished.
533 (defvar gud-gdb-fetch-lines-in-progress)
535 ;; Since output may arrive in fragments we accumulate partials strings here.
536 (defvar gud-gdb-fetch-lines-string)
538 ;; We need to know how much of the completion to chop off.
539 (defvar gud-gdb-fetch-lines-break)
541 ;; The completion list is constructed by the process filter.
542 (defvar gud-gdb-fetched-lines)
544 (defvar gud-comint-buffer nil)
546 (defun gud-gdb-complete-command ()
547 "Perform completion on the GDB command preceding point.
548 This is implemented using the GDB `complete' command which isn't
549 available with older versions of GDB."
552 (command (buffer-substring (comint-line-beginning-position) end))
554 ;; Find the word break. This match will always succeed.
555 (and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
556 (substring command (match-beginning 2))))
558 (gud-gdb-run-command-fetch-lines (concat "complete " command)
560 ;; From string-match above.
561 (match-beginning 2))))
562 ;; Protect against old versions of GDB.
564 (string-match "^Undefined command: \"complete\"" (car complete-list))
565 (error "This version of GDB doesn't support the `complete' command"))
566 ;; Sort the list like readline.
567 (setq complete-list (sort complete-list (function string-lessp)))
568 ;; Remove duplicates.
569 (let ((first complete-list)
570 (second (cdr complete-list)))
572 (if (string-equal (car first) (car second))
573 (setcdr first (setq second (cdr second)))
575 second (cdr second)))))
576 ;; Add a trailing single quote if there is a unique completion
577 ;; and it contains an odd number of unquoted single quotes.
578 (and (= (length complete-list) 1)
579 (let ((str (car complete-list))
582 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
583 (setq count (1+ count)
585 (and (= (mod count 2) 1)
586 (setq complete-list (list (concat str "'"))))))
587 ;; Let comint handle the rest.
588 (comint-dynamic-simple-complete command-word complete-list)))
590 ;; The completion process filter is installed temporarily to slurp the
591 ;; output of GDB up to the next prompt and build the completion list.
592 (defun gud-gdb-fetch-lines-filter (string filter)
593 "Filter used to read the list of lines output by a command.
594 STRING is the output to filter.
595 It is passed through FILTER before we look at it."
596 (setq string (funcall filter string))
597 (setq string (concat gud-gdb-fetch-lines-string string))
598 (while (string-match "\n" string)
599 (push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
600 gud-gdb-fetched-lines)
601 (setq string (substring string (match-end 0))))
602 (if (string-match comint-prompt-regexp string)
604 (setq gud-gdb-fetch-lines-in-progress nil)
607 (setq gud-gdb-fetch-lines-string string)
610 ;; gdb speedbar functions
612 (defun gud-gdb-goto-stackframe (text token indent)
613 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
614 (speedbar-with-attached-buffer
615 (gud-basic-call (concat "server frame " (nth 1 token)))
618 (defvar gud-gdb-fetched-stack-frame nil
619 "Stack frames we are fetching from GDB.")
621 ;(defun gud-gdb-get-scope-data (text token indent)
622 ; ;; checkdoc-params: (indent)
623 ; "Fetch data associated with a stack frame, and expand/contract it.
624 ;Data to do this is retrieved from TEXT and TOKEN."
625 ; (let ((args nil) (scope nil))
626 ; (gud-gdb-run-command-fetch-lines "info args")
628 ; (gud-gdb-run-command-fetch-lines "info local")
632 (defun gud-gdb-get-stackframe (buffer)
633 "Extract the current stack frame out of the GUD GDB BUFFER."
635 (fetched-stack-frame-list
636 (gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
637 (if (and (car fetched-stack-frame-list)
638 (string-match "No stack" (car fetched-stack-frame-list)))
639 ;; Go into some other mode???
641 (dolist (e fetched-stack-frame-list)
642 (let ((name nil) (num nil))
644 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
645 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
646 (if (not (string-match
647 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
650 (list (nth 0 (car newlst))
653 (match-string 2 e))))
654 (setq num (match-string 1 e)
655 name (match-string 2 e))
659 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
660 (list name num (match-string 1 e)
666 ;(defun gud-gdb-selected-frame-info (buffer)
667 ; "Learn GDB information for the currently selected stack frame in BUFFER."
670 (defun gud-gdb-run-command-fetch-lines (command buffer &optional skip)
671 "Run COMMAND, and return the list of lines it outputs.
672 BUFFER is the GUD buffer in which to run the command.
673 SKIP is the number of chars to skip on each lines, it defaults to 0."
674 (with-current-buffer buffer
676 (goto-char (point-max))
678 (not (looking-at comint-prompt-regexp)))
680 ;; Much of this copied from GDB complete, but I'm grabbing the stack
682 (let ((gud-gdb-fetch-lines-in-progress t)
683 (gud-gdb-fetched-lines nil)
684 (gud-gdb-fetch-lines-string nil)
685 (gud-gdb-fetch-lines-break (or skip 0))
687 `(lambda (string) (gud-gdb-fetch-lines-filter string ',gud-marker-filter))))
688 ;; Issue the command to GDB.
689 (gud-basic-call command)
691 (while gud-gdb-fetch-lines-in-progress
692 (accept-process-output (get-buffer-process buffer)))
693 (nreverse gud-gdb-fetched-lines)))))
696 ;; ======================================================================
699 ;; History of argument lists passed to sdb.
700 (defvar gud-sdb-history nil)
702 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
703 "If nil, we're on a System V Release 4 and don't need the tags hack.")
705 (defvar gud-sdb-lastfile nil)
707 (defun gud-sdb-marker-filter (string)
709 (if gud-marker-acc (concat gud-marker-acc string) string))
711 ;; Process all complete markers in this chunk
714 ;; System V Release 3.2 uses this format
715 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
716 gud-marker-acc start)
718 (cons (match-string 3 gud-marker-acc)
719 (string-to-int (match-string 4 gud-marker-acc)))))
720 ;; System V Release 4.0 quite often clumps two lines together
721 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
722 gud-marker-acc start)
723 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc))
725 (cons gud-sdb-lastfile
726 (string-to-int (match-string 3 gud-marker-acc)))))
727 ;; System V Release 4.0
728 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
729 gud-marker-acc start)
730 (setq gud-sdb-lastfile (match-string 2 gud-marker-acc)))
731 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
732 gud-marker-acc start))
734 (cons gud-sdb-lastfile
735 (string-to-int (match-string 1 gud-marker-acc)))))
737 (setq gud-sdb-lastfile nil)))
738 (setq start (match-end 0)))
740 ;; Search for the last incomplete line in this chunk
741 (while (string-match "\n" gud-marker-acc start)
742 (setq start (match-end 0)))
744 ;; If we have an incomplete line, store it in gud-marker-acc.
745 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
748 (defun gud-sdb-find-file (f)
749 (if gud-sdb-needs-tags (find-tag-noselect f) (find-file-noselect f)))
752 (defun sdb (command-line)
753 "Run sdb on program FILE in buffer *gud-FILE*.
754 The directory containing FILE becomes the initial working directory
755 and source-file directory for your debugger."
756 (interactive (list (gud-query-cmdline 'sdb)))
758 (if (and gud-sdb-needs-tags
759 (not (and (boundp 'tags-file-name)
760 (stringp tags-file-name)
761 (file-exists-p tags-file-name))))
762 (error "The sdb support requires a valid tags table to work"))
764 (gud-common-init command-line nil 'gud-sdb-marker-filter 'gud-sdb-find-file)
765 (set (make-local-variable 'gud-minor-mode) 'sdb)
767 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
768 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
769 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
770 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
771 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
772 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
773 (gud-def gud-cont "c" "\C-r" "Continue with display.")
774 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
776 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
777 (setq paragraph-start comint-prompt-regexp)
778 (run-hooks 'sdb-mode-hook)
781 ;; ======================================================================
784 ;; History of argument lists passed to dbx.
785 (defvar gud-dbx-history nil)
787 (defcustom gud-dbx-directories nil
788 "*A list of directories that dbx should search for source code.
789 If nil, only source files in the program directory
790 will be known to dbx.
792 The file names should be absolute, or relative to the directory
793 containing the executable being debugged."
794 :type '(choice (const :tag "Current Directory" nil)
799 (defun gud-dbx-massage-args (file args)
800 (nconc (let ((directories gud-dbx-directories)
803 (setq result (cons (car directories) (cons "-I" result)))
804 (setq directories (cdr directories)))
808 (defun gud-dbx-marker-filter (string)
809 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
812 ;; Process all complete markers in this chunk.
813 (while (or (string-match
814 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
815 gud-marker-acc start)
817 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
818 gud-marker-acc start))
820 (cons (match-string 2 gud-marker-acc)
821 (string-to-int (match-string 1 gud-marker-acc)))
822 start (match-end 0)))
824 ;; Search for the last incomplete line in this chunk
825 (while (string-match "\n" gud-marker-acc start)
826 (setq start (match-end 0)))
828 ;; If the incomplete line APPEARS to begin with another marker, keep it
829 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
830 ;; unnecessary concat during the next call.
832 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
833 (substring gud-marker-acc (match-beginning 0))
837 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
838 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
840 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
841 ;; We haven't tested gud on this system:
842 (string-match "^mips-[^-]*-riscos" system-configuration)
843 ;; It's documented on OSF/1.3
844 (string-match "^mips-[^-]*-osf1" system-configuration)
845 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
846 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
848 (defvar gud-dbx-command-name
849 (concat "dbx" (if gud-mips-p " -emacs")))
851 ;; This is just like the gdb one except for the regexps since we need to cope
852 ;; with an optional breakpoint number in [] before the ^Z^Z
853 (defun gud-mipsdbx-marker-filter (string)
854 (setq gud-marker-acc (concat gud-marker-acc string))
857 ;; Process all the complete markers in this chunk.
859 ;; This is like th gdb marker but with an optional
860 ;; leading break point number like `[1] '
861 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
865 ;; Extract the frame position from the marker.
867 (cons (match-string 1 gud-marker-acc)
868 (string-to-int (match-string 2 gud-marker-acc)))
870 ;; Append any text before the marker to the output we're going
871 ;; to return - we don't include the marker in this text.
872 output (concat output
873 (substring gud-marker-acc 0 (match-beginning 0)))
875 ;; Set the accumulator to the remaining text.
876 gud-marker-acc (substring gud-marker-acc (match-end 0))))
878 ;; Does the remaining text look like it might end with the
879 ;; beginning of another marker? If it does, then keep it in
880 ;; gud-marker-acc until we receive the rest of it. Since we
881 ;; know the full marker regexp above failed, it's pretty simple to
882 ;; test for marker starts.
883 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
885 ;; Everything before the potential marker start can be output.
886 (setq output (concat output (substring gud-marker-acc
887 0 (match-beginning 0))))
889 ;; Everything after, we save, to combine with later input.
891 (substring gud-marker-acc (match-beginning 0))))
893 (setq output (concat output gud-marker-acc)
898 ;; The dbx in IRIX is a pain. It doesn't print the file name when
899 ;; stopping at a breakpoint (but you do get it from the `up' and
900 ;; `down' commands...). The only way to extract the information seems
901 ;; to be with a `file' command, although the current line number is
902 ;; available in $curline. Thus we have to look for output which
903 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
904 ;; to output the information we want with a combination of the
905 ;; `printf' and `file' commands as a pseudo marker which we can
906 ;; recognise next time through the marker-filter. This would be like
907 ;; the gdb marker but you can't get the file name without a newline...
908 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
909 ;; number rather than a line number etc. Maybe this could be made to
910 ;; work by listing all the breakpoints and picking the one(s) with the
911 ;; correct line number, but life's too short.
912 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
915 (and (string-match "^mips-[^-]*-irix" system-configuration)
916 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
917 "Non-nil to assume the interface appropriate for IRIX dbx.
918 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
919 a better solution in 6.1 upwards.")
920 (defvar gud-dbx-use-stopformat-p
921 (string-match "irix[6-9]\\.[1-9]" system-configuration)
922 "Non-nil to use the dbx feature present at least from Irix 6.1
923 whereby $stopformat=1 produces an output format compatiable with
924 `gud-dbx-marker-filter'.")
925 ;; [Irix dbx seems to be a moving target. The dbx output changed
926 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
927 ;; the output from `up' is no longer spotted by gud (and it's probably
928 ;; not distinctive enough to try to match it -- use C-<, C->
929 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
930 ;; `long long'(why?!), so the printf stuff needed changing. The line
931 ;; number was cast to `long' as a compromise between the new `long
932 ;; long' and the original `int'. This is reported not to work in 6.2,
933 ;; so it's changed back to int -- don't make your sources too long.
934 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
935 ;; whereby `set $stopformat=1' reportedly produces output compatible
936 ;; with `gud-dbx-marker-filter', which we prefer.
938 ;; The process filter is also somewhat
939 ;; unreliable, sometimes not spotting the markers; I don't know
940 ;; whether there's anything that can be done about that. It would be
941 ;; much better if SGI could be persuaded to (re?)instate the MIPS
942 ;; -emacs flag for gdb-like output (which ought to be possible as most
943 ;; of the communication I've had over it has been from sgi.com).]
945 ;; this filter is influenced by the xdb one rather than the gdb one
946 (defun gud-irixdbx-marker-filter (string)
947 (let (result (case-fold-search nil))
948 (if (or (string-match comint-prompt-regexp string)
949 (string-match ".*\012" string))
950 (setq result (concat gud-marker-acc string)
952 (setq gud-marker-acc (concat gud-marker-acc string)))
955 ;; look for breakpoint or signal indication e.g.:
956 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
957 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
958 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
960 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
962 ;; prod dbx into printing out the line number and file
963 ;; name in a form we can grok as below
964 (process-send-string (get-buffer-process gud-comint-buffer)
965 "printf \"\032\032%1d:\",(int)$curline;file\n"))
966 ;; look for result of, say, "up" e.g.:
967 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
968 ;; (this will also catch one of the lines printed by "where")
970 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
972 (let ((file (match-string 1 result)))
973 (if (file-exists-p file)
975 (cons (match-string 1 result)
976 (string-to-int (match-string 2 result))))))
978 ((string-match ; kluged-up marker as above
979 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
980 (let ((file (gud-file-name (match-string 2 result))))
981 (if (and file (file-exists-p file))
984 (string-to-int (match-string 1 result))))))
985 (setq result (substring result 0 (match-beginning 0))))))
988 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
989 "Non-nil means to assume the interface approriate for DG/UX dbx.
990 This was tested using R4.11.")
992 ;; There are a couple of differences between DG's dbx output and normal
993 ;; dbx output which make it nontrivial to integrate this into the
994 ;; standard dbx-marker-filter (mainly, there are a different number of
995 ;; backreferences). The markers look like:
997 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
999 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
1002 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
1006 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
1008 ;; from up/down/where.
1010 (defun gud-dguxdbx-marker-filter (string)
1011 (setq gud-marker-acc (if gud-marker-acc
1012 (concat gud-marker-acc string)
1014 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
1015 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
1017 ;; Process all complete markers in this chunk.
1018 (while (string-match re gud-marker-acc start)
1019 (setq gud-last-frame
1020 (cons (match-string 4 gud-marker-acc)
1021 (string-to-int (match-string 3 gud-marker-acc)))
1022 start (match-end 0)))
1024 ;; Search for the last incomplete line in this chunk
1025 (while (string-match "\n" gud-marker-acc start)
1026 (setq start (match-end 0)))
1028 ;; If the incomplete line APPEARS to begin with another marker, keep it
1029 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
1030 ;; unnecessary concat during the next call.
1031 (setq gud-marker-acc
1032 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
1033 (substring gud-marker-acc (match-beginning 0))
1038 (defun dbx (command-line)
1039 "Run dbx on program FILE in buffer *gud-FILE*.
1040 The directory containing FILE becomes the initial working directory
1041 and source-file directory for your debugger."
1042 (interactive (list (gud-query-cmdline 'dbx)))
1046 (gud-common-init command-line nil 'gud-mipsdbx-marker-filter))
1048 (gud-common-init command-line 'gud-dbx-massage-args
1049 'gud-irixdbx-marker-filter))
1051 (gud-common-init command-line 'gud-dbx-massage-args
1052 'gud-dguxdbx-marker-filter))
1054 (gud-common-init command-line 'gud-dbx-massage-args
1055 'gud-dbx-marker-filter)))
1057 (set (make-local-variable 'gud-minor-mode) 'dbx)
1061 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1062 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1063 (gud-def gud-break "stop at \"%f\":%l"
1064 "\C-b" "Set breakpoint at current line.")
1065 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1067 (gud-def gud-break "stop at \"%d%f\":%l"
1068 "\C-b" "Set breakpoint at current line.")
1069 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1070 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1071 "<" "Up (numeric arg) stack frames.")
1072 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1073 ">" "Down (numeric arg) stack frames.")
1074 ;; Make dbx give out the source location info that we need.
1075 (process-send-string (get-buffer-process gud-comint-buffer)
1076 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1078 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1079 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1080 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1081 "\C-b" "Set breakpoint at current line.")
1082 (if gud-dbx-use-stopformat-p
1083 (process-send-string (get-buffer-process gud-comint-buffer)
1084 "set $stopformat=1\n"))))
1086 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1087 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1088 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1089 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1090 (gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
1091 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1092 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1093 (gud-def gud-run "run" nil "Run the program.")
1095 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1096 (setq paragraph-start comint-prompt-regexp)
1097 (run-hooks 'dbx-mode-hook)
1100 ;; ======================================================================
1101 ;; xdb (HP PARISC debugger) functions
1103 ;; History of argument lists passed to xdb.
1104 (defvar gud-xdb-history nil)
1106 (defcustom gud-xdb-directories nil
1107 "*A list of directories that xdb should search for source code.
1108 If nil, only source files in the program directory
1109 will be known to xdb.
1111 The file names should be absolute, or relative to the directory
1112 containing the executable being debugged."
1113 :type '(choice (const :tag "Current Directory" nil)
1118 (defun gud-xdb-massage-args (file args)
1119 (nconc (let ((directories gud-xdb-directories)
1122 (setq result (cons (car directories) (cons "-d" result)))
1123 (setq directories (cdr directories)))
1127 ;; xdb does not print the lines all at once, so we have to accumulate them
1128 (defun gud-xdb-marker-filter (string)
1130 (if (or (string-match comint-prompt-regexp string)
1131 (string-match ".*\012" string))
1132 (setq result (concat gud-marker-acc string)
1134 (setq gud-marker-acc (concat gud-marker-acc string)))
1136 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1138 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1140 (let ((line (string-to-int (match-string 2 result)))
1141 (file (gud-file-name (match-string 1 result))))
1143 (setq gud-last-frame (cons file line))))))
1147 (defun xdb (command-line)
1148 "Run xdb on program FILE in buffer *gud-FILE*.
1149 The directory containing FILE becomes the initial working directory
1150 and source-file directory for your debugger.
1152 You can set the variable 'gud-xdb-directories' to a list of program source
1153 directories if your program contains sources from more than one directory."
1154 (interactive (list (gud-query-cmdline 'xdb)))
1156 (gud-common-init command-line 'gud-xdb-massage-args
1157 'gud-xdb-marker-filter)
1158 (set (make-local-variable 'gud-minor-mode) 'xdb)
1160 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1161 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1162 "Set temporary breakpoint at current line.")
1163 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1164 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1165 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1166 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1167 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1168 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1169 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1170 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1172 (setq comint-prompt-regexp "^>")
1173 (setq paragraph-start comint-prompt-regexp)
1174 (run-hooks 'xdb-mode-hook))
1176 ;; ======================================================================
1179 ;; History of argument lists passed to perldb.
1180 (defvar gud-perldb-history nil)
1182 (defun gud-perldb-massage-args (file args)
1183 "Convert a command line as would be typed normally to run perldb
1184 into one that invokes an Emacs-enabled debugging session.
1185 \"-emacs\" is inserted where it will be $ARGV[0] (see perl5db.pl)."
1186 ;; FIXME: what if the command is `make perldb' and doesn't accept those extra
1188 (let* ((new-args nil)
1190 (shift (lambda () (push (pop args) new-args))))
1192 ;; Pass all switches and -e scripts through.
1194 (string-match "^-" (car args))
1195 (not (equal "-" (car args)))
1196 (not (equal "--" (car args))))
1197 (when (equal "-e" (car args))
1198 ;; -e goes with the next arg, so shift one extra.
1200 ;; -e as the last arg is an error in Perl.
1201 (error "No code specified for -e"))
1207 (string-match "^-" (car args)))
1208 (error "Can't use stdin as the script to debug"))
1209 ;; This is the program name.
1212 ;; If -e specified, make sure there is a -- so -emacs is not taken
1214 (if (and args (equal "--" (car args)))
1216 (and seen-e (push "--" new-args)))
1218 (push "-emacs" new-args)
1222 (nreverse new-args)))
1224 ;; There's no guarantee that Emacs will hand the filter the entire
1225 ;; marker at once; it could be broken up across several strings. We
1226 ;; might even receive a big chunk with several markers in it. If we
1227 ;; receive a chunk of text which looks like it might contain the
1228 ;; beginning of a marker, we save it here between calls to the
1230 (defun gud-perldb-marker-filter (string)
1231 (setq gud-marker-acc (concat gud-marker-acc string))
1234 ;; Process all the complete markers in this chunk.
1235 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1239 ;; Extract the frame position from the marker.
1241 (cons (match-string 1 gud-marker-acc)
1242 (string-to-int (match-string 3 gud-marker-acc)))
1244 ;; Append any text before the marker to the output we're going
1245 ;; to return - we don't include the marker in this text.
1246 output (concat output
1247 (substring gud-marker-acc 0 (match-beginning 0)))
1249 ;; Set the accumulator to the remaining text.
1250 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1252 ;; Does the remaining text look like it might end with the
1253 ;; beginning of another marker? If it does, then keep it in
1254 ;; gud-marker-acc until we receive the rest of it. Since we
1255 ;; know the full marker regexp above failed, it's pretty simple to
1256 ;; test for marker starts.
1257 (if (string-match "\032.*\\'" gud-marker-acc)
1259 ;; Everything before the potential marker start can be output.
1260 (setq output (concat output (substring gud-marker-acc
1261 0 (match-beginning 0))))
1263 ;; Everything after, we save, to combine with later input.
1264 (setq gud-marker-acc
1265 (substring gud-marker-acc (match-beginning 0))))
1267 (setq output (concat output gud-marker-acc)
1272 (defcustom gud-perldb-command-name "perl -d"
1273 "Default command to execute a Perl script under debugger."
1278 (defun perldb (command-line)
1279 "Run perldb on program FILE in buffer *gud-FILE*.
1280 The directory containing FILE becomes the initial working directory
1281 and source-file directory for your debugger."
1283 (list (gud-query-cmdline 'perldb
1284 (concat (or (buffer-file-name) "-e 0") " "))))
1286 (gud-common-init command-line 'gud-perldb-massage-args
1287 'gud-perldb-marker-filter)
1288 (set (make-local-variable 'gud-minor-mode) 'perldb)
1290 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1291 (gud-def gud-remove "B %l" "\C-d" "Remove breakpoint at current line")
1292 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1293 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1294 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1295 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1296 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1297 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1298 (gud-def gud-print "p %e" "\C-p" "Evaluate perl expression at point.")
1299 (gud-def gud-until "c %l" "\C-u" "Continue to current line.")
1302 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1303 (setq paragraph-start comint-prompt-regexp)
1304 (run-hooks 'perldb-mode-hook))
1306 ;; ======================================================================
1307 ;; pdb (Python debugger) functions
1309 ;; History of argument lists passed to pdb.
1310 (defvar gud-pdb-history nil)
1312 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1313 ;; Either file or function name may be omitted: "> <string>(0)?()"
1314 (defvar gud-pdb-marker-regexp
1315 "^> \\([-a-zA-Z0-9_/.:\\]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1316 (defvar gud-pdb-marker-regexp-file-group 1)
1317 (defvar gud-pdb-marker-regexp-line-group 2)
1318 (defvar gud-pdb-marker-regexp-fnname-group 3)
1320 (defvar gud-pdb-marker-regexp-start "^> ")
1322 ;; There's no guarantee that Emacs will hand the filter the entire
1323 ;; marker at once; it could be broken up across several strings. We
1324 ;; might even receive a big chunk with several markers in it. If we
1325 ;; receive a chunk of text which looks like it might contain the
1326 ;; beginning of a marker, we save it here between calls to the
1328 (defun gud-pdb-marker-filter (string)
1329 (setq gud-marker-acc (concat gud-marker-acc string))
1332 ;; Process all the complete markers in this chunk.
1333 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1336 ;; Extract the frame position from the marker.
1338 (let ((file (match-string gud-pdb-marker-regexp-file-group
1340 (line (string-to-int
1341 (match-string gud-pdb-marker-regexp-line-group
1343 (if (string-equal file "<string>")
1347 ;; Output everything instead of the below
1348 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1349 ;; ;; Append any text before the marker to the output we're going
1350 ;; ;; to return - we don't include the marker in this text.
1351 ;; output (concat output
1352 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1354 ;; Set the accumulator to the remaining text.
1355 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1357 ;; Does the remaining text look like it might end with the
1358 ;; beginning of another marker? If it does, then keep it in
1359 ;; gud-marker-acc until we receive the rest of it. Since we
1360 ;; know the full marker regexp above failed, it's pretty simple to
1361 ;; test for marker starts.
1362 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1364 ;; Everything before the potential marker start can be output.
1365 (setq output (concat output (substring gud-marker-acc
1366 0 (match-beginning 0))))
1368 ;; Everything after, we save, to combine with later input.
1369 (setq gud-marker-acc
1370 (substring gud-marker-acc (match-beginning 0))))
1372 (setq output (concat output gud-marker-acc)
1377 (defcustom gud-pdb-command-name "pdb"
1378 "File name for executing the Python debugger.
1379 This should be an executable on your path, or an absolute file name."
1384 (defun pdb (command-line)
1385 "Run pdb on program FILE in buffer `*gud-FILE*'.
1386 The directory containing FILE becomes the initial working directory
1387 and source-file directory for your debugger."
1389 (list (gud-query-cmdline 'pdb)))
1391 (gud-common-init command-line nil 'gud-pdb-marker-filter)
1392 (set (make-local-variable 'gud-minor-mode) 'pdb)
1394 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1395 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
1396 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1397 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1398 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1399 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1400 (gud-def gud-up "up" "<" "Up one stack frame.")
1401 (gud-def gud-down "down" ">" "Down one stack frame.")
1402 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1404 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1406 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1407 (setq comint-prompt-regexp "^(Pdb) *")
1408 (setq paragraph-start comint-prompt-regexp)
1409 (run-hooks 'pdb-mode-hook))
1411 ;; ======================================================================
1415 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1416 ;; Zoltan Kemenczy <zoltan@ieee.org;zkemenczy@rim.net>
1418 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1419 ;; UPDATED: Nov 11, 2001 Zoltan Kemenczy
1420 ;; Dec 10, 2002 Zoltan Kemenczy - added nested class support
1422 ;; INVOCATION NOTES:
1424 ;; You invoke jdb-mode with:
1428 ;; It responds with:
1430 ;; Run jdb (like this): jdb
1432 ;; type any jdb switches followed by the name of the class you'd like to debug.
1433 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1434 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1435 ;; See the known problems section below for restrictions when specifying jdb
1436 ;; command line switches (search forward for '-classpath').
1438 ;; You should see something like the following:
1440 ;; Current directory is ~/src/java/hello/
1441 ;; Initializing jdb...
1442 ;; 0xed2f6628:class(hello)
1445 ;; To set an initial breakpoint try:
1447 ;; > stop in hello.main
1448 ;; Breakpoint set in hello.main
1451 ;; To execute the program type:
1456 ;; Breakpoint hit: running ...
1457 ;; hello.main (hello:12)
1459 ;; Type M-n to step over the current line and M-s to step into it. That,
1460 ;; along with the JDB 'help' command should get you started. The 'quit'
1461 ;; JDB command will get out out of the debugger. There is some truly
1462 ;; pathetic JDB documentation available at:
1464 ;; http://java.sun.com/products/jdk/1.1/debugging/
1466 ;; KNOWN PROBLEMS AND FIXME's:
1468 ;; Not sure what happens with inner classes ... haven't tried them.
1470 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1472 ;; You must not put whitespace between "-classpath" and the path to
1473 ;; search for java classes even though it is required when invoking jdb
1474 ;; from the command line. See gud-jdb-massage-args for details.
1475 ;; The same applies for "-sourcepath".
1477 ;; Note: The following applies only if `gud-jdb-use-classpath' is nil;
1478 ;; refer to the documentation of `gud-jdb-use-classpath' and
1479 ;; `gud-jdb-classpath',`gud-jdb-sourcepath' variables for information
1480 ;; on using the classpath for locating java source files.
1482 ;; If any of the source files in the directories listed in
1483 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1484 ;; every file ending in ".java" in these directories parses without error.
1486 ;; All the .java files in the directories in gud-jdb-directories are
1487 ;; syntactically analyzed each time gud jdb is invoked. It would be
1488 ;; nice to keep as much information as possible between runs. It would
1489 ;; be really nice to analyze the files only as neccessary (when the
1490 ;; source needs to be displayed.) I'm not sure to what extent the former
1491 ;; can be accomplished and I'm not sure the latter can be done at all
1492 ;; since I don't know of any general way to tell which .class files are
1493 ;; defined by which .java file without analyzing all the .java files.
1494 ;; If anyone knows why JavaSoft didn't put the source file names in
1495 ;; debuggable .class files please clue me in so I find something else
1496 ;; to be spiteful and bitter about.
1498 ;; ======================================================================
1499 ;; gud jdb variables and functions
1501 (defcustom gud-jdb-command-name "jdb"
1502 "Command that executes the Java debugger."
1506 (defcustom gud-jdb-use-classpath t
1507 "If non-nil, search for Java source files in classpath directories.
1508 The list of directories to search is the value of `gud-jdb-classpath'.
1509 The file pathname is obtained by converting the fully qualified
1510 class information output by jdb to a relative pathname and appending
1511 it to `gud-jdb-classpath' element by element until a match is found.
1513 This method has a significant jdb startup time reduction advantage
1514 since it does not require the scanning of all `gud-jdb-directories'
1515 and parsing all Java files for class information.
1517 Set to nil to use `gud-jdb-directories' to scan java sources for
1518 class information on jdb startup (original method)."
1522 (defvar gud-jdb-classpath nil
1523 "Java/jdb classpath directories list.
1524 If `gud-jdb-use-classpath' is non-nil, gud-jdb derives the `gud-jdb-classpath'
1525 list automatically using the following methods in sequence
1526 \(with subsequent successful steps overriding the results of previous
1529 1) Read the CLASSPATH environment variable,
1530 2) Read any \"-classpath\" argument used to run jdb,
1531 or detected in jdb output (e.g. if jdb is run by a script
1532 that echoes the actual jdb command before starting jdb)
1533 3) Send a \"classpath\" command to jdb and scan jdb output for
1534 classpath information if jdb is invoked with an \"-attach\" (to
1535 an already running VM) argument (This case typically does not
1536 have a \"-classpath\" command line argument - that is provided
1537 to the VM when it is started).
1539 Note that method 3 cannot be used with oldjdb (or Java 1 jdb) since
1540 those debuggers do not support the classpath command. Use 1) or 2).")
1542 (defvar gud-jdb-sourcepath nil
1543 "Directory list provided by an (optional) \"-sourcepath\" option to jdb.
1544 This list is prepended to `gud-jdb-classpath' to form the complete
1545 list of directories searched for source files.")
1547 (defvar gud-marker-acc-max-length 4000
1548 "Maximum number of debugger output characters to keep.
1549 This variable limits the size of `gud-marker-acc' which holds
1550 the most recent debugger output history while searching for
1551 source file information.")
1553 (defvar gud-jdb-history nil
1554 "History of argument lists passed to jdb.")
1557 ;; List of Java source file directories.
1558 (defvar gud-jdb-directories (list ".")
1559 "*A list of directories that gud jdb should search for source code.
1560 The file names should be absolute, or relative to the current
1563 The set of .java files residing in the directories listed are
1564 syntactically analyzed to determine the classes they define and the
1565 packages in which these classes belong. In this way gud jdb maps the
1566 package-qualified class names output by the jdb debugger to the source
1567 file from which the class originated. This allows gud mode to keep
1568 the source code display in sync with the debugging session.")
1570 (defvar gud-jdb-source-files nil
1571 "List of the java source files for this debugging session.")
1573 ;; Association list of fully qualified class names (package + class name)
1574 ;; and their source files.
1575 (defvar gud-jdb-class-source-alist nil
1576 "Association list of fully qualified class names and source files.")
1578 ;; This is used to hold a source file during analysis.
1579 (defvar gud-jdb-analysis-buffer nil)
1581 (defvar gud-jdb-classpath-string nil
1582 "Holds temporary classpath values.")
1584 (defun gud-jdb-build-source-files-list (path extn)
1585 "Return a list of java source files (absolute paths).
1586 PATH gives the directories in which to search for files with
1587 extension EXTN. Normally EXTN is given as the regular expression
1589 (apply 'nconc (mapcar (lambda (d)
1590 (when (file-directory-p d)
1591 (directory-files d t extn nil)))
1594 ;; Move point past whitespace.
1595 (defun gud-jdb-skip-whitespace ()
1596 (skip-chars-forward " \n\r\t\014"))
1598 ;; Move point past a "// <eol>" type of comment.
1599 (defun gud-jdb-skip-single-line-comment ()
1602 ;; Move point past a "/* */" or "/** */" type of comment.
1603 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1607 (if (eq (following-char) ?*)
1611 (if (eq (following-char) ?/)
1614 (throw 'break nil)))))
1617 ;; Move point past any number of consecutive whitespace chars and/or comments.
1618 (defun gud-jdb-skip-whitespace-and-comments ()
1619 (gud-jdb-skip-whitespace)
1624 (gud-jdb-skip-single-line-comment)
1625 (gud-jdb-skip-whitespace))
1626 ((looking-at "/\\*")
1627 (gud-jdb-skip-traditional-or-documentation-comment)
1628 (gud-jdb-skip-whitespace))
1629 (t (throw 'done nil))))))
1631 ;; Move point past things that are id-like. The intent is to skip regular
1632 ;; id's, such as class or interface names as well as package and interface
1634 (defun gud-jdb-skip-id-ish-thing ()
1635 (skip-chars-forward "^ /\n\r\t\014,;{"))
1637 ;; Move point past a string literal.
1638 (defun gud-jdb-skip-string-literal ()
1641 ((eq (following-char) ?\\)
1643 ((eq (following-char) ?\042))))
1647 ;; Move point past a character literal.
1648 (defun gud-jdb-skip-character-literal ()
1652 (if (eq (following-char) ?\\)
1654 (not (eq (following-char) ?\')))
1658 ;; Move point past the following block. There may be (legal) cruft before
1659 ;; the block's opening brace. There must be a block or it's the end of life
1660 ;; in petticoat junction.
1661 (defun gud-jdb-skip-block ()
1663 ;; Find the begining of the block.
1665 (not (eq (following-char) ?{))
1667 ;; Skip any constructs that can harbor literal block delimiter
1668 ;; characters and/or the delimiters for the constructs themselves.
1671 (gud-jdb-skip-single-line-comment))
1672 ((looking-at "/\\*")
1673 (gud-jdb-skip-traditional-or-documentation-comment))
1674 ((eq (following-char) ?\042)
1675 (gud-jdb-skip-string-literal))
1676 ((eq (following-char) ?\')
1677 (gud-jdb-skip-character-literal))
1678 (t (forward-char))))
1680 ;; Now at the begining of the block.
1683 ;; Skip over the body of the block as well as the final brace.
1684 (let ((open-level 1))
1685 (while (not (eq open-level 0))
1688 (gud-jdb-skip-single-line-comment))
1689 ((looking-at "/\\*")
1690 (gud-jdb-skip-traditional-or-documentation-comment))
1691 ((eq (following-char) ?\042)
1692 (gud-jdb-skip-string-literal))
1693 ((eq (following-char) ?\')
1694 (gud-jdb-skip-character-literal))
1695 ((eq (following-char) ?{)
1696 (setq open-level (+ open-level 1))
1698 ((eq (following-char) ?})
1699 (setq open-level (- open-level 1))
1701 (t (forward-char))))))
1703 ;; Find the package and class definitions in Java source file FILE. Assumes
1704 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1705 ;; to hold the source during analysis.
1706 (defun gud-jdb-analyze-source (buf file)
1709 (insert-file-contents file nil nil nil t)
1714 (gud-jdb-skip-whitespace)
1718 ;; Any number of semi's following a block is legal. Move point
1719 ;; past them. Note that comments and whitespace may be
1720 ;; interspersed as well.
1721 ((eq (following-char) ?\073)
1724 ;; Move point past a single line comment.
1726 (gud-jdb-skip-single-line-comment))
1728 ;; Move point past a traditional or documentation comment.
1729 ((looking-at "/\\*")
1730 (gud-jdb-skip-traditional-or-documentation-comment))
1732 ;; Move point past a package statement, but save the PackageName.
1733 ((looking-at "package")
1735 (gud-jdb-skip-whitespace-and-comments)
1737 (gud-jdb-skip-id-ish-thing)
1738 (setq p (concat (buffer-substring s (point)) "."))
1739 (gud-jdb-skip-whitespace-and-comments)
1740 (if (eq (following-char) ?\073)
1743 ;; Move point past an import statement.
1744 ((looking-at "import")
1746 (gud-jdb-skip-whitespace-and-comments)
1747 (gud-jdb-skip-id-ish-thing)
1748 (gud-jdb-skip-whitespace-and-comments)
1749 (if (eq (following-char) ?\073)
1752 ;; Move point past the various kinds of ClassModifiers.
1753 ((looking-at "public")
1755 ((looking-at "abstract")
1757 ((looking-at "final")
1760 ;; Move point past a ClassDeclaraction, but save the class
1762 ((looking-at "class")
1764 (gud-jdb-skip-whitespace-and-comments)
1766 (gud-jdb-skip-id-ish-thing)
1768 l (nconc l (list (concat p (buffer-substring s (point)))))))
1769 (gud-jdb-skip-block))
1771 ;; Move point past an interface statement.
1772 ((looking-at "interface")
1774 (gud-jdb-skip-block))
1776 ;; Anything else means the input is invalid.
1778 (message (format "Error parsing file %s." file))
1779 (throw 'abort nil))))))
1782 (defun gud-jdb-build-class-source-alist-for-file (file)
1786 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1788 ;; Return an alist of fully qualified classes and the source files
1789 ;; holding their definitions. SOURCES holds a list of all the source
1790 ;; files to examine.
1791 (defun gud-jdb-build-class-source-alist (sources)
1792 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1797 'gud-jdb-build-class-source-alist-for-file
1799 (kill-buffer gud-jdb-analysis-buffer)
1800 (setq gud-jdb-analysis-buffer nil)))
1802 ;; Change what was given in the minibuffer to something that can be used to
1803 ;; invoke the debugger.
1804 (defun gud-jdb-massage-args (file args)
1805 ;; The jdb executable must have whitespace between "-classpath" and
1806 ;; its value while gud-common-init expects all switch values to
1807 ;; follow the switch keyword without intervening whitespace. We
1808 ;; require that when the user enters the "-classpath" switch in the
1809 ;; EMACS minibuffer that they do so without the intervening
1810 ;; whitespace. This function adds it back (it's called after
1811 ;; gud-common-init). There are more switches like this (for
1812 ;; instance "-host" and "-password") but I don't care about them
1815 (let (massaged-args user-error)
1817 (while (and args (not user-error))
1819 ((setq user-error (string-match "-classpath$" (car args))))
1820 ((setq user-error (string-match "-sourcepath$" (car args))))
1821 ((string-match "-classpath\\(.+\\)" (car args))
1823 (append massaged-args
1825 (setq gud-jdb-classpath-string
1826 (match-string 1 (car args)))))))
1827 ((string-match "-sourcepath\\(.+\\)" (car args))
1829 (append massaged-args
1831 (setq gud-jdb-sourcepath
1832 (match-string 1 (car args)))))))
1833 (t (setq massaged-args (append massaged-args (list (car args))))))
1834 (setq args (cdr args)))
1836 ;; By this point the current directory is all screwed up. Maybe we
1837 ;; could fix things and re-invoke gud-common-init, but for now I think
1838 ;; issueing the error is good enough.
1841 (kill-buffer (current-buffer))
1842 (error "Error: Omit whitespace between '-classpath or -sourcepath' and its value")))
1845 ;; Search for an association with P, a fully qualified class name, in
1846 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1847 ;; qualified file name of the source file which produced the class.
1848 (defun gud-jdb-find-source-file (p)
1849 (cdr (assoc p gud-jdb-class-source-alist)))
1851 ;; Note: Reset to this value every time a prompt is seen
1852 (defvar gud-jdb-lowest-stack-level 999)
1854 (defun gud-jdb-find-source-using-classpath (p)
1855 "Find source file corresponding to fully qualified class p.
1856 Convert p from jdb's output, converted to a pathname
1857 relative to a classpath directory."
1860 (;; Replace dots with slashes and append ".java" to generate file
1861 ;; name relative to classpath
1864 (mapconcat 'identity
1866 ;; Eliminate any subclass references in the class
1867 ;; name string. These start with a "$"
1869 (if (string-match "$.*" x)
1870 (replace-match "" t t x) p))
1874 (cplist (append gud-jdb-sourcepath gud-jdb-classpath))
1877 (not (setq found-file
1879 (concat (car cplist) "/" filename)))))
1880 (setq cplist (cdr cplist)))
1881 (if found-file (concat (car cplist) "/" filename)))))
1883 (defun gud-jdb-find-source (string)
1884 "Alias for function used to locate source files.
1885 Set to `gud-jdb-find-source-using-classpath' or `gud-jdb-find-source-file'
1886 during jdb initialization depending on the value of
1887 `gud-jdb-use-classpath'."
1890 (defun gud-jdb-parse-classpath-string (string)
1891 "Parse the classpath list and convert each item to an absolute pathname."
1892 (mapcar (lambda (s) (if (string-match "[/\\]$" s)
1893 (replace-match "" nil nil s) s))
1894 (mapcar 'file-truename
1897 (concat "[ \t\n\r,\"" path-separator "]+")))))
1899 ;; See comentary for other debugger's marker filters - there you will find
1900 ;; important notes about STRING.
1901 (defun gud-jdb-marker-filter (string)
1903 ;; Build up the accumulator.
1904 (setq gud-marker-acc
1906 (concat gud-marker-acc string)
1909 ;; Look for classpath information until gud-jdb-classpath-string is found
1910 ;; (interactive, multiple settings of classpath from jdb
1911 ;; not supported/followed)
1912 (if (and gud-jdb-use-classpath
1913 (not gud-jdb-classpath-string)
1914 (or (string-match "classpath:[ \t[]+\\([^]]+\\)" gud-marker-acc)
1915 (string-match "-classpath[ \t\"]+\\([^ \"]+\\)" gud-marker-acc)))
1916 (setq gud-jdb-classpath
1917 (gud-jdb-parse-classpath-string
1918 (setq gud-jdb-classpath-string
1919 (match-string 1 gud-marker-acc)))))
1921 ;; We process STRING from left to right. Each time through the
1922 ;; following loop we process at most one marker. After we've found a
1923 ;; marker, delete gud-marker-acc up to and including the match
1925 ;; Process each complete marker in the input.
1928 ;; Do we see a marker?
1930 ;; jdb puts out a string of the following form when it
1931 ;; hits a breakpoint:
1933 ;; <fully-qualified-class><method> (<class>:<line-number>)
1935 ;; <fully-qualified-class>'s are composed of Java ID's
1936 ;; separated by periods. <method> and <class> are
1937 ;; also Java ID's. <method> begins with a period and
1938 ;; may contain less-than and greater-than (constructors,
1939 ;; for instance, are called <init> in the symbol table.)
1940 ;; Java ID's begin with a letter followed by letters
1941 ;; and/or digits. The set of letters includes underscore
1944 ;; The first group matches <fully-qualified-class>,
1945 ;; the second group matches <class> and the third group
1946 ;; matches <line-number>. We don't care about using
1947 ;; <method> so we don't "group" it.
1949 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1952 ;; The "," in the last square-bracket is necessary because of
1953 ;; Sun's total disrespect for backwards compatibility in
1954 ;; reported line numbers from jdb - starting in 1.4.0 they
1955 ;; introduced a comma at the thousands position (how
1958 "\\(\[[0-9]+\] \\)*\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>(),]+ \
1959 \\(([a-zA-Z0-9.$_]+:\\|line=\\)\\([0-9,]+\\)"
1962 ;; A good marker is one that:
1963 ;; 1) does not have a "[n] " prefix (not part of a stack backtrace)
1964 ;; 2) does have an "[n] " prefix and n is the lowest prefix seen
1965 ;; since the last prompt
1966 ;; Figure out the line on which to position the debugging arrow.
1967 ;; Return the info as a cons of the form:
1969 ;; (<file-name> . <line-number>) .
1970 (if (if (match-beginning 1)
1972 (setq n (string-to-int (substring
1974 (1+ (match-beginning 1))
1975 (- (match-end 1) 2))))
1976 (if (< n gud-jdb-lowest-stack-level)
1977 (progn (setq gud-jdb-lowest-stack-level n) t)))
1979 (if (setq file-found
1980 (gud-jdb-find-source (match-string 2 gud-marker-acc)))
1981 (setq gud-last-frame
1985 ((numstr (match-string 4 gud-marker-acc)))
1986 (if (string-match "," numstr)
1987 (replace-match "" nil nil numstr)
1989 (message "Could not find source file.")))
1991 ;; Set the accumulator to the remaining text.
1992 (setq gud-marker-acc (substring gud-marker-acc (match-end 0))))
1994 (if (string-match comint-prompt-regexp gud-marker-acc)
1995 (setq gud-jdb-lowest-stack-level 999)))
1997 ;; Do not allow gud-marker-acc to grow without bound. If the source
1998 ;; file information is not within the last 3/4
1999 ;; gud-marker-acc-max-length characters, well,...
2000 (if (> (length gud-marker-acc) gud-marker-acc-max-length)
2001 (setq gud-marker-acc
2002 (substring gud-marker-acc
2003 (- (/ (* gud-marker-acc-max-length 3) 4)))))
2005 ;; We don't filter any debugger output so just return what we were given.
2008 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
2011 (defun jdb (command-line)
2012 "Run jdb with command line COMMAND-LINE in a buffer.
2013 The buffer is named \"*gud*\" if no initial class is given or
2014 \"*gud-<initial-class-basename>*\" if there is. If the \"-classpath\"
2015 switch is given, omit all whitespace between it and its value.
2017 See `gud-jdb-use-classpath' and `gud-jdb-classpath' documentation for
2018 information on how jdb accesses source files. Alternatively (if
2019 `gud-jdb-use-classpath' is nil), see `gud-jdb-directories' for the
2020 original source file access method.
2022 For general information about commands available to control jdb from
2023 gud, see `gud-mode'."
2025 (list (gud-query-cmdline 'jdb)))
2026 (setq gud-jdb-classpath nil)
2027 (setq gud-jdb-sourcepath nil)
2029 ;; Set gud-jdb-classpath from the CLASSPATH environment variable,
2030 ;; if CLASSPATH is set.
2031 (setq gud-jdb-classpath-string (getenv "CLASSPATH"))
2032 (if gud-jdb-classpath-string
2033 (setq gud-jdb-classpath
2034 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2035 (setq gud-jdb-classpath-string nil) ; prepare for next
2037 (gud-common-init command-line 'gud-jdb-massage-args
2038 'gud-jdb-marker-filter)
2039 (set (make-local-variable 'gud-minor-mode) 'jdb)
2041 ;; If a -classpath option was provided, set gud-jdb-classpath
2042 (if gud-jdb-classpath-string
2043 (setq gud-jdb-classpath
2044 (gud-jdb-parse-classpath-string gud-jdb-classpath-string)))
2045 (setq gud-jdb-classpath-string nil) ; prepare for next
2046 ;; If a -sourcepath option was provided, parse it
2047 (if gud-jdb-sourcepath
2048 (setq gud-jdb-sourcepath
2049 (gud-jdb-parse-classpath-string gud-jdb-sourcepath)))
2051 (gud-def gud-break "stop at %c:%l" "\C-b" "Set breakpoint at current line.")
2052 (gud-def gud-remove "clear %c:%l" "\C-d" "Remove breakpoint at current line")
2053 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2054 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2055 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
2056 (gud-def gud-finish "step up" "\C-f" "Continue until current method returns.")
2057 (gud-def gud-up "up\C-Mwhere" "<" "Up one stack frame.")
2058 (gud-def gud-down "down\C-Mwhere" ">" "Up one stack frame.")
2059 (gud-def gud-run "run" nil "Run the program.") ;if VM start using jdb
2061 (setq comint-prompt-regexp "^> \\|^[^ ]+\\[[0-9]+\\] ")
2062 (setq paragraph-start comint-prompt-regexp)
2063 (run-hooks 'jdb-mode-hook)
2065 (if gud-jdb-use-classpath
2066 ;; Get the classpath information from the debugger
2068 (if (string-match "-attach" command-line)
2069 (gud-call "classpath"))
2070 (fset 'gud-jdb-find-source
2071 'gud-jdb-find-source-using-classpath))
2073 ;; Else create and bind the class/source association list as well
2074 ;; as the source file list.
2075 (setq gud-jdb-class-source-alist
2076 (gud-jdb-build-class-source-alist
2077 (setq gud-jdb-source-files
2078 (gud-jdb-build-source-files-list gud-jdb-directories
2080 (fset 'gud-jdb-find-source 'gud-jdb-find-source-file)))
2083 ;; ======================================================================
2085 ;; BASHDB support. See http://bashdb.sourceforge.net
2087 ;; AUTHOR: Rocky Bernstein <rocky@panix.com>
2089 ;; CREATED: Sun Nov 10 10:46:38 2002 Rocky Bernstein.
2091 ;; INVOCATION NOTES:
2093 ;; You invoke bashdb-mode with:
2095 ;; M-x bashdb <enter>
2097 ;; It responds with:
2099 ;; Run bashdb (like this): bash
2102 ;; History of argument lists passed to bashdb.
2103 (defvar gud-bashdb-history nil)
2105 ;; Convert a command line as would be typed normally to run a script
2106 ;; into one that invokes an Emacs-enabled debugging session.
2107 ;; "--debugger" in inserted as the first switch.
2109 ;; There's no guarantee that Emacs will hand the filter the entire
2110 ;; marker at once; it could be broken up across several strings. We
2111 ;; might even receive a big chunk with several markers in it. If we
2112 ;; receive a chunk of text which looks like it might contain the
2113 ;; beginning of a marker, we save it here between calls to the
2115 (defun gud-bashdb-marker-filter (string)
2116 (setq gud-marker-acc (concat gud-marker-acc string))
2119 ;; Process all the complete markers in this chunk.
2120 ;; Format of line looks like this:
2121 ;; (/etc/init.d/ntp.init:16):
2122 ;; but we also allow DOS drive letters
2123 ;; (d:/etc/init.d/ntp.init:16):
2124 (while (string-match "\\(^\\|\n\\)(\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\)):.*\n"
2128 ;; Extract the frame position from the marker.
2130 (cons (match-string 2 gud-marker-acc)
2131 (string-to-int (match-string 4 gud-marker-acc)))
2133 ;; Append any text before the marker to the output we're going
2134 ;; to return - we don't include the marker in this text.
2135 output (concat output
2136 (substring gud-marker-acc 0 (match-beginning 0)))
2138 ;; Set the accumulator to the remaining text.
2139 gud-marker-acc (substring gud-marker-acc (match-end 0))))
2141 ;; Does the remaining text look like it might end with the
2142 ;; beginning of another marker? If it does, then keep it in
2143 ;; gud-marker-acc until we receive the rest of it. Since we
2144 ;; know the full marker regexp above failed, it's pretty simple to
2145 ;; test for marker starts.
2146 (if (string-match "\032.*\\'" gud-marker-acc)
2148 ;; Everything before the potential marker start can be output.
2149 (setq output (concat output (substring gud-marker-acc
2150 0 (match-beginning 0))))
2152 ;; Everything after, we save, to combine with later input.
2153 (setq gud-marker-acc
2154 (substring gud-marker-acc (match-beginning 0))))
2156 (setq output (concat output gud-marker-acc)
2161 (defcustom gud-bashdb-command-name "bash --debugger"
2162 "File name for executing bash debugger."
2167 (defun bashdb (command-line)
2168 "Run bashdb on program FILE in buffer *gud-FILE*.
2169 The directory containing FILE becomes the initial working directory
2170 and source-file directory for your debugger."
2172 (list (read-from-minibuffer "Run bashdb (like this): "
2173 (if (consp gud-bashdb-history)
2174 (car gud-bashdb-history)
2175 (concat gud-bashdb-command-name
2177 gud-minibuffer-local-map nil
2178 '(gud-bashdb-history . 1))))
2180 (gud-common-init command-line nil 'gud-bashdb-marker-filter)
2182 (set (make-local-variable 'gud-minor-mode) 'bashdb)
2184 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
2185 (gud-def gud-tbreak "tbreak %l" "\C-t" "Set temporary breakpoint at current line.")
2186 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
2187 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
2188 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
2189 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
2190 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
2191 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
2192 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
2193 (gud-def gud-print "x %e" "\C-p" "Evaluate BASH expression at point.")
2196 (gud-def gud-statement "eval %e" "\C-e" "Execute BASH statement at point.")
2198 (setq comint-prompt-regexp "^bashdb<+(*[0-9]+)*>+ ")
2199 (setq paragraph-start comint-prompt-regexp)
2200 (run-hooks 'bashdb-mode-hook)
2204 ;; End of debugger-specific information
2208 ;; When we send a command to the debugger via gud-call, it's annoying
2209 ;; to see the command and the new prompt inserted into the debugger's
2210 ;; buffer; we have other ways of knowing the command has completed.
2212 ;; If the buffer looks like this:
2213 ;; --------------------
2214 ;; (gdb) set args foo bar
2216 ;; --------------------
2217 ;; (the -!- marks the location of point), and we type `C-x SPC' in a
2218 ;; source file to set a breakpoint, we want the buffer to end up like
2220 ;; --------------------
2221 ;; (gdb) set args foo bar
2222 ;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
2224 ;; --------------------
2225 ;; Essentially, the old prompt is deleted, and the command's output
2226 ;; and the new prompt take its place.
2228 ;; Not echoing the command is easy enough; you send it directly using
2229 ;; process-send-string, and it never enters the buffer. However,
2230 ;; getting rid of the old prompt is trickier; you don't want to do it
2231 ;; when you send the command, since that will result in an annoying
2232 ;; flicker as the prompt is deleted, redisplay occurs while Emacs
2233 ;; waits for a response from the debugger, and the new prompt is
2234 ;; inserted. Instead, we'll wait until we actually get some output
2235 ;; from the subprocess before we delete the prompt. If the command
2236 ;; produced no output other than a new prompt, that prompt will most
2237 ;; likely be in the first chunk of output received, so we will delete
2238 ;; the prompt and then replace it with an identical one. If the
2239 ;; command produces output, the prompt is moving anyway, so the
2240 ;; flicker won't be annoying.
2242 ;; So - when we want to delete the prompt upon receipt of the next
2243 ;; chunk of debugger output, we position gud-delete-prompt-marker at
2244 ;; the start of the prompt; the process filter will notice this, and
2245 ;; delete all text between it and the process output marker. If
2246 ;; gud-delete-prompt-marker points nowhere, we leave the current
2248 (defvar gud-delete-prompt-marker nil)
2251 (put 'gud-mode 'mode-class 'special)
2253 (define-derived-mode gud-mode comint-mode "Debugger"
2254 "Major mode for interacting with an inferior debugger process.
2256 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2257 M-x perldb, M-x xdb, or M-x jdb. Each entry point finishes by executing a
2258 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2259 `perldb-mode-hook', `xdb-mode-hook', or `jdb-mode-hook' respectively.
2261 After startup, the following commands are available in both the GUD
2262 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2265 \\[gud-break] sets a breakpoint at the current file and line. In the
2266 GUD buffer, the current file and line are those of the last breakpoint or
2267 step. In a source buffer, they are the buffer's file and current line.
2269 \\[gud-remove] removes breakpoints on the current file and line.
2271 \\[gud-refresh] displays in the source window the last line referred to
2274 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2275 step-one-line (not entering function calls), and step-one-instruction
2276 and then update the source window with the current file and position.
2277 \\[gud-cont] continues execution.
2279 \\[gud-print] tries to find the largest C lvalue or function-call expression
2280 around point, and sends it to the debugger for value display.
2282 The above commands are common to all supported debuggers except xdb which
2283 does not support stepping instructions.
2285 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2286 except that the breakpoint is temporary; that is, it is removed when
2287 execution stops on it.
2289 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2290 frame. \\[gud-down] drops back down through one.
2292 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2293 the current function and stops.
2295 All the keystrokes above are accessible in the GUD buffer
2296 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2298 All pre-defined functions for which the concept make sense repeat
2299 themselves the appropriate number of times if you give a prefix
2302 You may use the `gud-def' macro in the initialization hook to define other
2305 Other commands for interacting with the debugger process are inherited from
2306 comint mode, which see."
2307 (setq mode-line-process '(":%s"))
2308 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2309 (set (make-local-variable 'gud-last-frame) nil)
2310 (set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
2311 (make-local-variable 'comint-prompt-regexp)
2312 ;; Don't put repeated commands in command history many times.
2313 (set (make-local-variable 'comint-input-ignoredups) t)
2314 (make-local-variable 'paragraph-start)
2315 (set (make-local-variable 'gud-delete-prompt-marker) (make-marker)))
2317 ;; Cause our buffers to be displayed, by default,
2318 ;; in the selected window.
2319 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2321 (defcustom gud-chdir-before-run t
2322 "Non-nil if GUD should `cd' to the debugged executable."
2326 (defvar gud-target-name "--unknown--"
2327 "The apparent name of the program being debugged in a gud buffer.")
2329 ;; Perform initializations common to all debuggers.
2330 ;; The first arg is the specified command line,
2331 ;; which starts with the program to debug.
2332 ;; The other three args specify the values to use
2333 ;; for local variables in the debugger buffer.
2334 (defun gud-common-init (command-line massage-args marker-filter
2335 &optional find-file)
2336 (let* ((words (split-string command-line))
2337 (program (car words))
2338 (dir default-directory)
2339 ;; Extract the file name from WORDS
2340 ;; and put t in its place.
2341 ;; Later on we will put the modified file name arg back there.
2342 (file-word (let ((w (cdr words)))
2343 (while (and w (= ?- (aref (car w) 0)))
2349 (and file-word (substitute-in-file-name file-word)))
2351 ;; If a directory was specified, expand the file name.
2352 ;; Otherwise, don't expand it, so GDB can use the PATH.
2353 ;; A file name without directory is literally valid
2354 ;; only if the file exists in ., and in that case,
2355 ;; omitting the expansion here has no visible effect.
2356 (file (and file-word
2357 (if (file-name-directory file-subst)
2358 (expand-file-name file-subst)
2360 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2361 (pop-to-buffer (concat "*gud" filepart "*"))
2362 ;; Set the dir, in case the buffer already existed with a different dir.
2363 (setq default-directory dir)
2364 ;; Set default-directory to the file's directory.
2366 gud-chdir-before-run
2367 ;; Don't set default-directory if no directory was specified.
2368 ;; In that case, either the file is found in the current directory,
2369 ;; in which case this setq is a no-op,
2370 ;; or it is found by searching PATH,
2371 ;; in which case we don't know what directory it was found in.
2372 (file-name-directory file)
2373 (setq default-directory (file-name-directory file)))
2374 (or (bolp) (newline))
2375 (insert "Current directory is " default-directory "\n")
2376 ;; Put the substituted and expanded file name back in its place.
2378 (while (and w (not (eq (car w) t)))
2382 (apply 'make-comint (concat "gud" filepart) program nil
2383 (if massage-args (funcall massage-args file args) args))
2384 ;; Since comint clobbered the mode, we don't set it until now.
2386 (set (make-local-variable 'gud-target-name)
2387 (and file-word (file-name-nondirectory file))))
2388 (set (make-local-variable 'gud-marker-filter) marker-filter)
2389 (if find-file (set (make-local-variable 'gud-find-file) find-file))
2390 (setq gud-running nil)
2391 (setq gud-last-last-frame nil)
2393 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2394 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2397 (defun gud-set-buffer ()
2398 (when (eq major-mode 'gud-mode)
2399 (setq gud-comint-buffer (current-buffer))))
2401 (defvar gud-filter-defer-flag nil
2402 "Non-nil means don't process anything from the debugger right now.
2403 It is saved for when this flag is not set.")
2405 (defvar gud-filter-pending-text nil
2406 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2408 ;; These functions are responsible for inserting output from your debugger
2409 ;; into the buffer. The hard work is done by the method that is
2410 ;; the value of gud-marker-filter.
2412 (defun gud-filter (proc string)
2413 ;; Here's where the actual buffer insertion is done
2414 (let (output process-window)
2415 (if (buffer-name (process-buffer proc))
2416 (if gud-filter-defer-flag
2417 ;; If we can't process any text now,
2418 ;; save it for later.
2419 (setq gud-filter-pending-text
2420 (concat (or gud-filter-pending-text "") string))
2422 ;; If we have to ask a question during the processing,
2423 ;; defer any additional text that comes from the debugger
2424 ;; during that time.
2425 (let ((gud-filter-defer-flag t))
2426 ;; Process now any text we previously saved up.
2427 (if gud-filter-pending-text
2428 (setq string (concat gud-filter-pending-text string)
2429 gud-filter-pending-text nil))
2431 (with-current-buffer (process-buffer proc)
2432 ;; If we have been so requested, delete the debugger prompt.
2435 (if (marker-buffer gud-delete-prompt-marker)
2437 (delete-region (process-mark proc)
2438 gud-delete-prompt-marker)
2439 (set-marker gud-delete-prompt-marker nil)))
2440 ;; Save the process output, checking for source file markers.
2441 (setq output (gud-marker-filter string))
2442 ;; Check for a filename-and-line number.
2443 ;; Don't display the specified file
2444 ;; unless (1) point is at or after the position where output appears
2445 ;; and (2) this buffer is on the screen.
2446 (setq process-window
2448 (>= (point) (process-mark proc))
2449 (get-buffer-window (current-buffer)))))
2451 ;; Let the comint filter do the actual insertion.
2452 ;; That lets us inherit various comint features.
2453 (comint-output-filter proc output))
2455 ;; Put the arrow on the source line.
2456 ;; This must be outside of the save-excursion
2457 ;; in case the source file is our current buffer.
2459 (save-selected-window
2460 (select-window process-window)
2461 (gud-display-frame))
2462 ;; We have to be in the proper buffer, (process-buffer proc),
2463 ;; but not in a save-excursion, because that would restore point.
2464 (let ((old-buf (current-buffer)))
2465 (set-buffer (process-buffer proc))
2468 (set-buffer old-buf)))))
2470 ;; If we deferred text that arrived during this processing,
2472 (if gud-filter-pending-text
2473 (gud-filter proc ""))))))
2475 (defvar gud-minor-mode-type nil)
2477 (defun gud-sentinel (proc msg)
2478 (cond ((null (buffer-name (process-buffer proc)))
2480 ;; Stop displaying an arrow in a source file.
2481 (setq overlay-arrow-position nil)
2482 (set-process-buffer proc nil)
2483 (if (eq gud-minor-mode-type 'gdba)
2486 ((memq (process-status proc) '(signal exit))
2487 ;; Stop displaying an arrow in a source file.
2488 (setq overlay-arrow-position nil)
2489 (with-current-buffer gud-comint-buffer
2490 (if (eq gud-minor-mode 'gdba)
2493 (let* ((obuf (current-buffer)))
2494 ;; save-excursion isn't the right thing if
2495 ;; process-buffer is current-buffer
2498 ;; Write something in *compilation* and hack its mode line,
2499 (set-buffer (process-buffer proc))
2500 ;; Fix the mode line.
2501 (setq mode-line-process
2503 (symbol-name (process-status proc))))
2504 (force-mode-line-update)
2506 (insert ?\n mode-name " " msg)
2508 (goto-char (point-max))
2509 (insert ?\n mode-name " " msg)))
2510 ;; If buffer and mode line will show that the process
2511 ;; is dead, we can delete it now. Otherwise it
2512 ;; will stay around until M-x list-processes.
2513 (delete-process proc))
2514 ;; Restore old buffer, but don't restore old point
2515 ;; if obuf is the gud buffer.
2516 (set-buffer obuf))))))
2518 (defun gud-kill-buffer-hook ()
2520 (setq gud-minor-mode-type gud-minor-mode)))
2522 (add-hook 'kill-buffer-hook 'gud-kill-buffer-hook)
2525 (dolist (buffer (buffer-list))
2526 (if (not (eq buffer gud-comint-buffer))
2529 (when gud-minor-mode
2530 (setq gud-minor-mode nil)
2531 (kill-local-variable 'tool-bar-map))))))
2533 (defun gud-display-frame ()
2534 "Find and obey the last filename-and-line marker from the debugger.
2535 Obeying it means displaying in another window the specified file and line."
2537 (when gud-last-frame
2539 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2540 (setq gud-last-last-frame gud-last-frame
2541 gud-last-frame nil)))
2543 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2544 ;; and that its line LINE is visible.
2545 ;; Put the overlay-arrow on the line LINE in that buffer.
2546 ;; Most of the trickiness in here comes from wanting to preserve the current
2547 ;; region-restriction if that's possible. We use an explicit display-buffer
2548 ;; to get around the fact that this is called inside a save-excursion.
2550 (defun gud-display-line (true-file line)
2551 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2553 (with-current-buffer gud-comint-buffer
2554 (gud-find-file true-file)))
2555 (window (and buffer (or (get-buffer-window buffer)
2556 (if (eq gud-minor-mode 'gdba)
2557 (gdb-display-source-buffer buffer)
2558 (display-buffer buffer)))))
2562 (with-current-buffer buffer
2563 (unless (or (verify-visited-file-modtime buffer) gud-keep-buffer)
2565 (format "File %s changed on disk. Reread from disk? "
2568 (setq gud-keep-buffer t)))
2573 (setq overlay-arrow-string "=>")
2574 (or overlay-arrow-position
2575 (setq overlay-arrow-position (make-marker)))
2576 (set-marker overlay-arrow-position (point) (current-buffer)))
2577 (cond ((or (< pos (point-min)) (> pos (point-max)))
2580 (if window (set-window-point window overlay-arrow-position))))))
2582 ;; The gud-call function must do the right thing whether its invoking
2583 ;; keystroke is from the GUD buffer itself (via major-mode binding)
2584 ;; or a C buffer. In the former case, we want to supply data from
2585 ;; gud-last-frame. Here's how we do it:
2587 (defun gud-format-command (str arg)
2588 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2589 (frame (or gud-last-frame gud-last-last-frame))
2591 (while (and str (string-match "\\([^%]*\\)%\\([adeflpc]\\)" str))
2592 (let ((key (string-to-char (match-string 2 str)))
2596 (setq subst (file-name-nondirectory (if insource
2600 (setq subst (file-name-sans-extension
2601 (file-name-nondirectory (if insource
2605 (setq subst (file-name-directory (if insource
2609 (setq subst (int-to-string
2613 (+ (count-lines (point-min) (point))
2617 (setq subst (gud-find-expr)))
2619 (setq subst (gud-read-address)))
2629 (+ (count-lines (point-min) (point))
2633 (setq subst (if arg (int-to-string arg)))))
2634 (setq result (concat result (match-string 1 str) subst)))
2635 (setq str (substring str (match-end 2))))
2636 ;; There might be text left in STR when the loop ends.
2637 (concat result str)))
2639 (defun gud-read-address ()
2640 "Return a string containing the core-address found in the buffer at point."
2643 (let ((pt (point)) found begin)
2644 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2646 (found (forward-char 2)
2647 (buffer-substring found
2648 (progn (re-search-forward "[^0-9a-f]")
2651 (t (setq begin (progn (re-search-backward "[^0-9]")
2655 (re-search-forward "[^0-9]")
2657 (buffer-substring begin (point))))))))
2659 (defun gud-call (fmt &optional arg)
2660 (let ((msg (gud-format-command fmt arg)))
2661 (message "Command: %s" msg)
2663 (gud-basic-call msg)))
2665 (defun gud-basic-call (command)
2666 "Invoke the debugger COMMAND displaying source in other window."
2669 (let ((proc (get-buffer-process gud-comint-buffer)))
2670 (or proc (error "Current buffer has no process"))
2671 ;; Arrange for the current prompt to get deleted.
2673 (set-buffer gud-comint-buffer)
2676 (goto-char (process-mark proc))
2678 (if (looking-at comint-prompt-regexp)
2679 (set-marker gud-delete-prompt-marker (point)))
2680 (if (eq gud-minor-mode 'gdba)
2681 (apply comint-input-sender (list proc command))
2682 (process-send-string proc (concat command "\n")))))))
2684 (defun gud-refresh (&optional arg)
2685 "Fix up a possibly garbled display, and redraw the arrow."
2687 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2691 ;; Code for parsing expressions out of C or Fortran code. The single entry
2692 ;; point is gud-find-expr, which tries to return an lvalue expression from
2695 (defvar gud-find-expr-function 'gud-find-c-expr)
2697 (defun gud-find-expr (&rest args)
2698 (apply gud-find-expr-function args))
2700 ;; The next eight functions are hacked from gdbsrc.el by
2701 ;; Debby Ayers <ayers@asc.slb.com>,
2702 ;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2704 (defun gud-find-c-expr ()
2705 "Returns the expr that surrounds point."
2709 (expr (gud-innermost-expr))
2710 (test-expr (gud-prev-expr)))
2711 (while (and test-expr (gud-expr-compound test-expr expr))
2712 (let ((prev-expr expr))
2713 (setq expr (cons (car test-expr) (cdr expr)))
2714 (goto-char (car expr))
2715 (setq test-expr (gud-prev-expr))
2716 ;; If we just pasted on the condition of an if or while,
2717 ;; throw it away again.
2718 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2719 '("if" "while" "for"))
2723 (setq test-expr (gud-next-expr))
2724 (while (gud-expr-compound expr test-expr)
2725 (setq expr (cons (car expr) (cdr test-expr)))
2726 (setq test-expr (gud-next-expr)))
2727 (buffer-substring (car expr) (cdr expr)))))
2729 (defun gud-innermost-expr ()
2730 "Returns the smallest expr that point is in; move point to beginning of it.
2731 The expr is represented as a cons cell, where the car specifies the point in
2732 the current buffer that marks the beginning of the expr and the cdr specifies
2733 the character after the end of the expr."
2734 (let ((p (point)) begin end)
2736 (setq begin (point))
2749 (defun gud-backward-sexp ()
2750 "Version of `backward-sexp' that catches errors."
2755 (defun gud-forward-sexp ()
2756 "Version of `forward-sexp' that catches errors."
2761 (defun gud-prev-expr ()
2762 "Returns the previous expr, point is set to beginning of that expr.
2763 The expr is represented as a cons cell, where the car specifies the point in
2764 the current buffer that marks the beginning of the expr and the cdr specifies
2765 the character after the end of the expr"
2766 (let ((begin) (end))
2768 (setq begin (point))
2774 (defun gud-next-expr ()
2775 "Returns the following expr, point is set to beginning of that expr.
2776 The expr is represented as a cons cell, where the car specifies the point in
2777 the current buffer that marks the beginning of the expr and the cdr specifies
2778 the character after the end of the expr."
2779 (let ((begin) (end))
2784 (setq begin (point))
2787 (defun gud-expr-compound-sep (span-start span-end)
2788 "Scan from SPAN-START to SPAN-END for punctuation characters.
2789 If `->' is found, return `?.'. If `.' is found, return `?.'.
2790 If any other punctuation is found, return `??'.
2791 If no punctuation is found, return `? '."
2794 (while (< span-start span-end)
2795 (setq syntax (char-syntax (char-after span-start)))
2798 ((= syntax ?.) (setq syntax (char-after span-start))
2800 ((= syntax ?.) (setq result ?.))
2801 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2803 (setq span-start (+ span-start 1)))
2804 (t (setq span-start span-end)
2805 (setq result ??)))))
2806 (setq span-start (+ span-start 1)))
2809 (defun gud-expr-compound (first second)
2810 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2811 The two exprs are represented as a cons cells, where the car
2812 specifies the point in the current buffer that marks the beginning of the
2813 expr and the cdr specifies the character after the end of the expr.
2814 Link exprs of the form:
2821 (let ((span-start (cdr first))
2822 (span-end (car second))
2824 (setq syntax (gud-expr-compound-sep span-start span-end))
2826 ((= (car first) (car second)) nil)
2827 ((= (cdr first) (cdr second)) nil)
2830 (setq span-start (char-after (- span-start 1)))
2831 (setq span-end (char-after span-end))
2833 ((= span-start ?)) t)
2834 ((= span-start ?]) t)
2840 (defun gud-find-class (f line)
2841 "Find fully qualified class in file F at line LINE.
2842 This function uses the `gud-jdb-classpath' (and optional
2843 `gud-jdb-sourcepath') list(s) to derive a file
2844 pathname relative to its classpath directory. The values in
2845 `gud-jdb-classpath' are assumed to have been converted to absolute
2846 pathname standards using file-truename.
2847 If F is visited by a buffer and its mode is CC-mode(Java),
2848 syntactic information of LINE is used to find the enclosing (nested)
2849 class string which is appended to the top level
2850 class of the file (using s to separate nested class ids)."
2851 ;; Convert f to a standard representation and remove suffix
2852 (if (and gud-jdb-use-classpath (or gud-jdb-classpath gud-jdb-sourcepath))
2854 (let ((cplist (append gud-jdb-sourcepath gud-jdb-classpath))
2855 (fbuffer (get-file-buffer f))
2856 syntax-symbol syntax-point class-found)
2857 (setq f (file-name-sans-extension (file-truename f)))
2858 ;; Syntax-symbol returns the symbol of the *first* element
2859 ;; in the syntactical analysis result list, syntax-point
2860 ;; returns the buffer position of same
2861 (fset 'syntax-symbol (lambda (x) (c-langelem-sym (car x))))
2862 (fset 'syntax-point (lambda (x) (c-langelem-pos (car x))))
2863 ;; Search through classpath list for an entry that is
2865 (while (and cplist (not class-found))
2866 (if (string-match (car cplist) f)
2868 (mapconcat 'identity
2870 (substring f (+ (match-end 0) 1))
2872 (setq cplist (cdr cplist)))
2873 ;; if f is visited by a java(cc-mode) buffer, walk up the
2874 ;; syntactic information chain and collect any 'inclass
2875 ;; symbols until 'topmost-intro is reached to find out if
2876 ;; point is within a nested class
2877 (if (and fbuffer (equal (symbol-file 'java-mode) "cc-mode"))
2879 (set-buffer fbuffer)
2880 (let ((nclass) (syntax))
2881 ;; While the c-syntactic information does not start
2882 ;; with the 'topmost-intro symbol, there may be
2883 ;; nested classes...
2884 (while (not (eq 'topmost-intro
2885 (syntax-symbol (c-guess-basic-syntax))))
2886 ;; Check if the current position c-syntactic
2887 ;; analysis has 'inclass
2888 (setq syntax (c-guess-basic-syntax))
2890 (and (not (eq 'inclass (syntax-symbol syntax)))
2892 (setq syntax (cdr syntax)))
2893 (if (eq 'inclass (syntax-symbol syntax))
2895 (goto-char (syntax-point syntax))
2896 ;; Now we're at the beginning of a class
2897 ;; definition. Find class name
2899 "[A-Za-z0-9 \t\n]*?class[ \t\n]+\\([^ \t\n]+\\)")
2901 (append (list (match-string-no-properties 1))
2903 (setq syntax (c-guess-basic-syntax))
2904 (while (and (not (syntax-point syntax)) (cdr syntax))
2905 (setq syntax (cdr syntax)))
2906 (goto-char (syntax-point syntax))
2908 (string-match (concat (car nclass) "$") class-found)
2910 (replace-match (mapconcat 'identity nclass "$")
2911 t t class-found)))))
2912 (if (not class-found)
2913 (message "gud-find-class: class for file %s not found!" f))
2915 ;; Not using classpath - try class/source association list
2916 (let ((class-found (rassoc f gud-jdb-class-source-alist)))
2919 (message "gud-find-class: class for file %s not found in gud-jdb-class-source-alist!" f)
2922 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2923 ;;; GDB script mode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2924 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2926 (defvar gdb-script-mode-syntax-table
2927 (let ((st (make-syntax-table)))
2928 (modify-syntax-entry ?' "\"" st)
2929 (modify-syntax-entry ?# "<" st)
2930 (modify-syntax-entry ?\n ">" st)
2933 (defvar gdb-script-font-lock-keywords
2934 '(("^define\\s-+\\(\\(\\w\\|\\s_\\)+\\)" (1 font-lock-function-name-face))
2935 ("\\$\\(\\w+\\)" (1 font-lock-variable-name-face))
2936 ("^\\s-*\\([a-z]+\\)" (1 font-lock-keyword-face))))
2938 (defvar gdb-script-font-lock-syntactic-keywords
2939 '(("^document\\s-.*\\(\n\\)" (1 "< b"))
2940 ;; It would be best to change the \n in front, but it's more difficult.
2941 ("^en\\(d\\)\\>" (1 "> b"))))
2943 (defun gdb-script-font-lock-syntactic-face (state)
2945 ((nth 3 state) font-lock-string-face)
2946 ((nth 7 state) font-lock-doc-face)
2947 (t font-lock-comment-face)))
2949 (defvar gdb-script-basic-indent 2)
2951 (defun gdb-script-skip-to-head ()
2952 "We're just in front of an `end' and we need to go to its head."
2953 (while (and (re-search-backward "^\\s-*\\(\\(end\\)\\|define\\|document\\|if\\|while\\)\\>" nil 'move)
2955 (gdb-script-skip-to-head)))
2957 (defun gdb-script-calculate-indentation ()
2959 ((looking-at "end\\>")
2960 (gdb-script-skip-to-head)
2961 (current-indentation))
2962 ((looking-at "else\\>")
2963 (while (and (re-search-backward "^\\s-*\\(if\\|\\(end\\)\\)\\>" nil 'move)
2965 (gdb-script-skip-to-head))
2966 (current-indentation))
2968 (forward-comment (- (point-max)))
2970 (skip-chars-forward " \t")
2971 (+ (current-indentation)
2972 (if (looking-at "\\(if\\|while\\|define\\|else\\)\\>")
2973 gdb-script-basic-indent 0)))))
2975 (defun gdb-script-indent-line ()
2976 "Indent current line of GDB script."
2978 (if (and (eq (get-text-property (point) 'face) font-lock-doc-face)
2981 (skip-chars-forward " \t")
2982 (not (looking-at "end\\>"))))
2984 (let* ((savep (point))
2985 (indent (condition-case nil
2988 (skip-chars-forward " \t")
2989 (if (>= (point) savep) (setq savep nil))
2990 (max (gdb-script-calculate-indentation) 0))
2993 (save-excursion (indent-line-to indent))
2994 (indent-line-to indent)))))
2997 (add-to-list 'auto-mode-alist '("/\\.gdbinit" . gdb-script-mode))
3000 (define-derived-mode gdb-script-mode nil "GDB-Script"
3001 "Major mode for editing GDB scripts"
3002 (set (make-local-variable 'comment-start) "#")
3003 (set (make-local-variable 'comment-start-skip) "#+\\s-*")
3004 (set (make-local-variable 'outline-regexp) "[ \t]")
3005 (set (make-local-variable 'imenu-generic-expression)
3006 '((nil "^define[ \t]+\\(\\w+\\)" 1)))
3007 (set (make-local-variable 'indent-line-function) 'gdb-script-indent-line)
3008 (set (make-local-variable 'font-lock-defaults)
3009 '(gdb-script-font-lock-keywords nil nil ((?_ . "w")) nil
3010 (font-lock-syntactic-keywords
3011 . gdb-script-font-lock-syntactic-keywords)
3012 (font-lock-syntactic-face-function
3013 . gdb-script-font-lock-syntactic-face))))
3017 ;;; arch-tag: 6d990948-df65-461a-be39-1c7fb83ac4c4
3018 ;;; gud.el ends here