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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (hide annotations)
Sun Feb 10 08:32:04 2008 UTC (6 years, 2 months ago) by heller
Branch: MAIN
Changes since 1.17: +0 -5 lines
Remove remaining traces of make-default-inspector.

* swank-scl.lisp (make-default-inspector, scl-inspector): Deleted.
* swank-lispworks.lisp (make-default-inspector)
(lispworks-inspector): Deleted.
1 dcrosher 1.1 ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2     ;;;
3     ;;; Scieneer Common Lisp code for SLIME.
4     ;;;
5     ;;; This code has been placed in the Public Domain. All warranties
6     ;;; are disclaimed.
7     ;;;
8    
9     (in-package :swank-backend)
10    
11    
12    
13     ;;; swank-mop
14    
15 dcrosher 1.7 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
16 dcrosher 1.1
17     (defun swank-mop:slot-definition-documentation (slot)
18 dcrosher 1.7 (documentation slot t))
19 dcrosher 1.1
20    
21     ;;;; TCP server
22     ;;;
23     ;;; SCL only supports the :spawn communication style.
24     ;;;
25    
26     (defimplementation preferred-communication-style ()
27     :spawn)
28    
29     (defimplementation create-socket (host port)
30     (let ((addr (resolve-hostname host)))
31     (ext:create-inet-listener port :stream :host addr :reuse-address t)))
32    
33     (defimplementation local-port (socket)
34     (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
35    
36     (defimplementation close-socket (socket)
37     (ext:close-socket (socket-fd socket)))
38    
39 heller 1.10 (defimplementation accept-connection (socket
40     &key external-format buffering timeout)
41 heller 1.12 (let ((external-format (or external-format :default))
42 heller 1.10 (buffering (or buffering :full))
43 dcrosher 1.6 (fd (socket-fd socket)))
44     (loop
45     (let ((ready (sys:wait-until-fd-usable fd :input timeout)))
46     (unless ready
47     (error "Timeout accepting connection on socket: ~S~%" socket)))
48     (let ((new-fd (ignore-errors (ext:accept-tcp-connection fd))))
49     (when new-fd
50     (return (make-socket-io-stream new-fd external-format buffering)))))))
51    
52     (defimplementation set-stream-timeout (stream timeout)
53     (check-type timeout (or null real))
54     (if (fboundp 'ext::stream-timeout)
55     (setf (ext::stream-timeout stream) timeout)
56 dcrosher 1.14 (setf (slot-value (slot-value stream 'lisp::stream) 'lisp::timeout)
57     timeout)))
58 dcrosher 1.1
59     ;;;;; Sockets
60    
61     (defun socket-fd (socket)
62     "Return the file descriptor for the socket represented by 'socket."
63     (etypecase socket
64     (fixnum socket)
65     (stream (sys:fd-stream-fd socket))))
66    
67     (defun resolve-hostname (hostname)
68     "Return the IP address of 'hostname as an integer (in host byte-order)."
69     (let ((hostent (ext:lookup-host-entry hostname)))
70     (car (ext:host-entry-addr-list hostent))))
71    
72 heller 1.12 (defvar *external-format-to-coding-system*
73     '((:iso-8859-1
74     "latin-1" "latin-1-unix" "iso-latin-1-unix"
75     "iso-8859-1" "iso-8859-1-unix")
76     (:utf-8 "utf-8" "utf-8-unix")
77     (:euc-jp "euc-jp" "euc-jp-unix")))
78    
79     (defimplementation find-external-format (coding-system)
80     (car (rassoc-if (lambda (x) (member coding-system x :test #'equal))
81     *external-format-to-coding-system*)))
82 dcrosher 1.1
83 dcrosher 1.3 (defun make-socket-io-stream (fd external-format buffering)
84 dcrosher 1.1 "Create a new input/output fd-stream for 'fd."
85 heller 1.12 (let* ((stream (sys:make-fd-stream fd :input t :output t
86 dcrosher 1.7 :element-type 'base-char
87     :buffering buffering
88     :external-format external-format)))
89     ;; Ignore character conversion errors. Without this the communication
90     ;; channel is prone to lockup if a character conversion error occurs.
91 dcrosher 1.14 (setf (lisp::character-conversion-stream-input-error-value stream) #\?)
92     (setf (lisp::character-conversion-stream-output-error-value stream) #\?)
93 dcrosher 1.7 stream))
94 dcrosher 1.1
95    
96     ;;;; Stream handling
97    
98     (defclass slime-input-stream (ext:character-input-stream)
99     ((buffer :initarg :buffer :type string)
100     (index :initarg :index :initform 0 :type fixnum)
101     (position :initarg :position :initform 0 :type integer)
102     (interactive :initarg :interactive :initform nil :type (member nil t))
103     (output-stream :initarg :output-stream :initform nil)
104     (input-fn :initarg :input-fn :type function)
105     ))
106    
107     (defun make-slime-input-stream (input-fn &optional output-stream)
108     (declare (function input-fn))
109     (make-instance 'slime-input-stream
110     :in-buffer (make-string 256)
111     :in-head 0 :in-tail 0
112     :out-buffer ""
113     :buffer "" :index 0
114     :input-fn input-fn
115     :output-stream output-stream))
116    
117     (defmethod print-object ((s slime-input-stream) stream)
118     (print-unreadable-object (s stream :type t)))
119    
120     ;;; input-stream-p inherits from input-stream.
121     ;;; output-stream-p inherits nil.
122    
123     (defmethod ext:stream-listen ((stream slime-input-stream))
124     (let* ((buffer (slot-value stream 'buffer))
125     (index (slot-value stream 'index))
126     (length (length buffer)))
127     (declare (type string buffer)
128     (fixnum index length))
129     (< index length)))
130    
131     (defmethod close ((stream slime-input-stream) &key ((:abort abort) nil))
132     (declare (ignore abort))
133     (when (ext:stream-open-p stream)
134     (setf (ext:stream-open-p stream) nil)
135     (setf (ext:stream-in-buffer stream) " ")
136     t))
137    
138     (defmethod ext:stream-clear-input ((stream slime-input-stream))
139     (let* ((input-buffer (slot-value stream 'buffer))
140     (index (slot-value stream 'index))
141     (input-length (length input-buffer))
142     (available (- input-length index))
143     (position (slot-value stream 'position))
144     (new-position (+ position available)))
145     (declare (type kernel:index index available position new-position))
146     (setf (slot-value stream 'position) new-position))
147     (setf (slot-value stream 'buffer) "")
148     (setf (slot-value stream 'index) 0)
149     nil)
150    
151     ;;; No 'stream-finish-output method.
152     ;;; No 'stream-force-output method.
153     ;;; No 'stream-clear-output method.
154    
155     ;;; stream-element-type inherits from character-stream.
156    
157     ;;; No 'stream-line-length method.
158     ;;; No 'stream-line-column method.
159    
160     ;;; Add the remaining input to the current position.
161     (defmethod file-length ((stream slime-input-stream))
162     (let* ((input-buffer (slot-value stream 'buffer))
163     (index (slot-value stream 'index))
164     (input-length (length input-buffer))
165     (available (- input-length index))
166     (position (slot-value stream 'position))
167     (file-length (+ position available)))
168     (declare (type kernel:index index available position file-length))
169     file-length))
170    
171     (defmethod ext:stream-file-position ((stream slime-input-stream)
172     &optional position)
173     (let ((current-position (slot-value stream 'position)))
174     (declare (type kernel:index current-position))
175     (cond (position
176     ;; Could make an attempt here, but just give up for now.
177     nil)
178     (t
179     current-position))))
180    
181     (defmethod interactive-stream-p ((stream slime-input-stream))
182     (slot-value stream 'interactive))
183    
184     ;;; No 'file-string-length method.
185    
186     (defmethod ext:stream-read-chars ((stream slime-input-stream) buffer
187     start requested waitp)
188     (declare (type simple-string buffer)
189     (type kernel:index start requested))
190     (let* ((input-buffer (slot-value stream 'buffer))
191     (index (slot-value stream 'index))
192     (input-length (length input-buffer))
193     (available (- input-length index))
194     (copy (min available requested)))
195     (declare (string input-buffer)
196     (type kernel:index index available copy))
197     (cond ((plusp copy)
198     (dotimes (i copy)
199     (declare (type kernel:index i))
200     (setf (aref buffer (+ start i)) (aref input-buffer (+ index i))))
201 dcrosher 1.5 (setf (slot-value stream 'index) (+ index copy))
202 dcrosher 1.1 (incf (slot-value stream 'position) copy)
203     copy)
204     (waitp
205     (let ((output-stream (slot-value stream 'output-stream))
206     (input-fn (slot-value stream 'input-fn)))
207     (declare (type function input-fn))
208     (when output-stream
209     (force-output output-stream))
210     (let ((new-input (funcall input-fn)))
211     (cond ((zerop (length new-input))
212     -1)
213     (t
214     (setf (slot-value stream 'buffer) new-input)
215     (setf (slot-value stream 'index) 0)
216 dcrosher 1.5 (ext:stream-read-chars stream buffer
217     start requested waitp))))))
218 dcrosher 1.1 (t
219     0))))
220    
221     ;;; Slime output stream.
222    
223     (defclass slime-output-stream (ext:character-output-stream)
224     ((output-fn :initarg :output-fn :type function)
225     (column :initform 0 :type kernel:index)
226     (interactive :initform nil :type (member nil t))
227     (position :initform 0 :type integer)))
228    
229     (defun make-slime-output-stream (output-fn)
230     (declare (function output-fn))
231     (make-instance 'slime-output-stream
232     :in-buffer ""
233     :out-buffer (make-string 256)
234     :output-fn output-fn))
235    
236     (defmethod print-object ((s slime-output-stream) stream)
237     (print-unreadable-object (s stream :type t)))
238    
239     ;;; Use default 'input-stream-p method for 'output-stream which returns 'nil.
240     ;;; Use default 'output-stream-p method for 'output-stream which returns 't.
241    
242     ;;; No 'stream-listen method.
243    
244     (defmethod close ((stream slime-output-stream) &key ((:abort abort) nil))
245     (when (ext:stream-open-p stream)
246     (unless abort
247     (finish-output stream))
248     (setf (ext:stream-open-p stream) nil)
249     (setf (ext:stream-out-buffer stream) " ")
250     t))
251    
252     ;;; No 'stream-clear-input method.
253    
254     (defmethod ext:stream-finish-output ((stream slime-output-stream))
255     nil)
256    
257     (defmethod ext:stream-force-output ((stream slime-output-stream))
258     nil)
259    
260     (defmethod ext:stream-clear-output ((stream slime-output-stream))
261     nil)
262    
263     ;;; Use default 'stream-element-type method for 'character-stream which
264     ;;; returns 'base-char.
265    
266     (defmethod ext:stream-line-length ((stream slime-output-stream))
267     80)
268    
269     (defmethod ext:stream-line-column ((stream slime-output-stream))
270     (slot-value stream 'column))
271    
272     (defmethod file-length ((stream slime-output-stream))
273     (slot-value stream 'position))
274    
275     (defmethod ext:stream-file-position ((stream slime-output-stream)
276     &optional position)
277     (declare (optimize (speed 3)))
278     (cond (position
279     (let* ((current-position (slot-value stream 'position))
280     (target-position (etypecase position
281     ((member :start) 0)
282     ((member :end) current-position)
283     (kernel:index position))))
284     (declare (type kernel:index current-position target-position))
285     (cond ((= target-position current-position)
286     t)
287     ((> target-position current-position)
288     (let ((output-fn (slot-value stream 'output-fn))
289     (fill-size (- target-position current-position)))
290     (declare (function output-fn))
291     (funcall output-fn (make-string fill-size
292     :initial-element #\space))
293     (setf (slot-value stream 'position) target-position))
294     t)
295     (t
296     nil))))
297     (t
298     (slot-value stream 'position))))
299    
300     (defmethod interactive-stream-p ((stream slime-output-stream))
301     (slot-value stream 'interactive))
302    
303     ;;; Use the default 'character-output-stream 'file-string-length method.
304    
305     ;;; stream-write-chars
306     ;;;
307     ;;; The stream out-buffer is typically large enough that there is little point
308     ;;; growing the stream output 'string large than the total size. For typical
309     ;;; usage this reduces consing. As the string grows larger then grow to
310     ;;; reduce the cost of copying strings around.
311     ;;;
312 dcrosher 1.8 (defmethod ext:stream-write-chars ((stream slime-output-stream)
313     string start end waitp)
314 dcrosher 1.1 (declare (simple-string string)
315 dcrosher 1.8 (type kernel:index start end)
316     (ignore waitp))
317 dcrosher 1.1 (declare (optimize (speed 3)))
318     (unless (ext:stream-open-p stream)
319     (error 'kernel:simple-stream-error
320     :stream stream
321     :format-control "Stream closed."))
322     (let* ((string-length (length string))
323     (start (cond ((< start 0) 0)
324     ((> start string-length) string-length)
325     (t start)))
326     (end (cond ((< end start) start)
327     ((> end string-length) string-length)
328     (t end)))
329     (length (- end start))
330     (output-fn (slot-value stream 'output-fn)))
331     (declare (type kernel:index start end length)
332     (type function output-fn))
333     (unless (zerop length)
334     (funcall output-fn (subseq string start end))
335     (let ((last-newline (position #\newline string :from-end t
336     :start start :end end)))
337     (setf (slot-value stream 'column)
338     (if last-newline
339     (- end last-newline 1)
340     (let ((column (slot-value stream 'column)))
341     (declare (type kernel:index column))
342     (+ column (- end start))))))))
343 dcrosher 1.8 (- end start))
344 dcrosher 1.1
345     ;;;
346    
347     (defimplementation make-fn-streams (input-fn output-fn)
348     (let* ((output (make-slime-output-stream output-fn))
349     (input (make-slime-input-stream input-fn output)))
350     (values input output)))
351    
352     (defimplementation make-stream-interactive (stream)
353     (when (or (typep stream 'slime-input-stream)
354     (typep stream 'slime-output-stream))
355     (setf (slot-value stream 'interactive) t)))
356    
357    
358     ;;;; Compilation Commands
359    
360     (defvar *previous-compiler-condition* nil
361     "Used to detect duplicates.")
362    
363     (defvar *previous-context* nil
364     "Previous compiler error context.")
365    
366     (defvar *buffer-name* nil
367     "The name of the Emacs buffer we are compiling from.
368     Nil if we aren't compiling from a buffer.")
369    
370     (defvar *buffer-start-position* nil)
371     (defvar *buffer-substring* nil)
372    
373     (defimplementation call-with-compilation-hooks (function)
374     (let ((*previous-compiler-condition* nil)
375     (*previous-context* nil)
376     (*print-readably* nil))
377     (handler-bind ((c::compiler-error #'handle-notification-condition)
378     (c::style-warning #'handle-notification-condition)
379     (c::warning #'handle-notification-condition))
380     (funcall function))))
381    
382 heller 1.12 (defimplementation swank-compile-file (filename load-p external-format)
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 dcrosher 1.1
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 heller 1.10 (:unknown :unknown))))))
1172 dcrosher 1.1
1173     ;;;;; Argument lists
1174    
1175 heller 1.10 (defimplementation arglist (fun)
1176 dcrosher 1.11 (etypecase fun
1177     (function (function-arglist fun))
1178     (symbol (function-arglist (or (macro-function fun)
1179     (symbol-function fun))))))
1180 dcrosher 1.1
1181 dcrosher 1.11 (defun function-arglist (fun)
1182 dcrosher 1.1 (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 heller 1.10 #+nil
1588 dcrosher 1.1 (defimplementation condition-extras ((c breakpoint))
1589     ;; simply pop up the source buffer
1590     `((:short-frame-source 0)))
1591    
1592     (defun signal-breakpoint (breakpoint frame)
1593     "Signal a breakpoint condition for BREAKPOINT in FRAME.
1594     Try to create a informative message."
1595     (flet ((brk (values fstring &rest args)
1596     (let ((msg (apply #'format nil fstring args))
1597     (debug:*stack-top-hint* frame))
1598     (break 'breakpoint :message msg :values values))))
1599     (with-struct (di::breakpoint- kind what) breakpoint
1600     (case kind
1601     (:code-location
1602     (case (di:code-location-kind what)
1603     ((:single-value-return :known-return :unknown-return)
1604     (let ((values (breakpoint-values breakpoint)))
1605     (brk values "Return value: ~{~S ~}" values)))
1606     (t
1607     #+(or)
1608     (when (eq (di:code-location-kind what) :call-site)
1609     (call-site-function breakpoint frame))
1610     (brk nil "Breakpoint: ~S ~S"
1611     (di:code-location-kind what)
1612     (di::compiled-code-location-pc what)))))
1613     (:function-start
1614     (brk nil "Function start breakpoint"))
1615     (t (brk nil "Breakpoint: ~A in ~A" breakpoint frame))))))
1616    
1617     #+nil
1618     (defimplementation sldb-break-at-start (fname)
1619     (let ((debug-fun (di:function-debug-function (coerce fname 'function))))
1620     (cond ((not debug-fun)
1621     `(:error ,(format nil "~S has no debug-function" fname)))
1622     (t
1623     (flet ((hook (frame bp &optional args cookie)
1624     (declare (ignore args cookie))
1625     (signal-breakpoint bp frame)))
1626     (let ((bp (di:make-breakpoint #'hook debug-fun
1627     :kind :function-start)))
1628     (di:activate-breakpoint bp)
1629     `(:ok ,(format nil "Set breakpoint in ~S" fname))))))))
1630    
1631     (defun frame-cfp (frame)
1632     "Return the Control-Stack-Frame-Pointer for FRAME."
1633     (etypecase frame
1634     (di::compiled-frame (di::frame-pointer frame))
1635     ((or di::interpreted-frame null) -1)))
1636    
1637     (defun frame-ip (frame)
1638     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1639     (if (not frame)
1640     -1
1641     (let ((debug-fun (di::frame-debug-function frame)))
1642     (etypecase debug-fun
1643     (di::compiled-debug-function
1644     (let* ((code-loc (di:frame-code-location frame))
1645     (component (di::compiled-debug-function-component debug-fun))
1646     (pc (di::compiled-code-location-pc code-loc))
1647     (ip (sys:without-gcing
1648     (sys:sap-int
1649     (sys:sap+ (kernel:code-instructions component) pc)))))
1650     (values ip pc)))
1651     ((or di::bogus-debug-function di::interpreted-debug-function)
1652     -1)))))
1653    
1654     (defun frame-registers (frame)
1655     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1656     (let* ((cfp (frame-cfp frame))
1657     (csp (frame-cfp (di::frame-up frame)))
1658     (ip (frame-ip frame))
1659     (ocfp (frame-cfp (di::frame-down frame)))
1660     (lra (frame-ip (di::frame-down frame))))
1661     (values csp cfp ip ocfp lra)))
1662    
1663     (defun print-frame-registers (frame-number)
1664     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1665     (flet ((fixnum (p) (etypecase p
1666     (integer p)
1667     (sys:system-area-pointer (sys:sap-int p)))))
1668     (apply #'format t "~
1669     CSP = ~X
1670     CFP = ~X
1671     IP = ~X
1672     OCFP = ~X
1673     LRA = ~X~%" (mapcar #'fixnum
1674     (multiple-value-list (frame-registers frame)))))))
1675    
1676    
1677     (defimplementation disassemble-frame (frame-number)
1678     "Return a string with the disassembly of frames code."
1679     (print-frame-registers frame-number)
1680     (terpri)
1681     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1682     (debug-fun (di::frame-debug-function frame)))
1683     (etypecase debug-fun
1684     (di::compiled-debug-function
1685     (let* ((component (di::compiled-debug-function-component debug-fun))
1686     (fun (di:debug-function-function debug-fun)))
1687     (if fun
1688     (disassemble fun)
1689     (disassem:disassemble-code-component component))))
1690     (di::bogus-debug-function
1691     (format t "~%[Disassembling bogus frames not implemented]")))))
1692    
1693    
1694     ;;;; Inspecting
1695    
1696     (defconstant +lowtag-symbols+
1697     '(vm:even-fixnum-type
1698     vm:instance-pointer-type
1699     vm:other-immediate-0-type
1700     vm:list-pointer-type
1701     vm:odd-fixnum-type
1702     vm:function-pointer-type
1703     vm:other-immediate-1-type
1704     vm:other-pointer-type)
1705     "Names of the constants that specify type tags.
1706     The `symbol-value' of each element is a type tag.")
1707    
1708     (defconstant +header-type-symbols+
1709     (labels ((suffixp (suffix string)
1710     (and (>= (length string) (length suffix))
1711     (string= string suffix :start1 (- (length string)
1712     (length suffix)))))
1713     (header-type-symbol-p (x)
1714     (and (suffixp (symbol-name '#:-type) (symbol-name x))
1715     (not (member x +lowtag-symbols+))
1716     (boundp x)
1717     (typep (symbol-value x) 'fixnum))))
1718     (remove-if-not #'header-type-symbol-p
1719     (append (apropos-list (symbol-name '#:-type) :vm)
1720     (apropos-list (symbol-name '#:-type) :bignum))))
1721     "A list of names of the type codes in boxed objects.")
1722    
1723     (defimplementation describe-primitive-type (object)
1724     (with-output-to-string (*standard-output*)
1725     (let* ((lowtag (kernel:get-lowtag object))
1726     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1727     (format t "lowtag: ~A" lowtag-symbol)
1728     (when (member lowtag (list vm:other-pointer-type
1729     vm:function-pointer-type
1730     vm:other-immediate-0-type
1731     vm:other-immediate-1-type
1732     ))
1733     (let* ((type (kernel:get-type object))
1734     (type-symbol (find type +header-type-symbols+
1735     :key #'symbol-value)))
1736     (format t ", type: ~A" type-symbol))))))
1737    
1738 heller 1.16 (defmethod emacs-inspect ((o t))
1739 dcrosher 1.1 (cond ((di::indirect-value-cell-p o)
1740 heller 1.17 `("Value: " (:value ,(c:value-cell-ref o))))
1741 dcrosher 1.1 ((alien::alien-value-p o)
1742     (inspect-alien-value o))
1743     (t
1744     (scl-inspect o))))
1745    
1746     (defun scl-inspect (o)
1747 dcrosher 1.4 (destructuring-bind (text labeledp . parts)
1748     (inspect::describe-parts o)
1749 heller 1.17 (list* (format nil "~A~%" text)
1750 dcrosher 1.1 (if labeledp
1751     (loop for (label . value) in parts
1752     append (label-value-line label value))
1753     (loop for value in parts for i from 0
1754     append (label-value-line i value))))))
1755    
1756 heller 1.16 (defmethod emacs-inspect ((o function))
1757 dcrosher 1.1 (let ((header (kernel:get-type o)))
1758     (cond ((= header vm:function-header-type)
1759 heller 1.17 (list* (format nil "~A is a function.~%" o)
1760 dcrosher 1.1 (append (label-value-line*
1761     ("Self" (kernel:%function-self o))
1762     ("Next" (kernel:%function-next o))
1763     ("Name" (kernel:%function-name o))
1764     ("Arglist" (kernel:%function-arglist o))
1765     ("Type" (kernel:%function-type o))
1766     ("Code" (kernel:function-code-header o)))
1767     (list
1768     (with-output-to-string (s)
1769     (disassem:disassemble-function o :stream s))))))
1770     ((= header vm:closure-header-type)
1771 heller 1.17 (list* (format nil "~A is a closure.~%" o)
1772 dcrosher 1.1 (append
1773     (label-value-line "Function" (kernel:%closure-function o))
1774     `("Environment:" (:newline))
1775 dcrosher 1.7 (loop for i from 0 below (- (kernel:get-closure-length o)
1776     (1- vm:closure-info-offset))
1777 dcrosher 1.1 append (label-value-line
1778     i (kernel:%closure-index-ref o i))))))
1779     ((eval::interpreted-function-p o)
1780     (scl-inspect o))
1781     (t
1782     (call-next-method)))))
1783    
1784    
1785 heller 1.16 (defmethod emacs-inspect ((o kernel:code-component))
1786 dcrosher 1.1 (append
1787     (label-value-line*
1788     ("code-size" (kernel:%code-code-size o))
1789     ("entry-points" (kernel:%code-entry-points o))
1790     ("debug-info" (kernel:%code-debug-info o))
1791     ("trace-table-offset" (kernel:code-header-ref
1792     o vm:code-trace-table-offset-slot)))
1793     `("Constants:" (:newline))
1794     (loop for i from vm:code-constants-offset
1795     below (kernel:get-header-data o)
1796     append (label-value-line i (kernel:code-header-ref o i)))
1797     `("Code:" (:newline)
1798     , (with-output-to-string (s)
1799     (cond ((kernel:%code-debug-info o)
1800     (disassem:disassemble-code-component o :stream s))
1801     (t
1802     (disassem:disassemble-memory
1803     (disassem::align
1804     (+ (logandc2 (kernel:get-lisp-obj-address o)
1805     vm:lowtag-mask)
1806     (* vm:code-constants-offset vm:word-bytes))
1807     (ash 1 vm:lowtag-bits))
1808     (ash (kernel:%code-code-size o) vm:word-shift)
1809 heller 1.17 :stream s)))))))
1810 dcrosher 1.1
1811 heller 1.16 (defmethod emacs-inspect ((o kernel:fdefn))
1812 heller 1.17 (label-value-line*
1813 dcrosher 1.1 ("name" (kernel:fdefn-name o))
1814     ("function" (kernel:fdefn-function o))
1815     ("raw-addr" (sys:sap-ref-32
1816     (sys:int-sap (kernel:get-lisp-obj-address o))
1817 heller 1.17 (* vm:fdefn-raw-addr-slot vm:word-bytes)))))
1818 dcrosher 1.1
1819 heller 1.16 (defmethod emacs-inspect ((o array))
1820 dcrosher 1.4 (cond ((kernel:array-header-p o)
1821 heller 1.17 (list* (format nil "~A is an array.~%" o)
1822 dcrosher 1.4 (label-value-line*
1823     (:header (describe-primitive-type o))
1824     (:rank (array-rank o))
1825     (:fill-pointer (kernel:%array-fill-pointer o))
1826     (:fill-pointer-p (kernel:%array-fill-pointer-p o))
1827     (:elements (kernel:%array-available-elements o))
1828     (:data (kernel:%array-data-vector o))
1829     (:displacement (kernel:%array-displacement o))
1830     (:displaced-p (kernel:%array-displaced-p o))
1831     (:dimensions (array-dimensions o)))))
1832     (t
1833 heller 1.17 (list* (format nil "~A is an simple-array.~%" o)
1834 dcrosher 1.4 (label-value-line*
1835     (:header (describe-primitive-type o))
1836     (:length (length o)))))))
1837 dcrosher 1.1
1838 heller 1.16 (defmethod emacs-inspect ((o simple-vector))
1839 heller 1.17 (list* (format nil "~A is a vector.~%" o)
1840 dcrosher 1.1 (append
1841     (label-value-line*
1842     (:header (describe-primitive-type o))
1843     (:length (c::vector-length o)))
1844     (unless (eq (array-element-type o) 'nil)
1845     (loop for i below (length o)
1846     append (label-value-line i (aref o i)))))))
1847    
1848     (defun inspect-alien-record (alien)
1849     (with-struct (alien::alien-value- sap type) alien
1850     (with-struct (alien::alien-record-type- kind name fields) type
1851     (append
1852     (label-value-line*
1853     (:sap sap)
1854     (:kind kind)
1855     (:name name))
1856     (loop for field in fields
1857     append (let ((slot (alien::alien-record-field-name field)))
1858 heller 1.17 (label-value-line slot (alien:slot alien slot))))))))
1859 dcrosher 1.1
1860     (defun inspect-alien-pointer (alien)
1861 heller 1.17 (with-struct (alien::alien-value- sap type) alien
1862 dcrosher 1.1 (label-value-line*
1863     (:sap sap)
1864     (:type type)
1865 heller 1.17 (:to (alien::deref alien)))))
1866 dcrosher 1.1
1867     (defun inspect-alien-value (alien)
1868     (typecase (alien::alien-value-type alien)
1869     (alien::alien-record-type (inspect-alien-record alien))
1870     (alien::alien-pointer-type (inspect-alien-pointer alien))
1871     (t (scl-inspect alien))))
1872    
1873     ;;;; Profiling
1874     (defimplementation profile (fname)
1875     (eval `(profile:profile ,fname)))
1876    
1877     (defimplementation unprofile (fname)
1878     (eval `(profile:unprofile ,fname)))
1879    
1880     (defimplementation unprofile-all ()
1881     (eval `(profile:unprofile))
1882     "All functions unprofiled.")
1883    
1884     (defimplementation profile-report ()
1885     (eval `(profile:report-time)))
1886    
1887     (defimplementation profile-reset ()
1888     (eval `(profile:reset-time))
1889     "Reset profiling counters.")
1890    
1891     (defimplementation profiled-functions ()
1892     profile:*timed-functions*)
1893    
1894     (defimplementation profile-package (package callers methods)
1895     (profile:profile-all :package package
1896     :callers-p callers
1897     #+nil :methods #+nil methods))
1898    
1899    
1900     ;;;; Multiprocessing
1901    
1902 dcrosher 1.11 (defimplementation spawn (fn &key name)
1903     (thread:thread-create fn :name (or name "Anonymous")))
1904 dcrosher 1.1
1905     (defvar *thread-id-counter* 0)
1906     (defvar *thread-id-counter-lock* (thread:make-lock "Thread ID counter"))
1907    
1908     (defimplementation thread-id (thread)
1909     (thread:with-lock-held (*thread-id-counter-lock*)
1910     (or (getf (thread:thread-plist thread) 'id)
1911     (setf (getf (thread:thread-plist thread) 'id)
1912     (incf *thread-id-counter*)))))
1913    
1914     (defimplementation find-thread (id)
1915 heller 1.10 (block find-thread
1916     (thread:map-over-threads
1917     #'(lambda (thread)
1918     (when (eql (getf (thread:thread-plist thread) 'id) id)
1919     (return-from find-thread thread))))))
1920 dcrosher 1.1
1921     (defimplementation thread-name (thread)
1922     (princ-to-string (thread:thread-name thread)))
1923    
1924     (defimplementation thread-status (thread)
1925     (let ((dynamic-values (thread::thread-dynamic-values thread)))
1926     (if (zerop dynamic-values) "Exited" "Running")))
1927    
1928     (defimplementation make-lock (&key name)
1929     (thread:make-lock name))
1930    
1931     (defimplementation call-with-lock-held (lock function)
1932     (declare (type function function))
1933     (thread:with-lock-held (lock) (funcall function)))
1934    
1935     (defimplementation current-thread ()
1936     thread:*thread*)
1937    
1938     (defimplementation all-threads ()
1939     (let ((all-threads nil))
1940     (thread:map-over-threads #'(lambda (thread) (push thread all-threads)))
1941     all-threads))
1942    
1943     (defimplementation interrupt-thread (thread fn)
1944     (thread:thread-interrupt thread #'(lambda ()
1945     (sys:with-interrupts
1946     (funcall fn)))))
1947    
1948     (defimplementation kill-thread (thread)
1949     (thread:destroy-thread thread))
1950    
1951     (defimplementation thread-alive-p (thread)
1952     (not (zerop (thread::thread-dynamic-values thread))))
1953    
1954     (defvar *mailbox-lock* (thread:make-lock "Mailbox lock"))
1955    
1956     (defstruct (mailbox)
1957 dcrosher 1.7 (lock (thread:make-lock "Thread mailbox" :type :error-check
1958     :interruptible nil)
1959 dcrosher 1.1 :type thread:error-check-lock)
1960     (queue '() :type list))
1961    
1962     (defun mailbox (thread)
1963     "Return 'thread's mailbox."
1964     (thread:with-lock-held (*mailbox-lock*)
1965     (or (getf (thread:thread-plist thread) 'mailbox)
1966     (setf (getf (thread:thread-plist thread) 'mailbox) (make-mailbox)))))
1967    
1968     (defimplementation send (thread message)
1969     (let* ((mbox (mailbox thread))
1970 dcrosher 1.7 (lock (mailbox-lock mbox)))
1971     (sys:without-interrupts
1972     (thread:with-lock-held (lock "Mailbox Send")
1973     (setf (mailbox-queue mbox) (nconc (mailbox-queue mbox)
1974     (list message)))))
1975     (mp:process-wakeup thread)
1976 dcrosher 1.1 message))
1977    
1978     (defimplementation receive ()
1979     (let* ((mbox (mailbox thread:*thread*))
1980 dcrosher 1.7 (lock (mailbox-lock mbox)))
1981     (loop
1982     (mp:process-wait-with-timeout "Mailbox read wait" 1
1983     #'(lambda () (mailbox-queue mbox)))
1984     (multiple-value-bind (message winp)
1985     (sys:without-interrupts
1986     (mp:with-lock-held (lock "Mailbox read")
1987     (let ((queue (mailbox-queue mbox)))
1988     (cond (queue
1989     (setf (mailbox-queue mbox) (cdr queue))
1990     (values (car queue) t))
1991     (t
1992     (values nil nil))))))
1993     (when winp
1994     (return message))))))
1995 dcrosher 1.1
1996    
1997    
1998     (defimplementation emacs-connected ())
1999    
2000    
2001     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2002     ;;Trace implementations
2003     ;; In SCL, we have:
2004     ;; (trace <name>)
2005     ;; (trace (method <name> <qualifier>? (<specializer>+)))
2006     ;; (trace :methods t '<name>) ;;to trace all methods of the gf <name>
2007     ;; <name> can be a normal name or a (setf name)
2008    
2009     (defun tracedp (spec)
2010     (member spec (eval '(trace)) :test #'equal))
2011    
2012     (defun toggle-trace-aux (spec &rest options)
2013     (cond ((tracedp spec)
2014     (eval `(untrace ,spec))
2015     (format nil "~S is now untraced." spec))
2016     (t
2017     (eval `(trace ,spec ,@options))
2018     (format nil "~S is now traced." spec))))
2019    
2020     (defimplementation toggle-trace (spec)
2021     (ecase (car spec)
2022     ((setf)
2023     (toggle-trace-aux spec))
2024     ((:defgeneric)
2025     (let ((name (second spec)))
2026     (toggle-trace-aux name :methods name)))
2027     ((:defmethod)
2028     nil)
2029     ((:call)
2030     (destructuring-bind (caller callee) (cdr spec)
2031     (toggle-trace-aux (process-fspec callee)
2032     :wherein (list (process-fspec caller)))))))
2033    
2034     (defun process-fspec (fspec)
2035     (cond ((consp fspec)
2036     (ecase (first fspec)
2037     ((:defun :defgeneric) (second fspec))
2038     ((:defmethod)
2039     `(method ,(second fspec) ,@(third fspec) ,(fourth fspec)))
2040     ;; this isn't actually supported
2041     ((:labels) `(labels ,(process-fspec (second fspec)) ,(third fspec)))
2042     ((:flet) `(flet ,(process-fspec (second fspec)) ,(third fspec)))))
2043     (t
2044     fspec)))
2045    
2046     ;;; Weak datastructures
2047    
2048     ;;; Not implemented in SCL.
2049     (defimplementation make-weak-key-hash-table (&rest args)
2050     (apply #'make-hash-table :weak-p t args))
2051    
2052     ;; Local Variables:
2053     ;; pbook-heading-regexp: "^;;;\\(;+\\)"
2054     ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
2055     ;; End:

  ViewVC Help
Powered by ViewVC 1.1.5