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