/[slime]/slime/swank-scl.lisp
ViewVC logotype

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (hide annotations)
Sat Aug 4 23:48:19 2012 UTC (20 months, 1 week ago) by sboukarev
Branch: MAIN
CVS Tags: HEAD
Changes since 1.41: +10 -12 lines
* clean up: (signal (make-condition ...)) => (signal ...)
1 dcrosher 1.1 ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2     ;;;
3     ;;; Scieneer Common Lisp code for SLIME.
4     ;;;
5     ;;; This code has been placed in the Public Domain. All warranties
6     ;;; are disclaimed.
7     ;;;
8    
9     (in-package :swank-backend)
10    
11    
12    
13     ;;; swank-mop
14    
15 dcrosher 1.7 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
16 dcrosher 1.1
17     (defun swank-mop:slot-definition-documentation (slot)
18 dcrosher 1.7 (documentation slot t))
19 dcrosher 1.1
20    
21     ;;;; TCP server
22     ;;;
23     ;;; SCL only supports the :spawn communication style.
24     ;;;
25    
26     (defimplementation preferred-communication-style ()
27     :spawn)
28    
29 heller 1.39 (defimplementation create-socket (host port &key backlog)
30 dcrosher 1.1 (let ((addr (resolve-hostname host)))
31 heller 1.39 (ext:create-inet-listener port :stream :host addr :reuse-address t
32 dcrosher 1.41 :backlog (or backlog 5))))
33 dcrosher 1.1
34     (defimplementation local-port (socket)
35     (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
36    
37     (defimplementation close-socket (socket)
38     (ext:close-socket (socket-fd socket)))
39    
40 heller 1.10 (defimplementation accept-connection (socket
41     &key external-format buffering timeout)
42 heller 1.38 (let ((buffering (or buffering :full))
43 dcrosher 1.6 (fd (socket-fd socket)))
44     (loop
45     (let ((ready (sys:wait-until-fd-usable fd :input timeout)))
46     (unless ready
47     (error "Timeout accepting connection on socket: ~S~%" socket)))
48     (let ((new-fd (ignore-errors (ext:accept-tcp-connection fd))))
49     (when new-fd
50 heller 1.38 (return (make-socket-io-stream new-fd external-format
51     (ecase buffering
52     ((t) :full)
53     ((nil) :none)
54     (:line :line)))))))))
55 dcrosher 1.6
56     (defimplementation set-stream-timeout (stream timeout)
57     (check-type timeout (or null real))
58     (if (fboundp 'ext::stream-timeout)
59     (setf (ext::stream-timeout stream) timeout)
60 dcrosher 1.14 (setf (slot-value (slot-value stream 'lisp::stream) 'lisp::timeout)
61     timeout)))
62 dcrosher 1.1
63     ;;;;; Sockets
64    
65     (defun socket-fd (socket)
66     "Return the file descriptor for the socket represented by 'socket."
67     (etypecase socket
68     (fixnum socket)
69     (stream (sys:fd-stream-fd socket))))
70    
71     (defun resolve-hostname (hostname)
72     "Return the IP address of 'hostname as an integer (in host byte-order)."
73     (let ((hostent (ext:lookup-host-entry hostname)))
74     (car (ext:host-entry-addr-list hostent))))
75    
76 heller 1.12 (defvar *external-format-to-coding-system*
77     '((:iso-8859-1
78     "latin-1" "latin-1-unix" "iso-latin-1-unix"
79     "iso-8859-1" "iso-8859-1-unix")
80     (:utf-8 "utf-8" "utf-8-unix")
81     (:euc-jp "euc-jp" "euc-jp-unix")))
82    
83     (defimplementation find-external-format (coding-system)
84     (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
85     *external-format-to-coding-system*)))
86 dcrosher 1.1
87 dcrosher 1.3 (defun make-socket-io-stream (fd external-format buffering)
88 dcrosher 1.1 "Create a new input/output fd-stream for 'fd."
89 heller 1.38 (cond ((not external-format)
90     (sys:make-fd-stream fd :input t :output t :buffering buffering
91     :element-type '(unsigned-byte 8)))
92     (t
93     (let* ((stream (sys:make-fd-stream fd :input t :output t
94     :element-type 'base-char
95     :buffering buffering
96     :external-format external-format)))
97     ;; Ignore character conversion errors. Without this the
98     ;; communication channel is prone to lockup if a character
99     ;; conversion error occurs.
100     (setf (lisp::character-conversion-stream-input-error-value stream)
101     #\?)
102     (setf (lisp::character-conversion-stream-output-error-value stream)
103     #\?)
104     stream))))
105 dcrosher 1.1
106    
107     ;;;; Stream handling
108    
109     (defclass slime-input-stream (ext:character-input-stream)
110     ((buffer :initarg :buffer :type string)
111     (index :initarg :index :initform 0 :type fixnum)
112     (position :initarg :position :initform 0 :type integer)
113     (interactive :initarg :interactive :initform nil :type (member nil t))
114     (input-fn :initarg :input-fn :type function)
115     ))
116    
117 heller 1.23 (defun make-slime-input-stream (input-fn)
118 dcrosher 1.1 (declare (function input-fn))
119     (make-instance 'slime-input-stream
120     :in-buffer (make-string 256)
121     :in-head 0 :in-tail 0
122     :out-buffer ""
123     :buffer "" :index 0
124 heller 1.23 :input-fn input-fn))
125 dcrosher 1.1
126     (defmethod print-object ((s slime-input-stream) stream)
127     (print-unreadable-object (s stream :type t)))
128    
129     ;;; input-stream-p inherits from input-stream.
130     ;;; output-stream-p inherits nil.
131    
132     (defmethod ext:stream-listen ((stream slime-input-stream))
133     (let* ((buffer (slot-value stream 'buffer))
134     (index (slot-value stream 'index))
135     (length (length buffer)))
136     (declare (type string buffer)
137     (fixnum index length))
138     (< index length)))
139    
140     (defmethod close ((stream slime-input-stream) &key ((:abort abort) nil))
141     (declare (ignore abort))
142     (when (ext:stream-open-p stream)
143     (setf (ext:stream-open-p stream) nil)
144     (setf (ext:stream-in-buffer stream) " ")
145     t))
146    
147     (defmethod ext:stream-clear-input ((stream slime-input-stream))
148     (let* ((input-buffer (slot-value stream 'buffer))
149     (index (slot-value stream 'index))
150     (input-length (length input-buffer))
151     (available (- input-length index))
152     (position (slot-value stream 'position))
153     (new-position (+ position available)))
154     (declare (type kernel:index index available position new-position))
155     (setf (slot-value stream 'position) new-position))
156     (setf (slot-value stream 'buffer) "")
157     (setf (slot-value stream 'index) 0)
158     nil)
159    
160     ;;; No 'stream-finish-output method.
161     ;;; No 'stream-force-output method.
162     ;;; No 'stream-clear-output method.
163    
164     ;;; stream-element-type inherits from character-stream.
165    
166     ;;; No 'stream-line-length method.
167     ;;; No 'stream-line-column method.
168    
169     ;;; Add the remaining input to the current position.
170     (defmethod file-length ((stream slime-input-stream))
171     (let* ((input-buffer (slot-value stream 'buffer))
172     (index (slot-value stream 'index))
173     (input-length (length input-buffer))
174     (available (- input-length index))
175     (position (slot-value stream 'position))
176     (file-length (+ position available)))
177     (declare (type kernel:index index available position file-length))
178     file-length))
179    
180     (defmethod ext:stream-file-position ((stream slime-input-stream)
181     &optional position)
182     (let ((current-position (slot-value stream 'position)))
183     (declare (type kernel:index current-position))
184     (cond (position
185     ;; Could make an attempt here, but just give up for now.
186     nil)
187     (t
188     current-position))))
189    
190     (defmethod interactive-stream-p ((stream slime-input-stream))
191     (slot-value stream 'interactive))
192    
193     ;;; No 'file-string-length method.
194    
195     (defmethod ext:stream-read-chars ((stream slime-input-stream) buffer
196     start requested waitp)
197     (declare (type simple-string buffer)
198     (type kernel:index start requested))
199     (let* ((input-buffer (slot-value stream 'buffer))
200     (index (slot-value stream 'index))
201     (input-length (length input-buffer))
202     (available (- input-length index))
203     (copy (min available requested)))
204     (declare (string input-buffer)
205     (type kernel:index index available copy))
206     (cond ((plusp copy)
207     (dotimes (i copy)
208     (declare (type kernel:index i))
209     (setf (aref buffer (+ start i)) (aref input-buffer (+ index i))))
210 dcrosher 1.5 (setf (slot-value stream 'index) (+ index copy))
211 dcrosher 1.1 (incf (slot-value stream 'position) copy)
212     copy)
213     (waitp
214 heller 1.23 (let ((input-fn (slot-value stream 'input-fn)))
215 dcrosher 1.1 (declare (type function input-fn))
216     (let ((new-input (funcall input-fn)))
217     (cond ((zerop (length new-input))
218     -1)
219     (t
220     (setf (slot-value stream 'buffer) new-input)
221     (setf (slot-value stream 'index) 0)
222 dcrosher 1.5 (ext:stream-read-chars stream buffer
223     start requested waitp))))))
224 dcrosher 1.1 (t
225     0))))
226    
227     ;;; Slime output stream.
228    
229     (defclass slime-output-stream (ext:character-output-stream)
230     ((output-fn :initarg :output-fn :type function)
231 dcrosher 1.26 (output-buffer :initarg :output-buffer :type simple-string)
232     (buffer-tail :initarg :buffer-tail :initform 0 :type kernel:index)
233     (last-write :initarg :last-write)
234 dcrosher 1.1 (column :initform 0 :type kernel:index)
235     (interactive :initform nil :type (member nil t))
236     (position :initform 0 :type integer)))
237    
238     (defun make-slime-output-stream (output-fn)
239     (declare (function output-fn))
240     (make-instance 'slime-output-stream
241     :in-buffer ""
242 dcrosher 1.26 :out-buffer ""
243     :output-buffer (make-string 256)
244     :output-fn output-fn
245     :last-write (get-internal-real-time)
246     ))
247 dcrosher 1.1
248     (defmethod print-object ((s slime-output-stream) stream)
249     (print-unreadable-object (s stream :type t)))
250    
251     ;;; Use default 'input-stream-p method for 'output-stream which returns 'nil.
252     ;;; Use default 'output-stream-p method for 'output-stream which returns 't.
253    
254     ;;; No 'stream-listen method.
255    
256     (defmethod close ((stream slime-output-stream) &key ((:abort abort) nil))
257     (when (ext:stream-open-p stream)
258     (unless abort
259     (finish-output stream))
260     (setf (ext:stream-open-p stream) nil)
261 dcrosher 1.26 (setf (slot-value stream 'output-buffer) "")
262 dcrosher 1.1 t))
263    
264     ;;; No 'stream-clear-input method.
265    
266     (defmethod ext:stream-finish-output ((stream slime-output-stream))
267 dcrosher 1.26 (let ((buffer-tail (slot-value stream 'buffer-tail)))
268     (declare (type kernel:index buffer-tail))
269     (when (> buffer-tail 0)
270     (let ((output-fn (slot-value stream 'output-fn))
271     (output-buffer (slot-value stream 'output-buffer)))
272     (declare (function output-fn)
273     (simple-string output-buffer))
274     (funcall output-fn (subseq output-buffer 0 buffer-tail))
275     (setf (slot-value stream 'buffer-tail) 0))
276     (setf (slot-value stream 'last-write) (get-internal-real-time))))
277 dcrosher 1.1 nil)
278    
279     (defmethod ext:stream-force-output ((stream slime-output-stream))
280 dcrosher 1.26 (ext:stream-finish-output stream)
281 dcrosher 1.1 nil)
282    
283     (defmethod ext:stream-clear-output ((stream slime-output-stream))
284 dcrosher 1.26 (decf (slot-value stream 'position) (slot-value stream 'buffer-tail))
285     (setf (slot-value stream 'buffer-tail) 0)
286 dcrosher 1.1 nil)
287    
288     ;;; Use default 'stream-element-type method for 'character-stream which
289     ;;; returns 'base-char.
290    
291     (defmethod ext:stream-line-length ((stream slime-output-stream))
292     80)
293    
294     (defmethod ext:stream-line-column ((stream slime-output-stream))
295     (slot-value stream 'column))
296    
297     (defmethod file-length ((stream slime-output-stream))
298     (slot-value stream 'position))
299    
300     (defmethod ext:stream-file-position ((stream slime-output-stream)
301     &optional position)
302     (declare (optimize (speed 3)))
303     (cond (position
304     (let* ((current-position (slot-value stream 'position))
305     (target-position (etypecase position
306     ((member :start) 0)
307     ((member :end) current-position)
308     (kernel:index position))))
309     (declare (type kernel:index current-position target-position))
310     (cond ((= target-position current-position)
311     t)
312     ((> target-position current-position)
313 dcrosher 1.26 (ext:stream-finish-output stream)
314 dcrosher 1.1 (let ((output-fn (slot-value stream 'output-fn))
315     (fill-size (- target-position current-position)))
316     (declare (function output-fn))
317     (funcall output-fn (make-string fill-size
318     :initial-element #\space))
319     (setf (slot-value stream 'position) target-position))
320 heller 1.40 (setf (slot-value stream 'last-write)
321     (get-internal-real-time))
322 dcrosher 1.1 t)
323     (t
324     nil))))
325     (t
326     (slot-value stream 'position))))
327    
328     (defmethod interactive-stream-p ((stream slime-output-stream))
329     (slot-value stream 'interactive))
330    
331     ;;; Use the default 'character-output-stream 'file-string-length method.
332    
333 dcrosher 1.26 ;;; stream-write-char -- internal
334     ;;;
335     (defmethod ext:stream-write-char ((stream slime-output-stream) character)
336     (declare (type character character)
337     (optimize (speed 3)))
338     (unless (ext:stream-open-p stream)
339     (error 'kernel:simple-stream-error
340     :stream stream
341     :format-control "Stream closed."))
342     ;;
343     ;; Fill the output buffer.
344     (let* ((buffer-tail (slot-value stream 'buffer-tail))
345     (output-buffer (slot-value stream 'output-buffer))
346     (buffer-length (length output-buffer)))
347     (declare (type kernel:index buffer-tail)
348     (simple-string output-buffer))
349     (when (>= buffer-tail buffer-length)
350     ;; Flush the output buffer to make room.
351     (let ((output-fn (slot-value stream 'output-fn)))
352     (declare (function output-fn))
353     (funcall output-fn output-buffer)
354     (setf buffer-tail 0)
355     (setf (slot-value stream 'last-write) (get-internal-real-time))))
356     (setf (aref output-buffer buffer-tail) character)
357     (incf buffer-tail)
358     (setf (slot-value stream 'buffer-tail) buffer-tail)
359     ;;
360     (let ((newline (char= character #\newline)))
361     (when (or newline
362     (let ((last-write (slot-value stream 'last-write)))
363     (declare (type integer last-write))
364     (> (get-internal-real-time)
365     (+ last-write (* 5 internal-time-units-per-second)))))
366     ;; Flush the output buffer.
367     (let ((output-fn (slot-value stream 'output-fn)))
368     (declare (function output-fn))
369     (funcall output-fn (subseq output-buffer 0 buffer-tail))
370     (setf buffer-tail 0)
371     (setf (slot-value stream 'buffer-tail) buffer-tail)
372     (setf (slot-value stream 'last-write) (get-internal-real-time))))
373     ;;
374     (setf (slot-value stream 'column)
375     (if newline
376     0
377     (let ((line-column (slot-value stream 'column)))
378     (declare (type kernel:index line-column))
379     (+ line-column 1))))
380     (incf (slot-value stream 'position))
381     ))
382     character)
383    
384 dcrosher 1.1 ;;; stream-write-chars
385     ;;;
386 dcrosher 1.8 (defmethod ext:stream-write-chars ((stream slime-output-stream)
387     string start end waitp)
388 dcrosher 1.1 (declare (simple-string string)
389 dcrosher 1.8 (type kernel:index start end)
390     (ignore waitp))
391 dcrosher 1.1 (declare (optimize (speed 3)))
392     (unless (ext:stream-open-p stream)
393     (error 'kernel:simple-stream-error
394     :stream stream
395     :format-control "Stream closed."))
396     (let* ((string-length (length string))
397     (start (cond ((< start 0) 0)
398     ((> start string-length) string-length)
399     (t start)))
400     (end (cond ((< end start) start)
401     ((> end string-length) string-length)
402     (t end)))
403     (length (- end start))
404     (output-fn (slot-value stream 'output-fn)))
405     (declare (type kernel:index start end length)
406     (type function output-fn))
407     (unless (zerop length)
408     (funcall output-fn (subseq string start end))
409     (let ((last-newline (position #\newline string :from-end t
410     :start start :end end)))
411     (setf (slot-value stream 'column)
412     (if last-newline
413     (- end last-newline 1)
414     (let ((column (slot-value stream 'column)))
415     (declare (type kernel:index column))
416 dcrosher 1.26 (+ column (- end start))))))
417     (incf (slot-value stream 'position) length)))
418 dcrosher 1.8 (- end start))
419 dcrosher 1.1
420     ;;;
421    
422 heller 1.23 (defimplementation make-output-stream (output-fn)
423     (make-slime-output-stream output-fn))
424    
425     (defimplementation make-input-stream (input-fn)
426     (make-slime-input-stream input-fn))
427 dcrosher 1.1
428    
429     ;;;; Compilation Commands
430    
431     (defvar *previous-compiler-condition* nil
432     "Used to detect duplicates.")
433    
434     (defvar *previous-context* nil
435     "Previous compiler error context.")
436    
437     (defvar *buffer-name* nil
438     "The name of the Emacs buffer we are compiling from.
439     Nil if we aren't compiling from a buffer.")
440    
441     (defvar *buffer-start-position* nil)
442     (defvar *buffer-substring* nil)
443    
444     (defimplementation call-with-compilation-hooks (function)
445     (let ((*previous-compiler-condition* nil)
446     (*previous-context* nil)
447     (*print-readably* nil))
448     (handler-bind ((c::compiler-error #'handle-notification-condition)
449     (c::style-warning #'handle-notification-condition)
450     (c::warning #'handle-notification-condition))
451     (funcall function))))
452    
453 heller 1.32 (defimplementation swank-compile-file (input-file output-file
454 sboukarev 1.36 load-p external-format
455     &key policy)
456     (declare (ignore policy))
457 heller 1.12 (with-compilation-hooks ()
458     (let ((*buffer-name* nil)
459     (ext:*ignore-extra-close-parentheses* nil))
460     (multiple-value-bind (output-file warnings-p failure-p)
461 heller 1.32 (compile-file input-file
462     :output-file output-file
463     :external-format external-format)
464 heller 1.28 (values output-file warnings-p
465     (or failure-p
466     (when load-p
467     ;; Cache the latest source file for definition-finding.
468 heller 1.32 (source-cache-get input-file
469     (file-write-date input-file))
470 heller 1.28 (not (load output-file)))))))))
471 dcrosher 1.1
472 heller 1.31 (defimplementation swank-compile-string (string &key buffer position filename
473 trittweiler 1.30 policy)
474 heller 1.31 (declare (ignore filename policy))
475 dcrosher 1.1 (with-compilation-hooks ()
476     (let ((*buffer-name* buffer)
477     (*buffer-start-position* position)
478     (*buffer-substring* string))
479     (with-input-from-string (stream string)
480     (ext:compile-from-stream
481     stream
482     :source-info `(:emacs-buffer ,buffer
483     :emacs-buffer-offset ,position
484     :emacs-buffer-string ,string))))))
485    
486    
487     ;;;;; Trapping notes
488     ;;;
489     ;;; We intercept conditions from the compiler and resignal them as
490     ;;; `swank:compiler-condition's.
491    
492     (defun handle-notification-condition (condition)
493     "Handle a condition caused by a compiler warning."
494     (unless (eq condition *previous-compiler-condition*)
495     (let ((context (c::find-error-context nil)))
496     (setq *previous-compiler-condition* condition)
497     (setq *previous-context* context)
498     (signal-compiler-condition condition context))))
499    
500     (defun signal-compiler-condition (condition context)
501 sboukarev 1.42 (signal 'compiler-condition
502     :original-condition condition
503     :severity (severity-for-emacs condition)
504     :message (brief-compiler-message-for-emacs condition)
505     :source-context (compiler-error-context context)
506     :location (if (read-error-p condition)
507     (read-error-location condition)
508     (compiler-note-location context))))
509 dcrosher 1.1
510     (defun severity-for-emacs (condition)
511     "Return the severity of 'condition."
512     (etypecase condition
513     ((satisfies read-error-p) :read-error)
514     (c::compiler-error :error)
515     (c::style-warning :note)
516     (c::warning :warning)))
517    
518     (defun read-error-p (condition)
519     (eq (type-of condition) 'c::compiler-read-error))
520    
521     (defun brief-compiler-message-for-emacs (condition)
522     "Briefly describe a compiler error for Emacs.
523     When Emacs presents the message it already has the source popped up
524     and the source form highlighted. This makes much of the information in
525     the error-context redundant."
526     (princ-to-string condition))
527    
528 heller 1.34 (defun compiler-error-context (error-context)
529 dcrosher 1.1 "Describe a compiler error for Emacs including context information."
530     (declare (type (or c::compiler-error-context null) error-context))
531     (multiple-value-bind (enclosing source)
532     (if error-context
533     (values (c::compiler-error-context-enclosing-source error-context)
534     (c::compiler-error-context-source error-context)))
535 heller 1.34 (if (and enclosing source)
536     (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]"
537     enclosing source))))
538 dcrosher 1.1
539     (defun read-error-location (condition)
540     (let* ((finfo (car (c::source-info-current-file c::*source-info*)))
541     (file (c::file-info-name finfo))
542     (pos (c::compiler-read-error-position condition)))
543     (cond ((and (eq file :stream) *buffer-name*)
544     (make-location (list :buffer *buffer-name*)
545 heller 1.25 (list :offset *buffer-start-position* pos)))
546 dcrosher 1.1 ((and (pathnamep file) (not *buffer-name*))
547     (make-location (list :file (unix-truename file))
548     (list :position (1+ pos))))
549     (t (break)))))
550    
551     (defun compiler-note-location (context)
552     "Derive the location of a complier message from its context.
553     Return a `location' record, or (:error <reason>) on failure."
554     (if (null context)
555     (note-error-location)
556     (let ((file (c::compiler-error-context-file-name context))
557     (source (c::compiler-error-context-original-source context))
558     (path
559 heller 1.40 (reverse
560     (c::compiler-error-context-original-source-path context))))
561 dcrosher 1.1 (or (locate-compiler-note file source path)
562     (note-error-location)))))
563    
564     (defun note-error-location ()
565     "Pseudo-location for notes that can't be located."
566     (list :error "No error location available."))
567    
568     (defun locate-compiler-note (file source source-path)
569     (cond ((and (eq file :stream) *buffer-name*)
570     ;; Compiling from a buffer
571 heller 1.25 (make-location (list :buffer *buffer-name*)
572     (list :offset *buffer-start-position*
573     (source-path-string-position
574     source-path *buffer-substring*))))
575 dcrosher 1.1 ((and (pathnamep file) (null *buffer-name*))
576     ;; Compiling from a file
577     (make-location (list :file (unix-truename file))
578 heller 1.25 (list :position (1+ (source-path-file-position
579     source-path file)))))
580 dcrosher 1.1 ((and (eq file :lisp) (stringp source))
581     ;; No location known, but we have the source form.
582     ;; XXX How is this case triggered? -luke (16/May/2004)
583     ;; This can happen if the compiler needs to expand a macro
584     ;; but the macro-expander is not yet compiled. Calling the
585     ;; (interpreted) macro-expander triggers IR1 conversion of
586     ;; the lambda expression for the expander and invokes the
587     ;; compiler recursively.
588     (make-location (list :source-form source)
589     (list :position 1)))))
590    
591     (defun unix-truename (pathname)
592     (ext:unix-namestring (truename pathname)))
593    
594    
595    
596     ;;; TODO
597     (defimplementation who-calls (name) nil)
598     (defimplementation who-references (name) nil)
599     (defimplementation who-binds (name) nil)
600     (defimplementation who-sets (name) nil)
601     (defimplementation who-specializes (symbol) nil)
602     (defimplementation who-macroexpands (name) nil)
603    
604    
605     ;;;; Find callers and callees
606     ;;;
607     ;;; Find callers and callees by looking at the constant pool of
608     ;;; compiled code objects. We assume every fdefn object in the
609     ;;; constant pool corresponds to a call to that function. A better
610     ;;; strategy would be to use the disassembler to find actual
611     ;;; call-sites.
612    
613     (declaim (inline map-code-constants))
614     (defun map-code-constants (code fn)
615     "Call 'fn for each constant in 'code's constant pool."
616     (check-type code kernel:code-component)
617     (loop for i from vm:code-constants-offset below (kernel:get-header-data code)
618     do (funcall fn (kernel:code-header-ref code i))))
619    
620     (defun function-callees (function)
621     "Return 'function's callees as a list of functions."
622     (let ((callees '()))
623     (map-code-constants
624     (vm::find-code-object function)
625     (lambda (obj)
626     (when (kernel:fdefn-p obj)
627     (push (kernel:fdefn-function obj) callees))))
628     callees))
629    
630     (declaim (ext:maybe-inline map-allocated-code-components))
631     (defun map-allocated-code-components (spaces fn)
632     "Call FN for each allocated code component in one of 'spaces. FN
633     receives the object as argument. 'spaces should be a list of the
634     symbols :dynamic, :static, or :read-only."
635     (dolist (space spaces)
636     (declare (inline vm::map-allocated-objects)
637     (optimize (ext:inhibit-warnings 3)))
638     (vm::map-allocated-objects
639     (lambda (obj header size)
640     (declare (type fixnum size) (ignore size))
641     (when (= vm:code-header-type header)
642     (funcall fn obj)))
643     space)))
644    
645     (declaim (ext:maybe-inline map-caller-code-components))
646     (defun map-caller-code-components (function spaces fn)
647     "Call 'fn for each code component with a fdefn for 'function in its
648     constant pool."
649     (let ((function (coerce function 'function)))
650     (declare (inline map-allocated-code-components))
651     (map-allocated-code-components
652     spaces
653     (lambda (obj)
654     (map-code-constants
655     obj
656     (lambda (constant)
657     (when (and (kernel:fdefn-p constant)
658     (eq (kernel:fdefn-function constant)
659     function))
660     (funcall fn obj))))))))
661    
662     (defun function-callers (function &optional (spaces '(:read-only :static
663     :dynamic)))
664     "Return 'function's callers. The result is a list of code-objects."
665     (let ((referrers '()))
666     (declare (inline map-caller-code-components))
667     (map-caller-code-components function spaces
668     (lambda (code) (push code referrers)))
669     referrers))
670    
671     (defun debug-info-definitions (debug-info)
672     "Return the defintions for a debug-info. This should only be used
673     for code-object without entry points, i.e., byte compiled
674     code (are theree others?)"
675     ;; This mess has only been tested with #'ext::skip-whitespace, a
676     ;; byte-compiled caller of #'read-char .
677     (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))
678     (let ((name (c::debug-info-name debug-info))
679     (source (c::debug-info-source debug-info)))
680     (destructuring-bind (first) source
681     (ecase (c::debug-source-from first)
682     (:file
683     (list (list name
684     (make-location
685     (list :file (unix-truename (c::debug-source-name first)))
686     (list :function-name (string name))))))))))
687    
688     (defun valid-function-name-p (name)
689     (or (symbolp name) (and (consp name)
690     (eq (car name) 'setf)
691     (symbolp (cadr name))
692     (not (cddr name)))))
693    
694     (defun code-component-entry-points (code)
695     "Return a list ((name location) ...) of function definitons for
696     the code omponent 'code."
697     (let ((names '()))
698     (do ((f (kernel:%code-entry-points code) (kernel::%function-next f)))
699     ((not f))
700     (let ((name (kernel:%function-name f)))
701     (when (valid-function-name-p name)
702     (push (list name (function-location f)) names))))
703     names))
704    
705     (defimplementation list-callers (symbol)
706     "Return a list ((name location) ...) of callers."
707     (let ((components (function-callers symbol))
708     (xrefs '()))
709     (dolist (code components)
710     (let* ((entry (kernel:%code-entry-points code))
711     (defs (if entry
712     (code-component-entry-points code)
713     ;; byte compiled stuff
714     (debug-info-definitions
715     (kernel:%code-debug-info code)))))
716     (setq xrefs (nconc defs xrefs))))
717     xrefs))
718    
719     (defimplementation list-callees (symbol)
720     (let ((fns (function-callees symbol)))
721     (mapcar (lambda (fn)
722     (list (kernel:%function-name fn)
723     (function-location fn)))
724     fns)))
725    
726    
727     ;;;; Resolving source locations
728     ;;;
729     ;;; Our mission here is to "resolve" references to code locations into
730     ;;; actual file/buffer names and character positions. The references
731     ;;; we work from come out of the compiler's statically-generated debug
732     ;;; information, such as `code-location''s and `debug-source''s. For
733     ;;; more details, see the "Debugger Programmer's Interface" section of
734     ;;; the SCL manual.
735     ;;;
736     ;;; The first step is usually to find the corresponding "source-path"
737     ;;; for the location. Once we have the source-path we can pull up the
738     ;;; source file and `READ' our way through to the right position. The
739     ;;; main source-code groveling work is done in
740     ;;; `swank-source-path-parser.lisp'.
741    
742     (defvar *debug-definition-finding* nil
743     "When true don't handle errors while looking for definitions.
744     This is useful when debugging the definition-finding code.")
745    
746     (defvar *source-snippet-size* 256
747     "Maximum number of characters in a snippet of source code.
748     Snippets at the beginning of definitions are used to tell Emacs what
749     the definitions looks like, so that it can accurately find them by
750     text search.")
751    
752     (defmacro safe-definition-finding (&body body)
753     "Execute 'body and return the source-location it returns.
754     If an error occurs and `*debug-definition-finding*' is false, then
755     return an error pseudo-location.
756    
757     The second return value is 'nil if no error occurs, otherwise it is the
758     condition object."
759     `(flet ((body () ,@body))
760     (if *debug-definition-finding*
761     (body)
762     (handler-case (values (progn ,@body) nil)
763     (error (c) (values (list :error (princ-to-string c)) c))))))
764    
765     (defun code-location-source-location (code-location)
766     "Safe wrapper around `code-location-from-source-location'."
767     (safe-definition-finding
768     (source-location-from-code-location code-location)))
769    
770     (defun source-location-from-code-location (code-location)
771     "Return the source location for 'code-location."
772     (let ((debug-fun (di:code-location-debug-function code-location)))
773     (when (di::bogus-debug-function-p debug-fun)
774     ;; Those lousy cheapskates! They've put in a bogus debug source
775     ;; because the code was compiled at a low debug setting.
776     (error "Bogus debug function: ~A" debug-fun)))
777     (let* ((debug-source (di:code-location-debug-source code-location))
778     (from (di:debug-source-from debug-source))
779     (name (di:debug-source-name debug-source)))
780     (ecase from
781     (:file
782     (location-in-file name code-location debug-source))
783     (:stream
784     (location-in-stream code-location debug-source))
785     (:lisp
786     ;; The location comes from a form passed to `compile'.
787     ;; The best we can do is return the form itself for printing.
788     (make-location
789     (list :source-form (with-output-to-string (*standard-output*)
790     (debug::print-code-location-source-form
791     code-location 100 t)))
792     (list :position 1))))))
793    
794     (defun location-in-file (filename code-location debug-source)
795     "Resolve the source location for 'code-location in 'filename."
796     (let* ((code-date (di:debug-source-created debug-source))
797     (source-code (get-source-code filename code-date)))
798     (with-input-from-string (s source-code)
799     (make-location (list :file (unix-truename filename))
800     (list :position (1+ (code-location-stream-position
801 heller 1.25 code-location s)))
802 dcrosher 1.1 `(:snippet ,(read-snippet s))))))
803    
804     (defun location-in-stream (code-location debug-source)
805     "Resolve the source location for a 'code-location from a stream.
806     This only succeeds if the code was compiled from an Emacs buffer."
807     (unless (debug-source-info-from-emacs-buffer-p debug-source)
808     (error "The code is compiled from a non-SLIME stream."))
809     (let* ((info (c::debug-source-info debug-source))
810     (string (getf info :emacs-buffer-string))
811     (position (code-location-string-offset
812     code-location
813     string)))
814     (make-location
815     (list :buffer (getf info :emacs-buffer))
816 heller 1.25 (list :offset (getf info :emacs-buffer-offset) position)
817 dcrosher 1.1 (list :snippet (with-input-from-string (s string)
818     (file-position s position)
819     (read-snippet s))))))
820    
821     ;;;;; Function-name locations
822     ;;;
823     (defun debug-info-function-name-location (debug-info)
824     "Return a function-name source-location for 'debug-info.
825     Function-name source-locations are a fallback for when precise
826     positions aren't available."
827     (with-struct (c::debug-info- (fname name) source) debug-info
828     (with-struct (c::debug-source- info from name) (car source)
829     (ecase from
830     (:file
831     (make-location (list :file (namestring (truename name)))
832     (list :function-name (string fname))))
833     (:stream
834     (assert (debug-source-info-from-emacs-buffer-p (car source)))
835     (make-location (list :buffer (getf info :emacs-buffer))
836     (list :function-name (string fname))))
837     (:lisp
838     (make-location (list :source-form (princ-to-string (aref name 0)))
839     (list :position 1)))))))
840    
841     (defun debug-source-info-from-emacs-buffer-p (debug-source)
842     "Does the `info' slot of 'debug-source contain an Emacs buffer location?
843     This is true for functions that were compiled directly from buffers."
844     (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
845    
846     (defun info-from-emacs-buffer-p (info)
847     (and info
848     (consp info)
849     (eq :emacs-buffer (car info))))
850    
851    
852     ;;;;; Groveling source-code for positions
853    
854     (defun code-location-stream-position (code-location stream)
855     "Return the byte offset of 'code-location in 'stream. Extract the
856     toplevel-form-number and form-number from 'code-location and use that
857     to find the position of the corresponding form.
858    
859     Finish with 'stream positioned at the start of the code location."
860     (let* ((location (debug::maybe-block-start-location code-location))
861     (tlf-offset (di:code-location-top-level-form-offset location))
862     (form-number (di:code-location-form-number location)))
863     (let ((pos (form-number-stream-position tlf-offset form-number stream)))
864     (file-position stream pos)
865     pos)))
866    
867     (defun form-number-stream-position (tlf-number form-number stream)
868     "Return the starting character position of a form in 'stream.
869     'tlf-number is the top-level-form number.
870     'form-number is an index into a source-path table for the TLF."
871     (multiple-value-bind (tlf position-map) (read-source-form tlf-number stream)
872     (let* ((path-table (di:form-number-translations tlf 0))
873     (source-path
874     (if (<= (length path-table) form-number) ; source out of sync?
875     (list 0) ; should probably signal a condition
876     (reverse (cdr (aref path-table form-number))))))
877     (source-path-source-position source-path tlf position-map))))
878    
879     (defun code-location-string-offset (code-location string)
880     "Return the byte offset of 'code-location in 'string.
881     See 'code-location-stream-position."
882     (with-input-from-string (s string)
883     (code-location-stream-position code-location s)))
884    
885    
886     ;;;; Finding definitions
887    
888     ;;; There are a great many different types of definition for us to
889     ;;; find. We search for definitions of every kind and return them in a
890     ;;; list.
891    
892     (defimplementation find-definitions (name)
893     (append (function-definitions name)
894     (setf-definitions name)
895     (variable-definitions name)
896     (class-definitions name)
897     (type-definitions name)
898     (compiler-macro-definitions name)
899     (source-transform-definitions name)
900     (function-info-definitions name)
901     (ir1-translator-definitions name)))
902    
903     ;;;;; Functions, macros, generic functions, methods
904     ;;;
905     ;;; We make extensive use of the compile-time debug information that
906     ;;; SCL records, in particular "debug functions" and "code
907     ;;; locations." Refer to the "Debugger Programmer's Interface" section
908     ;;; of the SCL manual for more details.
909    
910     (defun function-definitions (name)
911     "Return definitions for 'name in the \"function namespace\", i.e.,
912     regular functions, generic functions, methods and macros.
913     'name can any valid function name (e.g, (setf car))."
914     (let ((macro? (and (symbolp name) (macro-function name)))
915     (special? (and (symbolp name) (special-operator-p name)))
916     (function? (and (valid-function-name-p name)
917     (ext:info :function :definition name)
918     (if (symbolp name) (fboundp name) t))))
919     (cond (macro?
920     (list `((defmacro ,name)
921     ,(function-location (macro-function name)))))
922     (special?
923     (list `((:special-operator ,name)
924     (:error ,(format nil "Special operator: ~S" name)))))
925     (function?
926     (let ((function (fdefinition name)))
927     (if (genericp function)
928     (generic-function-definitions name function)
929     (list (list `(function ,name)
930     (function-location function)))))))))
931    
932     ;;;;;; Ordinary (non-generic/macro/special) functions
933     ;;;
934     ;;; First we test if FUNCTION is a closure created by defstruct, and
935     ;;; if so extract the defstruct-description (`dd') from the closure
936     ;;; and find the constructor for the struct. Defstruct creates a
937     ;;; defun for the default constructor and we use that as an
938     ;;; approximation to the source location of the defstruct.
939     ;;;
940     ;;; For an ordinary function we return the source location of the
941     ;;; first code-location we find.
942     ;;;
943     (defun function-location (function)
944     "Return the source location for FUNCTION."
945     (cond ((struct-closure-p function)
946     (struct-closure-location function))
947     ((c::byte-function-or-closure-p function)
948     (byte-function-location function))
949     (t
950     (compiled-function-location function))))
951    
952     (defun compiled-function-location (function)
953     "Return the location of a regular compiled function."
954     (multiple-value-bind (code-location error)
955     (safe-definition-finding (function-first-code-location function))
956     (cond (error (list :error (princ-to-string error)))
957     (t (code-location-source-location code-location)))))
958    
959     (defun function-first-code-location (function)
960     "Return the first code-location we can find for 'function."
961     (and (function-has-debug-function-p function)
962     (di:debug-function-start-location
963     (di:function-debug-function function))))
964    
965     (defun function-has-debug-function-p (function)
966     (di:function-debug-function function))
967    
968     (defun function-code-object= (closure function)
969     (and (eq (vm::find-code-object closure)
970     (vm::find-code-object function))
971     (not (eq closure function))))
972    
973    
974     (defun byte-function-location (fn)
975     "Return the location of the byte-compiled function 'fn."
976     (etypecase fn
977     ((or c::hairy-byte-function c::simple-byte-function)
978     (let* ((component (c::byte-function-component fn))
979     (debug-info (kernel:%code-debug-info component)))
980     (debug-info-function-name-location debug-info)))
981     (c::byte-closure
982     (byte-function-location (c::byte-closure-function fn)))))
983    
984     ;;; Here we deal with structure accessors. Note that `dd' is a
985     ;;; "defstruct descriptor" structure in SCL. A `dd' describes a
986     ;;; `defstruct''d structure.
987    
988     (defun struct-closure-p (function)
989     "Is 'function a closure created by defstruct?"
990     (or (function-code-object= function #'kernel::structure-slot-accessor)
991     (function-code-object= function #'kernel::structure-slot-setter)
992     (function-code-object= function #'kernel::%defstruct)))
993    
994     (defun struct-closure-location (function)
995     "Return the location of the structure that 'function belongs to."
996     (assert (struct-closure-p function))
997     (safe-definition-finding
998     (dd-location (struct-closure-dd function))))
999    
1000     (defun struct-closure-dd (function)
1001     "Return the defstruct-definition (dd) of FUNCTION."
1002     (assert (= (kernel:get-type function) vm:closure-header-type))
1003     (flet ((find-layout (function)
1004     (sys:find-if-in-closure
1005     (lambda (x)
1006     (let ((value (if (di::indirect-value-cell-p x)
1007     (c:value-cell-ref x)
1008     x)))
1009     (when (kernel::layout-p value)
1010     (return-from find-layout value))))
1011     function)))
1012     (kernel:layout-info (find-layout function))))
1013    
1014     (defun dd-location (dd)
1015     "Return the location of a `defstruct'."
1016     ;; Find the location in a constructor.
1017     (function-location (struct-constructor dd)))
1018    
1019     (defun struct-constructor (dd)
1020     "Return a constructor function from a defstruct definition.
1021     Signal an error if no constructor can be found."
1022     (let ((constructor (or (kernel:dd-default-constructor dd)
1023     (car (kernel::dd-constructors dd)))))
1024     (when (or (null constructor)
1025     (and (consp constructor) (null (car constructor))))
1026     (error "Cannot find structure's constructor: ~S"
1027     (kernel::dd-name dd)))
1028     (coerce (if (consp constructor) (first constructor) constructor)
1029     'function)))
1030    
1031     ;;;;;; Generic functions and methods
1032    
1033     (defun generic-function-definitions (name function)
1034     "Return the definitions of a generic function and its methods."
1035     (cons (list `(defgeneric ,name) (gf-location function))
1036     (gf-method-definitions function)))
1037    
1038     (defun gf-location (gf)
1039     "Return the location of the generic function GF."
1040     (definition-source-location gf (clos:generic-function-name gf)))
1041    
1042     (defun gf-method-definitions (gf)
1043     "Return the locations of all methods of the generic function GF."
1044     (mapcar #'method-definition (clos:generic-function-methods gf)))
1045    
1046     (defun method-definition (method)
1047     (list (method-dspec method)
1048     (method-location method)))
1049    
1050     (defun method-dspec (method)
1051     "Return a human-readable \"definition specifier\" for METHOD."
1052     (let* ((gf (clos:method-generic-function method))
1053     (name (clos:generic-function-name gf))
1054     (specializers (clos:method-specializers method))
1055     (qualifiers (clos:method-qualifiers method)))
1056 heller 1.40 `(method ,name ,@qualifiers ,specializers
1057     #+nil (clos::unparse-specializers specializers))))
1058 dcrosher 1.1
1059     ;; XXX maybe special case setters/getters
1060     (defun method-location (method)
1061     (function-location (clos:method-function method)))
1062    
1063     (defun genericp (fn)
1064     (typep fn 'generic-function))
1065    
1066     ;;;;;; Types and classes
1067    
1068     (defun type-definitions (name)
1069     "Return `deftype' locations for type NAME."
1070     (maybe-make-definition (ext:info :type :expander name) 'deftype name))
1071    
1072     (defun maybe-make-definition (function kind name)
1073     "If FUNCTION is non-nil then return its definition location."
1074     (if function
1075     (list (list `(,kind ,name) (function-location function)))))
1076    
1077     (defun class-definitions (name)
1078     "Return the definition locations for the class called NAME."
1079     (if (symbolp name)
1080     (let ((class (find-class name nil)))
1081     (etypecase class
1082     (null '())
1083     (structure-class
1084     (list (list `(defstruct ,name)
1085     (dd-location (find-dd name)))))
1086     (standard-class
1087     (list (list `(defclass ,name)
1088     (class-location (find-class name)))))
1089     ((or built-in-class
1090     kernel:funcallable-structure-class)
1091     (list (list `(kernel::define-type-class ,name)
1092     `(:error
1093     ,(format nil "No source info for ~A" name)))))))))
1094    
1095     (defun class-location (class)
1096     "Return the `defclass' location for CLASS."
1097     (definition-source-location class (class-name class)))
1098    
1099     (defun find-dd (name)
1100     "Find the defstruct-definition by the name of its structure-class."
1101     (let ((layout (ext:info :type :compiler-layout name)))
1102     (if layout
1103     (kernel:layout-info layout))))
1104    
1105     (defun condition-class-location (class)
1106     (let ((name (class-name class)))
1107     `(:error ,(format nil "No location info for condition: ~A" name))))
1108    
1109     (defun make-name-in-file-location (file string)
1110     (multiple-value-bind (filename c)
1111     (ignore-errors
1112     (unix-truename (merge-pathnames (make-pathname :type "lisp")
1113     file)))
1114     (cond (filename (make-location `(:file ,filename)
1115     `(:function-name ,(string string))))
1116     (t (list :error (princ-to-string c))))))
1117    
1118     (defun definition-source-location (object name)
1119     `(:error ,(format nil "No source info for: ~A" object)))
1120    
1121     (defun setf-definitions (name)
1122     (let ((function (or (ext:info :setf :inverse name)
1123     (ext:info :setf :expander name))))
1124     (if function
1125     (list (list `(setf ,name)
1126     (function-location (coerce function 'function)))))))
1127    
1128    
1129     (defun variable-location (symbol)
1130     `(:error ,(format nil "No source info for variable ~S" symbol)))
1131    
1132     (defun variable-definitions (name)
1133     (if (symbolp name)
1134     (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1135     (if recorded-p
1136     (list (list `(variable ,kind ,name)
1137     (variable-location name)))))))
1138    
1139     (defun compiler-macro-definitions (symbol)
1140     (maybe-make-definition (compiler-macro-function symbol)
1141     'define-compiler-macro
1142     symbol))
1143    
1144     (defun source-transform-definitions (name)
1145     (maybe-make-definition (ext:info :function :source-transform name)
1146     'c:def-source-transform
1147     name))
1148    
1149     (defun function-info-definitions (name)
1150     (let ((info (ext:info :function :info name)))
1151     (if info
1152     (append (loop for transform in (c::function-info-transforms info)
1153     collect (list `(c:deftransform ,name
1154     ,(c::type-specifier
1155     (c::transform-type transform)))
1156     (function-location (c::transform-function
1157     transform))))
1158     (maybe-make-definition (c::function-info-derive-type info)
1159     'c::derive-type name)
1160     (maybe-make-definition (c::function-info-optimizer info)
1161     'c::optimizer name)
1162     (maybe-make-definition (c::function-info-ltn-annotate info)
1163     'c::ltn-annotate name)
1164     (maybe-make-definition (c::function-info-ir2-convert info)
1165     'c::ir2-convert name)
1166     (loop for template in (c::function-info-templates info)
1167     collect (list `(c::vop ,(c::template-name template))
1168     (function-location
1169     (c::vop-info-generator-function
1170     template))))))))
1171    
1172     (defun ir1-translator-definitions (name)
1173     (maybe-make-definition (ext:info :function :ir1-convert name)
1174     'c:def-ir1-translator name))
1175    
1176    
1177     ;;;; Documentation.
1178    
1179     (defimplementation describe-symbol-for-emacs (symbol)
1180     (let ((result '()))
1181     (flet ((doc (kind)
1182     (or (documentation symbol kind) :not-documented))
1183     (maybe-push (property value)
1184     (when value
1185     (setf result (list* property value result)))))
1186     (maybe-push
1187     :variable (multiple-value-bind (kind recorded-p)
1188     (ext:info variable kind symbol)
1189     (declare (ignore kind))
1190     (if (or (boundp symbol) recorded-p)
1191     (doc 'variable))))
1192     (when (fboundp symbol)
1193     (maybe-push
1194     (cond ((macro-function symbol) :macro)
1195     ((special-operator-p symbol) :special-operator)
1196     ((genericp (fdefinition symbol)) :generic-function)
1197     (t :function))
1198     (doc 'function)))
1199     (maybe-push
1200     :setf (if (or (ext:info setf inverse symbol)
1201     (ext:info setf expander symbol))
1202     (doc 'setf)))
1203     (maybe-push
1204     :type (if (ext:info type kind symbol)
1205     (doc 'type)))
1206     (maybe-push
1207     :class (if (find-class symbol nil)
1208     (doc 'class)))
1209     (maybe-push
1210     :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
1211     (doc 'alien-type)))
1212     (maybe-push
1213     :alien-struct (if (ext:info alien-type struct symbol)
1214     (doc nil)))
1215     (maybe-push
1216     :alien-union (if (ext:info alien-type union symbol)
1217     (doc nil)))
1218     (maybe-push
1219     :alien-enum (if (ext:info alien-type enum symbol)
1220     (doc nil)))
1221     result)))
1222    
1223     (defimplementation describe-definition (symbol namespace)
1224     (describe (ecase namespace
1225     (:variable
1226     symbol)
1227     ((:function :generic-function)
1228     (symbol-function symbol))
1229     (:setf
1230     (or (ext:info setf inverse symbol)
1231     (ext:info setf expander symbol)))
1232     (:type
1233     (kernel:values-specifier-type symbol))
1234     (:class
1235     (find-class symbol))
1236     (:alien-struct
1237     (ext:info :alien-type :struct symbol))
1238     (:alien-union
1239     (ext:info :alien-type :union symbol))
1240     (:alien-enum
1241     (ext:info :alien-type :enum symbol))
1242     (:alien-type
1243     (ecase (ext:info :alien-type :kind symbol)
1244     (:primitive
1245     (let ((alien::*values-type-okay* t))
1246     (funcall (ext:info :alien-type :translator symbol)
1247     (list symbol))))
1248     ((:defined)
1249     (ext:info :alien-type :definition symbol))
1250 heller 1.10 (:unknown :unknown))))))
1251 dcrosher 1.1
1252     ;;;;; Argument lists
1253    
1254 heller 1.10 (defimplementation arglist (fun)
1255 dcrosher 1.26 (multiple-value-bind (args winp)
1256     (ext:function-arglist fun)
1257     (if winp args :not-available)))
1258 dcrosher 1.1
1259     (defimplementation function-name (function)
1260     (cond ((eval:interpreted-function-p function)
1261     (eval:interpreted-function-name function))
1262     ((typep function 'generic-function)
1263     (clos:generic-function-name function))
1264     ((c::byte-function-or-closure-p function)
1265     (c::byte-function-name function))
1266     (t (kernel:%function-name (kernel:%function-self function)))))
1267    
1268    
1269     ;;; A harder case: an approximate arglist is derived from available
1270     ;;; debugging information.
1271    
1272     (defun debug-function-arglist (debug-function)
1273     "Derive the argument list of DEBUG-FUNCTION from debug info."
1274     (let ((args (di::debug-function-lambda-list debug-function))
1275     (required '())
1276     (optional '())
1277     (rest '())
1278     (key '()))
1279     ;; collect the names of debug-vars
1280     (dolist (arg args)
1281     (etypecase arg
1282     (di::debug-variable
1283     (push (di::debug-variable-symbol arg) required))
1284     ((member :deleted)
1285     (push ':deleted required))
1286     (cons
1287     (ecase (car arg)
1288     (:keyword
1289     (push (second arg) key))
1290     (:optional
1291     (push (debug-variable-symbol-or-deleted (second arg)) optional))
1292     (:rest
1293     (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1294     ;; intersperse lambda keywords as needed
1295     (append (nreverse required)
1296     (if optional (cons '&optional (nreverse optional)))
1297     (if rest (cons '&rest (nreverse rest)))
1298     (if key (cons '&key (nreverse key))))))
1299    
1300     (defun debug-variable-symbol-or-deleted (var)
1301     (etypecase var
1302     (di:debug-variable
1303     (di::debug-variable-symbol var))
1304     ((member :deleted)
1305     '#:deleted)))
1306    
1307     (defun symbol-debug-function-arglist (fname)
1308     "Return FNAME's debug-function-arglist and %function-arglist.
1309     A utility for debugging DEBUG-FUNCTION-ARGLIST."
1310     (let ((fn (fdefinition fname)))
1311     (values (debug-function-arglist (di::function-debug-function fn))
1312     (kernel:%function-arglist (kernel:%function-self fn)))))
1313    
1314    
1315     ;;;; Miscellaneous.
1316    
1317     (defimplementation macroexpand-all (form)
1318     (macroexpand form))
1319    
1320     (defimplementation set-default-directory (directory)
1321     (setf (ext:default-directory) (namestring directory))
1322     ;; Setting *default-pathname-defaults* to an absolute directory
1323     ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1324     (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1325     (default-directory))
1326    
1327     (defimplementation default-directory ()
1328     (namestring (ext:default-directory)))
1329    
1330 heller 1.27 (defimplementation pathname-to-filename (pathname)
1331     (ext:unix-namestring pathname nil))
1332    
1333 dcrosher 1.1 (defimplementation getpid ()
1334     (unix:unix-getpid))
1335    
1336     (defimplementation lisp-implementation-type-name ()
1337     (if (eq ext:*case-mode* :upper) "scl" "scl-lower"))
1338    
1339     (defimplementation quit-lisp ()
1340     (ext:quit))
1341    
1342     ;;; source-path-{stream,file,string,etc}-position moved into
1343     ;;; swank-source-path-parser
1344    
1345    
1346     ;;;; Debugging
1347    
1348     (defvar *sldb-stack-top*)
1349    
1350     (defimplementation call-with-debugging-environment (debugger-loop-fn)
1351     (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1352     (debug:*stack-top-hint* nil)
1353     (kernel:*current-level* 0))
1354     (handler-bind ((di::unhandled-condition
1355     (lambda (condition)
1356 sboukarev 1.42 (error 'sldb-condition
1357     :original-condition condition))))
1358 dcrosher 1.1 (funcall debugger-loop-fn))))
1359    
1360     (defun frame-down (frame)
1361     (handler-case (di:frame-down frame)
1362     (di:no-debug-info () nil)))
1363    
1364     (defun nth-frame (index)
1365     (do ((frame *sldb-stack-top* (frame-down frame))
1366     (i index (1- i)))
1367     ((zerop i) frame)))
1368    
1369     (defimplementation compute-backtrace (start end)
1370     (let ((end (or end most-positive-fixnum)))
1371     (loop for f = (nth-frame start) then (frame-down f)
1372     for i from start below end
1373 heller 1.29 while f collect f)))
1374 dcrosher 1.1
1375 heller 1.29 (defimplementation print-frame (frame stream)
1376     (let ((*standard-output* stream))
1377 dcrosher 1.1 (handler-case
1378     (debug::print-frame-call frame :verbosity 1 :number nil)
1379     (error (e)
1380     (ignore-errors (princ e stream))))))
1381    
1382 heller 1.33 (defimplementation frame-source-location (index)
1383 dcrosher 1.1 (code-location-source-location (di:frame-code-location (nth-frame index))))
1384    
1385     (defimplementation eval-in-frame (form index)
1386     (di:eval-in-frame (nth-frame index) form))
1387    
1388     (defun frame-debug-vars (frame)
1389     "Return a vector of debug-variables in frame."
1390     (di::debug-function-debug-variables (di:frame-debug-function frame)))
1391    
1392     (defun debug-var-value (var frame location)
1393     (let ((validity (di:debug-variable-validity var location)))
1394     (ecase validity
1395     (:valid (di:debug-variable-value var frame))
1396     ((:invalid :unknown) (make-symbol (string validity))))))
1397    
1398     (defimplementation frame-locals (index)
1399     (let* ((frame (nth-frame index))
1400     (loc (di:frame-code-location frame))
1401     (vars (frame-debug-vars frame)))
1402     (loop for v across vars collect
1403     (list :name (di:debug-variable-symbol v)
1404     :id (di:debug-variable-id v)
1405     :value (debug-var-value v frame loc)))))
1406    
1407     (defimplementation frame-var-value (frame var)
1408     (let* ((frame (nth-frame frame))
1409     (dvar (aref (frame-debug-vars frame) var)))
1410     (debug-var-value dvar frame (di:frame-code-location frame))))
1411    
1412     (defimplementation frame-catch-tags (index)
1413     (mapcar #'car (di:frame-catches (nth-frame index))))
1414    
1415     (defimplementation return-from-frame (index form)
1416 dcrosher 1.4 (let ((sym (find-symbol (symbol-name '#:find-debug-tag-for-frame)
1417 dcrosher 1.1 :debug-internals)))
1418     (if sym
1419     (let* ((frame (nth-frame index))
1420     (probe (funcall sym frame)))
1421     (cond (probe (throw (car probe) (eval-in-frame form index)))
1422     (t (format nil "Cannot return from frame: ~S" frame))))
1423     "return-from-frame is not implemented in this version of SCL.")))
1424    
1425     (defimplementation activate-stepping (frame)
1426     (set-step-breakpoints (nth-frame frame)))
1427    
1428     (defimplementation sldb-break-on-return (frame)
1429     (break-on-return (nth-frame frame)))
1430    
1431     ;;; We set the breakpoint in the caller which might be a bit confusing.
1432     ;;;
1433     (defun break-on-return (frame)
1434     (let* ((caller (di:frame-down frame))
1435     (cl (di:frame-code-location caller)))
1436     (flet ((hook (frame bp)
1437     (when (frame-pointer= frame caller)
1438     (di:delete-breakpoint bp)
1439     (signal-breakpoint bp frame))))
1440     (let* ((info (ecase (di:code-location-kind cl)
1441     ((:single-value-return :unknown-return) nil)
1442     (:known-return (debug-function-returns
1443     (di:frame-debug-function frame)))))
1444     (bp (di:make-breakpoint #'hook cl :kind :code-location
1445     :info info)))
1446     (di:activate-breakpoint bp)
1447     `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1448    
1449     (defun frame-pointer= (frame1 frame2)
1450     "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1451     (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1452    
1453     ;;; The PC in escaped frames at a single-return-value point is
1454     ;;; actually vm:single-value-return-byte-offset bytes after the
1455     ;;; position given in the debug info. Here we try to recognize such
1456     ;;; cases.
1457     ;;;
1458     (defun next-code-locations (frame code-location)
1459     "Like `debug::next-code-locations' but be careful in escaped frames."
1460     (let ((next (debug::next-code-locations code-location)))
1461     (flet ((adjust-pc ()
1462     (let ((cl (di::copy-compiled-code-location code-location)))
1463     (incf (di::compiled-code-location-pc cl)
1464     vm:single-value-return-byte-offset)
1465     cl)))
1466     (cond ((and (di::compiled-frame-escaped frame)
1467     (eq (di:code-location-kind code-location)
1468     :single-value-return)
1469     (= (length next) 1)
1470     (di:code-location= (car next) (adjust-pc)))
1471     (debug::next-code-locations (car next)))
1472     (t
1473     next)))))
1474    
1475     (defun set-step-breakpoints (frame)
1476     (let ((cl (di:frame-code-location frame)))
1477     (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1478     (error "Cannot step in elsewhere code"))
1479     (let* ((debug::*bad-code-location-types*
1480     (remove :call-site debug::*bad-code-location-types*))
1481     (next (next-code-locations frame cl)))
1482     (cond (next
1483     (let ((steppoints '()))
1484     (flet ((hook (bp-frame bp)
1485     (signal-breakpoint bp bp-frame)
1486     (mapc #'di:delete-breakpoint steppoints)))
1487     (dolist (code-location next)
1488     (let ((bp (di:make-breakpoint #'hook code-location
1489     :kind :code-location)))
1490     (di:activate-breakpoint bp)
1491     (push bp steppoints))))))
1492     (t
1493     (break-on-return frame))))))
1494    
1495    
1496     ;; XXX the return values at return breakpoints should be passed to the
1497     ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1498    
1499     ;;; The sigcontext and the PC for a breakpoint invocation are not
1500     ;;; passed to user hook functions, but we need them to extract return
1501     ;;; values. So we advice di::handle-breakpoint and bind the values to
1502     ;;; special variables.
1503     ;;;
1504     (defvar *breakpoint-sigcontext*)
1505     (defvar *breakpoint-pc*)
1506    
1507     (defun sigcontext-object (sc index)
1508     "Extract the lisp object in sigcontext SC at offset INDEX."
1509     (kernel:make-lisp-obj (vm:ucontext-register sc index)))
1510    
1511     (defun known-return-point-values (sigcontext sc-offsets)
1512     (let ((fp (system:int-sap (vm:ucontext-register sigcontext
1513     vm::cfp-offset))))
1514     (system:without-gcing
1515     (loop for sc-offset across sc-offsets
1516     collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1517    
1518     ;;; SCL returns the first few values in registers and the rest on
1519     ;;; the stack. In the multiple value case, the number of values is
1520     ;;; stored in a dedicated register. The values of the registers can be
1521     ;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
1522     ;;; of return conventions: :single-value-return, :unknown-return, and
1523     ;;; :known-return.
1524     ;;;
1525     ;;; The :single-value-return convention returns the value in a
1526     ;;; register without setting the nargs registers.
1527     ;;;
1528     ;;; The :unknown-return variant is used for multiple values. A
1529     ;;; :unknown-return point consists actually of 2 breakpoints: one for
1530     ;;; the single value case and one for the general case. The single
1531     ;;; value breakpoint comes vm:single-value-return-byte-offset after
1532     ;;; the multiple value breakpoint.
1533     ;;;
1534     ;;; The :known-return convention is used by local functions.
1535     ;;; :known-return is currently not supported because we don't know
1536     ;;; where the values are passed.
1537     ;;;
1538     (defun breakpoint-values (breakpoint)
1539     "Return the list of return values for a return point."
1540     (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1541     (let ((sc (locally (declare (optimize (ext:inhibit-warnings 3)))
1542     (alien:sap-alien *breakpoint-sigcontext* (* unix:ucontext))))
1543     (cl (di:breakpoint-what breakpoint)))
1544     (ecase (di:code-location-kind cl)
1545     (:single-value-return
1546     (list (1st sc)))
1547     (:known-return
1548     (let ((info (di:breakpoint-info breakpoint)))
1549     (if (vectorp info)
1550     (known-return-point-values sc info)
1551     (progn
1552     ;;(break)
1553     (list "<<known-return convention not supported>>" info)))))
1554     (:unknown-return
1555     (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1556     (if (= mv-return-pc *breakpoint-pc*)
1557     (mv-function-end-breakpoint-values sc)
1558     (list (1st sc)))))))))
1559    
1560     (defun mv-function-end-breakpoint-values (sigcontext)
1561 heller 1.40 (let ((sym (find-symbol
1562     (symbol-name '#:function-end-breakpoint-values/standard)
1563     :debug-internals)))
1564 dcrosher 1.1 (cond (sym (funcall sym sigcontext))
1565     (t (di::get-function-end-breakpoint-values sigcontext)))))
1566    
1567     (defun debug-function-returns (debug-fun)
1568     "Return the return style of DEBUG-FUN."
1569     (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1570     (c::compiled-debug-function-returns cdfun)))
1571    
1572     (define-condition breakpoint (simple-condition)
1573     ((message :initarg :message :reader breakpoint.message)
1574     (values :initarg :values :reader breakpoint.values))
1575     (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1576    
1577 heller 1.10 #+nil
1578 dcrosher 1.1 (defimplementation condition-extras ((c breakpoint))
1579     ;; simply pop up the source buffer
1580     `((:short-frame-source 0)))
1581    
1582     (defun signal-breakpoint (breakpoint frame)
1583     "Signal a breakpoint condition for BREAKPOINT in FRAME.
1584     Try to create a informative message."
1585     (flet ((brk (values fstring &rest args)
1586     (let ((msg (apply #'format nil fstring args))
1587     (debug:*stack-top-hint* frame))
1588     (break 'breakpoint :message msg :values values))))
1589     (with-struct (di::breakpoint- kind what) breakpoint
1590     (case kind
1591     (:code-location
1592     (case (di:code-location-kind what)
1593     ((:single-value-return :known-return :unknown-return)
1594     (let ((values (breakpoint-values breakpoint)))
1595     (brk values "Return value: ~{~S ~}" values)))
1596     (t
1597     #+(or)
1598     (when (eq (di:code-location-kind what) :call-site)
1599     (call-site-function breakpoint frame))
1600     (brk nil "Breakpoint: ~S ~S"
1601     (di:code-location-kind what)
1602     (di::compiled-code-location-pc what)))))
1603     (:function-start
1604     (brk nil "Function start breakpoint"))
1605     (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1606    
1607     #+nil
1608     (defimplementation sldb-break-at-start (fname)
1609     (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1610     (cond ((not debug-fun)
1611     `(:error ,(format nil "~S has no debug-function" fname)))
1612     (t
1613     (flet ((hook (frame bp &optional args cookie)
1614     (declare (ignore args cookie))
1615     (signal-breakpoint bp frame)))
1616     (let ((bp (di:make-breakpoint #'hook debug-fun
1617     :kind :function-start)))
1618     (di:activate-breakpoint bp)
1619     `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1620    
1621     (defun frame-cfp (frame)
1622     "Return the Control-Stack-Frame-Pointer for FRAME."
1623     (etypecase frame
1624     (di::compiled-frame (di::frame-pointer frame))
1625     ((or di::interpreted-frame null) -1)))
1626    
1627     (defun frame-ip (frame)
1628     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1629     (if (not frame)
1630     -1
1631     (let ((debug-fun (di::frame-debug-function frame)))
1632     (etypecase debug-fun
1633     (di::compiled-debug-function
1634     (let* ((code-loc (di:frame-code-location frame))
1635     (component (di::compiled-debug-function-component debug-fun))
1636     (pc (di::compiled-code-location-pc code-loc))
1637     (ip (sys:without-gcing
1638     (sys:sap-int
1639     (sys:sap+ (kernel:code-instructions component) pc)))))
1640     (values ip pc)))
1641     ((or di::bogus-debug-function di::interpreted-debug-function)
1642     -1)))))
1643    
1644     (defun frame-registers (frame)
1645     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1646     (let* ((cfp (frame-cfp frame))
1647     (csp (frame-cfp (di::frame-up frame)))
1648     (ip (frame-ip frame))
1649     (ocfp (frame-cfp (di::frame-down frame)))
1650     (lra (frame-ip (di::frame-down frame))))
1651     (values csp cfp ip ocfp lra)))
1652    
1653     (defun print-frame-registers (frame-number)
1654     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1655     (flet ((fixnum (p) (etypecase p
1656     (integer p)
1657     (sys:system-area-pointer (sys:sap-int p)))))
1658     (apply #'format t "~
1659     CSP = ~X
1660     CFP = ~X
1661     IP = ~X
1662     OCFP = ~X
1663     LRA = ~X~%" (mapcar #'fixnum
1664     (multiple-value-list (frame-registers frame)))))))
1665    
1666    
1667     (defimplementation disassemble-frame (frame-number)
1668     "Return a string with the disassembly of frames code."
1669     (print-frame-registers frame-number)
1670     (terpri)
1671     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1672     (debug-fun (di::frame-debug-function frame)))
1673     (etypecase debug-fun
1674     (di::compiled-debug-function
1675     (let* ((component (di::compiled-debug-function-component debug-fun))
1676     (fun (di:debug-function-function debug-fun)))
1677     (if fun
1678     (disassemble fun)
1679     (disassem:disassemble-code-component component))))
1680     (di::bogus-debug-function
1681     (format t "~%[Disassembling bogus frames not implemented]")))))
1682    
1683    
1684     ;;;; Inspecting
1685    
1686     (defconstant +lowtag-symbols+
1687     '(vm:even-fixnum-type
1688     vm:instance-pointer-type
1689     vm:other-immediate-0-type
1690     vm:list-pointer-type
1691     vm:odd-fixnum-type
1692     vm:function-pointer-type
1693     vm:other-immediate-1-type
1694     vm:other-pointer-type)
1695     "Names of the constants that specify type tags.
1696     The `symbol-value' of each element is a type tag.")
1697    
1698     (defconstant +header-type-symbols+
1699     (labels ((suffixp (suffix string)
1700     (and (>= (length string) (length suffix))
1701     (string= string suffix :start1 (- (length string)
1702     (length suffix)))))
1703     (header-type-symbol-p (x)
1704     (and (suffixp (symbol-name '#:-type) (symbol-name x))
1705     (not (member x +lowtag-symbols+))
1706     (boundp x)
1707     (typep (symbol-value x) 'fixnum))))
1708     (remove-if-not #'header-type-symbol-p
1709     (append (apropos-list (symbol-name '#:-type) :vm)
1710     (apropos-list (symbol-name '#:-type) :bignum))))
1711     "A list of names of the type codes in boxed objects.")
1712    
1713     (defimplementation describe-primitive-type (object)
1714     (with-output-to-string (*standard-output*)
1715     (let* ((lowtag (kernel:get-lowtag object))
1716     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1717     (format t "lowtag: ~A" lowtag-symbol)
1718     (when (member lowtag (list vm:other-pointer-type
1719     vm:function-pointer-type
1720     vm:other-immediate-0-type
1721     vm:other-immediate-1-type
1722     ))
1723     (let* ((type (kernel:get-type object))
1724     (type-symbol (find type +header-type-symbols+
1725     :key #'symbol-value)))
1726     (format t ", type: ~A" type-symbol))))))
1727    
1728 heller 1.16 (defmethod emacs-inspect ((o t))
1729 dcrosher 1.1 (cond ((di::indirect-value-cell-p o)
1730 heller 1.17 `("Value: " (:value ,(c:value-cell-ref o))))
1731 dcrosher 1.1 ((alien::alien-value-p o)
1732     (inspect-alien-value o))
1733     (t
1734     (scl-inspect o))))
1735    
1736     (defun scl-inspect (o)
1737 dcrosher 1.4 (destructuring-bind (text labeledp . parts)
1738     (inspect::describe-parts o)
1739 heller 1.17 (list* (format nil "~A~%" text)
1740 dcrosher 1.1 (if labeledp
1741     (loop for (label . value) in parts
1742     append (label-value-line label value))
1743     (loop for value in parts for i from 0
1744     append (label-value-line i value))))))
1745    
1746 heller 1.16 (defmethod emacs-inspect ((o function))
1747 dcrosher 1.1 (let ((header (kernel:get-type o)))
1748     (cond ((= header vm:function-header-type)
1749 heller 1.17 (list* (format nil "~A is a function.~%" o)
1750 dcrosher 1.1 (append (label-value-line*
1751     ("Self" (kernel:%function-self o))
1752     ("Next" (kernel:%function-next o))
1753     ("Name" (kernel:%function-name o))
1754     ("Arglist" (kernel:%function-arglist o))
1755     ("Type" (kernel:%function-type o))
1756     ("Code" (kernel:function-code-header o)))
1757     (list
1758     (with-output-to-string (s)
1759     (disassem:disassemble-function o :stream s))))))
1760     ((= header vm:closure-header-type)
1761 heller 1.17 (list* (format nil "~A is a closure.~%" o)
1762 dcrosher 1.1 (append
1763     (label-value-line "Function" (kernel:%closure-function o))
1764     `("Environment:" (:newline))
1765 dcrosher 1.7 (loop for i from 0 below (- (kernel:get-closure-length o)
1766     (1- vm:closure-info-offset))
1767 dcrosher 1.1 append (label-value-line
1768     i (kernel:%closure-index-ref o i))))))
1769     ((eval::interpreted-function-p o)
1770     (scl-inspect o))
1771     (t
1772     (call-next-method)))))
1773    
1774    
1775 heller 1.16 (defmethod emacs-inspect ((o kernel:code-component))
1776 dcrosher 1.1 (append
1777     (label-value-line*
1778     ("code-size" (kernel:%code-code-size o))
1779     ("entry-points" (kernel:%code-entry-points o))
1780     ("debug-info" (kernel:%code-debug-info o))
1781     ("trace-table-offset" (kernel:code-header-ref
1782     o vm:code-trace-table-offset-slot)))
1783     `("Constants:" (:newline))
1784     (loop for i from vm:code-constants-offset
1785     below (kernel:get-header-data o)
1786     append (label-value-line i (kernel:code-header-ref o i)))
1787     `("Code:" (:newline)
1788     , (with-output-to-string (s)
1789     (cond ((kernel:%code-debug-info o)
1790     (disassem:disassemble-code-component o :stream s))
1791     (t
1792     (disassem:disassemble-memory
1793     (disassem::align
1794     (+ (logandc2 (kernel:get-lisp-obj-address o)
1795     vm:lowtag-mask)
1796     (* vm:code-constants-offset vm:word-bytes))
1797     (ash 1 vm:lowtag-bits))
1798     (ash (kernel:%code-code-size o) vm:word-shift)
1799 heller 1.17 :stream s)))))))
1800 dcrosher 1.1
1801 heller 1.16 (defmethod emacs-inspect ((o kernel:fdefn))
1802 heller 1.17 (label-value-line*
1803 dcrosher 1.1 ("name" (kernel:fdefn-name o))
1804     ("function" (kernel:fdefn-function o))
1805     ("raw-addr" (sys:sap-ref-32
1806     (sys:int-sap (kernel:get-lisp-obj-address o))
1807 heller 1.17 (* vm:fdefn-raw-addr-slot vm:word-bytes)))))
1808 dcrosher 1.1
1809 heller 1.16 (defmethod emacs-inspect ((o array))
1810 dcrosher 1.4 (cond ((kernel:array-header-p o)
1811 heller 1.17 (list* (format nil "~A is an array.~%" o)
1812 dcrosher 1.4 (label-value-line*
1813     (:header (describe-primitive-type o))
1814     (:rank (array-rank o))
1815     (:fill-pointer (kernel:%array-fill-pointer o))
1816     (:fill-pointer-p (kernel:%array-fill-pointer-p o))
1817     (:elements (kernel:%array-available-elements o))
1818     (:data (kernel:%array-data-vector o))
1819     (:displacement (kernel:%array-displacement o))
1820     (:displaced-p (kernel:%array-displaced-p o))
1821     (:dimensions (array-dimensions o)))))
1822     (t
1823 heller 1.17 (list* (format nil "~A is an simple-array.~%" o)
1824 dcrosher 1.4 (label-value-line*
1825     (:header (describe-primitive-type o))
1826     (:length (length o)))))))
1827 dcrosher 1.1
1828 heller 1.16 (defmethod emacs-inspect ((o simple-vector))
1829 heller 1.17 (list* (format nil "~A is a vector.~%" o)
1830 dcrosher 1.1 (append
1831     (label-value-line*
1832     (:header (describe-primitive-type o))
1833     (:length (c::vector-length o)))
1834     (unless (eq (array-element-type o) 'nil)
1835     (loop for i below (length o)
1836     append (label-value-line i (aref o i)))))))
1837    
1838     (defun inspect-alien-record (alien)
1839     (with-struct (alien::alien-value- sap type) alien
1840     (with-struct (alien::alien-record-type- kind name fields) type
1841     (append
1842     (label-value-line*
1843     (:sap sap)
1844     (:kind kind)
1845     (:name name))
1846     (loop for field in fields
1847     append (let ((slot (alien::alien-record-field-name field)))
1848 heller 1.17 (label-value-line slot (alien:slot alien slot))))))))
1849 dcrosher 1.1
1850     (defun inspect-alien-pointer (alien)
1851 heller 1.17 (with-struct (alien::alien-value- sap type) alien
1852 dcrosher 1.1 (label-value-line*
1853     (:sap sap)
1854     (:type type)
1855 heller 1.17 (:to (alien::deref alien)))))
1856 dcrosher 1.1
1857     (defun inspect-alien-value (alien)
1858     (typecase (alien::alien-value-type alien)
1859     (alien::alien-record-type (inspect-alien-record alien))
1860     (alien::alien-pointer-type (inspect-alien-pointer alien))
1861     (t (scl-inspect alien))))
1862    
1863     ;;;; Profiling
1864     (defimplementation profile (fname)
1865     (eval `(profile:profile ,fname)))
1866    
1867     (defimplementation unprofile (fname)
1868     (eval `(profile:unprofile ,fname)))
1869    
1870     (defimplementation unprofile-all ()
1871     (eval `(profile:unprofile))
1872     "All functions unprofiled.")
1873    
1874     (defimplementation profile-report ()
1875     (eval `(profile:report-time)))
1876    
1877     (defimplementation profile-reset ()
1878     (eval `(profile:reset-time))
1879     "Reset profiling counters.")
1880    
1881     (defimplementation profiled-functions ()
1882     profile:*timed-functions*)
1883    
1884     (defimplementation profile-package (package callers methods)
1885     (profile:profile-all :package package
1886     :callers-p callers
1887     #+nil :methods #+nil methods))
1888    
1889    
1890     ;;;; Multiprocessing
1891    
1892 dcrosher 1.11 (defimplementation spawn (fn &key name)
1893     (thread:thread-create fn :name (or name "Anonymous")))
1894 dcrosher 1.1
1895     (defvar *thread-id-counter* 0)
1896     (defvar *thread-id-counter-lock* (thread:make-lock "Thread ID counter"))
1897    
1898     (defimplementation thread-id (thread)
1899     (thread:with-lock-held (*thread-id-counter-lock*)
1900     (or (getf (thread:thread-plist thread) 'id)
1901     (setf (getf (thread:thread-plist thread) 'id)
1902     (incf *thread-id-counter*)))))
1903    
1904     (defimplementation find-thread (id)
1905 heller 1.10 (block find-thread
1906     (thread:map-over-threads
1907     #'(lambda (thread)
1908     (when (eql (getf (thread:thread-plist thread) 'id) id)
1909     (return-from find-thread thread))))))
1910 dcrosher 1.1
1911     (defimplementation thread-name (thread)
1912     (princ-to-string (thread:thread-name thread)))
1913    
1914     (defimplementation thread-status (thread)
1915     (let ((dynamic-values (thread::thread-dynamic-values thread)))
1916     (if (zerop dynamic-values) "Exited" "Running")))
1917    
1918     (defimplementation make-lock (&key name)
1919     (thread:make-lock name))
1920    
1921     (defimplementation call-with-lock-held (lock function)
1922     (declare (type function function))
1923     (thread:with-lock-held (lock) (funcall function)))
1924    
1925     (defimplementation current-thread ()
1926     thread:*thread*)
1927    
1928     (defimplementation all-threads ()
1929     (let ((all-threads nil))
1930     (thread:map-over-threads #'(lambda (thread) (push thread all-threads)))
1931     all-threads))
1932    
1933     (defimplementation interrupt-thread (thread fn)
1934     (thread:thread-interrupt thread #'(lambda ()
1935     (sys:with-interrupts
1936     (funcall fn)))))
1937    
1938     (defimplementation kill-thread (thread)
1939     (thread:destroy-thread thread))
1940    
1941     (defimplementation thread-alive-p (thread)
1942     (not (zerop (thread::thread-dynamic-values thread))))
1943    
1944 dcrosher 1.26 (defvar *mailbox-lock* (thread:make-lock "Mailbox lock" :interruptible nil))
1945 dcrosher 1.1
1946     (defstruct (mailbox)
1947 dcrosher 1.7 (lock (thread:make-lock "Thread mailbox" :type :error-check
1948     :interruptible nil)
1949 dcrosher 1.1 :type thread:error-check-lock)
1950     (queue '() :type list))
1951    
1952     (defun mailbox (thread)
1953     "Return 'thread's mailbox."
1954 dcrosher 1.26 (sys:without-interrupts
1955     (thread:with-lock-held (*mailbox-lock*)
1956     (or (getf (thread:thread-plist thread) 'mailbox)
1957 heller 1.40 (setf (getf (thread:thread-plist thread) 'mailbox)
1958     (make-mailbox))))))
1959 dcrosher 1.1
1960     (defimplementation send (thread message)
1961     (let* ((mbox (mailbox thread))
1962 dcrosher 1.7 (lock (mailbox-lock mbox)))
1963 dcrosher 1.26 (sys:without-interrupts
1964     (thread:with-lock-held (lock "Mailbox Send")
1965     (setf (mailbox-queue mbox) (nconc (mailbox-queue mbox)
1966     (list message)))))
1967 heller 1.21 (mp:process-wakeup thread)))
1968    
1969 dcrosher 1.26 #+nil
1970 dcrosher 1.1 (defimplementation receive ()
1971 heller 1.21 (receive-if (constantly t)))
1972 dcrosher 1.1
1973 dcrosher 1.26 (defimplementation receive-if (test &optional timeout)
1974 heller 1.20 (let ((mbox (mailbox thread:*thread*)))
1975 dcrosher 1.26 (assert (or (not timeout) (eq timeout t)))
1976 heller 1.20 (loop
1977 heller 1.21 (check-slime-interrupts)
1978 dcrosher 1.26 (sys:without-interrupts
1979     (mp:with-lock-held ((mailbox-lock mbox))
1980     (let* ((q (mailbox-queue mbox))
1981     (tail (member-if test q)))
1982     (when tail
1983     (setf (mailbox-queue mbox)
1984     (nconc (ldiff q tail) (cdr tail)))
1985     (return (car tail))))))
1986     (when (eq timeout t) (return (values nil t)))
1987 heller 1.21 (mp:process-wait-with-timeout
1988     "Mailbox read wait" 0.5 (lambda () (some test (mailbox-queue mbox)))))))
1989 heller 1.20
1990 dcrosher 1.1
1991    
1992     (defimplementation emacs-connected ())
1993    
1994    
1995     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1996     ;;Trace implementations
1997     ;; In SCL, we have:
1998     ;; (trace <name>)
1999     ;; (trace (method <name> <qualifier>? (<specializer>+)))
2000     ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2001     ;; <name> can be a normal name or a (setf name)
2002    
2003     (defun tracedp (spec)
2004     (member spec (eval '(trace)) :test #'equal))
2005    
2006     (defun toggle-trace-aux (spec &rest options)
2007     (cond ((tracedp spec)
2008     (eval `(untrace ,spec))
2009     (format nil "~S is now untraced." spec))
2010     (t
2011     (eval `(trace ,spec ,@options))
2012     (format nil "~S is now traced." spec))))
2013    
2014     (defimplementation toggle-trace (spec)
2015     (ecase (car spec)
2016     ((setf)
2017     (toggle-trace-aux spec))
2018     ((:defgeneric)
2019     (let ((name (second spec)))
2020     (toggle-trace-aux name :methods name)))
2021     ((:defmethod)
2022     nil)
2023     ((:call)
2024     (destructuring-bind (caller callee) (cdr spec)
2025     (toggle-trace-aux (process-fspec callee)
2026     :wherein (list (process-fspec caller)))))))
2027    
2028     (defun process-fspec (fspec)
2029     (cond ((consp fspec)
2030     (ecase (first fspec)
2031     ((:defun :defgeneric) (second fspec))
2032     ((:defmethod)
2033     `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2034     ;; this isn't actually supported
2035     ((:labels) `(labels ,(process-fspec (second fspec)) ,(third fspec)))
2036     ((:flet) `(flet ,(process-fspec (second fspec)) ,(third fspec)))))
2037     (t
2038     fspec)))
2039    
2040     ;;; Weak datastructures
2041    
2042     ;;; Not implemented in SCL.
2043     (defimplementation make-weak-key-hash-table (&rest args)
2044     (apply #'make-hash-table :weak-p t args))

  ViewVC Help
Powered by ViewVC 1.1.5