(shell-prompt-pattern): Use defvar.
[emacs.git] / lisp / gud.el
blob7be667ba8ba4d566a11f1bc82c55f838b35d5daf
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.3
5 ;; Keywords: unix, tools
7 ;; Copyright (C) 1992, 1993 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 ;;; Code:
35 (require 'comint)
36 (require 'etags)
38 ;; ======================================================================
39 ;; GUD commands must be visible in C buffers visited by GUD
41 (defvar gud-key-prefix "\C-x\C-a"
42 "Prefix of all GUD commands valid in C buffers.")
44 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
45 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
47 ;; ======================================================================
48 ;; the overloading mechanism
50 (defun gud-overload-functions (gud-overload-alist)
51 "Overload functions defined in GUD-OVERLOAD-ALIST.
52 This association list has elements of the form
53 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
54 (mapcar
55 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
56 gud-overload-alist))
58 (defun gud-debugger-startup (file args)
59 (error "GUD not properly entered."))
61 (defun gud-marker-filter (str)
62 (error "GUD not properly entered."))
64 (defun gud-find-file (f)
65 (error "GUD not properly entered."))
67 ;; ======================================================================
68 ;; command definition
70 ;; This macro is used below to define some basic debugger interface commands.
71 ;; Of course you may use `gud-def' with any other debugger command, including
72 ;; user defined ones.
74 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
75 ;; which defines FUNC to send the command NAME to the debugger, gives
76 ;; it the docstring DOC, and binds that function to KEY in the GUD
77 ;; major mode. The function is also bound in the global keymap with the
78 ;; GUD prefix.
80 (defmacro gud-def (func cmd key &optional doc)
81 "Define FUNC to be a command sending STR and bound to KEY, with
82 optional doc string DOC. Certain %-escapes in the string arguments
83 are interpreted specially if present. These are:
85 %f name of current source file.
86 %l number of current source line
87 %e text of the C lvalue or function-call expression surrounding point.
88 %a text of the hexadecimal address surrounding point
89 %p prefix argument to the command (if any) as a number
91 The `current' source file is the file of the current buffer (if
92 we're in a C file) or the source file current at the last break or
93 step (if we're in the GUD buffer).
94 The `current' line is that of the current buffer (if we're in a
95 source file) or the source line number at the last break or step (if
96 we're in the GUD buffer)."
97 (list 'progn
98 (list 'defun func '(arg)
99 (or doc "")
100 '(interactive "p")
101 (list 'gud-call cmd 'arg))
102 (if key
103 (list 'define-key
104 '(current-local-map)
105 (concat "\C-c" key)
106 (list 'quote func)))
107 (if key
108 (list 'global-set-key
109 (list 'concat 'gud-key-prefix key)
110 (list 'quote func)))))
112 ;; Where gud-display-frame should put the debugging arrow. This is
113 ;; set by the marker-filter, which scans the debugger's output for
114 ;; indications of the current program counter.
115 (defvar gud-last-frame nil)
117 ;; All debugger-specific information is collected here.
118 ;; Here's how it works, in case you ever need to add a debugger to the mode.
120 ;; Each entry must define the following at startup:
122 ;;<name>
123 ;; comint-prompt-regexp
124 ;; gud-<name>-debugger-startup
125 ;; gud-<name>-marker-filter
126 ;; gud-<name>-find-file
128 ;; The job of the startup-command method is to fire up a copy of the debugger,
129 ;; given a list of debugger arguments.
131 ;; The job of the marker-filter method is to detect file/line markers in
132 ;; strings and set the global gud-last-frame to indicate what display
133 ;; action (if any) should be triggered by the marker. Note that only
134 ;; whetever the method *returns* is displayed in the buffer; thus, you
135 ;; can filter the debugger's output, interpreting some and passing on
136 ;; the rest.
138 ;; The job of the find-file method is to visit and return the buffer indicated
139 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
140 ;; something else.
142 ;; ======================================================================
143 ;; gdb functions
145 (defun gud-gdb-debugger-startup (file args)
146 (apply 'make-comint (concat "gud-" file) "gdb" nil "-fullname" args))
148 (defun gud-gdb-marker-filter (string)
149 (if (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" string)
150 (progn
151 (setq gud-last-frame
152 (cons
153 (substring string (match-beginning 1) (match-end 1))
154 (string-to-int
155 (substring string (match-beginning 2) (match-end 2)))))
156 ;; this computation means the ^Z^Z-initiated marker in the
157 ;; input string is never emitted.
158 (concat
159 (substring string 0 (match-beginning 0))
160 (substring string (match-end 0))
162 string))
164 (defun gud-gdb-find-file (f)
165 (find-file-noselect f))
167 ;;;###autoload
168 (defun gdb (args)
169 "Run gdb on program FILE in buffer *gud-FILE*.
170 The directory containing FILE becomes the initial working directory
171 and source-file directory for your debugger."
172 (interactive "sRun gdb (like this): gdb ")
173 (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup)
174 (gud-marker-filter . gud-gdb-marker-filter)
175 (gud-find-file . gud-gdb-find-file)
178 (gud-common-init args)
180 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
181 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
182 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
183 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
184 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
185 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
186 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
187 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
188 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
189 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
190 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
192 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
193 (run-hooks 'gdb-mode-hook)
197 ;; ======================================================================
198 ;; sdb functions
200 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
201 "If nil, we're on a System V Release 4 and don't need the tags hack.")
203 (defvar gud-sdb-lastfile nil)
205 (defun gud-sdb-debugger-startup (file args)
206 (apply 'make-comint (concat "gud-" file) "sdb" nil args))
208 (defun gud-sdb-marker-filter (string)
209 (cond
210 ;; System V Release 3.2 uses this format
211 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
212 string)
213 (setq gud-last-frame
214 (cons
215 (substring string (match-beginning 2) (match-end 2))
216 (string-to-int
217 (substring string (match-beginning 3) (match-end 3))))))
218 ;; System V Release 4.0
219 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
220 string)
221 (setq gud-sdb-lastfile
222 (substring string (match-beginning 2) (match-end 2))))
223 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
224 (setq gud-last-frame
225 (cons
226 gud-sdb-lastfile
227 (string-to-int
228 (substring string (match-beginning 1) (match-end 1))))))
230 (setq gud-sdb-lastfile nil)))
231 string)
233 (defun gud-sdb-find-file (f)
234 (if gud-sdb-needs-tags
235 (find-tag-noselect f)
236 (find-file-noselect f)))
238 ;;;###autoload
239 (defun sdb (args)
240 "Run sdb on program FILE in buffer *gud-FILE*.
241 The directory containing FILE becomes the initial working directory
242 and source-file directory for your debugger."
243 (interactive "sRun sdb (like this): sdb ")
244 (if (and gud-sdb-needs-tags
245 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
246 (error "The sdb support requires a valid tags table to work."))
247 (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup)
248 (gud-marker-filter . gud-sdb-marker-filter)
249 (gud-find-file . gud-sdb-find-file)
252 (gud-common-init args)
254 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
255 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
256 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
257 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
258 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
259 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
260 (gud-def gud-cont "c" "\C-r" "Continue with display.")
261 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
263 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
264 (run-hooks 'sdb-mode-hook)
267 ;; ======================================================================
268 ;; dbx functions
270 (defun gud-dbx-debugger-startup (file args)
271 (apply 'make-comint (concat "gud-" file) "dbx" nil args))
273 (defun gud-dbx-marker-filter (string)
274 (if (string-match
275 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" string)
276 (setq gud-last-frame
277 (cons
278 (substring string (match-beginning 2) (match-end 2))
279 (string-to-int
280 (substring string (match-beginning 1) (match-end 1))))))
281 string)
283 (defun gud-dbx-find-file (f)
284 (find-file-noselect f))
286 ;;;###autoload
287 (defun dbx (args)
288 "Run dbx on program FILE in buffer *gud-FILE*.
289 The directory containing FILE becomes the initial working directory
290 and source-file directory for your debugger."
291 (interactive "sRun dbx (like this): dbx")
292 (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup)
293 (gud-marker-filter . gud-dbx-marker-filter)
294 (gud-find-file . gud-dbx-find-file)
297 (gud-common-init args)
299 (gud-def gud-break "stop at \"%f\":%l"
300 "\C-b" "Set breakpoint at current line.")
301 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
302 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
303 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
304 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
305 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
306 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
307 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
308 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
310 (setq comint-prompt-regexp "^[^)]*dbx) *")
311 (run-hooks 'dbx-mode-hook)
315 ;; End of debugger-specific information
318 ;;; When we send a command to the debugger via gud-call, it's annoying
319 ;;; to see the command and the new prompt inserted into the debugger's
320 ;;; buffer; we have other ways of knowing the command has completed.
322 ;;; If the buffer looks like this:
323 ;;; --------------------
324 ;;; (gdb) set args foo bar
325 ;;; (gdb) -!-
326 ;;; --------------------
327 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
328 ;;; source file to set a breakpoint, we want the buffer to end up like
329 ;;; this:
330 ;;; --------------------
331 ;;; (gdb) set args foo bar
332 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
333 ;;; (gdb) -!-
334 ;;; --------------------
335 ;;; Essentially, the old prompt is deleted, and the command's output
336 ;;; and the new prompt take its place.
338 ;;; Not echoing the command is easy enough; you send it directly using
339 ;;; process-send-string, and it never enters the buffer. However,
340 ;;; getting rid of the old prompt is trickier; you don't want to do it
341 ;;; when you send the command, since that will result in an annoying
342 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
343 ;;; waits for a response from the debugger, and the new prompt is
344 ;;; inserted. Instead, we'll wait until we actually get some output
345 ;;; from the subprocess before we delete the prompt. If the command
346 ;;; produced no output other than a new prompt, that prompt will most
347 ;;; likely be in the first chunk of output received, so we will delete
348 ;;; the prompt and then replace it with an identical one. If the
349 ;;; command produces output, the prompt is moving anyway, so the
350 ;;; flicker won't be annoying.
352 ;;; So - when we want to delete the prompt upon receipt of the next
353 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
354 ;;; the start of the prompt; the process filter will notice this, and
355 ;;; delete all text between it and the process output marker. If
356 ;;; gud-delete-prompt-marker points nowhere, we leave the current
357 ;;; prompt alone.
358 (defvar gud-delete-prompt-marker nil)
361 (defun gud-mode ()
362 "Major mode for interacting with an inferior debugger process.
364 You start it up with one of the commands M-x gdb, M-x sdb, or
365 M-x dbx. Each entry point finishes by executing a hook; gdb-mode-hook,
366 sdb-mode-hook or dbx-mode-hook respectively.
368 After startup, the following commands are available in both the GUD
369 interaction buffer and any source buffer GUD visits due to a breakpoint stop
370 or step operation:
372 \\[gud-break] sets a breakpoint at the current file and line. In the
373 GUD buffer, the current file and line are those of the last breakpoint or
374 step. In a source buffer, they are the buffer's file and current line.
376 \\[gud-remove] removes breakpoints on the current file and line.
378 \\[gud-refresh] displays in the source window the last line referred to
379 in the gud buffer.
381 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
382 step-one-line (not entering function calls), and step-one-instruction
383 and then update the source window with the current file and position.
384 \\[gud-cont] continues execution.
386 \\[gud-print] tries to find the largest C lvalue or function-call expression
387 around point, and sends it to the debugger for value display.
389 The above commands are common to all supported debuggers.
391 Under gdb and sdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
392 except that the breakpoint is temporary; that is, it is removed when
393 execution stops on it.
395 Under gdb and dbx, \\[gud-up] pops up through an enclosing stack
396 frame. \\[gud-down] drops back down through one.
398 If you are using gdb, \\[gdb-finish] runs execution to the return from
399 the current function and stops.
401 All the keystrokes above have synonyms (in the GUD buffer only) with
402 a prefix of C-c (this is for backward compatibility with old gdb.el).
404 All pre-defined functions for which the concept make sense repeat
405 themselves the appropriate number of times if you give a prefix
406 argument.
408 You may use the gud-def macro in the initialization hook to define other
409 commands.
411 Other commands for interacting with the debugger process are inherited from
412 comint mode, which see."
413 (interactive)
414 (comint-mode)
415 (setq major-mode 'gud-mode)
416 (setq mode-name "Debugger")
417 (setq mode-line-process '(": %s"))
418 (use-local-map (copy-keymap comint-mode-map))
419 (make-local-variable 'gud-last-frame)
420 (setq gud-last-frame nil)
421 (make-local-variable 'comint-prompt-regexp)
422 (make-local-variable 'gud-delete-prompt-marker)
423 (setq gud-delete-prompt-marker (make-marker))
424 (run-hooks 'gud-mode-hook)
427 (defvar gud-comint-buffer nil)
429 (defun gud-common-init (args)
430 ;; Perform initializations common to all debuggers
431 ;; There *must* be a cleaner way to lex the arglist...
432 (let (file i)
433 (if (string= args "")
434 (setq args nil)
435 (save-excursion
436 (set-buffer (get-buffer-create "*gud-scratch*"))
437 (erase-buffer)
438 (insert args)
439 (goto-char (point-max))
440 (insert "\")")
441 (goto-char (point-min))
442 (insert "(\"")
443 (while (re-search-forward " +" nil t)
444 (replace-match "\" \"" nil nil))
445 (goto-char (point-min))
446 (while (re-search-forward "\"\"" nil t)
447 (replace-match "" nil nil))
448 (setq args (read (buffer-string)))
449 (kill-buffer (current-buffer))))
450 (setq i (1- (length args)))
451 (while (and (>= i 0) (not (= (aref (nth i args) 0) ?-)))
452 (setq file (nth i args)) (setq i (1- i)))
453 (let* ((path (expand-file-name file))
454 (filepart (file-name-nondirectory path)))
455 (switch-to-buffer (concat "*gud-" filepart "*"))
456 (setq default-directory (file-name-directory path))
457 (or (bolp) (newline))
458 (insert "Current directory is " default-directory "\n")
459 (gud-debugger-startup filepart args)))
460 (gud-mode)
461 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
462 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
463 (gud-set-buffer)
466 (defun gud-set-buffer ()
467 (cond ((eq major-mode 'gud-mode)
468 (setq gud-comint-buffer (current-buffer)))))
470 ;; These functions are responsible for inserting output from your debugger
471 ;; into the buffer. The hard work is done by the method that is
472 ;; the value of gud-marker-filter.
474 (defun gud-filter (proc string)
475 ;; Here's where the actual buffer insertion is done
476 (let ((inhibit-quit t))
477 (save-excursion
478 (set-buffer (process-buffer proc))
479 (let (moving output-after-point)
480 (save-excursion
481 (goto-char (process-mark proc))
482 ;; If we have been so requested, delete the debugger prompt.
483 (if (marker-buffer gud-delete-prompt-marker)
484 (progn
485 (delete-region (point) gud-delete-prompt-marker)
486 (set-marker gud-delete-prompt-marker nil)))
487 (insert-before-markers (gud-marker-filter string))
488 (setq moving (= (point) (process-mark proc)))
489 (setq output-after-point (< (point) (process-mark proc)))
490 ;; Check for a filename-and-line number.
491 ;; Don't display the specified file
492 ;; unless (1) point is at or after the position where output appears
493 ;; and (2) this buffer is on the screen.
494 (if (and gud-last-frame
495 (not output-after-point)
496 (get-buffer-window (current-buffer)))
497 (gud-display-frame)))
498 (if moving (goto-char (process-mark proc)))))))
500 (defun gud-sentinel (proc msg)
501 (cond ((null (buffer-name (process-buffer proc)))
502 ;; buffer killed
503 ;; Stop displaying an arrow in a source file.
504 (setq overlay-arrow-position nil)
505 (set-process-buffer proc nil))
506 ((memq (process-status proc) '(signal exit))
507 ;; Stop displaying an arrow in a source file.
508 (setq overlay-arrow-position nil)
509 ;; Fix the mode line.
510 (setq mode-line-process
511 (concat ": "
512 (symbol-name (process-status proc))))
513 (let* ((obuf (current-buffer)))
514 ;; save-excursion isn't the right thing if
515 ;; process-buffer is current-buffer
516 (unwind-protect
517 (progn
518 ;; Write something in *compilation* and hack its mode line,
519 (set-buffer (process-buffer proc))
520 ;; Force mode line redisplay soon
521 (set-buffer-modified-p (buffer-modified-p))
522 (if (eobp)
523 (insert ?\n mode-name " " msg)
524 (save-excursion
525 (goto-char (point-max))
526 (insert ?\n mode-name " " msg)))
527 ;; If buffer and mode line will show that the process
528 ;; is dead, we can delete it now. Otherwise it
529 ;; will stay around until M-x list-processes.
530 (delete-process proc))
531 ;; Restore old buffer, but don't restore old point
532 ;; if obuf is the gud buffer.
533 (set-buffer obuf))))))
535 (defun gud-display-frame ()
536 "Find and obey the last filename-and-line marker from the debugger.
537 Obeying it means displaying in another window the specified file and line."
538 (interactive)
539 (if gud-last-frame
540 (progn
541 (gud-set-buffer)
542 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
543 (setq gud-last-frame nil))))
545 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
546 ;; and that its line LINE is visible.
547 ;; Put the overlay-arrow on the line LINE in that buffer.
548 ;; Most of the trickiness in here comes from wanting to preserve the current
549 ;; region-restriction if that's possible. We use an explicit display-buffer
550 ;; to get around the fact that this is called inside a save-excursion.
552 (defun gud-display-line (true-file line)
553 (let* ((buffer (gud-find-file true-file))
554 (window (display-buffer buffer))
555 (pos))
556 ;;; (if (equal buffer (current-buffer))
557 ;;; nil
558 ;;; (setq buffer-read-only nil))
559 (save-excursion
560 ;;; (setq buffer-read-only t)
561 (set-buffer buffer)
562 (save-restriction
563 (widen)
564 (goto-line line)
565 (setq pos (point))
566 (setq overlay-arrow-string "=>")
567 (or overlay-arrow-position
568 (setq overlay-arrow-position (make-marker)))
569 (set-marker overlay-arrow-position (point) (current-buffer)))
570 (cond ((or (< pos (point-min)) (> pos (point-max)))
571 (widen)
572 (goto-char pos))))
573 (set-window-point window overlay-arrow-position)))
575 ;;; The gud-call function must do the right thing whether its invoking
576 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
577 ;;; or a C buffer. In the former case, we want to supply data from
578 ;;; gud-last-frame. Here's how we do it:
580 (defun gud-format-command (str arg)
581 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
582 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
583 (progn
584 (setq str (concat
585 (substring str (match-beginning 1) (match-end 1))
586 (file-name-nondirectory (if insource
587 (buffer-file-name)
588 (car gud-last-frame)))
589 (substring str (match-beginning 2) (match-end 2))))))
590 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
591 (progn
592 (setq str (concat
593 (substring str (match-beginning 1) (match-end 1))
594 (if insource
595 (save-excursion
596 (beginning-of-line)
597 (save-restriction (widen)
598 (1+ (count-lines 1 (point)))))
599 (cdr gud-last-frame))
600 (substring str (match-beginning 2) (match-end 2))))))
601 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
602 (progn
603 (setq str (concat
604 (substring str (match-beginning 1) (match-end 1))
605 (find-c-expr)
606 (substring str (match-beginning 2) (match-end 2))))))
607 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
608 (progn
609 (setq str (concat
610 (substring str (match-beginning 1) (match-end 1))
611 (gud-read-address)
612 (substring str (match-beginning 2) (match-end 2))))))
613 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
614 (progn
615 (setq str (concat
616 (substring str (match-beginning 1) (match-end 1))
617 (if arg (int-to-string arg) "")
618 (substring str (match-beginning 2) (match-end 2))))))
623 (defun gud-read-address ()
624 "Return a string containing the core-address found in the buffer at point."
625 (save-excursion
626 (let ((pt (point)) found begin)
627 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
628 (cond
629 (found (forward-char 2)
630 (buffer-substring found
631 (progn (re-search-forward "[^0-9a-f]")
632 (forward-char -1)
633 (point))))
634 (t (setq begin (progn (re-search-backward "[^0-9]")
635 (forward-char 1)
636 (point)))
637 (forward-char 1)
638 (re-search-forward "[^0-9]")
639 (forward-char -1)
640 (buffer-substring begin (point)))))))
642 (defun gud-call (fmt &optional arg)
643 (let ((msg (gud-format-command fmt arg)))
644 (message "Command: %s" msg)
645 (sit-for 0)
646 (gud-basic-call msg)))
648 (defun gud-basic-call (command)
649 "Invoke the debugger COMMAND displaying source in other window."
650 (interactive)
651 (gud-set-buffer)
652 (let ((command (concat command "\n"))
653 (proc (get-buffer-process gud-comint-buffer)))
655 ;; Arrange for the current prompt to get deleted.
656 (save-excursion
657 (set-buffer gud-comint-buffer)
658 (goto-char (process-mark proc))
659 (beginning-of-line)
660 (if (looking-at comint-prompt-regexp)
661 (set-marker gud-delete-prompt-marker (point))))
662 (process-send-string proc command)))
664 (defun gud-refresh (&optional arg)
665 "Fix up a possibly garbled display, and redraw the arrow."
666 (interactive "P")
667 (recenter arg)
668 (gud-display-frame))
670 ;;; Code for parsing expressions out of C code. The single entry point is
671 ;;; find-c-expr, which tries to return an lvalue expression from around point.
673 ;;; The rest of this file is a hacked version of gdbsrc.el by
674 ;;; Debby Ayers <ayers@asc.slb.com>,
675 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
676 ;;; ??? We're waiting on papers from these people
678 (defun find-c-expr ()
679 "Returns the C expr that surrounds point."
680 (interactive)
681 (save-excursion
682 (let ((p) (expr) (test-expr))
683 (setq p (point))
684 (setq expr (expr-cur))
685 (setq test-expr (expr-prev))
686 (while (expr-compound test-expr expr)
687 (setq expr (cons (car test-expr) (cdr expr)))
688 (goto-char (car expr))
689 (setq test-expr (expr-prev))
691 (goto-char p)
692 (setq test-expr (expr-next))
693 (while (expr-compound expr test-expr)
694 (setq expr (cons (car expr) (cdr test-expr)))
695 (setq test-expr (expr-next))
697 (buffer-substring (car expr) (cdr expr))
702 (defun expr-cur ()
703 "Returns the expr that point is in; point is set to beginning of expr.
704 The expr is represented as a cons cell, where the car specifies the point in
705 the current buffer that marks the beginning of the expr and the cdr specifies
706 the character after the end of the expr"
707 (let ((p (point)) (begin) (end))
708 (back-expr)
709 (setq begin (point))
710 (forw-expr)
711 (setq end (point))
712 (if (>= p end)
713 (progn
714 (setq begin p)
715 (goto-char p)
716 (forw-expr)
717 (setq end (point))
720 (goto-char begin)
721 (cons begin end)
725 (defun back-expr ()
726 "Version of backward-sexp that catches errors"
727 (condition-case nil
728 (backward-sexp)
729 (error t)))
731 (defun forw-expr ()
732 "Version of forward-sexp that catches errors"
733 (condition-case nil
734 (forward-sexp)
735 (error t)))
737 (defun expr-prev ()
738 "Returns the previous expr, point is set to beginning of that expr.
739 The expr is represented as a cons cell, where the car specifies the point in
740 the current buffer that marks the beginning of the expr and the cdr specifies
741 the character after the end of the expr"
742 (let ((begin) (end))
743 (back-expr)
744 (setq begin (point))
745 (forw-expr)
746 (setq end (point))
747 (goto-char begin)
748 (cons begin end)))
750 (defun expr-next ()
751 "Returns the following expr, point is set to beginning of that expr.
752 The expr is represented as a cons cell, where the car specifies the point in
753 the current buffer that marks the beginning of the expr and the cdr specifies
754 the character after the end of the expr"
755 (let ((begin) (end))
756 (forw-expr)
757 (forw-expr)
758 (setq end (point))
759 (back-expr)
760 (setq begin (point))
761 (cons begin end)
765 (defun expr-compound-sep (span-start span-end)
766 "Returns '.' for '->' & '.', returns ' ' for white space,
767 returns '?' for other puctuation."
768 (let ((result ? )
769 (syntax))
770 (while (< span-start span-end)
771 (setq syntax (char-syntax (char-after span-start)))
772 (cond
773 ((= syntax ? ) t)
774 ((= syntax ?.) (setq syntax (char-after span-start))
775 (cond
776 ((= syntax ?.) (setq result ?.))
777 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
778 (setq result ?.)
779 (setq span-start (+ span-start 1)))
780 (t (setq span-start span-end)
781 (setq result ??)))))
782 (setq span-start (+ span-start 1)))
783 result
787 (defun expr-compound (first second)
788 "Returns non-nil if the concatenation of two exprs results in a single C
789 token. The two exprs are represented as a cons cells, where the car
790 specifies the point in the current buffer that marks the beginning of the
791 expr and the cdr specifies the character after the end of the expr
792 Link exprs of the form:
793 Expr -> Expr
794 Expr . Expr
795 Expr (Expr)
796 Expr [Expr]
797 (Expr) Expr
798 [Expr] Expr"
799 (let ((span-start (cdr first))
800 (span-end (car second))
801 (syntax))
802 (setq syntax (expr-compound-sep span-start span-end))
803 (cond
804 ((= (car first) (car second)) nil)
805 ((= (cdr first) (cdr second)) nil)
806 ((= syntax ?.) t)
807 ((= syntax ? )
808 (setq span-start (char-after (- span-start 1)))
809 (setq span-end (char-after span-end))
810 (cond
811 ((= span-start ?) ) t )
812 ((= span-start ?] ) t )
813 ((= span-end ?( ) t )
814 ((= span-end ?[ ) t )
815 (t nil))
817 (t nil))
821 ;;; There appears to be a bug in the byte compiler somewhere near macro
822 ;;; handling that (a) generates a spurious message about gud-key-prefix
823 ;;; when the global-set-key clause in gud-def is compiled, (b) generates
824 ;;; incorrect bytecode for gud-def. The symptom of this incorrectness
825 ;;; is that loading gud.elc brings in a compiled gud-def that doesn't
826 ;;; properly perform both global (C-x C-a) and local (C-c) bindings.
827 ;;; The workaround is to always load from source. Consequently, we try
828 ;;; to disable byte-compilation here.
830 ;;; Local Variables:
831 ;;; no-byte-compile: t
832 ;;; End:
834 ;;; gud.el ends here