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