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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations)
Fri Aug 8 13:43:33 2008 UTC (5 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.21: +0 -5 lines
Spawn the auto-flush thread in the front end.
This removes some copy&paste code in various backends.

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

  ViewVC Help
Powered by ViewVC 1.1.5