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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Sat Feb 25 17:46:13 2006 UTC (8 years, 1 month ago) by dcrosher
Branch: MAIN
Changes since 1.4: +3 -4 lines
o swank-scl.lisp: Fix slime input stream reading.
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     (import-swank-mop-symbols :clos '(:slot-definition-documentation
16     :eql-specializer
17     :eql-specializer-object))
18    
19     (defun swank-mop:slot-definition-documentation (slot)
20     (slot-value slot 'documentation))
21    
22     (defun swank-mop:specializer-direct-methods (obj)
23     (declare (ignore obj))
24     nil)
25    
26     (deftype swank-mop:eql-specializer ()
27     '(or kernel:member-type kernel:numeric-type))
28    
29     (defun swank-mop:eql-specializer-object (obj)
30     (etypecase obj
31     (kernel:numeric-type
32     (kernel:type-specifier obj))
33     (kernel:member-type
34     (first (kernel:member-type-members obj)))))
35    
36    
37     ;;;; TCP server
38     ;;;
39     ;;; SCL only supports the :spawn communication style.
40     ;;;
41    
42     (defimplementation preferred-communication-style ()
43     :spawn)
44    
45     (defimplementation create-socket (host port)
46     (let ((addr (resolve-hostname host)))
47     (ext:create-inet-listener port :stream :host addr :reuse-address t)))
48    
49     (defimplementation local-port (socket)
50     (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
51    
52     (defimplementation close-socket (socket)
53     (ext:close-socket (socket-fd socket)))
54    
55 dcrosher 1.3 (defimplementation accept-connection (socket &key
56     (external-format :iso-latin-1-unix)
57     (buffering :full))
58 dcrosher 1.1 (let ((external-format (or external-format :iso-latin-1-unix)))
59     (make-socket-io-stream (ext:accept-tcp-connection socket)
60 dcrosher 1.3 external-format buffering)))
61 dcrosher 1.1
62     ;;;;; Sockets
63    
64     (defun socket-fd (socket)
65     "Return the file descriptor for the socket represented by 'socket."
66     (etypecase socket
67     (fixnum socket)
68     (stream (sys:fd-stream-fd socket))))
69    
70     (defun resolve-hostname (hostname)
71     "Return the IP address of 'hostname as an integer (in host byte-order)."
72     (let ((hostent (ext:lookup-host-entry hostname)))
73     (car (ext:host-entry-addr-list hostent))))
74    
75     (defun find-external-format (coding-system)
76     (case coding-system
77     (:iso-latin-1-unix :iso-8859-1)
78     (:utf-8-unix :utf-8)
79 dcrosher 1.3 (:euc-jp-unix :euc-jp)
80 dcrosher 1.1 (t coding-system)))
81    
82 dcrosher 1.3 (defun make-socket-io-stream (fd external-format buffering)
83 dcrosher 1.1 "Create a new input/output fd-stream for 'fd."
84     (let ((external-format (find-external-format external-format)))
85     (sys:make-fd-stream fd :input t :output t :element-type 'base-char
86 dcrosher 1.3 :buffering buffering
87 dcrosher 1.1 :external-format external-format)))
88    
89    
90     ;;;; Stream handling
91    
92     (defclass slime-input-stream (ext:character-input-stream)
93     ((buffer :initarg :buffer :type string)
94     (index :initarg :index :initform 0 :type fixnum)
95     (position :initarg :position :initform 0 :type integer)
96     (interactive :initarg :interactive :initform nil :type (member nil t))
97     (output-stream :initarg :output-stream :initform nil)
98     (input-fn :initarg :input-fn :type function)
99     ))
100    
101     (defun make-slime-input-stream (input-fn &optional output-stream)
102     (declare (function input-fn))
103     (make-instance 'slime-input-stream
104     :in-buffer (make-string 256)
105     :in-head 0 :in-tail 0
106     :out-buffer ""
107     :buffer "" :index 0
108     :input-fn input-fn
109     :output-stream output-stream))
110    
111     (defmethod print-object ((s slime-input-stream) stream)
112     (print-unreadable-object (s stream :type t)))
113    
114     ;;; input-stream-p inherits from input-stream.
115     ;;; output-stream-p inherits nil.
116    
117     (defmethod ext:stream-listen ((stream slime-input-stream))
118     (let* ((buffer (slot-value stream 'buffer))
119     (index (slot-value stream 'index))
120     (length (length buffer)))
121     (declare (type string buffer)
122     (fixnum index length))
123     (< index length)))
124    
125     (defmethod close ((stream slime-input-stream) &key ((:abort abort) nil))
126     (declare (ignore abort))
127     (when (ext:stream-open-p stream)
128     (setf (ext:stream-open-p stream) nil)
129     (setf (ext:stream-in-buffer stream) " ")
130     t))
131    
132     (defmethod ext:stream-clear-input ((stream slime-input-stream))
133     (let* ((input-buffer (slot-value stream 'buffer))
134     (index (slot-value stream 'index))
135     (input-length (length input-buffer))
136     (available (- input-length index))
137     (position (slot-value stream 'position))
138     (new-position (+ position available)))
139     (declare (type kernel:index index available position new-position))
140     (setf (slot-value stream 'position) new-position))
141     (setf (slot-value stream 'buffer) "")
142     (setf (slot-value stream 'index) 0)
143     nil)
144    
145     ;;; No 'stream-finish-output method.
146     ;;; No 'stream-force-output method.
147     ;;; No 'stream-clear-output method.
148    
149     ;;; stream-element-type inherits from character-stream.
150    
151     ;;; No 'stream-line-length method.
152     ;;; No 'stream-line-column method.
153    
154     ;;; Add the remaining input to the current position.
155     (defmethod file-length ((stream slime-input-stream))
156     (let* ((input-buffer (slot-value stream 'buffer))
157     (index (slot-value stream 'index))
158     (input-length (length input-buffer))
159     (available (- input-length index))
160     (position (slot-value stream 'position))
161     (file-length (+ position available)))
162     (declare (type kernel:index index available position file-length))
163     file-length))
164    
165     (defmethod ext:stream-file-position ((stream slime-input-stream)
166     &optional position)
167     (let ((current-position (slot-value stream 'position)))
168     (declare (type kernel:index current-position))
169     (cond (position
170     ;; Could make an attempt here, but just give up for now.
171     nil)
172     (t
173     current-position))))
174    
175     (defmethod interactive-stream-p ((stream slime-input-stream))
176     (slot-value stream 'interactive))
177    
178     ;;; No 'file-string-length method.
179    
180     (defmethod ext:stream-read-chars ((stream slime-input-stream) buffer
181     start requested waitp)
182     (declare (type simple-string buffer)
183     (type kernel:index start requested))
184     (let* ((input-buffer (slot-value stream 'buffer))
185     (index (slot-value stream 'index))
186     (input-length (length input-buffer))
187     (available (- input-length index))
188     (copy (min available requested)))
189     (declare (string input-buffer)
190     (type kernel:index index available copy))
191     (cond ((plusp copy)
192     (dotimes (i copy)
193     (declare (type kernel:index i))
194     (setf (aref buffer (+ start i)) (aref input-buffer (+ index i))))
195 dcrosher 1.5 (setf (slot-value stream 'index) (+ index copy))
196 dcrosher 1.1 (incf (slot-value stream 'position) copy)
197     copy)
198     (waitp
199     (let ((output-stream (slot-value stream 'output-stream))
200     (input-fn (slot-value stream 'input-fn)))
201     (declare (type function input-fn))
202     (when output-stream
203     (force-output output-stream))
204     (let ((new-input (funcall input-fn)))
205     (cond ((zerop (length new-input))
206     -1)
207     (t
208     (setf (slot-value stream 'buffer) new-input)
209     (setf (slot-value stream 'index) 0)
210 dcrosher 1.5 (ext:stream-read-chars stream buffer
211     start requested waitp))))))
212 dcrosher 1.1 (t
213     0))))
214    
215     ;;; Slime output stream.
216    
217     (defclass slime-output-stream (ext:character-output-stream)
218     ((output-fn :initarg :output-fn :type function)
219     (column :initform 0 :type kernel:index)
220     (interactive :initform nil :type (member nil t))
221     (position :initform 0 :type integer)))
222    
223     (defun make-slime-output-stream (output-fn)
224     (declare (function output-fn))
225     (make-instance 'slime-output-stream
226     :in-buffer ""
227     :out-buffer (make-string 256)
228     :output-fn output-fn))
229    
230     (defmethod print-object ((s slime-output-stream) stream)
231     (print-unreadable-object (s stream :type t)))
232    
233     ;;; Use default 'input-stream-p method for 'output-stream which returns 'nil.
234     ;;; Use default 'output-stream-p method for 'output-stream which returns 't.
235    
236     ;;; No 'stream-listen method.
237    
238     (defmethod close ((stream slime-output-stream) &key ((:abort abort) nil))
239     (when (ext:stream-open-p stream)
240     (unless abort
241     (finish-output stream))
242     (setf (ext:stream-open-p stream) nil)
243     (setf (ext:stream-out-buffer stream) " ")
244     t))
245    
246     ;;; No 'stream-clear-input method.
247    
248     (defmethod ext:stream-finish-output ((stream slime-output-stream))
249     nil)
250    
251     (defmethod ext:stream-force-output ((stream slime-output-stream))
252     nil)
253    
254     (defmethod ext:stream-clear-output ((stream slime-output-stream))
255     nil)
256    
257     ;;; Use default 'stream-element-type method for 'character-stream which
258     ;;; returns 'base-char.
259    
260     (defmethod ext:stream-line-length ((stream slime-output-stream))
261     80)
262    
263     (defmethod ext:stream-line-column ((stream slime-output-stream))
264     (slot-value stream 'column))
265    
266     (defmethod file-length ((stream slime-output-stream))
267     (slot-value stream 'position))
268    
269     (defmethod ext:stream-file-position ((stream slime-output-stream)
270     &optional position)
271     (declare (optimize (speed 3)))
272     (cond (position
273     (let* ((current-position (slot-value stream 'position))
274     (target-position (etypecase position
275     ((member :start) 0)
276     ((member :end) current-position)
277     (kernel:index position))))
278     (declare (type kernel:index current-position target-position))
279     (cond ((= target-position current-position)
280     t)
281     ((> target-position current-position)
282     (let ((output-fn (slot-value stream 'output-fn))
283     (fill-size (- target-position current-position)))
284     (declare (function output-fn))
285     (funcall output-fn (make-string fill-size
286     :initial-element #\space))
287     (setf (slot-value stream 'position) target-position))
288     t)
289     (t
290     nil))))
291     (t
292     (slot-value stream 'position))))
293    
294     (defmethod interactive-stream-p ((stream slime-output-stream))
295     (slot-value stream 'interactive))
296    
297     ;;; Use the default 'character-output-stream 'file-string-length method.
298    
299     ;;; stream-write-chars
300     ;;;
301     ;;; The stream out-buffer is typically large enough that there is little point
302     ;;; growing the stream output 'string large than the total size. For typical
303     ;;; usage this reduces consing. As the string grows larger then grow to
304     ;;; reduce the cost of copying strings around.
305     ;;;
306     (defmethod ext:stream-write-chars ((stream slime-output-stream) string start end)
307     (declare (simple-string string)
308     (type kernel:index start end))
309     (declare (optimize (speed 3)))
310     (unless (ext:stream-open-p stream)
311     (error 'kernel:simple-stream-error
312     :stream stream
313     :format-control "Stream closed."))
314     (let* ((string-length (length string))
315     (start (cond ((< start 0) 0)
316     ((> start string-length) string-length)
317     (t start)))
318     (end (cond ((< end start) start)
319     ((> end string-length) string-length)
320     (t end)))
321     (length (- end start))
322     (output-fn (slot-value stream 'output-fn)))
323     (declare (type kernel:index start end length)
324     (type function output-fn))
325     (unless (zerop length)
326     (funcall output-fn (subseq string start end))
327     (let ((last-newline (position #\newline string :from-end t
328     :start start :end end)))
329     (setf (slot-value stream 'column)
330     (if last-newline
331     (- end last-newline 1)
332     (let ((column (slot-value stream 'column)))
333     (declare (type kernel:index column))
334     (+ column (- end start))))))))
335     string)
336    
337     ;;;
338    
339     (defimplementation make-fn-streams (input-fn output-fn)
340     (let* ((output (make-slime-output-stream output-fn))
341     (input (make-slime-input-stream input-fn output)))
342     (values input output)))
343    
344     (defimplementation make-stream-interactive (stream)
345     (when (or (typep stream 'slime-input-stream)
346     (typep stream 'slime-output-stream))
347     (setf (slot-value stream 'interactive) t)))
348    
349    
350     ;;;; Compilation Commands
351    
352     (defvar *previous-compiler-condition* nil
353     "Used to detect duplicates.")
354    
355     (defvar *previous-context* nil
356     "Previous compiler error context.")
357    
358     (defvar *buffer-name* nil
359     "The name of the Emacs buffer we are compiling from.
360     Nil if we aren't compiling from a buffer.")
361    
362     (defvar *buffer-start-position* nil)
363     (defvar *buffer-substring* nil)
364    
365     (defimplementation call-with-compilation-hooks (function)
366     (let ((*previous-compiler-condition* nil)
367     (*previous-context* nil)
368     (*print-readably* nil))
369     (handler-bind ((c::compiler-error #'handle-notification-condition)
370     (c::style-warning #'handle-notification-condition)
371     (c::warning #'handle-notification-condition))
372     (funcall function))))
373    
374     (defimplementation swank-compile-file (filename load-p
375     &optional external-format)
376     (let ((external-format (if external-format
377     (find-external-format external-format)
378     :default)))
379     (with-compilation-hooks ()
380     (let ((*buffer-name* nil)
381     (ext:*ignore-extra-close-parentheses* nil))
382     (multiple-value-bind (output-file warnings-p failure-p)
383     (compile-file filename :external-format external-format)
384     (unless failure-p
385     ;; Cache the latest source file for definition-finding.
386     (source-cache-get filename (file-write-date filename))
387     (when load-p (load output-file)))
388     (values output-file warnings-p failure-p))))))
389    
390     (defimplementation swank-compile-string (string &key buffer position directory)
391     (declare (ignore directory))
392     (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     (:unknown
1168     (return-from describe-definition
1169     (format nil "Unknown alien type: ~S" symbol))))))))
1170    
1171     ;;;;; Argument lists
1172    
1173     (defimplementation arglist ((name symbol))
1174     (cond ((and (symbolp name) (macro-function name))
1175     (arglist (macro-function name)))
1176     ((fboundp name)
1177     (arglist (fdefinition name)))
1178     (t
1179     :not-available)))
1180    
1181     (defimplementation arglist ((fun function))
1182     (flet ((compiled-function-arglist (x)
1183     (let ((args (kernel:%function-arglist x)))
1184     (if args
1185     (read-arglist x)
1186     :not-available))))
1187     (case (kernel:get-type fun)
1188     (#.vm:closure-header-type
1189     (compiled-function-arglist
1190     (kernel:%closure-function fun)))
1191     ((#.vm:function-header-type #.vm:closure-function-header-type)
1192     (compiled-function-arglist fun))
1193     (#.vm:funcallable-instance-header-type
1194     (typecase fun
1195     (kernel:byte-function
1196     :not-available)
1197     (kernel:byte-closure
1198     :not-available)
1199     (eval:interpreted-function
1200     (eval:interpreted-function-arglist fun))
1201     (otherwise
1202     (clos::generic-function-lambda-list fun))))
1203     (t
1204     :non-available))))
1205    
1206     (defimplementation function-name (function)
1207     (cond ((eval:interpreted-function-p function)
1208     (eval:interpreted-function-name function))
1209     ((typep function 'generic-function)
1210     (clos:generic-function-name function))
1211     ((c::byte-function-or-closure-p function)
1212     (c::byte-function-name function))
1213     (t (kernel:%function-name (kernel:%function-self function)))))
1214    
1215     ;;; A simple case: the arglist is available as a string that we can
1216     ;;; `read'.
1217    
1218     (defun read-arglist (fn)
1219     "Parse the arglist-string of the function object FN."
1220     (let ((string (kernel:%function-arglist
1221     (kernel:%function-self fn)))
1222     (package (find-package
1223     (c::compiled-debug-info-package
1224     (kernel:%code-debug-info
1225     (vm::find-code-object fn))))))
1226     (with-standard-io-syntax
1227     (let ((*package* (or package *package*)))
1228     (read-from-string string)))))
1229    
1230     ;;; A harder case: an approximate arglist is derived from available
1231     ;;; debugging information.
1232    
1233     (defun debug-function-arglist (debug-function)
1234     "Derive the argument list of DEBUG-FUNCTION from debug info."
1235     (let ((args (di::debug-function-lambda-list debug-function))
1236     (required '())
1237     (optional '())
1238     (rest '())
1239     (key '()))
1240     ;; collect the names of debug-vars
1241     (dolist (arg args)
1242     (etypecase arg
1243     (di::debug-variable
1244     (push (di::debug-variable-symbol arg) required))
1245     ((member :deleted)
1246     (push ':deleted required))
1247     (cons
1248     (ecase (car arg)
1249     (:keyword
1250     (push (second arg) key))
1251     (:optional
1252     (push (debug-variable-symbol-or-deleted (second arg)) optional))
1253     (:rest
1254     (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1255     ;; intersperse lambda keywords as needed
1256     (append (nreverse required)
1257     (if optional (cons '&optional (nreverse optional)))
1258     (if rest (cons '&rest (nreverse rest)))
1259     (if key (cons '&key (nreverse key))))))
1260    
1261     (defun debug-variable-symbol-or-deleted (var)
1262     (etypecase var
1263     (di:debug-variable
1264     (di::debug-variable-symbol var))
1265     ((member :deleted)
1266     '#:deleted)))
1267    
1268     (defun symbol-debug-function-arglist (fname)
1269     "Return FNAME's debug-function-arglist and %function-arglist.
1270     A utility for debugging DEBUG-FUNCTION-ARGLIST."
1271     (let ((fn (fdefinition fname)))
1272     (values (debug-function-arglist (di::function-debug-function fn))
1273     (kernel:%function-arglist (kernel:%function-self fn)))))
1274    
1275     ;;; Deriving arglists for byte-compiled functions:
1276     ;;;
1277     (defun byte-code-function-arglist (fn)
1278     ;; There doesn't seem to be much arglist information around for
1279     ;; byte-code functions. Use the arg-count and return something like
1280     ;; (arg0 arg1 ...)
1281     (etypecase fn
1282     (c::simple-byte-function
1283     (loop for i from 0 below (c::simple-byte-function-num-args fn)
1284     collect (make-arg-symbol i)))
1285     (c::hairy-byte-function
1286     (hairy-byte-function-arglist fn))
1287     (c::byte-closure
1288     (byte-code-function-arglist (c::byte-closure-function fn)))))
1289    
1290     (defun make-arg-symbol (i)
1291     (make-symbol (format nil "~A~D" (string 'arg) i)))
1292    
1293     ;;; A "hairy" byte-function is one that takes a variable number of
1294     ;;; arguments. `hairy-byte-function' is a type from the bytecode
1295     ;;; interpreter.
1296     ;;;
1297     (defun hairy-byte-function-arglist (fn)
1298     (let ((counter -1))
1299     (flet ((next-arg () (make-arg-symbol (incf counter))))
1300     (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1301     keywords-p keywords) fn
1302     (let ((arglist '())
1303     (optional (- max-args min-args)))
1304     ;; XXX isn't there a better way to write this?
1305     ;; (Looks fine to me. -luke)
1306     (dotimes (i min-args)
1307     (push (next-arg) arglist))
1308     (when (plusp optional)
1309     (push '&optional arglist)
1310     (dotimes (i optional)
1311     (push (next-arg) arglist)))
1312     (when rest-arg-p
1313     (push '&rest arglist)
1314     (push (next-arg) arglist))
1315     (when keywords-p
1316     (push '&key arglist)
1317     (loop for (key _ __) in keywords
1318     do (push key arglist))
1319     (when (eq keywords-p :allow-others)
1320     (push '&allow-other-keys arglist)))
1321     (nreverse arglist))))))
1322    
1323    
1324     ;;;; Miscellaneous.
1325    
1326     (defimplementation macroexpand-all (form)
1327     (macroexpand form))
1328    
1329     (defimplementation set-default-directory (directory)
1330     (setf (ext:default-directory) (namestring directory))
1331     ;; Setting *default-pathname-defaults* to an absolute directory
1332     ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1333     (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1334     (default-directory))
1335    
1336     (defimplementation default-directory ()
1337     (namestring (ext:default-directory)))
1338    
1339     (defimplementation call-without-interrupts (fn)
1340     (funcall fn))
1341    
1342     (defimplementation getpid ()
1343     (unix:unix-getpid))
1344    
1345     (defimplementation lisp-implementation-type-name ()
1346     (if (eq ext:*case-mode* :upper) "scl" "scl-lower"))
1347    
1348     (defimplementation quit-lisp ()
1349     (ext:quit))
1350    
1351     ;;; source-path-{stream,file,string,etc}-position moved into
1352     ;;; swank-source-path-parser
1353    
1354    
1355     ;;;; Debugging
1356    
1357     (defvar *sldb-stack-top*)
1358    
1359     (defimplementation call-with-debugging-environment (debugger-loop-fn)
1360     (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1361     (debug:*stack-top-hint* nil)
1362     (kernel:*current-level* 0))
1363     (handler-bind ((di::unhandled-condition
1364     (lambda (condition)
1365     (error (make-condition
1366     'sldb-condition
1367     :original-condition condition)))))
1368     (funcall debugger-loop-fn))))
1369    
1370     (defun frame-down (frame)
1371     (handler-case (di:frame-down frame)
1372     (di:no-debug-info () nil)))
1373    
1374     (defun nth-frame (index)
1375     (do ((frame *sldb-stack-top* (frame-down frame))
1376     (i index (1- i)))
1377     ((zerop i) frame)))
1378    
1379     (defimplementation compute-backtrace (start end)
1380     (let ((end (or end most-positive-fixnum)))
1381     (loop for f = (nth-frame start) then (frame-down f)
1382     for i from start below end
1383     while f
1384     collect f)))
1385    
1386     (defimplementation print-frame (frame stream)
1387     (let ((*standard-output* stream))
1388     (handler-case
1389     (debug::print-frame-call frame :verbosity 1 :number nil)
1390     (error (e)
1391     (ignore-errors (princ e stream))))))
1392    
1393     (defimplementation frame-source-location-for-emacs (index)
1394     (code-location-source-location (di:frame-code-location (nth-frame index))))
1395    
1396     (defimplementation eval-in-frame (form index)
1397     (di:eval-in-frame (nth-frame index) form))
1398    
1399     (defun frame-debug-vars (frame)
1400     "Return a vector of debug-variables in frame."
1401     (di::debug-function-debug-variables (di:frame-debug-function frame)))
1402    
1403     (defun debug-var-value (var frame location)
1404     (let ((validity (di:debug-variable-validity var location)))
1405     (ecase validity
1406     (:valid (di:debug-variable-value var frame))
1407     ((:invalid :unknown) (make-symbol (string validity))))))
1408    
1409     (defimplementation frame-locals (index)
1410     (let* ((frame (nth-frame index))
1411     (loc (di:frame-code-location frame))
1412     (vars (frame-debug-vars frame)))
1413     (loop for v across vars collect
1414     (list :name (di:debug-variable-symbol v)
1415     :id (di:debug-variable-id v)
1416     :value (debug-var-value v frame loc)))))
1417    
1418     (defimplementation frame-var-value (frame var)
1419     (let* ((frame (nth-frame frame))
1420     (dvar (aref (frame-debug-vars frame) var)))
1421     (debug-var-value dvar frame (di:frame-code-location frame))))
1422    
1423     (defimplementation frame-catch-tags (index)
1424     (mapcar #'car (di:frame-catches (nth-frame index))))
1425    
1426     (defimplementation return-from-frame (index form)
1427 dcrosher 1.4 (let ((sym (find-symbol (symbol-name '#:find-debug-tag-for-frame)
1428 dcrosher 1.1 :debug-internals)))
1429     (if sym
1430     (let* ((frame (nth-frame index))
1431     (probe (funcall sym frame)))
1432     (cond (probe (throw (car probe) (eval-in-frame form index)))
1433     (t (format nil "Cannot return from frame: ~S" frame))))
1434     "return-from-frame is not implemented in this version of SCL.")))
1435    
1436     (defimplementation activate-stepping (frame)
1437     (set-step-breakpoints (nth-frame frame)))
1438    
1439     (defimplementation sldb-break-on-return (frame)
1440     (break-on-return (nth-frame frame)))
1441    
1442     ;;; We set the breakpoint in the caller which might be a bit confusing.
1443     ;;;
1444     (defun break-on-return (frame)
1445     (let* ((caller (di:frame-down frame))
1446     (cl (di:frame-code-location caller)))
1447     (flet ((hook (frame bp)
1448     (when (frame-pointer= frame caller)
1449     (di:delete-breakpoint bp)
1450     (signal-breakpoint bp frame))))
1451     (let* ((info (ecase (di:code-location-kind cl)
1452     ((:single-value-return :unknown-return) nil)
1453     (:known-return (debug-function-returns
1454     (di:frame-debug-function frame)))))
1455     (bp (di:make-breakpoint #'hook cl :kind :code-location
1456     :info info)))
1457     (di:activate-breakpoint bp)
1458     `(:ok ,(format nil "Set breakpoint in ~A" caller))))))
1459    
1460     (defun frame-pointer= (frame1 frame2)
1461     "Return true if the frame pointers of FRAME1 and FRAME2 are the same."
1462     (sys:sap= (di::frame-pointer frame1) (di::frame-pointer frame2)))
1463    
1464     ;;; The PC in escaped frames at a single-return-value point is
1465     ;;; actually vm:single-value-return-byte-offset bytes after the
1466     ;;; position given in the debug info. Here we try to recognize such
1467     ;;; cases.
1468     ;;;
1469     (defun next-code-locations (frame code-location)
1470     "Like `debug::next-code-locations' but be careful in escaped frames."
1471     (let ((next (debug::next-code-locations code-location)))
1472     (flet ((adjust-pc ()
1473     (let ((cl (di::copy-compiled-code-location code-location)))
1474     (incf (di::compiled-code-location-pc cl)
1475     vm:single-value-return-byte-offset)
1476     cl)))
1477     (cond ((and (di::compiled-frame-escaped frame)
1478     (eq (di:code-location-kind code-location)
1479     :single-value-return)
1480     (= (length next) 1)
1481     (di:code-location= (car next) (adjust-pc)))
1482     (debug::next-code-locations (car next)))
1483     (t
1484     next)))))
1485    
1486     (defun set-step-breakpoints (frame)
1487     (let ((cl (di:frame-code-location frame)))
1488     (when (di:debug-block-elsewhere-p (di:code-location-debug-block cl))
1489     (error "Cannot step in elsewhere code"))
1490     (let* ((debug::*bad-code-location-types*
1491     (remove :call-site debug::*bad-code-location-types*))
1492     (next (next-code-locations frame cl)))
1493     (cond (next
1494     (let ((steppoints '()))
1495     (flet ((hook (bp-frame bp)
1496     (signal-breakpoint bp bp-frame)
1497     (mapc #'di:delete-breakpoint steppoints)))
1498     (dolist (code-location next)
1499     (let ((bp (di:make-breakpoint #'hook code-location
1500     :kind :code-location)))
1501     (di:activate-breakpoint bp)
1502     (push bp steppoints))))))
1503     (t
1504     (break-on-return frame))))))
1505    
1506    
1507     ;; XXX the return values at return breakpoints should be passed to the
1508     ;; user hooks. debug-int.lisp should be changed to do this cleanly.
1509    
1510     ;;; The sigcontext and the PC for a breakpoint invocation are not
1511     ;;; passed to user hook functions, but we need them to extract return
1512     ;;; values. So we advice di::handle-breakpoint and bind the values to
1513     ;;; special variables.
1514     ;;;
1515     (defvar *breakpoint-sigcontext*)
1516     (defvar *breakpoint-pc*)
1517    
1518     (defun sigcontext-object (sc index)
1519     "Extract the lisp object in sigcontext SC at offset INDEX."
1520     (kernel:make-lisp-obj (vm:ucontext-register sc index)))
1521    
1522     (defun known-return-point-values (sigcontext sc-offsets)
1523     (let ((fp (system:int-sap (vm:ucontext-register sigcontext
1524     vm::cfp-offset))))
1525     (system:without-gcing
1526     (loop for sc-offset across sc-offsets
1527     collect (di::sub-access-debug-var-slot fp sc-offset sigcontext)))))
1528    
1529     ;;; SCL returns the first few values in registers and the rest on
1530     ;;; the stack. In the multiple value case, the number of values is
1531     ;;; stored in a dedicated register. The values of the registers can be
1532     ;;; accessed in the sigcontext for the breakpoint. There are 3 kinds
1533     ;;; of return conventions: :single-value-return, :unknown-return, and
1534     ;;; :known-return.
1535     ;;;
1536     ;;; The :single-value-return convention returns the value in a
1537     ;;; register without setting the nargs registers.
1538     ;;;
1539     ;;; The :unknown-return variant is used for multiple values. A
1540     ;;; :unknown-return point consists actually of 2 breakpoints: one for
1541     ;;; the single value case and one for the general case. The single
1542     ;;; value breakpoint comes vm:single-value-return-byte-offset after
1543     ;;; the multiple value breakpoint.
1544     ;;;
1545     ;;; The :known-return convention is used by local functions.
1546     ;;; :known-return is currently not supported because we don't know
1547     ;;; where the values are passed.
1548     ;;;
1549     (defun breakpoint-values (breakpoint)
1550     "Return the list of return values for a return point."
1551     (flet ((1st (sc) (sigcontext-object sc (car vm::register-arg-offsets))))
1552     (let ((sc (locally (declare (optimize (ext:inhibit-warnings 3)))
1553     (alien:sap-alien *breakpoint-sigcontext* (* unix:ucontext))))
1554     (cl (di:breakpoint-what breakpoint)))
1555     (ecase (di:code-location-kind cl)
1556     (:single-value-return
1557     (list (1st sc)))
1558     (:known-return
1559     (let ((info (di:breakpoint-info breakpoint)))
1560     (if (vectorp info)
1561     (known-return-point-values sc info)
1562     (progn
1563     ;;(break)
1564     (list "<<known-return convention not supported>>" info)))))
1565     (:unknown-return
1566     (let ((mv-return-pc (di::compiled-code-location-pc cl)))
1567     (if (= mv-return-pc *breakpoint-pc*)
1568     (mv-function-end-breakpoint-values sc)
1569     (list (1st sc)))))))))
1570    
1571     (defun mv-function-end-breakpoint-values (sigcontext)
1572 dcrosher 1.4 (let ((sym (find-symbol (symbol-name '#:function-end-breakpoint-values/standard)
1573     :debug-internals)))
1574 dcrosher 1.1 (cond (sym (funcall sym sigcontext))
1575     (t (di::get-function-end-breakpoint-values sigcontext)))))
1576    
1577     (defun debug-function-returns (debug-fun)
1578     "Return the return style of DEBUG-FUN."
1579     (let* ((cdfun (di::compiled-debug-function-compiler-debug-fun debug-fun)))
1580     (c::compiled-debug-function-returns cdfun)))
1581    
1582     (define-condition breakpoint (simple-condition)
1583     ((message :initarg :message :reader breakpoint.message)
1584     (values :initarg :values :reader breakpoint.values))
1585     (:report (lambda (c stream) (princ (breakpoint.message c) stream))))
1586    
1587     (defimplementation condition-extras ((c breakpoint))
1588     ;; simply pop up the source buffer
1589     `((:short-frame-source 0)))
1590    
1591     (defun signal-breakpoint (breakpoint frame)
1592     "Signal a breakpoint condition for BREAKPOINT in FRAME.
1593     Try to create a informative message."
1594     (flet ((brk (values fstring &rest args)
1595     (let ((msg (apply #'format nil fstring args))
1596     (debug:*stack-top-hint* frame))
1597     (break 'breakpoint :message msg :values values))))
1598     (with-struct (di::breakpoint- kind what) breakpoint
1599     (case kind
1600     (:code-location
1601     (case (di:code-location-kind what)
1602     ((:single-value-return :known-return :unknown-return)
1603     (let ((values (breakpoint-values breakpoint)))
1604     (brk values "Return value: ~{~S ~}" values)))
1605     (t
1606     #+(or)
1607     (when (eq (di:code-location-kind what) :call-site)
1608     (call-site-function breakpoint frame))
1609     (brk nil "Breakpoint: ~S ~S"
1610     (di:code-location-kind what)
1611     (di::compiled-code-location-pc what)))))
1612     (:function-start
1613     (brk nil "Function start breakpoint"))
1614     (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1615    
1616     #+nil
1617     (defimplementation sldb-break-at-start (fname)
1618     (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1619     (cond ((not debug-fun)
1620     `(:error ,(format nil "~S has no debug-function" fname)))
1621     (t
1622     (flet ((hook (frame bp &optional args cookie)
1623     (declare (ignore args cookie))
1624     (signal-breakpoint bp frame)))
1625     (let ((bp (di:make-breakpoint #'hook debug-fun
1626     :kind :function-start)))
1627     (di:activate-breakpoint bp)
1628     `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1629    
1630     (defun frame-cfp (frame)
1631     "Return the Control-Stack-Frame-Pointer for FRAME."
1632     (etypecase frame
1633     (di::compiled-frame (di::frame-pointer frame))
1634     ((or di::interpreted-frame null) -1)))
1635    
1636     (defun frame-ip (frame)
1637     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1638     (if (not frame)
1639     -1
1640     (let ((debug-fun (di::frame-debug-function frame)))
1641     (etypecase debug-fun
1642     (di::compiled-debug-function
1643     (let* ((code-loc (di:frame-code-location frame))
1644     (component (di::compiled-debug-function-component debug-fun))
1645     (pc (di::compiled-code-location-pc code-loc))
1646     (ip (sys:without-gcing
1647     (sys:sap-int
1648     (sys:sap+ (kernel:code-instructions component) pc)))))
1649     (values ip pc)))
1650     ((or di::bogus-debug-function di::interpreted-debug-function)
1651     -1)))))
1652    
1653     (defun frame-registers (frame)
1654     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1655     (let* ((cfp (frame-cfp frame))
1656     (csp (frame-cfp (di::frame-up frame)))
1657     (ip (frame-ip frame))
1658     (ocfp (frame-cfp (di::frame-down frame)))
1659     (lra (frame-ip (di::frame-down frame))))
1660     (values csp cfp ip ocfp lra)))
1661    
1662     (defun print-frame-registers (frame-number)
1663     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1664     (flet ((fixnum (p) (etypecase p
1665     (integer p)
1666     (sys:system-area-pointer (sys:sap-int p)))))
1667     (apply #'format t "~
1668     CSP = ~X
1669     CFP = ~X
1670     IP = ~X
1671     OCFP = ~X
1672     LRA = ~X~%" (mapcar #'fixnum
1673     (multiple-value-list (frame-registers frame)))))))
1674    
1675    
1676     (defimplementation disassemble-frame (frame-number)
1677     "Return a string with the disassembly of frames code."
1678     (print-frame-registers frame-number)
1679     (terpri)
1680     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1681     (debug-fun (di::frame-debug-function frame)))
1682     (etypecase debug-fun
1683     (di::compiled-debug-function
1684     (let* ((component (di::compiled-debug-function-component debug-fun))
1685     (fun (di:debug-function-function debug-fun)))
1686     (if fun
1687     (disassemble fun)
1688     (disassem:disassemble-code-component component))))
1689     (di::bogus-debug-function
1690     (format t "~%[Disassembling bogus frames not implemented]")))))
1691    
1692    
1693     ;;;; Inspecting
1694    
1695     (defclass scl-inspector (inspector)
1696     ())
1697    
1698     (defimplementation make-default-inspector ()
1699     (make-instance 'scl-inspector))
1700    
1701     (defconstant +lowtag-symbols+
1702     '(vm:even-fixnum-type
1703     vm:instance-pointer-type
1704     vm:other-immediate-0-type
1705     vm:list-pointer-type
1706     vm:odd-fixnum-type
1707     vm:function-pointer-type
1708     vm:other-immediate-1-type
1709     vm:other-pointer-type)
1710     "Names of the constants that specify type tags.
1711     The `symbol-value' of each element is a type tag.")
1712    
1713     (defconstant +header-type-symbols+
1714     (labels ((suffixp (suffix string)
1715     (and (>= (length string) (length suffix))
1716     (string= string suffix :start1 (- (length string)
1717     (length suffix)))))
1718     (header-type-symbol-p (x)
1719     (and (suffixp (symbol-name '#:-type) (symbol-name x))
1720     (not (member x +lowtag-symbols+))
1721     (boundp x)
1722     (typep (symbol-value x) 'fixnum))))
1723     (remove-if-not #'header-type-symbol-p
1724     (append (apropos-list (symbol-name '#:-type) :vm)
1725     (apropos-list (symbol-name '#:-type) :bignum))))
1726     "A list of names of the type codes in boxed objects.")
1727    
1728     (defimplementation describe-primitive-type (object)
1729     (with-output-to-string (*standard-output*)
1730     (let* ((lowtag (kernel:get-lowtag object))
1731     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1732     (format t "lowtag: ~A" lowtag-symbol)
1733     (when (member lowtag (list vm:other-pointer-type
1734     vm:function-pointer-type
1735     vm:other-immediate-0-type
1736     vm:other-immediate-1-type
1737     ))
1738     (let* ((type (kernel:get-type object))
1739     (type-symbol (find type +header-type-symbols+
1740     :key #'symbol-value)))
1741     (format t ", type: ~A" type-symbol))))))
1742    
1743     (defimplementation inspect-for-emacs ((o t) (inspector scl-inspector))
1744     (cond ((di::indirect-value-cell-p o)
1745     (values (format nil "~A is a value cell." o)
1746     `("Value: " (:value ,(c:value-cell-ref o)))))
1747     ((alien::alien-value-p o)
1748     (inspect-alien-value o))
1749     (t
1750     (scl-inspect o))))
1751    
1752 dcrosher 1.4 (defimplementation inspect-for-emacs ((o standard-object)
1753     (inspector scl-inspector))
1754     (declare (ignore inspector))
1755     (let ((c (class-of o)))
1756     (values "An object."
1757     `("Class: " (:value ,c) (:newline)
1758     "Slots:" (:newline)
1759     ,@(loop for slotd in (clos:class-slots c)
1760     for name = (clos:slot-definition-name slotd)
1761     collect `(:value ,slotd ,(string name))
1762     collect " = "
1763     collect (if (clos:slot-boundp-using-class c o name)
1764     `(:value ,(clos:slot-value-using-class
1765     c o name))
1766     "#<unbound>")
1767     collect '(:newline))))))
1768    
1769 dcrosher 1.1 (defun scl-inspect (o)
1770 dcrosher 1.4 (destructuring-bind (text labeledp . parts)
1771     (inspect::describe-parts o)
1772 dcrosher 1.1 (values (format nil "~A~%" text)
1773     (if labeledp
1774     (loop for (label . value) in parts
1775     append (label-value-line label value))
1776     (loop for value in parts for i from 0
1777     append (label-value-line i value))))))
1778    
1779 dcrosher 1.3 (defmethod inspect-for-emacs ((o function) (inspector scl-inspector))
1780 dcrosher 1.1 (declare (ignore inspector))
1781     (let ((header (kernel:get-type o)))
1782     (cond ((= header vm:function-header-type)
1783     (values (format nil "~A is a function." o)
1784     (append (label-value-line*
1785     ("Self" (kernel:%function-self o))
1786     ("Next" (kernel:%function-next o))
1787     ("Name" (kernel:%function-name o))
1788     ("Arglist" (kernel:%function-arglist o))
1789     ("Type" (kernel:%function-type o))
1790     ("Code" (kernel:function-code-header o)))
1791     (list
1792     (with-output-to-string (s)
1793     (disassem:disassemble-function o :stream s))))))
1794     ((= header vm:closure-header-type)
1795     (values (format nil "~A is a closure" o)
1796     (append
1797     (label-value-line "Function" (kernel:%closure-function o))
1798     `("Environment:" (:newline))
1799     (loop for i from 0 below (1- (kernel:get-closure-length o))
1800     append (label-value-line
1801     i (kernel:%closure-index-ref o i))))))
1802     ((eval::interpreted-function-p o)
1803     (scl-inspect o))
1804     (t
1805     (call-next-method)))))
1806    
1807    
1808     (defmethod inspect-for-emacs ((o kernel:code-component) (_ scl-inspector))
1809     (declare (ignore _))
1810     (values (format nil "~A is a code data-block." o)
1811     (append
1812     (label-value-line*
1813     ("code-size" (kernel:%code-code-size o))
1814     ("entry-points" (kernel:%code-entry-points o))
1815     ("debug-info" (kernel:%code-debug-info o))
1816     ("trace-table-offset" (kernel:code-header-ref
1817     o vm:code-trace-table-offset-slot)))
1818     `("Constants:" (:newline))
1819     (loop for i from vm:code-constants-offset
1820     below (kernel:get-header-data o)
1821     append (label-value-line i (kernel:code-header-ref o i)))
1822     `("Code:" (:newline)
1823     , (with-output-to-string (s)
1824     (cond ((kernel:%code-debug-info o)
1825     (disassem:disassemble-code-component o :stream s))
1826     (t
1827     (disassem:disassemble-memory
1828     (disassem::align
1829     (+ (logandc2 (kernel:get-lisp-obj-address o)
1830     vm:lowtag-mask)
1831     (* vm:code-constants-offset vm:word-bytes))
1832     (ash 1 vm:lowtag-bits))
1833     (ash (kernel:%code-code-size o) vm:word-shift)
1834     :stream s))))))))
1835    
1836     (defmethod inspect-for-emacs ((o kernel:fdefn) (inspector scl-inspector))
1837     (declare (ignore inspector))
1838     (values (format nil "~A is a fdenf object." o)
1839     (label-value-line*
1840     ("name" (kernel:fdefn-name o))
1841     ("function" (kernel:fdefn-function o))
1842     ("raw-addr" (sys:sap-ref-32
1843     (sys:int-sap (kernel:get-lisp-obj-address o))
1844     (* vm:fdefn-raw-addr-slot vm:word-bytes))))))
1845    
1846     (defmethod inspect-for-emacs ((o array) (inspector scl-inspector))
1847     inspector
1848 dcrosher 1.4 (cond ((kernel:array-header-p o)
1849     (values (format nil "~A is an array." o)
1850     (label-value-line*
1851     (:header (describe-primitive-type o))
1852     (:rank (array-rank o))
1853     (:fill-pointer (kernel:%array-fill-pointer o))
1854     (:fill-pointer-p (kernel:%array-fill-pointer-p o))
1855     (:elements (kernel:%array-available-elements o))
1856     (:data (kernel:%array-data-vector o))
1857     (:displacement (kernel:%array-displacement o))
1858     (:displaced-p (kernel:%array-displaced-p o))
1859     (:dimensions (array-dimensions o)))))
1860     (t
1861     (values (format nil "~A is an simple-array." o)
1862     (label-value-line*
1863     (:header (describe-primitive-type o))
1864     (:length (length o)))))))
1865 dcrosher 1.1
1866 dcrosher 1.3 (defmethod inspect-for-emacs ((o simple-vector) (inspector scl-inspector))
1867 dcrosher 1.1 inspector
1868     (values (format nil "~A is a vector." o)
1869     (append
1870     (label-value-line*
1871     (:header (describe-primitive-type o))
1872     (:length (c::vector-length o)))
1873     (unless (eq (array-element-type o) 'nil)
1874     (loop for i below (length o)
1875     append (label-value-line i (aref o i)))))))
1876    
1877     (defun inspect-alien-record (alien)
1878     (values
1879     (format nil "~A is an alien value." alien)
1880     (with-struct (alien::alien-value- sap type) alien
1881     (with-struct (alien::alien-record-type- kind name fields) type
1882     (append
1883     (label-value-line*
1884     (:sap sap)
1885     (:kind kind)
1886     (:name name))
1887     (loop for field in fields
1888     append (let ((slot (alien::alien-record-field-name field)))
1889     (label-value-line slot (alien:slot alien slot)))))))))
1890    
1891     (defun inspect-alien-pointer (alien)
1892     (values
1893     (format nil "~A is an alien value." alien)
1894     (with-struct (alien::alien-value- sap type) alien
1895     (label-value-line*
1896     (:sap sap)
1897     (:type type)
1898     (:to (alien::deref alien))))))
1899    
1900     (defun inspect-alien-value (alien)
1901     (typecase (alien::alien-value-type alien)
1902     (alien::alien-record-type (inspect-alien-record alien))
1903     (alien::alien-pointer-type (inspect-alien-pointer alien))
1904     (t (scl-inspect alien))))
1905    
1906     ;;;; Profiling
1907     (defimplementation profile (fname)
1908     (eval `(profile:profile ,fname)))
1909    
1910     (defimplementation unprofile (fname)
1911     (eval `(profile:unprofile ,fname)))
1912    
1913     (defimplementation unprofile-all ()
1914     (eval `(profile:unprofile))
1915     "All functions unprofiled.")
1916    
1917     (defimplementation profile-report ()
1918     (eval `(profile:report-time)))
1919    
1920     (defimplementation profile-reset ()
1921     (eval `(profile:reset-time))
1922     "Reset profiling counters.")
1923    
1924     (defimplementation profiled-functions ()
1925     profile:*timed-functions*)
1926    
1927     (defimplementation profile-package (package callers methods)
1928     (profile:profile-all :package package
1929     :callers-p callers
1930     #+nil :methods #+nil methods))
1931    
1932    
1933     ;;;; Multiprocessing
1934    
1935     (defimplementation spawn (fn &key (name "Anonymous"))
1936     (thread:thread-create fn :name name))
1937    
1938     (defvar *thread-id-counter* 0)
1939     (defvar *thread-id-counter-lock* (thread:make-lock "Thread ID counter"))
1940    
1941     (defimplementation thread-id (thread)
1942     (thread:with-lock-held (*thread-id-counter-lock*)
1943     (or (getf (thread:thread-plist thread) 'id)
1944     (setf (getf (thread:thread-plist thread) 'id)
1945     (incf *thread-id-counter*)))))
1946    
1947     (defimplementation find-thread (id)
1948     (thread:map-over-threads
1949     #'(lambda (thread)
1950     (when (eql (getf (thread:thread-plist thread) 'id) id)
1951     (return-from find-thread thread)))))
1952    
1953     (defimplementation thread-name (thread)
1954     (princ-to-string (thread:thread-name thread)))
1955    
1956     (defimplementation thread-status (thread)
1957     (let ((dynamic-values (thread::thread-dynamic-values thread)))
1958     (if (zerop dynamic-values) "Exited" "Running")))
1959    
1960     (defimplementation make-lock (&key name)
1961     (thread:make-lock name))
1962    
1963     (defimplementation call-with-lock-held (lock function)
1964     (declare (type function function))
1965     (thread:with-lock-held (lock) (funcall function)))
1966    
1967     (defimplementation current-thread ()
1968     thread:*thread*)
1969    
1970     (defimplementation all-threads ()
1971     (let ((all-threads nil))
1972     (thread:map-over-threads #'(lambda (thread) (push thread all-threads)))
1973     all-threads))
1974    
1975     (defimplementation interrupt-thread (thread fn)
1976     (thread:thread-interrupt thread #'(lambda ()
1977     (sys:with-interrupts
1978     (funcall fn)))))
1979    
1980     (defimplementation kill-thread (thread)
1981     (thread:destroy-thread thread))
1982    
1983     (defimplementation thread-alive-p (thread)
1984     (not (zerop (thread::thread-dynamic-values thread))))
1985    
1986     (defvar *mailbox-lock* (thread:make-lock "Mailbox lock"))
1987    
1988     (defstruct (mailbox)
1989     (lock (thread:make-lock "Thread mailbox" :type :error-check)
1990     :type thread:error-check-lock)
1991     (cond-var (thread:make-cond-var "Thread mailbox") :type thread:cond-var)
1992     (queue '() :type list))
1993    
1994     (defun mailbox (thread)
1995     "Return 'thread's mailbox."
1996     (thread:with-lock-held (*mailbox-lock*)
1997     (or (getf (thread:thread-plist thread) 'mailbox)
1998     (setf (getf (thread:thread-plist thread) 'mailbox) (make-mailbox)))))
1999    
2000     (defimplementation send (thread message)
2001     (let* ((mbox (mailbox thread))
2002     (lock (mailbox-lock mbox))
2003     (cond-var (mailbox-cond-var mbox)))
2004     (thread:with-lock-held (lock "Mailbox Send")
2005     (setf (mailbox-queue mbox) (nconc (mailbox-queue mbox) (list message)))
2006     (thread:cond-var-broadcast cond-var))
2007     message))
2008    
2009     (defimplementation receive ()
2010     (let* ((mbox (mailbox thread:*thread*))
2011     (lock (mailbox-lock mbox))
2012     (cond-var (mailbox-cond-var mbox)))
2013     (thread:with-lock-held (lock "Mailbox Receive")
2014     (loop
2015     (when (mailbox-queue mbox)
2016     (return (pop (mailbox-queue mbox))))
2017     (thread:cond-var-timedwait cond-var lock 10 "Mailbox receive wait")))))
2018    
2019    
2020    
2021     (defimplementation emacs-connected ())
2022    
2023    
2024     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2025     ;;Trace implementations
2026     ;; In SCL, we have:
2027     ;; (trace <name>)
2028     ;; (trace (method <name> <qualifier>? (<specializer>+)))
2029     ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2030     ;; <name> can be a normal name or a (setf name)
2031    
2032     (defun tracedp (spec)
2033     (member spec (eval '(trace)) :test #'equal))
2034    
2035     (defun toggle-trace-aux (spec &rest options)
2036     (cond ((tracedp spec)
2037     (eval `(untrace ,spec))
2038     (format nil "~S is now untraced." spec))
2039     (t
2040     (eval `(trace ,spec ,@options))
2041     (format nil "~S is now traced." spec))))
2042    
2043     (defimplementation toggle-trace (spec)
2044     (ecase (car spec)
2045     ((setf)
2046     (toggle-trace-aux spec))
2047     ((:defgeneric)
2048     (let ((name (second spec)))
2049     (toggle-trace-aux name :methods name)))
2050     ((:defmethod)
2051     nil)
2052     ((:call)
2053     (destructuring-bind (caller callee) (cdr spec)
2054     (toggle-trace-aux (process-fspec callee)
2055     :wherein (list (process-fspec caller)))))))
2056    
2057     (defun process-fspec (fspec)
2058     (cond ((consp fspec)
2059     (ecase (first fspec)
2060     ((:defun :defgeneric) (second fspec))
2061     ((:defmethod)
2062     `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2063     ;; this isn't actually supported
2064     ((:labels) `(labels ,(process-fspec (second fspec)) ,(third fspec)))
2065     ((:flet) `(flet ,(process-fspec (second fspec)) ,(third fspec)))))
2066     (t
2067     fspec)))
2068    
2069     ;;; Weak datastructures
2070    
2071     ;;; Not implemented in SCL.
2072     (defimplementation make-weak-key-hash-table (&rest args)
2073     (apply #'make-hash-table :weak-p t args))
2074    
2075     ;; Local Variables:
2076     ;; pbook-heading-regexp: "^;;;\\(;+\\)"
2077     ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2078     ;; End:

  ViewVC Help
Powered by ViewVC 1.1.5