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