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