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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.115 - (hide annotations)
Mon Sep 13 16:42:31 2004 UTC (9 years, 7 months ago) by mbaringer
Branch: MAIN
Changes since 1.114: +77 -20 lines
2004-09-13  Marco Baringer  <mb@bese.it>

	* swank.lisp (inspected-parts): Added inspectors for pathnames,
	logical pathnames, standard-objects and numbers (float, ratio,
	integer and complex).

	* swank-backend.lisp: Define import-to-swank-mop.

	* swank-openmcl.lisp, swank-sbcl.lisp, swank-allegro.lisp: Don't
	define the import-to-swank-mop function (now defined in
	swank-backend.lisp).

	* swank-cmucl.lisp (swank-mop, function-name): Implement backend
	for inspector.
	(arglist): Add support for extracting arglists from function
	objects.
	(create-socket): Don't specify the host on PPC.
1 lgorrie 1.103 ;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;+" -*-
2     ;;;
3 heller 1.108 ;;; License: Public Domain
4     ;;;
5 lgorrie 1.103 ;;;; Introduction
6     ;;;
7     ;;; This is the CMUCL implementation of the `swank-backend' package.
8 dbarlow 1.1
9 heller 1.80 (in-package :swank-backend)
10 dbarlow 1.1
11 lgorrie 1.103 ;;;; "Hot fixes"
12     ;;;
13     ;;; Here are necessary bugfixes to the latest released version of
14     ;;; CMUCL (currently 18e). Any fixes placed here should also be
15     ;;; submitted to the `cmucl-imp' mailing list and confirmed as
16     ;;; good. When a new release is made that includes the fixes we should
17     ;;; promptly delete them from here. It is enough to be compatible with
18     ;;; the latest release.
19    
20 mbaringer 1.115 (import-to-swank-mop
21     '( ;; classes
22     cl:standard-generic-function
23     pcl:standard-slot-definition
24     cl:method
25     cl:standard-class
26     ;; standard-class readers
27     pcl:class-default-initargs
28     pcl:class-direct-default-initargs
29     pcl:class-direct-slots
30     pcl:class-direct-subclasses
31     pcl:class-direct-superclasses
32     pcl:class-finalized-p
33     cl:class-name
34     pcl:class-precedence-list
35     pcl:class-prototype
36     pcl:class-slots
37     ;; generic function readers
38     pcl:generic-function-argument-precedence-order
39     pcl:generic-function-declarations
40     pcl:generic-function-lambda-list
41     pcl:generic-function-methods
42     pcl:generic-function-method-class
43     pcl:generic-function-method-combination
44     pcl:generic-function-name
45     ;; method readers
46     pcl:method-generic-function
47     pcl:method-function
48     pcl:method-lambda-list
49     pcl:method-specializers
50     pcl:method-qualifiers
51     ;; slot readers
52     pcl:slot-definition-allocation
53     pcl:slot-definition-initargs
54     pcl:slot-definition-initform
55     pcl:slot-definition-initfunction
56     pcl:slot-definition-name
57     pcl:slot-definition-type
58     pcl:slot-definition-readers
59     pcl:slot-definition-writers))
60    
61     (defun swank-mop:slot-definition-documentation (slot)
62     (documentation slot t))
63    
64 heller 1.67 (in-package :lisp)
65    
66 lgorrie 1.103 ;;; `READ-SEQUENCE' with large sequences has problems in 18e. This new
67     ;;; definition works better.
68    
69 heller 1.67 #+cmu18e
70     (progn
71     (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
72     (when s
73     (setf (symbol-value s) nil)))
74    
75     (defun read-into-simple-string (s stream start end)
76     (declare (type simple-string s))
77     (declare (type stream stream))
78     (declare (type index start end))
79     (unless (subtypep (stream-element-type stream) 'character)
80     (error 'type-error
81     :datum (read-char stream nil #\Null)
82     :expected-type (stream-element-type stream)
83     :format-control "Trying to read characters from a binary stream."))
84     ;; Let's go as low level as it seems reasonable.
85     (let* ((numbytes (- end start))
86 heller 1.79 (total-bytes 0))
87     ;; read-n-bytes may return fewer bytes than requested, so we need
88     ;; to keep trying.
89     (loop while (plusp numbytes) do
90     (let ((bytes-read (system:read-n-bytes stream s start numbytes nil)))
91     (when (zerop bytes-read)
92     (return-from read-into-simple-string total-bytes))
93     (incf total-bytes bytes-read)
94     (incf start bytes-read)
95     (decf numbytes bytes-read)))
96     total-bytes))
97 heller 1.67
98     (let ((s (find-symbol (string :*enable-package-locked-errors*) :lisp)))
99     (when s
100     (setf (symbol-value s) t)))
101    
102     )
103    
104 heller 1.80 (in-package :swank-backend)
105 heller 1.67
106 lgorrie 1.25
107 lgorrie 1.103 ;;;; TCP server
108     ;;;
109     ;;; In CMUCL we support all communication styles. By default we use
110     ;;; `:SIGIO' because it is the most responsive, but it's somewhat
111     ;;; dangerous: CMUCL is not in general "signal safe", and you don't
112     ;;; know for sure what you'll be interrupting. Both `:FD-HANDLER' and
113     ;;; `:SPAWN' are reasonable alternatives.
114 lgorrie 1.25
115 heller 1.80 (defimplementation preferred-communication-style ()
116     :sigio)
117 heller 1.48
118 heller 1.63 (defimplementation create-socket (host port)
119 mbaringer 1.115 #+ppc (declare (ignore host))
120 heller 1.64 (ext:create-inet-listener port :stream
121 heller 1.63 :reuse-address t
122 mbaringer 1.115 #-ppc :host #-ppc (resolve-hostname host)))
123 heller 1.47
124 lgorrie 1.56 (defimplementation local-port (socket)
125 heller 1.47 (nth-value 1 (ext::get-socket-host-and-port (socket-fd socket))))
126    
127 lgorrie 1.56 (defimplementation close-socket (socket)
128 lgorrie 1.106 (sys:invalidate-descriptor socket)
129 heller 1.47 (ext:close-socket (socket-fd socket)))
130    
131 lgorrie 1.56 (defimplementation accept-connection (socket)
132 heller 1.47 (make-socket-io-stream (ext:accept-tcp-connection socket)))
133    
134 lgorrie 1.103 ;;;;; Sockets
135    
136     (defun socket-fd (socket)
137     "Return the filedescriptor for the socket represented by SOCKET."
138     (etypecase socket
139     (fixnum socket)
140     (sys:fd-stream (sys:fd-stream-fd socket))))
141    
142     (defun resolve-hostname (hostname)
143     "Return the IP address of HOSTNAME as an integer."
144     (let* ((hostent (ext:lookup-host-entry hostname))
145     (address (car (ext:host-entry-addr-list hostent))))
146     (ext:htonl address)))
147    
148     (defun make-socket-io-stream (fd)
149     "Create a new input/output fd-stream for FD."
150     (sys:make-fd-stream fd :input t :output t :element-type 'base-char))
151    
152     ;;;;; Signal-driven I/O
153    
154 heller 1.59 (defvar *sigio-handlers* '()
155 lgorrie 1.103 "List of (key . function) pairs.
156     All functions are called on SIGIO, and the key is used for removing
157     specific functions.")
158 heller 1.59
159     (defun set-sigio-handler ()
160 heller 1.101 (sys:enable-interrupt :sigio (lambda (signal code scp)
161     (sigio-handler signal code scp))))
162 heller 1.59
163 lgorrie 1.103 (defun sigio-handler (signal code scp)
164     (declare (ignore signal code scp))
165     (mapc #'funcall (mapcar #'cdr *sigio-handlers*)))
166    
167 heller 1.68 (defun fcntl (fd command arg)
168 lgorrie 1.103 "fcntl(2) - manipulate a file descriptor."
169 heller 1.68 (multiple-value-bind (ok error) (unix:unix-fcntl fd command arg)
170 lgorrie 1.103 (unless ok (error "fcntl: ~A" (unix:get-unix-error-msg error)))))
171 heller 1.68
172     (defimplementation add-sigio-handler (socket fn)
173     (set-sigio-handler)
174 heller 1.59 (let ((fd (socket-fd socket)))
175 heller 1.68 (fcntl fd unix:f-setown (unix:unix-getpid))
176 heller 1.92 (fcntl fd unix:f-setfl unix:fasync)
177 heller 1.68 (push (cons fd fn) *sigio-handlers*)))
178 lgorrie 1.45
179 heller 1.68 (defimplementation remove-sigio-handlers (socket)
180 heller 1.59 (let ((fd (socket-fd socket)))
181 lgorrie 1.103 (setf *sigio-handlers* (remove fd *sigio-handlers* :key #'car))
182 heller 1.68 (sys:invalidate-descriptor fd))
183 heller 1.50 (close socket))
184    
185 lgorrie 1.103 ;;;;; SERVE-EVENT
186    
187 heller 1.68 (defimplementation add-fd-handler (socket fn)
188     (let ((fd (socket-fd socket)))
189     (sys:add-fd-handler fd :input (lambda (_)
190     _
191     (funcall fn)))))
192    
193     (defimplementation remove-fd-handlers (socket)
194     (sys:invalidate-descriptor (socket-fd socket)))
195    
196 lgorrie 1.103
197     ;;;; Stream handling
198     ;;; XXX: How come we don't use Gray streams in CMUCL too? -luke (15/May/2004)
199    
200 lgorrie 1.56 (defimplementation make-fn-streams (input-fn output-fn)
201 lgorrie 1.45 (let* ((output (make-slime-output-stream output-fn))
202     (input (make-slime-input-stream input-fn output)))
203     (values input output)))
204 lgorrie 1.25
205 dbarlow 1.1 (defstruct (slime-output-stream
206 lgorrie 1.45 (:include lisp::lisp-stream
207     (lisp::misc #'sos/misc)
208     (lisp::out #'sos/out)
209     (lisp::sout #'sos/sout))
210     (:conc-name sos.)
211     (:print-function %print-slime-output-stream)
212     (:constructor make-slime-output-stream (output-fn)))
213     (output-fn nil :type function)
214 heller 1.16 (buffer (make-string 512) :type string)
215     (index 0 :type kernel:index)
216     (column 0 :type kernel:index))
217    
218 heller 1.17 (defun %print-slime-output-stream (s stream d)
219     (declare (ignore d))
220     (print-unreadable-object (s stream :type t :identity t)))
221    
222 heller 1.16 (defun sos/out (stream char)
223     (let ((buffer (sos.buffer stream))
224     (index (sos.index stream)))
225     (setf (schar buffer index) char)
226     (setf (sos.index stream) (1+ index))
227     (incf (sos.column stream))
228 heller 1.21 (when (char= #\newline char)
229     (setf (sos.column stream) 0))
230     (when (= index (1- (length buffer)))
231     (force-output stream)))
232 heller 1.16 char)
233    
234     (defun sos/sout (stream string start end)
235     (loop for i from start below end
236     do (sos/out stream (aref string i))))
237 lgorrie 1.45
238 heller 1.16 (defun sos/misc (stream operation &optional arg1 arg2)
239     (declare (ignore arg1 arg2))
240 dbarlow 1.1 (case operation
241 heller 1.17 ((:force-output :finish-output)
242 heller 1.16 (let ((end (sos.index stream)))
243     (unless (zerop end)
244 lgorrie 1.45 (funcall (sos.output-fn stream) (subseq (sos.buffer stream) 0 end))
245     (setf (sos.index stream) 0))))
246 heller 1.16 (:charpos (sos.column stream))
247     (:line-length 75)
248 dbarlow 1.1 (:file-position nil)
249 heller 1.16 (:element-type 'base-char)
250     (:get-command nil)
251 heller 1.22 (:close nil)
252 heller 1.16 (t (format *terminal-io* "~&~Astream: ~S~%" stream operation))))
253 dbarlow 1.1
254 heller 1.9 (defstruct (slime-input-stream
255 lgorrie 1.45 (:include string-stream
256     (lisp::in #'sis/in)
257     (lisp::misc #'sis/misc))
258     (:conc-name sis.)
259     (:print-function %print-slime-output-stream)
260     (:constructor make-slime-input-stream (input-fn sos)))
261     (input-fn nil :type function)
262     ;; We know our sibling output stream, so that we can force it before
263     ;; requesting input.
264     (sos nil :type slime-output-stream)
265     (buffer "" :type string)
266     (index 0 :type kernel:index))
267 heller 1.16
268     (defun sis/in (stream eof-errorp eof-value)
269 heller 1.18 (declare (ignore eof-errorp eof-value))
270 heller 1.16 (let ((index (sis.index stream))
271     (buffer (sis.buffer stream)))
272     (when (= index (length buffer))
273 lgorrie 1.45 (force-output (sis.sos stream))
274     (setf buffer (funcall (sis.input-fn stream)))
275 heller 1.16 (setf (sis.buffer stream) buffer)
276     (setf index 0))
277     (prog1 (aref buffer index)
278     (setf (sis.index stream) (1+ index)))))
279 heller 1.9
280 heller 1.16 (defun sis/misc (stream operation &optional arg1 arg2)
281 heller 1.14 (declare (ignore arg2))
282 heller 1.15 (ecase operation
283     (:file-position nil)
284 heller 1.16 (:file-length nil)
285     (:unread (setf (aref (sis.buffer stream)
286     (decf (sis.index stream)))
287     arg1))
288 heller 1.57 (:clear-input
289     (setf (sis.index stream) 0
290 heller 1.16 (sis.buffer stream) ""))
291     (:listen (< (sis.index stream) (length (sis.buffer stream))))
292     (:charpos nil)
293     (:line-length nil)
294     (:get-command nil)
295 heller 1.22 (:element-type 'base-char)
296     (:close nil)))
297 heller 1.16
298 lgorrie 1.25
299 dbarlow 1.1 ;;;; Compilation Commands
300    
301 lgorrie 1.24 (defvar *previous-compiler-condition* nil
302     "Used to detect duplicates.")
303    
304     (defvar *previous-context* nil
305     "Previous compiler error context.")
306    
307 lgorrie 1.103 (defvar *buffer-name* nil
308     "The name of the Emacs buffer we are compiling from.
309     NIL if we aren't compiling from a buffer.")
310    
311 heller 1.30 (defvar *buffer-start-position* nil)
312     (defvar *buffer-substring* nil)
313    
314 lgorrie 1.103 (defimplementation call-with-compilation-hooks (function)
315     (let ((*previous-compiler-condition* nil)
316     (*previous-context* nil)
317     (*print-readably* nil))
318     (handler-bind ((c::compiler-error #'handle-notification-condition)
319     (c::style-warning #'handle-notification-condition)
320     (c::warning #'handle-notification-condition))
321     (funcall function))))
322    
323     (defimplementation swank-compile-file (filename load-p)
324     (clear-xref-info filename)
325     (with-compilation-hooks ()
326     (let ((*buffer-name* nil))
327     (multiple-value-bind (output-file warnings-p failure-p)
328     (compile-file filename :load load-p)
329     (unless failure-p
330     ;; Cache the latest source file for definition-finding.
331     (source-cache-get filename (file-write-date filename)))
332     (values output-file warnings-p failure-p)))))
333    
334 pseibel 1.113 (defimplementation swank-compile-string (string &key buffer position directory)
335     (declare (ignore directory))
336 lgorrie 1.103 (with-compilation-hooks ()
337     (let ((*buffer-name* buffer)
338     (*buffer-start-position* position)
339     (*buffer-substring* string))
340     (with-input-from-string (stream string)
341     (ext:compile-from-stream
342     stream
343     :source-info `(:emacs-buffer ,buffer
344     :emacs-buffer-offset ,position
345     :emacs-buffer-string ,string))))))
346    
347 lgorrie 1.25
348 lgorrie 1.24 ;;;;; Trapping notes
349 lgorrie 1.103 ;;;
350     ;;; We intercept conditions from the compiler and resignal them as
351     ;;; `SWANK:COMPILER-CONDITION's.
352 lgorrie 1.24
353     (defun handle-notification-condition (condition)
354 lgorrie 1.103 "Handle a condition caused by a compiler warning."
355 dbarlow 1.1 (unless (eq condition *previous-compiler-condition*)
356 heller 1.65 (let ((context (c::find-error-context nil)))
357 dbarlow 1.1 (setq *previous-compiler-condition* condition)
358     (setq *previous-context* context)
359 lgorrie 1.24 (signal-compiler-condition condition context))))
360    
361     (defun signal-compiler-condition (condition context)
362     (signal (make-condition
363     'compiler-condition
364     :original-condition condition
365     :severity (severity-for-emacs condition)
366 heller 1.65 :short-message (brief-compiler-message-for-emacs condition)
367     :message (long-compiler-message-for-emacs condition context)
368 lgorrie 1.24 :location (compiler-note-location context))))
369    
370     (defun severity-for-emacs (condition)
371     "Return the severity of CONDITION."
372     (etypecase condition
373     (c::compiler-error :error)
374     (c::style-warning :note)
375     (c::warning :warning)))
376    
377 heller 1.65 (defun brief-compiler-message-for-emacs (condition)
378 lgorrie 1.24 "Briefly describe a compiler error for Emacs.
379     When Emacs presents the message it already has the source popped up
380     and the source form highlighted. This makes much of the information in
381     the error-context redundant."
382 heller 1.65 (princ-to-string condition))
383    
384     (defun long-compiler-message-for-emacs (condition error-context)
385     "Describe a compiler error for Emacs including context information."
386 lgorrie 1.24 (declare (type (or c::compiler-error-context null) error-context))
387 heller 1.30 (multiple-value-bind (enclosing source)
388     (if error-context
389     (values (c::compiler-error-context-enclosing-source error-context)
390     (c::compiler-error-context-source error-context)))
391     (format nil "~@[--> ~{~<~%--> ~1:;~A~> ~}~%~]~@[~{==>~%~A~^~%~}~]~A"
392     enclosing source condition)))
393    
394     (defun compiler-note-location (context)
395 lgorrie 1.103 "Derive the location of a complier message from its context.
396     Return a `location' record, or (:error REASON) on failure."
397     (if (null context)
398     (note-error-location)
399     (let ((file (c::compiler-error-context-file-name context))
400     (source (c::compiler-error-context-original-source context))
401     (path
402     (reverse (c::compiler-error-context-original-source-path context))))
403     (or (locate-compiler-note file source path)
404     (note-error-location)))))
405    
406     (defun note-error-location ()
407     "Pseudo-location for notes that can't be located."
408     (list :error "No error location available."))
409    
410     (defun locate-compiler-note (file source source-path)
411     (cond ((and (eq file :stream) *buffer-name*)
412     ;; Compiling from a buffer
413     (let ((position (+ *buffer-start-position*
414     (source-path-string-position
415     source-path *buffer-substring*))))
416     (make-location (list :buffer *buffer-name*)
417     (list :position position))))
418     ((and (pathnamep file) (null *buffer-name*))
419     ;; Compiling from a file
420     (make-location (list :file (unix-truename file))
421     (list :position
422     (1+ (source-path-file-position
423     source-path file)))))
424     ((and (eq file :lisp) (stringp source))
425     ;; No location known, but we have the source form.
426 heller 1.112 ;; XXX How is this case triggered? -luke (16/May/2004)
427     ;; This can happen if the compiler needs to expand a macro
428     ;; but the macro-expander is not yet compiled. Calling the
429     ;; (interpreted) macro-expander triggers IR1 conversion of
430     ;; the lambda expression for the expander and invokes the
431     ;; compiler recursively.
432 lgorrie 1.103 (make-location (list :source-form source)
433     (list :position 1)))))
434 heller 1.30
435 lgorrie 1.103 (defun unix-truename (pathname)
436     (ext:unix-namestring (truename pathname)))
437 dbarlow 1.1
438 lgorrie 1.25
439     ;;;; XREF
440 lgorrie 1.103 ;;;
441     ;;; Cross-reference support is based on the standard CMUCL `XREF'
442     ;;; package. This package has some caveats: XREF information is
443     ;;; recorded during compilation and not preserved in fasl files, and
444     ;;; XREF recording is disabled by default. Redefining functions can
445     ;;; also cause duplicate references to accumulate, but
446     ;;; `swank-compile-file' will automatically clear out any old records
447     ;;; from the same filename.
448     ;;;
449     ;;; To enable XREF recording, set `c:*record-xref-info*' to true. To
450     ;;; clear out the XREF database call `xref:init-xref-database'.
451 dbarlow 1.1
452 heller 1.81 (defmacro defxref (name function)
453     `(defimplementation ,name (name)
454 heller 1.82 (xref-results (,function name))))
455 heller 1.81
456     (defxref who-calls xref:who-calls)
457     (defxref who-references xref:who-references)
458     (defxref who-binds xref:who-binds)
459     (defxref who-sets xref:who-sets)
460 dbarlow 1.1
461 lgorrie 1.103 ;;; More types of XREF information were added since 18e:
462     ;;;
463 lgorrie 1.10 #+cmu19
464 heller 1.32 (progn
465 heller 1.81 (defxref who-macroexpands xref:who-macroexpands)
466     ;; XXX
467     (defimplementation who-specializes (symbol)
468     (let* ((methods (xref::who-specializes (find-class symbol)))
469 heller 1.86 (locations (mapcar #'method-location methods)))
470 heller 1.81 (mapcar #'list methods locations))))
471    
472     (defun xref-results (contexts)
473     (mapcar (lambda (xref)
474     (list (xref:xref-context-name xref)
475     (resolve-xref-location xref)))
476     contexts))
477 heller 1.30
478     (defun resolve-xref-location (xref)
479     (let ((name (xref:xref-context-name xref))
480     (file (xref:xref-context-file xref))
481     (source-path (xref:xref-context-source-path xref)))
482     (cond ((and file source-path)
483     (let ((position (source-path-file-position source-path file)))
484     (make-location (list :file (unix-truename file))
485     (list :position (1+ position)))))
486     (file
487     (make-location (list :file (unix-truename file))
488     (list :function-name (string name))))
489     (t
490 lgorrie 1.99 `(:error ,(format nil "Unknown source location: ~S ~S ~S "
491 heller 1.30 name file source-path))))))
492    
493 lgorrie 1.25 (defun clear-xref-info (namestring)
494 heller 1.66 "Clear XREF notes pertaining to NAMESTRING.
495 lgorrie 1.25 This is a workaround for a CMUCL bug: XREF records are cumulative."
496 heller 1.65 (when c:*record-xref-info*
497 heller 1.66 (let ((filename (truename namestring)))
498 lgorrie 1.25 (dolist (db (list xref::*who-calls*
499     #+cmu19 xref::*who-is-called*
500     #+cmu19 xref::*who-macroexpands*
501     xref::*who-references*
502     xref::*who-binds*
503     xref::*who-sets*))
504     (maphash (lambda (target contexts)
505 heller 1.66 ;; XXX update during traversal?
506 lgorrie 1.25 (setf (gethash target db)
507 heller 1.66 (delete filename contexts
508     :key #'xref:xref-context-file
509     :test #'equalp)))
510 lgorrie 1.25 db)))))
511    
512    
513     ;;;; Find callers and callees
514 lgorrie 1.103 ;;;
515 dbarlow 1.1 ;;; Find callers and callees by looking at the constant pool of
516     ;;; compiled code objects. We assume every fdefn object in the
517     ;;; constant pool corresponds to a call to that function. A better
518     ;;; strategy would be to use the disassembler to find actual
519     ;;; call-sites.
520    
521     (declaim (inline map-code-constants))
522     (defun map-code-constants (code fn)
523     "Call FN for each constant in CODE's constant pool."
524     (check-type code kernel:code-component)
525     (loop for i from vm:code-constants-offset below (kernel:get-header-data code)
526     do (funcall fn (kernel:code-header-ref code i))))
527    
528     (defun function-callees (function)
529 heller 1.30 "Return FUNCTION's callees as a list of functions."
530 dbarlow 1.1 (let ((callees '()))
531     (map-code-constants
532     (vm::find-code-object function)
533     (lambda (obj)
534     (when (kernel:fdefn-p obj)
535 heller 1.30 (push (kernel:fdefn-function obj) callees))))
536 dbarlow 1.1 callees))
537    
538 heller 1.19 (declaim (ext:maybe-inline map-allocated-code-components))
539 dbarlow 1.1 (defun map-allocated-code-components (spaces fn)
540     "Call FN for each allocated code component in one of SPACES. FN
541 heller 1.19 receives the object as argument. SPACES should be a list of the
542     symbols :dynamic, :static, or :read-only."
543 dbarlow 1.1 (dolist (space spaces)
544 heller 1.18 (declare (inline vm::map-allocated-objects))
545 dbarlow 1.1 (vm::map-allocated-objects
546     (lambda (obj header size)
547 heller 1.19 (declare (type fixnum size) (ignore size))
548 dbarlow 1.1 (when (= vm:code-header-type header)
549 heller 1.19 (funcall fn obj)))
550 dbarlow 1.1 space)))
551    
552 heller 1.19 (declaim (ext:maybe-inline map-caller-code-components))
553 dbarlow 1.1 (defun map-caller-code-components (function spaces fn)
554     "Call FN for each code component with a fdefn for FUNCTION in its
555     constant pool."
556     (let ((function (coerce function 'function)))
557 heller 1.19 (declare (inline map-allocated-code-components))
558 dbarlow 1.1 (map-allocated-code-components
559     spaces
560 heller 1.19 (lambda (obj)
561 dbarlow 1.1 (map-code-constants
562     obj
563     (lambda (constant)
564     (when (and (kernel:fdefn-p constant)
565     (eq (kernel:fdefn-function constant)
566     function))
567     (funcall fn obj))))))))
568    
569     (defun function-callers (function &optional (spaces '(:read-only :static
570     :dynamic)))
571 heller 1.30 "Return FUNCTION's callers. The result is a list of code-objects."
572 dbarlow 1.1 (let ((referrers '()))
573 heller 1.19 (declare (inline map-caller-code-components))
574 heller 1.108 ;;(ext:gc :full t)
575 heller 1.30 (map-caller-code-components function spaces
576     (lambda (code) (push code referrers)))
577 dbarlow 1.1 referrers))
578 heller 1.80
579 heller 1.30 (defun debug-info-definitions (debug-info)
580     "Return the defintions for a debug-info. This should only be used
581     for code-object without entry points, i.e., byte compiled
582     code (are theree others?)"
583     ;; This mess has only been tested with #'ext::skip-whitespace, a
584     ;; byte-compiled caller of #'read-char .
585     (check-type debug-info (and (not c::compiled-debug-info) c::debug-info))
586     (let ((name (c::debug-info-name debug-info))
587     (source (c::debug-info-source debug-info)))
588     (destructuring-bind (first) source
589     (ecase (c::debug-source-from first)
590     (:file
591 heller 1.80 (list (list name
592     (make-location
593     (list :file (unix-truename (c::debug-source-name first)))
594     (list :function-name name)))))))))
595 heller 1.30
596     (defun code-component-entry-points (code)
597 heller 1.80 "Return a list ((NAME LOCATION) ...) of function definitons for
598 heller 1.30 the code omponent CODE."
599     (delete-duplicates
600     (loop for e = (kernel:%code-entry-points code)
601     then (kernel::%function-next e)
602     while e
603 heller 1.80 collect (list (kernel:%function-name e)
604 heller 1.86 (function-location e)))
605 heller 1.30 :test #'equal))
606 dbarlow 1.1
607 heller 1.80 (defimplementation list-callers (symbol)
608     "Return a list ((NAME LOCATION) ...) of callers."
609     (let ((components (function-callers symbol))
610 heller 1.30 (xrefs '()))
611     (dolist (code components)
612     (let* ((entry (kernel:%code-entry-points code))
613     (defs (if entry
614     (code-component-entry-points code)
615     ;; byte compiled stuff
616     (debug-info-definitions
617     (kernel:%code-debug-info code)))))
618     (setq xrefs (nconc defs xrefs))))
619 heller 1.80 xrefs))
620 dbarlow 1.1
621 heller 1.80 (defimplementation list-callees (symbol)
622     (let ((fns (function-callees symbol)))
623     (mapcar (lambda (fn)
624     (list (kernel:%function-name fn)
625 heller 1.86 (function-location fn)))
626 heller 1.80 fns)))
627 dbarlow 1.1
628 lgorrie 1.42
629 lgorrie 1.103 ;;;; Resolving source locations
630     ;;;
631     ;;; Our mission here is to "resolve" references to code locations into
632     ;;; actual file/buffer names and character positions. The references
633     ;;; we work from come out of the compiler's statically-generated debug
634     ;;; information, such as `code-location''s and `debug-source''s. For
635     ;;; more details, see the "Debugger Programmer's Interface" section of
636     ;;; the CMUCL manual.
637     ;;;
638     ;;; The first step is usually to find the corresponding "source-path"
639     ;;; for the location. Once we have the source-path we can pull up the
640     ;;; source file and `READ' our way through to the right position. The
641     ;;; main source-code groveling work is done in
642     ;;; `swank-source-path-parser.lisp'.
643 dbarlow 1.1
644 lgorrie 1.35 (defvar *debug-definition-finding* nil
645 dbarlow 1.1 "When true don't handle errors while looking for definitions.
646     This is useful when debugging the definition-finding code.")
647    
648 lgorrie 1.99 (defvar *source-snippet-size* 256
649     "Maximum number of characters in a snippet of source code.
650     Snippets at the beginning of definitions are used to tell Emacs what
651     the definitions looks like, so that it can accurately find them by
652     text search.")
653    
654 lgorrie 1.103 (defmacro safe-definition-finding (&body body)
655     "Execute BODY and return the source-location it returns.
656     If an error occurs and `*debug-definition-finding*' is false, then
657     return an error pseudo-location.
658    
659     The second return value is NIL if no error occurs, otherwise it is the
660     condition object."
661     `(flet ((body () ,@body))
662     (if *debug-definition-finding*
663     (body)
664     (handler-case (values (progn ,@body) nil)
665     (error (c) (values (list :error (princ-to-string c)) c))))))
666    
667     (defun code-location-source-location (code-location)
668     "Safe wrapper around `code-location-from-source-location'."
669     (safe-definition-finding
670     (source-location-from-code-location code-location)))
671    
672     (defun source-location-from-code-location (code-location)
673     "Return the source location for CODE-LOCATION."
674     (let ((debug-fun (di:code-location-debug-function code-location)))
675     (when (di::bogus-debug-function-p debug-fun)
676     ;; Those lousy cheapskates! They've put in a bogus debug source
677     ;; because the code was compiled at a low debug setting.
678     (error "Bogus debug function: ~A" debug-fun)))
679     (let* ((debug-source (di:code-location-debug-source code-location))
680     (from (di:debug-source-from debug-source))
681     (name (di:debug-source-name debug-source)))
682     (ecase from
683     (:file
684     (location-in-file name code-location debug-source))
685     (:stream
686     (location-in-stream code-location debug-source))
687     (:lisp
688     ;; The location comes from a form passed to `compile'.
689     ;; The best we can do is return the form itself for printing.
690     (make-location
691     (list :source-form (with-output-to-string (*standard-output*)
692     (debug::print-code-location-source-form
693     code-location 100 t)))
694     (list :position 1))))))
695    
696     (defun location-in-file (filename code-location debug-source)
697     "Resolve the source location for CODE-LOCATION in FILENAME."
698     (let* ((code-date (di:debug-source-created debug-source))
699     (source-code (or (source-cache-get filename code-date)
700     (read-file filename))))
701     (make-location (list :file (unix-truename filename)) nil)
702     (with-input-from-string (s source-code)
703     (make-location (list :file (unix-truename filename))
704     (list :position
705     (1+ (code-location-stream-position
706     code-location s)))
707     `(:snippet ,(read-snippet s))))))
708    
709     (defun location-in-stream (code-location debug-source)
710     "Resolve the source location for a CODE-LOCATION from a stream.
711     This only succeeds if the code was compiled from an Emacs buffer."
712     (unless (debug-source-info-from-emacs-buffer-p debug-source)
713     (error "The code is compiled from a non-SLIME stream."))
714     (let* ((info (c::debug-source-info debug-source))
715     (string (getf info :emacs-buffer-string))
716     (position (code-location-string-offset
717     code-location
718     string)))
719     (make-location
720     (list :buffer (getf info :emacs-buffer))
721     (list :position (+ (getf info :emacs-buffer-offset) position))
722     (list :snippet (with-input-from-string (s string)
723     (file-position s position)
724     (read-snippet s))))))
725    
726     (defun read-file (filename)
727     "Return the entire contents of FILENAME as a string."
728     (with-open-file (s filename :direction :input)
729     (let ((string (make-string (file-length s))))
730     (read-sequence string s)
731     string)))
732    
733     (defun read-snippet (stream)
734     "Read a string of upto *SOURCE-SNIPPET-SIZE* characters from STREAM."
735     (read-upto-n-chars stream *source-snippet-size*))
736    
737     (defun read-upto-n-chars (stream n)
738     "Return a string of upto N chars from STREAM."
739     (let* ((string (make-string n))
740     (chars (read-sequence string stream)))
741     (subseq string 0 chars)))
742    
743     ;;;;; Function-name locations
744     ;;;
745     (defun debug-info-function-name-location (debug-info)
746     "Return a function-name source-location for DEBUG-INFO.
747     Function-name source-locations are a fallback for when precise
748     positions aren't available."
749     (with-struct (c::debug-info- (fname name) source) debug-info
750     (with-struct (c::debug-source- info from name) (car source)
751     (ecase from
752     (:file
753     (make-location (list :file (namestring (truename name)))
754     (list :function-name fname)))
755     (:stream
756     (assert (debug-source-info-from-emacs-buffer-p (car source)))
757     (make-location (list :buffer (getf info :emacs-buffer))
758     (list :function-name fname)))
759     (:lisp
760     (make-location (list :source-form (princ-to-string (aref name 0)))
761     (list :position 1)))))))
762    
763     (defun debug-source-info-from-emacs-buffer-p (debug-source)
764     "Does the `info' slot of DEBUG-SOURCE contain an Emacs buffer location?
765     This is true for functions that were compiled directly from buffers."
766     (info-from-emacs-buffer-p (c::debug-source-info debug-source)))
767    
768     (defun info-from-emacs-buffer-p (info)
769     (and info
770     (consp info)
771     (eq :emacs-buffer (car info))))
772    
773    
774     ;;;;; Groveling source-code for positions
775    
776     (defun code-location-stream-position (code-location stream)
777     "Return the byte offset of CODE-LOCATION in STREAM. Extract the
778     toplevel-form-number and form-number from CODE-LOCATION and use that
779     to find the position of the corresponding form.
780    
781     Finish with STREAM positioned at the start of the code location."
782     (let* ((location (debug::maybe-block-start-location code-location))
783     (tlf-offset (di:code-location-top-level-form-offset location))
784     (form-number (di:code-location-form-number location)))
785     (let ((pos (form-number-stream-position tlf-offset form-number stream)))
786     (file-position stream pos)
787     pos)))
788    
789     (defun form-number-stream-position (tlf-number form-number stream)
790     "Return the starting character position of a form in STREAM.
791     TLF-NUMBER is the top-level-form number.
792     FORM-NUMBER is an index into a source-path table for the TLF."
793     (let ((*read-suppress* t))
794     (dotimes (i tlf-number) (read stream))
795     (multiple-value-bind (tlf position-map) (read-and-record-source-map stream)
796     (let* ((path-table (di:form-number-translations tlf 0))
797     (source-path
798     (if (<= (length path-table) form-number) ; source out of sync?
799     (list 0) ; should probably signal a condition
800     (reverse (cdr (aref path-table form-number))))))
801     (source-path-source-position source-path tlf position-map)))))
802    
803     (defun code-location-string-offset (code-location string)
804     "Return the byte offset of CODE-LOCATION in STRING.
805     See CODE-LOCATION-STREAM-POSITION."
806     (with-input-from-string (s string)
807     (code-location-stream-position code-location s)))
808    
809     ;;;;; Source-file cache
810     ;;;
811     ;;; To robustly find source locations it's useful to have the exact
812     ;;; source code that the loaded code was compiled from. In this source
813     ;;; we can accurately find the right location, and from that location
814     ;;; we can extract a "snippet" of code to show what the definition
815     ;;; looks like. Emacs can use this snippet in a best-match search to
816     ;;; locate the right definition, which works well even if the buffer
817     ;;; has been modified.
818     ;;;
819     ;;; The idea is that if a definition previously started with
820     ;;; `(define-foo bar' then it probably still does.
821     ;;;
822     ;;; Whenever we see that the file on disk has the same
823     ;;; `file-write-date' as a location we're looking for, we cache the
824     ;;; whole file inside Lisp. That way we will still have the matching
825     ;;; version even if the file is later modified on disk. If the file is
826     ;;; later recompiled and reloaded then we replace our cache entry.
827    
828 lgorrie 1.99 (defvar *cache-sourcecode* t
829     "When true complete source files are cached.
830     The cache is used to keep known good copies of the source text which
831     correspond to the loaded code. Finding definitions is much more
832 lgorrie 1.103 reliable when the exact source is available, so we cache it in case it
833 lgorrie 1.99 gets edited on disk later.")
834    
835     (defvar *source-file-cache* (make-hash-table :test 'equal)
836     "Cache of source file contents.
837     Maps from truename to source-cache-entry structure.")
838    
839     (defstruct (source-cache-entry
840     (:conc-name source-cache-entry.)
841     (:constructor make-source-cache-entry (text date)))
842     text date)
843    
844     (defun source-cache-get (filename date)
845 lgorrie 1.103 "Return the source code for FILENAME as written on DATE in a string.
846 lgorrie 1.99 Return NIL if the right version cannot be found."
847     (let ((entry (gethash filename *source-file-cache*)))
848     (cond ((and entry (equal date (source-cache-entry.date entry)))
849     ;; Cache hit.
850     (source-cache-entry.text entry))
851     ((or (null entry)
852     (not (equal date (source-cache-entry.date entry))))
853     ;; Cache miss.
854     (if (equal (file-write-date filename) date)
855     ;; File on disk has the correct version.
856 lgorrie 1.100 (let ((source (read-file filename)))
857     (setf (gethash filename *source-file-cache*)
858     (make-source-cache-entry source date))
859     source)
860 lgorrie 1.99 nil)))))
861    
862 lgorrie 1.110 (defun source-cached-p (filename)
863     "Is any version of FILENAME in the source cache?"
864     (if (gethash filename *source-file-cache*) t))
865    
866     (defimplementation buffer-first-change (filename)
867     "Load a file into the cache when the user modifies its buffer.
868     This is a win if the user then saves the file and tries to M-. into it."
869     (unless (source-cached-p filename)
870     (ignore-errors (source-cache-get filename (file-write-date filename)))))
871    
872 lgorrie 1.103
873     ;;;; Finding definitions
874    
875     ;;; There are a great many different types of definition for us to
876     ;;; find. We search for definitions of every kind and return them in a
877     ;;; list.
878    
879     (defimplementation find-definitions (name)
880     (append (function-definitions name)
881     (setf-definitions name)
882     (variable-definitions name)
883     (class-definitions name)
884     (type-definitions name)
885     (compiler-macro-definitions name)
886     (source-transform-definitions name)
887     (function-info-definitions name)
888     (ir1-translator-definitions name)))
889    
890     ;;;;; Functions, macros, generic functions, methods
891     ;;;
892     ;;; We make extensive use of the compile-time debug information that
893     ;;; CMUCL records, in particular "debug functions" and "code
894     ;;; locations." Refer to the "Debugger Programmer's Interface" section
895     ;;; of the CMUCL manual for more details.
896    
897     (defun function-definitions (name)
898     "Return definitions for NAME in the \"function namespace\", i.e.,
899     regular functions, generic functions, methods and macros.
900     NAME can any valid function name (e.g, (setf car))."
901     (let ((macro? (and (symbolp name) (macro-function name)))
902     (special? (and (symbolp name) (special-operator-p name)))
903     (function? (and (ext:valid-function-name-p name)
904     (ext:info :function :definition name))))
905     (cond (macro?
906     (list `((defmacro ,name)
907     ,(function-location (macro-function name)))))
908     (special?
909     (list `((:special-operator ,name)
910     (:error ,(format nil "Special operator: ~S" name)))))
911     (function?
912     (let ((function (fdefinition name)))
913     (if (genericp function)
914     (generic-function-definitions name function)
915     (list (list `(function ,name)
916     (function-location function)))))))))
917    
918     ;;;;;; Ordinary (non-generic/macro/special) functions
919     ;;;
920     ;;; First we test if FUNCTION is a closure created by defstruct, and
921     ;;; if so extract the defstruct-description (`dd') from the closure
922     ;;; and find the constructor for the struct. Defstruct creates a
923     ;;; defun for the default constructor and we use that as an
924     ;;; approximation to the source location of the defstruct.
925     ;;;
926     ;;; For an ordinary function we return the source location of the
927     ;;; first code-location we find.
928     ;;;
929     (defun function-location (function)
930     "Return the source location for FUNCTION."
931     (cond ((struct-closure-p function)
932     (struct-closure-location function))
933     ((c::byte-function-or-closure-p function)
934     (byte-function-location function))
935     (t
936     (compiled-function-location function))))
937 lgorrie 1.100
938 lgorrie 1.103 (defun compiled-function-location (function)
939     "Return the location of a regular compiled function."
940     (multiple-value-bind (code-location error)
941     (safe-definition-finding (function-first-code-location function))
942     (cond (error (list :error (princ-to-string error)))
943     (t (code-location-source-location code-location)))))
944 heller 1.88
945 dbarlow 1.1 (defun function-first-code-location (function)
946 lgorrie 1.103 "Return the first code-location we can find for FUNCTION."
947 dbarlow 1.1 (and (function-has-debug-function-p function)
948     (di:debug-function-start-location
949     (di:function-debug-function function))))
950    
951     (defun function-has-debug-function-p (function)
952     (di:function-debug-function function))
953    
954     (defun function-code-object= (closure function)
955     (and (eq (vm::find-code-object closure)
956     (vm::find-code-object function))
957     (not (eq closure function))))
958    
959 lgorrie 1.103
960     (defun byte-function-location (fn)
961     "Return the location of the byte-compiled function FN."
962     (etypecase fn
963     ((or c::hairy-byte-function c::simple-byte-function)
964     (let* ((component (c::byte-function-component fn))
965     (debug-info (kernel:%code-debug-info component)))
966     (debug-info-function-name-location debug-info)))
967     (c::byte-closure
968     (byte-function-location (c::byte-closure-function fn)))))
969    
970     ;;; Here we deal with structure accessors. Note that `dd' is a
971     ;;; "defstruct descriptor" structure in CMUCL. A `dd' describes a
972     ;;; `defstruct''d structure.
973 heller 1.83
974 heller 1.4 (defun struct-closure-p (function)
975 lgorrie 1.103 "Is FUNCTION a closure created by defstruct?"
976 heller 1.4 (or (function-code-object= function #'kernel::structure-slot-accessor)
977     (function-code-object= function #'kernel::structure-slot-setter)
978     (function-code-object= function #'kernel::%defstruct)))
979 dbarlow 1.1
980 lgorrie 1.103 (defun struct-closure-location (function)
981     "Return the location of the structure that FUNCTION belongs to."
982     (assert (struct-closure-p function))
983     (safe-definition-finding
984     (dd-location (struct-closure-dd function))))
985    
986 heller 1.4 (defun struct-closure-dd (function)
987 lgorrie 1.103 "Return the defstruct-definition (dd) of FUNCTION."
988 dbarlow 1.1 (assert (= (kernel:get-type function) vm:closure-header-type))
989 heller 1.4 (flet ((find-layout (function)
990     (sys:find-if-in-closure
991     (lambda (x)
992 heller 1.18 (let ((value (if (di::indirect-value-cell-p x)
993     (c:value-cell-ref x)
994     x)))
995     (when (kernel::layout-p value)
996     (return-from find-layout value))))
997 heller 1.4 function)))
998     (kernel:layout-info (find-layout function))))
999 lgorrie 1.103
1000 heller 1.86 (defun dd-location (dd)
1001 lgorrie 1.103 "Return the location of a `defstruct'."
1002     ;; Find the location in a constructor.
1003     (function-location (struct-constructor dd)))
1004    
1005     (defun struct-constructor (dd)
1006     "Return a constructor function from a defstruct definition.
1007     Signal an error if no constructor can be found."
1008 dbarlow 1.1 (let ((constructor (or (kernel:dd-default-constructor dd)
1009 heller 1.32 (car (kernel::dd-constructors dd)))))
1010 lgorrie 1.103 (when (or (null constructor)
1011     (and (consp constructor) (null (car constructor))))
1012     (error "Cannot find structure's constructor: ~S"
1013 heller 1.32 (kernel::dd-name dd)))
1014 lgorrie 1.103 (coerce (if (consp constructor) (first constructor) constructor)
1015     'function)))
1016    
1017     ;;;;;; Generic functions and methods
1018 dbarlow 1.1
1019 lgorrie 1.103 (defun generic-function-definitions (name function)
1020     "Return the definitions of a generic function and its methods."
1021     (cons (list `(defgeneric ,name) (gf-location function))
1022     (gf-method-definitions function)))
1023    
1024     (defun gf-location (gf)
1025     "Return the location of the generic function GF."
1026     (definition-source-location gf (pcl::generic-function-name gf)))
1027 heller 1.101
1028 lgorrie 1.103 (defun gf-method-definitions (gf)
1029     "Return the locations of all methods of the generic function GF."
1030     (mapcar #'method-definition (pcl::generic-function-methods gf)))
1031 heller 1.83
1032 lgorrie 1.103 (defun method-definition (method)
1033     (list (method-dspec method)
1034     (method-location method)))
1035 heller 1.83
1036     (defun method-dspec (method)
1037 lgorrie 1.103 "Return a human-readable \"definition specifier\" for METHOD."
1038 heller 1.83 (let* ((gf (pcl:method-generic-function method))
1039     (name (pcl:generic-function-name gf))
1040 heller 1.92 (specializers (pcl:method-specializers method))
1041     (qualifiers (pcl:method-qualifiers method)))
1042     `(method ,name ,@qualifiers ,(pcl::unparse-specializers specializers))))
1043 heller 1.83
1044 lgorrie 1.103 ;; XXX maybe special case setters/getters
1045     (defun method-location (method)
1046     (function-location (or (pcl::method-fast-function method)
1047     (pcl:method-function method))))
1048 heller 1.83
1049 lgorrie 1.103 (defun genericp (fn)
1050     (typep fn 'generic-function))
1051 dbarlow 1.1
1052 lgorrie 1.105 ;;;;;; Types and classes
1053    
1054     (defun type-definitions (name)
1055     "Return `deftype' locations for type NAME."
1056     (maybe-make-definition (ext:info :type :expander name) 'deftype name))
1057    
1058 heller 1.88 (defun maybe-make-definition (function kind name)
1059 lgorrie 1.105 "If FUNCTION is non-nil then return its definition location."
1060 heller 1.87 (if function
1061 heller 1.88 (list (list `(,kind ,name) (function-location function)))))
1062 heller 1.87
1063 lgorrie 1.105 (defun class-definitions (name)
1064     "Return the definition locations for the class called NAME."
1065     (if (symbolp name)
1066     (let ((class (kernel::find-class name nil)))
1067     (etypecase class
1068     (null '())
1069     (kernel::structure-class
1070     (list (list `(defstruct ,name) (dd-location (find-dd name)))))
1071     #+(or)
1072     (conditions::condition-class
1073     (list (list `(define-condition ,name)
1074     (condition-class-location class))))
1075     (kernel::standard-class
1076     (list (list `(defclass ,name)
1077     (class-location (find-class name)))))
1078     ((or kernel::built-in-class
1079     conditions::condition-class
1080     kernel:funcallable-structure-class)
1081     (list (list `(kernel::define-type-class ,name)
1082     `(:error
1083     ,(format nil "No source info for ~A" name)))))))))
1084    
1085     (defun class-location (class)
1086     "Return the `defclass' location for CLASS."
1087     (definition-source-location class (pcl:class-name class)))
1088 heller 1.86
1089     (defun find-dd (name)
1090 lgorrie 1.105 "Find the defstruct-definition by the name of its structure-class."
1091 heller 1.86 (let ((layout (ext:info :type :compiler-layout name)))
1092     (if layout
1093     (kernel:layout-info layout))))
1094    
1095 heller 1.92 (defun condition-class-location (class)
1096     (let ((slots (conditions::condition-class-slots class))
1097     (name (conditions::condition-class-name class)))
1098     (cond ((null slots)
1099     `(:error ,(format nil "No location info for condition: ~A" name)))
1100     (t
1101 lgorrie 1.105 ;; Find the class via one of its slot-reader methods.
1102 heller 1.92 (let* ((slot (first slots))
1103     (gf (fdefinition
1104     (first (conditions::condition-slot-readers slot)))))
1105     (method-location
1106     (first
1107     (pcl:compute-applicable-methods-using-classes
1108     gf (list (find-class name))))))))))
1109    
1110 heller 1.93 (defun make-name-in-file-location (file string)
1111     (multiple-value-bind (filename c)
1112     (ignore-errors
1113     (unix-truename (merge-pathnames (make-pathname :type "lisp")
1114     file)))
1115     (cond (filename (make-location `(:file ,filename)
1116     `(:function-name ,string)))
1117     (t (list :error (princ-to-string c))))))
1118 heller 1.92
1119 heller 1.95 (defun source-location-form-numbers (location)
1120     (c::decode-form-numbers (c::form-numbers-form-numbers location)))
1121    
1122     (defun source-location-tlf-number (location)
1123     (nth-value 0 (source-location-form-numbers location)))
1124    
1125     (defun source-location-form-number (location)
1126     (nth-value 1 (source-location-form-numbers location)))
1127    
1128 heller 1.93 (defun resolve-file-source-location (location)
1129     (let ((filename (c::file-source-location-pathname location))
1130 heller 1.95 (tlf-number (source-location-tlf-number location))
1131     (form-number (source-location-form-number location)))
1132 heller 1.93 (with-open-file (s filename)
1133     (let ((pos (form-number-stream-position tlf-number form-number s)))
1134     (make-location `(:file ,(unix-truename filename))
1135     `(:position ,(1+ pos)))))))
1136    
1137 heller 1.95 (defun resolve-stream-source-location (location)
1138     (let ((info (c::stream-source-location-user-info location))
1139     (tlf-number (source-location-tlf-number location))
1140     (form-number (source-location-form-number location)))
1141 heller 1.93 ;; XXX duplication in frame-source-location
1142     (assert (info-from-emacs-buffer-p info))
1143     (destructuring-bind (&key emacs-buffer emacs-buffer-string
1144     emacs-buffer-offset) info
1145     (with-input-from-string (s emacs-buffer-string)
1146     (let ((pos (form-number-stream-position tlf-number form-number s)))
1147     (make-location `(:buffer ,emacs-buffer)
1148     `(:position ,(+ emacs-buffer-offset pos))))))))
1149    
1150 heller 1.102 ;; XXX predicates for 18e backward compatibilty. Remove them when
1151     ;; we're 19a only.
1152 heller 1.96 (defun file-source-location-p (object)
1153     (when (fboundp 'c::file-source-location-p)
1154     (c::file-source-location-p object)))
1155    
1156     (defun stream-source-location-p (object)
1157     (when (fboundp 'c::stream-source-location-p)
1158     (c::stream-source-location-p object)))
1159    
1160 heller 1.102 (defun source-location-p (object)
1161     (or (file-source-location-p object)
1162     (stream-source-location-p object)))
1163    
1164     (defun resolve-source-location (location)
1165     (etypecase location
1166     ((satisfies file-source-location-p)
1167     (resolve-file-source-location location))
1168     ((satisfies stream-source-location-p)
1169     (resolve-stream-source-location location))))
1170    
1171 heller 1.93 (defun definition-source-location (object name)
1172     (let ((source (pcl::definition-source object)))
1173     (etypecase source
1174     (null
1175     `(:error ,(format nil "No source info for: ~A" object)))
1176 heller 1.102 ((satisfies source-location-p)
1177     (resolve-source-location source))
1178 heller 1.93 (pathname
1179     (make-name-in-file-location source name))
1180     (cons
1181     (destructuring-bind ((dg name) pathname) source
1182     (declare (ignore dg))
1183     (etypecase pathname
1184     (pathname (make-name-in-file-location pathname (string name)))
1185     (null `(:error ,(format nil "Cannot resolve: ~S" source)))))))))
1186 heller 1.86
1187 heller 1.88 (defun setf-definitions (name)
1188     (let ((function (or (ext:info :setf :inverse name)
1189     (ext:info :setf :expander name))))
1190 heller 1.87 (if function
1191 heller 1.88 (list (list `(setf ,name)
1192 heller 1.87 (function-location (coerce function 'function)))))))
1193    
1194 heller 1.102
1195     (defun variable-location (symbol)
1196     (multiple-value-bind (location foundp)
1197     ;; XXX for 18e compatibilty. rewrite this when we drop 18e
1198     ;; support.
1199     (ignore-errors (eval `(ext:info :source-location :defvar ',symbol)))
1200     (if (and foundp location)
1201     (resolve-source-location location)
1202     `(:error ,(format nil "No source info for variable ~S" symbol)))))
1203    
1204     (defun variable-definitions (name)
1205     (if (symbolp name)
1206     (multiple-value-bind (kind recorded-p) (ext:info :variable :kind name)
1207     (if recorded-p
1208     (list (list `(variable ,kind ,name)
1209     (variable-location name)))))))
1210    
1211 heller 1.87 (defun compiler-macro-definitions (symbol)
1212     (maybe-make-definition (compiler-macro-function symbol)
1213     'define-compiler-macro
1214     symbol))
1215    
1216 heller 1.88 (defun source-transform-definitions (name)
1217     (maybe-make-definition (ext:info :function :source-transform name)
1218 heller 1.87 'c:def-source-transform
1219 heller 1.88 name))
1220 heller 1.87
1221 heller 1.88 (defun function-info-definitions (name)
1222     (let ((info (ext:info :function :info name)))
1223 heller 1.87 (if info
1224     (append (loop for transform in (c::function-info-transforms info)
1225 heller 1.88 collect (list `(c:deftransform ,name
1226 heller 1.87 ,(c::type-specifier
1227     (c::transform-type transform)))
1228     (function-location (c::transform-function
1229     transform))))
1230     (maybe-make-definition (c::function-info-derive-type info)
1231 heller 1.88 'c::derive-type name)
1232 heller 1.87 (maybe-make-definition (c::function-info-optimizer info)
1233 heller 1.88 'c::optimizer name)
1234 heller 1.87 (maybe-make-definition (c::function-info-ltn-annotate info)
1235 heller 1.88 'c::ltn-annotate name)
1236 heller 1.87 (maybe-make-definition (c::function-info-ir2-convert info)
1237 heller 1.88 'c::ir2-convert name)
1238 heller 1.87 (loop for template in (c::function-info-templates info)
1239     collect (list `(c::vop ,(c::template-name template))
1240     (function-location
1241     (c::vop-info-generator-function
1242     template))))))))
1243    
1244 heller 1.88 (defun ir1-translator-definitions (name)
1245     (maybe-make-definition (ext:info :function :ir1-convert name)
1246     'c:def-ir1-translator name))
1247    
1248 lgorrie 1.25
1249     ;;;; Documentation.
1250 dbarlow 1.1
1251 lgorrie 1.56 (defimplementation describe-symbol-for-emacs (symbol)
1252 dbarlow 1.1 (let ((result '()))
1253 lgorrie 1.24 (flet ((doc (kind)
1254     (or (documentation symbol kind) :not-documented))
1255     (maybe-push (property value)
1256     (when value
1257     (setf result (list* property value result)))))
1258 dbarlow 1.1 (maybe-push
1259     :variable (multiple-value-bind (kind recorded-p)
1260     (ext:info variable kind symbol)
1261     (declare (ignore kind))
1262     (if (or (boundp symbol) recorded-p)
1263     (doc 'variable))))
1264     (maybe-push
1265 heller 1.27 :generic-function
1266     (if (and (fboundp symbol)
1267     (typep (fdefinition symbol) 'generic-function))
1268     (doc 'function)))
1269     (maybe-push
1270     :function (if (and (fboundp symbol)
1271     (not (typep (fdefinition symbol) 'generic-function)))
1272     (doc 'function)))
1273 dbarlow 1.1 (maybe-push
1274     :setf (if (or (ext:info setf inverse symbol)
1275     (ext:info setf expander symbol))
1276     (doc 'setf)))
1277     (maybe-push
1278     :type (if (ext:info type kind symbol)
1279     (doc 'type)))
1280     (maybe-push
1281     :class (if (find-class symbol nil)
1282     (doc 'class)))
1283 heller 1.18 (maybe-push
1284     :alien-type (if (not (eq (ext:info alien-type kind symbol) :unknown))
1285     (doc 'alien-type)))
1286     (maybe-push
1287     :alien-struct (if (ext:info alien-type struct symbol)
1288     (doc nil)))
1289     (maybe-push
1290     :alien-union (if (ext:info alien-type union symbol)
1291     (doc nil)))
1292     (maybe-push
1293     :alien-enum (if (ext:info alien-type enum symbol)
1294     (doc nil)))
1295 lgorrie 1.24 result)))
1296 dbarlow 1.1
1297 heller 1.80 (defimplementation describe-definition (symbol namespace)
1298 lgorrie 1.103 (describe (ecase namespace
1299     (:variable
1300     symbol)
1301     ((:function :generic-function)
1302     (symbol-function symbol))
1303     (:setf
1304     (or (ext:info setf inverse symbol)
1305     (ext:info setf expander symbol)))
1306     (:type
1307     (kernel:values-specifier-type symbol))
1308     (:class
1309     (find-class symbol))
1310     (:alien-struct
1311     (ext:info :alien-type :struct symbol))
1312     (:alien-union
1313     (ext:info :alien-type :union symbol))
1314     (:alien-enum
1315     (ext:info :alien-type :enum symbol))
1316     (:alien-type
1317     (ecase (ext:info :alien-type :kind symbol)
1318     (:primitive
1319     (let ((alien::*values-type-okay* t))
1320 heller 1.80 (funcall (ext:info :alien-type :translator symbol)
1321 lgorrie 1.103 (list symbol))))
1322     ((:defined)
1323     (ext:info :alien-type :definition symbol))
1324     (:unknown
1325     (return-from describe-definition
1326     (format nil "Unknown alien type: ~S" symbol))))))))
1327    
1328     ;;;;; Argument lists
1329    
1330 mbaringer 1.115 (defimplementation arglist ((name symbol))
1331     (arglist (or (macro-function name)
1332     (symbol-function name)
1333     (error "~S does not name a known function."))))
1334    
1335     (defimplementation arglist ((fun function))
1336     (let ((arglist
1337     (cond ((eval:interpreted-function-p fun)
1338     (eval:interpreted-function-arglist fun))
1339     ((pcl::generic-function-p fun)
1340     (pcl:generic-function-lambda-list fun))
1341     ((c::byte-function-or-closure-p fun)
1342     (byte-code-function-arglist fun))
1343     ((kernel:%function-arglist (kernel:%function-self fun))
1344     (handler-case (read-arglist fun)
1345     (error () :not-available)))
1346     ;; this should work both for compiled-debug-function
1347     ;; and for interpreted-debug-function
1348     (t
1349     (handler-case (debug-function-arglist
1350     (di::function-debug-function fun))
1351     (di:unhandled-condition () :not-available))))))
1352 lgorrie 1.103 (check-type arglist (or list (member :not-available)))
1353     arglist))
1354 mbaringer 1.115
1355     (defimplementation function-name (function)
1356     (cond ((eval:interpreted-function-p function)
1357     (eval:interpreted-function-name function))
1358     ((pcl::generic-function-p function)
1359     (pcl::generic-function-name function))
1360     ((c::byte-function-or-closure-p function)
1361     (c::byte-function-name function))
1362     (t (kernel:%function-name (kernel:%function-self function)))))
1363 heller 1.18
1364 lgorrie 1.103 ;;; A simple case: the arglist is available as a string that we can
1365     ;;; `read'.
1366    
1367     (defun read-arglist (fn)
1368     "Parse the arglist-string of the function object FN."
1369     (let ((string (kernel:%function-arglist
1370     (kernel:%function-self fn)))
1371     (package (find-package
1372     (c::compiled-debug-info-package
1373     (kernel:%code-debug-info
1374     (vm::find-code-object fn))))))
1375     (with-standard-io-syntax
1376     (let ((*package* (or package *package*)))
1377     (read-from-string string)))))
1378    
1379     ;;; A harder case: an approximate arglist is derived from available
1380     ;;; debugging information.
1381 heller 1.93
1382 heller 1.92 (defun debug-function-arglist (debug-function)
1383 lgorrie 1.103 "Derive the argument list of DEBUG-FUNCTION from debug info."
1384 heller 1.92 (let ((args (di::debug-function-lambda-list debug-function))
1385     (required '())
1386     (optional '())
1387     (rest '())
1388     (key '()))
1389     ;; collect the names of debug-vars
1390 heller 1.91 (dolist (arg args)
1391     (etypecase arg
1392     (di::debug-variable
1393 heller 1.93 (push (di::debug-variable-symbol arg) required))
1394 lgorrie 1.103 ((member :deleted)
1395 heller 1.93 (push ':deleted required))
1396 heller 1.91 (cons
1397     (ecase (car arg)
1398 heller 1.92 (:keyword
1399     (push (second arg) key))
1400     (:optional
1401 heller 1.93 (push (debug-variable-symbol-or-deleted (second arg)) optional))
1402 heller 1.92 (:rest
1403 heller 1.93 (push (debug-variable-symbol-or-deleted (second arg)) rest))))))
1404 heller 1.92 ;; intersperse lambda keywords as needed
1405     (append (nreverse required)
1406     (if optional (cons '&optional (nreverse optional)))
1407     (if rest (cons '&rest (nreverse rest)))
1408     (if key (cons '&key (nreverse key))))))
1409    
1410 lgorrie 1.103 (defun debug-variable-symbol-or-deleted (var)
1411     (etypecase var
1412     (di:debug-variable
1413     (di::debug-variable-symbol var))
1414     ((member :deleted)
1415     '#:deleted)))
1416    
1417 heller 1.92 (defun symbol-debug-function-arglist (fname)
1418     "Return FNAME's debug-function-arglist and %function-arglist.
1419     A utility for debugging DEBUG-FUNCTION-ARGLIST."
1420     (let ((fn (fdefinition fname)))
1421     (values (debug-function-arglist (di::function-debug-function fn))
1422     (kernel:%function-arglist (kernel:%function-self fn)))))
1423 heller 1.91
1424 lgorrie 1.103 ;;; Deriving arglists for byte-compiled functions:
1425     ;;;
1426     (defun byte-code-function-arglist (fn)
1427     ;; There doesn't seem to be much arglist information around for
1428     ;; byte-code functions. Use the arg-count and return something like
1429     ;; (arg0 arg1 ...)
1430     (etypecase fn
1431     (c::simple-byte-function
1432     (loop for i from 0 below (c::simple-byte-function-num-args fn)
1433     collect (make-arg-symbol i)))
1434     (c::hairy-byte-function
1435     (hairy-byte-function-arglist fn))
1436     (c::byte-closure
1437     (byte-code-function-arglist (c::byte-closure-function fn)))))
1438 heller 1.97
1439 heller 1.101 (defun make-arg-symbol (i)
1440     (make-symbol (format nil "~A~D" (string 'arg) i)))
1441    
1442 lgorrie 1.103 ;;; A "hairy" byte-function is one that takes a variable number of
1443     ;;; arguments. `hairy-byte-function' is a type from the bytecode
1444     ;;; interpreter.
1445     ;;;
1446 heller 1.101 (defun hairy-byte-function-arglist (fn)
1447     (let ((counter -1))
1448     (flet ((next-arg () (make-arg-symbol (incf counter))))
1449     (with-struct (c::hairy-byte-function- min-args max-args rest-arg-p
1450     keywords-p keywords) fn
1451     (let ((arglist '())
1452     (optional (- max-args min-args)))
1453     ;; XXX isn't there a better way to write this?
1454 lgorrie 1.103 ;; (Looks fine to me. -luke)
1455 heller 1.101 (dotimes (i min-args)
1456     (push (next-arg) arglist))
1457     (when (plusp optional)
1458     (push '&optional arglist)
1459     (dotimes (i optional)
1460     (push (next-arg) arglist)))
1461     (when rest-arg-p
1462     (push '&rest arglist)
1463     (push (next-arg) arglist))
1464     (when keywords-p
1465     (push '&key arglist)
1466     (loop for (key _ __) in keywords
1467     do (push key arglist))
1468     (when (eq keywords-p :allow-others)
1469     (push '&allow-other-keys arglist)))
1470     (nreverse arglist))))))
1471 lgorrie 1.25
1472    
1473     ;;;; Miscellaneous.
1474 dbarlow 1.1
1475 lgorrie 1.56 (defimplementation macroexpand-all (form)
1476 lgorrie 1.24 (walker:macroexpand-all form))
1477 dbarlow 1.1
1478 mbaringer 1.90 (defimplementation set-default-directory (directory)
1479 lgorrie 1.25 (setf (ext:default-directory) (namestring directory))
1480     ;; Setting *default-pathname-defaults* to an absolute directory
1481     ;; makes the behavior of MERGE-PATHNAMES a bit more intuitive.
1482     (setf *default-pathname-defaults* (pathname (ext:default-directory)))
1483 heller 1.107 (default-directory))
1484    
1485     (defimplementation default-directory ()
1486 lgorrie 1.25 (namestring (ext:default-directory)))
1487    
1488 lgorrie 1.103 (defimplementation call-without-interrupts (fn)
1489     (sys:without-interrupts (funcall fn)))
1490 heller 1.19
1491 lgorrie 1.103 (defimplementation getpid ()
1492     (unix:unix-getpid))
1493 lgorrie 1.99
1494 lgorrie 1.103 (defimplementation lisp-implementation-type-name ()
1495     "cmucl")
1496 heller 1.92
1497 lgorrie 1.103 (defimplementation quit-lisp ()
1498     (ext::quit))
1499 heller 1.19
1500 lgorrie 1.103 ;;; source-path-{stream,file,string,etc}-position moved into
1501     ;;; swank-source-path-parser
1502 dbarlow 1.1
1503    
1504 lgorrie 1.25 ;;;; Debugging
1505 dbarlow 1.1
1506     (defvar *sldb-stack-top*)
1507    
1508 lgorrie 1.56 (defimplementation call-with-debugging-environment (debugger-loop-fn)
1509 dbarlow 1.1 (unix:unix-sigsetmask 0)
1510 lgorrie 1.25 (let* ((*sldb-stack-top* (or debug:*stack-top-hint* (di:top-frame)))
1511 heller 1.75 (debug:*stack-top-hint* nil))
1512 heller 1.112 (handler-bind ((di::unhandled-condition
1513 dbarlow 1.1 (lambda (condition)
1514 heller 1.112 (error (make-condition
1515     'sldb-condition
1516     :original-condition condition)))))
1517 lgorrie 1.25 (funcall debugger-loop-fn))))
1518 dbarlow 1.1
1519 heller 1.112 (defun frame-down (frame)
1520     (handler-case (di:frame-down frame)
1521     (di:no-debug-info () nil)))
1522    
1523 dbarlow 1.1 (defun nth-frame (index)
1524 heller 1.112 (do ((frame *sldb-stack-top* (frame-down frame))
1525 dbarlow 1.1 (i index (1- i)))
1526     ((zerop i) frame)))
1527    
1528 heller 1.80 (defimplementation compute-backtrace (start end)
1529 dbarlow 1.1 (let ((end (or end most-positive-fixnum)))
1530 heller 1.112 (loop for f = (nth-frame start) then (frame-down f)
1531 dbarlow 1.1 for i from start below end
1532     while f
1533 heller 1.80 collect f)))
1534 dbarlow 1.1
1535 heller 1.80 (defimplementation print-frame (frame stream)
1536     (let ((*standard-output* stream))
1537 heller 1.114 (handler-case
1538     (debug::print-frame-call frame :verbosity 1 :number nil)
1539     (error (e)
1540     (ignore-errors (princ e stream))))))
1541 dbarlow 1.1
1542 lgorrie 1.56 (defimplementation frame-source-location-for-emacs (index)
1543 heller 1.29 (code-location-source-location (di:frame-code-location (nth-frame index))))
1544 dbarlow 1.1
1545 lgorrie 1.56 (defimplementation eval-in-frame (form index)
1546 lgorrie 1.26 (di:eval-in-frame (nth-frame index) form))
1547 heller 1.19
1548 heller 1.108 (defun frame-debug-vars (frame)
1549     "Return a vector of debug-variables in frame."
1550     (di::debug-function-debug-variables (di:frame-debug-function frame)))
1551    
1552     (defun debug-var-value (var frame location)
1553     (ecase (di:debug-variable-validity var location)
1554     (:valid (di:debug-variable-value var frame))
1555     ((:invalid :unknown) ':<not-available>)))
1556    
1557 lgorrie 1.56 (defimplementation frame-locals (index)
1558 dbarlow 1.1 (let* ((frame (nth-frame index))
1559 heller 1.108 (loc (di:frame-code-location frame))
1560     (vars (frame-debug-vars frame)))
1561     (loop for v across vars collect
1562 heller 1.80 (list :name (di:debug-variable-symbol v)
1563 heller 1.43 :id (di:debug-variable-id v)
1564 heller 1.108 :value (debug-var-value v frame loc)))))
1565    
1566     (defimplementation frame-var-value (frame var)
1567     (let* ((frame (nth-frame frame))
1568     (dvar (aref (frame-debug-vars frame) var)))
1569     (debug-var-value dvar frame (di:frame-code-location frame))))
1570 dbarlow 1.1
1571 lgorrie 1.56 (defimplementation frame-catch-tags (index)
1572 heller 1.80 (mapcar #'car (di:frame-catches (nth-frame index))))
1573 heller 1.33
1574     (defun set-step-breakpoints (frame)
1575     (when (di:debug-block-elsewhere-p (di:code-location-debug-block
1576     (di:frame-code-location frame)))
1577     (error "Cannot step, in elsewhere code~%"))
1578     (let* ((code-location (di:frame-code-location frame))
1579 heller 1.36 (debug::*bad-code-location-types*
1580     (remove :call-site debug::*bad-code-location-types*))
1581 heller 1.33 (next (debug::next-code-locations code-location)))
1582     (cond (next
1583     (let ((steppoints '()))
1584     (flet ((hook (frame breakpoint)
1585     (let ((debug:*stack-top-hint* frame))
1586 heller 1.36 (mapc #'di:delete-breakpoint steppoints)
1587     (let ((cl (di::breakpoint-what breakpoint)))
1588     (break "Breakpoint: ~S ~S"
1589     (di:code-location-kind cl)
1590     (di::compiled-code-location-pc cl))))))
1591 heller 1.33 (dolist (code-location next)
1592     (let ((bp (di:make-breakpoint #'hook code-location
1593     :kind :code-location)))
1594     (di:activate-breakpoint bp)
1595     (push bp steppoints))))))
1596     (t
1597     (flet ((hook (frame breakpoint values cookie)
1598     (declare (ignore cookie))
1599 heller 1.36 (di:delete-breakpoint breakpoint)
1600 heller 1.33 (let ((debug:*stack-top-hint* frame))
1601     (break "Function-end: ~A ~A" breakpoint values))))
1602     (let* ((debug-function (di:frame-debug-function frame))
1603     (bp (di:make-breakpoint #'hook debug-function
1604     :kind :function-end)))
1605     (di:activate-breakpoint bp)))))))
1606    
1607 heller 1.107 (defimplementation sldb-step (frame)
1608     (cond ((find-restart 'continue)
1609     (set-step-breakpoints (nth-frame frame))
1610     (continue))
1611     (t
1612     (error "No continue restart."))))
1613 dbarlow 1.1
1614 heller 1.58 (defun frame-cfp (frame)
1615     "Return the Control-Stack-Frame-Pointer for FRAME."
1616     (etypecase frame
1617     (di::compiled-frame (di::frame-pointer frame))
1618     ((or di::interpreted-frame null) -1)))
1619    
1620     (defun frame-ip (frame)
1621     "Return the (absolute) instruction pointer and the relative pc of FRAME."
1622     (if (not frame)
1623     -1
1624     (let ((debug-fun (di::frame-debug-function frame)))
1625     (etypecase debug-fun
1626     (di::compiled-debug-function
1627     (let* ((code-loc (di:frame-code-location frame))
1628     (component (di::compiled-debug-function-component debug-fun))
1629     (pc (di::compiled-code-location-pc code-loc))
1630     (ip (sys:without-gcing
1631     (sys:sap-int
1632     (sys:sap+ (kernel:code-instructions component) pc)))))
1633     (values ip pc)))
1634     ((or di::bogus-debug-function di::interpreted-debug-function)
1635     -1)))))
1636    
1637     (defun frame-registers (frame)
1638     "Return the lisp registers CSP, CFP, IP, OCFP, LRA for FRAME-NUMBER."
1639     (let* ((cfp (frame-cfp frame))
1640     (csp (frame-cfp (di::frame-up frame)))
1641     (ip (frame-ip frame))
1642     (ocfp (frame-cfp (di::frame-down frame)))
1643     (lra (frame-ip (di::frame-down frame))))
1644     (values csp cfp ip ocfp lra)))
1645    
1646     (defun print-frame-registers (frame-number)
1647     (let ((frame (di::frame-real-frame (nth-frame frame-number))))
1648     (flet ((fixnum (p) (etypecase p
1649     (integer p)
1650     (sys:system-area-pointer (sys:sap-int p)))))
1651     (apply #'format t "~
1652     CSP = ~X
1653     CFP = ~X
1654     IP = ~X
1655     OCFP = ~X
1656     LRA = ~X~%" (mapcar #'fixnum
1657     (multiple-value-list (frame-registers frame)))))))
1658    
1659 heller 1.82
1660 heller 1.81 (defimplementation disassemble-frame (frame-number)
1661     "Return a string with the disassembly of frames code."
1662     (print-frame-registers frame-number)
1663     (terpri)
1664     (let* ((frame (di::frame-real-frame (nth-frame frame-number)))
1665     (debug-fun (di::frame-debug-function frame)))
1666     (etypecase debug-fun
1667     (di::compiled-debug-function
1668     (let* ((component (di::compiled-debug-function-component debug-fun))
1669     (fun (di:debug-function-function debug-fun)))
1670     (if fun
1671     (disassemble fun)
1672     (disassem:disassemble-code-component component))))
1673     (di::bogus-debug-function
1674     (format t "~%[Disassembling bogus frames not implemented]")))))
1675 heller 1.58
1676 dbarlow 1.1
1677 lgorrie 1.25 ;;;; Inspecting
1678 dbarlow 1.1
1679     (defconstant +lowtag-symbols+
1680     '(vm:even-fixnum-type
1681     vm:function-pointer-type
1682     vm:other-immediate-0-type
1683     vm:list-pointer-type
1684     vm:odd-fixnum-type
1685     vm:instance-pointer-type
1686     vm:other-immediate-1-type
1687 lgorrie 1.103 vm:other-pointer-type)
1688     "Names of the constants that specify type tags.
1689     The `symbol-value' of each element is a type tag.")
1690 dbarlow 1.1
1691     (defconstant +header-type-symbols+
1692 heller 1.102 (flet ((suffixp (suffix string)
1693     (and (>= (length string) (length suffix))
1694     (string= string suffix :start1 (- (length string)
1695     (length suffix))))))
1696     ;; Is there a convinient place for all those constants?
1697 dbarlow 1.1 (remove-if-not
1698 heller 1.102 (lambda (x) (and (suffixp "-TYPE" (symbol-name x))
1699     (not (member x +lowtag-symbols+))
1700     (boundp x)
1701     (typep (symbol-value x) 'fixnum)))
1702 dbarlow 1.1 (append (apropos-list "-TYPE" "VM" t)
1703 heller 1.102 (apropos-list "-TYPE" "BIGNUM" t))))
1704     "A list of names of the type codes in boxed objects.")
1705 heller 1.62
1706     (defimplementation describe-primitive-type (object)
1707 dbarlow 1.1 (with-output-to-string (*standard-output*)
1708     (let* ((lowtag (kernel:get-lowtag object))
1709     (lowtag-symbol (find lowtag +lowtag-symbols+ :key #'symbol-value)))
1710 heller 1.76 (format t "lowtag: ~A" lowtag-symbol)
1711     (when (member lowtag (list vm:other-pointer-type
1712     vm:function-pointer-type
1713     vm:other-immediate-0-type
1714     vm:other-immediate-1-type
1715     ))
1716     (let* ((type (kernel:get-type object))
1717     (type-symbol (find type +header-type-symbols+
1718     :key #'symbol-value)))
1719     (format t ", type: ~A" type-symbol))))))
1720 dbarlow 1.1
1721 heller 1.62 (defimplementation inspected-parts (o)
1722 dbarlow 1.1 (cond ((di::indirect-value-cell-p o)
1723     (inspected-parts-of-value-cell o))
1724     (t
1725     (destructuring-bind (text labeledp . parts)
1726     (inspect::describe-parts o)
1727     (let ((parts (if labeledp
1728     (loop for (label . value) in parts
1729     collect (cons (string label) value))
1730     (loop for value in parts
1731     for i from 0
1732     collect (cons (format nil "~D" i) value)))))
1733     (values text parts))))))
1734    
1735 heller 1.55 (defun inspected-parts-of-value-cell (o)
1736     (values (format nil "~A~% is a value cell." o)
1737     (list (cons "Value" (c:value-cell-ref o)))))
1738    
1739 dbarlow 1.1 (defmethod inspected-parts ((o function))
1740     (let ((header (kernel:get-type o)))
1741     (cond ((= header vm:function-header-type)
1742     (values
1743     (format nil "~A~% is a function." o)
1744     (list (cons "Self" (kernel:%function-self o))
1745     (cons "Next" (kernel:%function-next o))
1746     (cons "Name" (kernel:%function-name o))
1747     (cons "Arglist" (kernel:%function-arglist o))
1748     (cons "Type" (kernel:%function-type o))
1749     (cons "Code Object" (kernel:function-code-header o)))))
1750     ((= header vm:closure-header-type)
1751     (values (format nil "~A~% is a closure." o)
1752     (list*
1753     (cons "Function" (kernel:%closure-function o))
1754     (loop for i from 0 below (- (kernel:get-closure-length o)
1755     (1- vm:closure-info-offset))
1756     collect (cons (format nil "~D" i)
1757     (kernel:%closure-index-ref o i))))))
1758     (t (call-next-method o)))))
1759    
1760     (defmethod inspected-parts ((o kernel:code-component))
1761     (values (format nil "~A~% is a code data-block." o)
1762     `(("First entry point" . ,(kernel:%code-entry-points o))
1763     ,@(loop for i from vm:code-constants-offset
1764     below (kernel:get-header-data o)
1765     collect (cons (format nil "Constant#~D" i)
1766     (kernel:code-header-ref o i)))
1767     ("Debug info" . ,(kernel:%code-debug-info o))
1768     ("Instructions" . ,(kernel:code-instructions o)))))
1769    
1770     (defmethod inspected-parts ((o kernel:fdefn))
1771     (values (format nil "~A~% is a fdefn object." o)
1772     `(("Name" . ,(kernel:fdefn-name o))
1773     ("Function" . ,(kernel:fdefn-function o)))))
1774 heller 1.58
1775    
1776     ;;;; Profiling
1777     (defimplementation profile (fname)
1778     (eval `(profile:profile ,fname)))
1779    
1780     (defimplementation unprofile (fname)
1781     (eval `(profile:unprofile ,fname)))
1782    
1783     (defimplementation unprofile-all ()
1784 lgorrie 1.105 (profile:unprofile)
1785 heller 1.58 "All functions unprofiled.")
1786    
1787     (defimplementation profile-report ()
1788 lgorrie 1.105 (profile:report-time))
1789 heller 1.58
1790     (defimplementation profile-reset ()
1791 lgorrie 1.105 (profile:reset-time)
1792 heller 1.58 "Reset profiling counters.")
1793    
1794     (defimplementation profiled-functions ()
1795     profile:*timed-functions*)
1796    
1797     (defimplementation profile-package (package callers methods)
1798 heller 1.80 (profile:profile-all :package package
1799 heller 1.58 :callers-p callers
1800 lgorrie 1.105 #-cmu18e :methods #-cmu18e methods))
1801 dbarlow 1.1
1802 lgorrie 1.42
1803     ;;;; Multiprocessing
1804    
1805 heller 1.91 #+mp
1806 lgorrie 1.42 (progn
1807 lgorrie 1.56 (defimplementation startup-multiprocessing ()
1808 lgorrie 1.49 ;; Threads magic: this never returns! But top-level becomes
1809     ;; available again.
1810 lgorrie 1.42 (mp::startup-idle-and-top-level-loops))
1811 heller 1.54
1812 heller 1.59 (defimplementation spawn (fn &key (name "Anonymous"))
1813 heller 1.54 (mp:make-process fn :name name))
1814 lgorrie 1.42
1815 heller 1.109 (defvar *thread-id-counter* 0)
1816    
1817     (defimplementation thread-id (thread)
1818     (or (getf (mp:process-property-list thread) 'id)
1819     (setf (getf (mp:process-property-list thread) 'id)
1820     (incf *thread-id-counter*))))
1821    
1822     (defimplementation find-thread (id)
1823     (find id (all-threads)
1824     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
1825    
1826 heller 1.61 (defimplementation thread-name (thread)
1827     (mp:process-name thread))
1828 lgorrie 1.42
1829 heller 1.61 (defimplementation thread-status (thread)
1830     (mp:process-whostate thread))
1831 lgorrie 1.42
1832 heller 1.61 (defimplementation current-thread ()
1833     mp:*current-process*)
1834 lgorrie 1.42
1835 heller 1.61 (defimplementation all-threads ()
1836     (copy-list mp:*all-processes*))
1837 heller 1.62
1838     (defimplementation interrupt-thread (thread fn)
1839     (mp:process-interrupt thread fn))
1840    
1841 heller 1.73 (defimplementation kill-thread (thread)
1842     (mp:destroy-process thread))
1843    
1844 heller 1.62 (defvar *mailbox-lock* (mp:make-lock "mailbox lock"))
1845    
1846     (defstruct (mailbox (:conc-name mailbox.))
1847     (mutex (mp:make-lock "process mailbox"))
1848     (queue '() :type list))
1849    
1850     (defun mailbox (thread)
1851     "Return THREAD's mailbox."
1852     (mp:with-lock-held (*mailbox-lock*)
1853     (or (getf (mp:process-property-list thread) 'mailbox)
1854     (setf (getf (mp:process-property-list thread) 'mailbox)
1855     (make-mailbox)))))
1856    
1857     (defimplementation send (thread message)
1858     (let* ((mbox (mailbox thread))
1859     (mutex (mailbox.mutex mbox)))
1860     (mp:with-lock-held (mutex)
1861     (setf (mailbox.queue mbox)
1862     (nconc (mailbox.queue mbox) (list message))))))
1863    
1864     (defimplementation receive ()
1865     (let* ((mbox (mailbox mp:*current-process*))
1866     (mutex (mailbox.mutex mbox)))
1867     (mp:process-wait "receive" #'mailbox.queue mbox)
1868     (mp:with-lock-held (mutex)
1869     (pop (mailbox.queue mbox)))))
1870 heller 1.59
1871 lgorrie 1.103 ) ;; #+mp
1872    
1873     ;; Local Variables:
1874     ;; pbook-heading-regexp: "^;;;\\(;+\\)"
1875     ;; pbook-commentary-regexp: "^;;;\\($\\|[^;]\\)"
1876     ;; End:

  ViewVC Help
Powered by ViewVC 1.1.5