[comp.emacs] GDB Tool

cef@h.cs.cmu.edu (Charles Fineman) (07/07/87)

Note what my oirginal post asked for folks. I was talking about gdb *tool*,
NOT GDB! Thanks to those who told me what I already know! (not *really* a 
flame, just read a little more carefully folks ;-).

GDB tool is analogous to dbxTool (I guess, not ever having used the beast).
I have since dug up a man page but it wasn't really helpful (in the sense that
GDBtool is a simple to use as it seems).

As for those folks who are interested in where I got this widgit... the fellow
who maintains GDB here at CMU says he can't find the sources for it but when
he does, I would be more than happy to make it available to whomever wants to
use it. Personally, I prefer the typing interface to the debugger. The only 
real use I have for GDBtool is that it makes it pretty easy to stop at a 
given point in the text. Even then I find it equally as tedidious as figuring 
out the line number by hand and then telling the debugger to stop there
(especially when the point of interest is not in the file being displayed by 
GDBtool)

	Charles Fineman
	Carnegie-Mellon University
	cef@h.cs.cmu.edu (via seismo)

	"I curse the fates that bind me to this strange land"

umerin@flab.flab.fujitsu.JUNET (Masanobu UMEDA) (07/10/87)

In article <1069@h.cs.cmu.edu> cef@h.cs.cmu.edu (Charles Fineman) writes:
>GDB tool is analogous to dbxTool.
> Personally, I prefer the typing interface to the debugger. The only 
>real use I have for GDBtool is that it makes it pretty easy to stop at a 
>given point in the text. Even then I find it equally as tedidious as figuring 
>out the line number by hand and then telling the debugger to stop there
>(especially when the point of interest is not in the file being displayed by 
>GDBtool)

How about this? This is not for gdb, but for dbx. It is not so
difficult modifying `dbx.el' for gdb.

Masanobu UMEDA
umerin@flab.flab.Fujitsu.JUNET
umerin%flab.flab.Fujitsu.JUNET@seismo.CSS.GOV
---------------------------------------------------------------------------
;; Run dbx under Emacs (for GNU Emacs 18.41)
;; Copyright (C) 1987 Masanobu UMEDA (umerin@flab.flab.Fujitsu.JUNET)
;; $Header: dbx.el,v 1.5 87/05/14 21:55:16 umerin Exp $

;; This file is part of GNU Emacs.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.

;; Known bugs: On X Window System dbx mode does not work. This is
;; because it uses `send-string-to-terminal' to display an arrow (=>)
;; at the line being executed. But it still works on usual terminals.
;; RMS will solve this problem. Until then, I will personaly
;; distribute it.

