(remove-hook): Doc fix.
[bpt/emacs.git] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
2 ;;; under Emacs
3
4 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
5 ;; Maintainer: FSF
6 ;; Version: 1.3
7 ;; Keywords: unix, tools
8
9 ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to
25 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 ;;; Commentary:
28
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).
35
36 ;;; Code:
37
38 (require 'comint)
39 (require 'etags)
40
41 ;; ======================================================================
42 ;; GUD commands must be visible in C buffers visited by GUD
43
44 (defvar gud-key-prefix "\C-x\C-a"
45 "Prefix of all GUD commands valid in C buffers.")
46
47 (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
48 (global-set-key "\C-x " 'gud-break) ;; backward compatibility hack
49
50 ;; ======================================================================
51 ;; the overloading mechanism
52
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))
60
61 (defun gud-massage-args (file args)
62 (error "GUD not properly entered."))
63
64 (defun gud-marker-filter (str)
65 (error "GUD not properly entered."))
66
67 (defun gud-find-file (f)
68 (error "GUD not properly entered."))
69 \f
70 ;; ======================================================================
71 ;; command definition
72
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.
76
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.
82
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:
87
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
94
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)))))
115
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)
120
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)
125
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.
128 ;;
129 ;; Each entry must define the following at startup:
130 ;;
131 ;;<name>
132 ;; comint-prompt-regexp
133 ;; gud-<name>-massage-args
134 ;; gud-<name>-marker-filter
135 ;; gud-<name>-find-file
136 ;;
137 ;; The job of the massage-args method is to modify the given list of
138 ;; debugger arguments before running the debugger.
139 ;;
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.
146 ;;
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.
150 \f
151 ;; ======================================================================
152 ;; gdb functions
153
154 ;;; History of argument lists passed to gdb.
155 (defvar gud-gdb-history nil)
156
157 (defun gud-gdb-massage-args (file args)
158 (cons "-fullname" (cons file args)))
159
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 "")
167
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 ""))
172
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
177
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))))
184
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)))
189
190 ;; Set the accumulator to the remaining text.
191 gud-gdb-marker-acc (substring gud-gdb-marker-acc (match-end 0))))
192
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))))
203
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))))
207
208 (setq output (concat output gud-gdb-marker-acc)
209 gud-gdb-marker-acc ""))
210
211 output)))
212
213 (defun gud-gdb-find-file (f)
214 (find-file-noselect f))
215
216 ;;;###autoload
217 (defun gdb (command-line)
218 "Run gdb on program FILE in buffer *gud-FILE*.
219 The directory containing FILE becomes the initial working directory
220 and source-file directory for your debugger."
221 (interactive
222 (list (read-from-minibuffer "Run gdb (like this): "
223 (if (consp gud-gdb-history)
224 (car gud-gdb-history)
225 "gdb ")
226 nil nil
227 '(gud-gdb-history . 1))))
228 (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args)
229 (gud-marker-filter . gud-gdb-marker-filter)
230 (gud-find-file . gud-gdb-find-file)
231 ))
232
233 (gud-common-init command-line)
234
235 (gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
236 (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
237 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
238 (gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
239 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
240 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
241 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
242 (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
243 (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
244 (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
245 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
246
247 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
248 (run-hooks 'gdb-mode-hook)
249 )
250
251 \f
252 ;; ======================================================================
253 ;; sdb functions
254
255 ;;; History of argument lists passed to sdb.
256 (defvar gud-sdb-history nil)
257
258 (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
259 "If nil, we're on a System V Release 4 and don't need the tags hack.")
260
261 (defvar gud-sdb-lastfile nil)
262
263 (defun gud-sdb-massage-args (file args)
264 (cons file args))
265
266 (defun gud-sdb-marker-filter (string)
267 (cond
268 ;; System V Release 3.2 uses this format
269 ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
270 string)
271 (setq gud-last-frame
272 (cons
273 (substring string (match-beginning 2) (match-end 2))
274 (string-to-int
275 (substring string (match-beginning 3) (match-end 3))))))
276 ;; System V Release 4.0
277 ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
278 string)
279 (setq gud-sdb-lastfile
280 (substring string (match-beginning 2) (match-end 2))))
281 ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
282 (setq gud-last-frame
283 (cons
284 gud-sdb-lastfile
285 (string-to-int
286 (substring string (match-beginning 1) (match-end 1))))))
287 (t
288 (setq gud-sdb-lastfile nil)))
289 string)
290
291 (defun gud-sdb-find-file (f)
292 (if gud-sdb-needs-tags
293 (find-tag-noselect f)
294 (find-file-noselect f)))
295
296 ;;;###autoload
297 (defun sdb (command-line)
298 "Run sdb on program FILE in buffer *gud-FILE*.
299 The directory containing FILE becomes the initial working directory
300 and source-file directory for your debugger."
301 (interactive
302 (list (read-from-minibuffer "Run sdb (like this): "
303 (if (consp gud-sdb-history)
304 (car gud-sdb-history)
305 "sdb ")
306 nil nil
307 '(gud-sdb-history . 1))))
308 (if (and gud-sdb-needs-tags
309 (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name))))
310 (error "The sdb support requires a valid tags table to work."))
311 (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
312 (gud-marker-filter . gud-sdb-marker-filter)
313 (gud-find-file . gud-sdb-find-file)
314 ))
315
316 (gud-common-init command-line)
317
318 (gud-def gud-break "%l b" "\C-b" "Set breakpoint at current line.")
319 (gud-def gud-tbreak "%l c" "\C-t" "Set temporary breakpoint at current line.")
320 (gud-def gud-remove "%l d" "\C-d" "Remove breakpoint at current line")
321 (gud-def gud-step "s %p" "\C-s" "Step one source line with display.")
322 (gud-def gud-stepi "i %p" "\C-i" "Step one instruction with display.")
323 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
324 (gud-def gud-cont "c" "\C-r" "Continue with display.")
325 (gud-def gud-print "%e/" "\C-p" "Evaluate C expression at point.")
326
327 (setq comint-prompt-regexp "\\(^\\|\n\\)\\*")
328 (run-hooks 'sdb-mode-hook)
329 )
330 \f
331 ;; ======================================================================
332 ;; dbx functions
333
334 ;;; History of argument lists passed to dbx.
335 (defvar gud-dbx-history nil)
336
337 (defun gud-dbx-massage-args (file args)
338 (cons file args))
339
340 (defun gud-dbx-marker-filter (string)
341 (if (or (string-match
342 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
343 string)
344 (string-match
345 "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
346 string))
347 (setq gud-last-frame
348 (cons
349 (substring string (match-beginning 2) (match-end 2))
350 (string-to-int
351 (substring string (match-beginning 1) (match-end 1))))))
352 string)
353
354 (defun gud-dbx-find-file (f)
355 (find-file-noselect f))
356
357 ;;;###autoload
358 (defun dbx (command-line)
359 "Run dbx on program FILE in buffer *gud-FILE*.
360 The directory containing FILE becomes the initial working directory
361 and source-file directory for your debugger."
362 (interactive
363 (list (read-from-minibuffer "Run dbx (like this): "
364 (if (consp gud-dbx-history)
365 (car gud-dbx-history)
366 "dbx ")
367 nil nil
368 '(gud-dbx-history . 1))))
369 (gud-overload-functions '((gud-massage-args . gud-dbx-massage-args)
370 (gud-marker-filter . gud-dbx-marker-filter)
371 (gud-find-file . gud-dbx-find-file)
372 ))
373
374 (gud-common-init command-line)
375
376 (gud-def gud-break "file \"%d%f\"\nstop at %l"
377 "\C-b" "Set breakpoint at current line.")
378 ;; (gud-def gud-break "stop at \"%f\":%l"
379 ;; "\C-b" "Set breakpoint at current line.")
380 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
381 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
382 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
383 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
384 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
385 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
386 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
387 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
388
389 (setq comint-prompt-regexp "^[^)]*dbx) *")
390 (run-hooks 'dbx-mode-hook)
391 )
392 \f
393 ;; ======================================================================
394 ;; xdb (HP PARISC debugger) functions
395
396 ;;; History of argument lists passed to xdb.
397 (defvar gud-xdb-history nil)
398
399 (defvar gud-xdb-directories nil
400 "*A list of directories that xdb should search for source code.
401 If nil, only source files in the program directory
402 will be known to xdb.
403
404 The file names should be absolute, or relative to the directory
405 containing the executable being debugged.")
406
407 (defun gud-xdb-massage-args (file args)
408 (nconc (let ((directories gud-xdb-directories)
409 (result nil))
410 (while directories
411 (setq result (cons (car directories) (cons "-d" result)))
412 (setq directories (cdr directories)))
413 (nreverse (cons file result)))
414 args))
415
416 (defun gud-xdb-file-name (f)
417 "Transform a relative pathname to a full pathname in xdb mode"
418 (let ((result nil))
419 (if (file-exists-p f)
420 (setq result (expand-file-name f))
421 (let ((directories gud-xdb-directories))
422 (while directories
423 (let ((path (concat (car directories) "/" f)))
424 (if (file-exists-p path)
425 (setq result (expand-file-name path)
426 directories nil)))
427 (setq directories (cdr directories)))))
428 result))
429
430 ;; xdb does not print the lines all at once, so we have to accumulate them
431 (defvar gud-xdb-accumulation "")
432
433 (defun gud-xdb-marker-filter (string)
434 (let (result)
435 (if (or (string-match comint-prompt-regexp string)
436 (string-match ".*\012" string))
437 (setq result (concat gud-xdb-accumulation string)
438 gud-xdb-accumulation "")
439 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
440 (if result
441 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
442 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
443 result))
444 (let ((line (string-to-int
445 (substring result (match-beginning 2) (match-end 2))))
446 (file (gud-xdb-file-name
447 (substring result (match-beginning 1) (match-end 1)))))
448 (if file
449 (setq gud-last-frame (cons file line))))))
450 (or result "")))
451
452 (defun gud-xdb-find-file (f)
453 (let ((realf (gud-xdb-file-name f)))
454 (if realf (find-file-noselect realf))))
455
456 ;;;###autoload
457 (defun xdb (command-line)
458 "Run xdb on program FILE in buffer *gud-FILE*.
459 The directory containing FILE becomes the initial working directory
460 and source-file directory for your debugger.
461
462 You can set the variable 'gud-xdb-directories' to a list of program source
463 directories if your program contains sources from more than one directory."
464 (interactive
465 (list (read-from-minibuffer "Run xdb (like this): "
466 (if (consp gud-xdb-history)
467 (car gud-xdb-history)
468 "xdb ")
469 nil nil
470 '(gud-xdb-history . 1))))
471 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
472 (gud-marker-filter . gud-xdb-marker-filter)
473 (gud-find-file . gud-xdb-find-file)))
474
475 (gud-common-init command-line)
476
477 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
478 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
479 "Set temporary breakpoint at current line.")
480 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
481 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
482 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
483 (gud-def gud-cont "c" "\C-r" "Continue with display.")
484 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
485 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
486 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
487 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
488
489 (setq comint-prompt-regexp "^>")
490 (make-local-variable 'gud-xdb-accumulation)
491 (setq gud-xdb-accumulation "")
492 (run-hooks 'xdb-mode-hook))
493 \f
494 ;; ======================================================================
495 ;; perldb functions
496
497 ;;; History of argument lists passed to perldb.
498 (defvar gud-perldb-history nil)
499
500 (defun gud-perldb-massage-args (file args)
501 (cons "-d" (cons file (cons "-emacs" args))))
502
503 ;; There's no guarantee that Emacs will hand the filter the entire
504 ;; marker at once; it could be broken up across several strings. We
505 ;; might even receive a big chunk with several markers in it. If we
506 ;; receive a chunk of text which looks like it might contain the
507 ;; beginning of a marker, we save it here between calls to the
508 ;; filter.
509 (defvar gud-perldb-marker-acc "")
510
511 (defun gud-perldb-marker-filter (string)
512 (save-match-data
513 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
514 (let ((output ""))
515
516 ;; Process all the complete markers in this chunk.
517 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
518 gud-perldb-marker-acc)
519 (setq
520
521 ;; Extract the frame position from the marker.
522 gud-last-frame
523 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
524 (string-to-int (substring gud-perldb-marker-acc
525 (match-beginning 2)
526 (match-end 2))))
527
528 ;; Append any text before the marker to the output we're going
529 ;; to return - we don't include the marker in this text.
530 output (concat output
531 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
532
533 ;; Set the accumulator to the remaining text.
534 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
535
536 ;; Does the remaining text look like it might end with the
537 ;; beginning of another marker? If it does, then keep it in
538 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
539 ;; know the full marker regexp above failed, it's pretty simple to
540 ;; test for marker starts.
541 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
542 (progn
543 ;; Everything before the potential marker start can be output.
544 (setq output (concat output (substring gud-perldb-marker-acc
545 0 (match-beginning 0))))
546
547 ;; Everything after, we save, to combine with later input.
548 (setq gud-perldb-marker-acc
549 (substring gud-perldb-marker-acc (match-beginning 0))))
550
551 (setq output (concat output gud-perldb-marker-acc)
552 gud-perldb-marker-acc ""))
553
554 output)))
555
556 (defun gud-perldb-find-file (f)
557 (find-file-noselect f))
558
559 ;;;###autoload
560 (defun perldb (command-line)
561 "Run perldb on program FILE in buffer *gud-FILE*.
562 The directory containing FILE becomes the initial working directory
563 and source-file directory for your debugger."
564 (interactive
565 (list (read-from-minibuffer "Run perldb (like this): "
566 (if (consp gud-perldb-history)
567 (car gud-perldb-history)
568 "perl ")
569 nil nil
570 '(gud-perldb-history . 1))))
571 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
572 (gud-marker-filter . gud-perldb-marker-filter)
573 (gud-find-file . gud-perldb-find-file)
574 ))
575
576 (gud-common-init command-line)
577
578 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
579 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
580 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
581 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
582 (gud-def gud-cont "c" "\C-r" "Continue with display.")
583 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
584 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
585 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
586 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
587
588 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
589 (run-hooks 'perldb-mode-hook)
590 )
591
592 ;;
593 ;; End of debugger-specific information
594 ;;
595
596 \f
597 ;;; When we send a command to the debugger via gud-call, it's annoying
598 ;;; to see the command and the new prompt inserted into the debugger's
599 ;;; buffer; we have other ways of knowing the command has completed.
600 ;;;
601 ;;; If the buffer looks like this:
602 ;;; --------------------
603 ;;; (gdb) set args foo bar
604 ;;; (gdb) -!-
605 ;;; --------------------
606 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
607 ;;; source file to set a breakpoint, we want the buffer to end up like
608 ;;; this:
609 ;;; --------------------
610 ;;; (gdb) set args foo bar
611 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
612 ;;; (gdb) -!-
613 ;;; --------------------
614 ;;; Essentially, the old prompt is deleted, and the command's output
615 ;;; and the new prompt take its place.
616 ;;;
617 ;;; Not echoing the command is easy enough; you send it directly using
618 ;;; process-send-string, and it never enters the buffer. However,
619 ;;; getting rid of the old prompt is trickier; you don't want to do it
620 ;;; when you send the command, since that will result in an annoying
621 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
622 ;;; waits for a response from the debugger, and the new prompt is
623 ;;; inserted. Instead, we'll wait until we actually get some output
624 ;;; from the subprocess before we delete the prompt. If the command
625 ;;; produced no output other than a new prompt, that prompt will most
626 ;;; likely be in the first chunk of output received, so we will delete
627 ;;; the prompt and then replace it with an identical one. If the
628 ;;; command produces output, the prompt is moving anyway, so the
629 ;;; flicker won't be annoying.
630 ;;;
631 ;;; So - when we want to delete the prompt upon receipt of the next
632 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
633 ;;; the start of the prompt; the process filter will notice this, and
634 ;;; delete all text between it and the process output marker. If
635 ;;; gud-delete-prompt-marker points nowhere, we leave the current
636 ;;; prompt alone.
637 (defvar gud-delete-prompt-marker nil)
638
639 \f
640 (defun gud-mode ()
641 "Major mode for interacting with an inferior debugger process.
642
643 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
644 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
645 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
646
647 After startup, the following commands are available in both the GUD
648 interaction buffer and any source buffer GUD visits due to a breakpoint stop
649 or step operation:
650
651 \\[gud-break] sets a breakpoint at the current file and line. In the
652 GUD buffer, the current file and line are those of the last breakpoint or
653 step. In a source buffer, they are the buffer's file and current line.
654
655 \\[gud-remove] removes breakpoints on the current file and line.
656
657 \\[gud-refresh] displays in the source window the last line referred to
658 in the gud buffer.
659
660 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
661 step-one-line (not entering function calls), and step-one-instruction
662 and then update the source window with the current file and position.
663 \\[gud-cont] continues execution.
664
665 \\[gud-print] tries to find the largest C lvalue or function-call expression
666 around point, and sends it to the debugger for value display.
667
668 The above commands are common to all supported debuggers except xdb which
669 does not support stepping instructions.
670
671 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
672 except that the breakpoint is temporary; that is, it is removed when
673 execution stops on it.
674
675 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
676 frame. \\[gud-down] drops back down through one.
677
678 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
679 the current function and stops.
680
681 All the keystrokes above are accessible in the GUD buffer
682 with the prefix C-c, and in all buffers through the prefix C-x C-a.
683
684 All pre-defined functions for which the concept make sense repeat
685 themselves the appropriate number of times if you give a prefix
686 argument.
687
688 You may use the `gud-def' macro in the initialization hook to define other
689 commands.
690
691 Other commands for interacting with the debugger process are inherited from
692 comint mode, which see."
693 (interactive)
694 (comint-mode)
695 (setq major-mode 'gud-mode)
696 (setq mode-name "Debugger")
697 (setq mode-line-process '(": %s"))
698 (use-local-map (copy-keymap comint-mode-map))
699 (make-local-variable 'gud-last-frame)
700 (setq gud-last-frame nil)
701 (make-local-variable 'comint-prompt-regexp)
702 (make-local-variable 'gud-delete-prompt-marker)
703 (setq gud-delete-prompt-marker (make-marker))
704 (run-hooks 'gud-mode-hook)
705 )
706
707 (defvar gud-comint-buffer nil)
708
709 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
710 (defun gud-chop-words (string)
711 (let ((i 0) (beg 0)
712 (len (length string))
713 (words nil))
714 (while (< i len)
715 (if (memq (aref string i) '(?\t ? ))
716 (progn
717 (setq words (cons (substring string beg i) words)
718 beg (1+ i))
719 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
720 (setq beg (1+ beg)))
721 (setq i (1+ beg)))
722 (setq i (1+ i))))
723 (if (< beg len)
724 (setq words (cons (substring string beg) words)))
725 (nreverse words)))
726
727 ;; Perform initializations common to all debuggers.
728 (defun gud-common-init (command-line)
729 (let* ((words (gud-chop-words command-line))
730 (program (car words))
731 (file-word (let ((w (cdr words)))
732 (while (and w (= ?- (aref (car w) 0)))
733 (setq w (cdr w)))
734 (car w)))
735 (args (delq file-word (cdr words)))
736 (file (expand-file-name file-word))
737 (filepart (file-name-nondirectory file)))
738 (switch-to-buffer (concat "*gud-" filepart "*"))
739 (setq default-directory (file-name-directory file))
740 (or (bolp) (newline))
741 (insert "Current directory is " default-directory "\n")
742 (apply 'make-comint (concat "gud-" filepart) program nil
743 (gud-massage-args file args)))
744 (gud-mode)
745 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
746 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
747 (gud-set-buffer)
748 )
749
750 (defun gud-set-buffer ()
751 (cond ((eq major-mode 'gud-mode)
752 (setq gud-comint-buffer (current-buffer)))))
753
754 ;; These functions are responsible for inserting output from your debugger
755 ;; into the buffer. The hard work is done by the method that is
756 ;; the value of gud-marker-filter.
757
758 (defun gud-filter (proc string)
759 ;; Here's where the actual buffer insertion is done
760 (let ((inhibit-quit t))
761 (save-excursion
762 (set-buffer (process-buffer proc))
763 (let (moving output-after-point)
764 (save-excursion
765 (goto-char (process-mark proc))
766 ;; If we have been so requested, delete the debugger prompt.
767 (if (marker-buffer gud-delete-prompt-marker)
768 (progn
769 (delete-region (point) gud-delete-prompt-marker)
770 (set-marker gud-delete-prompt-marker nil)))
771 (insert-before-markers (gud-marker-filter string))
772 (setq moving (= (point) (process-mark proc)))
773 (setq output-after-point (< (point) (process-mark proc)))
774 ;; Check for a filename-and-line number.
775 ;; Don't display the specified file
776 ;; unless (1) point is at or after the position where output appears
777 ;; and (2) this buffer is on the screen.
778 (if (and gud-last-frame
779 (not output-after-point)
780 (get-buffer-window (current-buffer)))
781 (gud-display-frame)))
782 (if moving (goto-char (process-mark proc)))))))
783
784 (defun gud-sentinel (proc msg)
785 (cond ((null (buffer-name (process-buffer proc)))
786 ;; buffer killed
787 ;; Stop displaying an arrow in a source file.
788 (setq overlay-arrow-position nil)
789 (set-process-buffer proc nil))
790 ((memq (process-status proc) '(signal exit))
791 ;; Stop displaying an arrow in a source file.
792 (setq overlay-arrow-position nil)
793 ;; Fix the mode line.
794 (setq mode-line-process
795 (concat ": "
796 (symbol-name (process-status proc))))
797 (let* ((obuf (current-buffer)))
798 ;; save-excursion isn't the right thing if
799 ;; process-buffer is current-buffer
800 (unwind-protect
801 (progn
802 ;; Write something in *compilation* and hack its mode line,
803 (set-buffer (process-buffer proc))
804 ;; Force mode line redisplay soon
805 (set-buffer-modified-p (buffer-modified-p))
806 (if (eobp)
807 (insert ?\n mode-name " " msg)
808 (save-excursion
809 (goto-char (point-max))
810 (insert ?\n mode-name " " msg)))
811 ;; If buffer and mode line will show that the process
812 ;; is dead, we can delete it now. Otherwise it
813 ;; will stay around until M-x list-processes.
814 (delete-process proc))
815 ;; Restore old buffer, but don't restore old point
816 ;; if obuf is the gud buffer.
817 (set-buffer obuf))))))
818
819 (defun gud-display-frame ()
820 "Find and obey the last filename-and-line marker from the debugger.
821 Obeying it means displaying in another window the specified file and line."
822 (interactive)
823 (if gud-last-frame
824 (progn
825 (gud-set-buffer)
826 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
827 (setq gud-last-last-frame gud-last-frame
828 gud-last-frame nil))))
829
830 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
831 ;; and that its line LINE is visible.
832 ;; Put the overlay-arrow on the line LINE in that buffer.
833 ;; Most of the trickiness in here comes from wanting to preserve the current
834 ;; region-restriction if that's possible. We use an explicit display-buffer
835 ;; to get around the fact that this is called inside a save-excursion.
836
837 (defun gud-display-line (true-file line)
838 (let* ((buffer (gud-find-file true-file))
839 (window (display-buffer buffer))
840 (pos))
841 ;;; (if (equal buffer (current-buffer))
842 ;;; nil
843 ;;; (setq buffer-read-only nil))
844 (save-excursion
845 ;;; (setq buffer-read-only t)
846 (set-buffer buffer)
847 (save-restriction
848 (widen)
849 (goto-line line)
850 (setq pos (point))
851 (setq overlay-arrow-string "=>")
852 (or overlay-arrow-position
853 (setq overlay-arrow-position (make-marker)))
854 (set-marker overlay-arrow-position (point) (current-buffer)))
855 (cond ((or (< pos (point-min)) (> pos (point-max)))
856 (widen)
857 (goto-char pos))))
858 (set-window-point window overlay-arrow-position)))
859
860 ;;; The gud-call function must do the right thing whether its invoking
861 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
862 ;;; or a C buffer. In the former case, we want to supply data from
863 ;;; gud-last-frame. Here's how we do it:
864
865 (defun gud-format-command (str arg)
866 (let ((insource (not (eq (current-buffer) gud-comint-buffer))))
867 (if (string-match "\\(.*\\)%f\\(.*\\)" str)
868 (setq str (concat
869 (substring str (match-beginning 1) (match-end 1))
870 (file-name-nondirectory (if insource
871 (buffer-file-name)
872 (car gud-last-frame)))
873 (substring str (match-beginning 2) (match-end 2)))))
874 (if (string-match "\\(.*\\)%d\\(.*\\)" str)
875 (setq str (concat
876 (substring str (match-beginning 1) (match-end 1))
877 (file-name-directory (if insource
878 (buffer-file-name)
879 (car gud-last-frame)))
880 (substring str (match-beginning 2) (match-end 2)))))
881 (if (string-match "\\(.*\\)%l\\(.*\\)" str)
882 (setq str (concat
883 (substring str (match-beginning 1) (match-end 1))
884 (if insource
885 (save-excursion
886 (beginning-of-line)
887 (save-restriction (widen)
888 (1+ (count-lines 1 (point)))))
889 (cdr gud-last-frame))
890 (substring str (match-beginning 2) (match-end 2)))))
891 (if (string-match "\\(.*\\)%e\\(.*\\)" str)
892 (setq str (concat
893 (substring str (match-beginning 1) (match-end 1))
894 (find-c-expr)
895 (substring str (match-beginning 2) (match-end 2)))))
896 (if (string-match "\\(.*\\)%a\\(.*\\)" str)
897 (setq str (concat
898 (substring str (match-beginning 1) (match-end 1))
899 (gud-read-address)
900 (substring str (match-beginning 2) (match-end 2)))))
901 (if (string-match "\\(.*\\)%p\\(.*\\)" str)
902 (setq str (concat
903 (substring str (match-beginning 1) (match-end 1))
904 (if arg (int-to-string arg) "")
905 (substring str (match-beginning 2) (match-end 2)))))
906 )
907 str
908 )
909
910 (defun gud-read-address ()
911 "Return a string containing the core-address found in the buffer at point."
912 (save-excursion
913 (let ((pt (point)) found begin)
914 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
915 (cond
916 (found (forward-char 2)
917 (buffer-substring found
918 (progn (re-search-forward "[^0-9a-f]")
919 (forward-char -1)
920 (point))))
921 (t (setq begin (progn (re-search-backward "[^0-9]")
922 (forward-char 1)
923 (point)))
924 (forward-char 1)
925 (re-search-forward "[^0-9]")
926 (forward-char -1)
927 (buffer-substring begin (point)))))))
928
929 (defun gud-call (fmt &optional arg)
930 (let ((msg (gud-format-command fmt arg)))
931 (message "Command: %s" msg)
932 (sit-for 0)
933 (gud-basic-call msg)))
934
935 (defun gud-basic-call (command)
936 "Invoke the debugger COMMAND displaying source in other window."
937 (interactive)
938 (gud-set-buffer)
939 (let ((command (concat command "\n"))
940 (proc (get-buffer-process gud-comint-buffer)))
941
942 ;; Arrange for the current prompt to get deleted.
943 (save-excursion
944 (set-buffer gud-comint-buffer)
945 (goto-char (process-mark proc))
946 (beginning-of-line)
947 (if (looking-at comint-prompt-regexp)
948 (set-marker gud-delete-prompt-marker (point))))
949 (process-send-string proc command)))
950
951 (defun gud-refresh (&optional arg)
952 "Fix up a possibly garbled display, and redraw the arrow."
953 (interactive "P")
954 (recenter arg)
955 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
956 (gud-display-frame))
957 \f
958 ;;; Code for parsing expressions out of C code. The single entry point is
959 ;;; find-c-expr, which tries to return an lvalue expression from around point.
960 ;;;
961 ;;; The rest of this file is a hacked version of gdbsrc.el by
962 ;;; Debby Ayers <ayers@asc.slb.com>,
963 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
964
965 (defun find-c-expr ()
966 "Returns the C expr that surrounds point."
967 (interactive)
968 (save-excursion
969 (let ((p) (expr) (test-expr))
970 (setq p (point))
971 (setq expr (expr-cur))
972 (setq test-expr (expr-prev))
973 (while (expr-compound test-expr expr)
974 (setq expr (cons (car test-expr) (cdr expr)))
975 (goto-char (car expr))
976 (setq test-expr (expr-prev)))
977 (goto-char p)
978 (setq test-expr (expr-next))
979 (while (expr-compound expr test-expr)
980 (setq expr (cons (car expr) (cdr test-expr)))
981 (setq test-expr (expr-next))
982 )
983 (buffer-substring (car expr) (cdr expr)))))
984
985 (defun expr-cur ()
986 "Returns the expr that point is in; point is set to beginning of expr.
987 The expr is represented as a cons cell, where the car specifies the point in
988 the current buffer that marks the beginning of the expr and the cdr specifies
989 the character after the end of the expr."
990 (let ((p (point)) (begin) (end))
991 (expr-backward-sexp)
992 (setq begin (point))
993 (expr-forward-sexp)
994 (setq end (point))
995 (if (>= p end)
996 (progn
997 (setq begin p)
998 (goto-char p)
999 (expr-forward-sexp)
1000 (setq end (point))
1001 )
1002 )
1003 (goto-char begin)
1004 (cons begin end)))
1005
1006 (defun expr-backward-sexp ()
1007 "Version of `backward-sexp' that catches errors."
1008 (condition-case nil
1009 (backward-sexp)
1010 (error t)))
1011
1012 (defun expr-forward-sexp ()
1013 "Version of `forward-sexp' that catches errors."
1014 (condition-case nil
1015 (forward-sexp)
1016 (error t)))
1017
1018 (defun expr-prev ()
1019 "Returns the previous expr, point is set to beginning of that expr.
1020 The expr is represented as a cons cell, where the car specifies the point in
1021 the current buffer that marks the beginning of the expr and the cdr specifies
1022 the character after the end of the expr"
1023 (let ((begin) (end))
1024 (expr-backward-sexp)
1025 (setq begin (point))
1026 (expr-forward-sexp)
1027 (setq end (point))
1028 (goto-char begin)
1029 (cons begin end)))
1030
1031 (defun expr-next ()
1032 "Returns the following expr, point is set to beginning of that expr.
1033 The expr is represented as a cons cell, where the car specifies the point in
1034 the current buffer that marks the beginning of the expr and the cdr specifies
1035 the character after the end of the expr."
1036 (let ((begin) (end))
1037 (expr-forward-sexp)
1038 (expr-forward-sexp)
1039 (setq end (point))
1040 (expr-backward-sexp)
1041 (setq begin (point))
1042 (cons begin end)))
1043
1044 (defun expr-compound-sep (span-start span-end)
1045 "Returns '.' for '->' & '.', returns ' ' for white space,
1046 returns '?' for other punctuation."
1047 (let ((result ? )
1048 (syntax))
1049 (while (< span-start span-end)
1050 (setq syntax (char-syntax (char-after span-start)))
1051 (cond
1052 ((= syntax ? ) t)
1053 ((= syntax ?.) (setq syntax (char-after span-start))
1054 (cond
1055 ((= syntax ?.) (setq result ?.))
1056 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1057 (setq result ?.)
1058 (setq span-start (+ span-start 1)))
1059 (t (setq span-start span-end)
1060 (setq result ??)))))
1061 (setq span-start (+ span-start 1)))
1062 result))
1063
1064 (defun expr-compound (first second)
1065 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1066 The two exprs are represented as a cons cells, where the car
1067 specifies the point in the current buffer that marks the beginning of the
1068 expr and the cdr specifies the character after the end of the expr.
1069 Link exprs of the form:
1070 Expr -> Expr
1071 Expr . Expr
1072 Expr (Expr)
1073 Expr [Expr]
1074 (Expr) Expr
1075 [Expr] Expr"
1076 (let ((span-start (cdr first))
1077 (span-end (car second))
1078 (syntax))
1079 (setq syntax (expr-compound-sep span-start span-end))
1080 (cond
1081 ((= (car first) (car second)) nil)
1082 ((= (cdr first) (cdr second)) nil)
1083 ((= syntax ?.) t)
1084 ((= syntax ? )
1085 (setq span-start (char-after (- span-start 1)))
1086 (setq span-end (char-after span-end))
1087 (cond
1088 ((= span-start ?) ) t )
1089 ((= span-start ?] ) t )
1090 ((= span-end ?( ) t )
1091 ((= span-end ?[ ) t )
1092 (t nil))
1093 )
1094 (t nil))))
1095
1096 (provide 'gud)
1097
1098 ;;; gud.el ends here