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