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