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

Contents of /slime/swank-corman.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sun Jul 3 15:51:05 2005 UTC (8 years, 9 months ago) by heller
Branch: MAIN
Changes since 1.2: +93 -8 lines
(default-directory): Return a namestring instead of the pathname.

(inspect-for-emacs, inspect-structure): Teach the inspector how to deal with structures.

(spawn, send, receive): Implement rudimentary threading support. It's
now possible to connect with the :spawn communication style and to
bring up a listener.  Unfortunately, debugging the non-primary threads
doesn't work at all.  Still no support for interrupt-thread.
1 ;;;
2 ;;; swank-corman.lisp --- Corman Lisp specific code for SLIME.
3 ;;;
4 ;;; Copyright (C) 2004, 2005 Espen Wiborg (espenhw@grumblesmurf.org)
5 ;;;
6 ;;; License
7 ;;; =======
8 ;;; This software is provided 'as-is', without any express or implied
9 ;;; warranty. In no event will the author be held liable for any damages
10 ;;; arising from the use of this software.
11 ;;;
12 ;;; Permission is granted to anyone to use this software for any purpose,
13 ;;; including commercial applications, and to alter it and redistribute
14 ;;; it freely, subject to the following restrictions:
15 ;;;
16 ;;; 1. The origin of this software must not be misrepresented; you must
17 ;;; not claim that you wrote the original software. If you use this
18 ;;; software in a product, an acknowledgment in the product documentation
19 ;;; would be appreciated but is not required.
20 ;;;
21 ;;; 2. Altered source versions must be plainly marked as such, and must
22 ;;; not be misrepresented as being the original software.
23 ;;;
24 ;;; 3. This notice may not be removed or altered from any source
25 ;;; distribution.
26 ;;;
27 ;;; Notes
28 ;;; =====
29 ;;; You will need CCL 2.51, and you will *definitely* need to patch
30 ;;; CCL with the patches at
31 ;;; http://www.grumblesmurf.org/lisp/corman-patches, otherwise SLIME
32 ;;; will blow up in your face. You should also follow the
33 ;;; instructions on http://www.grumblesmurf.org/lisp/corman-slime.
34 ;;;
35 ;;; The only communication style currently supported is NIL.
36 ;;;
37 ;;; Starting CCL inside emacs (with M-x slime) seems to work for me
38 ;;; with Corman Lisp 2.51, but I have seen random failures with 2.5
39 ;;; (sometimes it works, other times it hangs on start or hangs when
40 ;;; initializing WinSock) - starting CCL externally and using M-x
41 ;;; slime-connect always works fine.
42 ;;;
43 ;;; Sometimes CCL gets confused and starts giving you random memory
44 ;;; access violation errors on startup; if this happens, try redumping
45 ;;; your image.
46 ;;;
47 ;;; What works
48 ;;; ==========
49 ;;; * Basic editing and evaluation
50 ;;; * Arglist display
51 ;;; * Compilation
52 ;;; * Loading files
53 ;;; * apropos/describe
54 ;;; * Debugger
55 ;;; * Inspector
56 ;;;
57 ;;; TODO
58 ;;; ====
59 ;;; * More debugger functionality (missing bits: restart-frame,
60 ;;; return-from-frame, disassemble-frame, activate-stepping,
61 ;;; toggle-trace)
62 ;;; * XREF
63 ;;; * Profiling
64 ;;; * More sophisticated communication styles than NIL
65 ;;;
66
67 (in-package :swank-backend)
68
69 ;;; Pull in various needed bits
70 (require :composite-streams)
71 (require :sockets)
72 (require :winbase)
73 (require :lp)
74
75 (use-package :gs)
76
77 ;; MOP stuff
78
79 (defclass swank-mop:standard-slot-definition ()
80 ()
81 (:documentation "Dummy class created so that swank.lisp will compile and load."))
82
83 (defun named-by-gensym-p (c)
84 (null (symbol-package (class-name c))))
85
86 (deftype swank-mop:eql-specializer ()
87 '(satisfies named-by-gensym-p))
88
89 (defun swank-mop:eql-specializer-object (specializer)
90 (with-hash-table-iterator (next-entry cl::*clos-singleton-specializers*)
91 (loop (multiple-value-bind (more key value)
92 (next-entry)
93 (unless more (return nil))
94 (when (eq specializer value)
95 (return key))))))
96
97 (defun swank-mop:class-finalized-p (class)
98 (declare (ignore class))
99 t)
100
101 (defun swank-mop:class-prototype (class)
102 (make-instance class))
103
104 (defun swank-mop:specializer-direct-methods (obj)
105 (declare (ignore obj))
106 nil)
107
108 (defun swank-mop:generic-function-argument-precedence-order (gf)
109 (generic-function-lambda-list gf))
110
111 (defun swank-mop:generic-function-method-combination (gf)
112 (declare (ignore gf))
113 :standard)
114
115 (defun swank-mop:generic-function-declarations (gf)
116 (declare (ignore gf))
117 nil)
118
119 (defun swank-mop:slot-definition-documentation (slot)
120 (declare (ignore slot))
121 (getf slot :documentation nil))
122
123 (defun swank-mop:slot-definition-type (slot)
124 (declare (ignore slot))
125 t)
126
127 (import-swank-mop-symbols :cl '(;; classes
128 :standard-slot-definition
129 :eql-specializer
130 :eql-specializer-object
131 ;; standard class readers
132 :class-default-initargs
133 :class-direct-default-initargs
134 :class-finalized-p
135 :class-prototype
136 :specializer-direct-methods
137 ;; gf readers
138 :generic-function-argument-precedence-order
139 :generic-function-declarations
140 :generic-function-method-combination
141 ;; method readers
142 ;; slot readers
143 :slot-definition-documentation
144 :slot-definition-type))
145
146 ;;;; swank implementations
147
148 ;;; Debugger
149
150 (defvar *stack-trace* nil)
151 (defvar *frame-trace* nil)
152
153 (defstruct frame
154 name function address debug-info variables)
155
156 (defimplementation call-with-debugging-environment (fn)
157 (let* ((real-stack-trace (cl::stack-trace))
158 (*stack-trace* (cdr (member 'cl:invoke-debugger real-stack-trace
159 :key #'car)))
160 (*frame-trace*
161 (let* ((db::*debug-level* 1)
162 (db::*debug-frame-pointer* (db::stash-ebp
163 (ct:create-foreign-ptr)))
164 (db::*debug-max-level* (length real-stack-trace))
165 (db::*debug-min-level* 1))
166 (cdr (member #'cl:invoke-debugger
167 (cons
168 (make-frame :function nil)
169 (loop for i from db::*debug-min-level*
170 upto db::*debug-max-level*
171 until (eq (db::get-frame-function i) cl::*top-level*)
172 collect
173 (make-frame :function (db::get-frame-function i)
174 :address (db::get-frame-address i))))
175 :key #'frame-function)))))
176 (funcall fn)))
177
178 (defimplementation compute-backtrace (start end)
179 (subseq *stack-trace* start (min end (length *stack-trace*))))
180
181 (defimplementation print-frame (frame stream)
182 (format stream "~S" frame))
183
184 (defun get-frame-debug-info (frame)
185 (or (frame-debug-info frame)
186 (setf (frame-debug-info frame)
187 (db::prepare-frame-debug-info (frame-function frame)
188 (frame-address frame)))))
189
190 (defimplementation frame-locals (frame-number)
191 (let* ((frame (elt *frame-trace* frame-number))
192 (info (get-frame-debug-info frame)))
193 (let ((var-list
194 (loop for i from 4 below (length info) by 2
195 collect `(list :name ',(svref info i) :id 0
196 :value (db::debug-filter ,(svref info i))))))
197 (let ((vars (eval-in-frame `(list ,@var-list) frame-number)))
198 (setf (frame-variables frame) vars)))))
199
200 (defimplementation eval-in-frame (form frame-number)
201 (let ((frame (elt *frame-trace* frame-number)))
202 (let ((cl::*compiler-environment* (get-frame-debug-info frame)))
203 (eval form))))
204
205 (defimplementation frame-catch-tags (index)
206 (declare (ignore index))
207 nil)
208
209 (defimplementation frame-var-value (frame-number var)
210 (let ((vars (frame-variables (elt *frame-trace* frame-number))))
211 (when vars
212 (second (elt vars var)))))
213
214 (defimplementation frame-source-location-for-emacs (frame-number)
215 (fspec-location (frame-function (elt *frame-trace* frame-number))))
216
217 ;;; Socket communication
218
219 (defimplementation create-socket (host port)
220 (sockets:start-sockets)
221 (sockets:make-server-socket :host host :port port))
222
223 (defimplementation local-port (socket)
224 (sockets:socket-port socket))
225
226 (defimplementation close-socket (socket)
227 (close socket))
228
229 (defimplementation accept-connection (socket
230 &key (external-format :iso-latin-1-unix))
231 (ecase external-format
232 (:iso-latin-1-unix
233 (sockets:make-socket-stream (sockets:accept-socket socket)))))
234
235 ;;; Misc
236
237 (defimplementation preferred-communication-style ()
238 nil)
239
240 (defimplementation getpid ()
241 ccl:*current-process-id*)
242
243 (defimplementation lisp-implementation-type-name ()
244 "cormanlisp")
245
246 (defimplementation quit-lisp ()
247 (sockets:stop-sockets)
248 (win32:exitprocess 0))
249
250 (defimplementation set-default-directory (directory)
251 (setf (ccl:current-directory) directory)
252 (directory-namestring (setf *default-pathname-defaults*
253 (truename (merge-pathnames directory)))))
254
255 (defimplementation default-directory ()
256 (directory-namestring (ccl:current-directory)))
257
258 (defimplementation macroexpand-all (form)
259 (ccl:macroexpand-all form))
260
261 ;;; Documentation
262
263 (defun fspec-location (fspec)
264 (when (symbolp fspec)
265 (setq fspec (symbol-function fspec)))
266 (let ((file (ccl::function-source-file fspec)))
267 (if file
268 (handler-case
269 (let ((truename (truename
270 (merge-pathnames file
271 ccl:*cormanlisp-directory*))))
272 (make-location (list :file (namestring truename))
273 (if (ccl::function-source-line fspec)
274 (list :line
275 (1+ (ccl::function-source-line fspec)))
276 (list :function-name (princ-to-string
277 (function-name fspec))))))
278 (error (c) (list :error (princ-to-string c))))
279 (list :error (format nil "No source information available for ~S"
280 fspec)))))
281
282 (defimplementation find-definitions (name)
283 (list (list name (fspec-location name))))
284
285 (defimplementation arglist (name)
286 (handler-case
287 (cond ((and (symbolp name)
288 (macro-function name))
289 (ccl::macro-lambda-list (symbol-function name)))
290 (t
291 (when (symbolp name)
292 (setq name (symbol-function name)))
293 (if (eq (class-of name) cl::the-class-standard-gf)
294 (generic-function-lambda-list name)
295 (ccl:function-lambda-list name))))
296 (error () :not-available)))
297
298 (defimplementation function-name (fn)
299 (handler-case (getf (cl::function-info-list fn) 'cl::function-name)
300 (error () nil)))
301
302 (defimplementation describe-symbol-for-emacs (symbol)
303 (let ((result '()))
304 (flet ((doc (kind &optional (sym symbol))
305 (or (documentation sym kind) :not-documented))
306 (maybe-push (property value)
307 (when value
308 (setf result (list* property value result)))))
309 (maybe-push
310 :variable (when (boundp symbol)
311 (doc 'variable)))
312 (maybe-push
313 :function (if (fboundp symbol)
314 (doc 'function)))
315 (maybe-push
316 :class (if (find-class symbol nil)
317 (doc 'class)))
318 result)))
319
320 (defimplementation describe-definition (symbol namespace)
321 (ecase namespace
322 (:variable
323 (describe symbol))
324 ((:function :generic-function)
325 (describe (symbol-function symbol)))
326 (:class
327 (describe (find-class symbol)))))
328
329 ;;; Compiler
330
331 (defvar *buffer-name* nil)
332 (defvar *buffer-position*)
333 (defvar *buffer-string*)
334 (defvar *compile-filename* nil)
335
336 ;; FIXME
337 (defimplementation call-with-compilation-hooks (FN)
338 (handler-bind ((error (lambda (c)
339 (signal (make-condition
340 'compiler-condition
341 :original-condition c
342 :severity :warning
343 :message (format nil "~A" c)
344 :location
345 (cond (*buffer-name*
346 (make-location
347 (list :buffer *buffer-name*)
348 (list :position *buffer-position*)))
349 (*compile-filename*
350 (make-location
351 (list :file *compile-filename*)
352 (list :position 1)))
353 (t
354 (list :error "No location"))))))))
355 (funcall fn)))
356
357 (defimplementation swank-compile-file (*compile-filename* load-p)
358 (with-compilation-hooks ()
359 (let ((*buffer-name* nil))
360 (compile-file *compile-filename*)
361 (when load-p
362 (load (compile-file-pathname *compile-filename*))))))
363
364 (defimplementation swank-compile-string (string &key buffer position directory)
365 (declare (ignore directory))
366 (with-compilation-hooks ()
367 (let ((*buffer-name* buffer)
368 (*buffer-position* position)
369 (*buffer-string* string))
370 (funcall (compile nil (read-from-string
371 (format nil "(~S () ~A)" 'lambda string)))))))
372
373 ;;;; Inspecting
374
375 (defclass corman-inspector (inspector)
376 ())
377
378 (defimplementation make-default-inspector ()
379 (make-instance 'corman-inspector))
380
381 (defun comma-separated (list &optional (callback (lambda (v)
382 `(:value ,v))))
383 (butlast (loop for e in list
384 collect (funcall callback e)
385 collect ", ")))
386
387 (defimplementation inspect-for-emacs ((class standard-class)
388 (inspector corman-inspector))
389 (declare (ignore inspector))
390 (values "A class."
391 `("Name: " (:value ,(class-name class))
392 (:newline)
393 "Super classes: "
394 ,@(comma-separated (swank-mop:class-direct-superclasses class))
395 (:newline)
396 "Direct Slots: "
397 ,@(comma-separated
398 (swank-mop:class-direct-slots class)
399 (lambda (slot)
400 `(:value ,slot ,(princ-to-string (swank-mop:slot-definition-name slot)))))
401 (:newline)
402 "Effective Slots: "
403 ,@(if (swank-mop:class-finalized-p class)
404 (comma-separated
405 (swank-mop:class-slots class)
406 (lambda (slot)
407 `(:value ,slot ,(princ-to-string
408 (swank-mop:slot-definition-name slot)))))
409 '("#<N/A (class not finalized)>"))
410 (:newline)
411 ,@(when (documentation class t)
412 `("Documentation:" (:newline) ,(documentation class t) (:newline)))
413 "Sub classes: "
414 ,@(comma-separated (swank-mop:class-direct-subclasses class)
415 (lambda (sub)
416 `(:value ,sub ,(princ-to-string (class-name sub)))))
417 (:newline)
418 "Precedence List: "
419 ,@(if (swank-mop:class-finalized-p class)
420 (comma-separated (swank-mop:class-precedence-list class)
421 (lambda (class)
422 `(:value ,class ,(princ-to-string (class-name class)))))
423 '("#<N/A (class not finalized)>"))
424 (:newline))))
425
426 (defimplementation inspect-for-emacs ((slot cons) (inspector corman-inspector))
427 ;; Inspects slot definitions
428 (declare (ignore corman-inspector))
429 (if (eq (car slot) :name)
430 (values "A slot."
431 `("Name: " (:value ,(swank-mop:slot-definition-name slot))
432 (:newline)
433 ,@(when (swank-mop:slot-definition-documentation slot)
434 `("Documentation:" (:newline)
435 (:value ,(swank-mop:slot-definition-documentation slot))
436 (:newline)))
437 "Init args: " (:value ,(swank-mop:slot-definition-initargs slot)) (:newline)
438 "Init form: " ,(if (swank-mop:slot-definition-initfunction slot)
439 `(:value ,(swank-mop:slot-definition-initform slot))
440 "#<unspecified>") (:newline)
441 "Init function: " (:value ,(swank-mop:slot-definition-initfunction slot))
442 (:newline)))
443 (call-next-method)))
444
445 (defimplementation inspect-for-emacs ((pathname pathnames::pathname-internal)
446 inspector)
447 (declare (ignore inspector))
448 (values (if (wild-pathname-p pathname)
449 "A wild pathname."
450 "A pathname.")
451 (append (label-value-line*
452 ("Namestring" (namestring pathname))
453 ("Host" (pathname-host pathname))
454 ("Device" (pathname-device pathname))
455 ("Directory" (pathname-directory pathname))
456 ("Name" (pathname-name pathname))
457 ("Type" (pathname-type pathname))
458 ("Version" (pathname-version pathname)))
459 (unless (or (wild-pathname-p pathname)
460 (not (probe-file pathname)))
461 (label-value-line "Truename" (truename pathname))))))
462
463 (defimplementation inspect-for-emacs ((o t) (inspector corman-inspector))
464 (cond ((cl::structurep o) (inspect-structure o))
465 (t (call-next-method))))
466
467 (defun inspect-structure (o)
468 (values
469 (format nil "~A is a structure" o)
470 (let* ((template (cl::uref o 1))
471 (num-slots (cl::struct-template-num-slots template)))
472 (cond ((symbolp template)
473 (loop for i below num-slots
474 append (label-value-line i (cl::uref o (+ 2 i)))))
475 (t
476 (loop for i below num-slots
477 append (label-value-line (elt template (+ 6 (* i 5)))
478 (cl::uref o (+ 2 i)))))))))
479
480
481 ;;; Threads
482
483 (require 'threads)
484
485 (defstruct (mailbox (:conc-name mailbox.))
486 thread
487 (lock (make-instance 'threads:critical-section))
488 (queue '() :type list))
489
490 (defvar *mailbox-lock* (make-instance 'threads:critical-section))
491 (defvar *mailboxes* (list))
492
493 (defmacro with-lock (lock &body body)
494 `(threads:with-synchronization (threads:cs ,lock)
495 ,@body))
496
497 (defimplementation spawn (fun &key name)
498 (declare (ignore name))
499 (threads:create-thread
500 (lambda ()
501 (unwind-protect (funcall fun)
502 (with-lock *mailbox-lock*
503 (setq *mailboxes* (remove cormanlisp:*current-thread-id*
504 *mailboxes* :key #'mailbox.thread)))))))
505
506 (defimplementation thread-id (thread)
507 thread)
508
509 (defimplementation find-thread (thread)
510 (if (thread-alive-p thread)
511 thread))
512
513 (defimplementation current-thread ()
514 cormanlisp:*current-thread-id*)
515
516 ;; XXX implement it
517 (defimplementation all-threads ()
518 '())
519
520 (defimplementation thread-alive-p (thread)
521 t)
522
523 ;; XXX something here is broken
524 (defimplementation kill-thread (thread)
525 (threads:terminate-thread thread 'killed))
526
527 (defun mailbox (thread)
528 (with-lock *mailbox-lock*
529 (or (find thread *mailboxes* :key #'mailbox.thread)
530 (let ((mb (make-mailbox :thread thread)))
531 (push mb *mailboxes*)
532 mb))))
533
534 (defimplementation send (thread message)
535 (let ((mbox (mailbox thread)))
536 (with-lock (mailbox.lock mbox)
537 (setf (mailbox.queue mbox)
538 (nconc (mailbox.queue mbox) (list message))))))
539
540 (defimplementation receive ()
541 (let ((mbox (mailbox cormanlisp:*current-thread-id*)))
542 (loop
543 (with-lock (mailbox.lock mbox)
544 (when (mailbox.queue mbox)
545 (return (pop (mailbox.queue mbox)))))
546 (sleep 0.1))))
547
548
549 ;;; This is probably not good, but it WFM
550 (in-package :common-lisp)
551
552 (defvar *old-documentation* #'documentation)
553 (defun documentation (thing &optional (type 'function))
554 (if (symbolp thing)
555 (funcall *old-documentation* thing type)
556 (values)))
557
558 (defmethod print-object ((restart restart) stream)
559 (if (or *print-escape*
560 *print-readably*)
561 (print-unreadable-object (restart stream :type t :identity t)
562 (princ (restart-name restart) stream))
563 (when (functionp (restart-report-function restart))
564 (funcall (restart-report-function restart) stream))))

  ViewVC Help
Powered by ViewVC 1.1.5