(x_set_frame_parameters, x_get_arg, x_window, Fx_create_frame,
[emacs.git] / lisp / gud.el
blob843999c4c31b34316e40f89e660daff22db03d16
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
2 ;;; under Emacs
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Maintainer: FSF
6 ;; Version: 1.3
7 ;; Keywords: unix, tools
9 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
11 ;; This file is part of GNU Emacs.
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
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
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.
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.
27 ;;; Commentary:
29 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
30 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
31 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
32 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
33 ;; who also hacked the mode to use comint.el. Shane Hartman <shane@spr.com>
34 ;; added support for xdb (HPUX debugger).
36 ;;; Code:
38 (require 'comint)
39 (require 'etags)
41 ;; ======================================================================
42 ;; GUD commands must be visible in C buffers visited by GUD
44 (defvar gud-key-prefix "\C-x\C-a"
45 "Prefix of all GUD commands valid in C buffers.")
47 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
48 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
50 ;; ======================================================================
51 ;; the overloading mechanism
53 (defun gud-overload-functions (gud-overload-alist)
54 "Overload functions defined in GUD-OVERLOAD-ALIST.
55 This association list has elements of the form
56 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
57 (mapcar
58 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
59 gud-overload-alist))
61 (defun gud-massage-args (file args)
62 (error "GUD not properly entered."))
64 (defun gud-marker-filter (str)
65 (error "GUD not properly entered."))
67 (defun gud-find-file (f)
68 (error "GUD not properly entered."))
70 ;; ======================================================================
71 ;; command definition
73 ;; This macro is used below to define some basic debugger interface commands.
74 ;; Of course you may use `gud-def' with any other debugger command, including
75 ;; user defined ones.
77 ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
78 ;; which defines FUNC to send the command NAME to the debugger, gives
79 ;; it the docstring DOC, and binds that function to KEY in the GUD
80 ;; major mode. The function is also bound in the global keymap with the
81 ;; GUD prefix.
83 (defmacro gud-def (func cmd key &optional doc)
84 "Define FUNC to be a command sending STR and bound to KEY, with
85 optional doc string DOC. Certain %-escapes in the string arguments
86 are interpreted specially if present. These are:
88 %f name (without directory) of current source file.
89 %d directory of current source file.
90 %l number of current source line
91 %e text of the C lvalue or function-call expression surrounding point.
92 %a text of the hexadecimal address surrounding point
93 %p prefix argument to the command (if any) as a number
95 The `current' source file is the file of the current buffer (if
96 we're in a C file) or the source file current at the last break or
97 step (if we're in the GUD buffer).
98 The `current' line is that of the current buffer (if we're in a
99 source file) or the source line number at the last break or step (if
100 we're in the GUD buffer)."
101 (list 'progn
102 (list 'defun func '(arg)
103 (or doc "")
104 '(interactive "p")
105 (list 'gud-call cmd 'arg))
106 (if key
107 (list 'define-key
108 '(current-local-map)
109 (concat "\C-c" key)
110 (list 'quote func)))
111 (if key
112 (list 'global-set-key
113 (list 'concat 'gud-key-prefix key)
114 (list 'quote func)))))
116 ;; Where gud-display-frame should put the debugging arrow. This is
117 ;; set by the marker-filter, which scans the debugger's output for
118 ;; indications of the current program counter.
119 (defvar gud-last-frame nil)
121 ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
122 ;; the last frame, even if it's been called before and gud-last-frame has
123 ;; been set to nil.
124 (defvar gud-last-last-frame nil)
126 ;; All debugger-specific information is collected here.
127 ;; Here's how it works, in case you ever need to add a debugger to the mode.
129 ;; Each entry must define the following at startup:
131 ;;<name>
132 ;; comint-prompt-regexp
133 ;; gud-<name>-massage-args
134 ;; gud-<name>-marker-filter
135 ;; gud-<name>-find-file
137 ;; The job of the massage-args method is to modify the given list of
138 ;; debugger arguments before running the debugger.
140 ;; The job of the marker-filter method is to detect file/line markers in
141 ;; strings and set the global gud-last-frame to indicate what display
142 ;; action (if any) should be triggered by the marker. Note that only
143 ;; whatever the method *returns* is displayed in the buffer; thus, you
144 ;; can filter the debugger's output, interpreting some and passing on
145 ;; the rest.
147 ;; The job of the find-file method is to visit and return the buffer indicated
148 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
149 ;; something else.
151 ;; ======================================================================
152 ;; gdb functions
154 ;;; History of argument lists passed to gdb.
155 (defvar gud-gdb-history nil)
157 (defun gud-gdb-massage-args (file args)
158 (cons "-fullname" (cons file args)))
160 ;; There's no guarantee that Emacs will hand the filter the entire
161 ;; marker at once; it could be broken up across several strings. We
162 ;; might even receive a big chunk with several markers in it. If we
163 ;; receive a chunk of text which looks like it might contain the
164 ;; beginning of a marker, we save it here between calls to the
165 ;; filter.
166 (defvar gud-gdb-marker-acc "")
168 (defun gud-gdb-marker-filter (string)
169 (save-match-data
170 (setq gud-gdb-marker-acc (concat gud-gdb-marker-acc string))
171 (let ((output ""))
173 ;; Process all the complete markers in this chunk.
174 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
175 gud-gdb-marker-acc)
176 (setq
178 ;; Extract the frame position from the marker.
179 gud-last-frame
180 (cons (substring gud-gdb-marker-acc (match-beginning 1) (match-end 1))
181 (string-to-int (substring gud-gdb-marker-acc
182 (match-beginning 2)
183 (match-end 2))))
185 ;; Append any text before the marker to the output we're going
186 ;; to return - we don't include the marker in this text.
187 output (concat output
188 (substring gud-gdb-marker-acc 0 (match-beginning 0)))
190 ;; Set the accumulator to the remaining text.
191 gud-gdb-marker-acc (substring gud-gdb-marker-acc (match-end 0))))
193 ;; Does the remaining text look like it might end with the
194 ;; beginning of another marker? If it does, then keep it in
195 ;; gud-gdb-marker-acc until we receive the rest of it. Since we
196 ;; know the full marker regexp above failed, it's pretty simple to
197 ;; test for marker starts.
198 (if (string-match "^\032.*\\'" gud-gdb-marker-acc)
199 (progn
200 ;; Everything before the potential marker start can be output.
201 (setq output (concat output (substring gud-gdb-marker-acc
202 0 (match-beginning 0))))
204 ;; Everything after, we save, to combine with later input.
205 (setq gud-gdb-marker-acc
206 (substring gud-gdb-marker-acc (match-beginning 0))))
208 (setq output (concat output gud-gdb-marker-acc)
209 gud-gdb-marker-acc ""))
211 output)))
213 (defun gud-gdb-find-file (f)
214 (find-file-noselect f))
216 (defvar gdb-minibuffer-local-map nil
217 "Keymap for minibuffer prompting of gdb startup command.")
218 (if gdb-minibuffer-local-map
220 (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
221 (define-key
222 gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
224 ;;;###autoload
225 (defun gdb (command-line)
226 "Run gdb on program FILE in buffer *gud-FILE*.
227 The directory containing FILE becomes the initial working directory
228 and source-file directory for your debugger."
229 (interactive
230 (list (read-from-minibuffer "Run gdb (like this): "
231 (if (consp gud-gdb-history)
232 (car gud-gdb-history)
233 "gdb ")
234 gdb-minibuffer-local-map nil
235 '(gud-gdb-history . 1))))
236 (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args)
237 (gud-marker-filter . gud-gdb-marker-filter)
238 (gud-find-file . gud-gdb-find-file)
241 (gud-common-init command-line)
243 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
244 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
245 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
246 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
247 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
248 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
249 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
250 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
251 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
252 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
253 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
255 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
256 (run-hooks 'gdb-mode-hook)
260 ;; ======================================================================
261 ;; sdb functions
263 ;;; History of argument lists passed to sdb.
264 (defvar gud-sdb-history nil)
266 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
267 "If nil, we're on a System V Release 4 and don't need the tags hack.")
269 (defvar gud-sdb-lastfile nil)
271 (defun gud-sdb-massage-args (file args)
272 (cons file args))
274 (defun gud-sdb-marker-filter (string)
275 (cond
276 ;; System V Release 3.2 uses this format
277 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
278 string)
279 (setq gud-last-frame
280 (cons
281 (substring string (match-beginning 2) (match-end 2))
282 (string-to-int
283 (substring string (match-beginning 3) (match-end 3))))))
284 ;; System V Release 4.0
285 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
286 string)
287 (setq gud-sdb-lastfile
288 (substring string (match-beginning 2) (match-end 2))))
289 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
290 (setq gud-last-frame
291 (cons
292 gud-sdb-lastfile
293 (string-to-int
294 (substring string (match-beginning 1) (match-end 1))))))
296 (setq gud-sdb-lastfile nil)))
297 string)
299 (defun gud-sdb-find-file (f)
300 (if gud-sdb-needs-tags
301 (find-tag-noselect f)
302 (find-file-noselect f)))
304 ;;;###autoload
305 (defun sdb (command-line)
306 "Run sdb on program FILE in buffer *gud-FILE*.
307 The directory containing FILE becomes the initial working directory
308 and source-file directory for your debugger."
309 (interactive
310 (list (read-from-minibuffer "Run sdb (like this): "
311 (if (consp gud-sdb-history)
312 (car gud-sdb-history)
313 "sdb ")
314 nil nil
315 '(gud-sdb-history . 1))))
316 (if (and gud-sdb-needs-tags
317 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
318 (error "The sdb support requires a valid tags table to work."))
319 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
320 (gud-marker-filter . gud-sdb-marker-filter)
321 (gud-find-file . gud-sdb-find-file)
324 (gud-common-init command-line)
326 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
327 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
328 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
329 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
330 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
331 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
332 (gud-def gud-cont "c" "\C-r" "Continue with display.")
333 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
335 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
336 (run-hooks 'sdb-mode-hook)
339 ;; ======================================================================
340 ;; dbx functions
342 ;;; History of argument lists passed to dbx.
343 (defvar gud-dbx-history nil)
345 (defun gud-dbx-massage-args (file args)
346 (cons file args))
348 (defun gud-dbx-marker-filter (string)
349 (if (or (string-match
350 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
351 string)
352 (string-match
353 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
354 string))
355 (setq gud-last-frame
356 (cons
357 (substring string (match-beginning 2) (match-end 2))
358 (string-to-int
359 (substring string (match-beginning 1) (match-end 1))))))
360 string)
362 ;; Functions for dbx on Mips/Ultrix.
363 ;; This is very similar to the code for gdb. The trick is to start dbx
364 ;; with the (undocumented) option `-emacs'.
366 ;; Are we running on a Mips system under Ultrix?
367 (defvar gud-dbx-mips-p (file-exists-p "/usr/include/mips"))
369 (defun gud-mipsdbx-massage-args (file args)
370 (cons "-emacs" (cons file args)))
372 ;; There's no guarantee that Emacs will hand the filter the entire
373 ;; marker at once; it could be broken up across several strings. We
374 ;; might even receive a big chunk with several markers in it. If we
375 ;; receive a chunk of text which looks like it might contain the
376 ;; beginning of a marker, we save it here between calls to the
377 ;; filter.
378 (defvar gud-mipsdbx-marker-acc "")
380 (defun gud-mipsdbx-marker-filter (string)
381 (save-match-data
382 (setq gud-mipsdbx-marker-acc (concat gud-mipsdbx-marker-acc string))
383 (let ((output ""))
385 ;; Process all the complete markers in this chunk.
386 (while (string-match
387 "^[] [0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
388 gud-mipsdbx-marker-acc)
389 (setq
391 ;; Extract the frame position from the marker.
392 gud-last-frame (cons
393 (substring gud-mipsdbx-marker-acc
394 (match-beginning 1) (match-end 1))
395 (string-to-int
396 (substring gud-mipsdbx-marker-acc
397 (match-beginning 2) (match-end 2))))
399 ;; Append any text before the marker to the output we're going
400 ;; to return - we don't include the marker in this text.
401 output (concat output (substring gud-mipsdbx-marker-acc
402 0 (match-beginning 0)))
404 ;; Set the accumulator to the remaining text.
405 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
406 (match-end 0))))
408 ;; Does the remaining text look like it might end with the
409 ;; beginning of another marker? If it does, then keep it in
410 ;; gud-mipsdbx-marker-acc until we receive the rest of it.
411 ;; Since we know the full marker regexp above failed, it's pretty
412 ;; simple to test for marker starts.
413 (if (string-match "^[] [0-9]*\032.*\\'" gud-mipsdbx-marker-acc)
414 (setq
415 ;; Everything before the potential marker start can be output.
416 output (concat output (substring gud-mipsdbx-marker-acc
417 0 (match-beginning 0)))
418 ;; Everything after, we save, to combine with later input.
419 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
420 (match-beginning 0)))
421 (setq output (concat output gud-mipsdbx-marker-acc)
422 gud-mipsdbx-marker-acc ""))
424 output)))
426 (defun gud-dbx-find-file (f)
427 (find-file-noselect f))
429 ;;;###autoload
430 (defun dbx (command-line)
431 "Run dbx on program FILE in buffer *gud-FILE*.
432 The directory containing FILE becomes the initial working directory
433 and source-file directory for your debugger."
434 (interactive
435 (list (read-from-minibuffer "Run dbx (like this): "
436 (if (consp gud-dbx-history)
437 (car gud-dbx-history)
438 "dbx ")
439 nil nil
440 '(gud-dbx-history . 1))))
442 (gud-overload-functions
443 (cond
444 (gud-dbx-mips-p
445 '((gud-massage-args . gud-mipsdbx-massage-args)
446 (gud-marker-filter . gud-mipsdbx-marker-filter)
447 (gud-find-file . gud-dbx-find-file)))
449 '((gud-massage-args . gud-dbx-massage-args)
450 (gud-marker-filter . gud-dbx-marker-filter)
451 (gud-find-file . gud-dbx-find-file)))))
453 (gud-common-init command-line)
455 (cond
456 (gud-dbx-mips-p
457 (gud-def gud-break "stop at \"%f\":%l"
458 "\C-b" "Set breakpoint at current line.")
459 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
461 (gud-def gud-break "file \"%d%f\"\nstop at %l"
462 "\C-b" "Set breakpoint at current line.")))
464 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
465 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
466 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
467 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
468 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
469 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
470 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
471 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
473 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
474 (run-hooks 'dbx-mode-hook)
477 ;; ======================================================================
478 ;; xdb (HP PARISC debugger) functions
480 ;;; History of argument lists passed to xdb.
481 (defvar gud-xdb-history nil)
483 (defvar gud-xdb-directories nil
484 "*A list of directories that xdb should search for source code.
485 If nil, only source files in the program directory
486 will be known to xdb.
488 The file names should be absolute, or relative to the directory
489 containing the executable being debugged.")
491 (defun gud-xdb-massage-args (file args)
492 (nconc (let ((directories gud-xdb-directories)
493 (result nil))
494 (while directories
495 (setq result (cons (car directories) (cons "-d" result)))
496 (setq directories (cdr directories)))
497 (nreverse (cons file result)))
498 args))
500 (defun gud-xdb-file-name (f)
501 "Transform a relative pathname to a full pathname in xdb mode"
502 (let ((result nil))
503 (if (file-exists-p f)
504 (setq result (expand-file-name f))
505 (let ((directories gud-xdb-directories))
506 (while directories
507 (let ((path (concat (car directories) "/" f)))
508 (if (file-exists-p path)
509 (setq result (expand-file-name path)
510 directories nil)))
511 (setq directories (cdr directories)))))
512 result))
514 ;; xdb does not print the lines all at once, so we have to accumulate them
515 (defvar gud-xdb-accumulation "")
517 (defun gud-xdb-marker-filter (string)
518 (let (result)
519 (if (or (string-match comint-prompt-regexp string)
520 (string-match ".*\012" string))
521 (setq result (concat gud-xdb-accumulation string)
522 gud-xdb-accumulation "")
523 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
524 (if result
525 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
526 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
527 result))
528 (let ((line (string-to-int
529 (substring result (match-beginning 2) (match-end 2))))
530 (file (gud-xdb-file-name
531 (substring result (match-beginning 1) (match-end 1)))))
532 (if file
533 (setq gud-last-frame (cons file line))))))
534 (or result "")))
536 (defun gud-xdb-find-file (f)
537 (let ((realf (gud-xdb-file-name f)))
538 (if realf (find-file-noselect realf))))
540 ;;;###autoload
541 (defun xdb (command-line)
542 "Run xdb on program FILE in buffer *gud-FILE*.
543 The directory containing FILE becomes the initial working directory
544 and source-file directory for your debugger.
546 You can set the variable 'gud-xdb-directories' to a list of program source
547 directories if your program contains sources from more than one directory."
548 (interactive
549 (list (read-from-minibuffer "Run xdb (like this): "
550 (if (consp gud-xdb-history)
551 (car gud-xdb-history)
552 "xdb ")
553 nil nil
554 '(gud-xdb-history . 1))))
555 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
556 (gud-marker-filter . gud-xdb-marker-filter)
557 (gud-find-file . gud-xdb-find-file)))
559 (gud-common-init command-line)
561 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
562 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
563 "Set temporary breakpoint at current line.")
564 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
565 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
566 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
567 (gud-def gud-cont "c" "\C-r" "Continue with display.")
568 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
569 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
570 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
571 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
573 (setq comint-prompt-regexp "^>")
574 (make-local-variable 'gud-xdb-accumulation)
575 (setq gud-xdb-accumulation "")
576 (run-hooks 'xdb-mode-hook))
578 ;; ======================================================================
579 ;; perldb functions
581 ;;; History of argument lists passed to perldb.
582 (defvar gud-perldb-history nil)
584 (defun gud-perldb-massage-args (file args)
585 (cons "-d" (cons file (cons "-emacs" args))))
587 ;; There's no guarantee that Emacs will hand the filter the entire
588 ;; marker at once; it could be broken up across several strings. We
589 ;; might even receive a big chunk with several markers in it. If we
590 ;; receive a chunk of text which looks like it might contain the
591 ;; beginning of a marker, we save it here between calls to the
592 ;; filter.
593 (defvar gud-perldb-marker-acc "")
595 (defun gud-perldb-marker-filter (string)
596 (save-match-data
597 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
598 (let ((output ""))
600 ;; Process all the complete markers in this chunk.
601 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
602 gud-perldb-marker-acc)
603 (setq
605 ;; Extract the frame position from the marker.
606 gud-last-frame
607 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
608 (string-to-int (substring gud-perldb-marker-acc
609 (match-beginning 2)
610 (match-end 2))))
612 ;; Append any text before the marker to the output we're going
613 ;; to return - we don't include the marker in this text.
614 output (concat output
615 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
617 ;; Set the accumulator to the remaining text.
618 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
620 ;; Does the remaining text look like it might end with the
621 ;; beginning of another marker? If it does, then keep it in
622 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
623 ;; know the full marker regexp above failed, it's pretty simple to
624 ;; test for marker starts.
625 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
626 (progn
627 ;; Everything before the potential marker start can be output.
628 (setq output (concat output (substring gud-perldb-marker-acc
629 0 (match-beginning 0))))
631 ;; Everything after, we save, to combine with later input.
632 (setq gud-perldb-marker-acc
633 (substring gud-perldb-marker-acc (match-beginning 0))))
635 (setq output (concat output gud-perldb-marker-acc)
636 gud-perldb-marker-acc ""))
638 output)))
640 (defun gud-perldb-find-file (f)
641 (find-file-noselect f))
643 ;;;###autoload
644 (defun perldb (command-line)
645 "Run perldb on program FILE in buffer *gud-FILE*.
646 The directory containing FILE becomes the initial working directory
647 and source-file directory for your debugger."
648 (interactive
649 (list (read-from-minibuffer "Run perldb (like this): "
650 (if (consp gud-perldb-history)
651 (car gud-perldb-history)
652 "perl ")
653 nil nil
654 '(gud-perldb-history . 1))))
655 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
656 (gud-marker-filter . gud-perldb-marker-filter)
657 (gud-find-file . gud-perldb-find-file)
660 (gud-common-init command-line)
662 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
663 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
664 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
665 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
666 (gud-def gud-cont "c" "\C-r" "Continue with display.")
667 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
668 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
669 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
670 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
672 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
673 (run-hooks 'perldb-mode-hook)
677 ;; End of debugger-specific information
681 ;;; When we send a command to the debugger via gud-call, it's annoying
682 ;;; to see the command and the new prompt inserted into the debugger's
683 ;;; buffer; we have other ways of knowing the command has completed.
685 ;;; If the buffer looks like this:
686 ;;; --------------------
687 ;;; (gdb) set args foo bar
688 ;;; (gdb) -!-
689 ;;; --------------------
690 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
691 ;;; source file to set a breakpoint, we want the buffer to end up like
692 ;;; this:
693 ;;; --------------------
694 ;;; (gdb) set args foo bar
695 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
696 ;;; (gdb) -!-
697 ;;; --------------------
698 ;;; Essentially, the old prompt is deleted, and the command's output
699 ;;; and the new prompt take its place.
701 ;;; Not echoing the command is easy enough; you send it directly using
702 ;;; process-send-string, and it never enters the buffer. However,
703 ;;; getting rid of the old prompt is trickier; you don't want to do it
704 ;;; when you send the command, since that will result in an annoying
705 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
706 ;;; waits for a response from the debugger, and the new prompt is
707 ;;; inserted. Instead, we'll wait until we actually get some output
708 ;;; from the subprocess before we delete the prompt. If the command
709 ;;; produced no output other than a new prompt, that prompt will most
710 ;;; likely be in the first chunk of output received, so we will delete
711 ;;; the prompt and then replace it with an identical one. If the
712 ;;; command produces output, the prompt is moving anyway, so the
713 ;;; flicker won't be annoying.
715 ;;; So - when we want to delete the prompt upon receipt of the next
716 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
717 ;;; the start of the prompt; the process filter will notice this, and
718 ;;; delete all text between it and the process output marker. If
719 ;;; gud-delete-prompt-marker points nowhere, we leave the current
720 ;;; prompt alone.
721 (defvar gud-delete-prompt-marker nil)
724 (defun gud-mode ()
725 "Major mode for interacting with an inferior debugger process.
727 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
728 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
729 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
731 After startup, the following commands are available in both the GUD
732 interaction buffer and any source buffer GUD visits due to a breakpoint stop
733 or step operation:
735 \\[gud-break] sets a breakpoint at the current file and line. In the
736 GUD buffer, the current file and line are those of the last breakpoint or
737 step. In a source buffer, they are the buffer's file and current line.
739 \\[gud-remove] removes breakpoints on the current file and line.
741 \\[gud-refresh] displays in the source window the last line referred to
742 in the gud buffer.
744 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
745 step-one-line (not entering function calls), and step-one-instruction
746 and then update the source window with the current file and position.
747 \\[gud-cont] continues execution.
749 \\[gud-print] tries to find the largest C lvalue or function-call expression
750 around point, and sends it to the debugger for value display.
752 The above commands are common to all supported debuggers except xdb which
753 does not support stepping instructions.
755 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
756 except that the breakpoint is temporary; that is, it is removed when
757 execution stops on it.
759 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
760 frame. \\[gud-down] drops back down through one.
762 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
763 the current function and stops.
765 All the keystrokes above are accessible in the GUD buffer
766 with the prefix C-c, and in all buffers through the prefix C-x C-a.
768 All pre-defined functions for which the concept make sense repeat
769 themselves the appropriate number of times if you give a prefix
770 argument.
772 You may use the `gud-def' macro in the initialization hook to define other
773 commands.
775 Other commands for interacting with the debugger process are inherited from
776 comint mode, which see."
777 (interactive)
778 (comint-mode)
779 (setq major-mode 'gud-mode)
780 (setq mode-name "Debugger")
781 (setq mode-line-process '(": %s"))
782 (use-local-map (copy-keymap comint-mode-map))
783 (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
784 (make-local-variable 'gud-last-frame)
785 (setq gud-last-frame nil)
786 (make-local-variable 'comint-prompt-regexp)
787 (make-local-variable 'gud-delete-prompt-marker)
788 (setq gud-delete-prompt-marker (make-marker))
789 (run-hooks 'gud-mode-hook)
792 (defvar gud-comint-buffer nil)
794 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
795 (defun gud-chop-words (string)
796 (let ((i 0) (beg 0)
797 (len (length string))
798 (words nil))
799 (while (< i len)
800 (if (memq (aref string i) '(?\t ? ))
801 (progn
802 (setq words (cons (substring string beg i) words)
803 beg (1+ i))
804 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
805 (setq beg (1+ beg)))
806 (setq i (1+ beg)))
807 (setq i (1+ i))))
808 (if (< beg len)
809 (setq words (cons (substring string beg) words)))
810 (nreverse words)))
812 ;; Perform initializations common to all debuggers.
813 (defun gud-common-init (command-line)
814 (let* ((words (gud-chop-words command-line))
815 (program (car words))
816 (file-word (let ((w (cdr words)))
817 (while (and w (= ?- (aref (car w) 0)))
818 (setq w (cdr w)))
819 (car w)))
820 (args (delq file-word (cdr words)))
821 (file (and file-word
822 (expand-file-name (substitute-in-file-name file-word))))
823 (filepart (and file-word (file-name-nondirectory file))))
824 (switch-to-buffer (concat "*gud-" filepart "*"))
825 (and file-word (setq default-directory (file-name-directory file)))
826 (or (bolp) (newline))
827 (insert "Current directory is " default-directory "\n")
828 (apply 'make-comint (concat "gud-" filepart) program nil
829 (if file-word (gud-massage-args file args))))
830 (gud-mode)
831 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
832 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
833 (gud-set-buffer)
836 (defun gud-set-buffer ()
837 (cond ((eq major-mode 'gud-mode)
838 (setq gud-comint-buffer (current-buffer)))))
840 ;; These functions are responsible for inserting output from your debugger
841 ;; into the buffer. The hard work is done by the method that is
842 ;; the value of gud-marker-filter.
844 ;; Rather than duplicating all the work of comint-output-filter, perhaps
845 ;; gud-filter should be implemented by adding appropriate hooks to
846 ;; comint-output-filter. Would somebody like to volunteer to do that?
847 (defun gud-filter (proc string)
848 ;; Here's where the actual buffer insertion is done
849 (let ((inhibit-quit t))
850 (save-excursion
851 (set-buffer (process-buffer proc))
852 (let (moving output-after-point)
853 (save-excursion
854 (goto-char (process-mark proc))
855 ;; If we have been so requested, delete the debugger prompt.
856 (if (marker-buffer gud-delete-prompt-marker)
857 (progn
858 (delete-region (point) gud-delete-prompt-marker)
859 (set-marker gud-delete-prompt-marker nil)))
860 (setq string (gud-marker-filter string))
861 (insert-before-markers string)
862 (and comint-last-input-end
863 (marker-buffer comint-last-input-end)
864 (= (point) comint-last-input-end)
865 (set-marker comint-last-input-end
866 (- comint-last-input-end (length string))))
867 (setq moving (= (point) (process-mark proc)))
868 (setq output-after-point (< (point) (process-mark proc)))
869 ;; Check for a filename-and-line number.
870 ;; Don't display the specified file
871 ;; unless (1) point is at or after the position where output appears
872 ;; and (2) this buffer is on the screen.
873 (if (and gud-last-frame
874 (not output-after-point)
875 (get-buffer-window (current-buffer)))
876 (gud-display-frame)))
877 (if moving (goto-char (process-mark proc)))))))
879 (defun gud-sentinel (proc msg)
880 (cond ((null (buffer-name (process-buffer proc)))
881 ;; buffer killed
882 ;; Stop displaying an arrow in a source file.
883 (setq overlay-arrow-position nil)
884 (set-process-buffer proc nil))
885 ((memq (process-status proc) '(signal exit))
886 ;; Stop displaying an arrow in a source file.
887 (setq overlay-arrow-position nil)
888 ;; Fix the mode line.
889 (setq mode-line-process
890 (concat ": "
891 (symbol-name (process-status proc))))
892 (let* ((obuf (current-buffer)))
893 ;; save-excursion isn't the right thing if
894 ;; process-buffer is current-buffer
895 (unwind-protect
896 (progn
897 ;; Write something in *compilation* and hack its mode line,
898 (set-buffer (process-buffer proc))
899 ;; Force mode line redisplay soon
900 (set-buffer-modified-p (buffer-modified-p))
901 (if (eobp)
902 (insert ?\n mode-name " " msg)
903 (save-excursion
904 (goto-char (point-max))
905 (insert ?\n mode-name " " msg)))
906 ;; If buffer and mode line will show that the process
907 ;; is dead, we can delete it now. Otherwise it
908 ;; will stay around until M-x list-processes.
909 (delete-process proc))
910 ;; Restore old buffer, but don't restore old point
911 ;; if obuf is the gud buffer.
912 (set-buffer obuf))))))
914 (defun gud-display-frame ()
915 "Find and obey the last filename-and-line marker from the debugger.
916 Obeying it means displaying in another window the specified file and line."
917 (interactive)
918 (if gud-last-frame
919 (progn
920 (gud-set-buffer)
921 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
922 (setq gud-last-last-frame gud-last-frame
923 gud-last-frame nil))))
925 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
926 ;; and that its line LINE is visible.
927 ;; Put the overlay-arrow on the line LINE in that buffer.
928 ;; Most of the trickiness in here comes from wanting to preserve the current
929 ;; region-restriction if that's possible. We use an explicit display-buffer
930 ;; to get around the fact that this is called inside a save-excursion.
932 (defun gud-display-line (true-file line)
933 (let* ((buffer (gud-find-file true-file))
934 (window (display-buffer buffer))
935 (pos))
936 ;;; (if (equal buffer (current-buffer))
937 ;;; nil
938 ;;; (setq buffer-read-only nil))
939 (save-excursion
940 ;;; (setq buffer-read-only t)
941 (set-buffer buffer)
942 (save-restriction
943 (widen)
944 (goto-line line)
945 (setq pos (point))
946 (setq overlay-arrow-string "=>")
947 (or overlay-arrow-position
948 (setq overlay-arrow-position (make-marker)))
949 (set-marker overlay-arrow-position (point) (current-buffer)))
950 (cond ((or (< pos (point-min)) (> pos (point-max)))
951 (widen)
952 (goto-char pos))))
953 (set-window-point window overlay-arrow-position)))
955 ;;; The gud-call function must do the right thing whether its invoking
956 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
957 ;;; or a C buffer. In the former case, we want to supply data from
958 ;;; gud-last-frame. Here's how we do it:
960 (defun gud-format-command (str arg)
961 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
962 (frame (or gud-last-frame gud-last-last-frame))
963 result)
964 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
965 (let ((key (string-to-char (substring str (match-beginning 2))))
966 subst)
967 (cond
968 ((eq key ?f)
969 (setq subst (file-name-nondirectory (if insource
970 (buffer-file-name)
971 (car frame)))))
972 ((eq key ?d)
973 (setq subst (file-name-directory (if insource
974 (buffer-file-name)
975 (car frame)))))
976 ((eq key ?l)
977 (setq subst (if insource
978 (save-excursion
979 (beginning-of-line)
980 (save-restriction (widen)
981 (1+ (count-lines 1 (point)))))
982 (cdr frame))))
983 ((eq key ?e)
984 (setq subst (find-c-expr)))
985 ((eq key ?a)
986 (setq subst (gud-read-address)))
987 ((eq key ?p)
988 (setq subst (if arg (int-to-string arg) ""))))
989 (setq result (concat result
990 (substring str (match-beginning 1) (match-end 1))
991 subst)))
992 (setq str (substring str (match-end 2))))
993 ;; There might be text left in STR when the loop ends.
994 (concat result str)))
996 (defun gud-read-address ()
997 "Return a string containing the core-address found in the buffer at point."
998 (save-excursion
999 (let ((pt (point)) found begin)
1000 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
1001 (cond
1002 (found (forward-char 2)
1003 (buffer-substring found
1004 (progn (re-search-forward "[^0-9a-f]")
1005 (forward-char -1)
1006 (point))))
1007 (t (setq begin (progn (re-search-backward "[^0-9]")
1008 (forward-char 1)
1009 (point)))
1010 (forward-char 1)
1011 (re-search-forward "[^0-9]")
1012 (forward-char -1)
1013 (buffer-substring begin (point)))))))
1015 (defun gud-call (fmt &optional arg)
1016 (let ((msg (gud-format-command fmt arg)))
1017 (message "Command: %s" msg)
1018 (sit-for 0)
1019 (gud-basic-call msg)))
1021 (defun gud-basic-call (command)
1022 "Invoke the debugger COMMAND displaying source in other window."
1023 (interactive)
1024 (gud-set-buffer)
1025 (let ((command (concat command "\n"))
1026 (proc (get-buffer-process gud-comint-buffer)))
1028 ;; Arrange for the current prompt to get deleted.
1029 (save-excursion
1030 (set-buffer gud-comint-buffer)
1031 (goto-char (process-mark proc))
1032 (beginning-of-line)
1033 (if (looking-at comint-prompt-regexp)
1034 (set-marker gud-delete-prompt-marker (point))))
1035 (process-send-string proc command)))
1037 (defun gud-refresh (&optional arg)
1038 "Fix up a possibly garbled display, and redraw the arrow."
1039 (interactive "P")
1040 (recenter arg)
1041 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1042 (gud-display-frame))
1044 ;;; Code for parsing expressions out of C code. The single entry point is
1045 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1047 ;;; The rest of this file is a hacked version of gdbsrc.el by
1048 ;;; Debby Ayers <ayers@asc.slb.com>,
1049 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1051 (defun find-c-expr ()
1052 "Returns the C expr that surrounds point."
1053 (interactive)
1054 (save-excursion
1055 (let ((p) (expr) (test-expr))
1056 (setq p (point))
1057 (setq expr (expr-cur))
1058 (setq test-expr (expr-prev))
1059 (while (expr-compound test-expr expr)
1060 (setq expr (cons (car test-expr) (cdr expr)))
1061 (goto-char (car expr))
1062 (setq test-expr (expr-prev)))
1063 (goto-char p)
1064 (setq test-expr (expr-next))
1065 (while (expr-compound expr test-expr)
1066 (setq expr (cons (car expr) (cdr test-expr)))
1067 (setq test-expr (expr-next))
1069 (buffer-substring (car expr) (cdr expr)))))
1071 (defun expr-cur ()
1072 "Returns the expr that point is in; point is set to beginning of expr.
1073 The expr is represented as a cons cell, where the car specifies the point in
1074 the current buffer that marks the beginning of the expr and the cdr specifies
1075 the character after the end of the expr."
1076 (let ((p (point)) (begin) (end))
1077 (expr-backward-sexp)
1078 (setq begin (point))
1079 (expr-forward-sexp)
1080 (setq end (point))
1081 (if (>= p end)
1082 (progn
1083 (setq begin p)
1084 (goto-char p)
1085 (expr-forward-sexp)
1086 (setq end (point))
1089 (goto-char begin)
1090 (cons begin end)))
1092 (defun expr-backward-sexp ()
1093 "Version of `backward-sexp' that catches errors."
1094 (condition-case nil
1095 (backward-sexp)
1096 (error t)))
1098 (defun expr-forward-sexp ()
1099 "Version of `forward-sexp' that catches errors."
1100 (condition-case nil
1101 (forward-sexp)
1102 (error t)))
1104 (defun expr-prev ()
1105 "Returns the previous expr, point is set to beginning of that expr.
1106 The expr is represented as a cons cell, where the car specifies the point in
1107 the current buffer that marks the beginning of the expr and the cdr specifies
1108 the character after the end of the expr"
1109 (let ((begin) (end))
1110 (expr-backward-sexp)
1111 (setq begin (point))
1112 (expr-forward-sexp)
1113 (setq end (point))
1114 (goto-char begin)
1115 (cons begin end)))
1117 (defun expr-next ()
1118 "Returns the following expr, point is set to beginning of that expr.
1119 The expr is represented as a cons cell, where the car specifies the point in
1120 the current buffer that marks the beginning of the expr and the cdr specifies
1121 the character after the end of the expr."
1122 (let ((begin) (end))
1123 (expr-forward-sexp)
1124 (expr-forward-sexp)
1125 (setq end (point))
1126 (expr-backward-sexp)
1127 (setq begin (point))
1128 (cons begin end)))
1130 (defun expr-compound-sep (span-start span-end)
1131 "Returns '.' for '->' & '.', returns ' ' for white space,
1132 returns '?' for other punctuation."
1133 (let ((result ? )
1134 (syntax))
1135 (while (< span-start span-end)
1136 (setq syntax (char-syntax (char-after span-start)))
1137 (cond
1138 ((= syntax ? ) t)
1139 ((= syntax ?.) (setq syntax (char-after span-start))
1140 (cond
1141 ((= syntax ?.) (setq result ?.))
1142 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1143 (setq result ?.)
1144 (setq span-start (+ span-start 1)))
1145 (t (setq span-start span-end)
1146 (setq result ??)))))
1147 (setq span-start (+ span-start 1)))
1148 result))
1150 (defun expr-compound (first second)
1151 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1152 The two exprs are represented as a cons cells, where the car
1153 specifies the point in the current buffer that marks the beginning of the
1154 expr and the cdr specifies the character after the end of the expr.
1155 Link exprs of the form:
1156 Expr -> Expr
1157 Expr . Expr
1158 Expr (Expr)
1159 Expr [Expr]
1160 (Expr) Expr
1161 [Expr] Expr"
1162 (let ((span-start (cdr first))
1163 (span-end (car second))
1164 (syntax))
1165 (setq syntax (expr-compound-sep span-start span-end))
1166 (cond
1167 ((= (car first) (car second)) nil)
1168 ((= (cdr first) (cdr second)) nil)
1169 ((= syntax ?.) t)
1170 ((= syntax ? )
1171 (setq span-start (char-after (- span-start 1)))
1172 (setq span-end (char-after span-end))
1173 (cond
1174 ((= span-start ?) ) t )
1175 ((= span-start ?] ) t )
1176 ((= span-end ?( ) t )
1177 ((= span-end ?[ ) t )
1178 (t nil))
1180 (t nil))))
1182 (provide 'gud)
1184 ;;; gud.el ends here