/[slime]/slime/swank-cmucl.lisp
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.55 by heller, Sun Jan 18 07:39:56 2004 UTC
# Line 4  Line 4 
4    
5  (in-package :swank)  (in-package :swank)
6    
7  ;; Turn on xref. [should we?]  
8  (setf c:*record-xref-info* t)  ;;;; TCP server.
9    
10  (defun without-interrupts* (body)  (setq *swank-in-background* :fd-handler)
11    (sys:without-interrupts (funcall body)))  
12    (defmethod 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))
20    
21    (defmethod local-port (socket)
22      (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
23    
24    (defmethod close-socket (socket)
25      (ext:close-socket (socket-fd socket)))
26    
27    (defmethod 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)))
31    
32    (defmethod 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)))
37    
38    (defmethod remove-input-handlers (socket)
39      (sys:invalidate-descriptor (socket-fd socket))
40      (close socket))
41    
42    (defmethod 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)))
46    
47    ;;;;; Socket helpers.
48    
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))))
54    
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)))
60    
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))
64    
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)))))
72    
73    
74  ;;;; TCP server.  ;;;; Unix signals
75    
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)))  
78    
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))))  
     (unwind-protect  
          (progn  
            (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))  
                      (make-slime-output-stream)))  
          (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*)))))  
   (sys:scrub-control-stack))  
81    
82    
83  ;;;; Stream handling  ;;;; Stream handling
84    
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))))
117    
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))))
133    
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))
147    
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"))))
288    
289  (defmacro with-compilation-hooks (() &body body)  (defmethod 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))))
297    
298  (defmethod compile-file-for-emacs (filename load-p)  (defmethod 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))))))
308    
309  (defmethod compile-string-for-emacs (string &key buffer position)  (defmethod compile-string-for-emacs (string &key buffer position)
310    (with-compilation-hooks ()    (with-compilation-hooks ()
# 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))))))
322    
323    (defmethod 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")))))
330    
331    
332  ;;;; XREF  ;;;; XREF
333    
# Line 380  reference   ::= (FUNCTION-SPECIFIER . SO Line 396  reference   ::= (FUNCTION-SPECIFIER . SO
396                xrefs)))                xrefs)))
397      (group-xrefs xrefs)))      (group-xrefs xrefs)))
398    
   
 (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))))
