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