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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Sat Feb 25 12:10:33 2006 UTC (8 years, 1 month ago) by dcrosher
Branch: MAIN
Changes since 1.3: +42 -14 lines
* swank-backend.lisp (operate-on-system): symbol case fix for
  SCL's lowercase mode.

* swak.lisp (setup-stream-indirection)
  (globally-redirect-io-to-connection)
  (revert-global-io-redirection): symbol case fixes.

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

  ViewVC Help
Powered by ViewVC 1.1.5