/[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.63 by heller, Sun Feb 8 19:19:42 2004 UTC
# Line 1  Line 1 
1  ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-  ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2    
3  (declaim (optimize debug))  (declaim (optimize (debug 2)))
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    (setq *swank-in-background* :sigio)
11    
12    (defimplementation create-socket (host port)
13      (ext:create-inet-listener 0 :stream
14                                :reuse-address t
15                                :host (resolve-hostname host)))
16    
17    (defimplementation local-port (socket)
18      (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
19    
20    (defimplementation close-socket (socket)
21      (ext:close-socket (socket-fd socket)))
22    
23  (defun without-interrupts* (body)  (defimplementation accept-connection (socket)
24    (sys:without-interrupts (funcall body)))    #+MP (mp:process-wait-until-fd-usable socket :input)
25      (make-socket-io-stream (ext:accept-tcp-connection socket)))
26    
27    (defvar *sigio-handlers* '()
28      "List of (key . (fn . args)) pairs to be called on SIGIO.")
29    
30    (defun add-sigio-handler (key fn)
31      (push (cons key fn) *sigio-handlers*))
32    
33    (defun remove-sigio-handler (key)
34      (setf *sigio-handlers* (delete key *sigio-handlers* :key #'car)))
35    
36    (defun sigio-handler (signal code scp)
37      (mapc (lambda (handler) (funcall (cdr handler))) *sigio-handlers*)
38      )
39    
40    (defun set-sigio-handler ()
41      (sys:enable-interrupt unix:SIGIO (lambda (signal code scp)
42                                         (sigio-handler signal code scp))))
43    (set-sigio-handler)
44    
45    (defimplementation add-input-handler (socket fn)
46      (let ((fd (socket-fd socket)))
47        (format *debug-io* "Adding input handler: ~S ~%" fd)
48        ;; XXX error checking
49        (unix:unix-fcntl fd unix:f-setown (unix:unix-getpid))
50        (unix:unix-fcntl fd unix:f-setfl unix:FASYNC)
51        (add-sigio-handler fd fn)))
52    
53    (defimplementation remove-input-handlers (socket)
54      (let ((fd (socket-fd socket)))
55        (remove-sigio-handler fd)
56        (sys:invalidate-descriptor fd))
57      (close socket))
58    
59    (defimplementation make-fn-streams (input-fn output-fn)
60      (let* ((output (make-slime-output-stream output-fn))
61             (input  (make-slime-input-stream input-fn output)))
62        (values input output)))
63    
64    ;;;;; Socket helpers.
65    
66    (defun socket-fd (socket)
67      "Return the filedescriptor for the socket represented by SOCKET."
68      (etypecase socket
69        (fixnum socket)
70        (sys:fd-stream (sys:fd-stream-fd socket))))
71    
72    (defun resolve-hostname (hostname)
73      "Return the IP address of HOSTNAME as an integer."
74      (let* ((hostent (ext:lookup-host-entry hostname))
75             (address (car (ext:host-entry-addr-list hostent))))
76        (ext:htonl address)))
77    
78    (defun make-socket-io-stream (fd)
79      "Create a new input/output fd-stream for FD."
80      (sys:make-fd-stream fd :input t :output t :element-type 'base-char))
81    
82  (defun set-fd-non-blocking (fd)  (defun set-fd-non-blocking (fd)
83    (flet ((fcntl (fd cmd arg)    (flet ((fcntl (fd cmd arg)
84             (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)             (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)
85               (or flags               (or flags
86                   (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))                   (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))
87      (let ((flags (fcntl fd unix:F-GETFL 0)))      (let ((flags (fcntl fd unix:F-GETFL 0)))
88        (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))        (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))
89    
90    
91  ;;;; TCP server.  ;;;; Unix signals
92    
93  (defun resolve-hostname (name)  (defmethod call-without-interrupts (fn)
94    (let* ((hostent (ext:lookup-host-entry name))    (sys:without-interrupts (funcall fn))
95           (address (car (ext:host-entry-addr-list hostent))))    ;;(funcall fn)
96      (ext:htonl address)))    )
97    
98  (defun create-swank-server (port &key (reuse-address t)  (defmethod getpid ()
99                              (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))  
100    
101    
102  ;;;; Stream handling  ;;;; Stream handling
103    
104  (defstruct (slime-output-stream  (defstruct (slime-output-stream
105               (:include lisp::lisp-stream               (:include lisp::lisp-stream
106                         (lisp::misc #'sos/misc)                         (lisp::misc #'sos/misc)
107                         (lisp::out #'sos/out)                         (lisp::out #'sos/out)
108                         (lisp::sout #'sos/sout))                         (lisp::sout #'sos/sout))
109               (:conc-name sos.)               (:conc-name sos.)
110               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
111                 (:constructor make-slime-output-stream (output-fn)))
112      (output-fn nil :type function)
113    (buffer (make-string 512) :type string)    (buffer (make-string 512) :type string)
114    (index 0 :type kernel:index)    (index 0 :type kernel:index)
115    (column 0 :type kernel:index))    (column 0 :type kernel:index))
# Line 117  The request is read from the socket as a Line 133  The request is read from the socket as a
133  (defun sos/sout (stream string start end)  (defun sos/sout (stream string start end)
134    (loop for i from start below end    (loop for i from start below end
135          do (sos/out stream (aref string i))))          do (sos/out stream (aref string i))))
136    
137  (defun sos/misc (stream operation &optional arg1 arg2)  (defun sos/misc (stream operation &optional arg1 arg2)
138    (declare (ignore arg1 arg2))    (declare (ignore arg1 arg2))
139    (case operation    (case operation
140      ((:force-output :finish-output)      ((:force-output :finish-output)
141       (let ((end (sos.index stream)))       (let ((end (sos.index stream)))
142         (unless (zerop end)         (unless (zerop end)
143           (send-to-emacs `(:read-output ,(subseq (sos.buffer stream) 0 end)))           (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
144           (setf (sos.index stream) 0))))           (setf (sos.index stream) 0))))
145      (:charpos (sos.column stream))      (:charpos (sos.column stream))
146      (:line-length 75)      (:line-length 75)
147      (:file-position nil)      (:file-position nil)
# Line 135  The request is read from the socket as a Line 151  The request is read from the socket as a
151      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
152    
153  (defstruct (slime-input-stream  (defstruct (slime-input-stream
154               (:include string-stream               (:include string-stream
155                         (lisp::in #'sis/in)                         (lisp::in #'sis/in)
156                         (lisp::misc #'sis/misc))                         (lisp::misc #'sis/misc))
157               (:conc-name sis.)               (:conc-name sis.)
158               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
159    (buffer "" :type string)               (:constructor make-slime-input-stream (input-fn sos)))
160    (index 0 :type kernel:index))    (input-fn nil :type function)
161      ;; We know our sibling output stream, so that we can force it before
162      ;; requesting input.
163      (sos      nil :type slime-output-stream)
164      (buffer   ""  :type string)
165      (index    0   :type kernel:index))
166    
167  (defun sis/in (stream eof-errorp eof-value)  (defun sis/in (stream eof-errorp eof-value)
168    (declare (ignore eof-errorp eof-value))    (declare (ignore eof-errorp eof-value))
169    (let ((index (sis.index stream))    (let ((index (sis.index stream))
170          (buffer (sis.buffer stream)))          (buffer (sis.buffer stream)))
171      (when (= index (length buffer))      (when (= index (length buffer))
172        (setf buffer (slime-read-string))        (force-output (sis.sos stream))
173          (setf buffer (funcall (sis.input-fn stream)))
174        (setf (sis.buffer stream) buffer)        (setf (sis.buffer stream) buffer)
175        (setf index 0))        (setf index 0))
176      (prog1 (aref buffer index)      (prog1 (aref buffer index)
# Line 162  The request is read from the socket as a Line 184  The request is read from the socket as a
184      (:unread (setf (aref (sis.buffer stream)      (:unread (setf (aref (sis.buffer stream)
185                           (decf (sis.index stream)))                           (decf (sis.index stream)))
186                     arg1))                     arg1))
187      (:clear-input (setf (sis.index stream) 0      (:clear-input
188         (setf (sis.index stream) 0
189                          (sis.buffer stream) ""))                          (sis.buffer stream) ""))
190      (:listen (< (sis.index stream) (length (sis.buffer stream))))      (:listen (< (sis.index stream) (length (sis.buffer stream))))
191      (:charpos nil)      (:charpos nil)
# Line 277  the error-context redundant." Line 300  the error-context redundant."
300           (make-location (list :file (namestring *compile-file-truename*))           (make-location (list :file (namestring *compile-file-truename*))
301                          (list :position 0)))                          (list :position 0)))
302          (*compile-filename*          (*compile-filename*
303             ;; XXX is this _ever_ used?  By what?  *compile-file-truename*
304             ;; should be set by the implementation inside any call to compile-file
305           (make-location (list :file *compile-filename*) (list :position 0)))           (make-location (list :file *compile-filename*) (list :position 0)))
306          (t          (t
307           (list :error "No error location available"))))           (list :error "No error location available"))))
308    
309  (defmacro with-compilation-hooks (() &body body)  (defimplementation call-with-compilation-hooks (function)
310    "Execute BODY and record the set of compiler notes."    (let ((*previous-compiler-condition* nil)
311    `(let ((*previous-compiler-condition* nil)          (*previous-context* nil)
312           (*previous-context* nil)          (*print-readably* nil))
          (*print-readably* nil))  
313      (handler-bind ((c::compiler-error #'handle-notification-condition)      (handler-bind ((c::compiler-error #'handle-notification-condition)
314                     (c::style-warning  #'handle-notification-condition)                     (c::style-warning  #'handle-notification-condition)
315                     (c::warning        #'handle-notification-condition))                     (c::warning        #'handle-notification-condition))
316        ,@body)))        (funcall function))))
317    
318  (defmethod compile-file-for-emacs (filename load-p)  (defimplementation compile-file-for-emacs (filename load-p)
319    (clear-xref-info filename)    (clear-xref-info filename)
320    (with-compilation-hooks ()    (with-compilation-hooks ()
321      (let ((*buffer-name* nil)      (let ((*buffer-name* nil)
322            (*compile-filename* filename))            (*compile-filename* filename))
323          (compile-file filename :load load-p))))        (multiple-value-bind (fasl-file warnings-p failure-p)
324              (compile-file filename)
325            (declare (ignore warnings-p))
326            (when (and load-p (not failure-p))
327              (load fasl-file))))))
328    
329  (defmethod compile-string-for-emacs (string &key buffer position)  (defimplementation compile-string-for-emacs (string &key buffer position)
330    (with-compilation-hooks ()    (with-compilation-hooks ()
331      (let ((*package* *buffer-package*)      (let ((*package* *buffer-package*)
332            (*compile-filename* nil)            (*compile-filename* nil)
# Line 312  the error-context redundant." Line 340  the error-context redundant."
340                          :emacs-buffer-offset ,position                          :emacs-buffer-offset ,position
341                          :emacs-buffer-string ,string))))))                          :emacs-buffer-string ,string))))))
342    
343    (defimplementation compile-system-for-emacs (system-name)
344      (with-compilation-hooks ()
345        (cond ((ext:featurep :asdf)
346               (let ((operate (find-symbol (string :operate) :asdf))
347                     (load-op (find-symbol (string :load-op) :asdf)))
348                 (funcall operate load-op system-name)))
349              (t (error "ASDF not loaded")))))
350    
351    
352  ;;;; XREF  ;;;; XREF
353    
354  (defun lookup-xrefs (finder name)  (defun lookup-xrefs (finder name)
355    (xref-results-for-emacs (funcall finder (from-string name))))    (xref-results-for-emacs (funcall finder (from-string name))))
356    
357  (defslimefun who-calls (function-name)  (defimplementation who-calls (function-name)
   "Return the places where FUNCTION-NAME is called."  
358    (lookup-xrefs #'xref:who-calls function-name))    (lookup-xrefs #'xref:who-calls function-name))
359    
360  (defslimefun who-references (variable)  (defimplementation who-references (variable)
   "Return the places where the global variable VARIABLE is referenced."  
361    (lookup-xrefs #'xref:who-references variable))    (lookup-xrefs #'xref:who-references variable))
362    
363  (defslimefun who-binds (variable)  (defimplementation who-binds (variable)
   "Return the places where the global variable VARIABLE is bound."  
364    (lookup-xrefs #'xref:who-binds variable))    (lookup-xrefs #'xref:who-binds variable))
365    
366  (defslimefun who-sets (variable)  (defimplementation who-sets (variable)
   "Return the places where the global variable VARIABLE is set."  
367    (lookup-xrefs #'xref:who-sets variable))    (lookup-xrefs #'xref:who-sets variable))
368    
369  #+cmu19  #+cmu19
370  (progn  (progn
371    (defslimefun who-macroexpands (macro)    (defimplementation who-macroexpands (macro)
     "Return the places where MACRO is expanded."  
372      (lookup-xrefs #'xref:who-macroexpands macro))      (lookup-xrefs #'xref:who-macroexpands macro))
373    
374    (defslimefun who-specializes (class)    (defimplementation who-specializes (class)
     "Return the methods with specializers for CLASS."  
375      (let* ((methods (xref::who-specializes (find-class (from-string class))))      (let* ((methods (xref::who-specializes (find-class (from-string class))))
376             (locations (mapcar #'method-source-location methods)))             (locations (mapcar #'method-source-location methods)))
377        (group-xrefs (mapcar (lambda (m l)        (group-xrefs (mapcar (lambda (m l)
378                               (cons (let ((*print-pretty* nil))                               (cons (let ((*print-pretty* nil))
379                                       (to-string m))                                       (to-string m))
380                                     l))                                     l))
381                             methods locations))))                             methods locations)))))
   )  
382    
383  (defun resolve-xref-location (xref)  (defun resolve-xref-location (xref)
384    (let ((name (xref:xref-context-name xref))    (let ((name (xref:xref-context-name xref))
# Line 380  reference   ::= (FUNCTION-SPECIFIER . SO Line 409  reference   ::= (FUNCTION-SPECIFIER . SO
409                xrefs)))                xrefs)))
410      (group-xrefs xrefs)))      (group-xrefs xrefs)))
411    
   
 (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))))  
   
412  (defun clear-xref-info (namestring)  (defun clear-xref-info (namestring)
413    "Clear XREF notes pertaining to FILENAME.    "Clear XREF notes pertaining to FILENAME.
414  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 529  the code omponent CODE." Line 535  the code omponent CODE."
535                         (function-source-location e)))                         (function-source-location e)))
536     :test #'equal))     :test #'equal))
537    
538  (defslimefun list-callers (symbol-name)  (defimplementation list-callers (symbol-name)
539    "Return a list ((FILE . ((NAME . LOCATION) ...)) ...) of callers."    "Return a list ((FILE . ((NAME . LOCATION) ...)) ...) of callers."
540    (let ((components (function-callers (from-string symbol-name)))    (let ((components (function-callers (from-string symbol-name)))
541          (xrefs '()))          (xrefs '()))
# Line 543  the code omponent CODE." Line 549  the code omponent CODE."
549          (setq xrefs (nconc defs xrefs))))          (setq xrefs (nconc defs xrefs))))
550      (group-xrefs xrefs)))      (group-xrefs xrefs)))
551    
552    (defimplementation list-callees (symbol-name)
 (defslimefun list-callees (symbol-name)  
553    (let ((fns (function-callees (from-string symbol-name))))    (let ((fns (function-callees (from-string symbol-name))))
554      (group-xrefs (mapcar (lambda (fn)      (group-xrefs (mapcar (lambda (fn)
555                             (cons (to-string (kernel:%function-name fn))                             (cons (to-string (kernel:%function-name fn))
556                                   (function-source-location fn)))                                   (function-source-location fn)))
557                           fns))))                           fns))))
558    
559    
560  ;;;; Definitions  ;;;; Definitions
561    
562  (defvar *debug-definition-finding* t  (defvar *debug-definition-finding* nil
563    "When true don't handle errors while looking for definitions.    "When true don't handle errors while looking for definitions.
564  This is useful when debugging the definition-finding code.")  This is useful when debugging the definition-finding code.")
565    
566  (defmacro safe-definition-finding (&body body)  (defmacro safe-definition-finding (&body body)
567    "Execute BODY ignoring errors.  Return a the source location    "Execute BODY ignoring errors.  Return the source location returned
568  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
569  The second return value is the condition or nil."  return value is the condition or nil."
570    `(flet ((body () ,@body))    `(flet ((body () ,@body))
571      (if *debug-definition-finding*      (if *debug-definition-finding*
572          (body)          (body)
# Line 629  The second return value is the condition Line 635  The second return value is the condition
635             (etypecase pathname             (etypecase pathname
636               (pathname               (pathname
637                (make-location `(:file ,(guess-source-file pathname))                (make-location `(:file ,(guess-source-file pathname))
638                               `(:function-name ,(string name)))))))))))                               `(:function-name ,(string name))))
639                 (null `(:error ,(format nil "Cannot resolve: ~S" def-source)))
640                 )))))))
641    
642  (defun method-source-location (method)  (defun method-source-location (method)
643    (function-source-location (or (pcl::method-fast-function method)    (function-source-location (or (pcl::method-fast-function method)
# Line 670  The second return value is the condition Line 678  The second return value is the condition
678    (destructuring-bind (first) (function-source-locations function)    (destructuring-bind (first) (function-source-locations function)
679      first))      first))
680    
681  (defslimefun find-function-locations (symbol-name)  (defimplementation find-function-locations (symbol-name)
682    "Return a list of source-locations for SYMBOL-NAME's functions."    "Return a list of source-locations for SYMBOL-NAME's functions."
683    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)
684      (cond ((not foundp)      (cond ((not foundp)
# Line 688  The second return value is the condition Line 696  The second return value is the condition
696    
697  ;;;; Documentation.  ;;;; Documentation.
698    
699  (defmethod describe-symbol-for-emacs (symbol)  (defimplementation describe-symbol-for-emacs (symbol)
700    (let ((result '()))    (let ((result '()))
701      (flet ((doc (kind)      (flet ((doc (kind)
702               (or (documentation symbol kind) :not-documented))               (or (documentation symbol kind) :not-documented))
# Line 734  The second return value is the condition Line 742  The second return value is the condition
742                         (doc nil)))                         (doc nil)))
743        result)))        result)))
744    
 (defslimefun describe-setf-function (symbol-name)  
   (print-description-to-string  
    (or (ext:info setf inverse (from-string symbol-name))  
        (ext:info setf expander (from-string symbol-name)))))  
   
 (defslimefun describe-type (symbol-name)  
   (print-description-to-string  
    (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)  
       (:primitive  
        (print-description-to-string  
         (let ((alien::*values-type-okay* t))  
           (funcall (ext:info :alien-type :translator name) (list name)))))  
       ((:defined)  
        (print-description-to-string (ext:info :alien-type :definition name)))  
       (:unknown  
        (format nil "Unkown alien type: ~A" symbol-name)))))  
   
745  (defmacro %describe-alien (symbol-name namespace)  (defmacro %describe-alien (symbol-name namespace)
746    `(print-description-to-string    `(print-description-to-string
747      (ext:info :alien-type ,namespace (from-string ,symbol-name))))      (ext:info :alien-type ,namespace (from-string ,symbol-name))))
748    
749  (defslimefun describe-alien-struct (symbol-name)  (defimplementation describe-definition (symbol-name type)
750    (%describe-alien symbol-name :struct))    (case type
751        (:variable
752  (defslimefun describe-alien-union (symbol-name)       (describe-symbol symbol-name))
753    (%describe-alien symbol-name :union))      ((:function :generic-function)
754         (describe-function symbol-name))
755        (:setf
756         (print-description-to-string
757          (or (ext:info setf inverse (from-string symbol-name))
758              (ext:info setf expander (from-string symbol-name)))))
759        (:type
760         (print-description-to-string
761          (kernel:values-specifier-type (from-string symbol-name))))
762        (:class
763         (print-description-to-string (find-class (from-string symbol-name) nil)))
764        (:alien-type
765         (let ((name (from-string symbol-name)))
766           (ecase (ext:info :alien-type :kind name)
767             (:primitive
768              (print-description-to-string
769               (let ((alien::*values-type-okay* t))
770                 (funcall (ext:info :alien-type :translator name) (list name)))))
771             ((:defined)
772              (print-description-to-string (ext:info :alien-type
773                                                     :definition name)))
774             (:unknown
775              (format nil "Unkown alien type: ~A" symbol-name)))))
776        (:alien-struct
777         (%describe-alien symbol-name :struct))
778        (:alien-union
779         (%describe-alien symbol-name :union))
780        (:alien-enum
781         (%describe-alien symbol-name :enum))))
782    
783  (defslimefun describe-alien-enum (symbol-name)  (defimplementation arglist-string (fname)
   (%describe-alien symbol-name :enum))  
   
 (defmethod arglist-string (fname)  
784    "Return a string describing the argument list for FNAME.    "Return a string describing the argument list for FNAME.
785  The result has the format \"(...)\"."  The result has the format \"(...)\"."
786    (declare (type string fname))    (declare (type string fname))
787    (multiple-value-bind (function condition)    (multiple-value-bind (function package) (find-symbol-designator fname)
788        (ignore-errors (values (find-symbol-designator fname *buffer-package*)))      (unless package
789      (when condition        (return-from arglist-string (format nil "(-- Unkown symbol: ~A)" fname)))
       (return-from arglist-string (format nil "(-- ~A)" condition)))  
790      (let ((arglist      (let ((arglist
791             (if (not (or (fboundp function)             (if (not (or (fboundp function)
792                          (functionp function)))                          (functionp function)))
# Line 786  The result has the format \"(...)\"." Line 794  The result has the format \"(...)\"."
794                 (let* ((fun (or (macro-function function)                 (let* ((fun (or (macro-function function)
795                                 (symbol-function function)))                                 (symbol-function function)))
796                        (df (di::function-debug-function fun))                        (df (di::function-debug-function fun))
797                        (arglist (kernel:%function-arglist fun)))                        (arglist (kernel:%function-arglist
798                                    (kernel:%function-self fun))))
799                   (cond ((eval:interpreted-function-p fun)                   (cond ((eval:interpreted-function-p fun)
800                          (eval:interpreted-function-arglist fun))                          (eval:interpreted-function-arglist fun))
801                         ((pcl::generic-function-p fun)                         ((pcl::generic-function-p fun)
802                          (pcl::gf-pretty-arglist fun))                          (pcl:generic-function-lambda-list fun))
803                         (arglist arglist)                         (arglist arglist)
804                         ;; this should work both for                         ;; this should work both for
805                         ;; compiled-debug-function and for                         ;; compiled-debug-function and for
806                         ;; interpreted-debug-function                         ;; interpreted-debug-function
807                         (df (di::debug-function-lambda-list df))                         (df (di::debug-function-lambda-list df))
808                         (t "(<arglist-unavailable>)"))))))                         (t "(<arglist-unavailable>)"))))))
809        (if (stringp arglist)        (etypecase arglist
810            arglist          (string arglist)
811            (to-string arglist)))))          (cons (let ((*print-case* :downcase)) (princ-to-string arglist)))
812            (null "()")))))
813    
814    
815  ;;;; Miscellaneous.  ;;;; Miscellaneous.
816    
817  (defmethod macroexpand-all (form)  (defimplementation macroexpand-all (form)
818    (walker:macroexpand-all form))    (walker:macroexpand-all form))
819    
820  (defun tracedp (fname)  (in-package :c)
821    (gethash (debug::trace-fdefinition fname)  
822             debug::*traced-functions*))  (defun swank::expand-ir1-top-level (form)
823      "A scaled down version of the first pass of the compiler."
824  (defslimefun toggle-trace-fdefinition (fname-string)    (with-compilation-unit ()
825    (let ((fname (from-string fname-string)))      (let* ((*lexical-environment*
826      (cond ((tracedp fname)              (make-lexenv :default (make-null-environment)
827             (debug::untrace-1 fname)                           :cookie *default-cookie*
828             (format nil "~S is now untraced." fname))                           :interface-cookie *default-interface-cookie*))
829            (t             (*source-info* (make-lisp-source-info form))
830             (debug::trace-1 fname (debug::make-trace-info))             (*block-compile* nil)
831             (format nil "~S is now traced." fname)))))             (*block-compile-default* nil))
832          (with-ir1-namespace
833              (clear-stuff)
834            (find-source-paths form 0)
835            (ir1-top-level form '(0) t)))))
836    
837    (in-package :swank)
838    
839    (defslimefun print-ir1-converted-blocks (form)
840      (with-output-to-string (*standard-output*)
841        (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
842    
843    (defslimefun print-compilation-trace (form)
844      (with-output-to-string (*standard-output*)
845        (with-input-from-string (s form)
846          (let ((*package* *buffer-package*))
847            (ext:compile-from-stream s
848                                     :verbose t
849                                     :progress t
850                                     :trace-stream *standard-output*)))))
851    
852  (defslimefun set-default-directory (directory)  (defslimefun set-default-directory (directory)
853    (setf (ext:default-directory) (namestring directory))    (setf (ext:default-directory) (namestring directory))
# Line 827  The result has the format \"(...)\"." Line 856  The result has the format \"(...)\"."
856    (setf *default-pathname-defaults* (pathname (ext:default-directory)))    (setf *default-pathname-defaults* (pathname (ext:default-directory)))
857    (namestring (ext:default-directory)))    (namestring (ext:default-directory)))
858    
859    ;;; source-path-{stream,file,string,etc}-position moved into
860  ;;;; 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))))))  
861    
862  (defun code-location-stream-position (code-location stream)  (defun code-location-stream-position (code-location stream)
863    "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 924  to find the position of the correspondin
924    (safe-definition-finding    (safe-definition-finding
925     (source-location-from-code-location code-location)))     (source-location-from-code-location code-location)))
926    
 (defslimefun getpid ()  
   (unix:unix-getpid))  
   
927    
928  ;;;; Debugging  ;;;; Debugging
929    
930  (defvar *sldb-stack-top*)  (defvar *sldb-stack-top*)
931  (defvar *sldb-restarts*)  (defvar *sldb-restarts*)
932    
933  (defmethod call-with-debugging-environment (debugger-loop-fn)  (defimplementation call-with-debugging-environment (debugger-loop-fn)
934    (unix:unix-sigsetmask 0)    (unix:unix-sigsetmask 0)
935    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
936           (*sldb-restarts* (compute-restarts *swank-debugger-condition*))           (*sldb-restarts* (compute-restarts *swank-debugger-condition*))
# Line 1028  format suitable for Emacs." Line 954  format suitable for Emacs."
954          collect (list (princ-to-string (restart-name restart))          collect (list (princ-to-string (restart-name restart))
955                        (princ-to-string restart))))                        (princ-to-string restart))))
956    
 (defun format-condition-for-emacs ()  
   (format nil "~A~%   [Condition of type ~S]"  
           (debug::safe-condition-message *swank-debugger-condition*)  
           (type-of *swank-debugger-condition*)))  
   
957  (defun nth-frame (index)  (defun nth-frame (index)
958    (do ((frame *sldb-stack-top* (di:frame-down frame))    (do ((frame *sldb-stack-top* (di:frame-down frame))
959         (i index (1- i)))         (i index (1- i)))
# Line 1041  format suitable for Emacs." Line 962  format suitable for Emacs."
962  (defun nth-restart (index)  (defun nth-restart (index)
963    (nth index *sldb-restarts*))    (nth index *sldb-restarts*))
964    
965  (defun format-frame-for-emacs (frame)  (defun format-frame-for-emacs (number frame)
966    (list (di:frame-number frame)    (print-with-frame-label
967          (with-output-to-string (*standard-output*)     number (lambda (*standard-output*)
968            (let ((*print-pretty* *sldb-pprint-frames*))              (debug::print-frame-call frame :verbosity 1 :number nil))))
             (debug::print-frame-call frame :verbosity 1 :number t)))))  
969    
970  (defun compute-backtrace (start end)  (defun compute-backtrace (start end)
971    "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 975  stack."
975      (loop for f = (nth-frame start) then (di:frame-down f)      (loop for f = (nth-frame start) then (di:frame-down f)
976            for i from start below end            for i from start below end
977            while f            while f
978            collect f)))            collect (cons i f))))
979    
980  (defmethod backtrace (start end)  (defimplementation backtrace (start end)
981    (mapcar #'format-frame-for-emacs (compute-backtrace start end)))    (loop for (n . frame) in (compute-backtrace start end)
982            collect (list n (format-frame-for-emacs n frame))))
983    
984  (defmethod debugger-info-for-emacs (start end)  (defimplementation debugger-info-for-emacs (start end)
985    (list (format-condition-for-emacs)    (list (debugger-condition-for-emacs)
986          (format-restarts-for-emacs)          (format-restarts-for-emacs)
987          (backtrace start end)))          (backtrace start end)))
988    
989  (defmethod frame-source-location-for-emacs (index)  (defimplementation frame-source-location-for-emacs (index)
990    (code-location-source-location (di:frame-code-location (nth-frame index))))    (code-location-source-location (di:frame-code-location (nth-frame index))))
991    
992  (defmethod eval-in-frame (form index)  (defimplementation eval-in-frame (form index)
993    (di:eval-in-frame (nth-frame index) form))    (di:eval-in-frame (nth-frame index) form))
994    
995  (defslimefun pprint-eval-string-in-frame (string index)  (defslimefun pprint-eval-string-in-frame (string index)
# Line 1080  stack." Line 1001  stack."
1001    (reset-inspector)    (reset-inspector)
1002    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))
1003    
1004  (defmethod frame-locals (index)  (defimplementation frame-locals (index)
1005    (let* ((frame (nth-frame index))    (let* ((frame (nth-frame index))
1006           (location (di:frame-code-location frame))           (location (di:frame-code-location frame))
1007           (debug-function (di:frame-debug-function frame))           (debug-function (di:frame-debug-function frame))
1008           (debug-variables (di::debug-function-debug-variables debug-function)))           (debug-variables (di::debug-function-debug-variables debug-function)))
1009      (loop for v across debug-variables      (loop for v across debug-variables collect
1010            collect (list            (list :name (to-string (di:debug-variable-symbol v))
1011                     :symbol (di:debug-variable-symbol v)                  :id (di:debug-variable-id v)
1012                     :id (di:debug-variable-id v)                  :value-string (ecase (di:debug-variable-validity v location)
1013                     :value-string                                  (:valid
1014                     (if (eq (di:debug-variable-validity v location)                                   (to-string (di:debug-variable-value v frame)))
1015                             :valid)                                  ((:invalid :unknown)
1016                         (to-string (di:debug-variable-value v frame))                                   "<not-available>"))))))
                        "<not-available>")))))  
1017    
1018  (defmethod frame-catch-tags (index)  (defimplementation frame-catch-tags (index)
1019    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))
1020          collect `(,tag . ,(code-location-source-location code-location))))          collect `(,tag . ,(code-location-source-location code-location))))
1021    
# Line 1105  stack." Line 1025  stack."
1025  (defslimefun sldb-abort ()  (defslimefun sldb-abort ()
1026    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
1027    
1028    (defun set-step-breakpoints (frame)
1029  ;;;; Inspecting    (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1030                                         (di:frame-code-location frame)))
1031        (error "Cannot step, in elsewhere code~%"))
1032      (let* ((code-location (di:frame-code-location frame))
1033             (debug::*bad-code-location-types*
1034              (remove :call-site debug::*bad-code-location-types*))
1035             (next (debug::next-code-locations code-location)))
1036        (cond (next
1037               (let ((steppoints '()))
1038                 (flet ((hook (frame breakpoint)
1039                          (let ((debug:*stack-top-hint* frame))
1040                            (mapc #'di:delete-breakpoint steppoints)
1041                            (let ((cl (di::breakpoint-what breakpoint)))
1042                              (break "Breakpoint: ~S ~S"
1043                                     (di:code-location-kind cl)
1044                                     (di::compiled-code-location-pc cl))))))
1045                   (dolist (code-location next)
1046                     (let ((bp (di:make-breakpoint #'hook code-location
1047                                                   :kind :code-location)))
1048                       (di:activate-breakpoint bp)
1049                       (push bp steppoints))))))
1050             (t
1051              (flet ((hook (frame breakpoint values cookie)
1052                       (declare (ignore cookie))
1053                       (di:delete-breakpoint breakpoint)
1054                       (let ((debug:*stack-top-hint* frame))
1055                         (break "Function-end: ~A ~A" breakpoint values))))
1056                (let* ((debug-function (di:frame-debug-function frame))
1057                       (bp (di:make-breakpoint #'hook debug-function
1058                                               :kind :function-end)))
1059                  (di:activate-breakpoint bp)))))))
1060    
1061    (defslimefun sldb-step (frame)
1062      (cond ((find-restart 'continue *swank-debugger-condition*)
1063             (set-step-breakpoints (nth-frame frame))
1064             (continue *swank-debugger-condition*))
1065            (t
1066             (error "Cannot continue in from condition: ~A"
1067                    *swank-debugger-condition*))))
1068    
1069  (defvar *inspectee*)  (defun frame-cfp (frame)
1070  (defvar *inspectee-parts*)    "Return the Control-Stack-Frame-Pointer for FRAME."
1071  (defvar *inspector-stack* '())    (etypecase frame
1072  (defvar *inspector-history* (make-array 10 :adjustable t :fill-pointer 0))      (di::compiled-frame (di::frame-pointer frame))
1073  (defvar *inspect-length* 30)      ((or di::interpreted-frame null) -1)))
1074    
1075  (defun reset-inspector ()  (defun frame-ip (frame)
1076    (setq *inspectee* nil)    "Return the (absolute) instruction pointer and the relative pc of FRAME."
1077    (setq *inspectee-parts* nil)    (if (not frame)
1078    (setq *inspector-stack* nil)        -1
1079    (setf (fill-pointer *inspector-history*) 0))        (let ((debug-fun (di::frame-debug-function frame)))
1080            (etypecase debug-fun
1081              (di::compiled-debug-function
1082               (let* ((code-loc (di:frame-code-location frame))
1083                      (component (di::compiled-debug-function-component debug-fun))
1084                      (pc (di::compiled-code-location-pc code-loc))
1085                      (ip (sys:without-gcing
1086                           (sys:sap-int
1087                            (sys:sap+ (kernel:code-instructions component) pc)))))
1088                 (values ip pc)))
1089              ((or di::bogus-debug-function di::interpreted-debug-function)
1090               -1)))))
1091    
1092    (defun frame-registers (frame)
1093      "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1094      (let* ((cfp (frame-cfp frame))
1095             (csp (frame-cfp (di::frame-up frame)))
1096             (ip (frame-ip frame))
1097             (ocfp (frame-cfp (di::frame-down frame)))
1098             (lra (frame-ip (di::frame-down frame))))
1099        (values csp cfp ip ocfp lra)))
1100    
1101    (defun print-frame-registers (frame-number)
1102      (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1103        (flet ((fixnum (p) (etypecase p
1104                             (integer p)
1105                             (sys:system-area-pointer (sys:sap-int p)))))
1106          (apply #'format t "~
1107    CSP  =  ~X
1108    CFP  =  ~X
1109    IP   =  ~X
1110    OCFP =  ~X
1111    LRA  =  ~X~%" (mapcar #'fixnum
1112                          (multiple-value-list (frame-registers frame)))))))
1113    
1114    (defslimefun sldb-disassemble (frame-number)
1115      "Return a string with the disassembly of frames code."
1116        (with-output-to-string (*standard-output*)
1117          (print-frame-registers frame-number)
1118          (terpri)
1119          (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1120                 (debug-fun (di::frame-debug-function frame)))
1121            (etypecase debug-fun
1122              (di::compiled-debug-function
1123               (let* ((component (di::compiled-debug-function-component debug-fun))
1124                      (fun (di:debug-function-function debug-fun)))
1125                 (if fun
1126                     (disassemble fun)
1127                     (disassem:disassemble-code-component component))))
1128              (di::bogus-debug-function
1129               (format t "~%[Disassembling bogus frames not implemented]"))))))
1130    
1131    #+(or)
1132    (defun print-binding-stack ()
1133      (flet ((bsp- (p) (sys:sap+ p (- (* vm:binding-size vm:word-bytes))))
1134             (frob (p offset) (kernel:make-lisp-obj (sys:sap-ref-32 p offset))))
1135        (do ((bsp (bsp- (kernel:binding-stack-pointer-sap)) (bsp- bsp))
1136             (start (sys:int-sap (lisp::binding-stack-start))))
1137            ((sys:sap= bsp start))
1138          (format t "~X:  ~S = ~S~%"
1139                  (sys:sap-int bsp)
1140                  (frob bsp (* vm:binding-symbol-slot vm:word-bytes))
1141                  (frob bsp (* vm:binding-value-slot vm:word-bytes))))))
1142    
1143    ;; (print-binding-stack)
1144    
1145    #+(or)
1146    (defun print-catch-blocks ()
1147      (do ((b (di::descriptor-sap lisp::*current-catch-block*)
1148              (sys:sap-ref-sap b (* vm:catch-block-previous-catch-slot
1149                                    vm:word-bytes))))
1150          (nil)
1151        (let ((int (sys:sap-int b)))
1152          (when (zerop int) (return))
1153          (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1154            (let ((uwp (ref vm:catch-block-current-uwp-slot))
1155                  (cfp (ref vm:catch-block-current-cont-slot))
1156                  (tag (ref vm:catch-block-tag-slot))
1157                  )
1158          (format t "~X:  uwp = ~8X  cfp = ~8X  tag = ~X~%"
1159                  int uwp cfp (kernel:make-lisp-obj tag)))))))
1160    
1161    ;; (print-catch-blocks)
1162    
1163    #+(or)
1164    (defun print-unwind-blocks ()
1165      (do ((b (di::descriptor-sap lisp::*current-unwind-protect-block*)
1166              (sys:sap-ref-sap b (* vm:unwind-block-current-uwp-slot
1167                                    vm:word-bytes))))
1168          (nil)
1169        (let ((int (sys:sap-int b)))
1170          (when (zerop int) (return))
1171          (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1172            (let ((cfp (ref vm:unwind-block-current-cont-slot)))
1173              (format t "~X:  cfp = ~X~%" int cfp))))))
1174    
1175  (defslimefun init-inspector (string)  ;; (print-unwind-blocks)
1176    (reset-inspector)  
1177    (inspect-object (eval (from-string string))))  
1178    ;;;; Inspecting
1179    
 (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))))))  
1180  (defconstant +lowtag-symbols+  (defconstant +lowtag-symbols+
1181    '(vm:even-fixnum-type    '(vm:even-fixnum-type
1182      vm:function-pointer-type      vm:function-pointer-type
# Line 1168  stack." Line 1201  stack."
1201       (append (apropos-list "-TYPE" "VM" t)       (append (apropos-list "-TYPE" "VM" t)
1202               (apropos-list "-TYPE" "BIGNUM" t)))))               (apropos-list "-TYPE" "BIGNUM" t)))))
1203    
1204  (defun describe-primitive-type (object)  
1205    (defimplementation describe-primitive-type (object)
1206    (with-output-to-string (*standard-output*)    (with-output-to-string (*standard-output*)
1207      (let* ((lowtag (kernel:get-lowtag object))      (let* ((lowtag (kernel:get-lowtag object))
1208             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
# Line 1184  stack." Line 1218  stack."
1218                 (format t ", type: ~A]" type-symbol)))                 (format t ", type: ~A]" type-symbol)))
1219              (t (format t "]"))))))              (t (format t "]"))))))
1220    
1221  (defun nth-part (index)  (defimplementation inspected-parts (o)
   (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."))  
   
 (defmethod inspected-parts (o)  
1222    (cond ((di::indirect-value-cell-p o)    (cond ((di::indirect-value-cell-p o)
1223           (inspected-parts-of-value-cell o))           (inspected-parts-of-value-cell o))
1224          (t          (t
# Line 1235  nil if there's no second element." Line 1236  nil if there's no second element."
1236    (values (format nil "~A~% is a value cell." o)    (values (format nil "~A~% is a value cell." o)
1237            (list (cons "Value" (c:value-cell-ref o)))))            (list (cons "Value" (c:value-cell-ref o)))))
1238    
 ;; 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.~%")))))))  
   
1239  (defmethod inspected-parts ((o function))  (defmethod inspected-parts ((o function))
1240    (let ((header (kernel:get-type o)))    (let ((header (kernel:get-type o)))
1241      (cond ((= header vm:function-header-type)      (cond ((= header vm:function-header-type)
# Line 1304  nil if there's no second element." Line 1272  nil if there's no second element."
1272            `(("Name" . ,(kernel:fdefn-name o))            `(("Name" . ,(kernel:fdefn-name o))
1273              ("Function" . ,(kernel:fdefn-function o)))))              ("Function" . ,(kernel:fdefn-function o)))))
1274    
1275    
1276    ;;;; Profiling
1277    (defimplementation profile (fname)
1278      (eval `(profile:profile ,fname)))
1279    
1280    (defimplementation unprofile (fname)
1281      (eval `(profile:unprofile ,fname)))
1282    
1283    (defimplementation unprofile-all ()
1284      (profile:unprofile)
1285      "All functions unprofiled.")
1286    
1287    (defimplementation profile-report ()
1288      (profile:report-time))
1289    
1290    (defimplementation profile-reset ()
1291      (profile:reset-time)
1292      "Reset profiling counters.")
1293    
1294    (defimplementation profiled-functions ()
1295      profile:*timed-functions*)
1296    
1297    (defimplementation profile-package (package callers methods)
1298      (profile:profile-all :package package
1299                           :callers-p callers
1300                           :methods methods))
1301    
1302    
1303    ;;;; Multiprocessing
1304    
1305    #+MP
1306    (progn
1307      (defimplementation startup-multiprocessing ()
1308        (setq *swank-in-background* :spawn)
1309        ;; Threads magic: this never returns! But top-level becomes
1310        ;; available again.
1311        (mp::startup-idle-and-top-level-loops))
1312    
1313      (defimplementation spawn (fn &key (name "Anonymous"))
1314        (mp:make-process fn :name name))
1315    
1316      (defimplementation thread-name (thread)
1317        (mp:process-name thread))
1318    
1319      (defimplementation thread-status (thread)
1320        (mp:process-whostate thread))
1321    
1322      (defimplementation current-thread ()
1323        mp:*current-process*)
1324    
1325      (defimplementation all-threads ()
1326        (copy-list mp:*all-processes*))
1327    
1328      (defimplementation interrupt-thread (thread fn)
1329        (mp:process-interrupt thread fn))
1330    
1331      (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
1332    
1333      (defstruct (mailbox (:conc-name mailbox.))
1334        (mutex (mp:make-lock "process mailbox"))
1335        (queue '() :type list))
1336    
1337      (defun mailbox (thread)
1338        "Return THREAD's mailbox."
1339        (mp:with-lock-held (*mailbox-lock*)
1340          (or (getf (mp:process-property-list thread) 'mailbox)
1341              (setf (getf (mp:process-property-list thread) 'mailbox)
1342                    (make-mailbox)))))
1343    
1344      (defimplementation send (thread message)
1345        (let* ((mbox (mailbox thread))
1346               (mutex (mailbox.mutex mbox)))
1347          (mp:with-lock-held (mutex)
1348            (setf (mailbox.queue mbox)
1349                  (nconc (mailbox.queue mbox) (list message))))))
1350    
1351      (defimplementation receive ()
1352        (let* ((mbox (mailbox mp:*current-process*))
1353               (mutex (mailbox.mutex mbox)))
1354          (mp:process-wait "receive" #'mailbox.queue mbox)
1355          (mp:with-lock-held (mutex)
1356            (pop (mailbox.queue mbox)))))
1357    
1358      )
1359    
1360    
1361    ;;;; Epilogue
1362  ;;; Local Variables:  ;;; Local Variables:
1363  ;;; 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))))
1364  ;;; End:  ;;; End:

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

  ViewVC Help
Powered by ViewVC 1.1.5