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