]> code.delx.au - gnu-emacs/blob - lisp/gud.el
entered into RCS
[gnu-emacs] / lisp / gud.el
1 ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, or dbx under Emacs
2
3 ;; Author: Eric S. Raymond <eric@snark.thyrsus.com>
4 ;; Keywords: unix, tools
5
6 ;; %W%
7
8 ;; Copyright (C) 1992 Free Software Foundation, Inc.
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to
24 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
29 ;; It was later rewritten by rms. Some ideas were due to Masanobu.
30 ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
31 ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
32 ;; who also hacked the mode to use comint.el.
33
34 ;; Note: use of this package with sdb requires that your tags.el support
35 ;; the find-tag-noselect entry point. Stock distributions up to 18.57 do
36 ;; *not* include this feature; if it's not included with this file, email
37 ;; esr@snark.thyrsus.com for it or get 18.58.
38
39 ;; Further note: due to lossage in the Emacs-18 byte compiler, compiled
40 ;; versions of this code will fail with a complaint about gud-step if
41 ;; you invoke the gdb or sdb initializers. This should be fixed in 19.
42
43 ;;; Code:
44
45 (require 'comint)
46 (require 'etags)
47
48 ;; ======================================================================
49 ;; the overloading mechanism
50
51 (defun gud-overload-functions (gud-overload-alist)
52 "Overload functions defined in GUD-OVERLOAD-ALIST.
53 This association list has elements of the form
54 (ORIGINAL-FUNCTION-NAME OVERLOAD-FUNCTION)"
55 (mapcar
56 (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
57 gud-overload-alist))
58
59 (defun gud-debugger-startup (f d)
60 (error "GUD not properly entered."))
61
62 (defun gud-marker-filter (proc s)
63 (error "GUD not properly entered."))
64
65 (defun gud-visit-file (f)
66 (error "GUD not properly entered."))
67
68 (defun gud-set-break (proc f n)
69 (error "GUD not properly entered."))
70
71 ;; This macro is used below to define some basic debugger interface commands.
72 ;; Of course you may use `gud-def' with any other debugger command, including
73 ;; user defined ones.
74
75 (defmacro gud-def (func name key &optional doc)
76 (let* ((cstr (list 'if '(not (= 1 arg))
77 (list 'format "%s %s" name 'arg) name)))
78 (list 'progn
79 (list 'defun func '(arg)
80 (or doc "")
81 '(interactive "p")
82 (list 'gud-call cstr))
83 (if key
84 (list 'define-key 'gud-mode-map key (list 'quote func))))))
85
86 ;; All debugger-specific information is collected here
87 ;; Here's how it works, in case you ever need to add a debugger to the table.
88 ;;
89 ;; Each entry must define the following at startup:
90 ;;
91 ;;<name>
92 ;; comint-prompt-regexp
93 ;; gud-<name>-debugger-startup
94 ;; gud-<name>-marker-filter
95 ;; gud-<name>-visit-file
96 ;; gud-<name>-set-break
97 ;;
98 ;; The job of the startup-command method is to fire up a copy of the debugger,
99 ;; given an object file and source directory.
100 ;;
101 ;; The job of the marker-filter method is to detect file/line markers in
102 ;; strings and set the global gud-last-frame to indicate what display
103 ;; action (if any) should be triggered by the marker. Note that only
104 ;; whetever the method *returns* is displayed in the buffer; thus, you
105 ;; can filter the debugger's output, interpreting some and passing on
106 ;; the rest.
107 ;;
108 ;; The job of the visit-file method is to visit and return the buffer indicated
109 ;; by the car of gud-tag-frame. This may be a file name, a tag name, or
110 ;; something else.
111 ;;
112 ;; The job of the gud-set-break method is to send the commands necessary
113 ;; to set a breakpoint at a given line in a given source file.
114 ;;
115 ;; Debugger-specific information begins here:
116
117 ;; ======================================================================
118 ;; gdb functions
119
120 (defun gud-gdb-debugger-startup (f d)
121 (make-comint (concat "gud-" f) "gdb" nil "-fullname" "-cd" d f))
122
123 (defun gud-gdb-marker-filter (proc s)
124 (if (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n" s)
125 (progn
126 (setq gud-last-frame
127 (cons
128 (substring string (match-beginning 1) (match-end 1))
129 (string-to-int
130 (substring string (match-beginning 2) (match-end 2)))))
131 ;; this computation means the ^Z^Z-initiated marker in the
132 ;; input string is never emitted.
133 (concat
134 (substring string 0 (match-beginning 0))
135 (substring string (match-end 0))
136 ))
137 string))
138
139 (defun gud-gdb-visit-file (f)
140 (find-file-noselect f))
141
142 (defun gud-gdb-set-break (proc f n)
143 (gud-call "break %s:%d" f n))
144
145 ;;;###autoload
146 (defun gdb (path)
147 "Run gdb on program FILE in buffer *gud-FILE*.
148 The directory containing FILE becomes the initial working directory
149 and source-file directory for your debugger."
150 (interactive "fRun gdb on file: ")
151 (gud-overload-functions '((gud-debugger-startup . gud-gdb-debugger-startup)
152 (gud-marker-filter . gud-gdb-marker-filter)
153 (gud-visit-file . gud-gdb-visit-file)
154 (gud-set-break . gud-gdb-set-break)))
155
156 (gud-def gud-step "step" "\C-c\C-s" "Step one source line with display")
157 (gud-def gud-stepi "stepi" "\C-c\C-i" "Step one instruction with display")
158 (gud-def gud-next "next" "\C-c\C-n" "Step one line (skip functions)")
159 (gud-def gud-cont "cont" "\C-c\C-c" "Continue with display")
160
161 (gud-def gud-finish "finish" "\C-c\C-f" "Finish executing current function")
162 (gud-def gud-up "up" "\C-c<" "Up N stack frames (numeric arg)")
163 (gud-def gud-down "down" "\C-c>" "Down N stack frames (numeric arg)")
164
165 (gud-common-init path)
166
167 (setq comint-prompt-regexp "^(.*gdb[+]?) *")
168 (run-hooks 'gdb-mode-hook)
169 )
170
171
172 ;; ======================================================================
173 ;; sdb functions
174
175 (defun gud-sdb-debugger-startup (f d)
176 (make-comint (concat "gud-" f) "sdb" nil f "-" d))
177
178 (defun gud-sdb-marker-filter (proc str)
179 (if (string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
180 str)
181 (setq gud-last-frame
182 (cons
183 (substring string (match-beginning 2) (match-end 2))
184 (string-to-int
185 (substring string (match-beginning 3) (match-end 3))))))
186 string)
187
188 (defun gud-sdb-visit-file (f)
189 (find-tag-noselect f))
190
191 (defun gud-sdb-set-break (proc f n)
192 (gud-queue-send (format "e %s" f) (format "%d b" n)))
193
194 ;;;###autoload
195 (defun sdb (path)
196 "Run sdb on program FILE in buffer *gud-FILE*.
197 The directory containing FILE becomes the initial working directory
198 and source-file directory for your debugger."
199 (interactive "fRun sdb on file: ")
200 (if (not (and (boundp 'tags-file-name) (file-exists-p tags-file-name)))
201 (error "The sdb support requires a valid tags table to work."))
202 (gud-overload-functions '((gud-debugger-startup . gud-sdb-debugger-startup)
203 (gud-marker-filter . gud-sdb-marker-filter)
204 (gud-visit-file . gud-sdb-visit-file)
205 (gud-set-break . gud-sdb-set-break)))
206
207 (gud-def gud-step "s" "\C-c\C-s" "Step one source line with display")
208 (gud-def gud-stepi "i" "\C-c\C-i" "Step one instruction with display")
209 (gud-def gud-next "S" "\C-c\C-n" "Step one source line (skip functions)")
210 (gud-def gud-cont "c" "\C-c\C-c" "Continue with display")
211
212 (gud-common-init path)
213
214 (setq comint-prompt-pattern "\\(^\\|\n\\)\\*")
215 (run-hooks 'sdb-mode-hook)
216 )
217
218 ;; ======================================================================
219 ;; dbx functions
220
221 (defun gud-dbx-debugger-startup (f d)
222 (make-comint (concat "gud-" file) "dbx" nil f))
223
224 (defun gud-dbx-marker-filter (proc str)
225 (if (string-match
226 "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\"" str)
227 (setq gud-last-frame
228 (cons
229 (substring string (match-beginning 2) (match-end 2))
230 (string-to-int
231 (substring string (match-beginning 1) (match-end 1))))))
232 string)
233
234 (defun gud-dbx-visit-file (f)
235 (find-file-noselect f))
236
237 (defun gud-dbx-set-break (proc f n)
238 (gud-call "stop at \"%s\":%d" f n))
239
240 ;;;###autoload
241 (defun dbx (path)
242 "Run dbx on program FILE in buffer *gud-FILE*.
243 The directory containing FILE becomes the initial working directory
244 and source-file directory for your debugger."
245 (interactive "fRun dbx on file: ")
246 (gud-overload-functions '((gud-debugger-startup . gud-dbx-debugger-startup)
247 (gud-marker-filter . gud-dbx-marker-filter)
248 (gud-visit-file . gud-dbx-visit-file)
249 (gud-set-break . gud-dbx-set-break)))
250
251 (gud-def gud-step "step" "\C-c\C-s" "Step one source line with display")
252 (gud-def gud-stepi "stepi" "\C-c\C-i" "Step one instruction with display")
253 (gud-def gud-next "next" "\C-c\C-n" "Step one line (skip functions)")
254 (gud-def gud-cont "cont" "\C-c\C-c" "Continue with display")
255
256 (gud-def gud-up "up" "\C-c<" "Up N stack frames (numeric arg)")
257 (gud-def gud-down "down" "\C-c>" "Down N stack frames (numeric arg)")
258
259 (gud-common-init path)
260 (setq comint-prompt-regexp "^[^)]*dbx) *")
261
262 (run-hooks 'dbx-mode-hook)
263 )
264
265 ;;
266 ;; End of debugger-specific information
267 ;;
268
269 (defvar gud-mode-map nil
270 "Keymap for gud-mode.")
271
272 (defvar gud-commands nil
273 "List of strings or functions used by send-gud-command.
274 It is for customization by you.")
275
276 (defvar gud-command-queue nil)
277
278 (if gud-mode-map
279 nil
280 (setq gud-mode-map (copy-keymap comint-mode-map))
281 (define-key gud-mode-map "\C-cl" 'gud-refresh))
282
283 ;; Global mappings --- we'll invoke these from a source buffer.
284 (define-key ctl-x-map " " 'gud-break)
285 (define-key ctl-x-map "&" 'send-gud-command)
286
287 \f
288 (defun gud-mode ()
289 "Major mode for interacting with an inferior debugger process.
290
291 You start it up with one of the commands M-x gdb, M-x sdb, or
292 M-x dbx. Each entry point finishes by executing a hook; gdb-mode-hook,
293 sdb-mode-hook or dbx-mode-hook respectively.
294
295 After startup, the following commands are available:
296
297 \\{gud-mode-map}
298
299 \\[gud-refresh] displays in the other window the last line referred to
300 in the gud buffer.
301
302 \\[gud-step], \\[gud-next], and \\[gud-stepi] in the gud window,
303 do a step-one-line, step-one-line (not entering function calls), and
304 step-one-instruction and then update the other window
305 with the current file and position. \\[gud-cont] continues
306 execution.
307
308 The above commands are common to all supported debuggers. If you are
309 using gdb or dbx, the following additional commands will be available:
310
311 \\[gud-up] pops up through an enclosing stack frame. \\[gud-down] drops
312 back down through one.
313
314 If you are using gdb, \\[gdb-finish] runs execution to the return from
315 the current function and stops.
316
317 These functions repeat themselves the appropriate number of times if you give a
318 prefix argument.
319
320 If you are in a source file, you may do the following:
321
322 Set a breakpoint at the current line by doing \\[gud-break]. This causes
323 an appropriate set-break to be send to the debugger; of course, if the file
324 you're visiting doesn't correspond to any code in the executable this will
325 have no effect or raise an error.
326
327 Execute a user-defined command at point with \\[send-gud-command]; the
328 prefix argument is taken as an index into the list of strings gud-commands.
329 A %s in a gud-commands string is substituted with a number or address picked
330 up from point.
331
332 Other commands for interacting with the debugger process are inherited from
333 comint mode, which see."
334 (interactive)
335 (comint-mode)
336 ; (kill-all-local-variables)
337 (setq major-mode 'gud-mode)
338 (setq mode-name "Debugger")
339 (setq mode-line-process '(": %s"))
340 (use-local-map gud-mode-map)
341 (make-local-variable 'gud-last-frame)
342 (setq gud-last-frame nil)
343 (make-local-variable 'comint-prompt-regexp)
344 (run-hooks 'gud-mode-hook)
345 )
346
347 (defvar current-gud-buffer nil)
348
349 (defun gud-common-init (path)
350 ;; perform initializations common to all debuggers
351 (setq path (expand-file-name path))
352 (let ((file (file-name-nondirectory path)))
353 (switch-to-buffer (concat "*gud-" file "*"))
354 (setq default-directory (file-name-directory path))
355 (or (bolp) (newline))
356 (insert "Current directory is " default-directory "\n")
357 (gud-debugger-startup file default-directory))
358 (gud-mode)
359 (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
360 (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
361 (setq gud-command-queue nil)
362 (gud-set-buffer)
363 )
364
365 (defun gud-set-buffer ()
366 (cond ((eq major-mode 'gud-mode)
367 (setq current-gud-buffer (current-buffer)))))
368 \f
369 (defun gud-filter (proc string)
370 ;; This function is responsible for inserting output from your debugger
371 ;; into the buffer. The hard work is done by the method that is
372 ;; the value of gud-marker-filter.
373 (let ((inhibit-quit t))
374 (gud-filter-insert proc (gud-marker-filter proc string))
375 ;; If we've got queued commands and we see a prompt, pop one and send it.
376 ;; In theory we should check that a prompt has been issued before sending
377 ;; queued commands. In practice, command responses from the first through
378 ;; penultimate elements of a command sequence are short enough that we
379 ;; don't really have to bother.
380 (if gud-command-queue
381 (progn
382 (gud-call (car gud-command-queue))
383 (setq gud-command-queue (cdr gud-command-queue))
384 )
385 )))
386
387 (defun gud-filter-insert (proc string)
388 ;; Here's where the actual buffer insertion is done
389 (let ((moving (= (point) (process-mark proc)))
390 (output-after-point (< (point) (process-mark proc)))
391 (old-buffer (current-buffer))
392 start)
393 (set-buffer (process-buffer proc))
394 (unwind-protect
395 (save-excursion
396 ;; Insert the text, moving the process-marker.
397 (goto-char (process-mark proc))
398 (setq start (point))
399 (insert-before-markers string)
400 (set-marker (process-mark proc) (point))
401 ;; Check for a filename-and-line number.
402 ;; Don't display the specified file
403 ;; unless (1) point is at or after the position where output appears
404 ;; and (2) this buffer is on the screen.
405 (if (and gud-last-frame (not output-after-point)
406 (get-buffer-window (current-buffer)))
407 (gud-display-frame))
408 )
409 (set-buffer old-buffer))
410 (if moving (goto-char (process-mark proc)))))
411
412 (defun gud-sentinel (proc msg)
413 (cond ((null (buffer-name (process-buffer proc)))
414 ;; buffer killed
415 ;; Stop displaying an arrow in a source file.
416 (setq overlay-arrow-position nil)
417 (set-process-buffer proc nil))
418 ((memq (process-status proc) '(signal exit))
419 ;; Stop displaying an arrow in a source file.
420 (setq overlay-arrow-position nil)
421 ;; Fix the mode line.
422 (setq mode-line-process
423 (concat ": "
424 (symbol-name (process-status proc))))
425 (let* ((obuf (current-buffer)))
426 ;; save-excursion isn't the right thing if
427 ;; process-buffer is current-buffer
428 (unwind-protect
429 (progn
430 ;; Write something in *compilation* and hack its mode line,
431 (set-buffer (process-buffer proc))
432 ;; Force mode line redisplay soon
433 (set-buffer-modified-p (buffer-modified-p))
434 (if (eobp)
435 (insert ?\n mode-name " " msg)
436 (save-excursion
437 (goto-char (point-max))
438 (insert ?\n mode-name " " msg)))
439 ;; If buffer and mode line will show that the process
440 ;; is dead, we can delete it now. Otherwise it
441 ;; will stay around until M-x list-processes.
442 (delete-process proc))
443 ;; Restore old buffer, but don't restore old point
444 ;; if obuf is the gud buffer.
445 (set-buffer obuf))))))
446
447
448 (defun gud-refresh (&optional arg)
449 "Fix up a possibly garbled display, and redraw the arrow."
450 (interactive "P")
451 (recenter arg)
452 (gud-display-frame))
453
454 (defun gud-display-frame ()
455 "Find and obey the last filename-and-line marker from the debugger.
456 Obeying it means displaying in another window the specified file and line."
457 (interactive)
458 (if gud-last-frame
459 (progn
460 (gud-set-buffer)
461 (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
462 (setq gud-last-frame nil))))
463
464 ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
465 ;; and that its line LINE is visible.
466 ;; Put the overlay-arrow on the line LINE in that buffer.
467
468 (defun gud-display-line (true-file line)
469 (let* ((buffer (gud-visit-file true-file))
470 (window (display-buffer buffer t))
471 (pos))
472 (save-excursion
473 (set-buffer buffer)
474 (save-restriction
475 (widen)
476 (goto-line line)
477 (setq pos (point))
478 (setq overlay-arrow-string "=>")
479 (or overlay-arrow-position
480 (setq overlay-arrow-position (make-marker)))
481 (set-marker overlay-arrow-position (point) (current-buffer)))
482 (cond ((or (< pos (point-min)) (> pos (point-max)))
483 (widen)
484 (goto-char pos))))
485 (set-window-point window overlay-arrow-position)))
486 \f
487 (defun gud-call (command &rest args)
488 "Invoke the debugger COMMAND displaying source in other window."
489 (interactive)
490 (gud-set-buffer)
491 (goto-char (point-max))
492 (let ((command (concat (apply 'format command args) "\n"))
493 (proc (get-buffer-process current-gud-buffer)))
494 (gud-filter-insert proc command)
495 (send-string proc command)
496 ))
497
498 (defun gud-queue-send (&rest cmdlist)
499 ;; Send the first command, queue the rest for send after successive
500 ;; send on subsequent prompts
501 (interactive)
502 (gud-call (car cmdlist))
503 (setq gud-command-queue (append gud-command-queue (cdr cmdlist))))
504
505 (defun gud-apply-from-source (func)
506 ;; Apply a method from the gud buffer environment, passing it file and line.
507 ;; This is intended to be used for gud commands called from a source file.
508 (if (not buffer-file-name)
509 (error "There is no file associated with this buffer"))
510 (let ((file (file-name-nondirectory buffer-file-name))
511 (line (save-restriction (widen) (1+ (count-lines 1 (point))))))
512 (save-excursion
513 (gud-set-buffer)
514 (funcall func
515 (get-buffer-process current-gud-buffer)
516 file
517 line)
518 )))
519
520 (defun gud-break ()
521 "Set breakpoint at this source line."
522 (interactive)
523 (gud-apply-from-source 'gud-set-break))
524
525 (defun gud-read-address()
526 "Return a string containing the core-address found in the buffer at point."
527 (save-excursion
528 (let ((pt (point)) found begin)
529 (setq found (if (search-backward "0x" (- pt 7) t)(point)))
530 (cond (found (forward-char 2)
531 (setq result
532 (buffer-substring found
533 (progn (re-search-forward "[^0-9a-f]")
534 (forward-char -1)
535 (point)))))
536 (t (setq begin (progn (re-search-backward "[^0-9]") (forward-char 1)
537 (point)))
538 (forward-char 1)
539 (re-search-forward "[^0-9]")
540 (forward-char -1)
541 (buffer-substring begin (point)))))))
542
543
544 (defun send-gud-command (arg)
545 "This command reads the number where the cursor is positioned. A numeric arg
546 selects the ARG'th member COMMAND of the list gud-commands. If COMMAND is a
547 string, (format COMMAND ADDR) is inserted at the end of the debugger buffer,
548 otherwise (funcall COMMAND ADDR) is inserted.
549 For example, \"p (rtx)%s->fld[0].rtint\" is a possible string to be a
550 member of gud-commands."
551 (interactive "P")
552 (let (comm addr)
553 (if arg (setq comm (nth arg gud-commands)))
554 (setq addr (gud-read-address))
555 (if (eq (current-buffer) current-gud-buffer)
556 (set-mark (point)))
557 (cond (comm
558 (setq comm
559 (if (stringp comm) (format comm addr) (funcall comm addr))))
560 (t (setq comm addr)))
561 (switch-to-buffer current-gud-buffer)
562 (goto-char (point-max))
563 (insert-string comm)))
564
565 ;;; gud.el ends here