]> code.delx.au - gnu-emacs/blob - lisp/gud.el
Doc fix.
[gnu-emacs] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, or dbx under Emacs
2
3 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
4 ;; Version: 1.3
5 ;; Keywords: unix, tools
6
7 ;; Copyright (C) 1992 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
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.
24
25 ;;; Commentary:
26
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.
32
33 ;;; Code:
34
35 (require 'comint)
36 (require 'etags)
37
38 ;; ======================================================================
39 ;; GUD commands must be visible in C buffers visited by GUD
40
41 (defvar gud-key-prefix "\C-x\C-a"
42 "Prefix of all GUD commands valid in C buffers.")
43
44 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
45 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
46
47 ;; ======================================================================
48 ;; the overloading mechanism
49
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))
57
58 (defun gud-debugger-startup (file args)
59 (error "GUD not properly entered."))
60
61 (defun gud-marker-filter (str)
62 (error "GUD not properly entered."))
63
64 (defun gud-find-file (f)
65 (error "GUD not properly entered."))
66
67 ;; ======================================================================
68 ;; command definition
69
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.
73
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.
79
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:
84
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
90
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 (progn
104 (list 'define-key
105 '(current-local-map)
106 (concat "\C-c" key)
107 (list 'quote func))
108 (list 'global-set-key
109 (concat gud-key-prefix key)
110 (list 'quote func))
111 ))))
112
113 ;; Where gud-display-frame should put the debugging arrow. This is
114 ;; set by the marker-filter, which scans the debugger's output for
115 ;; indications of the current program counter.
116 (defvar gud-last-frame nil)
117
118 ;; All debugger-specific information is collected here.
119 ;; Here's how it works, in case you ever need to add a debugger to the mode.
120 ;;
121 ;; Each entry must define the following at startup:
122 ;;
123 ;;<name>
124 ;; comint-prompt-regexp
125 ;; gud-<name>-debugger-startup
126 ;; gud-<name>-marker-filter
127 ;; gud-<name>-find-file
128 ;;
129 ;; The job of the startup-command method is to fire up a copy of the debugger,
130 ;; given a list of debugger arguments.
131 ;;
132 ;; The job of the marker-filter method is to detect file/line markers in
133 ;; strings and set the global gud-last-frame to indicate what display
134 ;; action (if any) should be triggered by the marker. Note that only
135 ;; whetever the method *returns* is displayed in the buffer; thus, you
136 ;; can filter the debugger's output, interpreting some and passing on
137 ;; the rest.
138 ;;
139 ;; The job of the find-file method is to visit and return the buffer indicated
140 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
141 ;; something else.
142
143 ;; ======================================================================
144 ;; gdb functions
145
146 (defun gud-gdb-debugger-startup (file args)
147 (apply 'make-comint (concat "gud-" file) "gdb" nil "-fullname" args))
148
149 (defun gud-gdb-marker-filter (string)
150 (if (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" string)
151 (progn
152 (setq gud-last-frame
153 (cons
154 (substring string (match-beginning 1) (match-end 1))
155 (string-to-int
156 (substring string (match-beginning 2) (match-end 2)))))
157 ;; this computation means the ^Z^Z-initiated marker in the
158 ;; input string is never emitted.
159 (concat
160 (substring string 0 (match-beginning 0))
161 (substring string (match-end 0))
162 ))
163 string))
164
165 (defun gud-gdb-find-file (f)
166 (find-file-noselect f))
167
168 ;;;###autoload
169 (defun gdb (args)
170 "Run gdb on program FILE in buffer *gud-FILE*.
171 The directory containing FILE becomes the initial working directory
172 and source-file directory for your debugger."
173 (interactive "sRun gdb (like this): gdb ")
174 (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup)
175 (gud-marker-filter . gud-gdb-marker-filter)
176 (gud-find-file . gud-gdb-find-file)
177 ))
178
179 (gud-common-init args)
180
181 (gud-def gud-break "break %f:%l" "b" "Set breakpoint at current line.")
182 (gud-def gud-tbreak "tbreak %f:%l" "t" "Set breakpoint at current line.")
183 (gud-def gud-remove "clear %l" "d" "Remove breakpoint at current line")
184 (gud-def gud-step "step %p" "s" "Step one source line with display.")
185 (gud-def gud-stepi "stepi %p" "i" "Step one instruction with display.")
186 (gud-def gud-next "next %p" "n" "Step one line (skip functions).")
187 (gud-def gud-cont "cont" "r" "Continue with display.")
188 (gud-def gud-finish "finish" "f" "Finish executing current function.")
189 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
190 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
191 (gud-def gud-print "print %e" "p" "Evaluate C expression at point.")
192
193 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
194 (run-hooks 'gdb-mode-hook)
195 )
196
197
198 ;; ======================================================================
199 ;; sdb functions
200
201 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
202 "If nil, we're on a System V Release 4 and don't need the tags hack.")
203
204 (defvar gud-sdb-lastfile nil)
205
206 (defun gud-sdb-debugger-startup (file args)
207 (apply 'make-comint (concat "gud-" file) "sdb" nil args))
208
209 (defun gud-sdb-marker-filter (string)
210 (cond
211 ;; System V Release 3.2 uses this format
212 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
213 string)
214 (setq gud-last-frame
215 (cons
216 (substring string (match-beginning 2) (match-end 2))
217 (string-to-int
218 (substring string (match-beginning 3) (match-end 3))))))
219 ;; System V Release 4.0
220 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
221 string)
222 (setq gud-sdb-lastfile
223 (substring string (match-beginning 2) (match-end 2))))
224 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
225 (setq gud-last-frame
226 (cons
227 gud-sdb-lastfile
228 (string-to-int
229 (substring string (match-beginning 1) (match-end 1))))))
230 (t
231 (setq gud-sdb-lastfile nil)))
232 string)
233
234 (defun gud-sdb-find-file (f)
235 (if gud-sdb-needs-tags
236 (find-tag-noselect f)
237 (find-file-noselect f)))
238
239 ;;;###autoload
240 (defun sdb (args)
241 "Run sdb on program FILE in buffer *gud-FILE*.
242 The directory containing FILE becomes the initial working directory
243 and source-file directory for your debugger."
244 (interactive "sRun sdb (like this): sdb ")
245 (if (and gud-sdb-needs-tags
246 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
247 (error "The sdb support requires a valid tags table to work."))
248 (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup)
249 (gud-marker-filter . gud-sdb-marker-filter)
250 (gud-find-file . gud-sdb-find-file)
251 ))
252
253 (gud-common-init args)
254
255 (gud-def gud-break "%l b" "b" "Set breakpoint at current line.")
256 (gud-def gud-tbreak "%l c" "t" "Set temporary breakpoint at current line.")
257 (gud-def gud-remove "%l d" "d" "Remove breakpoint at current line")
258 (gud-def gud-step "s %p" "s" "Step one source line with display.")
259 (gud-def gud-stepi "i %p" "i" "Step one instruction with display.")
260 (gud-def gud-next "S %p" "n" "Step one line (skip functions).")
261 (gud-def gud-cont "c" "r" "Continue with display.")
262 (gud-def gud-print "%e/" "p" "Evaluate C expression at point.")
263
264 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
265 (run-hooks 'sdb-mode-hook)
266 )
267
268 ;; ======================================================================
269 ;; dbx functions
270
271 (defun gud-dbx-debugger-startup (file args)
272 (apply 'make-comint (concat "gud-" file) "dbx" nil args))
273
274 (defun gud-dbx-marker-filter (string)
275 (if (string-match
276 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" string)
277 (setq gud-last-frame
278 (cons
279 (substring string (match-beginning 2) (match-end 2))
280 (string-to-int
281 (substring string (match-beginning 1) (match-end 1))))))
282 string)
283
284 (defun gud-dbx-find-file (f)
285 (find-file-noselect f))
286
287 ;;;###autoload
288 (defun dbx (args)
289 "Run dbx on program FILE in buffer *gud-FILE*.
290 The directory containing FILE becomes the initial working directory
291 and source-file directory for your debugger."
292 (interactive "sRun dbx (like this): dbx")
293 (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup)
294 (gud-marker-filter . gud-dbx-marker-filter)
295 (gud-find-file . gud-dbx-find-file)
296 ))
297
298 (gud-common-init args)
299
300 (gud-def gud-break "stop at \"%f\":%l"
301 "b" "Set breakpoint at current line.")
302 (gud-def gud-remove "clear %l" "d" "Remove breakpoint at current line")
303 (gud-def gud-step "step %p" "s" "Step one line with display.")
304 (gud-def gud-stepi "stepi %p" "i" "Step one instruction with display.")
305 (gud-def gud-next "next %p" "n" "Step one line (skip functions).")
306 (gud-def gud-cont "cont" "r" "Continue with display.")
307 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
308 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
309 (gud-def gud-print "print %e" "p" "Evaluate C expression at point.")
310
311 (setq comint-prompt-regexp "^[^)]*dbx) *")
312 (run-hooks 'dbx-mode-hook)
313 )
314
315 ;;
316 ;; End of debugger-specific information
317 ;;
318
319 ;;; When we send a command to the debugger via gud-call, it's annoying
320 ;;; to see the command and the new prompt inserted into the debugger's
321 ;;; buffer; we have other ways of knowing the command has completed.
322 ;;;
323 ;;; If the buffer looks like this:
324 ;;; --------------------
325 ;;; (gdb) set args foo bar
326 ;;; (gdb) -!-
327 ;;; --------------------
328 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
329 ;;; source file to set a breakpoint, we want the buffer to end up like
330 ;;; this:
331 ;;; --------------------
332 ;;; (gdb) set args foo bar
333 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
334 ;;; (gdb) -!-
335 ;;; --------------------
336 ;;; Essentially, the old prompt is deleted, and the command's output
337 ;;; and the new prompt take its place.
338 ;;;
339 ;;; Not echoing the command is easy enough; you send it directly using
340 ;;; process-send-string, and it never enters the buffer. However,
341 ;;; getting rid of the old prompt is trickier; you don't want to do it
342 ;;; when you send the command, since that will result in an annoying
343 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
344 ;;; waits for a response from the debugger, and the new prompt is
345 ;;; inserted. Instead, we'll wait until we actually get some output
346 ;;; from the subprocess before we delete the prompt. If the command
347 ;;; produced no output other than a new prompt, that prompt will most
348 ;;; likely be in the first chunk of output received, so we will delete
349 ;;; the prompt and then replace it with an identical one. If the
350 ;;; command produces output, the prompt is moving anyway, so the
351 ;;; flicker won't be annoying.
352 ;;;
353 ;;; So - when we want to delete the prompt upon receipt of the next
354 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
355 ;;; the start of the prompt; the process filter will notice this, and
356 ;;; delete all text between it and the process output marker. If
357 ;;; gud-delete-prompt-marker points nowhere, we leave the current
358 ;;; prompt alone.
359 (defvar gud-delete-prompt-marker nil)
360
361 \f
362 (defun gud-mode ()
363 "Major mode for interacting with an inferior debugger process.
364
365 You start it up with one of the commands M-x gdb, M-x sdb, or
366 M-x dbx. Each entry point finishes by executing a hook; gdb-mode-hook,
367 sdb-mode-hook or dbx-mode-hook respectively.
368
369 After startup, the following commands are available in both the GUD
370 interaction buffer and any source buffer GUD visits due to a breakpoint stop
371 or step operation:
372
373 \\[gud-break] sets a breakpoint at the current file and line. In the
374 GUD buffer, the current file and line are those of the last breakpoint or
375 step. In a source buffer, they are the buffer's file and current line.
376
377 \\[gud-remove] removes breakpoints on the current file and line.
378
379 \\[gud-refresh] displays in the source window the last line referred to
380 in the gud buffer.
381
382 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
383 step-one-line (not entering function calls), and step-one-instruction
384 and then update the source window with the current file and position.
385 \\[gud-cont] continues execution.
386
387 \\[gud-print] tries to find the largest C lvalue or function-call expression
388 around point, and sends it to the debugger for value display.
389
390 The above commands are common to all supported debuggers.
391
392 Under gdb and sdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
393 except that the breakpoint is temporary; that is, it is removed when
394 execution stops on it.
395
396 Under gdb and dbx, \\[gud-up] pops up through an enclosing stack
397 frame. \\[gud-down] drops back down through one.
398
399 If you are using gdb, \\[gdb-finish] runs execution to the return from
400 the current function and stops.
401
402 All the keystrokes above have synonyms (in the GUD buffer only) with
403 a prefix of C-c (this is for backward compatibility with old gdb.el).
404
405 All pre-defined functions for which the concept make sense repeat
406 themselves the appropriate number of times if you give a prefix
407 argument.
408
409 You may use the gud-def macro in the initialization hook to define other
410 commands.
411
412 Other commands for interacting with the debugger process are inherited from
413 comint mode, which see."
414 (interactive)
415 (comint-mode)
416 (setq major-mode 'gud-mode)
417 (setq mode-name "Debugger")
418 (setq mode-line-process '(": %s"))
419 (use-local-map (copy-keymap comint-mode-map))
420 (make-local-variable 'gud-last-frame)
421 (setq gud-last-frame nil)
422 (make-local-variable 'comint-prompt-regexp)
423 (make-local-variable 'gud-delete-prompt-marker)
424 (setq gud-delete-prompt-marker (make-marker))
425 (run-hooks 'gud-mode-hook)
426 )
427
428 (defvar gud-comint-buffer nil)
429
430 (defun gud-common-init (args)
431 ;; Perform initializations common to all debuggers
432 ;; There *must* be a cleaner way to lex the arglist...
433 (let (file i)
434 (if (string= args "")
435 (setq args nil)
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)
464 )
465
466 (defun gud-set-buffer ()
467 (cond ((eq major-mode 'gud-mode)
468 (setq gud-comint-buffer (current-buffer)))))
469
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.
473
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 (= (point) (process-mark proc)))
480 (output-after-point (< (point) (process-mark proc))))
481 (save-excursion
482 (goto-char (process-mark proc))
483 ;; If we have been so requested, delete the debugger prompt.
484 (if (marker-buffer gud-delete-prompt-marker)
485 (progn
486 (delete-region (point) gud-delete-prompt-marker)
487 (set-marker gud-delete-prompt-marker nil)))
488 (insert-before-markers (gud-marker-filter string))
489 ;; Check for a filename-and-line number.
490 ;; Don't display the specified file
491 ;; unless (1) point is at or after the position where output appears
492 ;; and (2) this buffer is on the screen.
493 (if (and gud-last-frame
494 (not output-after-point)
495 (get-buffer-window (current-buffer)))
496 (gud-display-frame)))
497 (if moving (goto-char (process-mark proc)))))))
498
499 (defun gud-sentinel (proc msg)
500 (cond ((null (buffer-name (process-buffer proc)))
501 ;; buffer killed
502 ;; Stop displaying an arrow in a source file.
503 (setq overlay-arrow-position nil)
504 (set-process-buffer proc nil))
505 ((memq (process-status proc) '(signal exit))
506 ;; Stop displaying an arrow in a source file.
507 (setq overlay-arrow-position nil)
508 ;; Fix the mode line.
509 (setq mode-line-process
510 (concat ": "
511 (symbol-name (process-status proc))))
512 (let* ((obuf (current-buffer)))
513 ;; save-excursion isn't the right thing if
514 ;; process-buffer is current-buffer
515 (unwind-protect
516 (progn
517 ;; Write something in *compilation* and hack its mode line,
518 (set-buffer (process-buffer proc))
519 ;; Force mode line redisplay soon
520 (set-buffer-modified-p (buffer-modified-p))
521 (if (eobp)
522 (insert ?\n mode-name " " msg)
523 (save-excursion
524 (goto-char (point-max))
525 (insert ?\n mode-name " " msg)))
526 ;; If buffer and mode line will show that the process
527 ;; is dead, we can delete it now. Otherwise it
528 ;; will stay around until M-x list-processes.
529 (delete-process proc))
530 ;; Restore old buffer, but don't restore old point
531 ;; if obuf is the gud buffer.
532 (set-buffer obuf))))))
533
534 (defun gud-display-frame ()
535 "Find and obey the last filename-and-line marker from the debugger.
536 Obeying it means displaying in another window the specified file and line."
537 (interactive)
538 (if gud-last-frame
539 (progn
540 (gud-set-buffer)
541 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
542 (setq gud-last-frame nil))))
543
544 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
545 ;; and that its line LINE is visible.
546 ;; Put the overlay-arrow on the line LINE in that buffer.
547 ;; Most of the trickiness in here comes from wanting to preserve the current
548 ;; region-restriction if that's possible. We use an explicit display-buffer
549 ;; to get around the fact that this is called inside a save-excursion.
550
551 (defun gud-display-line (true-file line)
552 (let* ((buffer (gud-find-file true-file))
553 (window (display-buffer buffer))
554 (pos))
555 (if (equal buffer (current-buffer))
556 nil
557 (setq buffer-read-only nil))
558 (save-excursion
559 (set-buffer buffer)
560 (setq buffer-read-only t)
561 (save-restriction
562 (widen)
563 (goto-line line)
564 (setq pos (point))
565 (setq overlay-arrow-string "=>")
566 (or overlay-arrow-position
567 (setq overlay-arrow-position (make-marker)))
568 (set-marker overlay-arrow-position (point) (current-buffer)))
569 (cond ((or (< pos (point-min)) (> pos (point-max)))
570 (widen)
571 (goto-char pos))))
572 (set-window-point window overlay-arrow-position)))
573
574 ;;; The gud-call function must do the right thing whether its invoking
575 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
576 ;;; or a C buffer. In the former case, we want to supply data from
577 ;;; gud-last-frame. Here's how we do it:
578
579 (defun gud-format-command (str arg)
580 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
581 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
582 (progn
583 (setq str (concat
584 (substring str (match-beginning 1) (match-end 1))
585 (file-name-nondirectory (if insource
586 (buffer-file-name)
587 (car gud-last-frame)))
588 (substring str (match-beginning 2) (match-end 2))))))
589 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
590 (progn
591 (setq str (concat
592 (substring str (match-beginning 1) (match-end 1))
593 (if insource
594 (save-excursion
595 (beginning-of-line)
596 (save-restriction (widen)
597 (1+ (count-lines 1 (point)))))
598 (cdr gud-last-frame))
599 (substring str (match-beginning 2) (match-end 2))))))
600 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
601 (progn
602 (setq str (concat
603 (substring str (match-beginning 1) (match-end 1))
604 (find-c-expr)
605 (substring str (match-beginning 2) (match-end 2))))))
606 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
607 (progn
608 (setq str (concat
609 (substring str (match-beginning 1) (match-end 1))
610 (gud-read-address)
611 (substring str (match-beginning 2) (match-end 2))))))
612 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
613 (progn
614 (setq str (concat
615 (substring str (match-beginning 1) (match-end 1))
616 (if arg (int-to-string arg) "")
617 (substring str (match-beginning 2) (match-end 2))))))
618 )
619 str
620 )
621
622 (defun gud-read-address ()
623 "Return a string containing the core-address found in the buffer at point."
624 (save-excursion
625 (let ((pt (point)) found begin)
626 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
627 (cond
628 (found (forward-char 2)
629 (buffer-substring found
630 (progn (re-search-forward "[^0-9a-f]")
631 (forward-char -1)
632 (point))))
633 (t (setq begin (progn (re-search-backward "[^0-9]")
634 (forward-char 1)
635 (point)))
636 (forward-char 1)
637 (re-search-forward "[^0-9]")
638 (forward-char -1)
639 (buffer-substring begin (point)))))))
640
641 (defun gud-call (fmt &optional arg)
642 (let ((msg (gud-format-command fmt arg)))
643 (message "Command: %s" msg)
644 (sit-for 0)
645 (gud-basic-call msg)))
646
647 (defun gud-basic-call (command)
648 "Invoke the debugger COMMAND displaying source in other window."
649 (interactive)
650 (gud-set-buffer)
651 (let ((command (concat command "\n"))
652 (proc (get-buffer-process gud-comint-buffer)))
653
654 ;; Arrange for the current prompt to get deleted.
655 (save-excursion
656 (set-buffer gud-comint-buffer)
657 (goto-char (process-mark proc))
658 (beginning-of-line)
659 (if (looking-at comint-prompt-regexp)
660 (set-marker gud-delete-prompt-marker (point))))
661 (process-send-string proc command)))
662
663 (defun gud-refresh (&optional arg)
664 "Fix up a possibly garbled display, and redraw the arrow."
665 (interactive "P")
666 (recenter arg)
667 (gud-display-frame))
668
669 ;;; Code for parsing expressions out of C code. The single entry point is
670 ;;; find-c-expr, which tries to return an lvalue expression from around point.
671 ;;;
672 ;;; The rest of this file is a hacked version of gdbsrc.el by
673 ;;; Debby Ayers <ayers@asc.slb.com>,
674 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
675 ;;; ??? We're waiting on papers from these people
676
677 (defun find-c-expr ()
678 "Returns the C expr that surrounds point."
679 (interactive)
680 (save-excursion
681 (let ((p) (expr) (test-expr))
682 (setq p (point))
683 (setq expr (expr-cur))
684 (setq test-expr (expr-prev))
685 (while (expr-compound test-expr expr)
686 (setq expr (cons (car test-expr) (cdr expr)))
687 (goto-char (car expr))
688 (setq test-expr (expr-prev))
689 )
690 (goto-char p)
691 (setq test-expr (expr-next))
692 (while (expr-compound expr test-expr)
693 (setq expr (cons (car expr) (cdr test-expr)))
694 (setq test-expr (expr-next))
695 )
696 (buffer-substring (car expr) (cdr expr))
697 )
698 )
699 )
700
701 (defun expr-cur ()
702 "Returns the expr that point is in; point is set to beginning of expr.
703 The expr is represented as a cons cell, where the car specifies the point in
704 the current buffer that marks the beginning of the expr and the cdr specifies
705 the character after the end of the expr"
706 (let ((p (point)) (begin) (end))
707 (back-expr)
708 (setq begin (point))
709 (forw-expr)
710 (setq end (point))
711 (if (>= p end)
712 (progn
713 (setq begin p)
714 (goto-char p)
715 (forw-expr)
716 (setq end (point))
717 )
718 )
719 (goto-char begin)
720 (cons begin end)
721 )
722 )
723
724 (defun back-expr ()
725 "Version of backward-sexp that catches errors"
726 (condition-case nil
727 (backward-sexp)
728 (error t)))
729
730 (defun forw-expr ()
731 "Version of forward-sexp that catches errors"
732 (condition-case nil
733 (forward-sexp)
734 (error t)))
735
736 (defun expr-prev ()
737 "Returns the previous expr, point is set to beginning of that expr.
738 The expr is represented as a cons cell, where the car specifies the point in
739 the current buffer that marks the beginning of the expr and the cdr specifies
740 the character after the end of the expr"
741 (let ((begin) (end))
742 (back-expr)
743 (setq begin (point))
744 (forw-expr)
745 (setq end (point))
746 (goto-char begin)
747 (cons begin end)))
748
749 (defun expr-next ()
750 "Returns the following expr, point is set to beginning of that expr.
751 The expr is represented as a cons cell, where the car specifies the point in
752 the current buffer that marks the beginning of the expr and the cdr specifies
753 the character after the end of the expr"
754 (let ((begin) (end))
755 (forw-expr)
756 (forw-expr)
757 (setq end (point))
758 (back-expr)
759 (setq begin (point))
760 (cons begin end)
761 )
762 )
763
764 (defun expr-compound-sep (span-start span-end)
765 "Returns '.' for '->' & '.', returns ' ' for white space,
766 returns '?' for other puctuation."
767 (let ((result ? )
768 (syntax))
769 (while (< span-start span-end)
770 (setq syntax (char-syntax (char-after span-start)))
771 (cond
772 ((= syntax ? ) t)
773 ((= syntax ?.) (setq syntax (char-after span-start))
774 (cond
775 ((= syntax ?.) (setq result ?.))
776 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
777 (setq result ?.)
778 (setq span-start (+ span-start 1)))
779 (t (setq span-start span-end)
780 (setq result ??)))))
781 (setq span-start (+ span-start 1)))
782 result
783 )
784 )
785
786 (defun expr-compound (first second)
787 "Returns non-nil if the concatenation of two exprs results in a single C
788 token. The two exprs are represented as a cons cells, where the car
789 specifies the point in the current buffer that marks the beginning of the
790 expr and the cdr specifies the character after the end of the expr
791 Link exprs of the form:
792 Expr -> Expr
793 Expr . Expr
794 Expr (Expr)
795 Expr [Expr]
796 (Expr) Expr
797 [Expr] Expr"
798 (let ((span-start (cdr first))
799 (span-end (car second))
800 (syntax))
801 (setq syntax (expr-compound-sep span-start span-end))
802 (cond
803 ((= (car first) (car second)) nil)
804 ((= (cdr first) (cdr second)) nil)
805 ((= syntax ?.) t)
806 ((= syntax ? )
807 (setq span-start (char-after (- span-start 1)))
808 (setq span-end (char-after span-end))
809 (cond
810 ((= span-start ?) ) t )
811 ((= span-start ?] ) t )
812 ((= span-end ?( ) t )
813 ((= span-end ?[ ) t )
814 (t nil))
815 )
816 (t nil))
817 )
818 )
819
820 ;;; There appears to be a bug in the byte compiler somewhere near macro
821 ;;; handling that (a) generates a spurious message about gud-key-prefix
822 ;;; when the global-set-key clause in gud-def is compiled, (b) generates
823 ;;; incorrect bytecode for gud-def. The symptom of this incorrectness
824 ;;; is that loading gud.elc brings in a compiled gud-def that doesn't
825 ;;; properly perform both global (C-x C-a) and local (C-c) bindings.
826 ;;; The workaround is to always load from source. Consequently, we try
827 ;;; to disable byte-compilation here.
828 ;;;
829 ;;; Local Variables:
830 ;;; no-byte-compile: t
831 ;;; End:
832
833 ;;; gud.el ends here