(archive-remote-regexp): Doc fix.
[emacs.git] / lisp / gud.el
blobd475e9be36225cc6159963cfadc7d1b545a5e3b8
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
2 ;;; under Emacs
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Maintainer: FSF
6 ;; Keywords: unix, tools
8 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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. Brian D. Carlstrom <bdc@ai.mit.edu> combined the IRIX kluge with
37 ;; the gud-xdb-directories hack producing gud-dbx-directories.
39 ;;; Code:
41 (require 'comint)
42 (require 'etags)
44 ;; ======================================================================
45 ;; GUD commands must be visible in C buffers visited by GUD
47 (defvar gud-key-prefix "\C-x\C-a"
48 "Prefix of all GUD commands valid in C buffers.")
50 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
51 (define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
53 (defvar gud-marker-filter nil)
54 (put 'gud-marker-filter 'permanent-local t)
55 (defvar gud-find-file nil)
56 (put 'gud-find-file 'permanent-local t)
58 (defun gud-marker-filter (&rest args)
59 (apply gud-marker-filter args))
61 (defun gud-find-file (file)
62 ;; Don't get confused by double slashes in the name that comes from GDB.
63 (while (string-match "//+" file)
64 (setq file (replace-match "/" t t file)))
65 (funcall gud-find-file file))
67 ;; Keymap definitions for menu bar entries common to all debuggers and
68 ;; slots for debugger-dependent ones in sensible places. (Defined here
69 ;; before use.)
70 (defvar gud-menu-map (make-sparse-keymap "Gud") nil)
71 (define-key gud-menu-map [refresh] '("Refresh" . gud-refresh))
72 (define-key gud-menu-map [remove] '("Remove Breakpoint" . gud-remove))
73 (define-key gud-menu-map [tbreak] nil) ; gdb, sdb and xdb
74 (define-key gud-menu-map [break] '("Set Breakpoint" . gud-break))
75 (define-key gud-menu-map [up] nil) ; gdb, dbx, and xdb
76 (define-key gud-menu-map [down] nil) ; gdb, dbx, and xdb
77 (define-key gud-menu-map [print] '("Print Expression" . gud-print))
78 (define-key gud-menu-map [finish] nil) ; gdb or xdb
79 (define-key gud-menu-map [stepi] '("Step Instruction" . gud-stepi))
80 (define-key gud-menu-map [step] '("Step Line" . gud-step))
81 (define-key gud-menu-map [next] '("Next Line" . gud-next))
82 (define-key gud-menu-map [cont] '("Continue" . gud-cont))
84 ;; ======================================================================
85 ;; command definition
87 ;; This macro is used below to define some basic debugger interface commands.
88 ;; Of course you may use `gud-def' with any other debugger command, including
89 ;; user defined ones.
91 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
92 ;; which defines FUNC to send the command NAME to the debugger, gives
93 ;; it the docstring DOC, and binds that function to KEY in the GUD
94 ;; major mode. The function is also bound in the global keymap with the
95 ;; GUD prefix.
97 (defmacro gud-def (func cmd key &optional doc)
98 "Define FUNC to be a command sending STR and bound to KEY, with
99 optional doc string DOC. Certain %-escapes in the string arguments
100 are interpreted specially if present. These are:
102 %f name (without directory) of current source file.
103 %d directory of current source file.
104 %l number of current source line
105 %e text of the C lvalue or function-call expression surrounding point.
106 %a text of the hexadecimal address surrounding point
107 %p prefix argument to the command (if any) as a number
109 The `current' source file is the file of the current buffer (if
110 we're in a C file) or the source file current at the last break or
111 step (if we're in the GUD buffer).
112 The `current' line is that of the current buffer (if we're in a
113 source file) or the source line number at the last break or step (if
114 we're in the GUD buffer)."
115 (list 'progn
116 (list 'defun func '(arg)
117 (or doc "")
118 '(interactive "p")
119 (list 'gud-call cmd 'arg))
120 (if key
121 (list 'define-key
122 '(current-local-map)
123 (concat "\C-c" key)
124 (list 'quote func)))
125 (if key
126 (list 'global-set-key
127 (list 'concat 'gud-key-prefix key)
128 (list 'quote func)))))
130 ;; Where gud-display-frame should put the debugging arrow. This is
131 ;; set by the marker-filter, which scans the debugger's output for
132 ;; indications of the current program counter.
133 (defvar gud-last-frame nil)
135 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
136 ;; the last frame, even if it's been called before and gud-last-frame has
137 ;; been set to nil.
138 (defvar gud-last-last-frame nil)
140 ;; All debugger-specific information is collected here.
141 ;; Here's how it works, in case you ever need to add a debugger to the mode.
143 ;; Each entry must define the following at startup:
145 ;;<name>
146 ;; comint-prompt-regexp
147 ;; gud-<name>-massage-args
148 ;; gud-<name>-marker-filter
149 ;; gud-<name>-find-file
151 ;; The job of the massage-args method is to modify the given list of
152 ;; debugger arguments before running the debugger.
154 ;; The job of the marker-filter method is to detect file/line markers in
155 ;; strings and set the global gud-last-frame to indicate what display
156 ;; action (if any) should be triggered by the marker. Note that only
157 ;; whatever the method *returns* is displayed in the buffer; thus, you
158 ;; can filter the debugger's output, interpreting some and passing on
159 ;; the rest.
161 ;; The job of the find-file method is to visit and return the buffer indicated
162 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
163 ;; something else. It would be good if it also copied the Gud menubar entry.
165 ;; ======================================================================
166 ;; gdb functions
168 ;;; History of argument lists passed to gdb.
169 (defvar gud-gdb-history nil)
171 (defun gud-gdb-massage-args (file args)
172 (cons "-fullname" args))
174 (defvar gud-gdb-marker-regexp
175 (concat "\032\032\\([^" path-separator "\n]*\\)" path-separator
176 "\\([0-9]*\\)" path-separator ".*\n"))
178 ;; There's no guarantee that Emacs will hand the filter the entire
179 ;; marker at once; it could be broken up across several strings. We
180 ;; might even receive a big chunk with several markers in it. If we
181 ;; receive a chunk of text which looks like it might contain the
182 ;; beginning of a marker, we save it here between calls to the
183 ;; filter.
184 (defvar gud-marker-acc "")
185 (make-variable-buffer-local 'gud-marker-acc)
187 (defun gud-gdb-marker-filter (string)
188 (setq gud-marker-acc (concat gud-marker-acc string))
189 (let ((output ""))
191 ;; Process all the complete markers in this chunk.
192 (while (string-match gud-gdb-marker-regexp gud-marker-acc)
193 (setq
195 ;; Extract the frame position from the marker.
196 gud-last-frame
197 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
198 (string-to-int (substring gud-marker-acc
199 (match-beginning 2)
200 (match-end 2))))
202 ;; Append any text before the marker to the output we're going
203 ;; to return - we don't include the marker in this text.
204 output (concat output
205 (substring gud-marker-acc 0 (match-beginning 0)))
207 ;; Set the accumulator to the remaining text.
208 gud-marker-acc (substring gud-marker-acc (match-end 0))))
210 ;; Does the remaining text look like it might end with the
211 ;; beginning of another marker? If it does, then keep it in
212 ;; gud-marker-acc until we receive the rest of it. Since we
213 ;; know the full marker regexp above failed, it's pretty simple to
214 ;; test for marker starts.
215 (if (string-match "\032.*\\'" gud-marker-acc)
216 (progn
217 ;; Everything before the potential marker start can be output.
218 (setq output (concat output (substring gud-marker-acc
219 0 (match-beginning 0))))
221 ;; Everything after, we save, to combine with later input.
222 (setq gud-marker-acc
223 (substring gud-marker-acc (match-beginning 0))))
225 (setq output (concat output gud-marker-acc)
226 gud-marker-acc ""))
228 output))
230 (defun gud-new-keymap (map)
231 "Return a new keymap which inherits from MAP and has name `Gud'."
232 (nconc (make-sparse-keymap "Gud") map))
234 (defun gud-make-debug-menu ()
235 "Make sure the current local map has a [menu-bar debug] submap.
236 If it doesn't, replace it with a new map that inherits it,
237 and create such a submap in that new map."
238 (if (and (current-local-map)
239 (lookup-key (current-local-map) [menu-bar debug]))
241 (use-local-map (gud-new-keymap (current-local-map)))
242 (define-key (current-local-map) [menu-bar debug]
243 (cons "Gud" (gud-new-keymap gud-menu-map)))))
245 (defun gud-gdb-find-file (f)
246 (save-excursion
247 (let ((buf (find-file-noselect f)))
248 (set-buffer buf)
249 (gud-make-debug-menu)
250 (local-set-key [menu-bar debug tbreak]
251 '("Temporary Breakpoint" . gud-tbreak))
252 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
253 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
254 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
255 buf)))
257 (defvar gdb-minibuffer-local-map nil
258 "Keymap for minibuffer prompting of gdb startup command.")
259 (if gdb-minibuffer-local-map
261 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
262 (define-key
263 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
265 ;;;###autoload
266 (defun gdb (command-line)
267 "Run gdb on program FILE in buffer *gud-FILE*.
268 The directory containing FILE becomes the initial working directory
269 and source-file directory for your debugger."
270 (interactive
271 (list (read-from-minibuffer "Run gdb (like this): "
272 (if (consp gud-gdb-history)
273 (car gud-gdb-history)
274 "gdb ")
275 gdb-minibuffer-local-map nil
276 '(gud-gdb-history . 1))))
278 (gud-common-init command-line 'gud-gdb-massage-args
279 'gud-gdb-marker-filter 'gud-gdb-find-file)
281 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
282 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
283 (gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
284 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
285 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
286 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
287 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
288 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
289 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
290 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
291 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
293 (local-set-key "\C-i" 'gud-gdb-complete-command)
294 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
295 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
296 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
297 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
298 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
299 (setq paragraph-start comint-prompt-regexp)
300 (run-hooks 'gdb-mode-hook)
303 ;; One of the nice features of GDB is its impressive support for
304 ;; context-sensitive command completion. We preserve that feature
305 ;; in the GUD buffer by using a GDB command designed just for Emacs.
307 ;; The completion process filter indicates when it is finished.
308 (defvar gud-gdb-complete-in-progress)
310 ;; Since output may arrive in fragments we accumulate partials strings here.
311 (defvar gud-gdb-complete-string)
313 ;; We need to know how much of the completion to chop off.
314 (defvar gud-gdb-complete-break)
316 ;; The completion list is constructed by the process filter.
317 (defvar gud-gdb-complete-list)
319 (defvar gud-comint-buffer nil)
321 (defun gud-gdb-complete-command ()
322 "Perform completion on the GDB command preceding point.
323 This is implemented using the GDB `complete' command which isn't
324 available with older versions of GDB."
325 (interactive)
326 (let* ((end (point))
327 (command (save-excursion
328 (beginning-of-line)
329 (and (looking-at comint-prompt-regexp)
330 (goto-char (match-end 0)))
331 (buffer-substring (point) end)))
332 command-word)
333 ;; Find the word break. This match will always succeed.
334 (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
335 (setq gud-gdb-complete-break (match-beginning 2)
336 command-word (substring command gud-gdb-complete-break))
337 ;; Temporarily install our filter function.
338 (let ((gud-marker-filter 'gud-gdb-complete-filter))
339 ;; Issue the command to GDB.
340 (gud-basic-call (concat "complete " command))
341 (setq gud-gdb-complete-in-progress t
342 gud-gdb-complete-string nil
343 gud-gdb-complete-list nil)
344 ;; Slurp the output.
345 (while gud-gdb-complete-in-progress
346 (accept-process-output (get-buffer-process gud-comint-buffer))))
347 ;; Protect against old versions of GDB.
348 (and gud-gdb-complete-list
349 (string-match "^Undefined command: \"complete\""
350 (car gud-gdb-complete-list))
351 (error "This version of GDB doesn't support the `complete' command."))
352 ;; Sort the list like readline.
353 (setq gud-gdb-complete-list
354 (sort gud-gdb-complete-list (function string-lessp)))
355 ;; Remove duplicates.
356 (let ((first gud-gdb-complete-list)
357 (second (cdr gud-gdb-complete-list)))
358 (while second
359 (if (string-equal (car first) (car second))
360 (setcdr first (setq second (cdr second)))
361 (setq first second
362 second (cdr second)))))
363 ;; Add a trailing single quote if there is a unique completion
364 ;; and it contains an odd number of unquoted single quotes.
365 (and (= (length gud-gdb-complete-list) 1)
366 (let ((str (car gud-gdb-complete-list))
367 (pos 0)
368 (count 0))
369 (while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
370 (setq count (1+ count)
371 pos (match-end 0)))
372 (and (= (mod count 2) 1)
373 (setq gud-gdb-complete-list (list (concat str "'"))))))
374 ;; Let comint handle the rest.
375 (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
377 ;; The completion process filter is installed temporarily to slurp the
378 ;; output of GDB up to the next prompt and build the completion list.
379 (defun gud-gdb-complete-filter (string)
380 (setq string (concat gud-gdb-complete-string string))
381 (while (string-match "\n" string)
382 (setq gud-gdb-complete-list
383 (cons (substring string gud-gdb-complete-break (match-beginning 0))
384 gud-gdb-complete-list))
385 (setq string (substring string (match-end 0))))
386 (if (string-match comint-prompt-regexp string)
387 (progn
388 (setq gud-gdb-complete-in-progress nil)
389 string)
390 (progn
391 (setq gud-gdb-complete-string string)
392 "")))
395 ;; ======================================================================
396 ;; sdb functions
398 ;;; History of argument lists passed to sdb.
399 (defvar gud-sdb-history nil)
401 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
402 "If nil, we're on a System V Release 4 and don't need the tags hack.")
404 (defvar gud-sdb-lastfile nil)
406 (defun gud-sdb-massage-args (file args) args)
408 (defun gud-sdb-marker-filter (string)
409 (setq gud-marker-acc
410 (if gud-marker-acc (concat gud-marker-acc string) string))
411 (let (start)
412 ;; Process all complete markers in this chunk
413 (while
414 (cond
415 ;; System V Release 3.2 uses this format
416 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
417 gud-marker-acc start)
418 (setq gud-last-frame
419 (cons
420 (substring gud-marker-acc (match-beginning 2) (match-end 2))
421 (string-to-int
422 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
423 ;; System V Release 4.0 quite often clumps two lines together
424 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n\\([0-9]+\\):"
425 gud-marker-acc start)
426 (setq gud-sdb-lastfile
427 (substring gud-marker-acc (match-beginning 2) (match-end 2)))
428 (setq gud-last-frame
429 (cons
430 gud-sdb-lastfile
431 (string-to-int
432 (substring gud-marker-acc (match-beginning 3) (match-end 3))))))
433 ;; System V Release 4.0
434 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
435 gud-marker-acc start)
436 (setq gud-sdb-lastfile
437 (substring gud-marker-acc (match-beginning 2) (match-end 2))))
438 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):"
439 gud-marker-acc start))
440 (setq gud-last-frame
441 (cons
442 gud-sdb-lastfile
443 (string-to-int
444 (substring gud-marker-acc (match-beginning 1) (match-end 1))))))
446 (setq gud-sdb-lastfile nil)))
447 (setq start (match-end 0)))
449 ;; Search for the last incomplete line in this chunk
450 (while (string-match "\n" gud-marker-acc start)
451 (setq start (match-end 0)))
453 ;; If we have an incomplete line, store it in gud-marker-acc.
454 ;; Otherwise clear gud-marker-acc. to avoid an
455 ;; unnecessary concat when this function runs next.
456 (setq gud-marker-acc
457 (if (= start (length gud-marker-acc))
458 (substring gud-marker-acc start)
459 nil)))
460 string)
462 (defun gud-sdb-find-file (f)
463 (save-excursion
464 (let ((buf (if gud-sdb-needs-tags
465 (find-tag-noselect f)
466 (find-file-noselect f))))
467 (set-buffer buf)
468 (gud-make-debug-menu)
469 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
470 buf)))
472 ;;;###autoload
473 (defun sdb (command-line)
474 "Run sdb on program FILE in buffer *gud-FILE*.
475 The directory containing FILE becomes the initial working directory
476 and source-file directory for your debugger."
477 (interactive
478 (list (read-from-minibuffer "Run sdb (like this): "
479 (if (consp gud-sdb-history)
480 (car gud-sdb-history)
481 "sdb ")
482 nil nil
483 '(gud-sdb-history . 1))))
484 (if (and gud-sdb-needs-tags
485 (not (and (boundp 'tags-file-name)
486 (stringp tags-file-name)
487 (file-exists-p tags-file-name))))
488 (error "The sdb support requires a valid tags table to work."))
490 (gud-common-init command-line 'gud-sdb-massage-args
491 'gud-sdb-marker-filter 'gud-sdb-find-file)
493 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
494 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
495 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
496 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
497 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
498 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
499 (gud-def gud-cont "c" "\C-r" "Continue with display.")
500 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
502 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
503 (setq paragraph-start comint-prompt-regexp)
504 (local-set-key [menu-bar debug tbreak]
505 '("Temporary Breakpoint" . gud-tbreak))
506 (run-hooks 'sdb-mode-hook)
509 ;; ======================================================================
510 ;; dbx functions
512 ;;; History of argument lists passed to dbx.
513 (defvar gud-dbx-history nil)
515 (defvar gud-dbx-directories nil
516 "*A list of directories that dbx should search for source code.
517 If nil, only source files in the program directory
518 will be known to dbx.
520 The file names should be absolute, or relative to the directory
521 containing the executable being debugged.")
523 (defun gud-dbx-massage-args (file args)
524 (nconc (let ((directories gud-dbx-directories)
525 (result nil))
526 (while directories
527 (setq result (cons (car directories) (cons "-I" result)))
528 (setq directories (cdr directories)))
529 (nreverse result))
530 args))
532 (defun gud-dbx-file-name (f)
533 "Transform a relative file name to an absolute file name, for dbx."
534 (let ((result nil))
535 (if (file-exists-p f)
536 (setq result (expand-file-name f))
537 (let ((directories gud-dbx-directories))
538 (while directories
539 (let ((path (concat (car directories) "/" f)))
540 (if (file-exists-p path)
541 (setq result (expand-file-name path)
542 directories nil)))
543 (setq directories (cdr directories)))))
544 result))
546 (defun gud-dbx-marker-filter (string)
547 (setq gud-marker-acc (if gud-marker-acc (concat gud-marker-acc string) string))
549 (let (start)
550 ;; Process all complete markers in this chunk.
551 (while (or (string-match
552 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
553 gud-marker-acc start)
554 (string-match
555 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
556 gud-marker-acc start))
557 (setq gud-last-frame
558 (cons
559 (substring gud-marker-acc (match-beginning 2) (match-end 2))
560 (string-to-int
561 (substring gud-marker-acc (match-beginning 1) (match-end 1))))
562 start (match-end 0)))
564 ;; Search for the last incomplete line in this chunk
565 (while (string-match "\n" gud-marker-acc start)
566 (setq start (match-end 0)))
568 ;; If the incomplete line APPEARS to begin with another marker, keep it
569 ;; in the accumulator. Otherwise, clear the accumulator to avoid an
570 ;; unnecessary concat during the next call.
571 (setq gud-marker-acc
572 (if (string-match "\\(stopped\\|signal\\)" gud-marker-acc start)
573 (substring gud-marker-acc (match-beginning 0))
574 nil)))
575 string)
577 ;; Functions for Mips-style dbx. Given the option `-emacs', documented in
578 ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
579 (defvar gud-mips-p
580 (or (string-match "^mips-[^-]*-ultrix" system-configuration)
581 ;; We haven't tested gud on this system:
582 (string-match "^mips-[^-]*-riscos" system-configuration)
583 ;; It's documented on OSF/1.3
584 (string-match "^mips-[^-]*-osf1" system-configuration)
585 (string-match "^alpha-[^-]*-osf" system-configuration))
586 "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
588 (defun gud-mipsdbx-massage-args (file args)
589 (cons "-emacs" args))
591 ;; This is just like the gdb one except for the regexps since we need to cope
592 ;; with an optional breakpoint number in [] before the ^Z^Z
593 (defun gud-mipsdbx-marker-filter (string)
594 (setq gud-marker-acc (concat gud-marker-acc string))
595 (let ((output ""))
597 ;; Process all the complete markers in this chunk.
598 (while (string-match
599 ;; This is like th gdb marker but with an optional
600 ;; leading break point number like `[1] '
601 "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
602 gud-marker-acc)
603 (setq
605 ;; Extract the frame position from the marker.
606 gud-last-frame
607 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
608 (string-to-int (substring gud-marker-acc
609 (match-beginning 2)
610 (match-end 2))))
612 ;; Append any text before the marker to the output we're going
613 ;; to return - we don't include the marker in this text.
614 output (concat output
615 (substring gud-marker-acc 0 (match-beginning 0)))
617 ;; Set the accumulator to the remaining text.
618 gud-marker-acc (substring gud-marker-acc (match-end 0))))
620 ;; Does the remaining text look like it might end with the
621 ;; beginning of another marker? If it does, then keep it in
622 ;; gud-marker-acc until we receive the rest of it. Since we
623 ;; know the full marker regexp above failed, it's pretty simple to
624 ;; test for marker starts.
625 (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
626 (progn
627 ;; Everything before the potential marker start can be output.
628 (setq output (concat output (substring gud-marker-acc
629 0 (match-beginning 0))))
631 ;; Everything after, we save, to combine with later input.
632 (setq gud-marker-acc
633 (substring gud-marker-acc (match-beginning 0))))
635 (setq output (concat output gud-marker-acc)
636 gud-marker-acc ""))
638 output))
640 ;; The dbx in IRIX is a pain. It doesn't print the file name when
641 ;; stopping at a breakpoint (but you do get it from the `up' and
642 ;; `down' commands...). The only way to extract the information seems
643 ;; to be with a `file' command, although the current line number is
644 ;; available in $curline. Thus we have to look for output which
645 ;; appears to indicate a breakpoint. Then we prod the dbx sub-process
646 ;; to output the information we want with a combination of the
647 ;; `printf' and `file' commands as a pseudo marker which we can
648 ;; recognise next time through the marker-filter. This would be like
649 ;; the gdb marker but you can't get the file name without a newline...
650 ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
651 ;; number rather than a line number etc. Maybe this could be made to
652 ;; work by listing all the breakpoints and picking the one(s) with the
653 ;; correct line number, but life's too short.
654 ;; d.love@dl.ac.uk (Dave Love) can be blamed for this
656 (defvar gud-irix-p (string-match "^mips-[^-]*-irix" system-configuration)
657 "Non-nil to assume the interface appropriate for IRIX dbx.
658 This works in IRIX 4, 5 and 6.")
659 ;; [Irix dbx seems to be a moving target. The dbx output changed
660 ;; subtly sometime between OS v4.0.5 and v5.2 so that, for instance,
661 ;; the output from `up' is no longer spotted by gud (and it's probably
662 ;; not distinctive enough to try to match it -- use C-<, C->
663 ;; exclusively) . For 5.3 and 6.0, the $curline variable changed to
664 ;; `long long'(why?!), so the printf stuff needed changing. The line
665 ;; number is cast to `long' as a compromise between the new `long
666 ;; long' and the original `int'. The process filter is also somewhat
667 ;; unreliable, sometimes not spotting the markers; I don't know
668 ;; whether there's anything that can be done about that. It would be
669 ;; much better if SGI could be persuaded to (re?)instate the MIPS
670 ;; -emacs flag for gdb-like output (which ought to be possible as most
671 ;; of the communication I've had over it has been from sgi.com).]
673 ;; this filter is influenced by the xdb one rather than the gdb one
674 (defun gud-irixdbx-marker-filter (string)
675 (let (result (case-fold-search nil))
676 (if (or (string-match comint-prompt-regexp string)
677 (string-match ".*\012" string))
678 (setq result (concat gud-marker-acc string)
679 gud-marker-acc "")
680 (setq gud-marker-acc (concat gud-marker-acc string)))
681 (if result
682 (cond
683 ;; look for breakpoint or signal indication e.g.:
684 ;; [2] Process 1267 (pplot) stopped at [params:338 ,0x400ec0]
685 ;; Process 1281 (pplot) stopped at [params:339 ,0x400ec8]
686 ;; Process 1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
687 ((string-match
688 "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n"
689 result)
690 ;; prod dbx into printing out the line number and file
691 ;; name in a form we can grok as below
692 (process-send-string (get-buffer-process gud-comint-buffer)
693 "printf \"\032\032%1d:\",(long)$curline;file\n"))
694 ;; look for result of, say, "up" e.g.:
695 ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
696 ;; (this will also catch one of the lines printed by "where")
697 ((string-match
698 "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
699 result)
700 (let ((file (substring result (match-beginning 1)
701 (match-end 1))))
702 (if (file-exists-p file)
703 (setq gud-last-frame
704 (cons
705 (substring
706 result (match-beginning 1) (match-end 1))
707 (string-to-int
708 (substring
709 result (match-beginning 2) (match-end 2)))))))
710 result)
711 ((string-match ; kluged-up marker as above
712 "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
713 (let ((file (gud-dbx-file-name
714 (substring result (match-beginning 2) (match-end 2)))))
715 (if (and file (file-exists-p file))
716 (setq gud-last-frame
717 (cons
718 file
719 (string-to-int
720 (substring
721 result (match-beginning 1) (match-end 1)))))))
722 (setq result (substring result 0 (match-beginning 0))))))
723 (or result "")))
725 (defun gud-dbx-find-file (f)
726 (save-excursion
727 (let ((realf (gud-dbx-file-name f)))
728 (if realf
729 (let ((buf (find-file-noselect f)))
730 (set-buffer buf)
731 (gud-make-debug-menu)
732 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
733 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
734 buf)
735 nil))))
737 ;;;###autoload
738 (defun dbx (command-line)
739 "Run dbx on program FILE in buffer *gud-FILE*.
740 The directory containing FILE becomes the initial working directory
741 and source-file directory for your debugger."
742 (interactive
743 (list (read-from-minibuffer "Run dbx (like this): "
744 (if (consp gud-dbx-history)
745 (car gud-dbx-history)
746 "dbx ")
747 nil nil
748 '(gud-dbx-history . 1))))
750 (cond
751 (gud-mips-p
752 (gud-common-init command-line 'gud-mipsdbx-massage-args
753 'gud-mipsdbx-marker-filter 'gud-dbx-find-file))
754 (gud-irix-p
755 (gud-common-init command-line 'gud-dbx-massage-args
756 'gud-irixdbx-marker-filter 'gud-dbx-find-file))
758 (gud-common-init command-line 'gud-dbx-massage-args
759 'gud-dbx-marker-filter 'gud-dbx-find-file)))
761 (cond
762 (gud-mips-p
763 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
764 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
765 (gud-def gud-break "stop at \"%f\":%l"
766 "\C-b" "Set breakpoint at current line.")
767 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
768 (gud-irix-p
769 (gud-def gud-break "stop at \"%d%f\":%l"
770 "\C-b" "Set breakpoint at current line.")
771 (gud-def gud-finish "return" "\C-f" "Finish executing current function.")
772 (gud-def gud-up "up %p; printf \"\032\032%1ld:\",(long)$curline;file\n"
773 "<" "Up (numeric arg) stack frames.")
774 (gud-def gud-down "down %p; printf \"\032\032%1ld:\",(long)$curline;file\n"
775 ">" "Down (numeric arg) stack frames.")
776 ;; Make dbx give out the source location info that we need.
777 (process-send-string (get-buffer-process gud-comint-buffer)
778 "printf \"\032\032%1d:\",(long)$curline;file\n"))
780 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
781 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
782 (gud-def gud-break "file \"%d%f\"\nstop at %l"
783 "\C-b" "Set breakpoint at current line.")))
785 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
786 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
787 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
788 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
789 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
790 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
792 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
793 (setq paragraph-start comint-prompt-regexp)
794 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
795 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
796 (run-hooks 'dbx-mode-hook)
799 ;; ======================================================================
800 ;; xdb (HP PARISC debugger) functions
802 ;;; History of argument lists passed to xdb.
803 (defvar gud-xdb-history nil)
805 (defvar gud-xdb-directories nil
806 "*A list of directories that xdb should search for source code.
807 If nil, only source files in the program directory
808 will be known to xdb.
810 The file names should be absolute, or relative to the directory
811 containing the executable being debugged.")
813 (defun gud-xdb-massage-args (file args)
814 (nconc (let ((directories gud-xdb-directories)
815 (result nil))
816 (while directories
817 (setq result (cons (car directories) (cons "-d" result)))
818 (setq directories (cdr directories)))
819 (nreverse result))
820 args))
822 (defun gud-xdb-file-name (f)
823 "Transform a relative pathname to a full pathname in xdb mode"
824 (let ((result nil))
825 (if (file-exists-p f)
826 (setq result (expand-file-name f))
827 (let ((directories gud-xdb-directories))
828 (while directories
829 (let ((path (concat (car directories) "/" f)))
830 (if (file-exists-p path)
831 (setq result (expand-file-name path)
832 directories nil)))
833 (setq directories (cdr directories)))))
834 result))
836 ;; xdb does not print the lines all at once, so we have to accumulate them
837 (defun gud-xdb-marker-filter (string)
838 (let (result)
839 (if (or (string-match comint-prompt-regexp string)
840 (string-match ".*\012" string))
841 (setq result (concat gud-marker-acc string)
842 gud-marker-acc "")
843 (setq gud-marker-acc (concat gud-marker-acc string)))
844 (if result
845 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\)[: ]"
846 result)
847 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
848 result))
849 (let ((line (string-to-int
850 (substring result (match-beginning 2) (match-end 2))))
851 (file (gud-xdb-file-name
852 (substring result (match-beginning 1) (match-end 1)))))
853 (if file
854 (setq gud-last-frame (cons file line))))))
855 (or result "")))
857 (defun gud-xdb-find-file (f)
858 (save-excursion
859 (let ((realf (gud-xdb-file-name f)))
860 (if realf
861 (let ((buf (find-file-noselect realf)))
862 (set-buffer buf)
863 (gud-make-debug-menu)
864 (local-set-key [menu-bar debug tbreak]
865 '("Temporary Breakpoint" . gud-tbreak))
866 (local-set-key [menu-bar debug finish]
867 '("Finish Function" . gud-finish))
868 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
869 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
870 buf)
871 nil))))
873 ;;;###autoload
874 (defun xdb (command-line)
875 "Run xdb on program FILE in buffer *gud-FILE*.
876 The directory containing FILE becomes the initial working directory
877 and source-file directory for your debugger.
879 You can set the variable 'gud-xdb-directories' to a list of program source
880 directories if your program contains sources from more than one directory."
881 (interactive
882 (list (read-from-minibuffer "Run xdb (like this): "
883 (if (consp gud-xdb-history)
884 (car gud-xdb-history)
885 "xdb ")
886 nil nil
887 '(gud-xdb-history . 1))))
889 (gud-common-init command-line 'gud-xdb-massage-args
890 'gud-xdb-marker-filter 'gud-xdb-find-file)
892 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
893 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
894 "Set temporary breakpoint at current line.")
895 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
896 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
897 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
898 (gud-def gud-cont "c" "\C-r" "Continue with display.")
899 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
900 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
901 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
902 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
904 (setq comint-prompt-regexp "^>")
905 (setq paragraph-start comint-prompt-regexp)
906 (local-set-key [menu-bar debug tbreak] '("Temporary Breakpoint" . gud-tbreak))
907 (local-set-key [menu-bar debug finish] '("Finish Function" . gud-finish))
908 (local-set-key [menu-bar debug up] '("Up Stack" . gud-up))
909 (local-set-key [menu-bar debug down] '("Down Stack" . gud-down))
910 (run-hooks 'xdb-mode-hook))
912 ;; ======================================================================
913 ;; perldb functions
915 ;;; History of argument lists passed to perldb.
916 (defvar gud-perldb-history nil)
918 (defun gud-perldb-massage-args (file args)
919 (cons "-d" (cons (car args) (cons "-emacs" (cdr args)))))
921 ;; There's no guarantee that Emacs will hand the filter the entire
922 ;; marker at once; it could be broken up across several strings. We
923 ;; might even receive a big chunk with several markers in it. If we
924 ;; receive a chunk of text which looks like it might contain the
925 ;; beginning of a marker, we save it here between calls to the
926 ;; filter.
927 (defvar gud-perldb-marker-acc "")
929 (defun gud-perldb-marker-filter (string)
930 (setq gud-marker-acc (concat gud-marker-acc string))
931 (let ((output ""))
933 ;; Process all the complete markers in this chunk.
934 (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
935 gud-marker-acc)
936 (setq
938 ;; Extract the frame position from the marker.
939 gud-last-frame
940 (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
941 (string-to-int (substring gud-marker-acc
942 (match-beginning 2)
943 (match-end 2))))
945 ;; Append any text before the marker to the output we're going
946 ;; to return - we don't include the marker in this text.
947 output (concat output
948 (substring gud-marker-acc 0 (match-beginning 0)))
950 ;; Set the accumulator to the remaining text.
951 gud-marker-acc (substring gud-marker-acc (match-end 0))))
953 ;; Does the remaining text look like it might end with the
954 ;; beginning of another marker? If it does, then keep it in
955 ;; gud-marker-acc until we receive the rest of it. Since we
956 ;; know the full marker regexp above failed, it's pretty simple to
957 ;; test for marker starts.
958 (if (string-match "\032.*\\'" gud-marker-acc)
959 (progn
960 ;; Everything before the potential marker start can be output.
961 (setq output (concat output (substring gud-marker-acc
962 0 (match-beginning 0))))
964 ;; Everything after, we save, to combine with later input.
965 (setq gud-marker-acc
966 (substring gud-marker-acc (match-beginning 0))))
968 (setq output (concat output gud-marker-acc)
969 gud-marker-acc ""))
971 output))
973 (defun gud-perldb-find-file (f)
974 (save-excursion
975 (let ((buf (find-file-noselect f)))
976 (set-buffer buf)
977 (gud-make-debug-menu)
978 buf)))
980 ;;;###autoload
981 (defun perldb (command-line)
982 "Run perldb on program FILE in buffer *gud-FILE*.
983 The directory containing FILE becomes the initial working directory
984 and source-file directory for your debugger."
985 (interactive
986 (list (read-from-minibuffer "Run perldb (like this): "
987 (if (consp gud-perldb-history)
988 (car gud-perldb-history)
989 "perl ")
990 nil nil
991 '(gud-perldb-history . 1))))
993 (gud-common-init command-line 'gud-perldb-massage-args
994 'gud-perldb-marker-filter 'gud-perldb-find-file)
996 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
997 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
998 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
999 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
1000 (gud-def gud-cont "c" "\C-r" "Continue with display.")
1001 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
1002 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
1003 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
1004 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
1006 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
1007 (setq paragraph-start comint-prompt-regexp)
1008 (run-hooks 'perldb-mode-hook)
1012 ;; End of debugger-specific information
1016 ;;; When we send a command to the debugger via gud-call, it's annoying
1017 ;;; to see the command and the new prompt inserted into the debugger's
1018 ;;; buffer; we have other ways of knowing the command has completed.
1020 ;;; If the buffer looks like this:
1021 ;;; --------------------
1022 ;;; (gdb) set args foo bar
1023 ;;; (gdb) -!-
1024 ;;; --------------------
1025 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
1026 ;;; source file to set a breakpoint, we want the buffer to end up like
1027 ;;; this:
1028 ;;; --------------------
1029 ;;; (gdb) set args foo bar
1030 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
1031 ;;; (gdb) -!-
1032 ;;; --------------------
1033 ;;; Essentially, the old prompt is deleted, and the command's output
1034 ;;; and the new prompt take its place.
1036 ;;; Not echoing the command is easy enough; you send it directly using
1037 ;;; process-send-string, and it never enters the buffer. However,
1038 ;;; getting rid of the old prompt is trickier; you don't want to do it
1039 ;;; when you send the command, since that will result in an annoying
1040 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
1041 ;;; waits for a response from the debugger, and the new prompt is
1042 ;;; inserted. Instead, we'll wait until we actually get some output
1043 ;;; from the subprocess before we delete the prompt. If the command
1044 ;;; produced no output other than a new prompt, that prompt will most
1045 ;;; likely be in the first chunk of output received, so we will delete
1046 ;;; the prompt and then replace it with an identical one. If the
1047 ;;; command produces output, the prompt is moving anyway, so the
1048 ;;; flicker won't be annoying.
1050 ;;; So - when we want to delete the prompt upon receipt of the next
1051 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
1052 ;;; the start of the prompt; the process filter will notice this, and
1053 ;;; delete all text between it and the process output marker. If
1054 ;;; gud-delete-prompt-marker points nowhere, we leave the current
1055 ;;; prompt alone.
1056 (defvar gud-delete-prompt-marker nil)
1059 (defun gud-mode ()
1060 "Major mode for interacting with an inferior debugger process.
1062 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
1063 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
1064 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
1066 After startup, the following commands are available in both the GUD
1067 interaction buffer and any source buffer GUD visits due to a breakpoint stop
1068 or step operation:
1070 \\[gud-break] sets a breakpoint at the current file and line. In the
1071 GUD buffer, the current file and line are those of the last breakpoint or
1072 step. In a source buffer, they are the buffer's file and current line.
1074 \\[gud-remove] removes breakpoints on the current file and line.
1076 \\[gud-refresh] displays in the source window the last line referred to
1077 in the gud buffer.
1079 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
1080 step-one-line (not entering function calls), and step-one-instruction
1081 and then update the source window with the current file and position.
1082 \\[gud-cont] continues execution.
1084 \\[gud-print] tries to find the largest C lvalue or function-call expression
1085 around point, and sends it to the debugger for value display.
1087 The above commands are common to all supported debuggers except xdb which
1088 does not support stepping instructions.
1090 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
1091 except that the breakpoint is temporary; that is, it is removed when
1092 execution stops on it.
1094 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
1095 frame. \\[gud-down] drops back down through one.
1097 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
1098 the current function and stops.
1100 All the keystrokes above are accessible in the GUD buffer
1101 with the prefix C-c, and in all buffers through the prefix C-x C-a.
1103 All pre-defined functions for which the concept make sense repeat
1104 themselves the appropriate number of times if you give a prefix
1105 argument.
1107 You may use the `gud-def' macro in the initialization hook to define other
1108 commands.
1110 Other commands for interacting with the debugger process are inherited from
1111 comint mode, which see."
1112 (interactive)
1113 (comint-mode)
1114 (setq major-mode 'gud-mode)
1115 (setq mode-name "Debugger")
1116 (setq mode-line-process '(":%s"))
1117 (use-local-map comint-mode-map)
1118 (gud-make-debug-menu)
1119 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
1120 (make-local-variable 'gud-last-frame)
1121 (setq gud-last-frame nil)
1122 (make-local-variable 'comint-prompt-regexp)
1123 (make-local-variable 'paragraph-start)
1124 (make-local-variable 'gud-delete-prompt-marker)
1125 (setq gud-delete-prompt-marker (make-marker))
1126 (run-hooks 'gud-mode-hook))
1128 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
1129 (defun gud-chop-words (string)
1130 (let ((i 0) (beg 0)
1131 (len (length string))
1132 (words nil))
1133 (while (< i len)
1134 (if (memq (aref string i) '(?\t ? ))
1135 (progn
1136 (setq words (cons (substring string beg i) words)
1137 beg (1+ i))
1138 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
1139 (setq beg (1+ beg)))
1140 (setq i (1+ beg)))
1141 (setq i (1+ i))))
1142 (if (< beg len)
1143 (setq words (cons (substring string beg) words)))
1144 (nreverse words)))
1146 ;; Perform initializations common to all debuggers.
1147 ;; The first arg is the specified command line,
1148 ;; which starts with the program to debug.
1149 ;; The other three args specify the values to use
1150 ;; for local variables in the debugger buffer.
1151 (defun gud-common-init (command-line massage-args marker-filter find-file)
1152 (let* ((words (gud-chop-words command-line))
1153 (program (car words))
1154 ;; Extract the file name from WORDS
1155 ;; and put t in its place.
1156 ;; Later on we will put the modified file name arg back there.
1157 (file-word (let ((w (cdr words)))
1158 (while (and w (= ?- (aref (car w) 0)))
1159 (setq w (cdr w)))
1160 (and w
1161 (prog1 (car w)
1162 (setcar w t)))))
1163 (file-subst
1164 (and file-word (substitute-in-file-name file-word)))
1165 (args (cdr words))
1166 ;; If a directory was specified, expand the file name.
1167 ;; Otherwise, don't expand it, so GDB can use the PATH.
1168 ;; A file name without directory is literally valid
1169 ;; only if the file exists in ., and in that case,
1170 ;; omitting the expansion here has no visible effect.
1171 (file (and file-word
1172 (if (file-name-directory file-subst)
1173 (expand-file-name file-subst)
1174 file-subst)))
1175 (filepart (and file-word (concat "-" (file-name-nondirectory file)))))
1176 (switch-to-buffer (concat "*gud" filepart "*"))
1177 ;; Set default-directory to the file's directory.
1178 (and file-word
1179 ;; Don't set default-directory if no directory was specified.
1180 ;; In that case, either the file is found in the current directory,
1181 ;; in which case this setq is a no-op,
1182 ;; or it is found by searching PATH,
1183 ;; in which case we don't know what directory it was found in.
1184 (file-name-directory file)
1185 (setq default-directory (file-name-directory file)))
1186 (or (bolp) (newline))
1187 (insert "Current directory is " default-directory "\n")
1188 ;; Put the substituted and expanded file name back in its place.
1189 (let ((w args))
1190 (while (and w (not (eq (car w) t)))
1191 (setq w (cdr w)))
1192 (if w
1193 (setcar w file)))
1194 (apply 'make-comint (concat "gud" filepart) program nil
1195 (funcall massage-args file args)))
1196 ;; Since comint clobbered the mode, we don't set it until now.
1197 (gud-mode)
1198 (make-local-variable 'gud-marker-filter)
1199 (setq gud-marker-filter marker-filter)
1200 (make-local-variable 'gud-find-file)
1201 (setq gud-find-file find-file)
1203 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
1204 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
1205 (gud-set-buffer)
1208 (defun gud-set-buffer ()
1209 (cond ((eq major-mode 'gud-mode)
1210 (setq gud-comint-buffer (current-buffer)))))
1212 (defvar gud-filter-defer-flag nil
1213 "Non-nil means don't process anything from the debugger right now.
1214 It is saved for when this flag is not set.")
1216 (defvar gud-filter-pending-text nil
1217 "Non-nil means this is text that has been saved for later in `gud-filter'.")
1219 ;; These functions are responsible for inserting output from your debugger
1220 ;; into the buffer. The hard work is done by the method that is
1221 ;; the value of gud-marker-filter.
1223 (defun gud-filter (proc string)
1224 ;; Here's where the actual buffer insertion is done
1225 (let (output process-window)
1226 (if (buffer-name (process-buffer proc))
1227 (if gud-filter-defer-flag
1228 ;; If we can't process any text now,
1229 ;; save it for later.
1230 (setq gud-filter-pending-text
1231 (concat (or gud-filter-pending-text "") string))
1232 (save-excursion
1233 ;; If we have to ask a question during the processing,
1234 ;; defer any additional text that comes from the debugger
1235 ;; during that time.
1236 (let ((gud-filter-defer-flag t))
1237 ;; Process now any text we previously saved up.
1238 (if gud-filter-pending-text
1239 (setq string (concat gud-filter-pending-text string)
1240 gud-filter-pending-text nil))
1241 (set-buffer (process-buffer proc))
1242 ;; If we have been so requested, delete the debugger prompt.
1243 (if (marker-buffer gud-delete-prompt-marker)
1244 (progn
1245 (delete-region (process-mark proc) gud-delete-prompt-marker)
1246 (set-marker gud-delete-prompt-marker nil)))
1247 ;; Save the process output, checking for source file markers.
1248 (setq output (gud-marker-filter string))
1249 ;; Check for a filename-and-line number.
1250 ;; Don't display the specified file
1251 ;; unless (1) point is at or after the position where output appears
1252 ;; and (2) this buffer is on the screen.
1253 (setq process-window
1254 (and gud-last-frame
1255 (>= (point) (process-mark proc))
1256 (get-buffer-window (current-buffer)))))
1257 (if process-window
1258 (save-selected-window
1259 (select-window process-window)
1260 (gud-display-frame)))
1262 ;; Let the comint filter do the actual insertion.
1263 ;; That lets us inherit various comint features.
1264 (comint-output-filter proc output)
1266 ;; If we deferred text that arrived during this processing,
1267 ;; handle it now.
1268 (if gud-filter-pending-text
1269 (gud-filter proc "")))))))
1271 (defun gud-sentinel (proc msg)
1272 (cond ((null (buffer-name (process-buffer proc)))
1273 ;; buffer killed
1274 ;; Stop displaying an arrow in a source file.
1275 (setq overlay-arrow-position nil)
1276 (set-process-buffer proc nil))
1277 ((memq (process-status proc) '(signal exit))
1278 ;; Stop displaying an arrow in a source file.
1279 (setq overlay-arrow-position nil)
1280 ;; Fix the mode line.
1281 (setq mode-line-process
1282 (concat ":"
1283 (symbol-name (process-status proc))))
1284 (let* ((obuf (current-buffer)))
1285 ;; save-excursion isn't the right thing if
1286 ;; process-buffer is current-buffer
1287 (unwind-protect
1288 (progn
1289 ;; Write something in *compilation* and hack its mode line,
1290 (set-buffer (process-buffer proc))
1291 (force-mode-line-update)
1292 (if (eobp)
1293 (insert ?\n mode-name " " msg)
1294 (save-excursion
1295 (goto-char (point-max))
1296 (insert ?\n mode-name " " msg)))
1297 ;; If buffer and mode line will show that the process
1298 ;; is dead, we can delete it now. Otherwise it
1299 ;; will stay around until M-x list-processes.
1300 (delete-process proc))
1301 ;; Restore old buffer, but don't restore old point
1302 ;; if obuf is the gud buffer.
1303 (set-buffer obuf))))))
1305 (defun gud-display-frame ()
1306 "Find and obey the last filename-and-line marker from the debugger.
1307 Obeying it means displaying in another window the specified file and line."
1308 (interactive)
1309 (if gud-last-frame
1310 (progn
1311 (gud-set-buffer)
1312 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
1313 (setq gud-last-last-frame gud-last-frame
1314 gud-last-frame nil))))
1316 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
1317 ;; and that its line LINE is visible.
1318 ;; Put the overlay-arrow on the line LINE in that buffer.
1319 ;; Most of the trickiness in here comes from wanting to preserve the current
1320 ;; region-restriction if that's possible. We use an explicit display-buffer
1321 ;; to get around the fact that this is called inside a save-excursion.
1323 (defun gud-display-line (true-file line)
1324 (let* ((last-nonmenu-event t) ; Prevent use of dialog box for questions.
1325 (buffer (gud-find-file true-file))
1326 (window (display-buffer buffer))
1327 (pos))
1328 ;;; (if (equal buffer (current-buffer))
1329 ;;; nil
1330 ;;; (setq buffer-read-only nil))
1331 (save-excursion
1332 ;;; (setq buffer-read-only t)
1333 (set-buffer buffer)
1334 (save-restriction
1335 (widen)
1336 (goto-line line)
1337 (setq pos (point))
1338 (setq overlay-arrow-string "=>")
1339 (or overlay-arrow-position
1340 (setq overlay-arrow-position (make-marker)))
1341 (set-marker overlay-arrow-position (point) (current-buffer)))
1342 (cond ((or (< pos (point-min)) (> pos (point-max)))
1343 (widen)
1344 (goto-char pos))))
1345 (set-window-point window overlay-arrow-position)))
1347 ;;; The gud-call function must do the right thing whether its invoking
1348 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
1349 ;;; or a C buffer. In the former case, we want to supply data from
1350 ;;; gud-last-frame. Here's how we do it:
1352 (defun gud-format-command (str arg)
1353 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
1354 (frame (or gud-last-frame gud-last-last-frame))
1355 result)
1356 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
1357 (let ((key (string-to-char (substring str (match-beginning 2))))
1358 subst)
1359 (cond
1360 ((eq key ?f)
1361 (setq subst (file-name-nondirectory (if insource
1362 (buffer-file-name)
1363 (car frame)))))
1364 ((eq key ?d)
1365 (setq subst (file-name-directory (if insource
1366 (buffer-file-name)
1367 (car frame)))))
1368 ((eq key ?l)
1369 (setq subst (if insource
1370 (save-excursion
1371 (beginning-of-line)
1372 (save-restriction (widen)
1373 (1+ (count-lines 1 (point)))))
1374 (cdr frame))))
1375 ((eq key ?e)
1376 (setq subst (find-c-expr)))
1377 ((eq key ?a)
1378 (setq subst (gud-read-address)))
1379 ((eq key ?p)
1380 (setq subst (if arg (int-to-string arg) ""))))
1381 (setq result (concat result
1382 (substring str (match-beginning 1) (match-end 1))
1383 subst)))
1384 (setq str (substring str (match-end 2))))
1385 ;; There might be text left in STR when the loop ends.
1386 (concat result str)))
1388 (defun gud-read-address ()
1389 "Return a string containing the core-address found in the buffer at point."
1390 (save-excursion
1391 (let ((pt (point)) found begin)
1392 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1393 (cond
1394 (found (forward-char 2)
1395 (buffer-substring found
1396 (progn (re-search-forward "[^0-9a-f]")
1397 (forward-char -1)
1398 (point))))
1399 (t (setq begin (progn (re-search-backward "[^0-9]")
1400 (forward-char 1)
1401 (point)))
1402 (forward-char 1)
1403 (re-search-forward "[^0-9]")
1404 (forward-char -1)
1405 (buffer-substring begin (point)))))))
1407 (defun gud-call (fmt &optional arg)
1408 (let ((msg (gud-format-command fmt arg)))
1409 (message "Command: %s" msg)
1410 (sit-for 0)
1411 (gud-basic-call msg)))
1413 (defun gud-basic-call (command)
1414 "Invoke the debugger COMMAND displaying source in other window."
1415 (interactive)
1416 (gud-set-buffer)
1417 (let ((command (concat command "\n"))
1418 (proc (get-buffer-process gud-comint-buffer)))
1419 (or proc (error "Current buffer has no process"))
1420 ;; Arrange for the current prompt to get deleted.
1421 (save-excursion
1422 (set-buffer gud-comint-buffer)
1423 (goto-char (process-mark proc))
1424 (beginning-of-line)
1425 (if (looking-at comint-prompt-regexp)
1426 (set-marker gud-delete-prompt-marker (point))))
1427 (process-send-string proc command)))
1429 (defun gud-refresh (&optional arg)
1430 "Fix up a possibly garbled display, and redraw the arrow."
1431 (interactive "P")
1432 (recenter arg)
1433 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1434 (gud-display-frame))
1436 ;;; Code for parsing expressions out of C code. The single entry point is
1437 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1439 ;;; The rest of this file is a hacked version of gdbsrc.el by
1440 ;;; Debby Ayers <ayers@asc.slb.com>,
1441 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1443 (defun find-c-expr ()
1444 "Returns the C expr that surrounds point."
1445 (interactive)
1446 (save-excursion
1447 (let ((p) (expr) (test-expr))
1448 (setq p (point))
1449 (setq expr (expr-cur))
1450 (setq test-expr (expr-prev))
1451 (while (expr-compound test-expr expr)
1452 (setq expr (cons (car test-expr) (cdr expr)))
1453 (goto-char (car expr))
1454 (setq test-expr (expr-prev)))
1455 (goto-char p)
1456 (setq test-expr (expr-next))
1457 (while (expr-compound expr test-expr)
1458 (setq expr (cons (car expr) (cdr test-expr)))
1459 (setq test-expr (expr-next))
1461 (buffer-substring (car expr) (cdr expr)))))
1463 (defun expr-cur ()
1464 "Returns the expr that point is in; point is set to beginning of expr.
1465 The expr is represented as a cons cell, where the car specifies the point in
1466 the current buffer that marks the beginning of the expr and the cdr specifies
1467 the character after the end of the expr."
1468 (let ((p (point)) (begin) (end))
1469 (expr-backward-sexp)
1470 (setq begin (point))
1471 (expr-forward-sexp)
1472 (setq end (point))
1473 (if (>= p end)
1474 (progn
1475 (setq begin p)
1476 (goto-char p)
1477 (expr-forward-sexp)
1478 (setq end (point))
1481 (goto-char begin)
1482 (cons begin end)))
1484 (defun expr-backward-sexp ()
1485 "Version of `backward-sexp' that catches errors."
1486 (condition-case nil
1487 (backward-sexp)
1488 (error t)))
1490 (defun expr-forward-sexp ()
1491 "Version of `forward-sexp' that catches errors."
1492 (condition-case nil
1493 (forward-sexp)
1494 (error t)))
1496 (defun expr-prev ()
1497 "Returns the previous expr, point is set to beginning of that expr.
1498 The expr is represented as a cons cell, where the car specifies the point in
1499 the current buffer that marks the beginning of the expr and the cdr specifies
1500 the character after the end of the expr"
1501 (let ((begin) (end))
1502 (expr-backward-sexp)
1503 (setq begin (point))
1504 (expr-forward-sexp)
1505 (setq end (point))
1506 (goto-char begin)
1507 (cons begin end)))
1509 (defun expr-next ()
1510 "Returns the following expr, point is set to beginning of that expr.
1511 The expr is represented as a cons cell, where the car specifies the point in
1512 the current buffer that marks the beginning of the expr and the cdr specifies
1513 the character after the end of the expr."
1514 (let ((begin) (end))
1515 (expr-forward-sexp)
1516 (expr-forward-sexp)
1517 (setq end (point))
1518 (expr-backward-sexp)
1519 (setq begin (point))
1520 (cons begin end)))
1522 (defun expr-compound-sep (span-start span-end)
1523 "Returns '.' for '->' & '.', returns ' ' for white space,
1524 returns '?' for other punctuation."
1525 (let ((result ? )
1526 (syntax))
1527 (while (< span-start span-end)
1528 (setq syntax (char-syntax (char-after span-start)))
1529 (cond
1530 ((= syntax ? ) t)
1531 ((= syntax ?.) (setq syntax (char-after span-start))
1532 (cond
1533 ((= syntax ?.) (setq result ?.))
1534 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1535 (setq result ?.)
1536 (setq span-start (+ span-start 1)))
1537 (t (setq span-start span-end)
1538 (setq result ??)))))
1539 (setq span-start (+ span-start 1)))
1540 result))
1542 (defun expr-compound (first second)
1543 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1544 The two exprs are represented as a cons cells, where the car
1545 specifies the point in the current buffer that marks the beginning of the
1546 expr and the cdr specifies the character after the end of the expr.
1547 Link exprs of the form:
1548 Expr -> Expr
1549 Expr . Expr
1550 Expr (Expr)
1551 Expr [Expr]
1552 (Expr) Expr
1553 [Expr] Expr"
1554 (let ((span-start (cdr first))
1555 (span-end (car second))
1556 (syntax))
1557 (setq syntax (expr-compound-sep span-start span-end))
1558 (cond
1559 ((= (car first) (car second)) nil)
1560 ((= (cdr first) (cdr second)) nil)
1561 ((= syntax ?.) t)
1562 ((= syntax ? )
1563 (setq span-start (char-after (- span-start 1)))
1564 (setq span-end (char-after span-end))
1565 (cond
1566 ((= span-start ?) ) t )
1567 ((= span-start ?] ) t )
1568 ((= span-end ?( ) t )
1569 ((= span-end ?[ ) t )
1570 (t nil))
1572 (t nil))))
1574 (provide 'gud)
1576 ;;; gud.el ends here