546    
547    
548  ;;;; Definitions  ;;;; Definitions
549    
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.")
553    
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))
666    
667  (defslimefun find-function-locations (symbol-name)  (defmethod 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 775  The second return value is the condition Line 769  The second return value is the condition
769    "Return a string describing the argument list for FNAME.    "Return a string describing the argument list for FNAME.
770  The result has the format \"(...)\"."  The result has the format \"(...)\"."
771    (declare (type string fname))    (declare (type string fname))
772    (multiple-value-bind (function condition)    (multiple-value-bind (function package) (find-symbol-designator fname)
773        (ignore-errors (values (find-symbol-designator fname *buffer-package*)))      (unless package
774      (when condition        (return-from arglist-string (format nil "(-- Unkown symbol: ~A)" fname)))
       (return-from arglist-string (format nil "(-- ~A)" condition)))  
775      (let ((arglist      (let ((arglist
776             (if (not (or (fboundp function)             (if (not (or (fboundp function)
777                          (functionp function)))                          (functionp function)))
# Line 786  The result has the format \"(...)\"." Line 779  The result has the format \"(...)\"."
779                 (let* ((fun (or (macro-function function)                 (let* ((fun (or (macro-function function)
780                                 (symbol-function function)))                                 (symbol-function function)))
781                        (df (di::function-debug-function fun))                        (df (di::function-debug-function fun))
782                        (arglist (kernel:%function-arglist fun)))                        (arglist (kernel:%function-arglist
783                                    (kernel:%function-self fun))))
784                   (cond ((eval:interpreted-function-p fun)                   (cond ((eval:interpreted-function-p fun)
785                          (eval:interpreted-function-arglist fun))                          (eval:interpreted-function-arglist fun))
786                         ((pcl::generic-function-p fun)                         ((pcl::generic-function-p fun)
787                          (pcl::gf-pretty-arglist fun))                          (pcl:generic-function-lambda-list fun))
788                         (arglist arglist)                         (arglist arglist)
789                         ;; this should work both for                         ;; this should work both for
790                         ;; compiled-debug-function and for                         ;; compiled-debug-function and for
791                         ;; interpreted-debug-function                         ;; interpreted-debug-function
792                         (df (di::debug-function-lambda-list df))                         (df (di::debug-function-lambda-list df))
793                         (t "(<arglist-unavailable>)"))))))                         (t "(<arglist-unavailable>)"))))))
794        (if (stringp arglist)        (etypecase arglist
795            arglist          (string arglist)
796            (to-string arglist)))))          (cons (let ((*print-case* :downcase)) (princ-to-string arglist)))
797            (null "()")))))
798    
799    
800  ;;;; Miscellaneous.  ;;;; Miscellaneous.
# Line 807  The result has the format \"(...)\"." Line 802  The result has the format \"(...)\"."
802  (defmethod macroexpand-all (form)  (defmethod macroexpand-all (form)
803    (walker:macroexpand-all form))    (walker:macroexpand-all form))
804    
805  (defun tracedp (fname)  (in-package :c)
806    (gethash (debug::trace-fdefinition fname)  
807             debug::*traced-functions*))  (defun swank::expand-ir1-top-level (form)
808      "A scaled down version of the first pass of the compiler."
809  (defslimefun toggle-trace-fdefinition (fname-string)    (with-compilation-unit ()
810    (let ((fname (from-string fname-string)))      (let* ((*lexical-environment*
811      (cond ((tracedp fname)              (make-lexenv :default (make-null-environment)
812             (debug::untrace-1 fname)                           :cookie *default-cookie*
813             (format nil "~S is now untraced." fname))                           :interface-cookie *default-interface-cookie*))
814            (t             (*source-info* (make-lisp-source-info form))
815             (debug::trace-1 fname (debug::make-trace-info))             (*block-compile* nil)
816             (format nil "~S is now traced." fname)))))             (*block-compile-default* nil))
817          (with-ir1-namespace
818              (clear-stuff)
819            (find-source-paths form 0)
820            (ir1-top-level form '(0) t)))))
821    
822    (in-package :swank)
823    
824    (defslimefun print-ir1-converted-blocks (form)
825      (with-output-to-string (*standard-output*)
826        (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
827    
828  (defslimefun set-default-directory (directory)  (defslimefun set-default-directory (directory)
829    (setf (ext:default-directory) (namestring directory))    (setf (ext:default-directory) (namestring directory))
# Line 827  The result has the format \"(...)\"." Line 832  The result has the format \"(...)\"."
832    (setf *default-pathname-defaults* (pathname (ext:default-directory)))    (setf *default-pathname-defaults* (pathname (ext:default-directory)))
833    (namestring (ext:default-directory)))    (namestring (ext:default-directory)))
834    
835    ;;; source-path-{stream,file,string,etc}-position moved into
836  ;;;; 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)))))  
     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*)  
         table)))  
   
 (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))))))  
837    
838  (defun code-location-stream-position (code-location stream)  (defun code-location-stream-position (code-location stream)
839    "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 900  to find the position of the correspondin
900    (safe-definition-finding    (safe-definition-finding
901     (source-location-from-code-location code-location)))     (source-location-from-code-location code-location)))
902    
 (defslimefun getpid ()  
   (unix:unix-getpid))  
   
903    
904  ;;;; Debugging  ;;;; Debugging
905    
# Line 1028  format suitable for Emacs." Line 930  format suitable for Emacs."
930          collect (list (princ-to-string (restart-name restart))          collect (list (princ-to-string (restart-name restart))
931                        (princ-to-string restart))))                        (princ-to-string restart))))
932    
 (defun format-condition-for-emacs ()  
   (format nil "~A~%   [Condition of type ~S]"  
           (debug::safe-condition-message *swank-debugger-condition*)  
           (type-of *swank-debugger-condition*)))  
   
