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