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