933  (defun nth-frame (index)  (defun nth-frame (index)
934    (do ((frame *sldb-stack-top* (di:frame-down frame))    (do ((frame *sldb-stack-top* (di:frame-down frame))
935         (i index (1- i)))         (i index (1- i)))
# Line 1041  format suitable for Emacs." Line 938  format suitable for Emacs."
938  (defun nth-restart (index)  (defun nth-restart (index)
939    (nth index *sldb-restarts*))    (nth index *sldb-restarts*))
940    
941  (defun format-frame-for-emacs (frame)  (defun format-frame-for-emacs (number frame)
942    (list (di:frame-number frame)    (print-with-frame-label
943          (with-output-to-string (*standard-output*)     number (lambda (*standard-output*)
944            (let ((*print-pretty* *sldb-pprint-frames*))              (debug::print-frame-call frame :verbosity 1 :number nil))))
             (debug::print-frame-call frame :verbosity 1 :number t)))))  
945    
946  (defun compute-backtrace (start end)  (defun compute-backtrace (start end)
947    "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 951  stack."
951      (loop for f = (nth-frame start) then (di:frame-down f)      (loop for f = (nth-frame start) then (di:frame-down f)
952            for i from start below end            for i from start below end
953            while f            while f
954            collect f)))            collect (cons i f))))
955    
956  (defmethod backtrace (start end)  (defmethod backtrace (start end)
957    (mapcar #'format-frame-for-emacs (compute-backtrace start end)))    (loop for (n . frame) in (compute-backtrace start end)
958            collect (list n (format-frame-for-emacs n frame))))
959    
960  (defmethod debugger-info-for-emacs (start end)  (defmethod debugger-info-for-emacs (start end)
961    (list (format-condition-for-emacs)    (list (debugger-condition-for-emacs)
962          (format-restarts-for-emacs)          (format-restarts-for-emacs)
963          (backtrace start end)))          (backtrace start end)))
964    
# Line 1085  stack." Line 982  stack."
982           (location (di:frame-code-location frame))           (location (di:frame-code-location frame))
983           (debug-function (di:frame-debug-function frame))           (debug-function (di:frame-debug-function frame))
984           (debug-variables (di::debug-function-debug-variables debug-function)))           (debug-variables (di::debug-function-debug-variables debug-function)))
985      (loop for v across debug-variables      (loop for v across debug-variables collect
986            collect (list            (list :name (to-string (di:debug-variable-symbol v))
987                     :symbol (di:debug-variable-symbol v)                  :id (di:debug-variable-id v)
988                     :id (di:debug-variable-id v)                  :value-string (ecase (di:debug-variable-validity v location)
989                     :value-string                                  (:valid
990                     (if (eq (di:debug-variable-validity v location)                                   (to-string (di:debug-variable-value v frame)))
991                             :valid)                                  ((:invalid :unknown)
992                         (to-string (di:debug-variable-value v frame))                                   "<not-available>"))))))
                        "<not-available>")))))  
