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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5