(gud-filter): Save up text that arrives while processing previous text.
[bpt/emacs.git] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
2 ;;; under Emacs
3
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Maintainer: FSF
6 ;; Keywords: unix, tools
7
8 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
33 ;; added support for xdb (HPUX debugger). Rick Sladkey <jrs@world.std.com>
34 ;; wrote the GDB command completion code. Dave Love <d.love@dl.ac.uk>
35 ;; added the IRIX kluge, re-implemented the Mips-ish variant and added
36 ;; a menu.
37
38 ;;; Code:
39
40 (require 'comint)
41 (require 'etags)
42
43 ;; ======================================================================
44 ;; GUD commands must be visible in C buffers visited by GUD
45
46 (defvar gud-key-prefix "\C-x\C-a"
47 "Prefix of all GUD commands valid in C buffers.")
48
49 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
50 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
51
52 (defvar gud-marker-filter nil)
53 (put 'gud-marker-filter 'permanent-local t)
54 (defvar gud-find-file nil)
55 (put 'gud-find-file 'permanent-local t)
56
57 (defun gud-marker-filter (&rest args)
58 (apply gud-marker-filter args))
59
60 (defun gud-find-file (file)
61 ;; Don't get confused by double slashes in the name that comes from GDB.
62 (while (string-match "//+" file)
63 (setq file (replace-match "/" t t file)))
64 (funcall gud-find-file file))
65
66 ;; Keymap definitions for menu bar entries common to all debuggers and
67 ;; slots for debugger-dependent ones in sensible places. (Defined here
68 ;; before use.)
69 (defvar gud-menu-map (make-sparse-keymap "Gud") nil)
70 (define-key gud-menu-map [refresh] '("Refresh" . gud-refresh))
71 (define-key gud-menu-map [remove] '("Remove Breakpoint" . gud-remove))
72 (define-key gud-menu-map [tbreak] nil) ; gdb, sdb and xdb
73 (define-key gud-menu-map [break] '("Set Breakpoint" . gud-break))
74 (define-key gud-menu-map [up] nil) ; gdb, dbx, and xdb
75 (define-key gud-menu-map [down] nil) ; gdb, dbx, and xdb
76 (define-key gud-menu-map [print] '("Print Expression" . gud-print))
77 (define-key gud-menu-map [finish] nil) ; gdb or xdb
78 (define-key gud-menu-map [stepi] '("Step Instruction" . gud-stepi))
79 (define-key gud-menu-map [step] '("Step Line" . gud-step))
80 (define-key gud-menu-map [next] '("Next Line" . gud-next))
81 (define-key gud-menu-map [cont] '("Continue" . gud-cont))
82 \f
83 ;; ======================================================================
84 ;; command definition
85
86 ;; This macro is used below to define some basic debugger interface commands.
87 ;; Of course you may use `gud-def' with any other debugger command, including
88 ;; user defined ones.
89
90 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
91 ;; which defines FUNC to send the command NAME to the debugger, gives
92 ;; it the docstring DOC, and binds that function to KEY in the GUD
93 ;; major mode. The function is also bound in the global keymap with the
94 ;; GUD prefix.
95
96 (defmacro gud-def (func cmd key &optional doc)
97 "Define FUNC to be a command sending STR and bound to KEY, with
98 optional doc string DOC. Certain %-escapes in the string arguments
99 are interpreted specially if present. These are:
100
101 %f name (without directory) of current source file.
102 %d directory of current source file.
103 %l number of current source line
104 %e text of the C lvalue or function-call expression surrounding point.
105 %a text of the hexadecimal address surrounding point
106 %p prefix argument to the command (if any) as a number
107
108 The `current' source file is the file of the current buffer (if
109 we're in a C file) or the source file current at the last break or
110 step (if we're in the GUD buffer).
111 The `current' line is that of the current buffer (if we're in a
112 source file) or the source line number at the last break or step (if
113 we're in the GUD buffer)."
114 (list 'progn
115 (list 'defun func '(arg)
116 (or doc "")
117 '(interactive "p")
118 (list 'gud-call cmd 'arg))
119 (if key
120 (list 'define-key
121 '(current-local-map)
122 (concat "\C-c" key)
123 (list 'quote func)))
124 (if key
125 (list 'global-set-key
126 (list 'concat 'gud-key-prefix key)
127 (list 'quote func)))))
128
129 ;; Where gud-display-frame should put the debugging arrow. This is
130 ;; set by the marker-filter, which scans the debugger's output for
131 ;; indications of the current program counter.
132 (defvar gud-last-frame nil)
133
134 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
135 ;; the last frame, even if it's been called before and gud-last-frame has
136 ;; been set to nil.
137 (defvar gud-last-last-frame nil)
138
139 ;; All debugger-specific information is collected here.
140 ;; Here's how it works, in case you ever need to add a debugger to the mode.
141 ;;
142 ;; Each entry must define the following at startup:
143 ;;
144 ;;<name>
145 ;; comint-prompt-regexp
146 ;; gud-<name>-massage-args
147 ;; gud-<name>-marker-filter
148 ;; gud-<name>-find-file
149 ;;
150 ;; The job of the massage-args method is to modify the given list of
151 ;; debugger arguments before running the debugger.
152 ;;
153 ;; The job of the marker-filter method is to detect file/line markers in
154 ;; strings and set the global gud-last-frame to indicate what display
155 ;; action (if any) should be triggered by the marker. Note that only
156 ;; whatever the method *returns* is displayed in the buffer; thus, you
157 ;; can filter the debugger's output, interpreting some and passing on
158 ;; the rest.
159 ;;
160 ;; The job of the find-file method is to visit and return the buffer indicated
161 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
162 ;; something else. It would be good if it also copied the Gud menubar entry.
163 \f
164 ;; ======================================================================
165 ;; gdb functions
166
167 ;;; History of argument lists passed to gdb.
168 (defvar gud-gdb-history nil)
169
170 (defun gud-gdb-massage-args (file args)
171 (cons "-fullname" args))
172
173 (defvar gud-gdb-marker-regexp
174 (concat "\032\032\\([^" path-separator "\n]*\\)" path-separator
175 "\\([0-9]*\\)" path-separator ".*\n"))
176
177 ;; There's no guarantee that Emacs will hand the filter the entire
178 ;; marker at once; it could be broken up across several strings. We
179 ;; might even receive a big chunk with several markers in it. If we
180 ;; receive a chunk of text which looks like it might contain the
181 ;; beginning of a marker, we save it here between calls to the
182 ;; filter.
183 (defvar gud-marker-acc "")
184 (make-variable-buffer-local 'gud-marker-acc)
185
186 (defun gud-gdb-marker-filter (string)
187 (setq gud-marker-acc (concat gud-marker-acc string))
188 (let ((output ""))
189
190 ;; Process all the complete markers in this chunk.
191 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
192 (setq
193
194 ;; Extract the frame position from the marker.
195 gud-last-frame
196 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
197 (string-to-int (substring gud-marker-acc
198 (match-beginning 2)
199 (match-end 2))))
200
201 ;; Append any text before the marker to the output we're going
202 ;; to return - we don't include the marker in this text.
203 output (concat output
204 (substring gud-marker-acc 0 (match-beginning 0)))
205
206 ;; Set the accumulator to the remaining text.
207 gud-marker-acc (substring gud-marker-acc (match-end 0))))
208
209 ;; Does the remaining text look like it might end with the
210 ;; beginning of another marker? If it does, then keep it in
211 ;; gud-marker-acc until we receive the rest of it. Since we
212 ;; know the full marker regexp above failed, it's pretty simple to
213 ;; test for marker starts.
214 (if (string-match "\032.*\\'" gud-marker-acc)
215 (progn
216 ;; Everything before the potential marker start can be output.
217 (setq output (concat output (substring gud-marker-acc
218 0 (match-beginning 0))))
219
220 ;; Everything after, we save, to combine with later input.
221 (setq gud-marker-acc
222 (substring gud-marker-acc (match-beginning 0))))
223
224 (setq output (concat output gud-marker-acc)
225 gud-marker-acc ""))
226
227 output))
228
229 (defun gud-new-keymap (map)
230 "Return a new keymap which inherits from MAP and has name `Gud'."
231 (nconc (make-sparse-keymap "Gud") map))
232
233 (defun gud-make-debug-menu ()
234 "Make sure the current local map has a [menu-bar debug] submap.
235 If it doesn't, replace it with a new map that inherits it,
236 and create such a submap in that new map."
237 (if (and (current-local-map)
238 (lookup-key (current-local-map) [menu-bar debug]))
239 nil
240 (use-local-map (gud-new-keymap (current-local-map)))
241 (define-key (current-local-map) [menu-bar debug]
242 (cons "Gud" (gud-new-keymap gud-menu-map)))))
243
244 (defun gud-gdb-find-file (f)
245 (save-excursion
246 (let ((buf (find-file-noselect f)))
247 (set-buffer buf)
248 (gud-make-debug-menu)
249 (local-set-key [menu-bar debug tbreak]
250 '("Temporary Breakpoint" . gud-tbreak))
251 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
252 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
253 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
254 buf)))
255
256 (defvar gdb-minibuffer-local-map nil
257 "Keymap for minibuffer prompting of gdb startup command.")
258 (if gdb-minibuffer-local-map
259 ()
260 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
261 (define-key
262 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
263
264 ;;;###autoload
265 (defun gdb (command-line)
266 "Run gdb on program FILE in buffer *gud-FILE*.
267 The directory containing FILE becomes the initial working directory
268 and source-file directory for your debugger."
269 (interactive
270 (list (read-from-minibuffer "Run gdb (like this): "
271 (if (consp gud-gdb-history)
272 (car gud-gdb-history)
273 "gdb ")
274 gdb-minibuffer-local-map nil
275 '(gud-gdb-history . 1))))
276
277 (gud-common-init command-line 'gud-gdb-massage-args
278 'gud-gdb-marker-filter 'gud-gdb-find-file)
279
280 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
281 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
282 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
283 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
284 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
285 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
286 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
287 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
288 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
289 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
290 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
291
292 (local-set-key "\C-i" 'gud-gdb-complete-command)
293 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
294 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
295 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
296 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
297 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
298 (setq paragraph-start comint-prompt-regexp)
299 (run-hooks 'gdb-mode-hook)
300 )
301
302 ;; One of the nice features of GDB is its impressive support for
303 ;; context-sensitive command completion. We preserve that feature
304 ;; in the GUD buffer by using a GDB command designed just for Emacs.
305
306 ;; The completion process filter indicates when it is finished.
307 (defvar gud-gdb-complete-in-progress)
308
309 ;; Since output may arrive in fragments we accumulate partials strings here.
310 (defvar gud-gdb-complete-string)
311
312 ;; We need to know how much of the completion to chop off.
313 (defvar gud-gdb-complete-break)
314
315 ;; The completion list is constructed by the process filter.
316 (defvar gud-gdb-complete-list)
317
318 (defvar gud-comint-buffer nil)
319
320 (defun gud-gdb-complete-command ()
321 "Perform completion on the GDB command preceding point.
322 This is implemented using the GDB `complete' command which isn't
323 available with older versions of GDB."
324 (interactive)
325 (let* ((end (point))
326 (command (save-excursion
327 (beginning-of-line)
328 (and (looking-at comint-prompt-regexp)
329 (goto-char (match-end 0)))
330 (buffer-substring (point) end)))
331 command-word)
332 ;; Find the word break. This match will always succeed.
333 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
334 (setq gud-gdb-complete-break (match-beginning 2)
335 command-word (substring command gud-gdb-complete-break))
336 ;; Temporarily install our filter function.
337 (let ((gud-marker-filter 'gud-gdb-complete-filter))
338 ;; Issue the command to GDB.
339 (gud-basic-call (concat "complete " command))
340 (setq gud-gdb-complete-in-progress t
341 gud-gdb-complete-string nil
342 gud-gdb-complete-list nil)
343 ;; Slurp the output.
344 (while gud-gdb-complete-in-progress
345 (accept-process-output (get-buffer-process gud-comint-buffer))))
346 ;; Protect against old versions of GDB.
347 (and gud-gdb-complete-list
348 (string-match "^Undefined command: \"complete\""
349 (car gud-gdb-complete-list))
350 (error "This version of GDB doesn't support the `complete' command."))
351 ;; Sort the list like readline.
352 (setq gud-gdb-complete-list
353 (sort gud-gdb-complete-list (function string-lessp)))
354 ;; Remove duplicates.
355 (let ((first gud-gdb-complete-list)
356 (second (cdr gud-gdb-complete-list)))
357 (while second
358 (if (string-equal (car first) (car second))
359 (setcdr first (setq second (cdr second)))
360 (setq first second
361 second (cdr second)))))
362 ;; Add a trailing single quote if there is a unique completion
363 ;; and it contains an odd number of unquoted single quotes.
364 (and (= (length gud-gdb-complete-list) 1)
365 (let ((str (car gud-gdb-complete-list))
366 (pos 0)
367 (count 0))
368 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
369 (setq count (1+ count)
370 pos (match-end 0)))
371 (and (= (mod count 2) 1)
372 (setq gud-gdb-complete-list (list (concat str "'"))))))
373 ;; Let comint handle the rest.
374 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
375
376 ;; The completion process filter is installed temporarily to slurp the
377 ;; output of GDB up to the next prompt and build the completion list.
378 (defun gud-gdb-complete-filter (string)
379 (setq string (concat gud-gdb-complete-string string))
380 (while (string-match "\n" string)
381 (setq gud-gdb-complete-list
382 (cons (substring string gud-gdb-complete-break (match-beginning 0))
383 gud-gdb-complete-list))
384 (setq string (substring string (match-end 0))))
385 (if (string-match comint-prompt-regexp string)
386 (progn
387 (setq gud-gdb-complete-in-progress nil)
388 string)
389 (progn
390 (setq gud-gdb-complete-string string)
391 "")))
392
393 \f
394 ;; ======================================================================
395 ;; sdb functions
396
397 ;;; History of argument lists passed to sdb.
398 (defvar gud-sdb-history nil)
399
400 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
401 "If nil, we're on a System V Release 4 and don't need the tags hack.")
402
403 (defvar gud-sdb-lastfile nil)
404
405 (defun gud-sdb-massage-args (file args) args)
406
407 (defun gud-sdb-marker-filter (string)
408 (setq gud-marker-acc
409 (if gud-marker-acc (concat gud-marker-acc string) string))
410 (let (start)
411 ;; Process all complete markers in this chunk
412 (while
413 (cond
414 ;; System V Release 3.2 uses this format
415 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
416 gud-marker-acc start)
417 (setq gud-last-frame
418 (cons
419 (substring gud-marker-acc (match-beginning 2) (match-end 2))
420 (string-to-int
421 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
422 ;; System V Release 4.0 quite often clumps two lines together
423 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
424 gud-marker-acc start)
425 (setq gud-sdb-lastfile
426 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
427 (setq gud-last-frame
428 (cons
429 gud-sdb-lastfile
430 (string-to-int
431 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
432 ;; System V Release 4.0
433 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
434 gud-marker-acc start)
435 (setq gud-sdb-lastfile
436 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
437 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
438 gud-marker-acc start))
439 (setq gud-last-frame
440 (cons
441 gud-sdb-lastfile
442 (string-to-int
443 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
444 (t
445 (setq gud-sdb-lastfile nil)))
446 (setq start (match-end 0)))
447
448 ;; Search for the last incomplete line in this chunk
449 (while (string-match "\n" gud-marker-acc start)
450 (setq start (match-end 0)))
451
452 ;; If we have an incomplete line, store it in gud-marker-acc.
453 ;; Otherwise clear gud-marker-acc. to avoid an
454 ;; unnecessary concat when this function runs next.
455 (setq gud-marker-acc
456 (if (= start (length gud-marker-acc))
457 (substring gud-marker-acc start)
458 nil)))
459 string)
460
461 (defun gud-sdb-find-file (f)
462 (save-excursion
463 (let ((buf (if gud-sdb-needs-tags
464 (find-tag-noselect f)
465 (find-file-noselect f))))
466 (set-buffer buf)
467 (gud-make-debug-menu)
468 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
469 buf)))
470
471 ;;;###autoload
472 (defun sdb (command-line)
473 "Run sdb on program FILE in buffer *gud-FILE*.
474 The directory containing FILE becomes the initial working directory
475 and source-file directory for your debugger."
476 (interactive
477 (list (read-from-minibuffer "Run sdb (like this): "
478 (if (consp gud-sdb-history)
479 (car gud-sdb-history)
480 "sdb ")
481 nil nil
482 '(gud-sdb-history . 1))))
483 (if (and gud-sdb-needs-tags
484 (not (and (boundp 'tags-file-name)
485 (stringp tags-file-name)
486 (file-exists-p tags-file-name))))
487 (error "The sdb support requires a valid tags table to work."))
488
489 (gud-common-init command-line 'gud-sdb-massage-args
490 'gud-sdb-marker-filter 'gud-sdb-find-file)
491
492 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
493 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
494 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
495 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
496 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
497 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
498 (gud-def gud-cont "c" "\C-r" "Continue with display.")
499 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
500
501 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
502 (setq paragraph-start comint-prompt-regexp)
503 (local-set-key [menu-bar debug tbreak]
504 '("Temporary Breakpoint" . gud-tbreak))
505 (run-hooks 'sdb-mode-hook)
506 )
507 \f
508 ;; ======================================================================
509 ;; dbx functions
510
511 ;;; History of argument lists passed to dbx.
512 (defvar gud-dbx-history nil)
513
514 (defun gud-dbx-massage-args (file args) args)
515
516 (defun gud-dbx-marker-filter (string)
517 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
518
519 (let (start)
520 ;; Process all complete markers in this chunk.
521 (while (or (string-match
522 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
523 gud-marker-acc start)
524 (string-match
525 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
526 gud-marker-acc start))
527 (setq gud-last-frame
528 (cons
529 (substring gud-marker-acc (match-beginning 2) (match-end 2))
530 (string-to-int
531 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
532 start (match-end 0)))
533
534 ;; Search for the last incomplete line in this chunk
535 (while (string-match "\n" gud-marker-acc start)
536 (setq start (match-end 0)))
537
538 ;; If the incomplete line APPEARS to begin with another marker, keep it
539 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
540 ;; unnecessary concat during the next call.
541 (setq gud-marker-acc
542 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
543 (substring gud-marker-acc (match-beginning 0))
544 nil)))
545 string)
546
547 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
548 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
549 (defvar gud-mips-p
550 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
551 ;; We haven't tested gud on this system:
552 (string-match "^mips-[^-]*-riscos" system-configuration)
553 ;; It's documented on OSF/1.3
554 (string-match "^mips-[^-]*-osf1" system-configuration)
555 (string-match "^alpha-[^-]*-osf" system-configuration))
556 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
557
558 (defun gud-mipsdbx-massage-args (file args)
559 (cons "-emacs" args))
560
561 ;; This is just like the gdb one except for the regexps since we need to cope
562 ;; with an optional breakpoint number in [] before the ^Z^Z
563 (defun gud-mipsdbx-marker-filter (string)
564 (setq gud-marker-acc (concat gud-marker-acc string))
565 (let ((output ""))
566
567 ;; Process all the complete markers in this chunk.
568 (while (string-match
569 ;; This is like th gdb marker but with an optional
570 ;; leading break point number like `[1] '
571 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
572 gud-marker-acc)
573 (setq
574
575 ;; Extract the frame position from the marker.
576 gud-last-frame
577 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
578 (string-to-int (substring gud-marker-acc
579 (match-beginning 2)
580 (match-end 2))))
581
582 ;; Append any text before the marker to the output we're going
583 ;; to return - we don't include the marker in this text.
584 output (concat output
585 (substring gud-marker-acc 0 (match-beginning 0)))
586
587 ;; Set the accumulator to the remaining text.
588 gud-marker-acc (substring gud-marker-acc (match-end 0))))
589
590 ;; Does the remaining text look like it might end with the
591 ;; beginning of another marker? If it does, then keep it in
592 ;; gud-marker-acc until we receive the rest of it. Since we
593 ;; know the full marker regexp above failed, it's pretty simple to
594 ;; test for marker starts.
595 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
596 (progn
597 ;; Everything before the potential marker start can be output.
598 (setq output (concat output (substring gud-marker-acc
599 0 (match-beginning 0))))
600
601 ;; Everything after, we save, to combine with later input.
602 (setq gud-marker-acc
603 (substring gud-marker-acc (match-beginning 0))))
604
605 (setq output (concat output gud-marker-acc)
606 gud-marker-acc ""))
607
608 output))
609
610 ;; The dbx in IRIX is a pain. It doesn't print the file name when
611 ;; stopping at a breakpoint (but you do get it from the `up' and
612 ;; `down' commands...). The only way to extract the information seems
613 ;; to be with a `file' command, although the current line number is
614 ;; available in $curline. Thus we have to look for output which
615 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
616 ;; to output the information we want with a combination of the
617 ;; `printf' and `file' commands as a pseudo marker which we can
618 ;; recognise next time through the marker-filter. This would be like
619 ;; the gdb marker but you can't get the file name without a newline...
620 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
621 ;; number rather than a line number etc. Maybe this could be made to
622 ;; work by listing all the breakpoints and picking the one(s) with the
623 ;; correct line number, but life's too short.
624 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
625
626 (defvar gud-irix-p (string-match "^mips-[^-]*-irix" system-configuration)
627 "Non-nil to assume the interface appropriate for IRIX dbx.
628 This works in IRIX 4, 5 and 6.")
629 ;; [Irix dbx seems to be a moving target. The dbx output changed
630 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
631 ;; the output from `up' is no longer spotted by gud (and it's probably
632 ;; not distinctive enough to try to match it -- use C-<, C->
633 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
634 ;; `long long'(why?!), so the printf stuff needed changing. The line
635 ;; number is cast to `long' as a compromise between the new `long
636 ;; long' and the original `int'. The process filter is also somewhat
637 ;; unreliable, sometimes not spotting the markers; I don't know
638 ;; whether there's anything that can be done about that. It would be
639 ;; much better if SGI could be persuaded to (re?)instate the MIPS
640 ;; -emacs flag for gdb-like output (which ought to be possible as most
641 ;; of the communication I've had over it has been from sgi.com).]
642
643 ;; this filter is influenced by the xdb one rather than the gdb one
644 (defun gud-irixdbx-marker-filter (string)
645 (let (result (case-fold-search nil))
646 (if (or (string-match comint-prompt-regexp string)
647 (string-match ".*\012" string))
648 (setq result (concat gud-marker-acc string)
649 gud-marker-acc "")
650 (setq gud-marker-acc (concat gud-marker-acc string)))
651 (if result
652 (cond
653 ;; look for breakpoint or signal indication e.g.:
654 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
655 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
656 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
657 ((string-match
658 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
659 result)
660 ;; prod dbx into printing out the line number and file
661 ;; name in a form we can grok as below
662 (process-send-string (get-buffer-process gud-comint-buffer)
663 "printf \"\032\032%1d:\",(long)$curline;file\n"))
664 ;; look for result of, say, "up" e.g.:
665 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
666 ;; (this will also catch one of the lines printed by "where")
667 ((string-match
668 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
669 result)
670 (let ((file (substring result (match-beginning 1)
671 (match-end 1))))
672 (if (file-exists-p file)
673 (setq gud-last-frame
674 (cons
675 (substring
676 result (match-beginning 1) (match-end 1))
677 (string-to-int
678 (substring
679 result (match-beginning 2) (match-end 2)))))))
680 result)
681 ((string-match ; kluged-up marker as above
682 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
683 (let ((file (substring result (match-beginning 2) (match-end 2))))
684 (if (file-exists-p file)
685 (setq gud-last-frame
686 (cons
687 file
688 (string-to-int
689 (substring
690 result (match-beginning 1) (match-end 1)))))))
691 (setq result (substring result 0 (match-beginning 0))))))
692 (or result "")))
693
694 (defun gud-dbx-find-file (f)
695 (save-excursion
696 (let ((buf (find-file-noselect f)))
697 (set-buffer buf)
698 (gud-make-debug-menu)
699 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
700 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
701 buf)))
702
703 ;;;###autoload
704 (defun dbx (command-line)
705 "Run dbx on program FILE in buffer *gud-FILE*.
706 The directory containing FILE becomes the initial working directory
707 and source-file directory for your debugger."
708 (interactive
709 (list (read-from-minibuffer "Run dbx (like this): "
710 (if (consp gud-dbx-history)
711 (car gud-dbx-history)
712 "dbx ")
713 nil nil
714 '(gud-dbx-history . 1))))
715
716 (cond
717 (gud-mips-p
718 (gud-common-init command-line 'gud-mipsdbx-massage-args
719 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
720 (gud-irix-p
721 (gud-common-init command-line 'gud-dbx-massage-args
722 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
723 (t
724 (gud-common-init command-line 'gud-dbx-massage-args
725 'gud-dbx-marker-filter 'gud-dbx-find-file)))
726
727 (cond
728 (gud-mips-p
729 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
730 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
731 (gud-def gud-break "stop at \"%f\":%l"
732 "\C-b" "Set breakpoint at current line.")
733 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
734 (gud-irix-p
735 (gud-def gud-break "stop at \"%d%f\":%l"
736 "\C-b" "Set breakpoint at current line.")
737 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
738 (gud-def gud-up "up %p; printf \"\032\032%1ld:\",(long)$curline;file\n"
739 "<" "Up (numeric arg) stack frames.")
740 (gud-def gud-down "down %p; printf \"\032\032%1ld:\",(long)$curline;file\n"
741 ">" "Down (numeric arg) stack frames.")
742 ;; Make dbx give out the source location info that we need.
743 (process-send-string (get-buffer-process gud-comint-buffer)
744 "printf \"\032\032%1d:\",(long)$curline;file\n"))
745 (t
746 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
747 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
748 (gud-def gud-break "file \"%d%f\"\nstop at %l"
749 "\C-b" "Set breakpoint at current line.")))
750
751 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
752 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
753 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
754 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
755 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
756 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
757
758 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
759 (setq paragraph-start comint-prompt-regexp)
760 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
761 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
762 (run-hooks 'dbx-mode-hook)
763 )
764 \f
765 ;; ======================================================================
766 ;; xdb (HP PARISC debugger) functions
767
768 ;;; History of argument lists passed to xdb.
769 (defvar gud-xdb-history nil)
770
771 (defvar gud-xdb-directories nil
772 "*A list of directories that xdb should search for source code.
773 If nil, only source files in the program directory
774 will be known to xdb.
775
776 The file names should be absolute, or relative to the directory
777 containing the executable being debugged.")
778
779 (defun gud-xdb-massage-args (file args)
780 (nconc (let ((directories gud-xdb-directories)
781 (result nil))
782 (while directories
783 (setq result (cons (car directories) (cons "-d" result)))
784 (setq directories (cdr directories)))
785 (nreverse result))
786 args))
787
788 (defun gud-xdb-file-name (f)
789 "Transform a relative pathname to a full pathname in xdb mode"
790 (let ((result nil))
791 (if (file-exists-p f)
792 (setq result (expand-file-name f))
793 (let ((directories gud-xdb-directories))
794 (while directories
795 (let ((path (concat (car directories) "/" f)))
796 (if (file-exists-p path)
797 (setq result (expand-file-name path)
798 directories nil)))
799 (setq directories (cdr directories)))))
800 result))
801
802 ;; xdb does not print the lines all at once, so we have to accumulate them
803 (defun gud-xdb-marker-filter (string)
804 (let (result)
805 (if (or (string-match comint-prompt-regexp string)
806 (string-match ".*\012" string))
807 (setq result (concat gud-marker-acc string)
808 gud-marker-acc "")
809 (setq gud-marker-acc (concat gud-marker-acc string)))
810 (if result
811 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
812 result)
813 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
814 result))
815 (let ((line (string-to-int
816 (substring result (match-beginning 2) (match-end 2))))
817 (file (gud-xdb-file-name
818 (substring result (match-beginning 1) (match-end 1)))))
819 (if file
820 (setq gud-last-frame (cons file line))))))
821 (or result "")))
822
823 (defun gud-xdb-find-file (f)
824 (save-excursion
825 (let ((realf (gud-xdb-file-name f)))
826 (if realf
827 (let ((buf (find-file-noselect realf)))
828 (set-buffer buf)
829 (gud-make-debug-menu)
830 (local-set-key [menu-bar debug tbreak]
831 '("Temporary Breakpoint" . gud-tbreak))
832 (local-set-key [menu-bar debug finish]
833 '("Finish Function" . gud-finish))
834 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
835 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
836 buf)
837 nil))))
838
839 ;;;###autoload
840 (defun xdb (command-line)
841 "Run xdb on program FILE in buffer *gud-FILE*.
842 The directory containing FILE becomes the initial working directory
843 and source-file directory for your debugger.
844
845 You can set the variable 'gud-xdb-directories' to a list of program source
846 directories if your program contains sources from more than one directory."
847 (interactive
848 (list (read-from-minibuffer "Run xdb (like this): "
849 (if (consp gud-xdb-history)
850 (car gud-xdb-history)
851 "xdb ")
852 nil nil
853 '(gud-xdb-history . 1))))
854
855 (gud-common-init command-line 'gud-xdb-massage-args
856 'gud-xdb-marker-filter 'gud-xdb-find-file)
857
858 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
859 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
860 "Set temporary breakpoint at current line.")
861 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
862 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
863 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
864 (gud-def gud-cont "c" "\C-r" "Continue with display.")
865 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
866 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
867 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
868 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
869
870 (setq comint-prompt-regexp "^>")
871 (setq paragraph-start comint-prompt-regexp)
872 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
873 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
874 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
875 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
876 (run-hooks 'xdb-mode-hook))
877 \f
878 ;; ======================================================================
879 ;; perldb functions
880
881 ;;; History of argument lists passed to perldb.
882 (defvar gud-perldb-history nil)
883
884 (defun gud-perldb-massage-args (file args)
885 (cons "-d" (cons (car args) (cons "-emacs" (cdr args)))))
886
887 ;; There's no guarantee that Emacs will hand the filter the entire
888 ;; marker at once; it could be broken up across several strings. We
889 ;; might even receive a big chunk with several markers in it. If we
890 ;; receive a chunk of text which looks like it might contain the
891 ;; beginning of a marker, we save it here between calls to the
892 ;; filter.
893 (defvar gud-perldb-marker-acc "")
894
895 (defun gud-perldb-marker-filter (string)
896 (setq gud-marker-acc (concat gud-marker-acc string))
897 (let ((output ""))
898
899 ;; Process all the complete markers in this chunk.
900 (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
901 gud-marker-acc)
902 (setq
903
904 ;; Extract the frame position from the marker.
905 gud-last-frame
906 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
907 (string-to-int (substring gud-marker-acc
908 (match-beginning 2)
909 (match-end 2))))
910
911 ;; Append any text before the marker to the output we're going
912 ;; to return - we don't include the marker in this text.
913 output (concat output
914 (substring gud-marker-acc 0 (match-beginning 0)))
915
916 ;; Set the accumulator to the remaining text.
917 gud-marker-acc (substring gud-marker-acc (match-end 0))))
918
919 ;; Does the remaining text look like it might end with the
920 ;; beginning of another marker? If it does, then keep it in
921 ;; gud-marker-acc until we receive the rest of it. Since we
922 ;; know the full marker regexp above failed, it's pretty simple to
923 ;; test for marker starts.
924 (if (string-match "\032.*\\'" gud-marker-acc)
925 (progn
926 ;; Everything before the potential marker start can be output.
927 (setq output (concat output (substring gud-marker-acc
928 0 (match-beginning 0))))
929
930 ;; Everything after, we save, to combine with later input.
931 (setq gud-marker-acc
932 (substring gud-marker-acc (match-beginning 0))))
933
934 (setq output (concat output gud-marker-acc)
935 gud-marker-acc ""))
936
937 output))
938
939 (defun gud-perldb-find-file (f)
940 (save-excursion
941 (let ((buf (find-file-noselect f)))
942 (set-buffer buf)
943 (gud-make-debug-menu)
944 buf)))
945
946 ;;;###autoload
947 (defun perldb (command-line)
948 "Run perldb on program FILE in buffer *gud-FILE*.
949 The directory containing FILE becomes the initial working directory
950 and source-file directory for your debugger."
951 (interactive
952 (list (read-from-minibuffer "Run perldb (like this): "
953 (if (consp gud-perldb-history)
954 (car gud-perldb-history)
955 "perl ")
956 nil nil
957 '(gud-perldb-history . 1))))
958
959 (gud-common-init command-line 'gud-perldb-massage-args
960 'gud-perldb-marker-filter 'gud-perldb-find-file)
961
962 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
963 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
964 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
965 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
966 (gud-def gud-cont "c" "\C-r" "Continue with display.")
967 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
968 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
969 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
970 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
971
972 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
973 (setq paragraph-start comint-prompt-regexp)
974 (run-hooks 'perldb-mode-hook)
975 )
976
977 ;;
978 ;; End of debugger-specific information
979 ;;
980
981 \f
982 ;;; When we send a command to the debugger via gud-call, it's annoying
983 ;;; to see the command and the new prompt inserted into the debugger's
984 ;;; buffer; we have other ways of knowing the command has completed.
985 ;;;
986 ;;; If the buffer looks like this:
987 ;;; --------------------
988 ;;; (gdb) set args foo bar
989 ;;; (gdb) -!-
990 ;;; --------------------
991 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
992 ;;; source file to set a breakpoint, we want the buffer to end up like
993 ;;; this:
994 ;;; --------------------
995 ;;; (gdb) set args foo bar
996 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
997 ;;; (gdb) -!-
998 ;;; --------------------
999 ;;; Essentially, the old prompt is deleted, and the command's output
1000 ;;; and the new prompt take its place.
1001 ;;;
1002 ;;; Not echoing the command is easy enough; you send it directly using
1003 ;;; process-send-string, and it never enters the buffer. However,
1004 ;;; getting rid of the old prompt is trickier; you don't want to do it
1005 ;;; when you send the command, since that will result in an annoying
1006 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1007 ;;; waits for a response from the debugger, and the new prompt is
1008 ;;; inserted. Instead, we'll wait until we actually get some output
1009 ;;; from the subprocess before we delete the prompt. If the command
1010 ;;; produced no output other than a new prompt, that prompt will most
1011 ;;; likely be in the first chunk of output received, so we will delete
1012 ;;; the prompt and then replace it with an identical one. If the
1013 ;;; command produces output, the prompt is moving anyway, so the
1014 ;;; flicker won't be annoying.
1015 ;;;
1016 ;;; So - when we want to delete the prompt upon receipt of the next
1017 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1018 ;;; the start of the prompt; the process filter will notice this, and
1019 ;;; delete all text between it and the process output marker. If
1020 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1021 ;;; prompt alone.
1022 (defvar gud-delete-prompt-marker nil)
1023
1024 \f
1025 (defun gud-mode ()
1026 "Major mode for interacting with an inferior debugger process.
1027
1028 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1029 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
1030 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
1031
1032 After startup, the following commands are available in both the GUD
1033 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1034 or step operation:
1035
1036 \\[gud-break] sets a breakpoint at the current file and line. In the
1037 GUD buffer, the current file and line are those of the last breakpoint or
1038 step. In a source buffer, they are the buffer's file and current line.
1039
1040 \\[gud-remove] removes breakpoints on the current file and line.
1041
1042 \\[gud-refresh] displays in the source window the last line referred to
1043 in the gud buffer.
1044
1045 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1046 step-one-line (not entering function calls), and step-one-instruction
1047 and then update the source window with the current file and position.
1048 \\[gud-cont] continues execution.
1049
1050 \\[gud-print] tries to find the largest C lvalue or function-call expression
1051 around point, and sends it to the debugger for value display.
1052
1053 The above commands are common to all supported debuggers except xdb which
1054 does not support stepping instructions.
1055
1056 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1057 except that the breakpoint is temporary; that is, it is removed when
1058 execution stops on it.
1059
1060 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1061 frame. \\[gud-down] drops back down through one.
1062
1063 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1064 the current function and stops.
1065
1066 All the keystrokes above are accessible in the GUD buffer
1067 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1068
1069 All pre-defined functions for which the concept make sense repeat
1070 themselves the appropriate number of times if you give a prefix
1071 argument.
1072
1073 You may use the `gud-def' macro in the initialization hook to define other
1074 commands.
1075
1076 Other commands for interacting with the debugger process are inherited from
1077 comint mode, which see."
1078 (interactive)
1079 (comint-mode)
1080 (setq major-mode 'gud-mode)
1081 (setq mode-name "Debugger")
1082 (setq mode-line-process '(":%s"))
1083 (use-local-map comint-mode-map)
1084 (gud-make-debug-menu)
1085 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
1086 (make-local-variable 'gud-last-frame)
1087 (setq gud-last-frame nil)
1088 (make-local-variable 'comint-prompt-regexp)
1089 (make-local-variable 'paragraph-start)
1090 (make-local-variable 'gud-delete-prompt-marker)
1091 (setq gud-delete-prompt-marker (make-marker))
1092 (run-hooks 'gud-mode-hook))
1093
1094 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1095 (defun gud-chop-words (string)
1096 (let ((i 0) (beg 0)
1097 (len (length string))
1098 (words nil))
1099 (while (< i len)
1100 (if (memq (aref string i) '(?\t ? ))
1101 (progn
1102 (setq words (cons (substring string beg i) words)
1103 beg (1+ i))
1104 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1105 (setq beg (1+ beg)))
1106 (setq i (1+ beg)))
1107 (setq i (1+ i))))
1108 (if (< beg len)
1109 (setq words (cons (substring string beg) words)))
1110 (nreverse words)))
1111
1112 ;; Perform initializations common to all debuggers.
1113 ;; The first arg is the specified command line,
1114 ;; which starts with the program to debug.
1115 ;; The other three args specify the values to use
1116 ;; for local variables in the debugger buffer.
1117 (defun gud-common-init (command-line massage-args marker-filter find-file)
1118 (let* ((words (gud-chop-words command-line))
1119 (program (car words))
1120 ;; Extract the file name from WORDS
1121 ;; and put t in its place.
1122 ;; Later on we will put the modified file name arg back there.
1123 (file-word (let ((w (cdr words)))
1124 (while (and w (= ?- (aref (car w) 0)))
1125 (setq w (cdr w)))
1126 (and w
1127 (prog1 (car w)
1128 (setcar w t)))))
1129 (file-subst
1130 (and file-word (substitute-in-file-name file-word)))
1131 (args (cdr words))
1132 ;; If a directory was specified, expand the file name.
1133 ;; Otherwise, don't expand it, so GDB can use the PATH.
1134 ;; A file name without directory is literally valid
1135 ;; only if the file exists in ., and in that case,
1136 ;; omitting the expansion here has no visible effect.
1137 (file (and file-word
1138 (if (file-name-directory file-subst)
1139 (expand-file-name file-subst)
1140 file-subst)))
1141 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
1142 (switch-to-buffer (concat "*gud" filepart "*"))
1143 ;; Set default-directory to the file's directory.
1144 (and file-word
1145 ;; Don't set default-directory if no directory was specified.
1146 ;; In that case, either the file is found in the current directory,
1147 ;; in which case this setq is a no-op,
1148 ;; or it is found by searching PATH,
1149 ;; in which case we don't know what directory it was found in.
1150 (file-name-directory file)
1151 (setq default-directory (file-name-directory file)))
1152 (or (bolp) (newline))
1153 (insert "Current directory is " default-directory "\n")
1154 ;; Put the substituted and expanded file name back in its place.
1155 (let ((w args))
1156 (while (and w (not (eq (car w) t)))
1157 (setq w (cdr w)))
1158 (if w
1159 (setcar w file)))
1160 (apply 'make-comint (concat "gud" filepart) program nil
1161 (funcall massage-args file args)))
1162 ;; Since comint clobbered the mode, we don't set it until now.
1163 (gud-mode)
1164 (make-local-variable 'gud-marker-filter)
1165 (setq gud-marker-filter marker-filter)
1166 (make-local-variable 'gud-find-file)
1167 (setq gud-find-file find-file)
1168
1169 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1170 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
1171 (gud-set-buffer)
1172 )
1173
1174 (defun gud-set-buffer ()
1175 (cond ((eq major-mode 'gud-mode)
1176 (setq gud-comint-buffer (current-buffer)))))
1177
1178 (defvar gud-filter-defer-flag nil
1179 "Non-nil means don't process anything from the debugger right now.
1180 It is saved for when this flag is not set.")
1181
1182 (defvar gud-filter-pending-text nil
1183 "Non-nil means this is text that has been saved for later in `gud-filter'.")
1184
1185 ;; These functions are responsible for inserting output from your debugger
1186 ;; into the buffer. The hard work is done by the method that is
1187 ;; the value of gud-marker-filter.
1188
1189 (defun gud-filter (proc string)
1190 ;; Here's where the actual buffer insertion is done
1191 (let (output)
1192 (if (buffer-name (process-buffer proc))
1193 (if gud-filter-defer-flag
1194 ;; If we can't process any text now,
1195 ;; save it for later.
1196 (setq gud-filter-pending-text
1197 (concat (or gud-filter-pending-text "") string))
1198 (save-excursion
1199 ;; If we have to ask a question during the processing,
1200 ;; defer any additional text that comes from the debugger
1201 ;; during that time.
1202 (let ((gud-filter-defer-flag t))
1203 ;; Process now any text we previously saved up.
1204 (if gud-filter-pending-text
1205 (setq string (concat gud-filter-pending-text string)
1206 gud-filter-pending-text nil))
1207 (set-buffer (process-buffer proc))
1208 ;; If we have been so requested, delete the debugger prompt.
1209 (if (marker-buffer gud-delete-prompt-marker)
1210 (progn
1211 (delete-region (process-mark proc) gud-delete-prompt-marker)
1212 (set-marker gud-delete-prompt-marker nil)))
1213 ;; Save the process output, checking for source file markers.
1214 (setq output (gud-marker-filter string))
1215 ;; Check for a filename-and-line number.
1216 ;; Don't display the specified file
1217 ;; unless (1) point is at or after the position where output appears
1218 ;; and (2) this buffer is on the screen.
1219 (if (and gud-last-frame
1220 (>= (point) (process-mark proc))
1221 (get-buffer-window (current-buffer)))
1222 (gud-display-frame))
1223 ;; Let the comint filter do the actual insertion.
1224 ;; That lets us inherit various comint features.
1225 (comint-output-filter proc output))
1226 ;; If we deferred text that arrived during this processing,
1227 ;; handle it now.
1228 (if gud-filter-pending-text
1229 (gud-filter proc "")))))))
1230
1231 (defun gud-sentinel (proc msg)
1232 (cond ((null (buffer-name (process-buffer proc)))
1233 ;; buffer killed
1234 ;; Stop displaying an arrow in a source file.
1235 (setq overlay-arrow-position nil)
1236 (set-process-buffer proc nil))
1237 ((memq (process-status proc) '(signal exit))
1238 ;; Stop displaying an arrow in a source file.
1239 (setq overlay-arrow-position nil)
1240 ;; Fix the mode line.
1241 (setq mode-line-process
1242 (concat ":"
1243 (symbol-name (process-status proc))))
1244 (let* ((obuf (current-buffer)))
1245 ;; save-excursion isn't the right thing if
1246 ;; process-buffer is current-buffer
1247 (unwind-protect
1248 (progn
1249 ;; Write something in *compilation* and hack its mode line,
1250 (set-buffer (process-buffer proc))
1251 (force-mode-line-update)
1252 (if (eobp)
1253 (insert ?\n mode-name " " msg)
1254 (save-excursion
1255 (goto-char (point-max))
1256 (insert ?\n mode-name " " msg)))
1257 ;; If buffer and mode line will show that the process
1258 ;; is dead, we can delete it now. Otherwise it
1259 ;; will stay around until M-x list-processes.
1260 (delete-process proc))
1261 ;; Restore old buffer, but don't restore old point
1262 ;; if obuf is the gud buffer.
1263 (set-buffer obuf))))))
1264
1265 (defun gud-display-frame ()
1266 "Find and obey the last filename-and-line marker from the debugger.
1267 Obeying it means displaying in another window the specified file and line."
1268 (interactive)
1269 (if gud-last-frame
1270 (progn
1271 (gud-set-buffer)
1272 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
1273 (setq gud-last-last-frame gud-last-frame
1274 gud-last-frame nil))))
1275
1276 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1277 ;; and that its line LINE is visible.
1278 ;; Put the overlay-arrow on the line LINE in that buffer.
1279 ;; Most of the trickiness in here comes from wanting to preserve the current
1280 ;; region-restriction if that's possible. We use an explicit display-buffer
1281 ;; to get around the fact that this is called inside a save-excursion.
1282
1283 (defun gud-display-line (true-file line)
1284 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
1285 (buffer (gud-find-file true-file))
1286 (window (display-buffer buffer))
1287 (pos))
1288 ;;; (if (equal buffer (current-buffer))
1289 ;;; nil
1290 ;;; (setq buffer-read-only nil))
1291 (save-excursion
1292 ;;; (setq buffer-read-only t)
1293 (set-buffer buffer)
1294 (save-restriction
1295 (widen)
1296 (goto-line line)
1297 (setq pos (point))
1298 (setq overlay-arrow-string "=>")
1299 (or overlay-arrow-position
1300 (setq overlay-arrow-position (make-marker)))
1301 (set-marker overlay-arrow-position (point) (current-buffer)))
1302 (cond ((or (< pos (point-min)) (> pos (point-max)))
1303 (widen)
1304 (goto-char pos))))
1305 (set-window-point window overlay-arrow-position)))
1306
1307 ;;; The gud-call function must do the right thing whether its invoking
1308 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1309 ;;; or a C buffer. In the former case, we want to supply data from
1310 ;;; gud-last-frame. Here's how we do it:
1311
1312 (defun gud-format-command (str arg)
1313 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
1314 (frame (or gud-last-frame gud-last-last-frame))
1315 result)
1316 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
1317 (let ((key (string-to-char (substring str (match-beginning 2))))
1318 subst)
1319 (cond
1320 ((eq key ?f)
1321 (setq subst (file-name-nondirectory (if insource
1322 (buffer-file-name)
1323 (car frame)))))
1324 ((eq key ?d)
1325 (setq subst (file-name-directory (if insource
1326 (buffer-file-name)
1327 (car frame)))))
1328 ((eq key ?l)
1329 (setq subst (if insource
1330 (save-excursion
1331 (beginning-of-line)
1332 (save-restriction (widen)
1333 (1+ (count-lines 1 (point)))))
1334 (cdr frame))))
1335 ((eq key ?e)
1336 (setq subst (find-c-expr)))
1337 ((eq key ?a)
1338 (setq subst (gud-read-address)))
1339 ((eq key ?p)
1340 (setq subst (if arg (int-to-string arg) ""))))
1341 (setq result (concat result
1342 (substring str (match-beginning 1) (match-end 1))
1343 subst)))
1344 (setq str (substring str (match-end 2))))
1345 ;; There might be text left in STR when the loop ends.
1346 (concat result str)))
1347
1348 (defun gud-read-address ()
1349 "Return a string containing the core-address found in the buffer at point."
1350 (save-excursion
1351 (let ((pt (point)) found begin)
1352 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1353 (cond
1354 (found (forward-char 2)
1355 (buffer-substring found
1356 (progn (re-search-forward "[^0-9a-f]")
1357 (forward-char -1)
1358 (point))))
1359 (t (setq begin (progn (re-search-backward "[^0-9]")
1360 (forward-char 1)
1361 (point)))
1362 (forward-char 1)
1363 (re-search-forward "[^0-9]")
1364 (forward-char -1)
1365 (buffer-substring begin (point)))))))
1366
1367 (defun gud-call (fmt &optional arg)
1368 (let ((msg (gud-format-command fmt arg)))
1369 (message "Command: %s" msg)
1370 (sit-for 0)
1371 (gud-basic-call msg)))
1372
1373 (defun gud-basic-call (command)
1374 "Invoke the debugger COMMAND displaying source in other window."
1375 (interactive)
1376 (gud-set-buffer)
1377 (let ((command (concat command "\n"))
1378 (proc (get-buffer-process gud-comint-buffer)))
1379 (or proc (error "Current buffer has no process"))
1380 ;; Arrange for the current prompt to get deleted.
1381 (save-excursion
1382 (set-buffer gud-comint-buffer)
1383 (goto-char (process-mark proc))
1384 (beginning-of-line)
1385 (if (looking-at comint-prompt-regexp)
1386 (set-marker gud-delete-prompt-marker (point))))
1387 (process-send-string proc command)))
1388
1389 (defun gud-refresh (&optional arg)
1390 "Fix up a possibly garbled display, and redraw the arrow."
1391 (interactive "P")
1392 (recenter arg)
1393 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1394 (gud-display-frame))
1395 \f
1396 ;;; Code for parsing expressions out of C code. The single entry point is
1397 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1398 ;;;
1399 ;;; The rest of this file is a hacked version of gdbsrc.el by
1400 ;;; Debby Ayers <ayers@asc.slb.com>,
1401 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1402
1403 (defun find-c-expr ()
1404 "Returns the C expr that surrounds point."
1405 (interactive)
1406 (save-excursion
1407 (let ((p) (expr) (test-expr))
1408 (setq p (point))
1409 (setq expr (expr-cur))
1410 (setq test-expr (expr-prev))
1411 (while (expr-compound test-expr expr)
1412 (setq expr (cons (car test-expr) (cdr expr)))
1413 (goto-char (car expr))
1414 (setq test-expr (expr-prev)))
1415 (goto-char p)
1416 (setq test-expr (expr-next))
1417 (while (expr-compound expr test-expr)
1418 (setq expr (cons (car expr) (cdr test-expr)))
1419 (setq test-expr (expr-next))
1420 )
1421 (buffer-substring (car expr) (cdr expr)))))
1422
1423 (defun expr-cur ()
1424 "Returns the expr that point is in; point is set to beginning of expr.
1425 The expr is represented as a cons cell, where the car specifies the point in
1426 the current buffer that marks the beginning of the expr and the cdr specifies
1427 the character after the end of the expr."
1428 (let ((p (point)) (begin) (end))
1429 (expr-backward-sexp)
1430 (setq begin (point))
1431 (expr-forward-sexp)
1432 (setq end (point))
1433 (if (>= p end)
1434 (progn
1435 (setq begin p)
1436 (goto-char p)
1437 (expr-forward-sexp)
1438 (setq end (point))
1439 )
1440 )
1441 (goto-char begin)
1442 (cons begin end)))
1443
1444 (defun expr-backward-sexp ()
1445 "Version of `backward-sexp' that catches errors."
1446 (condition-case nil
1447 (backward-sexp)
1448 (error t)))
1449
1450 (defun expr-forward-sexp ()
1451 "Version of `forward-sexp' that catches errors."
1452 (condition-case nil
1453 (forward-sexp)
1454 (error t)))
1455
1456 (defun expr-prev ()
1457 "Returns the previous expr, point is set to beginning of that expr.
1458 The expr is represented as a cons cell, where the car specifies the point in
1459 the current buffer that marks the beginning of the expr and the cdr specifies
1460 the character after the end of the expr"
1461 (let ((begin) (end))
1462 (expr-backward-sexp)
1463 (setq begin (point))
1464 (expr-forward-sexp)
1465 (setq end (point))
1466 (goto-char begin)
1467 (cons begin end)))
1468
1469 (defun expr-next ()
1470 "Returns the following expr, point is set to beginning of that expr.
1471 The expr is represented as a cons cell, where the car specifies the point in
1472 the current buffer that marks the beginning of the expr and the cdr specifies
1473 the character after the end of the expr."
1474 (let ((begin) (end))
1475 (expr-forward-sexp)
1476 (expr-forward-sexp)
1477 (setq end (point))
1478 (expr-backward-sexp)
1479 (setq begin (point))
1480 (cons begin end)))
1481
1482 (defun expr-compound-sep (span-start span-end)
1483 "Returns '.' for '->' & '.', returns ' ' for white space,
1484 returns '?' for other punctuation."
1485 (let ((result ? )
1486 (syntax))
1487 (while (< span-start span-end)
1488 (setq syntax (char-syntax (char-after span-start)))
1489 (cond
1490 ((= syntax ? ) t)
1491 ((= syntax ?.) (setq syntax (char-after span-start))
1492 (cond
1493 ((= syntax ?.) (setq result ?.))
1494 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1495 (setq result ?.)
1496 (setq span-start (+ span-start 1)))
1497 (t (setq span-start span-end)
1498 (setq result ??)))))
1499 (setq span-start (+ span-start 1)))
1500 result))
1501
1502 (defun expr-compound (first second)
1503 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1504 The two exprs are represented as a cons cells, where the car
1505 specifies the point in the current buffer that marks the beginning of the
1506 expr and the cdr specifies the character after the end of the expr.
1507 Link exprs of the form:
1508 Expr -> Expr
1509 Expr . Expr
1510 Expr (Expr)
1511 Expr [Expr]
1512 (Expr) Expr
1513 [Expr] Expr"
1514 (let ((span-start (cdr first))
1515 (span-end (car second))
1516 (syntax))
1517 (setq syntax (expr-compound-sep span-start span-end))
1518 (cond
1519 ((= (car first) (car second)) nil)
1520 ((= (cdr first) (cdr second)) nil)
1521 ((= syntax ?.) t)
1522 ((= syntax ? )
1523 (setq span-start (char-after (- span-start 1)))
1524 (setq span-end (char-after span-end))
1525 (cond
1526 ((= span-start ?) ) t )
1527 ((= span-start ?] ) t )
1528 ((= span-end ?( ) t )
1529 ((= span-end ?[ ) t )
1530 (t nil))
1531 )
1532 (t nil))))
1533
1534 (provide 'gud)
1535
1536 ;;; gud.el ends here