/[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.202 by heller, Fri Oct 17 21:26:53 2008 UTC
# Line 1  Line 1 
1  ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-  ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2    ;;;
3    ;;; License: Public Domain
4    ;;;
5    ;;;; Introduction
6    ;;;
7    ;;; This is the CMUCL implementation of the `swank-backend' package.
8    
9    (in-package :swank-backend)
10    
11    (import-swank-mop-symbols :pcl '(:slot-definition-documentation))
12    
13    (defun swank-mop:slot-definition-documentation (slot)
14      (documentation slot t))
15    
16    ;;;; "Hot fixes"
17    ;;;
18    ;;; Here are necessary bugfixes to the oldest supported version of
19    ;;; CMUCL (currently 18e). Any fixes placed here should also be
20    ;;; submitted to the `cmucl-imp' mailing list and confirmed as
21    ;;; good. When a new release is made that includes the fixes we should
22    ;;; promptly delete them from here. It is enough to be compatible with
23    ;;; the latest release.
24    
25  (declaim (optimize debug))  (in-package :lisp)
26    
27  (in-package :swank)  ;;; `READ-SEQUENCE' with large sequences has problems in 18e. This new
28    ;;; definition works better.
29    
30  ;; Turn on xref. [should we?]  #-cmu19
31  (setf c:*record-xref-info* t)  (progn
32      (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
33        (when s
34          (setf (symbol-value s) nil)))
35    
36      (defun read-into-simple-string (s stream start end)
37        (declare (type simple-string s))
38        (declare (type stream stream))
39        (declare (type index start end))
40        (unless (subtypep (stream-element-type stream) 'character)
41          (error 'type-error
42                 :datum (read-char stream nil #\Null)
43                 :expected-type (stream-element-type stream)
44                 :format-control "Trying to read characters from a binary stream."))
45        ;; Let's go as low level as it seems reasonable.
46        (let* ((numbytes (- end start))
47               (total-bytes 0))
48          ;; read-n-bytes may return fewer bytes than requested, so we need
49          ;; to keep trying.
50          (loop while (plusp numbytes) do
51                (let ((bytes-read (system:read-n-bytes stream s start numbytes nil)))
52                  (when (zerop bytes-read)
53                    (return-from read-into-simple-string total-bytes))
54                  (incf total-bytes bytes-read)
55                  (incf start bytes-read)
56                  (decf numbytes bytes-read)))
57          total-bytes))
58    
59      (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
60        (when s
61          (setf (symbol-value s) t)))
62    
63  (defun without-interrupts* (body)    )
   (sys:without-interrupts (funcall body)))  
64    
65  (defun set-fd-non-blocking (fd)  (in-package :swank-backend)
   (flet ((fcntl (fd cmd arg)  
            (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)  
              (or flags  
                  (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))  
     (let ((flags (fcntl fd unix:F-GETFL 0)))  
       (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))  
66    
67    
68  ;;;; TCP server.  ;;;; TCP server
69    ;;;
70    ;;; In CMUCL we support all communication styles. By default we use
71    ;;; `:SIGIO' because it is the most responsive, but it's somewhat
72    ;;; dangerous: CMUCL is not in general "signal safe", and you don't
73    ;;; know for sure what you'll be interrupting. Both `:FD-HANDLER' and
74    ;;; `:SPAWN' are reasonable alternatives.
75    
76    (defimplementation preferred-communication-style ()
77      :sigio)
78    
79    #-(or darwin mips)
80    (defimplementation create-socket (host port)
81      (let* ((addr (resolve-hostname host))
82             (addr (if (not (find-symbol "SOCKET-ERROR" :ext))
83                       (ext:htonl addr)
84                       addr)))
85        (ext:create-inet-listener port :stream :reuse-address t :host addr)))
86    
87    ;; There seems to be a bug in create-inet-listener on Mac/OSX and Irix.
88    #+(or darwin mips)
89    (defimplementation create-socket (host port)
90      (declare (ignore host))
91      (ext:create-inet-listener port :stream :reuse-address t))
92    
93    (defimplementation local-port (socket)
94      (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
95    
96    (defimplementation close-socket (socket)
97      (let ((fd (socket-fd socket)))
98        (sys:invalidate-descriptor fd)
99        (ext:close-socket fd)))
100    
101    (defimplementation accept-connection (socket &key
102                                          external-format buffering timeout)
103      (declare (ignore timeout))
104      (make-socket-io-stream (ext:accept-tcp-connection socket)
105                             (or buffering :full)
106                             (or external-format :iso-8859-1)))
107    
108    ;;;;; Sockets
109    
110    (defun socket-fd (socket)
111      "Return the filedescriptor for the socket represented by SOCKET."
112      (etypecase socket
113        (fixnum socket)
114        (sys:fd-stream (sys:fd-stream-fd socket))))
115    
116    (defun resolve-hostname (hostname)
117      "Return the IP address of HOSTNAME as an integer (in host byte-order)."
118      (let ((hostent (ext:lookup-host-entry hostname)))
119        (car (ext:host-entry-addr-list hostent))))
120    
121    (defvar *external-format-to-coding-system*
122      '((:iso-8859-1
123         "latin-1" "latin-1-unix" "iso-latin-1-unix"
124         "iso-8859-1" "iso-8859-1-unix")
125        #+unicode
126        (:utf-8 "utf-8" "utf-8-unix")))
127    
128    (defimplementation find-external-format (coding-system)
129      (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
130                      *external-format-to-coding-system*)))
131    
132    (defun make-socket-io-stream (fd buffering external-format)
133      "Create a new input/output fd-stream for FD."
134      #-unicode(declare (ignore external-format))
135      (sys:make-fd-stream fd :input t :output t :element-type 'base-char
136                          :buffering buffering
137                          #+unicode :external-format
138                          #+unicode external-format))
139    
140    ;;;;; Signal-driven I/O
141    
142    (defimplementation install-sigint-handler (function)
143      (sys:enable-interrupt :sigint (lambda (signal code scp)
144                                      (declare (ignore signal code scp))
145                                      (funcall function))))
146    
147    (defvar *sigio-handlers* '()
148      "List of (key . function) pairs.
149    All functions are called on SIGIO, and the key is used for removing
150    specific functions.")
151    
152    (defun set-sigio-handler ()
153      (sys:enable-interrupt :sigio (lambda (signal code scp)
154                                     (sigio-handler signal code scp))))
155    
156    (defun sigio-handler (signal code scp)
157      (declare (ignore signal code scp))
158      (mapc #'funcall (mapcar #'cdr *sigio-handlers*)))
159    
160    (defun fcntl (fd command arg)
161      "fcntl(2) - manipulate a file descriptor."
162      (multiple-value-bind (ok error) (unix:unix-fcntl fd command arg)
163        (cond (ok)
164              (t (error "fcntl: ~A" (unix:get-unix-error-msg error))))))
165    
166    (defimplementation add-sigio-handler (socket fn)
167      (set-sigio-handler)
168      (let ((fd (socket-fd socket)))
169        (fcntl fd unix:f-setown (unix:unix-getpid))
170        (let ((old-flags (fcntl fd unix:f-getfl 0)))
171          (fcntl fd unix:f-setfl (logior old-flags unix:fasync)))
172        (assert (not (assoc fd *sigio-handlers*)))
173        (push (cons fd fn) *sigio-handlers*)))
174    
175    (defimplementation remove-sigio-handlers (socket)
176      (let ((fd (socket-fd socket)))
177        (when (assoc fd *sigio-handlers*)
178          (setf *sigio-handlers* (remove fd *sigio-handlers* :key #'car))
179          (let ((old-flags (fcntl fd unix:f-getfl 0)))
180            (fcntl fd unix:f-setfl (logandc2 old-flags unix:fasync)))
181          (sys:invalidate-descriptor fd))
182        (assert (not (assoc fd *sigio-handlers*)))
183        (when (null *sigio-handlers*)
184          (sys:default-interrupt :sigio))))
185    
186    ;;;;; SERVE-EVENT
187    
188    (defimplementation add-fd-handler (socket fn)
189      (let ((fd (socket-fd socket)))
190        (sys:add-fd-handler fd :input (lambda (_) _ (funcall fn)))))
191    
192    (defimplementation remove-fd-handlers (socket)
193      (sys:invalidate-descriptor (socket-fd socket)))
194    
195    (defimplementation wait-for-input (streams &optional timeout)
196      (assert (member timeout '(nil t)))
197      (loop
198       (let ((ready (remove-if-not #'listen streams)))
199         (when ready (return ready)))
200       (when timeout (return nil))
201       (when (check-slime-interrupts) (return :interrupt))
202       (let* (#+(or)(lisp::*descriptor-handlers* '()) ; ignore other handlers
203              (f (constantly t))
204              (handlers (loop for s in streams
205                              collect (add-one-shot-handler s f))))
206         (unwind-protect
207              (sys:serve-event 0.2)
208           (mapc #'sys:remove-fd-handler handlers)))))
209    
210    (defun add-one-shot-handler (stream function)
211      (let (handler)
212        (setq handler (sys:add-fd-handler (sys:fd-stream-fd stream) :input
213                                          (lambda (fd)
214                                            (declare (ignore fd))
215                                            (sys:remove-fd-handler handler)
216                                            (funcall function stream))))))
217    
218    
 (defun resolve-hostname (name)  
   (let* ((hostent (ext:lookup-host-entry name))  
          (address (car (ext:host-entry-addr-list hostent))))  
     (ext:htonl address)))  
   
 (defun create-swank-server (port &key (reuse-address t)  
                             (address "localhost"))  
   "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))  
219    
220    
221  ;;;; Stream handling  ;;;; Stream handling
222    ;;; XXX: How come we don't use Gray streams in CMUCL too? -luke (15/May/2004)
223    
224    (defimplementation make-output-stream (write-string)
225      (make-slime-output-stream write-string))
226    
227    (defimplementation make-input-stream (read-string)
228      (make-slime-input-stream read-string))
229    
230  (defstruct (slime-output-stream  (defstruct (slime-output-stream
231               (:include lisp::lisp-stream               (:include lisp::lisp-stream
232                         (lisp::misc #'sos/misc)                         (lisp::misc #'sos/misc)
233                         (lisp::out #'sos/out)                         (lisp::out #'sos/write-char)
234                         (lisp::sout #'sos/sout))                         (lisp::sout #'sos/write-string))
235               (:conc-name sos.)               (:conc-name sos.)
236               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
237    (buffer (make-string 512) :type string)               (:constructor make-slime-output-stream (output-fn)))
238      (output-fn nil :type function)
239      (buffer (make-string 4000) :type string)
240    (index 0 :type kernel:index)    (index 0 :type kernel:index)
241    (column 0 :type kernel:index))    (column 0 :type kernel:index))
242    
# Line 102  The request is read from the socket as a Line 244  The request is read from the socket as a
244    (declare (ignore d))    (declare (ignore d))
245    (print-unreadable-object (s stream :type t :identity t)))    (print-unreadable-object (s stream :type t :identity t)))
246    
247  (defun sos/out (stream char)  (defun sos/write-char (stream char)
248    (let ((buffer (sos.buffer stream))    (let ((pending-output nil))
249          (index (sos.index stream)))      (system:without-interrupts
250      (setf (schar buffer index) char)        (let ((buffer (sos.buffer stream))
251      (setf (sos.index stream) (1+ index))              (index (sos.index stream)))
252      (incf (sos.column stream))          (setf (schar buffer index) char)
253      (when (char= #\newline char)          (setf (sos.index stream) (1+ index))
254        (setf (sos.column stream) 0))          (incf (sos.column stream))
255      (when (= index (1- (length buffer)))          (when (char= #\newline char)
256        (force-output stream)))            (setf (sos.column stream) 0)
257              #+(or)(setq pending-output (sos/reset-buffer stream))
258              )
259            (when (= index (1- (length buffer)))
260              (setq pending-output (sos/reset-buffer stream)))))
261        (when pending-output
262          (funcall (sos.output-fn stream) pending-output)))
263    char)    char)
264    
265  (defun sos/sout (stream string start end)  (defun sos/write-string (stream string start end)
266    (loop for i from start below end    (loop for i from start below end
267          do (sos/out stream (aref string i))))          do (sos/write-char stream (aref string i))))
268    
269    (defun sos/flush (stream)
270      (let ((string (sos/reset-buffer stream)))
271        (when string
272          (funcall (sos.output-fn stream) string))
273        nil))
274    
275    (defun sos/reset-buffer (stream)
276      (system:without-interrupts
277        (let ((end (sos.index stream)))
278          (unless (zerop end)
279            (prog1 (subseq (sos.buffer stream) 0 end)
280              (setf (sos.index stream) 0))))))
281    
282  (defun sos/misc (stream operation &optional arg1 arg2)  (defun sos/misc (stream operation &optional arg1 arg2)
283    (declare (ignore arg1 arg2))    (declare (ignore arg1 arg2))
284    (case operation    (case operation
285      ((:force-output :finish-output)      ((:force-output :finish-output) (sos/flush stream))
      (let ((end (sos.index stream)))  
        (unless (zerop end)  
          (send-to-emacs `(:read-output ,(subseq (sos.buffer stream) 0 end)))  
          (setf (sos.index stream) 0))))  
