All fsets changed to defaliases.
[emacs.git] / lisp / gud.el
blob646f463e349def25574779f17172a3ef4a8feb4e
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, or dbx under Emacs
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4 ;; Version: 1.2
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;;; Commentary:
27 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
28 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
29 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
30 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
31 ;; who also hacked the mode to use comint.el.
33 ;; This code will not work under Emacs 18. It relies on Emacs 19's
34 ;; minor-mode-keymap support and the find-tag-noselect entry point of etags.
36 ;;; Code:
38 (require 'comint)
39 (require 'etags)
41 ;; ======================================================================
42 ;; minor-mode machinery for C buffers visited by GUD
44 (defvar gud-key-prefix "\C-x\C-a"
45 "Prefix of all GUD minor-mode commands valid in C buffers.")
47 (defvar gud-minor-mode nil)
48 (or (assq 'gud-minor-mode minor-mode-alist)
49 (setq minor-mode-alist
50 (cons '(gud-minor-mode " GUD") minor-mode-alist)))
52 (defvar gud-mode-map nil)
53 (if gud-mode-map
54 nil
55 (setq gud-mode-map (make-sparse-keymap))
56 (define-key gud-mode-map gud-key-prefix (make-sparse-keymap))
57 (define-key gud-mode-map (concat gud-key-prefix "\C-l") 'gud-refresh)
60 (or (assq 'gud-minor-mode minor-mode-map-alist)
61 (setq minor-mode-map-alist
62 (cons
63 (cons 'gud-minor-mode gud-mode-map)
64 minor-mode-map-alist)))
66 (defun gud-minor-mode (&optional enable)
67 "GUD minor mode is enabled in C buffers visited due to a GUD stop at
68 breakpoint. All GUD-specific commands defined in GUD major mode will work,
69 but they get their current file and current line number from the context of
70 this buffer."
71 (interactive "P")
72 (setq gud-minor-mode
73 (if (null enable) (not gud-minor-mode)
74 (> (prefix-numeric-value enable) 0)))
77 ;; ======================================================================
78 ;; the overloading mechanism
80 (defun gud-overload-functions (gud-overload-alist)
81 "Overload functions defined in GUD-OVERLOAD-ALIST.
82 This association list has elements of the form
83 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
84 (mapcar
85 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
86 gud-overload-alist))
88 (defun gud-debugger-startup (file args)
89 (error "GUD not properly entered."))
91 (defun gud-marker-filter (str)
92 (error "GUD not properly entered."))
94 (defun gud-find-file (f)
95 (error "GUD not properly entered."))
97 ;; ======================================================================
98 ;; command definition
100 ;; This macro is used below to define some basic debugger interface commands.
101 ;; Of course you may use `gud-def' with any other debugger command, including
102 ;; user defined ones.
104 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
105 ;; which defines FUNC to send the command NAME to the debugger, gives
106 ;; it the docstring DOC, and binds that function to KEY in the GUD
107 ;; major mode. The function is also bound in the GUD minor-mode
108 ;; keymap. If a numeric prefix argument is given to FUNC, it gets
109 ;; sent after NAME.
111 (defmacro gud-def (func cmd key &optional doc)
112 "Define FUNC to be a command sending STR and bound to KEY, with
113 optional doc string DOC. Certain %-escapes in the string arguments
114 are interpreted specially if present. These are:
116 %f name of current source file.
117 %l number of current source line
118 %e text of the C lvalue or function-call expression surrounding point.
119 %a text of the hexadecimal address surrounding point
120 %p prefix argument to the command (if any) as a number
122 The `current' source file is the file of the current buffer (if we're in a
123 C file with gud-minor-mode active) or the source file current at the last
124 break or step (if we're in the GUD buffer).
125 The `current' line is that of the current buffer (if we're in a source
126 file with gud-minor-mode active) or the source line number at the last
127 break or step (if we're in the GUD buffer)."
128 (list 'progn
129 (list 'defun func '(arg)
130 (or doc "")
131 '(interactive "p")
132 (list 'gud-call cmd 'arg))
133 (if key
134 (list 'define-key
135 'gud-mode-map
136 (concat gud-key-prefix key)
137 (list 'quote func)))))
139 ;; Where gud-display-frame should put the debugging arrow. This is
140 ;; set by the marker-filter, which scans the debugger's output for
141 ;; indications of the current program counter.
142 (defvar gud-last-frame nil)
144 ;; All debugger-specific information is collected here.
145 ;; Here's how it works, in case you ever need to add a debugger to the mode.
147 ;; Each entry must define the following at startup:
149 ;;<name>
150 ;; comint-prompt-regexp
151 ;; gud-<name>-debugger-startup
152 ;; gud-<name>-marker-filter
153 ;; gud-<name>-find-file
155 ;; The job of the startup-command method is to fire up a copy of the debugger,
156 ;; given a list of debugger arguments.
158 ;; The job of the marker-filter method is to detect file/line markers in
159 ;; strings and set the global gud-last-frame to indicate what display
160 ;; action (if any) should be triggered by the marker. Note that only
161 ;; whetever the method *returns* is displayed in the buffer; thus, you
162 ;; can filter the debugger's output, interpreting some and passing on
163 ;; the rest.
165 ;; The job of the find-file method is to visit and return the buffer indicated
166 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
167 ;; something else.
169 ;; ======================================================================
170 ;; gdb functions
172 (defun gud-gdb-debugger-startup (file args)
173 (apply 'make-comint (concat "gud-" file) "gdb" nil "-fullname" args))
175 (defun gud-gdb-marker-filter (string)
176 (if (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" string)
177 (progn
178 (setq gud-last-frame
179 (cons
180 (substring string (match-beginning 1) (match-end 1))
181 (string-to-int
182 (substring string (match-beginning 2) (match-end 2)))))
183 ;; this computation means the ^Z^Z-initiated marker in the
184 ;; input string is never emitted.
185 (concat
186 (substring string 0 (match-beginning 0))
187 (substring string (match-end 0))
189 string))
191 (defun gud-gdb-find-file (f)
192 (find-file-noselect f))
194 ;;;###autoload
195 (defun gdb (args)
196 "Run gdb on program FILE in buffer *gud-FILE*.
197 The directory containing FILE becomes the initial working directory
198 and source-file directory for your debugger."
199 (interactive "sRun gdb (like this): gdb ")
200 (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup)
201 (gud-marker-filter . gud-gdb-marker-filter)
202 (gud-find-file . gud-gdb-find-file)
205 (gud-def gud-break "break %f:%l" "b" "Set breakpoint at current line.")
206 (gud-def gud-tbreak "tbreak %f:%l" "t" "Set breakpoint at current line.")
207 (gud-def gud-remove "clear %l" "d" "Remove breakpoint at current line")
208 (gud-def gud-step "step %p" "s" "Step one source line with display.")
209 (gud-def gud-stepi "stepi %p" "i" "Step one instruction with display.")
210 (gud-def gud-next "next %p" "n" "Step one line (skip functions).")
211 (gud-def gud-cont "cont" "r" "Continue with display.")
212 (gud-def gud-finish "finish" "f" "Finish executing current function.")
213 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
214 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
215 (gud-def gud-print "print %e" "p" "Evaluate C expression at point.")
217 (gud-common-init args)
219 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
220 (run-hooks 'gdb-mode-hook)
224 ;; ======================================================================
225 ;; sdb functions
227 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
228 "If nil, we're on a System V Release 4 and don't need the tags hack.")
230 (defvar gud-sdb-lastfile nil)
232 (defun gud-sdb-debugger-startup (file args)
233 (apply 'make-comint (concat "gud-" file) "sdb" nil args))
235 (defun gud-sdb-marker-filter (string)
236 (cond
237 ;; System V Release 3.2 uses this format
238 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
239 string)
240 (setq gud-last-frame
241 (cons
242 (substring string (match-beginning 2) (match-end 2))
243 (string-to-int
244 (substring string (match-beginning 3) (match-end 3))))))
245 ;; System V Release 4.0
246 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
247 string)
248 (setq gud-sdb-lastfile
249 (substring string (match-beginning 2) (match-end 2))))
250 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
251 (setq gud-last-frame
252 (cons
253 gud-sdb-lastfile
254 (string-to-int
255 (substring string (match-beginning 1) (match-end 1))))))
257 (setq gud-sdb-lastfile nil)))
258 string)
260 (defun gud-sdb-find-file (f)
261 (if gud-sdb-needs-tags
262 (find-tag-noselect f)
263 (find-file-noselect f)))
265 ;;;###autoload
266 (defun sdb (args)
267 "Run sdb on program FILE in buffer *gud-FILE*.
268 The directory containing FILE becomes the initial working directory
269 and source-file directory for your debugger."
271 (interactive "sRun sdb (like this): sdb ")
273 (if (and gud-sdb-needs-tags
274 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
275 (error "The sdb support requires a valid tags table to work."))
276 (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup)
277 (gud-marker-filter . gud-sdb-marker-filter)
278 (gud-find-file . gud-sdb-find-file)
281 (gud-def gud-break "%l b" "b" "Set breakpoint at current line.")
282 (gud-def gud-tbreak "%l c" "t" "Set temporary breakpoint at current line.")
283 (gud-def gud-remove "%l d" "d" "Remove breakpoint at current line")
284 (gud-def gud-step "s %p" "s" "Step one source line with display.")
285 (gud-def gud-stepi "i %p" "i" "Step one instruction with display.")
286 (gud-def gud-next "S %p" "n" "Step one line (skip functions).")
287 (gud-def gud-cont "c" "r" "Continue with display.")
288 (gud-def gud-print "%e/" "p" "Evaluate C expression at point.")
290 (gud-common-init args)
292 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
293 (run-hooks 'sdb-mode-hook)
296 ;; ======================================================================
297 ;; dbx functions
299 (defun gud-dbx-debugger-startup (file args)
300 (apply 'make-comint (concat "gud-" file) "dbx" nil args))
302 (defun gud-dbx-marker-filter (string)
303 (if (string-match
304 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" string)
305 (setq gud-last-frame
306 (cons
307 (substring string (match-beginning 2) (match-end 2))
308 (string-to-int
309 (substring string (match-beginning 1) (match-end 1))))))
310 string)
312 (defun gud-dbx-find-file (f)
313 (find-file-noselect f))
315 ;;;###autoload
316 (defun dbx (args)
317 "Run dbx on program FILE in buffer *gud-FILE*.
318 The directory containing FILE becomes the initial working directory
319 and source-file directory for your debugger."
320 (interactive "sRun dbx (like this): dbx")
321 (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup)
322 (gud-marker-filter . gud-dbx-marker-filter)
323 (gud-find-file . gud-dbx-find-file)
326 (gud-def gud-break "stop at \"%f\":%l"
327 "b" "Set breakpoint at current line.")
328 (gud-def gud-remove "clear %l" "d" "Remove breakpoint at current line")
329 (gud-def gud-step "step %p" "s" "Step one line with display.")
330 (gud-def gud-stepi "stepi %p" "i" "Step one instruction with display.")
331 (gud-def gud-next "next %p" "n" "Step one line (skip functions).")
332 (gud-def gud-cont "cont" "r" "Continue with display.")
333 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
334 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
335 (gud-def gud-print "print %e" "p" "Evaluate C expression at point.")
337 (gud-common-init args)
338 (setq comint-prompt-regexp "^[^)]*dbx) *")
340 (run-hooks 'dbx-mode-hook)
344 ;; End of debugger-specific information
347 ;;; When we send a command to the debugger via gud-call, it's annoying
348 ;;; to see the command and the new prompt inserted into the debugger's
349 ;;; buffer; we have other ways of knowing the command has completed.
351 ;;; If the buffer looks like this:
352 ;;; --------------------
353 ;;; (gdb) set args foo bar
354 ;;; (gdb) -!-
355 ;;; --------------------
356 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
357 ;;; source file to set a breakpoint, we want the buffer to end up like
358 ;;; this:
359 ;;; --------------------
360 ;;; (gdb) set args foo bar
361 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
362 ;;; (gdb) -!-
363 ;;; --------------------
364 ;;; Essentially, the old prompt is deleted, and the command's output
365 ;;; and the new prompt take its place.
367 ;;; Not echoing the command is easy enough; you send it directly using
368 ;;; process-send-string, and it never enters the buffer. However,
369 ;;; getting rid of the old prompt is trickier; you don't want to do it
370 ;;; when you send the command, since that will result in an annoying
371 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
372 ;;; waits for a response from the debugger, and the new prompt is
373 ;;; inserted. Instead, we'll wait until we actually get some output
374 ;;; from the subprocess before we delete the prompt. If the command
375 ;;; produced no output other than a new prompt, that prompt will most
376 ;;; likely be in the first chunk of output received, so we will delete
377 ;;; the prompt and then replace it with an identical one. If the
378 ;;; command produces output, the prompt is moving anyway, so the
379 ;;; flicker won't be annoying.
381 ;;; So - when we want to delete the prompt upon receipt of the next
382 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
383 ;;; the start of the prompt; the process filter will notice this, and
384 ;;; delete all text between it and the process output marker. If
385 ;;; gud-delete-prompt-marker points nowhere, we leave the current
386 ;;; prompt alone.
387 (defvar gud-delete-prompt-marker nil)
390 (defun gud-mode ()
391 "Major mode for interacting with an inferior debugger process.
393 You start it up with one of the commands M-x gdb, M-x sdb, or
394 M-x dbx. Each entry point finishes by executing a hook; gdb-mode-hook,
395 sdb-mode-hook or dbx-mode-hook respectively.
397 After startup, the following commands are available in both the GUD
398 interaction buffer and any source buffer GUD visits due to a breakpoint stop
399 or step operation:
401 \\{gud-mode-map}
403 \\[gud-break] sets a breakpoint at the current file and line. In the
404 GUD buffer, the current file and line are those of the last breakpoint or
405 step. In a source buffer, they are the buffer's file and current line.
407 \\[gud-remove] removes breakpoints on the current file and line.
409 \\[gud-refresh] displays in the source window the last line referred to
410 in the gud buffer.
412 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
413 step-one-line (not entering function calls), and step-one-instruction
414 and then update the source window with the current file and position.
415 \\[gud-cont] continues execution.
417 \\[gud-print] tries to find the largest C lvalue or function-call expression
418 around point, and sends it to the debugger for value display.
420 The above commands are common to all supported debuggers.
422 Under gdb and sdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
423 except that the breakpoint is temporary; that is, it is removed when
424 execution stops on it.
426 Under gdb and dbx, \\[gud-up] pops up through an enclosing stack
427 frame. \\[gud-down] drops back down through one.
429 If you are using gdb, \\[gdb-finish] runs execution to the return from
430 the current function and stops.
432 All the keystrokes above have synonyms (in the GUD buffer only) with
433 a prefix of C-c (this is for backward compatibility with old gdb.el).
435 All pre-defined functions for which the concept make sense repeat
436 themselves the appropriate number of times if you give a prefix
437 argument.
439 You may use the gud-def macro in the initialization hook to define other
440 commands.
442 Other commands for interacting with the debugger process are inherited from
443 comint mode, which see."
444 (interactive)
445 (comint-mode)
446 (setq major-mode 'gud-mode)
447 (setq mode-name "Debugger")
448 (setq mode-line-process '(": %s"))
449 (use-local-map (copy-keymap comint-mode-map))
450 (define-key (current-local-map)
451 gud-key-prefix (lookup-key gud-mode-map gud-key-prefix))
452 (define-key (current-local-map)
453 "\C-c" (lookup-key gud-mode-map gud-key-prefix))
454 (make-local-variable 'gud-last-frame)
455 (setq gud-last-frame nil)
456 (make-local-variable 'comint-prompt-regexp)
457 (make-local-variable 'gud-delete-prompt-marker)
458 (setq gud-delete-prompt-marker (make-marker))
459 (run-hooks 'gud-mode-hook)
462 (defvar gud-comint-buffer nil)
464 (defun gud-common-init (args)
465 ;; Perform initializations common to all debuggers
466 ;; There *must* be a cleaner way to lex the arglist...
467 (let (file i)
468 (if (string= args "")
469 (setq args nil)
470 (set-buffer (get-buffer-create "*gud-scratch*"))
471 (erase-buffer)
472 (insert args)
473 (goto-char (point-max))
474 (insert "\")")
475 (goto-char (point-min))
476 (insert "(\"")
477 (while (re-search-forward " +" nil t)
478 (replace-match "\" \"" nil nil))
479 (goto-char (point-min))
480 (while (re-search-forward "\"\"" nil t)
481 (replace-match "" nil nil))
482 (setq args (read (buffer-string)))
483 (kill-buffer (current-buffer)))
484 (setq i (1- (length args)))
485 (while (and (>= i 0) (not (= (aref (nth i args) 0) ?-)))
486 (setq file (nth i args)) (setq i (1- i)))
487 (let* ((path (expand-file-name file))
488 (filepart (file-name-nondirectory path)))
489 (switch-to-buffer (concat "*gud-" filepart "*"))
490 (setq default-directory (file-name-directory path))
491 (or (bolp) (newline))
492 (insert "Current directory is " default-directory "\n")
493 (gud-debugger-startup filepart args)))
494 (gud-mode)
495 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
496 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
497 (gud-set-buffer)
500 (defun gud-set-buffer ()
501 (cond ((eq major-mode 'gud-mode)
502 (setq gud-comint-buffer (current-buffer)))))
504 ;; These functions are responsible for inserting output from your debugger
505 ;; into the buffer. The hard work is done by the method that is
506 ;; the value of gud-marker-filter.
508 (defun gud-filter (proc string)
509 ;; Here's where the actual buffer insertion is done
510 (let ((inhibit-quit t))
511 (save-excursion
512 (set-buffer (process-buffer proc))
513 (let ((moving (= (point) (process-mark proc)))
514 (output-after-point (< (point) (process-mark proc))))
515 (save-excursion
516 (goto-char (process-mark proc))
517 ;; If we have been so requested, delete the debugger prompt.
518 (if (marker-buffer gud-delete-prompt-marker)
519 (progn
520 (delete-region (point) gud-delete-prompt-marker)
521 (set-marker gud-delete-prompt-marker nil)))
522 (insert-before-markers (gud-marker-filter string))
523 ;; Check for a filename-and-line number.
524 ;; Don't display the specified file
525 ;; unless (1) point is at or after the position where output appears
526 ;; and (2) this buffer is on the screen.
527 (if (and gud-last-frame
528 (not output-after-point)
529 (get-buffer-window (current-buffer)))
530 (gud-display-frame)))
531 (if moving (goto-char (process-mark proc)))))))
533 (defun gud-sentinel (proc msg)
534 (cond ((null (buffer-name (process-buffer proc)))
535 ;; buffer killed
536 ;; Stop displaying an arrow in a source file.
537 (setq overlay-arrow-position nil)
538 (setq gud-minor-mode nil)
539 (set-process-buffer proc nil))
540 ((memq (process-status proc) '(signal exit))
541 ;; Stop displaying an arrow in a source file.
542 (setq gud-minor-mode nil)
543 (setq overlay-arrow-position nil)
544 ;; Fix the mode line.
545 (setq mode-line-process
546 (concat ": "
547 (symbol-name (process-status proc))))
548 (let* ((obuf (current-buffer)))
549 ;; save-excursion isn't the right thing if
550 ;; process-buffer is current-buffer
551 (unwind-protect
552 (progn
553 ;; Write something in *compilation* and hack its mode line,
554 (set-buffer (process-buffer proc))
555 ;; Force mode line redisplay soon
556 (set-buffer-modified-p (buffer-modified-p))
557 (if (eobp)
558 (insert ?\n mode-name " " msg)
559 (save-excursion
560 (goto-char (point-max))
561 (insert ?\n mode-name " " msg)))
562 ;; If buffer and mode line will show that the process
563 ;; is dead, we can delete it now. Otherwise it
564 ;; will stay around until M-x list-processes.
565 (delete-process proc))
566 ;; Restore old buffer, but don't restore old point
567 ;; if obuf is the gud buffer.
568 (set-buffer obuf))))))
570 (defun gud-display-frame ()
571 "Find and obey the last filename-and-line marker from the debugger.
572 Obeying it means displaying in another window the specified file and line."
573 (interactive)
574 (if gud-last-frame
575 (progn
576 (gud-set-buffer)
577 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
578 (setq gud-last-frame nil))))
580 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
581 ;; and that its line LINE is visible.
582 ;; Put the overlay-arrow on the line LINE in that buffer.
583 ;; Most of the trickiness in here comes from wanting to preserve the current
584 ;; region-restriction if that's possible. We use an explicit display-buffer
585 ;; to get around the fact that this is called inside a save-excursion.
587 (defun gud-display-line (true-file line)
588 (let* ((buffer (gud-find-file true-file))
589 (window (display-buffer buffer))
590 (pos))
591 (save-excursion
592 (set-buffer buffer)
593 (make-local-variable 'gud-minor-mode)
594 (setq gud-minor-mode t)
595 (save-restriction
596 (widen)
597 (goto-line line)
598 (setq pos (point))
599 (setq overlay-arrow-string "=>")
600 (or overlay-arrow-position
601 (setq overlay-arrow-position (make-marker)))
602 (set-marker overlay-arrow-position (point) (current-buffer)))
603 (cond ((or (< pos (point-min)) (> pos (point-max)))
604 (widen)
605 (goto-char pos))))
606 (set-window-point window overlay-arrow-position)))
608 ;;; The gud-call function must do the right thing whether its invoking
609 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
610 ;;; or a C buffer in GUD minor mode. In the former case, we want to
611 ;;; supply data from gud-last-frame. Here's how we do it:
613 (defun gud-format-command (str arg)
614 (let ((minor (not (eq (current-buffer) gud-comint-buffer))))
615 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
616 (progn
617 (setq str (concat
618 (substring str (match-beginning 1) (match-end 1))
619 (if minor
620 (buffer-file-name)
621 (car gud-last-frame))
622 (substring str (match-beginning 2) (match-end 2))))))
623 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
624 (progn
625 (setq str (concat
626 (substring str (match-beginning 1) (match-end 1))
627 (if minor
628 (save-excursion
629 (beginning-of-line)
630 (save-restriction (widen)
631 (1+ (count-lines 1 (point)))))
632 (cdr gud-last-frame))
633 (substring str (match-beginning 2) (match-end 2))))))
634 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
635 (progn
636 (setq str (concat
637 (substring str (match-beginning 1) (match-end 1))
638 (find-c-expr)
639 (substring str (match-beginning 2) (match-end 2))))))
640 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
641 (progn
642 (setq str (concat
643 (substring str (match-beginning 1) (match-end 1))
644 (gud-read-address)
645 (substring str (match-beginning 2) (match-end 2))))))
646 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
647 (progn
648 (setq str (concat
649 (substring str (match-beginning 1) (match-end 1))
650 (if arg (int-to-string arg) "")
651 (substring str (match-beginning 2) (match-end 2))))))
656 (defun gud-read-address ()
657 "Return a string containing the core-address found in the buffer at point."
658 (save-excursion
659 (let ((pt (point)) found begin)
660 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
661 (cond
662 (found (forward-char 2)
663 (buffer-substring found
664 (progn (re-search-forward "[^0-9a-f]")
665 (forward-char -1)
666 (point))))
667 (t (setq begin (progn (re-search-backward "[^0-9]")
668 (forward-char 1)
669 (point)))
670 (forward-char 1)
671 (re-search-forward "[^0-9]")
672 (forward-char -1)
673 (buffer-substring begin (point)))))))
675 (defun gud-call (fmt &optional arg)
676 (let ((msg (gud-format-command fmt arg)))
677 (message "Command: %s" msg)
678 (sit-for 0)
679 (gud-basic-call msg)))
681 (defun gud-basic-call (command)
682 "Invoke the debugger COMMAND displaying source in other window."
683 (interactive)
684 (gud-set-buffer)
685 (let ((command (concat command "\n"))
686 (proc (get-buffer-process gud-comint-buffer)))
688 ;; Arrange for the current prompt to get deleted.
689 (save-excursion
690 (set-buffer gud-comint-buffer)
691 (goto-char (process-mark proc))
692 (beginning-of-line)
693 (if (looking-at comint-prompt-regexp)
694 (set-marker gud-delete-prompt-marker (point))))
695 (process-send-string proc command)))
697 (defun gud-refresh (&optional arg)
698 "Fix up a possibly garbled display, and redraw the arrow."
699 (interactive "P")
700 (recenter arg)
701 (gud-display-frame))
703 ;;; Code for parsing expressions out of C code. The single entry point is
704 ;;; find-c-expr, which tries to return an lvalue expression from around point.
706 ;;; The rest of this file is a hacked version of gdbsrc.el by
707 ;;; Debby Ayers <ayers@asc.slb.com>,
708 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
709 ;;; ??? We're waiting on papers from these people
711 (defun find-c-expr ()
712 "Returns the C expr that surrounds point."
713 (interactive)
714 (save-excursion
715 (let ((p) (expr) (test-expr))
716 (setq p (point))
717 (setq expr (expr-cur))
718 (setq test-expr (expr-prev))
719 (while (expr-compound test-expr expr)
720 (setq expr (cons (car test-expr) (cdr expr)))
721 (goto-char (car expr))
722 (setq test-expr (expr-prev))
724 (goto-char p)
725 (setq test-expr (expr-next))
726 (while (expr-compound expr test-expr)
727 (setq expr (cons (car expr) (cdr test-expr)))
728 (setq test-expr (expr-next))
730 (buffer-substring (car expr) (cdr expr))
735 (defun expr-cur ()
736 "Returns the expr that point is in; point is set to beginning of expr.
737 The expr is represented as a cons cell, where the car specifies the point in
738 the current buffer that marks the beginning of the expr and the cdr specifies
739 the character after the end of the expr"
740 (let ((p (point)) (begin) (end))
741 (back-expr)
742 (setq begin (point))
743 (forw-expr)
744 (setq end (point))
745 (if (>= p end)
746 (progn
747 (setq begin p)
748 (goto-char p)
749 (forw-expr)
750 (setq end (point))
753 (goto-char begin)
754 (cons begin end)
758 (defun back-expr ()
759 "Version of backward-sexp that catches errors"
760 (condition-case nil
761 (backward-sexp)
762 (error t)))
764 (defun forw-expr ()
765 "Version of forward-sexp that catches errors"
766 (condition-case nil
767 (forward-sexp)
768 (error t)))
770 (defun expr-prev ()
771 "Returns the previous expr, point is set to beginning of that expr.
772 The expr is represented as a cons cell, where the car specifies the point in
773 the current buffer that marks the beginning of the expr and the cdr specifies
774 the character after the end of the expr"
775 (let ((begin) (end))
776 (back-expr)
777 (setq begin (point))
778 (forw-expr)
779 (setq end (point))
780 (goto-char begin)
781 (cons begin end)))
783 (defun expr-next ()
784 "Returns the following expr, point is set to beginning of that expr.
785 The expr is represented as a cons cell, where the car specifies the point in
786 the current buffer that marks the beginning of the expr and the cdr specifies
787 the character after the end of the expr"
788 (let ((begin) (end))
789 (forw-expr)
790 (forw-expr)
791 (setq end (point))
792 (back-expr)
793 (setq begin (point))
794 (cons begin end)
798 (defun expr-compound-sep (span-start span-end)
799 "Returns '.' for '->' & '.', returns ' ' for white space,
800 returns '?' for other puctuation."
801 (let ((result ? )
802 (syntax))
803 (while (< span-start span-end)
804 (setq syntax (char-syntax (char-after span-start)))
805 (cond
806 ((= syntax ? ) t)
807 ((= syntax ?.) (setq syntax (char-after span-start))
808 (cond
809 ((= syntax ?.) (setq result ?.))
810 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
811 (setq result ?.)
812 (setq span-start (+ span-start 1)))
813 (t (setq span-start span-end)
814 (setq result ??)))))
815 (setq span-start (+ span-start 1)))
816 result
820 (defun expr-compound (first second)
821 "Returns non-nil if the concatenation of two exprs results in a single C
822 token. The two exprs are represented as a cons cells, where the car
823 specifies the point in the current buffer that marks the beginning of the
824 expr and the cdr specifies the character after the end of the expr
825 Link exprs of the form:
826 Expr -> Expr
827 Expr . Expr
828 Expr (Expr)
829 Expr [Expr]
830 (Expr) Expr
831 [Expr] Expr"
832 (let ((span-start (cdr first))
833 (span-end (car second))
834 (syntax))
835 (setq syntax (expr-compound-sep span-start span-end))
836 (cond
837 ((= (car first) (car second)) nil)
838 ((= (cdr first) (cdr second)) nil)
839 ((= syntax ?.) t)
840 ((= syntax ? )
841 (setq span-start (char-after (- span-start 1)))
842 (setq span-end (char-after span-end))
843 (cond
844 ((= span-start ?) ) t )
845 ((= span-start ?] ) t )
846 ((= span-end ?( ) t )
847 ((= span-end ?[ ) t )
848 (t nil))
850 (t nil))
854 ;;; gud.el ends here