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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.85 - (hide annotations)
Wed Mar 10 15:43:22 2004 UTC (10 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.84: +3 -3 lines
(unprofile-all): (eval '(profile:unprofile)) instead of just calling
it since it is a macro in cmucl.
1 lgorrie 1.24 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2 dbarlow 1.1
3 heller 1.59 (declaim (optimize (debug 2)))
4 dbarlow 1.1
5 heller 1.80 (in-package :swank-backend)
6 dbarlow 1.1
7 heller 1.67 (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 heller 1.79 (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 heller 1.67
39     (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
40     (when s
41     (setf (symbol-value s) t)))
42    
43     )
44    
45 heller 1.80 (in-package :swank-backend)
46 heller 1.67
47 lgorrie 1.25
48     ;;;; TCP server.
49    
50 heller 1.80 (defimplementation preferred-communication-style ()
51     :sigio)
52 heller 1.48
53 heller 1.63 (defimplementation create-socket (host port)
54 heller 1.64 (ext:create-inet-listener port :stream
55 heller 1.63 :reuse-address t
56     :host (resolve-hostname host)))
57 heller 1.47
58 lgorrie 1.56 (defimplementation local-port (socket)
59 heller 1.47 (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
60    
61 lgorrie 1.56 (defimplementation close-socket (socket)
62 heller 1.47 (ext:close-socket (socket-fd socket)))
63    
64 lgorrie 1.56 (defimplementation accept-connection (socket)
65 heller 1.59 #+MP (mp:process-wait-until-fd-usable socket :input)
66 heller 1.47 (make-socket-io-stream (ext:accept-tcp-connection socket)))
67    
68 heller 1.59 (defvar *sigio-handlers* '()
69     "List of (key . (fn . args)) pairs to be called on SIGIO.")
70    
71     (defun sigio-handler (signal code scp)
72 heller 1.65 (declare (ignore signal code scp))
73 heller 1.68 (mapc (lambda (handler) (funcall (cdr handler))) *sigio-handlers*))
74 heller 1.59
75     (defun set-sigio-handler ()
76     (sys:enable-interrupt unix:SIGIO (lambda (signal code scp)
77     (sigio-handler signal code scp))))
78    
79 heller 1.68 (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 heller 1.59 (let ((fd (socket-fd socket)))
87 heller 1.68 (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 lgorrie 1.45
92 heller 1.68 (defimplementation remove-sigio-handlers (socket)
93 heller 1.59 (let ((fd (socket-fd socket)))
94 heller 1.68 (setf *sigio-handlers* (delete fd *sigio-handlers* :key #'car))
95     (sys:invalidate-descriptor fd))
96 heller 1.50 (close socket))
97    
98 heller 1.68 (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 lgorrie 1.56 (defimplementation make-fn-streams (input-fn output-fn)
109 lgorrie 1.45 (let* ((output (make-slime-output-stream output-fn))
110     (input (make-slime-input-stream input-fn output)))
111     (values input output)))
112 lgorrie 1.25
113 lgorrie 1.44 ;;;;; Socket helpers.
114    
115 heller 1.47 (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 lgorrie 1.44
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 lgorrie 1.45 (defun make-socket-io-stream (fd)
128 lgorrie 1.44 "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 lgorrie 1.49 (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 lgorrie 1.25
140 heller 1.54 ;;;; Unix signals
141    
142     (defmethod call-without-interrupts (fn)
143 heller 1.70 (sys:without-interrupts (funcall fn)))
144 heller 1.54
145 heller 1.70 (defimplementation getpid ()
146 heller 1.54 (unix:unix-getpid))
147 heller 1.70
148     (defimplementation lisp-implementation-type-name ()
149     "cmucl")
150 heller 1.54
151    
152 lgorrie 1.25 ;;;; Stream handling
153 dbarlow 1.1
154     (defstruct (slime-output-stream
155 lgorrie 1.45 (: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 heller 1.16 (buffer (make-string 512) :type string)
164     (index 0 :type kernel:index)
165     (column 0 :type kernel:index))
166    
167 heller 1.17 (defun %print-slime-output-stream (s stream d)
168     (declare (ignore d))
169     (print-unreadable-object (s stream :type t :identity t)))
170    
171 heller 1.16 (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 heller 1.21 (when (char= #\newline char)
178     (setf (sos.column stream) 0))
179     (when (= index (1- (length buffer)))
180     (force-output stream)))
181 heller 1.16 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 lgorrie 1.45
187 heller 1.16 (defun sos/misc (stream operation &optional arg1 arg2)
188     (declare (ignore arg1 arg2))
189 dbarlow 1.1 (case operation
190 heller 1.17 ((:force-output :finish-output)
191 heller 1.16 (let ((end (sos.index stream)))
192     (unless (zerop end)
193 lgorrie 1.45 (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
194     (setf (sos.index stream) 0))))
195 heller 1.16 (:charpos (sos.column stream))
196     (:line-length 75)
197 dbarlow 1.1 (:file-position nil)
198 heller 1.16 (:element-type 'base-char)
199     (:get-command nil)
200 heller 1.22 (:close nil)
201 heller 1.16 (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
202 dbarlow 1.1
203 heller 1.9 (defstruct (slime-input-stream
204 lgorrie 1.45 (: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 heller 1.16
217     (defun sis/in (stream eof-errorp eof-value)
218 heller 1.18 (declare (ignore eof-errorp eof-value))
219 heller 1.16 (let ((index (sis.index stream))
220     (buffer (sis.buffer stream)))
221     (when (= index (length buffer))
222 lgorrie 1.45 (force-output (sis.sos stream))
223     (setf buffer (funcall (sis.input-fn stream)))
224 heller 1.16 (setf (sis.buffer stream) buffer)
225     (setf index 0))
226     (prog1 (aref buffer index)
227     (setf (sis.index stream) (1+ index)))))
228 heller 1.9
229 heller 1.16 (defun sis/misc (stream operation &optional arg1 arg2)
230 heller 1.14 (declare (ignore arg2))
231 heller 1.15 (ecase operation
232     (:file-position nil)
233 heller 1.16 (:file-length nil)
234     (:unread (setf (aref (sis.buffer stream)
235     (decf (sis.index stream)))
236     arg1))
237 heller 1.57 (:clear-input
238     (setf (sis.index stream) 0
239 heller 1.16 (sis.buffer stream) ""))
240     (:listen (< (sis.index stream) (length (sis.buffer stream))))
241     (:charpos nil)
242     (:line-length nil)
243     (:get-command nil)
244 heller 1.22 (:element-type 'base-char)
245     (:close nil)))
246 heller 1.16
247 lgorrie 1.25
248 dbarlow 1.1 ;;;; Compilation Commands
249    
250 lgorrie 1.24 (defvar *previous-compiler-condition* nil
251     "Used to detect duplicates.")
252    
253     (defvar *previous-context* nil
254     "Previous compiler error context.")
255    
256 heller 1.30 (defvar *buffer-name* nil)
257     (defvar *buffer-start-position* nil)
258     (defvar *buffer-substring* nil)
259    
260 lgorrie 1.25
261 lgorrie 1.24 ;;;;; Trapping notes
262    
263     (defun handle-notification-condition (condition)
264 dbarlow 1.1 "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 heller 1.65 (let ((context (c::find-error-context nil)))
271 dbarlow 1.1 (setq *previous-compiler-condition* condition)
272     (setq *previous-context* context)
273 lgorrie 1.24 (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 heller 1.65 :short-message (brief-compiler-message-for-emacs condition)
281     :message (long-compiler-message-for-emacs condition context)
282 lgorrie 1.24 :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 heller 1.65 (defun brief-compiler-message-for-emacs (condition)
292 lgorrie 1.24 "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 heller 1.65 (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 lgorrie 1.24 (declare (type (or c::compiler-error-context null) error-context))
301 heller 1.30 (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 heller 1.32 (defmethod resolve-note-location (b (f (eql :lisp)) pos path (source string))
334     (make-location
335     `(:source-form ,source)
336     `(:position 1)))
337    
338 heller 1.30 (defmethod resolve-note-location (buffer
339     (file (eql nil))
340     (pos (eql nil))
341     (path (eql nil))
342     (source (eql nil)))
343 heller 1.80 (list :error "No error location available"))
344 dbarlow 1.1
345 lgorrie 1.56 (defimplementation call-with-compilation-hooks (function)
346 dbarlow 1.38 (let ((*previous-compiler-condition* nil)
347     (*previous-context* nil)
348     (*print-readably* nil))
349 lgorrie 1.24 (handler-bind ((c::compiler-error #'handle-notification-condition)
350     (c::style-warning #'handle-notification-condition)
351     (c::warning #'handle-notification-condition))
352 dbarlow 1.38 (funcall function))))
353 lgorrie 1.24
354 heller 1.80 (defimplementation swank-compile-file (filename load-p)
355 lgorrie 1.24 (clear-xref-info filename)
356     (with-compilation-hooks ()
357 heller 1.80 (let ((*buffer-name* nil))
358 heller 1.74 (compile-file filename :load load-p))))
359 lgorrie 1.24
360 heller 1.80 (defimplementation swank-compile-string (string &key buffer position)
361 lgorrie 1.24 (with-compilation-hooks ()
362 heller 1.80 (let ((*buffer-name* buffer)
363 heller 1.30 (*buffer-start-position* position)
364     (*buffer-substring* string))
365 lgorrie 1.24 (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 dbarlow 1.1
372 lgorrie 1.25
373     ;;;; XREF
374 dbarlow 1.1
375 heller 1.81 (defmacro defxref (name function)
376     `(defimplementation ,name (name)
377 heller 1.82 (xref-results (,function name))))
378 heller 1.81
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 dbarlow 1.1
384 lgorrie 1.10 #+cmu19
385 heller 1.32 (progn
386 heller 1.81 (defxref who-macroexpands xref:who-macroexpands)
387     ;; XXX
388     (defimplementation who-specializes (symbol)
389     (let* ((methods (xref::who-specializes (find-class symbol)))
390 heller 1.32 (locations (mapcar #'method-source-location methods)))
391 heller 1.81 (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 heller 1.30
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 lgorrie 1.25 (defun clear-xref-info (namestring)
415 heller 1.66 "Clear XREF notes pertaining to NAMESTRING.
416 lgorrie 1.25 This is a workaround for a CMUCL bug: XREF records are cumulative."
417 heller 1.65 (when c:*record-xref-info*
418 heller 1.66 (let ((filename (truename namestring)))
419 lgorrie 1.25 (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 heller 1.66 ;; XXX update during traversal?
427 lgorrie 1.25 (setf (gethash target db)
428 heller 1.66 (delete filename contexts
429     :key #'xref:xref-context-file
430     :test #'equalp)))
431 lgorrie 1.25 db)))))
432    
433     (defun unix-truename (pathname)
434     (ext:unix-namestring (truename pathname)))
435    
436    
437     ;;;; Find callers and callees
438    
439 dbarlow 1.1 ;;; 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 heller 1.30 "Return FUNCTION's callees as a list of functions."
454 dbarlow 1.1 (let ((callees '()))
455     (map-code-constants
456     (vm::find-code-object function)
457     (lambda (obj)
458     (when (kernel:fdefn-p obj)
459 heller 1.30 (push (kernel:fdefn-function obj) callees))))
460 dbarlow 1.1 callees))
461    
462 heller 1.19 (declaim (ext:maybe-inline map-allocated-code-components))
463 dbarlow 1.1 (defun map-allocated-code-components (spaces fn)
464     "Call FN for each allocated code component in one of SPACES. FN
465 heller 1.19 receives the object as argument. SPACES should be a list of the
466     symbols :dynamic, :static, or :read-only."
467 dbarlow 1.1 (dolist (space spaces)
468 heller 1.18 (declare (inline vm::map-allocated-objects))
469 dbarlow 1.1 (vm::map-allocated-objects
470     (lambda (obj header size)
471 heller 1.19 (declare (type fixnum size) (ignore size))
472 dbarlow 1.1 (when (= vm:code-header-type header)
473 heller 1.19 (funcall fn obj)))
474 dbarlow 1.1 space)))
475    
476 heller 1.19 (declaim (ext:maybe-inline map-caller-code-components))
477 dbarlow 1.1 (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 heller 1.19 (declare (inline map-allocated-code-components))
482 dbarlow 1.1 (map-allocated-code-components
483     spaces
484 heller 1.19 (lambda (obj)
485 dbarlow 1.1 (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 heller 1.30 "Return FUNCTION's callers. The result is a list of code-objects."
496 dbarlow 1.1 (let ((referrers '()))
497 heller 1.19 (declare (inline map-caller-code-components))
498 heller 1.30 (ext:gc :full t)
499     (map-caller-code-components function spaces
500     (lambda (code) (push code referrers)))
501 dbarlow 1.1 referrers))
502 heller 1.80
503 heller 1.30 (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 heller 1.80 (list (list name
516     (make-location
517     (list :file (unix-truename (c::debug-source-name first)))
518     (list :function-name name)))))))))
519 heller 1.30
520     (defun code-component-entry-points (code)
521 heller 1.80 "Return a list ((NAME LOCATION) ...) of function definitons for
522 heller 1.30 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 heller 1.80 collect (list (kernel:%function-name e)
528 heller 1.30 (function-source-location e)))
529     :test #'equal))
530 dbarlow 1.1
531 heller 1.80 (defimplementation list-callers (symbol)
532     "Return a list ((NAME LOCATION) ...) of callers."
533     (let ((components (function-callers symbol))
534 heller 1.30 (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 heller 1.80 xrefs))
544 dbarlow 1.1
545 heller 1.80 (defimplementation list-callees (symbol)
546     (let ((fns (function-callees symbol)))
547     (mapcar (lambda (fn)
548     (list (kernel:%function-name fn)
549     (function-source-location fn)))
550     fns)))
551 dbarlow 1.1
552 lgorrie 1.42
553 dbarlow 1.1 ;;;; Definitions
554    
555 lgorrie 1.35 (defvar *debug-definition-finding* nil
556 dbarlow 1.1 "When true don't handle errors while looking for definitions.
557     This is useful when debugging the definition-finding code.")
558    
559 heller 1.29 (defmacro safe-definition-finding (&body body)
560 heller 1.43 "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 heller 1.29 `(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 dbarlow 1.1 (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 heller 1.83 (defun genericp (fn)
583     (typep fn 'generic-function))
584    
585 heller 1.4 (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 dbarlow 1.1
590 heller 1.4 (defun struct-closure-dd (function)
591 dbarlow 1.1 (assert (= (kernel:get-type function) vm:closure-header-type))
592 heller 1.4 (flet ((find-layout (function)
593     (sys:find-if-in-closure
594     (lambda (x)
595 heller 1.18 (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 heller 1.4 function)))
601     (kernel:layout-info (find-layout function))))
602    
603 dbarlow 1.1 (defun dd-source-location (dd)
604     (let ((constructor (or (kernel:dd-default-constructor dd)
605 heller 1.32 (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-source-location
611     (coerce (if (consp constructor) (car constructor) constructor)
612     'function))))
613 dbarlow 1.1
614 heller 1.83 (defun function-location (function)
615     "Return the source location for FUNCTION."
616 dbarlow 1.1 ;; 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 heller 1.4 (cond ((struct-closure-p function)
625 heller 1.83 (safe-definition-finding
626     (dd-source-location (struct-closure-dd function))))
627 heller 1.27 ((genericp function)
628 heller 1.83 (gf-location function))
629 heller 1.27 (t
630 heller 1.83 (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 (pcl:generic-function-name gf))))
661     (etypecase def-source
662     (pathname (make-name-in-file-location def-source name))
663     (cons
664     (destructuring-bind ((dg name) pathname) def-source
665     (declare (ignore dg))
666     (etypecase pathname
667     (pathname (make-name-in-file-location pathname (string name)))
668     (null `(:error ,(format nil "Cannot resolve: ~S" def-source)))))))))
669    
670     (defun gf-method-definitions (gf)
671     (mapcar #'method-definition (pcl::generic-function-methods gf)))
672    
673     (defun function-definitions (symbol)
674     "Return definitions in the \"function namespace\", i.e.,
675     regular functions, generic functions, methods and macros."
676     (cond ((macro-function symbol)
677 heller 1.84 (list `((defmacro ,symbol)
678 heller 1.83 ,(function-location (macro-function symbol)))))
679     ((special-operator-p symbol)
680     (list `((:special-operator ,symbol)
681     (:error ,(format nil "Don't know where `~A' is defined"
682     symbol)))))
683     ((fboundp symbol)
684     (let ((function (coerce symbol 'function)))
685     (cond ((genericp function)
686 heller 1.84 (cons (list `(defgeneric ,symbol)
687 heller 1.83 (function-location function))
688     (gf-method-definitions function)))
689     (t (list (list `(function ,symbol)
690     (function-location function)))))))))
691 dbarlow 1.1
692 heller 1.80 (defimplementation find-definitions (symbol)
693 heller 1.83 (function-definitions symbol))
694    
695 lgorrie 1.25
696     ;;;; Documentation.
697 dbarlow 1.1
698 lgorrie 1.56 (defimplementation describe-symbol-for-emacs (symbol)
699 dbarlow 1.1 (let ((result '()))
700 lgorrie 1.24 (flet ((doc (kind)
701     (or (documentation symbol kind) :not-documented))
702     (maybe-push (property value)
703     (when value
704     (setf result (list* property value result)))))
705 dbarlow 1.1 (maybe-push
706     :variable (multiple-value-bind (kind recorded-p)
707     (ext:info variable kind symbol)
708     (declare (ignore kind))
709     (if (or (boundp symbol) recorded-p)
710     (doc 'variable))))
711     (maybe-push
712 heller 1.27 :generic-function
713     (if (and (fboundp symbol)
714     (typep (fdefinition symbol) 'generic-function))
715     (doc 'function)))
716     (maybe-push
717     :function (if (and (fboundp symbol)
718     (not (typep (fdefinition symbol) 'generic-function)))
719     (doc 'function)))
720 dbarlow 1.1 (maybe-push
721     :setf (if (or (ext:info setf inverse symbol)
722     (ext:info setf expander symbol))
723     (doc 'setf)))
724     (maybe-push
725     :type (if (ext:info type kind symbol)
726     (doc 'type)))
727     (maybe-push
728     :class (if (find-class symbol nil)
729     (doc 'class)))
730 heller 1.18 (maybe-push
731     :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
732     (doc 'alien-type)))
733     (maybe-push
734     :alien-struct (if (ext:info alien-type struct symbol)
735     (doc nil)))
736     (maybe-push
737     :alien-union (if (ext:info alien-type union symbol)
738     (doc nil)))
739     (maybe-push
740     :alien-enum (if (ext:info alien-type enum symbol)
741     (doc nil)))
742 lgorrie 1.24 result)))
743 dbarlow 1.1
744 heller 1.80 (defimplementation describe-definition (symbol namespace)
745     (ecase namespace
746 lgorrie 1.56 (:variable
747 heller 1.80 (describe symbol))
748 lgorrie 1.56 ((:function :generic-function)
749 heller 1.80 (describe (symbol-function symbol)))
750 lgorrie 1.56 (:setf
751 heller 1.80 (describe (or (ext:info setf inverse symbol))
752     (ext:info setf expander symbol)))
753 lgorrie 1.56 (:type
754 heller 1.80 (describe (kernel:values-specifier-type symbol)))
755 lgorrie 1.56 (:class
756 heller 1.80 (describe (find-class symbol)))
757 lgorrie 1.56 (:alien-type
758 heller 1.80 (ecase (ext:info :alien-type :kind symbol)
759     (:primitive
760     (describe (let ((alien::*values-type-okay* t))
761     (funcall (ext:info :alien-type :translator symbol)
762     (list symbol)))))
763     ((:defined)
764     (describe (ext:info :alien-type :definition symbol)))
765     (:unknown
766     (format nil "Unkown alien type: ~S" symbol))))
767 lgorrie 1.56 (:alien-struct
768 heller 1.80 (describe (ext:info :alien-type :struct symbol)))
769 lgorrie 1.56 (:alien-union
770 heller 1.80 (describe (ext:info :alien-type :union symbol)))
771 lgorrie 1.56 (:alien-enum
772 heller 1.80 (describe (ext:info :alien-type :enum symbol)))))
773 heller 1.18
774 heller 1.80 (defimplementation arglist (symbol)
775     (let* ((fun (or (macro-function symbol)
776     (symbol-function symbol)))
777     (arglist
778     (cond ((eval:interpreted-function-p fun)
779     (eval:interpreted-function-arglist fun))
780     ((pcl::generic-function-p fun)
781     (pcl:generic-function-lambda-list fun))
782     ((kernel:%function-arglist (kernel:%function-self fun)))
783     ;; this should work both for
784     ;; compiled-debug-function and for
785     ;; interpreted-debug-function
786     (t (let ((df (di::function-debug-function fun)))
787     (if df
788     (di::debug-function-lambda-list df)
789     "(<arglist-unavailable>)"))))))
790     (check-type arglist (or list string))
791     arglist))
792 lgorrie 1.25
793    
794     ;;;; Miscellaneous.
795 dbarlow 1.1
796 lgorrie 1.56 (defimplementation macroexpand-all (form)
797 lgorrie 1.24 (walker:macroexpand-all form))
798 dbarlow 1.1
799 heller 1.80 ;; (in-package :c)
800     ;;
801     ;; (defun swank-backend::expand-ir1-top-level (form)
802     ;; "A scaled down version of the first pass of the compiler."
803     ;; (with-compilation-unit ()
804     ;; (let* ((*lexical-environment*
805     ;; (make-lexenv :default (make-null-environment)
806     ;; :cookie *default-cookie*
807     ;; :interface-cookie *default-interface-cookie*))
808     ;; (*source-info* (make-lisp-source-info form))
809     ;; (*block-compile* nil)
810     ;; (*block-compile-default* nil))
811     ;; (with-ir1-namespace
812     ;; (clear-stuff)
813     ;; (find-source-paths form 0)
814     ;; (ir1-top-level form '(0) t)))))
815     ;;
816     ;; (in-package :swank-backend)
817     ;;
818     ;; (defun print-ir1-converted-blocks (form)
819     ;; (with-output-to-string (*standard-output*)
820     ;; (c::print-all-blocks (expand-ir1-top-level (from-string form)))))
821     ;;
822     ;; (defun print-compilation-trace (form)
823     ;; (with-output-to-string (*standard-output*)
824     ;; (with-input-from-string (s form)
825     ;; (ext:compile-from-stream s
826     ;; :verbose t
827     ;; :progress t
828     ;; :trace-stream *standard-output*))))
829 heller 1.37
830 heller 1.80 (defun set-default-directory (directory)
831 lgorrie 1.25 (setf (ext:default-directory) (namestring directory))
832     ;; Setting *default-pathname-defaults* to an absolute directory
833     ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
834     (setf *default-pathname-defaults* (pathname (ext:default-directory)))
835     (namestring (ext:default-directory)))
836    
837 dbarlow 1.39 ;;; source-path-{stream,file,string,etc}-position moved into
838     ;;; swank-source-path-parser
839 heller 1.19
840     (defun code-location-stream-position (code-location stream)
841     "Return the byte offset of CODE-LOCATION in STREAM. Extract the
842     toplevel-form-number and form-number from CODE-LOCATION and use that
843     to find the position of the corresponding form."
844     (let* ((location (debug::maybe-block-start-location code-location))
845     (tlf-offset (di:code-location-top-level-form-offset location))
846     (form-number (di:code-location-form-number location))
847     (*read-suppress* t))
848     (dotimes (i tlf-offset) (read stream))
849     (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)
850     (let* ((path-table (di:form-number-translations tlf 0))
851 heller 1.30 (source-path
852     (if (<= (length path-table) form-number) ; source out of sync?
853     (list 0) ; should probably signal a condition
854     (reverse (cdr (aref path-table form-number))))))
855 heller 1.19 (source-path-source-position source-path tlf position-map)))))
856    
857     (defun code-location-string-offset (code-location string)
858     (with-input-from-string (s string)
859     (code-location-stream-position code-location s)))
860    
861     (defun code-location-file-position (code-location filename)
862     (with-open-file (s filename :direction :input)
863     (code-location-stream-position code-location s)))
864    
865     (defun debug-source-info-from-emacs-buffer-p (debug-source)
866     (let ((info (c::debug-source-info debug-source)))
867     (and info
868     (consp info)
869     (eq :emacs-buffer (car info)))))
870    
871 heller 1.30 (defun source-location-from-code-location (code-location)
872 heller 1.29 "Return the source location for CODE-LOCATION."
873 heller 1.30 (let ((debug-fun (di:code-location-debug-function code-location)))
874     (when (di::bogus-debug-function-p debug-fun)
875     (error "Bogus debug function: ~A" debug-fun)))
876 heller 1.19 (let* ((debug-source (di:code-location-debug-source code-location))
877 heller 1.30 (from (di:debug-source-from debug-source))
878     (name (di:debug-source-name debug-source)))
879 heller 1.19 (ecase from
880 heller 1.32 (:file
881     (make-location (list :file (unix-truename name))
882     (list :position (1+ (code-location-file-position
883     code-location name)))))
884 heller 1.19 (:stream
885     (assert (debug-source-info-from-emacs-buffer-p debug-source))
886     (let ((info (c::debug-source-info debug-source)))
887 heller 1.32 (make-location
888     (list :buffer (getf info :emacs-buffer))
889     (list :position (+ (getf info :emacs-buffer-offset)
890     (code-location-string-offset
891     code-location
892     (getf info :emacs-buffer-string)))))))
893 heller 1.19 (:lisp
894 heller 1.32 (make-location
895     (list :source-form (with-output-to-string (*standard-output*)
896     (debug::print-code-location-source-form
897     code-location 100 t)))
898     (list :position 1))))))
899 heller 1.19
900 heller 1.29 (defun code-location-source-location (code-location)
901     "Safe wrapper around `code-location-from-source-location'."
902     (safe-definition-finding
903 heller 1.30 (source-location-from-code-location code-location)))
904 dbarlow 1.1
905    
906 lgorrie 1.25 ;;;; Debugging
907 dbarlow 1.1
908     (defvar *sldb-stack-top*)
909    
910 lgorrie 1.56 (defimplementation call-with-debugging-environment (debugger-loop-fn)
911 dbarlow 1.1 (unix:unix-sigsetmask 0)
912 lgorrie 1.25 (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
913 heller 1.75 (debug:*stack-top-hint* nil))
914 dbarlow 1.1 (handler-bind ((di:debug-condition
915     (lambda (condition)
916 lgorrie 1.25 (signal (make-condition
917     'sldb-condition
918     :original-condition condition)))))
919     (funcall debugger-loop-fn))))
920 dbarlow 1.1
921     (defun nth-frame (index)
922     (do ((frame *sldb-stack-top* (di:frame-down frame))
923     (i index (1- i)))
924     ((zerop i) frame)))
925    
926 heller 1.80 (defimplementation compute-backtrace (start end)
927 dbarlow 1.1 (let ((end (or end most-positive-fixnum)))
928     (loop for f = (nth-frame start) then (di:frame-down f)
929     for i from start below end
930     while f
931 heller 1.80 collect f)))
932 dbarlow 1.1
933 heller 1.80 (defimplementation print-frame (frame stream)
934     (let ((*standard-output* stream))
935     (debug::print-frame-call frame :verbosity 1 :number nil)))
936 dbarlow 1.1
937 lgorrie 1.56 (defimplementation frame-source-location-for-emacs (index)
938 heller 1.29 (code-location-source-location (di:frame-code-location (nth-frame index))))
939 dbarlow 1.1
940 lgorrie 1.56 (defimplementation eval-in-frame (form index)
941 lgorrie 1.26 (di:eval-in-frame (nth-frame index) form))
942 heller 1.19
943 lgorrie 1.56 (defimplementation frame-locals (index)
944 dbarlow 1.1 (let* ((frame (nth-frame index))
945     (location (di:frame-code-location frame))
946     (debug-function (di:frame-debug-function frame))
947     (debug-variables (di::debug-function-debug-variables debug-function)))
948 heller 1.43 (loop for v across debug-variables collect
949 heller 1.80 (list :name (di:debug-variable-symbol v)
950 heller 1.43 :id (di:debug-variable-id v)
951 mbaringer 1.77 :value (ecase (di:debug-variable-validity v location)
952     (:valid
953     (di:debug-variable-value v frame))
954     ((:invalid :unknown)
955 heller 1.80 ':not-available))))))
956 dbarlow 1.1
957 lgorrie 1.56 (defimplementation frame-catch-tags (index)
958 heller 1.80 (mapcar #'car (di:frame-catches (nth-frame index))))
959 heller 1.33
960     (defun set-step-breakpoints (frame)
961     (when (di:debug-block-elsewhere-p (di:code-location-debug-block
962     (di:frame-code-location frame)))
963     (error "Cannot step, in elsewhere code~%"))
964     (let* ((code-location (di:frame-code-location frame))
965 heller 1.36 (debug::*bad-code-location-types*
966     (remove :call-site debug::*bad-code-location-types*))
967 heller 1.33 (next (debug::next-code-locations code-location)))
968     (cond (next
969     (let ((steppoints '()))
970     (flet ((hook (frame breakpoint)
971     (let ((debug:*stack-top-hint* frame))
972 heller 1.36 (mapc #'di:delete-breakpoint steppoints)
973     (let ((cl (di::breakpoint-what breakpoint)))
974     (break "Breakpoint: ~S ~S"
975     (di:code-location-kind cl)
976     (di::compiled-code-location-pc cl))))))
977 heller 1.33 (dolist (code-location next)
978     (let ((bp (di:make-breakpoint #'hook code-location
979     :kind :code-location)))
980     (di:activate-breakpoint bp)
981     (push bp steppoints))))))
982     (t
983     (flet ((hook (frame breakpoint values cookie)
984     (declare (ignore cookie))
985 heller 1.36 (di:delete-breakpoint breakpoint)
986 heller 1.33 (let ((debug:*stack-top-hint* frame))
987     (break "Function-end: ~A ~A" breakpoint values))))
988     (let* ((debug-function (di:frame-debug-function frame))
989     (bp (di:make-breakpoint #'hook debug-function
990     :kind :function-end)))
991     (di:activate-breakpoint bp)))))))
992    
993 heller 1.80 ;; (defslimefun sldb-step (frame)
994     ;; (cond ((find-restart 'continue *swank-debugger-condition*)
995     ;; (set-step-breakpoints (nth-frame frame))
996     ;; (continue *swank-debugger-condition*))
997     ;; (t
998     ;; (error "Cannot continue in from condition: ~A"
999     ;; *swank-debugger-condition*))))
1000 dbarlow 1.1
1001 heller 1.58 (defun frame-cfp (frame)
1002     "Return the Control-Stack-Frame-Pointer for FRAME."
1003     (etypecase frame
1004     (di::compiled-frame (di::frame-pointer frame))
1005     ((or di::interpreted-frame null) -1)))
1006    
1007     (defun frame-ip (frame)
1008     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1009     (if (not frame)
1010     -1
1011     (let ((debug-fun (di::frame-debug-function frame)))
1012     (etypecase debug-fun
1013     (di::compiled-debug-function
1014     (let* ((code-loc (di:frame-code-location frame))
1015     (component (di::compiled-debug-function-component debug-fun))
1016     (pc (di::compiled-code-location-pc code-loc))
1017     (ip (sys:without-gcing
1018     (sys:sap-int
1019     (sys:sap+ (kernel:code-instructions component) pc)))))
1020     (values ip pc)))
1021     ((or di::bogus-debug-function di::interpreted-debug-function)
1022     -1)))))
1023    
1024     (defun frame-registers (frame)
1025     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1026     (let* ((cfp (frame-cfp frame))
1027     (csp (frame-cfp (di::frame-up frame)))
1028     (ip (frame-ip frame))
1029     (ocfp (frame-cfp (di::frame-down frame)))
1030     (lra (frame-ip (di::frame-down frame))))
1031     (values csp cfp ip ocfp lra)))
1032    
1033     (defun print-frame-registers (frame-number)
1034     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1035     (flet ((fixnum (p) (etypecase p
1036     (integer p)
1037     (sys:system-area-pointer (sys:sap-int p)))))
1038     (apply #'format t "~
1039     CSP = ~X
1040     CFP = ~X
1041     IP = ~X
1042     OCFP = ~X
1043     LRA = ~X~%" (mapcar #'fixnum
1044     (multiple-value-list (frame-registers frame)))))))
1045    
1046 heller 1.82
1047 heller 1.81 (defimplementation disassemble-frame (frame-number)
1048     "Return a string with the disassembly of frames code."
1049     (print-frame-registers frame-number)
1050     (terpri)
1051     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1052     (debug-fun (di::frame-debug-function frame)))
1053     (etypecase debug-fun
1054     (di::compiled-debug-function
1055     (let* ((component (di::compiled-debug-function-component debug-fun))
1056     (fun (di:debug-function-function debug-fun)))
1057     (if fun
1058     (disassemble fun)
1059     (disassem:disassemble-code-component component))))
1060     (di::bogus-debug-function
1061     (format t "~%[Disassembling bogus frames not implemented]")))))
1062 heller 1.58
1063     #+(or)
1064     (defun print-binding-stack ()
1065 heller 1.60 (flet ((bsp- (p) (sys:sap+ p (- (* vm:binding-size vm:word-bytes))))
1066     (frob (p offset) (kernel:make-lisp-obj (sys:sap-ref-32 p offset))))
1067     (do ((bsp (bsp- (kernel:binding-stack-pointer-sap)) (bsp- bsp))
1068     (start (sys:int-sap (lisp::binding-stack-start))))
1069     ((sys:sap= bsp start))
1070     (format t "~X: ~S = ~S~%"
1071     (sys:sap-int bsp)
1072     (frob bsp (* vm:binding-symbol-slot vm:word-bytes))
1073     (frob bsp (* vm:binding-value-slot vm:word-bytes))))))
1074 heller 1.58
1075     ;; (print-binding-stack)
1076    
1077     #+(or)
1078     (defun print-catch-blocks ()
1079     (do ((b (di::descriptor-sap lisp::*current-catch-block*)
1080     (sys:sap-ref-sap b (* vm:catch-block-previous-catch-slot
1081     vm:word-bytes))))
1082     (nil)
1083     (let ((int (sys:sap-int b)))
1084     (when (zerop int) (return))
1085     (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1086     (let ((uwp (ref vm:catch-block-current-uwp-slot))
1087     (cfp (ref vm:catch-block-current-cont-slot))
1088     (tag (ref vm:catch-block-tag-slot))
1089     )
1090     (format t "~X: uwp = ~8X cfp = ~8X tag = ~X~%"
1091     int uwp cfp (kernel:make-lisp-obj tag)))))))
1092    
1093 heller 1.60 ;; (print-catch-blocks)
1094 heller 1.58
1095     #+(or)
1096     (defun print-unwind-blocks ()
1097     (do ((b (di::descriptor-sap lisp::*current-unwind-protect-block*)
1098     (sys:sap-ref-sap b (* vm:unwind-block-current-uwp-slot
1099     vm:word-bytes))))
1100     (nil)
1101     (let ((int (sys:sap-int b)))
1102     (when (zerop int) (return))
1103     (flet ((ref (offset) (sys:sap-ref-32 b (* offset vm:word-bytes))))
1104     (let ((cfp (ref vm:unwind-block-current-cont-slot)))
1105     (format t "~X: cfp = ~X~%" int cfp))))))
1106    
1107     ;; (print-unwind-blocks)
1108    
1109 dbarlow 1.1
1110 lgorrie 1.25 ;;;; Inspecting
1111 dbarlow 1.1
1112     (defconstant +lowtag-symbols+
1113     '(vm:even-fixnum-type
1114     vm:function-pointer-type
1115     vm:other-immediate-0-type
1116     vm:list-pointer-type
1117     vm:odd-fixnum-type
1118     vm:instance-pointer-type
1119     vm:other-immediate-1-type
1120     vm:other-pointer-type))
1121    
1122     (defconstant +header-type-symbols+
1123     ;; Is there a convinient place for all those constants?
1124     (flet ((tail-comp (string tail)
1125     (and (>= (length string) (length tail))
1126     (string= string tail :start1 (- (length string)
1127     (length tail))))))
1128     (remove-if-not
1129     (lambda (x) (and (tail-comp (symbol-name x) "-TYPE")
1130     (not (member x +lowtag-symbols+))
1131     (boundp x)
1132     (typep (symbol-value x) 'fixnum)))
1133     (append (apropos-list "-TYPE" "VM" t)
1134     (apropos-list "-TYPE" "BIGNUM" t)))))
1135    
1136 heller 1.62
1137     (defimplementation describe-primitive-type (object)
1138 dbarlow 1.1 (with-output-to-string (*standard-output*)
1139     (let* ((lowtag (kernel:get-lowtag object))
1140     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1141 heller 1.76 (format t "lowtag: ~A" lowtag-symbol)
1142     (when (member lowtag (list vm:other-pointer-type
1143     vm:function-pointer-type
1144     vm:other-immediate-0-type
1145     vm:other-immediate-1-type
1146     ))
1147     (let* ((type (kernel:get-type object))
1148     (type-symbol (find type +header-type-symbols+
1149     :key #'symbol-value)))
1150     (format t ", type: ~A" type-symbol))))))
1151 dbarlow 1.1
1152 heller 1.62 (defimplementation inspected-parts (o)
1153 dbarlow 1.1 (cond ((di::indirect-value-cell-p o)
1154     (inspected-parts-of-value-cell o))
1155     (t
1156     (destructuring-bind (text labeledp . parts)
1157     (inspect::describe-parts o)
1158     (let ((parts (if labeledp
1159     (loop for (label . value) in parts
1160     collect (cons (string label) value))
1161     (loop for value in parts
1162     for i from 0
1163     collect (cons (format nil "~D" i) value)))))
1164     (values text parts))))))
1165    
1166 heller 1.55 (defun inspected-parts-of-value-cell (o)
1167     (values (format nil "~A~% is a value cell." o)
1168     (list (cons "Value" (c:value-cell-ref o)))))
1169    
1170 dbarlow 1.1 (defmethod inspected-parts ((o function))
1171     (let ((header (kernel:get-type o)))
1172     (cond ((= header vm:function-header-type)
1173     (values
1174     (format nil "~A~% is a function." o)
1175     (list (cons "Self" (kernel:%function-self o))
1176     (cons "Next" (kernel:%function-next o))
1177     (cons "Name" (kernel:%function-name o))
1178     (cons "Arglist" (kernel:%function-arglist o))
1179     (cons "Type" (kernel:%function-type o))
1180     (cons "Code Object" (kernel:function-code-header o)))))
1181     ((= header vm:closure-header-type)
1182     (values (format nil "~A~% is a closure." o)
1183     (list*
1184     (cons "Function" (kernel:%closure-function o))
1185     (loop for i from 0 below (- (kernel:get-closure-length o)
1186     (1- vm:closure-info-offset))
1187     collect (cons (format nil "~D" i)
1188     (kernel:%closure-index-ref o i))))))
1189     (t (call-next-method o)))))
1190    
1191     (defmethod inspected-parts ((o kernel:code-component))
1192     (values (format nil "~A~% is a code data-block." o)
1193     `(("First entry point" . ,(kernel:%code-entry-points o))
1194     ,@(loop for i from vm:code-constants-offset
1195     below (kernel:get-header-data o)
1196     collect (cons (format nil "Constant#~D" i)
1197     (kernel:code-header-ref o i)))
1198     ("Debug info" . ,(kernel:%code-debug-info o))
1199     ("Instructions" . ,(kernel:code-instructions o)))))
1200    
1201     (defmethod inspected-parts ((o kernel:fdefn))
1202     (values (format nil "~A~% is a fdefn object." o)
1203     `(("Name" . ,(kernel:fdefn-name o))
1204     ("Function" . ,(kernel:fdefn-function o)))))
1205 heller 1.58
1206    
1207     ;;;; Profiling
1208     (defimplementation profile (fname)
1209     (eval `(profile:profile ,fname)))
1210    
1211     (defimplementation unprofile (fname)
1212     (eval `(profile:unprofile ,fname)))
1213    
1214     (defimplementation unprofile-all ()
1215 heller 1.85 (eval '(profile:unprofile))
1216 heller 1.58 "All functions unprofiled.")
1217    
1218     (defimplementation profile-report ()
1219 heller 1.85 (eval '(profile:report-time)))
1220 heller 1.58
1221     (defimplementation profile-reset ()
1222 heller 1.85 (eval '(profile:reset-time))
1223 heller 1.58 "Reset profiling counters.")
1224    
1225     (defimplementation profiled-functions ()
1226     profile:*timed-functions*)
1227    
1228     (defimplementation profile-package (package callers methods)
1229 heller 1.80 (profile:profile-all :package package
1230 heller 1.58 :callers-p callers
1231     :methods methods))
1232 dbarlow 1.1
1233 lgorrie 1.42
1234     ;;;; Multiprocessing
1235    
1236     #+MP
1237     (progn
1238 lgorrie 1.56 (defimplementation startup-multiprocessing ()
1239 lgorrie 1.49 ;; Threads magic: this never returns! But top-level becomes
1240     ;; available again.
1241 lgorrie 1.42 (mp::startup-idle-and-top-level-loops))
1242 heller 1.54
1243 heller 1.59 (defimplementation spawn (fn &key (name "Anonymous"))
1244 heller 1.54 (mp:make-process fn :name name))
1245 lgorrie 1.42
1246 heller 1.61 (defimplementation thread-name (thread)
1247     (mp:process-name thread))
1248 lgorrie 1.42
1249 heller 1.61 (defimplementation thread-status (thread)
1250     (mp:process-whostate thread))
1251 lgorrie 1.42
1252 heller 1.61 (defimplementation current-thread ()
1253     mp:*current-process*)
1254 lgorrie 1.42
1255 heller 1.61 (defimplementation all-threads ()
1256     (copy-list mp:*all-processes*))
1257 heller 1.62
1258     (defimplementation interrupt-thread (thread fn)
1259     (mp:process-interrupt thread fn))
1260    
1261 heller 1.73 (defimplementation kill-thread (thread)
1262     (mp:destroy-process thread))
1263    
1264 heller 1.62 (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
1265    
1266     (defstruct (mailbox (:conc-name mailbox.))
1267     (mutex (mp:make-lock "process mailbox"))
1268     (queue '() :type list))
1269    
1270     (defun mailbox (thread)
1271     "Return THREAD's mailbox."
1272     (mp:with-lock-held (*mailbox-lock*)
1273     (or (getf (mp:process-property-list thread) 'mailbox)
1274     (setf (getf (mp:process-property-list thread) 'mailbox)
1275     (make-mailbox)))))
1276    
1277     (defimplementation send (thread message)
1278     (let* ((mbox (mailbox thread))
1279     (mutex (mailbox.mutex mbox)))
1280     (mp:with-lock-held (mutex)
1281     (setf (mailbox.queue mbox)
1282     (nconc (mailbox.queue mbox) (list message))))))
1283    
1284     (defimplementation receive ()
1285     (let* ((mbox (mailbox mp:*current-process*))
1286     (mutex (mailbox.mutex mbox)))
1287     (mp:process-wait "receive" #'mailbox.queue mbox)
1288     (mp:with-lock-held (mutex)
1289     (pop (mailbox.queue mbox)))))
1290 heller 1.59
1291     )

  ViewVC Help
Powered by ViewVC 1.1.5