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