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