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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5