ViewVC logotype

Diff of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.32 by heller, Wed Dec 3 22:34:50 2003 UTC revision 1.56 by lgorrie, Mon Jan 19 20:14:35 2004 UTC
# Line 4  Line 4 
5  (in-package :swank)  (in-package :swank)
7  ;; Turn on xref. [should we?]  
8  (setf c:*record-xref-info* t)  ;;;; TCP server.
10  (defun without-interrupts* (body)  (setq *swank-in-background* :fd-handler)
11    (sys:without-interrupts (funcall body)))  
12    (defimplementation create-socket (port)
13      (let ((fd (ext:create-inet-listener port :stream
14                                          :reuse-address t
15                                          :host (resolve-hostname "localhost"))))
16        #+MP
17        (when *multiprocessing-enabled*
18          (set-fd-non-blocking fd))
19        fd))
21    (defimplementation local-port (socket)
22      (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
24    (defimplementation close-socket (socket)
25      (ext:close-socket (socket-fd socket)))
27    (defimplementation accept-connection (socket)
28      #+MP (when *multiprocessing-enabled*
29             (mp:process-wait-until-fd-usable socket :input))
30      (make-socket-io-stream (ext:accept-tcp-connection socket)))
32    (defimplementation add-input-handler (socket fn)
33      (flet ((callback (fd)
34               (declare (ignore fd))
35               (funcall fn)))
36        (system:add-fd-handler (socket-fd socket) :input #'callback)))
38    (defimplementation remove-input-handlers (socket)
39      (sys:invalidate-descriptor (socket-fd socket))
40      (close socket))
42    (defimplementation make-fn-streams (input-fn output-fn)
43      (let* ((output (make-slime-output-stream output-fn))
44             (input  (make-slime-input-stream input-fn output)))
45        (values input output)))
47    ;;;;; Socket helpers.
49    (defun socket-fd (socket)
50      "Return the filedescriptor for the socket represented by SOCKET."
51      (etypecase socket
52        (fixnum socket)
53        (sys:fd-stream (sys:fd-stream-fd socket))))
55    (defun resolve-hostname (hostname)
56      "Return the IP address of HOSTNAME as an integer."
57      (let* ((hostent (ext:lookup-host-entry hostname))
58             (address (car (ext:host-entry-addr-list hostent))))
59        (ext:htonl address)))
61    (defun make-socket-io-stream (fd)
62      "Create a new input/output fd-stream for FD."
63      (sys:make-fd-stream fd :input t :output t :element-type 'base-char))
65  (defun set-fd-non-blocking (fd)  (defun set-fd-non-blocking (fd)
66    (flet ((fcntl (fd cmd arg)    (flet ((fcntl (fd cmd arg)
67             (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)             (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)
68               (or flags               (or flags
69                   (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))                   (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))
70      (let ((flags (fcntl fd unix:F-GETFL 0)))      (let ((flags (fcntl fd unix:F-GETFL 0)))
71        (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))        (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))
74  ;;;; TCP server.  ;;;; Unix signals
76  (defun resolve-hostname (name)  (defmethod call-without-interrupts (fn)
77    (let* ((hostent (ext:lookup-host-entry name))    (sys:without-interrupts (funcall fn)))
          (address (car (ext:host-entry-addr-list hostent))))  
     (ext:htonl address)))  
79  (defun create-swank-server (port &key (reuse-address t)  (defmethod getpid ()
80                              (address "localhost"))    (unix:unix-getpid))
   "Create a SWANK TCP server."  
   (let* ((ip (resolve-hostname address))  
          (fd (ext:create-inet-listener port :stream  
                                        :reuse-address reuse-address  
                                        :host ip)))  
     (system:add-fd-handler fd :input #'accept-connection)  
     (nth-value 1 (ext::get-socket-host-and-port fd))))  
 (defun accept-connection (socket)  
   "Accept one Swank TCP connection on SOCKET and then close it."  
   (setup-request-handler (ext:accept-tcp-connection socket))  
   (sys:invalidate-descriptor socket)  
   (unix:unix-close socket))  
 (defun open-stream-to-emacs ()  
   "Return an output-stream to Emacs' output buffer."  
   (let* ((ip (resolve-hostname "localhost"))  
          (listener (ext:create-inet-listener 0 :stream :host ip))  
          (port (nth-value 1 (ext::get-socket-host-and-port listener))))  
            (eval-in-emacs `(slime-open-stream-to-lisp ,port))  
            (let ((fd (ext:accept-tcp-connection listener)))  
              (sys:make-fd-stream fd :output t)))  
       (ext:close-socket listener))))  
 (defvar *use-dedicated-output-stream* t)  
 (defun setup-request-handler (socket)  
   "Setup request handling for SOCKET."  
   (let* ((stream (sys:make-fd-stream socket  
                                      :input t :output t  
                                      :element-type 'base-char))  
          (input (make-slime-input-stream))  
          (output (if *use-dedicated-output-stream*  
                      (let ((*emacs-io* stream)) (open-stream-to-emacs))  
          (io (make-two-way-stream input output)))  
     (system:add-fd-handler socket  
                            :input (lambda (fd)  
                                     (declare (ignore fd))  
                                     (serve-request stream output input io)))))  
 (defun serve-request (*emacs-io* *slime-output* *slime-input* *slime-io*)  
   "Read and process a request from a SWANK client.  
 The request is read from the socket as a sexp and then evaluated."  
   (catch 'slime-toplevel  
     (with-simple-restart (abort "Return to Slime toplevel.")  
       (handler-case (read-from-emacs)  
         (slime-read-error (e)  
           (when *swank-debug-p*  
             (format *debug-io* "~&;; Connection to Emacs lost.~%;; [~A]~%" e))  
           (sys:invalidate-descriptor (sys:fd-stream-fd *emacs-io*))  
           (close *emacs-io*)))))  
83  ;;;; Stream handling  ;;;; Stream handling
85  (defstruct (slime-output-stream  (defstruct (slime-output-stream
86               (:include lisp::lisp-stream               (:include lisp::lisp-stream
87                         (lisp::misc #'sos/misc)                         (lisp::misc #'sos/misc)
88                         (lisp::out #'sos/out)                         (lisp::out #'sos/out)
89                         (lisp::sout #'sos/sout))                         (lisp::sout #'sos/sout))
90               (:conc-name sos.)               (:conc-name sos.)
91               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
92                 (:constructor make-slime-output-stream (output-fn)))
93      (output-fn nil :type function)
94    (buffer (make-string 512) :type string)    (buffer (make-string 512) :type string)
95    (index 0 :type kernel:index)    (index 0 :type kernel:index)
96    (column 0 :type kernel:index))    (column 0 :type kernel:index))
# Line 117  The request is read from the socket as a Line 114  The request is read from the socket as a
114  (defun sos/sout (stream string start end)  (defun sos/sout (stream string start end)
115    (loop for i from start below end    (loop for i from start below end
116          do (sos/out stream (aref string i))))          do (sos/out stream (aref string i))))
118  (defun sos/misc (stream operation &optional arg1 arg2)  (defun sos/misc (stream operation &optional arg1 arg2)
119    (declare (ignore arg1 arg2))    (declare (ignore arg1 arg2))
120    (case operation    (case operation
121      ((:force-output :finish-output)      ((:force-output :finish-output)
122       (let ((end (sos.index stream)))       (let ((end (sos.index stream)))
123         (unless (zerop end)         (unless (zerop end)
124           (send-to-emacs `(:read-output ,(subseq (sos.buffer stream) 0 end)))           (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
125           (setf (sos.index stream) 0))))           (setf (sos.index stream) 0))))
126      (:charpos (sos.column stream))      (:charpos (sos.column stream))
127      (:line-length 75)      (:line-length 75)
128      (:file-position nil)      (:file-position nil)
# Line 135  The request is read from the socket as a Line 132  The request is read from the socket as a
132      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
134  (defstruct (slime-input-stream  (defstruct (slime-input-stream
135               (:include string-stream               (:include string-stream
136                         (lisp::in #'sis/in)                         (lisp::in #'sis/in)
137                         (lisp::misc #'sis/misc))                         (lisp::misc #'sis/misc))
138               (:conc-name sis.)               (:conc-name sis.)
139               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
140    (buffer "" :type string)               (:constructor make-slime-input-stream (input-fn sos)))
141    (index 0 :type kernel:index))    (input-fn nil :type function)
142      ;; We know our sibling output stream, so that we can force it before
143      ;; requesting input.
144      (sos      nil :type slime-output-stream)
145      (buffer   ""  :type string)
146      (index    0   :type kernel:index))
148  (defun sis/in (stream eof-errorp eof-value)  (defun sis/in (stream eof-errorp eof-value)
149    (declare (ignore eof-errorp eof-value))    (declare (ignore eof-errorp eof-value))
150    (let ((index (sis.index stream))    (let ((index (sis.index stream))
151          (buffer (sis.buffer stream)))          (buffer (sis.buffer stream)))
152      (when (= index (length buffer))      (when (= index (length buffer))
153        (setf buffer (slime-read-string))        (force-output (sis.sos stream))
154          (setf buffer (funcall (sis.input-fn stream)))
155        (setf (sis.buffer stream) buffer)        (setf (sis.buffer stream) buffer)
156        (setf index 0))        (setf index 0))
157      (prog1 (aref buffer index)      (prog1 (aref buffer index)
# Line 277  the error-context redundant." Line 280  the error-context redundant."
280           (make-location (list :file (namestring *compile-file-truename*))           (make-location (list :file (namestring *compile-file-truename*))
281                          (list :position 0)))                          (list :position 0)))
282          (*compile-filename*          (*compile-filename*
283             ;; XXX is this _ever_ used?  By what?  *compile-file-truename*
284             ;; should be set by the implementation inside any call to compile-file
285           (make-location (list :file *compile-filename*) (list :position 0)))           (make-location (list :file *compile-filename*) (list :position 0)))
286          (t          (t
287           (list :error "No error location available"))))           (list :error "No error location available"))))
289  (defmacro with-compilation-hooks (() &body body)  (defimplementation call-with-compilation-hooks (function)
290    "Execute BODY and record the set of compiler notes."    (let ((*previous-compiler-condition* nil)
291    `(let ((*previous-compiler-condition* nil)          (*previous-context* nil)
292           (*previous-context* nil)          (*print-readably* nil))
          (*print-readably* nil))  
293      (handler-bind ((c::compiler-error #'handle-notification-condition)      (handler-bind ((c::compiler-error #'handle-notification-condition)
294                     (c::style-warning  #'handle-notification-condition)                     (c::style-warning  #'handle-notification-condition)
295                     (c::warning        #'handle-notification-condition))                     (c::warning        #'handle-notification-condition))
296        ,@body)))        (funcall function))))
298  (defmethod compile-file-for-emacs (filename load-p)  (defimplementation compile-file-for-emacs (filename load-p)
299    (clear-xref-info filename)    (clear-xref-info filename)
300    (with-compilation-hooks ()    (with-compilation-hooks ()
301      (let ((*buffer-name* nil)      (let ((*buffer-name* nil)
302            (*compile-filename* filename))            (*compile-filename* filename))
303          (compile-file filename :load load-p))))        (multiple-value-bind (fasl-file warnings-p failure-p)
304              (compile-file filename)
305            (declare (ignore warnings-p))
306            (when (and load-p (not failure-p))
307              (load fasl-file))))))
309  (defmethod compile-string-for-emacs (string &key buffer position)  (defimplementation compile-string-for-emacs (string &key buffer position)
310    (with-compilation-hooks ()    (with-compilation-hooks ()
311      (let ((*package* *buffer-package*)      (let ((*package* *buffer-package*)
312            (*compile-filename* nil)            (*compile-filename* nil)
# Line 312  the error-context redundant." Line 320  the error-context redundant."
320                          :emacs-buffer-offset ,position                          :emacs-buffer-offset ,position
321                          :emacs-buffer-string ,string))))))                          :emacs-buffer-string ,string))))))
323    (defimplementation compile-system-for-emacs (system-name)
324      (with-compilation-hooks ()
325        (cond ((ext:featurep :asdf)
326               (let ((operate (find-symbol (string :operate) :asdf))
327                     (load-op (find-symbol (string :load-op) :asdf)))
328                 (funcall operate load-op system-name)))
329              (t (error "ASDF not loaded")))))
332  ;;;; XREF  ;;;; XREF
# Line 380  reference   ::= (FUNCTION-SPECIFIER . SO Line 396  reference   ::= (FUNCTION-SPECIFIER . SO
396                xrefs)))                xrefs)))
397      (group-xrefs xrefs)))      (group-xrefs xrefs)))
 (defun location-buffer= (location1 location2)  
   (equalp location1 location2))  
 (defun file-xrefs-for-emacs (unix-filename contexts)  
   "Return a summary of the references from a particular file.  
 The result is a list of the form (FILENAME ((REFERRER SOURCE-PATH) ...))"  
   (list unix-filename  
         (loop for context in (sort-contexts-by-source-path contexts)  
               collect (list (let ((*print-pretty* nil))  
                               (to-string (xref:xref-context-name context)))  
                             (xref:xref-context-source-path context)))))  
 (defun sort-contexts-by-source-path (contexts)  
   "Sort xref contexts by lexical position of source-paths.  
 It is assumed that all contexts belong to the same file."  
   (sort contexts #'source-path< :key #'xref:xref-context-source-path))  
 (defun source-path< (path1 path2)  
   "Return true if PATH1 is lexically before PATH2."  
   (and (every #'< path1 path2)  
        (< (length path1) (length path2))))  
399  (defun clear-xref-info (namestring)  (defun clear-xref-info (namestring)
400    "Clear XREF notes pertaining to FILENAME.    "Clear XREF notes pertaining to FILENAME.
401  This is a workaround for a CMUCL bug: XREF records are cumulative."  This is a workaround for a CMUCL bug: XREF records are cumulative."
# Line 551  the code omponent CODE." Line 544  the code omponent CODE."
544                                   (function-source-location fn)))                                   (function-source-location fn)))
545                           fns))))                           fns))))
548  ;;;; Definitions  ;;;; Definitions
550  (defvar *debug-definition-finding* t  (defvar *debug-definition-finding* nil
551    "When true don't handle errors while looking for definitions.    "When true don't handle errors while looking for definitions.
552  This is useful when debugging the definition-finding code.")  This is useful when debugging the definition-finding code.")
554  (defmacro safe-definition-finding (&body body)  (defmacro safe-definition-finding (&body body)
555    "Execute BODY ignoring errors.  Return a the source location    "Execute BODY ignoring errors.  Return the source location returned
556  returned by BODY or if an error occurs a description of the error.  by BODY or if an error occurs a description of the error.  The second
557  The second return value is the condition or nil."  return value is the condition or nil."
558    `(flet ((body () ,@body))    `(flet ((body () ,@body))
559      (if *debug-definition-finding*      (if *debug-definition-finding*
560          (body)          (body)
# Line 670  The second return value is the condition Line 664  The second return value is the condition
664    (destructuring-bind (first) (function-source-locations function)    (destructuring-bind (first) (function-source-locations function)
665      first))      first))
667  (defslimefun find-function-locations (symbol-name)  (defimplementation find-function-locations (symbol-name)
668    "Return a list of source-locations for SYMBOL-NAME's functions."    "Return a list of source-locations for SYMBOL-NAME's functions."
669    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)
670      (cond ((not foundp)      (cond ((not foundp)
# Line 688  The second return value is the condition Line 682  The second return value is the condition
683  ;;;; Documentation.  ;;;; Documentation.
685  (defmethod describe-symbol-for-emacs (symbol)  (defimplementation describe-symbol-for-emacs (symbol)
686    (let ((result '()))    (let ((result '()))
687      (flet ((doc (kind)      (flet ((doc (kind)
688               (or (documentation symbol kind) :not-documented))               (or (documentation symbol kind) :not-documented))
# Line 734  The second return value is the condition Line 728  The second return value is the condition
728                         (doc nil)))                         (doc nil)))
729        result)))        result)))
 (defslimefun describe-setf-function (symbol-name)  
    (or (ext:info setf inverse (from-string symbol-name))  
        (ext:info setf expander (from-string symbol-name)))))  
 (defslimefun describe-type (symbol-name)  
    (kernel:values-specifier-type (from-string symbol-name))))  
 (defslimefun describe-class (symbol-name)  
   (print-description-to-string (find-class (from-string symbol-name) nil)))  
 (defslimefun describe-alien-type (symbol-name)  
   (let ((name (from-string symbol-name)))  
     (ecase (ext:info :alien-type :kind name)  
         (let ((alien::*values-type-okay* t))  
           (funcall (ext:info :alien-type :translator name) (list name)))))  
        (print-description-to-string (ext:info :alien-type :definition name)))  
        (format nil "Unkown alien type: ~A" symbol-name)))))  
731  (defmacro %describe-alien (symbol-name namespace)  (defmacro %describe-alien (symbol-name namespace)
732    `(print-description-to-string    `(print-description-to-string
733      (ext:info :alien-type ,namespace (from-string ,symbol-name))))      (ext:info :alien-type ,namespace (from-string ,symbol-name))))
735  (defslimefun describe-alien-struct (symbol-name)  (defimplementation describe-definition (symbol-name type)
736    (%describe-alien symbol-name :struct))    (case type
737        (:variable
738  (defslimefun describe-alien-union (symbol-name)       (describe-symbol symbol-name))
739    (%describe-alien symbol-name :union))      ((:function :generic-function)
740         (describe-function symbol-name))
741        (:setf
742         (print-description-to-string
743          (or (ext:info setf inverse (from-string symbol-name))
744              (ext:info setf expander (from-string symbol-name)))))
745        (:type
746         (print-description-to-string
747          (kernel:values-specifier-type (from-string symbol-name))))
748        (:class
749         (print-description-to-string (find-class (from-string symbol-name) nil)))
750        (:alien-type
751         (let ((name (from-string symbol-name)))
752           (ecase (ext:info :alien-type :kind name)
753             (:primitive
754              (print-description-to-string
755               (let ((alien::*values-type-okay* t))
756                 (funcall (ext:info :alien-type :translator name) (list name)))))
757             ((:defined)
758              (print-description-to-string (ext:info :alien-type
759                                                     :definition name)))
760             (:unknown
761              (format nil "Unkown alien type: ~A" symbol-name)))))
762        (:alien-struct
763         (%describe-alien symbol-name :struct))
764        (:alien-union
765         (%describe-alien symbol-name :union))
766        (:alien-enum
767         (%describe-alien symbol-name :enum))))
769  (defslimefun describe-alien-enum (symbol-name)  (defimplementation arglist-string (fname)
   (%describe-alien symbol-name :enum))  
 (defmethod arglist-string (fname)  
770    "Return a string describing the argument list for FNAME.    "Return a string describing the argument list for FNAME.
771  The result has the format \"(...)\"."  The result has the format \"(...)\"."
772    (declare (type string fname))    (declare (type string fname))
773    (multiple-value-bind (function condition)    (multiple-value-bind (function package) (find-symbol-designator fname)
774        (ignore-errors (values (find-symbol-designator fname *buffer-package*)))      (unless package
775      (when condition        (return-from arglist-string (format nil "(-- Unkown symbol: ~A)" fname)))
       (return-from arglist-string (format nil "(-- ~A)" condition)))  
776      (let ((arglist      (let ((arglist
777             (if (not (or (fboundp function)             (if (not (or (fboundp function)
778                          (functionp function)))                          (functionp function)))
# Line 786  The result has the format \"(...)\"." Line 780  The result has the format \"(...)\"."
780                 (let* ((fun (or (macro-function function)                 (let* ((fun (or (macro-function function)
781                                 (symbol-function function)))                                 (symbol-function function)))
782                        (df (di::function-debug-function fun))                        (df (di::function-debug-function fun))
783                        (arglist (kernel:%function-arglist fun)))                        (arglist (kernel:%function-arglist
784                                    (kernel:%function-self fun))))
785                   (cond ((eval:interpreted-function-p fun)                   (cond ((eval:interpreted-function-p fun)
786                          (eval:interpreted-function-arglist fun))                          (eval:interpreted-function-arglist fun))
787                         ((pcl::generic-function-p fun)                         ((pcl::generic-function-p fun)
788                          (pcl::gf-pretty-arglist fun))                          (pcl:generic-function-lambda-list fun))
789                         (arglist arglist)                         (arglist arglist)
790                         ;; this should work both for                         ;; this should work both for
791                         ;; compiled-debug-function and for                         ;; compiled-debug-function and for
792                         ;; interpreted-debug-function                         ;; interpreted-debug-function
793                         (df (di::debug-function-lambda-list df))                         (df (di::debug-function-lambda-list df))
794                         (t "(<arglist-unavailable>)"))))))                         (t "(<arglist-unavailable>)"))))))
795        (if (stringp arglist)        (etypecase arglist
796            arglist          (string arglist)
797            (to-string arglist)))))          (cons (let ((*print-case* :downcase)) (princ-to-string arglist)))
798            (null "()")))))
801  ;;;; Miscellaneous.  ;;;; Miscellaneous.
803  (defmethod macroexpand-all (form)  (defimplementation macroexpand-all (form)
804    (walker:macroexpand-all form))    (walker:macroexpand-all form))
806  (defun tracedp (fname)  (in-package :c)
807    (gethash (debug::trace-fdefinition fname)  
808             debug::*traced-functions*))  (defun swank::expand-ir1-top-level (form)
809      "A scaled down version of the first pass of the compiler."
810  (defslimefun toggle-trace-fdefinition (fname-string)    (with-compilation-unit ()
811    (let ((fname (from-string fname-string)))      (let* ((*lexical-environment*
812      (cond ((tracedp fname)              (make-lexenv :default (make-null-environment)
813             (debug::untrace-1 fname)                           :cookie *default-cookie*
814             (format nil "~S is now untraced." fname))                           :interface-cookie *default-interface-cookie*))
815            (t             (*source-info* (make-lisp-source-info form))
816             (debug::trace-1 fname (debug::make-trace-info))             (*block-compile* nil)
817             (format nil "~S is now traced." fname)))))             (*block-compile-default* nil))
818          (with-ir1-namespace
819              (clear-stuff)
820            (find-source-paths form 0)
821            (ir1-top-level form '(0) t)))))
823    (in-package :swank)
825    (defslimefun print-ir1-converted-blocks (form)
826      (with-output-to-string (*standard-output*)
827        (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
829  (defslimefun set-default-directory (directory)  (defslimefun set-default-directory (directory)
830    (setf (ext:default-directory) (namestring directory))    (setf (ext:default-directory) (namestring directory))
# Line 827  The result has the format \"(...)\"." Line 833  The result has the format \"(...)\"."
833    (setf *default-pathname-defaults* (pathname (ext:default-directory)))    (setf *default-pathname-defaults* (pathname (ext:default-directory)))
834    (namestring (ext:default-directory)))    (namestring (ext:default-directory)))
836    ;;; source-path-{stream,file,string,etc}-position moved into
837  ;;;; Source-paths  ;;; swank-source-path-parser
 ;;; CMUCL uses a data structure called "source-path" to locate  
 ;;; subforms.  The compiler assigns a source-path to each form in a  
 ;;; compilation unit.  Compiler notes usually contain the source-path  
 ;;; of the error location.  
 ;;; Compiled code objects don't contain source paths, only the  
 ;;; "toplevel-form-number" and the (sub-) "form-number".  To get from  
 ;;; the form-number to the source-path we need the entire toplevel-form  
 ;;; (i.e. we have to read the source code).  CMUCL has already some  
 ;;; utilities to do this translation, but we use some extended  
 ;;; versions, because we need more exact position info.  Apparently  
 ;;; Hemlock is happy with the position of the toplevel-form; we also  
 ;;; need the position of subforms.  
 ;;; We use a special readtable to get the positions of the subforms.  
 ;;; The readtable stores the start and end position for each subform in  
 ;;; hashtable for later retrieval.  
 (defun make-source-recorder (fn source-map)  
   "Return a macro character function that does the same as FN, but  
 additionally stores the result together with the stream positions  
 before and after of calling FN in the hashtable SOURCE-MAP."  
   (lambda (stream char)  
     (let ((start (file-position stream))  
           (values (multiple-value-list (funcall fn stream char)))  
           (end (file-position stream)))  
       #+(or) (format t "~&[~D ~{~A~^, ~} ~D]~%" start values end)  
       (unless (null values)  
         (push (cons start end) (gethash (car values) source-map)))  
       (values-list values))))  
 (defun make-source-recording-readtable (readtable source-map)  
   "Return a source position recording copy of READTABLE.  
 The source locations are stored in SOURCE-MAP."  
   (let* ((tab (copy-readtable readtable))  
          (*readtable* tab))  
     (dotimes (code char-code-limit)  
       (let ((char (code-char code)))  
         (multiple-value-bind (fn term) (get-macro-character char tab)  
           (when fn  
             (set-macro-character char (make-source-recorder fn source-map)  
                                  term tab)))))  
 (defun make-source-map ()  
   (make-hash-table :test #'eq))  
 (defvar *source-map* (make-source-map)  
   "The hashtable table used for source position recording.")  
 (defvar *recording-readtable-cache* '()  
   "An alist of (READTABLE . RECORDING-READTABLE) pairs.")  
 (defun lookup-recording-readtable (readtable)  
   "Find a cached or create a new recording readtable for READTABLE."  
   (or (cdr (assoc readtable *recording-readtable-cache*))  
       (let ((table (make-source-recording-readtable readtable *source-map*)))  
         (push (cons readtable table) *recording-readtable-cache*)  
 (defun read-and-record-source-map (stream)  
   "Read the next object from STREAM.  
 Return the object together with a hashtable that maps  
 subexpressions of the object to stream positions."  
   (let ((*readtable* (lookup-recording-readtable *readtable*)))  
     (clrhash *source-map*)  
     (values (read stream) *source-map*)))  
 (defun source-path-stream-position (path stream)  
   "Search the source-path PATH in STREAM and return its position."  
   (destructuring-bind (tlf-number . path) path  
     (let ((*read-suppress* t))  
       (dotimes (i tlf-number) (read stream))  
       (multiple-value-bind (form source-map)  
           (read-and-record-source-map stream)  
         (source-path-source-position (cons 0 path) form source-map)))))  
 (defun source-path-string-position (path string)  
   (with-input-from-string (s string)  
     (source-path-stream-position path s)))  
 (defun source-path-file-position (path filename)  
   (with-open-file (file filename)  
     (source-path-stream-position path file)))  
 (defun source-path-source-position (path form source-map)  
   "Return the start position of PATH form FORM and SOURCE-MAP.  All  
 subforms along the path are considered and the start and end position  
 of deepest (i.e. smallest) possible form is returned."  
   ;; compute all subforms along path  
   (let ((forms (loop for n in path  
                      for f = form then (nth n f)  
                      collect f)))  
     ;; select the first subform present in source-map  
     (loop for form in (reverse forms)  
           for positions = (gethash form source-map)  
           until (and positions (null (cdr positions)))  
           finally (destructuring-bind ((start . end)) positions  
                     (return (values (1- start) end))))))  
839  (defun code-location-stream-position (code-location stream)  (defun code-location-stream-position (code-location stream)
840    "Return the byte offset of CODE-LOCATION in STREAM.  Extract the    "Return the byte offset of CODE-LOCATION in STREAM.  Extract the
# Line 995  to find the position of the correspondin Line 901  to find the position of the correspondin
901    (safe-definition-finding    (safe-definition-finding
902     (source-location-from-code-location code-location)))     (source-location-from-code-location code-location)))
 (defslimefun getpid ()  
905  ;;;; Debugging  ;;;; Debugging
907  (defvar *sldb-stack-top*)  (defvar *sldb-stack-top*)
908  (defvar *sldb-restarts*)  (defvar *sldb-restarts*)
910  (defmethod call-with-debugging-environment (debugger-loop-fn)  (defimplementation call-with-debugging-environment (debugger-loop-fn)
911    (unix:unix-sigsetmask 0)    (unix:unix-sigsetmask 0)
912    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
913           (*sldb-restarts* (compute-restarts *swank-debugger-condition*))           (*sldb-restarts* (compute-restarts *swank-debugger-condition*))
# Line 1028  format suitable for Emacs." Line 931  format suitable for Emacs."
931          collect (list (princ-to-string (restart-name restart))          collect (list (princ-to-string (restart-name restart))
932                        (princ-to-string restart))))                        (princ-to-string restart))))
 (defun format-condition-for-emacs ()  
   (format nil "~A~%   [Condition of type ~S]"  
           (debug::safe-condition-message *swank-debugger-condition*)  
           (type-of *swank-debugger-condition*)))  
934  (defun nth-frame (index)  (defun nth-frame (index)
935    (do ((frame *sldb-stack-top* (di:frame-down frame))    (do ((frame *sldb-stack-top* (di:frame-down frame))
936         (i index (1- i)))         (i index (1- i)))
# Line 1041  format suitable for Emacs." Line 939  format suitable for Emacs."
939  (defun nth-restart (index)  (defun nth-restart (index)
940    (nth index *sldb-restarts*))    (nth index *sldb-restarts*))
942  (defun format-frame-for-emacs (frame)  (defun format-frame-for-emacs (number frame)
943    (list (di:frame-number frame)    (print-with-frame-label
944          (with-output-to-string (*standard-output*)     number (lambda (*standard-output*)
945            (let ((*print-pretty* *sldb-pprint-frames*))              (debug::print-frame-call frame :verbosity 1 :number nil))))
             (debug::print-frame-call frame :verbosity 1 :number t)))))  
947  (defun compute-backtrace (start end)  (defun compute-backtrace (start end)
948    "Return a list of frames starting with frame number START and    "Return a list of frames starting with frame number START and
# Line 1055  stack." Line 952  stack."
952      (loop for f = (nth-frame start) then (di:frame-down f)      (loop for f = (nth-frame start) then (di:frame-down f)
953            for i from start below end            for i from start below end
954            while f            while f
955            collect f)))            collect (cons i f))))
957  (defmethod backtrace (start end)  (defimplementation backtrace (start end)
958    (mapcar #'format-frame-for-emacs (compute-backtrace start end)))    (loop for (n . frame) in (compute-backtrace start end)
959            collect (list n (format-frame-for-emacs n frame))))
961  (defmethod debugger-info-for-emacs (start end)  (defimplementation debugger-info-for-emacs (start end)
962    (list (format-condition-for-emacs)    (list (debugger-condition-for-emacs)
963          (format-restarts-for-emacs)          (format-restarts-for-emacs)
964          (backtrace start end)))          (backtrace start end)))
966  (defmethod frame-source-location-for-emacs (index)  (defimplementation frame-source-location-for-emacs (index)
967    (code-location-source-location (di:frame-code-location (nth-frame index))))    (code-location-source-location (di:frame-code-location (nth-frame index))))
969  (defmethod eval-in-frame (form index)  (defimplementation eval-in-frame (form index)
970    (di:eval-in-frame (nth-frame index) form))    (di:eval-in-frame (nth-frame index) form))
972  (defslimefun pprint-eval-string-in-frame (string index)  (defslimefun pprint-eval-string-in-frame (string index)
# Line 1080  stack." Line 978  stack."
978    (reset-inspector)    (reset-inspector)
979    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))
981  (defmethod frame-locals (index)  (defimplementation frame-locals (index)
982    (let* ((frame (nth-frame index))    (let* ((frame (nth-frame index))
983           (location (di:frame-code-location frame))           (location (di:frame-code-location frame))
984           (debug-function (di:frame-debug-function frame))           (debug-function (di:frame-debug-function frame))
985           (debug-variables (di::debug-function-debug-variables debug-function)))           (debug-variables (di::debug-function-debug-variables debug-function)))
986      (loop for v across debug-variables      (loop for v across debug-variables collect
987            collect (list            (list :name (to-string (di:debug-variable-symbol v))
988                     :symbol (di:debug-variable-symbol v)                  :id (di:debug-variable-id v)
989                     :id (di:debug-variable-id v)                  :value-string (ecase (di:debug-variable-validity v location)
990                     :value-string                                  (:valid
991                     (if (eq (di:debug-variable-validity v location)                                   (to-string (di:debug-variable-value v frame)))
992                             :valid)                                  ((:invalid :unknown)
993                         (to-string (di:debug-variable-value v frame))                                   "<not-available>"))))))
995  (defmethod frame-catch-tags (index)  (defimplementation frame-catch-tags (index)
996    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))
997          collect `(,tag . ,(code-location-source-location code-location))))          collect `(,tag . ,(code-location-source-location code-location))))
# Line 1105  stack." Line 1002  stack."
1002  (defslimefun sldb-abort ()  (defslimefun sldb-abort ()
1003    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
1005    (defun set-step-breakpoints (frame)
1006      (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1007                                         (di:frame-code-location frame)))
1008        (error "Cannot step, in elsewhere code~%"))
1009      (let* ((code-location (di:frame-code-location frame))
1010             (debug::*bad-code-location-types*
1011              (remove :call-site debug::*bad-code-location-types*))
1012             (next (debug::next-code-locations code-location)))
1013        (cond (next
1014               (let ((steppoints '()))
1015                 (flet ((hook (frame breakpoint)
1016                          (let ((debug:*stack-top-hint* frame))
1017                            (mapc #'di:delete-breakpoint steppoints)
1018                            (let ((cl (di::breakpoint-what breakpoint)))
1019                              (break "Breakpoint: ~S ~S"
1020                                     (di:code-location-kind cl)
1021                                     (di::compiled-code-location-pc cl))))))
1022                   (dolist (code-location next)
1023                     (let ((bp (di:make-breakpoint #'hook code-location
1024                                                   :kind :code-location)))
1025                       (di:activate-breakpoint bp)
1026                       (push bp steppoints))))))
1027             (t
1028              (flet ((hook (frame breakpoint values cookie)
1029                       (declare (ignore cookie))
1030                       (di:delete-breakpoint breakpoint)
1031                       (let ((debug:*stack-top-hint* frame))
1032                         (break "Function-end: ~A ~A" breakpoint values))))
1033                (let* ((debug-function (di:frame-debug-function frame))
1034                       (bp (di:make-breakpoint #'hook debug-function
1035                                               :kind :function-end)))
1036                  (di:activate-breakpoint bp)))))))
1038    (defslimefun sldb-step (frame)
1039      (cond ((find-restart 'continue *swank-debugger-condition*)
1040             (set-step-breakpoints (nth-frame frame))
1041             (continue *swank-debugger-condition*))
1042            (t
1043             (error "Cannot continue in from condition: ~A"
1044                    *swank-debugger-condition*))))
1046    (defslimefun sldb-disassemble (frame-number)
1047      "Return a string with the disassembly of frames code."
1048      ;; this could need some refactoring.
1049      (let* ((frame (nth-frame frame-number))
1050             (real-frame (di::frame-real-frame frame))
1051             (frame-pointer (di::frame-pointer real-frame))
1052             (debug-fun (di:frame-debug-function real-frame)))
1053        (with-output-to-string (*standard-output*)
1054          (format t "Frame: ~S~%~:[Real Frame: ~S~%~;~]Frame Pointer: ~S~%"
1055                  frame (eq frame real-frame) real-frame frame-pointer)
1056          (etypecase debug-fun
1057            (di::compiled-debug-function
1058             (let* ((code-loc (di:frame-code-location frame))
1059                    (component (di::compiled-debug-function-component debug-fun))
1060                    (pc (di::compiled-code-location-pc code-loc))
1061                    (ip (sys:sap-int
1062                         (sys:sap+ (kernel:code-instructions component) pc)))
1063                    (kind (if (di:code-location-unknown-p code-loc)
1064                              :unkown
1065                              (di:code-location-kind code-loc)))
1066                    (fun (di:debug-function-function debug-fun)))
1067               (format t "Instruction pointer: #x~X [pc: ~S kind: ~S]~%~%~%"
1068                       ip pc kind)
1069               (if fun
1070                   (disassemble fun)
1071                   (disassem:disassemble-code-component component))))
1072            (di::bogus-debug-function
1073             (format t "~%[Disassembling bogus frames not implemented]"))))))
1075  ;;;; Inspecting  ;;;; Inspecting
 (defvar *inspectee*)  
 (defvar *inspectee-parts*)  
 (defvar *inspector-stack* '())  
 (defvar *inspector-history* (make-array 10 :adjustable t :fill-pointer 0))  
 (defvar *inspect-length* 30)  
 (defun reset-inspector ()  
   (setq *inspectee* nil)  
   (setq *inspectee-parts* nil)  
   (setq *inspector-stack* nil)  
   (setf (fill-pointer *inspector-history*) 0))  
 (defslimefun init-inspector (string)  
   (inspect-object (eval (from-string string))))  
 (defun print-part-to-string (value)  
   (let ((*print-pretty* nil))  
     (let ((string (to-string value))  
           (pos (position value *inspector-history*)))  
       (if pos  
           (format nil "#~D=~A" pos string)  
 (defun inspect-object (object)  
   (push (setq *inspectee* object) *inspector-stack*)  
   (unless (find object *inspector-history*)  
     (vector-push-extend object *inspector-history*))  
   (multiple-value-bind (text parts) (inspected-parts object)  
     (setq *inspectee-parts* parts)  
       (list :text text  
             :type (to-string (type-of object))  
             :primitive-type (describe-primitive-type object)  
             :parts (loop for (label . value) in parts  
                          collect (cons label  
                                        (print-part-to-string value))))))  
1077  (defconstant +lowtag-symbols+  (defconstant +lowtag-symbols+
1078    '(vm:even-fixnum-type    '(vm:even-fixnum-type
1079      vm:function-pointer-type      vm:function-pointer-type
# Line 1168  stack." Line 1098  stack."
1098       (append (apropos-list "-TYPE" "VM" t)       (append (apropos-list "-TYPE" "VM" t)
1099               (apropos-list "-TYPE" "BIGNUM" t)))))               (apropos-list "-TYPE" "BIGNUM" t)))))
1101  (defun describe-primitive-type (object)  (defmethod describe-primitive-type (object)
1102    (with-output-to-string (*standard-output*)    (with-output-to-string (*standard-output*)
1103      (let* ((lowtag (kernel:get-lowtag object))      (let* ((lowtag (kernel:get-lowtag object))
1104             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
# Line 1184  stack." Line 1114  stack."
1114                 (format t ", type: ~A]" type-symbol)))                 (format t ", type: ~A]" type-symbol)))
1115              (t (format t "]"))))))              (t (format t "]"))))))
 (defun nth-part (index)  
   (cdr (nth index *inspectee-parts*)))  
 (defslimefun inspect-nth-part (index)  
   (inspect-object (nth-part index)))  
 (defslimefun inspector-pop ()  
   "Drop the inspector stack and inspect the second element.  Return  
 nil if there's no second element."  
   (cond ((cdr *inspector-stack*)  
          (pop *inspector-stack*)  
          (inspect-object (pop *inspector-stack*)))  
         (t nil)))  
 (defslimefun inspector-next ()  
   "Inspect the next element in the *inspector-history*."  
   (let ((position (position *inspectee* *inspector-history*)))  
     (cond ((= (1+ position) (length *inspector-history*))  
           (t (inspect-object (aref *inspector-history* (1+ position)))))))  
 (defslimefun quit-inspector ()  
 (defslimefun describe-inspectee ()  
   "Describe the currently inspected object."  
   (print-description-to-string *inspectee*))  
 (defgeneric inspected-parts (object)  
    "Return a short description and a list of (label . value) pairs."))  
1117  (defmethod inspected-parts (o)  (defmethod inspected-parts (o)
1118    (cond ((di::indirect-value-cell-p o)    (cond ((di::indirect-value-cell-p o)
1119           (inspected-parts-of-value-cell o))           (inspected-parts-of-value-cell o))
# Line 1235  nil if there's no second element." Line 1132  nil if there's no second element."
1132    (values (format nil "~A~% is a value cell." o)    (values (format nil "~A~% is a value cell." o)
1133            (list (cons "Value" (c:value-cell-ref o)))))            (list (cons "Value" (c:value-cell-ref o)))))
 ;; borrowed from sbcl  
 (defmethod inspected-parts ((object cons))  
   (if (consp (cdr object))  
       (inspected-parts-of-nontrivial-list object)  
       (inspected-parts-of-simple-cons object)))  
 (defun inspected-parts-of-simple-cons (object)  
   (values "The object is a CONS."  
           (list (cons (string 'car) (car object))  
                 (cons (string 'cdr) (cdr object)))))  
 (defun inspected-parts-of-nontrivial-list (object)  
   (let ((length 0)  
         (in-list object)  
         (reversed-elements nil))  
     (flet ((done (description-format)  
              (return-from inspected-parts-of-nontrivial-list  
                (values (format nil description-format length)  
                        (nreverse reversed-elements)))))  
        (cond ((null in-list)  
               (done "The object is a proper list of length ~S.~%"))  
              ((>= length *inspect-length*)  
               (push (cons  (string 'rest) in-list) reversed-elements)  
               (done "The object is a long list (more than ~S elements).~%"))  
              ((consp in-list)  
               (push (cons (format nil "~D" length) (pop in-list))  
               (incf length))  
               (push (cons (string 'rest) in-list) reversed-elements)  
               (done "The object is an improper list of length ~S.~%")))))))  
1135  (defmethod inspected-parts ((o function))  (defmethod inspected-parts ((o function))
1136    (let ((header (kernel:get-type o)))    (let ((header (kernel:get-type o)))
1137      (cond ((= header vm:function-header-type)      (cond ((= header vm:function-header-type)
# Line 1304  nil if there's no second element." Line 1168  nil if there's no second element."
1168            `(("Name" . ,(kernel:fdefn-name o))            `(("Name" . ,(kernel:fdefn-name o))
1169              ("Function" . ,(kernel:fdefn-function o)))))              ("Function" . ,(kernel:fdefn-function o)))))
1172    ;;;; Multiprocessing
1174    #+MP
1175    (progn
1176      (defvar *known-processes* '()         ; FIXME: leakage. -luke
1177        "List of processes that have been assigned IDs.
1178         The ID is the position in the list.")
1180      (defimplementation startup-multiprocessing ()
1181        (setq *swank-in-background* :spawn)
1182        ;; Threads magic: this never returns! But top-level becomes
1183        ;; available again.
1184        (mp::startup-idle-and-top-level-loops))
1186      (defmethod spawn (fn &key (name "Anonymous"))
1187        (mp:make-process fn :name name))
1189      (defmethod thread-id ()
1190        (mp:without-scheduling
1191         (or (find-thread-id)
1192             (prog1 (length *known-processes*)
1193               (setq *known-processes*
1194                     (append *known-processes* (list (mp:current-process))))))))
1196      (defun find-thread-id (&optional (process (mp:current-process)))
1197        (position process *known-processes*))
1199      (defun lookup-thread (thread-id)
1200        (or (nth thread-id *known-processes*)
1201            (error "Unknown Thread-ID: ~S" thread-id)))
1203      (defimplementation thread-name (thread-id)
1204        (mp:process-name (lookup-thread thread-id)))
1206      (defimplementation make-lock (&key name)
1207        (mp:make-lock name))
1209      (defimplementation call-with-lock-held (lock function)
1210        (mp:with-lock-held (lock)
1211          (funcall function)))
1212    )
1215    ;;;; Epilogue
1216  ;;; Local Variables:  ;;; Local Variables:
1217  ;;; eval: (font-lock-add-keywords 'lisp-mode '(("(\\(defslimefun\\)\\s +\\(\\(\\w\\|\\s_\\)+\\)"  (1 font-lock-keyword-face) (2 font-lock-function-name-face))))  ;;; eval: (font-lock-add-keywords 'lisp-mode '(("(\\(defslimefun\\)\\s +\\(\\(\\w\\|\\s_\\)+\\)"  (1 font-lock-keyword-face) (2 font-lock-function-name-face))))
1218  ;;; End:  ;;; End:

Removed from v.1.32  
changed lines
  Added in v.1.56

  ViewVC Help
Powered by ViewVC 1.1.5