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 and other debuggers under Emacs.
50 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), and jdb."
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 (defun gud-find-file (file)
72 ;; Don't get confused by double slashes in the name that comes from GDB.
73 (while (string-match "//+" file
)
74 (setq file
(replace-match "/" t t file
)))
75 (funcall gud-find-file file
))
77 ;; Keymap definitions for menu bar entries common to all debuggers and
78 ;; slots for debugger-dependent ones in sensible places. (Defined here
80 (defvar gud-menu-map
(make-sparse-keymap "Gud") nil
)
81 (define-key gud-menu-map
[refresh] '("Refresh" . gud-refresh))
82 (define-key gud-menu-map [remove] '("Remove Breakpoint" . gud-remove))
83 (define-key gud-menu-map [tbreak] nil) ; gdb, sdb and xdb
84 (define-key gud-menu-map [break] '("Set Breakpoint" . gud-break))
85 (define-key gud-menu-map [up] nil) ; gdb, dbx, and xdb
86 (define-key gud-menu-map [down] nil) ; gdb, dbx, and xdb
87 (define-key gud-menu-map [print] '("Print Expression" . gud-print))
88 (define-key gud-menu-map [finish] nil) ; gdb or xdb
89 (define-key gud-menu-map [stepi] '("Step Instruction" . gud-stepi))
90 (define-key gud-menu-map [step] '("Step Line" . gud-step))
91 (define-key gud-menu-map [next] '("Next Line" . gud-next))
92 (define-key gud-menu-map [cont] '("Continue" . gud-cont))
94 ;; ======================================================================
97 ;; This macro is used below to define some basic debugger interface commands.
98 ;; Of course you may use `gud-def' with any other debugger command, including
101 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
102 ;; which defines FUNC to send the command NAME to the debugger, gives
103 ;; it the docstring DOC, and binds that function to KEY in the GUD
104 ;; major mode. The function is also bound in the global keymap with the
107 (defmacro gud-def (func cmd key &optional doc)
108 "Define FUNC to be a command sending STR and bound to KEY, with
109 optional doc string DOC. Certain %-escapes in the string arguments
110 are interpreted specially if present. These are:
112 %f name (without directory) of current source file.
113 %F name (without directory or extension) of current source file.
114 %d directory of current source file.
115 %l number of current source line
116 %e text of the C lvalue or function-call expression surrounding point.
117 %a text of the hexadecimal address surrounding point
118 %p prefix argument to the command (if any) as a number
120 The `current' source file is the file of the current buffer (if
121 we're in a C file) or the source file current at the last break or
122 step (if we're in the GUD buffer).
123 The `current' line is that of the current buffer (if we're in a
124 source file) or the source line number at the last break or step (if
125 we're in the GUD buffer)."
127 (list 'defun func '(arg)
130 (list 'gud-call cmd 'arg))
137 (list 'global-set-key
138 (list 'concat 'gud-key-prefix key)
139 (list 'quote func)))))
141 ;; Where gud-display-frame should put the debugging arrow; a cons of
142 ;; (filename . line-number). This is set by the marker-filter, which scans
143 ;; the debugger's output for indications of the current program counter.
144 (defvar gud-last-frame nil)
146 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
147 ;; the last frame, even if it's been called before and gud-last-frame has
149 (defvar gud-last-last-frame nil)
151 ;; All debugger-specific information is collected here.
152 ;; Here's how it works, in case you ever need to add a debugger to the mode.
154 ;; Each entry must define the following at startup:
157 ;; comint-prompt-regexp
158 ;; gud-<name>-massage-args
159 ;; gud-<name>-marker-filter
160 ;; gud-<name>-find-file
162 ;; The job of the massage-args method is to modify the given list of
163 ;; debugger arguments before running the debugger.
165 ;; The job of the marker-filter method is to detect file/line markers in
166 ;; strings and set the global gud-last-frame to indicate what display
167 ;; action (if any) should be triggered by the marker. Note that only
168 ;; whatever the method *returns* is displayed in the buffer; thus, you
169 ;; can filter the debugger's output, interpreting some and passing on
172 ;; The job of the find-file method is to visit and return the buffer indicated
173 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
174 ;; something else. It would be good if it also copied the Gud menubar entry.
176 ;; ======================================================================
177 ;; speedbar support functions and variables.
178 (eval-when-compile (require 'speedbar))
180 (defvar gud-last-speedbar-buffer nil
181 "The last GUD buffer used.")
183 (defvar gud-last-speedbar-stackframe nil
184 "Description of the currently displayed GUD stack.
185 t means that there is no stack, and we are in display-file mode.")
187 (defvar gud-speedbar-key-map nil
188 "Keymap used when in the buffers display mode.")
190 (defun gud-install-speedbar-variables ()
191 "Install those variables used by speedbar to enhance gud/gdb."
192 (if gud-speedbar-key-map
194 (setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
196 (define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
197 (define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
198 (define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)))
200 (defvar gud-speedbar-menu-items
201 ;; Note to self. Add expand, and turn off items when not available.
202 '(["Jump to stack frame" speedbar-edit-line t])
203 "Additional menu items to add the the speedbar frame.")
205 ;; Make sure our special speedbar mode is loaded
206 (if (featurep 'speedbar)
207 (gud-install-speedbar-variables)
208 (add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
210 (defun gud-speedbar-buttons (buffer)
211 "Create a speedbar display based on the current state of GUD.
212 If the GUD BUFFER is not running a supported debugger, then turn
213 off the specialized speedbar mode."
214 (if (and (save-excursion (goto-char (point-min))
215 (looking-at "Current Stack"))
216 (equal gud-last-last-frame gud-last-speedbar-stackframe))
218 (setq gud-last-speedbar-buffer buffer)
219 (let* ((ff (save-excursion (set-buffer buffer) gud-find-file))
220 ;;(lf (save-excursion (set-buffer buffer) gud-last-last-frame))
222 (cond ((eq ff 'gud-gdb-find-file)
223 (gud-gdb-get-stackframe buffer)
225 ;; Add more debuggers here!
227 (speedbar-remove-localized-speedbar-support buffer)
231 (insert "No Stack frames\n")
232 (insert "Current Stack:\n"))
234 (insert (nth 1 (car frames)) ":\n")
235 (if (= (length (car frames)) 2)
237 ; (speedbar-insert-button "[?]"
238 ; 'speedbar-button-face
240 (speedbar-insert-button (car (car frames))
241 'speedbar-directory-face
243 ; (speedbar-insert-button "[+]"
244 ; 'speedbar-button-face
245 ; 'speedbar-highlight-face
246 ; 'gud-gdb-get-scope-data
248 (speedbar-insert-button (car (car frames))
250 'speedbar-highlight-face
251 (cond ((eq ff 'gud-gdb-find-file)
252 'gud-gdb-goto-stackframe)
253 (t (error "Should never be here.")))
255 (setq frames (cdr frames)))
256 ; (let ((selected-frame
257 ; (cond ((eq ff 'gud-gdb-find-file)
258 ; (gud-gdb-selected-frame-info buffer))
259 ; (t (error "Should never be here."))))))
261 (setq gud-last-speedbar-stackframe gud-last-last-frame)))
264 ;; ======================================================================
267 ;;; History of argument lists passed to gdb.
268 (defvar gud-gdb-history nil)
270 (defun gud-gdb-massage-args (file args)
271 (cons "-fullname" args))
273 (defvar gud-gdb-marker-regexp
274 ;; This used to use path-separator instead of ":";
275 ;; however, we found that on both Windows 32 and MSDOS
276 ;; a colon is correct here.
277 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
278 "\\([0-9]*\\)" ":" ".*\n"))
280 ;; There's no guarantee that Emacs will hand the filter the entire
281 ;; marker at once; it could be broken up across several strings. We
282 ;; might even receive a big chunk with several markers in it. If we
283 ;; receive a chunk of text which looks like it might contain the
284 ;; beginning of a marker, we save it here between calls to the
286 (defvar gud-marker-acc "")
287 (make-variable-buffer-local 'gud-marker-acc)
289 (defun gud-gdb-marker-filter (string)
290 (setq gud-marker-acc (concat gud-marker-acc string))
293 ;; Process all the complete markers in this chunk.
294 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
297 ;; Extract the frame position from the marker.
299 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
300 (string-to-int (substring gud-marker-acc
304 ;; Append any text before the marker to the output we're going
305 ;; to return - we don't include the marker in this text.
306 output (concat output
307 (substring gud-marker-acc 0 (match-beginning 0)))
309 ;; Set the accumulator to the remaining text.
310 gud-marker-acc (substring gud-marker-acc (match-end 0))))
312 ;; Does the remaining text look like it might end with the
313 ;; beginning of another marker? If it does, then keep it in
314 ;; gud-marker-acc until we receive the rest of it. Since we
315 ;; know the full marker regexp above failed, it's pretty simple to
316 ;; test for marker starts.
317 (if (string-match "\032.*\\'" gud-marker-acc)
319 ;; Everything before the potential marker start can be output.
320 (setq output (concat output (substring gud-marker-acc
321 0 (match-beginning 0))))
323 ;; Everything after, we save, to combine with later input.
325 (substring gud-marker-acc (match-beginning 0))))
327 (setq output (concat output gud-marker-acc)
332 (defun gud-gdb-find-file (f)
334 (let ((buf (find-file-noselect f)))
336 (gud-make-debug-menu)
337 (local-set-key [menu-bar debug tbreak]
338 '("Temporary Breakpoint" . gud-tbreak))
339 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
340 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
341 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
344 (defvar gdb-minibuffer-local-map nil
345 "Keymap for minibuffer prompting of gdb startup command.")
346 (if gdb-minibuffer-local-map
348 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
350 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
353 (defun gdb (command-line)
354 "Run gdb on program FILE in buffer *gud-FILE*.
355 The directory containing FILE becomes the initial working directory
356 and source-file directory for your debugger."
358 (list (read-from-minibuffer "Run gdb (like this): "
359 (if (consp gud-gdb-history)
360 (car gud-gdb-history)
362 gdb-minibuffer-local-map nil
365 (gud-common-init command-line 'gud-gdb-massage-args
366 'gud-gdb-marker-filter 'gud-gdb-find-file)
368 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
369 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
370 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
371 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
372 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
373 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
374 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
375 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
376 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
377 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
378 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
380 (local-set-key "\C-i" 'gud-gdb-complete-command)
381 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
382 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
383 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
384 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
385 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
386 (setq paragraph-start comint-prompt-regexp)
387 (run-hooks 'gdb-mode-hook)
390 ;; One of the nice features of GDB is its impressive support for
391 ;; context-sensitive command completion. We preserve that feature
392 ;; in the GUD buffer by using a GDB command designed just for Emacs.
394 ;; The completion process filter indicates when it is finished.
395 (defvar gud-gdb-complete-in-progress)
397 ;; Since output may arrive in fragments we accumulate partials strings here.
398 (defvar gud-gdb-complete-string)
400 ;; We need to know how much of the completion to chop off.
401 (defvar gud-gdb-complete-break)
403 ;; The completion list is constructed by the process filter.
404 (defvar gud-gdb-complete-list)
406 (defvar gud-comint-buffer nil)
408 (defun gud-gdb-complete-command ()
409 "Perform completion on the GDB command preceding point.
410 This is implemented using the GDB `complete' command which isn't
411 available with older versions of GDB."
414 (command (save-excursion
416 (and (looking-at comint-prompt-regexp)
417 (goto-char (match-end 0)))
418 (buffer-substring (point) end)))
420 ;; Find the word break. This match will always succeed.
421 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
422 (setq gud-gdb-complete-break (match-beginning 2)
423 command-word (substring command gud-gdb-complete-break))
424 ;; Temporarily install our filter function.
425 (let ((gud-marker-filter 'gud-gdb-complete-filter))
426 ;; Issue the command to GDB.
427 (gud-basic-call (concat "complete " command))
428 (setq gud-gdb-complete-in-progress t
429 gud-gdb-complete-string nil
430 gud-gdb-complete-list nil)
432 (while gud-gdb-complete-in-progress
433 (accept-process-output (get-buffer-process gud-comint-buffer))))
434 ;; Protect against old versions of GDB.
435 (and gud-gdb-complete-list
436 (string-match "^Undefined command: \"complete\""
437 (car gud-gdb-complete-list))
438 (error "This version of GDB doesn't support the `complete' command."))
439 ;; Sort the list like readline.
440 (setq gud-gdb-complete-list
441 (sort gud-gdb-complete-list (function string-lessp)))
442 ;; Remove duplicates.
443 (let ((first gud-gdb-complete-list)
444 (second (cdr gud-gdb-complete-list)))
446 (if (string-equal (car first) (car second))
447 (setcdr first (setq second (cdr second)))
449 second (cdr second)))))
450 ;; Add a trailing single quote if there is a unique completion
451 ;; and it contains an odd number of unquoted single quotes.
452 (and (= (length gud-gdb-complete-list) 1)
453 (let ((str (car gud-gdb-complete-list))
456 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
457 (setq count (1+ count)
459 (and (= (mod count 2) 1)
460 (setq gud-gdb-complete-list (list (concat str "'"))))))
461 ;; Let comint handle the rest.
462 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
464 ;; The completion process filter is installed temporarily to slurp the
465 ;; output of GDB up to the next prompt and build the completion list.
466 (defun gud-gdb-complete-filter (string)
467 (setq string (concat gud-gdb-complete-string string))
468 (while (string-match "\n" string)
469 (setq gud-gdb-complete-list
470 (cons (substring string gud-gdb-complete-break (match-beginning 0))
471 gud-gdb-complete-list))
472 (setq string (substring string (match-end 0))))
473 (if (string-match comint-prompt-regexp string)
475 (setq gud-gdb-complete-in-progress nil)
478 (setq gud-gdb-complete-string string)
481 ;; gdb speedbar functions
483 (defun gud-gdb-goto-stackframe (text token indent)
484 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
485 (speedbar-with-attached-buffer
486 (gud-basic-call (concat "frame " (nth 1 token)))
489 (defvar gud-gdb-fetched-stack-frame nil
490 "Stack frames we are fetching from GDB.")
492 (defvar gud-gdb-fetched-stack-frame-list nil
493 "List of stack frames we are fetching from GDB.")
495 ;(defun gud-gdb-get-scope-data (text token indent)
496 ; ;; checkdoc-params: (indent)
497 ; "Fetch data associated with a stack frame, and expand/contract it.
498 ;Data to do this is retrieved from TEXT and TOKEN."
499 ; (let ((args nil) (scope nil))
500 ; (gud-gdb-run-command-fetch-lines "info args")
502 ; (gud-gdb-run-command-fetch-lines "info local")
506 (defun gud-gdb-get-stackframe (buffer)
507 "Extract the current stack frame out of the GUD GDB BUFFER."
509 (gud-gdb-fetched-stack-frame-list nil))
510 (gud-gdb-run-command-fetch-lines "backtrace" buffer)
511 (if (and (car gud-gdb-fetched-stack-frame-list)
512 (string-match "No stack" (car gud-gdb-fetched-stack-frame-list)))
513 ;; Go into some other mode???
515 (while gud-gdb-fetched-stack-frame-list
516 (let ((e (car gud-gdb-fetched-stack-frame-list))
517 (name nil) (num nil))
519 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
520 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
521 (if (not (string-match
522 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
525 (list (nth 0 (car newlst))
528 (match-string 2 e))))
529 (setq num (match-string 1 e)
530 name (match-string 2 e))
534 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
535 (list name num (match-string 1 e)
539 (setq gud-gdb-fetched-stack-frame-list
540 (cdr gud-gdb-fetched-stack-frame-list)))
543 ;(defun gud-gdb-selected-frame-info (buffer)
544 ; "Learn GDB information for the currently selected stack frame in BUFFER."
547 (defun gud-gdb-run-command-fetch-lines (command buffer)
548 "Run COMMAND, and return when `gud-gdb-fetched-stack-frame-list' is full.
549 BUFFER is the GUD buffer in which to run the command."
553 (goto-char (point-max))
555 (not (looking-at comint-prompt-regexp)))
557 ;; Much of this copied from GDB complete, but I'm grabbing the stack
559 (let ((gud-marker-filter 'gud-gdb-speedbar-stack-filter))
560 ;; Issue the command to GDB.
561 (gud-basic-call command)
562 (setq gud-gdb-complete-in-progress t ;; use this flag for our purposes.
563 gud-gdb-complete-string nil
564 gud-gdb-complete-list nil)
566 (while gud-gdb-complete-in-progress
567 (accept-process-output (get-buffer-process gud-comint-buffer)))
568 (setq gud-gdb-fetched-stack-frame nil
569 gud-gdb-fetched-stack-frame-list
570 (nreverse gud-gdb-fetched-stack-frame-list))))))
572 (defun gud-gdb-speedbar-stack-filter (string)
573 ;; checkdoc-params: (string)
574 "Filter used to read in the current GDB stack."
575 (setq string (concat gud-gdb-fetched-stack-frame string))
576 (while (string-match "\n" string)
577 (setq gud-gdb-fetched-stack-frame-list
578 (cons (substring string 0 (match-beginning 0))
579 gud-gdb-fetched-stack-frame-list))
580 (setq string (substring string (match-end 0))))
581 (if (string-match comint-prompt-regexp string)
583 (setq gud-gdb-complete-in-progress nil)
586 (setq gud-gdb-complete-string string)
590 ;; ======================================================================
593 ;;; History of argument lists passed to sdb.
594 (defvar gud-sdb-history nil)
596 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
597 "If nil, we're on a System V Release 4 and don't need the tags hack.")
599 (defvar gud-sdb-lastfile nil)
601 (defun gud-sdb-massage-args (file args) args)
603 (defun gud-sdb-marker-filter (string)
605 (if gud-marker-acc (concat gud-marker-acc string) string))
607 ;; Process all complete markers in this chunk
610 ;; System V Release 3.2 uses this format
611 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
612 gud-marker-acc start)
615 (substring gud-marker-acc (match-beginning 3) (match-end 3))
617 (substring gud-marker-acc (match-beginning 4) (match-end 4))))))
618 ;; System V Release 4.0 quite often clumps two lines together
619 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
620 gud-marker-acc start)
621 (setq gud-sdb-lastfile
622 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
627 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
628 ;; System V Release 4.0
629 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
630 gud-marker-acc start)
631 (setq gud-sdb-lastfile
632 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
633 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
634 gud-marker-acc start))
639 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
641 (setq gud-sdb-lastfile nil)))
642 (setq start (match-end 0)))
644 ;; Search for the last incomplete line in this chunk
645 (while (string-match "\n" gud-marker-acc start)
646 (setq start (match-end 0)))
648 ;; If we have an incomplete line, store it in gud-marker-acc.
649 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
652 (defun gud-sdb-find-file (f)
654 (let ((buf (if gud-sdb-needs-tags
655 (find-tag-noselect f)
656 (find-file-noselect f))))
658 (gud-make-debug-menu)
659 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
663 (defun sdb (command-line)
664 "Run sdb on program FILE in buffer *gud-FILE*.
665 The directory containing FILE becomes the initial working directory
666 and source-file directory for your debugger."
668 (list (read-from-minibuffer "Run sdb (like this): "
669 (if (consp gud-sdb-history)
670 (car gud-sdb-history)
674 (if (and gud-sdb-needs-tags
675 (not (and (boundp 'tags-file-name)
676 (stringp tags-file-name)
677 (file-exists-p tags-file-name))))
678 (error "The sdb support requires a valid tags table to work."))
680 (gud-common-init command-line 'gud-sdb-massage-args
681 'gud-sdb-marker-filter 'gud-sdb-find-file)
683 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
684 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
685 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
686 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
687 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
688 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
689 (gud-def gud-cont "c" "\C-r" "Continue with display.")
690 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
692 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
693 (setq paragraph-start comint-prompt-regexp)
694 (local-set-key [menu-bar debug tbreak]
695 '("Temporary Breakpoint" . gud-tbreak))
696 (run-hooks 'sdb-mode-hook)
699 ;; ======================================================================
702 ;;; History of argument lists passed to dbx.
703 (defvar gud-dbx-history nil)
705 (defcustom gud-dbx-directories nil
706 "*A list of directories that dbx should search for source code.
707 If nil, only source files in the program directory
708 will be known to dbx.
710 The file names should be absolute, or relative to the directory
711 containing the executable being debugged."
712 :type '(choice (const :tag "Current Directory" nil)
717 (defun gud-dbx-massage-args (file args)
718 (nconc (let ((directories gud-dbx-directories)
721 (setq result (cons (car directories) (cons "-I" result)))
722 (setq directories (cdr directories)))
726 (defun gud-dbx-file-name (f)
727 "Transform a relative file name to an absolute file name, for dbx."
729 (if (file-exists-p f)
730 (setq result (expand-file-name f))
731 (let ((directories gud-dbx-directories))
733 (let ((path (concat (car directories) "/" f)))
734 (if (file-exists-p path)
735 (setq result (expand-file-name path)
737 (setq directories (cdr directories)))))
740 (defun gud-dbx-marker-filter (string)
741 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
744 ;; Process all complete markers in this chunk.
745 (while (or (string-match
746 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
747 gud-marker-acc start)
749 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
750 gud-marker-acc start))
753 (substring gud-marker-acc (match-beginning 2) (match-end 2))
755 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
756 start (match-end 0)))
758 ;; Search for the last incomplete line in this chunk
759 (while (string-match "\n" gud-marker-acc start)
760 (setq start (match-end 0)))
762 ;; If the incomplete line APPEARS to begin with another marker, keep it
763 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
764 ;; unnecessary concat during the next call.
766 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
767 (substring gud-marker-acc (match-beginning 0))
771 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
772 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
774 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
775 ;; We haven't tested gud on this system:
776 (string-match "^mips-[^-]*-riscos" system-configuration)
777 ;; It's documented on OSF/1.3
778 (string-match "^mips-[^-]*-osf1" system-configuration)
779 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
780 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
782 (defun gud-mipsdbx-massage-args (file args)
783 (cons "-emacs" args))
785 ;; This is just like the gdb one except for the regexps since we need to cope
786 ;; with an optional breakpoint number in [] before the ^Z^Z
787 (defun gud-mipsdbx-marker-filter (string)
788 (setq gud-marker-acc (concat gud-marker-acc string))
791 ;; Process all the complete markers in this chunk.
793 ;; This is like th gdb marker but with an optional
794 ;; leading break point number like `[1] '
795 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
799 ;; Extract the frame position from the marker.
801 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
802 (string-to-int (substring gud-marker-acc
806 ;; Append any text before the marker to the output we're going
807 ;; to return - we don't include the marker in this text.
808 output (concat output
809 (substring gud-marker-acc 0 (match-beginning 0)))
811 ;; Set the accumulator to the remaining text.
812 gud-marker-acc (substring gud-marker-acc (match-end 0))))
814 ;; Does the remaining text look like it might end with the
815 ;; beginning of another marker? If it does, then keep it in
816 ;; gud-marker-acc until we receive the rest of it. Since we
817 ;; know the full marker regexp above failed, it's pretty simple to
818 ;; test for marker starts.
819 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
821 ;; Everything before the potential marker start can be output.
822 (setq output (concat output (substring gud-marker-acc
823 0 (match-beginning 0))))
825 ;; Everything after, we save, to combine with later input.
827 (substring gud-marker-acc (match-beginning 0))))
829 (setq output (concat output gud-marker-acc)
834 ;; The dbx in IRIX is a pain. It doesn't print the file name when
835 ;; stopping at a breakpoint (but you do get it from the `up' and
836 ;; `down' commands...). The only way to extract the information seems
837 ;; to be with a `file' command, although the current line number is
838 ;; available in $curline. Thus we have to look for output which
839 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
840 ;; to output the information we want with a combination of the
841 ;; `printf' and `file' commands as a pseudo marker which we can
842 ;; recognise next time through the marker-filter. This would be like
843 ;; the gdb marker but you can't get the file name without a newline...
844 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
845 ;; number rather than a line number etc. Maybe this could be made to
846 ;; work by listing all the breakpoints and picking the one(s) with the
847 ;; correct line number, but life's too short.
848 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
851 (and (string-match "^mips-[^-]*-irix" system-configuration)
852 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
853 "Non-nil to assume the interface appropriate for IRIX dbx.
854 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
855 a better solution in 6.1 upwards.")
856 (defvar gud-dbx-use-stopformat-p
857 (string-match "irix[6-9]\\.[1-9]" system-configuration)
858 "Non-nil to use the dbx feature present at least from Irix 6.1
859 whereby $stopformat=1 produces an output format compatiable with
860 `gud-dbx-marker-filter'.")
861 ;; [Irix dbx seems to be a moving target. The dbx output changed
862 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
863 ;; the output from `up' is no longer spotted by gud (and it's probably
864 ;; not distinctive enough to try to match it -- use C-<, C->
865 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
866 ;; `long long'(why?!), so the printf stuff needed changing. The line
867 ;; number was cast to `long' as a compromise between the new `long
868 ;; long' and the original `int'. This is reported not to work in 6.2,
869 ;; so it's changed back to int -- don't make your sources too long.
870 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
871 ;; whereby `set $stopformat=1' reportedly produces output compatible
872 ;; with `gud-dbx-marker-filter', which we prefer.
874 ;; The process filter is also somewhat
875 ;; unreliable, sometimes not spotting the markers; I don't know
876 ;; whether there's anything that can be done about that. It would be
877 ;; much better if SGI could be persuaded to (re?)instate the MIPS
878 ;; -emacs flag for gdb-like output (which ought to be possible as most
879 ;; of the communication I've had over it has been from sgi.com).]
881 ;; this filter is influenced by the xdb one rather than the gdb one
882 (defun gud-irixdbx-marker-filter (string)
883 (let (result (case-fold-search nil))
884 (if (or (string-match comint-prompt-regexp string)
885 (string-match ".*\012" string))
886 (setq result (concat gud-marker-acc string)
888 (setq gud-marker-acc (concat gud-marker-acc string)))
891 ;; look for breakpoint or signal indication e.g.:
892 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
893 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
894 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
896 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
898 ;; prod dbx into printing out the line number and file
899 ;; name in a form we can grok as below
900 (process-send-string (get-buffer-process gud-comint-buffer)
901 "printf \"\032\032%1d:\",(int)$curline;file\n"))
902 ;; look for result of, say, "up" e.g.:
903 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
904 ;; (this will also catch one of the lines printed by "where")
906 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
908 (let ((file (substring result (match-beginning 1)
910 (if (file-exists-p file)
914 result (match-beginning 1) (match-end 1))
917 result (match-beginning 2) (match-end 2)))))))
919 ((string-match ; kluged-up marker as above
920 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
921 (let ((file (gud-dbx-file-name
922 (substring result (match-beginning 2) (match-end 2)))))
923 (if (and file (file-exists-p file))
929 result (match-beginning 1) (match-end 1)))))))
930 (setq result (substring result 0 (match-beginning 0))))))
933 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
934 "Non-nil means to assume the interface approriate for DG/UX dbx.
935 This was tested using R4.11.")
937 ;; There are a couple of differences between DG's dbx output and normal
938 ;; dbx output which make it nontrivial to integrate this into the
939 ;; standard dbx-marker-filter (mainly, there are a different number of
940 ;; backreferences). The markers look like:
942 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
944 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
947 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
951 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
953 ;; from up/down/where.
955 (defun gud-dguxdbx-marker-filter (string)
956 (setq gud-marker-acc (if gud-marker-acc
957 (concat gud-marker-acc string)
959 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
960 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
962 ;; Process all complete markers in this chunk.
963 (while (string-match re gud-marker-acc start)
966 (substring gud-marker-acc (match-beginning 4) (match-end 4))
967 (string-to-int (substring gud-marker-acc
968 (match-beginning 3) (match-end 3))))
969 start (match-end 0)))
971 ;; Search for the last incomplete line in this chunk
972 (while (string-match "\n" gud-marker-acc start)
973 (setq start (match-end 0)))
975 ;; If the incomplete line APPEARS to begin with another marker, keep it
976 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
977 ;; unnecessary concat during the next call.
979 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
980 (substring gud-marker-acc (match-beginning 0))
984 (defun gud-dbx-find-file (f)
986 (let ((realf (gud-dbx-file-name f)))
988 (let ((buf (find-file-noselect realf)))
990 (gud-make-debug-menu)
991 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
992 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
997 (defun dbx (command-line)
998 "Run dbx on program FILE in buffer *gud-FILE*.
999 The directory containing FILE becomes the initial working directory
1000 and source-file directory for your debugger."
1002 (list (read-from-minibuffer "Run dbx (like this): "
1003 (if (consp gud-dbx-history)
1004 (car gud-dbx-history)
1011 (gud-common-init command-line 'gud-mipsdbx-massage-args
1012 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
1014 (gud-common-init command-line 'gud-dbx-massage-args
1015 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
1017 (gud-common-init command-line 'gud-dbx-massage-args
1018 'gud-dguxdbx-marker-filter 'gud-dbx-find-file))
1020 (gud-common-init command-line 'gud-dbx-massage-args
1021 'gud-dbx-marker-filter 'gud-dbx-find-file)))
1025 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1026 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1027 (gud-def gud-break "stop at \"%f\":%l"
1028 "\C-b" "Set breakpoint at current line.")
1029 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1031 (gud-def gud-break "stop at \"%d%f\":%l"
1032 "\C-b" "Set breakpoint at current line.")
1033 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1034 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1035 "<" "Up (numeric arg) stack frames.")
1036 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1037 ">" "Down (numeric arg) stack frames.")
1038 ;; Make dbx give out the source location info that we need.
1039 (process-send-string (get-buffer-process gud-comint-buffer)
1040 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1041 (gud-dbx-use-stopformat-p
1042 (process-send-string (get-buffer-process gud-comint-buffer)
1043 "set $stopformat=1\n"))
1045 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1046 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1047 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1048 "\C-b" "Set breakpoint at current line.")))
1050 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1051 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1052 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1053 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1054 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1055 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1057 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1058 (setq paragraph-start comint-prompt-regexp)
1059 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1060 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1061 (run-hooks 'dbx-mode-hook)
1064 ;; ======================================================================
1065 ;; xdb (HP PARISC debugger) functions
1067 ;;; History of argument lists passed to xdb.
1068 (defvar gud-xdb-history nil)
1070 (defcustom gud-xdb-directories nil
1071 "*A list of directories that xdb should search for source code.
1072 If nil, only source files in the program directory
1073 will be known to xdb.
1075 The file names should be absolute, or relative to the directory
1076 containing the executable being debugged."
1077 :type '(choice (const :tag "Current Directory" nil)
1082 (defun gud-xdb-massage-args (file args)
1083 (nconc (let ((directories gud-xdb-directories)
1086 (setq result (cons (car directories) (cons "-d" result)))
1087 (setq directories (cdr directories)))
1091 (defun gud-xdb-file-name (f)
1092 "Transform a relative pathname to a full pathname in xdb mode"
1094 (if (file-exists-p f)
1095 (setq result (expand-file-name f))
1096 (let ((directories gud-xdb-directories))
1098 (let ((path (concat (car directories) "/" f)))
1099 (if (file-exists-p path)
1100 (setq result (expand-file-name path)
1102 (setq directories (cdr directories)))))
1105 ;; xdb does not print the lines all at once, so we have to accumulate them
1106 (defun gud-xdb-marker-filter (string)
1108 (if (or (string-match comint-prompt-regexp string)
1109 (string-match ".*\012" string))
1110 (setq result (concat gud-marker-acc string)
1112 (setq gud-marker-acc (concat gud-marker-acc string)))
1114 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1116 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1118 (let ((line (string-to-int
1119 (substring result (match-beginning 2) (match-end 2))))
1120 (file (gud-xdb-file-name
1121 (substring result (match-beginning 1) (match-end 1)))))
1123 (setq gud-last-frame (cons file line))))))
1126 (defun gud-xdb-find-file (f)
1128 (let ((realf (gud-xdb-file-name f)))
1130 (let ((buf (find-file-noselect realf)))
1132 (gud-make-debug-menu)
1133 (local-set-key [menu-bar debug tbreak]
1134 '("Temporary Breakpoint" . gud-tbreak))
1135 (local-set-key [menu-bar debug finish]
1136 '("Finish Function" . gud-finish))
1137 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1138 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1143 (defun xdb (command-line)
1144 "Run xdb on program FILE in buffer *gud-FILE*.
1145 The directory containing FILE becomes the initial working directory
1146 and source-file directory for your debugger.
1148 You can set the variable 'gud-xdb-directories' to a list of program source
1149 directories if your program contains sources from more than one directory."
1151 (list (read-from-minibuffer "Run xdb (like this): "
1152 (if (consp gud-xdb-history)
1153 (car gud-xdb-history)
1158 (gud-common-init command-line 'gud-xdb-massage-args
1159 'gud-xdb-marker-filter 'gud-xdb-find-file)
1161 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1162 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1163 "Set temporary breakpoint at current line.")
1164 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1165 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1166 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1167 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1168 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1169 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1170 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1171 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1173 (setq comint-prompt-regexp "^>")
1174 (setq paragraph-start comint-prompt-regexp)
1175 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
1176 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1177 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1178 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1179 (run-hooks 'xdb-mode-hook))
1181 ;; ======================================================================
1184 ;;; History of argument lists passed to perldb.
1185 (defvar gud-perldb-history nil)
1187 ;; Convert a command line as would be typed normally to run a script
1188 ;; into one that invokes an Emacs-enabled debugging session.
1189 ;; "-d" in inserted as the first switch, and "-emacs" is inserted where
1190 ;; it will be $ARGV[0] (see perl5db.pl).
1191 (defun gud-perldb-massage-args (file args)
1192 (let* ((new-args '("-d"))
1195 (setq new-args (cons (car args) new-args))
1196 (setq args (cdr args)))))
1198 ;; Pass all switches and -e scripts through.
1200 (string-match "^-" (car args))
1201 (not (equal "-" (car args)))
1202 (not (equal "--" (car args))))
1203 (when (equal "-e" (car args))
1204 ;; -e goes with the next arg, so shift one extra.
1206 ;; -e as the last arg is an error in Perl.
1207 (error "No code specified for -e."))
1213 (string-match "^-" (car args)))
1214 (error "Can't use stdin as the script to debug."))
1215 ;; This is the program name.
1218 ;; If -e specified, make sure there is a -- so -emacs is not taken
1220 (if (and args (equal "--" (car args)))
1222 (and seen-e (setq new-args (cons "--" new-args))))
1224 (setq new-args (cons "-emacs" new-args))
1228 (nreverse new-args)))
1230 ;; There's no guarantee that Emacs will hand the filter the entire
1231 ;; marker at once; it could be broken up across several strings. We
1232 ;; might even receive a big chunk with several markers in it. If we
1233 ;; receive a chunk of text which looks like it might contain the
1234 ;; beginning of a marker, we save it here between calls to the
1236 (defun gud-perldb-marker-filter (string)
1237 (setq gud-marker-acc (concat gud-marker-acc string))
1240 ;; Process all the complete markers in this chunk.
1241 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1245 ;; Extract the frame position from the marker.
1247 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1248 (string-to-int (substring gud-marker-acc
1252 ;; Append any text before the marker to the output we're going
1253 ;; to return - we don't include the marker in this text.
1254 output (concat output
1255 (substring gud-marker-acc 0 (match-beginning 0)))
1257 ;; Set the accumulator to the remaining text.
1258 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1260 ;; Does the remaining text look like it might end with the
1261 ;; beginning of another marker? If it does, then keep it in
1262 ;; gud-marker-acc until we receive the rest of it. Since we
1263 ;; know the full marker regexp above failed, it's pretty simple to
1264 ;; test for marker starts.
1265 (if (string-match "\032.*\\'" gud-marker-acc)
1267 ;; Everything before the potential marker start can be output.
1268 (setq output (concat output (substring gud-marker-acc
1269 0 (match-beginning 0))))
1271 ;; Everything after, we save, to combine with later input.
1272 (setq gud-marker-acc
1273 (substring gud-marker-acc (match-beginning 0))))
1275 (setq output (concat output gud-marker-acc)
1280 (defun gud-perldb-find-file (f)
1282 (let ((buf (find-file-noselect f)))
1284 (gud-make-debug-menu)
1287 (defcustom gud-perldb-command-name "perl"
1288 "File name for executing Perl."
1293 (defun perldb (command-line)
1294 "Run perldb on program FILE in buffer *gud-FILE*.
1295 The directory containing FILE becomes the initial working directory
1296 and source-file directory for your debugger."
1298 (list (read-from-minibuffer "Run perldb (like this): "
1299 (if (consp gud-perldb-history)
1300 (car gud-perldb-history)
1301 (concat gud-perldb-command-name
1303 (or (buffer-file-name)
1307 'gud-perldb-history)))
1309 (gud-common-init command-line 'gud-perldb-massage-args
1310 'gud-perldb-marker-filter 'gud-perldb-find-file)
1312 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1313 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1314 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1315 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1316 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1317 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1318 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1319 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1320 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1322 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1323 (setq paragraph-start comint-prompt-regexp)
1324 (run-hooks 'perldb-mode-hook)
1327 ;; ======================================================================
1328 ;; pdb (Python debugger) functions
1330 ;;; History of argument lists passed to pdb.
1331 (defvar gud-pdb-history nil)
1333 (defun gud-pdb-massage-args (file args)
1336 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1337 ;; Either file or function name may be omitted: "> <string>(0)?()"
1338 (defvar gud-pdb-marker-regexp
1339 "^> \\([-a-zA-Z0-9_/.]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1340 (defvar gud-pdb-marker-regexp-file-group 1)
1341 (defvar gud-pdb-marker-regexp-line-group 2)
1342 (defvar gud-pdb-marker-regexp-fnname-group 3)
1344 (defvar gud-pdb-marker-regexp-start "^> ")
1346 ;; There's no guarantee that Emacs will hand the filter the entire
1347 ;; marker at once; it could be broken up across several strings. We
1348 ;; might even receive a big chunk with several markers in it. If we
1349 ;; receive a chunk of text which looks like it might contain the
1350 ;; beginning of a marker, we save it here between calls to the
1352 (defun gud-pdb-marker-filter (string)
1353 (setq gud-marker-acc (concat gud-marker-acc string))
1356 ;; Process all the complete markers in this chunk.
1357 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1360 ;; Extract the frame position from the marker.
1362 (let ((file (match-string gud-pdb-marker-regexp-file-group
1364 (line (string-to-int
1365 (match-string gud-pdb-marker-regexp-line-group
1367 (if (string-equal file "<string>")
1371 ;; Output everything instead of the below
1372 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1373 ;; ;; Append any text before the marker to the output we're going
1374 ;; ;; to return - we don't include the marker in this text.
1375 ;; output (concat output
1376 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1378 ;; Set the accumulator to the remaining text.
1379 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1381 ;; Does the remaining text look like it might end with the
1382 ;; beginning of another marker? If it does, then keep it in
1383 ;; gud-marker-acc until we receive the rest of it. Since we
1384 ;; know the full marker regexp above failed, it's pretty simple to
1385 ;; test for marker starts.
1386 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1388 ;; Everything before the potential marker start can be output.
1389 (setq output (concat output (substring gud-marker-acc
1390 0 (match-beginning 0))))
1392 ;; Everything after, we save, to combine with later input.
1393 (setq gud-marker-acc
1394 (substring gud-marker-acc (match-beginning 0))))
1396 (setq output (concat output gud-marker-acc)
1401 (defun gud-pdb-find-file (f)
1403 (let ((buf (find-file-noselect f)))
1405 (gud-make-debug-menu)
1406 ;; (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1407 ;; (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1408 ;; (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1411 (defvar pdb-minibuffer-local-map nil
1412 "Keymap for minibuffer prompting of pdb startup command.")
1413 (if pdb-minibuffer-local-map
1415 (setq pdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
1417 pdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
1419 (defcustom gud-pdb-command-name "pdb"
1420 "File name for executing the Python debugger.
1421 This should be an executable on your path, or an absolute file name."
1426 (defun pdb (command-line)
1427 "Run pdb on program FILE in buffer `*gud-FILE*'.
1428 The directory containing FILE becomes the initial working directory
1429 and source-file directory for your debugger."
1431 (list (read-from-minibuffer "Run pdb (like this): "
1432 (if (consp gud-pdb-history)
1433 (car gud-pdb-history)
1434 (concat gud-pdb-command-name " "))
1435 pdb-minibuffer-local-map nil
1438 (gud-common-init command-line 'gud-pdb-massage-args
1439 'gud-pdb-marker-filter 'gud-pdb-find-file)
1441 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1442 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1443 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1444 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1445 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1446 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1447 (gud-def gud-up "up" "<" "Up one stack frame.")
1448 (gud-def gud-down "down" ">" "Down one stack frame.")
1449 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1451 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1453 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1454 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1455 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1456 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1457 (setq comint-prompt-regexp "^(Pdb) *")
1458 (setq paragraph-start comint-prompt-regexp)
1459 (run-hooks 'pdb-mode-hook))
1461 ;; ======================================================================
1465 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1467 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1469 ;; INVOCATION NOTES:
1471 ;; You invoke jdb-mode with:
1475 ;; It responds with:
1477 ;; Run jdb (like this): jdb
1479 ;; type any jdb switches followed by the name of the class you'd like to debug.
1480 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1481 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1482 ;; See the known problems section below for restrictions when specifying jdb
1483 ;; command line switches (search forward for '-classpath').
1485 ;; You should see something like the following:
1487 ;; Current directory is ~/src/java/hello/
1488 ;; Initializing jdb...
1489 ;; 0xed2f6628:class(hello)
1492 ;; To set an initial breakpoint try:
1494 ;; > stop in hello.main
1495 ;; Breakpoint set in hello.main
1498 ;; To execute the program type:
1503 ;; Breakpoint hit: running ...
1504 ;; hello.main (hello:12)
1506 ;; Type M-n to step over the current line and M-s to step into it. That,
1507 ;; along with the JDB 'help' command should get you started. The 'quit'
1508 ;; JDB command will get out out of the debugger. There is some truly
1509 ;; pathetic JDB documentation available at:
1511 ;; http://java.sun.com/products/jdk/1.1/debugging/
1513 ;; KNOWN PROBLEMS AND FIXME's:
1515 ;; Not sure what happens with inner classes ... haven't tried them.
1517 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1519 ;; You must not put whitespace between "-classpath" and the path to
1520 ;; search for java classes even though it is required when invoking jdb
1521 ;; from the command line. See gud-jdb-massage-args for details.
1523 ;; If any of the source files in the directories listed in
1524 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1525 ;; every file ending in ".java" in these directories parses without error.
1527 ;; All the .java files in the directories in gud-jdb-directories are
1528 ;; syntactically analyzed each time gud jdb is invoked. It would be
1529 ;; nice to keep as much information as possible between runs. It would
1530 ;; be really nice to analyze the files only as neccessary (when the
1531 ;; source needs to be displayed.) I'm not sure to what extent the former
1532 ;; can be accomplished and I'm not sure the latter can be done at all
1533 ;; since I don't know of any general way to tell which .class files are
1534 ;; defined by which .java file without analyzing all the .java files.
1535 ;; If anyone knows why JavaSoft didn't put the source file names in
1536 ;; debuggable .class files please clue me in so I find something else
1537 ;; to be spiteful and bitter about.
1539 ;; ======================================================================
1540 ;; gud jdb variables and functions
1542 ;; History of argument lists passed to jdb.
1543 (defvar gud-jdb-history nil)
1545 ;; List of Java source file directories.
1546 (defvar gud-jdb-directories (list ".")
1547 "*A list of directories that gud jdb should search for source code.
1548 The file names should be absolute, or relative to the current directory.")
1550 ;; List of the java source files for this debugging session.
1551 (defvar gud-jdb-source-files nil)
1553 ;; Association list of fully qualified class names (package + class name) and
1554 ;; their source files.
1555 (defvar gud-jdb-class-source-alist nil)
1557 ;; This is used to hold a source file during analysis.
1558 (defvar gud-jdb-analysis-buffer nil)
1560 ;; Return a list of java source files. PATH gives the directories in
1561 ;; which to search for files with extension EXTN. Normally EXTN is
1562 ;; given as the regular expression "\\.java$" .
1563 (defun gud-jdb-build-source-files-list (path extn)
1564 (apply 'nconc (mapcar (lambda (d) (directory-files d t extn nil)) path)))
1566 ;; Move point past whitespace.
1567 (defun gud-jdb-skip-whitespace ()
1568 (skip-chars-forward " \n\r\t\014"))
1570 ;; Move point past a "// <eol>" type of comment.
1571 (defun gud-jdb-skip-single-line-comment ()
1574 ;; Move point past a "/* */" or "/** */" type of comment.
1575 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1579 (if (eq (following-char) ?*)
1583 (if (eq (following-char) ?/)
1586 (throw 'break nil)))))
1589 ;; Move point past any number of consecutive whitespace chars and/or comments.
1590 (defun gud-jdb-skip-whitespace-and-comments ()
1591 (gud-jdb-skip-whitespace)
1596 (gud-jdb-skip-single-line-comment)
1597 (gud-jdb-skip-whitespace))
1598 ((looking-at "/\\*")
1599 (gud-jdb-skip-traditional-or-documentation-comment)
1600 (gud-jdb-skip-whitespace))
1601 (t (throw 'done nil))))))
1603 ;; Move point past things that are id-like. The intent is to skip regular
1604 ;; id's, such as class or interface names as well as package and interface
1606 (defun gud-jdb-skip-id-ish-thing ()
1607 (skip-chars-forward "^ /\n\r\t\014,;{"))
1609 ;; Move point past a string literal.
1610 (defun gud-jdb-skip-string-literal ()
1613 ((eq (following-char) ?\\)
1615 ((eq (following-char) ?\042))))
1619 ;; Move point past a character literal.
1620 (defun gud-jdb-skip-character-literal ()
1624 (if (eq (following-char) ?\\)
1626 (not (eq (following-char) ?\')))
1630 ;; Move point past the following block. There may be (legal) cruft before
1631 ;; the block's opening brace. There must be a block or it's the end of life
1632 ;; in petticoat junction.
1633 (defun gud-jdb-skip-block ()
1635 ;; Find the begining of the block.
1637 (not (eq (following-char) ?{))
1639 ;; Skip any constructs that can harbor literal block delimiter
1640 ;; characters and/or the delimiters for the constructs themselves.
1643 (gud-jdb-skip-single-line-comment))
1644 ((looking-at "/\\*")
1645 (gud-jdb-skip-traditional-or-documentation-comment))
1646 ((eq (following-char) ?\042)
1647 (gud-jdb-skip-string-literal))
1648 ((eq (following-char) ?\')
1649 (gud-jdb-skip-character-literal))
1650 (t (forward-char))))
1652 ;; Now at the begining of the block.
1655 ;; Skip over the body of the block as well as the final brace.
1656 (let ((open-level 1))
1657 (while (not (eq open-level 0))
1660 (gud-jdb-skip-single-line-comment))
1661 ((looking-at "/\\*")
1662 (gud-jdb-skip-traditional-or-documentation-comment))
1663 ((eq (following-char) ?\042)
1664 (gud-jdb-skip-string-literal))
1665 ((eq (following-char) ?\')
1666 (gud-jdb-skip-character-literal))
1667 ((eq (following-char) ?{)
1668 (setq open-level (+ open-level 1))
1670 ((eq (following-char) ?})
1671 (setq open-level (- open-level 1))
1673 (t (forward-char))))))
1675 ;; Find the package and class definitions in Java source file FILE. Assumes
1676 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1677 ;; to hold the source during analysis.
1678 (defun gud-jdb-analyze-source (buf file)
1681 (insert-file-contents file nil nil nil t)
1686 (gud-jdb-skip-whitespace)
1690 ;; Any number of semi's following a block is legal. Move point
1691 ;; past them. Note that comments and whitespace may be
1692 ;; interspersed as well.
1693 ((eq (following-char) ?\073)
1696 ;; Move point past a single line comment.
1698 (gud-jdb-skip-single-line-comment))
1700 ;; Move point past a traditional or documentation comment.
1701 ((looking-at "/\\*")
1702 (gud-jdb-skip-traditional-or-documentation-comment))
1704 ;; Move point past a package statement, but save the PackageName.
1705 ((looking-at "package")
1707 (gud-jdb-skip-whitespace-and-comments)
1709 (gud-jdb-skip-id-ish-thing)
1710 (setq p (concat (buffer-substring s (point)) "."))
1711 (gud-jdb-skip-whitespace-and-comments)
1712 (if (eq (following-char) ?\073)
1715 ;; Move point past an import statement.
1716 ((looking-at "import")
1718 (gud-jdb-skip-whitespace-and-comments)
1719 (gud-jdb-skip-id-ish-thing)
1720 (gud-jdb-skip-whitespace-and-comments)
1721 (if (eq (following-char) ?\073)
1724 ;; Move point past the various kinds of ClassModifiers.
1725 ((looking-at "public")
1727 ((looking-at "abstract")
1729 ((looking-at "final")
1732 ;; Move point past a ClassDeclaraction, but save the class
1734 ((looking-at "class")
1736 (gud-jdb-skip-whitespace-and-comments)
1738 (gud-jdb-skip-id-ish-thing)
1740 l (nconc l (list (concat p (buffer-substring s (point)))))))
1741 (gud-jdb-skip-block))
1743 ;; Move point past an interface statement.
1744 ((looking-at "interface")
1746 (gud-jdb-skip-block))
1748 ;; Anything else means the input is invalid.
1750 (message (format "Error parsing file %s." file))
1751 (throw 'abort nil))))))
1754 (defun gud-jdb-build-class-source-alist-for-file (file)
1758 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1760 ;; Return an alist of fully qualified classes and the source files
1761 ;; holding their definitions. SOURCES holds a list of all the source
1762 ;; files to examine.
1763 (defun gud-jdb-build-class-source-alist (sources)
1764 (setq gud-jdb-analysis-buffer (get-buffer-create "*gud-jdb-scratch*"))
1769 'gud-jdb-build-class-source-alist-for-file
1771 (kill-buffer gud-jdb-analysis-buffer)
1772 (setq gud-jdb-analysis-buffer nil)))
1774 ;; Change what was given in the minibuffer to something that can be used to
1775 ;; invoke the debugger.
1776 (defun gud-jdb-massage-args (file args)
1777 ;; The jdb executable must have whitespace between "-classpath" and
1778 ;; its value while gud-common-init expects all switch values to
1779 ;; follow the switch keyword without intervening whitespace. We
1780 ;; require that when the user enters the "-classpath" switch in the
1781 ;; EMACS minibuffer that they do so without the intervening
1782 ;; whitespace. This function adds it back (it's called after
1783 ;; gud-common-init). There are more switches like this (for
1784 ;; instance "-host" and "-password") but I don't care about them
1787 (let (massaged-args user-error)
1791 (not (string-match "-classpath\\(.+\\)" (car args)))
1792 (not (setq user-error
1793 (string-match "-classpath$" (car args)))))
1794 (setq massaged-args (append massaged-args (list (car args))))
1795 (setq args (cdr args)))
1797 ;; By this point the current directory is all screwed up. Maybe we
1798 ;; could fix things and re-invoke gud-common-init, but for now I think
1799 ;; issueing the error is good enough.
1802 (kill-buffer (current-buffer))
1803 (error "Error: Omit whitespace between '-classpath' and its value")))
1813 (match-beginning 1) (match-end 1)))
1817 ;; Search for an association with P, a fully qualified class name, in
1818 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1819 ;; qualified file name of the source file which produced the class.
1820 (defun gud-jdb-find-source-file (p)
1821 (cdr (assoc p gud-jdb-class-source-alist)))
1823 ;; See comentary for other debugger's marker filters - there you will find
1824 ;; important notes about STRING.
1825 (defun gud-jdb-marker-filter (string)
1827 ;; Build up the accumulator.
1828 (setq gud-marker-acc
1830 (concat gud-marker-acc string)
1833 ;; We process STRING from left to right. Each time through the following
1834 ;; loop we process at most one marker. The start variable keeps track of
1835 ;; where we are in the input string through the iterations of this loop.
1836 (let (start file-found)
1838 ;; Process each complete marker in the input. There may be an incomplete
1839 ;; marker at the end of the input string. Incomplete markers are left
1840 ;; in the accumulator for processing the next time the function is called.
1843 ;; Do we see a marker?
1845 ;; jdb puts out a string of the following form when it
1846 ;; hits a breakpoint:
1848 ;; <fully-qualified-class><method> (<class>:<line-number>)
1850 ;; <fully-qualified-class>'s are composed of Java ID's
1851 ;; separated by periods. <method> and <class> are
1852 ;; also Java ID's. <method> begins with a period and
1853 ;; may contain less-than and greater-than (constructors,
1854 ;; for instance, are called <init> in the symbol table.)
1855 ;; Java ID's begin with a letter followed by letters
1856 ;; and/or digits. The set of letters includes underscore
1859 ;; The first group matches <fully-qualified-class>,
1860 ;; the second group matches <class> and the third group
1861 ;; matches <line-number>. We don't care about using
1862 ;; <method> so we don't "group" it.
1864 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1866 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1867 gud-marker-acc start)
1869 ;; Figure out the line on which to position the debugging arrow.
1870 ;; Return the info as a cons of the form:
1872 ;; (<file-name> . <line-number>) .
1875 (gud-jdb-find-source-file
1876 (substring gud-marker-acc
1879 (setq gud-last-frame
1883 (substring gud-marker-acc
1886 (message "Could not find source file."))
1888 ;; Set start after the last character of STRING that we've looked at
1889 ;; and loop to look for another marker.
1890 (setq start (match-end 0))))
1892 ;; We don't filter any debugger output so just return what we were given.
1895 (defun gud-jdb-find-file (f)
1896 (and (file-readable-p f)
1897 (find-file-noselect f)))
1899 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1902 (defun jdb (command-line)
1903 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1904 \"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1905 if there is. If the \"-classpath\" switch is given, omit all whitespace
1906 between it and it's value."
1908 (list (read-from-minibuffer "Run jdb (like this): "
1909 (if (consp gud-jdb-history)
1910 (car gud-jdb-history)
1911 (concat gud-jdb-command-name " "))
1915 (gud-common-init command-line 'gud-jdb-massage-args
1916 'gud-jdb-marker-filter 'gud-jdb-find-file)
1918 (gud-def gud-break "stop at %F:%l" "\C-b" "Set breakpoint at current line.")
1919 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1920 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1921 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1922 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1924 (setq comint-prompt-regexp "^> \\|^.+\\[[0-9]+\\] ")
1925 (setq paragraph-start comint-prompt-regexp)
1926 (run-hooks 'jdb-mode-hook)
1928 ;; Create and bind the class/source association list as well as the source
1931 gud-jdb-class-source-alist
1932 (gud-jdb-build-class-source-alist
1934 gud-jdb-source-files
1935 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1939 ;; End of debugger-specific information
1943 ;;; When we send a command to the debugger via gud-call, it's annoying
1944 ;;; to see the command and the new prompt inserted into the debugger's
1945 ;;; buffer; we have other ways of knowing the command has completed.
1947 ;;; If the buffer looks like this:
1948 ;;; --------------------
1949 ;;; (gdb) set args foo bar
1951 ;;; --------------------
1952 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1953 ;;; source file to set a breakpoint, we want the buffer to end up like
1955 ;;; --------------------
1956 ;;; (gdb) set args foo bar
1957 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1959 ;;; --------------------
1960 ;;; Essentially, the old prompt is deleted, and the command's output
1961 ;;; and the new prompt take its place.
1963 ;;; Not echoing the command is easy enough; you send it directly using
1964 ;;; process-send-string, and it never enters the buffer. However,
1965 ;;; getting rid of the old prompt is trickier; you don't want to do it
1966 ;;; when you send the command, since that will result in an annoying
1967 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1968 ;;; waits for a response from the debugger, and the new prompt is
1969 ;;; inserted. Instead, we'll wait until we actually get some output
1970 ;;; from the subprocess before we delete the prompt. If the command
1971 ;;; produced no output other than a new prompt, that prompt will most
1972 ;;; likely be in the first chunk of output received, so we will delete
1973 ;;; the prompt and then replace it with an identical one. If the
1974 ;;; command produces output, the prompt is moving anyway, so the
1975 ;;; flicker won't be annoying.
1977 ;;; So - when we want to delete the prompt upon receipt of the next
1978 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1979 ;;; the start of the prompt; the process filter will notice this, and
1980 ;;; delete all text between it and the process output marker. If
1981 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1983 (defvar gud-delete-prompt-marker nil)
1986 (put 'gud-mode 'mode-class 'special)
1989 "Major mode for interacting with an inferior debugger process.
1991 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1992 M-x perldb, or M-x xdb. Each entry point finishes by executing a
1993 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
1994 `perldb-mode-hook', or `xdb-mode-hook' respectively.
1996 After startup, the following commands are available in both the GUD
1997 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2000 \\[gud-break] sets a breakpoint at the current file and line. In the
2001 GUD buffer, the current file and line are those of the last breakpoint or
2002 step. In a source buffer, they are the buffer's file and current line.
2004 \\[gud-remove] removes breakpoints on the current file and line.
2006 \\[gud-refresh] displays in the source window the last line referred to
2009 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2010 step-one-line (not entering function calls), and step-one-instruction
2011 and then update the source window with the current file and position.
2012 \\[gud-cont] continues execution.
2014 \\[gud-print] tries to find the largest C lvalue or function-call expression
2015 around point, and sends it to the debugger for value display.
2017 The above commands are common to all supported debuggers except xdb which
2018 does not support stepping instructions.
2020 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2021 except that the breakpoint is temporary; that is, it is removed when
2022 execution stops on it.
2024 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2025 frame. \\[gud-down] drops back down through one.
2027 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2028 the current function and stops.
2030 All the keystrokes above are accessible in the GUD buffer
2031 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2033 All pre-defined functions for which the concept make sense repeat
2034 themselves the appropriate number of times if you give a prefix
2037 You may use the `gud-def' macro in the initialization hook to define other
2040 Other commands for interacting with the debugger process are inherited from
2041 comint mode, which see."
2044 (setq major-mode 'gud-mode)
2045 (setq mode-name "Debugger")
2046 (setq mode-line-process '(":%s"))
2047 (use-local-map comint-mode-map)
2048 (gud-make-debug-menu)
2049 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2050 (make-local-variable 'gud-last-frame)
2051 (setq gud-last-frame nil)
2052 (make-local-variable 'comint-prompt-regexp)
2053 ;; Don't put repeated commands in command history many times.
2054 (make-local-variable 'comint-input-ignoredups)
2055 (setq comint-input-ignoredups t)
2056 (make-local-variable 'paragraph-start)
2057 (make-local-variable 'gud-delete-prompt-marker)
2058 (setq gud-delete-prompt-marker (make-marker))
2059 (run-hooks 'gud-mode-hook))
2061 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
2062 (defun gud-chop-words (string)
2064 (len (length string))
2067 (if (memq (aref string i) '(?\t ? ))
2069 (setq words (cons (substring string beg i) words)
2071 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2072 (setq beg (1+ beg)))
2076 (setq words (cons (substring string beg) words)))
2079 ;; Cause our buffers to be displayed, by default,
2080 ;; in the selected window.
2081 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2083 ;; Perform initializations common to all debuggers.
2084 ;; The first arg is the specified command line,
2085 ;; which starts with the program to debug.
2086 ;; The other three args specify the values to use
2087 ;; for local variables in the debugger buffer.
2088 (defun gud-common-init (command-line massage-args marker-filter find-file)
2089 (let* ((words (gud-chop-words command-line))
2090 (program (car words))
2091 ;; Extract the file name from WORDS
2092 ;; and put t in its place.
2093 ;; Later on we will put the modified file name arg back there.
2094 (file-word (let ((w (cdr words)))
2095 (while (and w (= ?- (aref (car w) 0)))
2101 (and file-word (substitute-in-file-name file-word)))
2103 ;; If a directory was specified, expand the file name.
2104 ;; Otherwise, don't expand it, so GDB can use the PATH.
2105 ;; A file name without directory is literally valid
2106 ;; only if the file exists in ., and in that case,
2107 ;; omitting the expansion here has no visible effect.
2108 (file (and file-word
2109 (if (file-name-directory file-subst)
2110 (expand-file-name file-subst)
2112 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2113 (pop-to-buffer (concat "*gud" filepart "*"))
2114 ;; Set default-directory to the file's directory.
2116 ;; Don't set default-directory if no directory was specified.
2117 ;; In that case, either the file is found in the current directory,
2118 ;; in which case this setq is a no-op,
2119 ;; or it is found by searching PATH,
2120 ;; in which case we don't know what directory it was found in.
2121 (file-name-directory file)
2122 (setq default-directory (file-name-directory file)))
2123 (or (bolp) (newline))
2124 (insert "Current directory is " default-directory "\n")
2125 ;; Put the substituted and expanded file name back in its place.
2127 (while (and w (not (eq (car w) t)))
2131 (apply 'make-comint (concat "gud" filepart) program nil
2132 (funcall massage-args file args)))
2133 ;; Since comint clobbered the mode, we don't set it until now.
2135 (make-local-variable 'gud-marker-filter)
2136 (setq gud-marker-filter marker-filter)
2137 (make-local-variable 'gud-find-file)
2138 (setq gud-find-file find-file)
2140 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2141 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2145 (defun gud-set-buffer ()
2146 (cond ((eq major-mode 'gud-mode)
2147 (setq gud-comint-buffer (current-buffer)))))
2149 (defvar gud-filter-defer-flag nil
2150 "Non-nil means don't process anything from the debugger right now.
2151 It is saved for when this flag is not set.")
2153 (defvar gud-filter-pending-text nil
2154 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2156 ;; These functions are responsible for inserting output from your debugger
2157 ;; into the buffer. The hard work is done by the method that is
2158 ;; the value of gud-marker-filter.
2160 (defun gud-filter (proc string)
2161 ;; Here's where the actual buffer insertion is done
2162 (let (output process-window)
2163 (if (buffer-name (process-buffer proc))
2164 (if gud-filter-defer-flag
2165 ;; If we can't process any text now,
2166 ;; save it for later.
2167 (setq gud-filter-pending-text
2168 (concat (or gud-filter-pending-text "") string))
2170 ;; If we have to ask a question during the processing,
2171 ;; defer any additional text that comes from the debugger
2172 ;; during that time.
2173 (let ((gud-filter-defer-flag t))
2174 ;; Process now any text we previously saved up.
2175 (if gud-filter-pending-text
2176 (setq string (concat gud-filter-pending-text string)
2177 gud-filter-pending-text nil))
2179 (set-buffer (process-buffer proc))
2180 ;; If we have been so requested, delete the debugger prompt.
2181 (if (marker-buffer gud-delete-prompt-marker)
2183 (delete-region (process-mark proc) gud-delete-prompt-marker)
2184 (set-marker gud-delete-prompt-marker nil)))
2185 ;; Save the process output, checking for source file markers.
2186 (setq output (gud-marker-filter string))
2187 ;; Check for a filename-and-line number.
2188 ;; Don't display the specified file
2189 ;; unless (1) point is at or after the position where output appears
2190 ;; and (2) this buffer is on the screen.
2191 (setq process-window
2193 (>= (point) (process-mark proc))
2194 (get-buffer-window (current-buffer))))
2196 ;; Let the comint filter do the actual insertion.
2197 ;; That lets us inherit various comint features.
2198 (comint-output-filter proc output))
2200 ;; Put the arrow on the source line.
2201 ;; This must be outside of the save-excursion
2202 ;; in case the source file is our current buffer.
2204 (save-selected-window
2205 (select-window process-window)
2206 (gud-display-frame))
2207 ;; We have to be in the proper buffer, (process-buffer proc),
2208 ;; but not in a save-excursion, because that would restore point.
2209 (let ((old-buf (current-buffer)))
2210 (set-buffer (process-buffer proc))
2213 (set-buffer old-buf)))))
2215 ;; If we deferred text that arrived during this processing,
2217 (if gud-filter-pending-text
2218 (gud-filter proc ""))))))
2220 (defun gud-sentinel (proc msg)
2221 (cond ((null (buffer-name (process-buffer proc)))
2223 ;; Stop displaying an arrow in a source file.
2224 (setq overlay-arrow-position nil)
2225 (set-process-buffer proc nil))
2226 ((memq (process-status proc) '(signal exit))
2227 ;; Stop displaying an arrow in a source file.
2228 (setq overlay-arrow-position nil)
2229 (let* ((obuf (current-buffer)))
2230 ;; save-excursion isn't the right thing if
2231 ;; process-buffer is current-buffer
2234 ;; Write something in *compilation* and hack its mode line,
2235 (set-buffer (process-buffer proc))
2236 ;; Fix the mode line.
2237 (setq mode-line-process
2239 (symbol-name (process-status proc))))
2240 (force-mode-line-update)
2242 (insert ?\n mode-name " " msg)
2244 (goto-char (point-max))
2245 (insert ?\n mode-name " " msg)))
2246 ;; If buffer and mode line will show that the process
2247 ;; is dead, we can delete it now. Otherwise it
2248 ;; will stay around until M-x list-processes.
2249 (delete-process proc))
2250 ;; Restore old buffer, but don't restore old point
2251 ;; if obuf is the gud buffer.
2252 (set-buffer obuf))))))
2254 (defun gud-display-frame ()
2255 "Find and obey the last filename-and-line marker from the debugger.
2256 Obeying it means displaying in another window the specified file and line."
2261 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2262 (setq gud-last-last-frame gud-last-frame
2263 gud-last-frame nil))))
2265 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2266 ;; and that its line LINE is visible.
2267 ;; Put the overlay-arrow on the line LINE in that buffer.
2268 ;; Most of the trickiness in here comes from wanting to preserve the current
2269 ;; region-restriction if that's possible. We use an explicit display-buffer
2270 ;; to get around the fact that this is called inside a save-excursion.
2272 (defun gud-display-line (true-file line)
2273 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2276 (or (eq (current-buffer) gud-comint-buffer)
2277 (set-buffer gud-comint-buffer))
2278 (gud-find-file true-file)))
2279 (window (and buffer (or (get-buffer-window buffer)
2280 (display-buffer buffer))))
2290 (setq overlay-arrow-string "=>")
2291 (or overlay-arrow-position
2292 (setq overlay-arrow-position (make-marker)))
2293 (set-marker overlay-arrow-position (point) (current-buffer)))
2294 (cond ((or (< pos (point-min)) (> pos (point-max)))
2297 (set-window-point window overlay-arrow-position)))))
2299 ;;; The gud-call function must do the right thing whether its invoking
2300 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2301 ;;; or a C buffer. In the former case, we want to supply data from
2302 ;;; gud-last-frame. Here's how we do it:
2304 (defun gud-format-command (str arg)
2305 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2306 (frame (or gud-last-frame gud-last-last-frame))
2308 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2309 (let ((key (string-to-char (substring str (match-beginning 2))))
2313 (setq subst (file-name-nondirectory (if insource
2317 (setq subst (file-name-sans-extension
2318 (file-name-nondirectory (if insource
2322 (setq subst (file-name-directory (if insource
2326 (setq subst (if insource
2329 (save-restriction (widen)
2330 (1+ (count-lines 1 (point)))))
2333 (setq subst (gud-find-c-expr)))
2335 (setq subst (gud-read-address)))
2337 (setq subst (if arg (int-to-string arg) ""))))
2338 (setq result (concat result
2339 (substring str (match-beginning 1) (match-end 1))
2341 (setq str (substring str (match-end 2))))
2342 ;; There might be text left in STR when the loop ends.
2343 (concat result str)))
2345 (defun gud-read-address ()
2346 "Return a string containing the core-address found in the buffer at point."
2348 (let ((pt (point)) found begin)
2349 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2351 (found (forward-char 2)
2352 (buffer-substring found
2353 (progn (re-search-forward "[^0-9a-f]")
2356 (t (setq begin (progn (re-search-backward "[^0-9]")
2360 (re-search-forward "[^0-9]")
2362 (buffer-substring begin (point)))))))
2364 (defun gud-call (fmt &optional arg)
2365 (let ((msg (gud-format-command fmt arg)))
2366 (message "Command: %s" msg)
2368 (gud-basic-call msg)))
2370 (defun gud-basic-call (command)
2371 "Invoke the debugger COMMAND displaying source in other window."
2374 (let ((command (concat command "\n"))
2375 (proc (get-buffer-process gud-comint-buffer)))
2376 (or proc (error "Current buffer has no process"))
2377 ;; Arrange for the current prompt to get deleted.
2379 (set-buffer gud-comint-buffer)
2380 (goto-char (process-mark proc))
2382 (if (looking-at comint-prompt-regexp)
2383 (set-marker gud-delete-prompt-marker (point))))
2384 (process-send-string proc command)))
2386 (defun gud-refresh (&optional arg)
2387 "Fix up a possibly garbled display, and redraw the arrow."
2390 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2391 (gud-display-frame))
2394 (defun gud-new-keymap (map)
2395 "Return a new keymap which inherits from MAP and has name `Gud'."
2396 (nconc (make-sparse-keymap "Gud") map))
2398 (defun gud-make-debug-menu ()
2399 "Make sure the current local map has a [menu-bar debug] submap.
2400 If it doesn't, replace it with a new map that inherits it,
2401 and create such a submap in that new map."
2402 (use-local-map (gud-new-keymap (current-local-map)))
2403 (define-key (current-local-map) [menu-bar]
2404 (gud-new-keymap (lookup-key (current-local-map) [menu-bar])))
2405 (define-key (current-local-map) [menu-bar debug]
2406 (cons "Gud" (gud-new-keymap gud-menu-map))))
2408 ;;; Code for parsing expressions out of C code. The single entry point is
2409 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2411 ;;; The rest of this file is a hacked version of gdbsrc.el by
2412 ;;; Debby Ayers <ayers@asc.slb.com>,
2413 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2415 (defun gud-find-c-expr ()
2416 "Returns the C expr that surrounds point."
2419 (let (p expr test-expr)
2421 (setq expr (gud-innermost-expr))
2422 (setq test-expr (gud-prev-expr))
2423 (while (and test-expr (gud-expr-compound test-expr expr))
2424 (let ((prev-expr expr))
2425 (setq expr (cons (car test-expr) (cdr expr)))
2426 (goto-char (car expr))
2427 (setq test-expr (gud-prev-expr))
2428 ;; If we just pasted on the condition of an if or while,
2429 ;; throw it away again.
2430 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2431 '("if" "while" "for"))
2435 (setq test-expr (gud-next-expr))
2436 (while (gud-expr-compound expr test-expr)
2437 (setq expr (cons (car expr) (cdr test-expr)))
2438 (setq test-expr (gud-next-expr))
2440 (buffer-substring (car expr) (cdr expr)))))
2442 (defun gud-innermost-expr ()
2443 "Returns the smallest expr that point is in; move point to beginning of it.
2444 The expr is represented as a cons cell, where the car specifies the point in
2445 the current buffer that marks the beginning of the expr and the cdr specifies
2446 the character after the end of the expr."
2447 (let ((p (point)) begin end)
2449 (setq begin (point))
2462 (defun gud-backward-sexp ()
2463 "Version of `backward-sexp' that catches errors."
2468 (defun gud-forward-sexp ()
2469 "Version of `forward-sexp' that catches errors."
2474 (defun gud-prev-expr ()
2475 "Returns the previous expr, point is set to beginning of that expr.
2476 The expr is represented as a cons cell, where the car specifies the point in
2477 the current buffer that marks the beginning of the expr and the cdr specifies
2478 the character after the end of the expr"
2479 (let ((begin) (end))
2481 (setq begin (point))
2487 (defun gud-next-expr ()
2488 "Returns the following expr, point is set to beginning of that expr.
2489 The expr is represented as a cons cell, where the car specifies the point in
2490 the current buffer that marks the beginning of the expr and the cdr specifies
2491 the character after the end of the expr."
2492 (let ((begin) (end))
2497 (setq begin (point))
2500 (defun gud-expr-compound-sep (span-start span-end)
2501 "Scan from SPAN-START to SPAN-END for punctuation characters.
2502 If `->' is found, return `?.'. If `.' is found, return `?.'.
2503 If any other punctuation is found, return `??'.
2504 If no punctuation is found, return `? '."
2507 (while (< span-start span-end)
2508 (setq syntax (char-syntax (char-after span-start)))
2511 ((= syntax ?.) (setq syntax (char-after span-start))
2513 ((= syntax ?.) (setq result ?.))
2514 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2516 (setq span-start (+ span-start 1)))
2517 (t (setq span-start span-end)
2518 (setq result ??)))))
2519 (setq span-start (+ span-start 1)))
2522 (defun gud-expr-compound (first second)
2523 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2524 The two exprs are represented as a cons cells, where the car
2525 specifies the point in the current buffer that marks the beginning of the
2526 expr and the cdr specifies the character after the end of the expr.
2527 Link exprs of the form:
2534 (let ((span-start (cdr first))
2535 (span-end (car second))
2537 (setq syntax (gud-expr-compound-sep span-start span-end))
2539 ((= (car first) (car second)) nil)
2540 ((= (cdr first) (cdr second)) nil)
2543 (setq span-start (char-after (- span-start 1)))
2544 (setq span-end (char-after span-end))
2546 ((= span-start ?)) t)
2547 ((= span-start ?]) t)
2555 ;;; gud.el ends here