993    
994  (defmethod frame-catch-tags (index)  (defmethod frame-catch-tags (index)
995    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))
# Line 1105  stack." Line 1001  stack."
1001  (defslimefun sldb-abort ()  (defslimefun sldb-abort ()
1002    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
1003    
1004    (defun set-step-breakpoints (frame)
1005      (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1006                                         (di:frame-code-location frame)))
1007        (error "Cannot step, in elsewhere code~%"))
1008      (let* ((code-location (di:frame-code-location frame))
1009             (debug::*bad-code-location-types*
1010              (remove :call-site debug::*bad-code-location-types*))
1011             (next (debug::next-code-locations code-location)))
1012        (cond (next
1013               (let ((steppoints '()))
1014                 (flet ((hook (frame breakpoint)
1015                          (let ((debug:*stack-top-hint* frame))
1016                            (mapc #'di:delete-breakpoint steppoints)
1017                            (let ((cl (di::breakpoint-what breakpoint)))
1018                              (break "Breakpoint: ~S ~S"
1019                                     (di:code-location-kind cl)
1020                                     (di::compiled-code-location-pc cl))))))
1021                   (dolist (code-location next)
1022                     (let ((bp (di:make-breakpoint #'hook code-location
1023                                                   :kind :code-location)))
1024                       (di:activate-breakpoint bp)
1025                       (push bp steppoints))))))
1026             (t
1027              (flet ((hook (frame breakpoint values cookie)
1028                       (declare (ignore cookie))
1029                       (di:delete-breakpoint breakpoint)
1030                       (let ((debug:*stack-top-hint* frame))
1031                         (break "Function-end: ~A ~A" breakpoint values))))
1032                (let* ((debug-function (di:frame-debug-function frame))
1033                       (bp (di:make-breakpoint #'hook debug-function
1034                                               :kind :function-end)))
1035                  (di:activate-breakpoint bp)))))))
1036    
1037    (defslimefun sldb-step (frame)
1038      (cond ((find-restart 'continue *swank-debugger-condition*)
1039             (set-step-breakpoints (nth-frame frame))
1040             (continue *swank-debugger-condition*))
1041            (t
1042             (error "Cannot continue in from condition: ~A"
1043                    *swank-debugger-condition*))))
1044    
1045    (defslimefun sldb-disassemble (frame-number)
1046      "Return a string with the disassembly of frames code."
1047      ;; this could need some refactoring.
1048      (let* ((frame (nth-frame frame-number))
1049             (real-frame (di::frame-real-frame frame))
1050             (frame-pointer (di::frame-pointer real-frame))
1051             (debug-fun (di:frame-debug-function real-frame)))
1052        (with-output-to-string (*standard-output*)
1053          (format t "Frame: ~S~%~:[Real Frame: ~S~%~;~]Frame Pointer: ~S~%"
1054                  frame (eq frame real-frame) real-frame frame-pointer)
1055          (etypecase debug-fun
1056            (di::compiled-debug-function
1057             (let* ((code-loc (di:frame-code-location frame))
1058                    (component (di::compiled-debug-function-component debug-fun))
1059                    (pc (di::compiled-code-location-pc code-loc))
1060                    (ip (sys:sap-int
1061                         (sys:sap+ (kernel:code-instructions component) pc)))
1062                    (kind (if (di:code-location-unknown-p code-loc)
1063                              :unkown
1064                              (di:code-location-kind code-loc)))
1065                    (fun (di:debug-function-function debug-fun)))
1066               (format t "Instruction pointer: #x~X [pc: ~S kind: ~S]~%~%~%"
1067                       ip pc kind)
1068               (if fun
1069                   (disassemble fun)
1070                   (disassem:disassemble-code-component component))))
1071            (di::bogus-debug-function
1072             (format t "~%[Disassembling bogus frames not implemented]"))))))
1073    
1074  ;;;; Inspecting  ;;;; Inspecting
1075    
 (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)  
   (reset-inspector)  
   (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)  
           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))))))  
1076  (defconstant +lowtag-symbols+  (defconstant +lowtag-symbols+
1077    '(vm:even-fixnum-type    '(vm:even-fixnum-type
1078      vm:function-pointer-type      vm:function-pointer-type
# Line 1168  stack." Line 1097  stack."
1097       (append (apropos-list "-TYPE" "VM" t)       (append (apropos-list "-TYPE" "VM" t)
1098               (apropos-list "-TYPE" "BIGNUM" t)))))               (apropos-list "-TYPE" "BIGNUM" t)))))
1099    
1100  (defun describe-primitive-type (object)  (defmethod describe-primitive-type (object)
1101    (with-output-to-string (*standard-output*)    (with-output-to-string (*standard-output*)
1102      (let* ((lowtag (kernel:get-lowtag object))      (let* ((lowtag (kernel:get-lowtag object))
1103             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
# Line 1184  stack." Line 1113  stack."
1113                 (format t ", type: ~A]" type-symbol)))                 (format t ", type: ~A]" type-symbol)))
1114              (t (format t "]"))))))              (t (format t "]"))))))
1115    
 (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*))  
            nil)  
           (t (inspect-object (aref *inspector-history* (1+ position)))))))  
   
 (defslimefun quit-inspector ()  
   (reset-inspector)  
   nil)  
   
 (defslimefun describe-inspectee ()  
   "Describe the currently inspected object."  
   (print-description-to-string *inspectee*))  
   
 (defgeneric inspected-parts (object)  
   (:documentation  
    "Return a short description and a list of (label . value) pairs."))  
   
