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