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