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