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