Support pdb.
[bpt/emacs.git] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
2
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4 ;; Maintainer: FSF
5 ;; Keywords: unix, tools
6
7 ;; Copyright (C) 1992, 93, 94, 95, 96, 1998 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
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.)
39
40 ;;; Code:
41
42 (require 'comint)
43 (require 'etags)
44
45 ;; ======================================================================
46 ;; GUD commands must be visible in C buffers visited by GUD
47
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)
53
54
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)
59
60 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
61 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
62
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)
67
68 (defun gud-marker-filter (&rest args)
69 (apply gud-marker-filter args))
70
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))
76
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))
93 \f
94 ;; ======================================================================
95 ;; command definition
96
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.
100
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.
106
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:
111
112 %f name (without directory) of current source file.
113 %d directory of current source file.
114 %l number of current source line
115 %e text of the C lvalue or function-call expression surrounding point.
116 %a text of the hexadecimal address surrounding point
117 %p prefix argument to the command (if any) as a number
118
119 The `current' source file is the file of the current buffer (if
120 we're in a C file) or the source file current at the last break or
121 step (if we're in the GUD buffer).
122 The `current' line is that of the current buffer (if we're in a
123 source file) or the source line number at the last break or step (if
124 we're in the GUD buffer)."
125 (list 'progn
126 (list 'defun func '(arg)
127 (or doc "")
128 '(interactive "p")
129 (list 'gud-call cmd 'arg))
130 (if key
131 (list 'define-key
132 '(current-local-map)
133 (concat "\C-c" key)
134 (list 'quote func)))
135 (if key
136 (list 'global-set-key
137 (list 'concat 'gud-key-prefix key)
138 (list 'quote func)))))
139
140 ;; Where gud-display-frame should put the debugging arrow; a cons of
141 ;; (filename . line-number). This is set by the marker-filter, which scans
142 ;; the debugger's output for indications of the current program counter.
143 (defvar gud-last-frame nil)
144
145 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
146 ;; the last frame, even if it's been called before and gud-last-frame has
147 ;; been set to nil.
148 (defvar gud-last-last-frame nil)
149
150 ;; All debugger-specific information is collected here.
151 ;; Here's how it works, in case you ever need to add a debugger to the mode.
152 ;;
153 ;; Each entry must define the following at startup:
154 ;;
155 ;;<name>
156 ;; comint-prompt-regexp
157 ;; gud-<name>-massage-args
158 ;; gud-<name>-marker-filter
159 ;; gud-<name>-find-file
160 ;;
161 ;; The job of the massage-args method is to modify the given list of
162 ;; debugger arguments before running the debugger.
163 ;;
164 ;; The job of the marker-filter method is to detect file/line markers in
165 ;; strings and set the global gud-last-frame to indicate what display
166 ;; action (if any) should be triggered by the marker. Note that only
167 ;; whatever the method *returns* is displayed in the buffer; thus, you
168 ;; can filter the debugger's output, interpreting some and passing on
169 ;; the rest.
170 ;;
171 ;; The job of the find-file method is to visit and return the buffer indicated
172 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
173 ;; something else. It would be good if it also copied the Gud menubar entry.
174 \f
175 ;; ======================================================================
176 ;; speedbar support functions and variables.
177 (defvar gud-last-speedbar-buffer nil
178 "The last GUD buffer used.")
179
180 (defvar gud-last-speedbar-stackframe nil
181 "Description of the currently displayed GUD stack.
182 t means that there is no stack, and we are in display-file mode.")
183
184 (defvar gud-speedbar-menu-items
185 ;; Note to self. Add expand, and turn off items when not available.
186 '(["Jump to stack frame" speedbar-edit-line t])
187 "Additional menu items to add the the speedbar frame.")
188
189 (defun gud-speedbar-buttons (buffer)
190 "Create a speedbar display based on the current state of GUD.
191 If the GUD BUFFER is not running a supported debugger, then turn
192 off the specialized speedbar mode."
193 (if (and (save-excursion (goto-char (point-min))
194 (looking-at "Current Stack"))
195 (equal gud-last-last-frame gud-last-speedbar-stackframe))
196 nil
197 (setq gud-last-speedbar-buffer buffer)
198 (let* ((ff (save-excursion (set-buffer buffer) gud-find-file))
199 ;;(lf (save-excursion (set-buffer buffer) gud-last-last-frame))
200 (frames
201 (cond ((eq ff 'gud-gdb-find-file)
202 (gud-gdb-get-stackframe buffer)
203 )
204 ;; Add more debuggers here!
205 (t
206 (speedbar-remove-localized-speedbar-support buffer)
207 nil))))
208 (erase-buffer)
209 (if (not frames)
210 (insert "No Stack frames\n")
211 (insert "Current Stack:\n"))
212 (while frames
213 (insert (nth 1 (car frames)) ":\n")
214 (if (= (length (car frames)) 2)
215 (progn
216 ; (speedbar-insert-button "[?]"
217 ; 'speedbar-button-face
218 ; nil nil nil t)
219 (speedbar-insert-button (car (car frames))
220 'speedbar-directory-face
221 nil nil nil t))
222 ; (speedbar-insert-button "[+]"
223 ; 'speedbar-button-face
224 ; 'speedbar-highlight-face
225 ; 'gud-gdb-get-scope-data
226 ; (car frames) t)
227 (speedbar-insert-button (car (car frames))
228 'speedbar-file-face
229 'speedbar-highlight-face
230 (cond ((eq ff 'gud-gdb-find-file)
231 'gud-gdb-goto-stackframe)
232 (t (error "Should never be here.")))
233 (car frames) t))
234 (setq frames (cdr frames)))
235 ; (let ((selected-frame
236 ; (cond ((eq ff 'gud-gdb-find-file)
237 ; (gud-gdb-selected-frame-info buffer))
238 ; (t (error "Should never be here."))))))
239 )
240 (setq gud-last-speedbar-stackframe gud-last-last-frame)))
241
242 \f
243 ;; ======================================================================
244 ;; gdb functions
245
246 ;;; History of argument lists passed to gdb.
247 (defvar gud-gdb-history nil)
248
249 (defun gud-gdb-massage-args (file args)
250 (cons "-fullname" args))
251
252 (defvar gud-gdb-marker-regexp
253 ;; This used to use path-separator instead of ":";
254 ;; however, we found that on both Windows 32 and MSDOS
255 ;; a colon is correct here.
256 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
257 "\\([0-9]*\\)" ":" ".*\n"))
258
259 ;; There's no guarantee that Emacs will hand the filter the entire
260 ;; marker at once; it could be broken up across several strings. We
261 ;; might even receive a big chunk with several markers in it. If we
262 ;; receive a chunk of text which looks like it might contain the
263 ;; beginning of a marker, we save it here between calls to the
264 ;; filter.
265 (defvar gud-marker-acc "")
266 (make-variable-buffer-local 'gud-marker-acc)
267
268 (defun gud-gdb-marker-filter (string)
269 (setq gud-marker-acc (concat gud-marker-acc string))
270 (let ((output ""))
271
272 ;; Process all the complete markers in this chunk.
273 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
274 (setq
275
276 ;; Extract the frame position from the marker.
277 gud-last-frame
278 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
279 (string-to-int (substring gud-marker-acc
280 (match-beginning 2)
281 (match-end 2))))
282
283 ;; Append any text before the marker to the output we're going
284 ;; to return - we don't include the marker in this text.
285 output (concat output
286 (substring gud-marker-acc 0 (match-beginning 0)))
287
288 ;; Set the accumulator to the remaining text.
289 gud-marker-acc (substring gud-marker-acc (match-end 0))))
290
291 ;; Does the remaining text look like it might end with the
292 ;; beginning of another marker? If it does, then keep it in
293 ;; gud-marker-acc until we receive the rest of it. Since we
294 ;; know the full marker regexp above failed, it's pretty simple to
295 ;; test for marker starts.
296 (if (string-match "\032.*\\'" gud-marker-acc)
297 (progn
298 ;; Everything before the potential marker start can be output.
299 (setq output (concat output (substring gud-marker-acc
300 0 (match-beginning 0))))
301
302 ;; Everything after, we save, to combine with later input.
303 (setq gud-marker-acc
304 (substring gud-marker-acc (match-beginning 0))))
305
306 (setq output (concat output gud-marker-acc)
307 gud-marker-acc ""))
308
309 output))
310
311 (defun gud-gdb-find-file (f)
312 (save-excursion
313 (let ((buf (find-file-noselect f)))
314 (set-buffer buf)
315 (gud-make-debug-menu)
316 (local-set-key [menu-bar debug tbreak]
317 '("Temporary Breakpoint" . gud-tbreak))
318 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
319 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
320 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
321 buf)))
322
323 (defvar gdb-minibuffer-local-map nil
324 "Keymap for minibuffer prompting of gdb startup command.")
325 (if gdb-minibuffer-local-map
326 ()
327 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
328 (define-key
329 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
330
331 ;;;###autoload
332 (defun gdb (command-line)
333 "Run gdb on program FILE in buffer *gud-FILE*.
334 The directory containing FILE becomes the initial working directory
335 and source-file directory for your debugger."
336 (interactive
337 (list (read-from-minibuffer "Run gdb (like this): "
338 (if (consp gud-gdb-history)
339 (car gud-gdb-history)
340 "gdb ")
341 gdb-minibuffer-local-map nil
342 '(gud-gdb-history . 1))))
343
344 (gud-common-init command-line 'gud-gdb-massage-args
345 'gud-gdb-marker-filter 'gud-gdb-find-file)
346
347 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
348 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
349 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
350 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
351 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
352 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
353 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
354 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
355 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
356 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
357 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
358
359 (local-set-key "\C-i" 'gud-gdb-complete-command)
360 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
361 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
362 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
363 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
364 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
365 (setq paragraph-start comint-prompt-regexp)
366 (run-hooks 'gdb-mode-hook)
367 )
368
369 ;; One of the nice features of GDB is its impressive support for
370 ;; context-sensitive command completion. We preserve that feature
371 ;; in the GUD buffer by using a GDB command designed just for Emacs.
372
373 ;; The completion process filter indicates when it is finished.
374 (defvar gud-gdb-complete-in-progress)
375
376 ;; Since output may arrive in fragments we accumulate partials strings here.
377 (defvar gud-gdb-complete-string)
378
379 ;; We need to know how much of the completion to chop off.
380 (defvar gud-gdb-complete-break)
381
382 ;; The completion list is constructed by the process filter.
383 (defvar gud-gdb-complete-list)
384
385 (defvar gud-comint-buffer nil)
386
387 (defun gud-gdb-complete-command ()
388 "Perform completion on the GDB command preceding point.
389 This is implemented using the GDB `complete' command which isn't
390 available with older versions of GDB."
391 (interactive)
392 (let* ((end (point))
393 (command (save-excursion
394 (beginning-of-line)
395 (and (looking-at comint-prompt-regexp)
396 (goto-char (match-end 0)))
397 (buffer-substring (point) end)))
398 command-word)
399 ;; Find the word break. This match will always succeed.
400 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
401 (setq gud-gdb-complete-break (match-beginning 2)
402 command-word (substring command gud-gdb-complete-break))
403 ;; Temporarily install our filter function.
404 (let ((gud-marker-filter 'gud-gdb-complete-filter))
405 ;; Issue the command to GDB.
406 (gud-basic-call (concat "complete " command))
407 (setq gud-gdb-complete-in-progress t
408 gud-gdb-complete-string nil
409 gud-gdb-complete-list nil)
410 ;; Slurp the output.
411 (while gud-gdb-complete-in-progress
412 (accept-process-output (get-buffer-process gud-comint-buffer))))
413 ;; Protect against old versions of GDB.
414 (and gud-gdb-complete-list
415 (string-match "^Undefined command: \"complete\""
416 (car gud-gdb-complete-list))
417 (error "This version of GDB doesn't support the `complete' command."))
418 ;; Sort the list like readline.
419 (setq gud-gdb-complete-list
420 (sort gud-gdb-complete-list (function string-lessp)))
421 ;; Remove duplicates.
422 (let ((first gud-gdb-complete-list)
423 (second (cdr gud-gdb-complete-list)))
424 (while second
425 (if (string-equal (car first) (car second))
426 (setcdr first (setq second (cdr second)))
427 (setq first second
428 second (cdr second)))))
429 ;; Add a trailing single quote if there is a unique completion
430 ;; and it contains an odd number of unquoted single quotes.
431 (and (= (length gud-gdb-complete-list) 1)
432 (let ((str (car gud-gdb-complete-list))
433 (pos 0)
434 (count 0))
435 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
436 (setq count (1+ count)
437 pos (match-end 0)))
438 (and (= (mod count 2) 1)
439 (setq gud-gdb-complete-list (list (concat str "'"))))))
440 ;; Let comint handle the rest.
441 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
442
443 ;; The completion process filter is installed temporarily to slurp the
444 ;; output of GDB up to the next prompt and build the completion list.
445 (defun gud-gdb-complete-filter (string)
446 (setq string (concat gud-gdb-complete-string string))
447 (while (string-match "\n" string)
448 (setq gud-gdb-complete-list
449 (cons (substring string gud-gdb-complete-break (match-beginning 0))
450 gud-gdb-complete-list))
451 (setq string (substring string (match-end 0))))
452 (if (string-match comint-prompt-regexp string)
453 (progn
454 (setq gud-gdb-complete-in-progress nil)
455 string)
456 (progn
457 (setq gud-gdb-complete-string string)
458 "")))
459
460 ;; gdb speedbar functions
461
462 (defun gud-gdb-goto-stackframe (text token indent)
463 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
464 (speedbar-with-attached-buffer
465 (gud-basic-call (concat "frame " (nth 1 token)))
466 (sit-for 1)))
467
468 (defvar gud-gdb-fetched-stack-frame nil
469 "Stack frames we are fetching from GDB.")
470
471 (defvar gud-gdb-fetched-stack-frame-list nil
472 "List of stack frames we are fetching from GDB.")
473
474 ;(defun gud-gdb-get-scope-data (text token indent)
475 ; ;; checkdoc-params: (indent)
476 ; "Fetch data associated with a stack frame, and expand/contract it.
477 ;Data to do this is retrieved from TEXT and TOKEN."
478 ; (let ((args nil) (scope nil))
479 ; (gud-gdb-run-command-fetch-lines "info args")
480 ;
481 ; (gud-gdb-run-command-fetch-lines "info local")
482 ;
483 ; ))
484
485 (defun gud-gdb-get-stackframe (buffer)
486 "Extract the current stack frame out of the GUD GDB BUFFER."
487 (let ((newlst nil)
488 (gud-gdb-fetched-stack-frame-list nil))
489 (gud-gdb-run-command-fetch-lines "backtrace" buffer)
490 (if (string-match "No stack" (car gud-gdb-fetched-stack-frame-list))
491 ;; Go into some other mode???
492 nil
493 (while gud-gdb-fetched-stack-frame-list
494 (let ((e (car gud-gdb-fetched-stack-frame-list))
495 (name nil) (num nil))
496 (if (not (or
497 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([0-9a-zA-Z_]+\\) (" e)
498 (string-match "^#\\([0-9]+\\) +\\([0-9a-zA-Z_]+\\) (" e)))
499 (if (not (string-match
500 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
501 nil
502 (setcar newlst
503 (list (nth 0 (car newlst))
504 (nth 1 (car newlst))
505 (match-string 1 e)
506 (match-string 2 e))))
507 (setq num (match-string 1 e)
508 name (match-string 2 e))
509 (setq newlst
510 (cons
511 (if (string-match
512 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
513 (list name num (match-string 1 e)
514 (match-string 2 e))
515 (list name num))
516 newlst))))
517 (setq gud-gdb-fetched-stack-frame-list
518 (cdr gud-gdb-fetched-stack-frame-list)))
519 (nreverse newlst))))
520
521 ;(defun gud-gdb-selected-frame-info (buffer)
522 ; "Learn GDB information for the currently selected stack frame in BUFFER."
523 ; )
524
525 (defun gud-gdb-run-command-fetch-lines (command buffer)
526 "Run COMMAND, and return when `gud-gdb-fetched-stack-frame-list' is full.
527 BUFFER is the GUD buffer in which to run the command."
528 (save-excursion
529 (set-buffer buffer)
530 (if (save-excursion
531 (goto-char (point-max))
532 (beginning-of-line)
533 (not (looking-at comint-prompt-regexp)))
534 nil
535 ;; Much of this copied from GDB complete, but I'm grabbing the stack
536 ;; frame instead.
537 (let ((gud-marker-filter 'gud-gdb-speedbar-stack-filter))
538 ;; Issue the command to GDB.
539 (gud-basic-call command)
540 (setq gud-gdb-complete-in-progress t ;; use this flag for our purposes.
541 gud-gdb-complete-string nil
542 gud-gdb-complete-list nil)
543 ;; Slurp the output.
544 (while gud-gdb-complete-in-progress
545 (accept-process-output (get-buffer-process gud-comint-buffer)))
546 (setq gud-gdb-fetched-stack-frame nil
547 gud-gdb-fetched-stack-frame-list
548 (nreverse gud-gdb-fetched-stack-frame-list))))))
549
550 (defun gud-gdb-speedbar-stack-filter (string)
551 ;; checkdoc-params: (string)
552 "Filter used to read in the current GDB stack."
553 (setq string (concat gud-gdb-fetched-stack-frame string))
554 (while (string-match "\n" string)
555 (setq gud-gdb-fetched-stack-frame-list
556 (cons (substring string 0 (match-beginning 0))
557 gud-gdb-fetched-stack-frame-list))
558 (setq string (substring string (match-end 0))))
559 (if (string-match comint-prompt-regexp string)
560 (progn
561 (setq gud-gdb-complete-in-progress nil)
562 string)
563 (progn
564 (setq gud-gdb-complete-string string)
565 "")))
566
567 \f
568 ;; ======================================================================
569 ;; sdb functions
570
571 ;;; History of argument lists passed to sdb.
572 (defvar gud-sdb-history nil)
573
574 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
575 "If nil, we're on a System V Release 4 and don't need the tags hack.")
576
577 (defvar gud-sdb-lastfile nil)
578
579 (defun gud-sdb-massage-args (file args) args)
580
581 (defun gud-sdb-marker-filter (string)
582 (setq gud-marker-acc
583 (if gud-marker-acc (concat gud-marker-acc string) string))
584 (let (start)
585 ;; Process all complete markers in this chunk
586 (while
587 (cond
588 ;; System V Release 3.2 uses this format
589 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
590 gud-marker-acc start)
591 (setq gud-last-frame
592 (cons
593 (substring gud-marker-acc (match-beginning 3) (match-end 3))
594 (string-to-int
595 (substring gud-marker-acc (match-beginning 4) (match-end 4))))))
596 ;; System V Release 4.0 quite often clumps two lines together
597 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
598 gud-marker-acc start)
599 (setq gud-sdb-lastfile
600 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
601 (setq gud-last-frame
602 (cons
603 gud-sdb-lastfile
604 (string-to-int
605 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
606 ;; System V Release 4.0
607 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
608 gud-marker-acc start)
609 (setq gud-sdb-lastfile
610 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
611 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
612 gud-marker-acc start))
613 (setq gud-last-frame
614 (cons
615 gud-sdb-lastfile
616 (string-to-int
617 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
618 (t
619 (setq gud-sdb-lastfile nil)))
620 (setq start (match-end 0)))
621
622 ;; Search for the last incomplete line in this chunk
623 (while (string-match "\n" gud-marker-acc start)
624 (setq start (match-end 0)))
625
626 ;; If we have an incomplete line, store it in gud-marker-acc.
627 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
628 string)
629
630 (defun gud-sdb-find-file (f)
631 (save-excursion
632 (let ((buf (if gud-sdb-needs-tags
633 (find-tag-noselect f)
634 (find-file-noselect f))))
635 (set-buffer buf)
636 (gud-make-debug-menu)
637 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
638 buf)))
639
640 ;;;###autoload
641 (defun sdb (command-line)
642 "Run sdb on program FILE in buffer *gud-FILE*.
643 The directory containing FILE becomes the initial working directory
644 and source-file directory for your debugger."
645 (interactive
646 (list (read-from-minibuffer "Run sdb (like this): "
647 (if (consp gud-sdb-history)
648 (car gud-sdb-history)
649 "sdb ")
650 nil nil
651 '(gud-sdb-history . 1))))
652 (if (and gud-sdb-needs-tags
653 (not (and (boundp 'tags-file-name)
654 (stringp tags-file-name)
655 (file-exists-p tags-file-name))))
656 (error "The sdb support requires a valid tags table to work."))
657
658 (gud-common-init command-line 'gud-sdb-massage-args
659 'gud-sdb-marker-filter 'gud-sdb-find-file)
660
661 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
662 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
663 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
664 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
665 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
666 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
667 (gud-def gud-cont "c" "\C-r" "Continue with display.")
668 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
669
670 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
671 (setq paragraph-start comint-prompt-regexp)
672 (local-set-key [menu-bar debug tbreak]
673 '("Temporary Breakpoint" . gud-tbreak))
674 (run-hooks 'sdb-mode-hook)
675 )
676 \f
677 ;; ======================================================================
678 ;; dbx functions
679
680 ;;; History of argument lists passed to dbx.
681 (defvar gud-dbx-history nil)
682
683 (defcustom gud-dbx-directories nil
684 "*A list of directories that dbx should search for source code.
685 If nil, only source files in the program directory
686 will be known to dbx.
687
688 The file names should be absolute, or relative to the directory
689 containing the executable being debugged."
690 :type '(choice (const :tag "Current Directory" nil)
691 (repeat :value ("")
692 directory))
693 :group 'gud)
694
695 (defun gud-dbx-massage-args (file args)
696 (nconc (let ((directories gud-dbx-directories)
697 (result nil))
698 (while directories
699 (setq result (cons (car directories) (cons "-I" result)))
700 (setq directories (cdr directories)))
701 (nreverse result))
702 args))
703
704 (defun gud-dbx-file-name (f)
705 "Transform a relative file name to an absolute file name, for dbx."
706 (let ((result nil))
707 (if (file-exists-p f)
708 (setq result (expand-file-name f))
709 (let ((directories gud-dbx-directories))
710 (while directories
711 (let ((path (concat (car directories) "/" f)))
712 (if (file-exists-p path)
713 (setq result (expand-file-name path)
714 directories nil)))
715 (setq directories (cdr directories)))))
716 result))
717
718 (defun gud-dbx-marker-filter (string)
719 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
720
721 (let (start)
722 ;; Process all complete markers in this chunk.
723 (while (or (string-match
724 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
725 gud-marker-acc start)
726 (string-match
727 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
728 gud-marker-acc start))
729 (setq gud-last-frame
730 (cons
731 (substring gud-marker-acc (match-beginning 2) (match-end 2))
732 (string-to-int
733 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
734 start (match-end 0)))
735
736 ;; Search for the last incomplete line in this chunk
737 (while (string-match "\n" gud-marker-acc start)
738 (setq start (match-end 0)))
739
740 ;; If the incomplete line APPEARS to begin with another marker, keep it
741 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
742 ;; unnecessary concat during the next call.
743 (setq gud-marker-acc
744 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
745 (substring gud-marker-acc (match-beginning 0))
746 nil)))
747 string)
748
749 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
750 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
751 (defvar gud-mips-p
752 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
753 ;; We haven't tested gud on this system:
754 (string-match "^mips-[^-]*-riscos" system-configuration)
755 ;; It's documented on OSF/1.3
756 (string-match "^mips-[^-]*-osf1" system-configuration)
757 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
758 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
759
760 (defun gud-mipsdbx-massage-args (file args)
761 (cons "-emacs" args))
762
763 ;; This is just like the gdb one except for the regexps since we need to cope
764 ;; with an optional breakpoint number in [] before the ^Z^Z
765 (defun gud-mipsdbx-marker-filter (string)
766 (setq gud-marker-acc (concat gud-marker-acc string))
767 (let ((output ""))
768
769 ;; Process all the complete markers in this chunk.
770 (while (string-match
771 ;; This is like th gdb marker but with an optional
772 ;; leading break point number like `[1] '
773 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
774 gud-marker-acc)
775 (setq
776
777 ;; Extract the frame position from the marker.
778 gud-last-frame
779 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
780 (string-to-int (substring gud-marker-acc
781 (match-beginning 2)
782 (match-end 2))))
783
784 ;; Append any text before the marker to the output we're going
785 ;; to return - we don't include the marker in this text.
786 output (concat output
787 (substring gud-marker-acc 0 (match-beginning 0)))
788
789 ;; Set the accumulator to the remaining text.
790 gud-marker-acc (substring gud-marker-acc (match-end 0))))
791
792 ;; Does the remaining text look like it might end with the
793 ;; beginning of another marker? If it does, then keep it in
794 ;; gud-marker-acc until we receive the rest of it. Since we
795 ;; know the full marker regexp above failed, it's pretty simple to
796 ;; test for marker starts.
797 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
798 (progn
799 ;; Everything before the potential marker start can be output.
800 (setq output (concat output (substring gud-marker-acc
801 0 (match-beginning 0))))
802
803 ;; Everything after, we save, to combine with later input.
804 (setq gud-marker-acc
805 (substring gud-marker-acc (match-beginning 0))))
806
807 (setq output (concat output gud-marker-acc)
808 gud-marker-acc ""))
809
810 output))
811
812 ;; The dbx in IRIX is a pain. It doesn't print the file name when
813 ;; stopping at a breakpoint (but you do get it from the `up' and
814 ;; `down' commands...). The only way to extract the information seems
815 ;; to be with a `file' command, although the current line number is
816 ;; available in $curline. Thus we have to look for output which
817 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
818 ;; to output the information we want with a combination of the
819 ;; `printf' and `file' commands as a pseudo marker which we can
820 ;; recognise next time through the marker-filter. This would be like
821 ;; the gdb marker but you can't get the file name without a newline...
822 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
823 ;; number rather than a line number etc. Maybe this could be made to
824 ;; work by listing all the breakpoints and picking the one(s) with the
825 ;; correct line number, but life's too short.
826 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
827
828 (defvar gud-irix-p
829 (and (string-match "^mips-[^-]*-irix" system-configuration)
830 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
831 "Non-nil to assume the interface appropriate for IRIX dbx.
832 This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
833 a better solution in 6.1 upwards.")
834 (defvar gud-dbx-use-stopformat-p
835 (string-match "irix[6-9]\\.[1-9]" system-configuration)
836 "Non-nil to use the dbx feature present at least from Irix 6.1
837 whereby $stopformat=1 produces an output format compatiable with
838 `gud-dbx-marker-filter'.")
839 ;; [Irix dbx seems to be a moving target. The dbx output changed
840 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
841 ;; the output from `up' is no longer spotted by gud (and it's probably
842 ;; not distinctive enough to try to match it -- use C-<, C->
843 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
844 ;; `long long'(why?!), so the printf stuff needed changing. The line
845 ;; number was cast to `long' as a compromise between the new `long
846 ;; long' and the original `int'. This is reported not to work in 6.2,
847 ;; so it's changed back to int -- don't make your sources too long.
848 ;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
849 ;; whereby `set $stopformat=1' reportedly produces output compatible
850 ;; with `gud-dbx-marker-filter', which we prefer.
851
852 ;; The process filter is also somewhat
853 ;; unreliable, sometimes not spotting the markers; I don't know
854 ;; whether there's anything that can be done about that. It would be
855 ;; much better if SGI could be persuaded to (re?)instate the MIPS
856 ;; -emacs flag for gdb-like output (which ought to be possible as most
857 ;; of the communication I've had over it has been from sgi.com).]
858
859 ;; this filter is influenced by the xdb one rather than the gdb one
860 (defun gud-irixdbx-marker-filter (string)
861 (let (result (case-fold-search nil))
862 (if (or (string-match comint-prompt-regexp string)
863 (string-match ".*\012" string))
864 (setq result (concat gud-marker-acc string)
865 gud-marker-acc "")
866 (setq gud-marker-acc (concat gud-marker-acc string)))
867 (if result
868 (cond
869 ;; look for breakpoint or signal indication e.g.:
870 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
871 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
872 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
873 ((string-match
874 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
875 result)
876 ;; prod dbx into printing out the line number and file
877 ;; name in a form we can grok as below
878 (process-send-string (get-buffer-process gud-comint-buffer)
879 "printf \"\032\032%1d:\",(int)$curline;file\n"))
880 ;; look for result of, say, "up" e.g.:
881 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
882 ;; (this will also catch one of the lines printed by "where")
883 ((string-match
884 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
885 result)
886 (let ((file (substring result (match-beginning 1)
887 (match-end 1))))
888 (if (file-exists-p file)
889 (setq gud-last-frame
890 (cons
891 (substring
892 result (match-beginning 1) (match-end 1))
893 (string-to-int
894 (substring
895 result (match-beginning 2) (match-end 2)))))))
896 result)
897 ((string-match ; kluged-up marker as above
898 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
899 (let ((file (gud-dbx-file-name
900 (substring result (match-beginning 2) (match-end 2)))))
901 (if (and file (file-exists-p file))
902 (setq gud-last-frame
903 (cons
904 file
905 (string-to-int
906 (substring
907 result (match-beginning 1) (match-end 1)))))))
908 (setq result (substring result 0 (match-beginning 0))))))
909 (or result "")))
910
911 (defvar gud-dgux-p (string-match "-dgux" system-configuration)
912 "Non-nil means to assume the interface approriate for DG/UX dbx.
913 This was tested using R4.11.")
914
915 ;; There are a couple of differences between DG's dbx output and normal
916 ;; dbx output which make it nontrivial to integrate this into the
917 ;; standard dbx-marker-filter (mainly, there are a different number of
918 ;; backreferences). The markers look like:
919 ;;
920 ;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
921 ;;
922 ;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
923 ;; number), and
924 ;;
925 ;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
926 ;;
927 ;; from signals and
928 ;;
929 ;; Frame 21, line 974, routine command_loop(), file keyboard.c
930 ;;
931 ;; from up/down/where.
932
933 (defun gud-dguxdbx-marker-filter (string)
934 (setq gud-marker-acc (if gud-marker-acc
935 (concat gud-marker-acc string)
936 string))
937 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
938 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
939 start)
940 ;; Process all complete markers in this chunk.
941 (while (string-match re gud-marker-acc start)
942 (setq gud-last-frame
943 (cons
944 (substring gud-marker-acc (match-beginning 4) (match-end 4))
945 (string-to-int (substring gud-marker-acc
946 (match-beginning 3) (match-end 3))))
947 start (match-end 0)))
948
949 ;; Search for the last incomplete line in this chunk
950 (while (string-match "\n" gud-marker-acc start)
951 (setq start (match-end 0)))
952
953 ;; If the incomplete line APPEARS to begin with another marker, keep it
954 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
955 ;; unnecessary concat during the next call.
956 (setq gud-marker-acc
957 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
958 (substring gud-marker-acc (match-beginning 0))
959 nil)))
960 string)
961
962 (defun gud-dbx-find-file (f)
963 (save-excursion
964 (let ((realf (gud-dbx-file-name f)))
965 (if realf
966 (let ((buf (find-file-noselect realf)))
967 (set-buffer buf)
968 (gud-make-debug-menu)
969 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
970 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
971 buf)
972 nil))))
973
974 ;;;###autoload
975 (defun dbx (command-line)
976 "Run dbx on program FILE in buffer *gud-FILE*.
977 The directory containing FILE becomes the initial working directory
978 and source-file directory for your debugger."
979 (interactive
980 (list (read-from-minibuffer "Run dbx (like this): "
981 (if (consp gud-dbx-history)
982 (car gud-dbx-history)
983 "dbx ")
984 nil nil
985 '(gud-dbx-history . 1))))
986
987 (cond
988 (gud-mips-p
989 (gud-common-init command-line 'gud-mipsdbx-massage-args
990 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
991 (gud-irix-p
992 (gud-common-init command-line 'gud-dbx-massage-args
993 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
994 (gud-dgux-p
995 (gud-common-init command-line 'gud-dbx-massage-args
996 'gud-dguxdbx-marker-filter 'gud-dbx-find-file))
997 (t
998 (gud-common-init command-line 'gud-dbx-massage-args
999 'gud-dbx-marker-filter 'gud-dbx-find-file)))
1000
1001 (cond
1002 (gud-mips-p
1003 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1004 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1005 (gud-def gud-break "stop at \"%f\":%l"
1006 "\C-b" "Set breakpoint at current line.")
1007 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
1008 (gud-irix-p
1009 (gud-def gud-break "stop at \"%d%f\":%l"
1010 "\C-b" "Set breakpoint at current line.")
1011 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1012 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1013 "<" "Up (numeric arg) stack frames.")
1014 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
1015 ">" "Down (numeric arg) stack frames.")
1016 ;; Make dbx give out the source location info that we need.
1017 (process-send-string (get-buffer-process gud-comint-buffer)
1018 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1019 (gud-dbx-use-stopformat-p
1020 (process-send-string (get-buffer-process gud-comint-buffer)
1021 "set $stopformat=1\n"))
1022 (t
1023 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1024 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1025 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1026 "\C-b" "Set breakpoint at current line.")))
1027
1028 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1029 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1030 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1031 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1032 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1033 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
1034
1035 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
1036 (setq paragraph-start comint-prompt-regexp)
1037 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1038 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1039 (run-hooks 'dbx-mode-hook)
1040 )
1041 \f
1042 ;; ======================================================================
1043 ;; xdb (HP PARISC debugger) functions
1044
1045 ;;; History of argument lists passed to xdb.
1046 (defvar gud-xdb-history nil)
1047
1048 (defcustom gud-xdb-directories nil
1049 "*A list of directories that xdb should search for source code.
1050 If nil, only source files in the program directory
1051 will be known to xdb.
1052
1053 The file names should be absolute, or relative to the directory
1054 containing the executable being debugged."
1055 :type '(choice (const :tag "Current Directory" nil)
1056 (repeat :value ("")
1057 directory))
1058 :group 'gud)
1059
1060 (defun gud-xdb-massage-args (file args)
1061 (nconc (let ((directories gud-xdb-directories)
1062 (result nil))
1063 (while directories
1064 (setq result (cons (car directories) (cons "-d" result)))
1065 (setq directories (cdr directories)))
1066 (nreverse result))
1067 args))
1068
1069 (defun gud-xdb-file-name (f)
1070 "Transform a relative pathname to a full pathname in xdb mode"
1071 (let ((result nil))
1072 (if (file-exists-p f)
1073 (setq result (expand-file-name f))
1074 (let ((directories gud-xdb-directories))
1075 (while directories
1076 (let ((path (concat (car directories) "/" f)))
1077 (if (file-exists-p path)
1078 (setq result (expand-file-name path)
1079 directories nil)))
1080 (setq directories (cdr directories)))))
1081 result))
1082
1083 ;; xdb does not print the lines all at once, so we have to accumulate them
1084 (defun gud-xdb-marker-filter (string)
1085 (let (result)
1086 (if (or (string-match comint-prompt-regexp string)
1087 (string-match ".*\012" string))
1088 (setq result (concat gud-marker-acc string)
1089 gud-marker-acc "")
1090 (setq gud-marker-acc (concat gud-marker-acc string)))
1091 (if result
1092 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1093 result)
1094 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1095 result))
1096 (let ((line (string-to-int
1097 (substring result (match-beginning 2) (match-end 2))))
1098 (file (gud-xdb-file-name
1099 (substring result (match-beginning 1) (match-end 1)))))
1100 (if file
1101 (setq gud-last-frame (cons file line))))))
1102 (or result "")))
1103
1104 (defun gud-xdb-find-file (f)
1105 (save-excursion
1106 (let ((realf (gud-xdb-file-name f)))
1107 (if realf
1108 (let ((buf (find-file-noselect realf)))
1109 (set-buffer buf)
1110 (gud-make-debug-menu)
1111 (local-set-key [menu-bar debug tbreak]
1112 '("Temporary Breakpoint" . gud-tbreak))
1113 (local-set-key [menu-bar debug finish]
1114 '("Finish Function" . gud-finish))
1115 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1116 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1117 buf)
1118 nil))))
1119
1120 ;;;###autoload
1121 (defun xdb (command-line)
1122 "Run xdb on program FILE in buffer *gud-FILE*.
1123 The directory containing FILE becomes the initial working directory
1124 and source-file directory for your debugger.
1125
1126 You can set the variable 'gud-xdb-directories' to a list of program source
1127 directories if your program contains sources from more than one directory."
1128 (interactive
1129 (list (read-from-minibuffer "Run xdb (like this): "
1130 (if (consp gud-xdb-history)
1131 (car gud-xdb-history)
1132 "xdb ")
1133 nil nil
1134 '(gud-xdb-history . 1))))
1135
1136 (gud-common-init command-line 'gud-xdb-massage-args
1137 'gud-xdb-marker-filter 'gud-xdb-find-file)
1138
1139 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1140 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1141 "Set temporary breakpoint at current line.")
1142 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1143 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1144 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1145 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1146 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1147 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1148 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1149 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1150
1151 (setq comint-prompt-regexp "^>")
1152 (setq paragraph-start comint-prompt-regexp)
1153 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
1154 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1155 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1156 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1157 (run-hooks 'xdb-mode-hook))
1158 \f
1159 ;; ======================================================================
1160 ;; perldb functions
1161
1162 ;;; History of argument lists passed to perldb.
1163 (defvar gud-perldb-history nil)
1164
1165 (defun gud-perldb-massage-args (file args)
1166 (cond ((equal (car args) "-e")
1167 (cons "-d"
1168 (cons (car args)
1169 (cons (nth 1 args)
1170 (cons "--" (cons "-emacs" (cdr (cdr args))))))))
1171 (t
1172 (cons "-d" (cons (car args) (cons "-emacs" (cdr args)))))))
1173
1174 ;; There's no guarantee that Emacs will hand the filter the entire
1175 ;; marker at once; it could be broken up across several strings. We
1176 ;; might even receive a big chunk with several markers in it. If we
1177 ;; receive a chunk of text which looks like it might contain the
1178 ;; beginning of a marker, we save it here between calls to the
1179 ;; filter.
1180 (defun gud-perldb-marker-filter (string)
1181 (setq gud-marker-acc (concat gud-marker-acc string))
1182 (let ((output ""))
1183
1184 ;; Process all the complete markers in this chunk.
1185 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
1186 gud-marker-acc)
1187 (setq
1188
1189 ;; Extract the frame position from the marker.
1190 gud-last-frame
1191 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1192 (string-to-int (substring gud-marker-acc
1193 (match-beginning 3)
1194 (match-end 3))))
1195
1196 ;; Append any text before the marker to the output we're going
1197 ;; to return - we don't include the marker in this text.
1198 output (concat output
1199 (substring gud-marker-acc 0 (match-beginning 0)))
1200
1201 ;; Set the accumulator to the remaining text.
1202 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1203
1204 ;; Does the remaining text look like it might end with the
1205 ;; beginning of another marker? If it does, then keep it in
1206 ;; gud-marker-acc until we receive the rest of it. Since we
1207 ;; know the full marker regexp above failed, it's pretty simple to
1208 ;; test for marker starts.
1209 (if (string-match "\032.*\\'" gud-marker-acc)
1210 (progn
1211 ;; Everything before the potential marker start can be output.
1212 (setq output (concat output (substring gud-marker-acc
1213 0 (match-beginning 0))))
1214
1215 ;; Everything after, we save, to combine with later input.
1216 (setq gud-marker-acc
1217 (substring gud-marker-acc (match-beginning 0))))
1218
1219 (setq output (concat output gud-marker-acc)
1220 gud-marker-acc ""))
1221
1222 output))
1223
1224 (defun gud-perldb-find-file (f)
1225 (save-excursion
1226 (let ((buf (find-file-noselect f)))
1227 (set-buffer buf)
1228 (gud-make-debug-menu)
1229 buf)))
1230
1231 (defcustom gud-perldb-command-name "perl"
1232 "File name for executing Perl."
1233 :type 'string
1234 :group 'gud)
1235
1236 ;;;###autoload
1237 (defun perldb (command-line)
1238 "Run perldb on program FILE in buffer *gud-FILE*.
1239 The directory containing FILE becomes the initial working directory
1240 and source-file directory for your debugger."
1241 (interactive
1242 (list (read-from-minibuffer "Run perldb (like this): "
1243 (if (consp gud-perldb-history)
1244 (car gud-perldb-history)
1245 (concat gud-perldb-command-name
1246 " "
1247 (or (buffer-file-name)
1248 "-e 0")
1249 " "))
1250 nil nil
1251 '(gud-perldb-history . 1))))
1252
1253 (gud-common-init command-line 'gud-perldb-massage-args
1254 'gud-perldb-marker-filter 'gud-perldb-find-file)
1255
1256 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1257 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1258 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1259 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1260 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1261 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1262 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1263 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1264 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1265
1266 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
1267 (setq paragraph-start comint-prompt-regexp)
1268 (run-hooks 'perldb-mode-hook)
1269 )
1270 \f
1271 ;; ======================================================================
1272 ;; pdb (Python debugger) functions
1273
1274 ;;; History of argument lists passed to pdb.
1275 (defvar gud-pdb-history nil)
1276
1277 (defun gud-pdb-massage-args (file args)
1278 args)
1279
1280 ;; Last group is for return value, e.g. "> test.py(2)foo()->None"
1281 ;; Either file or function name may be omitted: "> <string>(0)?()"
1282 (defvar gud-pdb-marker-regexp
1283 "^> \\([-a-zA-Z0-9_/.]*\\|<string>\\)(\\([0-9]+\\))\\([a-zA-Z0-9_]*\\|\\?\\)()\\(->[^\n]*\\)?\n")
1284 (defvar gud-pdb-marker-regexp-file-group 1)
1285 (defvar gud-pdb-marker-regexp-line-group 2)
1286 (defvar gud-pdb-marker-regexp-fnname-group 3)
1287
1288 (defvar gud-pdb-marker-regexp-start "^> ")
1289
1290 ;; There's no guarantee that Emacs will hand the filter the entire
1291 ;; marker at once; it could be broken up across several strings. We
1292 ;; might even receive a big chunk with several markers in it. If we
1293 ;; receive a chunk of text which looks like it might contain the
1294 ;; beginning of a marker, we save it here between calls to the
1295 ;; filter.
1296 (defun gud-pdb-marker-filter (string)
1297 (setq gud-marker-acc (concat gud-marker-acc string))
1298 (let ((output ""))
1299
1300 ;; Process all the complete markers in this chunk.
1301 (while (string-match gud-pdb-marker-regexp gud-marker-acc)
1302 (setq
1303
1304 ;; Extract the frame position from the marker.
1305 gud-last-frame
1306 (let ((file (match-string gud-pdb-marker-regexp-file-group
1307 gud-marker-acc))
1308 (line (string-to-int
1309 (match-string gud-pdb-marker-regexp-line-group
1310 gud-marker-acc))))
1311 (if (string-equal file "<string>")
1312 gud-last-frame
1313 (cons file line)))
1314
1315 ;; Output everything instead of the below
1316 output (concat output (substring gud-marker-acc 0 (match-end 0)))
1317 ;; ;; Append any text before the marker to the output we're going
1318 ;; ;; to return - we don't include the marker in this text.
1319 ;; output (concat output
1320 ;; (substring gud-marker-acc 0 (match-beginning 0)))
1321
1322 ;; Set the accumulator to the remaining text.
1323 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1324
1325 ;; Does the remaining text look like it might end with the
1326 ;; beginning of another marker? If it does, then keep it in
1327 ;; gud-marker-acc until we receive the rest of it. Since we
1328 ;; know the full marker regexp above failed, it's pretty simple to
1329 ;; test for marker starts.
1330 (if (string-match gud-pdb-marker-regexp-start gud-marker-acc)
1331 (progn
1332 ;; Everything before the potential marker start can be output.
1333 (setq output (concat output (substring gud-marker-acc
1334 0 (match-beginning 0))))
1335
1336 ;; Everything after, we save, to combine with later input.
1337 (setq gud-marker-acc
1338 (substring gud-marker-acc (match-beginning 0))))
1339
1340 (setq output (concat output gud-marker-acc)
1341 gud-marker-acc ""))
1342
1343 output))
1344
1345 (defun gud-pdb-find-file (f)
1346 (save-excursion
1347 (let ((buf (find-file-noselect f)))
1348 (set-buffer buf)
1349 (gud-make-debug-menu)
1350 ;; (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1351 ;; (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1352 ;; (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1353 buf)))
1354
1355 (defvar pdb-minibuffer-local-map nil
1356 "Keymap for minibuffer prompting of pdb startup command.")
1357 (if pdb-minibuffer-local-map
1358 ()
1359 (setq pdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
1360 (define-key
1361 pdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
1362
1363 (defcustom gud-pdb-command-name "pdb"
1364 "File name for executing the Python debugger.
1365 This should be an executable on your path, or an absolute file name."
1366 :type 'string
1367 :group 'gud)
1368
1369 ;;;###autoload
1370 (defun pdb (command-line)
1371 "Run pdb on program FILE in buffer `*gud-FILE*'.
1372 The directory containing FILE becomes the initial working directory
1373 and source-file directory for your debugger."
1374 (interactive
1375 (list (read-from-minibuffer "Run pdb (like this): "
1376 (if (consp gud-pdb-history)
1377 (car gud-pdb-history)
1378 (concat gud-pdb-command-name " "))
1379 pdb-minibuffer-local-map nil
1380 '(gud-pdb-history . 1))))
1381
1382 (gud-common-init command-line 'gud-pdb-massage-args
1383 'gud-pdb-marker-filter 'gud-pdb-find-file)
1384
1385 (gud-def gud-break "break %l" "\C-b" "Set breakpoint at current line.")
1386 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1387 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1388 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1389 (gud-def gud-cont "continue" "\C-r" "Continue with display.")
1390 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
1391 (gud-def gud-up "up" "<" "Up one stack frame.")
1392 (gud-def gud-down "down" ">" "Down one stack frame.")
1393 (gud-def gud-print "p %e" "\C-p" "Evaluate Python expression at point.")
1394 ;; Is this right?
1395 (gud-def gud-statement "! %e" "\C-e" "Execute Python statement at point.")
1396
1397 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1398 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1399 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
1400 ;; (setq comint-prompt-regexp "^(.*pdb[+]?) *")
1401 (setq comint-prompt-regexp "^(Pdb) *")
1402 (setq paragraph-start comint-prompt-regexp)
1403 (run-hooks 'pdb-mode-hook))
1404 \f
1405 ;; ======================================================================
1406 ;;
1407 ;; JDB support.
1408 ;;
1409 ;; AUTHOR: Derek Davies <ddavies@world.std.com>
1410 ;;
1411 ;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1412 ;;
1413 ;; INVOCATION NOTES:
1414 ;;
1415 ;; You invoke jdb-mode with:
1416 ;;
1417 ;; M-x jdb <enter>
1418 ;;
1419 ;; It responds with:
1420 ;;
1421 ;; Run jdb (like this): jdb
1422 ;;
1423 ;; type any jdb switches followed by the name of the class you'd like to debug.
1424 ;; Supply a fully qualfied classname (these do not have the ".class" extension)
1425 ;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1426 ;; See the known problems section below for restrictions when specifying jdb
1427 ;; command line switches (search forward for '-classpath').
1428 ;;
1429 ;; You should see something like the following:
1430 ;;
1431 ;; Current directory is ~/src/java/hello/
1432 ;; Initializing jdb...
1433 ;; 0xed2f6628:class(hello)
1434 ;; >
1435 ;;
1436 ;; To set an initial breakpoint try:
1437 ;;
1438 ;; > stop in hello.main
1439 ;; Breakpoint set in hello.main
1440 ;; >
1441 ;;
1442 ;; To execute the program type:
1443 ;;
1444 ;; > run
1445 ;; run hello
1446 ;;
1447 ;; Breakpoint hit: running ...
1448 ;; hello.main (hello:12)
1449 ;;
1450 ;; Type M-n to step over the current line and M-s to step into it. That,
1451 ;; along with the JDB 'help' command should get you started. The 'quit'
1452 ;; JDB command will get out out of the debugger. There is some truly
1453 ;; pathetic JDB documentation available at:
1454 ;;
1455 ;; http://java.sun.com/products/jdk/1.1/debugging/
1456 ;;
1457 ;; KNOWN PROBLEMS AND FIXME's:
1458 ;;
1459 ;; Not sure what happens with inner classes ... haven't tried them.
1460 ;;
1461 ;; Does not grok UNICODE id's. Only ASCII id's are supported.
1462 ;;
1463 ;; You must not put whitespace between "-classpath" and the path to
1464 ;; search for java classes even though it is required when invoking jdb
1465 ;; from the command line. See gud-jdb-massage-args for details.
1466 ;;
1467 ;; If any of the source files in the directories listed in
1468 ;; gud-jdb-directories won't parse you'll have problems. Make sure
1469 ;; every file ending in ".java" in these directories parses without error.
1470 ;;
1471 ;; All the .java files in the directories in gud-jdb-directories are
1472 ;; syntactically analyzed each time gud jdb is invoked. It would be
1473 ;; nice to keep as much information as possible between runs. It would
1474 ;; be really nice to analyze the files only as neccessary (when the
1475 ;; source needs to be displayed.) I'm not sure to what extent the former
1476 ;; can be accomplished and I'm not sure the latter can be done at all
1477 ;; since I don't know of any general way to tell which .class files are
1478 ;; defined by which .java file without analyzing all the .java files.
1479 ;; If anyone knows why JavaSoft didn't put the source file names in
1480 ;; debuggable .class files please clue me in so I find something else
1481 ;; to be spiteful and bitter about.
1482 ;;
1483 ;; ======================================================================
1484 ;; gud jdb variables and functions
1485
1486 ;; History of argument lists passed to jdb.
1487 (defvar gud-jdb-history nil)
1488
1489 ;; List of Java source file directories.
1490 (defvar gud-jdb-directories (list ".")
1491 "*A list of directories that gud jdb should search for source code.
1492 The file names should be absolute, or relative to the current directory.")
1493
1494 ;; List of the java source files for this debugging session.
1495 (defvar gud-jdb-source-files nil)
1496
1497 ;; Return a list of java source files. PATH gives the directories in
1498 ;; which to search for files with extension EXTN. Normally EXTN is
1499 ;; given as the regular expression "\\.java$" .
1500 (defun gud-jdb-build-source-files-list (path extn)
1501 (apply 'nconc (mapcar (lambda (d) (directory-files d t extn nil)) path)))
1502
1503 ;; Move point past whitespace.
1504 (defun gud-jdb-skip-whitespace ()
1505 (skip-chars-forward " \n\r\t\014"))
1506
1507 ;; Move point past a "// <eol>" type of comment.
1508 (defun gud-jdb-skip-single-line-comment ()
1509 (end-of-line))
1510
1511 ;; Move point past a "/* */" or "/** */" type of comment.
1512 (defun gud-jdb-skip-traditional-or-documentation-comment ()
1513 (forward-char 2)
1514 (catch 'break
1515 (while (not (eobp))
1516 (if (eq (following-char) ?*)
1517 (progn
1518 (forward-char)
1519 (if (not (eobp))
1520 (if (eq (following-char) ?/)
1521 (progn
1522 (forward-char)
1523 (throw 'break nil)))))
1524 (forward-char)))))
1525
1526 ;; Move point past any number of consecutive whitespace chars and/or comments.
1527 (defun gud-jdb-skip-whitespace-and-comments ()
1528 (gud-jdb-skip-whitespace)
1529 (catch 'done
1530 (while t
1531 (cond
1532 ((looking-at "//")
1533 (gud-jdb-skip-single-line-comment)
1534 (gud-jdb-skip-whitespace))
1535 ((looking-at "/\\*")
1536 (gud-jdb-skip-traditional-or-documentation-comment)
1537 (gud-jdb-skip-whitespace))
1538 (t (throw 'done nil))))))
1539
1540 ;; Move point past things that are id-like. The intent is to skip regular
1541 ;; id's, such as class or interface names as well as package and interface
1542 ;; names.
1543 (defun gud-jdb-skip-id-ish-thing ()
1544 (skip-chars-forward "^ /\n\r\t\014,;{"))
1545
1546 ;; Move point past a string literal.
1547 (defun gud-jdb-skip-string-literal ()
1548 (forward-char)
1549 (while
1550 (progn
1551 (if (eq (following-char) ?\\)
1552 (forward-char 2))
1553 (not (eq (following-char) ?\042)))
1554 (forward-char))
1555 (forward-char))
1556
1557 ;; Move point past a character literal.
1558 (defun gud-jdb-skip-character-literal ()
1559 (forward-char)
1560 (while
1561 (progn
1562 (if (eq (following-char) ?\\)
1563 (forward-char 2))
1564 (not (eq (following-char) ?\')))
1565 (forward-char))
1566 (forward-char))
1567
1568 ;; Move point past the following block. There may be (legal) cruft before
1569 ;; the block's opening brace. There must be a block or it's the end of life
1570 ;; in petticoat junction.
1571 (defun gud-jdb-skip-block ()
1572
1573 ;; Find the begining of the block.
1574 (while
1575 (not (eq (following-char) ?{))
1576
1577 ;; Skip any constructs that can harbor literal block delimiter
1578 ;; characters and/or the delimiters for the constructs themselves.
1579 (cond
1580 ((looking-at "//")
1581 (gud-jdb-skip-single-line-comment))
1582 ((looking-at "/\\*")
1583 (gud-jdb-skip-traditional-or-documentation-comment))
1584 ((eq (following-char) ?\042)
1585 (gud-jdb-skip-string-literal))
1586 ((eq (following-char) ?\')
1587 (gud-jdb-skip-character-literal))
1588 (t (forward-char))))
1589
1590 ;; Now at the begining of the block.
1591 (forward-char)
1592
1593 ;; Skip over the body of the block as well as the final brace.
1594 (let ((open-level 1))
1595 (while (not (eq open-level 0))
1596 (cond
1597 ((looking-at "//")
1598 (gud-jdb-skip-single-line-comment))
1599 ((looking-at "/\\*")
1600 (gud-jdb-skip-traditional-or-documentation-comment))
1601 ((eq (following-char) ?\042)
1602 (gud-jdb-skip-string-literal))
1603 ((eq (following-char) ?\')
1604 (gud-jdb-skip-character-literal))
1605 ((eq (following-char) ?{)
1606 (setq open-level (+ open-level 1))
1607 (forward-char))
1608 ((eq (following-char) ?})
1609 (setq open-level (- open-level 1))
1610 (forward-char))
1611 (t (forward-char))))))
1612
1613 ;; Find the package and class definitions in Java source file FILE. Assumes
1614 ;; that FILE contains a legal Java program. BUF is a scratch buffer used
1615 ;; to hold the source during analysis.
1616 (defun gud-jdb-analyze-source (buf file)
1617 (let ((l nil))
1618 (set-buffer buf)
1619 (insert-file-contents file nil nil nil t)
1620 (goto-char 0)
1621 (catch 'abort
1622 (let ((p ""))
1623 (while (progn
1624 (gud-jdb-skip-whitespace)
1625 (not (eobp)))
1626 (cond
1627
1628 ;; Any number of semi's following a block is legal. Move point
1629 ;; past them. Note that comments and whitespace may be
1630 ;; interspersed as well.
1631 ((eq (following-char) ?\073)
1632 (forward-char))
1633
1634 ;; Move point past a single line comment.
1635 ((looking-at "//")
1636 (gud-jdb-skip-single-line-comment))
1637
1638 ;; Move point past a traditional or documentation comment.
1639 ((looking-at "/\\*")
1640 (gud-jdb-skip-traditional-or-documentation-comment))
1641
1642 ;; Move point past a package statement, but save the PackageName.
1643 ((looking-at "package")
1644 (forward-char 7)
1645 (gud-jdb-skip-whitespace-and-comments)
1646 (let ((s (point)))
1647 (gud-jdb-skip-id-ish-thing)
1648 (setq p (concat (buffer-substring s (point)) "."))
1649 (gud-jdb-skip-whitespace-and-comments)
1650 (if (eq (following-char) ?\073)
1651 (forward-char))))
1652
1653 ;; Move point past an import statement.
1654 ((looking-at "import")
1655 (forward-char 6)
1656 (gud-jdb-skip-whitespace-and-comments)
1657 (gud-jdb-skip-id-ish-thing)
1658 (gud-jdb-skip-whitespace-and-comments)
1659 (if (eq (following-char) ?\073)
1660 (forward-char)))
1661
1662 ;; Move point past the various kinds of ClassModifiers.
1663 ((looking-at "public")
1664 (forward-char 6))
1665 ((looking-at "abstract")
1666 (forward-char 8))
1667 ((looking-at "final")
1668 (forward-char 5))
1669
1670 ;; Move point past a ClassDeclaraction, but save the class
1671 ;; Identifier.
1672 ((looking-at "class")
1673 (forward-char 5)
1674 (gud-jdb-skip-whitespace-and-comments)
1675 (let ((s (point)))
1676 (gud-jdb-skip-id-ish-thing)
1677 (setq
1678 l (nconc l (list (concat p (buffer-substring s (point)))))))
1679 (gud-jdb-skip-block))
1680
1681 ;; Move point past an interface statement.
1682 ((looking-at "interface")
1683 (forward-char 9)
1684 (gud-jdb-skip-block))
1685
1686 ;; Anything else means the input is invalid.
1687 (t
1688 (message (format "Error parsing file %s." file))
1689 (throw 'abort nil))))))
1690 l))
1691
1692 (defun gud-jdb-build-class-source-alist-for-file (file)
1693 (mapcar
1694 (lambda (c)
1695 (cons c file))
1696 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
1697
1698 ;; Association list of fully qualified class names (package + class name) and
1699 ;; their source files.
1700 (defvar gud-jdb-class-source-alist nil)
1701
1702 ;; This is used to hold a source file during analysis.
1703 (defvar gud-jdb-analysis-buffer nil)
1704
1705 ;; Return an alist of fully qualified classes and the source files
1706 ;; holding their definitions. SOURCES holds a list of all the source
1707 ;; files to examine.
1708 (defun gud-jdb-build-class-source-alist (sources)
1709 (setq gud-jdb-analysis-buffer (get-buffer-create "*gud-jdb-scratch*"))
1710 (prog1
1711 (apply
1712 'nconc
1713 (mapcar
1714 'gud-jdb-build-class-source-alist-for-file
1715 sources))
1716 (kill-buffer gud-jdb-analysis-buffer)
1717 (setq gud-jdb-analysis-buffer nil)))
1718
1719 ;; Change what was given in the minibuffer to something that can be used to
1720 ;; invoke the debugger.
1721 (defun gud-jdb-massage-args (file args)
1722 ;; The jdb executable must have whitespace between "-classpath" and
1723 ;; its value while gud-common-init expects all switch values to
1724 ;; follow the switch keyword without intervening whitespace. We
1725 ;; require that when the user enters the "-classpath" switch in the
1726 ;; EMACS minibuffer that they do so without the intervening
1727 ;; whitespace. This function adds it back (it's called after
1728 ;; gud-common-init). There are more switches like this (for
1729 ;; instance "-host" and "-password") but I don't care about them
1730 ;; yet.
1731 (if args
1732 (let (massaged-args user-error)
1733
1734 (while
1735 (and args
1736 (not (string-match "-classpath\\(.+\\)" (car args)))
1737 (not (setq user-error
1738 (string-match "-classpath$" (car args)))))
1739 (setq massaged-args (append massaged-args (list (car args))))
1740 (setq args (cdr args)))
1741
1742 ;; By this point the current directory is all screwed up. Maybe we
1743 ;; could fix things and re-invoke gud-common-init, but for now I think
1744 ;; issueing the error is good enough.
1745 (if user-error
1746 (progn
1747 (kill-buffer (current-buffer))
1748 (error "Error: Omit whitespace between '-classpath' and it's value")))
1749
1750 (if args
1751 (setq massaged-args
1752 (append
1753 massaged-args
1754 (list "-classpath")
1755 (list
1756 (substring
1757 (car args)
1758 (match-beginning 1) (match-end 1)))
1759 (cdr args)))
1760 massaged-args))))
1761
1762 ;; Search for an association with P, a fully qualified class name, in
1763 ;; gud-jdb-class-source-alist. The asssociation gives the fully
1764 ;; qualified file name of the source file which produced the class.
1765 (defun gud-jdb-find-source-file (p)
1766 (cdr (assoc p gud-jdb-class-source-alist)))
1767
1768 ;; See comentary for other debugger's marker filters - there you will find
1769 ;; important notes about STRING.
1770 (defun gud-jdb-marker-filter (string)
1771
1772 ;; Build up the accumulator.
1773 (setq gud-marker-acc
1774 (if gud-marker-acc
1775 (concat gud-marker-acc string)
1776 string))
1777
1778 ;; We process STRING from left to right. Each time through the following
1779 ;; loop we process at most one marker. The start variable keeps track of
1780 ;; where we are in the input string through the iterations of this loop.
1781 (let (start file-found)
1782
1783 ;; Process each complete marker in the input. There may be an incomplete
1784 ;; marker at the end of the input string. Incomplete markers are left
1785 ;; in the accumulator for processing the next time the function is called.
1786 (while
1787
1788 ;; Do we see a marker?
1789 (string-match
1790 ;; jdb puts out a string of the following form when it
1791 ;; hits a breakpoint:
1792 ;;
1793 ;; <fully-qualified-class><method> (<class>:<line-number>)
1794 ;;
1795 ;; <fully-qualified-class>'s are composed of Java ID's
1796 ;; separated by periods. <method> and <class> are
1797 ;; also Java ID's. <method> begins with a period and
1798 ;; may contain less-than and greater-than (constructors,
1799 ;; for instance, are called <init> in the symbol table.)
1800 ;; Java ID's begin with a letter followed by letters
1801 ;; and/or digits. The set of letters includes underscore
1802 ;; and dollar sign.
1803 ;;
1804 ;; The first group matches <fully-qualified-class>,
1805 ;; the second group matches <class> and the third group
1806 ;; matches <line-number>. We don't care about using
1807 ;; <method> so we don't "group" it.
1808 ;;
1809 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1810 ;; ID's only.
1811 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1812 gud-marker-acc start)
1813
1814 ;; Figure out the line on which to position the debugging arrow.
1815 ;; Return the info as a cons of the form:
1816 ;;
1817 ;; (<file-name> . <line-number>) .
1818 (if (setq
1819 file-found
1820 (gud-jdb-find-source-file
1821 (substring gud-marker-acc
1822 (match-beginning 1)
1823 (match-end 1))))
1824 (setq gud-last-frame
1825 (cons
1826 file-found
1827 (string-to-int
1828 (substring gud-marker-acc
1829 (match-beginning 3)
1830 (match-end 3)))))
1831 (message "Could not find source file."))
1832
1833 ;; Set start after the last character of STRING that we've looked at
1834 ;; and loop to look for another marker.
1835 (setq start (match-end 0))))
1836
1837 ;; We don't filter any debugger output so just return what we were given.
1838 string)
1839
1840 (defun gud-jdb-find-file (f)
1841 (and (file-readable-p f)
1842 (find-file-noselect f)))
1843
1844 (defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1845
1846 ;;;###autoload
1847 (defun jdb (command-line)
1848 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1849 \"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1850 if there is. If the \"-classpath\" switch is given, omit all whitespace
1851 between it and it's value."
1852 (interactive
1853 (list (read-from-minibuffer "Run jdb (like this): "
1854 (if (consp gud-jdb-history)
1855 (car gud-jdb-history)
1856 (concat gud-jdb-command-name " "))
1857 nil nil
1858 '(gud-jdb-history . 1))))
1859
1860 (gud-common-init command-line 'gud-jdb-massage-args
1861 'gud-jdb-marker-filter 'gud-jdb-find-file)
1862
1863 (gud-def gud-break "stop at %l" "\C-b" "Set breakpoint at current line.")
1864 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1865 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1866 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1867 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1868
1869 (setq comint-prompt-regexp "^> \|^.+\[[0-9]+\] ")
1870 (setq paragraph-start comint-prompt-regexp)
1871 (run-hooks 'jdb-mode-hook)
1872
1873 ;; Create and bind the class/source association list as well as the source
1874 ;; file list.
1875 (setq
1876 gud-jdb-class-source-alist
1877 (gud-jdb-build-class-source-alist
1878 (setq
1879 gud-jdb-source-files
1880 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1881 \f
1882
1883 ;;
1884 ;; End of debugger-specific information
1885 ;;
1886
1887 \f
1888 ;;; When we send a command to the debugger via gud-call, it's annoying
1889 ;;; to see the command and the new prompt inserted into the debugger's
1890 ;;; buffer; we have other ways of knowing the command has completed.
1891 ;;;
1892 ;;; If the buffer looks like this:
1893 ;;; --------------------
1894 ;;; (gdb) set args foo bar
1895 ;;; (gdb) -!-
1896 ;;; --------------------
1897 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1898 ;;; source file to set a breakpoint, we want the buffer to end up like
1899 ;;; this:
1900 ;;; --------------------
1901 ;;; (gdb) set args foo bar
1902 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1903 ;;; (gdb) -!-
1904 ;;; --------------------
1905 ;;; Essentially, the old prompt is deleted, and the command's output
1906 ;;; and the new prompt take its place.
1907 ;;;
1908 ;;; Not echoing the command is easy enough; you send it directly using
1909 ;;; process-send-string, and it never enters the buffer. However,
1910 ;;; getting rid of the old prompt is trickier; you don't want to do it
1911 ;;; when you send the command, since that will result in an annoying
1912 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1913 ;;; waits for a response from the debugger, and the new prompt is
1914 ;;; inserted. Instead, we'll wait until we actually get some output
1915 ;;; from the subprocess before we delete the prompt. If the command
1916 ;;; produced no output other than a new prompt, that prompt will most
1917 ;;; likely be in the first chunk of output received, so we will delete
1918 ;;; the prompt and then replace it with an identical one. If the
1919 ;;; command produces output, the prompt is moving anyway, so the
1920 ;;; flicker won't be annoying.
1921 ;;;
1922 ;;; So - when we want to delete the prompt upon receipt of the next
1923 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1924 ;;; the start of the prompt; the process filter will notice this, and
1925 ;;; delete all text between it and the process output marker. If
1926 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1927 ;;; prompt alone.
1928 (defvar gud-delete-prompt-marker nil)
1929
1930 \f
1931 (put 'gud-mode 'mode-class 'special)
1932
1933 (defun gud-mode ()
1934 "Major mode for interacting with an inferior debugger process.
1935
1936 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1937 M-x perldb, or M-x xdb. Each entry point finishes by executing a
1938 hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
1939 `perldb-mode-hook', or `xdb-mode-hook' respectively.
1940
1941 After startup, the following commands are available in both the GUD
1942 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1943 or step operation:
1944
1945 \\[gud-break] sets a breakpoint at the current file and line. In the
1946 GUD buffer, the current file and line are those of the last breakpoint or
1947 step. In a source buffer, they are the buffer's file and current line.
1948
1949 \\[gud-remove] removes breakpoints on the current file and line.
1950
1951 \\[gud-refresh] displays in the source window the last line referred to
1952 in the gud buffer.
1953
1954 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1955 step-one-line (not entering function calls), and step-one-instruction
1956 and then update the source window with the current file and position.
1957 \\[gud-cont] continues execution.
1958
1959 \\[gud-print] tries to find the largest C lvalue or function-call expression
1960 around point, and sends it to the debugger for value display.
1961
1962 The above commands are common to all supported debuggers except xdb which
1963 does not support stepping instructions.
1964
1965 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1966 except that the breakpoint is temporary; that is, it is removed when
1967 execution stops on it.
1968
1969 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1970 frame. \\[gud-down] drops back down through one.
1971
1972 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1973 the current function and stops.
1974
1975 All the keystrokes above are accessible in the GUD buffer
1976 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1977
1978 All pre-defined functions for which the concept make sense repeat
1979 themselves the appropriate number of times if you give a prefix
1980 argument.
1981
1982 You may use the `gud-def' macro in the initialization hook to define other
1983 commands.
1984
1985 Other commands for interacting with the debugger process are inherited from
1986 comint mode, which see."
1987 (interactive)
1988 (comint-mode)
1989 (setq major-mode 'gud-mode)
1990 (setq mode-name "Debugger")
1991 (setq mode-line-process '(":%s"))
1992 (use-local-map comint-mode-map)
1993 (gud-make-debug-menu)
1994 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
1995 (make-local-variable 'gud-last-frame)
1996 (setq gud-last-frame nil)
1997 (make-local-variable 'comint-prompt-regexp)
1998 ;; Don't put repeated commands in command history many times.
1999 (make-local-variable 'comint-input-ignoredups)
2000 (setq comint-input-ignoredups t)
2001 (make-local-variable 'paragraph-start)
2002 (make-local-variable 'gud-delete-prompt-marker)
2003 (setq gud-delete-prompt-marker (make-marker))
2004 (run-hooks 'gud-mode-hook))
2005
2006 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
2007 (defun gud-chop-words (string)
2008 (let ((i 0) (beg 0)
2009 (len (length string))
2010 (words nil))
2011 (while (< i len)
2012 (if (memq (aref string i) '(?\t ? ))
2013 (progn
2014 (setq words (cons (substring string beg i) words)
2015 beg (1+ i))
2016 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
2017 (setq beg (1+ beg)))
2018 (setq i (1+ beg)))
2019 (setq i (1+ i))))
2020 (if (< beg len)
2021 (setq words (cons (substring string beg) words)))
2022 (nreverse words)))
2023
2024 ;; Perform initializations common to all debuggers.
2025 ;; The first arg is the specified command line,
2026 ;; which starts with the program to debug.
2027 ;; The other three args specify the values to use
2028 ;; for local variables in the debugger buffer.
2029 (defun gud-common-init (command-line massage-args marker-filter find-file)
2030 (let* ((words (gud-chop-words command-line))
2031 (program (car words))
2032 ;; Extract the file name from WORDS
2033 ;; and put t in its place.
2034 ;; Later on we will put the modified file name arg back there.
2035 (file-word (let ((w (cdr words)))
2036 (while (and w (= ?- (aref (car w) 0)))
2037 (setq w (cdr w)))
2038 (and w
2039 (prog1 (car w)
2040 (setcar w t)))))
2041 (file-subst
2042 (and file-word (substitute-in-file-name file-word)))
2043 (args (cdr words))
2044 ;; If a directory was specified, expand the file name.
2045 ;; Otherwise, don't expand it, so GDB can use the PATH.
2046 ;; A file name without directory is literally valid
2047 ;; only if the file exists in ., and in that case,
2048 ;; omitting the expansion here has no visible effect.
2049 (file (and file-word
2050 (if (file-name-directory file-subst)
2051 (expand-file-name file-subst)
2052 file-subst)))
2053 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
2054 (switch-to-buffer (concat "*gud" filepart "*"))
2055 ;; Set default-directory to the file's directory.
2056 (and file-word
2057 ;; Don't set default-directory if no directory was specified.
2058 ;; In that case, either the file is found in the current directory,
2059 ;; in which case this setq is a no-op,
2060 ;; or it is found by searching PATH,
2061 ;; in which case we don't know what directory it was found in.
2062 (file-name-directory file)
2063 (setq default-directory (file-name-directory file)))
2064 (or (bolp) (newline))
2065 (insert "Current directory is " default-directory "\n")
2066 ;; Put the substituted and expanded file name back in its place.
2067 (let ((w args))
2068 (while (and w (not (eq (car w) t)))
2069 (setq w (cdr w)))
2070 (if w
2071 (setcar w file)))
2072 (apply 'make-comint (concat "gud" filepart) program nil
2073 (funcall massage-args file args)))
2074 ;; Since comint clobbered the mode, we don't set it until now.
2075 (gud-mode)
2076 (make-local-variable 'gud-marker-filter)
2077 (setq gud-marker-filter marker-filter)
2078 (make-local-variable 'gud-find-file)
2079 (setq gud-find-file find-file)
2080
2081 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
2082 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
2083 (gud-set-buffer)
2084 )
2085
2086 (defun gud-set-buffer ()
2087 (cond ((eq major-mode 'gud-mode)
2088 (setq gud-comint-buffer (current-buffer)))))
2089
2090 (defvar gud-filter-defer-flag nil
2091 "Non-nil means don't process anything from the debugger right now.
2092 It is saved for when this flag is not set.")
2093
2094 (defvar gud-filter-pending-text nil
2095 "Non-nil means this is text that has been saved for later in `gud-filter'.")
2096
2097 ;; These functions are responsible for inserting output from your debugger
2098 ;; into the buffer. The hard work is done by the method that is
2099 ;; the value of gud-marker-filter.
2100
2101 (defun gud-filter (proc string)
2102 ;; Here's where the actual buffer insertion is done
2103 (let (output process-window)
2104 (if (buffer-name (process-buffer proc))
2105 (if gud-filter-defer-flag
2106 ;; If we can't process any text now,
2107 ;; save it for later.
2108 (setq gud-filter-pending-text
2109 (concat (or gud-filter-pending-text "") string))
2110
2111 ;; If we have to ask a question during the processing,
2112 ;; defer any additional text that comes from the debugger
2113 ;; during that time.
2114 (let ((gud-filter-defer-flag t))
2115 ;; Process now any text we previously saved up.
2116 (if gud-filter-pending-text
2117 (setq string (concat gud-filter-pending-text string)
2118 gud-filter-pending-text nil))
2119 (save-excursion
2120 (set-buffer (process-buffer proc))
2121 ;; If we have been so requested, delete the debugger prompt.
2122 (if (marker-buffer gud-delete-prompt-marker)
2123 (progn
2124 (delete-region (process-mark proc) gud-delete-prompt-marker)
2125 (set-marker gud-delete-prompt-marker nil)))
2126 ;; Save the process output, checking for source file markers.
2127 (setq output (gud-marker-filter string))
2128 ;; Check for a filename-and-line number.
2129 ;; Don't display the specified file
2130 ;; unless (1) point is at or after the position where output appears
2131 ;; and (2) this buffer is on the screen.
2132 (setq process-window
2133 (and gud-last-frame
2134 (>= (point) (process-mark proc))
2135 (get-buffer-window (current-buffer))))
2136
2137 ;; Let the comint filter do the actual insertion.
2138 ;; That lets us inherit various comint features.
2139 (comint-output-filter proc output)))
2140
2141 ;; Put the arrow on the source line.
2142 ;; This must be outside of the save-excursion
2143 ;; in case the source file is our current buffer.
2144 (if process-window
2145 (save-selected-window
2146 (select-window process-window)
2147 (gud-display-frame))
2148 ;; We have to be in the proper buffer, (process-buffer proc),
2149 ;; but not in a save-excursion, because that would restore point.
2150 (let ((old-buf (current-buffer)))
2151 (set-buffer (process-buffer proc))
2152 (unwind-protect
2153 (gud-display-frame)
2154 (set-buffer old-buf))))
2155
2156 ;; If we deferred text that arrived during this processing,
2157 ;; handle it now.
2158 (if gud-filter-pending-text
2159 (gud-filter proc ""))))))
2160
2161 (defun gud-sentinel (proc msg)
2162 (cond ((null (buffer-name (process-buffer proc)))
2163 ;; buffer killed
2164 ;; Stop displaying an arrow in a source file.
2165 (setq overlay-arrow-position nil)
2166 (set-process-buffer proc nil))
2167 ((memq (process-status proc) '(signal exit))
2168 ;; Stop displaying an arrow in a source file.
2169 (setq overlay-arrow-position nil)
2170 (let* ((obuf (current-buffer)))
2171 ;; save-excursion isn't the right thing if
2172 ;; process-buffer is current-buffer
2173 (unwind-protect
2174 (progn
2175 ;; Write something in *compilation* and hack its mode line,
2176 (set-buffer (process-buffer proc))
2177 ;; Fix the mode line.
2178 (setq mode-line-process
2179 (concat ":"
2180 (symbol-name (process-status proc))))
2181 (force-mode-line-update)
2182 (if (eobp)
2183 (insert ?\n mode-name " " msg)
2184 (save-excursion
2185 (goto-char (point-max))
2186 (insert ?\n mode-name " " msg)))
2187 ;; If buffer and mode line will show that the process
2188 ;; is dead, we can delete it now. Otherwise it
2189 ;; will stay around until M-x list-processes.
2190 (delete-process proc))
2191 ;; Restore old buffer, but don't restore old point
2192 ;; if obuf is the gud buffer.
2193 (set-buffer obuf))))))
2194
2195 (defun gud-display-frame ()
2196 "Find and obey the last filename-and-line marker from the debugger.
2197 Obeying it means displaying in another window the specified file and line."
2198 (interactive)
2199 (if gud-last-frame
2200 (progn
2201 (gud-set-buffer)
2202 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
2203 (setq gud-last-last-frame gud-last-frame
2204 gud-last-frame nil))))
2205
2206 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2207 ;; and that its line LINE is visible.
2208 ;; Put the overlay-arrow on the line LINE in that buffer.
2209 ;; Most of the trickiness in here comes from wanting to preserve the current
2210 ;; region-restriction if that's possible. We use an explicit display-buffer
2211 ;; to get around the fact that this is called inside a save-excursion.
2212
2213 (defun gud-display-line (true-file line)
2214 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
2215 (buffer
2216 (save-excursion
2217 (or (eq (current-buffer) gud-comint-buffer)
2218 (set-buffer gud-comint-buffer))
2219 (gud-find-file true-file)))
2220 (window (and buffer (or (get-buffer-window buffer)
2221 (display-buffer buffer))))
2222 (pos))
2223 (if buffer
2224 (progn
2225 (save-excursion
2226 (set-buffer buffer)
2227 (save-restriction
2228 (widen)
2229 (goto-line line)
2230 (setq pos (point))
2231 (setq overlay-arrow-string "=>")
2232 (or overlay-arrow-position
2233 (setq overlay-arrow-position (make-marker)))
2234 (set-marker overlay-arrow-position (point) (current-buffer)))
2235 (cond ((or (< pos (point-min)) (> pos (point-max)))
2236 (widen)
2237 (goto-char pos))))
2238 (set-window-point window overlay-arrow-position)))))
2239
2240 ;;; The gud-call function must do the right thing whether its invoking
2241 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
2242 ;;; or a C buffer. In the former case, we want to supply data from
2243 ;;; gud-last-frame. Here's how we do it:
2244
2245 (defun gud-format-command (str arg)
2246 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2247 (frame (or gud-last-frame gud-last-last-frame))
2248 result)
2249 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2250 (let ((key (string-to-char (substring str (match-beginning 2))))
2251 subst)
2252 (cond
2253 ((eq key ?f)
2254 (setq subst (file-name-nondirectory (if insource
2255 (buffer-file-name)
2256 (car frame)))))
2257 ((eq key ?d)
2258 (setq subst (file-name-directory (if insource
2259 (buffer-file-name)
2260 (car frame)))))
2261 ((eq key ?l)
2262 (setq subst (if insource
2263 (save-excursion
2264 (beginning-of-line)
2265 (save-restriction (widen)
2266 (1+ (count-lines 1 (point)))))
2267 (cdr frame))))
2268 ((eq key ?e)
2269 (setq subst (gud-find-c-expr)))
2270 ((eq key ?a)
2271 (setq subst (gud-read-address)))
2272 ((eq key ?p)
2273 (setq subst (if arg (int-to-string arg) ""))))
2274 (setq result (concat result
2275 (substring str (match-beginning 1) (match-end 1))
2276 subst)))
2277 (setq str (substring str (match-end 2))))
2278 ;; There might be text left in STR when the loop ends.
2279 (concat result str)))
2280
2281 (defun gud-read-address ()
2282 "Return a string containing the core-address found in the buffer at point."
2283 (save-excursion
2284 (let ((pt (point)) found begin)
2285 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
2286 (cond
2287 (found (forward-char 2)
2288 (buffer-substring found
2289 (progn (re-search-forward "[^0-9a-f]")
2290 (forward-char -1)
2291 (point))))
2292 (t (setq begin (progn (re-search-backward "[^0-9]")
2293 (forward-char 1)
2294 (point)))
2295 (forward-char 1)
2296 (re-search-forward "[^0-9]")
2297 (forward-char -1)
2298 (buffer-substring begin (point)))))))
2299
2300 (defun gud-call (fmt &optional arg)
2301 (let ((msg (gud-format-command fmt arg)))
2302 (message "Command: %s" msg)
2303 (sit-for 0)
2304 (gud-basic-call msg)))
2305
2306 (defun gud-basic-call (command)
2307 "Invoke the debugger COMMAND displaying source in other window."
2308 (interactive)
2309 (gud-set-buffer)
2310 (let ((command (concat command "\n"))
2311 (proc (get-buffer-process gud-comint-buffer)))
2312 (or proc (error "Current buffer has no process"))
2313 ;; Arrange for the current prompt to get deleted.
2314 (save-excursion
2315 (set-buffer gud-comint-buffer)
2316 (goto-char (process-mark proc))
2317 (beginning-of-line)
2318 (if (looking-at comint-prompt-regexp)
2319 (set-marker gud-delete-prompt-marker (point))))
2320 (process-send-string proc command)))
2321
2322 (defun gud-refresh (&optional arg)
2323 "Fix up a possibly garbled display, and redraw the arrow."
2324 (interactive "P")
2325 (recenter arg)
2326 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
2327 (gud-display-frame))
2328 \f
2329
2330 (defun gud-new-keymap (map)
2331 "Return a new keymap which inherits from MAP and has name `Gud'."
2332 (nconc (make-sparse-keymap "Gud") map))
2333
2334 (defun gud-make-debug-menu ()
2335 "Make sure the current local map has a [menu-bar debug] submap.
2336 If it doesn't, replace it with a new map that inherits it,
2337 and create such a submap in that new map."
2338 (if (and (current-local-map)
2339 (lookup-key (current-local-map) [menu-bar debug]))
2340 nil
2341 (use-local-map (gud-new-keymap (current-local-map)))
2342 (define-key (current-local-map) [menu-bar debug]
2343 (cons "Gud" (gud-new-keymap gud-menu-map)))))
2344 \f
2345 ;;; Code for parsing expressions out of C code. The single entry point is
2346 ;;; find-c-expr, which tries to return an lvalue expression from around point.
2347 ;;;
2348 ;;; The rest of this file is a hacked version of gdbsrc.el by
2349 ;;; Debby Ayers <ayers@asc.slb.com>,
2350 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
2351
2352 (defun gud-find-c-expr ()
2353 "Returns the C expr that surrounds point."
2354 (interactive)
2355 (save-excursion
2356 (let (p expr test-expr)
2357 (setq p (point))
2358 (setq expr (gud-innermost-expr))
2359 (setq test-expr (gud-prev-expr))
2360 (while (and test-expr (gud-expr-compound test-expr expr))
2361 (let ((prev-expr expr))
2362 (setq expr (cons (car test-expr) (cdr expr)))
2363 (goto-char (car expr))
2364 (setq test-expr (gud-prev-expr))
2365 ;; If we just pasted on the condition of an if or while,
2366 ;; throw it away again.
2367 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2368 '("if" "while" "for"))
2369 (setq test-expr nil
2370 expr prev-expr))))
2371 (goto-char p)
2372 (setq test-expr (gud-next-expr))
2373 (while (gud-expr-compound expr test-expr)
2374 (setq expr (cons (car expr) (cdr test-expr)))
2375 (setq test-expr (gud-next-expr))
2376 )
2377 (buffer-substring (car expr) (cdr expr)))))
2378
2379 (defun gud-innermost-expr ()
2380 "Returns the smallest expr that point is in; move point to beginning of it.
2381 The expr is represented as a cons cell, where the car specifies the point in
2382 the current buffer that marks the beginning of the expr and the cdr specifies
2383 the character after the end of the expr."
2384 (let ((p (point)) begin end)
2385 (gud-backward-sexp)
2386 (setq begin (point))
2387 (gud-forward-sexp)
2388 (setq end (point))
2389 (if (>= p end)
2390 (progn
2391 (setq begin p)
2392 (goto-char p)
2393 (gud-forward-sexp)
2394 (setq end (point)))
2395 )
2396 (goto-char begin)
2397 (cons begin end)))
2398
2399 (defun gud-backward-sexp ()
2400 "Version of `backward-sexp' that catches errors."
2401 (condition-case nil
2402 (backward-sexp)
2403 (error t)))
2404
2405 (defun gud-forward-sexp ()
2406 "Version of `forward-sexp' that catches errors."
2407 (condition-case nil
2408 (forward-sexp)
2409 (error t)))
2410
2411 (defun gud-prev-expr ()
2412 "Returns the previous expr, point is set to beginning of that expr.
2413 The expr is represented as a cons cell, where the car specifies the point in
2414 the current buffer that marks the beginning of the expr and the cdr specifies
2415 the character after the end of the expr"
2416 (let ((begin) (end))
2417 (gud-backward-sexp)
2418 (setq begin (point))
2419 (gud-forward-sexp)
2420 (setq end (point))
2421 (goto-char begin)
2422 (cons begin end)))
2423
2424 (defun gud-next-expr ()
2425 "Returns the following expr, point is set to beginning of that expr.
2426 The expr is represented as a cons cell, where the car specifies the point in
2427 the current buffer that marks the beginning of the expr and the cdr specifies
2428 the character after the end of the expr."
2429 (let ((begin) (end))
2430 (gud-forward-sexp)
2431 (gud-forward-sexp)
2432 (setq end (point))
2433 (gud-backward-sexp)
2434 (setq begin (point))
2435 (cons begin end)))
2436
2437 (defun gud-expr-compound-sep (span-start span-end)
2438 "Scan from SPAN-START to SPAN-END for punctuation characters.
2439 If `->' is found, return `?.'. If `.' is found, return `?.'.
2440 If any other punctuation is found, return `??'.
2441 If no punctuation is found, return `? '."
2442 (let ((result ?\ )
2443 (syntax))
2444 (while (< span-start span-end)
2445 (setq syntax (char-syntax (char-after span-start)))
2446 (cond
2447 ((= syntax ?\ ) t)
2448 ((= syntax ?.) (setq syntax (char-after span-start))
2449 (cond
2450 ((= syntax ?.) (setq result ?.))
2451 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2452 (setq result ?.)
2453 (setq span-start (+ span-start 1)))
2454 (t (setq span-start span-end)
2455 (setq result ??)))))
2456 (setq span-start (+ span-start 1)))
2457 result))
2458
2459 (defun gud-expr-compound (first second)
2460 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
2461 The two exprs are represented as a cons cells, where the car
2462 specifies the point in the current buffer that marks the beginning of the
2463 expr and the cdr specifies the character after the end of the expr.
2464 Link exprs of the form:
2465 Expr -> Expr
2466 Expr . Expr
2467 Expr (Expr)
2468 Expr [Expr]
2469 (Expr) Expr
2470 [Expr] Expr"
2471 (let ((span-start (cdr first))
2472 (span-end (car second))
2473 (syntax))
2474 (setq syntax (gud-expr-compound-sep span-start span-end))
2475 (cond
2476 ((= (car first) (car second)) nil)
2477 ((= (cdr first) (cdr second)) nil)
2478 ((= syntax ?.) t)
2479 ((= syntax ?\ )
2480 (setq span-start (char-after (- span-start 1)))
2481 (setq span-end (char-after span-end))
2482 (cond
2483 ((= span-start ?)) t)
2484 ((= span-start ?]) t)
2485 ((= span-end ?() t)
2486 ((= span-end ?[) t)
2487 (t nil)))
2488 (t nil))))
2489
2490 (provide 'gud)
2491
2492 ;;; gud.el ends here