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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5