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

Contents of /slime/swank-cmucl.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5