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