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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5