/[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.126 by heller, Thu Oct 28 21:34:36 2004 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 latest released 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?]  #+cmu18e
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    (defimplementation create-socket (host port)
80      #+ppc (declare (ignore host))
81      (ext:create-inet-listener port :stream
82                                :reuse-address t
83                                #-ppc :host #-ppc (resolve-hostname host)))
84    
85    (defimplementation local-port (socket)
86      (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
87    
88    (defimplementation close-socket (socket)
89      (sys:invalidate-descriptor socket)
90      (ext:close-socket (socket-fd socket)))
91    
92    (defimplementation accept-connection (socket)
93      (make-socket-io-stream (ext:accept-tcp-connection socket)))
94    
95    ;;;;; Sockets
96    
97  (defun resolve-hostname (name)  (defun socket-fd (socket)
98    (let* ((hostent (ext:lookup-host-entry name))    "Return the filedescriptor for the socket represented by SOCKET."
99      (etypecase socket
100        (fixnum socket)
101        (sys:fd-stream (sys:fd-stream-fd socket))))
102    
103    (defun resolve-hostname (hostname)
104      "Return the IP address of HOSTNAME as an integer."
105      (let* ((hostent (ext:lookup-host-entry hostname))
106           (address (car (ext:host-entry-addr-list hostent))))           (address (car (ext:host-entry-addr-list hostent))))
107      (ext:htonl address)))      (ext:htonl address)))
108    
109  (defun create-swank-server (port &key (reuse-address t)  (defun make-socket-io-stream (fd)
110                              (address "localhost"))    "Create a new input/output fd-stream for FD."
111    "Create a SWANK TCP server."    (sys:make-fd-stream fd :input t :output t :element-type 'base-char))
112    (let* ((ip (resolve-hostname address))  
113           (fd (ext:create-inet-listener port :stream  ;;;;; Signal-driven I/O
114                                         :reuse-address reuse-address  
115                                         :host ip)))  (defvar *sigio-handlers* '()
116      (system:add-fd-handler fd :input #'accept-connection)    "List of (key . function) pairs.
117      (nth-value 1 (ext::get-socket-host-and-port fd))))  All functions are called on SIGIO, and the key is used for removing
118    specific functions.")
119  (defun accept-connection (socket)  
120    "Accept one Swank TCP connection on SOCKET and then close it."  (defun set-sigio-handler ()
121    (setup-request-handler (ext:accept-tcp-connection socket))    (sys:enable-interrupt :sigio (lambda (signal code scp)
122    (sys:invalidate-descriptor socket)                                   (sigio-handler signal code scp))))
123    (unix:unix-close socket))  
124    (defun sigio-handler (signal code scp)
125      (declare (ignore signal code scp))
126      (mapc #'funcall (mapcar #'cdr *sigio-handlers*)))
127    
128    (defun fcntl (fd command arg)
129      "fcntl(2) - manipulate a file descriptor."
130      (multiple-value-bind (ok error) (unix:unix-fcntl fd command arg)
131        (unless ok (error "fcntl: ~A" (unix:get-unix-error-msg error)))))
132    
133    (defimplementation add-sigio-handler (socket fn)
134      (set-sigio-handler)
135      (let ((fd (socket-fd socket)))
136        (fcntl fd unix:f-setown (unix:unix-getpid))
137        (fcntl fd unix:f-setfl unix:fasync)
138        (push (cons fd fn) *sigio-handlers*)))
139    
140    (defimplementation remove-sigio-handlers (socket)
141      (let ((fd (socket-fd socket)))
142        (setf *sigio-handlers* (remove fd *sigio-handlers* :key #'car))
143        (sys:invalidate-descriptor fd))
144      (close socket))
145    
146    ;;;;; SERVE-EVENT
147    
148    (defimplementation add-fd-handler (socket fn)
149      (let ((fd (socket-fd socket)))
150        (sys:add-fd-handler fd :input (lambda (_)
151                                        _
152                                        (funcall fn)))))
153    
154  (defun open-stream-to-emacs ()  (defimplementation remove-fd-handlers (socket)
155    "Return an output-stream to Emacs' output buffer."    (sys:invalidate-descriptor (socket-fd socket)))
   (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))  
156    
157    
158  ;;;; Stream handling  ;;;; Stream handling
159    ;;; XXX: How come we don't use Gray streams in CMUCL too? -luke (15/May/2004)
160    
161    (defimplementation make-fn-streams (input-fn output-fn)
162      (let* ((output (make-slime-output-stream output-fn))
163             (input  (make-slime-input-stream input-fn output)))
164        (values input output)))
165    
166  (defstruct (slime-output-stream  (defstruct (slime-output-stream
167               (:include lisp::lisp-stream               (:include lisp::lisp-stream
168                         (lisp::misc #'sos/misc)                         (lisp::misc #'sos/misc)
169                         (lisp::out #'sos/out)                         (lisp::out #'sos/out)
170                         (lisp::sout #'sos/sout))                         (lisp::sout #'sos/sout))
171               (:conc-name sos.)               (:conc-name sos.)
172               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
173                 (:constructor make-slime-output-stream (output-fn)))
174      (output-fn nil :type function)
175    (buffer (make-string 512) :type string)    (buffer (make-string 512) :type string)
176    (index 0 :type kernel:index)    (index 0 :type kernel:index)
177    (column 0 :type kernel:index))    (column 0 :type kernel:index))
# Line 109  The request is read from the socket as a Line 187  The request is read from the socket as a
187      (setf (sos.index stream) (1+ index))      (setf (sos.index stream) (1+ index))
188      (incf (sos.column stream))      (incf (sos.column stream))
189      (when (char= #\newline char)      (when (char= #\newline char)
190        (setf (sos.column stream) 0))        (setf (sos.column stream) 0)
191          (force-output stream))
192      (when (= index (1- (length buffer)))      (when (= index (1- (length buffer)))
193        (force-output stream)))        (force-output stream)))
194    char)    char)
# Line 117  The request is read from the socket as a Line 196  The request is read from the socket as a
196  (defun sos/sout (stream string start end)  (defun sos/sout (stream string start end)
197    (loop for i from start below end    (loop for i from start below end
198          do (sos/out stream (aref string i))))          do (sos/out stream (aref string i))))
199    
200  (defun sos/misc (stream operation &optional arg1 arg2)  (defun sos/misc (stream operation &optional arg1 arg2)
201    (declare (ignore arg1 arg2))    (declare (ignore arg1 arg2))
202    (case operation    (case operation
203      ((:force-output :finish-output)      ((:force-output :finish-output)
204       (let ((end (sos.index stream)))       (let ((end (sos.index stream)))
205         (unless (zerop end)         (unless (zerop end)
206           (send-to-emacs `(:read-output ,(subseq (sos.buffer stream) 0 end)))           (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
207           (setf (sos.index stream) 0))))           (setf (sos.index stream) 0))))
208      (:charpos (sos.column stream))      (:charpos (sos.column stream))
209      (:line-length 75)      (:line-length 75)
210      (:file-position nil)      (:file-position nil)
# Line 135  The request is read from the socket as a Line 214  The request is read from the socket as a
214      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))      (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
215    
216  (defstruct (slime-input-stream  (defstruct (slime-input-stream
217               (:include string-stream               (:include string-stream
218                         (lisp::in #'sis/in)                         (lisp::in #'sis/in)
219                         (lisp::misc #'sis/misc))                         (lisp::misc #'sis/misc))
220               (:conc-name sis.)               (:conc-name sis.)
221               (:print-function %print-slime-output-stream))               (:print-function %print-slime-output-stream)
222    (buffer "" :type string)               (:constructor make-slime-input-stream (input-fn sos)))
223    (index 0 :type kernel:index))    (input-fn nil :type function)
224      ;; We know our sibling output stream, so that we can force it before
225      ;; requesting input.
226      (sos      nil :type slime-output-stream)
227      (buffer   ""  :type string)
228      (index    0   :type kernel:index))
229    
230  (defun sis/in (stream eof-errorp eof-value)  (defun sis/in (stream eof-errorp eof-value)
   (declare (ignore eof-errorp eof-value))  
231    (let ((index (sis.index stream))    (let ((index (sis.index stream))
232          (buffer (sis.buffer stream)))          (buffer (sis.buffer stream)))
233      (when (= index (length buffer))      (when (= index (length buffer))
234        (setf buffer (slime-read-string))        (force-output (sis.sos stream))
235        (setf (sis.buffer stream) buffer)        (let ((string (funcall (sis.input-fn stream))))
236        (setf index 0))          (cond ((zerop (length string))
237                   (return-from sis/in
238                     (if eof-errorp
239                         (error (make-condition 'end-of-file :stream stream))
240                         eof-value)))
241                  (t
242                   (setf buffer string)
243                   (setf (sis.buffer stream) buffer)
244                   (setf index 0)))))
245      (prog1 (aref buffer index)      (prog1 (aref buffer index)
246        (setf (sis.index stream) (1+ index)))))        (setf (sis.index stream) (1+ index)))))
247    
# Line 162  The request is read from the socket as a Line 253  The request is read from the socket as a
253      (:unread (setf (aref (sis.buffer stream)      (:unread (setf (aref (sis.buffer stream)
254                           (decf (sis.index stream)))                           (decf (sis.index stream)))
255                     arg1))                     arg1))
256      (:clear-input (setf (sis.index stream) 0      (:clear-input
257         (setf (sis.index stream) 0
258                          (sis.buffer stream) ""))                          (sis.buffer stream) ""))
259      (:listen (< (sis.index stream) (length (sis.buffer stream))))      (:listen (< (sis.index stream) (length (sis.buffer stream))))
260      (:charpos nil)      (:charpos nil)
# Line 174  The request is read from the socket as a Line 266  The request is read from the socket as a
266    
267  ;;;; Compilation Commands  ;;;; Compilation Commands
268    
 (defvar *swank-source-info* nil  
   "Bound to a SOURCE-INFO object during compilation.")  
   
269  (defvar *previous-compiler-condition* nil  (defvar *previous-compiler-condition* nil
270    "Used to detect duplicates.")    "Used to detect duplicates.")
271    
272  (defvar *previous-context* nil  (defvar *previous-context* nil
273    "Previous compiler error context.")    "Previous compiler error context.")
274    
275  (defvar *compiler-notes* '()  (defvar *buffer-name* nil
276    "List of compiler notes for the last compilation unit.")    "The name of the Emacs buffer we are compiling from.
277    NIL if we aren't compiling from a buffer.")
278    
 (defvar *buffer-name* nil)  
279  (defvar *buffer-start-position* nil)  (defvar *buffer-start-position* nil)
280  (defvar *buffer-substring* nil)  (defvar *buffer-substring* nil)
281  (defvar *compile-filename* nil)  
282    (defimplementation call-with-compilation-hooks (function)
283      (let ((*previous-compiler-condition* nil)
284            (*previous-context* nil)
285            (*print-readably* nil))
286        (handler-bind ((c::compiler-error #'handle-notification-condition)
287                       (c::style-warning  #'handle-notification-condition)
288                       (c::warning        #'handle-notification-condition))
289          (funcall function))))
290    
291    (defimplementation swank-compile-file (filename load-p)
292      (clear-xref-info filename)
293      (with-compilation-hooks ()
294        (let ((*buffer-name* nil))
295          (multiple-value-bind (output-file warnings-p failure-p)
296              (compile-file filename)
297            (unless failure-p
298              ;; Cache the latest source file for definition-finding.
299              (source-cache-get filename (file-write-date filename))
300              (load output-file))
301            (values output-file warnings-p failure-p)))))
302    
303    (defimplementation swank-compile-string (string &key buffer position directory)
304      (declare (ignore directory))
305      (with-compilation-hooks ()
306        (let ((*buffer-name* buffer)
307              (*buffer-start-position* position)
308              (*buffer-substring* string))
309          (with-input-from-string (stream string)
310            (ext:compile-from-stream
311             stream
312             :source-info `(:emacs-buffer ,buffer
313                            :emacs-buffer-offset ,position
314                            :emacs-buffer-string ,string))))))
315    
316    
317  ;;;;; Trapping notes  ;;;;; Trapping notes
318    ;;;
319    ;;; We intercept conditions from the compiler and resignal them as
320    ;;; `SWANK:COMPILER-CONDITION's.
321    
322  (defun handle-notification-condition (condition)  (defun handle-notification-condition (condition)
323    "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."  
324    (unless (eq condition *previous-compiler-condition*)    (unless (eq condition *previous-compiler-condition*)
325      (let ((context (or (c::find-error-context nil) *previous-context*)))      (let ((context (c::find-error-context nil)))
326        (setq *previous-compiler-condition* condition)        (setq *previous-compiler-condition* condition)
327        (setq *previous-context* context)        (setq *previous-context* context)
328        (signal-compiler-condition condition context))))        (signal-compiler-condition condition context))))
# Line 211  information." Line 332  information."
332             'compiler-condition             'compiler-condition
333             :original-condition condition             :original-condition condition
334             :severity (severity-for-emacs condition)             :severity (severity-for-emacs condition)
335             :message (brief-compiler-message-for-emacs condition context)             :short-message (brief-compiler-message-for-emacs condition)
336             :location (compiler-note-location context))))             :message (long-compiler-message-for-emacs condition context)
337               :location (if (eq (type-of condition) 'c::compiler-read-error)
338                             (read-error-location condition)
339                             (compiler-note-location context)))))
340    
341  (defun severity-for-emacs (condition)  (defun severity-for-emacs (condition)
342    "Return the severity of CONDITION."    "Return the severity of CONDITION."
# Line 221  information." Line 345  information."
345      (c::style-warning :note)      (c::style-warning :note)
346      (c::warning :warning)))      (c::warning :warning)))
347    
348  (defun brief-compiler-message-for-emacs (condition error-context)  (defun brief-compiler-message-for-emacs (condition)
349    "Briefly describe a compiler error for Emacs.    "Briefly describe a compiler error for Emacs.
350  When Emacs presents the message it already has the source popped up  When Emacs presents the message it already has the source popped up
351  and the source form highlighted. This makes much of the information in  and the source form highlighted. This makes much of the information in
352  the error-context redundant."  the error-context redundant."
353      (princ-to-string condition))
354    
355    (defun long-compiler-message-for-emacs (condition error-context)
356      "Describe a compiler error for Emacs including context information."
357    (declare (type (or c::compiler-error-context null) error-context))    (declare (type (or c::compiler-error-context null) error-context))
358    (multiple-value-bind (enclosing source)    (multiple-value-bind (enclosing source)
359        (if error-context        (if error-context
# Line 234  the error-context redundant." Line 362  the error-context redundant."
362      (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"      (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"
363              enclosing source condition)))              enclosing source condition)))
364    
365    (defun read-error-location (condition)
366      (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
367             (file (c::file-info-name finfo))
368             (pos (c::compiler-read-error-position condition)))
369        (cond ((and (eq file :stream) *buffer-name*)
370               (make-location (list :buffer *buffer-name*)
371                              (list :position *buffer-start-position* pos)))
372              ((and (pathnamep file) (not *buffer-name*))
373               (make-location (list :file (unix-truename file))
374                              (list :position pos)))
375              (t (break)))))
376    
377  (defun compiler-note-location (context)  (defun compiler-note-location (context)
378    (cond (context    "Derive the location of a complier message from its context.
379           (resolve-note-location  Return a `location' record, or (:error REASON) on failure."
380            *buffer-name*    (if (null context)
381            (c::compiler-error-context-file-name context)        (note-error-location)
382            (c::compiler-error-context-file-position context)        (let ((file (c::compiler-error-context-file-name context))
383            (reverse (c::compiler-error-context-original-source-path context))              (source (c::compiler-error-context-original-source context))
384            (c::compiler-error-context-original-source context)))              (path
385          (t               (reverse (c::compiler-error-context-original-source-path context))))
386           (resolve-note-location *buffer-name* nil nil nil nil))))          (or (locate-compiler-note file source path)
387                (note-error-location)))))
388  (defgeneric resolve-note-location (buffer file-name file-position  
389                                            source-path source))  (defun note-error-location ()
390      "Pseudo-location for notes that can't be located."
391  (defmethod resolve-note-location ((b (eql nil)) (f pathname) pos path source)    (list :error "No error location available."))
392    (make-location  
393     `(:file ,(unix-truename f))  (defun locate-compiler-note (file source source-path)
394     `(:position ,(1+ (source-path-file-position path f)))))    (cond ((and (eq file :stream) *buffer-name*)
395             ;; Compiling from a buffer
396  (defmethod resolve-note-location ((b string) (f (eql :stream)) pos path source)           (let ((position (+ *buffer-start-position*
397    (make-location                              (source-path-string-position
398     `(:buffer ,b)                               source-path *buffer-substring*))))
399     `(:position ,(+ *buffer-start-position*             (make-location (list :buffer *buffer-name*)
400                     (source-path-string-position path *buffer-substring*)))))                            (list :position position))))
401            ((and (pathnamep file) (null *buffer-name*))
402  (defmethod resolve-note-location (b (f (eql :lisp)) pos path (source string))           ;; Compiling from a file
403    (make-location           (make-location (list :file (unix-truename file))
404     `(:source-form ,source)                          (list :position
405     `(:position 1)))                                (1+ (source-path-file-position
406                                       source-path file)))))
407  (defmethod resolve-note-location (buffer          ((and (eq file :lisp) (stringp source))
408                                    (file (eql nil))           ;; No location known, but we have the source form.
409                                    (pos (eql nil))           ;; XXX How is this case triggered?  -luke (16/May/2004)
410                                    (path (eql nil))           ;; This can happen if the compiler needs to expand a macro
411                                    (source (eql nil)))           ;; but the macro-expander is not yet compiled.  Calling the
412    (cond (buffer           ;; (interpreted) macro-expander triggers IR1 conversion of
413           (make-location (list :buffer buffer)           ;; the lambda expression for the expander and invokes the
414                          (list :position *buffer-start-position*)))           ;; compiler recursively.
415          (*compile-file-truename*           (make-location (list :source-form source)
416           (make-location (list :file (namestring *compile-file-truename*))                          (list :position 1)))))
                         (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)))  
417    
418  (defmethod compile-file-for-emacs (filename load-p)  (defun unix-truename (pathname)
419    (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))))))  
420    
421    
422  ;;;; XREF  ;;;; XREF
423    ;;;
424    ;;; Cross-reference support is based on the standard CMUCL `XREF'
425    ;;; package. This package has some caveats: XREF information is
426    ;;; recorded during compilation and not preserved in fasl files, and
427    ;;; XREF recording is disabled by default. Redefining functions can
428    ;;; also cause duplicate references to accumulate, but
429    ;;; `swank-compile-file' will automatically clear out any old records
430    ;;; from the same filename.
431    ;;;
432    ;;; To enable XREF recording, set `c:*record-xref-info*' to true. To
433    ;;; clear out the XREF database call `xref:init-xref-database'.
434    
435  (defun lookup-xrefs (finder name)  (defmacro defxref (name function)
436    (xref-results-for-emacs (funcall finder (from-string name))))    `(defimplementation ,name (name)
437        (xref-results (,function name))))
438  (defslimefun who-calls (function-name)  
439    "Return the places where FUNCTION-NAME is called."  (defxref who-calls      xref:who-calls)
440    (lookup-xrefs #'xref:who-calls function-name))  (defxref who-references xref:who-references)
441    (defxref who-binds      xref:who-binds)
442  (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))  
443    
444    ;;; More types of XREF information were added since 18e:
445    ;;;
446  #+cmu19  #+cmu19
447  (progn  (progn
448    (defslimefun who-macroexpands (macro)    (defxref who-macroexpands xref:who-macroexpands)
449      "Return the places where MACRO is expanded."    ;; XXX
450      (lookup-xrefs #'xref:who-macroexpands macro))    (defimplementation who-specializes (symbol)
451        (let* ((methods (xref::who-specializes (find-class symbol)))
452    (defslimefun who-specializes (class)             (locations (mapcar #'method-location methods)))
453      "Return the methods with specializers for CLASS."        (mapcar #'list methods locations))))
454      (let* ((methods (xref::who-specializes (find-class (from-string class))))  
455             (locations (mapcar #'method-source-location methods)))  (defun xref-results (contexts)
456        (group-xrefs (mapcar (lambda (m l)    (mapcar (lambda (xref)
457                               (cons (let ((*print-pretty* nil))              (list (xref:xref-context-name xref)
458                                       (to-string m))                    (resolve-xref-location xref)))
459                                     l))            contexts))
                            methods locations))))  
   )  
460    
461  (defun resolve-xref-location (xref)  (defun resolve-xref-location (xref)
462    (let ((name (xref:xref-context-name xref))    (let ((name (xref:xref-context-name xref))
# Line 363  the error-context redundant." Line 470  the error-context redundant."
470             (make-location (list :file (unix-truename file))             (make-location (list :file (unix-truename file))
471                            (list :function-name (string name))))                            (list :function-name (string name))))
472            (t            (t
473             `(:error ,(format nil "Unkown source location: ~S ~S ~S "             `(:error ,(format nil "Unknown source location: ~S ~S ~S "
474                               name file source-path))))))                               name file source-path))))))
475    
   
 (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))))  
   
476  (defun clear-xref-info (namestring)  (defun clear-xref-info (namestring)
477    "Clear XREF notes pertaining to FILENAME.    "Clear XREF notes pertaining to NAMESTRING.
478  This is a workaround for a CMUCL bug: XREF records are cumulative."  This is a workaround for a CMUCL bug: XREF records are cumulative."
479    (let ((filename (parse-namestring namestring)))    (when c:*record-xref-info*
480      (when c:*record-xref-info*      (let ((filename (truename namestring)))
481        (dolist (db (list xref::*who-calls*        (dolist (db (list xref::*who-calls*
482                          #+cmu19 xref::*who-is-called*                          #+cmu19 xref::*who-is-called*
483                          #+cmu19 xref::*who-macroexpands*                          #+cmu19 xref::*who-macroexpands*
# Line 415  This is a workaround for a CMUCL bug: XR Line 485  This is a workaround for a CMUCL bug: XR
485                          xref::*who-binds*                          xref::*who-binds*
486                          xref::*who-sets*))                          xref::*who-sets*))
487          (maphash (lambda (target contexts)          (maphash (lambda (target contexts)
488                       ;; XXX update during traversal?
489                     (setf (gethash target db)                     (setf (gethash target db)
490                           (delete-if                           (delete filename contexts
491                            (lambda (ctx)                                   :key #'xref:xref-context-file
492                              (xref-context-derived-from-p ctx filename))                                   :test #'equalp)))
                           contexts)))  
493                   db)))))                   db)))))
494    
 (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)))  
   
495    
496  ;;;; Find callers and callees  ;;;; Find callers and callees
497    ;;;
498  ;;; Find callers and callees by looking at the constant pool of  ;;; Find callers and callees by looking at the constant pool of
499  ;;; compiled code objects.  We assume every fdefn object in the  ;;; compiled code objects.  We assume every fdefn object in the
500  ;;; constant pool corresponds to a call to that function.  A better  ;;; constant pool corresponds to a call to that function.  A better
# Line 465  This is a workaround for a CMUCL bug: XR Line 524  This is a workaround for a CMUCL bug: XR
524  receives the object as argument.  SPACES should be a list of the  receives the object as argument.  SPACES should be a list of the
525  symbols :dynamic, :static, or :read-only."  symbols :dynamic, :static, or :read-only."
526    (dolist (space spaces)    (dolist (space spaces)
527      (declare (inline vm::map-allocated-objects))      (declare (inline vm::map-allocated-objects)
528                 (optimize (ext:inhibit-warnings 3)))
529      (vm::map-allocated-objects      (vm::map-allocated-objects
530       (lambda (obj header size)       (lambda (obj header size)
531         (declare (type fixnum size) (ignore size))         (declare (type fixnum size) (ignore size))
# Line 495  constant pool." Line 555  constant pool."
555    "Return FUNCTION's callers.  The result is a list of code-objects."    "Return FUNCTION's callers.  The result is a list of code-objects."
556    (let ((referrers '()))    (let ((referrers '()))
557      (declare (inline map-caller-code-components))      (declare (inline map-caller-code-components))
558      (ext:gc :full t)      ;;(ext:gc :full t)
559      (map-caller-code-components function spaces      (map-caller-code-components function spaces
560                                  (lambda (code) (push code referrers)))                                  (lambda (code) (push code referrers)))
561      referrers))      referrers))
562    
563  (defun debug-info-definitions (debug-info)  (defun debug-info-definitions (debug-info)
564    "Return the defintions for a debug-info.  This should only be used    "Return the defintions for a debug-info.  This should only be used
565  for code-object without entry points, i.e., byte compiled  for code-object without entry points, i.e., byte compiled
# Line 512  code (are theree others?)" Line 572  code (are theree others?)"
572      (destructuring-bind (first) source      (destructuring-bind (first) source
573        (ecase (c::debug-source-from first)        (ecase (c::debug-source-from first)
574          (:file          (:file
575           (list           (list (list name
576            (cons name                       (make-location
577                  (make-location                        (list :file (unix-truename (c::debug-source-name first)))
578                   (list :file (unix-truename (c::debug-source-name first)))                        (list :function-name (string name))))))))))
                  (list :function-name name)))))))))  
579    
580  (defun code-component-entry-points (code)  (defun code-component-entry-points (code)
581    "Return a list ((NAME . LOCATION) ...) of function definitons for    "Return a list ((NAME LOCATION) ...) of function definitons for
582  the code omponent CODE."  the code omponent CODE."
583    (delete-duplicates    (let ((names '()))
584     (loop for e = (kernel:%code-entry-points code)      (do ((f (kernel:%code-entry-points code) (kernel::%function-next f)))
585           then (kernel::%function-next e)          ((not f))
586           while e        (let ((name (kernel:%function-name f)))
587           collect (cons (to-string (kernel:%function-name e))          (when (ext:valid-function-name-p name)
588                         (function-source-location e)))            (push (list name (function-location f)) names))))
589     :test #'equal))      names))
590    
591  (defslimefun list-callers (symbol-name)  (defimplementation list-callers (symbol)
592    "Return a list ((FILE . ((NAME . LOCATION) ...)) ...) of callers."    "Return a list ((NAME LOCATION) ...) of callers."
593    (let ((components (function-callers (from-string symbol-name)))    (let ((components (function-callers symbol))
594          (xrefs '()))          (xrefs '()))
595      (dolist (code components)      (dolist (code components)
596        (let* ((entry (kernel:%code-entry-points code))        (let* ((entry (kernel:%code-entry-points code))
# Line 541  the code omponent CODE." Line 600  the code omponent CODE."
600                         (debug-info-definitions                         (debug-info-definitions
601                          (kernel:%code-debug-info code)))))                          (kernel:%code-debug-info code)))))
602          (setq xrefs (nconc defs xrefs))))          (setq xrefs (nconc defs xrefs))))
603      (group-xrefs xrefs)))      xrefs))
604    
605    (defimplementation list-callees (symbol)
606      (let ((fns (function-callees symbol)))
607        (mapcar (lambda (fn)
608                  (list (kernel:%function-name fn)
609                        (function-location fn)))
610                fns)))
611    
612  (defslimefun list-callees (symbol-name)  
613    (let ((fns (function-callees (from-string symbol-name))))  ;;;; Resolving source locations
614      (group-xrefs (mapcar (lambda (fn)  ;;;
615                             (cons (to-string (kernel:%function-name fn))  ;;; Our mission here is to "resolve" references to code locations into
616                                   (function-source-location fn)))  ;;; actual file/buffer names and character positions. The references
617                           fns))))  ;;; we work from come out of the compiler's statically-generated debug
618    ;;; information, such as `code-location''s and `debug-source''s. For
619  ;;;; Definitions  ;;; more details, see the "Debugger Programmer's Interface" section of
620    ;;; the CMUCL manual.
621    ;;;
622    ;;; The first step is usually to find the corresponding "source-path"
623    ;;; for the location. Once we have the source-path we can pull up the
624    ;;; source file and `READ' our way through to the right position. The
625    ;;; main source-code groveling work is done in
626    ;;; `swank-source-path-parser.lisp'.
627    
628  (defvar *debug-definition-finding* t  (defvar *debug-definition-finding* nil
629    "When true don't handle errors while looking for definitions.    "When true don't handle errors while looking for definitions.
630  This is useful when debugging the definition-finding code.")  This is useful when debugging the definition-finding code.")
631    
632    (defvar *source-snippet-size* 256
633      "Maximum number of characters in a snippet of source code.
634    Snippets at the beginning of definitions are used to tell Emacs what
635    the definitions looks like, so that it can accurately find them by
636    text search.")
637    
638  (defmacro safe-definition-finding (&body body)  (defmacro safe-definition-finding (&body body)
639    "Execute BODY ignoring errors.  Return a the source location    "Execute BODY and return the source-location it returns.
640  returned by BODY or if an error occurs a description of the error.  If an error occurs and `*debug-definition-finding*' is false, then
641  The second return value is the condition or nil."  return an error pseudo-location.
642    
643    The second return value is NIL if no error occurs, otherwise it is the
644    condition object."
645    `(flet ((body () ,@body))    `(flet ((body () ,@body))
646      (if *debug-definition-finding*      (if *debug-definition-finding*
647          (body)          (body)
648          (handler-case (values (progn ,@body) nil)          (handler-case (values (progn ,@body) nil)
649            (error (c) (values (list :error (princ-to-string c)) c))))))            (error (c) (values (list :error (princ-to-string c)) c))))))
650    
651    (defun code-location-source-location (code-location)
652      "Safe wrapper around `code-location-from-source-location'."
653      (safe-definition-finding
654       (source-location-from-code-location code-location)))
655    
656    (defun source-location-from-code-location (code-location)
657      "Return the source location for CODE-LOCATION."
658      (let ((debug-fun (di:code-location-debug-function code-location)))
659        (when (di::bogus-debug-function-p debug-fun)
660          ;; Those lousy cheapskates! They've put in a bogus debug source
661          ;; because the code was compiled at a low debug setting.
662          (error "Bogus debug function: ~A" debug-fun)))
663      (let* ((debug-source (di:code-location-debug-source code-location))
664             (from (di:debug-source-from debug-source))
665             (name (di:debug-source-name debug-source)))
666        (ecase from
667          (:file
668           (location-in-file name code-location debug-source))
669          (:stream
670           (location-in-stream code-location debug-source))
671          (:lisp
672           ;; The location comes from a form passed to `compile'.
673           ;; The best we can do is return the form itself for printing.
674           (make-location
675            (list :source-form (with-output-to-string (*standard-output*)
676                                 (debug::print-code-location-source-form
677                                  code-location 100 t)))
678            (list :position 1))))))
679    
680    (defun location-in-file (filename code-location debug-source)
681      "Resolve the source location for CODE-LOCATION in FILENAME."
682      (let* ((code-date (di:debug-source-created debug-source))
683             (source-code (or (source-cache-get filename code-date)
684                              (read-file filename))))
685        (make-location (list :file (unix-truename filename)) nil)
686        (with-input-from-string (s source-code)
687          (make-location (list :file (unix-truename filename))
688                         (list :position
689                               (1+ (code-location-stream-position
690                                    code-location s)))
691                         `(:snippet ,(read-snippet s))))))
692    
693    (defun location-in-stream (code-location debug-source)
694      "Resolve the source location for a CODE-LOCATION from a stream.
695    This only succeeds if the code was compiled from an Emacs buffer."
696      (unless (debug-source-info-from-emacs-buffer-p debug-source)
697        (error "The code is compiled from a non-SLIME stream."))
698      (let* ((info (c::debug-source-info debug-source))
699             (string (getf info :emacs-buffer-string))
700             (position (code-location-string-offset
701                        code-location
702                        string)))
703        (make-location
704         (list :buffer (getf info :emacs-buffer))
705         (list :position (+ (getf info :emacs-buffer-offset) position))
706         (list :snippet (with-input-from-string (s string)
707                          (file-position s position)
708                          (read-snippet s))))))
709    
710    (defun read-file (filename)
711      "Return the entire contents of FILENAME as a string."
712      (with-open-file (s filename :direction :input)
713        (let ((string (make-string (file-length s))))
714          (read-sequence string s)
715          string)))
716    
717    (defun read-snippet (stream)
718      "Read a string of upto *SOURCE-SNIPPET-SIZE* characters from STREAM."
719      (read-upto-n-chars stream *source-snippet-size*))
720    
721    (defun read-upto-n-chars (stream n)
722      "Return a string of upto N chars from STREAM."
723      (let* ((string (make-string n))
724             (chars  (read-sequence string stream)))
725        (subseq string 0 chars)))
726    
727    ;;;;; Function-name locations
728    ;;;
729    (defun debug-info-function-name-location (debug-info)
730      "Return a function-name source-location for DEBUG-INFO.
731    Function-name source-locations are a fallback for when precise
732    positions aren't available."
733      (with-struct (c::debug-info- (fname name) source) debug-info
734        (with-struct (c::debug-source- info from name) (car source)
735          (ecase from
736            (:file
737             (make-location (list :file (namestring (truename name)))
738                            (list :function-name (string fname))))
739            (:stream
740             (assert (debug-source-info-from-emacs-buffer-p (car source)))
741             (make-location (list :buffer (getf info :emacs-buffer))
742                            (list :function-name (string fname))))
743            (:lisp
744             (make-location (list :source-form (princ-to-string (aref name 0)))
745                            (list :position 1)))))))
746    
747    (defun debug-source-info-from-emacs-buffer-p (debug-source)
748      "Does the `info' slot of DEBUG-SOURCE contain an Emacs buffer location?
749    This is true for functions that were compiled directly from buffers."
750      (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
751    
752    (defun info-from-emacs-buffer-p (info)
753      (and info
754           (consp info)
755           (eq :emacs-buffer (car info))))
756    
757    
758    ;;;;; Groveling source-code for positions
759    
760    (defun code-location-stream-position (code-location stream)
761      "Return the byte offset of CODE-LOCATION in STREAM.  Extract the
762    toplevel-form-number and form-number from CODE-LOCATION and use that
763    to find the position of the corresponding form.
764    
765    Finish with STREAM positioned at the start of the code location."
766      (let* ((location (debug::maybe-block-start-location code-location))
767             (tlf-offset (di:code-location-top-level-form-offset location))
768             (form-number (di:code-location-form-number location)))
769        (let ((pos (form-number-stream-position tlf-offset form-number stream)))
770          (file-position stream pos)
771          pos)))
772    
773    (defun form-number-stream-position (tlf-number form-number stream)
774      "Return the starting character position of a form in STREAM.
775    TLF-NUMBER is the top-level-form number.
776    FORM-NUMBER is an index into a source-path table for the TLF."
777      (let ((*read-suppress* t))
778        (dotimes (i tlf-number) (read stream))
779        (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)
780          (let* ((path-table (di:form-number-translations tlf 0))
781                 (source-path
782                  (if (<= (length path-table) form-number) ; source out of sync?
783                      (list 0)              ; should probably signal a condition
784                      (reverse (cdr (aref path-table form-number))))))
785            (source-path-source-position source-path tlf position-map)))))
786    
787    (defun code-location-string-offset (code-location string)
788      "Return the byte offset of CODE-LOCATION in STRING.
789    See CODE-LOCATION-STREAM-POSITION."
790      (with-input-from-string (s string)
791        (code-location-stream-position code-location s)))
792    
793    ;;;;; Source-file cache
794    ;;;
795    ;;; To robustly find source locations it's useful to have the exact
796    ;;; source code that the loaded code was compiled from. In this source
797    ;;; we can accurately find the right location, and from that location
798    ;;; we can extract a "snippet" of code to show what the definition
799    ;;; looks like. Emacs can use this snippet in a best-match search to
800    ;;; locate the right definition, which works well even if the buffer
801    ;;; has been modified.
802    ;;;
803    ;;; The idea is that if a definition previously started with
804    ;;; `(define-foo bar' then it probably still does.
805    ;;;
806    ;;; Whenever we see that the file on disk has the same
807    ;;; `file-write-date' as a location we're looking for, we cache the
808    ;;; whole file inside Lisp. That way we will still have the matching
809    ;;; version even if the file is later modified on disk. If the file is
810    ;;; later recompiled and reloaded then we replace our cache entry.
811    
812    (defvar *cache-sourcecode* t
813      "When true complete source files are cached.
814    The cache is used to keep known good copies of the source text which
815    correspond to the loaded code. Finding definitions is much more
816    reliable when the exact source is available, so we cache it in case it
817    gets edited on disk later.")
818    
819    (defvar *source-file-cache* (make-hash-table :test 'equal)
820      "Cache of source file contents.
821    Maps from truename to source-cache-entry structure.")
822    
823    (defstruct (source-cache-entry
824                 (:conc-name source-cache-entry.)
825                 (:constructor make-source-cache-entry (text date)))
826      text date)
827    
828    (defun source-cache-get (filename date)
829      "Return the source code for FILENAME as written on DATE in a string.
830    Return NIL if the right version cannot be found."
831      (let ((entry (gethash filename *source-file-cache*)))
832        (cond ((and entry (equal date (source-cache-entry.date entry)))
833               ;; Cache hit.
834               (source-cache-entry.text entry))
835              ((or (null entry)
836                   (not (equal date (source-cache-entry.date entry))))
837               ;; Cache miss.
838               (if (equal (file-write-date filename) date)
839                   ;; File on disk has the correct version.
840                   (let ((source (read-file filename)))
841                     (setf (gethash filename *source-file-cache*)
842                           (make-source-cache-entry source date))
843                     source)
844                   nil)))))
845    
846    (defun source-cached-p (filename)
847      "Is any version of FILENAME in the source cache?"
848      (if (gethash filename *source-file-cache*) t))
849    
850    (defimplementation buffer-first-change (filename)
851      "Load a file into the cache when the user modifies its buffer.
852    This is a win if the user then saves the file and tries to M-. into it."
853      (unless (source-cached-p filename)
854        (ignore-errors (source-cache-get filename (file-write-date filename)))))
855    
856    
857    ;;;; Finding definitions
858    
859    ;;; There are a great many different types of definition for us to
860    ;;; find. We search for definitions of every kind and return them in a
861    ;;; list.
862    
863    (defimplementation find-definitions (name)
864      (append (function-definitions name)
865              (setf-definitions name)
866              (variable-definitions name)
867              (class-definitions name)
868              (type-definitions name)
869              (compiler-macro-definitions name)
870              (source-transform-definitions name)
871              (function-info-definitions name)
872              (ir1-translator-definitions name)))
873    
874    ;;;;; Functions, macros, generic functions, methods
875    ;;;
876    ;;; We make extensive use of the compile-time debug information that
877    ;;; CMUCL records, in particular "debug functions" and "code
878    ;;; locations." Refer to the "Debugger Programmer's Interface" section
879    ;;; of the CMUCL manual for more details.
880    
881    (defun function-definitions (name)
882      "Return definitions for NAME in the \"function namespace\", i.e.,
883    regular functions, generic functions, methods and macros.
884    NAME can any valid function name (e.g, (setf car))."
885      (let ((macro?    (and (symbolp name) (macro-function name)))
886            (special?  (and (symbolp name) (special-operator-p name)))
887            (function? (and (ext:valid-function-name-p name)
888                            (ext:info :function :definition name))))
889        (cond (macro?
890               (list `((defmacro ,name)
891                       ,(function-location (macro-function name)))))
892              (special?
893               (list `((:special-operator ,name)
894                       (:error ,(format nil "Special operator: ~S" name)))))
895              (function?
896               (let ((function (fdefinition name)))
897                 (if (genericp function)
898                     (generic-function-definitions name function)
899                     (list (list `(function ,name)
900                                 (function-location function)))))))))
901    
902    ;;;;;; Ordinary (non-generic/macro/special) functions
903    ;;;
904    ;;; First we test if FUNCTION is a closure created by defstruct, and
905    ;;; if so extract the defstruct-description (`dd') from the closure
906    ;;; and find the constructor for the struct.  Defstruct creates a
907    ;;; defun for the default constructor and we use that as an
908    ;;; approximation to the source location of the defstruct.
909    ;;;
910    ;;; For an ordinary function we return the source location of the
911    ;;; first code-location we find.
912    ;;;
913    (defun function-location (function)
914      "Return the source location for FUNCTION."
915      (cond ((struct-closure-p function)
916             (struct-closure-location function))
917            ((c::byte-function-or-closure-p function)
918             (byte-function-location function))
919            (t
920             (compiled-function-location function))))
921    
922    (defun compiled-function-location (function)
923      "Return the location of a regular compiled function."
924      (multiple-value-bind (code-location error)
925          (safe-definition-finding (function-first-code-location function))
926        (cond (error (list :error (princ-to-string error)))
927              (t (code-location-source-location code-location)))))
928    
929  (defun function-first-code-location (function)  (defun function-first-code-location (function)
930      "Return the first code-location we can find for FUNCTION."
931    (and (function-has-debug-function-p function)    (and (function-has-debug-function-p function)
932         (di:debug-function-start-location         (di:debug-function-start-location
933          (di:function-debug-function function))))          (di:function-debug-function function))))
# Line 580  The second return value is the condition Line 940  The second return value is the condition
940             (vm::find-code-object function))             (vm::find-code-object function))
941         (not (eq closure function))))         (not (eq closure function))))
942    
943    
944    (defun byte-function-location (fn)
945      "Return the location of the byte-compiled function FN."
946      (etypecase fn
947        ((or c::hairy-byte-function c::simple-byte-function)
948         (let* ((component (c::byte-function-component fn))
949                (debug-info (kernel:%code-debug-info component)))
950           (debug-info-function-name-location debug-info)))
951        (c::byte-closure
952         (byte-function-location (c::byte-closure-function fn)))))
953    
954    ;;; Here we deal with structure accessors. Note that `dd' is a
955    ;;; "defstruct descriptor" structure in CMUCL. A `dd' describes a
956    ;;; `defstruct''d structure.
957    
958  (defun struct-closure-p (function)  (defun struct-closure-p (function)
959      "Is FUNCTION a closure created by defstruct?"
960    (or (function-code-object= function #'kernel::structure-slot-accessor)    (or (function-code-object= function #'kernel::structure-slot-accessor)
961        (function-code-object= function #'kernel::structure-slot-setter)        (function-code-object= function #'kernel::structure-slot-setter)
962        (function-code-object= function #'kernel::%defstruct)))        (function-code-object= function #'kernel::%defstruct)))
963    
964    (defun struct-closure-location (function)
965      "Return the location of the structure that FUNCTION belongs to."
966      (assert (struct-closure-p function))
967      (safe-definition-finding
968        (dd-location (struct-closure-dd function))))
969    
970  (defun struct-closure-dd (function)  (defun struct-closure-dd (function)
971      "Return the defstruct-definition (dd) of FUNCTION."
972    (assert (= (kernel:get-type function) vm:closure-header-type))    (assert (= (kernel:get-type function) vm:closure-header-type))
973    (flet ((find-layout (function)    (flet ((find-layout (function)
974             (sys:find-if-in-closure             (sys:find-if-in-closure
# Line 597  The second return value is the condition Line 980  The second return value is the condition
980                    (return-from find-layout value))))                    (return-from find-layout value))))
981              function)))              function)))
982      (kernel:layout-info (find-layout function))))      (kernel:layout-info (find-layout function))))
983    
984  (defun dd-source-location (dd)  (defun dd-location (dd)
985      "Return the location of a `defstruct'."
986      ;; Find the location in a constructor.
987      (function-location (struct-constructor dd)))
988    
989    (defun struct-constructor (dd)
990      "Return a constructor function from a defstruct definition.
991    Signal an error if no constructor can be found."
992    (let ((constructor (or (kernel:dd-default-constructor dd)    (let ((constructor (or (kernel:dd-default-constructor dd)
993                           (car (kernel::dd-constructors dd)))))                           (car (kernel::dd-constructors dd)))))
994      (when (or (not constructor) (and (consp constructor)      (when (or (null constructor)
995                                       (not (car constructor))))                (and (consp constructor) (null (car constructor))))
996        (error "Cannot locate struct without constructor: ~S"        (error "Cannot find structure's constructor: ~S"
997               (kernel::dd-name dd)))               (kernel::dd-name dd)))
998      (function-source-location      (coerce (if (consp constructor) (first constructor) constructor)
999       (coerce (if (consp constructor) (car constructor) constructor)              'function)))
1000               'function))))  
1001    ;;;;;; Generic functions and methods
1002    
1003    (defun generic-function-definitions (name function)
1004      "Return the definitions of a generic function and its methods."
1005      (cons (list `(defgeneric ,name) (gf-location function))
1006            (gf-method-definitions function)))
1007    
1008    (defun gf-location (gf)
1009      "Return the location of the generic function GF."
1010      (definition-source-location gf (pcl::generic-function-name gf)))
1011    
1012    (defun gf-method-definitions (gf)
1013      "Return the locations of all methods of the generic function GF."
1014      (mapcar #'method-definition (pcl::generic-function-methods gf)))
1015    
1016    (defun method-definition (method)
1017      (list (method-dspec method)
1018            (method-location method)))
1019    
1020    (defun method-dspec (method)
1021      "Return a human-readable \"definition specifier\" for METHOD."
1022      (let* ((gf (pcl:method-generic-function method))
1023             (name (pcl:generic-function-name gf))
1024             (specializers (pcl:method-specializers method))
1025             (qualifiers (pcl:method-qualifiers method)))
1026        `(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
1027    
1028    ;; XXX maybe special case setters/getters
1029    (defun method-location (method)
1030      (function-location (or (pcl::method-fast-function method)
1031                             (pcl:method-function method))))
1032    
1033  (defun genericp (fn)  (defun genericp (fn)
1034    (typep fn 'generic-function))    (typep fn 'generic-function))
1035    
1036  (defun gf-definition-location (gf)  ;;;;;; Types and classes
1037    (flet ((guess-source-file (faslfile)  
1038             (unix-truename  (defun type-definitions (name)
1039              (merge-pathnames (make-pathname :type "lisp")    "Return `deftype' locations for type NAME."
1040                               faslfile))))    (maybe-make-definition (ext:info :type :expander name) 'deftype name))
1041      (let ((def-source (pcl::definition-source gf))  
1042            (name (string (pcl:generic-function-name gf))))  (defun maybe-make-definition (function kind name)
1043        (etypecase def-source    "If FUNCTION is non-nil then return its definition location."
1044          (pathname (make-location    (if function
1045                     `(:file ,(guess-source-file def-source))        (list (list `(,kind ,name) (function-location function)))))
1046                     `(:function-name ,name)))  
1047          (cons  (defun class-definitions (name)
1048           (destructuring-bind ((dg name) pathname) def-source    "Return the definition locations for the class called NAME."
1049             (declare (ignore dg))    (if (symbolp name)
1050             (etypecase pathname        (let ((class (kernel::find-class name nil)))
1051               (pathname          (etypecase class
1052                (make-location `(:file ,(guess-source-file pathname))            (null '())
1053                               `(:function-name ,(string name)))))))))))            (kernel::structure-class
1054               (list (list `(defstruct ,name) (dd-location (find-dd name)))))
1055  (defun method-source-location (method)            #+(or)
1056    (function-source-location (or (pcl::method-fast-function method)            (conditions::condition-class
1057                                  (pcl:method-function method))))             (list (list `(define-condition ,name)
1058                           (condition-class-location class))))
1059  (defun gf-method-locations (gf)            (kernel::standard-class
1060    (let ((ms (pcl::generic-function-methods gf)))             (list (list `(defclass ,name)
1061      (mapcar #'method-source-location ms)))                         (class-location (find-class name)))))
1062              ((or kernel::built-in-class
1063  (defun gf-source-locations (gf)                 conditions::condition-class
1064    (list* (gf-definition-location gf)                 kernel:funcallable-structure-class)
1065           (gf-method-locations gf)))             (list (list `(kernel::define-type-class ,name)
1066                           `(:error
1067  (defun function-source-locations (function)                           ,(format nil "No source info for ~A" name)))))))))
1068    "Return a list of source locations for FUNCTION."  
1069    ;; First test if FUNCTION is a closure created by defstruct; if so  (defun class-location (class)
1070    ;; extract the defstruct-description (dd) from the closure and find    "Return the `defclass' location for CLASS."
1071    ;; the constructor for the struct.  Defstruct creates a defun for    (definition-source-location class (pcl:class-name class)))
1072    ;; the default constructor and we use that as an approximation to  
1073    ;; the source location of the defstruct.  (defun find-dd (name)
1074    ;;    "Find the defstruct-definition by the name of its structure-class."
1075    ;; For an ordinary function we return the source location of the    (let ((layout (ext:info :type :compiler-layout name)))
1076    ;; first code-location we find.      (if layout
1077    (cond ((struct-closure-p function)          (kernel:layout-info layout))))
1078           (list  
1079            (safe-definition-finding  (defun condition-class-location (class)
1080             (dd-source-location (struct-closure-dd function)))))    (let ((slots (conditions::condition-class-slots class))
1081          ((genericp function)          (name (conditions::condition-class-name class)))
1082           (gf-source-locations function))      (cond ((null slots)
1083          (t             `(:error ,(format nil "No location info for condition: ~A" name)))
1084           (list            (t
1085            (multiple-value-bind (code-location error)             ;; Find the class via one of its slot-reader methods.
1086                (safe-definition-finding (function-first-code-location function))             (let* ((slot (first slots))
1087              (cond (error (list :error (princ-to-string error)))                    (gf (fdefinition
1088                    (t (code-location-source-location code-location))))))))                         (first (conditions::condition-slot-readers slot)))))
1089                 (method-location
1090  (defun function-source-location (function)                (first
1091    (destructuring-bind (first) (function-source-locations function)                 (pcl:compute-applicable-methods-using-classes
1092      first))                  gf (list (find-class name))))))))))
1093    
1094  (defslimefun find-function-locations (symbol-name)  (defun make-name-in-file-location (file string)
1095    "Return a list of source-locations for SYMBOL-NAME's functions."    (multiple-value-bind (filename c)
1096    (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)        (ignore-errors
1097      (cond ((not foundp)          (unix-truename (merge-pathnames (make-pathname :type "lisp")
1098             (list (list :error (format nil "Unkown symbol: ~A" symbol-name))))                                          file)))
1099            ((macro-function symbol)      (cond (filename (make-location `(:file ,filename)
1100             (function-source-locations (macro-function symbol)))                                     `(:function-name ,(string string))))
1101            ((special-operator-p symbol)            (t (list :error (princ-to-string c))))))
1102             (list (list :error (format nil "~A is a special-operator" symbol))))  
1103            ((fboundp symbol)  (defun source-location-form-numbers (location)
1104             (function-source-locations (coerce symbol 'function)))    (c::decode-form-numbers (c::form-numbers-form-numbers location)))
1105            (t (list (list :error  
1106                           (format nil "Symbol not fbound: ~A" symbol-name))))  (defun source-location-tlf-number (location)
1107            )))    (nth-value 0 (source-location-form-numbers location)))
1108    
1109    (defun source-location-form-number (location)
1110      (nth-value 1 (source-location-form-numbers location)))
1111    
1112    (defun resolve-file-source-location (location)
1113      (let ((filename (c::file-source-location-pathname location))
1114            (tlf-number (source-location-tlf-number location))
1115            (form-number (source-location-form-number location)))
1116        (with-open-file (s filename)
1117          (let ((pos (form-number-stream-position tlf-number form-number s)))
1118            (make-location `(:file ,(unix-truename filename))
1119                           `(:position ,(1+ pos)))))))
1120    
1121    (defun resolve-stream-source-location (location)
1122      (let ((info (c::stream-source-location-user-info location))
1123            (tlf-number (source-location-tlf-number location))
1124            (form-number (source-location-form-number location)))
1125        ;; XXX duplication in frame-source-location
1126        (assert (info-from-emacs-buffer-p info))
1127        (destructuring-bind (&key emacs-buffer emacs-buffer-string
1128                                  emacs-buffer-offset) info
1129          (with-input-from-string (s emacs-buffer-string)
1130            (let ((pos (form-number-stream-position tlf-number form-number s)))
1131              (make-location `(:buffer ,emacs-buffer)
1132                             `(:position ,(+ emacs-buffer-offset pos))))))))
1133    
1134    ;; XXX predicates for 18e backward compatibilty.  Remove them when
1135    ;; we're 19a only.
1136    (defun file-source-location-p (object)
1137      (when (fboundp 'c::file-source-location-p)
1138        (c::file-source-location-p object)))
1139    
1140    (defun stream-source-location-p (object)
1141      (when (fboundp 'c::stream-source-location-p)
1142        (c::stream-source-location-p object)))
1143    
1144    (defun source-location-p (object)
1145      (or (file-source-location-p object)
1146          (stream-source-location-p object)))
1147    
1148    (defun resolve-source-location (location)
1149      (etypecase location
1150        ((satisfies file-source-location-p)
1151         (resolve-file-source-location location))
1152        ((satisfies stream-source-location-p)
1153         (resolve-stream-source-location location))))
1154    
1155    (defun definition-source-location (object name)
1156      (let ((source (pcl::definition-source object)))
1157        (etypecase source
1158          (null
1159           `(:error ,(format nil "No source info for: ~A" object)))
1160          ((satisfies source-location-p)
1161           (resolve-source-location source))
1162          (pathname
1163           (make-name-in-file-location source name))
1164          (cons
1165           (destructuring-bind ((dg name) pathname) source
1166             (declare (ignore dg))
1167             (etypecase pathname
1168               (pathname (make-name-in-file-location pathname (string name)))
1169               (null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
1170    
1171    (defun setf-definitions (name)
1172      (let ((function (or (ext:info :setf :inverse name)
1173                          (ext:info :setf :expander name))))
1174        (if function
1175            (list (list `(setf ,name)
1176                        (function-location (coerce function 'function)))))))
1177    
1178    
1179    (defun variable-location (symbol)
1180      (multiple-value-bind (location foundp)
1181          ;; XXX for 18e compatibilty. rewrite this when we drop 18e
1182          ;; support.
1183          (ignore-errors (eval `(ext:info :source-location :defvar ',symbol)))
1184        (if (and foundp location)
1185            (resolve-source-location location)
1186            `(:error ,(format nil "No source info for variable ~S" symbol)))))
1187    
1188    (defun variable-definitions (name)
1189      (if (symbolp name)
1190          (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1191            (if recorded-p
1192                (list (list `(variable ,kind ,name)
1193                            (variable-location name)))))))
1194    
1195    (defun compiler-macro-definitions (symbol)
1196      (maybe-make-definition (compiler-macro-function symbol)
1197                             'define-compiler-macro
1198                             symbol))
1199    
1200    (defun source-transform-definitions (name)
1201      (maybe-make-definition (ext:info :function :source-transform name)
1202                             'c:def-source-transform
1203                             name))
1204    
1205    (defun function-info-definitions (name)
1206      (let ((info (ext:info :function :info name)))
1207        (if info
1208            (append (loop for transform in (c::function-info-transforms info)
1209                          collect (list `(c:deftransform ,name
1210                                          ,(c::type-specifier
1211                                            (c::transform-type transform)))
1212                                        (function-location (c::transform-function
1213                                                            transform))))
1214                    (maybe-make-definition (c::function-info-derive-type info)
1215                                           'c::derive-type name)
1216                    (maybe-make-definition (c::function-info-optimizer info)
1217                                           'c::optimizer name)
1218                    (maybe-make-definition (c::function-info-ltn-annotate info)
1219                                           'c::ltn-annotate name)
1220                    (maybe-make-definition (c::function-info-ir2-convert info)
1221                                           'c::ir2-convert name)
1222                    (loop for template in (c::function-info-templates info)
1223                          collect (list `(c::vop ,(c::template-name template))
1224                                        (function-location
1225                                         (c::vop-info-generator-function
1226                                          template))))))))
1227    
1228    (defun ir1-translator-definitions (name)
1229      (maybe-make-definition (ext:info :function :ir1-convert name)
1230                             'c:def-ir1-translator name))
1231    
1232    
1233  ;;;; Documentation.  ;;;; Documentation.
1234    
1235  (defmethod describe-symbol-for-emacs (symbol)  (defimplementation describe-symbol-for-emacs (symbol)
1236    (let ((result '()))    (let ((result '()))
1237      (flet ((doc (kind)      (flet ((doc (kind)
1238               (or (documentation symbol kind) :not-documented))               (or (documentation symbol kind) :not-documented))
# Line 734  The second return value is the condition Line 1278  The second return value is the condition
1278                         (doc nil)))                         (doc nil)))
1279        result)))        result)))
1280    
1281  (defslimefun describe-setf-function (symbol-name)  (defimplementation describe-definition (symbol namespace)
1282    (print-description-to-string    (describe (ecase namespace
1283     (or (ext:info setf inverse (from-string symbol-name))                (:variable
1284         (ext:info setf expander (from-string symbol-name)))))                 symbol)
1285                  ((:function :generic-function)
1286  (defslimefun describe-type (symbol-name)                 (symbol-function symbol))
1287    (print-description-to-string                (:setf
1288     (kernel:values-specifier-type (from-string symbol-name))))                 (or (ext:info setf inverse symbol)
1289                       (ext:info setf expander symbol)))
1290  (defslimefun describe-class (symbol-name)                (:type
1291    (print-description-to-string (find-class (from-string symbol-name) nil)))                 (kernel:values-specifier-type symbol))
1292                  (:class
1293  (defslimefun describe-alien-type (symbol-name)                 (find-class symbol))
1294    (let ((name (from-string symbol-name)))                (:alien-struct
1295      (ecase (ext:info :alien-type :kind name)                 (ext:info :alien-type :struct symbol))
1296        (:primitive                (:alien-union
1297         (print-description-to-string                 (ext:info :alien-type :union symbol))
1298          (let ((alien::*values-type-okay* t))                (:alien-enum
1299            (funcall (ext:info :alien-type :translator name) (list name)))))                 (ext:info :alien-type :enum symbol))
1300        ((:defined)                (:alien-type
1301         (print-description-to-string (ext:info :alien-type :definition name)))                 (ecase (ext:info :alien-type :kind symbol)
1302        (:unknown                   (:primitive
1303         (format nil "Unkown alien type: ~A" symbol-name)))))                    (let ((alien::*values-type-okay* t))
1304                        (funcall (ext:info :alien-type :translator symbol)
1305  (defmacro %describe-alien (symbol-name namespace)                               (list symbol))))
1306    `(print-description-to-string                   ((:defined)
1307      (ext:info :alien-type ,namespace (from-string ,symbol-name))))                    (ext:info :alien-type :definition symbol))
1308                     (:unknown
1309  (defslimefun describe-alien-struct (symbol-name)                    (return-from describe-definition
1310    (%describe-alien symbol-name :struct))                      (format nil "Unknown alien type: ~S" symbol))))))))
1311    
1312  (defslimefun describe-alien-union (symbol-name)  ;;;;; Argument lists
1313    (%describe-alien symbol-name :union))  
1314    (defimplementation arglist ((name symbol))
1315  (defslimefun describe-alien-enum (symbol-name)    (arglist (or (macro-function name)
1316    (%describe-alien symbol-name :enum))                 (symbol-function name))))
1317    
1318  (defmethod arglist-string (fname)  (defimplementation arglist ((fun function))
1319    "Return a string describing the argument list for FNAME.    (let ((arglist
1320  The result has the format \"(...)\"."           (cond ((eval:interpreted-function-p fun)
1321    (declare (type string fname))                  (eval:interpreted-function-arglist fun))
1322    (multiple-value-bind (function condition)                 ((pcl::generic-function-p fun)
1323        (ignore-errors (values (find-symbol-designator fname *buffer-package*)))                  (pcl:generic-function-lambda-list fun))
1324      (when condition                 ((c::byte-function-or-closure-p fun)
1325        (return-from arglist-string (format nil "(-- ~A)" condition)))                  (byte-code-function-arglist fun))
1326      (let ((arglist                 ((kernel:%function-arglist (kernel:%function-self fun))
1327             (if (not (or (fboundp function)                  (handler-case (read-arglist fun)
1328                          (functionp function)))                    (error () :not-available)))
1329                 "(-- <Unknown-Function>)"                 ;; this should work both for compiled-debug-function
1330                 (let* ((fun (or (macro-function function)                 ;; and for interpreted-debug-function
1331                                 (symbol-function function)))                 (t
1332                        (df (di::function-debug-function fun))                  (handler-case (debug-function-arglist
1333                        (arglist (kernel:%function-arglist fun)))                                 (di::function-debug-function fun))
1334                   (cond ((eval:interpreted-function-p fun)                    (di:unhandled-condition () :not-available))))))
1335                          (eval:interpreted-function-arglist fun))      (check-type arglist (or list (member :not-available)))
1336                         ((pcl::generic-function-p fun)      arglist))
1337                          (pcl::gf-pretty-arglist fun))  
1338                         (arglist arglist)  (defimplementation function-name (function)
1339                         ;; this should work both for    (cond ((eval:interpreted-function-p function)
1340                         ;; compiled-debug-function and for           (eval:interpreted-function-name function))
1341                         ;; interpreted-debug-function          ((pcl::generic-function-p function)
1342                         (df (di::debug-function-lambda-list df))           (pcl::generic-function-name function))
1343                         (t "(<arglist-unavailable>)"))))))          ((c::byte-function-or-closure-p function)
1344        (if (stringp arglist)           (c::byte-function-name function))
1345            arglist          (t (kernel:%function-name (kernel:%function-self function)))))
1346            (to-string arglist)))))  
1347    ;;; A simple case: the arglist is available as a string that we can
1348    ;;; `read'.
1349    
1350    (defun read-arglist (fn)
1351      "Parse the arglist-string of the function object FN."
1352      (let ((string (kernel:%function-arglist
1353                     (kernel:%function-self fn)))
1354            (package (find-package
1355                      (c::compiled-debug-info-package
1356                       (kernel:%code-debug-info
1357                        (vm::find-code-object fn))))))
1358        (with-standard-io-syntax
1359          (let ((*package* (or package *package*)))
1360            (read-from-string string)))))
1361    
1362    ;;; A harder case: an approximate arglist is derived from available
1363    ;;; debugging information.
1364    
1365    (defun debug-function-arglist (debug-function)
1366      "Derive the argument list of DEBUG-FUNCTION from debug info."
1367      (let ((args (di::debug-function-lambda-list debug-function))
1368            (required '())
1369            (optional '())
1370            (rest '())
1371            (key '()))
1372        ;; collect the names of debug-vars
1373        (dolist (arg args)
1374          (etypecase arg
1375            (di::debug-variable
1376             (push (di::debug-variable-symbol arg) required))
1377            ((member :deleted)
1378             (push ':deleted required))
1379            (cons
1380             (ecase (car arg)
1381               (:keyword
1382                (push (second arg) key))
1383               (:optional
1384                (push (debug-variable-symbol-or-deleted (second arg)) optional))
1385               (:rest
1386                (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1387        ;; intersperse lambda keywords as needed
1388        (append (nreverse required)
1389                (if optional (cons '&optional (nreverse optional)))
1390                (if rest (cons '&rest (nreverse rest)))
1391                (if key (cons '&key (nreverse key))))))
1392    
1393    (defun debug-variable-symbol-or-deleted (var)
1394      (etypecase var
1395        (di:debug-variable
1396         (di::debug-variable-symbol var))
1397        ((member :deleted)
1398         '#:deleted)))
1399    
1400    (defun symbol-debug-function-arglist (fname)
1401      "Return FNAME's debug-function-arglist and %function-arglist.
1402    A utility for debugging DEBUG-FUNCTION-ARGLIST."
1403      (let ((fn (fdefinition fname)))
1404        (values (debug-function-arglist (di::function-debug-function fn))
1405                (kernel:%function-arglist (kernel:%function-self fn)))))
1406    
1407    ;;; Deriving arglists for byte-compiled functions:
1408    ;;;
1409    (defun byte-code-function-arglist (fn)
1410      ;; There doesn't seem to be much arglist information around for
1411      ;; byte-code functions.  Use the arg-count and return something like
1412      ;; (arg0 arg1 ...)
1413      (etypecase fn
1414        (c::simple-byte-function
1415         (loop for i from 0 below (c::simple-byte-function-num-args fn)
1416               collect (make-arg-symbol i)))
1417        (c::hairy-byte-function
1418         (hairy-byte-function-arglist fn))
1419        (c::byte-closure
1420         (byte-code-function-arglist (c::byte-closure-function fn)))))
1421    
1422    (defun make-arg-symbol (i)
1423      (make-symbol (format nil "~A~D" (string 'arg) i)))
1424    
1425    ;;; A "hairy" byte-function is one that takes a variable number of
1426    ;;; arguments. `hairy-byte-function' is a type from the bytecode
1427    ;;; interpreter.
1428    ;;;
1429    (defun hairy-byte-function-arglist (fn)
1430      (let ((counter -1))
1431        (flet ((next-arg () (make-arg-symbol (incf counter))))
1432          (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1433                                                keywords-p keywords) fn
1434            (let ((arglist '())
1435                  (optional (- max-args min-args)))
1436              ;; XXX isn't there a better way to write this?
1437              ;; (Looks fine to me. -luke)
1438              (dotimes (i min-args)
1439                (push (next-arg) arglist))
1440              (when (plusp optional)
1441                (push '&optional arglist)
1442                (dotimes (i optional)
1443                  (push (next-arg) arglist)))
1444              (when rest-arg-p
1445                (push '&rest arglist)
1446                (push (next-arg) arglist))
1447              (when keywords-p
1448                (push '&key arglist)
1449                (loop for (key _ __) in keywords
1450                      do (push key arglist))
1451                (when (eq keywords-p :allow-others)
1452                  (push '&allow-other-keys arglist)))
1453              (nreverse arglist))))))
1454    
1455    
1456  ;;;; Miscellaneous.  ;;;; Miscellaneous.
1457    
1458  (defmethod macroexpand-all (form)  (defimplementation macroexpand-all (form)
1459    (walker:macroexpand-all form))    (walker:macroexpand-all form))
1460    
1461  (defun tracedp (fname)  (defimplementation set-default-directory (directory)
   (gethash (debug::trace-fdefinition fname)  
            debug::*traced-functions*))  
   
 (defslimefun toggle-trace-fdefinition (fname-string)  
   (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)))))  
   
 (defslimefun set-default-directory (directory)  
1462    (setf (ext:default-directory) (namestring directory))    (setf (ext:default-directory) (namestring directory))
1463    ;; Setting *default-pathname-defaults* to an absolute directory    ;; Setting *default-pathname-defaults* to an absolute directory
1464    ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.    ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1465    (setf *default-pathname-defaults* (pathname (ext:default-directory)))    (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1466    (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)))))  
   
 (defun source-path-string-position (path string)  
   (with-input-from-string (s string)  
     (source-path-stream-position path s)))  
1467    
1468  (defun source-path-file-position (path filename)  (defimplementation default-directory ()
1469    (with-open-file (file filename)    (namestring (ext:default-directory)))
     (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)))  
1470    
1471  (defun code-location-file-position (code-location filename)  (defimplementation call-without-interrupts (fn)
1472    (with-open-file (s filename :direction :input)    (sys:without-interrupts (funcall fn)))
     (code-location-stream-position code-location s)))  
1473    
1474  (defun debug-source-info-from-emacs-buffer-p (debug-source)  (defimplementation getpid ()
1475    (let ((info (c::debug-source-info debug-source)))    (unix:unix-getpid))
     (and info  
          (consp info)  
          (eq :emacs-buffer (car info)))))  
1476    
1477  (defun source-location-from-code-location (code-location)  (defimplementation lisp-implementation-type-name ()
1478    "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))))))  
1479    
1480  (defun code-location-source-location (code-location)  (defimplementation quit-lisp ()
1481    "Safe wrapper around `code-location-from-source-location'."    (ext::quit))
   (safe-definition-finding  
    (source-location-from-code-location code-location)))  
1482    
1483  (defslimefun getpid ()  ;;; source-path-{stream,file,string,etc}-position moved into
1484    (unix:unix-getpid))  ;;; swank-source-path-parser
1485    
1486    
1487  ;;;; Debugging  ;;;; Debugging
1488    
1489  (defvar *sldb-stack-top*)  (defvar *sldb-stack-top*)
 (defvar *sldb-restarts*)  
1490    
1491  (defmethod call-with-debugging-environment (debugger-loop-fn)  (defimplementation call-with-debugging-environment (debugger-loop-fn)
1492    (unix:unix-sigsetmask 0)    (unix:unix-sigsetmask 0)
1493    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))    (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1494           (*sldb-restarts* (compute-restarts *swank-debugger-condition*))           (debug:*stack-top-hint* nil))
1495           (debug:*stack-top-hint* nil)      (handler-bind ((di::unhandled-condition
          (*debugger-hook* nil)  
          (*readtable* (or debug:*debug-readtable* *readtable*))  
          (*print-level* debug:*debug-print-level*)  
          (*print-length* debug:*debug-print-length*)  
          (*print-readably* nil))  
     (handler-bind ((di:debug-condition  
1496                      (lambda (condition)                      (lambda (condition)
1497                        (signal (make-condition                        (error (make-condition
1498                                 'sldb-condition                                'sldb-condition
1499                                 :original-condition condition)))))                                :original-condition condition)))))
1500        (funcall debugger-loop-fn))))        (funcall debugger-loop-fn))))
1501    
1502  (defun format-restarts-for-emacs ()  (defun frame-down (frame)
1503    "Return a list of restarts for *swank-debugger-condition* in a    (handler-case (di:frame-down frame)
1504  format suitable for Emacs."      (di:no-debug-info () nil)))
   (loop for restart in *sldb-restarts*  
         collect (list (princ-to-string (restart-name restart))  
                       (princ-to-string restart))))  
   
 (defun format-condition-for-emacs ()  
   (format nil "~A~%   [Condition of type ~S]"  
           (debug::safe-condition-message *swank-debugger-condition*)  
           (type-of *swank-debugger-condition*)))  
1505    
1506  (defun nth-frame (index)  (defun nth-frame (index)
1507    (do ((frame *sldb-stack-top* (di:frame-down frame))    (do ((frame *sldb-stack-top* (frame-down frame))
1508         (i index (1- i)))         (i index (1- i)))
1509        ((zerop i) frame)))        ((zerop i) frame)))
1510    
1511  (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."  
1512    (let ((end (or end most-positive-fixnum)))    (let ((end (or end most-positive-fixnum)))
1513      (loop for f = (nth-frame start) then (di:frame-down f)      (loop for f = (nth-frame start) then (frame-down f)
1514            for i from start below end            for i from start below end
1515            while f            while f
1516            collect f)))            collect f)))
1517    
1518  (defmethod backtrace (start end)  (defimplementation print-frame (frame stream)
1519    (mapcar #'format-frame-for-emacs (compute-backtrace start end)))    (let ((*standard-output* stream))
1520        (handler-case
1521  (defmethod debugger-info-for-emacs (start end)          (debug::print-frame-call frame :verbosity 1 :number nil)
1522    (list (format-condition-for-emacs)        (error (e)
1523          (format-restarts-for-emacs)          (ignore-errors (princ e stream))))))
1524          (backtrace start end)))  
1525    (defimplementation frame-package (frame-number)
1526  (defmethod frame-source-location-for-emacs (index)    (find-package
1527       (ignore-errors
1528         (c::compiled-debug-info-package
1529          (kernel:%code-debug-info
1530           (kernel:function-code-header
1531            (di:debug-function-function
1532             (di:frame-debug-function (nth-frame frame-number)))))))))
1533    
1534    (defimplementation frame-source-location-for-emacs (index)
1535    (code-location-source-location (di:frame-code-location (nth-frame index))))    (code-location-source-location (di:frame-code-location (nth-frame index))))
1536    
1537  (defmethod eval-in-frame (form index)  (defimplementation eval-in-frame (form index)
1538    (di:eval-in-frame (nth-frame index) form))    (di:eval-in-frame (nth-frame index) form))
1539    
1540  (defslimefun pprint-eval-string-in-frame (string index)  (defun frame-debug-vars (frame)
1541    (swank-pprint    "Return a vector of debug-variables in frame."
1542     (multiple-value-list    (di::debug-function-debug-variables (di:frame-debug-function frame)))
1543      (di:eval-in-frame (nth-frame index) (from-string string)))))  
1544    (defun debug-var-value (var frame location)
1545  (defslimefun inspect-in-frame (string index)    (ecase (di:debug-variable-validity var location)
1546    (reset-inspector)      (:valid (di:debug-variable-value var frame))
1547    (inspect-object (di:eval-in-frame (nth-frame index) (from-string string))))      ((:invalid :unknown) ':<not-available>)))
1548    
1549  (defmethod frame-locals (index)  (defimplementation frame-locals (index)
1550    (let* ((frame (nth-frame index))    (let* ((frame (nth-frame index))
1551           (location (di:frame-code-location frame))           (loc (di:frame-code-location frame))
1552           (debug-function (di:frame-debug-function frame))           (vars (frame-debug-vars frame)))
1553           (debug-variables (di::debug-function-debug-variables debug-function)))      (loop for v across vars collect
1554      (loop for v across debug-variables            (list :name (di:debug-variable-symbol v)
1555            collect (list                  :id (di:debug-variable-id v)
1556                     :symbol (di:debug-variable-symbol v)                  :value (debug-var-value v frame loc)))))
1557                     :id (di:debug-variable-id v)  
1558                     :value-string  (defimplementation frame-var-value (frame var)
1559                     (if (eq (di:debug-variable-validity v location)    (let* ((frame (nth-frame frame))
1560                             :valid)           (dvar (aref (frame-debug-vars frame) var)))
1561                         (to-string (di:debug-variable-value v frame))      (debug-var-value dvar frame (di:frame-code-location frame))))
1562                         "<not-available>")))))  
1563    (defimplementation frame-catch-tags (index)
1564  (defmethod frame-catch-tags (index)    (mapcar #'car (di:frame-catches (nth-frame index))))
1565    (loop for (tag . code-location) in (di:frame-catches (nth-frame index))  
1566          collect `(,tag . ,(code-location-source-location code-location))))  (defimplementation return-from-frame (index form)
1567      (let ((sym (find-symbol (string 'find-debug-tag-for-frame)
1568                              :debug-internals)))
1569        (if sym
1570            (let* ((frame (nth-frame index))
1571                   (probe (funcall sym frame)))
1572              (cond (probe (throw (car probe) (eval-in-frame form index)))
1573                    (t (format nil "Cannot return from frame: ~S" frame))))
1574            "return-from-frame is not implemented in this version of CMUCL.")))
1575    
1576  (defslimefun invoke-nth-restart (index)  (defimplementation activate-stepping (frame)
1577    (invoke-restart-interactively (nth-restart index)))    (set-step-breakpoints (nth-frame frame)))
1578    
1579  (defslimefun sldb-abort ()  (defimplementation sldb-break-on-return (frame)
1580    (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))    (break-on-return (nth-frame frame)))
1581    
1582    ;;; We set the breakpoint the caller which might be a bit confusing.
1583    ;;;
1584    (defun break-on-return (frame)
1585      (let* ((caller (di:frame-down frame))
1586             (cl (di:frame-code-location caller)))
1587        (flet ((hook (frame bp)
1588                 (when (frame-pointer= frame caller)
1589                   (di:delete-breakpoint bp)
1590                   (signal-breakpoint bp frame))))
1591          (let* ((info (ecase (di:code-location-kind cl)
1592                         ((:single-value-return :unknown-return) nil)
1593                         (:known-return (debug-function-returns
1594                                         (di:frame-debug-function frame)))))
1595                 (bp (di:make-breakpoint #'hook cl :kind :code-location
1596                                         :info info)))
1597            (di:activate-breakpoint bp)
1598            `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1599    
1600    (defun frame-pointer= (frame1 frame2)
1601      "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1602      (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1603    
1604    ;;; The PC in escaped frames at a single-return-value point is
1605    ;;; actually vm:single-value-return-byte-offset bytes after the
1606    ;;; position given in the debug info.  Here we try to recognize such
1607    ;;; cases.
1608    ;;;
1609    (defun next-code-locations (frame code-location)
1610      "Like `debug::next-code-locations' but be careful in escaped frames."
1611      (let ((next (debug::next-code-locations code-location)))
1612        (flet ((adjust-pc ()
1613                 (let ((cl (di::copy-compiled-code-location code-location)))
1614                   (incf (di::compiled-code-location-pc cl)
1615                         vm:single-value-return-byte-offset)
1616                   cl)))
1617          (cond ((and (di::compiled-frame-escaped frame)
1618                      (eq (di:code-location-kind code-location)
1619                          :single-value-return)
1620                      (= (length next) 1)
1621                      (di:code-location= (car next) (adjust-pc)))
1622                 (debug::next-code-locations (car next)))
1623                (t
1624                 next)))))
1625    
1626    (defun set-step-breakpoints (frame)
1627      (let ((cl (di:frame-code-location frame)))
1628        (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1629          (error "Cannot step in elsewhere code"))
1630        (let* ((debug::*bad-code-location-types*
1631                (remove :call-site debug::*bad-code-location-types*))
1632               (next (next-code-locations frame cl)))
1633          (cond (next
1634                 (let ((steppoints '()))
1635                   (flet ((hook (bp-frame bp)
1636                            (signal-breakpoint bp bp-frame)
1637                            (mapc #'di:delete-breakpoint steppoints)))
1638                     (dolist (code-location next)
1639                       (let ((bp (di:make-breakpoint #'hook code-location
1640                                                     :kind :code-location)))
1641                         (di:activate-breakpoint bp)
1642                         (push bp steppoints))))))
1643                (t
1644                 (break-on-return frame))))))
1645    
1646    
1647    ;; XXX the return values at return breakpoints should be passed to the
1648    ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1649    
1650    ;;; The sigcontext and the PC for a breakpoint invocation are not
1651    ;;; passed to user hook functions, but we need them to extract return
1652    ;;; values. So we advice di::handle-breakpoint and bind the values to
1653    ;;; special variables.
1654    ;;;
1655    (defvar *breakpoint-sigcontext*)
1656    (defvar *breakpoint-pc*)
1657    
1658    ;; XXX don't break old versions without fwrappers.  Remove this one day.
1659    #+#.(cl:if (cl:find-package :fwrappers) '(and) '(or))
1660    (progn
1661      (fwrappers:define-fwrapper bind-breakpoint-sigcontext (offset c sigcontext)
1662        (let ((*breakpoint-sigcontext* sigcontext)
1663              (*breakpoint-pc* offset))
1664          (fwrappers:call-next-function)))
1665      (fwrappers:set-fwrappers 'di::handle-breakpoint '())
1666      (fwrappers:fwrap 'di::handle-breakpoint #'bind-breakpoint-sigcontext))
1667    
1668    (defun sigcontext-object (sc index)
1669      "Extract the lisp object in sigcontext SC at offset INDEX."
1670      (kernel:make-lisp-obj (vm:sigcontext-register sc index)))
1671    
1672    (defun known-return-point-values (sigcontext sc-offsets)
1673      (let ((fp (system:int-sap (vm:sigcontext-register sigcontext
1674                                                        vm::cfp-offset))))
1675        (system:without-gcing
1676         (loop for sc-offset across sc-offsets
1677               collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1678    
1679    ;;; CMUCL returns the first few values in registers and the rest on
1680    ;;; the stack. In the multiple value case, the number of values is
1681    ;;; stored in a dedicated register. The values of the registers can be
1682    ;;; accessed in the sigcontext for the breakpoint.  There are 3 kinds
1683    ;;; of return conventions: :single-value-return, :unknown-return, and
1684    ;;; :known-return.
1685    ;;;
1686    ;;; The :single-value-return convention returns the value in a
1687    ;;; register without setting the nargs registers.
1688    ;;;
1689    ;;; The :unknown-return variant is used for multiple values. A
1690    ;;; :unknown-return point consists actually of 2 breakpoints: one for
1691    ;;; the single value case and one for the general case.  The single
1692    ;;; value breakpoint comes vm:single-value-return-byte-offset after
1693    ;;; the multiple value breakpoint.
1694    ;;;
1695    ;;; The :known-return convention is used by local functions.
1696    ;;; :known-return is currently not supported because we don't know
1697    ;;; where the values are passed.
1698    ;;;
1699    (defun breakpoint-values (breakpoint)
1700      "Return the list of return values for a return point."
1701      (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1702        (let ((sc (locally (declare (optimize (ext:inhibit-warnings 3)))
1703                    (alien:sap-alien *breakpoint-sigcontext* (* unix:sigcontext))))
1704              (cl (di:breakpoint-what breakpoint)))
1705          (ecase (di:code-location-kind cl)
1706            (:single-value-return
1707             (list (1st sc)))
1708            (:known-return
1709             (let ((info (di:breakpoint-info breakpoint)))
1710               (if (vectorp info)
1711                   (known-return-point-values sc info)
1712                   (list "<<known-return convention not supported>>"))))
1713            (:unknown-return
1714             (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1715               (if (= mv-return-pc *breakpoint-pc*)
1716                   (di::get-function-end-breakpoint-values sc)
1717                   (list (1st sc)))))))))
1718    
1719    (defun debug-function-returns (debug-fun)
1720      "Return the return style of DEBUG-FUN."
1721      (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1722        (c::compiled-debug-function-returns cdfun)))
1723    
1724    (define-condition breakpoint (simple-condition)
1725      ((message :initarg :message :reader breakpoint.message))
1726      (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1727    
1728    (defimplementation condition-extras ((c breakpoint))
1729      ;; simply pop up the source buffer
1730      `((:short-frame-source 0)))
1731    
1732    (defun signal-breakpoint (breakpoint frame)
1733      "Signal a breakpoint condition for BREAKPOINT in FRAME.
1734    Try to create a informative message."
1735      (flet ((brk (fstring &rest args)
1736               (let ((msg (apply #'format nil fstring args))
1737                     (debug:*stack-top-hint* frame))
1738                 (break 'breakpoint :message msg))))
1739      (with-struct (di::breakpoint- kind what) breakpoint
1740        (case kind
1741          (:code-location
1742           (case (di:code-location-kind what)
1743             ((:single-value-return :known-return :unknown-return)
1744              (brk "Return value: ~{~S ~}" (breakpoint-values breakpoint)))
1745             (t
1746              (brk "Breakpoint: ~S ~S"
1747                   (di:code-location-kind what)
1748                   (di::compiled-code-location-pc what)))))
1749          (:function-start
1750           (brk "Function start breakpoint"))
1751          (t (brk "Breakpoint: ~A in ~A" breakpoint frame))))))
1752    
1753    (defimplementation sldb-break-at-start (fname)
1754      (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1755        (cond ((not debug-fun)
1756               `(:error ,(format nil "~S has no debug-function" fname)))
1757              (t
1758               (flet ((hook (frame bp &optional args cookie)
1759                        (declare (ignore args cookie))
1760                        (signal-breakpoint bp frame)))
1761                 (let ((bp (di:make-breakpoint #'hook debug-fun
1762                                               :kind :function-start)))
1763                   (di:activate-breakpoint bp)
1764                   `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1765    
1766    (defun frame-cfp (frame)
1767      "Return the Control-Stack-Frame-Pointer for FRAME."
1768      (etypecase frame
1769        (di::compiled-frame (di::frame-pointer frame))
1770        ((or di::interpreted-frame null) -1)))
1771    
1772    (defun frame-ip (frame)
1773      "Return the (absolute) instruction pointer and the relative pc of FRAME."
1774      (if (not frame)
1775          -1
1776          (let ((debug-fun (di::frame-debug-function frame)))
1777            (etypecase debug-fun
1778              (di::compiled-debug-function
1779               (let* ((code-loc (di:frame-code-location frame))
1780                      (component (di::compiled-debug-function-component debug-fun))
1781                      (pc (di::compiled-code-location-pc code-loc))
1782                      (ip (sys:without-gcing
1783                           (sys:sap-int
1784                            (sys:sap+ (kernel:code-instructions component) pc)))))
1785                 (values ip pc)))
1786              ((or di::bogus-debug-function di::interpreted-debug-function)
1787               -1)))))
1788    
1789    (defun frame-registers (frame)
1790      "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1791      (let* ((cfp (frame-cfp frame))
1792             (csp (frame-cfp (di::frame-up frame)))
1793             (ip (frame-ip frame))
1794             (ocfp (frame-cfp (di::frame-down frame)))
1795             (lra (frame-ip (di::frame-down frame))))
1796        (values csp cfp ip ocfp lra)))
1797    
1798    (defun print-frame-registers (frame-number)
1799      (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1800        (flet ((fixnum (p) (etypecase p
1801                             (integer p)
1802                             (sys:system-area-pointer (sys:sap-int p)))))
1803          (apply #'format t "~
1804    CSP  =  ~X
1805    CFP  =  ~X
1806    IP   =  ~X
1807    OCFP =  ~X
1808    LRA  =  ~X~%" (mapcar #'fixnum
1809                          (multiple-value-list (frame-registers frame)))))))
1810    
1811    
1812    (defimplementation disassemble-frame (frame-number)
1813      "Return a string with the disassembly of frames code."
1814      (print-frame-registers frame-number)
1815      (terpri)
1816      (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1817             (debug-fun (di::frame-debug-function frame)))
1818        (etypecase debug-fun
1819          (di::compiled-debug-function
1820           (let* ((component (di::compiled-debug-function-component debug-fun))
1821                  (fun (di:debug-function-function debug-fun)))
1822             (if fun
1823                 (disassemble fun)
1824                 (disassem:disassemble-code-component component))))
1825          (di::bogus-debug-function
1826           (format t "~%[Disassembling bogus frames not implemented]")))))
1827    
1828    
1829  ;;;; Inspecting  ;;;; Inspecting
1830    
1831  (defvar *inspectee*)  (defclass cmucl-inspector (inspector)
1832  (defvar *inspectee-parts*)    ())
1833  (defvar *inspector-stack* '())  
1834  (defvar *inspector-history* (make-array 10 :adjustable t :fill-pointer 0))  (defimplementation make-default-inspector ()
1835  (defvar *inspect-length* 30)    (make-instance 'cmucl-inspector))
1836    
 (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))))))  
1837  (defconstant +lowtag-symbols+  (defconstant +lowtag-symbols+
1838    '(vm:even-fixnum-type    '(vm:even-fixnum-type
1839      vm:function-pointer-type      vm:function-pointer-type
# Line 1152  stack." Line 1842  stack."
1842      vm:odd-fixnum-type      vm:odd-fixnum-type
1843      vm:instance-pointer-type      vm:instance-pointer-type
1844      vm:other-immediate-1-type      vm:other-immediate-1-type
1845      vm:other-pointer-type))      vm:other-pointer-type)
1846      "Names of the constants that specify type tags.
1847    The `symbol-value' of each element is a type tag.")
1848    
1849  (defconstant +header-type-symbols+  (defconstant +header-type-symbols+
1850    ;; Is there a convinient place for all those constants?    (flet ((suffixp (suffix string)
1851    (flet ((tail-comp (string tail)             (and (>= (length string) (length suffix))
1852             (and (>= (length string) (length tail))                  (string= string suffix :start1 (- (length string)
1853                  (string= string tail :start1 (- (length string)                                                    (length suffix))))))
1854                                                  (length tail))))))      ;; Is there a convinient place for all those constants?
1855      (remove-if-not      (remove-if-not
1856       (lambda (x) (and (tail-comp (symbol-name x) "-TYPE")       (lambda (x) (and (suffixp "-TYPE" (symbol-name x))
1857                        (not (member x +lowtag-symbols+))                        (not (member x +lowtag-symbols+))
1858                        (boundp x)                        (boundp x)
1859                        (typep (symbol-value x) 'fixnum)))                        (typep (symbol-value x) 'fixnum)))
1860       (append (apropos-list "-TYPE" "VM" t)       (append (apropos-list "-TYPE" "VM" t)
1861               (apropos-list "-TYPE" "BIGNUM" t)))))               (apropos-list "-TYPE" "BIGNUM" t))))
1862      "A list of names of the type codes in boxed objects.")
1863    
1864  (defun describe-primitive-type (object)  (defimplementation describe-primitive-type (object)
1865    (with-output-to-string (*standard-output*)    (with-output-to-string (*standard-output*)
1866      (let* ((lowtag (kernel:get-lowtag object))      (let* ((lowtag (kernel:get-lowtag object))
1867             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))             (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1868        (format t "[lowtag: ~A" lowtag-symbol)        (format t "lowtag: ~A" lowtag-symbol)
1869        (cond ((member lowtag (list vm:other-pointer-type        (when (member lowtag (list vm:other-pointer-type
1870                                    vm:function-pointer-type                                   vm:function-pointer-type
1871                                    vm:other-immediate-0-type                                   vm:other-immediate-0-type
1872                                    vm:other-immediate-1-type                                   vm:other-immediate-1-type
1873                                    ))                                   ))
1874               (let* ((type (kernel:get-type object))          (let* ((type (kernel:get-type object))
1875                      (type-symbol (find type +header-type-symbols+                 (type-symbol (find type +header-type-symbols+
1876                                         :key #'symbol-value)))                                    :key #'symbol-value)))
1877                 (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."))  
1878    
1879  (defmethod inspected-parts (o)  (defimplementation inspect-for-emacs ((o t) (inspector cmucl-inspector))
1880    (cond ((di::indirect-value-cell-p o)    (cond ((di::indirect-value-cell-p o)
1881           (inspected-parts-of-value-cell o))           (values (format nil "~A is a value cell." o)
1882                     `("Value: " (:value ,(c:value-cell-ref o)))))
1883          (t          (t
1884           (destructuring-bind (text labeledp . parts)           (destructuring-bind (text labeledp . parts)
1885               (inspect::describe-parts o)               (inspect::describe-parts o)
1886             (let ((parts (if labeledp             (values (format nil "~A~%" text)
1887                              (loop for (label . value) in parts                     (if labeledp
1888                                    collect (cons (string label) value))                         (loop for (label . value) in parts
1889                              (loop for value in parts                               append (label-value-line label value))
1890                                    for i from 0                         (loop for value in parts  for i from 0
1891                                    collect (cons (format nil "~D" i) value)))))                               append (label-value-line 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.~%")))))))  
1892    
1893  (defmethod inspected-parts ((o function))  (defmethod inspect-for-emacs :around ((o function) (inspector cmucl-inspector))
1894      (declare (ignore inspector))
1895    (let ((header (kernel:get-type o)))    (let ((header (kernel:get-type o)))
1896      (cond ((= header vm:function-header-type)      (cond ((= header vm:function-header-type)
1897             (values             (values (format nil "~A is a function." o)
1898              (format nil "~A~% is a function." o)                     (append (label-value-line*
1899              (list (cons "Self" (kernel:%function-self o))                              ("Self" (kernel:%function-self o))
1900                    (cons "Next" (kernel:%function-next o))                              ("Next" (kernel:%function-next o))
1901                    (cons "Name" (kernel:%function-name o))                              ("Name" (kernel:%function-name o))
1902                    (cons "Arglist" (kernel:%function-arglist o))                              ("Arglist" (kernel:%function-arglist o))
1903                    (cons "Type" (kernel:%function-type o))                              ("Type" (kernel:%function-type o))
1904                    (cons "Code Object" (kernel:function-code-header o)))))                              ("Code" (kernel:function-code-header o)))
1905            ((= header vm:closure-header-type)                             (list
1906             (values (format nil "~A~% is a closure." o)                              (with-output-to-string (s)
1907                     (list*                                (disassem:disassemble-function o :stream s))))))
1908                      (cons "Function" (kernel:%closure-function o))            ((= header vm:closure-header-type)
1909                      (loop for i from 0 below (- (kernel:get-closure-length o)             (values (format nil "~A is a closure" o)
1910                                                  (1- vm:closure-info-offset))                     (append
1911                            collect (cons (format nil "~D" i)                      (label-value-line "Function" (kernel:%closure-function o))
1912                                          (kernel:%closure-index-ref o i))))))                      `("Environment:" (:newline))
1913            (t (call-next-method o)))))                      (loop for i from 0 below (1- (kernel:get-closure-length o))
1914                              append (label-value-line
1915  (defmethod inspected-parts ((o kernel:code-component))                                    i (kernel:%closure-index-ref o i))))))
1916    (values (format nil "~A~% is a code data-block." o)            (t
1917            `(("First entry point" . ,(kernel:%code-entry-points o))             (call-next-method)))))
1918              ,@(loop for i from vm:code-constants-offset  
1919                      below (kernel:get-header-data o)  
1920                      collect (cons (format nil "Constant#~D" i)  (defmethod inspect-for-emacs ((o kernel:code-component) (_ cmucl-inspector))
1921                                    (kernel:code-header-ref o i)))    (declare (ignore _))
1922              ("Debug info" . ,(kernel:%code-debug-info o))    (values (format nil "~A is a code data-block." o)
1923              ("Instructions"  . ,(kernel:code-instructions o)))))            (append
1924               (label-value-line*
1925  (defmethod inspected-parts ((o kernel:fdefn))              ("code-size" (kernel:%code-code-size o))
1926    (values (format nil "~A~% is a fdefn object." o)              ("entry-points" (kernel:%code-entry-points o))
1927            `(("Name" . ,(kernel:fdefn-name o))              ("debug-info" (kernel:%code-debug-info o))
1928              ("Function" . ,(kernel:fdefn-function o)))))              ("trace-table-offset" (kernel:code-header-ref
1929                                       o vm:code-trace-table-offset-slot)))
1930  ;;; Local Variables:             `("Constants:" (:newline))
1931  ;;; eval: (font-lock-add-keywords 'lisp-mode '(("(\\(defslimefun\\)\\s +\\(\\(\\w\\|\\s_\\)+\\)"  (1 font-lock-keyword-face) (2 font-lock-function-name-face))))             (loop for i from vm:code-constants-offset
1932  ;;; End:                   below (kernel:get-header-data o)
1933                     append (label-value-line i (kernel:code-header-ref o i)))
1934               `("Code:" (:newline)
1935                 , (with-output-to-string (s)
1936                     (cond ((kernel:%code-debug-info o)
1937                            (disassem:disassemble-code-component o :stream s))
1938                           (t
1939                            (disassem:disassemble-memory
1940                             (disassem::align
1941                              (+ (logandc2 (kernel:get-lisp-obj-address o)
1942                                           vm:lowtag-mask)
1943                                 (* vm:code-constants-offset vm:word-bytes))
1944                              (ash 1 vm:lowtag-bits))
1945                             (ash (kernel:%code-code-size o) vm:word-shift)
1946                             :stream s))))))))
1947    
1948    (defmethod inspect-for-emacs ((o kernel:fdefn) (inspector cmucl-inspector))
1949      (declare (ignore inspector))
1950      (values (format nil "~A is a fdenf object." o)
1951              (label-value-line*
1952               ("name" (kernel:fdefn-name o))
1953               ("function" (kernel:fdefn-function o))
1954               ("raw-addr" (sys:sap-ref-32
1955                            (sys:int-sap (kernel:get-lisp-obj-address o))
1956                            (* vm:fdefn-raw-addr-slot vm:word-bytes))))))
1957    
1958    
1959    ;;;; Profiling
1960    (defimplementation profile (fname)
1961      (eval `(profile:profile ,fname)))
1962    
1963    (defimplementation unprofile (fname)
1964      (eval `(profile:unprofile ,fname)))
1965    
1966    (defimplementation unprofile-all ()
1967      (eval `(profile:unprofile))
1968      "All functions unprofiled.")
1969    
1970    (defimplementation profile-report ()
1971      (eval `(profile:report-time)))
1972    
1973    (defimplementation profile-reset ()
1974      (eval `(profile:reset-time))
1975      "Reset profiling counters.")
1976    
1977    (defimplementation profiled-functions ()
1978      profile:*timed-functions*)
1979    
1980    (defimplementation profile-package (package callers methods)
1981      (profile:profile-all :package package
1982                           :callers-p callers
1983                           #-cmu18e :methods #-cmu18e methods))
1984    
1985    
1986    ;;;; Multiprocessing
1987    
1988    #+mp
1989    (progn
1990      (defimplementation startup-multiprocessing ()
1991        ;; Threads magic: this never returns! But top-level becomes
1992        ;; available again.
1993        (mp::startup-idle-and-top-level-loops))
1994    
1995      (defimplementation spawn (fn &key (name "Anonymous"))
1996        (mp:make-process fn :name name))
1997    
1998      (defvar *thread-id-counter* 0)
1999    
2000      (defimplementation thread-id (thread)
2001        (or (getf (mp:process-property-list thread) 'id)
2002            (setf (getf (mp:process-property-list thread) 'id)
2003                  (incf *thread-id-counter*))))
2004    
2005      (defimplementation find-thread (id)
2006        (find id (all-threads)
2007              :key (lambda (p) (getf (mp:process-property-list p) 'id))))
2008    
2009      (defimplementation thread-name (thread)
2010        (mp:process-name thread))
2011    
2012      (defimplementation thread-status (thread)
2013        (mp:process-whostate thread))
2014    
2015      (defimplementation current-thread ()
2016        mp:*current-process*)
2017    
2018      (defimplementation all-threads ()
2019        (copy-list mp:*all-processes*))
2020    
2021      (defimplementation interrupt-thread (thread fn)
2022        (mp:process-interrupt thread fn))
2023    
2024      (defimplementation kill-thread (thread)
2025        (mp:destroy-process thread))
2026    
2027      (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
2028    
2029      (defstruct (mailbox (:conc-name mailbox.))
2030        (mutex (mp:make-lock "process mailbox"))
2031        (queue '() :type list))
2032    
2033      (defun mailbox (thread)
2034        "Return THREAD's mailbox."
2035        (mp:with-lock-held (*mailbox-lock*)
2036          (or (getf (mp:process-property-list thread) 'mailbox)
2037              (setf (getf (mp:process-property-list thread) 'mailbox)
2038                    (make-mailbox)))))
2039    
2040      (defimplementation send (thread message)
2041        (let* ((mbox (mailbox thread))
2042               (mutex (mailbox.mutex mbox)))
2043          (mp:with-lock-held (mutex)
2044            (setf (mailbox.queue mbox)
2045                  (nconc (mailbox.queue mbox) (list message))))))
2046    
2047      (defimplementation receive ()
2048        (let* ((mbox (mailbox mp:*current-process*))
2049               (mutex (mailbox.mutex mbox)))
2050          (mp:process-wait "receive" #'mailbox.queue mbox)
2051          (mp:with-lock-held (mutex)
2052            (pop (mailbox.queue mbox)))))
2053    
2054      ) ;; #+mp
2055    
2056    ;; Local Variables:
2057    ;; pbook-heading-regexp:    "^;;;\\(;+\\)"
2058    ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2059    ;; End:

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

  ViewVC Help
Powered by ViewVC 1.1.5