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