Commit 0f9c2d46 authored by Juanma Barranquero's avatar Juanma Barranquero
Browse files

Moved from lisp/.

parent e6e96fbf
2003-06-05 Juanma Barranquero <>
* gud.el: Moved to progmodes.
2003-06-05 Benjamin Riefenstahl <> (tiny change)
* progmodes/tcl.el (tcl-mode): Set imenu-generic-expression to the
value of tcl-imenu-generic-expression instead of the symbol.
2003-06-05 Luc Teirlinck <>
* info.el (Info-mode): Mention `c' and remove duplicate mention
;;; gud.el --- Grand Unified Debugger mode for running GDB and other debuggers
;; Author: Eric S. Raymond <>
;; Maintainer: FSF
;; Keywords: unix, tools
;; Copyright (C) 1992,93,94,95,96,1998,2000,02,2003 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; The ancestral gdb.el was by W. Schelter <>
;; It was later rewritten by rms. Some ideas were due to Masanobu.
;; Grand Unification (sdb/dbx support) by Eric S. Raymond <>
;; The overloading code was then rewritten by Barry Warsaw <>,
;; who also hacked the mode to use comint.el. Shane Hartman <>
;; added support for xdb (HPUX debugger). Rick Sladkey <>
;; wrote the GDB command completion code. Dave Love <>
;; added the IRIX kluge, re-implemented the Mips-ish variant and added
;; a menu. Brian D. Carlstrom <> combined the IRIX kluge with
;; the gud-xdb-directories hack producing gud-dbx-directories. Derek L. Davies
;; <> added support for jdb (Java debugger.)
;;; Code:
(require 'comint)
(require 'etags)
;; ======================================================================
;; GUD commands must be visible in C buffers visited by GUD
(defgroup gud nil
"Grand Unified Debugger mode for gdb and other debuggers under Emacs.
Supported debuggers include gdb, sdb, dbx, xdb, perldb, pdb (Python), jdb, and bash."
:group 'unix
:group 'tools)
(defcustom gud-key-prefix "\C-x\C-a"
"Prefix of all GUD commands valid in C buffers."
:type 'string
:group 'gud)
(global-set-key (concat gud-key-prefix "\C-l") 'gud-refresh)
(define-key ctl-x-map " " 'gud-break) ;; backward compatibility hack
(defvar gud-marker-filter nil)
(put 'gud-marker-filter 'permanent-local t)
(defvar gud-find-file nil)
(put 'gud-find-file 'permanent-local t)
(defun gud-marker-filter (&rest args)
(apply gud-marker-filter args))
(defvar gud-minor-mode nil)
(put 'gud-minor-mode 'permanent-local t)
(defvar gud-keep-buffer nil)
(defun gud-symbol (sym &optional soft minor-mode)
"Return the symbol used for SYM in MINOR-MODE.
MINOR-MODE defaults to `gud-minor-mode.
The symbol returned is `gud-<MINOR-MODE>-<SYM>'.
If SOFT is non-nil, returns nil if the symbol doesn't already exist."
(unless (or minor-mode gud-minor-mode) (error "Gud internal error"))
(funcall (if soft 'intern-soft 'intern)
(format "gud-%s-%s" (or minor-mode gud-minor-mode) sym)))
(defun gud-val (sym &optional minor-mode)
"Return the value of `gud-symbol' SYM. Default to nil."
(let ((sym (gud-symbol sym t minor-mode)))
(if (boundp sym) (symbol-value sym))))
(defvar gud-running nil
"Non-nil if debuggee is running.
Used to grey out relevant toolbar icons.")
(easy-mmode-defmap gud-menu-map
'(([refresh] "Refresh" . gud-refresh)
([run] menu-item "Run" gud-run
:enable (and (not gud-running)
(memq gud-minor-mode '(gdba gdb jdb))))
([goto] menu-item "Continue to selection" gud-until
:enable (and (not gud-running)
(memq gud-minor-mode '(gdba gdb))))
([remove] menu-item "Remove Breakpoint" gud-remove
:enable (not gud-running))
([tbreak] menu-item "Temporary Breakpoint" gud-tbreak
:enable (memq gud-minor-mode '(gdba gdb sdb xdb bashdb)))
([break] menu-item "Set Breakpoint" gud-break
:enable (not gud-running))
([up] menu-item "Up Stack" gud-up
:enable (and (not gud-running)
(memq gud-minor-mode
'(gdba gdb dbx xdb jdb pdb bashdb))))
([down] menu-item "Down Stack" gud-down
:enable (and (not gud-running)
(memq gud-minor-mode
'(gdba gdb dbx xdb jdb pdb bashdb))))
([print] menu-item "Print Expression" gud-print
:enable (not gud-running))
([display] menu-item "Display Expression" gud-display
:enable (and (not gud-running)
(eq gud-minor-mode 'gdba)))
([finish] menu-item "Finish Function" gud-finish
:enable (and (not gud-running)
(memq gud-minor-mode
'(gdba gdb xdb jdb pdb bashdb))))
([stepi] menu-item "Step Instruction" gud-stepi
:enable (and (not gud-running)
(memq gud-minor-mode
'(gdba gdb dbx))))
([nexti] menu-item "Next Instruction" gud-nexti
:enable (and (not gud-running)
(memq gud-minor-mode
'(gdba gdb))))
([step] menu-item "Step Line" gud-step
:enable (not gud-running))
([next] menu-item "Next Line" gud-next
:enable (not gud-running))
([cont] menu-item "Continue" gud-cont
:enable (not gud-running)))
"Menu for `gud-mode'."
:name "Gud")
(easy-mmode-defmap gud-minor-mode-map
`(([menu-bar debug] . ("Gud" . ,gud-menu-map)))
"Map used in visited files.")
(let ((m (assq 'gud-minor-mode minor-mode-map-alist)))
(if m (setcdr m gud-minor-mode-map)
(push (cons 'gud-minor-mode gud-minor-mode-map) minor-mode-map-alist)))
(defvar gud-mode-map
;; Will inherit from comint-mode via define-derived-mode.
"`gud-mode' keymap.")
(defvar gud-tool-bar-map
(if (display-graphic-p)
(let ((map (make-sparse-keymap)))
(dolist (x '((gud-break . "gud-break")
(gud-remove . "gud-remove")
(gud-print . "gud-print")
(gud-display . "gud-display")
(gud-run . "gud-run")
(gud-until . "gud-until")
(gud-cont . "gud-cont")
(gud-step . "gud-step")
(gud-next . "gud-next")
(gud-finish . "gud-finish")
(gud-stepi . "gud-stepi")
(gud-nexti . "gud-nexti")
(gud-up . "gud-up")
(gud-down . "gud-down"))
(car x) (cdr x) map gud-minor-mode-map)))))
(defun gud-file-name (f)
"Transform a relative file name to an absolute file name.
Uses `gud-<MINOR-MODE>-directories' to find the source files."
(if (file-exists-p f) (expand-file-name f)
(let ((directories (gud-val 'directories))
(result nil))
(while directories
(let ((path (expand-file-name f (car directories))))
(if (file-exists-p path)
(setq result path
directories nil)))
(setq directories (cdr directories)))
(defun gud-find-file (file)
;; Don't get confused by double slashes in the name that comes from GDB.
(while (string-match "//+" file)
(setq file (replace-match "/" t t file)))
(let ((minor-mode gud-minor-mode)
(buf (funcall (or gud-find-file 'gud-file-name) file)))
(when (stringp buf)
(setq buf (and (file-readable-p buf) (find-file-noselect buf 'nowarn))))
(when buf
;; Copy `gud-minor-mode' to the found buffer to turn on the menu.
(with-current-buffer buf
(set (make-local-variable 'gud-minor-mode) minor-mode)
(set (make-local-variable 'tool-bar-map) gud-tool-bar-map)
(make-local-variable 'gud-keep-buffer))
;; ======================================================================
;; command definition
;; This macro is used below to define some basic debugger interface commands.
;; Of course you may use `gud-def' with any other debugger command, including
;; user defined ones.
;; A macro call like (gud-def FUNC NAME KEY DOC) expands to a form
;; which defines FUNC to send the command NAME to the debugger, gives
;; it the docstring DOC, and binds that function to KEY in the GUD
;; major mode. The function is also bound in the global keymap with the
;; GUD prefix.
(defmacro gud-def (func cmd key &optional doc)
"Define FUNC to be a command sending STR and bound to KEY, with
optional doc string DOC. Certain %-escapes in the string arguments
are interpreted specially if present. These are:
%f name (without directory) of current source file.
%F name (without directory or extension) of current source file.
%d directory of current source file.
%l number of current source line
%e text of the C lvalue or function-call expression surrounding point.
%a text of the hexadecimal address surrounding point
%p prefix argument to the command (if any) as a number
The `current' source file is the file of the current buffer (if
we're in a C file) or the source file current at the last break or
step (if we're in the GUD buffer).
The `current' line is that of the current buffer (if we're in a
source file) or the source line number at the last break or step (if
we're in the GUD buffer)."
(defun ,func (arg)
,@(if doc (list doc))
(interactive "p")
,(if (stringp cmd)
`(gud-call ,cmd arg)
,(if key `(local-set-key ,(concat "\C-c" key) ',func))
,(if key `(global-set-key (vconcat gud-key-prefix ,key) ',func))))
;; Where gud-display-frame should put the debugging arrow; a cons of
;; (filename . line-number). This is set by the marker-filter, which scans
;; the debugger's output for indications of the current program counter.
(defvar gud-last-frame nil)
;; Used by gud-refresh, which should cause gud-display-frame to redisplay
;; the last frame, even if it's been called before and gud-last-frame has
;; been set to nil.
(defvar gud-last-last-frame nil)
;; All debugger-specific information is collected here.
;; Here's how it works, in case you ever need to add a debugger to the mode.
;; Each entry must define the following at startup:
;; comint-prompt-regexp
;; gud-<name>-massage-args
;; gud-<name>-marker-filter
;; gud-<name>-find-file
;; The job of the massage-args method is to modify the given list of
;; debugger arguments before running the debugger.
;; The job of the marker-filter method is to detect file/line markers in
;; strings and set the global gud-last-frame to indicate what display
;; action (if any) should be triggered by the marker. Note that only
;; whatever the method *returns* is displayed in the buffer; thus, you
;; can filter the debugger's output, interpreting some and passing on
;; the rest.
;; The job of the find-file method is to visit and return the buffer indicated
;; by the car of gud-tag-frame. This may be a file name, a tag name, or
;; something else.
;; ======================================================================
;; speedbar support functions and variables.
(eval-when-compile (require 'speedbar)) ;For speedbar-with-attached-buffer.
(defvar gud-last-speedbar-buffer nil
"The last GUD buffer used.")
(defvar gud-last-speedbar-stackframe nil
"Description of the currently displayed GUD stack.
t means that there is no stack, and we are in display-file mode.")
(defvar gud-speedbar-key-map nil
"Keymap used when in the buffers display mode.")
(defun gud-install-speedbar-variables ()
"Install those variables used by speedbar to enhance gud/gdb."
(if gud-speedbar-key-map
(setq gud-speedbar-key-map (speedbar-make-specialized-keymap))
(define-key gud-speedbar-key-map "j" 'speedbar-edit-line)
(define-key gud-speedbar-key-map "e" 'speedbar-edit-line)
(define-key gud-speedbar-key-map "\C-m" 'speedbar-edit-line)))
(defvar gud-speedbar-menu-items
;; Note to self. Add expand, and turn off items when not available.
'(["Jump to stack frame" speedbar-edit-line t])
"Additional menu items to add to the speedbar frame.")
;; Make sure our special speedbar mode is loaded
(if (featurep 'speedbar)
(add-hook 'speedbar-load-hook 'gud-install-speedbar-variables))
(defun gud-speedbar-buttons (buffer)
"Create a speedbar display based on the current state of GUD.
If the GUD BUFFER is not running a supported debugger, then turn
off the specialized speedbar mode."
(if (and (save-excursion (goto-char (point-min))
(looking-at "Current Stack"))
(equal gud-last-last-frame gud-last-speedbar-stackframe))
(setq gud-last-speedbar-buffer buffer)
(let* ((minor-mode (with-current-buffer buffer gud-minor-mode))
(cond ((memq minor-mode '(gdba gdb))
(gud-gdb-get-stackframe buffer))
;; Add more debuggers here!
(speedbar-remove-localized-speedbar-support buffer)
(if (not frames)
(insert "No Stack frames\n")
(insert "Current Stack:\n"))
(while frames
(insert (nth 1 (car frames)) ":\n")
(if (= (length (car frames)) 2)
; (speedbar-insert-button "[?]"
; 'speedbar-button-face
; nil nil nil t)
(speedbar-insert-button (car (car frames))
nil nil nil t))
; (speedbar-insert-button "[+]"
; 'speedbar-button-face
; 'speedbar-highlight-face
; 'gud-gdb-get-scope-data
; (car frames) t)
(speedbar-insert-button (car (car frames))
(cond ((memq minor-mode '(gdba gdb))
(t (error "Should never be here")))
(car frames) t))
(setq frames (cdr frames)))
; (let ((selected-frame
; (cond ((eq ff 'gud-gdb-find-file)
; (gud-gdb-selected-frame-info buffer))
; (t (error "Should never be here"))))))
(setq gud-last-speedbar-stackframe gud-last-last-frame)))
;; ======================================================================
;; gdb functions
;; History of argument lists passed to gdb.
(defvar gud-gdb-history nil)
(defcustom gud-gdb-command-name "gdb --fullname"
"Default command to execute an executable under the GDB debugger."
:type 'string
:group 'gud)
(defvar gud-gdb-marker-regexp
;; This used to use path-separator instead of ":";
;; however, we found that on both Windows 32 and MSDOS
;; a colon is correct here.
(concat "\032\032\\(.:?[^" ":" "\n]*\\)" ":"
"\\([0-9]*\\)" ":" ".*\n"))
;; There's no guarantee that Emacs will hand the filter the entire
;; marker at once; it could be broken up across several strings. We
;; might even receive a big chunk with several markers in it. If we
;; receive a chunk of text which looks like it might contain the
;; beginning of a marker, we save it here between calls to the
;; filter.
(defvar gud-marker-acc "")
(make-variable-buffer-local 'gud-marker-acc)
(defun gud-gdb-marker-filter (string)
(setq gud-marker-acc (concat gud-marker-acc string))
(let ((output ""))
;; Process all the complete markers in this chunk.
(while (string-match gud-gdb-marker-regexp gud-marker-acc)
;; Extract the frame position from the marker.
gud-last-frame (cons (match-string 1 gud-marker-acc)
(string-to-int (match-string 2 gud-marker-acc)))
;; Append any text before the marker to the output we're going
;; to return - we don't include the marker in this text.
output (concat output
(substring gud-marker-acc 0 (match-beginning 0)))
;; Set the accumulator to the remaining text.
gud-marker-acc (substring gud-marker-acc (match-end 0))))
;; Does the remaining text look like it might end with the
;; beginning of another marker? If it does, then keep it in
;; gud-marker-acc until we receive the rest of it. Since we
;; know the full marker regexp above failed, it's pretty simple to
;; test for marker starts.
(if (string-match "\032.*\\'" gud-marker-acc)
;; Everything before the potential marker start can be output.
(setq output (concat output (substring gud-marker-acc
0 (match-beginning 0))))
;; Everything after, we save, to combine with later input.
(setq gud-marker-acc
(substring gud-marker-acc (match-beginning 0))))
(setq output (concat output gud-marker-acc)
gud-marker-acc ""))
(easy-mmode-defmap gud-minibuffer-local-map
'(("\C-i" . comint-dynamic-complete-filename))
"Keymap for minibuffer prompting of gud startup command."
:inherit minibuffer-local-map)
(defun gud-query-cmdline (minor-mode &optional init)
(let* ((hist-sym (gud-symbol 'history nil minor-mode))
(cmd-name (gud-val 'command-name minor-mode)))
(unless (boundp hist-sym) (set hist-sym nil))
(format "Run %s (like this): " minor-mode)
(or (car-safe (symbol-value hist-sym))
(concat (or cmd-name (symbol-name minor-mode))
" "
(or init
(let ((file nil))
(dolist (f (directory-files default-directory) file)
(if (and (file-executable-p f)
(not (file-directory-p f))
(or (not file)
(file-newer-than-file-p f file)))
(setq file f)))))))
gud-minibuffer-local-map nil
(defun gdb (command-line)
"Run gdb on program FILE in buffer *gud-FILE*.
The directory containing FILE becomes the initial working directory
and source-file directory for your debugger."
(interactive (list (gud-query-cmdline 'gdb)))
(gud-common-init command-line nil 'gud-gdb-marker-filter)
(set (make-local-variable 'gud-minor-mode) 'gdb)
(gud-def gud-break "break %f:%l" "\C-b" "Set breakpoint at current line.")
(gud-def gud-tbreak "tbreak %f:%l" "\C-t" "Set temporary breakpoint at current line.")
(gud-def gud-remove "clear %f:%l" "\C-d" "Remove breakpoint at current line")
(gud-def gud-step "step %p" "\C-s" "Step one source line with display.")
(gud-def gud-stepi "stepi %p" "\C-i" "Step one instruction with display.")
(gud-def gud-next "next %p" "\C-n" "Step one line (skip functions).")
(gud-def gud-nexti "nexti %p" nil "Step one instruction (skip functions).")
(gud-def gud-cont "cont" "\C-r" "Continue with display.")
(gud-def gud-finish "finish" "\C-f" "Finish executing current function.")
(gud-def gud-jump "tbreak %f:%l\njump %f:%l" "\C-j" "Relocate execution address to line at point in source buffer.")
(gud-def gud-up "up %p" "<" "Up N stack frames (numeric arg).")
(gud-def gud-down "down %p" ">" "Down N stack frames (numeric arg).")
(gud-def gud-print "print %e" "\C-p" "Evaluate C expression at point.")
(gud-def gud-until "until %l" "\C-u" "Continue to current line.")
(gud-def gud-run "run" nil "Run the program.")
(local-set-key "\C-i" 'gud-gdb-complete-command)
(setq comint-prompt-regexp "^(.*gdb[+]?) *")
(setq paragraph-start comint-prompt-regexp)
(run-hooks 'gdb-mode-hook)
;; One of the nice features of GDB is its impressive support for
;; context-sensitive command completion. We preserve that feature
;; in the GUD buffer by using a GDB command designed just for Emacs.
;; The completion process filter indicates when it is finished.
(defvar gud-gdb-fetch-lines-in-progress)
;; Since output may arrive in fragments we accumulate partials strings here.
(defvar gud-gdb-fetch-lines-string)
;; We need to know how much of the completion to chop off.
(defvar gud-gdb-fetch-lines-break)
;; The completion list is constructed by the process filter.
(defvar gud-gdb-fetched-lines)
(defvar gud-comint-buffer nil)
(defun gud-gdb-complete-command ()
"Perform completion on the GDB command preceding point.
This is implemented using the GDB `complete' command which isn't
available with older versions of GDB."
(let* ((end (point))
(command (buffer-substring (comint-line-beginning-position) end))
;; Find the word break. This match will always succeed.
(and (string-match "\\(\\`\\| \\)\\([^ ]*\\)\\'" command)
(substring command (match-beginning 2))))
(gud-gdb-run-command-fetch-lines (concat "complete " command)
;; From string-match above.
(match-beginning 2))))
;; Protect against old versions of GDB.
(and complete-list
(string-match "^Undefined command: \"complete\"" (car complete-list))
(error "This version of GDB doesn't support the `complete' command"))
;; Sort the list like readline.
(setq complete-list (sort complete-list (function string-lessp)))
;; Remove duplicates.
(let ((first complete-list)
(second (cdr complete-list)))
(while second
(if (string-equal (car first) (car second))
(setcdr first (setq second (cdr second)))
(setq first second
second (cdr second)))))
;; Add a trailing single quote if there is a unique completion
;; and it contains an odd number of unquoted single quotes.
(and (= (length complete-list) 1)
(let ((str (car complete-list))
(pos 0)
(count 0))
(while (string-match "\\([^'\\]\\|\\\\'\\)*'" str pos)
(setq count (1+ count)
pos (match-end 0)))
(and (= (mod count 2) 1)
(setq complete-list (list (concat str "'"))))))
;; Let comint handle the rest.
(comint-dynamic-simple-complete command-word complete-list)))
;; The completion process filter is installed temporarily to slurp the
;; output of GDB up to the next prompt and build the completion list.
(defun gud-gdb-fetch-lines-filter (string filter)
"Filter used to read the list of lines output by a command.
STRING is the output to filter.
It is passed through FILTER before we look at it."
(setq string (funcall filter string))
(setq string (concat gud-gdb-fetch-lines-string string))
(while (string-match "\n" string)
(push (substring string gud-gdb-fetch-lines-break (match-beginning 0))
(setq string (substring string (match-end 0))))
(if (string-match comint-prompt-regexp string)
(setq gud-gdb-fetch-lines-in-progress nil)
(setq gud-gdb-fetch-lines-string string)
;; gdb speedbar functions
(defun gud-gdb-goto-stackframe (text token indent)
"Goto the stackframe described by TEXT, TOKEN, and INDENT."
(gud-basic-call (concat "server frame " (nth 1 token)))
(sit-for 1)))
(defvar gud-gdb-fetched-stack-frame nil
"Stack frames we are fetching from GDB.")
;(defun gud-gdb-get-scope-data (text token indent)
; ;; checkdoc-params: (indent)
; "Fetch data associated with a stack frame, and expand/contract it.
;Data to do this is retrieved from TEXT and TOKEN."
; (let ((args nil) (scope nil))
; (gud-gdb-run-command-fetch-lines "info args")
; (gud-gdb-run-command-fetch-lines "info local")
; ))
(defun gud-gdb-get-stackframe (buffer)
"Extract the current stack frame out of the GUD GDB BUFFER."
(let ((newlst nil)
(gud-gdb-run-command-fetch-lines "server backtrace" buffer)))
(if (and (car fetched-stack-frame-list)
(string-match "No stack" (car fetched-stack-frame-list)))
;; Go into some other mode???
(dolist (e fetched-stack-frame-list)
(let ((name nil) (num nil))
(if (not (or
(string-match "^#\\([0-9]+\\) +[0-9a-fx]+ in \\([:0-9a-zA-Z_]+\\) (" e)
(string-match "^#\\([0-9]+\\) +\\([:0-9a-zA-Z_]+\\) (" e)))