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 'nowarn)))
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 (buffer-substring (comint-line-beginning-position) end))
416 ;; Find the word break. This match will always succeed.
417 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
418 (setq gud-gdb-complete-break (match-beginning 2)
419 command-word (substring command gud-gdb-complete-break))
420 ;; Temporarily install our filter function.
421 (let ((gud-marker-filter 'gud-gdb-complete-filter))
422 ;; Issue the command to GDB.
423 (gud-basic-call (concat "complete " command))
424 (setq gud-gdb-complete-in-progress t
425 gud-gdb-complete-string nil
426 gud-gdb-complete-list nil)
428 (while gud-gdb-complete-in-progress
429 (accept-process-output (get-buffer-process gud-comint-buffer))))
430 ;; Protect against old versions of GDB.
431 (and gud-gdb-complete-list
432 (string-match "^Undefined command: \"complete\""
433 (car gud-gdb-complete-list))
434 (error "This version of GDB doesn't support the `complete' command."))
435 ;; Sort the list like readline.
436 (setq gud-gdb-complete-list
437 (sort gud-gdb-complete-list (function string-lessp)))
438 ;; Remove duplicates.
439 (let ((first gud-gdb-complete-list)
440 (second (cdr gud-gdb-complete-list)))
442 (if (string-equal (car first) (car second))
443 (setcdr first (setq second (cdr second)))
445 second (cdr second)))))
446 ;; Add a trailing single quote if there is a unique completion
447 ;; and it contains an odd number of unquoted single quotes.
448 (and (= (length gud-gdb-complete-list) 1)
449 (let ((str (car gud-gdb-complete-list))
452 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
453 (setq count (1+ count)
455 (and (= (mod count 2) 1)
456 (setq gud-gdb-complete-list (list (concat str "'"))))))
457 ;; Let comint handle the rest.
458 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
460 ;; The completion process filter is installed temporarily to slurp the
461 ;; output of GDB up to the next prompt and build the completion list.
462 (defun gud-gdb-complete-filter (string)
463 (setq string (concat gud-gdb-complete-string string))
464 (while (string-match "\n" string)
465 (setq gud-gdb-complete-list
466 (cons (substring string gud-gdb-complete-break (match-beginning 0))
467 gud-gdb-complete-list))
468 (setq string (substring string (match-end 0))))
469 (if (string-match comint-prompt-regexp string)
471 (setq gud-gdb-complete-in-progress nil)
474 (setq gud-gdb-complete-string string)
477 ;; gdb speedbar functions
479 (defun gud-gdb-goto-stackframe (text token indent)
480 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
481 (speedbar-with-attached-buffer
482 (gud-basic-call (concat "frame " (nth 1 token)))
485 (defvar gud-gdb-fetched-stack-frame nil
486 "Stack frames we are fetching from GDB.")
488 (defvar gud-gdb-fetched-stack-frame-list nil
489 "List of stack frames we are fetching from GDB.")
491 ;(defun gud-gdb-get-scope-data (text token indent)
492 ; ;; checkdoc-params: (indent)
493 ; "Fetch data associated with a stack frame, and expand/contract it.
494 ;Data to do this is retrieved from TEXT and TOKEN."
495 ; (let ((args nil) (scope nil))
496 ; (gud-gdb-run-command-fetch-lines "info args")
498 ; (gud-gdb-run-command-fetch-lines "info local")
502 (defun gud-gdb-get-stackframe (buffer)
503 "Extract the current stack frame out of the GUD GDB BUFFER."
505 (gud-gdb-fetched-stack-frame-list nil))
506 (gud-gdb-run-command-fetch-lines "backtrace" buffer)
507 (if (and (car gud-gdb-fetched-stack-frame-list)
508 (string-match "No stack" (car gud-gdb-fetched-stack-frame-list)))
509 ;; Go into some other mode???
511 (while gud-gdb-fetched-stack-frame-list
512 (let ((e (car gud-gdb-fetched-stack-frame-list))
513 (name nil) (num nil))
515 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
516 (string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))
517 (if (not (string-match
518 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
521 (list (nth 0 (car newlst))
524 (match-string 2 e))))
525 (setq num (match-string 1 e)
526 name (match-string 2 e))
530 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
531 (list name num (match-string 1 e)
535 (setq gud-gdb-fetched-stack-frame-list
536 (cdr gud-gdb-fetched-stack-frame-list)))
539 ;(defun gud-gdb-selected-frame-info (buffer)
540 ; "Learn GDB information for the currently selected stack frame in BUFFER."
543 (defun gud-gdb-run-command-fetch-lines (command buffer)
544 "Run COMMAND, and return when `gud-gdb-fetched-stack-frame-list' is full.
545 BUFFER is the GUD buffer in which to run the command."
549 (goto-char (point-max))
551 (not (looking-at comint-prompt-regexp)))
553 ;; Much of this copied from GDB complete, but I'm grabbing the stack
555 (let ((gud-marker-filter 'gud-gdb-speedbar-stack-filter))
556 ;; Issue the command to GDB.
557 (gud-basic-call command)
558 (setq gud-gdb-complete-in-progress t ;; use this flag for our purposes.
559 gud-gdb-complete-string nil
560 gud-gdb-complete-list nil)
562 (while gud-gdb-complete-in-progress
563 (accept-process-output (get-buffer-process gud-comint-buffer)))
564 (setq gud-gdb-fetched-stack-frame nil
565 gud-gdb-fetched-stack-frame-list
566 (nreverse gud-gdb-fetched-stack-frame-list))))))
568 (defun gud-gdb-speedbar-stack-filter (string)
569 ;; checkdoc-params: (string)
570 "Filter used to read in the current GDB stack."
571 (setq string (concat gud-gdb-fetched-stack-frame string))
572 (while (string-match "\n" string)
573 (setq gud-gdb-fetched-stack-frame-list
574 (cons (substring string 0 (match-beginning 0))
575 gud-gdb-fetched-stack-frame-list))
576 (setq string (substring string (match-end 0))))
577 (if (string-match comint-prompt-regexp string)
579 (setq gud-gdb-complete-in-progress nil)
582 (setq gud-gdb-complete-string string)
586 ;; ======================================================================
589 ;;; History of argument lists passed to sdb.
590 (defvar gud-sdb-history nil)
592 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
593 "If nil, we're on a System V Release 4 and don't need the tags hack.")
595 (defvar gud-sdb-lastfile nil)
597 (defun gud-sdb-massage-args (file args) args)
599 (defun gud-sdb-marker-filter (string)
601 (if gud-marker-acc (concat gud-marker-acc string) string))
603 ;; Process all complete markers in this chunk
606 ;; System V Release 3.2 uses this format
607 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
608 gud-marker-acc start)
611 (substring gud-marker-acc (match-beginning 3) (match-end 3))
613 (substring gud-marker-acc (match-beginning 4) (match-end 4))))))
614 ;; System V Release 4.0 quite often clumps two lines together
615 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
616 gud-marker-acc start)
617 (setq gud-sdb-lastfile
618 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
623 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
624 ;; System V Release 4.0
625 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
626 gud-marker-acc start)
627 (setq gud-sdb-lastfile
628 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
629 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
630 gud-marker-acc start))
635 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
637 (setq gud-sdb-lastfile nil)))
638 (setq start (match-end 0)))
640 ;; Search for the last incomplete line in this chunk
641 (while (string-match "\n" gud-marker-acc start)
642 (setq start (match-end 0)))
644 ;; If we have an incomplete line, store it in gud-marker-acc.
645 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
648 (defun gud-sdb-find-file (f)
650 (let ((buf (if gud-sdb-needs-tags
651 (find-tag-noselect f)
652 (find-file-noselect f))))
654 (gud-make-debug-menu)
655 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
659 (defun sdb (command-line)
660 "Run sdb on program FILE in buffer *gud-FILE*.
661 The directory containing FILE becomes the initial working directory
662 and source-file directory for your debugger."
664 (list (read-from-minibuffer "Run sdb (like this): "
665 (if (consp gud-sdb-history)
666 (car gud-sdb-history)
670 (if (and gud-sdb-needs-tags
671 (not (and (boundp 'tags-file-name)
672 (stringp tags-file-name)
673 (file-exists-p tags-file-name))))
674 (error "The sdb support requires a valid tags table to work."))
676 (gud-common-init command-line 'gud-sdb-massage-args
677 'gud-sdb-marker-filter 'gud-sdb-find-file)
679 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
680 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
681 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
682 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
683 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
684 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
685 (gud-def gud-cont "c" "\C-r" "Continue with display.")
686 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
688 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
689 (setq paragraph-start comint-prompt-regexp)
690 (local-set-key [menu-bar debug tbreak]
691 '("Temporary Breakpoint" . gud-tbreak))
692 (run-hooks 'sdb-mode-hook)
695 ;; ======================================================================
698 ;;; History of argument lists passed to dbx.
699 (defvar gud-dbx-history nil)
701 (defcustom gud-dbx-directories nil
702 "*A list of directories that dbx should search for source code.
703 If nil, only source files in the program directory
704 will be known to dbx.
706 The file names should be absolute, or relative to the directory
707 containing the executable being debugged."
708 :type '(choice (const :tag "Current Directory" nil)
713 (defun gud-dbx-massage-args (file args)
714 (nconc (let ((directories gud-dbx-directories)
717 (setq result (cons (car directories) (cons "-I" result)))
718 (setq directories (cdr directories)))
722 (defun gud-dbx-file-name (f)
723 "Transform a relative file name to an absolute file name, for dbx."
725 (if (file-exists-p f)
726 (setq result (expand-file-name f))
727 (let ((directories gud-dbx-directories))
729 (let ((path (concat (car directories) "/" f)))
730 (if (file-exists-p path)
731 (setq result (expand-file-name path)
733 (setq directories (cdr directories)))))
736 (defun gud-dbx-marker-filter (string)
737 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
740 ;; Process all complete markers in this chunk.
741 (while (or (string-match
742 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
743 gud-marker-acc start)
745 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
746 gud-marker-acc start))
749 (substring gud-marker-acc (match-beginning 2) (match-end 2))
751 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
752 start (match-end 0)))
754 ;; Search for the last incomplete line in this chunk
755 (while (string-match "\n" gud-marker-acc start)
756 (setq start (match-end 0)))
758 ;; If the incomplete line APPEARS to begin with another marker, keep it
759 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
760 ;; unnecessary concat during the next call.
762 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
763 (substring gud-marker-acc (match-beginning 0))
767 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
768 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
770 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
771 ;; We haven't tested gud on this system:
772 (string-match "^mips-[^-]*-riscos" system-configuration)
773 ;; It's documented on OSF/1.3
774 (string-match "^mips-[^-]*-osf1" system-configuration)
775 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
776 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
778 (defun gud-mipsdbx-massage-args (file args)
779 (cons "-emacs" args))
781 ;; This is just like the gdb one except for the regexps since we need to cope
782 ;; with an optional breakpoint number in [] before the ^Z^Z
783 (defun gud-mipsdbx-marker-filter (string)
784 (setq gud-marker-acc (concat gud-marker-acc string))
787 ;; Process all the complete markers in this chunk.
789 ;; This is like th gdb marker but with an optional
790 ;; leading break point number like `[1] '
791 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
795 ;; Extract the frame position from the marker.
797 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
798 (string-to-int (substring gud-marker-acc
802 ;; Append any text before the marker to the output we're going
803 ;; to return - we don't include the marker in this text.
804 output (concat output
805 (substring gud-marker-acc 0 (match-beginning 0)))
807 ;; Set the accumulator to the remaining text.
808 gud-marker-acc (substring gud-marker-acc (match-end 0))))
810 ;; Does the remaining text look like it might end with the
811 ;; beginning of another marker? If it does, then keep it in
812 ;; gud-marker-acc until we receive the rest of it. Since we
813 ;; know the full marker regexp above failed, it's pretty simple to
814 ;; test for marker starts.
815 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
817 ;; Everything before the potential marker start can be output.
818 (setq output (concat output (substring gud-marker-acc
819 0 (match-beginning 0))))
821 ;; Everything after, we save, to combine with later input.
823 (substring gud-marker-acc (match-beginning 0))))
825 (setq output (concat output gud-marker-acc)
830 ;; The dbx in IRIX is a pain. It doesn't print the file name when
831 ;; stopping at a breakpoint (but you do get it from the `up' and
832 ;; `down' commands...). The only way to extract the information seems
833 ;; to be with a `file' command, although the current line number is
834 ;; available in $curline. Thus we have to look for output which
835 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
836 ;; to output the information we want with a combination of the
837 ;; `printf' and `file' commands as a pseudo marker which we can
838 ;; recognise next time through the marker-filter. This would be like
839 ;; the gdb marker but you can't get the file name without a newline...
840 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
841 ;; number rather than a line number etc. Maybe this could be made to
842 ;; work by listing all the breakpoints and picking the one(s) with the
843 ;; correct line number, but life's too short.
844 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
847 (and (string-match "^mips-[^-]*-irix" system-configuration)
848 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
849 "Non-nil to assume the interface appropriate for IRIX dbx.
850 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
851 a better solution in 6.1 upwards.")
852 (defvar gud-dbx-use-stopformat-p
853 (string-match "irix[6-9]\\.[1-9]" system-configuration)
854 "Non-nil to use the dbx feature present at least from Irix 6.1
855 whereby $stopformat=1 produces an output format compatiable with
856 `gud-dbx-marker-filter'.")
857 ;; [Irix dbx seems to be a moving target. The dbx output changed
858 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
859 ;; the output from `up' is no longer spotted by gud (and it's probably
860 ;; not distinctive enough to try to match it -- use C-<, C->
861 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
862 ;; `long long'(why?!), so the printf stuff needed changing. The line
863 ;; number was cast to `long' as a compromise between the new `long
864 ;; long' and the original `int'. This is reported not to work in 6.2,
865 ;; so it's changed back to int -- don't make your sources too long.
866 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
867 ;; whereby `set $stopformat=1' reportedly produces output compatible
868 ;; with `gud-dbx-marker-filter', which we prefer.
870 ;; The process filter is also somewhat
871 ;; unreliable, sometimes not spotting the markers; I don't know
872 ;; whether there's anything that can be done about that. It would be
873 ;; much better if SGI could be persuaded to (re?)instate the MIPS
874 ;; -emacs flag for gdb-like output (which ought to be possible as most
875 ;; of the communication I've had over it has been from sgi.com).]
877 ;; this filter is influenced by the xdb one rather than the gdb one
878 (defun gud-irixdbx-marker-filter (string)
879 (let (result (case-fold-search nil))
880 (if (or (string-match comint-prompt-regexp string)
881 (string-match ".*\012" string))
882 (setq result (concat gud-marker-acc string)
884 (setq gud-marker-acc (concat gud-marker-acc string)))
887 ;; look for breakpoint or signal indication e.g.:
888 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
889 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
890 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
892 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
894 ;; prod dbx into printing out the line number and file
895 ;; name in a form we can grok as below
896 (process-send-string (get-buffer-process gud-comint-buffer)
897 "printf \"\032\032%1d:\",(int)$curline;file\n"))
898 ;; look for result of, say, "up" e.g.:
899 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
900 ;; (this will also catch one of the lines printed by "where")
902 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
904 (let ((file (substring result (match-beginning 1)
906 (if (file-exists-p file)
910 result (match-beginning 1) (match-end 1))
913 result (match-beginning 2) (match-end 2)))))))
915 ((string-match ; kluged-up marker as above
916 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
917 (let ((file (gud-dbx-file-name
918 (substring result (match-beginning 2) (match-end 2)))))
919 (if (and file (file-exists-p file))
925 result (match-beginning 1) (match-end 1)))))))
926 (setq result (substring result 0 (match-beginning 0))))))
929 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
930 "Non-nil means to assume the interface approriate for DG/UX dbx.
931 This was tested using R4.11.")
933 ;; There are a couple of differences between DG's dbx output and normal
934 ;; dbx output which make it nontrivial to integrate this into the
935 ;; standard dbx-marker-filter (mainly, there are a different number of
936 ;; backreferences). The markers look like:
938 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
940 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
943 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
947 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
949 ;; from up/down/where.
951 (defun gud-dguxdbx-marker-filter (string)
952 (setq gud-marker-acc (if gud-marker-acc
953 (concat gud-marker-acc string)
955 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
956 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
958 ;; Process all complete markers in this chunk.
959 (while (string-match re gud-marker-acc start)
962 (substring gud-marker-acc (match-beginning 4) (match-end 4))
963 (string-to-int (substring gud-marker-acc
964 (match-beginning 3) (match-end 3))))
965 start (match-end 0)))
967 ;; Search for the last incomplete line in this chunk
968 (while (string-match "\n" gud-marker-acc start)
969 (setq start (match-end 0)))
971 ;; If the incomplete line APPEARS to begin with another marker, keep it
972 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
973 ;; unnecessary concat during the next call.
975 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
976 (substring gud-marker-acc (match-beginning 0))
980 (defun gud-dbx-find-file (f)
982 (let ((realf (gud-dbx-file-name f)))
984 (let ((buf (find-file-noselect realf)))
986 (gud-make-debug-menu)
987 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
988 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
993 (defun dbx (command-line)
994 "Run dbx on program FILE in buffer *gud-FILE*.
995 The directory containing FILE becomes the initial working directory
996 and source-file directory for your debugger."
998 (list (read-from-minibuffer "Run dbx (like this): "
999 (if (consp gud-dbx-history)
1000 (car gud-dbx-history)
1007 (gud-common-init command-line 'gud-mipsdbx-massage-args
1008 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
1010 (gud-common-init command-line 'gud-dbx-massage-args
1011 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
1013 (gud-common-init command-line 'gud-dbx-massage-args
1014 'gud-dguxdbx-marker-filter 'gud-dbx-find-file))
1016 (gud-common-init command-line 'gud-dbx-massage-args
1017 'gud-dbx-marker-filter 'gud-dbx-find-file)))
1021 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1022 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1023 (gud-def gud-break "stop at \"%f\":%l"
1024 "\C-b" "Set breakpoint at current line.")
1025 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1027 (gud-def gud-break "stop at \"%d%f\":%l"
1028 "\C-b" "Set breakpoint at current line.")
1029 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1030 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1031 "<" "Up (numeric arg) stack frames.")
1032 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1033 ">" "Down (numeric arg) stack frames.")
1034 ;; Make dbx give out the source location info that we need.
1035 (process-send-string (get-buffer-process gud-comint-buffer)
1036 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1037 (gud-dbx-use-stopformat-p
1038 (process-send-string (get-buffer-process gud-comint-buffer)
1039 "set $stopformat=1\n"))
1041 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1042 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1043 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1044 "\C-b" "Set breakpoint at current line.")))
1046 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1047 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1048 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1049 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1050 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1051 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1053 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1054 (setq paragraph-start comint-prompt-regexp)
1055 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1056 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1057 (run-hooks 'dbx-mode-hook)
1060 ;; ======================================================================
1061 ;; xdb (HP PARISC debugger) functions
1063 ;;; History of argument lists passed to xdb.
1064 (defvar gud-xdb-history nil)
1066 (defcustom gud-xdb-directories nil
1067 "*A list of directories that xdb should search for source code.
1068 If nil, only source files in the program directory
1069 will be known to xdb.
1071 The file names should be absolute, or relative to the directory
1072 containing the executable being debugged."
1073 :type '(choice (const :tag "Current Directory" nil)
1078 (defun gud-xdb-massage-args (file args)
1079 (nconc (let ((directories gud-xdb-directories)
1082 (setq result (cons (car directories) (cons "-d" result)))
1083 (setq directories (cdr directories)))
1087 (defun gud-xdb-file-name (f)
1088 "Transform a relative pathname to a full pathname in xdb mode"
1090 (if (file-exists-p f)
1091 (setq result (expand-file-name f))
1092 (let ((directories gud-xdb-directories))
1094 (let ((path (concat (car directories) "/" f)))
1095 (if (file-exists-p path)
1096 (setq result (expand-file-name path)
1098 (setq directories (cdr directories)))))
1101 ;; xdb does not print the lines all at once, so we have to accumulate them
1102 (defun gud-xdb-marker-filter (string)
1104 (if (or (string-match comint-prompt-regexp string)
1105 (string-match ".*\012" string))
1106 (setq result (concat gud-marker-acc string)
1108 (setq gud-marker-acc (concat gud-marker-acc string)))
1110 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1112 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1114 (let ((line (string-to-int
1115 (substring result (match-beginning 2) (match-end 2))))
1116 (file (gud-xdb-file-name
1117 (substring result (match-beginning 1) (match-end 1)))))
1119 (setq gud-last-frame (cons file line))))))
1122 (defun gud-xdb-find-file (f)
1124 (let ((realf (gud-xdb-file-name f)))
1126 (let ((buf (find-file-noselect realf)))
1128 (gud-make-debug-menu)
1129 (local-set-key [menu-bar debug tbreak]
1130 '("Temporary Breakpoint" . gud-tbreak))
1131 (local-set-key [menu-bar debug finish]
1132 '("Finish Function" . gud-finish))
1133 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1134 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1139 (defun xdb (command-line)
1140 "Run xdb on program FILE in buffer *gud-FILE*.
1141 The directory containing FILE becomes the initial working directory
1142 and source-file directory for your debugger.
1144 You can set the variable 'gud-xdb-directories' to a list of program source
1145 directories if your program contains sources from more than one directory."
1147 (list (read-from-minibuffer "Run xdb (like this): "
1148 (if (consp gud-xdb-history)
1149 (car gud-xdb-history)
1154 (gud-common-init command-line 'gud-xdb-massage-args
1155 'gud-xdb-marker-filter 'gud-xdb-find-file)
1157 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1158 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1159 "Set temporary breakpoint at current line.")
1160 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1161 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1162 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1163 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1164 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1165 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1166 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1167 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1169 (setq comint-prompt-regexp "^>")
1170 (setq paragraph-start comint-prompt-regexp)
1171 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
1172 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1173 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1174 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1175 (run-hooks 'xdb-mode-hook))
1177 ;; ======================================================================
1180 ;;; History of argument lists passed to perldb.
1181 (defvar gud-perldb-history nil)
1183 ;; Convert a command line as would be typed normally to run a script
1184 ;; into one that invokes an Emacs-enabled debugging session.
1185 ;; "-d" in inserted as the first switch, and "-emacs" is inserted where
1186 ;; it will be $ARGV[0] (see perl5db.pl).
1187 (defun gud-perldb-massage-args (file args)
1188 (let* ((new-args '("-d"))
1191 (setq new-args (cons (car args) new-args))
1192 (setq args (cdr args)))))
1194 ;; Pass all switches and -e scripts through.
1196 (string-match "^-" (car args))
1197 (not (equal "-" (car args)))
1198 (not (equal "--" (car args))))
1199 (when (equal "-e" (car args))
1200 ;; -e goes with the next arg, so shift one extra.
1202 ;; -e as the last arg is an error in Perl.
1203 (error "No code specified for -e."))
1209 (string-match "^-" (car args)))
1210 (error "Can't use stdin as the script to debug."))
1211 ;; This is the program name.
1214 ;; If -e specified, make sure there is a -- so -emacs is not taken
1216 (if (and args (equal "--" (car args)))
1218 (and seen-e (setq new-args (cons "--" new-args))))
1220 (setq new-args (cons "-emacs" new-args))
1224 (nreverse new-args)))
1226 ;; There's no guarantee that Emacs will hand the filter the entire
1227 ;; marker at once; it could be broken up across several strings. We
1228 ;; might even receive a big chunk with several markers in it. If we
1229 ;; receive a chunk of text which looks like it might contain the
1230 ;; beginning of a marker, we save it here between calls to the
1232 (defun gud-perldb-marker-filter (string)
1233 (setq gud-marker-acc (concat gud-marker-acc string))
1236 ;; Process all the complete markers in this chunk.
1237 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1241 ;; Extract the frame position from the marker.
1243 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1244 (string-to-int (substring gud-marker-acc
1248 ;; Append any text before the marker to the output we're going
1249 ;; to return - we don't include the marker in this text.
1250 output (concat output
1251 (substring gud-marker-acc 0 (match-beginning 0)))
1253 ;; Set the accumulator to the remaining text.
1254 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1256 ;; Does the remaining text look like it might end with the
1257 ;; beginning of another marker? If it does, then keep it in
1258 ;; gud-marker-acc until we receive the rest of it. Since we
1259 ;; know the full marker regexp above failed, it's pretty simple to
1260 ;; test for marker starts.
1261 (if (string-match "\032.*\\'" gud-marker-acc)
1263 ;; Everything before the potential marker start can be output.
1264 (setq output (concat output (substring gud-marker-acc
1265 0 (match-beginning 0))))
1267 ;; Everything after, we save, to combine with later input.
1268 (setq gud-marker-acc
1269 (substring gud-marker-acc (match-beginning 0))))
1271 (setq output (concat output gud-marker-acc)
1276 (defun gud-perldb-find-file (f)
1278 (let ((buf (find-file-noselect f)))
1280 (gud-make-debug-menu)
1283 (defcustom gud-perldb-command-name "perl"
1284 "File name for executing Perl."
1289 (defun perldb (command-line)
1290 "Run perldb on program FILE in buffer *gud-FILE*.
1291 The directory containing FILE becomes the initial working directory
1292 and source-file directory for your debugger."
1294 (list (read-from-minibuffer "Run perldb (like this): "
1295 (if (consp gud-perldb-history)
1296 (car gud-perldb-history)
1297 (concat gud-perldb-command-name
1299 (or (buffer-file-name)
1303 'gud-perldb-history)))
1305 (gud-common-init command-line 'gud-perldb-massage-args
1306 'gud-perldb-marker-filter 'gud-perldb-find-file)
1308 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1309 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1310 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1311 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1312 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1313 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1314 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1315 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1316 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1318 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1319 (setq paragraph-start comint-prompt-regexp)
1320 (run-hooks 'perldb-mode-hook)
1323 ;; ======================================================================
1324 ;; pdb (Python debugger) functions
1326 ;;; History of argument lists passed to pdb.
1327 (defvar gud-pdb-history nil)
1329 (defun gud-pdb-massage-args (file args)
1332 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1333 ;; Either file or function name may be omitted: "> <string>(0)?()"
1334 (defvar gud-pdb-marker-regexp
1335 "^> \\([-a-zA-Z0-9_/.]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1336 (defvar gud-pdb-marker-regexp-file-group 1)
1337 (defvar gud-pdb-marker-regexp-line-group 2)
1338 (defvar gud-pdb-marker-regexp-fnname-group 3)
1340 (defvar gud-pdb-marker-regexp-start "^> ")
1342 ;; There's no guarantee that Emacs will hand the filter the entire
1343 ;; marker at once; it could be broken up across several strings. We
1344 ;; might even receive a big chunk with several markers in it. If we
1345 ;; receive a chunk of text which looks like it might contain the
1346 ;; beginning of a marker, we save it here between calls to the
1348 (defun gud-pdb-marker-filter (string)
1349 (setq gud-marker-acc (concat gud-marker-acc string))
1352 ;; Process all the complete markers in this chunk.
1353 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1356 ;; Extract the frame position from the marker.
1358 (let ((file (match-string gud-pdb-marker-regexp-file-group
1360 (line (string-to-int
1361 (match-string gud-pdb-marker-regexp-line-group
1363 (if (string-equal file "<string>")
1367 ;; Output everything instead of the below
1368 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1369 ;; ;; Append any text before the marker to the output we're going
1370 ;; ;; to return - we don't include the marker in this text.
1371 ;; output (concat output
1372 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1374 ;; Set the accumulator to the remaining text.
1375 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1377 ;; Does the remaining text look like it might end with the
1378 ;; beginning of another marker? If it does, then keep it in
1379 ;; gud-marker-acc until we receive the rest of it. Since we
1380 ;; know the full marker regexp above failed, it's pretty simple to
1381 ;; test for marker starts.
1382 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1384 ;; Everything before the potential marker start can be output.
1385 (setq output (concat output (substring gud-marker-acc
1386 0 (match-beginning 0))))
1388 ;; Everything after, we save, to combine with later input.
1389 (setq gud-marker-acc
1390 (substring gud-marker-acc (match-beginning 0))))
1392 (setq output (concat output gud-marker-acc)
1397 (defun gud-pdb-find-file (f)
1399 (let ((buf (find-file-noselect f)))
1401 (gud-make-debug-menu)
1402 ;; (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1403 ;; (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1404 ;; (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1407 (defvar pdb-minibuffer-local-map nil
1408 "Keymap for minibuffer prompting of pdb startup command.")
1409 (if pdb-minibuffer-local-map
1411 (setq pdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
1413 pdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
1415 (defcustom gud-pdb-command-name "pdb"
1416 "File name for executing the Python debugger.
1417 This should be an executable on your path, or an absolute file name."
1422 (defun pdb (command-line)
1423 "Run pdb on program FILE in buffer `*gud-FILE*'.
1424 The directory containing FILE becomes the initial working directory
1425 and source-file directory for your debugger."
1427 (list (read-from-minibuffer "Run pdb (like this): "
1428 (if (consp gud-pdb-history)
1429 (car gud-pdb-history)
1430 (concat gud-pdb-command-name " "))
1431 pdb-minibuffer-local-map nil
1434 (gud-common-init command-line 'gud-pdb-massage-args
1435 'gud-pdb-marker-filter 'gud-pdb-find-file)
1437 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1438 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1439 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1440 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1441 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1442 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1443 (gud-def gud-up "up" "<" "Up one stack frame.")
1444 (gud-def gud-down "down" ">" "Down one stack frame.")
1445 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1447 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1449 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1450 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1451 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1452 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1453 (setq comint-prompt-regexp "^(Pdb) *")
1454 (setq paragraph-start comint-prompt-regexp)
1455 (run-hooks 'pdb-mode-hook))
1457 ;; ======================================================================
1461 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1463 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1465 ;; INVOCATION NOTES:
1467 ;; You invoke jdb-mode with:
1471 ;; It responds with:
1473 ;; Run jdb (like this): jdb
1475 ;; type any jdb switches followed by the name of the class you'd like to debug.
1476 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1477 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1478 ;; See the known problems section below for restrictions when specifying jdb
1479 ;; command line switches (search forward for '-classpath').
1481 ;; You should see something like the following:
1483 ;; Current directory is ~/src/java/hello/
1484 ;; Initializing jdb...
1485 ;; 0xed2f6628:class(hello)
1488 ;; To set an initial breakpoint try:
1490 ;; > stop in hello.main
1491 ;; Breakpoint set in hello.main
1494 ;; To execute the program type:
1499 ;; Breakpoint hit: running ...
1500 ;; hello.main (hello:12)
1502 ;; Type M-n to step over the current line and M-s to step into it. That,
1503 ;; along with the JDB 'help' command should get you started. The 'quit'
1504 ;; JDB command will get out out of the debugger. There is some truly
1505 ;; pathetic JDB documentation available at:
1507 ;; http://java.sun.com/products/jdk/1.1/debugging/
1509 ;; KNOWN PROBLEMS AND FIXME's:
1511 ;; Not sure what happens with inner classes ... haven't tried them.
1513 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1515 ;; You must not put whitespace between "-classpath" and the path to
1516 ;; search for java classes even though it is required when invoking jdb
1517 ;; from the command line. See gud-jdb-massage-args for details.
1519 ;; If any of the source files in the directories listed in
1520 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1521 ;; every file ending in ".java" in these directories parses without error.
1523 ;; All the .java files in the directories in gud-jdb-directories are
1524 ;; syntactically analyzed each time gud jdb is invoked. It would be
1525 ;; nice to keep as much information as possible between runs. It would
1526 ;; be really nice to analyze the files only as neccessary (when the
1527 ;; source needs to be displayed.) I'm not sure to what extent the former
1528 ;; can be accomplished and I'm not sure the latter can be done at all
1529 ;; since I don't know of any general way to tell which .class files are
1530 ;; defined by which .java file without analyzing all the .java files.
1531 ;; If anyone knows why JavaSoft didn't put the source file names in
1532 ;; debuggable .class files please clue me in so I find something else
1533 ;; to be spiteful and bitter about.
1535 ;; ======================================================================
1536 ;; gud jdb variables and functions
1538 ;; History of argument lists passed to jdb.
1539 (defvar gud-jdb-history nil)
1541 ;; List of Java source file directories.
1542 (defvar gud-jdb-directories (list ".")
1543 "*A list of directories that gud jdb should search for source code.
1544 The file names should be absolute, or relative to the current
1547 The set of .java files residing in the directories listed are
1548 syntactically analyzed to determine the classes they define and the
1549 packages in which these classes belong. In this way gud jdb maps the
1550 package-qualified class names output by the jdb debugger to the source
1551 file from which the class originated. This allows gud mode to keep
1552 the source code display in sync with the debugging session.")
1554 ;; List of the java source files for this debugging session.
1555 (defvar gud-jdb-source-files nil)
1557 ;; Association list of fully qualified class names (package + class name) and
1558 ;; their source files.
1559 (defvar gud-jdb-class-source-alist nil)
1561 ;; This is used to hold a source file during analysis.
1562 (defvar gud-jdb-analysis-buffer nil)
1564 ;; Return a list of java source files. PATH gives the directories in
1565 ;; which to search for files with extension EXTN. Normally EXTN is
1566 ;; given as the regular expression "\\.java$" .
1567 (defun gud-jdb-build-source-files-list (path extn)
1568 (apply 'nconc (mapcar (lambda (d)
1569 (when (file-directory-p d)
1570 (directory-files d t extn nil)) path))))
1572 ;; Move point past whitespace.
1573 (defun gud-jdb-skip-whitespace ()
1574 (skip-chars-forward " \n\r\t\014"))
1576 ;; Move point past a "// <eol>" type of comment.
1577 (defun gud-jdb-skip-single-line-comment ()
1580 ;; Move point past a "/* */" or "/** */" type of comment.
1581 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1585 (if (eq (following-char) ?*)
1589 (if (eq (following-char) ?/)
1592 (throw 'break nil)))))
1595 ;; Move point past any number of consecutive whitespace chars and/or comments.
1596 (defun gud-jdb-skip-whitespace-and-comments ()
1597 (gud-jdb-skip-whitespace)
1602 (gud-jdb-skip-single-line-comment)
1603 (gud-jdb-skip-whitespace))
1604 ((looking-at "/\\*")
1605 (gud-jdb-skip-traditional-or-documentation-comment)
1606 (gud-jdb-skip-whitespace))
1607 (t (throw 'done nil))))))
1609 ;; Move point past things that are id-like. The intent is to skip regular
1610 ;; id's, such as class or interface names as well as package and interface
1612 (defun gud-jdb-skip-id-ish-thing ()
1613 (skip-chars-forward "^ /\n\r\t\014,;{"))
1615 ;; Move point past a string literal.
1616 (defun gud-jdb-skip-string-literal ()
1619 ((eq (following-char) ?\\)
1621 ((eq (following-char) ?\042))))
1625 ;; Move point past a character literal.
1626 (defun gud-jdb-skip-character-literal ()
1630 (if (eq (following-char) ?\\)
1632 (not (eq (following-char) ?\')))
1636 ;; Move point past the following block. There may be (legal) cruft before
1637 ;; the block's opening brace. There must be a block or it's the end of life
1638 ;; in petticoat junction.
1639 (defun gud-jdb-skip-block ()
1641 ;; Find the begining of the block.
1643 (not (eq (following-char) ?{))
1645 ;; Skip any constructs that can harbor literal block delimiter
1646 ;; characters and/or the delimiters for the constructs themselves.
1649 (gud-jdb-skip-single-line-comment))
1650 ((looking-at "/\\*")
1651 (gud-jdb-skip-traditional-or-documentation-comment))
1652 ((eq (following-char) ?\042)
1653 (gud-jdb-skip-string-literal))
1654 ((eq (following-char) ?\')
1655 (gud-jdb-skip-character-literal))
1656 (t (forward-char))))
1658 ;; Now at the begining of the block.
1661 ;; Skip over the body of the block as well as the final brace.
1662 (let ((open-level 1))
1663 (while (not (eq open-level 0))
1666 (gud-jdb-skip-single-line-comment))
1667 ((looking-at "/\\*")
1668 (gud-jdb-skip-traditional-or-documentation-comment))
1669 ((eq (following-char) ?\042)
1670 (gud-jdb-skip-string-literal))
1671 ((eq (following-char) ?\')
1672 (gud-jdb-skip-character-literal))
1673 ((eq (following-char) ?{)
1674 (setq open-level (+ open-level 1))
1676 ((eq (following-char) ?})
1677 (setq open-level (- open-level 1))
1679 (t (forward-char))))))
1681 ;; Find the package and class definitions in Java source file FILE. Assumes
1682 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1683 ;; to hold the source during analysis.
1684 (defun gud-jdb-analyze-source (buf file)
1687 (insert-file-contents file nil nil nil t)
1692 (gud-jdb-skip-whitespace)
1696 ;; Any number of semi's following a block is legal. Move point
1697 ;; past them. Note that comments and whitespace may be
1698 ;; interspersed as well.
1699 ((eq (following-char) ?\073)
1702 ;; Move point past a single line comment.
1704 (gud-jdb-skip-single-line-comment))
1706 ;; Move point past a traditional or documentation comment.
1707 ((looking-at "/\\*")
1708 (gud-jdb-skip-traditional-or-documentation-comment))
1710 ;; Move point past a package statement, but save the PackageName.
1711 ((looking-at "package")
1713 (gud-jdb-skip-whitespace-and-comments)
1715 (gud-jdb-skip-id-ish-thing)
1716 (setq p (concat (buffer-substring s (point)) "."))
1717 (gud-jdb-skip-whitespace-and-comments)
1718 (if (eq (following-char) ?\073)
1721 ;; Move point past an import statement.
1722 ((looking-at "import")
1724 (gud-jdb-skip-whitespace-and-comments)
1725 (gud-jdb-skip-id-ish-thing)
1726 (gud-jdb-skip-whitespace-and-comments)
1727 (if (eq (following-char) ?\073)
1730 ;; Move point past the various kinds of ClassModifiers.
1731 ((looking-at "public")
1733 ((looking-at "abstract")
1735 ((looking-at "final")
1738 ;; Move point past a ClassDeclaraction, but save the class
1740 ((looking-at "class")
1742 (gud-jdb-skip-whitespace-and-comments)
1744 (gud-jdb-skip-id-ish-thing)
1746 l (nconc l (list (concat p (buffer-substring s (point)))))))
1747 (gud-jdb-skip-block))
1749 ;; Move point past an interface statement.
1750 ((looking-at "interface")
1752 (gud-jdb-skip-block))
1754 ;; Anything else means the input is invalid.
1756 (message (format "Error parsing file %s." file))
1757 (throw 'abort nil))))))
1760 (defun gud-jdb-build-class-source-alist-for-file (file)
1764 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1766 ;; Return an alist of fully qualified classes and the source files
1767 ;; holding their definitions. SOURCES holds a list of all the source
1768 ;; files to examine.
1769 (defun gud-jdb-build-class-source-alist (sources)
1770 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1775 'gud-jdb-build-class-source-alist-for-file
1777 (kill-buffer gud-jdb-analysis-buffer)
1778 (setq gud-jdb-analysis-buffer nil)))
1780 ;; Change what was given in the minibuffer to something that can be used to
1781 ;; invoke the debugger.
1782 (defun gud-jdb-massage-args (file args)
1783 ;; The jdb executable must have whitespace between "-classpath" and
1784 ;; its value while gud-common-init expects all switch values to
1785 ;; follow the switch keyword without intervening whitespace. We
1786 ;; require that when the user enters the "-classpath" switch in the
1787 ;; EMACS minibuffer that they do so without the intervening
1788 ;; whitespace. This function adds it back (it's called after
1789 ;; gud-common-init). There are more switches like this (for
1790 ;; instance "-host" and "-password") but I don't care about them
1793 (let (massaged-args user-error)
1797 (not (string-match "-classpath\\(.+\\)" (car args)))
1798 (not (setq user-error
1799 (string-match "-classpath$" (car args)))))
1800 (setq massaged-args (append massaged-args (list (car args))))
1801 (setq args (cdr args)))
1803 ;; By this point the current directory is all screwed up. Maybe we
1804 ;; could fix things and re-invoke gud-common-init, but for now I think
1805 ;; issueing the error is good enough.
1808 (kill-buffer (current-buffer))
1809 (error "Error: Omit whitespace between '-classpath' and its value")))
1819 (match-beginning 1) (match-end 1)))
1823 ;; Search for an association with P, a fully qualified class name, in
1824 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1825 ;; qualified file name of the source file which produced the class.
1826 (defun gud-jdb-find-source-file (p)
1827 (cdr (assoc p gud-jdb-class-source-alist)))
1829 ;; See comentary for other debugger's marker filters - there you will find
1830 ;; important notes about STRING.
1831 (defun gud-jdb-marker-filter (string)
1833 ;; Build up the accumulator.
1834 (setq gud-marker-acc
1836 (concat gud-marker-acc string)
1839 ;; We process STRING from left to right. Each time through the following
1840 ;; loop we process at most one marker. The start variable keeps track of
1841 ;; where we are in the input string through the iterations of this loop.
1842 (let (start file-found)
1844 ;; Process each complete marker in the input. There may be an incomplete
1845 ;; marker at the end of the input string. Incomplete markers are left
1846 ;; in the accumulator for processing the next time the function is called.
1849 ;; Do we see a marker?
1851 ;; jdb puts out a string of the following form when it
1852 ;; hits a breakpoint:
1854 ;; <fully-qualified-class><method> (<class>:<line-number>)
1856 ;; <fully-qualified-class>'s are composed of Java ID's
1857 ;; separated by periods. <method> and <class> are
1858 ;; also Java ID's. <method> begins with a period and
1859 ;; may contain less-than and greater-than (constructors,
1860 ;; for instance, are called <init> in the symbol table.)
1861 ;; Java ID's begin with a letter followed by letters
1862 ;; and/or digits. The set of letters includes underscore
1865 ;; The first group matches <fully-qualified-class>,
1866 ;; the second group matches <class> and the third group
1867 ;; matches <line-number>. We don't care about using
1868 ;; <method> so we don't "group" it.
1870 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1872 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1873 gud-marker-acc start)
1875 ;; Figure out the line on which to position the debugging arrow.
1876 ;; Return the info as a cons of the form:
1878 ;; (<file-name> . <line-number>) .
1881 (gud-jdb-find-source-file
1882 (substring gud-marker-acc
1885 (setq gud-last-frame
1889 (substring gud-marker-acc
1892 (message "Could not find source file."))
1894 ;; Set start after the last character of STRING that we've looked at
1895 ;; and loop to look for another marker.
1896 (setq start (match-end 0))))
1898 ;; We don't filter any debugger output so just return what we were given.
1901 (defun gud-jdb-find-file (f)
1902 (and (file-readable-p f)
1903 (find-file-noselect f)))
1905 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1908 (defun jdb (command-line)
1909 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1910 \"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1911 if there is. If the \"-classpath\" switch is given, omit all whitespace
1912 between it and it's value."
1914 (list (read-from-minibuffer "Run jdb (like this): "
1915 (if (consp gud-jdb-history)
1916 (car gud-jdb-history)
1917 (concat gud-jdb-command-name " "))
1921 (gud-common-init command-line 'gud-jdb-massage-args
1922 'gud-jdb-marker-filter 'gud-jdb-find-file)
1924 (gud-def gud-break "stop at %F:%l" "\C-b" "Set breakpoint at current line.")
1925 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1926 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1927 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1928 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1930 (setq comint-prompt-regexp "^> \\|^.+\\[[0-9]+\\] ")
1931 (setq paragraph-start comint-prompt-regexp)
1932 (run-hooks 'jdb-mode-hook)
1934 ;; Create and bind the class/source association list as well as the source
1937 gud-jdb-class-source-alist
1938 (gud-jdb-build-class-source-alist
1940 gud-jdb-source-files
1941 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1945 ;; End of debugger-specific information
1949 ;;; When we send a command to the debugger via gud-call, it's annoying
1950 ;;; to see the command and the new prompt inserted into the debugger's
1951 ;;; buffer; we have other ways of knowing the command has completed.
1953 ;;; If the buffer looks like this:
1954 ;;; --------------------
1955 ;;; (gdb) set args foo bar
1957 ;;; --------------------
1958 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1959 ;;; source file to set a breakpoint, we want the buffer to end up like
1961 ;;; --------------------
1962 ;;; (gdb) set args foo bar
1963 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1965 ;;; --------------------
1966 ;;; Essentially, the old prompt is deleted, and the command's output
1967 ;;; and the new prompt take its place.
1969 ;;; Not echoing the command is easy enough; you send it directly using
1970 ;;; process-send-string, and it never enters the buffer. However,
1971 ;;; getting rid of the old prompt is trickier; you don't want to do it
1972 ;;; when you send the command, since that will result in an annoying
1973 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1974 ;;; waits for a response from the debugger, and the new prompt is
1975 ;;; inserted. Instead, we'll wait until we actually get some output
1976 ;;; from the subprocess before we delete the prompt. If the command
1977 ;;; produced no output other than a new prompt, that prompt will most
1978 ;;; likely be in the first chunk of output received, so we will delete
1979 ;;; the prompt and then replace it with an identical one. If the
1980 ;;; command produces output, the prompt is moving anyway, so the
1981 ;;; flicker won't be annoying.
1983 ;;; So - when we want to delete the prompt upon receipt of the next
1984 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1985 ;;; the start of the prompt; the process filter will notice this, and
1986 ;;; delete all text between it and the process output marker. If
1987 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1989 (defvar gud-delete-prompt-marker nil)
1992 (put 'gud-mode 'mode-class 'special)
1995 "Major mode for interacting with an inferior debugger process.
1997 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1998 M-x perldb, or M-x xdb. Each entry point finishes by executing a
1999 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2000 `perldb-mode-hook', or `xdb-mode-hook' respectively.
2002 After startup, the following commands are available in both the GUD
2003 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2006 \\[gud-break] sets a breakpoint at the current file and line. In the
2007 GUD buffer, the current file and line are those of the last breakpoint or
2008 step. In a source buffer, they are the buffer's file and current line.
2010 \\[gud-remove] removes breakpoints on the current file and line.
2012 \\[gud-refresh] displays in the source window the last line referred to
2015 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2016 step-one-line (not entering function calls), and step-one-instruction
2017 and then update the source window with the current file and position.
2018 \\[gud-cont] continues execution.
2020 \\[gud-print] tries to find the largest C lvalue or function-call expression
2021 around point, and sends it to the debugger for value display.
2023 The above commands are common to all supported debuggers except xdb which
2024 does not support stepping instructions.
2026 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2027 except that the breakpoint is temporary; that is, it is removed when
2028 execution stops on it.
2030 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2031 frame. \\[gud-down] drops back down through one.
2033 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2034 the current function and stops.
2036 All the keystrokes above are accessible in the GUD buffer
2037 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2039 All pre-defined functions for which the concept make sense repeat
2040 themselves the appropriate number of times if you give a prefix
2043 You may use the `gud-def' macro in the initialization hook to define other
2046 Other commands for interacting with the debugger process are inherited from
2047 comint mode, which see."
2050 (setq major-mode 'gud-mode)
2051 (setq mode-name "Debugger")
2052 (setq mode-line-process '(":%s"))
2053 (use-local-map comint-mode-map)
2054 (gud-make-debug-menu)
2055 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2056 (make-local-variable 'gud-last-frame)
2057 (setq gud-last-frame nil)
2058 (make-local-variable 'comint-prompt-regexp)
2059 ;; Don't put repeated commands in command history many times.
2060 (make-local-variable 'comint-input-ignoredups)
2061 (setq comint-input-ignoredups t)
2062 (make-local-variable 'paragraph-start)
2063 (make-local-variable 'gud-delete-prompt-marker)
2064 (setq gud-delete-prompt-marker (make-marker))
2065 (run-hooks 'gud-mode-hook))
2067 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
2068 (defun gud-chop-words (string)
2070 (len (length string))
2073 (if (memq (aref string i) '(?\t ? ))
2075 (setq words (cons (substring string beg i) words)
2077 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2078 (setq beg (1+ beg)))
2082 (setq words (cons (substring string beg) words)))
2085 ;; Cause our buffers to be displayed, by default,
2086 ;; in the selected window.
2087 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2089 ;; Perform initializations common to all debuggers.
2090 ;; The first arg is the specified command line,
2091 ;; which starts with the program to debug.
2092 ;; The other three args specify the values to use
2093 ;; for local variables in the debugger buffer.
2094 (defun gud-common-init (command-line massage-args marker-filter find-file)
2095 (let* ((words (gud-chop-words command-line))
2096 (program (car words))
2097 ;; Extract the file name from WORDS
2098 ;; and put t in its place.
2099 ;; Later on we will put the modified file name arg back there.
2100 (file-word (let ((w (cdr words)))
2101 (while (and w (= ?- (aref (car w) 0)))
2107 (and file-word (substitute-in-file-name file-word)))
2109 ;; If a directory was specified, expand the file name.
2110 ;; Otherwise, don't expand it, so GDB can use the PATH.
2111 ;; A file name without directory is literally valid
2112 ;; only if the file exists in ., and in that case,
2113 ;; omitting the expansion here has no visible effect.
2114 (file (and file-word
2115 (if (file-name-directory file-subst)
2116 (expand-file-name file-subst)
2118 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2119 (pop-to-buffer (concat "*gud" filepart "*"))
2120 ;; Set default-directory to the file's directory.
2122 ;; Don't set default-directory if no directory was specified.
2123 ;; In that case, either the file is found in the current directory,
2124 ;; in which case this setq is a no-op,
2125 ;; or it is found by searching PATH,
2126 ;; in which case we don't know what directory it was found in.
2127 (file-name-directory file)
2128 (setq default-directory (file-name-directory file)))
2129 (or (bolp) (newline))
2130 (insert "Current directory is " default-directory "\n")
2131 ;; Put the substituted and expanded file name back in its place.
2133 (while (and w (not (eq (car w) t)))
2137 (apply 'make-comint (concat "gud" filepart) program nil
2138 (funcall massage-args file args)))
2139 ;; Since comint clobbered the mode, we don't set it until now.
2141 (make-local-variable 'gud-marker-filter)
2142 (setq gud-marker-filter marker-filter)
2143 (make-local-variable 'gud-find-file)
2144 (setq gud-find-file find-file)
2146 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2147 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2151 (defun gud-set-buffer ()
2152 (cond ((eq major-mode 'gud-mode)
2153 (setq gud-comint-buffer (current-buffer)))))
2155 (defvar gud-filter-defer-flag nil
2156 "Non-nil means don't process anything from the debugger right now.
2157 It is saved for when this flag is not set.")
2159 (defvar gud-filter-pending-text nil
2160 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2162 ;; These functions are responsible for inserting output from your debugger
2163 ;; into the buffer. The hard work is done by the method that is
2164 ;; the value of gud-marker-filter.
2166 (defun gud-filter (proc string)
2167 ;; Here's where the actual buffer insertion is done
2168 (let (output process-window)
2169 (if (buffer-name (process-buffer proc))
2170 (if gud-filter-defer-flag
2171 ;; If we can't process any text now,
2172 ;; save it for later.
2173 (setq gud-filter-pending-text
2174 (concat (or gud-filter-pending-text "") string))
2176 ;; If we have to ask a question during the processing,
2177 ;; defer any additional text that comes from the debugger
2178 ;; during that time.
2179 (let ((gud-filter-defer-flag t))
2180 ;; Process now any text we previously saved up.
2181 (if gud-filter-pending-text
2182 (setq string (concat gud-filter-pending-text string)
2183 gud-filter-pending-text nil))
2185 (set-buffer (process-buffer proc))
2186 ;; If we have been so requested, delete the debugger prompt.
2187 (if (marker-buffer gud-delete-prompt-marker)
2189 (delete-region (process-mark proc) gud-delete-prompt-marker)
2190 (set-marker gud-delete-prompt-marker nil)))
2191 ;; Save the process output, checking for source file markers.
2192 (setq output (gud-marker-filter string))
2193 ;; Check for a filename-and-line number.
2194 ;; Don't display the specified file
2195 ;; unless (1) point is at or after the position where output appears
2196 ;; and (2) this buffer is on the screen.
2197 (setq process-window
2199 (>= (point) (process-mark proc))
2200 (get-buffer-window (current-buffer))))
2202 ;; Let the comint filter do the actual insertion.
2203 ;; That lets us inherit various comint features.
2204 (comint-output-filter proc output))
2206 ;; Put the arrow on the source line.
2207 ;; This must be outside of the save-excursion
2208 ;; in case the source file is our current buffer.
2210 (save-selected-window
2211 (select-window process-window)
2212 (gud-display-frame))
2213 ;; We have to be in the proper buffer, (process-buffer proc),
2214 ;; but not in a save-excursion, because that would restore point.
2215 (let ((old-buf (current-buffer)))
2216 (set-buffer (process-buffer proc))
2219 (set-buffer old-buf)))))
2221 ;; If we deferred text that arrived during this processing,
2223 (if gud-filter-pending-text
2224 (gud-filter proc ""))))))
2226 (defun gud-sentinel (proc msg)
2227 (cond ((null (buffer-name (process-buffer proc)))
2229 ;; Stop displaying an arrow in a source file.
2230 (setq overlay-arrow-position nil)
2231 (set-process-buffer proc nil))
2232 ((memq (process-status proc) '(signal exit))
2233 ;; Stop displaying an arrow in a source file.
2234 (setq overlay-arrow-position nil)
2235 (let* ((obuf (current-buffer)))
2236 ;; save-excursion isn't the right thing if
2237 ;; process-buffer is current-buffer
2240 ;; Write something in *compilation* and hack its mode line,
2241 (set-buffer (process-buffer proc))
2242 ;; Fix the mode line.
2243 (setq mode-line-process
2245 (symbol-name (process-status proc))))
2246 (force-mode-line-update)
2248 (insert ?\n mode-name " " msg)
2250 (goto-char (point-max))
2251 (insert ?\n mode-name " " msg)))
2252 ;; If buffer and mode line will show that the process
2253 ;; is dead, we can delete it now. Otherwise it
2254 ;; will stay around until M-x list-processes.
2255 (delete-process proc))
2256 ;; Restore old buffer, but don't restore old point
2257 ;; if obuf is the gud buffer.
2258 (set-buffer obuf))))))
2260 (defun gud-display-frame ()
2261 "Find and obey the last filename-and-line marker from the debugger.
2262 Obeying it means displaying in another window the specified file and line."
2267 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2268 (setq gud-last-last-frame gud-last-frame
2269 gud-last-frame nil))))
2271 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2272 ;; and that its line LINE is visible.
2273 ;; Put the overlay-arrow on the line LINE in that buffer.
2274 ;; Most of the trickiness in here comes from wanting to preserve the current
2275 ;; region-restriction if that's possible. We use an explicit display-buffer
2276 ;; to get around the fact that this is called inside a save-excursion.
2278 (defun gud-display-line (true-file line)
2279 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2282 (or (eq (current-buffer) gud-comint-buffer)
2283 (set-buffer gud-comint-buffer))
2284 (gud-find-file true-file)))
2285 (window (and buffer (or (get-buffer-window buffer)
2286 (display-buffer buffer))))
2296 (setq overlay-arrow-string "=>")
2297 (or overlay-arrow-position
2298 (setq overlay-arrow-position (make-marker)))
2299 (set-marker overlay-arrow-position (point) (current-buffer)))
2300 (cond ((or (< pos (point-min)) (> pos (point-max)))
2303 (set-window-point window overlay-arrow-position)))))
2305 ;;; The gud-call function must do the right thing whether its invoking
2306 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2307 ;;; or a C buffer. In the former case, we want to supply data from
2308 ;;; gud-last-frame. Here's how we do it:
2310 (defun gud-format-command (str arg)
2311 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2312 (frame (or gud-last-frame gud-last-last-frame))
2314 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2315 (let ((key (string-to-char (substring str (match-beginning 2))))
2319 (setq subst (file-name-nondirectory (if insource
2323 (setq subst (file-name-sans-extension
2324 (file-name-nondirectory (if insource
2328 (setq subst (file-name-directory (if insource
2332 (setq subst (if insource
2337 (int-to-string (1+ (count-lines 1 (point))))))
2340 (setq subst (gud-find-c-expr)))
2342 (setq subst (gud-read-address)))
2344 (setq subst (if arg (int-to-string arg)))))
2345 (setq result (concat result (match-string 1 str) subst)))
2346 (setq str (substring str (match-end 2))))
2347 ;; There might be text left in STR when the loop ends.
2348 (concat result str)))
2350 (defun gud-read-address ()
2351 "Return a string containing the core-address found in the buffer at point."
2353 (let ((pt (point)) found begin)
2354 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2356 (found (forward-char 2)
2357 (buffer-substring found
2358 (progn (re-search-forward "[^0-9a-f]")
2361 (t (setq begin (progn (re-search-backward "[^0-9]")
2365 (re-search-forward "[^0-9]")
2367 (buffer-substring begin (point)))))))
2369 (defun gud-call (fmt &optional arg)
2370 (let ((msg (gud-format-command fmt arg)))
2371 (message "Command: %s" msg)
2373 (gud-basic-call msg)))
2375 (defun gud-basic-call (command)
2376 "Invoke the debugger COMMAND displaying source in other window."
2379 (let ((command (concat command "\n"))
2380 (proc (get-buffer-process gud-comint-buffer)))
2381 (or proc (error "Current buffer has no process"))
2382 ;; Arrange for the current prompt to get deleted.
2384 (set-buffer gud-comint-buffer)
2385 (goto-char (process-mark proc))
2387 (if (looking-at comint-prompt-regexp)
2388 (set-marker gud-delete-prompt-marker (point))))
2389 (process-send-string proc command)))
2391 (defun gud-refresh (&optional arg)
2392 "Fix up a possibly garbled display, and redraw the arrow."
2395 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2396 (gud-display-frame))
2399 (defun gud-new-keymap (map)
2400 "Return a new keymap which inherits from MAP and has name `Gud'."
2401 (nconc (make-sparse-keymap "Gud") map))
2403 (defun gud-make-debug-menu ()
2404 "Make sure the current local map has a [menu-bar debug] submap.
2405 If it doesn't, replace it with a new map that inherits it,
2406 and create such a submap in that new map."
2407 (use-local-map (gud-new-keymap (current-local-map)))
2408 (define-key (current-local-map) [menu-bar]
2409 (gud-new-keymap (lookup-key (current-local-map) [menu-bar])))
2410 (define-key (current-local-map) [menu-bar debug]
2411 (cons "Gud" (gud-new-keymap gud-menu-map))))
2413 ;;; Code for parsing expressions out of C code. The single entry point is
2414 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2416 ;;; The rest of this file is a hacked version of gdbsrc.el by
2417 ;;; Debby Ayers <ayers@asc.slb.com>,
2418 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2420 (defun gud-find-c-expr ()
2421 "Returns the C expr that surrounds point."
2424 (let (p expr test-expr)
2426 (setq expr (gud-innermost-expr))
2427 (setq test-expr (gud-prev-expr))
2428 (while (and test-expr (gud-expr-compound test-expr expr))
2429 (let ((prev-expr expr))
2430 (setq expr (cons (car test-expr) (cdr expr)))
2431 (goto-char (car expr))
2432 (setq test-expr (gud-prev-expr))
2433 ;; If we just pasted on the condition of an if or while,
2434 ;; throw it away again.
2435 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2436 '("if" "while" "for"))
2440 (setq test-expr (gud-next-expr))
2441 (while (gud-expr-compound expr test-expr)
2442 (setq expr (cons (car expr) (cdr test-expr)))
2443 (setq test-expr (gud-next-expr))
2445 (buffer-substring (car expr) (cdr expr)))))
2447 (defun gud-innermost-expr ()
2448 "Returns the smallest expr that point is in; move point to beginning of it.
2449 The expr is represented as a cons cell, where the car specifies the point in
2450 the current buffer that marks the beginning of the expr and the cdr specifies
2451 the character after the end of the expr."
2452 (let ((p (point)) begin end)
2454 (setq begin (point))
2467 (defun gud-backward-sexp ()
2468 "Version of `backward-sexp' that catches errors."
2473 (defun gud-forward-sexp ()
2474 "Version of `forward-sexp' that catches errors."
2479 (defun gud-prev-expr ()
2480 "Returns the previous expr, point is set to beginning of that expr.
2481 The expr is represented as a cons cell, where the car specifies the point in
2482 the current buffer that marks the beginning of the expr and the cdr specifies
2483 the character after the end of the expr"
2484 (let ((begin) (end))
2486 (setq begin (point))
2492 (defun gud-next-expr ()
2493 "Returns the following expr, point is set to beginning of that expr.
2494 The expr is represented as a cons cell, where the car specifies the point in
2495 the current buffer that marks the beginning of the expr and the cdr specifies
2496 the character after the end of the expr."
2497 (let ((begin) (end))
2502 (setq begin (point))
2505 (defun gud-expr-compound-sep (span-start span-end)
2506 "Scan from SPAN-START to SPAN-END for punctuation characters.
2507 If `->' is found, return `?.'. If `.' is found, return `?.'.
2508 If any other punctuation is found, return `??'.
2509 If no punctuation is found, return `? '."
2512 (while (< span-start span-end)
2513 (setq syntax (char-syntax (char-after span-start)))
2516 ((= syntax ?.) (setq syntax (char-after span-start))
2518 ((= syntax ?.) (setq result ?.))
2519 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2521 (setq span-start (+ span-start 1)))
2522 (t (setq span-start span-end)
2523 (setq result ??)))))
2524 (setq span-start (+ span-start 1)))
2527 (defun gud-expr-compound (first second)
2528 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2529 The two exprs are represented as a cons cells, where the car
2530 specifies the point in the current buffer that marks the beginning of the
2531 expr and the cdr specifies the character after the end of the expr.
2532 Link exprs of the form:
2539 (let ((span-start (cdr first))
2540 (span-end (car second))
2542 (setq syntax (gud-expr-compound-sep span-start span-end))
2544 ((= (car first) (car second)) nil)
2545 ((= (cdr first) (cdr second)) nil)
2548 (setq span-start (char-after (- span-start 1)))
2549 (setq span-end (char-after span-end))
2551 ((= span-start ?)) t)
2552 ((= span-start ?]) t)
2560 ;;; gud.el ends here