1116  (defmethod inspected-parts (o)  (defmethod inspected-parts (o)
1117    (cond ((di::indirect-value-cell-p o)    (cond ((di::indirect-value-cell-p o)
1118           (inspected-parts-of-value-cell o))           (inspected-parts-of-value-cell o))
# Line 1235  nil if there's no second element." Line 1131  nil if there's no second element."
1131    (values (format nil "~A~% is a value cell." o)    (values (format nil "~A~% is a value cell." o)
1132            (list (cons "Value" (c:value-cell-ref o)))))            (list (cons "Value" (c:value-cell-ref o)))))
1133    
 ;; 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)))))  
       (loop  
        (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))  
                     reversed-elements)  
               (incf length))  
              (t  
               (push (cons (string 'rest) in-list) reversed-elements)  
               (done "The object is an improper list of length ~S.~%")))))))  
   
1134  (defmethod inspected-parts ((o function))  (defmethod inspected-parts ((o function))
1135    (let ((header (kernel:get-type o)))    (let ((header (kernel:get-type o)))
1136      (cond ((= header vm:function-header-type)      (cond ((= header vm:function-header-type)
# Line 1304  nil if there's no second element." Line 1167  nil if there's no second element."
1167            `(("Name" . ,(kernel:fdefn-name o))            `(("Name" . ,(kernel:fdefn-name o))
1168              ("Function" . ,(kernel:fdefn-function o)))))              ("Function" . ,(kernel:fdefn-function o)))))
1169    
1170    
1171    ;;;; Multiprocessing
1172    
1173    #+MP
1174    (progn
1175      (defvar *known-processes* '()         ; FIXME: leakage. -luke
1176        "List of processes that have been assigned IDs.
1177         The ID is the position in the list.")
1178    
1179      (defmethod startup-multiprocessing ()
1180        (setq *swank-in-background* :spawn)
1181        ;; Threads magic: this never returns! But top-level becomes
1182        ;; available again.
1183        (mp::startup-idle-and-top-level-loops))
1184    
1185      (defmethod spawn (fn &key (name "Anonymous"))
1186        (mp:make-process fn :name name))
1187    
1188      (defmethod thread-id ()
1189        (mp:without-scheduling
1190         (or (find-thread-id)
1191             (prog1 (length *known-processes*)
1192               (setq *known-processes*
1193                     (append *known-processes* (list (mp:current-process))))))))
1194    
1195      (defun find-thread-id (&optional (process (mp:current-process)))
1196        (position process *known-processes*))
1197    
1198      (defun lookup-thread (thread-id)
1199        (or (nth thread-id *known-processes*)
1200            (error "Unknown Thread-ID: ~S" thread-id)))
1201    
1202      (defmethod thread-name (thread-id)
1203        (mp:process-name (lookup-thread thread-id)))
1204    
1205      (defmethod make-lock (&key name)
1206        (mp:make-lock name))
1207    
1208      (defmethod call-with-lock-held (lock function)
1209        (mp:with-lock-held (lock)
1210          (funcall function)))
1211    )
1212    
1213    
1214    ;;;; Epilogue
1215  ;;; Local Variables:  ;;; Local Variables:
1216  ;;; 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))))
1217  ;;; End:  ;;; End:

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.55

  ViewVC Help
Powered by ViewVC 1.1.5