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