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