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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.88 - (show annotations)
Fri Mar 12 21:12:57 2004 UTC (10 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.87: +50 -51 lines
(find-definitions): Allow names (setf car).
1 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2
3 (declaim (optimize (debug 2)))
4
5 (in-package :swank-backend)
6
7 (in-package :lisp)
8
9 ;; Fix for read-sequence in 18e
10 #+cmu18e
11 (progn
12 (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
13 (when s
14 (setf (symbol-value s) nil)))
15
16 (defun read-into-simple-string (s stream start end)
17 (declare (type simple-string s))
18 (declare (type stream stream))
19 (declare (type index start end))
20 (unless (subtypep (stream-element-type stream) 'character)
21 (error 'type-error
22 :datum (read-char stream nil #\Null)
23 :expected-type (stream-element-type stream)
24 :format-control "Trying to read characters from a binary stream."))
25 ;; Let's go as low level as it seems reasonable.
26 (let* ((numbytes (- end start))
27 (total-bytes 0))
28 ;; read-n-bytes may return fewer bytes than requested, so we need
29 ;; to keep trying.
30 (loop while (plusp numbytes) do
31 (let ((bytes-read (system:read-n-bytes stream s start numbytes nil)))
32 (when (zerop bytes-read)
33 (return-from read-into-simple-string total-bytes))
34 (incf total-bytes bytes-read)
35 (incf start bytes-read)
36 (decf numbytes bytes-read)))
37 total-bytes))
38
39 (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
40 (when s
41 (setf (symbol-value s) t)))
42
43 )
44
45 (in-package :swank-backend)
46
47
48 ;;;; TCP server.
49
50 (defimplementation preferred-communication-style ()
51 :sigio)
52
53 (defimplementation create-socket (host port)
54 (ext:create-inet-listener port :stream
55 :reuse-address t
56 :host (resolve-hostname host)))
57
58 (defimplementation local-port (socket)
59 (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
60
61 (defimplementation close-socket (socket)
62 (ext:close-socket (socket-fd socket)))
63
64 (defimplementation accept-connection (socket)
65 #+MP (mp:process-wait-until-fd-usable socket :input)
66 (make-socket-io-stream (ext:accept-tcp-connection socket)))
67
68 (defvar *sigio-handlers* '()
69 "List of (key . (fn . args)) pairs to be called on SIGIO.")
70
71 (defun sigio-handler (signal code scp)
72 (declare (ignore signal code scp))
73 (mapc (lambda (handler) (funcall (cdr handler))) *sigio-handlers*))
74
75 (defun set-sigio-handler ()
76 (sys:enable-interrupt unix:SIGIO (lambda (signal code scp)
77 (sigio-handler signal code scp))))
78
79 (defun fcntl (fd command arg)
80 (multiple-value-bind (ok error) (unix:unix-fcntl fd command arg)
81 (cond (ok)
82 (t (error "fcntl: ~A" (unix:get-unix-error-msg error))))))
83
84 (defimplementation add-sigio-handler (socket fn)
85 (set-sigio-handler)
86 (let ((fd (socket-fd socket)))
87 (format *debug-io* "; Adding input handler: ~S ~%" fd)
88 (fcntl fd unix:f-setown (unix:unix-getpid))
89 (fcntl fd unix:f-setfl unix:FASYNC)
90 (push (cons fd fn) *sigio-handlers*)))
91
92 (defimplementation remove-sigio-handlers (socket)
93 (let ((fd (socket-fd socket)))
94 (setf *sigio-handlers* (delete fd *sigio-handlers* :key #'car))
95 (sys:invalidate-descriptor fd))
96 (close socket))
97
98 (defimplementation add-fd-handler (socket fn)
99 (let ((fd (socket-fd socket)))
100 (format *debug-io* "; Adding fd handler: ~S ~%" fd)
101 (sys:add-fd-handler fd :input (lambda (_)
102 _
103 (funcall fn)))))
104
105 (defimplementation remove-fd-handlers (socket)
106 (sys:invalidate-descriptor (socket-fd socket)))
107
108 (defimplementation make-fn-streams (input-fn output-fn)
109 (let* ((output (make-slime-output-stream output-fn))
110 (input (make-slime-input-stream input-fn output)))
111 (values input output)))
112
113 ;;;;; Socket helpers.
114
115 (defun socket-fd (socket)
116 "Return the filedescriptor for the socket represented by SOCKET."
117 (etypecase socket
118 (fixnum socket)
119 (sys:fd-stream (sys:fd-stream-fd socket))))
120
121 (defun resolve-hostname (hostname)
122 "Return the IP address of HOSTNAME as an integer."
123 (let* ((hostent (ext:lookup-host-entry hostname))
124 (address (car (ext:host-entry-addr-list hostent))))
125 (ext:htonl address)))
126
127 (defun make-socket-io-stream (fd)
128 "Create a new input/output fd-stream for FD."
129 (sys:make-fd-stream fd :input t :output t :element-type 'base-char))
130
131 (defun set-fd-non-blocking (fd)
132 (flet ((fcntl (fd cmd arg)
133 (multiple-value-bind (flags errno) (unix:unix-fcntl fd cmd arg)
134 (or flags
135 (error "fcntl: ~A" (unix:get-unix-error-msg errno))))))
136 (let ((flags (fcntl fd unix:F-GETFL 0)))
137 (fcntl fd unix:F-SETFL (logior flags unix:O_NONBLOCK)))))
138
139
140 ;;;; Unix signals
141
142 (defmethod call-without-interrupts (fn)
143 (sys:without-interrupts (funcall fn)))
144
145 (defimplementation getpid ()
146 (unix:unix-getpid))
147
148 (defimplementation lisp-implementation-type-name ()
149 "cmucl")
150
151
152 ;;;; Stream handling
153
154 (defstruct (slime-output-stream
155 (:include lisp::lisp-stream
156 (lisp::misc #'sos/misc)
157 (lisp::out #'sos/out)
158 (lisp::sout #'sos/sout))
159 (:conc-name sos.)
160 (:print-function %print-slime-output-stream)
161 (:constructor make-slime-output-stream (output-fn)))
162 (output-fn nil :type function)
163 (buffer (make-string 512) :type string)
164 (index 0 :type kernel:index)
165 (column 0 :type kernel:index))
166
167 (defun %print-slime-output-stream (s stream d)
168 (declare (ignore d))
169 (print-unreadable-object (s stream :type t :identity t)))
170
171 (defun sos/out (stream char)
172 (let ((buffer (sos.buffer stream))
173 (index (sos.index stream)))
174 (setf (schar buffer index) char)
175 (setf (sos.index stream) (1+ index))
176 (incf (sos.column stream))
177 (when (char= #\newline char)
178 (setf (sos.column stream) 0))
179 (when (= index (1- (length buffer)))
180 (force-output stream)))
181 char)
182
183 (defun sos/sout (stream string start end)
184 (loop for i from start below end
185 do (sos/out stream (aref string i))))
186
187 (defun sos/misc (stream operation &optional arg1 arg2)
188 (declare (ignore arg1 arg2))
189 (case operation
190 ((:force-output :finish-output)
191 (let ((end (sos.index stream)))
192 (unless (zerop end)
193 (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
194 (setf (sos.index stream) 0))))
195 (:charpos (sos.column stream))
196 (:line-length 75)
197 (:file-position nil)
198 (:element-type 'base-char)
199 (:get-command nil)
200 (:close nil)
201 (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
202
203 (defstruct (slime-input-stream
204 (:include string-stream
205 (lisp::in #'sis/in)
206 (lisp::misc #'sis/misc))
207 (:conc-name sis.)
208 (:print-function %print-slime-output-stream)
209 (:constructor make-slime-input-stream (input-fn sos)))
210 (input-fn nil :type function)
211 ;; We know our sibling output stream, so that we can force it before
212 ;; requesting input.
213 (sos nil :type slime-output-stream)
214 (buffer "" :type string)
215 (index 0 :type kernel:index))
216
217 (defun sis/in (stream eof-errorp eof-value)
218 (declare (ignore eof-errorp eof-value))
219 (let ((index (sis.index stream))
220 (buffer (sis.buffer stream)))
221 (when (= index (length buffer))
222 (force-output (sis.sos stream))
223 (setf buffer (funcall (sis.input-fn stream)))
224 (setf (sis.buffer stream) buffer)
225 (setf index 0))
226 (prog1 (aref buffer index)
227 (setf (sis.index stream) (1+ index)))))
228
229 (defun sis/misc (stream operation &optional arg1 arg2)
230 (declare (ignore arg2))
231 (ecase operation
232 (:file-position nil)
233 (:file-length nil)
234 (:unread (setf (aref (sis.buffer stream)
235 (decf (sis.index stream)))
236 arg1))
237 (:clear-input
238 (setf (sis.index stream) 0
239 (sis.buffer stream) ""))
240 (:listen (< (sis.index stream) (length (sis.buffer stream))))
241 (:charpos nil)
242 (:line-length nil)
243 (:get-command nil)
244 (:element-type 'base-char)
245 (:close nil)))
246
247
248 ;;;; Compilation Commands
249
250 (defvar *previous-compiler-condition* nil
251 "Used to detect duplicates.")
252
253 (defvar *previous-context* nil
254 "Previous compiler error context.")
255
256 (defvar *buffer-name* nil)
257 (defvar *buffer-start-position* nil)
258 (defvar *buffer-substring* nil)
259
260
261 ;;;;; Trapping notes
262
263 (defun handle-notification-condition (condition)
264 "Handle a condition caused by a compiler warning.
265 This traps all compiler conditions at a lower-level than using
266 C:*COMPILER-NOTIFICATION-FUNCTION*. The advantage is that we get to
267 craft our own error messages, which can omit a lot of redundant
268 information."
269 (unless (eq condition *previous-compiler-condition*)
270 (let ((context (c::find-error-context nil)))
271 (setq *previous-compiler-condition* condition)
272 (setq *previous-context* context)
273 (signal-compiler-condition condition context))))
274
275 (defun signal-compiler-condition (condition context)
276 (signal (make-condition
277 'compiler-condition
278 :original-condition condition
279 :severity (severity-for-emacs condition)
280 :short-message (brief-compiler-message-for-emacs condition)
281 :message (long-compiler-message-for-emacs condition context)
282 :location (compiler-note-location context))))
283
284 (defun severity-for-emacs (condition)
285 "Return the severity of CONDITION."
286 (etypecase condition
287 (c::compiler-error :error)
288 (c::style-warning :note)
289 (c::warning :warning)))
290
291 (defun brief-compiler-message-for-emacs (condition)
292 "Briefly describe a compiler error for Emacs.
293 When Emacs presents the message it already has the source popped up
294 and the source form highlighted. This makes much of the information in
295 the error-context redundant."
296 (princ-to-string condition))
297
298 (defun long-compiler-message-for-emacs (condition error-context)
299 "Describe a compiler error for Emacs including context information."
300 (declare (type (or c::compiler-error-context null) error-context))
301 (multiple-value-bind (enclosing source)
302 (if error-context
303 (values (c::compiler-error-context-enclosing-source error-context)
304 (c::compiler-error-context-source error-context)))
305 (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"
306 enclosing source condition)))
307
308 (defun compiler-note-location (context)
309 (cond (context
310 (resolve-note-location
311 *buffer-name*
312 (c::compiler-error-context-file-name context)
313 (c::compiler-error-context-file-position context)
314 (reverse (c::compiler-error-context-original-source-path context))
315 (c::compiler-error-context-original-source context)))
316 (t
317 (resolve-note-location *buffer-name* nil nil nil nil))))
318
319 (defgeneric resolve-note-location (buffer file-name file-position
320 source-path source))
321
322 (defmethod resolve-note-location ((b (eql nil)) (f pathname) pos path source)
323 (make-location
324 `(:file ,(unix-truename f))
325 `(:position ,(1+ (source-path-file-position path f)))))
326
327 (defmethod resolve-note-location ((b string) (f (eql :stream)) pos path source)
328 (make-location
329 `(:buffer ,b)
330 `(:position ,(+ *buffer-start-position*
331 (source-path-string-position path *buffer-substring*)))))
332
333 (defmethod resolve-note-location (b (f (eql :lisp)) pos path (source string))
334 (make-location
335 `(:source-form ,source)
336 `(:position 1)))
337
338 (defmethod resolve-note-location (buffer
339 (file (eql nil))
340 (pos (eql nil))
341 (path (eql nil))
342 (source (eql nil)))
343 (list :error "No error location available"))
344
345 (defimplementation call-with-compilation-hooks (function)
346 (let ((*previous-compiler-condition* nil)
347 (*previous-context* nil)
348 (*print-readably* nil))
349 (handler-bind ((c::compiler-error #'handle-notification-condition)
350 (c::style-warning #'handle-notification-condition)
351 (c::warning #'handle-notification-condition))
352 (funcall function))))
353
354 (defimplementation swank-compile-file (filename load-p)
355 (clear-xref-info filename)
356 (with-compilation-hooks ()
357 (let ((*buffer-name* nil))
358 (compile-file filename :load load-p))))
359
360 (defimplementation swank-compile-string (string &key buffer position)
361 (with-compilation-hooks ()
362 (let ((*buffer-name* buffer)
363 (*buffer-start-position* position)
364 (*buffer-substring* string))
365 (with-input-from-string (stream string)
366 (ext:compile-from-stream
367 stream
368 :source-info `(:emacs-buffer ,buffer
369 :emacs-buffer-offset ,position
370 :emacs-buffer-string ,string))))))
371
372
373 ;;;; XREF
374
375 (defmacro defxref (name function)
376 `(defimplementation ,name (name)
377 (xref-results (,function name))))
378
379 (defxref who-calls xref:who-calls)
380 (defxref who-references xref:who-references)
381 (defxref who-binds xref:who-binds)
382 (defxref who-sets xref:who-sets)
383
384 #+cmu19
385 (progn
386 (defxref who-macroexpands xref:who-macroexpands)
387 ;; XXX
388 (defimplementation who-specializes (symbol)
389 (let* ((methods (xref::who-specializes (find-class symbol)))
390 (locations (mapcar #'method-location methods)))
391 (mapcar #'list methods locations))))
392
393 (defun xref-results (contexts)
394 (mapcar (lambda (xref)
395 (list (xref:xref-context-name xref)
396 (resolve-xref-location xref)))
397 contexts))
398
399 (defun resolve-xref-location (xref)
400 (let ((name (xref:xref-context-name xref))
401 (file (xref:xref-context-file xref))
402 (source-path (xref:xref-context-source-path xref)))
403 (cond ((and file source-path)
404 (let ((position (source-path-file-position source-path file)))
405 (make-location (list :file (unix-truename file))
406 (list :position (1+ position)))))
407 (file
408 (make-location (list :file (unix-truename file))
409 (list :function-name (string name))))
410 (t
411 `(:error ,(format nil "Unkown source location: ~S ~S ~S "
412 name file source-path))))))
413
414 (defun clear-xref-info (namestring)
415 "Clear XREF notes pertaining to NAMESTRING.
416 This is a workaround for a CMUCL bug: XREF records are cumulative."
417 (when c:*record-xref-info*
418 (let ((filename (truename namestring)))
419 (dolist (db (list xref::*who-calls*
420 #+cmu19 xref::*who-is-called*
421 #+cmu19 xref::*who-macroexpands*
422 xref::*who-references*
423 xref::*who-binds*
424 xref::*who-sets*))
425 (maphash (lambda (target contexts)
426 ;; XXX update during traversal?
427 (setf (gethash target db)
428 (delete filename contexts
429 :key #'xref:xref-context-file
430 :test #'equalp)))
431 db)))))
432
433 (defun unix-truename (pathname)
434 (ext:unix-namestring (truename pathname)))
435
436
437 ;;;; Find callers and callees
438
439 ;;; Find callers and callees by looking at the constant pool of
440 ;;; compiled code objects. We assume every fdefn object in the
441 ;;; constant pool corresponds to a call to that function. A better
442 ;;; strategy would be to use the disassembler to find actual
443 ;;; call-sites.
444
445 (declaim (inline map-code-constants))
446 (defun map-code-constants (code fn)
447 "Call FN for each constant in CODE's constant pool."
448 (check-type code kernel:code-component)
449 (loop for i from vm:code-constants-offset below (kernel:get-header-data code)
450 do (funcall fn (kernel:code-header-ref code i))))
451
452 (defun function-callees (function)
453 "Return FUNCTION's callees as a list of functions."
454 (let ((callees '()))
455 (map-code-constants
456 (vm::find-code-object function)
457 (lambda (obj)
458 (when (kernel:fdefn-p obj)
459 (push (kernel:fdefn-function obj) callees))))
460 callees))
461
462 (declaim (ext:maybe-inline map-allocated-code-components))
463 (defun map-allocated-code-components (spaces fn)
464 "Call FN for each allocated code component in one of SPACES. FN
465 receives the object as argument. SPACES should be a list of the
466 symbols :dynamic, :static, or :read-only."
467 (dolist (space spaces)
468 (declare (inline vm::map-allocated-objects))
469 (vm::map-allocated-objects
470 (lambda (obj header size)
471 (declare (type fixnum size) (ignore size))
472 (when (= vm:code-header-type header)
473 (funcall fn obj)))
474 space)))
475
476 (declaim (ext:maybe-inline map-caller-code-components))
477 (defun map-caller-code-components (function spaces fn)
478 "Call FN for each code component with a fdefn for FUNCTION in its
479 constant pool."
480 (let ((function (coerce function 'function)))
481 (declare (inline map-allocated-code-components))
482 (map-allocated-code-components
483 spaces
484 (lambda (obj)
485 (map-code-constants
486 obj
487 (lambda (constant)
488 (when (and (kernel:fdefn-p constant)
489 (eq (kernel:fdefn-function constant)
490 function))
491 (funcall fn obj))))))))
492
493 (defun function-callers (function &optional (spaces '(:read-only :static
494 :dynamic)))
495 "Return FUNCTION's callers. The result is a list of code-objects."
496 (let ((referrers '()))
497 (declare (inline map-caller-code-components))
498 (ext:gc :full t)
499 (map-caller-code-components function spaces
500 (lambda (code) (push code referrers)))
501 referrers))
502
503 (defun debug-info-definitions (debug-info)
504 "Return the defintions for a debug-info. This should only be used
505 for code-object without entry points, i.e., byte compiled
506 code (are theree others?)"
507 ;; This mess has only been tested with #'ext::skip-whitespace, a
508 ;; byte-compiled caller of #'read-char .
509 (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))
510 (let ((name (c::debug-info-name debug-info))
511 (source (c::debug-info-source debug-info)))
512 (destructuring-bind (first) source
513 (ecase (c::debug-source-from first)
514 (:file
515 (list (list name
516 (make-location
517 (list :file (unix-truename (c::debug-source-name first)))
518 (list :function-name name)))))))))
519
520 (defun code-component-entry-points (code)
521 "Return a list ((NAME LOCATION) ...) of function definitons for
522 the code omponent CODE."
523 (delete-duplicates
524 (loop for e = (kernel:%code-entry-points code)
525 then (kernel::%function-next e)
526 while e
527 collect (list (kernel:%function-name e)
528 (function-location e)))
529 :test #'equal))
530
531 (defimplementation list-callers (symbol)
532 "Return a list ((NAME LOCATION) ...) of callers."
533 (let ((components (function-callers symbol))
534 (xrefs '()))
535 (dolist (code components)
536 (let* ((entry (kernel:%code-entry-points code))
537 (defs (if entry
538 (code-component-entry-points code)
539 ;; byte compiled stuff
540 (debug-info-definitions
541 (kernel:%code-debug-info code)))))
542 (setq xrefs (nconc defs xrefs))))
543 xrefs))
544
545 (defimplementation list-callees (symbol)
546 (let ((fns (function-callees symbol)))
547 (mapcar (lambda (fn)
548 (list (kernel:%function-name fn)
549 (function-location fn)))
550 fns)))
551
552
553 ;;;; Definitions
554
555 (defvar *debug-definition-finding* nil
556 "When true don't handle errors while looking for definitions.
557 This is useful when debugging the definition-finding code.")
558
559 (defmacro safe-definition-finding (&body body)
560 "Execute BODY ignoring errors. Return the source location returned
561 by BODY or if an error occurs a description of the error. The second
562 return value is the condition or nil."
563 `(flet ((body () ,@body))
564 (if *debug-definition-finding*
565 (body)
566 (handler-case (values (progn ,@body) nil)
567 (error (c) (values (list :error (princ-to-string c)) c))))))
568
569 (defun function-first-code-location (function)
570 (and (function-has-debug-function-p function)
571 (di:debug-function-start-location
572 (di:function-debug-function function))))
573
574 (defun function-has-debug-function-p (function)
575 (di:function-debug-function function))
576
577 (defun function-code-object= (closure function)
578 (and (eq (vm::find-code-object closure)
579 (vm::find-code-object function))
580 (not (eq closure function))))
581
582 (defun genericp (fn)
583 (typep fn 'generic-function))
584
585 (defun struct-closure-p (function)
586 (or (function-code-object= function #'kernel::structure-slot-accessor)
587 (function-code-object= function #'kernel::structure-slot-setter)
588 (function-code-object= function #'kernel::%defstruct)))
589
590 (defun struct-closure-dd (function)
591 (assert (= (kernel:get-type function) vm:closure-header-type))
592 (flet ((find-layout (function)
593 (sys:find-if-in-closure
594 (lambda (x)
595 (let ((value (if (di::indirect-value-cell-p x)
596 (c:value-cell-ref x)
597 x)))
598 (when (kernel::layout-p value)
599 (return-from find-layout value))))
600 function)))
601 (kernel:layout-info (find-layout function))))
602
603 (defun dd-location (dd)
604 (let ((constructor (or (kernel:dd-default-constructor dd)
605 (car (kernel::dd-constructors dd)))))
606 (when (or (not constructor) (and (consp constructor)
607 (not (car constructor))))
608 (error "Cannot locate struct without constructor: ~S"
609 (kernel::dd-name dd)))
610 (function-location
611 (coerce (if (consp constructor) (car constructor) constructor)
612 'function))))
613
614 (defun function-location (function)
615 "Return the source location for FUNCTION."
616 ;; First test if FUNCTION is a closure created by defstruct; if so
617 ;; extract the defstruct-description (dd) from the closure and find
618 ;; the constructor for the struct. Defstruct creates a defun for
619 ;; the default constructor and we use that as an approximation to
620 ;; the source location of the defstruct.
621 ;;
622 ;; For an ordinary function we return the source location of the
623 ;; first code-location we find.
624 (cond ((struct-closure-p function)
625 (safe-definition-finding
626 (dd-location (struct-closure-dd function))))
627 ((genericp function)
628 (gf-location function))
629 (t
630 (multiple-value-bind (code-location error)
631 (safe-definition-finding (function-first-code-location function))
632 (cond (error (list :error (princ-to-string error)))
633 (t (code-location-source-location code-location)))))))
634
635 (defun method-location (method)
636 (function-location (or (pcl::method-fast-function method)
637 (pcl:method-function method))))
638
639 (defun method-dspec (method)
640 (let* ((gf (pcl:method-generic-function method))
641 (name (pcl:generic-function-name gf))
642 (specializers (pcl:method-specializers method)))
643 `(method ,name ,(pcl::unparse-specializers specializers))))
644
645 (defun method-definition (method)
646 (list (method-dspec method)
647 (method-location method)))
648
649 (defun make-name-in-file-location (file string)
650 (multiple-value-bind (filename c)
651 (ignore-errors (unix-truename
652 (merge-pathnames (make-pathname :type "lisp")
653 file)))
654 (cond (filename (make-location `(:file ,filename)
655 `(:function-name ,string)))
656 (t (list :error (princ-to-string c))))))
657
658 (defun gf-location (gf)
659 (let ((def-source (pcl::definition-source gf))
660 (name (string (nth-value 1 (ext:valid-function-name-p
661 (pcl:generic-function-name gf))))))
662 (etypecase def-source
663 (pathname (make-name-in-file-location def-source name))
664 (cons
665 (destructuring-bind ((dg name) pathname) def-source
666 (declare (ignore dg))
667 (etypecase pathname
668 (pathname (make-name-in-file-location pathname (string name)))
669 (null `(:error ,(format nil "Cannot resolve: ~S" def-source)))))))))
670
671 (defun gf-method-definitions (gf)
672 (mapcar #'method-definition (pcl::generic-function-methods gf)))
673
674 (defun function-definitions (name)
675 "Return definitions for NAME in the \"function namespace\", i.e.,
676 regular functions, generic functions, methods and macros.
677 NAME can any valid function name (e.g, (setf car))."
678 (cond ((and (symbolp name) (macro-function name))
679 (list `((defmacro ,name)
680 ,(function-location (macro-function name)))))
681 ((and (symbolp name) (special-operator-p name))
682 (list `((:special-operator ,name)
683 (:error ,(format nil "Special operator: ~S" name)))))
684 ((and (ext:valid-function-name-p name)
685 (ext:info :function :definition name))
686 (let ((function (coerce name 'function)))
687 (cond ((genericp function)
688 (cons (list `(defgeneric ,name)
689 (function-location function))
690 (gf-method-definitions function)))
691 (t (list (list `(function ,name)
692 (function-location function)))))))))
693
694 (defun maybe-make-definition (function kind name)
695 (if function
696 (list (list `(,kind ,name) (function-location function)))))
697
698 (defun type-definitions (name)
699 (maybe-make-definition (ext:info :type :expander name) 'deftype name))
700
701 (defun find-dd (name)
702 (let ((layout (ext:info :type :compiler-layout name)))
703 (if layout
704 (kernel:layout-info layout))))
705
706 (defun struct-definitions (name)
707 (let ((dd (and (symbolp name) (find-dd name))))
708 (if dd
709 (list (list `(defstruct ,name) (dd-location dd))))))
710
711 (defun setf-definitions (name)
712 (let ((function (or (ext:info :setf :inverse name)
713 (ext:info :setf :expander name))))
714 (if function
715 (list (list `(setf ,name)
716 (function-location (coerce function 'function)))))))
717
718 (defun compiler-macro-definitions (symbol)
719 (maybe-make-definition (compiler-macro-function symbol)
720 'define-compiler-macro
721 symbol))
722
723 (defun source-transform-definitions (name)
724 (maybe-make-definition (ext:info :function :source-transform name)
725 'c:def-source-transform
726 name))
727
728 (defun function-info-definitions (name)
729 (let ((info (ext:info :function :info name)))
730 (if info
731 (append (loop for transform in (c::function-info-transforms info)
732 collect (list `(c:deftransform ,name
733 ,(c::type-specifier
734 (c::transform-type transform)))
735 (function-location (c::transform-function
736 transform))))
737 (maybe-make-definition (c::function-info-derive-type info)
738 'c::derive-type name)
739 (maybe-make-definition (c::function-info-optimizer info)
740 'c::optimizer name)
741 (maybe-make-definition (c::function-info-ltn-annotate info)
742 'c::ltn-annotate name)
743 (maybe-make-definition (c::function-info-ir2-convert info)
744 'c::ir2-convert name)
745 (loop for template in (c::function-info-templates info)
746 collect (list `(c::vop ,(c::template-name template))
747 (function-location
748 (c::vop-info-generator-function
749 template))))))))
750
751 (defun ir1-translator-definitions (name)
752 (maybe-make-definition (ext:info :function :ir1-convert name)
753 'c:def-ir1-translator name))
754
755 (defimplementation find-definitions (name)
756 (append (function-definitions name)
757 (setf-definitions name)
758 (struct-definitions name)
759 (type-definitions name)
760 (compiler-macro-definitions name)
761 (source-transform-definitions name)
762 (function-info-definitions name)
763 (ir1-translator-definitions name)))
764
765 ;;;; Documentation.
766
767 (defimplementation describe-symbol-for-emacs (symbol)
768 (let ((result '()))
769 (flet ((doc (kind)
770 (or (documentation symbol kind) :not-documented))
771 (maybe-push (property value)
772 (when value
773 (setf result (list* property value result)))))
774 (maybe-push
775 :variable (multiple-value-bind (kind recorded-p)
776 (ext:info variable kind symbol)
777 (declare (ignore kind))
778 (if (or (boundp symbol) recorded-p)
779 (doc 'variable))))
780 (maybe-push
781 :generic-function
782 (if (and (fboundp symbol)
783 (typep (fdefinition symbol) 'generic-function))
784 (doc 'function)))
785 (maybe-push
786 :function (if (and (fboundp symbol)
787 (not (typep (fdefinition symbol) 'generic-function)))
788 (doc 'function)))
789 (maybe-push
790 :setf (if (or (ext:info setf inverse symbol)
791 (ext:info setf expander symbol))
792 (doc 'setf)))
793 (maybe-push
794 :type (if (ext:info type kind symbol)
795 (doc 'type)))
796 (maybe-push
797 :class (if (find-class symbol nil)
798 (doc 'class)))
799 (maybe-push
800 :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
801 (doc 'alien-type)))
802 (maybe-push
803 :alien-struct (if (ext:info alien-type struct symbol)
804 (doc nil)))
805 (maybe-push
806 :alien-union (if (ext:info alien-type union symbol)
807 (doc nil)))
808 (maybe-push
809 :alien-enum (if (ext:info alien-type enum symbol)
810 (doc nil)))
811 result)))
812
813 (defimplementation describe-definition (symbol namespace)
814 (ecase namespace
815 (:variable
816 (describe symbol))
817 ((:function :generic-function)
818 (describe (symbol-function symbol)))
819 (:setf
820 (describe (or (ext:info setf inverse symbol))
821 (ext:info setf expander symbol)))
822 (:type
823 (describe (kernel:values-specifier-type symbol)))
824 (:class
825 (describe (find-class symbol)))
826 (:alien-type
827 (ecase (ext:info :alien-type :kind symbol)
828 (:primitive
829 (describe (let ((alien::*values-type-okay* t))
830 (funcall (ext:info :alien-type :translator symbol)
831 (list symbol)))))
832 ((:defined)
833 (describe (ext:info :alien-type :definition symbol)))
834 (:unknown
835 (format nil "Unkown alien type: ~S" symbol))))
836 (:alien-struct
837 (describe (ext:info :alien-type :struct symbol)))
838 (:alien-union
839 (describe (ext:info :alien-type :union symbol)))
840 (:alien-enum
841 (describe (ext:info :alien-type :enum symbol)))))
842
843 (defimplementation arglist (symbol)
844 (let* ((fun (or (macro-function symbol)
845 (symbol-function symbol)))
846 (arglist
847 (cond ((eval:interpreted-function-p fun)
848 (eval:interpreted-function-arglist fun))
849 ((pcl::generic-function-p fun)
850 (pcl:generic-function-lambda-list fun))
851 ((kernel:%function-arglist (kernel:%function-self fun)))
852 ;; this should work both for
853 ;; compiled-debug-function and for
854 ;; interpreted-debug-function
855 (t (let ((df (di::function-debug-function fun)))
856 (if df
857 (di::debug-function-lambda-list df)
858 "(<arglist-unavailable>)"))))))
859 (check-type arglist (or list string))
860 arglist))
861
862
863 ;;;; Miscellaneous.
864
865 (defimplementation macroexpand-all (form)
866 (walker:macroexpand-all form))
867
868 ;; (in-package :c)
869 ;;
870 ;; (defun swank-backend::expand-ir1-top-level (form)
871 ;; "A scaled down version of the first pass of the compiler."
872 ;; (with-compilation-unit ()
873 ;; (let* ((*lexical-environment*
874 ;; (make-lexenv :default (make-null-environment)
875 ;; :cookie *default-cookie*
876 ;; :interface-cookie *default-interface-cookie*))
877 ;; (*source-info* (make-lisp-source-info form))
878 ;; (*block-compile* nil)
879 ;; (*block-compile-default* nil))
880 ;; (with-ir1-namespace
881 ;; (clear-stuff)
882 ;; (find-source-paths form 0)
883 ;; (ir1-top-level form '(0) t)))))
884 ;;
885 ;; (in-package :swank-backend)
886 ;;
887 ;; (defun print-ir1-converted-blocks (form)
888 ;; (with-output-to-string (*standard-output*)
889 ;; (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
890 ;;
891 ;; (defun print-compilation-trace (form)
892 ;; (with-output-to-string (*standard-output*)
893 ;; (with-input-from-string (s form)
894 ;; (ext:compile-from-stream s
895 ;; :verbose t
896 ;; :progress t
897 ;; :trace-stream *standard-output*))))
898
899 (defun set-default-directory (directory)
900 (setf (ext:default-directory) (namestring directory))
901 ;; Setting *default-pathname-defaults* to an absolute directory
902 ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
903 (setf *default-pathname-defaults* (pathname (ext:default-directory)))
904 (namestring (ext:default-directory)))
905
906 ;;; source-path-{stream,file,string,etc}-position moved into
907 ;;; swank-source-path-parser
908
909 (defun code-location-stream-position (code-location stream)
910 "Return the byte offset of CODE-LOCATION in STREAM. Extract the
911 toplevel-form-number and form-number from CODE-LOCATION and use that
912 to find the position of the corresponding form."
913 (let* ((location (debug::maybe-block-start-location code-location))
914 (tlf-offset (di:code-location-top-level-form-offset location))
915 (form-number (di:code-location-form-number location))
916 (*read-suppress* t))
917 (dotimes (i tlf-offset) (read stream))
918 (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)
919 (let* ((path-table (di:form-number-translations tlf 0))
920 (source-path
921 (if (<= (length path-table) form-number) ; source out of sync?
922 (list 0) ; should probably signal a condition
923 (reverse (cdr (aref path-table form-number))))))
924 (source-path-source-position source-path tlf position-map)))))
925
926 (defun code-location-string-offset (code-location string)
927 (with-input-from-string (s string)
928 (code-location-stream-position code-location s)))
929
930 (defun code-location-file-position (code-location filename)
931 (with-open-file (s filename :direction :input)
932 (code-location-stream-position code-location s)))
933
934 (defun debug-source-info-from-emacs-buffer-p (debug-source)
935 (let ((info (c::debug-source-info debug-source)))
936 (and info
937 (consp info)
938 (eq :emacs-buffer (car info)))))
939
940 (defun source-location-from-code-location (code-location)
941 "Return the source location for CODE-LOCATION."
942 (let ((debug-fun (di:code-location-debug-function code-location)))
943 (when (di::bogus-debug-function-p debug-fun)
944 (error "Bogus debug function: ~A" debug-fun)))
945 (let* ((debug-source (di:code-location-debug-source code-location))
946 (from (di:debug-source-from debug-source))
947 (name (di:debug-source-name debug-source)))
948 (ecase from
949 (:file
950 (make-location (list :file (unix-truename name))
951 (list :position (1+ (code-location-file-position
952 code-location name)))))
953 (:stream
954 (assert (debug-source-info-from-emacs-buffer-p debug-source))
955 (let ((info (c::debug-source-info debug-source)))
956 (make-location
957 (list :buffer (getf info :emacs-buffer))
958 (list :position (+ (getf info :emacs-buffer-offset)
959 (code-location-string-offset
960 code-location
961 (getf info :emacs-buffer-string)))))))
962 (:lisp
963 (make-location
964 (list :source-form (with-output-to-string (*standard-output*)
965 (debug::print-code-location-source-form
966 code-location 100 t)))
967 (list :position 1))))))
968
969 (defun code-location-source-location (code-location)
970 "Safe wrapper around `code-location-from-source-location'."
971 (safe-definition-finding
972 (source-location-from-code-location code-location)))
973
974
975 ;;;; Debugging
976
977 (defvar *sldb-stack-top*)
978
979 (defimplementation call-with-debugging-environment (debugger-loop-fn)
980 (unix:unix-sigsetmask 0)
981 (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
982 (debug:*stack-top-hint* nil))
983 (handler-bind ((di:debug-condition
984 (lambda (condition)
985 (signal (make-condition
986 'sldb-condition
987 :original-condition condition)))))
988 (funcall debugger-loop-fn))))
989
990 (defun nth-frame (index)
991 (do ((frame *sldb-stack-top* (di:frame-down frame))
992 (i index (1- i)))
993 ((zerop i) frame)))
994
995 (defimplementation compute-backtrace (start end)
996 (let ((end (or end most-positive-fixnum)))
997 (loop for f = (nth-frame start) then (di:frame-down f)
998 for i from start below end
999 while f
1000 collect f)))
1001
1002 (defimplementation print-frame (frame stream)
1003 (let ((*standard-output* stream))
1004 (debug::print-frame-call frame :verbosity 1 :number nil)))
1005
1006 (defimplementation frame-source-location-for-emacs (index)
1007 (code-location-source-location (di:frame-code-location (nth-frame index))))
1008
1009 (defimplementation eval-in-frame (form index)
1010 (di:eval-in-frame (nth-frame index) form))
1011
1012 (defimplementation frame-locals (index)
1013 (let* ((frame (nth-frame index))
1014 (location (di:frame-code-location frame))
1015 (debug-function (di:frame-debug-function frame))
1016 (debug-variables (di::debug-function-debug-variables debug-function)))
1017 (loop for v across debug-variables collect
1018 (list :name (di:debug-variable-symbol v)
1019 :id (di:debug-variable-id v)
1020 :value (ecase (di:debug-variable-validity v location)
1021 (:valid
1022 (di:debug-variable-value v frame))
1023 ((:invalid :unknown)
1024 ':not-available))))))
1025
1026 (defimplementation frame-catch-tags (index)
1027 (mapcar #'car (di:frame-catches (nth-frame index))))
1028
1029 (defun set-step-breakpoints (frame)
1030 (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1031 (di:frame-code-location frame)))
1032 (error "Cannot step, in elsewhere code~%"))
1033 (let* ((code-location (di:frame-code-location frame))
1034 (debug::*bad-code-location-types*
1035 (remove :call-site debug::*bad-code-location-types*))
1036 (next (debug::next-code-locations code-location)))
1037 (cond (next
1038 (let ((steppoints '()))
1039 (flet ((hook (frame breakpoint)
1040 (let ((debug:*stack-top-hint* frame))
1041 (mapc #'di:delete-breakpoint steppoints)
1042 (let ((cl (di::breakpoint-what breakpoint)))
1043 (break "Breakpoint: ~S ~S"
1044 (di:code-location-kind cl)
1045 (di::compiled-code-location-pc cl))))))
1046 (dolist (code-location next)
1047 (let ((bp (di:make-breakpoint #'hook code-location
1048 :kind :code-location)))
1049 (di:activate-breakpoint bp)
1050 (push bp steppoints))))))
1051 (t
1052 (flet ((hook (frame breakpoint values cookie)
1053 (declare (ignore cookie))
1054 (di:delete-breakpoint breakpoint)
1055 (let ((debug:*stack-top-hint* frame))
1056 (break "Function-end: ~A ~A" breakpoint values))))
1057 (let* ((debug-function (di:frame-debug-function frame))
1058 (bp (di:make-breakpoint #'hook debug-function
1059 :kind :function-end)))
1060 (di:activate-breakpoint bp)))))))
1061
1062 ;; (defslimefun sldb-step (frame)
1063 ;; (cond ((find-restart 'continue *swank-debugger-condition*)
1064 ;; (set-step-breakpoints (nth-frame frame))
1065 ;; (continue *swank-debugger-condition*))
1066 ;; (t
1067 ;; (error "Cannot continue in from condition: ~A"
1068 ;; *swank-debugger-condition*))))
1069
1070 (defun frame-cfp (frame)
1071 "Return the Control-Stack-Frame-Pointer for FRAME."
1072 (etypecase frame
1073 (di::compiled-frame (di::frame-pointer frame))
1074 ((or di::interpreted-frame null) -1)))
1075
1076 (defun frame-ip (frame)
1077 "Return the (absolute) instruction pointer and the relative pc of FRAME."
1078 (if (not frame)
1079 -1
1080 (let ((debug-fun (di::frame-debug-function frame)))
1081 (etypecase debug-fun
1082 (di::compiled-debug-function
1083 (let* ((code-loc (di:frame-code-location frame))
1084 (component (di::compiled-debug-function-component debug-fun))
1085 (pc (di::compiled-code-location-pc code-loc))
1086 (ip (sys:without-gcing
1087 (sys:sap-int
1088 (sys:sap+ (kernel:code-instructions component) pc)))))
1089 (values ip pc)))
1090 ((or di::bogus-debug-function di::interpreted-debug-function)
1091 -1)))))
1092
1093 (defun frame-registers (frame)
1094 "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1095 (let* ((cfp (frame-cfp frame))
1096 (csp (frame-cfp (di::frame-up frame)))
1097 (ip (frame-ip frame))
1098 (ocfp (frame-cfp (di::frame-down frame)))
1099 (lra (frame-ip (di::frame-down frame))))
1100 (values csp cfp ip ocfp lra)))
1101
1102 (defun print-frame-registers (frame-number)
1103 (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1104 (flet ((fixnum (p) (etypecase p
1105 (integer p)
1106 (sys:system-area-pointer (sys:sap-int p)))))
1107 (apply #'format t "~
1108 CSP = ~X
1109 CFP = ~X
1110 IP = ~X
1111 OCFP = ~X
1112 LRA = ~X~%" (mapcar #'fixnum
1113 (multiple-value-list (frame-registers frame)))))))
1114
1115
1116 (defimplementation disassemble-frame (frame-number)
1117 "Return a string with the disassembly of frames code."
1118 (print-frame-registers frame-number)
1119 (terpri)
1120 (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1121 (debug-fun (di::frame-debug-function frame)))
1122 (etypecase debug-fun
1123 (di::compiled-debug-function
1124 (let* ((component (di::compiled-debug-function-component debug-fun))
1125 (fun (di:debug-function-function debug-fun)))
1126 (if fun
1127 (disassemble fun)
1128 (disassem:disassemble-code-component component))))
1129 (di::bogus-debug-function
1130 (format t "~%[Disassembling bogus frames not implemented]")))))
1131
1132 #+(or)
1133 (defun print-binding-stack ()
1134 (flet ((bsp- (p) (sys:sap+ p (- (* vm:binding-size vm:word-bytes))))
1135 (frob (p offset) (kernel:make-lisp-obj (sys:sap-ref-32 p offset))))
1136 (do ((bsp (bsp- (kernel:binding-stack-pointer-sap)) (bsp- bsp))
1137 (start (sys:int-sap (lisp::binding-stack-start))))
1138 ((sys:sap= bsp start))
1139 (format t "~X: ~S = ~S~%"
1140 (sys:sap-int bsp)
1141 (frob bsp (* vm:binding-symbol-slot vm:word-bytes))
1142 (frob bsp (* vm:binding-value-slot vm:word-bytes))))))
1143
1144 ;; (print-binding-stack)
1145
1146 #+(or)
1147 (defun print-catch-blocks ()
1148 (do ((b (di::descriptor-sap lisp::*current-catch-block*)
1149 (sys:sap-ref-sap b (* vm:catch-block-previous-catch-slot
1150 vm:word-bytes))))
1151 (nil)
1152 (let ((int (sys:sap-int b)))
1153 (when (zerop int) (return))
1154 (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1155 (let ((uwp (ref vm:catch-block-current-uwp-slot))
1156 (cfp (ref vm:catch-block-current-cont-slot))
1157 (tag (ref vm:catch-block-tag-slot))
1158 )
1159 (format t "~X: uwp = ~8X cfp = ~8X tag = ~X~%"
1160 int uwp cfp (kernel:make-lisp-obj tag)))))))
1161
1162 ;; (print-catch-blocks)
1163
1164 #+(or)
1165 (defun print-unwind-blocks ()
1166 (do ((b (di::descriptor-sap lisp::*current-unwind-protect-block*)
1167 (sys:sap-ref-sap b (* vm:unwind-block-current-uwp-slot
1168 vm:word-bytes))))
1169 (nil)
1170 (let ((int (sys:sap-int b)))
1171 (when (zerop int) (return))
1172 (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1173 (let ((cfp (ref vm:unwind-block-current-cont-slot)))
1174 (format t "~X: cfp = ~X~%" int cfp))))))
1175
1176 ;; (print-unwind-blocks)
1177
1178
1179 ;;;; Inspecting
1180
1181 (defconstant +lowtag-symbols+
1182 '(vm:even-fixnum-type
1183 vm:function-pointer-type
1184 vm:other-immediate-0-type
1185 vm:list-pointer-type
1186 vm:odd-fixnum-type
1187 vm:instance-pointer-type
1188 vm:other-immediate-1-type
1189 vm:other-pointer-type))
1190
1191 (defconstant +header-type-symbols+
1192 ;; Is there a convinient place for all those constants?
1193 (flet ((tail-comp (string tail)
1194 (and (>= (length string) (length tail))
1195 (string= string tail :start1 (- (length string)
1196 (length tail))))))
1197 (remove-if-not
1198 (lambda (x) (and (tail-comp (symbol-name x) "-TYPE")
1199 (not (member x +lowtag-symbols+))
1200 (boundp x)
1201 (typep (symbol-value x) 'fixnum)))
1202 (append (apropos-list "-TYPE" "VM" t)
1203 (apropos-list "-TYPE" "BIGNUM" t)))))
1204
1205 (defimplementation describe-primitive-type (object)
1206 (with-output-to-string (*standard-output*)
1207 (let* ((lowtag (kernel:get-lowtag object))
1208 (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1209 (format t "lowtag: ~A" lowtag-symbol)
1210 (when (member lowtag (list vm:other-pointer-type
1211 vm:function-pointer-type
1212 vm:other-immediate-0-type
1213 vm:other-immediate-1-type
1214 ))
1215 (let* ((type (kernel:get-type object))
1216 (type-symbol (find type +header-type-symbols+
1217 :key #'symbol-value)))
1218 (format t ", type: ~A" type-symbol))))))
1219
1220 (defimplementation inspected-parts (o)
1221 (cond ((di::indirect-value-cell-p o)
1222 (inspected-parts-of-value-cell o))
1223 (t
1224 (destructuring-bind (text labeledp . parts)
1225 (inspect::describe-parts o)
1226 (let ((parts (if labeledp
1227 (loop for (label . value) in parts
1228 collect (cons (string label) value))
1229 (loop for value in parts
1230 for i from 0
1231 collect (cons (format nil "~D" i) value)))))
1232 (values text parts))))))
1233
1234 (defun inspected-parts-of-value-cell (o)
1235 (values (format nil "~A~% is a value cell." o)
1236 (list (cons "Value" (c:value-cell-ref o)))))
1237
1238 (defmethod inspected-parts ((o function))
1239 (let ((header (kernel:get-type o)))
1240 (cond ((= header vm:function-header-type)
1241 (values
1242 (format nil "~A~% is a function." o)
1243 (list (cons "Self" (kernel:%function-self o))
1244 (cons "Next" (kernel:%function-next o))
1245 (cons "Name" (kernel:%function-name o))
1246 (cons "Arglist" (kernel:%function-arglist o))
1247 (cons "Type" (kernel:%function-type o))
1248 (cons "Code Object" (kernel:function-code-header o)))))
1249 ((= header vm:closure-header-type)
1250 (values (format nil "~A~% is a closure." o)
1251 (list*
1252 (cons "Function" (kernel:%closure-function o))
1253 (loop for i from 0 below (- (kernel:get-closure-length o)
1254 (1- vm:closure-info-offset))
1255 collect (cons (format nil "~D" i)
1256 (kernel:%closure-index-ref o i))))))
1257 (t (call-next-method o)))))
1258
1259 (defmethod inspected-parts ((o kernel:code-component))
1260 (values (format nil "~A~% is a code data-block." o)
1261 `(("First entry point" . ,(kernel:%code-entry-points o))
1262 ,@(loop for i from vm:code-constants-offset
1263 below (kernel:get-header-data o)
1264 collect (cons (format nil "Constant#~D" i)
1265 (kernel:code-header-ref o i)))
1266 ("Debug info" . ,(kernel:%code-debug-info o))
1267 ("Instructions" . ,(kernel:code-instructions o)))))
1268
1269 (defmethod inspected-parts ((o kernel:fdefn))
1270 (values (format nil "~A~% is a fdefn object." o)
1271 `(("Name" . ,(kernel:fdefn-name o))
1272 ("Function" . ,(kernel:fdefn-function o)))))
1273
1274
1275 ;;;; Profiling
1276 (defimplementation profile (fname)
1277 (eval `(profile:profile ,fname)))
1278
1279 (defimplementation unprofile (fname)
1280 (eval `(profile:unprofile ,fname)))
1281
1282 (defimplementation unprofile-all ()
1283 (eval '(profile:unprofile))
1284 "All functions unprofiled.")
1285
1286 (defimplementation profile-report ()
1287 (eval '(profile:report-time)))
1288
1289 (defimplementation profile-reset ()
1290 (eval '(profile:reset-time))
1291 "Reset profiling counters.")
1292
1293 (defimplementation profiled-functions ()
1294 profile:*timed-functions*)
1295
1296 (defimplementation profile-package (package callers methods)
1297 (profile:profile-all :package package
1298 :callers-p callers
1299 :methods methods))
1300
1301
1302 ;;;; Multiprocessing
1303
1304 #+MP
1305 (progn
1306 (defimplementation startup-multiprocessing ()
1307 ;; Threads magic: this never returns! But top-level becomes
1308 ;; available again.
1309 (mp::startup-idle-and-top-level-loops))
1310
1311 (defimplementation spawn (fn &key (name "Anonymous"))
1312 (mp:make-process fn :name name))
1313
1314 (defimplementation thread-name (thread)
1315 (mp:process-name thread))
1316
1317 (defimplementation thread-status (thread)
1318 (mp:process-whostate thread))
1319
1320 (defimplementation current-thread ()
1321 mp:*current-process*)
1322
1323 (defimplementation all-threads ()
1324 (copy-list mp:*all-processes*))
1325
1326 (defimplementation interrupt-thread (thread fn)
1327 (mp:process-interrupt thread fn))
1328
1329 (defimplementation kill-thread (thread)
1330 (mp:destroy-process thread))
1331
1332 (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
1333
1334 (defstruct (mailbox (:conc-name mailbox.))
1335 (mutex (mp:make-lock "process mailbox"))
1336 (queue '() :type list))
1337
1338 (defun mailbox (thread)
1339 "Return THREAD's mailbox."
1340 (mp:with-lock-held (*mailbox-lock*)
1341 (or (getf (mp:process-property-list thread) 'mailbox)
1342 (setf (getf (mp:process-property-list thread) 'mailbox)
1343 (make-mailbox)))))
1344
1345 (defimplementation send (thread message)
1346 (let* ((mbox (mailbox thread))
1347 (mutex (mailbox.mutex mbox)))
1348 (mp:with-lock-held (mutex)
1349 (setf (mailbox.queue mbox)
1350 (nconc (mailbox.queue mbox) (list message))))))
1351
1352 (defimplementation receive ()
1353 (let* ((mbox (mailbox mp:*current-process*))
1354 (mutex (mailbox.mutex mbox)))
1355 (mp:process-wait "receive" #'mailbox.queue mbox)
1356 (mp:with-lock-held (mutex)
1357 (pop (mailbox.queue mbox)))))
1358
1359 )

  ViewVC Help
Powered by ViewVC 1.1.5