(require 'shell)

(defvar dbx-debugger "dbx"
  "Dbx debugger name")

(defvar dbx-prompt-pattern "^[^)]*dbx) *"
  "Dbx prompt pattern")

(defvar dbx-language-mode-list '(c-mode)
  "List of major mode names of the language supported by both emacs and dbx.")

(defvar dbx-trace-flag nil
  "Dbx trace switch.")

(defvar dbx-break-point
  "stopped in .* at line \\([0-9]*\\) in file \"\\([^\"]*\\)\""
  "Regexp of pattern that dbx writes at break point.")

(defvar inferior-dbx-mode-map nil)
(if inferior-dbx-mode-map
    nil
  (setq inferior-dbx-mode-map (copy-alist shell-mode-map))
  (define-key inferior-dbx-mode-map "\C-c\C-p" 'dbx-where)
  (define-key inferior-dbx-mode-map "\C-c\C-t" 'dbx-trace-mode)
  (global-set-key "\C-c\C-@" 'dbx-stop-at))

(defun inferior-dbx-mode ()
  "Major mode for interacting with an inferior Dbx process.

The following commands are available:
\\{inferior-dbx-mode-map}

Entry to this mode calls the value of dbx-mode-hook with no arguments,
if that value is non-nil.  Likewise with the value of shell-mode-hook.
dbx-mode-hook is called after shell-mode-hook.

You can display the debugging program in other window and point out
the line being executed using the command \\[dbx-where].

\\[dbx-trace-mode] toggles dbx-trace mode. In dbx-trace mode,
debugging program is automatically traced using output from dbx.

The command \\[dbx-stop-at] sets break point at current line of the
program in the buffer. Major mode name of the buffer must be in
dbx-language-mode-list.

Commands:

Return at end of buffer sends line as input.
Return not at end copies rest of line to end and sends it.
\\[shell-send-eof] sends end-of-file as input.
\\[kill-shell-input] and \\[backward-kill-word] are kill commands, imitating normal Unix input editing.
\\[interrupt-shell-subjob] interrupts the shell or its current subjob if any.
\\[stop-shell-subjob] stops, likewise. \\[quit-shell-subjob] sends quit signal, likewise.
\\[dbx-where] displays debugging program in other window and
 points out the line being executed.
\\[dbx-trace-mode] toggles dbx-trace mode.
\\[dbx-stop-at] sets break point at current line."
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'inferior-dbx-mode)
  (setq mode-name "Inferior Dbx")
  (setq mode-line-process '(": %s"))
  (if (not (assoc 'dbx-trace-flag minor-mode-alist))
      (setq minor-mode-alist
	    (cons (list 'dbx-trace-flag " Trace") minor-mode-alist)))
  (use-local-map inferior-dbx-mode-map)
  (make-local-variable 'last-input-start)
  (setq last-input-start (make-marker))
  (make-local-variable 'last-input-end)
  (setq last-input-end (make-marker))
  (make-local-variable 'dbx-trace-flag)
  (setq dbx-trace-flag t)
  (make-variable-buffer-local 'shell-prompt-pattern)
  (setq shell-prompt-pattern dbx-prompt-pattern) ;Set dbx prompt pattern
  (run-hooks 'shell-mode-hook 'dbx-mode-hook))

(defun run-dbx ()
  "Run an inferior Dbx process, input and output via buffer *dbx*."
  (interactive)
  (switch-to-buffer (make-shell "dbx" dbx-debugger))
  (set-process-filter (get-process "dbx") 'dbx-filter)
  (inferior-dbx-mode))

(defun dbx-trace-mode (arg)
  "Toggle dbx-trace mode.
With arg, turn dbx-trace mode on iff arg is positive.
In dbx-trace mode, user program is automatically traced."
  (interactive "P")
  (if (not (eql major-mode 'inferior-dbx-mode))
      (error "Not in inferior-dbx mode."))
  (setq dbx-trace-flag
	(if (null arg)
	    (not dbx-trace-flag)
	  (> (prefix-numeric-value arg) 0)))
  (set-buffer-modified-p (buffer-modified-p))) ;Updates mode line.

(defun dbx-filter (process string)
  "Trace debugging program automatically if dbx-trace-flag is not nil."
  (save-excursion
    (set-buffer (process-buffer process))
    (goto-char (point-max))
    (let ((beg (point)))
      (insert string)
      (if dbx-trace-flag		;Trace mode is on?
	  (dbx-where beg t)))
    (if (process-mark process)
	(set-marker (process-mark process) (point-max))))
  (if (eq (process-buffer process)
	  (current-buffer))
      (goto-char (point-max)))
  )

(defun dbx-where (&optional begin quiet)
  "Display dbx'ed program in other window and
point out the line being executed.
BEGIN bounds the search. If QUIET, just return nil (no error) if fail."
  (interactive)
  (let (file line)
    (save-excursion
      (if (re-search-backward dbx-break-point begin quiet)
	  (progn
	    (setq line (buffer-substring (match-beginning 1) (match-end 1)))
	    (setq file (buffer-substring (match-beginning 2) (match-end 2)))
	    )))
    (if (and file line)			;Find break point?
	(progn
	  (find-file-other-window (expand-file-name file nil))
	  (setq buffer-read-only t)	;Read-only
	  (beginning-of-line)
	  (sit-for 0)			;Wait a little
	  (send-string-to-terminal "\r  \r") ;Clear "=>"
	  (send-string-to-terminal	;Re-display
	   (buffer-substring (point) (min (+ (point) 2)
					  (progn (end-of-line) (point)))))
	  (goto-line (string-to-int line)) ;Jump to the line
	  (sit-for 0)			;Wait a little
	  (send-string-to-terminal "\r=>") ;Print "=>"
	  (other-window 1))		;Return to dbx
      )))

(defun dbx-stop-at ()
  "Set break point at current line."
  (interactive)
  (if (not (memq major-mode dbx-language-mode-list))
      (error "Dbx-stop-at: this language is not supported yet."))
  (let ((file-name
	 (substring buffer-file-name
		    (length (file-name-directory buffer-file-name))))
	(line (what-line)))
    ;; I don't know how to get the line number directly.
    (setq line (string-to-int (substring line (cdr (read-from-string line)))))
    (send-string "dbx" (concat "stop at \"" file-name "\":" line "\n")))
  )
-- 
Masanobu UMEDA
umerin@flab.flab.Fujitsu.JUNET
umerin%flab.flab.Fujitsu.JUNET@seismo.CSS.GOV