home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / comint / gdb.el < prev    next >
Encoding:
Text File  |  1995-08-15  |  21.9 KB  |  639 lines

  1. ;;; gdb.el --- run gdb under Emacs
  2.  
  3. ;; Author: W. Schelter, University of Texas
  4. ;;     wfs@rascal.ics.utexas.edu
  5. ;; Rewritten by rms.
  6. ;; Keywords: c, unix, tools, debugging
  7.  
  8. ;; Some ideas are due to Masanobu.
  9.  
  10. ;; This file is part of XEmacs.
  11.  
  12. ;; XEmacs is free software; you can redistribute it and/or modify it
  13. ;; under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16.  
  17. ;; XEmacs is distributed in the hope that it will be useful, but
  18. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20. ;; General Public License for more details.
  21.  
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  24. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. ;; Description of GDB interface:
  27.  
  28. ;; A facility is provided for the simultaneous display of the source code
  29. ;; in one window, while using gdb to step through a function in the
  30. ;; other.  A small arrow in the source window, indicates the current
  31. ;; line.
  32.  
  33. ;; Starting up:
  34.  
  35. ;; In order to use this facility, invoke the command GDB to obtain a
  36. ;; shell window with the appropriate command bindings.  You will be asked
  37. ;; for the name of a file to run.  Gdb will be invoked on this file, in a
  38. ;; window named *gdb-foo* if the file is foo.
  39.  
  40. ;; M-s steps by one line, and redisplays the source file and line.
  41.  
  42. ;; You may easily create additional commands and bindings to interact
  43. ;; with the display.  For example to put the gdb command next on \M-n
  44. ;; (def-gdb next "\M-n")
  45.  
  46. ;; This causes the emacs command gdb-next to be defined, and runs
  47. ;; gdb-display-frame after the command.
  48.  
  49. ;; gdb-display-frame is the basic display function.  It tries to display
  50. ;; in the other window, the file and line corresponding to the current
  51. ;; position in the gdb window.  For example after a gdb-step, it would
  52. ;; display the line corresponding to the position for the last step.  Or
  53. ;; if you have done a backtrace in the gdb buffer, and move the cursor
  54. ;; into one of the frames, it would display the position corresponding to
  55. ;; that frame.
  56.  
  57. ;; gdb-display-frame is invoked automatically when a filename-and-line-number
  58. ;; appears in the output.
  59.  
  60. (require 'comint)
  61. (require 'shell)
  62.  
  63. (condition-case nil
  64.     (if (featurep 'toolbar)
  65.     (require 'eos-toolbar "sun-eos-toolbar"))
  66.   (error nil))
  67.  
  68. (defvar gdb-last-frame)
  69. (defvar gdb-delete-prompt-marker)
  70. (defvar gdb-filter-accumulator)
  71. (defvar gdb-last-frame-displayed-p)
  72. (defvar gdb-arrow-extent nil)
  73. (or (fboundp 'make-glyph) (fset 'make-glyph 'identity)) ; work w/ pre beta v12
  74. (defvar gdb-arrow-glyph (make-glyph "=>"))
  75.  
  76. (make-face 'gdb-arrow-face)
  77. (or (face-differs-from-default-p 'gdb-arrow-face)
  78.    ;; Usually has a better default value than highlight does
  79.    (copy-face 'isearch 'gdb-arrow-face))
  80.  
  81. ;; Hooks can side-effect extent arg to change extent properties
  82. (defvar gdb-arrow-extent-hooks '())
  83.  
  84. (defvar gdb-prompt-pattern "^>\\|^(.*gdb[+]?) *\\|^---Type <return> to.*--- *"
  85.   "A regexp to recognize the prompt for gdb or gdb+.") 
  86.  
  87. (defvar gdb-mode-map nil
  88.   "Keymap for gdb-mode.")
  89.  
  90. (defvar gdb-toolbar
  91.   '([eos::toolbar-stop-at-icon
  92.      gdb-toolbar-break
  93.      t
  94.      "Stop at selected position"]
  95.     [eos::toolbar-stop-in-icon
  96.      gdb-toolbar-break
  97.      t
  98.      "Stop in function whose name is selected"]
  99.     [eos::toolbar-clear-at-icon
  100.      gdb-toolbar-clear
  101.      t
  102.      "Clear at selected position"]
  103.     [eos::toolbar-evaluate-icon
  104.      nil
  105.      nil
  106.      "Evaluate selected expression; shows in separate XEmacs frame"]
  107.     [eos::toolbar-evaluate-star-icon
  108.      nil
  109.      nil
  110.      "Evaluate selected expression as a pointer; shows in separate XEmacs frame"]
  111.     [eos::toolbar-run-icon
  112.      gdb-run
  113.      t
  114.      "Run current program"]
  115.     [eos::toolbar-cont-icon
  116.      gdb-cont
  117.      t
  118.      "Continue current program"]
  119.     [eos::toolbar-step-into-icon
  120.      gdb-step
  121.      t
  122.      "Step into (aka step)"]
  123.     [eos::toolbar-step-over-icon
  124.      gdb-next
  125.      t
  126.      "Step over (aka next)"]
  127.     [eos::toolbar-up-icon
  128.      gdb-up
  129.      t
  130.      "Stack Up (towards \"cooler\" - less recently visited - frames)"]
  131.     [eos::toolbar-down-icon
  132.      gdb-down
  133.      t
  134.      "Stack Down (towards \"warmer\" - more recently visited - frames)"]
  135.     [eos::toolbar-fix-icon    nil    nil    "Fix (not available with gdb)"]
  136.     [eos::toolbar-build-icon
  137.      toolbar-compile
  138.      t
  139.      "Build (aka make -NYI)"]
  140.     ))
  141.  
  142. (if gdb-mode-map
  143.    nil
  144.   (setq gdb-mode-map (make-sparse-keymap))
  145.   (set-keymap-name gdb-mode-map 'gdb-mode-map)
  146.   (set-keymap-parents gdb-mode-map (list comint-mode-map))
  147.   (define-key gdb-mode-map "\C-l" 'gdb-refresh)
  148.   (define-key gdb-mode-map "\C-c\C-c" 'gdb-control-c-subjob)
  149.   (define-key gdb-mode-map "\t" 'comint-dynamic-complete)
  150.   (define-key gdb-mode-map "\M-?" 'comint-dynamic-list-completions))
  151.  
  152. (define-key ctl-x-map " " 'gdb-break)
  153. (define-key ctl-x-map "&" 'send-gdb-command)
  154.  
  155. ;;Of course you may use `def-gdb' with any other gdb command, including
  156. ;;user defined ones.   
  157.  
  158. (defmacro def-gdb (name key &optional doc &rest forms)
  159.   (let* ((fun (intern (format "gdb-%s" name)))
  160.      (cstr (list 'if '(not (= 1 arg))
  161.              (list 'format "%s %s" name 'arg)
  162.              name)))
  163.     (list 'progn
  164.       (nconc (list 'defun fun '(arg)
  165.                (or doc "")
  166.                '(interactive "p")
  167.                (list 'gdb-call cstr))
  168.          forms)
  169.       (and key (list 'define-key 'gdb-mode-map key  (list 'quote fun))))))
  170.  
  171. (def-gdb "step"   "\M-s" "Step one source line with display"
  172.   (gdb-delete-arrow-extent))
  173. (def-gdb "stepi"  "\M-i" "Step one instruction with display"
  174.   (gdb-delete-arrow-extent))
  175. (def-gdb "finish" "\C-c\C-f" "Finish executing current function"
  176.   (gdb-delete-arrow-extent))
  177. (def-gdb "run" nil "Run the current program"
  178.   (gdb-delete-arrow-extent))
  179.  
  180. ;;"next" and "cont" were bound to M-n and M-c in Emacs 18, but these are
  181. ;;poor choices, since M-n is used for history navigation and M-c is
  182. ;;capitalize-word.  These are defined without key bindings so that users
  183. ;;may choose their own bindings.
  184. (def-gdb "next"   "\C-c\C-n" "Step one source line (skip functions)"
  185.   (gdb-delete-arrow-extent))
  186. (def-gdb "cont"   "\C-c\M-c" "Proceed with the program"
  187.   (gdb-delete-arrow-extent))
  188.  
  189. (def-gdb "up"     "\C-c<" "Go up N stack frames (numeric arg) with display")
  190. (def-gdb "down"   "\C-c>" "Go down N stack frames (numeric arg) with display")
  191.  
  192. (defvar gdb-display-mode nil
  193.   "Minor mode for gdb frame display")
  194. (or (assq 'gdb-display-mode minor-mode-alist)
  195.     (setq minor-mode-alist
  196.       (purecopy
  197.        (append minor-mode-alist
  198.            '((gdb-display-mode " Frame"))))))
  199.  
  200. (defun gdb-display-mode (&optional arg)
  201.   "Toggle GDB Frame display mode
  202. With arg, turn display mode on if and only if arg is positive.
  203. In the display minor mode, source file are displayed in another
  204. window for repective \\[gdb-display-frame] commands."
  205.   (interactive "P")
  206.   (setq gdb-display-mode (if (null arg)
  207.                  (not gdb-display-mode)
  208.                (> (prefix-numeric-value arg) 0))))
  209.  
  210.  
  211. (defun gdb-mode ()
  212.   "Major mode for interacting with an inferior Gdb process.
  213. The following commands are available:
  214.  
  215. \\{gdb-mode-map}
  216.  
  217. \\[gdb-display-frame] displays in the other window
  218. the last line referred to in the gdb buffer. See also
  219. \\[gdb-display-mode].
  220.  
  221. \\[gdb-step],\\[gdb-next], and \\[gdb-nexti] in the gdb window,
  222. call gdb to step,next or nexti and then update the other window
  223. with the current file and position.
  224.  
  225. If you are in a source file, you may select a point to break
  226. at, by doing \\[gdb-break].
  227.  
  228. Commands:
  229. Many commands are inherited from comint mode. 
  230. Additionally we have:
  231.  
  232. \\[gdb-display-frame] display frames file in other window
  233. \\[gdb-step] advance one line in program
  234. \\[send-gdb-command] used for special printing of an arg at the current point.
  235. C-x SPACE sets break point at current line."
  236.   (interactive)
  237.   (comint-mode)
  238.   (use-local-map gdb-mode-map)
  239.   (set-syntax-table c-mode-syntax-table)
  240.   (make-local-variable 'gdb-last-frame-displayed-p)
  241.   (make-local-variable 'gdb-last-frame)
  242.   (make-local-variable 'gdb-delete-prompt-marker)
  243.   (make-local-variable 'gdb-display-mode)
  244.   (make-local-variable' gdb-filter-accumulator)
  245.   (setq gdb-last-frame nil
  246.         gdb-delete-prompt-marker nil
  247.         gdb-filter-accumulator nil
  248.     gdb-display-mode t
  249.         major-mode 'gdb-mode
  250.         mode-name "Inferior GDB"
  251.         comint-prompt-regexp gdb-prompt-pattern
  252.         gdb-last-frame-displayed-p t)
  253.   (set (make-local-variable 'shell-dirtrackp) t)
  254.   ;;(make-local-variable 'gdb-arrow-extent)
  255.   (and (extentp gdb-arrow-extent)
  256.        (delete-extent gdb-arrow-extent))
  257.   (setq gdb-arrow-extent nil)
  258.   (make-local-variable 'kill-buffer-hook)
  259.   (add-hook 'kill-buffer-hook 'gdb-delete-arrow-extent)
  260.   (setq comint-input-sentinel 'shell-directory-tracker)
  261.   (run-hooks 'gdb-mode-hook))
  262.  
  263. (defun gdb-delete-arrow-extent ()
  264.   (let ((inhibit-quit t))
  265.     (if gdb-arrow-extent
  266.         (delete-extent gdb-arrow-extent))
  267.     (setq gdb-arrow-extent nil)))
  268.  
  269. (defvar current-gdb-buffer nil)
  270.  
  271. ;;;###autoload
  272. (defvar gdb-command-name "gdb"
  273.   "Pathname for executing gdb.")
  274.  
  275. ;;;###autoload
  276. (defun gdb (path &optional corefile)
  277.   "Run gdb on program FILE in buffer *gdb-FILE*.
  278. The directory containing FILE becomes the initial working directory
  279. and source-file directory for GDB.  If you wish to change this, use
  280. the GDB commands `cd DIR' and `directory'."
  281.   (interactive "FRun gdb on file: ")
  282.   (setq path (file-truename (expand-file-name path)))
  283.   (let ((file (file-name-nondirectory path)))
  284.     (switch-to-buffer (concat "*gdb-" file "*"))
  285.     (setq default-directory (file-name-directory path))
  286.     (or (bolp) (newline))
  287.     (insert "Current directory is " default-directory "\n")
  288.     (apply 'make-comint
  289.        (concat "gdb-" file)
  290.        (substitute-in-file-name gdb-command-name)
  291.        nil
  292.        "-fullname"
  293.        "-cd" default-directory
  294.        file
  295.        (and corefile (list corefile)))
  296.     (set-process-filter (get-buffer-process (current-buffer)) 'gdb-filter)
  297.     (set-process-sentinel (get-buffer-process (current-buffer)) 'gdb-sentinel)
  298.     ;; XEmacs change: turn on gdb mode after setting up the proc filters
  299.     ;; for the benefit of shell-font.el
  300.     (gdb-mode)
  301.     (gdb-set-buffer)))
  302.  
  303. ;;;####autoload
  304. (defun gdb-with-core (file corefile)
  305.   "Debug a program using a corefile."
  306.   (interactive "fProgram to debug: \nfCore file to use: ")
  307.   (gdb file corefile))
  308.  
  309. (defun gdb-set-buffer ()
  310.   (cond ((eq major-mode 'gdb-mode)
  311.      (setq current-gdb-buffer (current-buffer))
  312.      (if (featurep 'eos-toolbar)
  313.          (set-specifier default-toolbar (cons (current-buffer)
  314.                           gdb-toolbar))))))
  315.  
  316.  
  317. ;; This function is responsible for inserting output from GDB
  318. ;; into the buffer.
  319. ;; Aside from inserting the text, it notices and deletes
  320. ;; each filename-and-line-number;
  321. ;; that GDB prints to identify the selected frame.
  322. ;; It records the filename and line number, and maybe displays that file.
  323. (defun gdb-filter (proc string)
  324.   (let ((inhibit-quit t))
  325.     (save-current-buffer
  326.      (set-buffer (process-buffer proc))
  327.      (if gdb-filter-accumulator
  328.      (gdb-filter-accumulate-marker
  329.       proc (concat gdb-filter-accumulator string))
  330.        (gdb-filter-scan-input proc string)))))
  331.  
  332. (defun gdb-filter-accumulate-marker (proc string)
  333.   (setq gdb-filter-accumulator nil)
  334.   (if (> (length string) 1)
  335.       (if (= (aref string 1) ?\032)
  336.       (let ((end (string-match "\n" string)))
  337.         (if end
  338.         (progn
  339.           (let* ((first-colon (string-match ":" string 2))
  340.              (second-colon
  341.               (string-match ":" string (1+ first-colon))))
  342.             (setq gdb-last-frame
  343.               (cons (substring string 2 first-colon)
  344.                 (string-to-int
  345.                  (substring string (1+ first-colon)
  346.                         second-colon)))))
  347.           (setq gdb-last-frame-displayed-p nil)
  348.           (gdb-filter-scan-input proc
  349.                      (substring string (1+ end))))
  350.           (setq gdb-filter-accumulator string)))
  351.     (gdb-filter-insert proc "\032")
  352.     (gdb-filter-scan-input proc (substring string 1)))
  353.     (setq gdb-filter-accumulator string)))
  354.  
  355. (defun gdb-filter-scan-input (proc string)
  356.   (if (equal string "")
  357.       (setq gdb-filter-accumulator nil)
  358.     (let ((start (string-match "\032" string)))
  359.       (if start
  360.       (progn (gdb-filter-insert proc (substring string 0 start))
  361.          (gdb-filter-accumulate-marker proc
  362.                            (substring string start)))
  363.     (gdb-filter-insert proc string)))))
  364.  
  365. (defun gdb-filter-insert (proc string)
  366.   (let ((moving (= (point) (process-mark proc)))
  367.     (output-after-point (< (point) (process-mark proc))))
  368.     (save-excursion
  369.       ;; Insert the text, moving the process-marker.
  370.       (goto-char (process-mark proc))
  371.       (insert-before-markers string)
  372.       (set-marker (process-mark proc) (point))
  373.       (gdb-maybe-delete-prompt)
  374.       ;; Check for a filename-and-line number.
  375.       (gdb-display-frame
  376.        ;; Don't display the specified file
  377.        ;; unless (1) point is at or after the position where output appears
  378.        ;; and (2) this buffer is on the screen.
  379.        (or output-after-point
  380.            (not (get-buffer-window (current-buffer))))
  381.        ;; Display a file only when a new filename-and-line-number appears.
  382.        t))
  383.     (if moving (goto-char (process-mark proc)))))
  384.  
  385. (defun gdb-sentinel (proc msg)
  386.   (cond ((null (buffer-name (process-buffer proc)))
  387.      ;; buffer killed
  388.      ;; Stop displaying an arrow in a source file.
  389.      ;(setq overlay-arrow-position nil) -- done by kill-buffer-hook
  390.      (set-process-buffer proc nil))
  391.     ((memq (process-status proc) '(signal exit))
  392.      ;; Stop displaying an arrow in a source file.
  393.          (gdb-delete-arrow-extent)
  394.      ;; Fix the mode line.
  395.      (setq modeline-process
  396.            (concat ": gdb " (symbol-name (process-status proc))))
  397.      (let* ((obuf (current-buffer)))
  398.        ;; save-excursion isn't the right thing if
  399.        ;;  process-buffer is current-buffer
  400.        (unwind-protect
  401.            (progn
  402.          ;; Write something in *compilation* and hack its mode line,
  403.          (set-buffer (process-buffer proc))
  404.          ;; Force mode line redisplay soon
  405.          (set-buffer-modified-p (buffer-modified-p))
  406.          (if (eobp)
  407.              (insert ?\n mode-name " " msg)
  408.            (save-excursion
  409.              (goto-char (point-max))
  410.              (insert ?\n mode-name " " msg)))
  411.          ;; If buffer and mode line will show that the process
  412.          ;; is dead, we can delete it now.  Otherwise it
  413.          ;; will stay around until M-x list-processes.
  414.          (delete-process proc))
  415.          ;; Restore old buffer, but don't restore old point
  416.          ;; if obuf is the gdb buffer.
  417.          (set-buffer obuf))))))
  418.  
  419.  
  420. (defun gdb-refresh (&optional arg)
  421.   "Fix up a possibly garbled display, and redraw the arrow."
  422.   (interactive "P")
  423.   (recenter arg)
  424.   (gdb-display-frame))
  425.  
  426. (defun gdb-display-frame (&optional nodisplay noauto)
  427.   "Find, obey and delete the last filename-and-line marker from GDB.
  428. The marker looks like \\032\\032FILENAME:LINE:CHARPOS\\n.
  429. Obeying it means displaying in another window the specified file and line."
  430.   (interactive)
  431.   (gdb-set-buffer)
  432.   (and gdb-last-frame (not nodisplay)
  433.        gdb-display-mode
  434.        (or (not gdb-last-frame-displayed-p) (not noauto))
  435.        (progn (gdb-display-line (car gdb-last-frame) (cdr gdb-last-frame))
  436.           (setq gdb-last-frame-displayed-p t))))
  437.  
  438. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  439. ;; and that its line LINE is visible.
  440. ;; Put the overlay-arrow on the line LINE in that buffer.
  441.  
  442. (defun gdb-display-line (true-file line &optional select-method)
  443.   ;; FILE to display
  444.   ;; LINE number to highlight and make visible
  445.   ;; SELECT-METHOD 'source, 'debugger, or 'none.  (default is 'debugger)
  446.   (and (null select-method) (setq select-method 'debugger))
  447.   (let* ((pre-display-buffer-function nil) ; screw it, put it all in one screen
  448.      (pop-up-windows t)
  449.      (source-buffer (find-file-noselect true-file))
  450.      (source-window (display-buffer source-buffer))
  451.      (debugger-window (get-buffer-window current-gdb-buffer))
  452.          (extent gdb-arrow-extent)
  453.      pos)
  454.     ;; XEmacs change: make sure we find a window displaying the source file
  455.     ;; even if we are already sitting in it when a breakpoint is hit.
  456.     ;; Otherwise the t argument to display-buffer will prevent it from being
  457.     ;; displayed.
  458.     (save-excursion 
  459.       (cond ((eq select-method 'debugger)
  460.          ;; might not already be displayed
  461.          (setq debugger-window (display-buffer current-gdb-buffer))
  462.          (select-window debugger-window))
  463.         ((eq select-method 'source)
  464.          (select-window source-window))))
  465.     (and extent
  466.      (not (eq (extent-buffer extent) source-buffer))
  467.      (setq extent (delete-extent extent)))
  468.     (or extent
  469.         (progn
  470.           (setq extent (make-extent 1 1 source-buffer))
  471.           (set-extent-face extent 'gdb-arrow-face)
  472.       (set-extent-begin-glyph extent gdb-arrow-glyph)
  473.           (set-extent-layout extent 'whitespace)
  474.           (set-extent-priority extent 2000)
  475.           (setq gdb-arrow-extent extent)))
  476.     (save-current-buffer
  477.       (set-buffer source-buffer)
  478.       (save-restriction
  479.     (widen)
  480.     (goto-line line)
  481.     (set-window-point source-window (point))
  482.     (setq pos (point))
  483.         (end-of-line)
  484.         (set-extent-endpoints extent pos (point))
  485.         (run-hook-with-args 'gdb-arrow-extent-hooks extent))
  486.       (cond ((or (< pos (point-min)) (> pos (point-max)))
  487.          (widen)
  488.          (goto-char pos))))
  489.     ;; Added by Stig.  It caused lots of problems for several users
  490.     ;; and since its purpose is unclear it is getting commented out.
  491.     ;;(and debugger-window
  492.     ;; (set-window-point debugger-window pos))
  493.     ))
  494.  
  495. (defun gdb-call (command)
  496.   "Invoke gdb COMMAND displaying source in other window."
  497.   (interactive)
  498.   (goto-char (point-max))
  499.   ;; Record info on the last prompt in the buffer and its position.
  500.   ;; This is used in  gdb-maybe-delete-prompt
  501.   ;; to prevent multiple prompts from accumulating.
  502.   (save-excursion
  503.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  504.     (let ((pt (point)))
  505.       (beginning-of-line)
  506.       (setq gdb-delete-prompt-marker
  507.         (if (= (point) pt)
  508.         nil
  509.           (list (point-marker) (- pt (point))
  510.             (buffer-substring (point) pt))))))
  511.   (gdb-set-buffer)
  512.   (process-send-string (get-buffer-process current-gdb-buffer)
  513.            (concat command "\n")))
  514.  
  515. (defun gdb-maybe-delete-prompt ()
  516.   (if gdb-delete-prompt-marker
  517.       ;; Get the string that we used as the prompt before.
  518.       (let ((prompt (nth 2 gdb-delete-prompt-marker))
  519.         (length (nth 1 gdb-delete-prompt-marker)))
  520.     ;; Position after it.
  521.     (goto-char (+ (car gdb-delete-prompt-marker) length))
  522.     ;; Delete any duplicates of it which follow right after.
  523.     (while (and (<= (+ (point) length) (point-max))
  524.             (string= prompt
  525.                  (buffer-substring (point) (+ (point) length))))
  526.       (delete-region (point) (+ (point) length)))
  527.     ;; If that didn't take us to where output is arriving,
  528.     ;; we have encountered something other than a prompt,
  529.     ;; so stop trying to delete any more prompts.
  530.     (if (not (= (point)
  531.             (process-mark (get-buffer-process current-gdb-buffer))))
  532.         (progn
  533.           (set-marker (car gdb-delete-prompt-marker) nil)
  534.           (setq gdb-delete-prompt-marker nil))))))
  535.  
  536. (defun gdb-break (temp)
  537.   "Set GDB breakpoint at this source line.  With ARG set temporary breakpoint."
  538.   (interactive "P")
  539.   (let* ((file-name (file-name-nondirectory buffer-file-name))
  540.      (line (save-restriction
  541.          (widen)
  542.          (beginning-of-line)
  543.          (1+ (count-lines 1 (point)))))
  544.      (cmd (concat (if temp "tbreak " "break ") file-name ":"
  545.               (int-to-string line))))
  546.     (set-buffer current-gdb-buffer)
  547.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  548.     (delete-region (point) (point-max))
  549.     (insert cmd)
  550.     (comint-send-input)
  551.     ;;(process-send-string (get-buffer-process current-gdb-buffer) cmd)
  552.     ))
  553.  
  554. (defun gdb-clear ()
  555.   "Set GDB breakpoint at this source line."
  556.   (interactive)
  557.   (let* ((file-name (file-name-nondirectory buffer-file-name))
  558.      (line (save-restriction
  559.          (widen)
  560.          (beginning-of-line)
  561.          (1+ (count-lines 1 (point)))))
  562.      (cmd (concat "clear " file-name ":"
  563.               (int-to-string line))))
  564.     (set-buffer current-gdb-buffer)
  565.     (goto-char (process-mark (get-buffer-process current-gdb-buffer)))
  566.     (delete-region (point) (point-max))
  567.     (insert cmd)
  568.     (comint-send-input)
  569.     ;;(process-send-string (get-buffer-process current-gdb-buffer) cmd)
  570.     ))
  571.  
  572. (defun gdb-read-address()
  573.   "Return a string containing the core-address found in the buffer at point."
  574.   (save-excursion
  575.    (let ((pt (point)) found begin)
  576.      (setq found (if (search-backward "0x" (- pt 7) t)(point)))
  577.      (cond (found (forward-char 2)
  578.           (buffer-substring found
  579.                     (progn (re-search-forward "[^0-9a-f]")
  580.                        (forward-char -1)
  581.                        (point))))
  582.        (t (setq begin (progn (re-search-backward "[^0-9]") (forward-char 1)
  583.                  (point)))
  584.           (forward-char 1)
  585.           (re-search-forward "[^0-9]")
  586.           (forward-char -1)
  587.           (buffer-substring begin (point)))))))
  588.  
  589.  
  590. (defvar gdb-commands nil
  591.   "List of strings or functions used by send-gdb-command.
  592. It is for customization by you.")
  593.  
  594. (defun send-gdb-command (arg)
  595.  
  596.   "This command reads the number where the cursor is positioned.  It
  597.  then inserts this ADDR at the end of the gdb buffer.  A numeric arg
  598.  selects the ARG'th member COMMAND of the list gdb-print-command.  If
  599.  COMMAND is a string, (format COMMAND ADDR) is inserted, otherwise
  600.  (funcall COMMAND ADDR) is inserted.  eg. \"p (rtx)%s->fld[0].rtint\"
  601.  is a possible string to be a member of gdb-commands.  "
  602.  
  603.  
  604.   (interactive "P")
  605.   (let (comm addr)
  606.     (if arg (setq comm (nth arg gdb-commands)))
  607.     (setq addr (gdb-read-address))
  608.     (if (eq (current-buffer) current-gdb-buffer)
  609.     (set-mark (point)))
  610.     (cond (comm
  611.        (setq comm
  612.          (if (stringp comm) (format comm addr) (funcall comm addr))))
  613.       (t (setq comm addr)))
  614.     (switch-to-buffer current-gdb-buffer)
  615.     (goto-char (point-max))
  616.     (insert comm)))
  617.  
  618. (defun gdb-control-c-subjob ()
  619.   "Send a Control-C to the subprocess."
  620.   (interactive)
  621.   (process-send-string (get-buffer-process (current-buffer))
  622.                "\C-c"))
  623.  
  624. (defun gdb-toolbar-break ()
  625.   (interactive)
  626.   (save-excursion
  627.     (message (car gdb-last-frame))
  628.     (set-buffer (find-file-noselect (car gdb-last-frame)))
  629.     (gdb-break nil)))
  630.  
  631. (defun gdb-toolbar-clear ()
  632.   (interactive)
  633.   (save-excursion
  634.     (message (car gdb-last-frame))
  635.     (set-buffer (find-file-noselect (car gdb-last-frame)))
  636.     (gdb-clear)))
  637.  
  638. (provide 'gdb)
  639.