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