home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / gud.el < prev    next >
Encoding:
Text File  |  1994-05-29  |  50.2 KB  |  1,358 lines

  1. ;;; gud.el --- Grand Unified Debugger mode for gdb, sdb, dbx, or xdb
  2. ;;;            under Emacs
  3.  
  4. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  5. ;; Maintainer: FSF
  6. ;; Version: 1.3
  7. ;; Keywords: unix, tools
  8.  
  9. ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  10.  
  11. ;; This file is part of GNU Emacs.
  12.  
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  25. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; The ancestral gdb.el was by W. Schelter <wfs@rascal.ics.utexas.edu>
  30. ;; It was later rewritten by rms.  Some ideas were due to Masanobu. 
  31. ;; Grand Unification (sdb/dbx support) by Eric S. Raymond <esr@thyrsus.com>
  32. ;; The overloading code was then rewritten by Barry Warsaw <bwarsaw@cen.com>,
  33. ;; who also hacked the mode to use comint.el.  Shane Hartman <shane@spr.com>
  34. ;; added support for xdb (HPUX debugger).  Rick Sladkey <jrs@world.std.com>
  35. ;; wrote the GDB command completion code.  Dave Love <d.love@dl.ac.uk>
  36. ;; added the IRIX kluge and re-implemented the Mips-ish variant.
  37.  
  38. ;;; Code:
  39.  
  40. (require 'comint)
  41. (require 'etags)
  42.  
  43. ;; ======================================================================
  44. ;; GUD commands must be visible in C buffers visited by GUD
  45.  
  46. (defvar gud-key-prefix "\C-x\C-a"
  47.   "Prefix of all GUD commands valid in C buffers.")
  48.  
  49. (global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
  50. (define-key ctl-x-map " " 'gud-break)    ;; backward compatibility hack
  51.  
  52. ;; ======================================================================
  53. ;; the overloading mechanism
  54.  
  55. (defun gud-overload-functions (gud-overload-alist)
  56.   "Overload functions defined in GUD-OVERLOAD-ALIST.
  57. This association list has elements of the form
  58.      (ORIGINAL-FUNCTION-NAME  OVERLOAD-FUNCTION)"
  59.   (mapcar
  60.    (function (lambda (p) (fset (car p) (symbol-function (cdr p)))))
  61.    gud-overload-alist))
  62.  
  63. (defun gud-massage-args (file args)
  64.   (error "GUD not properly entered."))
  65.  
  66. (defun gud-marker-filter (str)
  67.   (error "GUD not properly entered."))
  68.  
  69. (defun gud-find-file (f)
  70.   (error "GUD not properly entered."))
  71.  
  72. ;; ======================================================================
  73. ;; command definition
  74.  
  75. ;; This macro is used below to define some basic debugger interface commands.
  76. ;; Of course you may use `gud-def' with any other debugger command, including
  77. ;; user defined ones.
  78.  
  79. ;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
  80. ;; which defines FUNC to send the command NAME to the debugger, gives
  81. ;; it the docstring DOC, and binds that function to KEY in the GUD
  82. ;; major mode.  The function is also bound in the global keymap with the
  83. ;; GUD prefix.
  84.  
  85. (defmacro gud-def (func cmd key &optional doc)
  86.   "Define FUNC to be a command sending STR and bound to KEY, with
  87. optional doc string DOC.  Certain %-escapes in the string arguments
  88. are interpreted specially if present.  These are:
  89.  
  90.   %f    name (without directory) of current source file. 
  91.   %d    directory of current source file. 
  92.   %l    number of current source line
  93.   %e    text of the C lvalue or function-call expression surrounding point.
  94.   %a    text of the hexadecimal address surrounding point
  95.   %p    prefix argument to the command (if any) as a number
  96.  
  97.   The `current' source file is the file of the current buffer (if
  98. we're in a C file) or the source file current at the last break or
  99. step (if we're in the GUD buffer).
  100.   The `current' line is that of the current buffer (if we're in a
  101. source file) or the source line number at the last break or step (if
  102. we're in the GUD buffer)."
  103.   (list 'progn
  104.     (list 'defun func '(arg)
  105.           (or doc "")
  106.           '(interactive "p")
  107.           (list 'gud-call cmd 'arg))
  108.     (if key
  109.         (list 'define-key
  110.           '(current-local-map)
  111.           (concat "\C-c" key)
  112.           (list 'quote func)))
  113.     (if key
  114.         (list 'global-set-key
  115.           (list 'concat 'gud-key-prefix key)
  116.           (list 'quote func)))))
  117.  
  118. ;; Where gud-display-frame should put the debugging arrow.  This is
  119. ;; set by the marker-filter, which scans the debugger's output for
  120. ;; indications of the current program counter.
  121. (defvar gud-last-frame nil)
  122.  
  123. ;; Used by gud-refresh, which should cause gud-display-frame to redisplay
  124. ;; the last frame, even if it's been called before and gud-last-frame has
  125. ;; been set to nil.
  126. (defvar gud-last-last-frame nil)
  127.  
  128. ;; All debugger-specific information is collected here.
  129. ;; Here's how it works, in case you ever need to add a debugger to the mode.
  130. ;;
  131. ;; Each entry must define the following at startup:
  132. ;;
  133. ;;<name>
  134. ;; comint-prompt-regexp
  135. ;; gud-<name>-massage-args
  136. ;; gud-<name>-marker-filter
  137. ;; gud-<name>-find-file
  138. ;;
  139. ;; The job of the massage-args method is to modify the given list of
  140. ;; debugger arguments before running the debugger.
  141. ;;
  142. ;; The job of the marker-filter method is to detect file/line markers in
  143. ;; strings and set the global gud-last-frame to indicate what display
  144. ;; action (if any) should be triggered by the marker.  Note that only
  145. ;; whatever the method *returns* is displayed in the buffer; thus, you
  146. ;; can filter the debugger's output, interpreting some and passing on
  147. ;; the rest.
  148. ;;
  149. ;; The job of the find-file method is to visit and return the buffer indicated
  150. ;; by the car of gud-tag-frame.  This may be a file name, a tag name, or
  151. ;; something else.
  152.  
  153. ;; ======================================================================
  154. ;; gdb functions
  155.  
  156. ;;; History of argument lists passed to gdb.
  157. (defvar gud-gdb-history nil)
  158.  
  159. (defun gud-gdb-massage-args (file args)
  160.   (cons "-fullname" (cons file args)))
  161.  
  162. ;; There's no guarantee that Emacs will hand the filter the entire
  163. ;; marker at once; it could be broken up across several strings.  We
  164. ;; might even receive a big chunk with several markers in it.  If we
  165. ;; receive a chunk of text which looks like it might contain the
  166. ;; beginning of a marker, we save it here between calls to the
  167. ;; filter.
  168. (defvar gud-marker-acc "")
  169. (make-variable-buffer-local 'gud-marker-acc)
  170.  
  171. (defun gud-gdb-marker-filter (string)
  172.   (save-match-data
  173.     (setq gud-marker-acc (concat gud-marker-acc string))
  174.     (let ((output ""))
  175.  
  176.       ;; Process all the complete markers in this chunk.
  177.       (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  178.                gud-marker-acc)
  179.     (setq
  180.  
  181.      ;; Extract the frame position from the marker.
  182.      gud-last-frame
  183.      (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
  184.            (string-to-int (substring gud-marker-acc
  185.                      (match-beginning 2)
  186.                      (match-end 2))))
  187.  
  188.      ;; Append any text before the marker to the output we're going
  189.      ;; to return - we don't include the marker in this text.
  190.      output (concat output
  191.             (substring gud-marker-acc 0 (match-beginning 0)))
  192.  
  193.      ;; Set the accumulator to the remaining text.
  194.      gud-marker-acc (substring gud-marker-acc (match-end 0))))
  195.  
  196.       ;; Does the remaining text look like it might end with the
  197.       ;; beginning of another marker?  If it does, then keep it in
  198.       ;; gud-marker-acc until we receive the rest of it.  Since we
  199.       ;; know the full marker regexp above failed, it's pretty simple to
  200.       ;; test for marker starts.
  201.       (if (string-match "\032.*\\'" gud-marker-acc)
  202.       (progn
  203.         ;; Everything before the potential marker start can be output.
  204.         (setq output (concat output (substring gud-marker-acc
  205.                            0 (match-beginning 0))))
  206.  
  207.         ;; Everything after, we save, to combine with later input.
  208.         (setq gud-marker-acc
  209.           (substring gud-marker-acc (match-beginning 0))))
  210.  
  211.     (setq output (concat output gud-marker-acc)
  212.           gud-marker-acc ""))
  213.  
  214.       output)))
  215.  
  216. (defun gud-gdb-find-file (f)
  217.   (find-file-noselect f))
  218.  
  219. (defvar gdb-minibuffer-local-map nil
  220.   "Keymap for minibuffer prompting of gdb startup command.")
  221. (if gdb-minibuffer-local-map
  222.     ()
  223.   (setq gdb-minibuffer-local-map (copy-keymap minibuffer-local-map))
  224.   (define-key
  225.     gdb-minibuffer-local-map "\C-i" 'comint-dynamic-complete-filename))
  226.  
  227. ;;;###autoload
  228. (defun gdb (command-line)
  229.   "Run gdb on program FILE in buffer *gud-FILE*.
  230. The directory containing FILE becomes the initial working directory
  231. and source-file directory for your debugger."
  232.   (interactive
  233.    (list (read-from-minibuffer "Run gdb (like this): "
  234.                    (if (consp gud-gdb-history)
  235.                    (car gud-gdb-history)
  236.                  "gdb ")
  237.                    gdb-minibuffer-local-map nil
  238.                    '(gud-gdb-history . 1))))
  239.   (gud-overload-functions '((gud-massage-args . gud-gdb-massage-args)
  240.                 (gud-marker-filter . gud-gdb-marker-filter)
  241.                 (gud-find-file . gud-gdb-find-file)
  242.                 ))
  243.  
  244.   (gud-common-init command-line)
  245.  
  246.   (gud-def gud-break  "break %f:%l"  "\C-b" "Set breakpoint at current line.")
  247.   (gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set breakpoint at current line.")
  248.   (gud-def gud-remove "clear %l"     "\C-d" "Remove breakpoint at current line")
  249.   (gud-def gud-step   "step %p"      "\C-s" "Step one source line with display.")
  250.   (gud-def gud-stepi  "stepi %p"     "\C-i" "Step one instruction with display.")
  251.   (gud-def gud-next   "next %p"      "\C-n" "Step one line (skip functions).")
  252.   (gud-def gud-cont   "cont"         "\C-r" "Continue with display.")
  253.   (gud-def gud-finish "finish"       "\C-f" "Finish executing current function.")
  254.   (gud-def gud-up     "up %p"        "<" "Up N stack frames (numeric arg).")
  255.   (gud-def gud-down   "down %p"      ">" "Down N stack frames (numeric arg).")
  256.   (gud-def gud-print  "print %e"     "\C-p" "Evaluate C expression at point.")
  257.  
  258.   (local-set-key "\C-i" 'gud-gdb-complete-command)
  259.   (setq comint-prompt-regexp "^(.*gdb[+]?) *")
  260.   (setq paragraph-start comint-prompt-regexp)
  261.   (run-hooks 'gdb-mode-hook)
  262.   )
  263.  
  264. ;; One of the nice features of GDB is its impressive support for
  265. ;; context-sensitive command completion.  We preserve that feature
  266. ;; in the GUD buffer by using a GDB command designed just for Emacs.
  267.  
  268. ;; The completion process filter indicates when it is finished.
  269. (defvar gud-gdb-complete-in-progress)
  270.  
  271. ;; Since output may arrive in fragments we accumulate partials strings here.
  272. (defvar gud-gdb-complete-string)
  273.  
  274. ;; We need to know how much of the completion to chop off.
  275. (defvar gud-gdb-complete-break)
  276.  
  277. ;; The completion list is constructed by the process filter.
  278. (defvar gud-gdb-complete-list)
  279.  
  280. (defvar gud-comint-buffer nil)
  281.  
  282. (defun gud-gdb-complete-command ()
  283.   "Perform completion on the GDB command preceding point.
  284. This is implemented using the GDB `complete' command which isn't
  285. available with older versions of GDB."
  286.   (interactive)
  287.   (let* ((end (point))
  288.      (command (save-excursion
  289.             (beginning-of-line)
  290.             (and (looking-at comint-prompt-regexp)
  291.              (goto-char (match-end 0)))
  292.             (buffer-substring (point) end)))
  293.      command-word)
  294.     ;; Find the word break.  This match will always succeed.
  295.     (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
  296.     (setq gud-gdb-complete-break (match-beginning 2)
  297.       command-word (substring command gud-gdb-complete-break))
  298.     (unwind-protect
  299.     (progn
  300.       ;; Temporarily install our filter function.
  301.       (gud-overload-functions
  302.        '((gud-marker-filter . gud-gdb-complete-filter)))
  303.       ;; Issue the command to GDB.
  304.       (gud-basic-call (concat "complete " command))
  305.       (setq gud-gdb-complete-in-progress t
  306.         gud-gdb-complete-string nil
  307.         gud-gdb-complete-list nil)
  308.       ;; Slurp the output.
  309.       (while gud-gdb-complete-in-progress
  310.         (accept-process-output (get-buffer-process gud-comint-buffer))))
  311.       ;; Restore the old filter function.
  312.       (gud-overload-functions '((gud-marker-filter . gud-gdb-marker-filter))))
  313.     ;; Protect against old versions of GDB.
  314.     (and gud-gdb-complete-list
  315.      (string-match "^Undefined command: \"complete\""
  316.                (car gud-gdb-complete-list))
  317.      (error "This version of GDB doesn't support the `complete' command."))
  318.     ;; Sort the list like readline.
  319.     (setq gud-gdb-complete-list
  320.       (sort gud-gdb-complete-list (function string-lessp)))
  321.     ;; Remove duplicates.
  322.     (let ((first gud-gdb-complete-list)
  323.       (second (cdr gud-gdb-complete-list)))
  324.       (while second
  325.     (if (string-equal (car first) (car second))
  326.         (setcdr first (setq second (cdr second)))
  327.       (setq first second
  328.         second (cdr second)))))
  329.     ;; Let comint handle the rest.
  330.     (comint-dynamic-simple-complete command-word gud-gdb-complete-list)))
  331.     
  332. ;; The completion process filter is installed temporarily to slurp the
  333. ;; output of GDB up to the next prompt and build the completion list.
  334. (defun gud-gdb-complete-filter (string)
  335.   (setq string (concat gud-gdb-complete-string string))
  336.   (while (string-match "\n" string)
  337.     (setq gud-gdb-complete-list
  338.       (cons (substring string gud-gdb-complete-break (match-beginning 0))
  339.         gud-gdb-complete-list))
  340.     (setq string (substring string (match-end 0))))
  341.   (if (string-match comint-prompt-regexp string)
  342.       (progn
  343.     (setq gud-gdb-complete-in-progress nil)
  344.     string)
  345.     (progn
  346.       (setq gud-gdb-complete-string string)
  347.       "")))
  348.  
  349.  
  350. ;; ======================================================================
  351. ;; sdb functions
  352.  
  353. ;;; History of argument lists passed to sdb.
  354. (defvar gud-sdb-history nil)
  355.  
  356. (defvar gud-sdb-needs-tags (not (file-exists-p "/var"))
  357.   "If nil, we're on a System V Release 4 and don't need the tags hack.")
  358.  
  359. (defvar gud-sdb-lastfile nil)
  360.  
  361. (defun gud-sdb-massage-args (file args)
  362.   (cons file args))
  363.  
  364. (defun gud-sdb-marker-filter (string)
  365.   (cond 
  366.    ;; System V Release 3.2 uses this format
  367.    ((string-match "\\(^0x\\w* in \\|^\\|\n\\)\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  368.             string)
  369.     (setq gud-last-frame
  370.       (cons
  371.        (substring string (match-beginning 2) (match-end 2))
  372.        (string-to-int 
  373.         (substring string (match-beginning 3) (match-end 3))))))
  374.    ;; System V Release 4.0 
  375.    ((string-match "^\\(BREAKPOINT\\|STEPPED\\) process [0-9]+ function [^ ]+ in \\(.+\\)\n"
  376.                string)
  377.     (setq gud-sdb-lastfile
  378.       (substring string (match-beginning 2) (match-end 2))))
  379.    ((and gud-sdb-lastfile (string-match "^\\([0-9]+\\):" string))
  380.      (setq gud-last-frame
  381.            (cons
  382.         gud-sdb-lastfile
  383.         (string-to-int 
  384.          (substring string (match-beginning 1) (match-end 1))))))
  385.    (t 
  386.     (setq gud-sdb-lastfile nil)))
  387.   string)
  388.  
  389. (defun gud-sdb-find-file (f)
  390.   (if gud-sdb-needs-tags
  391.       (find-tag-noselect f)
  392.     (find-file-noselect f)))
  393.  
  394. ;;;###autoload
  395. (defun sdb (command-line)
  396.   "Run sdb on program FILE in buffer *gud-FILE*.
  397. The directory containing FILE becomes the initial working directory
  398. and source-file directory for your debugger."
  399.   (interactive
  400.    (list (read-from-minibuffer "Run sdb (like this): "
  401.                    (if (consp gud-sdb-history)
  402.                    (car gud-sdb-history)
  403.                  "sdb ")
  404.                    nil nil
  405.                    '(gud-sdb-history . 1))))
  406.   (if (and gud-sdb-needs-tags
  407.        (not (and (boundp 'tags-file-name)
  408.              (stringp tags-file-name)
  409.              (file-exists-p tags-file-name))))
  410.       (error "The sdb support requires a valid tags table to work."))
  411.   (gud-overload-functions '((gud-massage-args . gud-sdb-massage-args)
  412.                 (gud-marker-filter . gud-sdb-marker-filter)
  413.                 (gud-find-file . gud-sdb-find-file)
  414.                 ))
  415.  
  416.   (gud-common-init command-line)
  417.  
  418.   (gud-def gud-break  "%l b" "\C-b"   "Set breakpoint at current line.")
  419.   (gud-def gud-tbreak "%l c" "\C-t"   "Set temporary breakpoint at current line.")
  420.   (gud-def gud-remove "%l d" "\C-d"   "Remove breakpoint at current line")
  421.   (gud-def gud-step   "s %p" "\C-s"   "Step one source line with display.")
  422.   (gud-def gud-stepi  "i %p" "\C-i"   "Step one instruction with display.")
  423.   (gud-def gud-next   "S %p" "\C-n"   "Step one line (skip functions).")
  424.   (gud-def gud-cont   "c"    "\C-r"   "Continue with display.")
  425.   (gud-def gud-print  "%e/"  "\C-p"   "Evaluate C expression at point.")
  426.  
  427.   (setq comint-prompt-regexp  "\\(^\\|\n\\)\\*")
  428.   (setq paragraph-start comint-prompt-regexp)
  429.   (run-hooks 'sdb-mode-hook)
  430.   )
  431.  
  432. ;; ======================================================================
  433. ;; dbx functions
  434.  
  435. ;;; History of argument lists passed to dbx.
  436. (defvar gud-dbx-history nil)
  437.  
  438. (defun gud-dbx-massage-args (file args)
  439.   (cons file args))
  440.  
  441. (defun gud-dbx-marker-filter (string)
  442.   (if (or (string-match
  443.          "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
  444.          string)
  445.         (string-match
  446.          "signal .* in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
  447.          string))
  448.       (setq gud-last-frame
  449.         (cons
  450.          (substring string (match-beginning 2) (match-end 2))
  451.          (string-to-int 
  452.           (substring string (match-beginning 1) (match-end 1))))))
  453.   string)
  454.  
  455. ;; Functions for Mips-style dbx.  Given the option `-emacs', documented in
  456. ;; OSF1, not necessarily elsewhere, it produces markers similar to gdb's.
  457. (defvar gud-mips-p
  458.   (or (string-match "^mips-[^-]*-ultrix" system-configuration)
  459.       ;; We haven't tested gud on this system:
  460.       (string-match "^mips-[^-]*-riscos" system-configuration)
  461.       ;; It's documented on OSF/1.3
  462.       (string-match "^mips-[^-]*-osf1" system-configuration))
  463.   "Non-nil to assume the MIPS/OSF dbx conventions (argument `-emacs').")
  464.  
  465. (defun gud-mipsdbx-massage-args (file args)
  466.   (cons "-emacs" (cons file args)))
  467.  
  468. ;; This is just like the gdb one except for the regexps since we need to cope
  469. ;; with an optional breakpoint number in [] before the ^Z^Z
  470. (defun gud-mipsdbx-marker-filter (string)
  471.   (save-match-data
  472.     (setq gud-marker-acc (concat gud-marker-acc string))
  473.     (let ((output ""))
  474.  
  475.       ;; Process all the complete markers in this chunk.
  476.       (while (string-match
  477.           ;; This is like th gdb marker but with an optional
  478.           ;; leading break point number like `[1] '
  479.           "[][ 0-9]*\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  480.           gud-marker-acc)
  481.     (setq
  482.  
  483.      ;; Extract the frame position from the marker.
  484.      gud-last-frame
  485.      (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
  486.            (string-to-int (substring gud-marker-acc
  487.                      (match-beginning 2)
  488.                      (match-end 2))))
  489.  
  490.      ;; Append any text before the marker to the output we're going
  491.      ;; to return - we don't include the marker in this text.
  492.      output (concat output
  493.             (substring gud-marker-acc 0 (match-beginning 0)))
  494.  
  495.      ;; Set the accumulator to the remaining text.
  496.      gud-marker-acc (substring gud-marker-acc (match-end 0))))
  497.  
  498.       ;; Does the remaining text look like it might end with the
  499.       ;; beginning of another marker?  If it does, then keep it in
  500.       ;; gud-marker-acc until we receive the rest of it.  Since we
  501.       ;; know the full marker regexp above failed, it's pretty simple to
  502.       ;; test for marker starts.
  503.       (if (string-match "[][ 0-9]*\032.*\\'" gud-marker-acc)
  504.       (progn
  505.         ;; Everything before the potential marker start can be output.
  506.         (setq output (concat output (substring gud-marker-acc
  507.                            0 (match-beginning 0))))
  508.  
  509.         ;; Everything after, we save, to combine with later input.
  510.         (setq gud-marker-acc
  511.           (substring gud-marker-acc (match-beginning 0))))
  512.  
  513.     (setq output (concat output gud-marker-acc)
  514.           gud-marker-acc ""))
  515.  
  516.       output)))
  517.  
  518. ;; The dbx in IRIX is a pain.  It doesn't print the file name when
  519. ;; stopping at a breakpoint (but you do get it from the `up' and
  520. ;; `down' commands...).  The only way to extract the information seems
  521. ;; to be with a `file' command, although the current line number is
  522. ;; available in $curline.  Thus we have to look for output which
  523. ;; appears to indicate a breakpoint.  Then we prod the dbx sub-process
  524. ;; to output the information we want with a combination of the
  525. ;; `printf' and `file' commands as a pseudo marker which we can
  526. ;; recognise next time through the marker-filter.  This would be like
  527. ;; the gdb marker but you can't get the file name without a newline...
  528. ;; Note that gud-remove won't work since Irix dbx expects a breakpoint
  529. ;; number rather than a line number etc.  Maybe this could be made to
  530. ;; work by listing all the breakpoints and picking the one(s) with the
  531. ;; correct line number, but life's too short.
  532. ;;   d.love@dl.ac.uk (Dave Love) can be blamed for this
  533.  
  534. (defvar gud-irix-p (string-match "^mips-[^-]*-irix" system-configuration)
  535.   "Non-nil to assume the interface appropriate for IRIX dbx.
  536. This works in IRIX 4 and probably IRIX 5.")
  537. ;; (It's been tested in IRIX 4 and the output from dbx on IRIX 5 looks
  538. ;; the same.)
  539.  
  540. ;; this filter is influenced by the xdb one rather than the gdb one
  541. (defun gud-irixdbx-marker-filter (string)
  542.   (save-match-data
  543.     (let (result (case-fold-search nil))
  544.       (if (or (string-match comint-prompt-regexp string)
  545.               (string-match ".*\012" string))
  546.           (setq result (concat gud-marker-acc string)
  547.                 gud-marker-acc "")
  548.         (setq gud-marker-acc (concat gud-marker-acc string)))
  549.       (if result
  550.           (cond
  551.            ;; look for breakpoint or signal indication e.g.:
  552.            ;; [2] Process  1267 (pplot) stopped at [params:338 ,0x400ec0]
  553.            ;; Process  1281 (pplot) stopped at [params:339 ,0x400ec8]
  554.            ;; Process  1270 (pplot) Floating point exception [._read._read:16 ,0x452188]
  555.            ((string-match
  556.              "^\\(\\[[0-9]+] \\)?Process +[0-9]+ ([^)]*) [^[]+\\[[^]\n]*]\n" 
  557.              result)
  558.         ;; prod dbx into printing out the line number and file
  559.         ;; name in a form we can grok as below
  560.             (process-send-string (get-buffer-process gud-comint-buffer)
  561.                  "printf \"\032\032%1d:\",$curline;file\n"))
  562.            ;; look for result of, say, "up" e.g.:
  563.            ;; .pplot.pplot(0x800) ["src/pplot.f":261, 0x400c7c]
  564.        ;; (this will also catch one of the lines printed by "where")
  565.            ((string-match
  566.              "^[^ ][^[]*\\[\"\\([^\"]+\\)\":\\([0-9]+\\), [^]]+]\n"
  567.              result)
  568.             (let ((file (substring result (match-beginning 1)
  569.                                    (match-end 1))))
  570.               (if (file-exists-p file)
  571.                   (setq gud-last-frame
  572.                         (cons
  573.                          (substring
  574.                           result (match-beginning 1) (match-end 1))
  575.                          (string-to-int 
  576.                           (substring
  577.                            result (match-beginning 2) (match-end 2)))))))
  578.             result)
  579.            ((string-match               ; kluged-up marker as above
  580.              "\032\032\\([0-9]*\\):\\(.*\\)\n" result)
  581.             (let ((file (substring result (match-beginning 2) (match-end 2))))
  582.               (if (file-exists-p file)
  583.                   (setq gud-last-frame
  584.                         (cons
  585.                          file
  586.                          (string-to-int 
  587.                           (substring
  588.                            result (match-beginning 1) (match-end 1)))))))
  589.             (setq result (substring result 0 (match-beginning 0))))))
  590.       (or result ""))))
  591.  
  592. (defun gud-dbx-find-file (f)
  593.   (find-file-noselect f))
  594.  
  595. ;;;###autoload
  596. (defun dbx (command-line)
  597.   "Run dbx on program FILE in buffer *gud-FILE*.
  598. The directory containing FILE becomes the initial working directory
  599. and source-file directory for your debugger."
  600.   (interactive
  601.    (list (read-from-minibuffer "Run dbx (like this): "
  602.                    (if (consp gud-dbx-history)
  603.                    (car gud-dbx-history)
  604.                  "dbx ")
  605.                    nil nil
  606.                    '(gud-dbx-history . 1))))
  607.  
  608.   (gud-overload-functions
  609.    (cond
  610.     (gud-mips-p
  611.      '((gud-massage-args . gud-mipsdbx-massage-args)
  612.        (gud-marker-filter . gud-mipsdbx-marker-filter)
  613.        (gud-find-file . gud-dbx-find-file)))
  614.     (gud-irix-p
  615.      '((gud-massage-args . gud-dbx-massage-args)
  616.        (gud-marker-filter . gud-irixdbx-marker-filter)
  617.        (gud-find-file . gud-dbx-find-file)))
  618.     (t
  619.      '((gud-massage-args . gud-dbx-massage-args)
  620.        (gud-marker-filter . gud-dbx-marker-filter)
  621.        (gud-find-file . gud-dbx-find-file)))))
  622.  
  623.   (gud-common-init command-line)
  624.  
  625.   (cond
  626.    (gud-mips-p
  627.     (gud-def gud-break "stop at \"%f\":%l"
  628.                   "\C-b" "Set breakpoint at current line.")
  629.     (gud-def gud-finish "return"  "\C-f" "Finish executing current function."))
  630.    (gud-irix-p
  631.     (gud-def gud-break "stop at \"%d%f\":%l"
  632.                   "\C-b" "Set breakpoint at current line.")
  633.     (gud-def gud-finish "return"  "\C-f" "Finish executing current function.")
  634.     ;; Make dbx give out the source location info that we need.
  635.     (process-send-string (get-buffer-process gud-comint-buffer)
  636.              "printf \"\032\032%1d:\",$curline;file\n"))
  637.    ((or (string-match "-sunos" (symbol-name system-type))
  638.     (string-match "-solaris" (symbol-name system-type)))
  639.     ;; The following works for both the UCB and SunPro 2.0.1 versions
  640.     ;; of dbx.  The `stop' is lost using the `\n' separator as in the
  641.     ;; default case.  Is there a dbx where the newline is actually
  642.     ;; necessary?  (d.love@dl.ac.uk)
  643.     (gud-def gud-break "file \"%d%f\";stop at %l"
  644.                   "\C-b" "Set breakpoint at current line."))
  645.    (t
  646.     (gud-def gud-break "file \"%d%f\"\nstop at %l"
  647.                   "\C-b" "Set breakpoint at current line.")))
  648.  
  649.   (gud-def gud-remove "clear %l"  "\C-d" "Remove breakpoint at current line")
  650.   (gud-def gud-step   "step %p"      "\C-s" "Step one line with display.")
  651.   (gud-def gud-stepi  "stepi %p"  "\C-i" "Step one instruction with display.")
  652.   (gud-def gud-next   "next %p"      "\C-n" "Step one line (skip functions).")
  653.   (gud-def gud-cont   "cont"      "\C-r" "Continue with display.")
  654.   (gud-def gud-up     "up %p"      "<" "Up (numeric arg) stack frames.")
  655.   (gud-def gud-down   "down %p"      ">" "Down (numeric arg) stack frames.")
  656.   (gud-def gud-print  "print %e"  "\C-p" "Evaluate C expression at point.")
  657.  
  658.   (setq comint-prompt-regexp  "^[^)\n]*dbx) *")
  659.   (setq paragraph-start comint-prompt-regexp)
  660.   (run-hooks 'dbx-mode-hook)
  661.   )
  662.  
  663. ;; ======================================================================
  664. ;; xdb (HP PARISC debugger) functions
  665.  
  666. ;;; History of argument lists passed to xdb.
  667. (defvar gud-xdb-history nil)
  668.  
  669. (defvar gud-xdb-directories nil
  670.   "*A list of directories that xdb should search for source code.
  671. If nil, only source files in the program directory
  672. will be known to xdb.
  673.  
  674. The file names should be absolute, or relative to the directory
  675. containing the executable being debugged.")
  676.  
  677. (defun gud-xdb-massage-args (file args)
  678.   (nconc (let ((directories gud-xdb-directories)
  679.            (result nil))
  680.        (while directories
  681.          (setq result (cons (car directories) (cons "-d" result)))
  682.          (setq directories (cdr directories)))
  683.        (nreverse (cons file result)))
  684.      args))
  685.  
  686. (defun gud-xdb-file-name (f)
  687.   "Transform a relative pathname to a full pathname in xdb mode"
  688.   (let ((result nil))
  689.     (if (file-exists-p f)
  690.         (setq result (expand-file-name f))
  691.       (let ((directories gud-xdb-directories))
  692.         (while directories
  693.           (let ((path (concat (car directories) "/" f)))
  694.             (if (file-exists-p path)
  695.                 (setq result (expand-file-name path)
  696.                       directories nil)))
  697.           (setq directories (cdr directories)))))
  698.     result))
  699.  
  700. ;; xdb does not print the lines all at once, so we have to accumulate them
  701. (defun gud-xdb-marker-filter (string)
  702.   (let (result)
  703.     (if (or (string-match comint-prompt-regexp string)
  704.             (string-match ".*\012" string))
  705.         (setq result (concat gud-marker-acc string)
  706.               gud-marker-acc "")
  707.       (setq gud-marker-acc (concat gud-marker-acc string)))
  708.     (if result
  709.         (if (or (string-match "\\([^\n \t:]+\\): [^:]+: \\([0-9]+\\):" result)
  710.                 (string-match "[^: \t]+:[ \t]+\\([^:]+\\): [^:]+: \\([0-9]+\\):"
  711.                               result))
  712.             (let ((line (string-to-int 
  713.                          (substring result (match-beginning 2) (match-end 2))))
  714.                   (file (gud-xdb-file-name
  715.                          (substring result (match-beginning 1) (match-end 1)))))
  716.               (if file
  717.                   (setq gud-last-frame (cons file line))))))
  718.     (or result "")))    
  719.                
  720. (defun gud-xdb-find-file (f)
  721.   (let ((realf (gud-xdb-file-name f)))
  722.     (if realf (find-file-noselect realf))))
  723.  
  724. ;;;###autoload
  725. (defun xdb (command-line)
  726.   "Run xdb on program FILE in buffer *gud-FILE*.
  727. The directory containing FILE becomes the initial working directory
  728. and source-file directory for your debugger.
  729.  
  730. You can set the variable 'gud-xdb-directories' to a list of program source
  731. directories if your program contains sources from more than one directory."
  732.   (interactive
  733.    (list (read-from-minibuffer "Run xdb (like this): "
  734.                    (if (consp gud-xdb-history)
  735.                    (car gud-xdb-history)
  736.                  "xdb ")
  737.                    nil nil
  738.                    '(gud-xdb-history . 1))))
  739.   (gud-overload-functions '((gud-massage-args . gud-xdb-massage-args)
  740.                 (gud-marker-filter . gud-xdb-marker-filter)
  741.                 (gud-find-file . gud-xdb-find-file)))
  742.  
  743.   (gud-common-init command-line)
  744.  
  745.   (gud-def gud-break  "b %f:%l"    "\C-b" "Set breakpoint at current line.")
  746.   (gud-def gud-tbreak "b %f:%l\\t" "\C-t"
  747.            "Set temporary breakpoint at current line.")
  748.   (gud-def gud-remove "db"         "\C-d" "Remove breakpoint at current line")
  749.   (gud-def gud-step   "s %p"       "\C-s" "Step one line with display.")
  750.   (gud-def gud-next   "S %p"       "\C-n" "Step one line (skip functions).")
  751.   (gud-def gud-cont   "c"       "\C-r" "Continue with display.")
  752.   (gud-def gud-up     "up %p"       "<"    "Up (numeric arg) stack frames.")
  753.   (gud-def gud-down   "down %p"       ">"    "Down (numeric arg) stack frames.")
  754.   (gud-def gud-finish "bu\\t"      "\C-f" "Finish executing current function.")
  755.   (gud-def gud-print  "p %e"       "\C-p" "Evaluate C expression at point.")
  756.  
  757.   (setq comint-prompt-regexp  "^>")
  758.   (setq paragraph-start comint-prompt-regexp)
  759.   (run-hooks 'xdb-mode-hook))
  760.  
  761. ;; ======================================================================
  762. ;; perldb functions
  763.  
  764. ;;; History of argument lists passed to perldb.
  765. (defvar gud-perldb-history nil)
  766.  
  767. (defun gud-perldb-massage-args (file args)
  768.   (cons "-d" (cons file (cons "-emacs" args))))
  769.  
  770. ;; There's no guarantee that Emacs will hand the filter the entire
  771. ;; marker at once; it could be broken up across several strings.  We
  772. ;; might even receive a big chunk with several markers in it.  If we
  773. ;; receive a chunk of text which looks like it might contain the
  774. ;; beginning of a marker, we save it here between calls to the
  775. ;; filter.
  776. (defvar gud-perldb-marker-acc "")
  777.  
  778. (defun gud-perldb-marker-filter (string)
  779.   (save-match-data
  780.     (setq gud-marker-acc (concat gud-marker-acc string))
  781.     (let ((output ""))
  782.  
  783.       ;; Process all the complete markers in this chunk.
  784.       (while (string-match "\032\032\\([^:\n]*\\):\\([0-9]*\\):.*\n"
  785.                gud-marker-acc)
  786.     (setq
  787.  
  788.      ;; Extract the frame position from the marker.
  789.      gud-last-frame
  790.      (cons (substring gud-marker-acc (match-beginning 1) (match-end 1))
  791.            (string-to-int (substring gud-marker-acc
  792.                      (match-beginning 2)
  793.                      (match-end 2))))
  794.  
  795.      ;; Append any text before the marker to the output we're going
  796.      ;; to return - we don't include the marker in this text.
  797.      output (concat output
  798.             (substring gud-marker-acc 0 (match-beginning 0)))
  799.  
  800.      ;; Set the accumulator to the remaining text.
  801.      gud-marker-acc (substring gud-marker-acc (match-end 0))))
  802.  
  803.       ;; Does the remaining text look like it might end with the
  804.       ;; beginning of another marker?  If it does, then keep it in
  805.       ;; gud-marker-acc until we receive the rest of it.  Since we
  806.       ;; know the full marker regexp above failed, it's pretty simple to
  807.       ;; test for marker starts.
  808.       (if (string-match "\032.*\\'" gud-marker-acc)
  809.       (progn
  810.         ;; Everything before the potential marker start can be output.
  811.         (setq output (concat output (substring gud-marker-acc
  812.                            0 (match-beginning 0))))
  813.  
  814.         ;; Everything after, we save, to combine with later input.
  815.         (setq gud-marker-acc
  816.           (substring gud-marker-acc (match-beginning 0))))
  817.  
  818.     (setq output (concat output gud-marker-acc)
  819.           gud-marker-acc ""))
  820.  
  821.       output)))
  822.  
  823. (defun gud-perldb-find-file (f)
  824.   (find-file-noselect f))
  825.  
  826. ;;;###autoload
  827. (defun perldb (command-line)
  828.   "Run perldb on program FILE in buffer *gud-FILE*.
  829. The directory containing FILE becomes the initial working directory
  830. and source-file directory for your debugger."
  831.   (interactive
  832.    (list (read-from-minibuffer "Run perldb (like this): "
  833.                    (if (consp gud-perldb-history)
  834.                    (car gud-perldb-history)
  835.                  "perl ")
  836.                    nil nil
  837.                    '(gud-perldb-history . 1))))
  838.   (gud-overload-functions '((gud-massage-args . gud-perldb-massage-args)
  839.                 (gud-marker-filter . gud-perldb-marker-filter)
  840.                 (gud-find-file . gud-perldb-find-file)
  841.                 ))
  842.  
  843.   (gud-common-init command-line)
  844.  
  845.   (gud-def gud-break  "b %l"         "\C-b" "Set breakpoint at current line.")
  846.   (gud-def gud-remove "d %l"         "\C-d" "Remove breakpoint at current line")
  847.   (gud-def gud-step   "s"            "\C-s" "Step one source line with display.")
  848.   (gud-def gud-next   "n"            "\C-n" "Step one line (skip functions).")
  849.   (gud-def gud-cont   "c"            "\C-r" "Continue with display.")
  850. ;  (gud-def gud-finish "finish"       "\C-f" "Finish executing current function.")
  851. ;  (gud-def gud-up     "up %p"        "<" "Up N stack frames (numeric arg).")
  852. ;  (gud-def gud-down   "down %p"      ">" "Down N stack frames (numeric arg).")
  853.   (gud-def gud-print  "%e"           "\C-p" "Evaluate perl expression at point.")
  854.  
  855.   (setq comint-prompt-regexp "^  DB<[0-9]+> ")
  856.   (setq paragraph-start comint-prompt-regexp)
  857.   (run-hooks 'perldb-mode-hook)
  858.   )
  859.  
  860. ;;
  861. ;; End of debugger-specific information
  862. ;;
  863.  
  864.  
  865. ;;; When we send a command to the debugger via gud-call, it's annoying
  866. ;;; to see the command and the new prompt inserted into the debugger's
  867. ;;; buffer; we have other ways of knowing the command has completed.
  868. ;;;
  869. ;;; If the buffer looks like this:
  870. ;;; --------------------
  871. ;;; (gdb) set args foo bar
  872. ;;; (gdb) -!-
  873. ;;; --------------------
  874. ;;; (the -!- marks the location of point), and we type `C-x SPC' in a
  875. ;;; source file to set a breakpoint, we want the buffer to end up like
  876. ;;; this:
  877. ;;; --------------------
  878. ;;; (gdb) set args foo bar
  879. ;;; Breakpoint 1 at 0x92: file make-docfile.c, line 49.
  880. ;;; (gdb) -!-
  881. ;;; --------------------
  882. ;;; Essentially, the old prompt is deleted, and the command's output
  883. ;;; and the new prompt take its place.
  884. ;;;
  885. ;;; Not echoing the command is easy enough; you send it directly using
  886. ;;; process-send-string, and it never enters the buffer.  However,
  887. ;;; getting rid of the old prompt is trickier; you don't want to do it
  888. ;;; when you send the command, since that will result in an annoying
  889. ;;; flicker as the prompt is deleted, redisplay occurs while Emacs
  890. ;;; waits for a response from the debugger, and the new prompt is
  891. ;;; inserted.  Instead, we'll wait until we actually get some output
  892. ;;; from the subprocess before we delete the prompt.  If the command
  893. ;;; produced no output other than a new prompt, that prompt will most
  894. ;;; likely be in the first chunk of output received, so we will delete
  895. ;;; the prompt and then replace it with an identical one.  If the
  896. ;;; command produces output, the prompt is moving anyway, so the
  897. ;;; flicker won't be annoying.
  898. ;;;
  899. ;;; So - when we want to delete the prompt upon receipt of the next
  900. ;;; chunk of debugger output, we position gud-delete-prompt-marker at
  901. ;;; the start of the prompt; the process filter will notice this, and
  902. ;;; delete all text between it and the process output marker.  If
  903. ;;; gud-delete-prompt-marker points nowhere, we leave the current
  904. ;;; prompt alone.
  905. (defvar gud-delete-prompt-marker nil)
  906.  
  907.  
  908. (defun gud-mode ()
  909.   "Major mode for interacting with an inferior debugger process.
  910.  
  911.    You start it up with one of the commands M-x gdb, M-x sdb, M-x dbx,
  912. or M-x xdb.  Each entry point finishes by executing a hook; `gdb-mode-hook',
  913. `sdb-mode-hook', `dbx-mode-hook' or `xdb-mode-hook' respectively.
  914.  
  915. After startup, the following commands are available in both the GUD
  916. interaction buffer and any source buffer GUD visits due to a breakpoint stop
  917. or step operation:
  918.  
  919. \\[gud-break] sets a breakpoint at the current file and line.  In the
  920. GUD buffer, the current file and line are those of the last breakpoint or
  921. step.  In a source buffer, they are the buffer's file and current line.
  922.  
  923. \\[gud-remove] removes breakpoints on the current file and line.
  924.  
  925. \\[gud-refresh] displays in the source window the last line referred to
  926. in the gud buffer.
  927.  
  928. \\[gud-step], \\[gud-next], and \\[gud-stepi] do a step-one-line,
  929. step-one-line (not entering function calls), and step-one-instruction
  930. and then update the source window with the current file and position.
  931. \\[gud-cont] continues execution.
  932.  
  933. \\[gud-print] tries to find the largest C lvalue or function-call expression
  934. around point, and sends it to the debugger for value display.
  935.  
  936. The above commands are common to all supported debuggers except xdb which
  937. does not support stepping instructions.
  938.  
  939. Under gdb, sdb and xdb, \\[gud-tbreak] behaves exactly like \\[gud-break],
  940. except that the breakpoint is temporary; that is, it is removed when
  941. execution stops on it.
  942.  
  943. Under gdb, dbx, and xdb, \\[gud-up] pops up through an enclosing stack
  944. frame.  \\[gud-down] drops back down through one.
  945.  
  946. If you are using gdb or xdb, \\[gud-finish] runs execution to the return from
  947. the current function and stops.
  948.  
  949. All the keystrokes above are accessible in the GUD buffer
  950. with the prefix C-c, and in all buffers through the prefix C-x C-a.
  951.  
  952. All pre-defined functions for which the concept make sense repeat
  953. themselves the appropriate number of times if you give a prefix
  954. argument.
  955.  
  956. You may use the `gud-def' macro in the initialization hook to define other
  957. commands.
  958.  
  959. Other commands for interacting with the debugger process are inherited from
  960. comint mode, which see."
  961.   (interactive)
  962.   (comint-mode)
  963.   (setq major-mode 'gud-mode)
  964.   (setq mode-name "Debugger")
  965.   (setq mode-line-process '(":%s"))
  966.   (use-local-map (copy-keymap comint-mode-map))
  967.   (define-key (current-local-map) "\C-c\C-l" 'gud-refresh)
  968.   (make-local-variable 'gud-last-frame)
  969.   (setq gud-last-frame nil)
  970.   (make-local-variable 'comint-prompt-regexp)
  971.   (make-local-variable 'paragraph-start)
  972.   (make-local-variable 'gud-delete-prompt-marker)
  973.   (setq gud-delete-prompt-marker (make-marker))
  974.   (run-hooks 'gud-mode-hook)
  975. )
  976.  
  977. ;; Chop STRING into words separated by SPC or TAB and return a list of them.
  978. (defun gud-chop-words (string)
  979.   (let ((i 0) (beg 0)
  980.     (len (length string))
  981.     (words nil))
  982.     (while (< i len)
  983.       (if (memq (aref string i) '(?\t ? ))
  984.       (progn
  985.         (setq words (cons (substring string beg i) words)
  986.           beg (1+ i))
  987.         (while (and (< beg len) (memq (aref string beg) '(?\t ? )))
  988.           (setq beg (1+ beg)))
  989.         (setq i (1+ beg)))
  990.     (setq i (1+ i))))
  991.     (if (< beg len)
  992.     (setq words (cons (substring string beg) words)))
  993.     (nreverse words)))
  994.  
  995. ;; Perform initializations common to all debuggers.
  996. (defun gud-common-init (command-line)
  997.   (let* ((words (gud-chop-words command-line))
  998.      (program (car words))
  999.      (file-word (let ((w (cdr words)))
  1000.               (while (and w (= ?- (aref (car w) 0)))
  1001.             (setq w (cdr w)))
  1002.               (car w)))
  1003.      (args (delq file-word (cdr words)))
  1004.      (file (and file-word
  1005.             (expand-file-name (substitute-in-file-name file-word))))
  1006.      (filepart (and file-word (file-name-nondirectory file))))
  1007.       (switch-to-buffer (concat "*gud-" filepart "*"))
  1008.       (and file-word (setq default-directory (file-name-directory file)))
  1009.       (or (bolp) (newline))
  1010.       (insert "Current directory is " default-directory "\n")
  1011.       (apply 'make-comint (concat "gud-" filepart) program nil
  1012.          (if file-word (gud-massage-args file args))))
  1013.   (gud-mode)
  1014.   (set-process-filter (get-buffer-process (current-buffer)) 'gud-filter)
  1015.   (set-process-sentinel (get-buffer-process (current-buffer)) 'gud-sentinel)
  1016.   (gud-set-buffer)
  1017.   )
  1018.  
  1019. (defun gud-set-buffer ()
  1020.   (cond ((eq major-mode 'gud-mode)
  1021.     (setq gud-comint-buffer (current-buffer)))))
  1022.  
  1023. ;; These functions are responsible for inserting output from your debugger
  1024. ;; into the buffer.  The hard work is done by the method that is
  1025. ;; the value of gud-marker-filter.
  1026.  
  1027. (defun gud-filter (proc string)
  1028.   ;; Here's where the actual buffer insertion is done
  1029.   (let ((inhibit-quit t) 
  1030.     output)
  1031.     (save-excursion
  1032.       (set-buffer (process-buffer proc))
  1033.       ;; If we have been so requested, delete the debugger prompt.
  1034.       (if (marker-buffer gud-delete-prompt-marker)
  1035.       (progn
  1036.         (delete-region (process-mark proc) gud-delete-prompt-marker)
  1037.         (set-marker gud-delete-prompt-marker nil)))
  1038.       ;; Save the process output, checking for source file markers.
  1039.       (setq output (gud-marker-filter string))
  1040.       ;; Check for a filename-and-line number.
  1041.       ;; Don't display the specified file
  1042.       ;; unless (1) point is at or after the position where output appears
  1043.       ;; and (2) this buffer is on the screen.
  1044.       (if (and gud-last-frame
  1045.            (>= (point) (process-mark proc))
  1046.            (get-buffer-window (current-buffer)))
  1047.       (gud-display-frame))
  1048.       ;; Let the comint filter do the actual insertion.
  1049.       ;; That lets us inherit various comint features.
  1050.       (comint-output-filter proc output))))
  1051.  
  1052. (defun gud-sentinel (proc msg)
  1053.   (cond ((null (buffer-name (process-buffer proc)))
  1054.      ;; buffer killed
  1055.      ;; Stop displaying an arrow in a source file.
  1056.      (setq overlay-arrow-position nil)
  1057.      (set-process-buffer proc nil))
  1058.     ((memq (process-status proc) '(signal exit))
  1059.      ;; Stop displaying an arrow in a source file.
  1060.      (setq overlay-arrow-position nil)
  1061.      ;; Fix the mode line.
  1062.      (setq mode-line-process
  1063.            (concat ":"
  1064.                (symbol-name (process-status proc))))
  1065.      (let* ((obuf (current-buffer)))
  1066.        ;; save-excursion isn't the right thing if
  1067.        ;;  process-buffer is current-buffer
  1068.        (unwind-protect
  1069.            (progn
  1070.          ;; Write something in *compilation* and hack its mode line,
  1071.          (set-buffer (process-buffer proc))
  1072.          ;; Force mode line redisplay soon
  1073.          (set-buffer-modified-p (buffer-modified-p))
  1074.          (if (eobp)
  1075.              (insert ?\n mode-name " " msg)
  1076.            (save-excursion
  1077.              (goto-char (point-max))
  1078.              (insert ?\n mode-name " " msg)))
  1079.          ;; If buffer and mode line will show that the process
  1080.          ;; is dead, we can delete it now.  Otherwise it
  1081.          ;; will stay around until M-x list-processes.
  1082.          (delete-process proc))
  1083.          ;; Restore old buffer, but don't restore old point
  1084.          ;; if obuf is the gud buffer.
  1085.          (set-buffer obuf))))))
  1086.  
  1087. (defun gud-display-frame ()
  1088.   "Find and obey the last filename-and-line marker from the debugger.
  1089. Obeying it means displaying in another window the specified file and line."
  1090.   (interactive)
  1091.   (if gud-last-frame
  1092.    (progn
  1093.      (gud-set-buffer)
  1094.      (gud-display-line (car gud-last-frame) (cdr gud-last-frame))
  1095.      (setq gud-last-last-frame gud-last-frame
  1096.        gud-last-frame nil))))
  1097.  
  1098. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  1099. ;; and that its line LINE is visible.
  1100. ;; Put the overlay-arrow on the line LINE in that buffer.
  1101. ;; Most of the trickiness in here comes from wanting to preserve the current
  1102. ;; region-restriction if that's possible.  We use an explicit display-buffer
  1103. ;; to get around the fact that this is called inside a save-excursion.
  1104.  
  1105. (defun gud-display-line (true-file line)
  1106.   (let* ((buffer (gud-find-file true-file))
  1107.      (window (display-buffer buffer))
  1108.      (pos))
  1109. ;;;    (if (equal buffer (current-buffer))
  1110. ;;;    nil
  1111. ;;;      (setq buffer-read-only nil))
  1112.     (save-excursion
  1113. ;;;      (setq buffer-read-only t)
  1114.       (set-buffer buffer)
  1115.       (save-restriction
  1116.     (widen)
  1117.     (goto-line line)
  1118.     (setq pos (point))
  1119.     (setq overlay-arrow-string "=>")
  1120.     (or overlay-arrow-position
  1121.         (setq overlay-arrow-position (make-marker)))
  1122.     (set-marker overlay-arrow-position (point) (current-buffer)))
  1123.       (cond ((or (< pos (point-min)) (> pos (point-max)))
  1124.          (widen)
  1125.          (goto-char pos))))
  1126.     (set-window-point window overlay-arrow-position)))
  1127.  
  1128. ;;; The gud-call function must do the right thing whether its invoking
  1129. ;;; keystroke is from the GUD buffer itself (via major-mode binding)
  1130. ;;; or a C buffer.  In the former case, we want to supply data from
  1131. ;;; gud-last-frame.  Here's how we do it:
  1132.  
  1133. (defun gud-format-command (str arg)
  1134.   (let ((insource (not (eq (current-buffer) gud-comint-buffer)))
  1135.     (frame (or gud-last-frame gud-last-last-frame))
  1136.     result)
  1137.     (while (and str (string-match "\\([^%]*\\)%\\([adeflp]\\)" str))
  1138.       (let ((key (string-to-char (substring str (match-beginning 2))))
  1139.         subst)
  1140.     (cond
  1141.      ((eq key ?f)
  1142.       (setq subst (file-name-nondirectory (if insource
  1143.                           (buffer-file-name)
  1144.                         (car frame)))))
  1145.      ((eq key ?d)
  1146.       (setq subst (file-name-directory (if insource
  1147.                            (buffer-file-name)
  1148.                          (car frame)))))
  1149.      ((eq key ?l)
  1150.       (setq subst (if insource
  1151.               (save-excursion
  1152.                 (beginning-of-line)
  1153.                 (save-restriction (widen) 
  1154.                           (1+ (count-lines 1 (point)))))
  1155.             (cdr frame))))
  1156.      ((eq key ?e)
  1157.       (setq subst (find-c-expr)))
  1158.      ((eq key ?a)
  1159.       (setq subst (gud-read-address)))
  1160.      ((eq key ?p)
  1161.       (setq subst (if arg (int-to-string arg) ""))))
  1162.     (setq result (concat result
  1163.                  (substring str (match-beginning 1) (match-end 1))
  1164.                  subst)))
  1165.       (setq str (substring str (match-end 2))))
  1166.     ;; There might be text left in STR when the loop ends.
  1167.     (concat result str)))
  1168.  
  1169. (defun gud-read-address ()
  1170.   "Return a string containing the core-address found in the buffer at point."
  1171.   (save-excursion
  1172.     (let ((pt (point)) found begin)
  1173.       (setq found (if (search-backward "0x" (- pt 7) t) (point)))
  1174.       (cond
  1175.        (found (forward-char 2)
  1176.           (buffer-substring found
  1177.                 (progn (re-search-forward "[^0-9a-f]")
  1178.                        (forward-char -1)
  1179.                        (point))))
  1180.        (t (setq begin (progn (re-search-backward "[^0-9]") 
  1181.                  (forward-char 1)
  1182.                  (point)))
  1183.       (forward-char 1)
  1184.       (re-search-forward "[^0-9]")
  1185.       (forward-char -1)
  1186.       (buffer-substring begin (point)))))))
  1187.  
  1188. (defun gud-call (fmt &optional arg)
  1189.   (let ((msg (gud-format-command fmt arg)))
  1190.     (message "Command: %s" msg)
  1191.     (sit-for 0)
  1192.     (gud-basic-call msg)))
  1193.  
  1194. (defun gud-basic-call (command)
  1195.   "Invoke the debugger COMMAND displaying source in other window."
  1196.   (interactive)
  1197.   (gud-set-buffer)
  1198.   (let ((command (concat command "\n"))
  1199.     (proc (get-buffer-process gud-comint-buffer)))
  1200.  
  1201.     ;; Arrange for the current prompt to get deleted.
  1202.     (save-excursion
  1203.       (set-buffer gud-comint-buffer)
  1204.       (goto-char (process-mark proc))
  1205.       (beginning-of-line)
  1206.       (if (looking-at comint-prompt-regexp)
  1207.       (set-marker gud-delete-prompt-marker (point))))
  1208.     (process-send-string proc command)))
  1209.  
  1210. (defun gud-refresh (&optional arg)
  1211.   "Fix up a possibly garbled display, and redraw the arrow."
  1212.   (interactive "P")
  1213.   (recenter arg)
  1214.   (or gud-last-frame (setq gud-last-frame gud-last-last-frame))
  1215.   (gud-display-frame))
  1216.  
  1217. ;;; Code for parsing expressions out of C code.  The single entry point is
  1218. ;;; find-c-expr, which tries to return an lvalue expression from around point.
  1219. ;;;
  1220. ;;; The rest of this file is a hacked version of gdbsrc.el by
  1221. ;;; Debby Ayers <ayers@asc.slb.com>,
  1222. ;;; Rich Schaefer <schaefer@asc.slb.com> Schlumberger, Austin, Tx.
  1223.  
  1224. (defun find-c-expr ()
  1225.   "Returns the C expr that surrounds point."
  1226.   (interactive)
  1227.   (save-excursion
  1228.     (let ((p) (expr) (test-expr))
  1229.       (setq p (point))
  1230.       (setq expr (expr-cur))
  1231.       (setq test-expr (expr-prev))
  1232.       (while (expr-compound test-expr expr)
  1233.     (setq expr (cons (car test-expr) (cdr expr)))
  1234.     (goto-char (car expr))
  1235.     (setq test-expr (expr-prev)))
  1236.       (goto-char p)
  1237.       (setq test-expr (expr-next))
  1238.       (while (expr-compound expr test-expr)
  1239.     (setq expr (cons (car expr) (cdr test-expr)))
  1240.     (setq test-expr (expr-next))
  1241.     )
  1242.       (buffer-substring (car expr) (cdr expr)))))
  1243.  
  1244. (defun expr-cur ()
  1245.   "Returns the expr that point is in; point is set to beginning of expr.
  1246. The expr is represented as a cons cell, where the car specifies the point in
  1247. the current buffer that marks the beginning of the expr and the cdr specifies 
  1248. the character after the end of the expr."
  1249.   (let ((p (point)) (begin) (end))
  1250.     (expr-backward-sexp)
  1251.     (setq begin (point))
  1252.     (expr-forward-sexp)
  1253.     (setq end (point))
  1254.     (if (>= p end) 
  1255.     (progn
  1256.      (setq begin p)
  1257.      (goto-char p)
  1258.      (expr-forward-sexp)
  1259.      (setq end (point))
  1260.      )
  1261.       )
  1262.     (goto-char begin)
  1263.     (cons begin end)))
  1264.  
  1265. (defun expr-backward-sexp ()
  1266.   "Version of `backward-sexp' that catches errors."
  1267.   (condition-case nil
  1268.       (backward-sexp)
  1269.     (error t)))
  1270.  
  1271. (defun expr-forward-sexp ()
  1272.   "Version of `forward-sexp' that catches errors."
  1273.   (condition-case nil
  1274.      (forward-sexp)
  1275.     (error t)))
  1276.  
  1277. (defun expr-prev ()
  1278.   "Returns the previous expr, point is set to beginning of that expr.
  1279. The expr is represented as a cons cell, where the car specifies the point in
  1280. the current buffer that marks the beginning of the expr and the cdr specifies 
  1281. the character after the end of the expr"
  1282.   (let ((begin) (end))
  1283.     (expr-backward-sexp)
  1284.     (setq begin (point))
  1285.     (expr-forward-sexp)
  1286.     (setq end (point))
  1287.     (goto-char begin)
  1288.     (cons begin end)))
  1289.  
  1290. (defun expr-next ()
  1291.   "Returns the following expr, point is set to beginning of that expr.
  1292. The expr is represented as a cons cell, where the car specifies the point in
  1293. the current buffer that marks the beginning of the expr and the cdr specifies 
  1294. the character after the end of the expr."
  1295.   (let ((begin) (end))
  1296.     (expr-forward-sexp)
  1297.     (expr-forward-sexp)
  1298.     (setq end (point))
  1299.     (expr-backward-sexp)
  1300.     (setq begin (point))
  1301.     (cons begin end)))
  1302.  
  1303. (defun expr-compound-sep (span-start span-end)
  1304.   "Returns '.' for '->' & '.', returns ' ' for white space,
  1305. returns '?' for other punctuation."
  1306.   (let ((result ? )
  1307.     (syntax))
  1308.     (while (< span-start span-end)
  1309.       (setq syntax (char-syntax (char-after span-start)))
  1310.       (cond
  1311.        ((= syntax ? ) t)
  1312.        ((= syntax ?.) (setq syntax (char-after span-start))
  1313.     (cond 
  1314.      ((= syntax ?.) (setq result ?.))
  1315.      ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
  1316.       (setq result ?.)
  1317.       (setq span-start (+ span-start 1)))
  1318.      (t (setq span-start span-end)
  1319.         (setq result ??)))))
  1320.       (setq span-start (+ span-start 1)))
  1321.     result))
  1322.  
  1323. (defun expr-compound (first second)
  1324.   "Non-nil if concatenating FIRST and SECOND makes a single C token.
  1325. The two exprs are represented as a cons cells, where the car 
  1326. specifies the point in the current buffer that marks the beginning of the 
  1327. expr and the cdr specifies the character after the end of the expr.
  1328. Link exprs of the form:
  1329.       Expr -> Expr
  1330.       Expr . Expr
  1331.       Expr (Expr)
  1332.       Expr [Expr]
  1333.       (Expr) Expr
  1334.       [Expr] Expr"
  1335.   (let ((span-start (cdr first))
  1336.     (span-end (car second))
  1337.     (syntax))
  1338.     (setq syntax (expr-compound-sep span-start span-end))
  1339.     (cond
  1340.      ((= (car first) (car second)) nil)
  1341.      ((= (cdr first) (cdr second)) nil)
  1342.      ((= syntax ?.) t)
  1343.      ((= syntax ? )
  1344.      (setq span-start (char-after (- span-start 1)))
  1345.      (setq span-end (char-after span-end))
  1346.      (cond
  1347.       ((= span-start ?) ) t )
  1348.       ((= span-start ?] ) t )
  1349.           ((= span-end ?( ) t )
  1350.       ((= span-end ?[ ) t )
  1351.       (t nil))
  1352.      )
  1353.      (t nil))))
  1354.  
  1355. (provide 'gud)
  1356.  
  1357. ;;; gud.el ends here
  1358.