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

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

  ViewVC Help
Powered by ViewVC 1.1.5