(setup-8-bit-environment):
[bpt/emacs.git] / lisp / gud.el
CommitLineData
c3fd0eea 1;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
f961a17c 2
ee0155df 3;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
8ba194fc 4;; Maintainer: FSF
f961a17c
ER
5;; Keywords: unix, tools
6
c3fd0eea 7;; Copyright (C) 1992, 93, 94, 95, 96, 1998 Free Software Foundation, Inc.
13b80a60
ER
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
24d725c2 13;; the Free Software Foundation; either version 2, or (at your option)
13b80a60
ER
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
b578f267
EN
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.
13b80a60 25
f961a17c
ER
26;;; Commentary:
27
13b80a60 28;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
24d725c2 29;; It was later rewritten by rms. Some ideas were due to Masanobu.
f961a17c 30;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
13b80a60 31;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
6496f3d2 32;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
0e0de659 33;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
f266485d 34;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
2291bfaa 35;; added the IRIX kluge, re-implemented the Mips-ish variant and added
3b3703fa 36;; a menu. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
c3fd0eea
RS
37;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
38;; <ddavies@world.std.com> added support for jdb (Java debugger.)
13b80a60 39
f961a17c
ER
40;;; Code:
41
13b80a60 42(require 'comint)
e8a57935 43(require 'etags)
13b80a60 44
ee0155df 45;; ======================================================================
5b08a462 46;; GUD commands must be visible in C buffers visited by GUD
ee0155df 47
69c1dd37 48(defgroup gud nil
f6aa627c 49 "Grand Unified Debugger mode for gdb, sdb, dbx, xdb or perldb under Emacs."
69c1dd37
RS
50 :group 'unix
51 :group 'tools)
52
53
54(defcustom gud-key-prefix "\C-x\C-a"
55 "Prefix of all GUD commands valid in C buffers."
56 :type 'string
57 :group 'gud)
ee0155df 58
5b08a462 59(global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
16776e8d 60(define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
ee0155df 61
ee97eac3
RS
62(defvar gud-marker-filter nil)
63(put 'gud-marker-filter 'permanent-local t)
64(defvar gud-find-file nil)
65(put 'gud-find-file 'permanent-local t)
66
ee97eac3
RS
67(defun gud-marker-filter (&rest args)
68 (apply gud-marker-filter args))
69
70(defun gud-find-file (file)
71 ;; Don't get confused by double slashes in the name that comes from GDB.
72 (while (string-match "//+" file)
73 (setq file (replace-match "/" t t file)))
74 (funcall gud-find-file file))
9bb4544b
RS
75
76;; Keymap definitions for menu bar entries common to all debuggers and
77;; slots for debugger-dependent ones in sensible places. (Defined here
78;; before use.)
79(defvar gud-menu-map (make-sparse-keymap "Gud") nil)
80(define-key gud-menu-map [refresh] '("Refresh" . gud-refresh))
b3ede706 81(define-key gud-menu-map [remove] '("Remove Breakpoint" . gud-remove))
9bb4544b 82(define-key gud-menu-map [tbreak] nil) ; gdb, sdb and xdb
b3ede706 83(define-key gud-menu-map [break] '("Set Breakpoint" . gud-break))
9bb4544b
RS
84(define-key gud-menu-map [up] nil) ; gdb, dbx, and xdb
85(define-key gud-menu-map [down] nil) ; gdb, dbx, and xdb
b3ede706 86(define-key gud-menu-map [print] '("Print Expression" . gud-print))
9bb4544b 87(define-key gud-menu-map [finish] nil) ; gdb or xdb
b3ede706
KH
88(define-key gud-menu-map [stepi] '("Step Instruction" . gud-stepi))
89(define-key gud-menu-map [step] '("Step Line" . gud-step))
90(define-key gud-menu-map [next] '("Next Line" . gud-next))
9bb4544b 91(define-key gud-menu-map [cont] '("Continue" . gud-cont))
575661b1 92\f
ee0155df
ER
93;; ======================================================================
94;; command definition
13b80a60
ER
95
96;; This macro is used below to define some basic debugger interface commands.
f961a17c 97;; Of course you may use `gud-def' with any other debugger command, including
6bde8427
JB
98;; user defined ones.
99
100;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
101;; which defines FUNC to send the command NAME to the debugger, gives
ee0155df 102;; it the docstring DOC, and binds that function to KEY in the GUD
5b08a462
ER
103;; major mode. The function is also bound in the global keymap with the
104;; GUD prefix.
ee0155df
ER
105
106(defmacro gud-def (func cmd key &optional doc)
107 "Define FUNC to be a command sending STR and bound to KEY, with
108optional doc string DOC. Certain %-escapes in the string arguments
109are interpreted specially if present. These are:
110
7f359a21
RS
111 %f name (without directory) of current source file.
112 %d directory of current source file.
ee0155df
ER
113 %l number of current source line
114 %e text of the C lvalue or function-call expression surrounding point.
115 %a text of the hexadecimal address surrounding point
116 %p prefix argument to the command (if any) as a number
117
5b08a462
ER
118 The `current' source file is the file of the current buffer (if
119we're in a C file) or the source file current at the last break or
120step (if we're in the GUD buffer).
121 The `current' line is that of the current buffer (if we're in a
122source file) or the source line number at the last break or step (if
123we're in the GUD buffer)."
ee0155df
ER
124 (list 'progn
125 (list 'defun func '(arg)
126 (or doc "")
127 '(interactive "p")
128 (list 'gud-call cmd 'arg))
129 (if key
dd8e46c7
RS
130 (list 'define-key
131 '(current-local-map)
132 (concat "\C-c" key)
133 (list 'quote func)))
134 (if key
135 (list 'global-set-key
2352cbea 136 (list 'concat 'gud-key-prefix key)
dd8e46c7 137 (list 'quote func)))))
13b80a60 138
d840a865
JB
139;; Where gud-display-frame should put the debugging arrow. This is
140;; set by the marker-filter, which scans the debugger's output for
ee0155df 141;; indications of the current program counter.
d840a865
JB
142(defvar gud-last-frame nil)
143
32ab4c10
JB
144;; Used by gud-refresh, which should cause gud-display-frame to redisplay
145;; the last frame, even if it's been called before and gud-last-frame has
146;; been set to nil.
7447c37a 147(defvar gud-last-last-frame nil)
32ab4c10 148
ee0155df
ER
149;; All debugger-specific information is collected here.
150;; Here's how it works, in case you ever need to add a debugger to the mode.
13b80a60
ER
151;;
152;; Each entry must define the following at startup:
153;;
154;;<name>
155;; comint-prompt-regexp
a223b10d 156;; gud-<name>-massage-args
13b80a60 157;; gud-<name>-marker-filter
ee0155df 158;; gud-<name>-find-file
13b80a60 159;;
a223b10d
RM
160;; The job of the massage-args method is to modify the given list of
161;; debugger arguments before running the debugger.
10a4c11f
JB
162;;
163;; The job of the marker-filter method is to detect file/line markers in
164;; strings and set the global gud-last-frame to indicate what display
165;; action (if any) should be triggered by the marker. Note that only
eb8c3be9 166;; whatever the method *returns* is displayed in the buffer; thus, you
10a4c11f
JB
167;; can filter the debugger's output, interpreting some and passing on
168;; the rest.
169;;
ee0155df 170;; The job of the find-file method is to visit and return the buffer indicated
10a4c11f 171;; by the car of gud-tag-frame. This may be a file name, a tag name, or
2291bfaa 172;; something else. It would be good if it also copied the Gud menubar entry.
575661b1 173\f
e9a918f9
EL
174;; ======================================================================
175;; speedbar support functions and variables.
176(defvar gud-last-speedbar-buffer nil
177 "The last GUD buffer used.")
178
179(defvar gud-last-speedbar-stackframe nil
180 "Description of the currently displayed GUD stack.
181t means that there is no stack, and we are in display-file mode.")
182
183(defvar gud-speedbar-menu-items
184 ;; Note to self. Add expand, and turn off items when not available.
185 '(["Jump to stack frame" speedbar-edit-line t])
186 "Additional menu items to add the the speedbar frame.")
187
188(defun gud-speedbar-buttons (buffer)
189 "Create a speedbar display based on the current state of GUD.
190If the GUD BUFFER is not running a supported debugger, then turn
191off the specialized speedbar mode."
192 (if (and (save-excursion (goto-char (point-min))
193 (looking-at "Current Stack"))
194 (equal gud-last-last-frame gud-last-speedbar-stackframe))
195 nil
196 (setq gud-last-speedbar-buffer buffer)
197 (let* ((ff (save-excursion (set-buffer buffer) gud-find-file))
198 ;;(lf (save-excursion (set-buffer buffer) gud-last-last-frame))
199 (frames
200 (cond ((eq ff 'gud-gdb-find-file)
201 (gud-gdb-get-stackframe buffer)
202 )
203 ;; Add more debuggers here!
204 (t
205 (speedbar-remove-localized-speedbar-support buffer)
206 nil))))
207 (erase-buffer)
208 (if (not frames)
209 (insert "No Stack frames\n")
210 (insert "Current Stack:\n"))
211 (while frames
212 (insert (nth 1 (car frames)) ":\n")
213 (if (= (length (car frames)) 2)
214 (progn
215; (speedbar-insert-button "[?]"
216; 'speedbar-button-face
217; nil nil nil t)
218 (speedbar-insert-button (car (car frames))
219 'speedbar-directory-face
220 nil nil nil t))
221; (speedbar-insert-button "[+]"
222; 'speedbar-button-face
223; 'speedbar-highlight-face
224; 'gud-gdb-get-scope-data
225; (car frames) t)
226 (speedbar-insert-button (car (car frames))
227 'speedbar-file-face
228 'speedbar-highlight-face
229 (cond ((eq ff 'gud-gdb-find-file)
230 'gud-gdb-goto-stackframe)
231 (t (error "Should never be here.")))
232 (car frames) t))
233 (setq frames (cdr frames)))
234; (let ((selected-frame
235; (cond ((eq ff 'gud-gdb-find-file)
236; (gud-gdb-selected-frame-info buffer))
237; (t (error "Should never be here."))))))
238 )
239 (setq gud-last-speedbar-stackframe gud-last-last-frame)))
240
241\f
13b80a60
ER
242;; ======================================================================
243;; gdb functions
244
f6376199
JB
245;;; History of argument lists passed to gdb.
246(defvar gud-gdb-history nil)
247
a223b10d 248(defun gud-gdb-massage-args (file args)
dfbd82a6 249 (cons "-fullname" args))
13b80a60 250
dfb7d195 251(defvar gud-gdb-marker-regexp
1ac95fab
RS
252 ;; This used to use path-separator instead of ":";
253 ;; however, we found that on both Windows 32 and MSDOS
254 ;; a colon is correct here.
15cf3ecc 255 (concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
1ac95fab 256 "\\([0-9]*\\)" ":" ".*\n"))
dfb7d195 257
768c29d0
JB
258;; There's no guarantee that Emacs will hand the filter the entire
259;; marker at once; it could be broken up across several strings. We
260;; might even receive a big chunk with several markers in it. If we
261;; receive a chunk of text which looks like it might contain the
262;; beginning of a marker, we save it here between calls to the
263;; filter.
f266485d 264(defvar gud-marker-acc "")
0d29cfe9 265(make-variable-buffer-local 'gud-marker-acc)
768c29d0 266
ee0155df 267(defun gud-gdb-marker-filter (string)
bed6a98d
RS
268 (setq gud-marker-acc (concat gud-marker-acc string))
269 (let ((output ""))
270
271 ;; Process all the complete markers in this chunk.
dfb7d195 272 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
bed6a98d
RS
273 (setq
274
275 ;; Extract the frame position from the marker.
276 gud-last-frame
277 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
278 (string-to-int (substring gud-marker-acc
279 (match-beginning 2)
280 (match-end 2))))
281
282 ;; Append any text before the marker to the output we're going
283 ;; to return - we don't include the marker in this text.
284 output (concat output
285 (substring gud-marker-acc 0 (match-beginning 0)))
286
287 ;; Set the accumulator to the remaining text.
288 gud-marker-acc (substring gud-marker-acc (match-end 0))))
289
290 ;; Does the remaining text look like it might end with the
291 ;; beginning of another marker? If it does, then keep it in
292 ;; gud-marker-acc until we receive the rest of it. Since we
293 ;; know the full marker regexp above failed, it's pretty simple to
294 ;; test for marker starts.
295 (if (string-match "\032.*\\'" gud-marker-acc)
296 (progn
297 ;; Everything before the potential marker start can be output.
298 (setq output (concat output (substring gud-marker-acc
299 0 (match-beginning 0))))
300
301 ;; Everything after, we save, to combine with later input.
302 (setq gud-marker-acc
303 (substring gud-marker-acc (match-beginning 0))))
304
305 (setq output (concat output gud-marker-acc)
306 gud-marker-acc ""))
307
308 output))
13b80a60 309
ee0155df 310(defun gud-gdb-find-file (f)
9bb4544b
RS
311 (save-excursion
312 (let ((buf (find-file-noselect f)))
313 (set-buffer buf)
07fc7c8a 314 (gud-make-debug-menu)
9bb4544b 315 (local-set-key [menu-bar debug tbreak]
b3ede706
KH
316 '("Temporary Breakpoint" . gud-tbreak))
317 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
318 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
319 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
9bb4544b 320 buf)))
13b80a60 321
e886137a
KH
322(defvar gdb-minibuffer-local-map nil
323 "Keymap for minibuffer prompting of gdb startup command.")
324(if gdb-minibuffer-local-map
325 ()
326 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
327 (define-key
328 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
329
10a4c11f 330;;;###autoload
a223b10d 331(defun gdb (command-line)
13b80a60
ER
332 "Run gdb on program FILE in buffer *gud-FILE*.
333The directory containing FILE becomes the initial working directory
334and source-file directory for your debugger."
f6376199 335 (interactive
f5792262
RS
336 (list (read-from-minibuffer "Run gdb (like this): "
337 (if (consp gud-gdb-history)
338 (car gud-gdb-history)
339 "gdb ")
340 gdb-minibuffer-local-map nil
341 '(gud-gdb-history . 1))))
ee0155df 342
ee97eac3
RS
343 (gud-common-init command-line 'gud-gdb-massage-args
344 'gud-gdb-marker-filter 'gud-gdb-find-file)
5b08a462 345
dd8e46c7 346 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
2fb419e1 347 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
23a3aa0a 348 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
dd8e46c7
RS
349 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
350 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
351 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
352 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
353 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
ee0155df
ER
354 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
355 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
dd8e46c7 356 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
ee0155df 357
0e0de659 358 (local-set-key "\C-i" 'gud-gdb-complete-command)
b3ede706
KH
359 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
360 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
361 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
362 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
13b80a60 363 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
f54877b2 364 (setq paragraph-start comint-prompt-regexp)
13b80a60
ER
365 (run-hooks 'gdb-mode-hook)
366 )
367
0e0de659
RS
368;; One of the nice features of GDB is its impressive support for
369;; context-sensitive command completion. We preserve that feature
370;; in the GUD buffer by using a GDB command designed just for Emacs.
371
372;; The completion process filter indicates when it is finished.
373(defvar gud-gdb-complete-in-progress)
374
375;; Since output may arrive in fragments we accumulate partials strings here.
376(defvar gud-gdb-complete-string)
377
378;; We need to know how much of the completion to chop off.
379(defvar gud-gdb-complete-break)
380
381;; The completion list is constructed by the process filter.
382(defvar gud-gdb-complete-list)
383
30df2a53
RS
384(defvar gud-comint-buffer nil)
385
0e0de659
RS
386(defun gud-gdb-complete-command ()
387 "Perform completion on the GDB command preceding point.
388This is implemented using the GDB `complete' command which isn't
389available with older versions of GDB."
390 (interactive)
391 (let* ((end (point))
392 (command (save-excursion
393 (beginning-of-line)
394 (and (looking-at comint-prompt-regexp)
395 (goto-char (match-end 0)))
396 (buffer-substring (point) end)))
397 command-word)
398 ;; Find the word break. This match will always succeed.
399 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
400 (setq gud-gdb-complete-break (match-beginning 2)
401 command-word (substring command gud-gdb-complete-break))
ee97eac3
RS
402 ;; Temporarily install our filter function.
403 (let ((gud-marker-filter 'gud-gdb-complete-filter))
404 ;; Issue the command to GDB.
405 (gud-basic-call (concat "complete " command))
406 (setq gud-gdb-complete-in-progress t
407 gud-gdb-complete-string nil
408 gud-gdb-complete-list nil)
409 ;; Slurp the output.
410 (while gud-gdb-complete-in-progress
411 (accept-process-output (get-buffer-process gud-comint-buffer))))
0e0de659
RS
412 ;; Protect against old versions of GDB.
413 (and gud-gdb-complete-list
414 (string-match "^Undefined command: \"complete\""
415 (car gud-gdb-complete-list))
416 (error "This version of GDB doesn't support the `complete' command."))
417 ;; Sort the list like readline.
418 (setq gud-gdb-complete-list
419 (sort gud-gdb-complete-list (function string-lessp)))
420 ;; Remove duplicates.
421 (let ((first gud-gdb-complete-list)
422 (second (cdr gud-gdb-complete-list)))
423 (while second
424 (if (string-equal (car first) (car second))
425 (setcdr first (setq second (cdr second)))
426 (setq first second
427 second (cdr second)))))
fd63b4f4
KH
428 ;; Add a trailing single quote if there is a unique completion
429 ;; and it contains an odd number of unquoted single quotes.
430 (and (= (length gud-gdb-complete-list) 1)
431 (let ((str (car gud-gdb-complete-list))
432 (pos 0)
433 (count 0))
434 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
435 (setq count (1+ count)
436 pos (match-end 0)))
437 (and (= (mod count 2) 1)
438 (setq gud-gdb-complete-list (list (concat str "'"))))))
0e0de659
RS
439 ;; Let comint handle the rest.
440 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
441
442;; The completion process filter is installed temporarily to slurp the
443;; output of GDB up to the next prompt and build the completion list.
444(defun gud-gdb-complete-filter (string)
445 (setq string (concat gud-gdb-complete-string string))
446 (while (string-match "\n" string)
447 (setq gud-gdb-complete-list
448 (cons (substring string gud-gdb-complete-break (match-beginning 0))
449 gud-gdb-complete-list))
450 (setq string (substring string (match-end 0))))
e9a918f9
EL
451 (if (string-match comint-prompt-regexp string)
452 (progn
453 (setq gud-gdb-complete-in-progress nil)
454 string)
455 (progn
456 (setq gud-gdb-complete-string string)
457 "")))
458
459;; gdb speedbar functions
460
461(defun gud-gdb-goto-stackframe (text token indent)
462 "Goto the stackframe described by TEXT, TOKEN, and INDENT."
463 (speedbar-with-attached-buffer
464 (gud-basic-call (concat "frame " (nth 1 token)))
465 (sit-for 1)))
466
467(defvar gud-gdb-fetched-stack-frame nil
468 "Stack frames we are fetching from GDB.")
469
470(defvar gud-gdb-fetched-stack-frame-list nil
471 "List of stack frames we are fetching from GDB.")
472
473;(defun gud-gdb-get-scope-data (text token indent)
474; ;; checkdoc-params: (indent)
475; "Fetch data associated with a stack frame, and expand/contract it.
476;Data to do this is retrieved from TEXT and TOKEN."
477; (let ((args nil) (scope nil))
478; (gud-gdb-run-command-fetch-lines "info args")
479;
480; (gud-gdb-run-command-fetch-lines "info local")
481;
482; ))
483
484(defun gud-gdb-get-stackframe (buffer)
485 "Extract the current stack frame out of the GUD GDB BUFFER."
486 (let ((newlst nil)
487 (gud-gdb-fetched-stack-frame-list nil))
488 (gud-gdb-run-command-fetch-lines "backtrace" buffer)
489 (if (string-match "No stack" (car gud-gdb-fetched-stack-frame-list))
490 ;; Go into some other mode???
491 nil
492 (while gud-gdb-fetched-stack-frame-list
493 (let ((e (car gud-gdb-fetched-stack-frame-list))
494 (name nil) (num nil))
495 (if (not (or
496 (string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([0-9a-zA-Z_]+\\) (" e)
497 (string-match "^#\\([0-9]+\\) +\\([0-9a-zA-Z_]+\\) (" e)))
498 (if (not (string-match
499 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e))
500 nil
501 (setcar newlst
502 (list (nth 0 (car newlst))
503 (nth 1 (car newlst))
504 (match-string 1 e)
505 (match-string 2 e))))
506 (setq num (match-string 1 e)
507 name (match-string 2 e))
508 (setq newlst
509 (cons
510 (if (string-match
511 "at \\([-0-9a-zA-Z_.]+\\):\\([0-9]+\\)$" e)
512 (list name num (match-string 1 e)
513 (match-string 2 e))
514 (list name num))
515 newlst))))
516 (setq gud-gdb-fetched-stack-frame-list
517 (cdr gud-gdb-fetched-stack-frame-list)))
518 (nreverse newlst))))
519
520;(defun gud-gdb-selected-frame-info (buffer)
521; "Learn GDB information for the currently selected stack frame in BUFFER."
522; )
523
524(defun gud-gdb-run-command-fetch-lines (command buffer)
525 "Run COMMAND, and return when `gud-gdb-fetched-stack-frame-list' is full.
526BUFFER is the GUD buffer in which to run the command."
527 (save-excursion
528 (set-buffer buffer)
529 (if (save-excursion
530 (goto-char (point-max))
531 (beginning-of-line)
532 (not (looking-at comint-prompt-regexp)))
533 nil
534 ;; Much of this copied from GDB complete, but I'm grabbing the stack
535 ;; frame instead.
536 (let ((gud-marker-filter 'gud-gdb-speedbar-stack-filter))
537 ;; Issue the command to GDB.
538 (gud-basic-call command)
539 (setq gud-gdb-complete-in-progress t ;; use this flag for our purposes.
540 gud-gdb-complete-string nil
541 gud-gdb-complete-list nil)
542 ;; Slurp the output.
543 (while gud-gdb-complete-in-progress
544 (accept-process-output (get-buffer-process gud-comint-buffer)))
545 (setq gud-gdb-fetched-stack-frame nil
546 gud-gdb-fetched-stack-frame-list
547 (nreverse gud-gdb-fetched-stack-frame-list))))))
548
549(defun gud-gdb-speedbar-stack-filter (string)
550 ;; checkdoc-params: (string)
551 "Filter used to read in the current GDB stack."
552 (setq string (concat gud-gdb-fetched-stack-frame string))
553 (while (string-match "\n" string)
554 (setq gud-gdb-fetched-stack-frame-list
555 (cons (substring string 0 (match-beginning 0))
556 gud-gdb-fetched-stack-frame-list))
557 (setq string (substring string (match-end 0))))
0e0de659
RS
558 (if (string-match comint-prompt-regexp string)
559 (progn
560 (setq gud-gdb-complete-in-progress nil)
561 string)
562 (progn
563 (setq gud-gdb-complete-string string)
564 "")))
565
575661b1 566\f
13b80a60
ER
567;; ======================================================================
568;; sdb functions
569
f6376199
JB
570;;; History of argument lists passed to sdb.
571(defvar gud-sdb-history nil)
572
ee0155df
ER
573(defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
574 "If nil, we're on a System V Release 4 and don't need the tags hack.")
575
576(defvar gud-sdb-lastfile nil)
577
dfbd82a6 578(defun gud-sdb-massage-args (file args) args)
13b80a60 579
ee0155df 580(defun gud-sdb-marker-filter (string)
7f629252
RS
581 (setq gud-marker-acc
582 (if gud-marker-acc (concat gud-marker-acc string) string))
583 (let (start)
584 ;; Process all complete markers in this chunk
585 (while
586 (cond
587 ;; System V Release 3.2 uses this format
bcdef904 588 ((string-match "\\(^\\|\n\\)\\*?\\(0x\\w* in \\)?\\([^:\n]*\\):\\([0-9]*\\):.*\n"
7f629252
RS
589 gud-marker-acc start)
590 (setq gud-last-frame
591 (cons
bcdef904 592 (substring gud-marker-acc (match-beginning 3) (match-end 3))
7f629252 593 (string-to-int
bcdef904 594 (substring gud-marker-acc (match-beginning 4) (match-end 4))))))
7f629252
RS
595 ;; System V Release 4.0 quite often clumps two lines together
596 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
597 gud-marker-acc start)
598 (setq gud-sdb-lastfile
599 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
600 (setq gud-last-frame
601 (cons
602 gud-sdb-lastfile
603 (string-to-int
604 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
605 ;; System V Release 4.0
606 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
607 gud-marker-acc start)
608 (setq gud-sdb-lastfile
609 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
610 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
611 gud-marker-acc start))
612 (setq gud-last-frame
613 (cons
614 gud-sdb-lastfile
615 (string-to-int
616 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
617 (t
618 (setq gud-sdb-lastfile nil)))
619 (setq start (match-end 0)))
620
621 ;; Search for the last incomplete line in this chunk
622 (while (string-match "\n" gud-marker-acc start)
623 (setq start (match-end 0)))
624
625 ;; If we have an incomplete line, store it in gud-marker-acc.
bcdef904 626 (setq gud-marker-acc (substring gud-marker-acc (or start 0))))
13b80a60
ER
627 string)
628
ee0155df 629(defun gud-sdb-find-file (f)
9bb4544b
RS
630 (save-excursion
631 (let ((buf (if gud-sdb-needs-tags
632 (find-tag-noselect f)
633 (find-file-noselect f))))
634 (set-buffer buf)
07fc7c8a 635 (gud-make-debug-menu)
b3ede706 636 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
9bb4544b 637 buf)))
13b80a60 638
10a4c11f 639;;;###autoload
a223b10d 640(defun sdb (command-line)
13b80a60
ER
641 "Run sdb on program FILE in buffer *gud-FILE*.
642The directory containing FILE becomes the initial working directory
643and source-file directory for your debugger."
f6376199 644 (interactive
a223b10d 645 (list (read-from-minibuffer "Run sdb (like this): "
f6376199
JB
646 (if (consp gud-sdb-history)
647 (car gud-sdb-history)
a223b10d 648 "sdb ")
f6376199
JB
649 nil nil
650 '(gud-sdb-history . 1))))
ee0155df 651 (if (and gud-sdb-needs-tags
e290aebb
RS
652 (not (and (boundp 'tags-file-name)
653 (stringp tags-file-name)
654 (file-exists-p tags-file-name))))
13b80a60 655 (error "The sdb support requires a valid tags table to work."))
13b80a60 656
ee97eac3
RS
657 (gud-common-init command-line 'gud-sdb-massage-args
658 'gud-sdb-marker-filter 'gud-sdb-find-file)
5b08a462 659
dd8e46c7
RS
660 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
661 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
662 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
663 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
664 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
665 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
666 (gud-def gud-cont "c" "\C-r" "Continue with display.")
667 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
13b80a60 668
6bde8427 669 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
f54877b2 670 (setq paragraph-start comint-prompt-regexp)
2291bfaa 671 (local-set-key [menu-bar debug tbreak]
b3ede706 672 '("Temporary Breakpoint" . gud-tbreak))
13b80a60
ER
673 (run-hooks 'sdb-mode-hook)
674 )
575661b1 675\f
13b80a60
ER
676;; ======================================================================
677;; dbx functions
678
f6376199
JB
679;;; History of argument lists passed to dbx.
680(defvar gud-dbx-history nil)
681
69c1dd37 682(defcustom gud-dbx-directories nil
3b3703fa
RS
683 "*A list of directories that dbx should search for source code.
684If nil, only source files in the program directory
685will be known to dbx.
686
687The file names should be absolute, or relative to the directory
69c1dd37
RS
688containing the executable being debugged."
689 :type '(choice (const :tag "Current Directory" nil)
690 (repeat :value ("")
691 directory))
692 :group 'gud)
3b3703fa
RS
693
694(defun gud-dbx-massage-args (file args)
695 (nconc (let ((directories gud-dbx-directories)
696 (result nil))
697 (while directories
698 (setq result (cons (car directories) (cons "-I" result)))
699 (setq directories (cdr directories)))
700 (nreverse result))
701 args))
702
703(defun gud-dbx-file-name (f)
704 "Transform a relative file name to an absolute file name, for dbx."
705 (let ((result nil))
706 (if (file-exists-p f)
707 (setq result (expand-file-name f))
708 (let ((directories gud-dbx-directories))
709 (while directories
710 (let ((path (concat (car directories) "/" f)))
711 (if (file-exists-p path)
712 (setq result (expand-file-name path)
713 directories nil)))
714 (setq directories (cdr directories)))))
715 result))
13b80a60 716
ee0155df 717(defun gud-dbx-marker-filter (string)
7f629252
RS
718 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
719
720 (let (start)
721 ;; Process all complete markers in this chunk.
722 (while (or (string-match
723 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
724 gud-marker-acc start)
725 (string-match
726 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
727 gud-marker-acc start))
13b80a60
ER
728 (setq gud-last-frame
729 (cons
7f629252 730 (substring gud-marker-acc (match-beginning 2) (match-end 2))
13b80a60 731 (string-to-int
7f629252
RS
732 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
733 start (match-end 0)))
734
735 ;; Search for the last incomplete line in this chunk
736 (while (string-match "\n" gud-marker-acc start)
737 (setq start (match-end 0)))
738
739 ;; If the incomplete line APPEARS to begin with another marker, keep it
740 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
741 ;; unnecessary concat during the next call.
742 (setq gud-marker-acc
743 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
744 (substring gud-marker-acc (match-beginning 0))
745 nil)))
13b80a60
ER
746 string)
747
f266485d
RS
748;; Functions for Mips-style dbx. Given the option `-emacs', documented in
749;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
750(defvar gud-mips-p
751 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
752 ;; We haven't tested gud on this system:
753 (string-match "^mips-[^-]*-riscos" system-configuration)
754 ;; It's documented on OSF/1.3
9f24ea14 755 (string-match "^mips-[^-]*-osf1" system-configuration)
7c82656b 756 (string-match "^alpha[^-]*-[^-]*-osf" system-configuration))
f266485d 757 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
13eaa026
RS
758
759(defun gud-mipsdbx-massage-args (file args)
dfbd82a6 760 (cons "-emacs" args))
13eaa026 761
f266485d
RS
762;; This is just like the gdb one except for the regexps since we need to cope
763;; with an optional breakpoint number in [] before the ^Z^Z
13eaa026 764(defun gud-mipsdbx-marker-filter (string)
bed6a98d
RS
765 (setq gud-marker-acc (concat gud-marker-acc string))
766 (let ((output ""))
767
768 ;; Process all the complete markers in this chunk.
769 (while (string-match
770 ;; This is like th gdb marker but with an optional
771 ;; leading break point number like `[1] '
772 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
773 gud-marker-acc)
774 (setq
775
776 ;; Extract the frame position from the marker.
777 gud-last-frame
778 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
779 (string-to-int (substring gud-marker-acc
780 (match-beginning 2)
781 (match-end 2))))
782
783 ;; Append any text before the marker to the output we're going
784 ;; to return - we don't include the marker in this text.
785 output (concat output
786 (substring gud-marker-acc 0 (match-beginning 0)))
787
788 ;; Set the accumulator to the remaining text.
789 gud-marker-acc (substring gud-marker-acc (match-end 0))))
790
791 ;; Does the remaining text look like it might end with the
792 ;; beginning of another marker? If it does, then keep it in
793 ;; gud-marker-acc until we receive the rest of it. Since we
794 ;; know the full marker regexp above failed, it's pretty simple to
795 ;; test for marker starts.
796 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
797 (progn
798 ;; Everything before the potential marker start can be output.
799 (setq output (concat output (substring gud-marker-acc
800 0 (match-beginning 0))))
801
802 ;; Everything after, we save, to combine with later input.
803 (setq gud-marker-acc
804 (substring gud-marker-acc (match-beginning 0))))
805
806 (setq output (concat output gud-marker-acc)
807 gud-marker-acc ""))
808
809 output))
13eaa026 810
f266485d
RS
811;; The dbx in IRIX is a pain. It doesn't print the file name when
812;; stopping at a breakpoint (but you do get it from the `up' and
813;; `down' commands...). The only way to extract the information seems
814;; to be with a `file' command, although the current line number is
815;; available in $curline. Thus we have to look for output which
816;; appears to indicate a breakpoint. Then we prod the dbx sub-process
817;; to output the information we want with a combination of the
818;; `printf' and `file' commands as a pseudo marker which we can
819;; recognise next time through the marker-filter. This would be like
820;; the gdb marker but you can't get the file name without a newline...
821;; Note that gud-remove won't work since Irix dbx expects a breakpoint
822;; number rather than a line number etc. Maybe this could be made to
823;; work by listing all the breakpoints and picking the one(s) with the
824;; correct line number, but life's too short.
825;; d.love@dl.ac.uk (Dave Love) can be blamed for this
826
ac8da950
KH
827(defvar gud-irix-p
828 (and (string-match "^mips-[^-]*-irix" system-configuration)
829 (not (string-match "irix[6-9]\\.[1-9]" system-configuration)))
f266485d 830 "Non-nil to assume the interface appropriate for IRIX dbx.
ac8da950
KH
831This works in IRIX 4, 5 and 6, but `gud-dbx-use-stopformat-p' provides
832a better solution in 6.1 upwards.")
833(defvar gud-dbx-use-stopformat-p
834 (string-match "irix[6-9]\\.[1-9]" system-configuration)
835 "Non-nil to use the dbx feature present at least from Irix 6.1
836 whereby $stopformat=1 produces an output format compatiable with
837 `gud-dbx-marker-filter'.")
2fb419e1
RS
838;; [Irix dbx seems to be a moving target. The dbx output changed
839;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
840;; the output from `up' is no longer spotted by gud (and it's probably
841;; not distinctive enough to try to match it -- use C-<, C->
842;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
843;; `long long'(why?!), so the printf stuff needed changing. The line
ac8da950
KH
844;; number was cast to `long' as a compromise between the new `long
845;; long' and the original `int'. This is reported not to work in 6.2,
846;; so it's changed back to int -- don't make your sources too long.
847;; From Irix6.1 (but not 6.0?) dbx supports an undocumented feature
848;; whereby `set $stopformat=1' reportedly produces output compatible
849;; with `gud-dbx-marker-filter', which we prefer.
850
851;; The process filter is also somewhat
2fb419e1
RS
852;; unreliable, sometimes not spotting the markers; I don't know
853;; whether there's anything that can be done about that. It would be
854;; much better if SGI could be persuaded to (re?)instate the MIPS
855;; -emacs flag for gdb-like output (which ought to be possible as most
856;; of the communication I've had over it has been from sgi.com).]
f266485d
RS
857
858;; this filter is influenced by the xdb one rather than the gdb one
859(defun gud-irixdbx-marker-filter (string)
bed6a98d
RS
860 (let (result (case-fold-search nil))
861 (if (or (string-match comint-prompt-regexp string)
862 (string-match ".*\012" string))
863 (setq result (concat gud-marker-acc string)
864 gud-marker-acc "")
865 (setq gud-marker-acc (concat gud-marker-acc string)))
866 (if result
867 (cond
868 ;; look for breakpoint or signal indication e.g.:
869 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
870 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
871 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
872 ((string-match
873 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
874 result)
875 ;; prod dbx into printing out the line number and file
876 ;; name in a form we can grok as below
877 (process-send-string (get-buffer-process gud-comint-buffer)
ac8da950 878 "printf \"\032\032%1d:\",(int)$curline;file\n"))
bed6a98d
RS
879 ;; look for result of, say, "up" e.g.:
880 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
881 ;; (this will also catch one of the lines printed by "where")
882 ((string-match
883 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
884 result)
885 (let ((file (substring result (match-beginning 1)
886 (match-end 1))))
887 (if (file-exists-p file)
888 (setq gud-last-frame
889 (cons
890 (substring
891 result (match-beginning 1) (match-end 1))
892 (string-to-int
893 (substring
894 result (match-beginning 2) (match-end 2)))))))
895 result)
896 ((string-match ; kluged-up marker as above
897 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
3b3703fa
RS
898 (let ((file (gud-dbx-file-name
899 (substring result (match-beginning 2) (match-end 2)))))
900 (if (and file (file-exists-p file))
bed6a98d
RS
901 (setq gud-last-frame
902 (cons
903 file
904 (string-to-int
905 (substring
906 result (match-beginning 1) (match-end 1)))))))
907 (setq result (substring result 0 (match-beginning 0))))))
908 (or result "")))
f266485d 909
34c8b673
RS
910(defvar gud-dgux-p (string-match "-dgux" system-configuration)
911 "Non-nil means to assume the interface approriate for DG/UX dbx.
912This was tested using R4.11.")
913
914;; There are a couple of differences between DG's dbx output and normal
915;; dbx output which make it nontrivial to integrate this into the
916;; standard dbx-marker-filter (mainly, there are a different number of
917;; backreferences). The markers look like:
918;;
919;; (0) Stopped at line 10, routine main(argc=1, argv=0xeffff0e0), file t.c
920;;
921;; from breakpoints (the `(0)' there isn't constant, it's the breakpoint
922;; number), and
923;;
924;; Stopped at line 13, routine main(argc=1, argv=0xeffff0e0), file t.c
925;;
926;; from signals and
927;;
928;; Frame 21, line 974, routine command_loop(), file keyboard.c
929;;
930;; from up/down/where.
931
932(defun gud-dguxdbx-marker-filter (string)
933 (setq gud-marker-acc (if gud-marker-acc
934 (concat gud-marker-acc string)
935 string))
936 (let ((re (concat "^\\(\\(([0-9]+) \\)?Stopped at\\|Frame [0-9]+,\\)"
937 " line \\([0-9]+\\), routine .*, file \\([^ \t\n]+\\)"))
938 start)
939 ;; Process all complete markers in this chunk.
940 (while (string-match re gud-marker-acc start)
941 (setq gud-last-frame
942 (cons
943 (substring gud-marker-acc (match-beginning 4) (match-end 4))
944 (string-to-int (substring gud-marker-acc
945 (match-beginning 3) (match-end 3))))
946 start (match-end 0)))
947
948 ;; Search for the last incomplete line in this chunk
949 (while (string-match "\n" gud-marker-acc start)
950 (setq start (match-end 0)))
951
952 ;; If the incomplete line APPEARS to begin with another marker, keep it
953 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
954 ;; unnecessary concat during the next call.
955 (setq gud-marker-acc
956 (if (string-match "Stopped\\|Frame" gud-marker-acc start)
957 (substring gud-marker-acc (match-beginning 0))
958 nil)))
959 string)
960
ee0155df 961(defun gud-dbx-find-file (f)
9bb4544b 962 (save-excursion
3b3703fa
RS
963 (let ((realf (gud-dbx-file-name f)))
964 (if realf
8744b08b 965 (let ((buf (find-file-noselect realf)))
3b3703fa
RS
966 (set-buffer buf)
967 (gud-make-debug-menu)
968 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
969 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
970 buf)
971 nil))))
13b80a60 972
10a4c11f 973;;;###autoload
a223b10d 974(defun dbx (command-line)
13b80a60
ER
975 "Run dbx on program FILE in buffer *gud-FILE*.
976The directory containing FILE becomes the initial working directory
977and source-file directory for your debugger."
f6376199 978 (interactive
a223b10d 979 (list (read-from-minibuffer "Run dbx (like this): "
f6376199
JB
980 (if (consp gud-dbx-history)
981 (car gud-dbx-history)
a223b10d 982 "dbx ")
f6376199
JB
983 nil nil
984 '(gud-dbx-history . 1))))
13eaa026 985
ee97eac3
RS
986 (cond
987 (gud-mips-p
988 (gud-common-init command-line 'gud-mipsdbx-massage-args
989 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
990 (gud-irix-p
991 (gud-common-init command-line 'gud-dbx-massage-args
992 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
34c8b673
RS
993 (gud-dgux-p
994 (gud-common-init command-line 'gud-dbx-massage-args
995 'gud-dguxdbx-marker-filter 'gud-dbx-find-file))
ee97eac3
RS
996 (t
997 (gud-common-init command-line 'gud-dbx-massage-args
998 'gud-dbx-marker-filter 'gud-dbx-find-file)))
5b08a462 999
13eaa026 1000 (cond
f266485d 1001 (gud-mips-p
2fb419e1
RS
1002 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1003 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
13eaa026
RS
1004 (gud-def gud-break "stop at \"%f\":%l"
1005 "\C-b" "Set breakpoint at current line.")
1006 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
f266485d
RS
1007 (gud-irix-p
1008 (gud-def gud-break "stop at \"%d%f\":%l"
1009 "\C-b" "Set breakpoint at current line.")
1010 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
ac8da950 1011 (gud-def gud-up "up %p; printf \"\032\032%1d:\",(int)$curline;file\n"
2fb419e1 1012 "<" "Up (numeric arg) stack frames.")
ac8da950 1013 (gud-def gud-down "down %p; printf \"\032\032%1d:\",(int)$curline;file\n"
2fb419e1 1014 ">" "Down (numeric arg) stack frames.")
f266485d
RS
1015 ;; Make dbx give out the source location info that we need.
1016 (process-send-string (get-buffer-process gud-comint-buffer)
ac8da950
KH
1017 "printf \"\032\032%1d:\",(int)$curline;file\n"))
1018 (gud-dbx-use-stopformat-p
1019 (process-send-string (get-buffer-process gud-comint-buffer)
1020 "set $stopformat=1\n"))
13eaa026 1021 (t
2fb419e1
RS
1022 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1023 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
13eaa026
RS
1024 (gud-def gud-break "file \"%d%f\"\nstop at %l"
1025 "\C-b" "Set breakpoint at current line.")))
1026
dd8e46c7
RS
1027 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1028 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
1029 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
1030 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
1031 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
dd8e46c7 1032 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
ee0155df 1033
13eaa026 1034 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
f54877b2 1035 (setq paragraph-start comint-prompt-regexp)
b3ede706
KH
1036 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1037 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
13b80a60
ER
1038 (run-hooks 'dbx-mode-hook)
1039 )
575661b1 1040\f
6496f3d2
RS
1041;; ======================================================================
1042;; xdb (HP PARISC debugger) functions
1043
f6376199
JB
1044;;; History of argument lists passed to xdb.
1045(defvar gud-xdb-history nil)
1046
69c1dd37 1047(defcustom gud-xdb-directories nil
fce222c7
RS
1048 "*A list of directories that xdb should search for source code.
1049If nil, only source files in the program directory
1050will be known to xdb.
1051
1052The file names should be absolute, or relative to the directory
69c1dd37
RS
1053containing the executable being debugged."
1054 :type '(choice (const :tag "Current Directory" nil)
1055 (repeat :value ("")
1056 directory))
1057 :group 'gud)
fce222c7 1058
a223b10d
RM
1059(defun gud-xdb-massage-args (file args)
1060 (nconc (let ((directories gud-xdb-directories)
1061 (result nil))
1062 (while directories
1063 (setq result (cons (car directories) (cons "-d" result)))
1064 (setq directories (cdr directories)))
dfbd82a6 1065 (nreverse result))
a223b10d 1066 args))
6496f3d2
RS
1067
1068(defun gud-xdb-file-name (f)
1069 "Transform a relative pathname to a full pathname in xdb mode"
1070 (let ((result nil))
1071 (if (file-exists-p f)
1072 (setq result (expand-file-name f))
fce222c7
RS
1073 (let ((directories gud-xdb-directories))
1074 (while directories
1075 (let ((path (concat (car directories) "/" f)))
6496f3d2
RS
1076 (if (file-exists-p path)
1077 (setq result (expand-file-name path)
fce222c7
RS
1078 directories nil)))
1079 (setq directories (cdr directories)))))
6496f3d2
RS
1080 result))
1081
1082;; xdb does not print the lines all at once, so we have to accumulate them
6496f3d2
RS
1083(defun gud-xdb-marker-filter (string)
1084 (let (result)
1085 (if (or (string-match comint-prompt-regexp string)
1086 (string-match ".*\012" string))
f266485d
RS
1087 (setq result (concat gud-marker-acc string)
1088 gud-marker-acc "")
1089 (setq gud-marker-acc (concat gud-marker-acc string)))
6496f3d2 1090 (if result
1dc9668c
RS
1091 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
1092 result)
6496f3d2
RS
1093 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
1094 result))
1dc9668c 1095 (let ((line (string-to-int
6496f3d2
RS
1096 (substring result (match-beginning 2) (match-end 2))))
1097 (file (gud-xdb-file-name
1098 (substring result (match-beginning 1) (match-end 1)))))
1099 (if file
1100 (setq gud-last-frame (cons file line))))))
1101 (or result "")))
1102
6496f3d2 1103(defun gud-xdb-find-file (f)
9bb4544b
RS
1104 (save-excursion
1105 (let ((realf (gud-xdb-file-name f)))
1106 (if realf
1107 (let ((buf (find-file-noselect realf)))
1108 (set-buffer buf)
07fc7c8a 1109 (gud-make-debug-menu)
9bb4544b 1110 (local-set-key [menu-bar debug tbreak]
b3ede706 1111 '("Temporary Breakpoint" . gud-tbreak))
9bb4544b 1112 (local-set-key [menu-bar debug finish]
b3ede706
KH
1113 '("Finish Function" . gud-finish))
1114 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1115 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
9bb4544b
RS
1116 buf)
1117 nil))))
6496f3d2
RS
1118
1119;;;###autoload
a223b10d 1120(defun xdb (command-line)
6496f3d2
RS
1121 "Run xdb on program FILE in buffer *gud-FILE*.
1122The directory containing FILE becomes the initial working directory
1123and source-file directory for your debugger.
1124
fce222c7 1125You can set the variable 'gud-xdb-directories' to a list of program source
6496f3d2 1126directories if your program contains sources from more than one directory."
f6376199 1127 (interactive
a223b10d 1128 (list (read-from-minibuffer "Run xdb (like this): "
f6376199
JB
1129 (if (consp gud-xdb-history)
1130 (car gud-xdb-history)
a223b10d 1131 "xdb ")
f6376199
JB
1132 nil nil
1133 '(gud-xdb-history . 1))))
6496f3d2 1134
ee97eac3
RS
1135 (gud-common-init command-line 'gud-xdb-massage-args
1136 'gud-xdb-marker-filter 'gud-xdb-find-file)
6496f3d2
RS
1137
1138 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
1139 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
1140 "Set temporary breakpoint at current line.")
1141 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
1142 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
1143 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
1144 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1145 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
1146 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
1147 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
1148 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
1149
1150 (setq comint-prompt-regexp "^>")
f54877b2 1151 (setq paragraph-start comint-prompt-regexp)
b3ede706
KH
1152 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
1153 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
1154 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
1155 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
6496f3d2 1156 (run-hooks 'xdb-mode-hook))
575661b1
RS
1157\f
1158;; ======================================================================
1159;; perldb functions
1160
1161;;; History of argument lists passed to perldb.
1162(defvar gud-perldb-history nil)
1163
1164(defun gud-perldb-massage-args (file args)
aa9063cb
RS
1165 (cond ((equal (car args) "-e")
1166 (cons "-d"
1167 (cons (car args)
1168 (cons (nth 1 args)
1169 (cons "--" (cons "-emacs" (cdr (cdr args))))))))
1170 (t
1171 (cons "-d" (cons (car args) (cons "-emacs" (cdr args)))))))
575661b1
RS
1172
1173;; There's no guarantee that Emacs will hand the filter the entire
1174;; marker at once; it could be broken up across several strings. We
1175;; might even receive a big chunk with several markers in it. If we
1176;; receive a chunk of text which looks like it might contain the
1177;; beginning of a marker, we save it here between calls to the
1178;; filter.
1179(defvar gud-perldb-marker-acc "")
1180
1181(defun gud-perldb-marker-filter (string)
bed6a98d
RS
1182 (setq gud-marker-acc (concat gud-marker-acc string))
1183 (let ((output ""))
1184
1185 ;; Process all the complete markers in this chunk.
aa9063cb 1186 (while (string-match "\032\032\\(\\([a-zA-Z]:\\)?[^:\n]*\\):\\([0-9]*\\):.*\n"
bed6a98d
RS
1187 gud-marker-acc)
1188 (setq
1189
1190 ;; Extract the frame position from the marker.
1191 gud-last-frame
1192 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
1193 (string-to-int (substring gud-marker-acc
aa9063cb
RS
1194 (match-beginning 3)
1195 (match-end 3))))
bed6a98d
RS
1196
1197 ;; Append any text before the marker to the output we're going
1198 ;; to return - we don't include the marker in this text.
1199 output (concat output
1200 (substring gud-marker-acc 0 (match-beginning 0)))
1201
1202 ;; Set the accumulator to the remaining text.
1203 gud-marker-acc (substring gud-marker-acc (match-end 0))))
1204
1205 ;; Does the remaining text look like it might end with the
1206 ;; beginning of another marker? If it does, then keep it in
1207 ;; gud-marker-acc until we receive the rest of it. Since we
1208 ;; know the full marker regexp above failed, it's pretty simple to
1209 ;; test for marker starts.
1210 (if (string-match "\032.*\\'" gud-marker-acc)
1211 (progn
1212 ;; Everything before the potential marker start can be output.
1213 (setq output (concat output (substring gud-marker-acc
1214 0 (match-beginning 0))))
1215
1216 ;; Everything after, we save, to combine with later input.
1217 (setq gud-marker-acc
1218 (substring gud-marker-acc (match-beginning 0))))
1219
1220 (setq output (concat output gud-marker-acc)
1221 gud-marker-acc ""))
1222
1223 output))
575661b1
RS
1224
1225(defun gud-perldb-find-file (f)
9bb4544b
RS
1226 (save-excursion
1227 (let ((buf (find-file-noselect f)))
1228 (set-buffer buf)
39cf49be 1229 (gud-make-debug-menu)
9bb4544b 1230 buf)))
575661b1 1231
c3fd0eea 1232(defcustom gud-perldb-command-name "perl"
69c1dd37
RS
1233 "File name for executing Perl."
1234 :type 'string
1235 :group 'gud)
9ab5d005 1236
575661b1
RS
1237;;;###autoload
1238(defun perldb (command-line)
1239 "Run perldb on program FILE in buffer *gud-FILE*.
1240The directory containing FILE becomes the initial working directory
1241and source-file directory for your debugger."
1242 (interactive
1243 (list (read-from-minibuffer "Run perldb (like this): "
1244 (if (consp gud-perldb-history)
1245 (car gud-perldb-history)
c3fd0eea 1246 (concat gud-perldb-command-name
5dd9db3b
RS
1247 " "
1248 (or (buffer-file-name)
d6efd584
RS
1249 "-e 0")
1250 " "))
575661b1
RS
1251 nil nil
1252 '(gud-perldb-history . 1))))
575661b1 1253
ee97eac3
RS
1254 (gud-common-init command-line 'gud-perldb-massage-args
1255 'gud-perldb-marker-filter 'gud-perldb-find-file)
575661b1 1256
2c32e5c6
RS
1257 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
1258 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
1259 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
1260 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1261 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1262; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1263; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1264; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1265 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1266
aa9063cb 1267 (setq comint-prompt-regexp "^ DB<+[0-9]+>+ ")
f54877b2 1268 (setq paragraph-start comint-prompt-regexp)
2c32e5c6 1269 (run-hooks 'perldb-mode-hook)
575661b1 1270 )
c3fd0eea
RS
1271\f
1272;; ======================================================================
1273;;
1274;; JDB support.
1275;;
1276;; AUTHOR: Derek Davies <ddavies@world.std.com>
1277;;
1278;; CREATED: Sun Feb 22 10:46:38 1998 Derek Davies.
1279;;
1280;; INVOCATION NOTES:
1281;;
1282;; You invoke jdb-mode with:
1283;;
1284;; M-x jdb <enter>
1285;;
1286;; It responds with:
1287;;
1288;; Run jdb (like this): jdb
1289;;
1290;; type any jdb switches followed by the name of the class you'd like to debug.
1291;; Supply a fully qualfied classname (these do not have the ".class" extension)
1292;; for the name of the class to debug (e.g. "COM.the-kind.ddavies.CoolClass").
1293;; See the known problems section below for restrictions when specifying jdb
1294;; command line switches (search forward for '-classpath').
1295;;
1296;; You should see something like the following:
1297;;
1298;; Current directory is ~/src/java/hello/
1299;; Initializing jdb...
1300;; 0xed2f6628:class(hello)
1301;; >
1302;;
1303;; To set an initial breakpoint try:
1304;;
1305;; > stop in hello.main
1306;; Breakpoint set in hello.main
1307;; >
1308;;
1309;; To execute the program type:
1310;;
1311;; > run
1312;; run hello
1313;;
1314;; Breakpoint hit: running ...
1315;; hello.main (hello:12)
1316;;
1317;; Type M-n to step over the current line and M-s to step into it. That,
1318;; along with the JDB 'help' command should get you started. The 'quit'
35dd3c55
KH
1319;; JDB command will get out out of the debugger. There is some truly
1320;; pathetic JDB documentation available at:
1321;;
1322;; http://java.sun.com/products/jdk/1.1/debugging/
c3fd0eea
RS
1323;;
1324;; KNOWN PROBLEMS AND FIXME's:
1325;;
35dd3c55 1326;; Not sure what happens with inner classes ... haven't tried them.
c3fd0eea
RS
1327;;
1328;; Does not grok UNICODE id's. Only ASCII id's are supported.
1329;;
c3fd0eea
RS
1330;; You must not put whitespace between "-classpath" and the path to
1331;; search for java classes even though it is required when invoking jdb
1332;; from the command line. See gud-jdb-massage-args for details.
1333;;
35dd3c55
KH
1334;; If any of the source files in the directories listed in
1335;; gud-jdb-directories won't parse you'll have problems. Make sure
1336;; every file ending in ".java" in these directories parses without error.
1337;;
1338;; All the .java files in the directories in gud-jdb-directories are
1339;; syntactically analyzed each time gud jdb is invoked. It would be
1340;; nice to keep as much information as possible between runs. It would
1341;; be really nice to analyze the files only as neccessary (when the
1342;; source needs to be displayed.) I'm not sure to what extent the former
1343;; can be accomplished and I'm not sure the latter can be done at all
1344;; since I don't know of any general way to tell which .class files are
1345;; defined by which .java file without analyzing all the .java files.
1346;; If anyone knows why JavaSoft didn't put the source file names in
1347;; debuggable .class files please clue me in so I find something else
1348;; to be spiteful and bitter about.
1349;;
c3fd0eea
RS
1350;; ======================================================================
1351;; gud jdb variables and functions
1352
1353;; History of argument lists passed to jdb.
1354(defvar gud-jdb-history nil)
1355
1356;; List of Java source file directories.
1357(defvar gud-jdb-directories (list ".")
1358 "*A list of directories that gud jdb should search for source code.
1359The file names should be absolute, or relative to the current directory.")
1360
1361;; List of the java source files for this debugging session.
1362(defvar gud-jdb-source-files nil)
1363
1364;; Return a list of java source files. PATH gives the directories in
1365;; which to search for files with extension EXTN. Normally EXTN is
1366;; given as the regular expression "\\.java$" .
1367(defun gud-jdb-build-source-files-list (path extn)
1368 (apply 'nconc (mapcar (lambda (d) (directory-files d t extn nil)) path)))
1369
35dd3c55
KH
1370;; Move point past whitespace.
1371(defun gud-jdb-skip-whitespace ()
1372 (skip-chars-forward " \n\r\t\014"))
1373
1374;; Move point past a "// <eol>" type of comment.
1375(defun gud-jdb-skip-single-line-comment ()
1376 (end-of-line))
1377
1378;; Move point past a "/* */" or "/** */" type of comment.
1379(defun gud-jdb-skip-traditional-or-documentation-comment ()
1380 (forward-char 2)
1381 (catch 'break
1382 (while (not (eobp))
1383 (if (eq (following-char) ?*)
1384 (progn
1385 (forward-char)
1386 (if (not (eobp))
1387 (if (eq (following-char) ?/)
1388 (progn
1389 (forward-char)
1390 (throw 'break nil)))))
1391 (forward-char)))))
1392
1393;; Move point past any number of consecutive whitespace chars and/or comments.
1394(defun gud-jdb-skip-whitespace-and-comments ()
1395 (gud-jdb-skip-whitespace)
1396 (catch 'done
1397 (while t
1398 (cond
1399 ((looking-at "//")
1400 (gud-jdb-skip-single-line-comment)
1401 (gud-jdb-skip-whitespace))
1402 ((looking-at "/\\*")
1403 (gud-jdb-skip-traditional-or-documentation-comment)
1404 (gud-jdb-skip-whitespace))
1405 (t (throw 'done nil))))))
1406
1407;; Move point past things that are id-like. The intent is to skip regular
1408;; id's, such as class or interface names as well as package and interface
1409;; names.
1410(defun gud-jdb-skip-id-ish-thing ()
1411 (skip-chars-forward "^ /\n\r\t\014,;{"))
1412
1413;; Move point past a string literal.
1414(defun gud-jdb-skip-string-literal ()
1415 (forward-char)
1416 (while
1417 (progn
1418 (if (eq (following-char) ?\\)
1419 (forward-char 2))
1420 (not (eq (following-char) ?\042)))
1421 (forward-char))
1422 (forward-char))
1423
1424;; Move point past a character literal.
1425(defun gud-jdb-skip-character-literal ()
1426 (forward-char)
1427 (while
1428 (progn
1429 (if (eq (following-char) ?\\)
1430 (forward-char 2))
1431 (not (eq (following-char) ?\')))
1432 (forward-char))
1433 (forward-char))
1434
1435;; Move point past the following block. There may be (legal) cruft before
1436;; the block's opening brace. There must be a block or it's the end of life
1437;; in petticoat junction.
1438(defun gud-jdb-skip-block ()
1439
1440 ;; Find the begining of the block.
1441 (while
1442 (not (eq (following-char) ?{))
1443
1444 ;; Skip any constructs that can harbor literal block delimiter
1445 ;; characters and/or the delimiters for the constructs themselves.
1446 (cond
1447 ((looking-at "//")
1448 (gud-jdb-skip-single-line-comment))
1449 ((looking-at "/\\*")
1450 (gud-jdb-skip-traditional-or-documentation-comment))
1451 ((eq (following-char) ?\042)
1452 (gud-jdb-skip-string-literal))
1453 ((eq (following-char) ?\')
1454 (gud-jdb-skip-character-literal))
1455 (t (forward-char))))
1456
1457 ;; Now at the begining of the block.
1458 (forward-char)
1459
1460 ;; Skip over the body of the block as well as the final brace.
1461 (let ((open-level 1))
1462 (while (not (eq open-level 0))
1463 (cond
1464 ((looking-at "//")
1465 (gud-jdb-skip-single-line-comment))
1466 ((looking-at "/\\*")
1467 (gud-jdb-skip-traditional-or-documentation-comment))
1468 ((eq (following-char) ?\042)
1469 (gud-jdb-skip-string-literal))
1470 ((eq (following-char) ?\')
1471 (gud-jdb-skip-character-literal))
1472 ((eq (following-char) ?{)
1473 (setq open-level (+ open-level 1))
1474 (forward-char))
1475 ((eq (following-char) ?})
1476 (setq open-level (- open-level 1))
1477 (forward-char))
1478 (t (forward-char))))))
1479
1480;; Find the package and class definitions in Java source file FILE. Assumes
1481;; that FILE contains a legal Java program. BUF is a scratch buffer used
1482;; to hold the source during analysis.
1483(defun gud-jdb-analyze-source (buf file)
1484 (let ((l nil))
1485 (set-buffer buf)
1486 (insert-file-contents file nil nil nil t)
1487 (goto-char 0)
1488 (catch 'abort
1489 (let ((p ""))
1490 (while (progn
1491 (gud-jdb-skip-whitespace)
1492 (not (eobp)))
1493 (cond
1494
1495 ;; Any number of semi's following a block is legal. Move point
1496 ;; past them. Note that comments and whitespace may be
1497 ;; interspersed as well.
1498 ((eq (following-char) ?\073)
1499 (forward-char))
1500
1501 ;; Move point past a single line comment.
1502 ((looking-at "//")
1503 (gud-jdb-skip-single-line-comment))
1504
1505 ;; Move point past a traditional or documentation comment.
1506 ((looking-at "/\\*")
1507 (gud-jdb-skip-traditional-or-documentation-comment))
1508
1509 ;; Move point past a package statement, but save the PackageName.
1510 ((looking-at "package")
1511 (forward-char 7)
1512 (gud-jdb-skip-whitespace-and-comments)
1513 (let ((s (point)))
1514 (gud-jdb-skip-id-ish-thing)
1515 (setq p (concat (buffer-substring s (point)) "."))
1516 (gud-jdb-skip-whitespace-and-comments)
1517 (if (eq (following-char) ?\073)
1518 (forward-char))))
1519
1520 ;; Move point past an import statement.
1521 ((looking-at "import")
1522 (forward-char 6)
1523 (gud-jdb-skip-whitespace-and-comments)
1524 (gud-jdb-skip-id-ish-thing)
1525 (gud-jdb-skip-whitespace-and-comments)
1526 (if (eq (following-char) ?\073)
1527 (forward-char)))
1528
1529 ;; Move point past the various kinds of ClassModifiers.
1530 ((looking-at "public")
1531 (forward-char 6))
1532 ((looking-at "abstract")
1533 (forward-char 8))
1534 ((looking-at "final")
1535 (forward-char 5))
1536
1537 ;; Move point past a ClassDeclaraction, but save the class
1538 ;; Identifier.
1539 ((looking-at "class")
1540 (forward-char 5)
1541 (gud-jdb-skip-whitespace-and-comments)
1542 (let ((s (point)))
1543 (gud-jdb-skip-id-ish-thing)
1544 (setq
1545 l (nconc l (list (concat p (buffer-substring s (point)))))))
1546 (gud-jdb-skip-block))
1547
1548 ;; Move point past an interface statement.
1549 ((looking-at "interface")
1550 (forward-char 9)
1551 (gud-jdb-skip-block))
1552
1553 ;; Anything else means the input is invalid.
1554 (t
1555 (message (format "Error parsing file %s." file))
1556 (throw 'abort nil))))))
1557 l))
1558
1559(defun gud-jdb-build-class-source-alist-for-file (file)
1560 (mapcar
1561 (lambda (c)
1562 (cons c file))
1563 (gud-jdb-analyze-source gud-jdb-analysis-buffer file)))
c3fd0eea
RS
1564
1565;; Association list of fully qualified class names (package + class name) and
1566;; their source files.
1567(defvar gud-jdb-class-source-alist nil)
1568
35dd3c55
KH
1569;; This is used to hold a source file during analysis.
1570(defvar gud-jdb-analysis-buffer nil)
1571
c3fd0eea
RS
1572;; Return an alist of fully qualified classes and the source files
1573;; holding their definitions. SOURCES holds a list of all the source
1574;; files to examine.
1575(defun gud-jdb-build-class-source-alist (sources)
35dd3c55
KH
1576 (setq gud-jdb-analysis-buffer (get-buffer-create "*gud-jdb-scratch*"))
1577 (prog1
1578 (apply
1579 'nconc
1580 (mapcar
1581 'gud-jdb-build-class-source-alist-for-file
1582 sources))
1583 (kill-buffer gud-jdb-analysis-buffer)
1584 (setq gud-jdb-analysis-buffer nil)))
c3fd0eea
RS
1585
1586;; Change what was given in the minibuffer to something that can be used to
1587;; invoke the debugger.
1588(defun gud-jdb-massage-args (file args)
1589 ;; The jdb executable must have whitespace between "-classpath" and
1590 ;; it's value while gud-common-init expects all switch values to
1591 ;; follow the switch keyword without intervening whitespace. We
1592 ;; require that when the user enters the "-classpath" switch in the
1593 ;; EMACS minibuffer that they do so without the intervening
1594 ;; whitespace. This function adds it back (it's called after
1595 ;; gud-common-init). There are more switches like this (for
1596 ;; instance "-host" and "-password") but I don't care about them
1597 ;; yet.
1598 (if args
1599 (let (massaged-args user-error)
1600
1601 (while
1602 (and args
1603 (not (string-match "-classpath\\(.+\\)" (car args)))
1604 (not (setq user-error
1605 (string-match "-classpath$" (car args)))))
1606 (setq massaged-args (append massaged-args (list (car args))))
1607 (setq args (cdr args)))
1608
1609 ;; By this point the current directory is all screwed up. Maybe we
1610 ;; could fix things and re-invoke gud-common-init, but for now I think
1611 ;; issueing the error is good enough.
1612 (if user-error
1613 (progn
1614 (kill-buffer (current-buffer))
1615 (error "Error: Omit whitespace between '-classpath' and it's value")))
1616
1617 (if args
1618 (setq massaged-args
1619 (append
1620 massaged-args
1621 (list "-classpath")
1622 (list
1623 (substring
1624 (car args)
1625 (match-beginning 1) (match-end 1)))
1626 (cdr args)))
1627 massaged-args))))
1628
1629;; Search for an association with P, a fully qualified class name, in
1630;; gud-jdb-class-source-alist. The asssociation gives the fully
1631;; qualified file name of the source file which produced the class.
1632(defun gud-jdb-find-source-file (p)
1633 (cdr (assoc p gud-jdb-class-source-alist)))
1634
1635;; See comentary for other debugger's marker filters - there you will find
1636;; important notes about STRING.
1637(defun gud-jdb-marker-filter (string)
1638
1639 ;; Build up the accumulator.
1640 (setq gud-marker-acc
1641 (if gud-marker-acc
1642 (concat gud-marker-acc string)
1643 string))
1644
1645 ;; We process STRING from left to right. Each time through the following
1646 ;; loop we process at most one marker. The start variable keeps track of
1647 ;; where we are in the input string through the iterations of this loop.
1648 (let (start file-found)
1649
1650 ;; Process each complete marker in the input. There may be an incomplete
1651 ;; marker at the end of the input string. Incomplete markers are left
1652 ;; in the accumulator for processing the next time the function is called.
1653 (while
1654
1655 ;; Do we see a marker?
1656 (string-match
1657 ;; jdb puts out a string of the following form when it
1658 ;; hits a breakpoint:
1659 ;;
1660 ;; <fully-qualified-class><method> (<class>:<line-number>)
1661 ;;
1662 ;; <fully-qualified-class>'s are composed of Java ID's
1663 ;; separated by periods. <method> and <class> are
1664 ;; also Java ID's. <method> begins with a period and
1665 ;; may contain less-than and greater-than (constructors,
1666 ;; for instance, are called <init> in the symbol table.)
1667 ;; Java ID's begin with a letter followed by letters
1668 ;; and/or digits. The set of letters includes underscore
1669 ;; and dollar sign.
1670 ;;
1671 ;; The first group matches <fully-qualified-class>,
1672 ;; the second group matches <class> and the third group
1673 ;; matches <line-number>. We don't care about using
1674 ;; <method> so we don't "group" it.
1675 ;;
1676 ;; FIXME: Java ID's are UNICODE strings, this matches ASCII
1677 ;; ID's only.
1678 "\\([a-zA-Z0-9.$_]+\\)\\.[a-zA-Z0-9$_<>]+ (\\([a-zA-Z0-9$_]+\\):\\([0-9]+\\))"
1679 gud-marker-acc start)
1680
1681 ;; Figure out the line on which to position the debugging arrow.
1682 ;; Return the info as a cons of the form:
1683 ;;
1684 ;; (<file-name> . <line-number>) .
1685 (if (setq
1686 file-found
1687 (gud-jdb-find-source-file
1688 (substring gud-marker-acc
1689 (match-beginning 1)
1690 (match-end 1))))
1691 (setq gud-last-frame
1692 (cons
1693 file-found
1694 (string-to-int
1695 (substring gud-marker-acc
1696 (match-beginning 3)
1697 (match-end 3)))))
1698 (message "Could not find source file."))
1699
1700 ;; Set start after the last character of STRING that we've looked at
1701 ;; and loop to look for another marker.
1702 (setq start (match-end 0))))
1703
1704 ;; We don't filter any debugger output so just return what we were given.
1705 string)
1706
1707(defun gud-jdb-find-file (f)
1708 (and (file-readable-p f)
1709 (find-file-noselect f)))
1710
1711(defvar gud-jdb-command-name "jdb" "Command that executes the Java debugger.")
1712
1713;;;###autoload
1714(defun jdb (command-line)
1715 "Run jdb with command line COMMAND-LINE in a buffer. The buffer is named
1716\"*gud*\" if no initial class is given or \"*gud-<initial-class-basename>*\"
1717if there is. If the \"-classpath\" switch is given, omit all whitespace
1718between it and it's value."
1719 (interactive
1720 (list (read-from-minibuffer "Run jdb (like this): "
1721 (if (consp gud-jdb-history)
1722 (car gud-jdb-history)
1723 (concat gud-jdb-command-name " "))
1724 nil nil
1725 '(gud-jdb-history . 1))))
1726
1727 (gud-common-init command-line 'gud-jdb-massage-args
1728 'gud-jdb-marker-filter 'gud-jdb-find-file)
1729
1730 (gud-def gud-break "stop at %l" "\C-b" "Set breakpoint at current line.")
1731 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
1732 (gud-def gud-step "step" "\C-s" "Step one source line with display.")
1733 (gud-def gud-next "next" "\C-n" "Step one line (skip functions).")
1734 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
1735
1736 (setq comint-prompt-regexp "^> \|^.+\[[0-9]+\] ")
1737 (setq paragraph-start comint-prompt-regexp)
1738 (run-hooks 'jdb-mode-hook)
1739
1740 ;; Create and bind the class/source association list as well as the source
1741 ;; file list.
1742 (setq
1743 gud-jdb-class-source-alist
1744 (gud-jdb-build-class-source-alist
1745 (setq
1746 gud-jdb-source-files
1747 (gud-jdb-build-source-files-list gud-jdb-directories "\\.java$")))))
1748\f
6496f3d2 1749
13b80a60
ER
1750;;
1751;; End of debugger-specific information
10a4c11f 1752;;
13b80a60 1753
575661b1 1754\f
53eb3a97
JB
1755;;; When we send a command to the debugger via gud-call, it's annoying
1756;;; to see the command and the new prompt inserted into the debugger's
1757;;; buffer; we have other ways of knowing the command has completed.
1758;;;
1759;;; If the buffer looks like this:
1760;;; --------------------
1761;;; (gdb) set args foo bar
1762;;; (gdb) -!-
1763;;; --------------------
1764;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1765;;; source file to set a breakpoint, we want the buffer to end up like
1766;;; this:
1767;;; --------------------
1768;;; (gdb) set args foo bar
1769;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1770;;; (gdb) -!-
1771;;; --------------------
1772;;; Essentially, the old prompt is deleted, and the command's output
1773;;; and the new prompt take its place.
1774;;;
1775;;; Not echoing the command is easy enough; you send it directly using
1776;;; process-send-string, and it never enters the buffer. However,
1777;;; getting rid of the old prompt is trickier; you don't want to do it
1778;;; when you send the command, since that will result in an annoying
1779;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1780;;; waits for a response from the debugger, and the new prompt is
1781;;; inserted. Instead, we'll wait until we actually get some output
1782;;; from the subprocess before we delete the prompt. If the command
1783;;; produced no output other than a new prompt, that prompt will most
1784;;; likely be in the first chunk of output received, so we will delete
1785;;; the prompt and then replace it with an identical one. If the
1786;;; command produces output, the prompt is moving anyway, so the
1787;;; flicker won't be annoying.
1788;;;
1789;;; So - when we want to delete the prompt upon receipt of the next
1790;;; chunk of debugger output, we position gud-delete-prompt-marker at
1791;;; the start of the prompt; the process filter will notice this, and
1792;;; delete all text between it and the process output marker. If
1793;;; gud-delete-prompt-marker points nowhere, we leave the current
1794;;; prompt alone.
1795(defvar gud-delete-prompt-marker nil)
1796
13b80a60 1797\f
c47264b3
RS
1798(put 'gud-mode 'mode-class 'special)
1799
13b80a60
ER
1800(defun gud-mode ()
1801 "Major mode for interacting with an inferior debugger process.
24d725c2 1802
6496f3d2 1803 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
b0764b2d
RS
1804M-x perldb, or M-x xdb. Each entry point finishes by executing a
1805hook; `gdb-mode-hook', `sdb-mode-hook', `dbx-mode-hook',
1806`perldb-mode-hook', or `xdb-mode-hook' respectively.
24d725c2 1807
ee0155df
ER
1808After startup, the following commands are available in both the GUD
1809interaction buffer and any source buffer GUD visits due to a breakpoint stop
1810or step operation:
13b80a60 1811
ee0155df
ER
1812\\[gud-break] sets a breakpoint at the current file and line. In the
1813GUD buffer, the current file and line are those of the last breakpoint or
1814step. In a source buffer, they are the buffer's file and current line.
1815
45025813
ER
1816\\[gud-remove] removes breakpoints on the current file and line.
1817
ee0155df 1818\\[gud-refresh] displays in the source window the last line referred to
24d725c2 1819in the gud buffer.
13b80a60 1820
ee0155df
ER
1821\\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1822step-one-line (not entering function calls), and step-one-instruction
1823and then update the source window with the current file and position.
1824\\[gud-cont] continues execution.
13b80a60 1825
ee0155df
ER
1826\\[gud-print] tries to find the largest C lvalue or function-call expression
1827around point, and sends it to the debugger for value display.
24d725c2 1828
6496f3d2
RS
1829The above commands are common to all supported debuggers except xdb which
1830does not support stepping instructions.
24d725c2 1831
6496f3d2 1832Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
ee0155df
ER
1833except that the breakpoint is temporary; that is, it is removed when
1834execution stops on it.
24d725c2 1835
6496f3d2 1836Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
ee0155df 1837frame. \\[gud-down] drops back down through one.
24d725c2 1838
6496f3d2 1839If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
ee0155df 1840the current function and stops.
24d725c2 1841
dcec1a3b
RS
1842All the keystrokes above are accessible in the GUD buffer
1843with the prefix C-c, and in all buffers through the prefix C-x C-a.
45025813 1844
ee0155df
ER
1845All pre-defined functions for which the concept make sense repeat
1846themselves the appropriate number of times if you give a prefix
1847argument.
13b80a60 1848
dcec1a3b 1849You may use the `gud-def' macro in the initialization hook to define other
ee0155df 1850commands.
13b80a60 1851
24d725c2
ER
1852Other commands for interacting with the debugger process are inherited from
1853comint mode, which see."
13b80a60
ER
1854 (interactive)
1855 (comint-mode)
13b80a60
ER
1856 (setq major-mode 'gud-mode)
1857 (setq mode-name "Debugger")
0b358e64 1858 (setq mode-line-process '(":%s"))
07fc7c8a
KH
1859 (use-local-map comint-mode-map)
1860 (gud-make-debug-menu)
2b64612d 1861 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
13b80a60
ER
1862 (make-local-variable 'gud-last-frame)
1863 (setq gud-last-frame nil)
1864 (make-local-variable 'comint-prompt-regexp)
aa9063cb
RS
1865 ;; Don't put repeated commands in command history many times.
1866 (make-local-variable 'comint-input-ignoredups)
1867 (setq comint-input-ignoredups t)
f54877b2 1868 (make-local-variable 'paragraph-start)
53eb3a97
JB
1869 (make-local-variable 'gud-delete-prompt-marker)
1870 (setq gud-delete-prompt-marker (make-marker))
ee97eac3 1871 (run-hooks 'gud-mode-hook))
13b80a60 1872
a223b10d
RM
1873;; Chop STRING into words separated by SPC or TAB and return a list of them.
1874(defun gud-chop-words (string)
1875 (let ((i 0) (beg 0)
1876 (len (length string))
1877 (words nil))
1878 (while (< i len)
1879 (if (memq (aref string i) '(?\t ? ))
1880 (progn
1881 (setq words (cons (substring string beg i) words)
1882 beg (1+ i))
1883 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1884 (setq beg (1+ beg)))
1885 (setq i (1+ beg)))
1886 (setq i (1+ i))))
1887 (if (< beg len)
1888 (setq words (cons (substring string beg) words)))
1889 (nreverse words)))
1890
1891;; Perform initializations common to all debuggers.
ee97eac3
RS
1892;; The first arg is the specified command line,
1893;; which starts with the program to debug.
1894;; The other three args specify the values to use
1895;; for local variables in the debugger buffer.
1896(defun gud-common-init (command-line massage-args marker-filter find-file)
a223b10d
RM
1897 (let* ((words (gud-chop-words command-line))
1898 (program (car words))
dfbd82a6
RS
1899 ;; Extract the file name from WORDS
1900 ;; and put t in its place.
1901 ;; Later on we will put the modified file name arg back there.
a223b10d
RM
1902 (file-word (let ((w (cdr words)))
1903 (while (and w (= ?- (aref (car w) 0)))
1904 (setq w (cdr w)))
c27c1042
KH
1905 (and w
1906 (prog1 (car w)
1907 (setcar w t)))))
0ec5b01d
RS
1908 (file-subst
1909 (and file-word (substitute-in-file-name file-word)))
dfbd82a6 1910 (args (cdr words))
0ec5b01d
RS
1911 ;; If a directory was specified, expand the file name.
1912 ;; Otherwise, don't expand it, so GDB can use the PATH.
1913 ;; A file name without directory is literally valid
1914 ;; only if the file exists in ., and in that case,
1915 ;; omitting the expansion here has no visible effect.
52b85866 1916 (file (and file-word
0ec5b01d
RS
1917 (if (file-name-directory file-subst)
1918 (expand-file-name file-subst)
1919 file-subst)))
c27c1042
KH
1920 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
1921 (switch-to-buffer (concat "*gud" filepart "*"))
0ec5b01d
RS
1922 ;; Set default-directory to the file's directory.
1923 (and file-word
1924 ;; Don't set default-directory if no directory was specified.
1925 ;; In that case, either the file is found in the current directory,
1926 ;; in which case this setq is a no-op,
1927 ;; or it is found by searching PATH,
1928 ;; in which case we don't know what directory it was found in.
1929 (file-name-directory file)
1930 (setq default-directory (file-name-directory file)))
ee97eac3
RS
1931 (or (bolp) (newline))
1932 (insert "Current directory is " default-directory "\n")
dfbd82a6
RS
1933 ;; Put the substituted and expanded file name back in its place.
1934 (let ((w args))
1935 (while (and w (not (eq (car w) t)))
1936 (setq w (cdr w)))
c27c1042
KH
1937 (if w
1938 (setcar w file)))
1939 (apply 'make-comint (concat "gud" filepart) program nil
2b1ffa34 1940 (funcall massage-args file args)))
ee97eac3 1941 ;; Since comint clobbered the mode, we don't set it until now.
13b80a60 1942 (gud-mode)
ee97eac3
RS
1943 (make-local-variable 'gud-marker-filter)
1944 (setq gud-marker-filter marker-filter)
1945 (make-local-variable 'gud-find-file)
1946 (setq gud-find-file find-file)
1947
13b80a60
ER
1948 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1949 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
13b80a60
ER
1950 (gud-set-buffer)
1951 )
1952
1953(defun gud-set-buffer ()
1954 (cond ((eq major-mode 'gud-mode)
ee0155df
ER
1955 (setq gud-comint-buffer (current-buffer)))))
1956
11736488
RS
1957(defvar gud-filter-defer-flag nil
1958 "Non-nil means don't process anything from the debugger right now.
1959It is saved for when this flag is not set.")
1960
1961(defvar gud-filter-pending-text nil
1962 "Non-nil means this is text that has been saved for later in `gud-filter'.")
1963
ee0155df
ER
1964;; These functions are responsible for inserting output from your debugger
1965;; into the buffer. The hard work is done by the method that is
1966;; the value of gud-marker-filter.
13b80a60 1967
ee0155df 1968(defun gud-filter (proc string)
13b80a60 1969 ;; Here's where the actual buffer insertion is done
7f4bda7c 1970 (let (output process-window)
c561a0cb 1971 (if (buffer-name (process-buffer proc))
11736488
RS
1972 (if gud-filter-defer-flag
1973 ;; If we can't process any text now,
1974 ;; save it for later.
1975 (setq gud-filter-pending-text
1976 (concat (or gud-filter-pending-text "") string))
c7856dbe
RS
1977
1978 ;; If we have to ask a question during the processing,
1979 ;; defer any additional text that comes from the debugger
1980 ;; during that time.
1981 (let ((gud-filter-defer-flag t))
1982 ;; Process now any text we previously saved up.
1983 (if gud-filter-pending-text
1984 (setq string (concat gud-filter-pending-text string)
1985 gud-filter-pending-text nil))
1986 (save-excursion
11736488
RS
1987 (set-buffer (process-buffer proc))
1988 ;; If we have been so requested, delete the debugger prompt.
1989 (if (marker-buffer gud-delete-prompt-marker)
1990 (progn
1991 (delete-region (process-mark proc) gud-delete-prompt-marker)
1992 (set-marker gud-delete-prompt-marker nil)))
1993 ;; Save the process output, checking for source file markers.
1994 (setq output (gud-marker-filter string))
1995 ;; Check for a filename-and-line number.
1996 ;; Don't display the specified file
1997 ;; unless (1) point is at or after the position where output appears
1998 ;; and (2) this buffer is on the screen.
7f4bda7c
RS
1999 (setq process-window
2000 (and gud-last-frame
2001 (>= (point) (process-mark proc))
c7856dbe
RS
2002 (get-buffer-window (current-buffer))))
2003
2004 ;; Let the comint filter do the actual insertion.
2005 ;; That lets us inherit various comint features.
2006 (comint-output-filter proc output)))
2007
2008 ;; Put the arrow on the source line.
58684193 2009 ;; This must be outside of the save-excursion
c7856dbe
RS
2010 ;; in case the source file is our current buffer.
2011 (if process-window
2012 (save-selected-window
2013 (select-window process-window)
58684193
RS
2014 (gud-display-frame))
2015 ;; We have to be in the proper buffer, (process-buffer proc),
2016 ;; but not in a save-excursion, because that would restore point.
2017 (let ((old-buf (current-buffer)))
2018 (set-buffer (process-buffer proc))
2019 (unwind-protect
2020 (gud-display-frame)
2021 (set-buffer old-buf))))
c7856dbe
RS
2022
2023 ;; If we deferred text that arrived during this processing,
2024 ;; handle it now.
2025 (if gud-filter-pending-text
2026 (gud-filter proc ""))))))
13b80a60
ER
2027
2028(defun gud-sentinel (proc msg)
2029 (cond ((null (buffer-name (process-buffer proc)))
2030 ;; buffer killed
2031 ;; Stop displaying an arrow in a source file.
2032 (setq overlay-arrow-position nil)
2033 (set-process-buffer proc nil))
2034 ((memq (process-status proc) '(signal exit))
2035 ;; Stop displaying an arrow in a source file.
2036 (setq overlay-arrow-position nil)
13b80a60
ER
2037 (let* ((obuf (current-buffer)))
2038 ;; save-excursion isn't the right thing if
2039 ;; process-buffer is current-buffer
2040 (unwind-protect
2041 (progn
2042 ;; Write something in *compilation* and hack its mode line,
2043 (set-buffer (process-buffer proc))
1a3ce5c4
AS
2044 ;; Fix the mode line.
2045 (setq mode-line-process
2046 (concat ":"
2047 (symbol-name (process-status proc))))
e726eb56 2048 (force-mode-line-update)
13b80a60
ER
2049 (if (eobp)
2050 (insert ?\n mode-name " " msg)
2051 (save-excursion
2052 (goto-char (point-max))
2053 (insert ?\n mode-name " " msg)))
2054 ;; If buffer and mode line will show that the process
2055 ;; is dead, we can delete it now. Otherwise it
2056 ;; will stay around until M-x list-processes.
2057 (delete-process proc))
2058 ;; Restore old buffer, but don't restore old point
2059 ;; if obuf is the gud buffer.
2060 (set-buffer obuf))))))
2061
13b80a60
ER
2062(defun gud-display-frame ()
2063 "Find and obey the last filename-and-line marker from the debugger.
2064Obeying it means displaying in another window the specified file and line."
2065 (interactive)
2066 (if gud-last-frame
ee0155df 2067 (progn
13b80a60
ER
2068 (gud-set-buffer)
2069 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
32ab4c10
JB
2070 (setq gud-last-last-frame gud-last-frame
2071 gud-last-frame nil))))
13b80a60
ER
2072
2073;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
2074;; and that its line LINE is visible.
2075;; Put the overlay-arrow on the line LINE in that buffer.
ee0155df
ER
2076;; Most of the trickiness in here comes from wanting to preserve the current
2077;; region-restriction if that's possible. We use an explicit display-buffer
2078;; to get around the fact that this is called inside a save-excursion.
13b80a60
ER
2079
2080(defun gud-display-line (true-file line)
8f6c93e2 2081 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
36d6372a
RS
2082 (buffer
2083 (save-excursion
2084 (or (eq (current-buffer) gud-comint-buffer)
2085 (set-buffer gud-comint-buffer))
2086 (gud-find-file true-file)))
f55d65a1
RS
2087 (window (and buffer (or (get-buffer-window buffer)
2088 (display-buffer buffer))))
13b80a60 2089 (pos))
87a3fd19
RS
2090 (if buffer
2091 (progn
2092 (save-excursion
2093 (set-buffer buffer)
2094 (save-restriction
2095 (widen)
2096 (goto-line line)
2097 (setq pos (point))
2098 (setq overlay-arrow-string "=>")
2099 (or overlay-arrow-position
2100 (setq overlay-arrow-position (make-marker)))
2101 (set-marker overlay-arrow-position (point) (current-buffer)))
2102 (cond ((or (< pos (point-min)) (> pos (point-max)))
2103 (widen)
2104 (goto-char pos))))
2105 (set-window-point window overlay-arrow-position)))))
53eb3a97 2106
ee0155df
ER
2107;;; The gud-call function must do the right thing whether its invoking
2108;;; keystroke is from the GUD buffer itself (via major-mode binding)
5b08a462
ER
2109;;; or a C buffer. In the former case, we want to supply data from
2110;;; gud-last-frame. Here's how we do it:
13b80a60 2111
ee0155df 2112(defun gud-format-command (str arg)
13eaa026
RS
2113 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
2114 (frame (or gud-last-frame gud-last-last-frame))
2115 result)
2116 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
2117 (let ((key (string-to-char (substring str (match-beginning 2))))
2118 subst)
2119 (cond
2120 ((eq key ?f)
2121 (setq subst (file-name-nondirectory (if insource
2122 (buffer-file-name)
2123 (car frame)))))
2124 ((eq key ?d)
2125 (setq subst (file-name-directory (if insource
7f359a21 2126 (buffer-file-name)
13eaa026
RS
2127 (car frame)))))
2128 ((eq key ?l)
2129 (setq subst (if insource
2130 (save-excursion
2131 (beginning-of-line)
2132 (save-restriction (widen)
2133 (1+ (count-lines 1 (point)))))
2134 (cdr frame))))
2135 ((eq key ?e)
ab4b0d2f 2136 (setq subst (gud-find-c-expr)))
13eaa026
RS
2137 ((eq key ?a)
2138 (setq subst (gud-read-address)))
2139 ((eq key ?p)
2140 (setq subst (if arg (int-to-string arg) ""))))
2141 (setq result (concat result
2142 (substring str (match-beginning 1) (match-end 1))
2143 subst)))
2144 (setq str (substring str (match-end 2))))
2145 ;; There might be text left in STR when the loop ends.
2146 (concat result str)))
13b80a60 2147
6bde8427 2148(defun gud-read-address ()
13b80a60
ER
2149 "Return a string containing the core-address found in the buffer at point."
2150 (save-excursion
6bde8427 2151 (let ((pt (point)) found begin)
ee0155df 2152 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
6bde8427
JB
2153 (cond
2154 (found (forward-char 2)
2155 (buffer-substring found
2156 (progn (re-search-forward "[^0-9a-f]")
2157 (forward-char -1)
2158 (point))))
2159 (t (setq begin (progn (re-search-backward "[^0-9]")
2160 (forward-char 1)
2161 (point)))
2162 (forward-char 1)
2163 (re-search-forward "[^0-9]")
2164 (forward-char -1)
2165 (buffer-substring begin (point)))))))
13b80a60 2166
ee0155df
ER
2167(defun gud-call (fmt &optional arg)
2168 (let ((msg (gud-format-command fmt arg)))
2169 (message "Command: %s" msg)
2170 (sit-for 0)
2171 (gud-basic-call msg)))
13b80a60 2172
ee0155df
ER
2173(defun gud-basic-call (command)
2174 "Invoke the debugger COMMAND displaying source in other window."
2175 (interactive)
2176 (gud-set-buffer)
2177 (let ((command (concat command "\n"))
2178 (proc (get-buffer-process gud-comint-buffer)))
4b8b296e 2179 (or proc (error "Current buffer has no process"))
ee0155df
ER
2180 ;; Arrange for the current prompt to get deleted.
2181 (save-excursion
2182 (set-buffer gud-comint-buffer)
2183 (goto-char (process-mark proc))
2184 (beginning-of-line)
2185 (if (looking-at comint-prompt-regexp)
2186 (set-marker gud-delete-prompt-marker (point))))
2187 (process-send-string proc command)))
2188
2189(defun gud-refresh (&optional arg)
2190 "Fix up a possibly garbled display, and redraw the arrow."
13b80a60 2191 (interactive "P")
ee0155df 2192 (recenter arg)
32ab4c10 2193 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
ee0155df 2194 (gud-display-frame))
dcec1a3b 2195\f
67580ab5
RS
2196
2197(defun gud-new-keymap (map)
2198 "Return a new keymap which inherits from MAP and has name `Gud'."
2199 (nconc (make-sparse-keymap "Gud") map))
2200
2201(defun gud-make-debug-menu ()
2202 "Make sure the current local map has a [menu-bar debug] submap.
2203If it doesn't, replace it with a new map that inherits it,
2204and create such a submap in that new map."
2205 (if (and (current-local-map)
2206 (lookup-key (current-local-map) [menu-bar debug]))
2207 nil
2208 (use-local-map (gud-new-keymap (current-local-map)))
2209 (define-key (current-local-map) [menu-bar debug]
2210 (cons "Gud" (gud-new-keymap gud-menu-map)))))
2211\f
ee0155df
ER
2212;;; Code for parsing expressions out of C code. The single entry point is
2213;;; find-c-expr, which tries to return an lvalue expression from around point.
2214;;;
2215;;; The rest of this file is a hacked version of gdbsrc.el by
2216;;; Debby Ayers <ayers@asc.slb.com>,
2217;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
ee0155df 2218
ab4b0d2f 2219(defun gud-find-c-expr ()
ee0155df
ER
2220 "Returns the C expr that surrounds point."
2221 (interactive)
2222 (save-excursion
ab4b0d2f 2223 (let (p expr test-expr)
ee0155df 2224 (setq p (point))
ab4b0d2f
RS
2225 (setq expr (gud-innermost-expr))
2226 (setq test-expr (gud-prev-expr))
2227 (while (and test-expr (gud-expr-compound test-expr expr))
2228 (let ((prev-expr expr))
2229 (setq expr (cons (car test-expr) (cdr expr)))
2230 (goto-char (car expr))
2231 (setq test-expr (gud-prev-expr))
2232 ;; If we just pasted on the condition of an if or while,
2233 ;; throw it away again.
2234 (if (member (buffer-substring (car test-expr) (cdr test-expr))
2235 '("if" "while" "for"))
2236 (setq test-expr nil
2237 expr prev-expr))))
ee0155df 2238 (goto-char p)
ab4b0d2f
RS
2239 (setq test-expr (gud-next-expr))
2240 (while (gud-expr-compound expr test-expr)
ee0155df 2241 (setq expr (cons (car expr) (cdr test-expr)))
ab4b0d2f 2242 (setq test-expr (gud-next-expr))
ee0155df 2243 )
dcec1a3b 2244 (buffer-substring (car expr) (cdr expr)))))
ee0155df 2245
ab4b0d2f
RS
2246(defun gud-innermost-expr ()
2247 "Returns the smallest expr that point is in; move point to beginning of it.
ee0155df
ER
2248The expr is represented as a cons cell, where the car specifies the point in
2249the current buffer that marks the beginning of the expr and the cdr specifies
dcec1a3b 2250the character after the end of the expr."
ab4b0d2f
RS
2251 (let ((p (point)) begin end)
2252 (gud-backward-sexp)
ee0155df 2253 (setq begin (point))
ab4b0d2f 2254 (gud-forward-sexp)
ee0155df
ER
2255 (setq end (point))
2256 (if (>= p end)
2257 (progn
2258 (setq begin p)
2259 (goto-char p)
ab4b0d2f
RS
2260 (gud-forward-sexp)
2261 (setq end (point)))
ee0155df
ER
2262 )
2263 (goto-char begin)
dcec1a3b 2264 (cons begin end)))
ee0155df 2265
ab4b0d2f 2266(defun gud-backward-sexp ()
dcec1a3b 2267 "Version of `backward-sexp' that catches errors."
ee0155df
ER
2268 (condition-case nil
2269 (backward-sexp)
2270 (error t)))
2271
ab4b0d2f 2272(defun gud-forward-sexp ()
dcec1a3b 2273 "Version of `forward-sexp' that catches errors."
ee0155df
ER
2274 (condition-case nil
2275 (forward-sexp)
2276 (error t)))
2277
ab4b0d2f 2278(defun gud-prev-expr ()
ee0155df
ER
2279 "Returns the previous expr, point is set to beginning of that expr.
2280The expr is represented as a cons cell, where the car specifies the point in
2281the current buffer that marks the beginning of the expr and the cdr specifies
2282the character after the end of the expr"
2283 (let ((begin) (end))
ab4b0d2f 2284 (gud-backward-sexp)
ee0155df 2285 (setq begin (point))
ab4b0d2f 2286 (gud-forward-sexp)
ee0155df
ER
2287 (setq end (point))
2288 (goto-char begin)
2289 (cons begin end)))
2290
ab4b0d2f 2291(defun gud-next-expr ()
ee0155df
ER
2292 "Returns the following expr, point is set to beginning of that expr.
2293The expr is represented as a cons cell, where the car specifies the point in
2294the current buffer that marks the beginning of the expr and the cdr specifies
dcec1a3b 2295the character after the end of the expr."
ee0155df 2296 (let ((begin) (end))
ab4b0d2f
RS
2297 (gud-forward-sexp)
2298 (gud-forward-sexp)
ee0155df 2299 (setq end (point))
ab4b0d2f 2300 (gud-backward-sexp)
ee0155df 2301 (setq begin (point))
dcec1a3b 2302 (cons begin end)))
ee0155df 2303
ab4b0d2f
RS
2304(defun gud-expr-compound-sep (span-start span-end)
2305 "Scan from SPAN-START to SPAN-END for punctuation characters.
2306If `->' is found, return `?.'. If `.' is found, return `?.'.
2307If any other punctuation is found, return `??'.
2308If no punctuation is found, return `? '."
2309 (let ((result ?\ )
ee0155df
ER
2310 (syntax))
2311 (while (< span-start span-end)
2312 (setq syntax (char-syntax (char-after span-start)))
2313 (cond
ab4b0d2f 2314 ((= syntax ?\ ) t)
ee0155df
ER
2315 ((= syntax ?.) (setq syntax (char-after span-start))
2316 (cond
2317 ((= syntax ?.) (setq result ?.))
2318 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
2319 (setq result ?.)
2320 (setq span-start (+ span-start 1)))
2321 (t (setq span-start span-end)
2322 (setq result ??)))))
2323 (setq span-start (+ span-start 1)))
dcec1a3b 2324 result))
ee0155df 2325
ab4b0d2f
RS
2326(defun gud-expr-compound (first second)
2327 "Non-nil if concatenating FIRST and SECOND makes a single C expression.
dcec1a3b 2328The two exprs are represented as a cons cells, where the car
ee0155df 2329specifies the point in the current buffer that marks the beginning of the
dcec1a3b 2330expr and the cdr specifies the character after the end of the expr.
ee0155df
ER
2331Link exprs of the form:
2332 Expr -> Expr
2333 Expr . Expr
2334 Expr (Expr)
2335 Expr [Expr]
2336 (Expr) Expr
2337 [Expr] Expr"
2338 (let ((span-start (cdr first))
2339 (span-end (car second))
2340 (syntax))
ab4b0d2f 2341 (setq syntax (gud-expr-compound-sep span-start span-end))
ee0155df
ER
2342 (cond
2343 ((= (car first) (car second)) nil)
2344 ((= (cdr first) (cdr second)) nil)
2345 ((= syntax ?.) t)
ab4b0d2f 2346 ((= syntax ?\ )
ee0155df
ER
2347 (setq span-start (char-after (- span-start 1)))
2348 (setq span-end (char-after span-end))
2349 (cond
ab4b0d2f
RS
2350 ((= span-start ?)) t)
2351 ((= span-start ?]) t)
2352 ((= span-end ?() t)
2353 ((= span-end ?[) t)
2354 (t nil)))
dcec1a3b 2355 (t nil))))
5b08a462 2356
96f4e22e
RM
2357(provide 'gud)
2358
f961a17c 2359;;; gud.el ends here