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