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