(easy-menu-define): Docstring fix.
[emacs.git] / lisp / gud.el
blobc2e9994895494c38c6a04b62d34610ab7dbecb37
1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4 ;; Maintainer: FSF
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992, 93, 94, 95, 96, 1998 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38 ;; <ddavies@world.std.com> added support for jdb (Java debugger.)
40 ;;; Code:
42 (require 'comint)
43 (require 'etags)
45 ;; ======================================================================
46 ;; GUD commands must be visible in C buffers visited by GUD
48 (defgroup gud nil
49 "Grand Unified Debugger mode for gdb and other debuggers under Emacs.
50 Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), and jdb."
51 :group 'unix
52 :group 'tools)
55 (defcustom gud-key-prefix "\C-x\C-a"
56 "Prefix of all GUD commands valid in C buffers."
57 :type 'string
58 :group 'gud)
60 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
61 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
63 (defvar gud-marker-filter nil)
64 (put 'gud-marker-filter 'permanent-local t)
65 (defvar gud-find-file nil)
66 (put 'gud-find-file 'permanent-local t)
68 (defun gud-marker-filter (&rest args)
69 (apply gud-marker-filter args))
71 (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
79 ;; before use.)
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 ;; ======================================================================
95 ;; command definition
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
99 ;; user defined ones.
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
105 ;; GUD prefix.
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)."
126 (list 'progn
127 (list 'defun func '(arg)
128 (or doc "")
129 '(interactive "p")
130 (list 'gud-call cmd 'arg))
131 (if key
132 (list 'define-key
133 '(current-local-map)
134 (concat "\C-c" key)
135 (list 'quote func)))
136 (if key
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
148 ;; been set to nil.
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:
156 ;;<name>
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
170 ;; the rest.
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))
221 (frames
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)
228 nil))))
229 (erase-buffer)
230 (if (not frames)
231 (insert "No Stack frames\n")
232 (insert "Current Stack:\n"))
233 (while frames
234 (insert (nth 1 (car frames)) ":\n")
235 (if (= (length (car frames)) 2)
236 (progn
237 ; (speedbar-insert-button "[?]"
238 ; 'speedbar-button-face
239 ; nil nil nil t)
240 (speedbar-insert-button (car (car frames))
241 'speedbar-directory-face
242 nil nil nil t))
243 ; (speedbar-insert-button "[+]"
244 ; 'speedbar-button-face
245 ; 'speedbar-highlight-face
246 ; 'gud-gdb-get-scope-data
247 ; (car frames) t)
248 (speedbar-insert-button (car (car frames))
249 'speedbar-file-face
250 'speedbar-highlight-face
251 (cond ((eq ff 'gud-gdb-find-file)
252 'gud-gdb-goto-stackframe)
253 (t (error "Should never be here.")))
254 (car frames) t))
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 ;; ======================================================================
265 ;; gdb functions
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
285 ;; filter.
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))
291 (let ((output ""))
293 ;; Process all the complete markers in this chunk.
294 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
295 (setq
297 ;; Extract the frame position from the marker.
298 gud-last-frame
299 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
300 (string-to-int (substring gud-marker-acc
301 (match-beginning 2)
302 (match-end 2))))
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)
318 (progn
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.
324 (setq gud-marker-acc
325 (substring gud-marker-acc (match-beginning 0))))
327 (setq output (concat output gud-marker-acc)
328 gud-marker-acc ""))
330 output))
332 (defun gud-gdb-find-file (f)
333 (save-excursion
334 (let ((buf (find-file-noselect f 'nowarn)))
335 (set-buffer buf)
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))
342 buf)))
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))
349 (define-key
350 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
352 ;;;###autoload
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."
357 (interactive
358 (list (read-from-minibuffer "Run gdb (like this): "
359 (if (consp gud-gdb-history)
360 (car gud-gdb-history)
361 "gdb ")
362 gdb-minibuffer-local-map nil
363 'gud-gdb-history)))
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."
412 (interactive)
413 (let* ((end (point))
414 (command (save-excursion
415 (beginning-of-line)
416 (and (looking-at comint-prompt-regexp)
417 (goto-char (match-end 0)))
418 (buffer-substring (point) end)))
419 command-word)
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)
431 ;; Slurp the output.
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)))
445 (while second
446 (if (string-equal (car first) (car second))
447 (setcdr first (setq second (cdr second)))
448 (setq first 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))
454 (pos 0)
455 (count 0))
456 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
457 (setq count (1+ count)
458 pos (match-end 0)))
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)
474 (progn
475 (setq gud-gdb-complete-in-progress nil)
476 string)
477 (progn
478 (setq gud-gdb-complete-string string)
479 "")))
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)))
487 (sit-for 1)))
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")
504 ; ))
506 (defun gud-gdb-get-stackframe (buffer)
507 "Extract the current stack frame out of the GUD GDB BUFFER."
508 (let ((newlst nil)
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))
518 (if (not (or
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))
524 (setcar newlst
525 (list (nth 0 (car newlst))
526 (nth 1 (car newlst))
527 (match-string 1 e)
528 (match-string 2 e))))
529 (setq num (match-string 1 e)
530 name (match-string 2 e))
531 (setq newlst
532 (cons
533 (if (string-match
534 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
535 (list name num (match-string 1 e)
536 (match-string 2 e))
537 (list name num))
538 newlst))))
539 (setq gud-gdb-fetched-stack-frame-list
540 (cdr gud-gdb-fetched-stack-frame-list)))
541 (nreverse newlst))))
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."
550 (save-excursion
551 (set-buffer buffer)
552 (if (save-excursion
553 (goto-char (point-max))
554 (beginning-of-line)
555 (not (looking-at comint-prompt-regexp)))
557 ;; Much of this copied from GDB complete, but I'm grabbing the stack
558 ;; frame instead.
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)
565 ;; Slurp the output.
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)
582 (progn
583 (setq gud-gdb-complete-in-progress nil)
584 string)
585 (progn
586 (setq gud-gdb-complete-string string)
587 "")))
590 ;; ======================================================================
591 ;; sdb functions
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)
604 (setq gud-marker-acc
605 (if gud-marker-acc (concat gud-marker-acc string) string))
606 (let (start)
607 ;; Process all complete markers in this chunk
608 (while
609 (cond
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)
613 (setq gud-last-frame
614 (cons
615 (substring gud-marker-acc (match-beginning 3) (match-end 3))
616 (string-to-int
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)))
623 (setq gud-last-frame
624 (cons
625 gud-sdb-lastfile
626 (string-to-int
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))
635 (setq gud-last-frame
636 (cons
637 gud-sdb-lastfile
638 (string-to-int
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))))
650 string)
652 (defun gud-sdb-find-file (f)
653 (save-excursion
654 (let ((buf (if gud-sdb-needs-tags
655 (find-tag-noselect f)
656 (find-file-noselect f))))
657 (set-buffer buf)
658 (gud-make-debug-menu)
659 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
660 buf)))
662 ;;;###autoload
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."
667 (interactive
668 (list (read-from-minibuffer "Run sdb (like this): "
669 (if (consp gud-sdb-history)
670 (car gud-sdb-history)
671 "sdb ")
672 nil nil
673 '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 ;; ======================================================================
700 ;; dbx functions
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)
713 (repeat :value ("")
714 directory))
715 :group 'gud)
717 (defun gud-dbx-massage-args (file args)
718 (nconc (let ((directories gud-dbx-directories)
719 (result nil))
720 (while directories
721 (setq result (cons (car directories) (cons "-I" result)))
722 (setq directories (cdr directories)))
723 (nreverse result))
724 args))
726 (defun gud-dbx-file-name (f)
727 "Transform a relative file name to an absolute file name, for dbx."
728 (let ((result nil))
729 (if (file-exists-p f)
730 (setq result (expand-file-name f))
731 (let ((directories gud-dbx-directories))
732 (while directories
733 (let ((path (concat (car directories) "/" f)))
734 (if (file-exists-p path)
735 (setq result (expand-file-name path)
736 directories nil)))
737 (setq directories (cdr directories)))))
738 result))
740 (defun gud-dbx-marker-filter (string)
741 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
743 (let (start)
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)
748 (string-match
749 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
750 gud-marker-acc start))
751 (setq gud-last-frame
752 (cons
753 (substring gud-marker-acc (match-beginning 2) (match-end 2))
754 (string-to-int
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.
765 (setq gud-marker-acc
766 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
767 (substring gud-marker-acc (match-beginning 0))
768 nil)))
769 string)
771 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
772 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
773 (defvar gud-mips-p
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))
789 (let ((output ""))
791 ;; Process all the complete markers in this chunk.
792 (while (string-match
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"
796 gud-marker-acc)
797 (setq
799 ;; Extract the frame position from the marker.
800 gud-last-frame
801 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
802 (string-to-int (substring gud-marker-acc
803 (match-beginning 2)
804 (match-end 2))))
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)
820 (progn
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.
826 (setq gud-marker-acc
827 (substring gud-marker-acc (match-beginning 0))))
829 (setq output (concat output gud-marker-acc)
830 gud-marker-acc ""))
832 output))
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
850 (defvar gud-irix-p
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)
887 gud-marker-acc "")
888 (setq gud-marker-acc (concat gud-marker-acc string)))
889 (if result
890 (cond
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]
895 ((string-match
896 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
897 result)
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")
905 ((string-match
906 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
907 result)
908 (let ((file (substring result (match-beginning 1)
909 (match-end 1))))
910 (if (file-exists-p file)
911 (setq gud-last-frame
912 (cons
913 (substring
914 result (match-beginning 1) (match-end 1))
915 (string-to-int
916 (substring
917 result (match-beginning 2) (match-end 2)))))))
918 result)
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))
924 (setq gud-last-frame
925 (cons
926 file
927 (string-to-int
928 (substring
929 result (match-beginning 1) (match-end 1)))))))
930 (setq result (substring result 0 (match-beginning 0))))))
931 (or result "")))
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
945 ;; number), and
947 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
949 ;; from signals and
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)
958 string))
959 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
960 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
961 start)
962 ;; Process all complete markers in this chunk.
963 (while (string-match re gud-marker-acc start)
964 (setq gud-last-frame
965 (cons
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.
978 (setq gud-marker-acc
979 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
980 (substring gud-marker-acc (match-beginning 0))
981 nil)))
982 string)
984 (defun gud-dbx-find-file (f)
985 (save-excursion
986 (let ((realf (gud-dbx-file-name f)))
987 (if realf
988 (let ((buf (find-file-noselect realf)))
989 (set-buffer buf)
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))
993 buf)
994 nil))))
996 ;;;###autoload
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."
1001 (interactive
1002 (list (read-from-minibuffer "Run dbx (like this): "
1003 (if (consp gud-dbx-history)
1004 (car gud-dbx-history)
1005 "dbx ")
1006 nil nil
1007 'gud-dbx-history)))
1009 (cond
1010 (gud-mips-p
1011 (gud-common-init command-line 'gud-mipsdbx-massage-args
1012 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
1013 (gud-irix-p
1014 (gud-common-init command-line 'gud-dbx-massage-args
1015 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
1016 (gud-dgux-p
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)))
1023 (cond
1024 (gud-mips-p
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."))
1030 (gud-irix-p
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)
1078 (repeat :value ("")
1079 directory))
1080 :group 'gud)
1082 (defun gud-xdb-massage-args (file args)
1083 (nconc (let ((directories gud-xdb-directories)
1084 (result nil))
1085 (while directories
1086 (setq result (cons (car directories) (cons "-d" result)))
1087 (setq directories (cdr directories)))
1088 (nreverse result))
1089 args))
1091 (defun gud-xdb-file-name (f)
1092 "Transform a relative pathname to a full pathname in xdb mode"
1093 (let ((result nil))
1094 (if (file-exists-p f)
1095 (setq result (expand-file-name f))
1096 (let ((directories gud-xdb-directories))
1097 (while directories
1098 (let ((path (concat (car directories) "/" f)))
1099 (if (file-exists-p path)
1100 (setq result (expand-file-name path)
1101 directories nil)))
1102 (setq directories (cdr directories)))))
1103 result))
1105 ;; xdb does not print the lines all at once, so we have to accumulate them
1106 (defun gud-xdb-marker-filter (string)
1107 (let (result)
1108 (if (or (string-match comint-prompt-regexp string)
1109 (string-match ".*\012" string))
1110 (setq result (concat gud-marker-acc string)
1111 gud-marker-acc "")
1112 (setq gud-marker-acc (concat gud-marker-acc string)))
1113 (if result
1114 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1115 result)
1116 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1117 result))
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)))))
1122 (if file
1123 (setq gud-last-frame (cons file line))))))
1124 (or result "")))
1126 (defun gud-xdb-find-file (f)
1127 (save-excursion
1128 (let ((realf (gud-xdb-file-name f)))
1129 (if realf
1130 (let ((buf (find-file-noselect realf)))
1131 (set-buffer buf)
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))
1139 buf)
1140 nil))))
1142 ;;;###autoload
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."
1150 (interactive
1151 (list (read-from-minibuffer "Run xdb (like this): "
1152 (if (consp gud-xdb-history)
1153 (car gud-xdb-history)
1154 "xdb ")
1155 nil nil
1156 '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 ;; ======================================================================
1182 ;; perldb functions
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"))
1193 (seen-e nil)
1194 (shift (lambda ()
1195 (setq new-args (cons (car args) new-args))
1196 (setq args (cdr args)))))
1198 ;; Pass all switches and -e scripts through.
1199 (while (and args
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.
1205 (or (funcall shift)
1206 ;; -e as the last arg is an error in Perl.
1207 (error "No code specified for -e."))
1208 (setq seen-e t))
1209 (funcall shift))
1211 (when (not seen-e)
1212 (if (or (not args)
1213 (string-match "^-" (car args)))
1214 (error "Can't use stdin as the script to debug."))
1215 ;; This is the program name.
1216 (funcall shift))
1218 ;; If -e specified, make sure there is a -- so -emacs is not taken
1219 ;; as -e macs.
1220 (if (and args (equal "--" (car args)))
1221 (funcall shift)
1222 (and seen-e (setq new-args (cons "--" new-args))))
1224 (setq new-args (cons "-emacs" new-args))
1225 (while args
1226 (funcall shift))
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
1235 ;; filter.
1236 (defun gud-perldb-marker-filter (string)
1237 (setq gud-marker-acc (concat gud-marker-acc string))
1238 (let ((output ""))
1240 ;; Process all the complete markers in this chunk.
1241 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1242 gud-marker-acc)
1243 (setq
1245 ;; Extract the frame position from the marker.
1246 gud-last-frame
1247 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1248 (string-to-int (substring gud-marker-acc
1249 (match-beginning 3)
1250 (match-end 3))))
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)
1266 (progn
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)
1276 gud-marker-acc ""))
1278 output))
1280 (defun gud-perldb-find-file (f)
1281 (save-excursion
1282 (let ((buf (find-file-noselect f)))
1283 (set-buffer buf)
1284 (gud-make-debug-menu)
1285 buf)))
1287 (defcustom gud-perldb-command-name "perl"
1288 "File name for executing Perl."
1289 :type 'string
1290 :group 'gud)
1292 ;;;###autoload
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."
1297 (interactive
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)
1304 "-e 0")
1305 " "))
1306 nil nil
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)
1334 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
1351 ;; filter.
1352 (defun gud-pdb-marker-filter (string)
1353 (setq gud-marker-acc (concat gud-marker-acc string))
1354 (let ((output ""))
1356 ;; Process all the complete markers in this chunk.
1357 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1358 (setq
1360 ;; Extract the frame position from the marker.
1361 gud-last-frame
1362 (let ((file (match-string gud-pdb-marker-regexp-file-group
1363 gud-marker-acc))
1364 (line (string-to-int
1365 (match-string gud-pdb-marker-regexp-line-group
1366 gud-marker-acc))))
1367 (if (string-equal file "<string>")
1368 gud-last-frame
1369 (cons file line)))
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)
1387 (progn
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)
1397 gud-marker-acc ""))
1399 output))
1401 (defun gud-pdb-find-file (f)
1402 (save-excursion
1403 (let ((buf (find-file-noselect f)))
1404 (set-buffer buf)
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))
1409 buf)))
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))
1416 (define-key
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."
1422 :type 'string
1423 :group 'gud)
1425 ;;;###autoload
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."
1430 (interactive
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
1436 'gud-pdb-history)))
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.")
1450 ;; Is this right?
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 ;; ======================================================================
1463 ;; JDB support.
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:
1473 ;; M-x jdb <enter>
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)
1490 ;; >
1492 ;; To set an initial breakpoint try:
1494 ;; > stop in hello.main
1495 ;; Breakpoint set in hello.main
1496 ;; >
1498 ;; To execute the program type:
1500 ;; > run
1501 ;; run hello
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
1549 directory.
1551 The set of .java files residing in the directories listed are
1552 syntactically analyzed to determine the classes they define and the
1553 packages in which these classes belong. In this way gud jdb maps the
1554 package-qualified class names output by the jdb debugger to the source
1555 file from which the class originated. This allows gud mode to keep
1556 the source code display in sync with the debugging session.")
1558 ;; List of the java source files for this debugging session.
1559 (defvar gud-jdb-source-files nil)
1561 ;; Association list of fully qualified class names (package + class name) and
1562 ;; their source files.
1563 (defvar gud-jdb-class-source-alist nil)
1565 ;; This is used to hold a source file during analysis.
1566 (defvar gud-jdb-analysis-buffer nil)
1568 ;; Return a list of java source files. PATH gives the directories in
1569 ;; which to search for files with extension EXTN. Normally EXTN is
1570 ;; given as the regular expression "\\.java$" .
1571 (defun gud-jdb-build-source-files-list (path extn)
1572 (apply 'nconc (mapcar (lambda (d)
1573 (when (file-directory-p d)
1574 (directory-files d t extn nil)) path))))
1576 ;; Move point past whitespace.
1577 (defun gud-jdb-skip-whitespace ()
1578 (skip-chars-forward " \n\r\t\014"))
1580 ;; Move point past a "// <eol>" type of comment.
1581 (defun gud-jdb-skip-single-line-comment ()
1582 (end-of-line))
1584 ;; Move point past a "/* */" or "/** */" type of comment.
1585 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1586 (forward-char 2)
1587 (catch 'break
1588 (while (not (eobp))
1589 (if (eq (following-char) ?*)
1590 (progn
1591 (forward-char)
1592 (if (not (eobp))
1593 (if (eq (following-char) ?/)
1594 (progn
1595 (forward-char)
1596 (throw 'break nil)))))
1597 (forward-char)))))
1599 ;; Move point past any number of consecutive whitespace chars and/or comments.
1600 (defun gud-jdb-skip-whitespace-and-comments ()
1601 (gud-jdb-skip-whitespace)
1602 (catch 'done
1603 (while t
1604 (cond
1605 ((looking-at "//")
1606 (gud-jdb-skip-single-line-comment)
1607 (gud-jdb-skip-whitespace))
1608 ((looking-at "/\\*")
1609 (gud-jdb-skip-traditional-or-documentation-comment)
1610 (gud-jdb-skip-whitespace))
1611 (t (throw 'done nil))))))
1613 ;; Move point past things that are id-like. The intent is to skip regular
1614 ;; id's, such as class or interface names as well as package and interface
1615 ;; names.
1616 (defun gud-jdb-skip-id-ish-thing ()
1617 (skip-chars-forward "^ /\n\r\t\014,;{"))
1619 ;; Move point past a string literal.
1620 (defun gud-jdb-skip-string-literal ()
1621 (forward-char)
1622 (while (not (cond
1623 ((eq (following-char) ?\\)
1624 (forward-char))
1625 ((eq (following-char) ?\042))))
1626 (forward-char))
1627 (forward-char))
1629 ;; Move point past a character literal.
1630 (defun gud-jdb-skip-character-literal ()
1631 (forward-char)
1632 (while
1633 (progn
1634 (if (eq (following-char) ?\\)
1635 (forward-char 2))
1636 (not (eq (following-char) ?\')))
1637 (forward-char))
1638 (forward-char))
1640 ;; Move point past the following block. There may be (legal) cruft before
1641 ;; the block's opening brace. There must be a block or it's the end of life
1642 ;; in petticoat junction.
1643 (defun gud-jdb-skip-block ()
1645 ;; Find the begining of the block.
1646 (while
1647 (not (eq (following-char) ?{))
1649 ;; Skip any constructs that can harbor literal block delimiter
1650 ;; characters and/or the delimiters for the constructs themselves.
1651 (cond
1652 ((looking-at "//")
1653 (gud-jdb-skip-single-line-comment))
1654 ((looking-at "/\\*")
1655 (gud-jdb-skip-traditional-or-documentation-comment))
1656 ((eq (following-char) ?\042)
1657 (gud-jdb-skip-string-literal))
1658 ((eq (following-char) ?\')
1659 (gud-jdb-skip-character-literal))
1660 (t (forward-char))))
1662 ;; Now at the begining of the block.
1663 (forward-char)
1665 ;; Skip over the body of the block as well as the final brace.
1666 (let ((open-level 1))
1667 (while (not (eq open-level 0))
1668 (cond
1669 ((looking-at "//")
1670 (gud-jdb-skip-single-line-comment))
1671 ((looking-at "/\\*")
1672 (gud-jdb-skip-traditional-or-documentation-comment))
1673 ((eq (following-char) ?\042)
1674 (gud-jdb-skip-string-literal))
1675 ((eq (following-char) ?\')
1676 (gud-jdb-skip-character-literal))
1677 ((eq (following-char) ?{)
1678 (setq open-level (+ open-level 1))
1679 (forward-char))
1680 ((eq (following-char) ?})
1681 (setq open-level (- open-level 1))
1682 (forward-char))
1683 (t (forward-char))))))
1685 ;; Find the package and class definitions in Java source file FILE. Assumes
1686 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1687 ;; to hold the source during analysis.
1688 (defun gud-jdb-analyze-source (buf file)
1689 (let ((l nil))
1690 (set-buffer buf)
1691 (insert-file-contents file nil nil nil t)
1692 (goto-char 0)
1693 (catch 'abort
1694 (let ((p ""))
1695 (while (progn
1696 (gud-jdb-skip-whitespace)
1697 (not (eobp)))
1698 (cond
1700 ;; Any number of semi's following a block is legal. Move point
1701 ;; past them. Note that comments and whitespace may be
1702 ;; interspersed as well.
1703 ((eq (following-char) ?\073)
1704 (forward-char))
1706 ;; Move point past a single line comment.
1707 ((looking-at "//")
1708 (gud-jdb-skip-single-line-comment))
1710 ;; Move point past a traditional or documentation comment.
1711 ((looking-at "/\\*")
1712 (gud-jdb-skip-traditional-or-documentation-comment))
1714 ;; Move point past a package statement, but save the PackageName.
1715 ((looking-at "package")
1716 (forward-char 7)
1717 (gud-jdb-skip-whitespace-and-comments)
1718 (let ((s (point)))
1719 (gud-jdb-skip-id-ish-thing)
1720 (setq p (concat (buffer-substring s (point)) "."))
1721 (gud-jdb-skip-whitespace-and-comments)
1722 (if (eq (following-char) ?\073)
1723 (forward-char))))
1725 ;; Move point past an import statement.
1726 ((looking-at "import")
1727 (forward-char 6)
1728 (gud-jdb-skip-whitespace-and-comments)
1729 (gud-jdb-skip-id-ish-thing)
1730 (gud-jdb-skip-whitespace-and-comments)
1731 (if (eq (following-char) ?\073)
1732 (forward-char)))
1734 ;; Move point past the various kinds of ClassModifiers.
1735 ((looking-at "public")
1736 (forward-char 6))
1737 ((looking-at "abstract")
1738 (forward-char 8))
1739 ((looking-at "final")
1740 (forward-char 5))
1742 ;; Move point past a ClassDeclaraction, but save the class
1743 ;; Identifier.
1744 ((looking-at "class")
1745 (forward-char 5)
1746 (gud-jdb-skip-whitespace-and-comments)
1747 (let ((s (point)))
1748 (gud-jdb-skip-id-ish-thing)
1749 (setq
1750 l (nconc l (list (concat p (buffer-substring s (point)))))))
1751 (gud-jdb-skip-block))
1753 ;; Move point past an interface statement.
1754 ((looking-at "interface")
1755 (forward-char 9)
1756 (gud-jdb-skip-block))
1758 ;; Anything else means the input is invalid.
1760 (message (format "Error parsing file %s." file))
1761 (throw 'abort nil))))))
1764 (defun gud-jdb-build-class-source-alist-for-file (file)
1765 (mapcar
1766 (lambda (c)
1767 (cons c file))
1768 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1770 ;; Return an alist of fully qualified classes and the source files
1771 ;; holding their definitions. SOURCES holds a list of all the source
1772 ;; files to examine.
1773 (defun gud-jdb-build-class-source-alist (sources)
1774 (setq gud-jdb-analysis-buffer (get-buffer-create " *gud-jdb-scratch*"))
1775 (prog1
1776 (apply
1777 'nconc
1778 (mapcar
1779 'gud-jdb-build-class-source-alist-for-file
1780 sources))
1781 (kill-buffer gud-jdb-analysis-buffer)
1782 (setq gud-jdb-analysis-buffer nil)))
1784 ;; Change what was given in the minibuffer to something that can be used to
1785 ;; invoke the debugger.
1786 (defun gud-jdb-massage-args (file args)
1787 ;; The jdb executable must have whitespace between "-classpath" and
1788 ;; its value while gud-common-init expects all switch values to
1789 ;; follow the switch keyword without intervening whitespace. We
1790 ;; require that when the user enters the "-classpath" switch in the
1791 ;; EMACS minibuffer that they do so without the intervening
1792 ;; whitespace. This function adds it back (it's called after
1793 ;; gud-common-init). There are more switches like this (for
1794 ;; instance "-host" and "-password") but I don't care about them
1795 ;; yet.
1796 (if args
1797 (let (massaged-args user-error)
1799 (while
1800 (and args
1801 (not (string-match "-classpath\\(.+\\)" (car args)))
1802 (not (setq user-error
1803 (string-match "-classpath$" (car args)))))
1804 (setq massaged-args (append massaged-args (list (car args))))
1805 (setq args (cdr args)))
1807 ;; By this point the current directory is all screwed up. Maybe we
1808 ;; could fix things and re-invoke gud-common-init, but for now I think
1809 ;; issueing the error is good enough.
1810 (if user-error
1811 (progn
1812 (kill-buffer (current-buffer))
1813 (error "Error: Omit whitespace between '-classpath' and its value")))
1815 (if args
1816 (setq massaged-args
1817 (append
1818 massaged-args
1819 (list "-classpath")
1820 (list
1821 (substring
1822 (car args)
1823 (match-beginning 1) (match-end 1)))
1824 (cdr args)))
1825 massaged-args))))
1827 ;; Search for an association with P, a fully qualified class name, in
1828 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1829 ;; qualified file name of the source file which produced the class.
1830 (defun gud-jdb-find-source-file (p)
1831 (cdr (assoc p gud-jdb-class-source-alist)))
1833 ;; See comentary for other debugger's marker filters - there you will find
1834 ;; important notes about STRING.
1835 (defun gud-jdb-marker-filter (string)
1837 ;; Build up the accumulator.
1838 (setq gud-marker-acc
1839 (if gud-marker-acc
1840 (concat gud-marker-acc string)
1841 string))
1843 ;; We process STRING from left to right. Each time through the following
1844 ;; loop we process at most one marker. The start variable keeps track of
1845 ;; where we are in the input string through the iterations of this loop.
1846 (let (start file-found)
1848 ;; Process each complete marker in the input. There may be an incomplete
1849 ;; marker at the end of the input string. Incomplete markers are left
1850 ;; in the accumulator for processing the next time the function is called.
1851 (while
1853 ;; Do we see a marker?
1854 (string-match
1855 ;; jdb puts out a string of the following form when it
1856 ;; hits a breakpoint:
1858 ;; <fully-qualified-class><method> (<class>:<line-number>)
1860 ;; <fully-qualified-class>'s are composed of Java ID's
1861 ;; separated by periods. <method> and <class> are
1862 ;; also Java ID's. <method> begins with a period and
1863 ;; may contain less-than and greater-than (constructors,
1864 ;; for instance, are called <init> in the symbol table.)
1865 ;; Java ID's begin with a letter followed by letters
1866 ;; and/or digits. The set of letters includes underscore
1867 ;; and dollar sign.
1869 ;; The first group matches <fully-qualified-class>,
1870 ;; the second group matches <class> and the third group
1871 ;; matches <line-number>. We don't care about using
1872 ;; <method> so we don't "group" it.
1874 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1875 ;; ID's only.
1876 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1877 gud-marker-acc start)
1879 ;; Figure out the line on which to position the debugging arrow.
1880 ;; Return the info as a cons of the form:
1882 ;; (<file-name> . <line-number>) .
1883 (if (setq
1884 file-found
1885 (gud-jdb-find-source-file
1886 (substring gud-marker-acc
1887 (match-beginning 1)
1888 (match-end 1))))
1889 (setq gud-last-frame
1890 (cons
1891 file-found
1892 (string-to-int
1893 (substring gud-marker-acc
1894 (match-beginning 3)
1895 (match-end 3)))))
1896 (message "Could not find source file."))
1898 ;; Set start after the last character of STRING that we've looked at
1899 ;; and loop to look for another marker.
1900 (setq start (match-end 0))))
1902 ;; We don't filter any debugger output so just return what we were given.
1903 string)
1905 (defun gud-jdb-find-file (f)
1906 (and (file-readable-p f)
1907 (find-file-noselect f)))
1909 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1911 ;;;###autoload
1912 (defun jdb (command-line)
1913 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1914 \"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1915 if there is. If the \"-classpath\" switch is given, omit all whitespace
1916 between it and it's value."
1917 (interactive
1918 (list (read-from-minibuffer "Run jdb (like this): "
1919 (if (consp gud-jdb-history)
1920 (car gud-jdb-history)
1921 (concat gud-jdb-command-name " "))
1922 nil nil
1923 'gud-jdb-history)))
1925 (gud-common-init command-line 'gud-jdb-massage-args
1926 'gud-jdb-marker-filter 'gud-jdb-find-file)
1928 (gud-def gud-break "stop at %F:%l" "\C-b" "Set breakpoint at current line.")
1929 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1930 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1931 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1932 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1934 (setq comint-prompt-regexp "^> \\|^.+\\[[0-9]+\\] ")
1935 (setq paragraph-start comint-prompt-regexp)
1936 (run-hooks 'jdb-mode-hook)
1938 ;; Create and bind the class/source association list as well as the source
1939 ;; file list.
1940 (setq
1941 gud-jdb-class-source-alist
1942 (gud-jdb-build-class-source-alist
1943 (setq
1944 gud-jdb-source-files
1945 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1949 ;; End of debugger-specific information
1953 ;;; When we send a command to the debugger via gud-call, it's annoying
1954 ;;; to see the command and the new prompt inserted into the debugger's
1955 ;;; buffer; we have other ways of knowing the command has completed.
1957 ;;; If the buffer looks like this:
1958 ;;; --------------------
1959 ;;; (gdb) set args foo bar
1960 ;;; (gdb) -!-
1961 ;;; --------------------
1962 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1963 ;;; source file to set a breakpoint, we want the buffer to end up like
1964 ;;; this:
1965 ;;; --------------------
1966 ;;; (gdb) set args foo bar
1967 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1968 ;;; (gdb) -!-
1969 ;;; --------------------
1970 ;;; Essentially, the old prompt is deleted, and the command's output
1971 ;;; and the new prompt take its place.
1973 ;;; Not echoing the command is easy enough; you send it directly using
1974 ;;; process-send-string, and it never enters the buffer. However,
1975 ;;; getting rid of the old prompt is trickier; you don't want to do it
1976 ;;; when you send the command, since that will result in an annoying
1977 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1978 ;;; waits for a response from the debugger, and the new prompt is
1979 ;;; inserted. Instead, we'll wait until we actually get some output
1980 ;;; from the subprocess before we delete the prompt. If the command
1981 ;;; produced no output other than a new prompt, that prompt will most
1982 ;;; likely be in the first chunk of output received, so we will delete
1983 ;;; the prompt and then replace it with an identical one. If the
1984 ;;; command produces output, the prompt is moving anyway, so the
1985 ;;; flicker won't be annoying.
1987 ;;; So - when we want to delete the prompt upon receipt of the next
1988 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1989 ;;; the start of the prompt; the process filter will notice this, and
1990 ;;; delete all text between it and the process output marker. If
1991 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1992 ;;; prompt alone.
1993 (defvar gud-delete-prompt-marker nil)
1996 (put 'gud-mode 'mode-class 'special)
1998 (defun gud-mode ()
1999 "Major mode for interacting with an inferior debugger process.
2001 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
2002 M-x perldb, or M-x xdb. Each entry point finishes by executing a
2003 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
2004 `perldb-mode-hook', or `xdb-mode-hook' respectively.
2006 After startup, the following commands are available in both the GUD
2007 interaction buffer and any source buffer GUD visits due to a breakpoint stop
2008 or step operation:
2010 \\[gud-break] sets a breakpoint at the current file and line. In the
2011 GUD buffer, the current file and line are those of the last breakpoint or
2012 step. In a source buffer, they are the buffer's file and current line.
2014 \\[gud-remove] removes breakpoints on the current file and line.
2016 \\[gud-refresh] displays in the source window the last line referred to
2017 in the gud buffer.
2019 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
2020 step-one-line (not entering function calls), and step-one-instruction
2021 and then update the source window with the current file and position.
2022 \\[gud-cont] continues execution.
2024 \\[gud-print] tries to find the largest C lvalue or function-call expression
2025 around point, and sends it to the debugger for value display.
2027 The above commands are common to all supported debuggers except xdb which
2028 does not support stepping instructions.
2030 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
2031 except that the breakpoint is temporary; that is, it is removed when
2032 execution stops on it.
2034 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
2035 frame. \\[gud-down] drops back down through one.
2037 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
2038 the current function and stops.
2040 All the keystrokes above are accessible in the GUD buffer
2041 with the prefix C-c, and in all buffers through the prefix C-x C-a.
2043 All pre-defined functions for which the concept make sense repeat
2044 themselves the appropriate number of times if you give a prefix
2045 argument.
2047 You may use the `gud-def' macro in the initialization hook to define other
2048 commands.
2050 Other commands for interacting with the debugger process are inherited from
2051 comint mode, which see."
2052 (interactive)
2053 (comint-mode)
2054 (setq major-mode 'gud-mode)
2055 (setq mode-name "Debugger")
2056 (setq mode-line-process '(":%s"))
2057 (use-local-map comint-mode-map)
2058 (gud-make-debug-menu)
2059 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
2060 (make-local-variable 'gud-last-frame)
2061 (setq gud-last-frame nil)
2062 (make-local-variable 'comint-prompt-regexp)
2063 ;; Don't put repeated commands in command history many times.
2064 (make-local-variable 'comint-input-ignoredups)
2065 (setq comint-input-ignoredups t)
2066 (make-local-variable 'paragraph-start)
2067 (make-local-variable 'gud-delete-prompt-marker)
2068 (setq gud-delete-prompt-marker (make-marker))
2069 (run-hooks 'gud-mode-hook))
2071 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
2072 (defun gud-chop-words (string)
2073 (let ((i 0) (beg 0)
2074 (len (length string))
2075 (words nil))
2076 (while (< i len)
2077 (if (memq (aref string i) '(?\t ? ))
2078 (progn
2079 (setq words (cons (substring string beg i) words)
2080 beg (1+ i))
2081 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2082 (setq beg (1+ beg)))
2083 (setq i (1+ beg)))
2084 (setq i (1+ i))))
2085 (if (< beg len)
2086 (setq words (cons (substring string beg) words)))
2087 (nreverse words)))
2089 ;; Cause our buffers to be displayed, by default,
2090 ;; in the selected window.
2091 ;;;###autoload (add-hook 'same-window-regexps "\\*gud-.*\\*\\(\\|<[0-9]+>\\)")
2093 ;; Perform initializations common to all debuggers.
2094 ;; The first arg is the specified command line,
2095 ;; which starts with the program to debug.
2096 ;; The other three args specify the values to use
2097 ;; for local variables in the debugger buffer.
2098 (defun gud-common-init (command-line massage-args marker-filter find-file)
2099 (let* ((words (gud-chop-words command-line))
2100 (program (car words))
2101 ;; Extract the file name from WORDS
2102 ;; and put t in its place.
2103 ;; Later on we will put the modified file name arg back there.
2104 (file-word (let ((w (cdr words)))
2105 (while (and w (= ?- (aref (car w) 0)))
2106 (setq w (cdr w)))
2107 (and w
2108 (prog1 (car w)
2109 (setcar w t)))))
2110 (file-subst
2111 (and file-word (substitute-in-file-name file-word)))
2112 (args (cdr words))
2113 ;; If a directory was specified, expand the file name.
2114 ;; Otherwise, don't expand it, so GDB can use the PATH.
2115 ;; A file name without directory is literally valid
2116 ;; only if the file exists in ., and in that case,
2117 ;; omitting the expansion here has no visible effect.
2118 (file (and file-word
2119 (if (file-name-directory file-subst)
2120 (expand-file-name file-subst)
2121 file-subst)))
2122 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2123 (pop-to-buffer (concat "*gud" filepart "*"))
2124 ;; Set default-directory to the file's directory.
2125 (and file-word
2126 ;; Don't set default-directory if no directory was specified.
2127 ;; In that case, either the file is found in the current directory,
2128 ;; in which case this setq is a no-op,
2129 ;; or it is found by searching PATH,
2130 ;; in which case we don't know what directory it was found in.
2131 (file-name-directory file)
2132 (setq default-directory (file-name-directory file)))
2133 (or (bolp) (newline))
2134 (insert "Current directory is " default-directory "\n")
2135 ;; Put the substituted and expanded file name back in its place.
2136 (let ((w args))
2137 (while (and w (not (eq (car w) t)))
2138 (setq w (cdr w)))
2139 (if w
2140 (setcar w file)))
2141 (apply 'make-comint (concat "gud" filepart) program nil
2142 (funcall massage-args file args)))
2143 ;; Since comint clobbered the mode, we don't set it until now.
2144 (gud-mode)
2145 (make-local-variable 'gud-marker-filter)
2146 (setq gud-marker-filter marker-filter)
2147 (make-local-variable 'gud-find-file)
2148 (setq gud-find-file find-file)
2150 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2151 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2152 (gud-set-buffer)
2155 (defun gud-set-buffer ()
2156 (cond ((eq major-mode 'gud-mode)
2157 (setq gud-comint-buffer (current-buffer)))))
2159 (defvar gud-filter-defer-flag nil
2160 "Non-nil means don't process anything from the debugger right now.
2161 It is saved for when this flag is not set.")
2163 (defvar gud-filter-pending-text nil
2164 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2166 ;; These functions are responsible for inserting output from your debugger
2167 ;; into the buffer. The hard work is done by the method that is
2168 ;; the value of gud-marker-filter.
2170 (defun gud-filter (proc string)
2171 ;; Here's where the actual buffer insertion is done
2172 (let (output process-window)
2173 (if (buffer-name (process-buffer proc))
2174 (if gud-filter-defer-flag
2175 ;; If we can't process any text now,
2176 ;; save it for later.
2177 (setq gud-filter-pending-text
2178 (concat (or gud-filter-pending-text "") string))
2180 ;; If we have to ask a question during the processing,
2181 ;; defer any additional text that comes from the debugger
2182 ;; during that time.
2183 (let ((gud-filter-defer-flag t))
2184 ;; Process now any text we previously saved up.
2185 (if gud-filter-pending-text
2186 (setq string (concat gud-filter-pending-text string)
2187 gud-filter-pending-text nil))
2188 (save-excursion
2189 (set-buffer (process-buffer proc))
2190 ;; If we have been so requested, delete the debugger prompt.
2191 (if (marker-buffer gud-delete-prompt-marker)
2192 (progn
2193 (delete-region (process-mark proc) gud-delete-prompt-marker)
2194 (set-marker gud-delete-prompt-marker nil)))
2195 ;; Save the process output, checking for source file markers.
2196 (setq output (gud-marker-filter string))
2197 ;; Check for a filename-and-line number.
2198 ;; Don't display the specified file
2199 ;; unless (1) point is at or after the position where output appears
2200 ;; and (2) this buffer is on the screen.
2201 (setq process-window
2202 (and gud-last-frame
2203 (>= (point) (process-mark proc))
2204 (get-buffer-window (current-buffer))))
2206 ;; Let the comint filter do the actual insertion.
2207 ;; That lets us inherit various comint features.
2208 (comint-output-filter proc output))
2210 ;; Put the arrow on the source line.
2211 ;; This must be outside of the save-excursion
2212 ;; in case the source file is our current buffer.
2213 (if process-window
2214 (save-selected-window
2215 (select-window process-window)
2216 (gud-display-frame))
2217 ;; We have to be in the proper buffer, (process-buffer proc),
2218 ;; but not in a save-excursion, because that would restore point.
2219 (let ((old-buf (current-buffer)))
2220 (set-buffer (process-buffer proc))
2221 (unwind-protect
2222 (gud-display-frame)
2223 (set-buffer old-buf)))))
2225 ;; If we deferred text that arrived during this processing,
2226 ;; handle it now.
2227 (if gud-filter-pending-text
2228 (gud-filter proc ""))))))
2230 (defun gud-sentinel (proc msg)
2231 (cond ((null (buffer-name (process-buffer proc)))
2232 ;; buffer killed
2233 ;; Stop displaying an arrow in a source file.
2234 (setq overlay-arrow-position nil)
2235 (set-process-buffer proc nil))
2236 ((memq (process-status proc) '(signal exit))
2237 ;; Stop displaying an arrow in a source file.
2238 (setq overlay-arrow-position nil)
2239 (let* ((obuf (current-buffer)))
2240 ;; save-excursion isn't the right thing if
2241 ;; process-buffer is current-buffer
2242 (unwind-protect
2243 (progn
2244 ;; Write something in *compilation* and hack its mode line,
2245 (set-buffer (process-buffer proc))
2246 ;; Fix the mode line.
2247 (setq mode-line-process
2248 (concat ":"
2249 (symbol-name (process-status proc))))
2250 (force-mode-line-update)
2251 (if (eobp)
2252 (insert ?\n mode-name " " msg)
2253 (save-excursion
2254 (goto-char (point-max))
2255 (insert ?\n mode-name " " msg)))
2256 ;; If buffer and mode line will show that the process
2257 ;; is dead, we can delete it now. Otherwise it
2258 ;; will stay around until M-x list-processes.
2259 (delete-process proc))
2260 ;; Restore old buffer, but don't restore old point
2261 ;; if obuf is the gud buffer.
2262 (set-buffer obuf))))))
2264 (defun gud-display-frame ()
2265 "Find and obey the last filename-and-line marker from the debugger.
2266 Obeying it means displaying in another window the specified file and line."
2267 (interactive)
2268 (if gud-last-frame
2269 (progn
2270 (gud-set-buffer)
2271 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2272 (setq gud-last-last-frame gud-last-frame
2273 gud-last-frame nil))))
2275 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2276 ;; and that its line LINE is visible.
2277 ;; Put the overlay-arrow on the line LINE in that buffer.
2278 ;; Most of the trickiness in here comes from wanting to preserve the current
2279 ;; region-restriction if that's possible. We use an explicit display-buffer
2280 ;; to get around the fact that this is called inside a save-excursion.
2282 (defun gud-display-line (true-file line)
2283 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2284 (buffer
2285 (save-excursion
2286 (or (eq (current-buffer) gud-comint-buffer)
2287 (set-buffer gud-comint-buffer))
2288 (gud-find-file true-file)))
2289 (window (and buffer (or (get-buffer-window buffer)
2290 (display-buffer buffer))))
2291 (pos))
2292 (if buffer
2293 (progn
2294 (save-excursion
2295 (set-buffer buffer)
2296 (save-restriction
2297 (widen)
2298 (goto-line line)
2299 (setq pos (point))
2300 (setq overlay-arrow-string "=>")
2301 (or overlay-arrow-position
2302 (setq overlay-arrow-position (make-marker)))
2303 (set-marker overlay-arrow-position (point) (current-buffer)))
2304 (cond ((or (< pos (point-min)) (> pos (point-max)))
2305 (widen)
2306 (goto-char pos))))
2307 (set-window-point window overlay-arrow-position)))))
2309 ;;; The gud-call function must do the right thing whether its invoking
2310 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2311 ;;; or a C buffer. In the former case, we want to supply data from
2312 ;;; gud-last-frame. Here's how we do it:
2314 (defun gud-format-command (str arg)
2315 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2316 (frame (or gud-last-frame gud-last-last-frame))
2317 result)
2318 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2319 (let ((key (string-to-char (substring str (match-beginning 2))))
2320 subst)
2321 (cond
2322 ((eq key ?f)
2323 (setq subst (file-name-nondirectory (if insource
2324 (buffer-file-name)
2325 (car frame)))))
2326 ((eq key ?F)
2327 (setq subst (file-name-sans-extension
2328 (file-name-nondirectory (if insource
2329 (buffer-file-name)
2330 (car frame))))))
2331 ((eq key ?d)
2332 (setq subst (file-name-directory (if insource
2333 (buffer-file-name)
2334 (car frame)))))
2335 ((eq key ?l)
2336 (setq subst (if insource
2337 (save-excursion
2338 (beginning-of-line)
2339 (save-restriction
2340 (widen)
2341 (int-to-string (1+ (count-lines 1 (point))))))
2342 (cdr frame))))
2343 ((eq key ?e)
2344 (setq subst (gud-find-c-expr)))
2345 ((eq key ?a)
2346 (setq subst (gud-read-address)))
2347 ((eq key ?p)
2348 (setq subst (if arg (int-to-string arg)))))
2349 (setq result (concat result (match-string 1 str) subst)))
2350 (setq str (substring str (match-end 2))))
2351 ;; There might be text left in STR when the loop ends.
2352 (concat result str)))
2354 (defun gud-read-address ()
2355 "Return a string containing the core-address found in the buffer at point."
2356 (save-excursion
2357 (let ((pt (point)) found begin)
2358 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2359 (cond
2360 (found (forward-char 2)
2361 (buffer-substring found
2362 (progn (re-search-forward "[^0-9a-f]")
2363 (forward-char -1)
2364 (point))))
2365 (t (setq begin (progn (re-search-backward "[^0-9]")
2366 (forward-char 1)
2367 (point)))
2368 (forward-char 1)
2369 (re-search-forward "[^0-9]")
2370 (forward-char -1)
2371 (buffer-substring begin (point)))))))
2373 (defun gud-call (fmt &optional arg)
2374 (let ((msg (gud-format-command fmt arg)))
2375 (message "Command: %s" msg)
2376 (sit-for 0)
2377 (gud-basic-call msg)))
2379 (defun gud-basic-call (command)
2380 "Invoke the debugger COMMAND displaying source in other window."
2381 (interactive)
2382 (gud-set-buffer)
2383 (let ((command (concat command "\n"))
2384 (proc (get-buffer-process gud-comint-buffer)))
2385 (or proc (error "Current buffer has no process"))
2386 ;; Arrange for the current prompt to get deleted.
2387 (save-excursion
2388 (set-buffer gud-comint-buffer)
2389 (goto-char (process-mark proc))
2390 (beginning-of-line)
2391 (if (looking-at comint-prompt-regexp)
2392 (set-marker gud-delete-prompt-marker (point))))
2393 (process-send-string proc command)))
2395 (defun gud-refresh (&optional arg)
2396 "Fix up a possibly garbled display, and redraw the arrow."
2397 (interactive "P")
2398 (recenter arg)
2399 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2400 (gud-display-frame))
2403 (defun gud-new-keymap (map)
2404 "Return a new keymap which inherits from MAP and has name `Gud'."
2405 (nconc (make-sparse-keymap "Gud") map))
2407 (defun gud-make-debug-menu ()
2408 "Make sure the current local map has a [menu-bar debug] submap.
2409 If it doesn't, replace it with a new map that inherits it,
2410 and create such a submap in that new map."
2411 (use-local-map (gud-new-keymap (current-local-map)))
2412 (define-key (current-local-map) [menu-bar]
2413 (gud-new-keymap (lookup-key (current-local-map) [menu-bar])))
2414 (define-key (current-local-map) [menu-bar debug]
2415 (cons "Gud" (gud-new-keymap gud-menu-map))))
2417 ;;; Code for parsing expressions out of C code. The single entry point is
2418 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2420 ;;; The rest of this file is a hacked version of gdbsrc.el by
2421 ;;; Debby Ayers <ayers@asc.slb.com>,
2422 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2424 (defun gud-find-c-expr ()
2425 "Returns the C expr that surrounds point."
2426 (interactive)
2427 (save-excursion
2428 (let (p expr test-expr)
2429 (setq p (point))
2430 (setq expr (gud-innermost-expr))
2431 (setq test-expr (gud-prev-expr))
2432 (while (and test-expr (gud-expr-compound test-expr expr))
2433 (let ((prev-expr expr))
2434 (setq expr (cons (car test-expr) (cdr expr)))
2435 (goto-char (car expr))
2436 (setq test-expr (gud-prev-expr))
2437 ;; If we just pasted on the condition of an if or while,
2438 ;; throw it away again.
2439 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2440 '("if" "while" "for"))
2441 (setq test-expr nil
2442 expr prev-expr))))
2443 (goto-char p)
2444 (setq test-expr (gud-next-expr))
2445 (while (gud-expr-compound expr test-expr)
2446 (setq expr (cons (car expr) (cdr test-expr)))
2447 (setq test-expr (gud-next-expr))
2449 (buffer-substring (car expr) (cdr expr)))))
2451 (defun gud-innermost-expr ()
2452 "Returns the smallest expr that point is in; move point to beginning of it.
2453 The expr is represented as a cons cell, where the car specifies the point in
2454 the current buffer that marks the beginning of the expr and the cdr specifies
2455 the character after the end of the expr."
2456 (let ((p (point)) begin end)
2457 (gud-backward-sexp)
2458 (setq begin (point))
2459 (gud-forward-sexp)
2460 (setq end (point))
2461 (if (>= p end)
2462 (progn
2463 (setq begin p)
2464 (goto-char p)
2465 (gud-forward-sexp)
2466 (setq end (point)))
2468 (goto-char begin)
2469 (cons begin end)))
2471 (defun gud-backward-sexp ()
2472 "Version of `backward-sexp' that catches errors."
2473 (condition-case nil
2474 (backward-sexp)
2475 (error t)))
2477 (defun gud-forward-sexp ()
2478 "Version of `forward-sexp' that catches errors."
2479 (condition-case nil
2480 (forward-sexp)
2481 (error t)))
2483 (defun gud-prev-expr ()
2484 "Returns the previous expr, point is set to beginning of that expr.
2485 The expr is represented as a cons cell, where the car specifies the point in
2486 the current buffer that marks the beginning of the expr and the cdr specifies
2487 the character after the end of the expr"
2488 (let ((begin) (end))
2489 (gud-backward-sexp)
2490 (setq begin (point))
2491 (gud-forward-sexp)
2492 (setq end (point))
2493 (goto-char begin)
2494 (cons begin end)))
2496 (defun gud-next-expr ()
2497 "Returns the following expr, point is set to beginning of that expr.
2498 The expr is represented as a cons cell, where the car specifies the point in
2499 the current buffer that marks the beginning of the expr and the cdr specifies
2500 the character after the end of the expr."
2501 (let ((begin) (end))
2502 (gud-forward-sexp)
2503 (gud-forward-sexp)
2504 (setq end (point))
2505 (gud-backward-sexp)
2506 (setq begin (point))
2507 (cons begin end)))
2509 (defun gud-expr-compound-sep (span-start span-end)
2510 "Scan from SPAN-START to SPAN-END for punctuation characters.
2511 If `->' is found, return `?.'. If `.' is found, return `?.'.
2512 If any other punctuation is found, return `??'.
2513 If no punctuation is found, return `? '."
2514 (let ((result ?\ )
2515 (syntax))
2516 (while (< span-start span-end)
2517 (setq syntax (char-syntax (char-after span-start)))
2518 (cond
2519 ((= syntax ?\ ) t)
2520 ((= syntax ?.) (setq syntax (char-after span-start))
2521 (cond
2522 ((= syntax ?.) (setq result ?.))
2523 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2524 (setq result ?.)
2525 (setq span-start (+ span-start 1)))
2526 (t (setq span-start span-end)
2527 (setq result ??)))))
2528 (setq span-start (+ span-start 1)))
2529 result))
2531 (defun gud-expr-compound (first second)
2532 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2533 The two exprs are represented as a cons cells, where the car
2534 specifies the point in the current buffer that marks the beginning of the
2535 expr and the cdr specifies the character after the end of the expr.
2536 Link exprs of the form:
2537 Expr -> Expr
2538 Expr . Expr
2539 Expr (Expr)
2540 Expr [Expr]
2541 (Expr) Expr
2542 [Expr] Expr"
2543 (let ((span-start (cdr first))
2544 (span-end (car second))
2545 (syntax))
2546 (setq syntax (gud-expr-compound-sep span-start span-end))
2547 (cond
2548 ((= (car first) (car second)) nil)
2549 ((= (cdr first) (cdr second)) nil)
2550 ((= syntax ?.) t)
2551 ((= syntax ?\ )
2552 (setq span-start (char-after (- span-start 1)))
2553 (setq span-end (char-after span-end))
2554 (cond
2555 ((= span-start ?)) t)
2556 ((= span-start ?]) t)
2557 ((= span-end ?() t)
2558 ((= span-end ?[) t)
2559 (t nil)))
2560 (t nil))))
2562 (provide 'gud)
2564 ;;; gud.el ends here