1 ;;; gdbsrc.el -- Source-based (as opposed to comint-based) debugger
2 ;; interaction mode eventually, this will be unified with GUD
3 ;; (after gud works reliably w/ XEmacs...)
4 ;; Keywords: c, unix, tools, debugging
6 ;; Copyright (C) 1990 Debby Ayers <ayers@austin.ibm.com>, and
7 ;; Rich Schaefer <schaefer@asc.slb.com>
8 ;; Copyright (C) 1994, 1995 Tinker Systems and INS Engineering Corp.
10 ;; This file is part of XEmacs.
12 ;; XEmacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2 of the License, or
15 ;; (at your option) any later version.
17 ;; XEmacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with XEmacs; if not, write to the Free Software
24 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; Based upon code for version18 by Debra Ayers <ayers@austin.ibm.com>
29 ;;; Gdbsrc extends the emacs GDB interface to accept gdb commands issued
30 ;;; from the source code buffer. Gdbsrc behaves similar to gdb except
31 ;;; now most debugging may be done from the source code using the *gdb*
32 ;;; buffer to view output. Supports a point and click model under X to
33 ;;; evaluate source code expressions (no more typing long variable names).
35 ;;; Supports C source at the moment but C++ support will be added if there
36 ;;; is sufficient interest.
39 ;; GDBSRC::Gdb Source Mode Interface description.
40 ;; Gdbsrc extends the emacs GDB interface to accept gdb commands issued
41 ;; from the source code buffer. Gdbsrc behaves similar to gdb except now all
42 ;; debugging may be done from the currently focused source buffer using
43 ;; the *gdb* buffer to view output.
45 ;; When source files are displayed through gdbsrc, buffers are put in
46 ;; gdbsrc-mode minor mode. This mode puts the buffer in read-only state
47 ;; and sets up a special key and mouse map to invoke communication with
48 ;; the current gdb process. The minor mode may be toggled on/off as needed.
51 ;; C-expressions may be evaluated by gdbsrc by simply pointing at text in the
52 ;; current source buffer with the mouse or by centering the cursor over text
53 ;; and typing a single key command. ('p' for print, '*' for print *).
55 ;; As code is debugged and new buffers are displayed, the focus of gdbsrc
56 ;; follows to each new source buffer. Makes debugging fun. (sound like a
57 ;; commercial or what!)
64 ;; r gdb-return-from-src GDB return command
65 ;; n gdb-next-from-src GDB next command
66 ;; b gdb-back-from-src GDB back command
67 ;; w gdb-where-from-src GDB where command
68 ;; f gdb-finish-from-src GDB finish command
69 ;; u gdb-up-from-src GDB up command
70 ;; d gdb-down-from-src GDB down command
71 ;; c gdb-cont-from-src GDB continue command
72 ;; i gdb-stepi-from-src GDB step instruction command
73 ;; s gdb-step-from-src GDB step command
74 ;; ? gdb-whatis-c-sexp GDB whatis command for data at
76 ;; x gdbsrc-delete GDB Delete all breakpoints if no arg
77 ;; given or delete arg (C-u arg x)
78 ;; m gdbsrc-frame GDB Display current frame if no arg,
79 ;; given or display frame arg
80 ;; * gdb-*print-c-sexp GDB print * command for data at
82 ;; ! gdbsrc-goto-gdb Goto the GDB output buffer
83 ;; p gdb-print-c-sexp GDB print * command for data at
85 ;; g gdbsrc-goto-gdb Goto the GDB output buffer
86 ;; t gdbsrc-mode Toggles Gdbsrc mode (turns it off)
88 ;; C-c C-f gdb-finish-from-src GDB finish command
90 ;; C-x SPC gdb-break Set break for line with point
91 ;; ESC t gdbsrc-mode Toggle Gdbsrc mode
93 ;; Local Bindings for buffer when you exit Gdbsrc minor mode
95 ;; C-x SPC gdb-break Set break for line with point
96 ;; ESC t gdbsrc-mode Toggle Gdbsrc mode
99 ;;; (eval-when-compile
100 ;;; (or noninteractive
102 ;;; (message "ONLY compile gdbsrc except with -batch because of advice")
106 (require 'gdb "gdb") ; NOT gud! (yet...)
108 (defvar gdbsrc-active-p t
109 "*Set to nil if you do not want source files put in gdbsrc-mode")
111 (defvar gdbsrc-call-p nil
112 "True if gdb command issued from a source buffer")
114 (defvar gdbsrc-associated-buffer nil
115 "Buffer name of attached gdb process")
117 (defvar gdbsrc-mode nil
118 "Indicates whether buffer is in gdbsrc-mode or not")
119 (make-variable-buffer-local 'gdbsrc-mode)
121 (defvar gdbsrc-global-mode nil
122 "Indicates whether global gdbsrc bindings are in effect or not")
124 (defvar gdb-prompt-pattern "^[^)#$%>\n]*[)#$%>] *"
125 "A regexp for matching the end of the gdb prompt")
129 (defvar gdbsrc-global-map
130 (let ((map (make-sparse-keymap)))
131 (set-keymap-name map 'gdbsrc-global-map)
132 (define-key map "\C-x " 'gdb-break)
133 (define-key map "\M-\C-t" 'gdbsrc-mode)
134 (define-key map "\M-\C-g" 'gdbsrc-goto-gdb)
136 ;; middle button to select and print expressions...
137 (define-key map '(meta button2) 'gdbsrc-print-csexp)
138 (define-key map '(meta shift button2) 'gdbsrc-*print-csexp)
139 ;; left button to position breakpoints
140 (define-key map '(meta button1) 'gdbsrc-set-break)
141 (define-key map '(meta shift button1) 'gdbsrc-set-tbreak-continue)
143 "Global minor keymap that is active whenever gdbsrc is running.")
145 (add-minor-mode 'gdbsrc-global-mode " GdbGlobal" gdbsrc-global-map)
147 (defvar gdbsrc-mode-map
148 (let ((map (make-sparse-keymap)))
149 (suppress-keymap map)
150 (set-keymap-name map 'gdbsrc-mode-map)
151 ;; inherit keys from global gdbsrc map just in case that somehow gets turned off.
152 (set-keymap-parents map (list gdbsrc-global-map))
153 (define-key map "\C-x\C-q" 'gdbsrc-mode) ; toggle read-only
154 (define-key map "\C-c\C-c" 'gdbsrc-mode)
155 (define-key map "b" 'gdb-break)
156 (define-key map "g" 'gdbsrc-goto-gdb)
157 (define-key map "!" 'gdbsrc-goto-gdb)
158 (define-key map "p" 'gdb-print-c-sexp)
159 (define-key map "*" 'gdb-*print-c-sexp)
160 (define-key map "?" 'gdb-whatis-c-sexp)
161 (define-key map "R" 'gdbsrc-reset)
163 "Minor keymap for buffers in gdbsrc-mode")
165 (add-minor-mode 'gdbsrc-mode " GdbSrc" gdbsrc-mode-map)
167 (defvar gdbsrc-toolbar
168 '([debug::toolbar-stop-at-icon
171 "Stop at selected position"]
172 [debug::toolbar-stop-in-icon
175 "Stop in function whose name is selected"]
176 [debug::toolbar-clear-at-icon
179 "Clear at selected position"]
180 [debug::toolbar-evaluate-icon
183 "Evaluate selected expression; shows in separate XEmacs frame"]
184 [debug::toolbar-evaluate-star-icon
187 "Evaluate selected expression as a pointer; shows in separate XEmacs frame"]
188 [debug::toolbar-run-icon
191 "Run current program"]
192 [debug::toolbar-cont-icon
195 "Continue current program"]
196 [debug::toolbar-step-into-icon
199 "Step into (aka step)"]
200 [debug::toolbar-step-over-icon
203 "Step over (aka next)"]
204 [debug::toolbar-up-icon
207 "Stack Up (towards \"cooler\" - less recently visited - frames)"]
208 [debug::toolbar-down-icon
211 "Stack Down (towards \"warmer\" - more recently visited - frames)"]
212 [debug::toolbar-fix-icon
215 "Fix (not available with gdb)"]
216 [debug::toolbar-build-icon
219 "Build (aka make -NYI)"]
222 (defmacro def-gdb-from-src (gdb-command key &optional doc &rest forms)
223 "Create a function that will call GDB-COMMAND with KEY."
224 (let* ((fname (format "gdbsrc-%s" gdb-command))
226 (list 'format "%s %s" gdb-command '(prefix-numeric-value arg))
229 (while (string-match " " fname)
230 (aset fname (match-beginning 0) ?-))
231 (setq fun (intern fname))
234 (nconc (list 'defun fun '(arg)
237 (list 'gdb-call-from-src cstr))
239 (list 'define-key 'gdbsrc-mode-map key (list 'quote fun)))))
241 (def-gdb-from-src "step" "s" "Step one instruction in src"
242 (gdb-delete-arrow-extent))
243 (def-gdb-from-src "stepi" "i" "Step one source line (skip functions)"
244 (gdb-delete-arrow-extent))
245 (def-gdb-from-src "cont" "c" "Continue with display"
246 (gdb-delete-arrow-extent))
247 (def-gdb-from-src "down" "d" "Go down N stack frames (numeric arg) ")
248 (def-gdb-from-src "up" "u" "Go up N stack frames (numeric arg)")
249 (def-gdb-from-src "finish" "f" "Finish frame")
250 (def-gdb-from-src "where" "w" "Display (N frames of) backtrace")
251 (def-gdb-from-src "next" "n" "Step one line with display"
252 (gdb-delete-arrow-extent))
253 (def-gdb-from-src "run" "r" "Run program from start"
254 (gdb-delete-arrow-extent))
255 (def-gdb-from-src "return" "R" "Return from selected stack frame")
256 (def-gdb-from-src "disable" "x" "Disable all breakpoints")
257 (def-gdb-from-src "delete" "X" "Delete all breakpoints")
258 (def-gdb-from-src "quit" "Q" "Quit gdb."
259 (gdb-delete-arrow-extent))
260 (def-gdb-from-src "info locals" "l" "Show local variables")
261 (def-gdb-from-src "info break" "B" "Show breakpoints")
262 (def-gdb-from-src "" "\r" "Repeat last command")
263 (def-gdb-from-src "frame" "m" "Show frame if no arg, with arg go to frame")
268 (defun gdbsrc (path &optional core-or-pid)
269 "Activates a gdb session with gdbsrc-mode turned on. A numeric prefix
270 argument can be used to specify a running process to attach, and a non-numeric
271 prefix argument will cause you to be prompted for a core file to debug."
272 (interactive (let ((file (read-file-name "Program to debug: " nil nil t)))
273 (cond ((numberp current-prefix-arg)
274 (list file (int-to-string current-prefix-arg)))
276 (list file (read-file-name "Core file: " nil nil t)))
279 ;; FIXME - this is perhaps an uncool thing to do --Stig
280 (delete-other-windows)
281 (split-window-vertically)
284 (gdb path core-or-pid)
285 (local-set-key 'button2 'gdbsrc-select-or-yank)
286 (setq mode-motion-hook 'gdbsrc-mode-motion)
288 (make-local-hook 'kill-buffer-hook)
289 (add-hook 'kill-buffer-hook 'gdbsrc-reset nil t))
291 (defun gdbsrc-global-mode ()
292 ;; this can be used as a hook for gdb-mode....
293 (or current-gdb-buffer
294 (and (eq major-mode 'gdb-mode) ; doesn't work w/ energize yet
295 (setq current-gdb-buffer (current-buffer))
298 (make-local-hook 'kill-buffer-hook)
299 (add-hook 'kill-buffer-hook 'gdbsrc-reset nil t)))
300 (error "Cannot determine current-gdb-buffer"))
301 ;;; (set-process-filter
302 ;;; (get-buffer-process current-gdb-buffer) 'gdbsrc-mode-filter)
303 ;;; (set-process-sentinel
304 ;;; (get-buffer-process current-gdb-buffer) 'gdbsrc-mode-sentinel)
305 ;; gdbsrc-global-mode was set to t here but that tended to piss
307 (setq gdbsrc-global-mode nil
311 (message "Gbd source mode active"))
313 (add-hook 'gdb-mode-hook 'gdbsrc-global-mode)
315 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
316 ;;; Gdb Source minor mode.
319 (defvar gdbsrc-associated-buffer nil
320 "The gdb buffer to send commands to.")
321 (defvar gdbsrc-initial-readonly 'undefined
322 "read-only status of buffer when not in gdbsrc-mode")
323 (defvar gdbsrc-old-toolbar nil
324 "saved toolbar for buffer")
326 (defun gdbsrc-mode (arg &optional quiet)
327 "Minor mode for interacting with gdb from a c source file.
328 With arg, turn gdbsrc-mode on iff arg is positive. In gdbsrc-mode,
329 you may send an associated gdb buffer commands from the current buffer
330 containing c source code."
335 (> (prefix-numeric-value arg) 0)))
338 (cond ((not (local-variable-p 'gdbsrc-initial-readonly (current-buffer)))
339 (set (make-local-variable 'gdbsrc-initial-readonly)
341 (set (make-local-variable 'gdbsrc-associated-buffer)
343 (if (featurep 'toolbar)
344 (set (make-local-variable 'gdbsrc-old-toolbar)
345 (specifier-specs default-toolbar (current-buffer))))
348 (if (featurep 'toolbar)
349 (set-specifier default-toolbar (cons (current-buffer)
351 (setq buffer-read-only t)
352 (or quiet (message "Entering gdbsrc-mode...")))
354 (and (local-variable-p 'gdbsrc-initial-readonly (current-buffer))
356 (if (featurep 'toolbar)
357 (if gdbsrc-old-toolbar
358 (set-specifier default-toolbar
359 (cons (current-buffer)
361 (remove-specifier default-toolbar (current-buffer))))
362 (kill-local-variable 'gdbsrc-old-toolbar)
363 (setq buffer-read-only gdbsrc-initial-readonly)
364 (kill-local-variable 'gdbsrc-initial-readonly)
365 (kill-local-variable 'gdbsrc-associated-buffer)
367 (or quiet (message "Exiting gdbsrc-mode..."))))
371 ;; Sends commands to gdb process.
373 (defun gdb-call-from-src (command)
374 "Send associated gdb process COMMAND displaying source in this window."
375 (setq gdbsrc-call-p t)
376 (let ((src-win (selected-window))
377 (buf (or gdbsrc-associated-buffer current-gdb-buffer)))
378 (or (buffer-name buf)
379 (error "GDB buffer deleted"))
381 (goto-char (point-max))
383 ;; Go past gdb prompt
385 gdb-prompt-pattern (save-excursion (end-of-line) (point)) t)
386 ;; Delete any not-supposed-to-be-there text
387 (delete-region (point) (point-max))
390 (select-window src-win)
393 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
395 ;;; Define Commands for GDB SRC Mode Buffer
398 ;;; ;; #### - move elsewhere
399 (or (fboundp 'event-buffer)
400 (defun event-buffer (event)
401 "Return buffer associated with EVENT, or nil."
402 (let ((win (event-window event)))
403 (and win (window-buffer win)))))
405 (defun set-gdbsrc-mode-motion-extent (st en action)
406 ;; by Stig@hackvan.com
407 (let ((ex (make-extent st en)))
408 (set-extent-face ex 'highlight)
409 (set-extent-property ex 'gdbsrc t)
410 (set-extent-property ex 'action action)
411 (setq mode-motion-extent ex)))
413 (defun nuke-mode-motion-extent ()
414 ;; by Stig@hackvan.com
415 (cond (mode-motion-extent
416 (delete-extent mode-motion-extent)
417 (setq mode-motion-extent nil))))
419 (defun looking-at-any (regex-list)
420 ;; by Stig@hackvan.com
423 (and (looking-at (car regex-list))
425 (setq regex-list (cdr regex-list)))))
427 (defconst gdb-breakpoint-patterns
429 ;; when execution stops...
430 ;;Breakpoint 1, XlwMenuRedisplay (w=0x4d2e00, ev=0xefffe3f8, region=0x580e60)
431 ;; at /net/stig/src/xemacs/lwlib/xlwmenu.c:2518
432 "^[BW][ra][et][ac][kh]point [0-9]+, .*\\(\n\\s .*\\)*"
433 ;; output of the breakpoint command:
434 ;;Breakpoint 1 at 0x19f5c8: file /net/stig/src/xemacs/lwlib/xlwmenu.c, line 2715.
435 "^[BW][ra][et][ac][kh]point [0-9]+ at .*: file \\([^ ,\n]+\\), line \\([0-9]+\\)."
436 ;;Num Type Disp Enb Address What
437 ;;1 breakpoint keep y 0x0019ee60 in XlwMenuRedisplay
438 ;; at /net/stig/src/xemacs/lwlib/xlwmenu.c:2518
439 "^[0-9]+\\s +[bw][ra][et][ac][kh]point.* in .*\\(\n\\s +\\)?at [^ :\n]+:[0-9]+\\(\n\\s .*\\)*"
441 "list of patterns to match gdb's various ways of displaying a breakpoint")
443 (defun gdbsrc-make-breakpoint-action (string)
444 ;; by Stig@hackvan.com
445 (if (or (string-match "file \\([^ ,\n]+\\), line \\([0-9]+\\)" string)
446 (string-match "at \\([^ :\n]+\\):\\([0-9]+\\)" string))
447 (list 'gdbsrc-display
448 (match-string 1 string)
449 (string-to-int (match-string 2 string)))))
451 (defconst gdb-stack-frame-pattern
452 ;;#9 0x62f08 in emacs_Xt_next_event (emacs_event=0x4cf804)
453 ;; at /net/stig/src/xemacs/src/event-Xt.c:1778
454 "^#\\([0-9]+\\)\\s +\\(0x[0-9a-f]+ in .*\\|.*\\sw+.* (.*) at .*\\)\\(\n\\s .*\\)*"
455 "matches the first line of a gdb stack frame and all continuation lines.
456 subex 1 is frame number.")
458 (defun gdbsrc-mode-motion (ee)
459 ;; by Stig@hackvan.com
461 (set-buffer (event-buffer ee))
463 (if (not (event-point ee))
464 (nuke-mode-motion-extent)
465 (goto-char (event-point ee))
467 (while (and (not (bobp)) (eq ? (char-syntax (following-char))))
469 (if (extent-at (point) (current-buffer) 'gdbsrc)
471 (nuke-mode-motion-extent)
472 (cond ((looking-at-any gdb-breakpoint-patterns)
473 (set-gdbsrc-mode-motion-extent
476 (gdbsrc-make-breakpoint-action (match-string 0))))
477 ((looking-at gdb-stack-frame-pattern)
478 (set-gdbsrc-mode-motion-extent
482 (string-to-int (match-string 1)))))
486 (defun gdbsrc-display (file line)
487 ;; by Stig@hackvan.com
488 (select-window (display-buffer (find-file-noselect file)))
491 (defun click-inside-selection-p (click)
492 (or (click-inside-extent-p click primary-selection-extent)
493 (click-inside-extent-p click zmacs-region-extent)
496 (defun click-inside-extent-p (click extent)
497 "Returns non-nil if the button event is within the bounds of the primary
498 selection-extent, nil otherwise."
500 (let ((ewin (event-window click))
501 (epnt (event-point click)))
505 (eq (window-buffer ewin)
506 (extent-object extent))
507 (extent-start-position extent)
508 (> epnt (extent-start-position extent))
509 (> (extent-end-position extent) epnt))))
511 (defun point-inside-extent-p (extent)
512 "Returns non-nil if the point is within or just after the bounds of the
513 primary selection-extent, nil otherwise."
515 (and extent ; FIXME - I'm such a sinner...
517 (extent-object extent))
518 (> (point) (extent-start-position extent))
519 (>= (extent-end-position extent) (point))))
521 (defun gdbsrc-select-or-yank (ee)
522 ;; by Stig@hackvan.com
524 (let ((action (save-excursion
525 (set-buffer (event-buffer ee))
526 (and mode-motion-extent
527 (click-inside-extent-p ee mode-motion-extent)
528 (extent-property mode-motion-extent 'action)))
534 (defvar gdb-print-format ""
535 "Set this variable to a valid format string to print c-sexps in a
536 different way (hex,octal, etc).")
538 (defun gdb-print-c-sexp ()
539 "Find the nearest c-mode sexp. Send it to gdb with print command."
541 (let* ((tag (find-c-sexp))
542 (command (concat "print " gdb-print-format tag)))
543 (gdb-call-from-src command)))
545 (defun gdb-*print-c-sexp ()
546 "Find the nearest c-mode sexp. Send it to gdb with the print * command."
548 (let* ((tag (find-c-sexp))
549 (command (concat "print " gdb-print-format "*" tag)))
550 (gdb-call-from-src command)))
552 (defun gdb-whatis-c-sexp ()
553 "Find the nearest c-mode sexp. Send it to gdb with the whatis command. "
555 (let* ((tag (gdbsrc-selection-or-sexp))
556 (command (concat "whatis " tag)))
557 (gdb-call-from-src command)))
559 (defun gdbsrc-goto-gdb ()
560 "Hop back and forth between the gdb interaction buffer and the gdb source
562 ;; by Stig@hackvan.com
564 (let ((gbuf (or gdbsrc-associated-buffer current-gdb-buffer)))
565 (cond ((eq (current-buffer) gbuf)
566 (and gdb-arrow-extent
567 (extent-object gdb-arrow-extent)
568 (progn (pop-to-buffer (extent-object gdb-arrow-extent))
569 (goto-char (extent-start-position gdb-arrow-extent)))))
570 ((buffer-name gbuf) (pop-to-buffer gbuf))
571 ((y-or-n-p "No debugger. Start a new one? ")
572 (call-interactively 'gdbsrc))
573 (t (error "No gdb buffer."))
576 (defvar gdbsrc-last-src-buffer nil)
578 (defun gdbsrc-goto-src ()
580 (let* ((valid (and gdbsrc-last-src-buffer
581 (memq gdbsrc-last-src-buffer (buffer-list))))
583 (get-buffer-window gdbsrc-last-src-buffer))))
584 (cond (win (select-window win))
585 (valid (pop-to-buffer gdbsrc-last-src-buffer)))))
587 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
589 ;;; The following functions are used to extract the closest surrounding
590 ;;; c expression from point
593 "Version of backward-sexp that catches errors"
599 "Version of forward-sexp that catches errors"
604 (defun sexp-compound-sep (span-start span-end)
605 "Returns '.' for '->' & '.', returns ' ' for white space,
606 returns '?' for other punctuation"
609 (while (< span-start span-end)
610 (setq syntax (char-syntax (char-after span-start)))
613 ((= syntax ?.) (setq syntax (char-after span-start))
615 ((= syntax ?.) (setq result ?.))
616 ((and (= syntax ?-) (= (char-after (+ span-start 1)) ?>))
618 (setq span-start (+ span-start 1)))
619 (t (setq span-start span-end)
621 (setq span-start (+ span-start 1)))
626 (defun sexp-compound (first second)
627 "Returns non-nil if the concatenation of two S-EXPs result in a Single C
628 token. The two S-EXPs are represented as a cons cells, where the car
629 specifies the point in the current buffer that marks the begging of the
630 S-EXP and the cdr specifies the character after the end of the S-EXP
631 Link S-Exps of the form:
634 Sexp (Sexp) Maybe exclude if first Sexp is: if, while, do, for, switch
638 (let ((span-start (cdr first))
639 (span-end (car second))
641 (setq syntax (sexp-compound-sep span-start span-end))
643 ((= (car first) (car second)) nil)
644 ((= (cdr first) (cdr second)) nil)
647 (setq span-start (char-after (- span-start 1)))
648 (setq span-end (char-after span-end))
650 ((= span-start ?) ) t )
651 ((= span-start ?] ) t )
652 ((= span-end ?( ) t )
653 ((= span-end ?[ ) t )
661 "Returns the S-EXP that Point is a member, Point is set to begging of S-EXP.
662 The S-EXPs is represented as a cons cell, where the car specifies the point in
663 the current buffer that marks the begging of the S-EXP and the cdr specifies
664 the character after the end of the S-EXP"
665 (let ((p (point)) (begin) (end))
684 "Returns the previous S-EXP, Point is set to begging of that S-EXP.
685 The S-EXPs is represented as a cons cell, where the car specifies the point in
686 the current buffer that marks the begging of the S-EXP and the cdr specifies
687 the character after the end of the S-EXP"
698 "Returns the following S-EXP, Point is set to begging of that S-EXP.
699 The S-EXPs is represented as a cons cell, where the car specifies the point in
700 the current buffer that marks the begging of the S-EXP and the cdr specifies
701 the character after the end of the S-EXP"
712 (defun find-c-sexp ()
713 "Returns the Complex S-EXP that surrounds Point"
716 (let ((p) (sexp) (test-sexp))
718 (setq sexp (sexp-cur))
719 (setq test-sexp (sexp-prev))
720 (while (sexp-compound test-sexp sexp)
721 (setq sexp (cons (car test-sexp) (cdr sexp)))
722 (goto-char (car sexp))
723 (setq test-sexp (sexp-prev))
726 (setq test-sexp (sexp-next))
727 (while (sexp-compound sexp test-sexp)
728 (setq sexp (cons (car sexp) (cdr test-sexp)))
729 (setq test-sexp (sexp-next))
731 (buffer-substring (car sexp) (cdr sexp))
736 (defun gdbsrc-selection-or-sexp (&optional ee)
737 ;; FIXME - fix this docstring
738 "If the EVENT is within the primary selection, then return the selected
739 text, otherwise parse the expression at the point of the mouse click and
740 return that. If EVENT is nil, then return the C sexp at point."
742 (cond ((or (and ee (click-inside-selection-p ee))
743 (and (not ee) (point-inside-selection-p)))
744 (replace-in-string (extent-string primary-selection-extent) "\n\\s *" " "))
746 (gdbsrc-get-csexp-at-click ee))
751 (defun gdbsrc-get-csexp-at-click (ee)
752 "Returns the containing s-expression located at the mouse cursor to point."
754 ;; by Stig@hackvan.com
755 (let ((ewin (event-window ee))
756 (epnt (event-point ee)))
758 (error "Must click within a window"))
760 (set-buffer (window-buffer ewin))
765 (defun gdbsrc-print-csexp (&optional ee)
767 (or ee (setq ee current-mouse-event))
769 (concat "print " gdb-print-format (gdbsrc-selection-or-sexp ee))))
771 (defun gdbsrc-*print-csexp (&optional ee)
773 (or ee (setq ee current-mouse-event))
775 (concat "print *" gdb-print-format (gdbsrc-selection-or-sexp ee))))
777 ;; (defun gdbsrc-print-region (arg)
778 ;; (let (( command (concat "print " gdb-print-format (x-get-cut-buffer))))
779 ;; (gdb-call-from-src command)))
781 ;; (defun gdbsrc-*print-region (arg)
782 ;; (let (( command (concat "print *" gdb-print-format (x-get-cut-buffer))))
783 ;; (gdb-call-from-src command)))
785 (defun gdbsrc-file:lno ()
786 "returns \"file:lno\" specification for location of point. "
787 ;; by Stig@hackvan.com
789 (file-name-nondirectory buffer-file-name)
792 (1+ (count-lines (point-min)
793 (save-excursion (beginning-of-line) (point)))))
796 (defun gdbsrc-set-break (ee)
797 "Sets a breakpoint. Click on the selection and it will set a breakpoint
798 using the selected text. Click anywhere in a source file, and it will set
799 a breakpoint at that line number of that file."
800 ;; by Stig@hackvan.com
801 ;; there is already gdb-break, so this only needs to work with mouse clicks.
805 (if (click-inside-selection-p ee)
806 (extent-string primary-selection-extent)
808 (or buffer-file-name (error "No file in window"))
812 (defun gdbsrc-set-tbreak-continue (&optional ee)
813 "Set a temporary breakpoint at the position of the mouse click and then
814 continues. This can be bound to either a key or a mouse button."
815 ;; by Stig@hackvan.com
817 (or ee (setq ee current-mouse-event))
818 (and ee (mouse-set-point ee))
819 (gdb-call-from-src (concat "tbreak " (gdbsrc-file:lno)))
820 (gdb-call-from-src "c"))
822 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
823 ;; Functions extended from gdb.el for gdbsrc.
825 ;; gdbsrc-set-buffer - added a check to set buffer to gdbsrc-associated-buffer
826 ;; to handle multiple gdb sessions being driven from src
831 (defadvice gdb-set-buffer (after gdbsrc activate) ; ()
832 "Advised to work from a source buffer instead of just the gdb buffer."
833 ;; by Stig@hackvan.com
834 ;; the operations below have tests which are disjoint from the tests in
835 ;; the original `gdb-set-buffer'. Current-gdb-buffer cannot be set twice.
837 gdbsrc-associated-buffer
838 (setq current-gdb-buffer gdbsrc-associated-buffer)))
840 (defadvice gdb-display-line (around gdbsrc activate)
841 ;; (true-file line &optional select-method)
842 "Advised to select the source buffer instead of the gdb-buffer"
843 ;; by Stig@hackvan.com
844 (ad-set-arg 2 'source) ; tell it not to select the gdb window
847 (let* ((buf (extent-object gdb-arrow-extent))
848 (win (gdb-display-window buf line)))
849 (setq gdbsrc-last-src-buffer buf)
851 (set-window-point win (extent-start-position gdb-arrow-extent))
855 (not (eq (current-buffer) current-gdb-buffer))
858 (defadvice gdb-filter (after gdbsrc activate) ; (proc string)
859 ;; by Stig@hackvan.com
860 ;; if we got a gdb prompt and it wasn't a gdbsrc command, then it's gdb
861 ;; hitting a breakpoint or having a core dump, so bounce back to the gdb
863 (let* ((selbuf (window-buffer (selected-window)))
865 ;; if we're at a gdb prompt, then display the buffer
866 (and (save-match-data (string-match gdb-prompt-pattern (ad-get-arg 1)))
869 (setq gdbsrc-call-p nil))
870 (setq win (display-buffer current-gdb-buffer))
871 ;; if we're not in either the source buffer or the gdb buffer,
872 ;; then select the window too...
873 (not (eq selbuf current-gdb-buffer))
874 (not (eq selbuf gdbsrc-last-src-buffer))
877 (select-window win)))
880 (defun gdbsrc-reset ()
881 ;; tidy house and turn off gdbsrc-mode in all buffers
882 ;; by Stig@hackvan.com
883 (gdb-delete-arrow-extent)
884 (setq gdbsrc-global-mode nil)
885 (mapcar #'(lambda (buffer)
887 (cond ((eq gdbsrc-associated-buffer current-gdb-buffer)
891 (defadvice gdb-sentinel (after gdbsrc freeze) ; (proc msg)
892 ;; by Stig@hackvan.com
894 (message "Gdbsrc finished"))