]> code.delx.au - gnu-emacs/blob - lisp/gud.el
(Fwrite_region): Don't change START and END from nil
[gnu-emacs] / 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 ;; Functions for dbx on Mips/Ultrix.
355 ;; This is very similar to the code for gdb. The trick is to start dbx
356 ;; with the (undocumented) option `-emacs'.
357
358 ;; Are we running on a Mips system under Ultrix?
359 (defvar gud-dbx-mips-p (file-exists-p "/usr/include/mips"))
360
361 (defun gud-mipsdbx-massage-args (file args)
362 (cons "-emacs" (cons file args)))
363
364 ;; There's no guarantee that Emacs will hand the filter the entire
365 ;; marker at once; it could be broken up across several strings. We
366 ;; might even receive a big chunk with several markers in it. If we
367 ;; receive a chunk of text which looks like it might contain the
368 ;; beginning of a marker, we save it here between calls to the
369 ;; filter.
370 (defvar gud-mipsdbx-marker-acc "")
371
372 (defun gud-mipsdbx-marker-filter (string)
373 (save-match-data
374 (setq gud-mipsdbx-marker-acc (concat gud-mipsdbx-marker-acc string))
375 (let ((output ""))
376
377 ;; Process all the complete markers in this chunk.
378 (while (string-match
379 "^[] [0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
380 gud-mipsdbx-marker-acc)
381 (setq
382
383 ;; Extract the frame position from the marker.
384 gud-last-frame (cons
385 (substring gud-mipsdbx-marker-acc
386 (match-beginning 1) (match-end 1))
387 (string-to-int
388 (substring gud-mipsdbx-marker-acc
389 (match-beginning 2) (match-end 2))))
390
391 ;; Append any text before the marker to the output we're going
392 ;; to return - we don't include the marker in this text.
393 output (concat output (substring gud-mipsdbx-marker-acc
394 0 (match-beginning 0)))
395
396 ;; Set the accumulator to the remaining text.
397 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
398 (match-end 0))))
399
400 ;; Does the remaining text look like it might end with the
401 ;; beginning of another marker? If it does, then keep it in
402 ;; gud-mipsdbx-marker-acc until we receive the rest of it.
403 ;; Since we know the full marker regexp above failed, it's pretty
404 ;; simple to test for marker starts.
405 (if (string-match "^[] [0-9]*\032.*\\'" gud-mipsdbx-marker-acc)
406 (setq
407 ;; Everything before the potential marker start can be output.
408 output (concat output (substring gud-mipsdbx-marker-acc
409 0 (match-beginning 0)))
410 ;; Everything after, we save, to combine with later input.
411 gud-mipsdbx-marker-acc (substring gud-mipsdbx-marker-acc
412 (match-beginning 0)))
413 (setq output (concat output gud-mipsdbx-marker-acc)
414 gud-mipsdbx-marker-acc ""))
415
416 output)))
417
418 (defun gud-dbx-find-file (f)
419 (find-file-noselect f))
420
421 ;;;###autoload
422 (defun dbx (command-line)
423 "Run dbx on program FILE in buffer *gud-FILE*.
424 The directory containing FILE becomes the initial working directory
425 and source-file directory for your debugger."
426 (interactive
427 (list (read-from-minibuffer "Run dbx (like this): "
428 (if (consp gud-dbx-history)
429 (car gud-dbx-history)
430 "dbx ")
431 nil nil
432 '(gud-dbx-history . 1))))
433
434 (gud-overload-functions
435 (cond
436 (gud-dbx-mips-p
437 '((gud-massage-args . gud-mipsdbx-massage-args)
438 (gud-marker-filter . gud-mipsdbx-marker-filter)
439 (gud-find-file . gud-dbx-find-file)))
440 (t
441 '((gud-massage-args . gud-dbx-massage-args)
442 (gud-marker-filter . gud-dbx-marker-filter)
443 (gud-find-file . gud-dbx-find-file)))))
444
445 (gud-common-init command-line)
446
447 (cond
448 (gud-dbx-mips-p
449 (gud-def gud-break "stop at \"%f\":%l"
450 "\C-b" "Set breakpoint at current line.")
451 (gud-def gud-finish "return" "\C-f" "Finish executing current function."))
452 (t
453 (gud-def gud-break "file \"%d%f\"\nstop at %l"
454 "\C-b" "Set breakpoint at current line.")))
455
456 (gud-def gud-remove "clear %l" "\C-d" "Remove breakpoint at current line")
457 (gud-def gud-step "step %p" "\C-s" "Step one line with display.")
458 (gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
459 (gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
460 (gud-def gud-cont "cont" "\C-r" "Continue with display.")
461 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
462 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
463 (gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
464
465 (setq comint-prompt-regexp "^[^)\n]*dbx) *")
466 (run-hooks 'dbx-mode-hook)
467 )
468 \f
469 ;; ======================================================================
470 ;; xdb (HP PARISC debugger) functions
471
472 ;;; History of argument lists passed to xdb.
473 (defvar gud-xdb-history nil)
474
475 (defvar gud-xdb-directories nil
476 "*A list of directories that xdb should search for source code.
477 If nil, only source files in the program directory
478 will be known to xdb.
479
480 The file names should be absolute, or relative to the directory
481 containing the executable being debugged.")
482
483 (defun gud-xdb-massage-args (file args)
484 (nconc (let ((directories gud-xdb-directories)
485 (result nil))
486 (while directories
487 (setq result (cons (car directories) (cons "-d" result)))
488 (setq directories (cdr directories)))
489 (nreverse (cons file result)))
490 args))
491
492 (defun gud-xdb-file-name (f)
493 "Transform a relative pathname to a full pathname in xdb mode"
494 (let ((result nil))
495 (if (file-exists-p f)
496 (setq result (expand-file-name f))
497 (let ((directories gud-xdb-directories))
498 (while directories
499 (let ((path (concat (car directories) "/" f)))
500 (if (file-exists-p path)
501 (setq result (expand-file-name path)
502 directories nil)))
503 (setq directories (cdr directories)))))
504 result))
505
506 ;; xdb does not print the lines all at once, so we have to accumulate them
507 (defvar gud-xdb-accumulation "")
508
509 (defun gud-xdb-marker-filter (string)
510 (let (result)
511 (if (or (string-match comint-prompt-regexp string)
512 (string-match ".*\012" string))
513 (setq result (concat gud-xdb-accumulation string)
514 gud-xdb-accumulation "")
515 (setq gud-xdb-accumulation (concat gud-xdb-accumulation string)))
516 (if result
517 (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
518 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
519 result))
520 (let ((line (string-to-int
521 (substring result (match-beginning 2) (match-end 2))))
522 (file (gud-xdb-file-name
523 (substring result (match-beginning 1) (match-end 1)))))
524 (if file
525 (setq gud-last-frame (cons file line))))))
526 (or result "")))
527
528 (defun gud-xdb-find-file (f)
529 (let ((realf (gud-xdb-file-name f)))
530 (if realf (find-file-noselect realf))))
531
532 ;;;###autoload
533 (defun xdb (command-line)
534 "Run xdb on program FILE in buffer *gud-FILE*.
535 The directory containing FILE becomes the initial working directory
536 and source-file directory for your debugger.
537
538 You can set the variable 'gud-xdb-directories' to a list of program source
539 directories if your program contains sources from more than one directory."
540 (interactive
541 (list (read-from-minibuffer "Run xdb (like this): "
542 (if (consp gud-xdb-history)
543 (car gud-xdb-history)
544 "xdb ")
545 nil nil
546 '(gud-xdb-history . 1))))
547 (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
548 (gud-marker-filter . gud-xdb-marker-filter)
549 (gud-find-file . gud-xdb-find-file)))
550
551 (gud-common-init command-line)
552
553 (gud-def gud-break "b %f:%l" "\C-b" "Set breakpoint at current line.")
554 (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
555 "Set temporary breakpoint at current line.")
556 (gud-def gud-remove "db" "\C-d" "Remove breakpoint at current line")
557 (gud-def gud-step "s %p" "\C-s" "Step one line with display.")
558 (gud-def gud-next "S %p" "\C-n" "Step one line (skip functions).")
559 (gud-def gud-cont "c" "\C-r" "Continue with display.")
560 (gud-def gud-up "up %p" "<" "Up (numeric arg) stack frames.")
561 (gud-def gud-down "down %p" ">" "Down (numeric arg) stack frames.")
562 (gud-def gud-finish "bu\\t" "\C-f" "Finish executing current function.")
563 (gud-def gud-print "p %e" "\C-p" "Evaluate C expression at point.")
564
565 (setq comint-prompt-regexp "^>")
566 (make-local-variable 'gud-xdb-accumulation)
567 (setq gud-xdb-accumulation "")
568 (run-hooks 'xdb-mode-hook))
569 \f
570 ;; ======================================================================
571 ;; perldb functions
572
573 ;;; History of argument lists passed to perldb.
574 (defvar gud-perldb-history nil)
575
576 (defun gud-perldb-massage-args (file args)
577 (cons "-d" (cons file (cons "-emacs" args))))
578
579 ;; There's no guarantee that Emacs will hand the filter the entire
580 ;; marker at once; it could be broken up across several strings. We
581 ;; might even receive a big chunk with several markers in it. If we
582 ;; receive a chunk of text which looks like it might contain the
583 ;; beginning of a marker, we save it here between calls to the
584 ;; filter.
585 (defvar gud-perldb-marker-acc "")
586
587 (defun gud-perldb-marker-filter (string)
588 (save-match-data
589 (setq gud-perldb-marker-acc (concat gud-perldb-marker-acc string))
590 (let ((output ""))
591
592 ;; Process all the complete markers in this chunk.
593 (while (string-match "^\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
594 gud-perldb-marker-acc)
595 (setq
596
597 ;; Extract the frame position from the marker.
598 gud-last-frame
599 (cons (substring gud-perldb-marker-acc (match-beginning 1) (match-end 1))
600 (string-to-int (substring gud-perldb-marker-acc
601 (match-beginning 2)
602 (match-end 2))))
603
604 ;; Append any text before the marker to the output we're going
605 ;; to return - we don't include the marker in this text.
606 output (concat output
607 (substring gud-perldb-marker-acc 0 (match-beginning 0)))
608
609 ;; Set the accumulator to the remaining text.
610 gud-perldb-marker-acc (substring gud-perldb-marker-acc (match-end 0))))
611
612 ;; Does the remaining text look like it might end with the
613 ;; beginning of another marker? If it does, then keep it in
614 ;; gud-perldb-marker-acc until we receive the rest of it. Since we
615 ;; know the full marker regexp above failed, it's pretty simple to
616 ;; test for marker starts.
617 (if (string-match "^\032.*\\'" gud-perldb-marker-acc)
618 (progn
619 ;; Everything before the potential marker start can be output.
620 (setq output (concat output (substring gud-perldb-marker-acc
621 0 (match-beginning 0))))
622
623 ;; Everything after, we save, to combine with later input.
624 (setq gud-perldb-marker-acc
625 (substring gud-perldb-marker-acc (match-beginning 0))))
626
627 (setq output (concat output gud-perldb-marker-acc)
628 gud-perldb-marker-acc ""))
629
630 output)))
631
632 (defun gud-perldb-find-file (f)
633 (find-file-noselect f))
634
635 ;;;###autoload
636 (defun perldb (command-line)
637 "Run perldb on program FILE in buffer *gud-FILE*.
638 The directory containing FILE becomes the initial working directory
639 and source-file directory for your debugger."
640 (interactive
641 (list (read-from-minibuffer "Run perldb (like this): "
642 (if (consp gud-perldb-history)
643 (car gud-perldb-history)
644 "perl ")
645 nil nil
646 '(gud-perldb-history . 1))))
647 (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
648 (gud-marker-filter . gud-perldb-marker-filter)
649 (gud-find-file . gud-perldb-find-file)
650 ))
651
652 (gud-common-init command-line)
653
654 (gud-def gud-break "b %l" "\C-b" "Set breakpoint at current line.")
655 (gud-def gud-remove "d %l" "\C-d" "Remove breakpoint at current line")
656 (gud-def gud-step "s" "\C-s" "Step one source line with display.")
657 (gud-def gud-next "n" "\C-n" "Step one line (skip functions).")
658 (gud-def gud-cont "c" "\C-r" "Continue with display.")
659 ; (gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
660 ; (gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
661 ; (gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
662 (gud-def gud-print "%e" "\C-p" "Evaluate perl expression at point.")
663
664 (setq comint-prompt-regexp "^ DB<[0-9]+> ")
665 (run-hooks 'perldb-mode-hook)
666 )
667
668 ;;
669 ;; End of debugger-specific information
670 ;;
671
672 \f
673 ;;; When we send a command to the debugger via gud-call, it's annoying
674 ;;; to see the command and the new prompt inserted into the debugger's
675 ;;; buffer; we have other ways of knowing the command has completed.
676 ;;;
677 ;;; If the buffer looks like this:
678 ;;; --------------------
679 ;;; (gdb) set args foo bar
680 ;;; (gdb) -!-
681 ;;; --------------------
682 ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
683 ;;; source file to set a breakpoint, we want the buffer to end up like
684 ;;; this:
685 ;;; --------------------
686 ;;; (gdb) set args foo bar
687 ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
688 ;;; (gdb) -!-
689 ;;; --------------------
690 ;;; Essentially, the old prompt is deleted, and the command's output
691 ;;; and the new prompt take its place.
692 ;;;
693 ;;; Not echoing the command is easy enough; you send it directly using
694 ;;; process-send-string, and it never enters the buffer. However,
695 ;;; getting rid of the old prompt is trickier; you don't want to do it
696 ;;; when you send the command, since that will result in an annoying
697 ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
698 ;;; waits for a response from the debugger, and the new prompt is
699 ;;; inserted. Instead, we'll wait until we actually get some output
700 ;;; from the subprocess before we delete the prompt. If the command
701 ;;; produced no output other than a new prompt, that prompt will most
702 ;;; likely be in the first chunk of output received, so we will delete
703 ;;; the prompt and then replace it with an identical one. If the
704 ;;; command produces output, the prompt is moving anyway, so the
705 ;;; flicker won't be annoying.
706 ;;;
707 ;;; So - when we want to delete the prompt upon receipt of the next
708 ;;; chunk of debugger output, we position gud-delete-prompt-marker at
709 ;;; the start of the prompt; the process filter will notice this, and
710 ;;; delete all text between it and the process output marker. If
711 ;;; gud-delete-prompt-marker points nowhere, we leave the current
712 ;;; prompt alone.
713 (defvar gud-delete-prompt-marker nil)
714
715 \f
716 (defun gud-mode ()
717 "Major mode for interacting with an inferior debugger process.
718
719 You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
720 or M-x xdb. Each entry point finishes by executing a hook; `gdb-mode-hook',
721 `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
722
723 After startup, the following commands are available in both the GUD
724 interaction buffer and any source buffer GUD visits due to a breakpoint stop
725 or step operation:
726
727 \\[gud-break] sets a breakpoint at the current file and line. In the
728 GUD buffer, the current file and line are those of the last breakpoint or
729 step. In a source buffer, they are the buffer's file and current line.
730
731 \\[gud-remove] removes breakpoints on the current file and line.
732
733 \\[gud-refresh] displays in the source window the last line referred to
734 in the gud buffer.
735
736 \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
737 step-one-line (not entering function calls), and step-one-instruction
738 and then update the source window with the current file and position.
739 \\[gud-cont] continues execution.
740
741 \\[gud-print] tries to find the largest C lvalue or function-call expression
742 around point, and sends it to the debugger for value display.
743
744 The above commands are common to all supported debuggers except xdb which
745 does not support stepping instructions.
746
747 Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
748 except that the breakpoint is temporary; that is, it is removed when
749 execution stops on it.
750
751 Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
752 frame. \\[gud-down] drops back down through one.
753
754 If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
755 the current function and stops.
756
757 All the keystrokes above are accessible in the GUD buffer
758 with the prefix C-c, and in all buffers through the prefix C-x C-a.
759
760 All pre-defined functions for which the concept make sense repeat
761 themselves the appropriate number of times if you give a prefix
762 argument.
763
764 You may use the `gud-def' macro in the initialization hook to define other
765 commands.
766
767 Other commands for interacting with the debugger process are inherited from
768 comint mode, which see."
769 (interactive)
770 (comint-mode)
771 (setq major-mode 'gud-mode)
772 (setq mode-name "Debugger")
773 (setq mode-line-process '(": %s"))
774 (use-local-map (copy-keymap comint-mode-map))
775 (make-local-variable 'gud-last-frame)
776 (setq gud-last-frame nil)
777 (make-local-variable 'comint-prompt-regexp)
778 (make-local-variable 'gud-delete-prompt-marker)
779 (setq gud-delete-prompt-marker (make-marker))
780 (run-hooks 'gud-mode-hook)
781 )
782
783 (defvar gud-comint-buffer nil)
784
785 ;; Chop STRING into words separated by SPC or TAB and return a list of them.
786 (defun gud-chop-words (string)
787 (let ((i 0) (beg 0)
788 (len (length string))
789 (words nil))
790 (while (< i len)
791 (if (memq (aref string i) '(?\t ? ))
792 (progn
793 (setq words (cons (substring string beg i) words)
794 beg (1+ i))
795 (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
796 (setq beg (1+ beg)))
797 (setq i (1+ beg)))
798 (setq i (1+ i))))
799 (if (< beg len)
800 (setq words (cons (substring string beg) words)))
801 (nreverse words)))
802
803 ;; Perform initializations common to all debuggers.
804 (defun gud-common-init (command-line)
805 (let* ((words (gud-chop-words command-line))
806 (program (car words))
807 (file-word (let ((w (cdr words)))
808 (while (and w (= ?- (aref (car w) 0)))
809 (setq w (cdr w)))
810 (car w)))
811 (args (delq file-word (cdr words)))
812 (file (expand-file-name (substitute-in-file-name file-word)))
813 (filepart (file-name-nondirectory file)))
814 (switch-to-buffer (concat "*gud-" filepart "*"))
815 (setq default-directory (file-name-directory file))
816 (or (bolp) (newline))
817 (insert "Current directory is " default-directory "\n")
818 (apply 'make-comint (concat "gud-" filepart) program nil
819 (gud-massage-args file args)))
820 (gud-mode)
821 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
822 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
823 (gud-set-buffer)
824 )
825
826 (defun gud-set-buffer ()
827 (cond ((eq major-mode 'gud-mode)
828 (setq gud-comint-buffer (current-buffer)))))
829
830 ;; These functions are responsible for inserting output from your debugger
831 ;; into the buffer. The hard work is done by the method that is
832 ;; the value of gud-marker-filter.
833
834 (defun gud-filter (proc string)
835 ;; Here's where the actual buffer insertion is done
836 (let ((inhibit-quit t))
837 (save-excursion
838 (set-buffer (process-buffer proc))
839 (let (moving output-after-point)
840 (save-excursion
841 (goto-char (process-mark proc))
842 ;; If we have been so requested, delete the debugger prompt.
843 (if (marker-buffer gud-delete-prompt-marker)
844 (progn
845 (delete-region (point) gud-delete-prompt-marker)
846 (set-marker gud-delete-prompt-marker nil)))
847 (insert-before-markers (gud-marker-filter string))
848 (setq moving (= (point) (process-mark proc)))
849 (setq output-after-point (< (point) (process-mark proc)))
850 ;; Check for a filename-and-line number.
851 ;; Don't display the specified file
852 ;; unless (1) point is at or after the position where output appears
853 ;; and (2) this buffer is on the screen.
854 (if (and gud-last-frame
855 (not output-after-point)
856 (get-buffer-window (current-buffer)))
857 (gud-display-frame)))
858 (if moving (goto-char (process-mark proc)))))))
859
860 (defun gud-sentinel (proc msg)
861 (cond ((null (buffer-name (process-buffer proc)))
862 ;; buffer killed
863 ;; Stop displaying an arrow in a source file.
864 (setq overlay-arrow-position nil)
865 (set-process-buffer proc nil))
866 ((memq (process-status proc) '(signal exit))
867 ;; Stop displaying an arrow in a source file.
868 (setq overlay-arrow-position nil)
869 ;; Fix the mode line.
870 (setq mode-line-process
871 (concat ": "
872 (symbol-name (process-status proc))))
873 (let* ((obuf (current-buffer)))
874 ;; save-excursion isn't the right thing if
875 ;; process-buffer is current-buffer
876 (unwind-protect
877 (progn
878 ;; Write something in *compilation* and hack its mode line,
879 (set-buffer (process-buffer proc))
880 ;; Force mode line redisplay soon
881 (set-buffer-modified-p (buffer-modified-p))
882 (if (eobp)
883 (insert ?\n mode-name " " msg)
884 (save-excursion
885 (goto-char (point-max))
886 (insert ?\n mode-name " " msg)))
887 ;; If buffer and mode line will show that the process
888 ;; is dead, we can delete it now. Otherwise it
889 ;; will stay around until M-x list-processes.
890 (delete-process proc))
891 ;; Restore old buffer, but don't restore old point
892 ;; if obuf is the gud buffer.
893 (set-buffer obuf))))))
894
895 (defun gud-display-frame ()
896 "Find and obey the last filename-and-line marker from the debugger.
897 Obeying it means displaying in another window the specified file and line."
898 (interactive)
899 (if gud-last-frame
900 (progn
901 (gud-set-buffer)
902 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
903 (setq gud-last-last-frame gud-last-frame
904 gud-last-frame nil))))
905
906 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
907 ;; and that its line LINE is visible.
908 ;; Put the overlay-arrow on the line LINE in that buffer.
909 ;; Most of the trickiness in here comes from wanting to preserve the current
910 ;; region-restriction if that's possible. We use an explicit display-buffer
911 ;; to get around the fact that this is called inside a save-excursion.
912
913 (defun gud-display-line (true-file line)
914 (let* ((buffer (gud-find-file true-file))
915 (window (display-buffer buffer))
916 (pos))
917 ;;; (if (equal buffer (current-buffer))
918 ;;; nil
919 ;;; (setq buffer-read-only nil))
920 (save-excursion
921 ;;; (setq buffer-read-only t)
922 (set-buffer buffer)
923 (save-restriction
924 (widen)
925 (goto-line line)
926 (setq pos (point))
927 (setq overlay-arrow-string "=>")
928 (or overlay-arrow-position
929 (setq overlay-arrow-position (make-marker)))
930 (set-marker overlay-arrow-position (point) (current-buffer)))
931 (cond ((or (< pos (point-min)) (> pos (point-max)))
932 (widen)
933 (goto-char pos))))
934 (set-window-point window overlay-arrow-position)))
935
936 ;;; The gud-call function must do the right thing whether its invoking
937 ;;; keystroke is from the GUD buffer itself (via major-mode binding)
938 ;;; or a C buffer. In the former case, we want to supply data from
939 ;;; gud-last-frame. Here's how we do it:
940
941 (defun gud-format-command (str arg)
942 (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
943 (frame (or gud-last-frame gud-last-last-frame))
944 result)
945 (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
946 (let ((key (string-to-char (substring str (match-beginning 2))))
947 subst)
948 (cond
949 ((eq key ?f)
950 (setq subst (file-name-nondirectory (if insource
951 (buffer-file-name)
952 (car frame)))))
953 ((eq key ?d)
954 (setq subst (file-name-directory (if insource
955 (buffer-file-name)
956 (car frame)))))
957 ((eq key ?l)
958 (setq subst (if insource
959 (save-excursion
960 (beginning-of-line)
961 (save-restriction (widen)
962 (1+ (count-lines 1 (point)))))
963 (cdr frame))))
964 ((eq key ?e)
965 (setq subst (find-c-expr)))
966 ((eq key ?a)
967 (setq subst (gud-read-address)))
968 ((eq key ?p)
969 (setq subst (if arg (int-to-string arg) ""))))
970 (setq result (concat result
971 (substring str (match-beginning 1) (match-end 1))
972 subst)))
973 (setq str (substring str (match-end 2))))
974 ;; There might be text left in STR when the loop ends.
975 (concat result str)))
976
977 (defun gud-read-address ()
978 "Return a string containing the core-address found in the buffer at point."
979 (save-excursion
980 (let ((pt (point)) found begin)
981 (setq found (if (search-backward "0x" (- pt 7) t) (point)))
982 (cond
983 (found (forward-char 2)
984 (buffer-substring found
985 (progn (re-search-forward "[^0-9a-f]")
986 (forward-char -1)
987 (point))))
988 (t (setq begin (progn (re-search-backward "[^0-9]")
989 (forward-char 1)
990 (point)))
991 (forward-char 1)
992 (re-search-forward "[^0-9]")
993 (forward-char -1)
994 (buffer-substring begin (point)))))))
995
996 (defun gud-call (fmt &optional arg)
997 (let ((msg (gud-format-command fmt arg)))
998 (message "Command: %s" msg)
999 (sit-for 0)
1000 (gud-basic-call msg)))
1001
1002 (defun gud-basic-call (command)
1003 "Invoke the debugger COMMAND displaying source in other window."
1004 (interactive)
1005 (gud-set-buffer)
1006 (let ((command (concat command "\n"))
1007 (proc (get-buffer-process gud-comint-buffer)))
1008
1009 ;; Arrange for the current prompt to get deleted.
1010 (save-excursion
1011 (set-buffer gud-comint-buffer)
1012 (goto-char (process-mark proc))
1013 (beginning-of-line)
1014 (if (looking-at comint-prompt-regexp)
1015 (set-marker gud-delete-prompt-marker (point))))
1016 (process-send-string proc command)))
1017
1018 (defun gud-refresh (&optional arg)
1019 "Fix up a possibly garbled display, and redraw the arrow."
1020 (interactive "P")
1021 (recenter arg)
1022 (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
1023 (gud-display-frame))
1024 \f
1025 ;;; Code for parsing expressions out of C code. The single entry point is
1026 ;;; find-c-expr, which tries to return an lvalue expression from around point.
1027 ;;;
1028 ;;; The rest of this file is a hacked version of gdbsrc.el by
1029 ;;; Debby Ayers <ayers@asc.slb.com>,
1030 ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
1031
1032 (defun find-c-expr ()
1033 "Returns the C expr that surrounds point."
1034 (interactive)
1035 (save-excursion
1036 (let ((p) (expr) (test-expr))
1037 (setq p (point))
1038 (setq expr (expr-cur))
1039 (setq test-expr (expr-prev))
1040 (while (expr-compound test-expr expr)
1041 (setq expr (cons (car test-expr) (cdr expr)))
1042 (goto-char (car expr))
1043 (setq test-expr (expr-prev)))
1044 (goto-char p)
1045 (setq test-expr (expr-next))
1046 (while (expr-compound expr test-expr)
1047 (setq expr (cons (car expr) (cdr test-expr)))
1048 (setq test-expr (expr-next))
1049 )
1050 (buffer-substring (car expr) (cdr expr)))))
1051
1052 (defun expr-cur ()
1053 "Returns the expr that point is in; point is set to beginning of expr.
1054 The expr is represented as a cons cell, where the car specifies the point in
1055 the current buffer that marks the beginning of the expr and the cdr specifies
1056 the character after the end of the expr."
1057 (let ((p (point)) (begin) (end))
1058 (expr-backward-sexp)
1059 (setq begin (point))
1060 (expr-forward-sexp)
1061 (setq end (point))
1062 (if (>= p end)
1063 (progn
1064 (setq begin p)
1065 (goto-char p)
1066 (expr-forward-sexp)
1067 (setq end (point))
1068 )
1069 )
1070 (goto-char begin)
1071 (cons begin end)))
1072
1073 (defun expr-backward-sexp ()
1074 "Version of `backward-sexp' that catches errors."
1075 (condition-case nil
1076 (backward-sexp)
1077 (error t)))
1078
1079 (defun expr-forward-sexp ()
1080 "Version of `forward-sexp' that catches errors."
1081 (condition-case nil
1082 (forward-sexp)
1083 (error t)))
1084
1085 (defun expr-prev ()
1086 "Returns the previous expr, point is set to beginning of that expr.
1087 The expr is represented as a cons cell, where the car specifies the point in
1088 the current buffer that marks the beginning of the expr and the cdr specifies
1089 the character after the end of the expr"
1090 (let ((begin) (end))
1091 (expr-backward-sexp)
1092 (setq begin (point))
1093 (expr-forward-sexp)
1094 (setq end (point))
1095 (goto-char begin)
1096 (cons begin end)))
1097
1098 (defun expr-next ()
1099 "Returns the following expr, point is set to beginning of that expr.
1100 The expr is represented as a cons cell, where the car specifies the point in
1101 the current buffer that marks the beginning of the expr and the cdr specifies
1102 the character after the end of the expr."
1103 (let ((begin) (end))
1104 (expr-forward-sexp)
1105 (expr-forward-sexp)
1106 (setq end (point))
1107 (expr-backward-sexp)
1108 (setq begin (point))
1109 (cons begin end)))
1110
1111 (defun expr-compound-sep (span-start span-end)
1112 "Returns '.' for '->' & '.', returns ' ' for white space,
1113 returns '?' for other punctuation."
1114 (let ((result ? )
1115 (syntax))
1116 (while (< span-start span-end)
1117 (setq syntax (char-syntax (char-after span-start)))
1118 (cond
1119 ((= syntax ? ) t)
1120 ((= syntax ?.) (setq syntax (char-after span-start))
1121 (cond
1122 ((= syntax ?.) (setq result ?.))
1123 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
1124 (setq result ?.)
1125 (setq span-start (+ span-start 1)))
1126 (t (setq span-start span-end)
1127 (setq result ??)))))
1128 (setq span-start (+ span-start 1)))
1129 result))
1130
1131 (defun expr-compound (first second)
1132 "Non-nil if concatenating FIRST and SECOND makes a single C token.
1133 The two exprs are represented as a cons cells, where the car
1134 specifies the point in the current buffer that marks the beginning of the
1135 expr and the cdr specifies the character after the end of the expr.
1136 Link exprs of the form:
1137 Expr -> Expr
1138 Expr . Expr
1139 Expr (Expr)
1140 Expr [Expr]
1141 (Expr) Expr
1142 [Expr] Expr"
1143 (let ((span-start (cdr first))
1144 (span-end (car second))
1145 (syntax))
1146 (setq syntax (expr-compound-sep span-start span-end))
1147 (cond
1148 ((= (car first) (car second)) nil)
1149 ((= (cdr first) (cdr second)) nil)
1150 ((= syntax ?.) t)
1151 ((= syntax ? )
1152 (setq span-start (char-after (- span-start 1)))
1153 (setq span-end (char-after span-end))
1154 (cond
1155 ((= span-start ?) ) t )
1156 ((= span-start ?] ) t )
1157 ((= span-end ?( ) t )
1158 ((= span-end ?[ ) t )
1159 (t nil))
1160 )
1161 (t nil))))
1162
1163 (provide 'gud)
1164
1165 ;;; gud.el ends here