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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.51 - (hide annotations)
Tue Jul 5 20:30:59 2005 UTC (8 years, 9 months ago) by heller
Branch: MAIN
Changes since 1.50: +11 -16 lines
(swank-compile-file): New optional argument `external-format'.
1 heller 1.1 ;;;; SWANK support for CLISP.
2    
3 vsedach 1.3 ;;;; Copyright (C) 2003, 2004 W. Jenkner, V. Sedach
4 heller 1.1
5     ;;;; swank-clisp.lisp is free software; you can redistribute it and/or
6     ;;;; modify it under the terms of the GNU General Public License as
7     ;;;; published by the Free Software Foundation; either version 2, or
8     ;;;; (at your option) any later version.
9    
10     ;;; This is work in progress, but it's already usable. Many things
11     ;;; are adapted from other swank-*.lisp, in particular from
12     ;;; swank-allegro (I don't use allegro at all, but it's the shortest
13     ;;; one and I found Helmut Eller's code there enlightening).
14    
15 vsedach 1.3 ;;; This code is developed using the current CVS version of CLISP and
16     ;;; CLISP 2.32 on Linux. Older versions may not work (2.29 and below
17     ;;; are confirmed non-working; please upgrade). You need an image
18 wjenkner 1.16 ;;; containing the "SOCKET", "REGEXP", and "LINUX" packages. The
19     ;;; portable xref from the CMU AI repository and metering.lisp from
20 heller 1.20 ;;; CLOCC [1] are also required (alternatively, you have to manually
21     ;;; comment out some code below).
22     ;;;
23     ;;; [1] http://cvs.sourceforge.net/viewcvs.py/clocc/clocc/src/tools/metering/
24 heller 1.1
25 heller 1.25 (in-package :swank-backend)
26 heller 1.1
27     (eval-when (:compile-toplevel :load-toplevel :execute)
28 heller 1.33 ;;(use-package "SOCKET")
29 heller 1.1 (use-package "GRAY"))
30    
31 wjenkner 1.10 (eval-when (:compile-toplevel :execute)
32     (when (find-package "LINUX")
33     (pushnew :linux *features*)))
34 heller 1.1
35 heller 1.49 ;;;; if this lisp has the complete CLOS then we use it, otherwise we
36     ;;;; build up a "fake" swank-mop and then override the methods in the
37 mbaringer 1.36 ;;;; inspector.
38    
39     (eval-when (:compile-toplevel :load-toplevel :execute)
40     (defvar *have-mop*
41 mbaringer 1.37 (and (find-package :clos)
42     (eql :external
43 heller 1.44 (nth-value 1 (find-symbol (string ':standard-slot-definition)
44     :clos))))
45 heller 1.49 "True in those CLISP images which have a complete MOP implementation."))
46 mbaringer 1.37
47     #+#.(cl:if swank-backend::*have-mop* '(cl:and) '(cl:or))
48     (progn
49 heller 1.44 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
50 mbaringer 1.37
51     (defun swank-mop:slot-definition-documentation (slot)
52     (clos::slot-definition-documentation slot)))
53 mbaringer 1.35
54 mbaringer 1.37 #-#.(cl:if swank-backend::*have-mop* '(and) '(or))
55 mbaringer 1.36 (defclass swank-mop:standard-slot-definition ()
56     ()
57 heller 1.44 (:documentation
58     "Dummy class created so that swank.lisp will compile and load."))
59 mbaringer 1.36
60 vsedach 1.3 #+linux
61 wjenkner 1.10 (defmacro with-blocked-signals ((&rest signals) &body body)
62     (ext:with-gensyms ("SIGPROCMASK" ret mask)
63     `(multiple-value-bind (,ret ,mask)
64     (linux:sigprocmask-set-n-save
65     ,linux:SIG_BLOCK
66     ,(do ((sigset (linux:sigset-empty)
67     (linux:sigset-add sigset (the fixnum (pop signals)))))
68     ((null signals) sigset)))
69     (linux:check-res ,ret 'linux:sigprocmask-set-n-save)
70     (unwind-protect
71     (progn ,@body)
72     (linux:sigprocmask-set ,linux:SIG_SETMASK ,mask nil)))))
73 heller 1.25
74     ;; XXX currently only works in CVS version. 2.32 breaks.
75 heller 1.15 ;; #+linux
76 heller 1.27 ;; (defimplementation call-without-interrupts (fn)
77 heller 1.15 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
78     ;;
79     ;; #-linux
80 heller 1.27 (defimplementation call-without-interrupts (fn)
81 heller 1.12 (funcall fn))
82 vsedach 1.3
83 heller 1.49 (let ((getpid (or (find-symbol "PROCESS-ID" :system)
84     ;; old name prior to 2005-03-01, clisp <= 2.33.2
85     (find-symbol "PROGRAM-ID" :system)
86     #+win32 ; integrated into the above since 2005-02-24
87     (and (find-package :win32) ; optional modules/win32
88     (find-symbol "GetCurrentProcessId" :win32)))))
89     (defimplementation getpid () ; a required interface
90     (cond
91     (getpid (funcall getpid))
92     #+win32 ((ext:getenv "PID")) ; where does that come from?
93     (t -1))))
94 heller 1.1
95 heller 1.21 (defimplementation lisp-implementation-type-name ()
96     "clisp")
97    
98 heller 1.28 (defimplementation set-default-directory (directory)
99     (setf (ext:default-directory) directory)
100     (namestring (setf *default-pathname-defaults* (ext:default-directory))))
101    
102 wjenkner 1.4
103 heller 1.1 ;;; TCP Server
104    
105 heller 1.17 (defimplementation create-socket (host port)
106     (declare (ignore host))
107 heller 1.8 (socket:socket-server port))
108 lgorrie 1.7
109 wjenkner 1.14 (defimplementation local-port (socket)
110 heller 1.8 (socket:socket-server-port socket))
111 vsedach 1.5
112 wjenkner 1.14 (defimplementation close-socket (socket)
113 heller 1.8 (socket:socket-server-close socket))
114 vsedach 1.5
115 heller 1.42 (defun find-encoding (external-format)
116     (ecase external-format
117     (:iso-latin-1-unix (ext:make-encoding :charset 'charset:iso-8859-1
118     :line-terminator :unix))
119     (:utf-8-unix (ext:make-encoding :charset 'charset:utf-8
120     :line-terminator :unix))))
121    
122 heller 1.43 (defimplementation accept-connection (socket
123     &key (external-format :iso-latin-1-unix))
124 heller 1.8 (socket:socket-accept socket
125     :buffered nil ;; XXX should be t
126     :element-type 'character
127 heller 1.42 :external-format (find-encoding external-format)))
128 vsedach 1.3
129     ;;; Swank functions
130 heller 1.1
131 heller 1.25 (defimplementation arglist (fname)
132 heller 1.30 (block nil
133     (or (ignore-errors (return (ext:arglist fname)))
134     :not-available)))
135 heller 1.1
136 wjenkner 1.14 (defimplementation macroexpand-all (form)
137 heller 1.1 (ext:expand-form form))
138    
139 wjenkner 1.14 (defimplementation describe-symbol-for-emacs (symbol)
140 heller 1.1 "Return a plist describing SYMBOL.
141     Return NIL if the symbol is unbound."
142     (let ((result ()))
143 heller 1.47 (flet ((doc (kind)
144     (or (documentation symbol kind) :not-documented))
145     (maybe-push (property value)
146     (when value
147     (setf result (list* property value result)))))
148     (maybe-push :variable (when (boundp symbol) (doc 'variable)))
149 heller 1.1 (when (fboundp symbol)
150 heller 1.47 (maybe-push
151     ;; Report WHEN etc. as macros, even though they may be
152     ;; implemented as special operators.
153     (if (macro-function symbol) :macro
154     (typecase (fdefinition symbol)
155     (generic-function :generic-function)
156     (function :function)
157     ;; (type-of 'progn) -> ext:special-operator
158     (t :special-operator)))
159     (doc 'function)))
160 heller 1.50 (when (or (get symbol 'system::setf-function) ; e.g. #'(setf elt)
161     (get symbol 'system::setf-expander)); defsetf
162     (maybe-push :setf (doc 'setf)))
163     (when (or (get symbol 'system::type-symbol); cf. clisp/src/describe.lisp
164     (get symbol 'system::defstruct-description)
165     (get symbol 'system::deftype-expander))
166     (maybe-push :type (doc 'type))) ; even for 'structure
167     (when (find-class symbol nil)
168     (maybe-push :class (doc 'type)))
169 heller 1.48 ;; Let this code work compiled in images without FFI
170     (let ((types (load-time-value
171     (and (find-package "FFI")
172     (symbol-value
173     (find-symbol "*C-TYPE-TABLE*" "FFI"))))))
174     ;; Use ffi::*c-type-table* so as not to suffer the overhead of
175     ;; (ignore-errors (ffi:parse-c-type symbol)) for 99.9% of symbols
176     ;; which are not FFI type names.
177     (when (and types (nth-value 1 (gethash symbol types)))
178     ;; Maybe use (case (head (ffi:deparse-c-type)))
179     ;; to distinguish struct and union types?
180     (maybe-push :alien-type :not-documented)))
181 heller 1.1 result)))
182    
183 heller 1.25 (defimplementation describe-definition (symbol namespace)
184     (ecase namespace
185     (:variable (describe symbol))
186     (:macro (describe (macro-function symbol)))
187     (:function (describe (symbol-function symbol)))
188     (:class (describe (find-class symbol)))))
189    
190 heller 1.26 (defun fspec-pathname (symbol)
191 heller 1.1 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
192     (if (and path
193     (member (pathname-type path)
194     custom:*compiled-file-types* :test #'string=))
195 heller 1.26 (loop for suffix in custom:*source-file-types*
196     thereis (make-pathname :defaults path :type suffix))
197 heller 1.1 path)))
198    
199 heller 1.26 (defun fspec-location (fspec)
200     (let ((file (fspec-pathname fspec)))
201     (cond (file
202     (multiple-value-bind (truename c) (ignore-errors (truename file))
203     (cond (truename
204     (make-location (list :file (namestring truename))
205     (list :function-name (string fspec))))
206     (t (list :error (princ-to-string c))))))
207     (t (list :error (format nil "No source information available for: ~S"
208     fspec))))))
209 heller 1.25
210     (defimplementation find-definitions (name)
211 heller 1.26 (list (list name (fspec-location name))))
212 heller 1.1
213     (defvar *sldb-topframe*)
214     (defvar *sldb-botframe*)
215     (defvar *sldb-source*)
216     (defvar *sldb-debugmode* 4)
217    
218 heller 1.30 (defun frame-down (frame)
219     (sys::frame-down-1 frame sys::*debug-mode*))
220    
221     (defun frame-up (frame)
222     (sys::frame-up-1 frame sys::*debug-mode*))
223    
224 wjenkner 1.14 (defimplementation call-with-debugging-environment (debugger-loop-fn)
225 heller 1.1 (let* ((sys::*break-count* (1+ sys::*break-count*))
226     (sys::*driver* debugger-loop-fn)
227     (sys::*fasoutput-stream* nil)
228     (sys::*frame-limit1* (sys::frame-limit1 0))
229 heller 1.30 (sys::*frame-limit2* (sys::frame-limit2))
230 heller 1.1 (sys::*debug-mode* *sldb-debugmode*)
231 heller 1.30 (*sldb-topframe* sys::*frame-limit1*))
232 heller 1.1 (funcall debugger-loop-fn)))
233    
234     (defun nth-frame (index)
235 heller 1.30 (loop for frame = *sldb-topframe* then (frame-up frame)
236 heller 1.1 repeat index
237 heller 1.15 finally (return frame)))
238 heller 1.1
239 heller 1.25 (defimplementation compute-backtrace (start end)
240 heller 1.1 (let ((end (or end most-positive-fixnum)))
241 heller 1.30 (loop for last = nil then frame
242     for frame = (nth-frame start) then (frame-up frame)
243 heller 1.1 for i from start below end
244 heller 1.30 until (or (eq frame last) (system::driver-frame-p frame))
245     collect frame)))
246 heller 1.1
247 heller 1.25 (defimplementation print-frame (frame stream)
248     (write-string (string-left-trim '(#\Newline)
249     (with-output-to-string (stream)
250     (sys::describe-frame stream frame)))
251     stream))
252 heller 1.1
253 wjenkner 1.14 (defimplementation eval-in-frame (form frame-number)
254 heller 1.1 (sys::eval-at (nth-frame frame-number) form))
255    
256 wjenkner 1.14 (defimplementation frame-locals (frame-number)
257 heller 1.1 (let* ((frame (nth-frame frame-number))
258     (frame-env (sys::eval-at frame '(sys::the-environment))))
259     (append
260     (frame-do-venv frame (svref frame-env 0))
261     (frame-do-fenv frame (svref frame-env 1))
262     (frame-do-benv frame (svref frame-env 2))
263     (frame-do-genv frame (svref frame-env 3))
264     (frame-do-denv frame (svref frame-env 4)))))
265 heller 1.33
266     (defimplementation frame-var-value (frame var)
267     (getf (nth var (frame-locals frame)) :value))
268 heller 1.1
269     ;; Interpreter-Variablen-Environment has the shape
270     ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
271    
272     (defun frame-do-venv (frame venv)
273 heller 1.2 (loop for i from 1 below (length venv) by 2
274     as symbol = (svref venv (1- i))
275     and value = (svref venv i)
276 mbaringer 1.24 collect (list :name symbol :id 0
277     :value (if (eq sys::specdecl value)
278     ;; special variable
279     (sys::eval-at frame symbol)
280     ;; lexical variable or symbol macro
281     value))))
282 heller 1.1
283     (defun frame-do-fenv (frame fenv)
284     (declare (ignore frame fenv))
285     nil)
286    
287     (defun frame-do-benv (frame benv)
288     (declare (ignore frame benv))
289     nil)
290    
291     (defun frame-do-genv (frame genv)
292     (declare (ignore frame genv))
293     nil)
294    
295     (defun frame-do-denv (frame denv)
296     (declare (ignore frame denv))
297     nil)
298    
299 wjenkner 1.14 (defimplementation frame-catch-tags (index)
300 heller 1.1 (declare (ignore index))
301     nil)
302    
303 wjenkner 1.14 (defimplementation return-from-frame (index form)
304 heller 1.25 (sys::return-from-eval-frame (nth-frame index) form))
305 wjenkner 1.14
306     (defimplementation restart-frame (index)
307     (sys::redo-eval-frame (nth-frame index)))
308    
309     (defimplementation frame-source-location-for-emacs (index)
310 heller 1.39 (let ((f (nth-frame index)))
311     (list :error (format nil "Cannot find source for frame: ~A ~A ~A"
312     f
313     (sys::eval-frame-p f)
314     (sys::the-frame)))))
315 heller 1.1
316 wjenkner 1.16 ;;; Profiling
317    
318     (defimplementation profile (fname)
319     (eval `(mon:monitor ,fname))) ;monitor is a macro
320    
321     (defimplementation profiled-functions ()
322     mon:*monitored-functions*)
323    
324     (defimplementation unprofile (fname)
325     (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
326    
327     (defimplementation unprofile-all ()
328     (mon:unmonitor))
329    
330     (defimplementation profile-report ()
331     (mon:report-monitoring))
332    
333     (defimplementation profile-reset ()
334     (mon:reset-all-monitoring))
335    
336     (defimplementation profile-package (package callers-p methods)
337     (declare (ignore callers-p methods))
338     (mon:monitor-all package))
339 heller 1.1
340     ;;; Handle compiler conditions (find out location of error etc.)
341    
342     (defmacro compile-file-frobbing-notes ((&rest args) &body body)
343     "Pass ARGS to COMPILE-FILE, send the compiler notes to
344     *STANDARD-INPUT* and frob them in BODY."
345     `(let ((*error-output* (make-string-output-stream))
346     (*compile-verbose* t))
347     (multiple-value-prog1
348 vsedach 1.6 (compile-file ,@args)
349     (handler-case
350 heller 1.1 (with-input-from-string
351 vsedach 1.6 (*standard-input* (get-output-stream-string *error-output*))
352     ,@body)
353     (sys::simple-end-of-file () nil)))))
354 heller 1.1
355 heller 1.25 (defvar *orig-c-warn* (symbol-function 'system::c-warn))
356     (defvar *orig-c-style-warn* (symbol-function 'system::c-style-warn))
357     (defvar *orig-c-error* (symbol-function 'system::c-error))
358     (defvar *orig-c-report-problems* (symbol-function 'system::c-report-problems))
359    
360     (defmacro dynamic-flet (names-functions &body body)
361     "(dynamic-flet ((NAME FUNCTION) ...) BODY ...)
362 heller 1.46 Execute BODY with NAME's function slot set to FUNCTION."
363 heller 1.25 `(ext:letf* ,(loop for (name function) in names-functions
364     collect `((symbol-function ',name) ,function))
365     ,@body))
366 heller 1.45
367     (defvar *buffer-name* nil)
368     (defvar *buffer-offset*)
369    
370 heller 1.25 (defun compiler-note-location ()
371 heller 1.27 "Return the current compiler location."
372 heller 1.25 (let ((lineno1 sys::*compile-file-lineno1*)
373     (lineno2 sys::*compile-file-lineno2*)
374     (file sys::*compile-file-truename*))
375     (cond ((and file lineno1 lineno2)
376 heller 1.31 (make-location (list ':file (namestring file))
377     (list ':line lineno1)))
378 heller 1.25 (*buffer-name*
379 heller 1.31 (make-location (list ':buffer *buffer-name*)
380     (list ':position *buffer-offset*)))
381 heller 1.25 (t
382     (list :error "No error location available")))))
383    
384     (defun signal-compiler-warning (cstring args severity orig-fn)
385     (signal (make-condition 'compiler-condition
386     :severity severity
387     :message (apply #'format nil cstring args)
388     :location (compiler-note-location)))
389     (apply orig-fn cstring args))
390    
391     (defun c-warn (cstring &rest args)
392     (signal-compiler-warning cstring args :warning *orig-c-warn*))
393    
394     (defun c-style-warn (cstring &rest args)
395     (dynamic-flet ((sys::c-warn *orig-c-warn*))
396     (signal-compiler-warning cstring args :style-warning *orig-c-style-warn*)))
397    
398     (defun c-error (cstring &rest args)
399     (signal-compiler-warning cstring args :error *orig-c-error*))
400    
401 wjenkner 1.14 (defimplementation call-with-compilation-hooks (function)
402 heller 1.25 (handler-bind ((warning #'handle-notification-condition))
403     (dynamic-flet ((system::c-warn #'c-warn)
404     (system::c-style-warn #'c-style-warn)
405     (system::c-error #'c-error))
406     (funcall function))))
407 heller 1.1
408     (defun handle-notification-condition (condition)
409     "Handle a condition caused by a compiler warning."
410 heller 1.25 (signal (make-condition 'compiler-condition
411     :original-condition condition
412     :severity :warning
413     :message (princ-to-string condition)
414     :location (compiler-note-location))))
415 heller 1.1
416 heller 1.51 (defimplementation swank-compile-file (filename load-p
417     &optional external-format)
418     (let ((ef (if external-format
419     (find-encoding external-format)
420     :default)))
421     (with-compilation-hooks ()
422     (with-compilation-unit ()
423     (let ((fasl-file (compile-file filename :external-format ef)))
424     (when (and load-p fasl-file)
425     (load fasl-file))
426     nil)))))
427 heller 1.1
428 pseibel 1.34 (defimplementation swank-compile-string (string &key buffer position directory)
429     (declare (ignore directory))
430 heller 1.1 (with-compilation-hooks ()
431 heller 1.25 (let ((*buffer-name* buffer)
432 heller 1.1 (*buffer-offset* position))
433 heller 1.25 (funcall (compile nil (read-from-string
434 heller 1.27 (format nil "(~S () ~A)" 'lambda string)))))))
435 heller 1.1
436     ;;; Portable XREF from the CMU AI repository.
437    
438 lgorrie 1.32 (setq pxref::*handle-package-forms* '(cl:in-package))
439 heller 1.1
440 heller 1.25 (defmacro defxref (name function)
441     `(defimplementation ,name (name)
442     (xref-results (,function name))))
443    
444 lgorrie 1.32 (defxref who-calls pxref:list-callers)
445     (defxref who-references pxref:list-readers)
446     (defxref who-binds pxref:list-setters)
447     (defxref who-sets pxref:list-setters)
448     (defxref list-callers pxref:list-callers)
449     (defxref list-callees pxref:list-callees)
450 heller 1.1
451 heller 1.28 (defun xref-results (symbols)
452 heller 1.1 (let ((xrefs '()))
453 heller 1.28 (dolist (symbol symbols)
454     (push (list symbol (fspec-location symbol)) xrefs))
455 heller 1.25 xrefs))
456 heller 1.1
457     (when (find-package :swank-loader)
458     (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
459     (lambda ()
460     (let ((home (user-homedir-pathname)))
461     (and (ext:probe-directory home)
462     (probe-file (format nil "~A/.swank.lisp"
463     (namestring (truename home)))))))))
464    
465     ;; Don't set *debugger-hook* to nil on break.
466     (ext:without-package-lock ()
467     (defun break (&optional (format-string "Break") &rest args)
468     (if (not sys::*use-clcs*)
469     (progn
470     (terpri *error-output*)
471     (apply #'format *error-output*
472     (concatenate 'string "*** - " format-string)
473     args)
474     (funcall ext:*break-driver* t))
475     (let ((condition
476     (make-condition 'simple-condition
477     :format-control format-string
478     :format-arguments args))
479     ;;(*debugger-hook* nil)
480     ;; Issue 91
481     )
482     (ext:with-restarts
483 heller 1.28 ((continue
484 heller 1.1 :report (lambda (stream)
485 heller 1.28 (format stream (sys::text "Return from ~S loop")
486 heller 1.1 'break))
487     ()))
488 heller 1.28 (with-condition-restarts condition (list (find-restart 'continue))
489 heller 1.1 (invoke-debugger condition)))))
490     nil))
491 heller 1.40
492 heller 1.23 ;;; Inspecting
493    
494 mbaringer 1.35 (defclass clisp-inspector (inspector)
495     ())
496    
497     (defimplementation make-default-inspector ()
498     (make-instance 'clisp-inspector))
499    
500     (defmethod inspect-for-emacs ((o t) (inspector clisp-inspector))
501     (declare (ignore inspector))
502 heller 1.23 (let* ((*print-array* nil) (*print-pretty* t)
503     (*print-circle* t) (*print-escape* t)
504     (*print-lines* custom:*inspect-print-lines*)
505     (*print-level* custom:*inspect-print-level*)
506     (*print-length* custom:*inspect-print-length*)
507     (sys::*inspect-all* (make-array 10 :fill-pointer 0 :adjustable t))
508     (tmp-pack (make-package (gensym "INSPECT-TMP-PACKAGE-")))
509     (*package* tmp-pack)
510     (sys::*inspect-unbound-value* (intern "#<unbound>" tmp-pack)))
511     (let ((inspection (sys::inspect-backend o)))
512     (values (format nil "~S~% ~A~{~%~A~}" o
513     (sys::insp-title inspection)
514     (sys::insp-blurb inspection))
515 mbaringer 1.35 (loop with count = (sys::insp-num-slots inspection)
516     for i upto count
517 heller 1.44 for (value name) = (multiple-value-list
518     (funcall (sys::insp-nth-slot
519     inspection) i))
520     collect `((:value ,name) " = " (:value ,value)
521     (:newline)))))))
522 heller 1.1
523 mbaringer 1.29 (defimplementation quit-lisp ()
524 heller 1.39 #+lisp=cl (ext:quit)
525     #-lisp=cl (lisp:quit))
526 mbaringer 1.29
527 heller 1.1 ;;; Local Variables:
528     ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
529 heller 1.25 ;;; eval: (put 'dynamic-flet 'common-lisp-indent-function 1)
530 heller 1.1 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5