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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5