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

Contents of /slime/swank-corman.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (hide annotations)
Thu Aug 23 19:03:37 2007 UTC (6 years, 7 months ago) by heller
Branch: MAIN
Changes since 1.10: +5 -5 lines
Introduce backend-inspector class.

* swank-backend.lisp (backend-inspector): New class. Introduce a named
  class to give as another way to dispatch to backend methods.

* swank-cmucl.lisp: Use backend-inspector class.
* swank-sbcl.lisp: Use backend-inspector class.
* swank-clisp.lisp: Use backend-inspector class.
* swank-lispworks.lisp: Use backend-inspector class.
* swank-allegro.lisp: Use backend-inspector class.
* swank-openmcl.lisp: Use backend-inspector class.
* swank-abcl.lisp: Use backend-inspector class.
* swank-corman.lisp: Use backend-inspector class.
* swank-scl.lisp: Use backend-inspector class.
1 ewiborg 1.2 ;;;
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 heller 1.4 (let* ((db::*debug-level* (1+ db::*debug-level*))
162 ewiborg 1.2 (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 heller 1.3 (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 ewiborg 1.2
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 heller 1.4 (defun break (&optional (format-control "Break") &rest format-arguments)
218     (with-simple-restart (continue "Return from BREAK.")
219     (let ();(*debugger-hook* nil))
220     (let ((condition
221     (make-condition 'simple-condition
222     :format-control format-control
223     :format-arguments format-arguments)))
224     ;;(format *debug-io* ";;; User break: ~A~%" condition)
225     (invoke-debugger condition))))
226     nil)
227    
228 ewiborg 1.2 ;;; Socket communication
229    
230     (defimplementation create-socket (host port)
231     (sockets:start-sockets)
232     (sockets:make-server-socket :host host :port port))
233    
234     (defimplementation local-port (socket)
235     (sockets:socket-port socket))
236    
237     (defimplementation close-socket (socket)
238     (close socket))
239    
240     (defimplementation accept-connection (socket
241 heller 1.9 &key external-format buffering timeout)
242 heller 1.10 (declare (ignore buffering timeout external-format))
243     (sockets:make-socket-stream (sockets:accept-socket socket)))
244 ewiborg 1.2
245     ;;; Misc
246    
247     (defimplementation preferred-communication-style ()
248     nil)
249    
250     (defimplementation getpid ()
251     ccl:*current-process-id*)
252    
253     (defimplementation lisp-implementation-type-name ()
254     "cormanlisp")
255    
256     (defimplementation quit-lisp ()
257     (sockets:stop-sockets)
258     (win32:exitprocess 0))
259    
260     (defimplementation set-default-directory (directory)
261     (setf (ccl:current-directory) directory)
262     (directory-namestring (setf *default-pathname-defaults*
263     (truename (merge-pathnames directory)))))
264    
265     (defimplementation default-directory ()
266 heller 1.3 (directory-namestring (ccl:current-directory)))
267 ewiborg 1.2
268     (defimplementation macroexpand-all (form)
269     (ccl:macroexpand-all form))
270    
271     ;;; Documentation
272    
273     (defun fspec-location (fspec)
274     (when (symbolp fspec)
275     (setq fspec (symbol-function fspec)))
276     (let ((file (ccl::function-source-file fspec)))
277     (if file
278     (handler-case
279     (let ((truename (truename
280     (merge-pathnames file
281     ccl:*cormanlisp-directory*))))
282     (make-location (list :file (namestring truename))
283     (if (ccl::function-source-line fspec)
284 heller 1.3 (list :line
285     (1+ (ccl::function-source-line fspec)))
286 ewiborg 1.2 (list :function-name (princ-to-string
287     (function-name fspec))))))
288     (error (c) (list :error (princ-to-string c))))
289     (list :error (format nil "No source information available for ~S"
290     fspec)))))
291    
292     (defimplementation find-definitions (name)
293     (list (list name (fspec-location name))))
294    
295     (defimplementation arglist (name)
296     (handler-case
297     (cond ((and (symbolp name)
298     (macro-function name))
299     (ccl::macro-lambda-list (symbol-function name)))
300     (t
301     (when (symbolp name)
302     (setq name (symbol-function name)))
303     (if (eq (class-of name) cl::the-class-standard-gf)
304     (generic-function-lambda-list name)
305     (ccl:function-lambda-list name))))
306     (error () :not-available)))
307    
308     (defimplementation function-name (fn)
309     (handler-case (getf (cl::function-info-list fn) 'cl::function-name)
310     (error () nil)))
311    
312     (defimplementation describe-symbol-for-emacs (symbol)
313     (let ((result '()))
314     (flet ((doc (kind &optional (sym symbol))
315     (or (documentation sym kind) :not-documented))
316     (maybe-push (property value)
317     (when value
318     (setf result (list* property value result)))))
319     (maybe-push
320     :variable (when (boundp symbol)
321     (doc 'variable)))
322     (maybe-push
323     :function (if (fboundp symbol)
324     (doc 'function)))
325     (maybe-push
326     :class (if (find-class symbol nil)
327     (doc 'class)))
328     result)))
329    
330     (defimplementation describe-definition (symbol namespace)
331     (ecase namespace
332     (:variable
333     (describe symbol))
334     ((:function :generic-function)
335     (describe (symbol-function symbol)))
336     (:class
337     (describe (find-class symbol)))))
338    
339     ;;; Compiler
340    
341     (defvar *buffer-name* nil)
342     (defvar *buffer-position*)
343     (defvar *buffer-string*)
344     (defvar *compile-filename* nil)
345    
346     ;; FIXME
347     (defimplementation call-with-compilation-hooks (FN)
348     (handler-bind ((error (lambda (c)
349     (signal (make-condition
350     'compiler-condition
351     :original-condition c
352     :severity :warning
353     :message (format nil "~A" c)
354     :location
355     (cond (*buffer-name*
356     (make-location
357     (list :buffer *buffer-name*)
358     (list :position *buffer-position*)))
359     (*compile-filename*
360     (make-location
361     (list :file *compile-filename*)
362     (list :position 1)))
363     (t
364     (list :error "No location"))))))))
365     (funcall fn)))
366    
367 heller 1.4 (defimplementation swank-compile-file (*compile-filename* load-p
368 heller 1.10 external-format)
369 heller 1.4 (declare (ignore external-format))
370 ewiborg 1.2 (with-compilation-hooks ()
371     (let ((*buffer-name* nil))
372     (compile-file *compile-filename*)
373     (when load-p
374     (load (compile-file-pathname *compile-filename*))))))
375    
376     (defimplementation swank-compile-string (string &key buffer position directory)
377     (declare (ignore directory))
378     (with-compilation-hooks ()
379     (let ((*buffer-name* buffer)
380     (*buffer-position* position)
381     (*buffer-string* string))
382     (funcall (compile nil (read-from-string
383     (format nil "(~S () ~A)" 'lambda string)))))))
384    
385     ;;;; Inspecting
386    
387 ewiborg 1.7 ;; Hack to make swank.lisp load, at least
388     (defclass file-stream ())
389    
390 heller 1.11 (defclass corman-inspector (backend-inspector)
391 ewiborg 1.2 ())
392    
393     (defimplementation make-default-inspector ()
394     (make-instance 'corman-inspector))
395    
396     (defun comma-separated (list &optional (callback (lambda (v)
397     `(:value ,v))))
398     (butlast (loop for e in list
399     collect (funcall callback e)
400     collect ", ")))
401    
402 heller 1.8 (defmethod inspect-for-emacs ((class standard-class)
403 heller 1.11 (inspector backend-inspector))
404 ewiborg 1.2 (declare (ignore inspector))
405     (values "A class."
406     `("Name: " (:value ,(class-name class))
407     (:newline)
408     "Super classes: "
409     ,@(comma-separated (swank-mop:class-direct-superclasses class))
410     (:newline)
411     "Direct Slots: "
412     ,@(comma-separated
413     (swank-mop:class-direct-slots class)
414     (lambda (slot)
415     `(:value ,slot ,(princ-to-string (swank-mop:slot-definition-name slot)))))
416     (:newline)
417     "Effective Slots: "
418     ,@(if (swank-mop:class-finalized-p class)
419     (comma-separated
420     (swank-mop:class-slots class)
421     (lambda (slot)
422     `(:value ,slot ,(princ-to-string
423     (swank-mop:slot-definition-name slot)))))
424     '("#<N/A (class not finalized)>"))
425     (:newline)
426     ,@(when (documentation class t)
427     `("Documentation:" (:newline) ,(documentation class t) (:newline)))
428     "Sub classes: "
429     ,@(comma-separated (swank-mop:class-direct-subclasses class)
430     (lambda (sub)
431     `(:value ,sub ,(princ-to-string (class-name sub)))))
432     (:newline)
433     "Precedence List: "
434     ,@(if (swank-mop:class-finalized-p class)
435     (comma-separated (swank-mop:class-precedence-list class)
436     (lambda (class)
437     `(:value ,class ,(princ-to-string (class-name class)))))
438     '("#<N/A (class not finalized)>"))
439     (:newline))))
440    
441 heller 1.11 (defmethod inspect-for-emacs ((slot cons) (inspector backend-inspector))
442 ewiborg 1.2 ;; Inspects slot definitions
443 heller 1.11 (declare (ignore inspector))
444 ewiborg 1.2 (if (eq (car slot) :name)
445     (values "A slot."
446     `("Name: " (:value ,(swank-mop:slot-definition-name slot))
447     (:newline)
448     ,@(when (swank-mop:slot-definition-documentation slot)
449     `("Documentation:" (:newline)
450     (:value ,(swank-mop:slot-definition-documentation slot))
451     (:newline)))
452     "Init args: " (:value ,(swank-mop:slot-definition-initargs slot)) (:newline)
453     "Init form: " ,(if (swank-mop:slot-definition-initfunction slot)
454     `(:value ,(swank-mop:slot-definition-initform slot))
455     "#<unspecified>") (:newline)
456     "Init function: " (:value ,(swank-mop:slot-definition-initfunction slot))
457     (:newline)))
458     (call-next-method)))
459    
460 heller 1.8 (defmethod inspect-for-emacs ((pathname pathnames::pathname-internal)
461 ewiborg 1.2 inspector)
462     (declare (ignore inspector))
463     (values (if (wild-pathname-p pathname)
464     "A wild pathname."
465     "A pathname.")
466     (append (label-value-line*
467     ("Namestring" (namestring pathname))
468     ("Host" (pathname-host pathname))
469     ("Device" (pathname-device pathname))
470     ("Directory" (pathname-directory pathname))
471     ("Name" (pathname-name pathname))
472     ("Type" (pathname-type pathname))
473     ("Version" (pathname-version pathname)))
474     (unless (or (wild-pathname-p pathname)
475     (not (probe-file pathname)))
476     (label-value-line "Truename" (truename pathname))))))
477    
478 heller 1.11 (defmethod inspect-for-emacs ((o t) (inspector backend-inspector))
479 heller 1.3 (cond ((cl::structurep o) (inspect-structure o))
480     (t (call-next-method))))
481    
482     (defun inspect-structure (o)
483     (values
484     (format nil "~A is a structure" o)
485     (let* ((template (cl::uref o 1))
486     (num-slots (cl::struct-template-num-slots template)))
487     (cond ((symbolp template)
488     (loop for i below num-slots
489     append (label-value-line i (cl::uref o (+ 2 i)))))
490     (t
491     (loop for i below num-slots
492     append (label-value-line (elt template (+ 6 (* i 5)))
493     (cl::uref o (+ 2 i)))))))))
494    
495    
496     ;;; Threads
497    
498     (require 'threads)
499    
500     (defstruct (mailbox (:conc-name mailbox.))
501     thread
502     (lock (make-instance 'threads:critical-section))
503     (queue '() :type list))
504    
505     (defvar *mailbox-lock* (make-instance 'threads:critical-section))
506     (defvar *mailboxes* (list))
507    
508     (defmacro with-lock (lock &body body)
509     `(threads:with-synchronization (threads:cs ,lock)
510     ,@body))
511    
512     (defimplementation spawn (fun &key name)
513     (declare (ignore name))
514 heller 1.4 (th:create-thread
515 heller 1.3 (lambda ()
516 heller 1.4 (handler-bind ((serious-condition #'invoke-debugger))
517     (unwind-protect (funcall fun)
518     (with-lock *mailbox-lock*
519     (setq *mailboxes* (remove cormanlisp:*current-thread-id*
520     *mailboxes* :key #'mailbox.thread))))))))
521 heller 1.3
522 heller 1.4 (defimplementation thread-id (thread)
523 heller 1.3 thread)
524    
525     (defimplementation find-thread (thread)
526     (if (thread-alive-p thread)
527     thread))
528    
529 heller 1.4 (defimplementation thread-alive-p (thread)
530     (if (threads:thread-handle thread) t nil))
531    
532 heller 1.3 (defimplementation current-thread ()
533     cormanlisp:*current-thread-id*)
534    
535     ;; XXX implement it
536     (defimplementation all-threads ()
537     '())
538    
539     ;; XXX something here is broken
540     (defimplementation kill-thread (thread)
541     (threads:terminate-thread thread 'killed))
542    
543     (defun mailbox (thread)
544     (with-lock *mailbox-lock*
545     (or (find thread *mailboxes* :key #'mailbox.thread)
546     (let ((mb (make-mailbox :thread thread)))
547     (push mb *mailboxes*)
548     mb))))
549    
550     (defimplementation send (thread message)
551     (let ((mbox (mailbox thread)))
552     (with-lock (mailbox.lock mbox)
553     (setf (mailbox.queue mbox)
554     (nconc (mailbox.queue mbox) (list message))))))
555    
556     (defimplementation receive ()
557     (let ((mbox (mailbox cormanlisp:*current-thread-id*)))
558     (loop
559     (with-lock (mailbox.lock mbox)
560     (when (mailbox.queue mbox)
561     (return (pop (mailbox.queue mbox)))))
562     (sleep 0.1))))
563    
564    
565 ewiborg 1.2 ;;; This is probably not good, but it WFM
566     (in-package :common-lisp)
567    
568     (defvar *old-documentation* #'documentation)
569     (defun documentation (thing &optional (type 'function))
570     (if (symbolp thing)
571     (funcall *old-documentation* thing type)
572     (values)))
573    
574     (defmethod print-object ((restart restart) stream)
575     (if (or *print-escape*
576     *print-readably*)
577     (print-unreadable-object (restart stream :type t :identity t)
578     (princ (restart-name restart) stream))
579     (when (functionp (restart-report-function restart))
580     (funcall (restart-report-function restart) stream))))

  ViewVC Help
Powered by ViewVC 1.1.5