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 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, sdb, dbx, xdb or perldb under Emacs."
54 (defcustom gud-key-prefix
"\C-x\C-a"
55 "Prefix of all GUD commands valid in C buffers."
59 (global-set-key (concat gud-key-prefix
"\C-l") 'gud-refresh
)
60 (define-key ctl-x-map
" " 'gud-break
) ;; backward compatibility hack
62 (defvar gud-marker-filter nil
)
63 (put 'gud-marker-filter
'permanent-local t
)
64 (defvar gud-find-file nil
)
65 (put 'gud-find-file
'permanent-local t
)
67 (defun gud-marker-filter (&rest args
)
68 (apply gud-marker-filter args
))
70 (defun gud-find-file (file)
71 ;; Don't get confused by double slashes in the name that comes from GDB.
72 (while (string-match "//+" file
)
73 (setq file
(replace-match "/" t t file
)))
74 (funcall gud-find-file file
))
76 ;; Keymap definitions for menu bar entries common to all debuggers and
77 ;; slots for debugger-dependent ones in sensible places. (Defined here
79 (defvar gud-menu-map
(make-sparse-keymap "Gud") nil
)
80 (define-key gud-menu-map
[refresh] '("Refresh" . gud-refresh))
81 (define-key gud-menu-map [remove] '("Remove Breakpoint" . gud-remove))
82 (define-key gud-menu-map [tbreak] nil) ; gdb, sdb and xdb
83 (define-key gud-menu-map [break] '("Set Breakpoint" . gud-break))
84 (define-key gud-menu-map [up] nil) ; gdb, dbx, and xdb
85 (define-key gud-menu-map [down] nil) ; gdb, dbx, and xdb
86 (define-key gud-menu-map [print] '("Print Expression" . gud-print))
87 (define-key gud-menu-map [finish] nil) ; gdb or xdb
88 (define-key gud-menu-map [stepi] '("Step Instruction" . gud-stepi))
89 (define-key gud-menu-map [step] '("Step Line" . gud-step))
90 (define-key gud-menu-map [next] '("Next Line" . gud-next))
91 (define-key gud-menu-map [cont] '("Continue" . gud-cont))
93 ;; ======================================================================
96 ;; This macro is used below to define some basic debugger interface commands.
97 ;; Of course you may use `gud-def' with any other debugger command, including
100 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
101 ;; which defines FUNC to send the command NAME to the debugger, gives
102 ;; it the docstring DOC, and binds that function to KEY in the GUD
103 ;; major mode. The function is also bound in the global keymap with the
106 (defmacro gud-def (func cmd key &optional doc)
107 "Define FUNC to be a command sending STR and bound to KEY, with
108 optional doc string DOC. Certain %-escapes in the string arguments
109 are interpreted specially if present. These are:
111 %f name (without directory) of current source file.
112 %d directory of current source file.
113 %l number of current source line
114 %e text of the C lvalue or function-call expression surrounding point.
115 %a text of the hexadecimal address surrounding point
116 %p prefix argument to the command (if any) as a number
118 The `current' source file is the file of the current buffer (if
119 we're in a C file) or the source file current at the last break or
120 step (if we're in the GUD buffer).
121 The `current' line is that of the current buffer (if we're in a
122 source file) or the source line number at the last break or step (if
123 we're in the GUD buffer)."
125 (list 'defun func '(arg)
128 (list 'gud-call cmd 'arg))
135 (list 'global-set-key
136 (list 'concat 'gud-key-prefix key)
137 (list 'quote func)))))
139 ;; Where gud-display-frame should put the debugging arrow. This is
140 ;; set by the marker-filter, which scans the debugger's output for
141 ;; indications of the current program counter.
142 (defvar gud-last-frame nil)
144 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
145 ;; the last frame, even if it's been called before and gud-last-frame has
147 (defvar gud-last-last-frame nil)
149 ;; All debugger-specific information is collected here.
150 ;; Here's how it works, in case you ever need to add a debugger to the mode.
152 ;; Each entry must define the following at startup:
155 ;; comint-prompt-regexp
156 ;; gud-<name>-massage-args
157 ;; gud-<name>-marker-filter
158 ;; gud-<name>-find-file
160 ;; The job of the massage-args method is to modify the given list of
161 ;; debugger arguments before running the debugger.
163 ;; The job of the marker-filter method is to detect file/line markers in
164 ;; strings and set the global gud-last-frame to indicate what display
165 ;; action (if any) should be triggered by the marker. Note that only
166 ;; whatever the method *returns* is displayed in the buffer; thus, you
167 ;; can filter the debugger's output, interpreting some and passing on
170 ;; The job of the find-file method is to visit and return the buffer indicated
171 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
172 ;; something else. It would be good if it also copied the Gud menubar entry.
174 ;; ======================================================================
175 ;; speedbar support functions and variables.
176 (defvar gud-last-speedbar-buffer nil
177 "The last GUD buffer used.")
179 (defvar gud-last-speedbar-stackframe nil
180 "Description of the currently displayed GUD stack.
181 t means that there is no stack, and we are in display-file mode.")
183 (defvar gud-speedbar-menu-items
184 ;; Note to self. Add expand, and turn off items when not available.
185 '(["Jump to stack frame" speedbar-edit-line t])
186 "Additional menu items to add the the speedbar frame.")
188 (defun gud-speedbar-buttons (buffer)
189 "Create a speedbar display based on the current state of GUD.
190 If the GUD BUFFER is not running a supported debugger, then turn
191 off the specialized speedbar mode."
192 (if (and (save-excursion (goto-char (point-min))
193 (looking-at "Current Stack"))
194 (equal gud-last-last-frame gud-last-speedbar-stackframe))
196 (setq gud-last-speedbar-buffer buffer)
197 (let* ((ff (save-excursion (set-buffer buffer) gud-find-file))
198 ;;(lf (save-excursion (set-buffer buffer) gud-last-last-frame))
200 (cond ((eq ff 'gud-gdb-find-file)
201 (gud-gdb-get-stackframe buffer)
203 ;; Add more debuggers here!
205 (speedbar-remove-localized-speedbar-support buffer)
209 (insert "No Stack frames\n")
210 (insert "Current Stack:\n"))
212 (insert (nth 1 (car frames)) ":\n")
213 (if (= (length (car frames)) 2)
215 ; (speedbar-insert-button "[?]"
216 ; 'speedbar-button-face
218 (speedbar-insert-button (car (car frames))
219 'speedbar-directory-face
221 ; (speedbar-insert-button "[+]"
222 ; 'speedbar-button-face
223 ; 'speedbar-highlight-face
224 ; 'gud-gdb-get-scope-data
226 (speedbar-insert-button (car (car frames))
228 'speedbar-highlight-face
229 (cond ((eq ff 'gud-gdb-find-file)
230 'gud-gdb-goto-stackframe)
231 (t (error "Should never be here.")))
233 (setq frames (cdr frames)))
234 ; (let ((selected-frame
235 ; (cond ((eq ff 'gud-gdb-find-file)
236 ; (gud-gdb-selected-frame-info buffer))
237 ; (t (error "Should never be here."))))))
239 (setq gud-last-speedbar-stackframe gud-last-last-frame)))
242 ;; ======================================================================
245 ;;; History of argument lists passed to gdb.
246 (defvar gud-gdb-history nil)
248 (defun gud-gdb-massage-args (file args)
249 (cons "-fullname" args))
251 (defvar gud-gdb-marker-regexp
252 ;; This used to use path-separator instead of ":";
253 ;; however, we found that on both Windows 32 and MSDOS
254 ;; a colon is correct here.
255 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
256 "\\([0-9]*\\)" ":" ".*\n"))
258 ;; There's no guarantee that Emacs will hand the filter the entire
259 ;; marker at once; it could be broken up across several strings. We
260 ;; might even receive a big chunk with several markers in it. If we
261 ;; receive a chunk of text which looks like it might contain the
262 ;; beginning of a marker, we save it here between calls to the
264 (defvar gud-marker-acc "")
265 (make-variable-buffer-local 'gud-marker-acc)
267 (defun gud-gdb-marker-filter (string)
268 (setq gud-marker-acc (concat gud-marker-acc string))
271 ;; Process all the complete markers in this chunk.
272 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
275 ;; Extract the frame position from the marker.
277 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
278 (string-to-int (substring gud-marker-acc
282 ;; Append any text before the marker to the output we're going
283 ;; to return - we don't include the marker in this text.
284 output (concat output
285 (substring gud-marker-acc 0 (match-beginning 0)))
287 ;; Set the accumulator to the remaining text.
288 gud-marker-acc (substring gud-marker-acc (match-end 0))))
290 ;; Does the remaining text look like it might end with the
291 ;; beginning of another marker? If it does, then keep it in
292 ;; gud-marker-acc until we receive the rest of it. Since we
293 ;; know the full marker regexp above failed, it's pretty simple to
294 ;; test for marker starts.
295 (if (string-match "\032.*\\'" gud-marker-acc)
297 ;; Everything before the potential marker start can be output.
298 (setq output (concat output (substring gud-marker-acc
299 0 (match-beginning 0))))
301 ;; Everything after, we save, to combine with later input.
303 (substring gud-marker-acc (match-beginning 0))))
305 (setq output (concat output gud-marker-acc)
310 (defun gud-gdb-find-file (f)
312 (let ((buf (find-file-noselect f)))
314 (gud-make-debug-menu)
315 (local-set-key [menu-bar debug tbreak]
316 '("Temporary Breakpoint" . gud-tbreak))
317 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
318 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
319 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
322 (defvar gdb-minibuffer-local-map nil
323 "Keymap for minibuffer prompting of gdb startup command.")
324 (if gdb-minibuffer-local-map
326 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
328 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
331 (defun gdb (command-line)
332 "Run gdb on program FILE in buffer *gud-FILE*.
333 The directory containing FILE becomes the initial working directory
334 and source-file directory for your debugger."
336 (list (read-from-minibuffer "Run gdb (like this): "
337 (if (consp gud-gdb-history)
338 (car gud-gdb-history)
340 gdb-minibuffer-local-map nil
341 '(gud-gdb-history . 1))))
343 (gud-common-init command-line 'gud-gdb-massage-args
344 'gud-gdb-marker-filter 'gud-gdb-find-file)
346 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
347 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
348 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
349 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
350 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
351 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
352 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
353 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
354 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
355 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
356 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
358 (local-set-key "\C-i" 'gud-gdb-complete-command)
359 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
360 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
361 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
362 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
363 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
364 (setq paragraph-start comint-prompt-regexp)
365 (run-hooks 'gdb-mode-hook)
368 ;; One of the nice features of GDB is its impressive support for
369 ;; context-sensitive command completion. We preserve that feature
370 ;; in the GUD buffer by using a GDB command designed just for Emacs.
372 ;; The completion process filter indicates when it is finished.
373 (defvar gud-gdb-complete-in-progress)
375 ;; Since output may arrive in fragments we accumulate partials strings here.
376 (defvar gud-gdb-complete-string)
378 ;; We need to know how much of the completion to chop off.
379 (defvar gud-gdb-complete-break)
381 ;; The completion list is constructed by the process filter.
382 (defvar gud-gdb-complete-list)
384 (defvar gud-comint-buffer nil)
386 (defun gud-gdb-complete-command ()
387 "Perform completion on the GDB command preceding point.
388 This is implemented using the GDB `complete' command which isn't
389 available with older versions of GDB."
392 (command (save-excursion
394 (and (looking-at comint-prompt-regexp)
395 (goto-char (match-end 0)))
396 (buffer-substring (point) end)))
398 ;; Find the word break. This match will always succeed.
399 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
400 (setq gud-gdb-complete-break (match-beginning 2)
401 command-word (substring command gud-gdb-complete-break))
402 ;; Temporarily install our filter function.
403 (let ((gud-marker-filter 'gud-gdb-complete-filter))
404 ;; Issue the command to GDB.
405 (gud-basic-call (concat "complete " command))
406 (setq gud-gdb-complete-in-progress t
407 gud-gdb-complete-string nil
408 gud-gdb-complete-list nil)
410 (while gud-gdb-complete-in-progress
411 (accept-process-output (get-buffer-process gud-comint-buffer))))
412 ;; Protect against old versions of GDB.
413 (and gud-gdb-complete-list
414 (string-match "^Undefined command: \"complete\""
415 (car gud-gdb-complete-list))
416 (error "This version of GDB doesn't support the `complete' command."))
417 ;; Sort the list like readline.
418 (setq gud-gdb-complete-list
419 (sort gud-gdb-complete-list (function string-lessp)))
420 ;; Remove duplicates.
421 (let ((first gud-gdb-complete-list)
422 (second (cdr gud-gdb-complete-list)))
424 (if (string-equal (car first) (car second))
425 (setcdr first (setq second (cdr second)))
427 second (cdr second)))))
428 ;; Add a trailing single quote if there is a unique completion
429 ;; and it contains an odd number of unquoted single quotes.
430 (and (= (length gud-gdb-complete-list) 1)
431 (let ((str (car gud-gdb-complete-list))
434 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
435 (setq count (1+ count)
437 (and (= (mod count 2) 1)
438 (setq gud-gdb-complete-list (list (concat str "'"))))))
439 ;; Let comint handle the rest.
440 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
442 ;; The completion process filter is installed temporarily to slurp the
443 ;; output of GDB up to the next prompt and build the completion list.
444 (defun gud-gdb-complete-filter (string)
445 (setq string (concat gud-gdb-complete-string string))
446 (while (string-match "\n" string)
447 (setq gud-gdb-complete-list
448 (cons (substring string gud-gdb-complete-break (match-beginning 0))
449 gud-gdb-complete-list))
450 (setq string (substring string (match-end 0))))
451 (if (string-match comint-prompt-regexp string)
453 (setq gud-gdb-complete-in-progress nil)
456 (setq gud-gdb-complete-string string)
459 ;; gdb speedbar functions
461 (defun gud-gdb-goto-stackframe (text token indent)
462 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
463 (speedbar-with-attached-buffer
464 (gud-basic-call (concat "frame " (nth 1 token)))
467 (defvar gud-gdb-fetched-stack-frame nil
468 "Stack frames we are fetching from GDB.")
470 (defvar gud-gdb-fetched-stack-frame-list nil
471 "List of stack frames we are fetching from GDB.")
473 ;(defun gud-gdb-get-scope-data (text token indent)
474 ; ;; checkdoc-params: (indent)
475 ; "Fetch data associated with a stack frame, and expand/contract it.
476 ;Data to do this is retrieved from TEXT and TOKEN."
477 ; (let ((args nil) (scope nil))
478 ; (gud-gdb-run-command-fetch-lines "info args")
480 ; (gud-gdb-run-command-fetch-lines "info local")
484 (defun gud-gdb-get-stackframe (buffer)
485 "Extract the current stack frame out of the GUD GDB BUFFER."
487 (gud-gdb-fetched-stack-frame-list nil))
488 (gud-gdb-run-command-fetch-lines "backtrace" buffer)
489 (if (string-match "No stack" (car gud-gdb-fetched-stack-frame-list))
490 ;; Go into some other mode???
492 (while gud-gdb-fetched-stack-frame-list
493 (let ((e (car gud-gdb-fetched-stack-frame-list))
494 (name nil) (num nil))
496 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([0-9a-zA-Z_]+\\) (" e)
497 (string-match "^#\\([0-9]+\\) +\\([0-9a-zA-Z_]+\\) (" e)))
498 (if (not (string-match
499 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
502 (list (nth 0 (car newlst))
505 (match-string 2 e))))
506 (setq num (match-string 1 e)
507 name (match-string 2 e))
511 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
512 (list name num (match-string 1 e)
516 (setq gud-gdb-fetched-stack-frame-list
517 (cdr gud-gdb-fetched-stack-frame-list)))
520 ;(defun gud-gdb-selected-frame-info (buffer)
521 ; "Learn GDB information for the currently selected stack frame in BUFFER."
524 (defun gud-gdb-run-command-fetch-lines (command buffer)
525 "Run COMMAND, and return when `gud-gdb-fetched-stack-frame-list' is full.
526 BUFFER is the GUD buffer in which to run the command."
530 (goto-char (point-max))
532 (not (looking-at comint-prompt-regexp)))
534 ;; Much of this copied from GDB complete, but I'm grabbing the stack
536 (let ((gud-marker-filter 'gud-gdb-speedbar-stack-filter))
537 ;; Issue the command to GDB.
538 (gud-basic-call command)
539 (setq gud-gdb-complete-in-progress t ;; use this flag for our purposes.
540 gud-gdb-complete-string nil
541 gud-gdb-complete-list nil)
543 (while gud-gdb-complete-in-progress
544 (accept-process-output (get-buffer-process gud-comint-buffer)))
545 (setq gud-gdb-fetched-stack-frame nil
546 gud-gdb-fetched-stack-frame-list
547 (nreverse gud-gdb-fetched-stack-frame-list))))))
549 (defun gud-gdb-speedbar-stack-filter (string)
550 ;; checkdoc-params: (string)
551 "Filter used to read in the current GDB stack."
552 (setq string (concat gud-gdb-fetched-stack-frame string))
553 (while (string-match "\n" string)
554 (setq gud-gdb-fetched-stack-frame-list
555 (cons (substring string 0 (match-beginning 0))
556 gud-gdb-fetched-stack-frame-list))
557 (setq string (substring string (match-end 0))))
558 (if (string-match comint-prompt-regexp string)
560 (setq gud-gdb-complete-in-progress nil)
563 (setq gud-gdb-complete-string string)
567 ;; ======================================================================
570 ;;; History of argument lists passed to sdb.
571 (defvar gud-sdb-history nil)
573 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
574 "If nil, we're on a System V Release 4 and don't need the tags hack.")
576 (defvar gud-sdb-lastfile nil)
578 (defun gud-sdb-massage-args (file args) args)
580 (defun gud-sdb-marker-filter (string)
582 (if gud-marker-acc (concat gud-marker-acc string) string))
584 ;; Process all complete markers in this chunk
587 ;; System V Release 3.2 uses this format
588 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
589 gud-marker-acc start)
592 (substring gud-marker-acc (match-beginning 3) (match-end 3))
594 (substring gud-marker-acc (match-beginning 4) (match-end 4))))))
595 ;; System V Release 4.0 quite often clumps two lines together
596 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
597 gud-marker-acc start)
598 (setq gud-sdb-lastfile
599 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
604 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
605 ;; System V Release 4.0
606 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
607 gud-marker-acc start)
608 (setq gud-sdb-lastfile
609 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
610 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
611 gud-marker-acc start))
616 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
618 (setq gud-sdb-lastfile nil)))
619 (setq start (match-end 0)))
621 ;; Search for the last incomplete line in this chunk
622 (while (string-match "\n" gud-marker-acc start)
623 (setq start (match-end 0)))
625 ;; If we have an incomplete line, store it in gud-marker-acc.
626 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
629 (defun gud-sdb-find-file (f)
631 (let ((buf (if gud-sdb-needs-tags
632 (find-tag-noselect f)
633 (find-file-noselect f))))
635 (gud-make-debug-menu)
636 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
640 (defun sdb (command-line)
641 "Run sdb on program FILE in buffer *gud-FILE*.
642 The directory containing FILE becomes the initial working directory
643 and source-file directory for your debugger."
645 (list (read-from-minibuffer "Run sdb (like this): "
646 (if (consp gud-sdb-history)
647 (car gud-sdb-history)
650 '(gud-sdb-history . 1))))
651 (if (and gud-sdb-needs-tags
652 (not (and (boundp 'tags-file-name)
653 (stringp tags-file-name)
654 (file-exists-p tags-file-name))))
655 (error "The sdb support requires a valid tags table to work."))
657 (gud-common-init command-line 'gud-sdb-massage-args
658 'gud-sdb-marker-filter 'gud-sdb-find-file)
660 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
661 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
662 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
663 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
664 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
665 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
666 (gud-def gud-cont "c" "\C-r" "Continue with display.")
667 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
669 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
670 (setq paragraph-start comint-prompt-regexp)
671 (local-set-key [menu-bar debug tbreak]
672 '("Temporary Breakpoint" . gud-tbreak))
673 (run-hooks 'sdb-mode-hook)
676 ;; ======================================================================
679 ;;; History of argument lists passed to dbx.
680 (defvar gud-dbx-history nil)
682 (defcustom gud-dbx-directories nil
683 "*A list of directories that dbx should search for source code.
684 If nil, only source files in the program directory
685 will be known to dbx.
687 The file names should be absolute, or relative to the directory
688 containing the executable being debugged."
689 :type '(choice (const :tag "Current Directory" nil)
694 (defun gud-dbx-massage-args (file args)
695 (nconc (let ((directories gud-dbx-directories)
698 (setq result (cons (car directories) (cons "-I" result)))
699 (setq directories (cdr directories)))
703 (defun gud-dbx-file-name (f)
704 "Transform a relative file name to an absolute file name, for dbx."
706 (if (file-exists-p f)
707 (setq result (expand-file-name f))
708 (let ((directories gud-dbx-directories))
710 (let ((path (concat (car directories) "/" f)))
711 (if (file-exists-p path)
712 (setq result (expand-file-name path)
714 (setq directories (cdr directories)))))
717 (defun gud-dbx-marker-filter (string)
718 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
721 ;; Process all complete markers in this chunk.
722 (while (or (string-match
723 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
724 gud-marker-acc start)
726 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
727 gud-marker-acc start))
730 (substring gud-marker-acc (match-beginning 2) (match-end 2))
732 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
733 start (match-end 0)))
735 ;; Search for the last incomplete line in this chunk
736 (while (string-match "\n" gud-marker-acc start)
737 (setq start (match-end 0)))
739 ;; If the incomplete line APPEARS to begin with another marker, keep it
740 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
741 ;; unnecessary concat during the next call.
743 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
744 (substring gud-marker-acc (match-beginning 0))
748 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
749 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
751 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
752 ;; We haven't tested gud on this system:
753 (string-match "^mips-[^-]*-riscos" system-configuration)
754 ;; It's documented on OSF/1.3
755 (string-match "^mips-[^-]*-osf1" system-configuration)
756 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
757 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
759 (defun gud-mipsdbx-massage-args (file args)
760 (cons "-emacs" args))
762 ;; This is just like the gdb one except for the regexps since we need to cope
763 ;; with an optional breakpoint number in [] before the ^Z^Z
764 (defun gud-mipsdbx-marker-filter (string)
765 (setq gud-marker-acc (concat gud-marker-acc string))
768 ;; Process all the complete markers in this chunk.
770 ;; This is like th gdb marker but with an optional
771 ;; leading break point number like `[1] '
772 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
776 ;; Extract the frame position from the marker.
778 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
779 (string-to-int (substring gud-marker-acc
783 ;; Append any text before the marker to the output we're going
784 ;; to return - we don't include the marker in this text.
785 output (concat output
786 (substring gud-marker-acc 0 (match-beginning 0)))
788 ;; Set the accumulator to the remaining text.
789 gud-marker-acc (substring gud-marker-acc (match-end 0))))
791 ;; Does the remaining text look like it might end with the
792 ;; beginning of another marker? If it does, then keep it in
793 ;; gud-marker-acc until we receive the rest of it. Since we
794 ;; know the full marker regexp above failed, it's pretty simple to
795 ;; test for marker starts.
796 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
798 ;; Everything before the potential marker start can be output.
799 (setq output (concat output (substring gud-marker-acc
800 0 (match-beginning 0))))
802 ;; Everything after, we save, to combine with later input.
804 (substring gud-marker-acc (match-beginning 0))))
806 (setq output (concat output gud-marker-acc)
811 ;; The dbx in IRIX is a pain. It doesn't print the file name when
812 ;; stopping at a breakpoint (but you do get it from the `up' and
813 ;; `down' commands...). The only way to extract the information seems
814 ;; to be with a `file' command, although the current line number is
815 ;; available in $curline. Thus we have to look for output which
816 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
817 ;; to output the information we want with a combination of the
818 ;; `printf' and `file' commands as a pseudo marker which we can
819 ;; recognise next time through the marker-filter. This would be like
820 ;; the gdb marker but you can't get the file name without a newline...
821 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
822 ;; number rather than a line number etc. Maybe this could be made to
823 ;; work by listing all the breakpoints and picking the one(s) with the
824 ;; correct line number, but life's too short.
825 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
828 (and (string-match "^mips-[^-]*-irix" system-configuration)
829 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
830 "Non-nil to assume the interface appropriate for IRIX dbx.
831 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
832 a better solution in 6.1 upwards.")
833 (defvar gud-dbx-use-stopformat-p
834 (string-match "irix[6-9]\\.[1-9]" system-configuration)
835 "Non-nil to use the dbx feature present at least from Irix 6.1
836 whereby $stopformat=1 produces an output format compatiable with
837 `gud-dbx-marker-filter'.")
838 ;; [Irix dbx seems to be a moving target. The dbx output changed
839 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
840 ;; the output from `up' is no longer spotted by gud (and it's probably
841 ;; not distinctive enough to try to match it -- use C-<, C->
842 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
843 ;; `long long'(why?!), so the printf stuff needed changing. The line
844 ;; number was cast to `long' as a compromise between the new `long
845 ;; long' and the original `int'. This is reported not to work in 6.2,
846 ;; so it's changed back to int -- don't make your sources too long.
847 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
848 ;; whereby `set $stopformat=1' reportedly produces output compatible
849 ;; with `gud-dbx-marker-filter', which we prefer.
851 ;; The process filter is also somewhat
852 ;; unreliable, sometimes not spotting the markers; I don't know
853 ;; whether there's anything that can be done about that. It would be
854 ;; much better if SGI could be persuaded to (re?)instate the MIPS
855 ;; -emacs flag for gdb-like output (which ought to be possible as most
856 ;; of the communication I've had over it has been from sgi.com).]
858 ;; this filter is influenced by the xdb one rather than the gdb one
859 (defun gud-irixdbx-marker-filter (string)
860 (let (result (case-fold-search nil))
861 (if (or (string-match comint-prompt-regexp string)
862 (string-match ".*\012" string))
863 (setq result (concat gud-marker-acc string)
865 (setq gud-marker-acc (concat gud-marker-acc string)))
868 ;; look for breakpoint or signal indication e.g.:
869 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
870 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
871 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
873 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
875 ;; prod dbx into printing out the line number and file
876 ;; name in a form we can grok as below
877 (process-send-string (get-buffer-process gud-comint-buffer)
878 "printf \"\032\032%1d:\",(int)$curline;file\n"))
879 ;; look for result of, say, "up" e.g.:
880 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
881 ;; (this will also catch one of the lines printed by "where")
883 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
885 (let ((file (substring result (match-beginning 1)
887 (if (file-exists-p file)
891 result (match-beginning 1) (match-end 1))
894 result (match-beginning 2) (match-end 2)))))))
896 ((string-match ; kluged-up marker as above
897 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
898 (let ((file (gud-dbx-file-name
899 (substring result (match-beginning 2) (match-end 2)))))
900 (if (and file (file-exists-p file))
906 result (match-beginning 1) (match-end 1)))))))
907 (setq result (substring result 0 (match-beginning 0))))))
910 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
911 "Non-nil means to assume the interface approriate for DG/UX dbx.
912 This was tested using R4.11.")
914 ;; There are a couple of differences between DG's dbx output and normal
915 ;; dbx output which make it nontrivial to integrate this into the
916 ;; standard dbx-marker-filter (mainly, there are a different number of
917 ;; backreferences). The markers look like:
919 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
921 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
924 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
928 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
930 ;; from up/down/where.
932 (defun gud-dguxdbx-marker-filter (string)
933 (setq gud-marker-acc (if gud-marker-acc
934 (concat gud-marker-acc string)
936 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
937 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
939 ;; Process all complete markers in this chunk.
940 (while (string-match re gud-marker-acc start)
943 (substring gud-marker-acc (match-beginning 4) (match-end 4))
944 (string-to-int (substring gud-marker-acc
945 (match-beginning 3) (match-end 3))))
946 start (match-end 0)))
948 ;; Search for the last incomplete line in this chunk
949 (while (string-match "\n" gud-marker-acc start)
950 (setq start (match-end 0)))
952 ;; If the incomplete line APPEARS to begin with another marker, keep it
953 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
954 ;; unnecessary concat during the next call.
956 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
957 (substring gud-marker-acc (match-beginning 0))
961 (defun gud-dbx-find-file (f)
963 (let ((realf (gud-dbx-file-name f)))
965 (let ((buf (find-file-noselect realf)))
967 (gud-make-debug-menu)
968 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
969 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
974 (defun dbx (command-line)
975 "Run dbx on program FILE in buffer *gud-FILE*.
976 The directory containing FILE becomes the initial working directory
977 and source-file directory for your debugger."
979 (list (read-from-minibuffer "Run dbx (like this): "
980 (if (consp gud-dbx-history)
981 (car gud-dbx-history)
984 '(gud-dbx-history . 1))))
988 (gud-common-init command-line 'gud-mipsdbx-massage-args
989 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
991 (gud-common-init command-line 'gud-dbx-massage-args
992 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
994 (gud-common-init command-line 'gud-dbx-massage-args
995 'gud-dguxdbx-marker-filter 'gud-dbx-find-file))
997 (gud-common-init command-line 'gud-dbx-massage-args
998 'gud-dbx-marker-filter 'gud-dbx-find-file)))
1002 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1003 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1004 (gud-def gud-break "stop at \"%f\":%l"
1005 "\C-b" "Set breakpoint at current line.")
1006 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1008 (gud-def gud-break "stop at \"%d%f\":%l"
1009 "\C-b" "Set breakpoint at current line.")
1010 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1011 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1012 "<" "Up (numeric arg) stack frames.")
1013 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1014 ">" "Down (numeric arg) stack frames.")
1015 ;; Make dbx give out the source location info that we need.
1016 (process-send-string (get-buffer-process gud-comint-buffer)
1017 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1018 (gud-dbx-use-stopformat-p
1019 (process-send-string (get-buffer-process gud-comint-buffer)
1020 "set $stopformat=1\n"))
1022 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1023 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1024 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1025 "\C-b" "Set breakpoint at current line.")))
1027 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1028 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1029 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1030 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1031 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1032 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1034 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1035 (setq paragraph-start comint-prompt-regexp)
1036 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1037 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1038 (run-hooks 'dbx-mode-hook)
1041 ;; ======================================================================
1042 ;; xdb (HP PARISC debugger) functions
1044 ;;; History of argument lists passed to xdb.
1045 (defvar gud-xdb-history nil)
1047 (defcustom gud-xdb-directories nil
1048 "*A list of directories that xdb should search for source code.
1049 If nil, only source files in the program directory
1050 will be known to xdb.
1052 The file names should be absolute, or relative to the directory
1053 containing the executable being debugged."
1054 :type '(choice (const :tag "Current Directory" nil)
1059 (defun gud-xdb-massage-args (file args)
1060 (nconc (let ((directories gud-xdb-directories)
1063 (setq result (cons (car directories) (cons "-d" result)))
1064 (setq directories (cdr directories)))
1068 (defun gud-xdb-file-name (f)
1069 "Transform a relative pathname to a full pathname in xdb mode"
1071 (if (file-exists-p f)
1072 (setq result (expand-file-name f))
1073 (let ((directories gud-xdb-directories))
1075 (let ((path (concat (car directories) "/" f)))
1076 (if (file-exists-p path)
1077 (setq result (expand-file-name path)
1079 (setq directories (cdr directories)))))
1082 ;; xdb does not print the lines all at once, so we have to accumulate them
1083 (defun gud-xdb-marker-filter (string)
1085 (if (or (string-match comint-prompt-regexp string)
1086 (string-match ".*\012" string))
1087 (setq result (concat gud-marker-acc string)
1089 (setq gud-marker-acc (concat gud-marker-acc string)))
1091 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1093 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1095 (let ((line (string-to-int
1096 (substring result (match-beginning 2) (match-end 2))))
1097 (file (gud-xdb-file-name
1098 (substring result (match-beginning 1) (match-end 1)))))
1100 (setq gud-last-frame (cons file line))))))
1103 (defun gud-xdb-find-file (f)
1105 (let ((realf (gud-xdb-file-name f)))
1107 (let ((buf (find-file-noselect realf)))
1109 (gud-make-debug-menu)
1110 (local-set-key [menu-bar debug tbreak]
1111 '("Temporary Breakpoint" . gud-tbreak))
1112 (local-set-key [menu-bar debug finish]
1113 '("Finish Function" . gud-finish))
1114 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1115 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1120 (defun xdb (command-line)
1121 "Run xdb on program FILE in buffer *gud-FILE*.
1122 The directory containing FILE becomes the initial working directory
1123 and source-file directory for your debugger.
1125 You can set the variable 'gud-xdb-directories' to a list of program source
1126 directories if your program contains sources from more than one directory."
1128 (list (read-from-minibuffer "Run xdb (like this): "
1129 (if (consp gud-xdb-history)
1130 (car gud-xdb-history)
1133 '(gud-xdb-history . 1))))
1135 (gud-common-init command-line 'gud-xdb-massage-args
1136 'gud-xdb-marker-filter 'gud-xdb-find-file)
1138 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1139 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1140 "Set temporary breakpoint at current line.")
1141 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1142 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1143 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1144 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1145 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1146 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1147 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1148 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1150 (setq comint-prompt-regexp "^>")
1151 (setq paragraph-start comint-prompt-regexp)
1152 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
1153 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1154 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1155 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1156 (run-hooks 'xdb-mode-hook))
1158 ;; ======================================================================
1161 ;;; History of argument lists passed to perldb.
1162 (defvar gud-perldb-history nil)
1164 (defun gud-perldb-massage-args (file args)
1165 (cond ((equal (car args) "-e")
1169 (cons "--" (cons "-emacs" (cdr (cdr args))))))))
1171 (cons "-d" (cons (car args) (cons "-emacs" (cdr args)))))))
1173 ;; There's no guarantee that Emacs will hand the filter the entire
1174 ;; marker at once; it could be broken up across several strings. We
1175 ;; might even receive a big chunk with several markers in it. If we
1176 ;; receive a chunk of text which looks like it might contain the
1177 ;; beginning of a marker, we save it here between calls to the
1179 (defvar gud-perldb-marker-acc "")
1181 (defun gud-perldb-marker-filter (string)
1182 (setq gud-marker-acc (concat gud-marker-acc string))
1185 ;; Process all the complete markers in this chunk.
1186 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1190 ;; Extract the frame position from the marker.
1192 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1193 (string-to-int (substring gud-marker-acc
1197 ;; Append any text before the marker to the output we're going
1198 ;; to return - we don't include the marker in this text.
1199 output (concat output
1200 (substring gud-marker-acc 0 (match-beginning 0)))
1202 ;; Set the accumulator to the remaining text.
1203 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1205 ;; Does the remaining text look like it might end with the
1206 ;; beginning of another marker? If it does, then keep it in
1207 ;; gud-marker-acc until we receive the rest of it. Since we
1208 ;; know the full marker regexp above failed, it's pretty simple to
1209 ;; test for marker starts.
1210 (if (string-match "\032.*\\'" gud-marker-acc)
1212 ;; Everything before the potential marker start can be output.
1213 (setq output (concat output (substring gud-marker-acc
1214 0 (match-beginning 0))))
1216 ;; Everything after, we save, to combine with later input.
1217 (setq gud-marker-acc
1218 (substring gud-marker-acc (match-beginning 0))))
1220 (setq output (concat output gud-marker-acc)
1225 (defun gud-perldb-find-file (f)
1227 (let ((buf (find-file-noselect f)))
1229 (gud-make-debug-menu)
1232 (defcustom gud-perldb-command-name "perl"
1233 "File name for executing Perl."
1238 (defun perldb (command-line)
1239 "Run perldb on program FILE in buffer *gud-FILE*.
1240 The directory containing FILE becomes the initial working directory
1241 and source-file directory for your debugger."
1243 (list (read-from-minibuffer "Run perldb (like this): "
1244 (if (consp gud-perldb-history)
1245 (car gud-perldb-history)
1246 (concat gud-perldb-command-name
1248 (or (buffer-file-name)
1252 '(gud-perldb-history . 1))))
1254 (gud-common-init command-line 'gud-perldb-massage-args
1255 'gud-perldb-marker-filter 'gud-perldb-find-file)
1257 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1258 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1259 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1260 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1261 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1262 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1263 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1264 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1265 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1267 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1268 (setq paragraph-start comint-prompt-regexp)
1269 (run-hooks 'perldb-mode-hook)
1272 ;; ======================================================================
1276 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1278 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1280 ;; INVOCATION NOTES:
1282 ;; You invoke jdb-mode with:
1286 ;; It responds with:
1288 ;; Run jdb (like this): jdb
1290 ;; type any jdb switches followed by the name of the class you'd like to debug.
1291 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1292 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1293 ;; See the known problems section below for restrictions when specifying jdb
1294 ;; command line switches (search forward for '-classpath').
1296 ;; You should see something like the following:
1298 ;; Current directory is ~/src/java/hello/
1299 ;; Initializing jdb...
1300 ;; 0xed2f6628:class(hello)
1303 ;; To set an initial breakpoint try:
1305 ;; > stop in hello.main
1306 ;; Breakpoint set in hello.main
1309 ;; To execute the program type:
1314 ;; Breakpoint hit: running ...
1315 ;; hello.main (hello:12)
1317 ;; Type M-n to step over the current line and M-s to step into it. That,
1318 ;; along with the JDB 'help' command should get you started. The 'quit'
1319 ;; JDB command will get out out of the debugger. There is some truly
1320 ;; pathetic JDB documentation available at:
1322 ;; http://java.sun.com/products/jdk/1.1/debugging/
1324 ;; KNOWN PROBLEMS AND FIXME's:
1326 ;; Not sure what happens with inner classes ... haven't tried them.
1328 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1330 ;; You must not put whitespace between "-classpath" and the path to
1331 ;; search for java classes even though it is required when invoking jdb
1332 ;; from the command line. See gud-jdb-massage-args for details.
1334 ;; If any of the source files in the directories listed in
1335 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1336 ;; every file ending in ".java" in these directories parses without error.
1338 ;; All the .java files in the directories in gud-jdb-directories are
1339 ;; syntactically analyzed each time gud jdb is invoked. It would be
1340 ;; nice to keep as much information as possible between runs. It would
1341 ;; be really nice to analyze the files only as neccessary (when the
1342 ;; source needs to be displayed.) I'm not sure to what extent the former
1343 ;; can be accomplished and I'm not sure the latter can be done at all
1344 ;; since I don't know of any general way to tell which .class files are
1345 ;; defined by which .java file without analyzing all the .java files.
1346 ;; If anyone knows why JavaSoft didn't put the source file names in
1347 ;; debuggable .class files please clue me in so I find something else
1348 ;; to be spiteful and bitter about.
1350 ;; ======================================================================
1351 ;; gud jdb variables and functions
1353 ;; History of argument lists passed to jdb.
1354 (defvar gud-jdb-history nil)
1356 ;; List of Java source file directories.
1357 (defvar gud-jdb-directories (list ".")
1358 "*A list of directories that gud jdb should search for source code.
1359 The file names should be absolute, or relative to the current directory.")
1361 ;; List of the java source files for this debugging session.
1362 (defvar gud-jdb-source-files nil)
1364 ;; Return a list of java source files. PATH gives the directories in
1365 ;; which to search for files with extension EXTN. Normally EXTN is
1366 ;; given as the regular expression "\\.java$" .
1367 (defun gud-jdb-build-source-files-list (path extn)
1368 (apply 'nconc (mapcar (lambda (d) (directory-files d t extn nil)) path)))
1370 ;; Move point past whitespace.
1371 (defun gud-jdb-skip-whitespace ()
1372 (skip-chars-forward " \n\r\t\014"))
1374 ;; Move point past a "// <eol>" type of comment.
1375 (defun gud-jdb-skip-single-line-comment ()
1378 ;; Move point past a "/* */" or "/** */" type of comment.
1379 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1383 (if (eq (following-char) ?*)
1387 (if (eq (following-char) ?/)
1390 (throw 'break nil)))))
1393 ;; Move point past any number of consecutive whitespace chars and/or comments.
1394 (defun gud-jdb-skip-whitespace-and-comments ()
1395 (gud-jdb-skip-whitespace)
1400 (gud-jdb-skip-single-line-comment)
1401 (gud-jdb-skip-whitespace))
1402 ((looking-at "/\\*")
1403 (gud-jdb-skip-traditional-or-documentation-comment)
1404 (gud-jdb-skip-whitespace))
1405 (t (throw 'done nil))))))
1407 ;; Move point past things that are id-like. The intent is to skip regular
1408 ;; id's, such as class or interface names as well as package and interface
1410 (defun gud-jdb-skip-id-ish-thing ()
1411 (skip-chars-forward "^ /\n\r\t\014,;{"))
1413 ;; Move point past a string literal.
1414 (defun gud-jdb-skip-string-literal ()
1418 (if (eq (following-char) ?\\)
1420 (not (eq (following-char) ?\042)))
1424 ;; Move point past a character literal.
1425 (defun gud-jdb-skip-character-literal ()
1429 (if (eq (following-char) ?\\)
1431 (not (eq (following-char) ?\')))
1435 ;; Move point past the following block. There may be (legal) cruft before
1436 ;; the block's opening brace. There must be a block or it's the end of life
1437 ;; in petticoat junction.
1438 (defun gud-jdb-skip-block ()
1440 ;; Find the begining of the block.
1442 (not (eq (following-char) ?{))
1444 ;; Skip any constructs that can harbor literal block delimiter
1445 ;; characters and/or the delimiters for the constructs themselves.
1448 (gud-jdb-skip-single-line-comment))
1449 ((looking-at "/\\*")
1450 (gud-jdb-skip-traditional-or-documentation-comment))
1451 ((eq (following-char) ?\042)
1452 (gud-jdb-skip-string-literal))
1453 ((eq (following-char) ?\')
1454 (gud-jdb-skip-character-literal))
1455 (t (forward-char))))
1457 ;; Now at the begining of the block.
1460 ;; Skip over the body of the block as well as the final brace.
1461 (let ((open-level 1))
1462 (while (not (eq open-level 0))
1465 (gud-jdb-skip-single-line-comment))
1466 ((looking-at "/\\*")
1467 (gud-jdb-skip-traditional-or-documentation-comment))
1468 ((eq (following-char) ?\042)
1469 (gud-jdb-skip-string-literal))
1470 ((eq (following-char) ?\')
1471 (gud-jdb-skip-character-literal))
1472 ((eq (following-char) ?{)
1473 (setq open-level (+ open-level 1))
1475 ((eq (following-char) ?})
1476 (setq open-level (- open-level 1))
1478 (t (forward-char))))))
1480 ;; Find the package and class definitions in Java source file FILE. Assumes
1481 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1482 ;; to hold the source during analysis.
1483 (defun gud-jdb-analyze-source (buf file)
1486 (insert-file-contents file nil nil nil t)
1491 (gud-jdb-skip-whitespace)
1495 ;; Any number of semi's following a block is legal. Move point
1496 ;; past them. Note that comments and whitespace may be
1497 ;; interspersed as well.
1498 ((eq (following-char) ?\073)
1501 ;; Move point past a single line comment.
1503 (gud-jdb-skip-single-line-comment))
1505 ;; Move point past a traditional or documentation comment.
1506 ((looking-at "/\\*")
1507 (gud-jdb-skip-traditional-or-documentation-comment))
1509 ;; Move point past a package statement, but save the PackageName.
1510 ((looking-at "package")
1512 (gud-jdb-skip-whitespace-and-comments)
1514 (gud-jdb-skip-id-ish-thing)
1515 (setq p (concat (buffer-substring s (point)) "."))
1516 (gud-jdb-skip-whitespace-and-comments)
1517 (if (eq (following-char) ?\073)
1520 ;; Move point past an import statement.
1521 ((looking-at "import")
1523 (gud-jdb-skip-whitespace-and-comments)
1524 (gud-jdb-skip-id-ish-thing)
1525 (gud-jdb-skip-whitespace-and-comments)
1526 (if (eq (following-char) ?\073)
1529 ;; Move point past the various kinds of ClassModifiers.
1530 ((looking-at "public")
1532 ((looking-at "abstract")
1534 ((looking-at "final")
1537 ;; Move point past a ClassDeclaraction, but save the class
1539 ((looking-at "class")
1541 (gud-jdb-skip-whitespace-and-comments)
1543 (gud-jdb-skip-id-ish-thing)
1545 l (nconc l (list (concat p (buffer-substring s (point)))))))
1546 (gud-jdb-skip-block))
1548 ;; Move point past an interface statement.
1549 ((looking-at "interface")
1551 (gud-jdb-skip-block))
1553 ;; Anything else means the input is invalid.
1555 (message (format "Error parsing file %s." file))
1556 (throw 'abort nil))))))
1559 (defun gud-jdb-build-class-source-alist-for-file (file)
1563 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1565 ;; Association list of fully qualified class names (package + class name) and
1566 ;; their source files.
1567 (defvar gud-jdb-class-source-alist nil)
1569 ;; This is used to hold a source file during analysis.
1570 (defvar gud-jdb-analysis-buffer nil)
1572 ;; Return an alist of fully qualified classes and the source files
1573 ;; holding their definitions. SOURCES holds a list of all the source
1574 ;; files to examine.
1575 (defun gud-jdb-build-class-source-alist (sources)
1576 (setq gud-jdb-analysis-buffer (get-buffer-create "*gud-jdb-scratch*"))
1581 'gud-jdb-build-class-source-alist-for-file
1583 (kill-buffer gud-jdb-analysis-buffer)
1584 (setq gud-jdb-analysis-buffer nil)))
1586 ;; Change what was given in the minibuffer to something that can be used to
1587 ;; invoke the debugger.
1588 (defun gud-jdb-massage-args (file args)
1589 ;; The jdb executable must have whitespace between "-classpath" and
1590 ;; it's value while gud-common-init expects all switch values to
1591 ;; follow the switch keyword without intervening whitespace. We
1592 ;; require that when the user enters the "-classpath" switch in the
1593 ;; EMACS minibuffer that they do so without the intervening
1594 ;; whitespace. This function adds it back (it's called after
1595 ;; gud-common-init). There are more switches like this (for
1596 ;; instance "-host" and "-password") but I don't care about them
1599 (let (massaged-args user-error)
1603 (not (string-match "-classpath\\(.+\\)" (car args)))
1604 (not (setq user-error
1605 (string-match "-classpath$" (car args)))))
1606 (setq massaged-args (append massaged-args (list (car args))))
1607 (setq args (cdr args)))
1609 ;; By this point the current directory is all screwed up. Maybe we
1610 ;; could fix things and re-invoke gud-common-init, but for now I think
1611 ;; issueing the error is good enough.
1614 (kill-buffer (current-buffer))
1615 (error "Error: Omit whitespace between '-classpath' and it's value")))
1625 (match-beginning 1) (match-end 1)))
1629 ;; Search for an association with P, a fully qualified class name, in
1630 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1631 ;; qualified file name of the source file which produced the class.
1632 (defun gud-jdb-find-source-file (p)
1633 (cdr (assoc p gud-jdb-class-source-alist)))
1635 ;; See comentary for other debugger's marker filters - there you will find
1636 ;; important notes about STRING.
1637 (defun gud-jdb-marker-filter (string)
1639 ;; Build up the accumulator.
1640 (setq gud-marker-acc
1642 (concat gud-marker-acc string)
1645 ;; We process STRING from left to right. Each time through the following
1646 ;; loop we process at most one marker. The start variable keeps track of
1647 ;; where we are in the input string through the iterations of this loop.
1648 (let (start file-found)
1650 ;; Process each complete marker in the input. There may be an incomplete
1651 ;; marker at the end of the input string. Incomplete markers are left
1652 ;; in the accumulator for processing the next time the function is called.
1655 ;; Do we see a marker?
1657 ;; jdb puts out a string of the following form when it
1658 ;; hits a breakpoint:
1660 ;; <fully-qualified-class><method> (<class>:<line-number>)
1662 ;; <fully-qualified-class>'s are composed of Java ID's
1663 ;; separated by periods. <method> and <class> are
1664 ;; also Java ID's. <method> begins with a period and
1665 ;; may contain less-than and greater-than (constructors,
1666 ;; for instance, are called <init> in the symbol table.)
1667 ;; Java ID's begin with a letter followed by letters
1668 ;; and/or digits. The set of letters includes underscore
1671 ;; The first group matches <fully-qualified-class>,
1672 ;; the second group matches <class> and the third group
1673 ;; matches <line-number>. We don't care about using
1674 ;; <method> so we don't "group" it.
1676 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1678 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1679 gud-marker-acc start)
1681 ;; Figure out the line on which to position the debugging arrow.
1682 ;; Return the info as a cons of the form:
1684 ;; (<file-name> . <line-number>) .
1687 (gud-jdb-find-source-file
1688 (substring gud-marker-acc
1691 (setq gud-last-frame
1695 (substring gud-marker-acc
1698 (message "Could not find source file."))
1700 ;; Set start after the last character of STRING that we've looked at
1701 ;; and loop to look for another marker.
1702 (setq start (match-end 0))))
1704 ;; We don't filter any debugger output so just return what we were given.
1707 (defun gud-jdb-find-file (f)
1708 (and (file-readable-p f)
1709 (find-file-noselect f)))
1711 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1714 (defun jdb (command-line)
1715 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1716 \"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1717 if there is. If the \"-classpath\" switch is given, omit all whitespace
1718 between it and it's value."
1720 (list (read-from-minibuffer "Run jdb (like this): "
1721 (if (consp gud-jdb-history)
1722 (car gud-jdb-history)
1723 (concat gud-jdb-command-name " "))
1725 '(gud-jdb-history . 1))))
1727 (gud-common-init command-line 'gud-jdb-massage-args
1728 'gud-jdb-marker-filter 'gud-jdb-find-file)
1730 (gud-def gud-break "stop at %l" "\C-b" "Set breakpoint at current line.")
1731 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1732 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1733 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1734 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1736 (setq comint-prompt-regexp "^> \|^.+\[[0-9]+\] ")
1737 (setq paragraph-start comint-prompt-regexp)
1738 (run-hooks 'jdb-mode-hook)
1740 ;; Create and bind the class/source association list as well as the source
1743 gud-jdb-class-source-alist
1744 (gud-jdb-build-class-source-alist
1746 gud-jdb-source-files
1747 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1751 ;; End of debugger-specific information
1755 ;;; When we send a command to the debugger via gud-call, it's annoying
1756 ;;; to see the command and the new prompt inserted into the debugger's
1757 ;;; buffer; we have other ways of knowing the command has completed.
1759 ;;; If the buffer looks like this:
1760 ;;; --------------------
1761 ;;; (gdb) set args foo bar
1763 ;;; --------------------
1764 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1765 ;;; source file to set a breakpoint, we want the buffer to end up like
1767 ;;; --------------------
1768 ;;; (gdb) set args foo bar
1769 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1771 ;;; --------------------
1772 ;;; Essentially, the old prompt is deleted, and the command's output
1773 ;;; and the new prompt take its place.
1775 ;;; Not echoing the command is easy enough; you send it directly using
1776 ;;; process-send-string, and it never enters the buffer. However,
1777 ;;; getting rid of the old prompt is trickier; you don't want to do it
1778 ;;; when you send the command, since that will result in an annoying
1779 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1780 ;;; waits for a response from the debugger, and the new prompt is
1781 ;;; inserted. Instead, we'll wait until we actually get some output
1782 ;;; from the subprocess before we delete the prompt. If the command
1783 ;;; produced no output other than a new prompt, that prompt will most
1784 ;;; likely be in the first chunk of output received, so we will delete
1785 ;;; the prompt and then replace it with an identical one. If the
1786 ;;; command produces output, the prompt is moving anyway, so the
1787 ;;; flicker won't be annoying.
1789 ;;; So - when we want to delete the prompt upon receipt of the next
1790 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1791 ;;; the start of the prompt; the process filter will notice this, and
1792 ;;; delete all text between it and the process output marker. If
1793 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1795 (defvar gud-delete-prompt-marker nil)
1798 (put 'gud-mode 'mode-class 'special)
1801 "Major mode for interacting with an inferior debugger process.
1803 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1804 M-x perldb, or M-x xdb. Each entry point finishes by executing a
1805 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
1806 `perldb-mode-hook', or `xdb-mode-hook' respectively.
1808 After startup, the following commands are available in both the GUD
1809 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1812 \\[gud-break] sets a breakpoint at the current file and line. In the
1813 GUD buffer, the current file and line are those of the last breakpoint or
1814 step. In a source buffer, they are the buffer's file and current line.
1816 \\[gud-remove] removes breakpoints on the current file and line.
1818 \\[gud-refresh] displays in the source window the last line referred to
1821 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1822 step-one-line (not entering function calls), and step-one-instruction
1823 and then update the source window with the current file and position.
1824 \\[gud-cont] continues execution.
1826 \\[gud-print] tries to find the largest C lvalue or function-call expression
1827 around point, and sends it to the debugger for value display.
1829 The above commands are common to all supported debuggers except xdb which
1830 does not support stepping instructions.
1832 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1833 except that the breakpoint is temporary; that is, it is removed when
1834 execution stops on it.
1836 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1837 frame. \\[gud-down] drops back down through one.
1839 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1840 the current function and stops.
1842 All the keystrokes above are accessible in the GUD buffer
1843 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1845 All pre-defined functions for which the concept make sense repeat
1846 themselves the appropriate number of times if you give a prefix
1849 You may use the `gud-def' macro in the initialization hook to define other
1852 Other commands for interacting with the debugger process are inherited from
1853 comint mode, which see."
1856 (setq major-mode 'gud-mode)
1857 (setq mode-name "Debugger")
1858 (setq mode-line-process '(":%s"))
1859 (use-local-map comint-mode-map)
1860 (gud-make-debug-menu)
1861 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
1862 (make-local-variable 'gud-last-frame)
1863 (setq gud-last-frame nil)
1864 (make-local-variable 'comint-prompt-regexp)
1865 ;; Don't put repeated commands in command history many times.
1866 (make-local-variable 'comint-input-ignoredups)
1867 (setq comint-input-ignoredups t)
1868 (make-local-variable 'paragraph-start)
1869 (make-local-variable 'gud-delete-prompt-marker)
1870 (setq gud-delete-prompt-marker (make-marker))
1871 (run-hooks 'gud-mode-hook))
1873 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1874 (defun gud-chop-words (string)
1876 (len (length string))
1879 (if (memq (aref string i) '(?\t ? ))
1881 (setq words (cons (substring string beg i) words)
1883 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1884 (setq beg (1+ beg)))
1888 (setq words (cons (substring string beg) words)))
1891 ;; Perform initializations common to all debuggers.
1892 ;; The first arg is the specified command line,
1893 ;; which starts with the program to debug.
1894 ;; The other three args specify the values to use
1895 ;; for local variables in the debugger buffer.
1896 (defun gud-common-init (command-line massage-args marker-filter find-file)
1897 (let* ((words (gud-chop-words command-line))
1898 (program (car words))
1899 ;; Extract the file name from WORDS
1900 ;; and put t in its place.
1901 ;; Later on we will put the modified file name arg back there.
1902 (file-word (let ((w (cdr words)))
1903 (while (and w (= ?- (aref (car w) 0)))
1909 (and file-word (substitute-in-file-name file-word)))
1911 ;; If a directory was specified, expand the file name.
1912 ;; Otherwise, don't expand it, so GDB can use the PATH.
1913 ;; A file name without directory is literally valid
1914 ;; only if the file exists in ., and in that case,
1915 ;; omitting the expansion here has no visible effect.
1916 (file (and file-word
1917 (if (file-name-directory file-subst)
1918 (expand-file-name file-subst)
1920 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
1921 (switch-to-buffer (concat "*gud" filepart "*"))
1922 ;; Set default-directory to the file's directory.
1924 ;; Don't set default-directory if no directory was specified.
1925 ;; In that case, either the file is found in the current directory,
1926 ;; in which case this setq is a no-op,
1927 ;; or it is found by searching PATH,
1928 ;; in which case we don't know what directory it was found in.
1929 (file-name-directory file)
1930 (setq default-directory (file-name-directory file)))
1931 (or (bolp) (newline))
1932 (insert "Current directory is " default-directory "\n")
1933 ;; Put the substituted and expanded file name back in its place.
1935 (while (and w (not (eq (car w) t)))
1939 (apply 'make-comint (concat "gud" filepart) program nil
1940 (funcall massage-args file args)))
1941 ;; Since comint clobbered the mode, we don't set it until now.
1943 (make-local-variable 'gud-marker-filter)
1944 (setq gud-marker-filter marker-filter)
1945 (make-local-variable 'gud-find-file)
1946 (setq gud-find-file find-file)
1948 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1949 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
1953 (defun gud-set-buffer ()
1954 (cond ((eq major-mode 'gud-mode)
1955 (setq gud-comint-buffer (current-buffer)))))
1957 (defvar gud-filter-defer-flag nil
1958 "Non-nil means don't process anything from the debugger right now.
1959 It is saved for when this flag is not set.")
1961 (defvar gud-filter-pending-text nil
1962 "Non-nil means this is text that has been saved for later in `gud-filter'.")
1964 ;; These functions are responsible for inserting output from your debugger
1965 ;; into the buffer. The hard work is done by the method that is
1966 ;; the value of gud-marker-filter.
1968 (defun gud-filter (proc string)
1969 ;; Here's where the actual buffer insertion is done
1970 (let (output process-window)
1971 (if (buffer-name (process-buffer proc))
1972 (if gud-filter-defer-flag
1973 ;; If we can't process any text now,
1974 ;; save it for later.
1975 (setq gud-filter-pending-text
1976 (concat (or gud-filter-pending-text "") string))
1978 ;; If we have to ask a question during the processing,
1979 ;; defer any additional text that comes from the debugger
1980 ;; during that time.
1981 (let ((gud-filter-defer-flag t))
1982 ;; Process now any text we previously saved up.
1983 (if gud-filter-pending-text
1984 (setq string (concat gud-filter-pending-text string)
1985 gud-filter-pending-text nil))
1987 (set-buffer (process-buffer proc))
1988 ;; If we have been so requested, delete the debugger prompt.
1989 (if (marker-buffer gud-delete-prompt-marker)
1991 (delete-region (process-mark proc) gud-delete-prompt-marker)
1992 (set-marker gud-delete-prompt-marker nil)))
1993 ;; Save the process output, checking for source file markers.
1994 (setq output (gud-marker-filter string))
1995 ;; Check for a filename-and-line number.
1996 ;; Don't display the specified file
1997 ;; unless (1) point is at or after the position where output appears
1998 ;; and (2) this buffer is on the screen.
1999 (setq process-window
2001 (>= (point) (process-mark proc))
2002 (get-buffer-window (current-buffer))))
2004 ;; Let the comint filter do the actual insertion.
2005 ;; That lets us inherit various comint features.
2006 (comint-output-filter proc output)))
2008 ;; Put the arrow on the source line.
2009 ;; This must be outside of the save-excursion
2010 ;; in case the source file is our current buffer.
2012 (save-selected-window
2013 (select-window process-window)
2014 (gud-display-frame))
2015 ;; We have to be in the proper buffer, (process-buffer proc),
2016 ;; but not in a save-excursion, because that would restore point.
2017 (let ((old-buf (current-buffer)))
2018 (set-buffer (process-buffer proc))
2021 (set-buffer old-buf))))
2023 ;; If we deferred text that arrived during this processing,
2025 (if gud-filter-pending-text
2026 (gud-filter proc ""))))))
2028 (defun gud-sentinel (proc msg)
2029 (cond ((null (buffer-name (process-buffer proc)))
2031 ;; Stop displaying an arrow in a source file.
2032 (setq overlay-arrow-position nil)
2033 (set-process-buffer proc nil))
2034 ((memq (process-status proc) '(signal exit))
2035 ;; Stop displaying an arrow in a source file.
2036 (setq overlay-arrow-position nil)
2037 (let* ((obuf (current-buffer)))
2038 ;; save-excursion isn't the right thing if
2039 ;; process-buffer is current-buffer
2042 ;; Write something in *compilation* and hack its mode line,
2043 (set-buffer (process-buffer proc))
2044 ;; Fix the mode line.
2045 (setq mode-line-process
2047 (symbol-name (process-status proc))))
2048 (force-mode-line-update)
2050 (insert ?\n mode-name " " msg)
2052 (goto-char (point-max))
2053 (insert ?\n mode-name " " msg)))
2054 ;; If buffer and mode line will show that the process
2055 ;; is dead, we can delete it now. Otherwise it
2056 ;; will stay around until M-x list-processes.
2057 (delete-process proc))
2058 ;; Restore old buffer, but don't restore old point
2059 ;; if obuf is the gud buffer.
2060 (set-buffer obuf))))))
2062 (defun gud-display-frame ()
2063 "Find and obey the last filename-and-line marker from the debugger.
2064 Obeying it means displaying in another window the specified file and line."
2069 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2070 (setq gud-last-last-frame gud-last-frame
2071 gud-last-frame nil))))
2073 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2074 ;; and that its line LINE is visible.
2075 ;; Put the overlay-arrow on the line LINE in that buffer.
2076 ;; Most of the trickiness in here comes from wanting to preserve the current
2077 ;; region-restriction if that's possible. We use an explicit display-buffer
2078 ;; to get around the fact that this is called inside a save-excursion.
2080 (defun gud-display-line (true-file line)
2081 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2084 (or (eq (current-buffer) gud-comint-buffer)
2085 (set-buffer gud-comint-buffer))
2086 (gud-find-file true-file)))
2087 (window (and buffer (or (get-buffer-window buffer)
2088 (display-buffer buffer))))
2098 (setq overlay-arrow-string "=>")
2099 (or overlay-arrow-position
2100 (setq overlay-arrow-position (make-marker)))
2101 (set-marker overlay-arrow-position (point) (current-buffer)))
2102 (cond ((or (< pos (point-min)) (> pos (point-max)))
2105 (set-window-point window overlay-arrow-position)))))
2107 ;;; The gud-call function must do the right thing whether its invoking
2108 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2109 ;;; or a C buffer. In the former case, we want to supply data from
2110 ;;; gud-last-frame. Here's how we do it:
2112 (defun gud-format-command (str arg)
2113 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2114 (frame (or gud-last-frame gud-last-last-frame))
2116 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2117 (let ((key (string-to-char (substring str (match-beginning 2))))
2121 (setq subst (file-name-nondirectory (if insource
2125 (setq subst (file-name-directory (if insource
2129 (setq subst (if insource
2132 (save-restriction (widen)
2133 (1+ (count-lines 1 (point)))))
2136 (setq subst (gud-find-c-expr)))
2138 (setq subst (gud-read-address)))
2140 (setq subst (if arg (int-to-string arg) ""))))
2141 (setq result (concat result
2142 (substring str (match-beginning 1) (match-end 1))
2144 (setq str (substring str (match-end 2))))
2145 ;; There might be text left in STR when the loop ends.
2146 (concat result str)))
2148 (defun gud-read-address ()
2149 "Return a string containing the core-address found in the buffer at point."
2151 (let ((pt (point)) found begin)
2152 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2154 (found (forward-char 2)
2155 (buffer-substring found
2156 (progn (re-search-forward "[^0-9a-f]")
2159 (t (setq begin (progn (re-search-backward "[^0-9]")
2163 (re-search-forward "[^0-9]")
2165 (buffer-substring begin (point)))))))
2167 (defun gud-call (fmt &optional arg)
2168 (let ((msg (gud-format-command fmt arg)))
2169 (message "Command: %s" msg)
2171 (gud-basic-call msg)))
2173 (defun gud-basic-call (command)
2174 "Invoke the debugger COMMAND displaying source in other window."
2177 (let ((command (concat command "\n"))
2178 (proc (get-buffer-process gud-comint-buffer)))
2179 (or proc (error "Current buffer has no process"))
2180 ;; Arrange for the current prompt to get deleted.
2182 (set-buffer gud-comint-buffer)
2183 (goto-char (process-mark proc))
2185 (if (looking-at comint-prompt-regexp)
2186 (set-marker gud-delete-prompt-marker (point))))
2187 (process-send-string proc command)))
2189 (defun gud-refresh (&optional arg)
2190 "Fix up a possibly garbled display, and redraw the arrow."
2193 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2194 (gud-display-frame))
2197 (defun gud-new-keymap (map)
2198 "Return a new keymap which inherits from MAP and has name `Gud'."
2199 (nconc (make-sparse-keymap "Gud") map))
2201 (defun gud-make-debug-menu ()
2202 "Make sure the current local map has a [menu-bar debug] submap.
2203 If it doesn't, replace it with a new map that inherits it,
2204 and create such a submap in that new map."
2205 (if (and (current-local-map)
2206 (lookup-key (current-local-map) [menu-bar debug]))
2208 (use-local-map (gud-new-keymap (current-local-map)))
2209 (define-key (current-local-map) [menu-bar debug]
2210 (cons "Gud" (gud-new-keymap gud-menu-map)))))
2212 ;;; Code for parsing expressions out of C code. The single entry point is
2213 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2215 ;;; The rest of this file is a hacked version of gdbsrc.el by
2216 ;;; Debby Ayers <ayers@asc.slb.com>,
2217 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2219 (defun gud-find-c-expr ()
2220 "Returns the C expr that surrounds point."
2223 (let (p expr test-expr)
2225 (setq expr (gud-innermost-expr))
2226 (setq test-expr (gud-prev-expr))
2227 (while (and test-expr (gud-expr-compound test-expr expr))
2228 (let ((prev-expr expr))
2229 (setq expr (cons (car test-expr) (cdr expr)))
2230 (goto-char (car expr))
2231 (setq test-expr (gud-prev-expr))
2232 ;; If we just pasted on the condition of an if or while,
2233 ;; throw it away again.
2234 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2235 '("if" "while" "for"))
2239 (setq test-expr (gud-next-expr))
2240 (while (gud-expr-compound expr test-expr)
2241 (setq expr (cons (car expr) (cdr test-expr)))
2242 (setq test-expr (gud-next-expr))
2244 (buffer-substring (car expr) (cdr expr)))))
2246 (defun gud-innermost-expr ()
2247 "Returns the smallest expr that point is in; move point to beginning of it.
2248 The expr is represented as a cons cell, where the car specifies the point in
2249 the current buffer that marks the beginning of the expr and the cdr specifies
2250 the character after the end of the expr."
2251 (let ((p (point)) begin end)
2253 (setq begin (point))
2266 (defun gud-backward-sexp ()
2267 "Version of `backward-sexp' that catches errors."
2272 (defun gud-forward-sexp ()
2273 "Version of `forward-sexp' that catches errors."
2278 (defun gud-prev-expr ()
2279 "Returns the previous expr, point is set to beginning of that expr.
2280 The expr is represented as a cons cell, where the car specifies the point in
2281 the current buffer that marks the beginning of the expr and the cdr specifies
2282 the character after the end of the expr"
2283 (let ((begin) (end))
2285 (setq begin (point))
2291 (defun gud-next-expr ()
2292 "Returns the following expr, point is set to beginning of that expr.
2293 The expr is represented as a cons cell, where the car specifies the point in
2294 the current buffer that marks the beginning of the expr and the cdr specifies
2295 the character after the end of the expr."
2296 (let ((begin) (end))
2301 (setq begin (point))
2304 (defun gud-expr-compound-sep (span-start span-end)
2305 "Scan from SPAN-START to SPAN-END for punctuation characters.
2306 If `->' is found, return `?.'. If `.' is found, return `?.'.
2307 If any other punctuation is found, return `??'.
2308 If no punctuation is found, return `? '."
2311 (while (< span-start span-end)
2312 (setq syntax (char-syntax (char-after span-start)))
2315 ((= syntax ?.) (setq syntax (char-after span-start))
2317 ((= syntax ?.) (setq result ?.))
2318 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2320 (setq span-start (+ span-start 1)))
2321 (t (setq span-start span-end)
2322 (setq result ??)))))
2323 (setq span-start (+ span-start 1)))
2326 (defun gud-expr-compound (first second)
2327 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2328 The two exprs are represented as a cons cells, where the car
2329 specifies the point in the current buffer that marks the beginning of the
2330 expr and the cdr specifies the character after the end of the expr.
2331 Link exprs of the form:
2338 (let ((span-start (cdr first))
2339 (span-end (car second))
2341 (setq syntax (gud-expr-compound-sep span-start span-end))
2343 ((= (car first) (car second)) nil)
2344 ((= (cdr first) (cdr second)) nil)
2347 (setq span-start (char-after (- span-start 1)))
2348 (setq span-end (char-after span-end))
2350 ((= span-start ?)) t)
2351 ((= span-start ?]) t)
2359 ;;; gud.el ends here