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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5