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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.95 - (show annotations)
Sat Apr 17 09:25:58 2004 UTC (10 years ago) by heller
Branch: MAIN
Changes since 1.94: +17 -8 lines
(source-location-tlf-number, source-location-form-number): New
functions.
(resolve-stream-source-location, resolve-file-source-location): Use them.
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 ;; XXX maybe special case setters/getters
636 (defun method-location (method)
637 (function-location (or (pcl::method-fast-function method)
638 (pcl:method-function method))))
639
640 (defun method-dspec (method)
641 (let* ((gf (pcl:method-generic-function method))
642 (name (pcl:generic-function-name gf))
643 (specializers (pcl:method-specializers method))
644 (qualifiers (pcl:method-qualifiers method)))
645 `(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
646
647 (defun method-definition (method)
648 (list (method-dspec method)
649 (method-location method)))
650
651 (defun gf-location (gf)
652 (definition-source-location gf (pcl::generic-function-name gf)))
653
654 (defun gf-method-definitions (gf)
655 (mapcar #'method-definition (pcl::generic-function-methods gf)))
656
657 (defun function-definitions (name)
658 "Return definitions for NAME in the \"function namespace\", i.e.,
659 regular functions, generic functions, methods and macros.
660 NAME can any valid function name (e.g, (setf car))."
661 (cond ((and (symbolp name) (macro-function name))
662 (list `((defmacro ,name)
663 ,(function-location (macro-function name)))))
664 ((and (symbolp name) (special-operator-p name))
665 (list `((:special-operator ,name)
666 (:error ,(format nil "Special operator: ~S" name)))))
667 ((and (ext:valid-function-name-p name)
668 (ext:info :function :definition name))
669 (let ((function (coerce name 'function)))
670 (cond ((genericp function)
671 (cons (list `(defgeneric ,name)
672 (function-location function))
673 (gf-method-definitions function)))
674 (t (list (list `(function ,name)
675 (function-location function)))))))))
676
677 (defun maybe-make-definition (function kind name)
678 (if function
679 (list (list `(,kind ,name) (function-location function)))))
680
681 (defun type-definitions (name)
682 (maybe-make-definition (ext:info :type :expander name) 'deftype name))
683
684 (defun find-dd (name)
685 (let ((layout (ext:info :type :compiler-layout name)))
686 (if layout
687 (kernel:layout-info layout))))
688
689 (defun condition-class-location (class)
690 (let ((slots (conditions::condition-class-slots class))
691 (name (conditions::condition-class-name class)))
692 (cond ((null slots)
693 `(:error ,(format nil "No location info for condition: ~A" name)))
694 (t
695 (let* ((slot (first slots))
696 (gf (fdefinition
697 (first (conditions::condition-slot-readers slot)))))
698 (method-location
699 (first
700 (pcl:compute-applicable-methods-using-classes
701 gf (list (find-class name))))))))))
702
703 (defun class-location (class)
704 (definition-source-location class (pcl:class-name class)))
705
706 (defun make-name-in-file-location (file string)
707 (multiple-value-bind (filename c)
708 (ignore-errors
709 (unix-truename (merge-pathnames (make-pathname :type "lisp")
710 file)))
711 (cond (filename (make-location `(:file ,filename)
712 `(:function-name ,string)))
713 (t (list :error (princ-to-string c))))))
714
715 (defun source-location-form-numbers (location)
716 (c::decode-form-numbers (c::form-numbers-form-numbers location)))
717
718 (defun source-location-tlf-number (location)
719 (nth-value 0 (source-location-form-numbers location)))
720
721 (defun source-location-form-number (location)
722 (nth-value 1 (source-location-form-numbers location)))
723
724 (defun resolve-file-source-location (location)
725 (let ((filename (c::file-source-location-pathname location))
726 (tlf-number (source-location-tlf-number location))
727 (form-number (source-location-form-number location)))
728 (with-open-file (s filename)
729 (let ((pos (form-number-stream-position tlf-number form-number s)))
730 (make-location `(:file ,(unix-truename filename))
731 `(:position ,(1+ pos)))))))
732
733 (defun resolve-stream-source-location (location)
734 (let ((info (c::stream-source-location-user-info location))
735 (tlf-number (source-location-tlf-number location))
736 (form-number (source-location-form-number location)))
737 ;; XXX duplication in frame-source-location
738 (assert (info-from-emacs-buffer-p info))
739 (destructuring-bind (&key emacs-buffer emacs-buffer-string
740 emacs-buffer-offset) info
741 (with-input-from-string (s emacs-buffer-string)
742 (let ((pos (form-number-stream-position tlf-number form-number s)))
743 (make-location `(:buffer ,emacs-buffer)
744 `(:position ,(+ emacs-buffer-offset pos))))))))
745
746 (defun definition-source-location (object name)
747 (let ((source (pcl::definition-source object)))
748 (etypecase source
749 (null
750 `(:error ,(format nil "No source info for: ~A" object)))
751 (c::file-source-location
752 (resolve-file-source-location source))
753 (c::stream-source-location
754 (resolve-stream-source-location source))
755 (pathname
756 (make-name-in-file-location source name))
757 (cons
758 (destructuring-bind ((dg name) pathname) source
759 (declare (ignore dg))
760 (etypecase pathname
761 (pathname (make-name-in-file-location pathname (string name)))
762 (null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
763
764 (defun class-definitions (name)
765 (if (symbolp name)
766 (let ((class (kernel::find-class name nil)))
767 (etypecase class
768 (null '())
769 (kernel::structure-class
770 (list (list `(defstruct ,name) (dd-location (find-dd name)))))
771 (conditions::condition-class
772 (list (list `(define-condition ,name)
773 (condition-class-location class))))
774 (kernel::standard-class
775 (list (list `(defclass ,name)
776 (class-location (find-class name)))))
777 (kernel::built-in-class
778 (list (list `(kernel::define-type-class ,name)
779 `(:error
780 ,(format nil "No source info for built-in-class: ~A"
781 name)))))))))
782
783 (defun setf-definitions (name)
784 (let ((function (or (ext:info :setf :inverse name)
785 (ext:info :setf :expander name))))
786 (if function
787 (list (list `(setf ,name)
788 (function-location (coerce function 'function)))))))
789
790 (defun compiler-macro-definitions (symbol)
791 (maybe-make-definition (compiler-macro-function symbol)
792 'define-compiler-macro
793 symbol))
794
795 (defun source-transform-definitions (name)
796 (maybe-make-definition (ext:info :function :source-transform name)
797 'c:def-source-transform
798 name))
799
800 (defun function-info-definitions (name)
801 (let ((info (ext:info :function :info name)))
802 (if info
803 (append (loop for transform in (c::function-info-transforms info)
804 collect (list `(c:deftransform ,name
805 ,(c::type-specifier
806 (c::transform-type transform)))
807 (function-location (c::transform-function
808 transform))))
809 (maybe-make-definition (c::function-info-derive-type info)
810 'c::derive-type name)
811 (maybe-make-definition (c::function-info-optimizer info)
812 'c::optimizer name)
813 (maybe-make-definition (c::function-info-ltn-annotate info)
814 'c::ltn-annotate name)
815 (maybe-make-definition (c::function-info-ir2-convert info)
816 'c::ir2-convert name)
817 (loop for template in (c::function-info-templates info)
818 collect (list `(c::vop ,(c::template-name template))
819 (function-location
820 (c::vop-info-generator-function
821 template))))))))
822
823 (defun ir1-translator-definitions (name)
824 (maybe-make-definition (ext:info :function :ir1-convert name)
825 'c:def-ir1-translator name))
826
827 (defimplementation find-definitions (name)
828 (append (function-definitions name)
829 (setf-definitions name)
830 (class-definitions name)
831 (type-definitions name)
832 (compiler-macro-definitions name)
833 (source-transform-definitions name)
834 (function-info-definitions name)
835 (ir1-translator-definitions name)))
836
837 ;;;; Documentation.
838
839 (defimplementation describe-symbol-for-emacs (symbol)
840 (let ((result '()))
841 (flet ((doc (kind)
842 (or (documentation symbol kind) :not-documented))
843 (maybe-push (property value)
844 (when value
845 (setf result (list* property value result)))))
846 (maybe-push
847 :variable (multiple-value-bind (kind recorded-p)
848 (ext:info variable kind symbol)
849 (declare (ignore kind))
850 (if (or (boundp symbol) recorded-p)
851 (doc 'variable))))
852 (maybe-push
853 :generic-function
854 (if (and (fboundp symbol)
855 (typep (fdefinition symbol) 'generic-function))
856 (doc 'function)))
857 (maybe-push
858 :function (if (and (fboundp symbol)
859 (not (typep (fdefinition symbol) 'generic-function)))
860 (doc 'function)))
861 (maybe-push
862 :setf (if (or (ext:info setf inverse symbol)
863 (ext:info setf expander symbol))
864 (doc 'setf)))
865 (maybe-push
866 :type (if (ext:info type kind symbol)
867 (doc 'type)))
868 (maybe-push
869 :class (if (find-class symbol nil)
870 (doc 'class)))
871 (maybe-push
872 :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
873 (doc 'alien-type)))
874 (maybe-push
875 :alien-struct (if (ext:info alien-type struct symbol)
876 (doc nil)))
877 (maybe-push
878 :alien-union (if (ext:info alien-type union symbol)
879 (doc nil)))
880 (maybe-push
881 :alien-enum (if (ext:info alien-type enum symbol)
882 (doc nil)))
883 result)))
884
885 (defimplementation describe-definition (symbol namespace)
886 (ecase namespace
887 (:variable
888 (describe symbol))
889 ((:function :generic-function)
890 (describe (symbol-function symbol)))
891 (:setf
892 (describe (or (ext:info setf inverse symbol))
893 (ext:info setf expander symbol)))
894 (:type
895 (describe (kernel:values-specifier-type symbol)))
896 (:class
897 (describe (find-class symbol)))
898 (:alien-type
899 (ecase (ext:info :alien-type :kind symbol)
900 (:primitive
901 (describe (let ((alien::*values-type-okay* t))
902 (funcall (ext:info :alien-type :translator symbol)
903 (list symbol)))))
904 ((:defined)
905 (describe (ext:info :alien-type :definition symbol)))
906 (:unknown
907 (format nil "Unkown alien type: ~S" symbol))))
908 (:alien-struct
909 (describe (ext:info :alien-type :struct symbol)))
910 (:alien-union
911 (describe (ext:info :alien-type :union symbol)))
912 (:alien-enum
913 (describe (ext:info :alien-type :enum symbol)))))
914
915 (defun debug-variable-symbol-or-deleted (var)
916 (etypecase var
917 (di:debug-variable
918 (di::debug-variable-symbol var))
919 ((member :deleted)
920 '#:deleted)))
921
922 (defun debug-function-arglist (debug-function)
923 (let ((args (di::debug-function-lambda-list debug-function))
924 (required '())
925 (optional '())
926 (rest '())
927 (key '()))
928 ;; collect the names of debug-vars
929 (dolist (arg args)
930 (etypecase arg
931 (di::debug-variable
932 (push (di::debug-variable-symbol arg) required))
933 ((member :deleted)
934 (push ':deleted required))
935 (cons
936 (ecase (car arg)
937 (:keyword
938 (push (second arg) key))
939 (:optional
940 (push (debug-variable-symbol-or-deleted (second arg)) optional))
941 (:rest
942 (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
943 ;; intersperse lambda keywords as needed
944 (append (nreverse required)
945 (if optional (cons '&optional (nreverse optional)))
946 (if rest (cons '&rest (nreverse rest)))
947 (if key (cons '&key (nreverse key))))))
948
949 (defun symbol-debug-function-arglist (fname)
950 "Return FNAME's debug-function-arglist and %function-arglist.
951 A utility for debugging DEBUG-FUNCTION-ARGLIST."
952 (let ((fn (fdefinition fname)))
953 (values (debug-function-arglist (di::function-debug-function fn))
954 (kernel:%function-arglist (kernel:%function-self fn)))))
955
956 (defimplementation arglist (symbol)
957 (let* ((fun (or (macro-function symbol)
958 (symbol-function symbol)))
959 (arglist
960 (cond ((eval:interpreted-function-p fun)
961 (eval:interpreted-function-arglist fun))
962 ((pcl::generic-function-p fun)
963 (pcl:generic-function-lambda-list fun))
964 ((kernel:%function-arglist (kernel:%function-self fun)))
965 ;; this should work both for
966 ;; compiled-debug-function and for
967 ;; interpreted-debug-function
968 (t (let ((df (di::function-debug-function fun)))
969 (if df
970 (debug-function-arglist df)
971 "(<arglist-unavailable>)"))))))
972 (check-type arglist (or list string))
973 arglist))
974
975
976 ;;;; Miscellaneous.
977
978 (defimplementation macroexpand-all (form)
979 (walker:macroexpand-all form))
980
981 ;; (in-package :c)
982 ;;
983 ;; (defun swank-backend::expand-ir1-top-level (form)
984 ;; "A scaled down version of the first pass of the compiler."
985 ;; (with-compilation-unit ()
986 ;; (let* ((*lexical-environment*
987 ;; (make-lexenv :default (make-null-environment)
988 ;; :cookie *default-cookie*
989 ;; :interface-cookie *default-interface-cookie*))
990 ;; (*source-info* (make-lisp-source-info form))
991 ;; (*block-compile* nil)
992 ;; (*block-compile-default* nil))
993 ;; (with-ir1-namespace
994 ;; (clear-stuff)
995 ;; (find-source-paths form 0)
996 ;; (ir1-top-level form '(0) t)))))
997 ;;
998 ;; (in-package :swank-backend)
999 ;;
1000 ;; (defun print-ir1-converted-blocks (form)
1001 ;; (with-output-to-string (*standard-output*)
1002 ;; (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
1003 ;;
1004 ;; (defun print-compilation-trace (form)
1005 ;; (with-output-to-string (*standard-output*)
1006 ;; (with-input-from-string (s form)
1007 ;; (ext:compile-from-stream s
1008 ;; :verbose t
1009 ;; :progress t
1010 ;; :trace-stream *standard-output*))))
1011
1012 (defimplementation set-default-directory (directory)
1013 (setf (ext:default-directory) (namestring directory))
1014 ;; Setting *default-pathname-defaults* to an absolute directory
1015 ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1016 (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1017 (namestring (ext:default-directory)))
1018
1019 ;;; source-path-{stream,file,string,etc}-position moved into
1020 ;;; swank-source-path-parser
1021
1022 (defun code-location-stream-position (code-location stream)
1023 "Return the byte offset of CODE-LOCATION in STREAM. Extract the
1024 toplevel-form-number and form-number from CODE-LOCATION and use that
1025 to find the position of the corresponding form."
1026 (let* ((location (debug::maybe-block-start-location code-location))
1027 (tlf-offset (di:code-location-top-level-form-offset location))
1028 (form-number (di:code-location-form-number location)))
1029 (form-number-stream-position tlf-offset form-number stream)))
1030
1031 (defun form-number-stream-position (tlf-number form-number stream)
1032 (let ((*read-suppress* t))
1033 (dotimes (i tlf-number) (read stream))
1034 (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)
1035 (let* ((path-table (di:form-number-translations tlf 0))
1036 (source-path
1037 (if (<= (length path-table) form-number) ; source out of sync?
1038 (list 0) ; should probably signal a condition
1039 (reverse (cdr (aref path-table form-number))))))
1040 (source-path-source-position source-path tlf position-map)))))
1041
1042 (defun code-location-string-offset (code-location string)
1043 (with-input-from-string (s string)
1044 (code-location-stream-position code-location s)))
1045
1046 (defun code-location-file-position (code-location filename)
1047 (with-open-file (s filename :direction :input)
1048 (code-location-stream-position code-location s)))
1049
1050 (defun info-from-emacs-buffer-p (info)
1051 (and info
1052 (consp info)
1053 (eq :emacs-buffer (car info))))
1054
1055 (defun debug-source-info-from-emacs-buffer-p (debug-source)
1056 (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
1057
1058 (defun source-location-from-code-location (code-location)
1059 "Return the source location for CODE-LOCATION."
1060 (let ((debug-fun (di:code-location-debug-function code-location)))
1061 (when (di::bogus-debug-function-p debug-fun)
1062 (error "Bogus debug function: ~A" debug-fun)))
1063 (let* ((debug-source (di:code-location-debug-source code-location))
1064 (from (di:debug-source-from debug-source))
1065 (name (di:debug-source-name debug-source)))
1066 (ecase from
1067 (:file
1068 (make-location (list :file (unix-truename name))
1069 (list :position (1+ (code-location-file-position
1070 code-location name)))))
1071 (:stream
1072 (assert (debug-source-info-from-emacs-buffer-p debug-source))
1073 (let ((info (c::debug-source-info debug-source)))
1074 (make-location
1075 (list :buffer (getf info :emacs-buffer))
1076 (list :position (+ (getf info :emacs-buffer-offset)
1077 (code-location-string-offset
1078 code-location
1079 (getf info :emacs-buffer-string)))))))
1080 (:lisp
1081 (make-location
1082 (list :source-form (with-output-to-string (*standard-output*)
1083 (debug::print-code-location-source-form
1084 code-location 100 t)))
1085 (list :position 1))))))
1086
1087 (defun code-location-source-location (code-location)
1088 "Safe wrapper around `code-location-from-source-location'."
1089 (safe-definition-finding
1090 (source-location-from-code-location code-location)))
1091
1092
1093 ;;;; Debugging
1094
1095 (defvar *sldb-stack-top*)
1096
1097 (defimplementation call-with-debugging-environment (debugger-loop-fn)
1098 (unix:unix-sigsetmask 0)
1099 (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1100 (debug:*stack-top-hint* nil))
1101 (handler-bind ((di:debug-condition
1102 (lambda (condition)
1103 (signal (make-condition
1104 'sldb-condition
1105 :original-condition condition)))))
1106 (funcall debugger-loop-fn))))
1107
1108 (defun nth-frame (index)
1109 (do ((frame *sldb-stack-top* (di:frame-down frame))
1110 (i index (1- i)))
1111 ((zerop i) frame)))
1112
1113 (defimplementation compute-backtrace (start end)
1114 (let ((end (or end most-positive-fixnum)))
1115 (loop for f = (nth-frame start) then (di:frame-down f)
1116 for i from start below end
1117 while f
1118 collect f)))
1119
1120 (defimplementation print-frame (frame stream)
1121 (let ((*standard-output* stream))
1122 (debug::print-frame-call frame :verbosity 1 :number nil)))
1123
1124 (defimplementation frame-source-location-for-emacs (index)
1125 (code-location-source-location (di:frame-code-location (nth-frame index))))
1126
1127 (defimplementation eval-in-frame (form index)
1128 (di:eval-in-frame (nth-frame index) form))
1129
1130 (defimplementation frame-locals (index)
1131 (let* ((frame (nth-frame index))
1132 (location (di:frame-code-location frame))
1133 (debug-function (di:frame-debug-function frame))
1134 (debug-variables (di::debug-function-debug-variables debug-function)))
1135 (loop for v across debug-variables collect
1136 (list :name (di:debug-variable-symbol v)
1137 :id (di:debug-variable-id v)
1138 :value (ecase (di:debug-variable-validity v location)
1139 (:valid
1140 (di:debug-variable-value v frame))
1141 ((:invalid :unknown)
1142 ':not-available))))))
1143
1144 (defimplementation frame-catch-tags (index)
1145 (mapcar #'car (di:frame-catches (nth-frame index))))
1146
1147 (defun set-step-breakpoints (frame)
1148 (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1149 (di:frame-code-location frame)))
1150 (error "Cannot step, in elsewhere code~%"))
1151 (let* ((code-location (di:frame-code-location frame))
1152 (debug::*bad-code-location-types*
1153 (remove :call-site debug::*bad-code-location-types*))
1154 (next (debug::next-code-locations code-location)))
1155 (cond (next
1156 (let ((steppoints '()))
1157 (flet ((hook (frame breakpoint)
1158 (let ((debug:*stack-top-hint* frame))
1159 (mapc #'di:delete-breakpoint steppoints)
1160 (let ((cl (di::breakpoint-what breakpoint)))
1161 (break "Breakpoint: ~S ~S"
1162 (di:code-location-kind cl)
1163 (di::compiled-code-location-pc cl))))))
1164 (dolist (code-location next)
1165 (let ((bp (di:make-breakpoint #'hook code-location
1166 :kind :code-location)))
1167 (di:activate-breakpoint bp)
1168 (push bp steppoints))))))
1169 (t
1170 (flet ((hook (frame breakpoint values cookie)
1171 (declare (ignore cookie))
1172 (di:delete-breakpoint breakpoint)
1173 (let ((debug:*stack-top-hint* frame))
1174 (break "Function-end: ~A ~A" breakpoint values))))
1175 (let* ((debug-function (di:frame-debug-function frame))
1176 (bp (di:make-breakpoint #'hook debug-function
1177 :kind :function-end)))
1178 (di:activate-breakpoint bp)))))))
1179
1180 ;; (defslimefun sldb-step (frame)
1181 ;; (cond ((find-restart 'continue *swank-debugger-condition*)
1182 ;; (set-step-breakpoints (nth-frame frame))
1183 ;; (continue *swank-debugger-condition*))
1184 ;; (t
1185 ;; (error "Cannot continue in from condition: ~A"
1186 ;; *swank-debugger-condition*))))
1187
1188 (defun frame-cfp (frame)
1189 "Return the Control-Stack-Frame-Pointer for FRAME."
1190 (etypecase frame
1191 (di::compiled-frame (di::frame-pointer frame))
1192 ((or di::interpreted-frame null) -1)))
1193
1194 (defun frame-ip (frame)
1195 "Return the (absolute) instruction pointer and the relative pc of FRAME."
1196 (if (not frame)
1197 -1
1198 (let ((debug-fun (di::frame-debug-function frame)))
1199 (etypecase debug-fun
1200 (di::compiled-debug-function
1201 (let* ((code-loc (di:frame-code-location frame))
1202 (component (di::compiled-debug-function-component debug-fun))
1203 (pc (di::compiled-code-location-pc code-loc))
1204 (ip (sys:without-gcing
1205 (sys:sap-int
1206 (sys:sap+ (kernel:code-instructions component) pc)))))
1207 (values ip pc)))
1208 ((or di::bogus-debug-function di::interpreted-debug-function)
1209 -1)))))
1210
1211 (defun frame-registers (frame)
1212 "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1213 (let* ((cfp (frame-cfp frame))
1214 (csp (frame-cfp (di::frame-up frame)))
1215 (ip (frame-ip frame))
1216 (ocfp (frame-cfp (di::frame-down frame)))
1217 (lra (frame-ip (di::frame-down frame))))
1218 (values csp cfp ip ocfp lra)))
1219
1220 (defun print-frame-registers (frame-number)
1221 (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1222 (flet ((fixnum (p) (etypecase p
1223 (integer p)
1224 (sys:system-area-pointer (sys:sap-int p)))))
1225 (apply #'format t "~
1226 CSP = ~X
1227 CFP = ~X
1228 IP = ~X
1229 OCFP = ~X
1230 LRA = ~X~%" (mapcar #'fixnum
1231 (multiple-value-list (frame-registers frame)))))))
1232
1233
1234 (defimplementation disassemble-frame (frame-number)
1235 "Return a string with the disassembly of frames code."
1236 (print-frame-registers frame-number)
1237 (terpri)
1238 (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1239 (debug-fun (di::frame-debug-function frame)))
1240 (etypecase debug-fun
1241 (di::compiled-debug-function
1242 (let* ((component (di::compiled-debug-function-component debug-fun))
1243 (fun (di:debug-function-function debug-fun)))
1244 (if fun
1245 (disassemble fun)
1246 (disassem:disassemble-code-component component))))
1247 (di::bogus-debug-function
1248 (format t "~%[Disassembling bogus frames not implemented]")))))
1249
1250 #+(or)
1251 (defun print-binding-stack ()
1252 (flet ((bsp- (p) (sys:sap+ p (- (* vm:binding-size vm:word-bytes))))
1253 (frob (p offset) (kernel:make-lisp-obj (sys:sap-ref-32 p offset))))
1254 (do ((bsp (bsp- (kernel:binding-stack-pointer-sap)) (bsp- bsp))
1255 (start (sys:int-sap (lisp::binding-stack-start))))
1256 ((sys:sap= bsp start))
1257 (format t "~X: ~S = ~S~%"
1258 (sys:sap-int bsp)
1259 (frob bsp (* vm:binding-symbol-slot vm:word-bytes))
1260 (frob bsp (* vm:binding-value-slot vm:word-bytes))))))
1261
1262 ;; (print-binding-stack)
1263
1264 #+(or)
1265 (defun print-catch-blocks ()
1266 (do ((b (di::descriptor-sap lisp::*current-catch-block*)
1267 (sys:sap-ref-sap b (* vm:catch-block-previous-catch-slot
1268 vm:word-bytes))))
1269 (nil)
1270 (let ((int (sys:sap-int b)))
1271 (when (zerop int) (return))
1272 (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1273 (let ((uwp (ref vm:catch-block-current-uwp-slot))
1274 (cfp (ref vm:catch-block-current-cont-slot))
1275 (tag (ref vm:catch-block-tag-slot))
1276 )
1277 (format t "~X: uwp = ~8X cfp = ~8X tag = ~X~%"
1278 int uwp cfp (kernel:make-lisp-obj tag)))))))
1279
1280 ;; (print-catch-blocks)
1281
1282 #+(or)
1283 (defun print-unwind-blocks ()
1284 (do ((b (di::descriptor-sap lisp::*current-unwind-protect-block*)
1285 (sys:sap-ref-sap b (* vm:unwind-block-current-uwp-slot
1286 vm:word-bytes))))
1287 (nil)
1288 (let ((int (sys:sap-int b)))
1289 (when (zerop int) (return))
1290 (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1291 (let ((cfp (ref vm:unwind-block-current-cont-slot)))
1292 (format t "~X: cfp = ~X~%" int cfp))))))
1293
1294 ;; (print-unwind-blocks)
1295
1296
1297 ;;;; Inspecting
1298
1299 (defconstant +lowtag-symbols+
1300 '(vm:even-fixnum-type
1301 vm:function-pointer-type
1302 vm:other-immediate-0-type
1303 vm:list-pointer-type
1304 vm:odd-fixnum-type
1305 vm:instance-pointer-type
1306 vm:other-immediate-1-type
1307 vm:other-pointer-type))
1308
1309 (defconstant +header-type-symbols+
1310 ;; Is there a convinient place for all those constants?
1311 (flet ((tail-comp (string tail)
1312 (and (>= (length string) (length tail))
1313 (string= string tail :start1 (- (length string)
1314 (length tail))))))
1315 (remove-if-not
1316 (lambda (x) (and (tail-comp (symbol-name x) "-TYPE")
1317 (not (member x +lowtag-symbols+))
1318 (boundp x)
1319 (typep (symbol-value x) 'fixnum)))
1320 (append (apropos-list "-TYPE" "VM" t)
1321 (apropos-list "-TYPE" "BIGNUM" t)))))
1322
1323 (defimplementation describe-primitive-type (object)
1324 (with-output-to-string (*standard-output*)
1325 (let* ((lowtag (kernel:get-lowtag object))
1326 (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1327 (format t "lowtag: ~A" lowtag-symbol)
1328 (when (member lowtag (list vm:other-pointer-type
1329 vm:function-pointer-type
1330 vm:other-immediate-0-type
1331 vm:other-immediate-1-type
1332 ))
1333 (let* ((type (kernel:get-type object))
1334 (type-symbol (find type +header-type-symbols+
1335 :key #'symbol-value)))
1336 (format t ", type: ~A" type-symbol))))))
1337
1338 (defimplementation inspected-parts (o)
1339 (cond ((di::indirect-value-cell-p o)
1340 (inspected-parts-of-value-cell o))
1341 (t
1342 (destructuring-bind (text labeledp . parts)
1343 (inspect::describe-parts o)
1344 (let ((parts (if labeledp
1345 (loop for (label . value) in parts
1346 collect (cons (string label) value))
1347 (loop for value in parts
1348 for i from 0
1349 collect (cons (format nil "~D" i) value)))))
1350 (values text parts))))))
1351
1352 (defun inspected-parts-of-value-cell (o)
1353 (values (format nil "~A~% is a value cell." o)
1354 (list (cons "Value" (c:value-cell-ref o)))))
1355
1356 (defmethod inspected-parts ((o function))
1357 (let ((header (kernel:get-type o)))
1358 (cond ((= header vm:function-header-type)
1359 (values
1360 (format nil "~A~% is a function." o)
1361 (list (cons "Self" (kernel:%function-self o))
1362 (cons "Next" (kernel:%function-next o))
1363 (cons "Name" (kernel:%function-name o))
1364 (cons "Arglist" (kernel:%function-arglist o))
1365 (cons "Type" (kernel:%function-type o))
1366 (cons "Code Object" (kernel:function-code-header o)))))
1367 ((= header vm:closure-header-type)
1368 (values (format nil "~A~% is a closure." o)
1369 (list*
1370 (cons "Function" (kernel:%closure-function o))
1371 (loop for i from 0 below (- (kernel:get-closure-length o)
1372 (1- vm:closure-info-offset))
1373 collect (cons (format nil "~D" i)
1374 (kernel:%closure-index-ref o i))))))
1375 (t (call-next-method o)))))
1376
1377 (defmethod inspected-parts ((o kernel:code-component))
1378 (values (format nil "~A~% is a code data-block." o)
1379 `(("First entry point" . ,(kernel:%code-entry-points o))
1380 ,@(loop for i from vm:code-constants-offset
1381 below (kernel:get-header-data o)
1382 collect (cons (format nil "Constant#~D" i)
1383 (kernel:code-header-ref o i)))
1384 ("Debug info" . ,(kernel:%code-debug-info o))
1385 ("Instructions" . ,(kernel:code-instructions o)))))
1386
1387 (defmethod inspected-parts ((o kernel:fdefn))
1388 (values (format nil "~A~% is a fdefn object." o)
1389 `(("Name" . ,(kernel:fdefn-name o))
1390 ("Function" . ,(kernel:fdefn-function o)))))
1391
1392
1393 ;;;; Profiling
1394 (defimplementation profile (fname)
1395 (eval `(profile:profile ,fname)))
1396
1397 (defimplementation unprofile (fname)
1398 (eval `(profile:unprofile ,fname)))
1399
1400 (defimplementation unprofile-all ()
1401 (eval '(profile:unprofile))
1402 "All functions unprofiled.")
1403
1404 (defimplementation profile-report ()
1405 (eval '(profile:report-time)))
1406
1407 (defimplementation profile-reset ()
1408 (eval '(profile:reset-time))
1409 "Reset profiling counters.")
1410
1411 (defimplementation profiled-functions ()
1412 profile:*timed-functions*)
1413
1414 (defimplementation profile-package (package callers methods)
1415 (profile:profile-all :package package
1416 :callers-p callers
1417 :methods methods))
1418
1419
1420 ;;;; Multiprocessing
1421
1422 #+mp
1423 (progn
1424 (defimplementation startup-multiprocessing ()
1425 ;; Threads magic: this never returns! But top-level becomes
1426 ;; available again.
1427 (mp::startup-idle-and-top-level-loops))
1428
1429 (defimplementation spawn (fn &key (name "Anonymous"))
1430 (mp:make-process fn :name name))
1431
1432 (defimplementation thread-name (thread)
1433 (mp:process-name thread))
1434
1435 (defimplementation thread-status (thread)
1436 (mp:process-whostate thread))
1437
1438 (defimplementation current-thread ()
1439 mp:*current-process*)
1440
1441 (defimplementation all-threads ()
1442 (copy-list mp:*all-processes*))
1443
1444 (defimplementation interrupt-thread (thread fn)
1445 (mp:process-interrupt thread fn))
1446
1447 (defimplementation kill-thread (thread)
1448 (mp:destroy-process thread))
1449
1450 (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
1451
1452 (defstruct (mailbox (:conc-name mailbox.))
1453 (mutex (mp:make-lock "process mailbox"))
1454 (queue '() :type list))
1455
1456 (defun mailbox (thread)
1457 "Return THREAD's mailbox."
1458 (mp:with-lock-held (*mailbox-lock*)
1459 (or (getf (mp:process-property-list thread) 'mailbox)
1460 (setf (getf (mp:process-property-list thread) 'mailbox)
1461 (make-mailbox)))))
1462
1463 (defimplementation send (thread message)
1464 (let* ((mbox (mailbox thread))
1465 (mutex (mailbox.mutex mbox)))
1466 (mp:with-lock-held (mutex)
1467 (setf (mailbox.queue mbox)
1468 (nconc (mailbox.queue mbox) (list message))))))
1469
1470 (defimplementation receive ()
1471 (let* ((mbox (mailbox mp:*current-process*))
1472 (mutex (mailbox.mutex mbox)))
1473 (mp:process-wait "receive" #'mailbox.queue mbox)
1474 (mp:with-lock-held (mutex)
1475 (pop (mailbox.queue mbox)))))
1476
1477 )
1478
1479 (defimplementation quit-lisp ()
1480 (ext::quit))

  ViewVC Help
Powered by ViewVC 1.1.5