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

Contents of /slime/swank-scl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (show annotations)
Mon Aug 10 19:30:22 2009 UTC (4 years, 8 months ago) by heller
Branch: MAIN
Changes since 1.33: +6 -5 lines
Separate context info from compiler message text.

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

  ViewVC Help
Powered by ViewVC 1.1.5