286      (:charpos (sos.column stream))      (:charpos (sos.column stream))
287      (:line-length 75)      (:line-length 75)
288      (:file-position nil)      (:file-position nil)
# Line 135  The request is read from the socket as a Line 292  The request is read from the socket as a
292      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
293    
294  (defstruct (slime-input-stream  (defstruct (slime-input-stream
295               (:include string-stream               (:include string-stream
296                         (lisp::in #'sis/in)                         (lisp::in #'sis/in)
297                         (lisp::misc #'sis/misc))                         (lisp::misc #'sis/misc))
298               (:conc-name sis.)               (:conc-name sis.)
299               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
300    (buffer "" :type string)               (:constructor make-slime-input-stream (input-fn)))
301    (index 0 :type kernel:index))    (input-fn nil :type function)
302      (buffer   ""  :type string)
303      (index    0   :type kernel:index))
304    
305  (defun sis/in (stream eof-errorp eof-value)  (defun sis/in (stream eof-errorp eof-value)
   (declare (ignore eof-errorp eof-value))  
306    (let ((index (sis.index stream))    (let ((index (sis.index stream))
307          (buffer (sis.buffer stream)))          (buffer (sis.buffer stream)))
308      (when (= index (length buffer))      (when (= index (length buffer))
309        (setf buffer (slime-read-string))        (let ((string (funcall (sis.input-fn stream))))
310        (setf (sis.buffer stream) buffer)          (cond ((zerop (length string))
311        (setf index 0))                 (return-from sis/in
312                     (if eof-errorp
313                         (error (make-condition 'end-of-file :stream stream))
314                         eof-value)))
315                  (t
316                   (setf buffer string)
317                   (setf (sis.buffer stream) buffer)
318                   (setf index 0)))))
319      (prog1 (aref buffer index)      (prog1 (aref buffer index)
320        (setf (sis.index stream) (1+ index)))))        (setf (sis.index stream) (1+ index)))))
321    
# Line 162  The request is read from the socket as a Line 327  The request is read from the socket as a
327      (:unread (setf (aref (sis.buffer stream)      (:unread (setf (aref (sis.buffer stream)
328                           (decf (sis.index stream)))                           (decf (sis.index stream)))
329                     arg1))                     arg1))
330      (:clear-input (setf (sis.index stream) 0      (:clear-input
331         (setf (sis.index stream) 0
332                          (sis.buffer stream) ""))                          (sis.buffer stream) ""))
333      (:listen (< (sis.index stream) (length (sis.buffer stream))))      (:listen (< (sis.index stream) (length (sis.buffer stream))))
334      (:charpos nil)      (:charpos nil)
335      (:line-length nil)      (:line-length nil)
336      (:get-command nil)      (:get-command nil)
337      (:element-type 'base-char)      (:element-type 'base-char)
338      (:close nil)))      (:close nil)
339        (:interactive-p t)))
340    
341    
342  ;;;; Compilation Commands  ;;;; Compilation Commands
343    
 (defvar *swank-source-info* nil  
   "Bound to a SOURCE-INFO object during compilation.")  
   
344  (defvar *previous-compiler-condition* nil  (defvar *previous-compiler-condition* nil
345    "Used to detect duplicates.")    "Used to detect duplicates.")
346    
347  (defvar *previous-context* nil  (defvar *previous-context* nil
348    "Previous compiler error context.")    "Previous compiler error context.")
349    
350  (defvar *compiler-notes* '()  (defvar *buffer-name* nil
351    "List of compiler notes for the last compilation unit.")    "The name of the Emacs buffer we are compiling from.
352    NIL if we aren't compiling from a buffer.")
353    
 (defvar *buffer-name* nil)  
354  (defvar *buffer-start-position* nil)  (defvar *buffer-start-position* nil)
355  (defvar *buffer-substring* nil)  (defvar *buffer-substring* nil)
 (defvar *compile-filename* nil)  
356    
357    (defimplementation call-with-compilation-hooks (function)
358      (let ((*previous-compiler-condition* nil)
359            (*previous-context* nil)
360            (*print-readably* nil))
361        (handler-bind ((c::compiler-error #'handle-notification-condition)
362                       (c::style-warning  #'handle-notification-condition)
363                       (c::warning        #'handle-notification-condition))
364          (funcall function))))
365    
366    (defimplementation swank-compile-file (filename load-p external-format)
367      (declare (ignore external-format))
368      (clear-xref-info filename)
369      (with-compilation-hooks ()
370        (let ((*buffer-name* nil)
371              (ext:*ignore-extra-close-parentheses* nil))
372          (multiple-value-bind (output-file warnings-p failure-p)
373              (compile-file filename)
374            (values output-file warnings-p
375                    (or failure-p
376                        (when load-p
377                          ;; Cache the latest source file for definition-finding.
378                          (source-cache-get filename (file-write-date filename))
379                          (not (load output-file)))))))))
380    
381    (defimplementation swank-compile-string (string &key buffer position directory
382                                                    debug)
383      (declare (ignore directory debug))
384      (with-compilation-hooks ()
385        (let ((*buffer-name* buffer)
386              (*buffer-start-position* position)
387              (*buffer-substring* string)
388              (source-info (list :emacs-buffer buffer
389                                 :emacs-buffer-offset position
390                                 :emacs-buffer-string string)))
391          (with-input-from-string (stream string)
392            (let ((failurep (ext:compile-from-stream stream :source-info
393                                                    source-info)))
394              (not failurep))))))
395    
396    
397  ;;;;; Trapping notes  ;;;;; Trapping notes
398    ;;;
399    ;;; We intercept conditions from the compiler and resignal them as
400    ;;; `SWANK:COMPILER-CONDITION's.
401    
402  (defun handle-notification-condition (condition)  (defun handle-notification-condition (condition)
403    "Handle a condition caused by a compiler warning.    "Handle a condition caused by a compiler warning."
 This traps all compiler conditions at a lower-level than using  
 C:*COMPILER-NOTIFICATION-FUNCTION*. The advantage is that we get to  
 craft our own error messages, which can omit a lot of redundant  
 information."  
404    (unless (eq condition *previous-compiler-condition*)    (unless (eq condition *previous-compiler-condition*)
405      (let ((context (or (c::find-error-context nil) *previous-context*)))      (let ((context (c::find-error-context nil)))
406        (setq *previous-compiler-condition* condition)        (setq *previous-compiler-condition* condition)
407        (setq *previous-context* context)        (setq *previous-context* context)
408        (signal-compiler-condition condition context))))        (signal-compiler-condition condition context))))
# Line 211  information." Line 412  information."
412             'compiler-condition             'compiler-condition
413             :original-condition condition             :original-condition condition
414             :severity (severity-for-emacs condition)             :severity (severity-for-emacs condition)
415             :message (brief-compiler-message-for-emacs condition context)             :short-message (brief-compiler-message-for-emacs condition)
416             :location (compiler-note-location context))))             :message (long-compiler-message-for-emacs condition context)
417               :location (if (read-error-p condition)
418                             (read-error-location condition)
419                             (compiler-note-location context)))))
420    
421  (defun severity-for-emacs (condition)  (defun severity-for-emacs (condition)
422    "Return the severity of CONDITION."    "Return the severity of CONDITION."
423    (etypecase condition    (etypecase condition
424        ((satisfies read-error-p) :read-error)
425      (c::compiler-error :error)      (c::compiler-error :error)
426      (c::style-warning :note)      (c::style-warning :note)
427      (c::warning :warning)))      (c::warning :warning)))
428    
429  (defun brief-compiler-message-for-emacs (condition error-context)  (defun read-error-p (condition)
430      (eq (type-of condition) 'c::compiler-read-error))
431    
432    (defun brief-compiler-message-for-emacs (condition)
433    "Briefly describe a compiler error for Emacs.    "Briefly describe a compiler error for Emacs.
434  When Emacs presents the message it already has the source popped up  When Emacs presents the message it already has the source popped up
435  and the source form highlighted. This makes much of the information in  and the source form highlighted. This makes much of the information in
436  the error-context redundant."  the error-context redundant."
437      (princ-to-string condition))
438    
439    (defun long-compiler-message-for-emacs (condition error-context)
440      "Describe a compiler error for Emacs including context information."
441    (declare (type (or c::compiler-error-context null) error-context))    (declare (type (or c::compiler-error-context null) error-context))
442    (multiple-value-bind (enclosing source)    (multiple-value-bind (enclosing source)
443        (if error-context        (if error-context
# Line 234  the error-context redundant." Line 446  the error-context redundant."
446      (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"      (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"
447              enclosing source condition)))              enclosing source condition)))
448    
449    (defun read-error-location (condition)
450      (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
451             (file (c::file-info-name finfo))
452             (pos (c::compiler-read-error-position condition)))
453        (cond ((and (eq file :stream) *buffer-name*)
454               (make-location (list :buffer *buffer-name*)
455                              (list :offset *buffer-start-position* pos)))
456              ((and (pathnamep file) (not *buffer-name*))
457               (make-location (list :file (unix-truename file))
458                              (list :position (1+ pos))))
459              (t (break)))))
460    
461  (defun compiler-note-location (context)  (defun compiler-note-location (context)
462    (cond (context    "Derive the location of a complier message from its context.
463           (resolve-note-location  Return a `location' record, or (:error REASON) on failure."
464            *buffer-name*    (if (null context)
465            (c::compiler-error-context-file-name context)        (note-error-location)
466            (c::compiler-error-context-file-position context)        (let ((file (c::compiler-error-context-file-name context))
467            (reverse (c::compiler-error-context-original-source-path context))              (source (c::compiler-error-context-original-source context))
468            (c::compiler-error-context-original-source context)))              (path
469          (t               (reverse (c::compiler-error-context-original-source-path context))))
470           (resolve-note-location *buffer-name* nil nil nil nil))))          (or (locate-compiler-note file source path)
471                (note-error-location)))))
472  (defgeneric resolve-note-location (buffer file-name file-position  
473                                            source-path source))  (defun note-error-location ()
474      "Pseudo-location for notes that can't be located."
475  (defmethod resolve-note-location ((b (eql nil)) (f pathname) pos path source)    (list :error "No error location available."))
476    (make-location  
477     `(:file ,(unix-truename f))  (defun locate-compiler-note (file source source-path)
478     `(:position ,(1+ (source-path-file-position path f)))))    (cond ((and (eq file :stream) *buffer-name*)
479             ;; Compiling from a buffer
480  (defmethod resolve-note-location ((b string) (f (eql :stream)) pos path source)           (make-location (list :buffer *buffer-name*)
481    (make-location                          (list :offset *buffer-start-position*
482     `(:buffer ,b)                                (source-path-string-position
483     `(:position ,(+ *buffer-start-position*                                 source-path *buffer-substring*))))
484                     (source-path-string-position path *buffer-substring*)))))          ((and (pathnamep file) (null *buffer-name*))
485             ;; Compiling from a file
486  (defmethod resolve-note-location (b (f (eql :lisp)) pos path (source string))           (make-location (list :file (unix-truename file))
487    (make-location                          (list :position (1+ (source-path-file-position
488     `(:source-form ,source)                                               source-path file)))))
489     `(:position 1)))          ((and (eq file :lisp) (stringp source))
490             ;; No location known, but we have the source form.
491  (defmethod resolve-note-location (buffer           ;; XXX How is this case triggered?  -luke (16/May/2004)
492                                    (file (eql nil))           ;; This can happen if the compiler needs to expand a macro
493                                    (pos (eql nil))           ;; but the macro-expander is not yet compiled.  Calling the
494                                    (path (eql nil))           ;; (interpreted) macro-expander triggers IR1 conversion of
495                                    (source (eql nil)))           ;; the lambda expression for the expander and invokes the
496    (cond (buffer           ;; compiler recursively.
497           (make-location (list :buffer buffer)           (make-location (list :source-form source)
498                          (list :position *buffer-start-position*)))                          (list :position 1)))))
         (*compile-file-truename*  
          (make-location (list :file (namestring *compile-file-truename*))  
                         (list :position 0)))  
         (*compile-filename*  
          (make-location (list :file *compile-filename*) (list :position 0)))  
         (t  
          (list :error "No error location available"))))  
   
 (defmacro with-compilation-hooks (() &body body)  
   "Execute BODY and record the set of compiler notes."  
   `(let ((*previous-compiler-condition* nil)  
          (*previous-context* nil)  
          (*print-readably* nil))  
     (handler-bind ((c::compiler-error #'handle-notification-condition)  
                    (c::style-warning  #'handle-notification-condition)  
                    (c::warning        #'handle-notification-condition))  
       ,@body)))  
499    
500  (defmethod compile-file-for-emacs (filename load-p)  (defun unix-truename (pathname)
501    (clear-xref-info filename)    (ext:unix-namestring (truename pathname)))
   (with-compilation-hooks ()  
     (let ((*buffer-name* nil)  
           (*compile-filename* filename))  
         (compile-file filename :load load-p))))  
   
 (defmethod compile-string-for-emacs (string &key buffer position)  
   (with-compilation-hooks ()  
     (let ((*package* *buffer-package*)  
           (*compile-filename* nil)  
           (*buffer-name* buffer)  
           (*buffer-start-position* position)  
           (*buffer-substring* string))  
       (with-input-from-string (stream string)  
         (ext:compile-from-stream  
          stream  
          :source-info `(:emacs-buffer ,buffer  
                         :emacs-buffer-offset ,position  
                         :emacs-buffer-string ,string))))))  
502    
503    
504  ;;;; XREF  ;;;; XREF
505    ;;;
506    ;;; Cross-reference support is based on the standard CMUCL `XREF'
507    ;;; package. This package has some caveats: XREF information is
508    ;;; recorded during compilation and not preserved in fasl files, and
509    ;;; XREF recording is disabled by default. Redefining functions can
510    ;;; also cause duplicate references to accumulate, but
511    ;;; `swank-compile-file' will automatically clear out any old records
512    ;;; from the same filename.
513    ;;;
514    ;;; To enable XREF recording, set `c:*record-xref-info*' to true. To
515    ;;; clear out the XREF database call `xref:init-xref-database'.
516    
517  (defun lookup-xrefs (finder name)  (defmacro defxref (name function)
518    (xref-results-for-emacs (funcall finder (from-string name))))    `(defimplementation ,name (name)
519        (xref-results (,function name))))
520  (defslimefun who-calls (function-name)  
521    "Return the places where FUNCTION-NAME is called."  (defxref who-calls      xref:who-calls)
522    (lookup-xrefs #'xref:who-calls function-name))  (defxref who-references xref:who-references)
523    (defxref who-binds      xref:who-binds)
524  (defslimefun who-references (variable)  (defxref who-sets       xref:who-sets)
   "Return the places where the global variable VARIABLE is referenced."  
   (lookup-xrefs #'xref:who-references variable))  
   
 (defslimefun who-binds (variable)  
   "Return the places where the global variable VARIABLE is bound."  
   (lookup-xrefs #'xref:who-binds variable))  
   
 (defslimefun who-sets (variable)  
   "Return the places where the global variable VARIABLE is set."  
   (lookup-xrefs #'xref:who-sets variable))  
525    
526    ;;; More types of XREF information were added since 18e:
527    ;;;
528  #+cmu19  #+cmu19
529  (progn  (progn
530    (defslimefun who-macroexpands (macro)    (defxref who-macroexpands xref:who-macroexpands)
531      "Return the places where MACRO is expanded."    ;; XXX
532      (lookup-xrefs #'xref:who-macroexpands macro))    (defimplementation who-specializes (symbol)
533        (let* ((methods (xref::who-specializes (find-class symbol)))
534    (defslimefun who-specializes (class)             (locations (mapcar #'method-location methods)))
535      "Return the methods with specializers for CLASS."        (mapcar #'list methods locations))))
536      (let* ((methods (xref::who-specializes (find-class (from-string class))))  
537             (locations (mapcar #'method-source-location methods)))  (defun xref-results (contexts)
538        (group-xrefs (mapcar (lambda (m l)    (mapcar (lambda (xref)
539                               (cons (let ((*print-pretty* nil))              (list (xref:xref-context-name xref)
540                                       (to-string m))                    (resolve-xref-location xref)))
541                                     l))            contexts))
                            methods locations))))  
   )  
542    
543  (defun resolve-xref-location (xref)  (defun resolve-xref-location (xref)
544    (let ((name (xref:xref-context-name xref))    (let ((name (xref:xref-context-name xref))
# Line 363  the error-context redundant." Line 552  the error-context redundant."
552             (make-location (list :file (unix-truename file))             (make-location (list :file (unix-truename file))
553                            (list :function-name (string name))))                            (list :function-name (string name))))
554            (t            (t
555             `(:error ,(format nil "Unkown source location: ~S ~S ~S "             `(:error ,(format nil "Unknown source location: ~S ~S ~S "
556                               name file source-path))))))                               name file source-path))))))
557    
   
 (defun xref-results-for-emacs (contexts)  
   "Prepare a list of xref contexts for Emacs.  
 The result is a list of xrefs:  
 group       ::= (FILENAME . ({reference}+))  
 reference   ::= (FUNCTION-SPECIFIER . SOURCE-LOCATION)"  
   (let ((xrefs '()))  
     (dolist (cxt contexts)  
       (let ((name (xref:xref-context-name cxt)))  
         (push (cons (to-string name)  
                     (resolve-xref-location cxt))  
               xrefs)))  
     (group-xrefs xrefs)))  
   
   
 (defun location-buffer= (location1 location2)  
   (equalp location1 location2))  
   
 (defun file-xrefs-for-emacs (unix-filename contexts)  
   "Return a summary of the references from a particular file.  
 The result is a list of the form (FILENAME ((REFERRER SOURCE-PATH) ...))"  
   (list unix-filename  
         (loop for context in (sort-contexts-by-source-path contexts)  
               collect (list (let ((*print-pretty* nil))  
                               (to-string (xref:xref-context-name context)))  
                             (xref:xref-context-source-path context)))))  
   
 (defun sort-contexts-by-source-path (contexts)  
   "Sort xref contexts by lexical position of source-paths.  
 It is assumed that all contexts belong to the same file."  
   (sort contexts #'source-path< :key #'xref:xref-context-source-path))  
   
 (defun source-path< (path1 path2)  
   "Return true if PATH1 is lexically before PATH2."  
   (and (every #'< path1 path2)  
        (< (length path1) (length path2))))  
   
558  (defun clear-xref-info (namestring)  (defun clear-xref-info (namestring)
559    "Clear XREF notes pertaining to FILENAME.    "Clear XREF notes pertaining to NAMESTRING.
560  This is a workaround for a CMUCL bug: XREF records are cumulative."  This is a workaround for a CMUCL bug: XREF records are cumulative."
561    (let ((filename (parse-namestring namestring)))    (when c:*record-xref-info*
562      (when c:*record-xref-info*      (let ((filename (truename namestring)))
563        (dolist (db (list xref::*who-calls*        (dolist (db (list xref::*who-calls*
564                          #+cmu19 xref::*who-is-called*                          #+cmu19 xref::*who-is-called*
565                          #+cmu19 xref::*who-macroexpands*                          #+cmu19 xref::*who-macroexpands*
# Line 415  This is a workaround for a CMUCL bug: XR Line 567  This is a workaround for a CMUCL bug: XR
567                          xref::*who-binds*                          xref::*who-binds*
568                          xref::*who-sets*))                          xref::*who-sets*))
569          (maphash (lambda (target contexts)          (maphash (lambda (target contexts)
570                       ;; XXX update during traversal?
571                     (setf (gethash target db)                     (setf (gethash target db)
572                           (delete-if                           (delete filename contexts
573                            (lambda (ctx)                                   :key #'xref:xref-context-file
574                              (xref-context-derived-from-p ctx filename))                                   :test #'equalp)))
                           contexts)))  
575                   db)))))                   db)))))
576    
 (defun xref-context-derived-from-p (context filename)  
   (let ((xref-file (xref:xref-context-file context)))  
     (and xref-file (pathname= filename xref-file))))  
   
 (defun pathname= (&rest pathnames)  
   "True if PATHNAMES refer to the same file."  
   (apply #'string= (mapcar #'unix-truename pathnames)))  
   
 (defun unix-truename (pathname)  
   (ext:unix-namestring (truename pathname)))  
   
577    
578  ;;;; Find callers and callees  ;;;; Find callers and callees
579    ;;;
580  ;;; Find callers and callees by looking at the constant pool of  ;;; Find callers and callees by looking at the constant pool of
581  ;;; compiled code objects.  We assume every fdefn object in the  ;;; compiled code objects.  We assume every fdefn object in the
582  ;;; constant pool corresponds to a call to that function.  A better  ;;; constant pool corresponds to a call to that function.  A better
583  ;;; strategy would be to use the disassembler to find actual  ;;; strategy would be to use the disassembler to find actual
584  ;;; call-sites.  ;;; call-sites.
585    
586  (declaim (inline map-code-constants))  (labels ((make-stack () (make-array 100 :fill-pointer 0 :adjustable t))
587  (defun map-code-constants (code fn)           (map-cpool (code fun)
588    "Call FN for each constant in CODE's constant pool."             (declare (type kernel:code-component code) (type function fun))
589    (check-type code kernel:code-component)             (loop for i from vm:code-constants-offset
590    (loop for i from vm:code-constants-offset below (kernel:get-header-data code)                   below (kernel:get-header-data code)
591          do (funcall fn (kernel:code-header-ref code i))))                   do (funcall fun (kernel:code-header-ref code i))))
592    
593  (defun function-callees (function)           (callees (fun)
594    "Return FUNCTION's callees as a list of functions."             (let ((callees (make-stack)))
595    (let ((callees '()))               (map-cpool (vm::find-code-object fun)
596      (map-code-constants                          (lambda (o)
597       (vm::find-code-object function)                            (when (kernel:fdefn-p o)
598       (lambda (obj)                              (vector-push-extend (kernel:fdefn-function o)
599         (when (kernel:fdefn-p obj)                                                  callees))))
600           (push (kernel:fdefn-function obj) callees))))               (coerce callees 'list)))
601      callees))  
602             (callers (fun)
603  (declaim (ext:maybe-inline map-allocated-code-components))             (declare (function fun))
604  (defun map-allocated-code-components (spaces fn)             (let ((callers (make-stack)))
605    "Call FN for each allocated code component in one of SPACES.  FN               (ext:gc :full t)
606  receives the object as argument.  SPACES should be a list of the               ;; scan :dynamic first to avoid the need for even more gcing
607  symbols :dynamic, :static, or :read-only."               (dolist (space '(:dynamic :read-only :static))
608    (dolist (space spaces)                 (vm::map-allocated-objects
609      (declare (inline vm::map-allocated-objects))                  (lambda (obj header size)
610      (vm::map-allocated-objects                    (declare (type fixnum header) (ignore size))
611       (lambda (obj header size)                    (when (= vm:code-header-type header)
612         (declare (type fixnum size) (ignore size))                      (map-cpool obj
613         (when (= vm:code-header-type header)                                 (lambda (c)
614           (funcall fn obj)))                                   (when (and (kernel:fdefn-p c)
615       space)))                                              (eq (kernel:fdefn-function c) fun))
616                                       (vector-push-extend obj callers))))))
617  (declaim (ext:maybe-inline map-caller-code-components))                  space)
618  (defun map-caller-code-components (function spaces fn)                 (ext:gc))
619    "Call FN for each code component with a fdefn for FUNCTION in its               (coerce callers 'list)))
620  constant pool."  
621    (let ((function (coerce function 'function)))           (entry-points (code)
622      (declare (inline map-allocated-code-components))             (loop for entry = (kernel:%code-entry-points code)
623      (map-allocated-code-components                   then (kernel::%function-next entry)
624       spaces                   while entry
625       (lambda (obj)                   collect entry))
626         (map-code-constants  
627          obj           (guess-main-entry-point (entry-points)
628          (lambda (constant)             (or (find-if (lambda (fun)
629            (when (and (kernel:fdefn-p constant)                            (ext:valid-function-name-p
630                       (eq (kernel:fdefn-function constant)                             (kernel:%function-name fun)))
631                           function))                          entry-points)
632              (funcall fn obj))))))))                 (car entry-points)))
633    
634  (defun function-callers (function &optional (spaces '(:read-only :static           (fun-dspec (fun)
635                                                        :dynamic)))             (list (kernel:%function-name fun) (function-location fun)))
636    "Return FUNCTION's callers.  The result is a list of code-objects."  
637    (let ((referrers '()))           (code-dspec (code)
638      (declare (inline map-caller-code-components))             (let ((eps (entry-points code))
639      (ext:gc :full t)                   (di (kernel:%code-debug-info code)))
640      (map-caller-code-components function spaces               (cond (eps (fun-dspec (guess-main-entry-point eps)))
641                                  (lambda (code) (push code referrers)))                     (di (list (c::debug-info-name di)
642      referrers))                               (debug-info-function-name-location di)))
643                       (t (list (princ-to-string code)
644  (defun debug-info-definitions (debug-info)                              `(:error "No src-loc available")))))))
645    "Return the defintions for a debug-info.  This should only be used    (declare (inline map-cpool))
646  for code-object without entry points, i.e., byte compiled  
647  code (are theree others?)"    (defimplementation list-callers (symbol)
648    ;; This mess has only been tested with #'ext::skip-whitespace, a      (mapcar #'code-dspec (callers (coerce symbol 'function) )))
649    ;; byte-compiled caller of #'read-char .  
650    (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))    (defimplementation list-callees (symbol)
651    (let ((name (c::debug-info-name debug-info))      (mapcar #'fun-dspec (callees symbol))))
652          (source (c::debug-info-source debug-info)))  
653      (destructuring-bind (first) source  (defun test-list-callers (count)
654        (ecase (c::debug-source-from first)    (let ((funsyms '()))
655          (:file      (do-all-symbols (s)
656           (list        (when (and (fboundp s)
657            (cons name                   (functionp (symbol-function s))
658                  (make-location                   (not (macro-function s))
659                   (list :file (unix-truename (c::debug-source-name first)))                   (not (special-operator-p s)))
660                   (list :function-name name)))))))))          (push s funsyms)))
661        (let ((len (length funsyms)))
662  (defun code-component-entry-points (code)        (dotimes (i count)
663    "Return a list ((NAME . LOCATION) ...) of function definitons for          (let ((sym (nth (random len) funsyms)))
664  the code omponent CODE."            (format t "~s -> ~a~%" sym (mapcar #'car (list-callers sym))))))))
   (delete-duplicates  
    (loop for e = (kernel:%code-entry-points code)  
          then (kernel::%function-next e)  
          while e  
          collect (cons (to-string (kernel:%function-name e))  
                        (function-source-location e)))  
    :test #'equal))  
   
 (defslimefun list-callers (symbol-name)  
   "Return a list ((FILE . ((NAME . LOCATION) ...)) ...) of callers."  
   (let ((components (function-callers (from-string symbol-name)))  
         (xrefs '()))  
     (dolist (code components)  
       (let* ((entry (kernel:%code-entry-points code))  
              (defs (if entry  
                        (code-component-entry-points code)  
                        ;; byte compiled stuff  
                        (debug-info-definitions  
                         (kernel:%code-debug-info code)))))  
         (setq xrefs (nconc defs xrefs))))  
     (group-xrefs xrefs)))  
   
   
 (defslimefun list-callees (symbol-name)  
   (let ((fns (function-callees (from-string symbol-name))))  
     (group-xrefs (mapcar (lambda (fn)  
                            (cons (to-string (kernel:%function-name fn))  
                                  (function-source-location fn)))  
                          fns))))  
665    
666  ;;;; Definitions  ;; (test-list-callers 100)
667    
668    
669    ;;;; Resolving source locations
670    ;;;
671    ;;; Our mission here is to "resolve" references to code locations into
672    ;;; actual file/buffer names and character positions. The references
673    ;;; we work from come out of the compiler's statically-generated debug
674    ;;; information, such as `code-location''s and `debug-source''s. For
675    ;;; more details, see the "Debugger Programmer's Interface" section of
676    ;;; the CMUCL manual.
677    ;;;
678    ;;; The first step is usually to find the corresponding "source-path"
679    ;;; for the location. Once we have the source-path we can pull up the
680    ;;; source file and `READ' our way through to the right position. The
681    ;;; main source-code groveling work is done in
682    ;;; `swank-source-path-parser.lisp'.
683    
684  (defvar *debug-definition-finding* t  (defvar *debug-definition-finding* nil
685    "When true don't handle errors while looking for definitions.    "When true don't handle errors while looking for definitions.
686  This is useful when debugging the definition-finding code.")  This is useful when debugging the definition-finding code.")
687    
688    (defvar *source-snippet-size* 256
689      "Maximum number of characters in a snippet of source code.
690    Snippets at the beginning of definitions are used to tell Emacs what
691    the definitions looks like, so that it can accurately find them by
692    text search.")
693    
694  (defmacro safe-definition-finding (&body body)  (defmacro safe-definition-finding (&body body)
695    "Execute BODY ignoring errors.  Return a the source location    "Execute BODY and return the source-location it returns.
696  returned by BODY or if an error occurs a description of the error.  If an error occurs and `*debug-definition-finding*' is false, then
697  The second return value is the condition or nil."  return an error pseudo-location.
698    
699    The second return value is NIL if no error occurs, otherwise it is the
700    condition object."
701    `(flet ((body () ,@body))    `(flet ((body () ,@body))
702      (if *debug-definition-finding*      (if *debug-definition-finding*
703          (body)          (body)
704          (handler-case (values (progn ,@body) nil)          (handler-case (values (progn ,@body) nil)
705            (error (c) (values (list :error (princ-to-string c)) c))))))            (error (c) (values `(:error ,(trim-whitespace (princ-to-string c)))
706                                 c))))))
707    
708    (defun trim-whitespace (string)
709      (string-trim #(#\newline #\space #\tab) string))
710    
711    (defun code-location-source-location (code-location)
712      "Safe wrapper around `code-location-from-source-location'."
713      (safe-definition-finding
714       (source-location-from-code-location code-location)))
715    
716    (defun source-location-from-code-location (code-location)
717      "Return the source location for CODE-LOCATION."
718      (let ((debug-fun (di:code-location-debug-function code-location)))
719        (when (di::bogus-debug-function-p debug-fun)
720          ;; Those lousy cheapskates! They've put in a bogus debug source
721          ;; because the code was compiled at a low debug setting.
722          (error "Bogus debug function: ~A" debug-fun)))
723      (let* ((debug-source (di:code-location-debug-source code-location))
724             (from (di:debug-source-from debug-source))
725             (name (di:debug-source-name debug-source)))
726        (ecase from
727          (:file
728           (location-in-file name code-location debug-source))
729          (:stream
730           (location-in-stream code-location debug-source))
731          (:lisp
732           ;; The location comes from a form passed to `compile'.
733           ;; The best we can do is return the form itself for printing.
734           (make-location
735            (list :source-form (with-output-to-string (*standard-output*)
736                                 (debug::print-code-location-source-form
737                                  code-location 100 t)))
738            (list :position 1))))))
739    
740    (defun location-in-file (filename code-location debug-source)
741      "Resolve the source location for CODE-LOCATION in FILENAME."
742      (let* ((code-date (di:debug-source-created debug-source))
743             (source-code (get-source-code filename code-date)))
744        (with-input-from-string (s source-code)
745          (make-location (list :file (unix-truename filename))
746                         (list :position (1+ (code-location-stream-position
747                                              code-location s)))
748                         `(:snippet ,(read-snippet s))))))
749    
750    (defun location-in-stream (code-location debug-source)
751      "Resolve the source location for a CODE-LOCATION from a stream.
752    This only succeeds if the code was compiled from an Emacs buffer."
753      (unless (debug-source-info-from-emacs-buffer-p debug-source)
754        (error "The code is compiled from a non-SLIME stream."))
755      (let* ((info (c::debug-source-info debug-source))
756             (string (getf info :emacs-buffer-string))
757             (position (code-location-string-offset
758                        code-location
759                        string)))
760        (make-location
761         (list :buffer (getf info :emacs-buffer))
762         (list :offset (getf info :emacs-buffer-offset) position)
763         (list :snippet (with-input-from-string (s string)
764                          (file-position s position)
765                          (read-snippet s))))))
766    
767    ;;;;; Function-name locations
768    ;;;
769    (defun debug-info-function-name-location (debug-info)
770      "Return a function-name source-location for DEBUG-INFO.
771    Function-name source-locations are a fallback for when precise
772    positions aren't available."
773      (with-struct (c::debug-info- (fname name) source) debug-info
774        (with-struct (c::debug-source- info from name) (car source)
775          (ecase from
776            (:file
777             (make-location (list :file (namestring (truename name)))
778                            (list :function-name (string fname))))
779            (:stream
780             (assert (debug-source-info-from-emacs-buffer-p (car source)))
781             (make-location (list :buffer (getf info :emacs-buffer))
782                            (list :function-name (string fname))))
783            (:lisp
784             (make-location (list :source-form (princ-to-string (aref name 0)))
785                            (list :position 1)))))))
786    
787    (defun debug-source-info-from-emacs-buffer-p (debug-source)
788      "Does the `info' slot of DEBUG-SOURCE contain an Emacs buffer location?
789    This is true for functions that were compiled directly from buffers."
790      (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
791    
792    (defun info-from-emacs-buffer-p (info)
793      (and info
794           (consp info)
795           (eq :emacs-buffer (car info))))
796    
797    
798    ;;;;; Groveling source-code for positions
799    
800    (defun code-location-stream-position (code-location stream)
801      "Return the byte offset of CODE-LOCATION in STREAM.  Extract the
802    toplevel-form-number and form-number from CODE-LOCATION and use that
803    to find the position of the corresponding form.
804    
805    Finish with STREAM positioned at the start of the code location."
806      (let* ((location (debug::maybe-block-start-location code-location))
807             (tlf-offset (di:code-location-top-level-form-offset location))
808             (form-number (di:code-location-form-number location)))
809        (let ((pos (form-number-stream-position tlf-offset form-number stream)))
810          (file-position stream pos)
811          pos)))
812    
813    (defun form-number-stream-position (tlf-number form-number stream)
814      "Return the starting character position of a form in STREAM.
815    TLF-NUMBER is the top-level-form number.
816    FORM-NUMBER is an index into a source-path table for the TLF."
817      (multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
818        (let* ((path-table (di:form-number-translations tlf 0))
819               (source-path
820                (if (<= (length path-table) form-number) ; source out of sync?
821                    (list 0)                ; should probably signal a condition
822                    (reverse (cdr (aref path-table form-number))))))
823          (source-path-source-position source-path tlf position-map))))
824    
825    (defun code-location-string-offset (code-location string)
826      "Return the byte offset of CODE-LOCATION in STRING.
827    See CODE-LOCATION-STREAM-POSITION."
828      (with-input-from-string (s string)
829        (code-location-stream-position code-location s)))
830    
831    
832    ;;;; Finding definitions
833    
834    ;;; There are a great many different types of definition for us to
835    ;;; find. We search for definitions of every kind and return them in a
836    ;;; list.
837    
838    (defimplementation find-definitions (name)
839      (append (function-definitions name)
840              (setf-definitions name)
841              (variable-definitions name)
842              (class-definitions name)
843              (type-definitions name)
844              (compiler-macro-definitions name)
845              (source-transform-definitions name)
846              (function-info-definitions name)
847              (ir1-translator-definitions name)))
848    
849    ;;;;; Functions, macros, generic functions, methods
850    ;;;
851    ;;; We make extensive use of the compile-time debug information that
852    ;;; CMUCL records, in particular "debug functions" and "code
853    ;;; locations." Refer to the "Debugger Programmer's Interface" section
854    ;;; of the CMUCL manual for more details.
855    
856    (defun function-definitions (name)
857      "Return definitions for NAME in the \"function namespace\", i.e.,
858    regular functions, generic functions, methods and macros.
859    NAME can any valid function name (e.g, (setf car))."
860      (let ((macro?    (and (symbolp name) (macro-function name)))
861            (special?  (and (symbolp name) (special-operator-p name)))
862            (function? (and (ext:valid-function-name-p name)
863                            (ext:info :function :definition name)
864                            (if (symbolp name) (fboundp name) t))))
865        (cond (macro?
866               (list `((defmacro ,name)
867                       ,(function-location (macro-function name)))))
868              (special?
869               (list `((:special-operator ,name)
870                       (:error ,(format nil "Special operator: ~S" name)))))
871              (function?
872               (let ((function (fdefinition name)))
873                 (if (genericp function)
874                     (generic-function-definitions name function)
875                     (list (list `(function ,name)
876                                 (function-location function)))))))))
877    
878    ;;;;;; Ordinary (non-generic/macro/special) functions
879    ;;;
880    ;;; First we test if FUNCTION is a closure created by defstruct, and
881    ;;; if so extract the defstruct-description (`dd') from the closure
882    ;;; and find the constructor for the struct.  Defstruct creates a
883    ;;; defun for the default constructor and we use that as an
884    ;;; approximation to the source location of the defstruct.
885    ;;;
886    ;;; For an ordinary function we return the source location of the
887    ;;; first code-location we find.
888    ;;;
889    (defun function-location (function)
890      "Return the source location for FUNCTION."
891      (cond ((struct-closure-p function)
892             (struct-closure-location function))
893            ((c::byte-function-or-closure-p function)
894             (byte-function-location function))
895            (t
896             (compiled-function-location function))))
897    
898    (defun compiled-function-location (function)
899      "Return the location of a regular compiled function."
900      (multiple-value-bind (code-location error)
901          (safe-definition-finding (function-first-code-location function))
902        (cond (error (list :error (princ-to-string error)))
903              (t (code-location-source-location code-location)))))
904    
905  (defun function-first-code-location (function)  (defun function-first-code-location (function)
906      "Return the first code-location we can find for FUNCTION."
907    (and (function-has-debug-function-p function)    (and (function-has-debug-function-p function)
908         (di:debug-function-start-location         (di:debug-function-start-location
909          (di:function-debug-function function))))          (di:function-debug-function function))))
# Line 580  The second return value is the condition Line 916  The second return value is the condition
916             (vm::find-code-object function))             (vm::find-code-object function))
917         (not (eq closure function))))         (not (eq closure function))))
918    
919    (defun byte-function-location (fun)
920      "Return the location of the byte-compiled function FUN."
921      (etypecase fun
922        ((or c::hairy-byte-function c::simple-byte-function)
923         (let* ((di (kernel:%code-debug-info (c::byte-function-component fun))))
924           (if di
925               (debug-info-function-name-location di)
926               `(:error
927                 ,(format nil "Byte-function without debug-info: ~a" fun)))))
928        (c::byte-closure
929         (byte-function-location (c::byte-closure-function fun)))))
930    
931    ;;; Here we deal with structure accessors. Note that `dd' is a
932    ;;; "defstruct descriptor" structure in CMUCL. A `dd' describes a
933    ;;; `defstruct''d structure.
934    
935  (defun struct-closure-p (function)  (defun struct-closure-p (function)
936      "Is FUNCTION a closure created by defstruct?"
937    (or (function-code-object= function #'kernel::structure-slot-accessor)    (or (function-code-object= function #'kernel::structure-slot-accessor)
938        (function-code-object= function #'kernel::structure-slot-setter)        (function-code-object= function #'kernel::structure-slot-setter)
939        (function-code-object= function #'kernel::%defstruct)))        (function-code-object= function #'kernel::%defstruct)))
940    
941    (defun struct-closure-location (function)
942      "Return the location of the structure that FUNCTION belongs to."
943      (assert (struct-closure-p function))
944      (safe-definition-finding
945        (dd-location (struct-closure-dd function))))
946    
947  (defun struct-closure-dd (function)  (defun struct-closure-dd (function)
948      "Return the defstruct-definition (dd) of FUNCTION."
949    (assert (= (kernel:get-type function) vm:closure-header-type))    (assert (= (kernel:get-type function) vm:closure-header-type))
950    (flet ((find-layout (function)    (flet ((find-layout (function)
951             (sys:find-if-in-closure             (sys:find-if-in-closure
# Line 597  The second return value is the condition Line 957  The second return value is the condition
957                    (return-from find-layout value))))                    (return-from find-layout value))))
958              function)))              function)))
959      (kernel:layout-info (find-layout function))))      (kernel:layout-info (find-layout function))))
960    
961  (defun dd-source-location (dd)  (defun dd-location (dd)
962      "Return the location of a `defstruct'."
963      ;; Find the location in a constructor.
964      (function-location (struct-constructor dd)))
965    
966    (defun struct-constructor (dd)
967      "Return a constructor function from a defstruct definition.
968    Signal an error if no constructor can be found."
969    (let ((constructor (or (kernel:dd-default-constructor dd)    (let ((constructor (or (kernel:dd-default-constructor dd)
970                           (car (kernel::dd-constructors dd)))))                           (car (kernel::dd-constructors dd)))))
971      (when (or (not constructor) (and (consp constructor)      (when (or (null constructor)
972                                       (not (car constructor))))                (and (consp constructor) (null (car constructor))))
973        (error "Cannot locate struct without constructor: ~S"        (error "Cannot find structure's constructor: ~S"
974               (kernel::dd-name dd)))               (kernel::dd-name dd)))
975      (function-source-location      (coerce (if (consp constructor) (first constructor) constructor)
976       (coerce (if (consp constructor) (car constructor) constructor)              'function)))
977               'function))))  
978    ;;;;;; Generic functions and methods
979    
980    (defun generic-function-definitions (name function)
981      "Return the definitions of a generic function and its methods."
982      (cons (list `(defgeneric ,name) (gf-location function))
983            (gf-method-definitions function)))
984    
985    (defun gf-location (gf)
986      "Return the location of the generic function GF."
987      (definition-source-location gf (pcl::generic-function-name gf)))
988    
989    (defun gf-method-definitions (gf)
990      "Return the locations of all methods of the generic function GF."
991      (mapcar #'method-definition (pcl::generic-function-methods gf)))
992    
993    (defun method-definition (method)
994      (list (method-dspec method)
995            (method-location method)))
996    
997    (defun method-dspec (method)
998      "Return a human-readable \"definition specifier\" for METHOD."
999      (let* ((gf (pcl:method-generic-function method))
1000             (name (pcl:generic-function-name gf))
1001             (specializers (pcl:method-specializers method))
1002             (qualifiers (pcl:method-qualifiers method)))
1003        `(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
1004    
1005    ;; XXX maybe special case setters/getters
1006    (defun method-location (method)
1007      (function-location (or (pcl::method-fast-function method)
1008                             (pcl:method-function method))))
1009    
1010  (defun genericp (fn)  (defun genericp (fn)
1011    (typep fn 'generic-function))    (typep fn 'generic-function))
1012    
1013  (defun gf-definition-location (gf)  ;;;;;; Types and classes
1014    (flet ((guess-source-file (faslfile)  
1015             (unix-truename  (defun type-definitions (name)
1016              (merge-pathnames (make-pathname :type "lisp")    "Return `deftype' locations for type NAME."
1017                               faslfile))))    (maybe-make-definition (ext:info :type :expander name) 'deftype name))
1018      (let ((def-source (pcl::definition-source gf))  
1019            (name (string (pcl:generic-function-name gf))))  (defun maybe-make-definition (function kind name)
1020        (etypecase def-source    "If FUNCTION is non-nil then return its definition location."
1021          (pathname (make-location    (if function
1022                     `(:file ,(guess-source-file def-source))        (list (list `(,kind ,name) (function-location function)))))
1023                     `(:function-name ,name)))  
1024          (cons  (defun class-definitions (name)
1025           (destructuring-bind ((dg name) pathname) def-source    "Return the definition locations for the class called NAME."
1026             (declare (ignore dg))    (if (symbolp name)
1027             (etypecase pathname        (let ((class (kernel::find-class name nil)))
1028               (pathname          (etypecase class
1029                (make-location `(:file ,(guess-source-file pathname))            (null '())
1030                               `(:function-name ,(string name)))))))))))            (kernel::structure-class
1031               (list (list `(defstruct ,name) (dd-location (find-dd name)))))
1032  (defun method-source-location (method)            #+(or)
1033    (function-source-location (or (pcl::method-fast-function method)            (conditions::condition-class
1034                                  (pcl:method-function method))))             (list (list `(define-condition ,name)
1035                           (condition-class-location class))))
1036  (defun gf-method-locations (gf)            (kernel::standard-class
1037    (let ((ms (pcl::generic-function-methods gf)))             (list (list `(defclass ,name)
1038      (mapcar #'method-source-location ms)))                         (class-location (find-class name)))))
1039              ((or kernel::built-in-class
1040  (defun gf-source-locations (gf)                 conditions::condition-class
1041    (list* (gf-definition-location gf)                 kernel:funcallable-structure-class)
1042           (gf-method-locations gf)))             (list (list `(kernel::define-type-class ,name)
1043                           `(:error
1044  (defun function-source-locations (function)                           ,(format nil "No source info for ~A" name)))))))))
1045    "Return a list of source locations for FUNCTION."  
1046    ;; First test if FUNCTION is a closure created by defstruct; if so  (defun class-location (class)
1047    ;; extract the defstruct-description (dd) from the closure and find    "Return the `defclass' location for CLASS."
1048    ;; the constructor for the struct.  Defstruct creates a defun for    (definition-source-location class (pcl:class-name class)))
1049    ;; the default constructor and we use that as an approximation to  
1050    ;; the source location of the defstruct.  (defun find-dd (name)
1051    ;;    "Find the defstruct-definition by the name of its structure-class."
1052    ;; For an ordinary function we return the source location of the    (let ((layout (ext:info :type :compiler-layout name)))
1053    ;; first code-location we find.      (if layout
1054    (cond ((struct-closure-p function)          (kernel:layout-info layout))))
1055           (list  
1056            (safe-definition-finding  (defun condition-class-location (class)
1057             (dd-source-location (struct-closure-dd function)))))    (let ((slots (conditions::condition-class-slots class))
1058          ((genericp function)          (name (conditions::condition-class-name class)))
1059           (gf-source-locations function))      (cond ((null slots)
1060          (t             `(:error ,(format nil "No location info for condition: ~A" name)))
1061           (list            (t
1062            (multiple-value-bind (code-location error)             ;; Find the class via one of its slot-reader methods.
1063                (safe-definition-finding (function-first-code-location function))             (let* ((slot (first slots))
1064              (cond (error (list :error (princ-to-string error)))                    (gf (fdefinition
1065                    (t (code-location-source-location code-location))))))))                         (first (conditions::condition-slot-readers slot)))))
1066                 (method-location
1067  (defun function-source-location (function)                (first
1068    (destructuring-bind (first) (function-source-locations function)                 (pcl:compute-applicable-methods-using-classes
1069      first))                  gf (list (find-class name))))))))))
1070    
1071  (defslimefun find-function-locations (symbol-name)  (defun make-name-in-file-location (file string)
1072    "Return a list of source-locations for SYMBOL-NAME's functions."    (multiple-value-bind (filename c)
1073    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)        (ignore-errors
1074      (cond ((not foundp)          (unix-truename (merge-pathnames (make-pathname :type "lisp")
1075             (list (list :error (format nil "Unkown symbol: ~A" symbol-name))))                                          file)))
1076            ((macro-function symbol)      (cond (filename (make-location `(:file ,filename)
1077             (function-source-locations (macro-function symbol)))                                     `(:function-name ,(string string))))
1078            ((special-operator-p symbol)            (t (list :error (princ-to-string c))))))
1079             (list (list :error (format nil "~A is a special-operator" symbol))))  
1080            ((fboundp symbol)  (defun source-location-form-numbers (location)
1081             (function-source-locations (coerce symbol 'function)))    (c::decode-form-numbers (c::form-numbers-form-numbers location)))
1082            (t (list (list :error  
1083                           (format nil "Symbol not fbound: ~A" symbol-name))))  (defun source-location-tlf-number (location)
1084            )))    (nth-value 0 (source-location-form-numbers location)))
1085    
1086    (defun source-location-form-number (location)
1087      (nth-value 1 (source-location-form-numbers location)))
1088    
1089    (defun resolve-file-source-location (location)
1090      (let ((filename (c::file-source-location-pathname location))
1091            (tlf-number (source-location-tlf-number location))
1092            (form-number (source-location-form-number location)))
1093        (with-open-file (s filename)
1094          (let ((pos (form-number-stream-position tlf-number form-number s)))
1095            (make-location `(:file ,(unix-truename filename))
1096                           `(:position ,(1+ pos)))))))
1097    
1098    (defun resolve-stream-source-location (location)
1099      (let ((info (c::stream-source-location-user-info location))
1100            (tlf-number (source-location-tlf-number location))
1101            (form-number (source-location-form-number location)))
1102        ;; XXX duplication in frame-source-location
1103        (assert (info-from-emacs-buffer-p info))
1104        (destructuring-bind (&key emacs-buffer emacs-buffer-string
1105                                  emacs-buffer-offset) info
1106          (with-input-from-string (s emacs-buffer-string)
1107            (let ((pos (form-number-stream-position tlf-number form-number s)))
1108              (make-location `(:buffer ,emacs-buffer)
1109                             `(:offset ,emacs-buffer-offset ,pos)))))))
1110    
1111    ;; XXX predicates for 18e backward compatibilty.  Remove them when
1112    ;; we're 19a only.
1113    (defun file-source-location-p (object)
1114      (when (fboundp 'c::file-source-location-p)
1115        (c::file-source-location-p object)))
1116    
1117    (defun stream-source-location-p (object)
1118      (when (fboundp 'c::stream-source-location-p)
1119        (c::stream-source-location-p object)))
1120    
1121    (defun source-location-p (object)
1122      (or (file-source-location-p object)
1123          (stream-source-location-p object)))
1124    
1125    (defun resolve-source-location (location)
1126      (etypecase location
1127        ((satisfies file-source-location-p)
1128         (resolve-file-source-location location))
1129        ((satisfies stream-source-location-p)
1130         (resolve-stream-source-location location))))
1131    
1132    (defun definition-source-location (object name)
1133      (let ((source (pcl::definition-source object)))
1134        (etypecase source
1135          (null
1136           `(:error ,(format nil "No source info for: ~A" object)))
1137          ((satisfies source-location-p)
1138           (resolve-source-location source))
1139          (pathname
1140           (make-name-in-file-location source name))
1141          (cons
1142           (destructuring-bind ((dg name) pathname) source
1143             (declare (ignore dg))
1144             (etypecase pathname
1145               (pathname (make-name-in-file-location pathname (string name)))
1146               (null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
1147    
1148    (defun setf-definitions (name)
1149      (let ((function (or (ext:info :setf :inverse name)
1150                          (ext:info :setf :expander name)
1151                          (and (symbolp name)
1152                               (fboundp `(setf ,name))
1153                               (fdefinition `(setf ,name))))))
1154        (if function
1155            (list (list `(setf ,name)
1156                        (function-location (coerce function 'function)))))))
1157    
1158    
1159    (defun variable-location (symbol)
1160      (multiple-value-bind (location foundp)
1161          ;; XXX for 18e compatibilty. rewrite this when we drop 18e
1162          ;; support.
1163          (ignore-errors (eval `(ext:info :source-location :defvar ',symbol)))
1164        (if (and foundp location)
1165            (resolve-source-location location)
1166            `(:error ,(format nil "No source info for variable ~S" symbol)))))
1167    
1168    (defun variable-definitions (name)
1169      (if (symbolp name)
1170          (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1171            (if recorded-p
1172                (list (list `(variable ,kind ,name)
1173                            (variable-location name)))))))
1174    
1175    (defun compiler-macro-definitions (symbol)
1176      (maybe-make-definition (compiler-macro-function symbol)
1177                             'define-compiler-macro
1178                             symbol))
1179    
1180    (defun source-transform-definitions (name)
1181      (maybe-make-definition (ext:info :function :source-transform name)
1182                             'c:def-source-transform
1183                             name))
1184    
1185    (defun function-info-definitions (name)
1186      (let ((info (ext:info :function :info name)))
1187        (if info
1188            (append (loop for transform in (c::function-info-transforms info)
1189                          collect (list `(c:deftransform ,name
1190                                          ,(c::type-specifier
1191                                            (c::transform-type transform)))
1192                                        (function-location (c::transform-function
1193                                                            transform))))
1194                    (maybe-make-definition (c::function-info-derive-type info)
1195                                           'c::derive-type name)
1196                    (maybe-make-definition (c::function-info-optimizer info)
1197                                           'c::optimizer name)
1198                    (maybe-make-definition (c::function-info-ltn-annotate info)
1199                                           'c::ltn-annotate name)
1200                    (maybe-make-definition (c::function-info-ir2-convert info)
1201                                           'c::ir2-convert name)
1202                    (loop for template in (c::function-info-templates info)
1203                          collect (list `(c::vop ,(c::template-name template))
1204                                        (function-location
1205                                         (c::vop-info-generator-function
1206                                          template))))))))
1207    
1208    (defun ir1-translator-definitions (name)
1209      (maybe-make-definition (ext:info :function :ir1-convert name)
1210                             'c:def-ir1-translator name))
1211    
1212    
1213  ;;;; Documentation.  ;;;; Documentation.
1214    
1215  (defmethod describe-symbol-for-emacs (symbol)  (defimplementation describe-symbol-for-emacs (symbol)
1216    (let ((result '()))    (let ((result '()))
1217      (flet ((doc (kind)      (flet ((doc (kind)
1218               (or (documentation symbol kind) :not-documented))               (or (documentation symbol kind) :not-documented))
# Line 701  The second return value is the condition Line 1225  The second return value is the condition
1225                     (declare (ignore kind))                     (declare (ignore kind))
1226                     (if (or (boundp symbol) recorded-p)                     (if (or (boundp symbol) recorded-p)
1227                         (doc 'variable))))                         (doc 'variable))))
1228        (maybe-push        (when (fboundp symbol)
1229         :generic-function          (maybe-push
1230         (if (and (fboundp symbol)           (cond ((macro-function symbol)     :macro)
1231                  (typep (fdefinition symbol) 'generic-function))                 ((special-operator-p symbol) :special-operator)
1232             (doc 'function)))                 ((genericp (fdefinition symbol)) :generic-function)
1233        (maybe-push                 (t :function))
1234         :function (if (and (fboundp symbol)           (doc 'function)))
                           (not (typep (fdefinition symbol) 'generic-function)))  
                      (doc 'function)))  
1235        (maybe-push        (maybe-push
1236         :setf (if (or (ext:info setf inverse symbol)         :setf (if (or (ext:info setf inverse symbol)
1237                       (ext:info setf expander symbol))                       (ext:info setf expander symbol))
# Line 734  The second return value is the condition Line 1256  The second return value is the condition
1256                         (doc nil)))                         (doc nil)))
1257        result)))        result)))
1258    
1259  (defslimefun describe-setf-function (symbol-name)  (defimplementation describe-definition (symbol namespace)
1260    (print-description-to-string    (describe (ecase namespace
1261     (or (ext:info setf inverse (from-string symbol-name))                (:variable
1262         (ext:info setf expander (from-string symbol-name)))))                 symbol)
1263                  ((:function :generic-function)
1264  (defslimefun describe-type (symbol-name)                 (symbol-function symbol))
1265    (print-description-to-string                (:setf
1266     (kernel:values-specifier-type (from-string symbol-name))))                 (or (ext:info setf inverse symbol)
1267                       (ext:info setf expander symbol)))
1268  (defslimefun describe-class (symbol-name)                (:type
1269    (print-description-to-string (find-class (from-string symbol-name) nil)))                 (kernel:values-specifier-type symbol))
1270                  (:class
1271  (defslimefun describe-alien-type (symbol-name)                 (find-class symbol))
1272    (let ((name (from-string symbol-name)))                (:alien-struct
1273      (ecase (ext:info :alien-type :kind name)                 (ext:info :alien-type :struct symbol))
1274        (:primitive                (:alien-union
1275         (print-description-to-string                 (ext:info :alien-type :union symbol))
1276          (let ((alien::*values-type-okay* t))                (:alien-enum
1277            (funcall (ext:info :alien-type :translator name) (list name)))))                 (ext:info :alien-type :enum symbol))
1278        ((:defined)                (:alien-type
1279         (print-description-to-string (ext:info :alien-type :definition name)))                 (ecase (ext:info :alien-type :kind symbol)
1280        (:unknown                   (:primitive
1281         (format nil "Unkown alien type: ~A" symbol-name)))))                    (let ((alien::*values-type-okay* t))
1282                        (funcall (ext:info :alien-type :translator symbol)
1283  (defmacro %describe-alien (symbol-name namespace)                               (list symbol))))
1284    `(print-description-to-string                   ((:defined)
1285      (ext:info :alien-type ,namespace (from-string ,symbol-name))))                    (ext:info :alien-type :definition symbol))
1286                     (:unknown :unkown))))))
1287  (defslimefun describe-alien-struct (symbol-name)  
1288    (%describe-alien symbol-name :struct))  ;;;;; Argument lists
1289    
1290  (defslimefun describe-alien-union (symbol-name)  (defimplementation arglist (fun)
1291    (%describe-alien symbol-name :union))    (etypecase fun
1292        (function (function-arglist fun))
1293  (defslimefun describe-alien-enum (symbol-name)      (symbol (function-arglist (or (macro-function fun)
1294    (%describe-alien symbol-name :enum))                                    (symbol-function fun))))))
1295    
1296  (defmethod arglist-string (fname)  (defun function-arglist (fun)
1297    "Return a string describing the argument list for FNAME.    (let ((arglist
1298  The result has the format \"(...)\"."           (cond ((eval:interpreted-function-p fun)
1299    (declare (type string fname))                  (eval:interpreted-function-arglist fun))
1300    (multiple-value-bind (function condition)                 ((pcl::generic-function-p fun)
1301        (ignore-errors (values (find-symbol-designator fname *buffer-package*)))                  (pcl:generic-function-lambda-list fun))
1302      (when condition                 ((c::byte-function-or-closure-p fun)
1303        (return-from arglist-string (format nil "(-- ~A)" condition)))                  (byte-code-function-arglist fun))
1304      (let ((arglist                 ((kernel:%function-arglist (kernel:%function-self fun))
1305             (if (not (or (fboundp function)                  (handler-case (read-arglist fun)
1306                          (functionp function)))                    (error () :not-available)))
1307                 "(-- <Unknown-Function>)"                 ;; this should work both for compiled-debug-function
1308                 (let* ((fun (or (macro-function function)                 ;; and for interpreted-debug-function
1309                                 (symbol-function function)))                 (t
1310                        (df (di::function-debug-function fun))                  (handler-case (debug-function-arglist
1311                        (arglist (kernel:%function-arglist fun)))                                 (di::function-debug-function fun))
1312                   (cond ((eval:interpreted-function-p fun)                    (di:unhandled-condition () :not-available))))))
1313                          (eval:interpreted-function-arglist fun))      (check-type arglist (or list (member :not-available)))
1314                         ((pcl::generic-function-p fun)      arglist))
1315                          (pcl::gf-pretty-arglist fun))  
1316                         (arglist arglist)  (defimplementation function-name (function)
1317                         ;; this should work both for    (cond ((eval:interpreted-function-p function)
1318                         ;; compiled-debug-function and for           (eval:interpreted-function-name function))
1319                         ;; interpreted-debug-function          ((pcl::generic-function-p function)
1320                         (df (di::debug-function-lambda-list df))           (pcl::generic-function-name function))
1321                         (t "(<arglist-unavailable>)"))))))          ((c::byte-function-or-closure-p function)
1322        (if (stringp arglist)           (c::byte-function-name function))
1323            arglist          (t (kernel:%function-name (kernel:%function-self function)))))
1324            (to-string arglist)))))  
1325    ;;; A simple case: the arglist is available as a string that we can
1326    ;;; `read'.
1327    
1328    (defun read-arglist (fn)
1329      "Parse the arglist-string of the function object FN."
1330      (let ((string (kernel:%function-arglist
1331                     (kernel:%function-self fn)))
1332            (package (find-package
1333                      (c::compiled-debug-info-package
1334                       (kernel:%code-debug-info
1335                        (vm::find-code-object fn))))))
1336        (with-standard-io-syntax
1337          (let ((*package* (or package *package*)))
1338            (read-from-string string)))))
1339    
1340    ;;; A harder case: an approximate arglist is derived from available
1341    ;;; debugging information.
1342    
1343    (defun debug-function-arglist (debug-function)
1344      "Derive the argument list of DEBUG-FUNCTION from debug info."
1345      (let ((args (di::debug-function-lambda-list debug-function))
1346            (required '())
1347            (optional '())
1348            (rest '())
1349            (key '()))
1350        ;; collect the names of debug-vars
1351        (dolist (arg args)
1352          (etypecase arg
1353            (di::debug-variable
1354             (push (di::debug-variable-symbol arg) required))
1355            ((member :deleted)
1356             (push ':deleted required))
1357            (cons
1358             (ecase (car arg)
1359               (:keyword
1360                (push (second arg) key))
1361               (:optional
1362                (push (debug-variable-symbol-or-deleted (second arg)) optional))
1363               (:rest
1364                (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1365        ;; intersperse lambda keywords as needed
1366        (append (nreverse required)
1367                (if optional (cons '&optional (nreverse optional)))
1368                (if rest (cons '&rest (nreverse rest)))
1369                (if key (cons '&key (nreverse key))))))
1370    
1371    (defun debug-variable-symbol-or-deleted (var)
1372      (etypecase var
1373        (di:debug-variable
1374         (di::debug-variable-symbol var))
1375        ((member :deleted)
1376         '#:deleted)))
1377    
1378    (defun symbol-debug-function-arglist (fname)
1379      "Return FNAME's debug-function-arglist and %function-arglist.
1380    A utility for debugging DEBUG-FUNCTION-ARGLIST."
1381      (let ((fn (fdefinition fname)))
1382        (values (debug-function-arglist (di::function-debug-function fn))
1383                (kernel:%function-arglist (kernel:%function-self fn)))))
1384    
1385    ;;; Deriving arglists for byte-compiled functions:
1386    ;;;
1387    (defun byte-code-function-arglist (fn)
1388      ;; There doesn't seem to be much arglist information around for
1389      ;; byte-code functions.  Use the arg-count and return something like
1390      ;; (arg0 arg1 ...)
1391      (etypecase fn
1392        (c::simple-byte-function
1393         (loop for i from 0 below (c::simple-byte-function-num-args fn)
1394               collect (make-arg-symbol i)))
1395        (c::hairy-byte-function
1396         (hairy-byte-function-arglist fn))
1397        (c::byte-closure
1398         (byte-code-function-arglist (c::byte-closure-function fn)))))
1399    
1400    (defun make-arg-symbol (i)
1401      (make-symbol (format nil "~A~D" (string 'arg) i)))
1402    
1403    ;;; A "hairy" byte-function is one that takes a variable number of
1404    ;;; arguments. `hairy-byte-function' is a type from the bytecode
1405    ;;; interpreter.
1406    ;;;
1407    (defun hairy-byte-function-arglist (fn)
1408      (let ((counter -1))
1409        (flet ((next-arg () (make-arg-symbol (incf counter))))
1410          (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1411                                                keywords-p keywords) fn
1412            (let ((arglist '())
1413                  (optional (- max-args min-args)))
1414              ;; XXX isn't there a better way to write this?
1415              ;; (Looks fine to me. -luke)
1416              (dotimes (i min-args)
1417                (push (next-arg) arglist))
1418              (when (plusp optional)
1419                (push '&optional arglist)
1420                (dotimes (i optional)
1421                  (push (next-arg) arglist)))
1422              (when rest-arg-p
1423                (push '&rest arglist)
1424                (push (next-arg) arglist))
1425              (when keywords-p
1426                (push '&key arglist)
1427                (loop for (key _ __) in keywords
1428                      do (push key arglist))
1429                (when (eq keywords-p :allow-others)
1430                  (push '&allow-other-keys arglist)))
1431              (nreverse arglist))))))
1432    
1433    
1434  ;;;; Miscellaneous.  ;;;; Miscellaneous.
1435    
1436  (defmethod macroexpand-all (form)  (defimplementation macroexpand-all (form)
1437    (walker:macroexpand-all form))    (walker:macroexpand-all form))
1438    
1439  (defun tracedp (fname)  (defimplementation compiler-macroexpand-1 (form &optional env)
1440    (gethash (debug::trace-fdefinition fname)    (ext:compiler-macroexpand-1 form env))
1441             debug::*traced-functions*))  
1442    (defimplementation compiler-macroexpand (form &optional env)
1443  (defslimefun toggle-trace-fdefinition (fname-string)    (ext:compiler-macroexpand form env))
   (let ((fname (from-string fname-string)))  
     (cond ((tracedp fname)  
            (debug::untrace-1 fname)  
            (format nil "~S is now untraced." fname))  
           (t  
            (debug::trace-1 fname (debug::make-trace-info))  
            (format nil "~S is now traced." fname)))))  
1444    
1445  (defslimefun set-default-directory (directory)  (defimplementation set-default-directory (directory)
1446    (setf (ext:default-directory) (namestring directory))    (setf (ext:default-directory) (namestring directory))
1447    ;; Setting *default-pathname-defaults* to an absolute directory    ;; Setting *default-pathname-defaults* to an absolute directory
1448    ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.    ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1449    (setf *default-pathname-defaults* (pathname (ext:default-directory)))    (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1450    (namestring (ext:default-directory)))    (default-directory))
   
   
 ;;;; Source-paths  
   
 ;;; 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)))))  
1451    
1452  (defun source-path-string-position (path string)  (defimplementation default-directory ()
1453    (with-input-from-string (s string)    (namestring (ext:default-directory)))
     (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))))))  
   
 (defun code-location-stream-position (code-location stream)  
   "Return the byte offset of CODE-LOCATION in STREAM.  Extract the  
 toplevel-form-number and form-number from CODE-LOCATION and use that  
 to find the position of the corresponding form."  
   (let* ((location (debug::maybe-block-start-location code-location))  
          (tlf-offset (di:code-location-top-level-form-offset location))  
          (form-number (di:code-location-form-number location))  
          (*read-suppress* t))  
     (dotimes (i tlf-offset) (read stream))  
     (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)  
       (let* ((path-table (di:form-number-translations tlf 0))  
              (source-path  
               (if (<= (length path-table) form-number) ; source out of sync?  
                   (list 0)              ; should probably signal a condition  
                   (reverse (cdr (aref path-table form-number))))))  
         (source-path-source-position source-path tlf position-map)))))  
   
 (defun code-location-string-offset (code-location string)  
   (with-input-from-string (s string)  
     (code-location-stream-position code-location s)))  
1454    
1455  (defun code-location-file-position (code-location filename)  (defimplementation call-without-interrupts (fn)
1456    (with-open-file (s filename :direction :input)    (sys:without-interrupts (funcall fn)))
     (code-location-stream-position code-location s)))  
1457    
1458  (defun debug-source-info-from-emacs-buffer-p (debug-source)  (defimplementation getpid ()
1459    (let ((info (c::debug-source-info debug-source)))    (unix:unix-getpid))
     (and info  
          (consp info)  
          (eq :emacs-buffer (car info)))))  
1460    
1461  (defun source-location-from-code-location (code-location)  (defimplementation lisp-implementation-type-name ()
1462    "Return the source location for CODE-LOCATION."    "cmucl")
   (let ((debug-fun (di:code-location-debug-function code-location)))  
     (when (di::bogus-debug-function-p debug-fun)  
       (error "Bogus debug function: ~A" debug-fun)))  
   (let* ((debug-source (di:code-location-debug-source code-location))  
          (from (di:debug-source-from debug-source))  
          (name (di:debug-source-name debug-source)))  
     (ecase from  
       (:file  
        (make-location (list :file (unix-truename name))  
                       (list :position (1+ (code-location-file-position  
                                            code-location name)))))  
       (:stream  
        (assert (debug-source-info-from-emacs-buffer-p debug-source))  
        (let ((info (c::debug-source-info debug-source)))  
          (make-location  
           (list :buffer (getf info :emacs-buffer))  
           (list :position (+ (getf info :emacs-buffer-offset)  
                              (code-location-string-offset  
                               code-location  
                               (getf info :emacs-buffer-string)))))))  
       (:lisp  
        (make-location  
         (list :source-form (with-output-to-string (*standard-output*)  
                              (debug::print-code-location-source-form  
                               code-location 100 t)))  
         (list :position 1))))))  
1463    
1464  (defun code-location-source-location (code-location)  (defimplementation quit-lisp ()
1465    "Safe wrapper around `code-location-from-source-location'."    (ext::quit))
   (safe-definition-finding  
    (source-location-from-code-location code-location)))  
1466    
1467  (defslimefun getpid ()  ;;; source-path-{stream,file,string,etc}-position moved into
1468    (unix:unix-getpid))  ;;; swank-source-path-parser
1469    
1470    
1471  ;;;; Debugging  ;;;; Debugging
1472    
1473  (defvar *sldb-stack-top*)  (defvar *sldb-stack-top*)
 (defvar *sldb-restarts*)  
1474    
1475  (defmethod call-with-debugging-environment (debugger-loop-fn)  (defimplementation call-with-debugging-environment (debugger-loop-fn)
1476    (unix:unix-sigsetmask 0)    (unix:unix-sigsetmask 0)
1477    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
          (*sldb-restarts* (compute-restarts *swank-debugger-condition*))  
1478           (debug:*stack-top-hint* nil)           (debug:*stack-top-hint* nil)
1479           (*debugger-hook* nil)           (kernel:*current-level* 0))
1480           (*readtable* (or debug:*debug-readtable* *readtable*))      (handler-bind ((di::unhandled-condition
          (*print-level* debug:*debug-print-level*)  
          (*print-length* debug:*debug-print-length*)  
          (*print-readably* nil))  
     (handler-bind ((di:debug-condition  
1481                      (lambda (condition)                      (lambda (condition)
1482                        (signal (make-condition                        (error (make-condition
1483                                 'sldb-condition                                'sldb-condition
1484                                 :original-condition condition)))))                                :original-condition condition)))))
1485        (funcall debugger-loop-fn))))        (unwind-protect
1486               (progn
1487  (defun format-restarts-for-emacs ()               #+(or)(sys:scrub-control-stack)
1488    "Return a list of restarts for *swank-debugger-condition* in a               (funcall debugger-loop-fn))
1489  format suitable for Emacs."          #+(or)(sys:scrub-control-stack)
1490    (loop for restart in *sldb-restarts*          ))))
1491          collect (list (princ-to-string (restart-name restart))  
1492                        (princ-to-string restart))))  (defun frame-down (frame)
1493      (handler-case (di:frame-down frame)
1494  (defun format-condition-for-emacs ()      (di:no-debug-info () nil)))
   (format nil "~A~%   [Condition of type ~S]"  
           (debug::safe-condition-message *swank-debugger-condition*)  
           (type-of *swank-debugger-condition*)))  
1495    
1496  (defun nth-frame (index)  (defun nth-frame (index)
1497    (do ((frame *sldb-stack-top* (di:frame-down frame))    (do ((frame *sldb-stack-top* (frame-down frame))
1498         (i index (1- i)))         (i index (1- i)))
1499        ((zerop i) frame)))        ((zerop i) frame)))
1500    
1501  (defun nth-restart (index)  (defimplementation compute-backtrace (start end)
   (nth index *sldb-restarts*))  
   
 (defun format-frame-for-emacs (frame)  
   (list (di:frame-number frame)  
         (with-output-to-string (*standard-output*)  
           (let ((*print-pretty* *sldb-pprint-frames*))  
             (debug::print-frame-call frame :verbosity 1 :number t)))))  
   
 (defun compute-backtrace (start end)  
   "Return a list of frames starting with frame number START and  
 continuing to frame number END or, if END is nil, the last frame on the  
 stack."  
1502    (let ((end (or end most-positive-fixnum)))    (let ((end (or end most-positive-fixnum)))
1503      (loop for f = (nth-frame start) then (di:frame-down f)      (loop for f = (nth-frame start) then (frame-down f)
1504            for i from start below end            for i from start below end
1505            while f            while f collect f)))
           collect f)))  
1506    
1507  (defmethod backtrace (start end)  (defimplementation print-frame (frame stream)
1508    (mapcar #'format-frame-for-emacs (compute-backtrace start end)))    (let ((*standard-output* stream))
1509        (handler-case
1510            (debug::print-frame-call frame :verbosity 1 :number nil)
1511          (error (e)
1512            (ignore-errors (princ e stream))))))
1513    
1514  (defmethod debugger-info-for-emacs (start end)  (defimplementation frame-source-location-for-emacs (index)
   (list (format-condition-for-emacs)  
         (format-restarts-for-emacs)  
         (backtrace start end)))  
   
 (defmethod frame-source-location-for-emacs (index)  
1515    (code-location-source-location (di:frame-code-location (nth-frame index))))    (code-location-source-location (di:frame-code-location (nth-frame index))))
1516    
1517  (defmethod eval-in-frame (form index)  (defimplementation eval-in-frame (form index)
1518    (di:eval-in-frame (nth-frame index) form))    (di:eval-in-frame (nth-frame index) form))
1519    
1520  (defslimefun pprint-eval-string-in-frame (string index)  (defun frame-debug-vars (frame)
1521    (swank-pprint    "Return a vector of debug-variables in frame."
1522     (multiple-value-list    (di::debug-function-debug-variables (di:frame-debug-function frame)))
1523      (di:eval-in-frame (nth-frame index) (from-string string)))))  
1524    (defun debug-var-value (var frame location)
1525  (defslimefun inspect-in-frame (string index)    (let ((validity (di:debug-variable-validity var location)))
1526    (reset-inspector)      (ecase validity
1527    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))        (:valid (di:debug-variable-value var frame))
1528          ((:invalid :unknown) (make-symbol (string validity))))))
1529    
1530  (defmethod frame-locals (index)  (defimplementation frame-locals (index)
1531    (let* ((frame (nth-frame index))    (let* ((frame (nth-frame index))
1532           (location (di:frame-code-location frame))           (loc (di:frame-code-location frame))
1533           (debug-function (di:frame-debug-function frame))           (vars (frame-debug-vars frame)))
1534           (debug-variables (di::debug-function-debug-variables debug-function)))      (loop for v across vars collect
1535      (loop for v across debug-variables            (list :name (di:debug-variable-symbol v)
1536            collect (list                  :id (di:debug-variable-id v)
1537                     :symbol (di:debug-variable-symbol v)                  :value (debug-var-value v frame loc)))))
1538                     :id (di:debug-variable-id v)  
1539                     :value-string  (defimplementation frame-var-value (frame var)
1540                     (if (eq (di:debug-variable-validity v location)    (let* ((frame (nth-frame frame))
1541                             :valid)           (dvar (aref (frame-debug-vars frame) var)))
1542                         (to-string (di:debug-variable-value v frame))      (debug-var-value dvar frame (di:frame-code-location frame))))
1543                         "<not-available>")))))  
1544    (defimplementation frame-catch-tags (index)
1545  (defmethod frame-catch-tags (index)    (mapcar #'car (di:frame-catches (nth-frame index))))
1546    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))  
1547          collect `(,tag . ,(code-location-source-location code-location))))  (defimplementation return-from-frame (index form)
1548      (let ((sym (find-symbol (string 'find-debug-tag-for-frame)
1549                              :debug-internals)))
1550        (if sym
1551            (let* ((frame (nth-frame index))
1552                   (probe (funcall sym frame)))
1553              (cond (probe (throw (car probe) (eval-in-frame form index)))
1554                    (t (format nil "Cannot return from frame: ~S" frame))))
1555            "return-from-frame is not implemented in this version of CMUCL.")))
1556    
1557    (defimplementation activate-stepping (frame)
1558      (set-step-breakpoints (nth-frame frame)))
1559    
1560  (defslimefun invoke-nth-restart (index)  (defimplementation sldb-break-on-return (frame)
1561    (invoke-restart-interactively (nth-restart index)))    (break-on-return (nth-frame frame)))
1562    
1563  (defslimefun sldb-abort ()  ;;; We set the breakpoint in the caller which might be a bit confusing.
1564    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))  ;;;
1565    (defun break-on-return (frame)
1566      (let* ((caller (di:frame-down frame))
1567             (cl (di:frame-code-location caller)))
1568        (flet ((hook (frame bp)
1569                 (when (frame-pointer= frame caller)
1570                   (di:delete-breakpoint bp)
1571                   (signal-breakpoint bp frame))))
1572          (let* ((info (ecase (di:code-location-kind cl)
1573                         ((:single-value-return :unknown-return) nil)
1574                         (:known-return (debug-function-returns
1575                                         (di:frame-debug-function frame)))))
1576                 (bp (di:make-breakpoint #'hook cl :kind :code-location
1577                                         :info info)))
1578            (di:activate-breakpoint bp)
1579            `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1580    
1581    (defun frame-pointer= (frame1 frame2)
1582      "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1583      (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1584    
1585    ;;; The PC in escaped frames at a single-return-value point is
1586    ;;; actually vm:single-value-return-byte-offset bytes after the
1587    ;;; position given in the debug info.  Here we try to recognize such
1588    ;;; cases.
1589    ;;;
1590    (defun next-code-locations (frame code-location)
1591      "Like `debug::next-code-locations' but be careful in escaped frames."
1592      (let ((next (debug::next-code-locations code-location)))
1593        (flet ((adjust-pc ()
1594                 (let ((cl (di::copy-compiled-code-location code-location)))
1595                   (incf (di::compiled-code-location-pc cl)
1596                         vm:single-value-return-byte-offset)
1597                   cl)))
1598          (cond ((and (di::compiled-frame-escaped frame)
1599                      (eq (di:code-location-kind code-location)
1600                          :single-value-return)
1601                      (= (length next) 1)
1602                      (di:code-location= (car next) (adjust-pc)))
1603                 (debug::next-code-locations (car next)))
1604                (t
1605                 next)))))
1606    
1607    (defun set-step-breakpoints (frame)
1608      (let ((cl (di:frame-code-location frame)))
1609        (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1610          (error "Cannot step in elsewhere code"))
1611        (let* ((debug::*bad-code-location-types*
1612                (remove :call-site debug::*bad-code-location-types*))
1613               (next (next-code-locations frame cl)))
1614          (cond (next
1615                 (let ((steppoints '()))
1616                   (flet ((hook (bp-frame bp)
1617                            (signal-breakpoint bp bp-frame)
1618                            (mapc #'di:delete-breakpoint steppoints)))
1619                     (dolist (code-location next)
1620                       (let ((bp (di:make-breakpoint #'hook code-location
1621                                                     :kind :code-location)))
1622                         (di:activate-breakpoint bp)
1623                         (push bp steppoints))))))
1624                (t
1625                 (break-on-return frame))))))
1626    
1627    
1628    ;; XXX the return values at return breakpoints should be passed to the
1629    ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1630    
1631    ;;; The sigcontext and the PC for a breakpoint invocation are not
1632    ;;; passed to user hook functions, but we need them to extract return
1633    ;;; values. So we advice di::handle-breakpoint and bind the values to
1634    ;;; special variables.
1635    ;;;
1636    (defvar *breakpoint-sigcontext*)
1637    (defvar *breakpoint-pc*)
1638    
1639    ;; XXX don't break old versions without fwrappers.  Remove this one day.
1640    #+#.(cl:if (cl:find-package :fwrappers) '(and) '(or))
1641    (progn
1642      (fwrappers:define-fwrapper bind-breakpoint-sigcontext (offset c sigcontext)
1643        (let ((*breakpoint-sigcontext* sigcontext)
1644              (*breakpoint-pc* offset))
1645          (fwrappers:call-next-function)))
1646      (fwrappers:set-fwrappers 'di::handle-breakpoint '())
1647      (fwrappers:fwrap 'di::handle-breakpoint #'bind-breakpoint-sigcontext))
1648    
1649    (defun sigcontext-object (sc index)
1650      "Extract the lisp object in sigcontext SC at offset INDEX."
1651      (kernel:make-lisp-obj (vm:sigcontext-register sc index)))
1652    
1653    (defun known-return-point-values (sigcontext sc-offsets)
1654      (let ((fp (system:int-sap (vm:sigcontext-register sigcontext
1655                                                        vm::cfp-offset))))
1656        (system:without-gcing
1657         (loop for sc-offset across sc-offsets
1658               collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1659    
1660    ;;; CMUCL returns the first few values in registers and the rest on
1661    ;;; the stack. In the multiple value case, the number of values is
1662    ;;; stored in a dedicated register. The values of the registers can be
1663    ;;; accessed in the sigcontext for the breakpoint.  There are 3 kinds
1664    ;;; of return conventions: :single-value-return, :unknown-return, and
1665    ;;; :known-return.
1666    ;;;
1667    ;;; The :single-value-return convention returns the value in a
1668    ;;; register without setting the nargs registers.
1669    ;;;
1670    ;;; The :unknown-return variant is used for multiple values. A
1671    ;;; :unknown-return point consists actually of 2 breakpoints: one for
1672    ;;; the single value case and one for the general case.  The single
1673    ;;; value breakpoint comes vm:single-value-return-byte-offset after
1674    ;;; the multiple value breakpoint.
1675    ;;;
1676    ;;; The :known-return convention is used by local functions.
1677    ;;; :known-return is currently not supported because we don't know
1678    ;;; where the values are passed.
1679    ;;;
1680    (defun breakpoint-values (breakpoint)
1681      "Return the list of return values for a return point."
1682      (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1683        (let ((sc (locally (declare (optimize (speed 0)))
1684                    (alien:sap-alien *breakpoint-sigcontext* (* unix:sigcontext))))
1685              (cl (di:breakpoint-what breakpoint)))
1686          (ecase (di:code-location-kind cl)
1687            (:single-value-return
1688             (list (1st sc)))
1689            (:known-return
1690             (let ((info (di:breakpoint-info breakpoint)))
1691               (if (vectorp info)
1692                   (known-return-point-values sc info)
1693                   (progn
1694                     ;;(break)
1695                     (list "<<known-return convention not supported>>" info)))))
1696            (:unknown-return
1697             (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1698               (if (= mv-return-pc *breakpoint-pc*)
1699                   (mv-function-end-breakpoint-values sc)
1700                   (list (1st sc)))))))))
1701    
1702    ;; XXX: di::get-function-end-breakpoint-values takes 2 arguments in
1703    ;; newer versions of CMUCL (after ~March 2005).
1704    (defun mv-function-end-breakpoint-values (sigcontext)
1705      (let ((sym (find-symbol "FUNCTION-END-BREAKPOINT-VALUES/STANDARD" :di)))
1706        (cond (sym (funcall sym sigcontext))
1707              (t (funcall 'di::get-function-end-breakpoint-values sigcontext)))))
1708    
1709    (defun debug-function-returns (debug-fun)
1710      "Return the return style of DEBUG-FUN."
1711      (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1712        (c::compiled-debug-function-returns cdfun)))
1713    
1714    (define-condition breakpoint (simple-condition)
1715      ((message :initarg :message :reader breakpoint.message)
1716       (values  :initarg :values  :reader breakpoint.values))
1717      (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1718    
1719    (defimplementation condition-extras (condition)
1720      (typecase condition
1721        (breakpoint
1722         ;; pop up the source buffer
1723         `((:show-frame-source 0)))
1724        (t '())))
1725    
1726    (defun signal-breakpoint (breakpoint frame)
1727      "Signal a breakpoint condition for BREAKPOINT in FRAME.
1728    Try to create a informative message."
1729      (flet ((brk (values fstring &rest args)
1730               (let ((msg (apply #'format nil fstring args))
1731                     (debug:*stack-top-hint* frame))
1732                 (break 'breakpoint :message msg :values values))))
1733        (with-struct (di::breakpoint- kind what) breakpoint
1734          (case kind
1735            (:code-location
1736             (case (di:code-location-kind what)
1737               ((:single-value-return :known-return :unknown-return)
1738                (let ((values (breakpoint-values breakpoint)))
1739                  (brk values "Return value: ~{~S ~}" values)))
1740               (t
1741                #+(or)
1742                (when (eq (di:code-location-kind what) :call-site)
1743                  (call-site-function breakpoint frame))
1744                (brk nil "Breakpoint: ~S ~S"
1745                     (di:code-location-kind what)
1746                     (di::compiled-code-location-pc what)))))
1747            (:function-start
1748             (brk nil "Function start breakpoint"))
1749            (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1750    
1751    (defimplementation sldb-break-at-start (fname)
1752      (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1753        (cond ((not debug-fun)
1754               `(:error ,(format nil "~S has no debug-function" fname)))
1755              (t
1756               (flet ((hook (frame bp &optional args cookie)
1757                        (declare (ignore args cookie))
1758                        (signal-breakpoint bp frame)))
1759                 (let ((bp (di:make-breakpoint #'hook debug-fun
1760                                               :kind :function-start)))
1761                   (di:activate-breakpoint bp)
1762                   `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1763    
1764    (defun frame-cfp (frame)
1765      "Return the Control-Stack-Frame-Pointer for FRAME."
1766      (etypecase frame
1767        (di::compiled-frame (di::frame-pointer frame))
1768        ((or di::interpreted-frame null) -1)))
1769    
1770    (defun frame-ip (frame)
1771      "Return the (absolute) instruction pointer and the relative pc of FRAME."
1772      (if (not frame)
1773          -1
1774          (let ((debug-fun (di::frame-debug-function frame)))
1775            (etypecase debug-fun
1776              (di::compiled-debug-function
1777               (let* ((code-loc (di:frame-code-location frame))
1778                      (component (di::compiled-debug-function-component debug-fun))
1779                      (pc (di::compiled-code-location-pc code-loc))
1780                      (ip (sys:without-gcing
1781                           (sys:sap-int
1782                            (sys:sap+ (kernel:code-instructions component) pc)))))
1783                 (values ip pc)))
1784              ((or di::bogus-debug-function di::interpreted-debug-function)
1785               -1)))))
1786    
1787    (defun frame-registers (frame)
1788      "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1789      (let* ((cfp (frame-cfp frame))
1790             (csp (frame-cfp (di::frame-up frame)))
1791             (ip (frame-ip frame))
1792             (ocfp (frame-cfp (di::frame-down frame)))
1793             (lra (frame-ip (di::frame-down frame))))
1794        (values csp cfp ip ocfp lra)))
1795    
1796    (defun print-frame-registers (frame-number)
1797      (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1798        (flet ((fixnum (p) (etypecase p
1799                             (integer p)
1800                             (sys:system-area-pointer (sys:sap-int p)))))
1801          (apply #'format t "~
1802    CSP  =  ~X
1803    CFP  =  ~X
1804    IP   =  ~X
1805    OCFP =  ~X
1806    LRA  =  ~X~%" (mapcar #'fixnum
1807                          (multiple-value-list (frame-registers frame)))))))
1808    
1809    
1810    (defimplementation disassemble-frame (frame-number)
1811      "Return a string with the disassembly of frames code."
1812      (print-frame-registers frame-number)
1813      (terpri)
1814      (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1815             (debug-fun (di::frame-debug-function frame)))
1816        (etypecase debug-fun
1817          (di::compiled-debug-function
1818           (let* ((component (di::compiled-debug-function-component debug-fun))
1819                  (fun (di:debug-function-function debug-fun)))
1820             (if fun
1821                 (disassemble fun)
1822                 (disassem:disassemble-code-component component))))
1823          (di::bogus-debug-function
1824           (format t "~%[Disassembling bogus frames not implemented]")))))
1825    
1826    
1827  ;;;; Inspecting  ;;;; Inspecting
1828    
 (defvar *inspectee*)  
 (defvar *inspectee-parts*)  
 (defvar *inspector-stack* '())  
 (defvar *inspector-history* (make-array 10 :adjustable t :fill-pointer 0))  
 (defvar *inspect-length* 30)  
   
 (defun reset-inspector ()  
   (setq *inspectee* nil)  
   (setq *inspectee-parts* nil)  
   (setq *inspector-stack* nil)  
   (setf (fill-pointer *inspector-history*) 0))  
   
 (defslimefun init-inspector (string)  
   (reset-inspector)  
   (inspect-object (eval (from-string string))))  
   
 (defun print-part-to-string (value)  
   (let ((*print-pretty* nil))  
     (let ((string (to-string value))  
           (pos (position value *inspector-history*)))  
       (if pos  
           (format nil "#~D=~A" pos string)  
           string))))  
   
 (defun inspect-object (object)  
   (push (setq *inspectee* object) *inspector-stack*)  
   (unless (find object *inspector-history*)  
     (vector-push-extend object *inspector-history*))  
   (multiple-value-bind (text parts) (inspected-parts object)  
     (setq *inspectee-parts* parts)  
       (list :text text  
             :type (to-string (type-of object))  
             :primitive-type (describe-primitive-type object)  
             :parts (loop for (label . value) in parts  
                          collect (cons label  
                                        (print-part-to-string value))))))  
1829  (defconstant +lowtag-symbols+  (defconstant +lowtag-symbols+
1830    '(vm:even-fixnum-type    '(vm:even-fixnum-type
1831      vm:function-pointer-type      vm:function-pointer-type
# Line 1152  stack." Line 1834  stack."
1834      vm:odd-fixnum-type      vm:odd-fixnum-type
1835      vm:instance-pointer-type      vm:instance-pointer-type
1836      vm:other-immediate-1-type      vm:other-immediate-1-type
1837      vm:other-pointer-type))      vm:other-pointer-type)
1838      "Names of the constants that specify type tags.
1839    The `symbol-value' of each element is a type tag.")
1840    
1841  (defconstant +header-type-symbols+  (defconstant +header-type-symbols+
1842    ;; Is there a convinient place for all those constants?    (labels ((suffixp (suffix string)
1843    (flet ((tail-comp (string tail)               (and (>= (length string) (length suffix))
1844             (and (>= (length string) (length tail))                    (string= string suffix :start1 (- (length string)
1845                  (string= string tail :start1 (- (length string)                                                      (length suffix)))))
1846                                                  (length tail))))))             (header-type-symbol-p (x)
1847      (remove-if-not               (and (suffixp "-TYPE" (symbol-name x))
1848       (lambda (x) (and (tail-comp (symbol-name x) "-TYPE")                    (not (member x +lowtag-symbols+))
1849                        (not (member x +lowtag-symbols+))                    (boundp x)
1850                        (boundp x)                    (typep (symbol-value x) 'fixnum))))
1851                        (typep (symbol-value x) 'fixnum)))      (remove-if-not #'header-type-symbol-p
1852       (append (apropos-list "-TYPE" "VM" t)                     (append (apropos-list "-TYPE" "VM")
1853               (apropos-list "-TYPE" "BIGNUM" t)))))                             (apropos-list "-TYPE" "BIGNUM"))))
1854      "A list of names of the type codes in boxed objects.")
1855    
1856  (defun describe-primitive-type (object)  (defimplementation describe-primitive-type (object)
1857    (with-output-to-string (*standard-output*)    (with-output-to-string (*standard-output*)
1858      (let* ((lowtag (kernel:get-lowtag object))      (let* ((lowtag (kernel:get-lowtag object))
1859             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1860        (format t "[lowtag: ~A" lowtag-symbol)        (format t "lowtag: ~A" lowtag-symbol)
1861        (cond ((member lowtag (list vm:other-pointer-type        (when (member lowtag (list vm:other-pointer-type
1862                                    vm:function-pointer-type                                   vm:function-pointer-type
1863                                    vm:other-immediate-0-type                                   vm:other-immediate-0-type
1864                                    vm:other-immediate-1-type                                   vm:other-immediate-1-type
1865                                    ))                                   ))
1866               (let* ((type (kernel:get-type object))          (let* ((type (kernel:get-type object))
1867                      (type-symbol (find type +header-type-symbols+                 (type-symbol (find type +header-type-symbols+
1868                                         :key #'symbol-value)))                                    :key #'symbol-value)))
1869                 (format t ", type: ~A]" type-symbol)))            (format t ", type: ~A" type-symbol))))))
             (t (format t "]"))))))  
   
 (defun nth-part (index)  
   (cdr (nth index *inspectee-parts*)))  
   
 (defslimefun inspect-nth-part (index)  
   (inspect-object (nth-part index)))  
   
 (defslimefun inspector-pop ()  
   "Drop the inspector stack and inspect the second element.  Return  
 nil if there's no second element."  
   (cond ((cdr *inspector-stack*)  
          (pop *inspector-stack*)  
          (inspect-object (pop *inspector-stack*)))  
         (t nil)))  
   
 (defslimefun inspector-next ()  
   "Inspect the next element in the *inspector-history*."  
   (let ((position (position *inspectee* *inspector-history*)))  
     (cond ((= (1+ position) (length *inspector-history*))  
            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."))  
1870    
1871  (defmethod inspected-parts (o)  (defmethod emacs-inspect ((o t))
1872    (cond ((di::indirect-value-cell-p o)    (cond ((di::indirect-value-cell-p o)
1873           (inspected-parts-of-value-cell o))           `("Value: " (:value ,(c:value-cell-ref o))))
1874            ((alien::alien-value-p o)
1875             (inspect-alien-value o))
1876          (t          (t
1877           (destructuring-bind (text labeledp . parts)           (cmucl-inspect o))))
              (inspect::describe-parts o)  
            (let ((parts (if labeledp  
                             (loop for (label . value) in parts  
                                   collect (cons (string label) value))  
                             (loop for value in parts  
                                   for i from 0  
                                   collect (cons (format nil "~D" i) value)))))  
              (values text parts))))))  
   
 (defun inspected-parts-of-value-cell (o)  
   (values (format nil "~A~% is a value cell." o)  
           (list (cons "Value" (c:value-cell-ref o)))))  
   
 ;; borrowed from sbcl  
 (defmethod inspected-parts ((object cons))  
   (if (consp (cdr object))  
       (inspected-parts-of-nontrivial-list object)  
       (inspected-parts-of-simple-cons object)))  
   
 (defun inspected-parts-of-simple-cons (object)  
   (values "The object is a CONS."  
           (list (cons (string 'car) (car object))  
                 (cons (string 'cdr) (cdr object)))))  
   
 (defun inspected-parts-of-nontrivial-list (object)  
   (let ((length 0)  
         (in-list object)  
         (reversed-elements nil))  
     (flet ((done (description-format)  
              (return-from inspected-parts-of-nontrivial-list  
                (values (format nil description-format length)  
                        (nreverse reversed-elements)))))  
       (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.~%")))))))  
1878    
1879  (defmethod inspected-parts ((o function))  (defun cmucl-inspect (o)
1880      (destructuring-bind (text labeledp . parts) (inspect::describe-parts o)
1881        (list* (format nil "~A~%" text)
1882               (if labeledp
1883                   (loop for (label . value) in parts
1884                         append (label-value-line label value))
1885                   (loop for value in parts  for i from 0
1886                         append (label-value-line i value))))))
1887    
1888    (defmethod emacs-inspect ((o function))
1889    (let ((header (kernel:get-type o)))    (let ((header (kernel:get-type o)))
1890      (cond ((= header vm:function-header-type)      (cond ((= header vm:function-header-type)
1891             (values             (append (label-value-line*
1892              (format nil "~A~% is a function." o)                      ("Self" (kernel:%function-self o))
1893              (list (cons "Self" (kernel:%function-self o))                      ("Next" (kernel:%function-next o))
1894                    (cons "Next" (kernel:%function-next o))                      ("Name" (kernel:%function-name o))
1895                    (cons "Name" (kernel:%function-name o))                      ("Arglist" (kernel:%function-arglist o))
1896                    (cons "Arglist" (kernel:%function-arglist o))                      ("Type" (kernel:%function-type o))
1897                    (cons "Type" (kernel:%function-type o))                      ("Code" (kernel:function-code-header o)))
1898                    (cons "Code Object" (kernel:function-code-header o)))))                     (list
1899            ((= header vm:closure-header-type)                      (with-output-to-string (s)
1900             (values (format nil "~A~% is a closure." o)                        (disassem:disassemble-function o :stream s)))))
1901                     (list*            ((= header vm:closure-header-type)
1902                      (cons "Function" (kernel:%closure-function o))             (list* (format nil "~A is a closure.~%" o)
1903                      (loop for i from 0 below (- (kernel:get-closure-length o)                    (append
1904                                                  (1- vm:closure-info-offset))                     (label-value-line "Function" (kernel:%closure-function o))
1905                            collect (cons (format nil "~D" i)                     `("Environment:" (:newline))
1906                                          (kernel:%closure-index-ref o i))))))                     (loop for i from 0 below (1- (kernel:get-closure-length o))
1907            (t (call-next-method o)))))                           append (label-value-line
1908                                     i (kernel:%closure-index-ref o i))))))
1909  (defmethod inspected-parts ((o kernel:code-component))            ((eval::interpreted-function-p o)
1910    (values (format nil "~A~% is a code data-block." o)             (cmucl-inspect o))
1911            `(("First entry point" . ,(kernel:%code-entry-points o))            (t
1912              ,@(loop for i from vm:code-constants-offset             (call-next-method)))))
1913                      below (kernel:get-header-data o)  
1914                      collect (cons (format nil "Constant#~D" i)  (defmethod emacs-inspect ((o kernel:funcallable-instance))
1915                                    (kernel:code-header-ref o i)))    (append (label-value-line*
1916              ("Debug info" . ,(kernel:%code-debug-info o))             (:function (kernel:%funcallable-instance-function o))
1917              ("Instructions"  . ,(kernel:code-instructions o)))))             (:lexenv  (kernel:%funcallable-instance-lexenv o))
1918               (:layout  (kernel:%funcallable-instance-layout o)))
1919  (defmethod inspected-parts ((o kernel:fdefn))            (cmucl-inspect o)))
1920    (values (format nil "~A~% is a fdefn object." o)  
1921            `(("Name" . ,(kernel:fdefn-name o))  (defmethod emacs-inspect ((o kernel:code-component))
1922              ("Function" . ,(kernel:fdefn-function o)))))    (append
1923       (label-value-line*
1924  ;;; Local Variables:      ("code-size" (kernel:%code-code-size o))
1925  ;;; eval: (font-lock-add-keywords 'lisp-mode '(("(\\(defslimefun\\)\\s +\\(\\(\\w\\|\\s_\\)+\\)"  (1 font-lock-keyword-face) (2 font-lock-function-name-face))))      ("entry-points" (kernel:%code-entry-points o))
1926  ;;; End:      ("debug-info" (kernel:%code-debug-info o))
1927        ("trace-table-offset" (kernel:code-header-ref
1928                               o vm:code-trace-table-offset-slot)))
1929       `("Constants:" (:newline))
1930       (loop for i from vm:code-constants-offset
1931             below (kernel:get-header-data o)
1932             append (label-value-line i (kernel:code-header-ref o i)))
1933       `("Code:"
1934         (:newline)
1935         , (with-output-to-string (*standard-output*)
1936             (cond ((c::compiled-debug-info-p (kernel:%code-debug-info o))
1937                    (disassem:disassemble-code-component o))
1938                   ((or
1939                     (c::debug-info-p (kernel:%code-debug-info o))
1940                     (consp (kernel:code-header-ref
1941                             o vm:code-trace-table-offset-slot)))
1942                    (c:disassem-byte-component o))
1943                   (t
1944                    (disassem:disassemble-memory
1945                     (disassem::align
1946                      (+ (logandc2 (kernel:get-lisp-obj-address o)
1947                                   vm:lowtag-mask)
1948                         (* vm:code-constants-offset vm:word-bytes))
1949                      (ash 1 vm:lowtag-bits))
1950                     (ash (kernel:%code-code-size o) vm:word-shift))))))))
1951    
1952    (defmethod emacs-inspect ((o kernel:fdefn))
1953      (label-value-line*
1954       ("name" (kernel:fdefn-name o))
1955       ("function" (kernel:fdefn-function o))
1956       ("raw-addr" (sys:sap-ref-32
1957                    (sys:int-sap (kernel:get-lisp-obj-address o))
1958                    (* vm:fdefn-raw-addr-slot vm:word-bytes)))))
1959    
1960    #+(or)
1961    (defmethod emacs-inspect ((o array))
1962      (if (typep o 'simple-array)
1963          (call-next-method)
1964          (label-value-line*
1965           (:header (describe-primitive-type o))
1966           (:rank (array-rank o))
1967           (:fill-pointer (kernel:%array-fill-pointer o))
1968           (:fill-pointer-p (kernel:%array-fill-pointer-p o))
1969           (:elements (kernel:%array-available-elements o))
1970           (:data (kernel:%array-data-vector o))
1971           (:displacement (kernel:%array-displacement o))
1972           (:displaced-p (kernel:%array-displaced-p o))
1973           (:dimensions (array-dimensions o)))))
1974    
1975    (defmethod emacs-inspect ((o simple-vector))
1976      (append
1977       (label-value-line*
1978        (:header (describe-primitive-type o))
1979        (:length (c::vector-length o)))
1980       (loop for i below (length o)
1981             append (label-value-line i (aref o i)))))
1982    
1983    (defun inspect-alien-record (alien)
1984      (with-struct (alien::alien-value- sap type) alien
1985        (with-struct (alien::alien-record-type- kind name fields) type
1986          (append
1987           (label-value-line*
1988            (:sap sap)
1989            (:kind kind)
1990            (:name name))
1991           (loop for field in fields
1992                 append (let ((slot (alien::alien-record-field-name field)))
1993                          (declare (optimize (speed 0)))
1994                          (label-value-line slot (alien:slot alien slot))))))))
1995    
1996    (defun inspect-alien-pointer (alien)
1997      (with-struct (alien::alien-value- sap type) alien
1998        (label-value-line*
1999         (:sap sap)
2000         (:type type)
2001         (:to (alien::deref alien)))))
2002    
2003    (defun inspect-alien-value (alien)
2004      (typecase (alien::alien-value-type alien)
2005        (alien::alien-record-type (inspect-alien-record alien))
2006        (alien::alien-pointer-type (inspect-alien-pointer alien))
2007        (t (cmucl-inspect alien))))
2008    
2009    ;;;; Profiling
2010    (defimplementation profile (fname)
2011      (eval `(profile:profile ,fname)))
2012    
2013    (defimplementation unprofile (fname)
2014      (eval `(profile:unprofile ,fname)))
2015    
2016    (defimplementation unprofile-all ()
2017      (eval `(profile:unprofile))
2018      "All functions unprofiled.")
2019    
2020    (defimplementation profile-report ()
2021      (eval `(profile:report-time)))
2022    
2023    (defimplementation profile-reset ()
2024      (eval `(profile:reset-time))
2025      "Reset profiling counters.")
2026    
2027    (defimplementation profiled-functions ()
2028      profile:*timed-functions*)
2029    
2030    (defimplementation profile-package (package callers methods)
2031      (profile:profile-all :package package
2032                           :callers-p callers
2033                           #-cmu18e :methods #-cmu18e methods))
2034    
2035    
2036    ;;;; Multiprocessing
2037    
2038    #+mp
2039    (progn
2040      (defimplementation initialize-multiprocessing (continuation)
2041        (mp::init-multi-processing)
2042        (mp:make-process continuation :name "swank")
2043        ;; Threads magic: this never returns! But top-level becomes
2044        ;; available again.
2045        (unless mp::*idle-process*
2046          (mp::startup-idle-and-top-level-loops)))
2047    
2048      (defimplementation spawn (fn &key name)
2049        (mp:make-process fn :name (or name "Anonymous")))
2050    
2051      (defvar *thread-id-counter* 0)
2052    
2053      (defimplementation thread-id (thread)
2054        (or (getf (mp:process-property-list thread) 'id)
2055            (setf (getf (mp:process-property-list thread) 'id)
2056                  (incf *thread-id-counter*))))
2057    
2058      (defimplementation find-thread (id)
2059        (find id (all-threads)
2060              :key (lambda (p) (getf (mp:process-property-list p) 'id))))
2061    
2062      (defimplementation thread-name (thread)
2063        (mp:process-name thread))
2064    
2065      (defimplementation thread-status (thread)
2066        (mp:process-whostate thread))
2067    
2068      (defimplementation current-thread ()
2069        mp:*current-process*)
2070    
2071      (defimplementation all-threads ()
2072        (copy-list mp:*all-processes*))
2073    
2074      (defimplementation interrupt-thread (thread fn)
2075        (mp:process-interrupt thread fn))
2076    
2077      (defimplementation kill-thread (thread)
2078        (mp:destroy-process thread))
2079    
2080      (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
2081    
2082      (defstruct (mailbox (:conc-name mailbox.))
2083        (mutex (mp:make-lock "process mailbox"))
2084        (queue '() :type list))
2085    
2086      (defun mailbox (thread)
2087        "Return THREAD's mailbox."
2088        (mp:with-lock-held (*mailbox-lock*)
2089          (or (getf (mp:process-property-list thread) 'mailbox)
2090              (setf (getf (mp:process-property-list thread) 'mailbox)
2091                    (make-mailbox)))))
2092    
2093      (defimplementation send (thread message)
2094        (check-slime-interrupts)
2095        (let* ((mbox (mailbox thread)))
2096          (mp:with-lock-held ((mailbox.mutex mbox))
2097            (setf (mailbox.queue mbox)
2098                  (nconc (mailbox.queue mbox) (list message))))))
2099    
2100      (defimplementation receive-if (test &optional timeout)
2101        (let ((mbox (mailbox mp:*current-process*)))
2102          (assert (or (not timeout) (eq timeout t)))
2103          (loop
2104           (check-slime-interrupts)
2105           (mp:with-lock-held ((mailbox.mutex mbox))
2106             (let* ((q (mailbox.queue mbox))
2107                    (tail (member-if test q)))
2108               (when tail
2109                 (setf (mailbox.queue mbox)
2110                       (nconc (ldiff q tail) (cdr tail)))
2111                 (return (car tail)))))
2112           (when (eq timeout t) (return (values nil t)))
2113           (mp:process-wait-with-timeout
2114            "receive-if" 0.5 (lambda () (some test (mailbox.queue mbox)))))))
2115    
2116    
2117      ) ;; #+mp
2118    
2119    
2120    
2121    ;;;; GC hooks
2122    ;;;
2123    ;;; Display GC messages in the echo area to avoid cluttering the
2124    ;;; normal output.
2125    ;;;
2126    
2127    ;; this should probably not be here, but where else?
2128    (defun background-message (message)
2129      (funcall (find-symbol (string :background-message) :swank)
2130               message))
2131    
2132    (defun print-bytes (nbytes &optional stream)
2133      "Print the number NBYTES to STREAM in KB, MB, or GB units."
2134      (let ((names '((0 bytes) (10 kb) (20 mb) (30 gb) (40 tb) (50 eb))))
2135        (multiple-value-bind (power name)
2136            (loop for ((p1 n1) (p2 n2)) on names
2137                  while n2 do
2138                  (when (<= (expt 2 p1) nbytes (1- (expt 2 p2)))
2139                    (return (values p1 n1))))
2140          (cond (name
2141                 (format stream "~,1F ~A" (/ nbytes (expt 2 power)) name))
2142                (t
2143                 (format stream "~:D bytes" nbytes))))))
2144    
2145    (defconstant gc-generations 6)
2146    
2147    #+gencgc
2148    (defun generation-stats ()
2149      "Return a string describing the size distribution among the generations."
2150      (let* ((alloc (loop for i below gc-generations
2151                          collect (lisp::gencgc-stats i)))
2152             (sum (coerce (reduce #'+ alloc) 'float)))
2153        (format nil "~{~3F~^/~}"
2154                (mapcar (lambda (size) (/ size sum))
2155                        alloc))))
2156    
2157    (defvar *gc-start-time* 0)
2158    
2159    (defun pre-gc-hook (bytes-in-use)
2160      (setq *gc-start-time* (get-internal-real-time))
2161      (let ((msg (format nil "[Commencing GC with ~A in use.]"
2162                         (print-bytes bytes-in-use))))
2163        (background-message msg)))
2164    
2165    (defun post-gc-hook (bytes-retained bytes-freed trigger)
2166      (declare (ignore trigger))
2167      (let* ((seconds (/ (- (get-internal-real-time) *gc-start-time*)
2168                         internal-time-units-per-second))
2169             (msg (format nil "[GC done. ~A freed  ~A retained  ~A  ~4F sec]"
2170                         (print-bytes bytes-freed)
2171                         (print-bytes bytes-retained)
2172                         #+gencgc(generation-stats)
2173                         #-gencgc""
2174                         seconds)))
2175        (background-message msg)))
2176    
2177    (defun install-gc-hooks ()
2178      (setq ext:*gc-notify-before* #'pre-gc-hook)
2179      (setq ext:*gc-notify-after* #'post-gc-hook))
2180    
2181    (defun remove-gc-hooks ()
2182      (setq ext:*gc-notify-before* #'lisp::default-gc-notify-before)
2183      (setq ext:*gc-notify-after* #'lisp::default-gc-notify-after))
2184    
2185    (defvar *install-gc-hooks* t
2186      "If non-nil install GC hooks")
2187    
2188    (defimplementation emacs-connected ()
2189      (when *install-gc-hooks*
2190        (install-gc-hooks)))
2191    
2192    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2193    ;;Trace implementations
2194    ;;In CMUCL, we have:
2195    ;; (trace <name>)
2196    ;; (trace (method <name> <qualifier>? (<specializer>+)))
2197    ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2198    ;; <name> can be a normal name or a (setf name)
2199    
2200    (defun tracedp (spec)
2201      (member spec (eval '(trace)) :test #'equal))
2202    
2203    (defun toggle-trace-aux (spec &rest options)
2204      (cond ((tracedp spec)
2205             (eval `(untrace ,spec))
2206             (format nil "~S is now untraced." spec))
2207            (t
2208             (eval `(trace ,spec ,@options))
2209             (format nil "~S is now traced." spec))))
2210    
2211    (defimplementation toggle-trace (spec)
2212      (ecase (car spec)
2213        ((setf)
2214         (toggle-trace-aux spec))
2215        ((:defgeneric)
2216         (let ((name (second spec)))
2217           (toggle-trace-aux name :methods name)))
2218        ((:defmethod)
2219         (cond ((fboundp `(method ,@(cdr spec)))
2220                (toggle-trace-aux `(method ,(cdr spec))))
2221               ;; Man, is this ugly
2222               ((fboundp `(pcl::fast-method ,@(cdr spec)))
2223                (toggle-trace-aux `(pcl::fast-method ,@(cdr spec))))
2224               (t
2225                (error 'undefined-function :name (cdr spec)))))
2226        ((:call)
2227         (destructuring-bind (caller callee) (cdr spec)
2228           (toggle-trace-aux (process-fspec callee)
2229                             :wherein (list (process-fspec caller)))))
2230        ;; doesn't work properly
2231        ;; ((:labels :flet) (toggle-trace-aux (process-fspec spec)))
2232        ))
2233    
2234    (defun process-fspec (fspec)
2235      (cond ((consp fspec)
2236             (ecase (first fspec)
2237               ((:defun :defgeneric) (second fspec))
2238               ((:defmethod)
2239                `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2240               ((:labels) `(labels ,(third fspec) ,(process-fspec (second fspec))))
2241               ((:flet) `(flet ,(third fspec) ,(process-fspec (second fspec))))))
2242            (t
2243             fspec)))
2244    
2245    ;;; Weak datastructures
2246    
2247    (defimplementation make-weak-key-hash-table (&rest args)
2248      (apply #'make-hash-table :weak-p t args))
2249    
2250    
2251    ;;; Save image
2252    
2253    (defimplementation save-image (filename &optional restart-function)
2254      (multiple-value-bind (pid error) (unix:unix-fork)
2255        (when (not pid) (error "fork: ~A" (unix:get-unix-error-msg error)))
2256        (cond ((= pid 0)
2257               (let ((args `(,filename
2258                             ,@(if restart-function
2259                                   `((:init-function ,restart-function))))))
2260                 (apply #'ext:save-lisp args)))
2261              (t
2262               (let ((status (waitpid pid)))
2263                 (destructuring-bind (&key exited? status &allow-other-keys) status
2264                   (assert (and exited? (equal status 0)) ()
2265                           "Invalid exit status: ~a" status)))))))
2266    
2267    (defun waitpid (pid)
2268      (alien:with-alien ((status c-call:int))
2269        (let ((code (alien:alien-funcall
2270                     (alien:extern-alien
2271                      waitpid (alien:function c-call:int c-call:int
2272                                              (* c-call:int) c-call:int))
2273                     pid (alien:addr status) 0)))
2274          (cond ((= code -1) (error "waitpid: ~A" (unix:get-unix-error-msg)))
2275                (t (assert (= code pid))
2276                   (decode-wait-status status))))))
2277    
2278    (defun decode-wait-status (status)
2279      (let ((output (with-output-to-string (s)
2280                      (call-program (list (process-status-program)
2281                                          (format nil "~d" status))
2282                                    :output s))))
2283        (read-from-string output)))
2284    
2285    (defun call-program (args &key output)
2286      (destructuring-bind (program &rest args) args
2287        (let ((process (ext:run-program program args :output output)))
2288          (when (not program) (error "fork failed"))
2289          (unless (and (eq (ext:process-status process) :exited)
2290                       (= (ext:process-exit-code process) 0))
2291            (error "Non-zero exit status")))))
2292    
2293    (defvar *process-status-program* nil)
2294    
2295    (defun process-status-program ()
2296      (or *process-status-program*
2297          (setq *process-status-program*
2298                (compile-process-status-program))))
2299    
2300    (defun compile-process-status-program ()
2301      (let ((infile (system::pick-temporary-file-name
2302                     "/tmp/process-status~d~c.c")))
2303        (with-open-file (stream infile :direction :output :if-exists :supersede)
2304          (format stream "
2305    #include <stdio.h>
2306    #include <stdlib.h>
2307    #include <sys/types.h>
2308    #include <sys/wait.h>
2309    #include <assert.h>
2310    
2311    #define FLAG(value) (value ? \"t\" : \"nil\")
2312    
2313    int main (int argc, char** argv) {
2314      assert (argc == 2);
2315      {
2316        char* endptr = NULL;
2317        char* arg = argv[1];
2318        long int status = strtol (arg, &endptr, 10);
2319        assert (endptr != arg && *endptr == '\\0');
2320        printf (\"(:exited? %s :status %d :signal? %s :signal %d :coredump? %s\"
2321                \" :stopped? %s :stopsig %d)\\n\",
2322                FLAG(WIFEXITED(status)), WEXITSTATUS(status),
2323                FLAG(WIFSIGNALED(status)), WTERMSIG(status),
2324                FLAG(WCOREDUMP(status)),
2325                FLAG(WIFSTOPPED(status)), WSTOPSIG(status));
2326        fflush (NULL);
2327        return 0;
2328      }
2329    }
2330    ")
2331          (finish-output stream))
2332        (let* ((outfile (system::pick-temporary-file-name))
2333               (args (list "cc" "-o" outfile infile)))
2334          (warn "Running cc: ~{~a ~}~%" args)
2335          (call-program args :output t)
2336          (delete-file infile)
2337          outfile)))
2338    
2339    ;; (save-image "/tmp/x.core")
2340    
2341    ;; Local Variables:
2342    ;; pbook-heading-regexp:    "^;;;\\(;+\\)"
2343    ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2344    ;; End:

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

  ViewVC Help
Powered by ViewVC 1.1.5