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