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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (hide annotations)
Wed Dec 15 22:35:20 2004 UTC (9 years, 4 months ago) by heller
Branch: MAIN
Changes since 1.44: +4 -4 lines
(*buffer-name* *buffer-offset*): Move definitions upward before the
first use.
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 mbaringer 1.36 ;;;; if this listp has the complete CLOS then we use it, othewise we
36     ;;;; build up a "fake" swank-mop and then overide the methods in the
37     ;;;; 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 mbaringer 1.37 "True in those CLISP imagse which have a complete MOP implementation."))
46    
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.44 #+unix
84     (defmethod getpid ()
85     (system::program-id))
86 heller 1.39 #+win32
87     (defmethod getpid ()
88     (cond ((find-package :win32)
89     (funcall (find-symbol "GetCurrentProcessId" :win32)))
90     (t
91     (system::getenv "PID"))))
92    
93 vsedach 1.3 ;; the above is likely broken; we need windows NT users!
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     (labels ((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     (when (fboundp symbol)
149     (if (macro-function symbol)
150     (setf (getf result :macro) (doc 'function))
151 heller 1.25 (setf (getf result :function) (doc 'function))))
152 heller 1.1 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
153     (maybe-push :class (when (find-class symbol nil)
154     (doc 'type))) ;this should be fixed
155     result)))
156    
157 heller 1.25 (defimplementation describe-definition (symbol namespace)
158     (ecase namespace
159     (:variable (describe symbol))
160     (:macro (describe (macro-function symbol)))
161     (:function (describe (symbol-function symbol)))
162     (:class (describe (find-class symbol)))))
163    
164 heller 1.26 (defun fspec-pathname (symbol)
165 heller 1.1 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
166     (if (and path
167     (member (pathname-type path)
168     custom:*compiled-file-types* :test #'string=))
169 heller 1.26 (loop for suffix in custom:*source-file-types*
170     thereis (make-pathname :defaults path :type suffix))
171 heller 1.1 path)))
172    
173 heller 1.26 (defun fspec-location (fspec)
174     (let ((file (fspec-pathname fspec)))
175     (cond (file
176     (multiple-value-bind (truename c) (ignore-errors (truename file))
177     (cond (truename
178     (make-location (list :file (namestring truename))
179     (list :function-name (string fspec))))
180     (t (list :error (princ-to-string c))))))
181     (t (list :error (format nil "No source information available for: ~S"
182     fspec))))))
183 heller 1.25
184     (defimplementation find-definitions (name)
185 heller 1.26 (list (list name (fspec-location name))))
186 heller 1.1
187     (defvar *sldb-topframe*)
188     (defvar *sldb-botframe*)
189     (defvar *sldb-source*)
190     (defvar *sldb-debugmode* 4)
191    
192 heller 1.30 (defun frame-down (frame)
193     (sys::frame-down-1 frame sys::*debug-mode*))
194    
195     (defun frame-up (frame)
196     (sys::frame-up-1 frame sys::*debug-mode*))
197    
198 wjenkner 1.14 (defimplementation call-with-debugging-environment (debugger-loop-fn)
199 heller 1.1 (let* ((sys::*break-count* (1+ sys::*break-count*))
200     (sys::*driver* debugger-loop-fn)
201     (sys::*fasoutput-stream* nil)
202     (sys::*frame-limit1* (sys::frame-limit1 0))
203 heller 1.30 (sys::*frame-limit2* (sys::frame-limit2))
204 heller 1.1 (sys::*debug-mode* *sldb-debugmode*)
205 heller 1.30 (*sldb-topframe* sys::*frame-limit1*))
206 heller 1.1 (funcall debugger-loop-fn)))
207    
208     (defun nth-frame (index)
209 heller 1.30 (loop for frame = *sldb-topframe* then (frame-up frame)
210 heller 1.1 repeat index
211 heller 1.15 finally (return frame)))
212 heller 1.1
213 heller 1.25 (defimplementation compute-backtrace (start end)
214 heller 1.1 (let ((end (or end most-positive-fixnum)))
215 heller 1.30 (loop for last = nil then frame
216     for frame = (nth-frame start) then (frame-up frame)
217 heller 1.1 for i from start below end
218 heller 1.30 until (or (eq frame last) (system::driver-frame-p frame))
219     collect frame)))
220 heller 1.1
221 heller 1.25 (defimplementation print-frame (frame stream)
222     (write-string (string-left-trim '(#\Newline)
223     (with-output-to-string (stream)
224     (sys::describe-frame stream frame)))
225     stream))
226 heller 1.1
227 wjenkner 1.14 (defimplementation eval-in-frame (form frame-number)
228 heller 1.1 (sys::eval-at (nth-frame frame-number) form))
229    
230 wjenkner 1.14 (defimplementation frame-locals (frame-number)
231 heller 1.1 (let* ((frame (nth-frame frame-number))
232     (frame-env (sys::eval-at frame '(sys::the-environment))))
233     (append
234     (frame-do-venv frame (svref frame-env 0))
235     (frame-do-fenv frame (svref frame-env 1))
236     (frame-do-benv frame (svref frame-env 2))
237     (frame-do-genv frame (svref frame-env 3))
238     (frame-do-denv frame (svref frame-env 4)))))
239 heller 1.33
240     (defimplementation frame-var-value (frame var)
241     (getf (nth var (frame-locals frame)) :value))
242 heller 1.1
243     ;; Interpreter-Variablen-Environment has the shape
244     ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
245    
246     (defun frame-do-venv (frame venv)
247 heller 1.2 (loop for i from 1 below (length venv) by 2
248     as symbol = (svref venv (1- i))
249     and value = (svref venv i)
250 mbaringer 1.24 collect (list :name symbol :id 0
251     :value (if (eq sys::specdecl value)
252     ;; special variable
253     (sys::eval-at frame symbol)
254     ;; lexical variable or symbol macro
255     value))))
256 heller 1.1
257     (defun frame-do-fenv (frame fenv)
258     (declare (ignore frame fenv))
259     nil)
260    
261     (defun frame-do-benv (frame benv)
262     (declare (ignore frame benv))
263     nil)
264    
265     (defun frame-do-genv (frame genv)
266     (declare (ignore frame genv))
267     nil)
268    
269     (defun frame-do-denv (frame denv)
270     (declare (ignore frame denv))
271     nil)
272    
273 wjenkner 1.14 (defimplementation frame-catch-tags (index)
274 heller 1.1 (declare (ignore index))
275     nil)
276    
277 wjenkner 1.14 (defimplementation return-from-frame (index form)
278 heller 1.25 (sys::return-from-eval-frame (nth-frame index) form))
279 wjenkner 1.14
280     (defimplementation restart-frame (index)
281     (sys::redo-eval-frame (nth-frame index)))
282    
283     (defimplementation frame-source-location-for-emacs (index)
284 heller 1.39 (let ((f (nth-frame index)))
285     (list :error (format nil "Cannot find source for frame: ~A ~A ~A"
286     f
287     (sys::eval-frame-p f)
288     (sys::the-frame)))))
289 heller 1.1
290 wjenkner 1.16 ;;; Profiling
291    
292     (defimplementation profile (fname)
293     (eval `(mon:monitor ,fname))) ;monitor is a macro
294    
295     (defimplementation profiled-functions ()
296     mon:*monitored-functions*)
297    
298     (defimplementation unprofile (fname)
299     (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
300    
301     (defimplementation unprofile-all ()
302     (mon:unmonitor))
303    
304     (defimplementation profile-report ()
305     (mon:report-monitoring))
306    
307     (defimplementation profile-reset ()
308     (mon:reset-all-monitoring))
309    
310     (defimplementation profile-package (package callers-p methods)
311     (declare (ignore callers-p methods))
312     (mon:monitor-all package))
313 heller 1.1
314     ;;; Handle compiler conditions (find out location of error etc.)
315    
316     (defmacro compile-file-frobbing-notes ((&rest args) &body body)
317     "Pass ARGS to COMPILE-FILE, send the compiler notes to
318     *STANDARD-INPUT* and frob them in BODY."
319     `(let ((*error-output* (make-string-output-stream))
320     (*compile-verbose* t))
321     (multiple-value-prog1
322 vsedach 1.6 (compile-file ,@args)
323     (handler-case
324 heller 1.1 (with-input-from-string
325 vsedach 1.6 (*standard-input* (get-output-stream-string *error-output*))
326     ,@body)
327     (sys::simple-end-of-file () nil)))))
328 heller 1.1
329 heller 1.25 (defvar *orig-c-warn* (symbol-function 'system::c-warn))
330     (defvar *orig-c-style-warn* (symbol-function 'system::c-style-warn))
331     (defvar *orig-c-error* (symbol-function 'system::c-error))
332     (defvar *orig-c-report-problems* (symbol-function 'system::c-report-problems))
333    
334     (defmacro dynamic-flet (names-functions &body body)
335     "(dynamic-flet ((NAME FUNCTION) ...) BODY ...)
336 heller 1.27 Execute BODY with NAME's funtion slot set to FUNCTION."
337 heller 1.25 `(ext:letf* ,(loop for (name function) in names-functions
338     collect `((symbol-function ',name) ,function))
339     ,@body))
340 heller 1.45
341     (defvar *buffer-name* nil)
342     (defvar *buffer-offset*)
343    
344 heller 1.25 (defun compiler-note-location ()
345 heller 1.27 "Return the current compiler location."
346 heller 1.25 (let ((lineno1 sys::*compile-file-lineno1*)
347     (lineno2 sys::*compile-file-lineno2*)
348     (file sys::*compile-file-truename*))
349     (cond ((and file lineno1 lineno2)
350 heller 1.31 (make-location (list ':file (namestring file))
351     (list ':line lineno1)))
352 heller 1.25 (*buffer-name*
353 heller 1.31 (make-location (list ':buffer *buffer-name*)
354     (list ':position *buffer-offset*)))
355 heller 1.25 (t
356     (list :error "No error location available")))))
357    
358     (defun signal-compiler-warning (cstring args severity orig-fn)
359     (signal (make-condition 'compiler-condition
360     :severity severity
361     :message (apply #'format nil cstring args)
362     :location (compiler-note-location)))
363     (apply orig-fn cstring args))
364    
365     (defun c-warn (cstring &rest args)
366     (signal-compiler-warning cstring args :warning *orig-c-warn*))
367    
368     (defun c-style-warn (cstring &rest args)
369     (dynamic-flet ((sys::c-warn *orig-c-warn*))
370     (signal-compiler-warning cstring args :style-warning *orig-c-style-warn*)))
371    
372     (defun c-error (cstring &rest args)
373     (signal-compiler-warning cstring args :error *orig-c-error*))
374    
375 wjenkner 1.14 (defimplementation call-with-compilation-hooks (function)
376 heller 1.25 (handler-bind ((warning #'handle-notification-condition))
377     (dynamic-flet ((system::c-warn #'c-warn)
378     (system::c-style-warn #'c-style-warn)
379     (system::c-error #'c-error))
380     (funcall function))))
381 heller 1.1
382     (defun handle-notification-condition (condition)
383     "Handle a condition caused by a compiler warning."
384 heller 1.25 (signal (make-condition 'compiler-condition
385     :original-condition condition
386     :severity :warning
387     :message (princ-to-string condition)
388     :location (compiler-note-location))))
389 heller 1.1
390 heller 1.25 (defimplementation swank-compile-file (filename load-p)
391 heller 1.1 (with-compilation-hooks ()
392 heller 1.25 (with-compilation-unit ()
393     (let ((fasl-file (compile-file filename)))
394     (when (and load-p fasl-file)
395     (load fasl-file))
396     nil))))
397 heller 1.1
398 pseibel 1.34 (defimplementation swank-compile-string (string &key buffer position directory)
399     (declare (ignore directory))
400 heller 1.1 (with-compilation-hooks ()
401 heller 1.25 (let ((*buffer-name* buffer)
402 heller 1.1 (*buffer-offset* position))
403 heller 1.25 (funcall (compile nil (read-from-string
404 heller 1.27 (format nil "(~S () ~A)" 'lambda string)))))))
405 heller 1.1
406     ;;; Portable XREF from the CMU AI repository.
407    
408 lgorrie 1.32 (setq pxref::*handle-package-forms* '(cl:in-package))
409 heller 1.1
410 heller 1.25 (defmacro defxref (name function)
411     `(defimplementation ,name (name)
412     (xref-results (,function name))))
413    
414 lgorrie 1.32 (defxref who-calls pxref:list-callers)
415     (defxref who-references pxref:list-readers)
416     (defxref who-binds pxref:list-setters)
417     (defxref who-sets pxref:list-setters)
418     (defxref list-callers pxref:list-callers)
419     (defxref list-callees pxref:list-callees)
420 heller 1.1
421 heller 1.28 (defun xref-results (symbols)
422 heller 1.1 (let ((xrefs '()))
423 heller 1.28 (dolist (symbol symbols)
424     (push (list symbol (fspec-location symbol)) xrefs))
425 heller 1.25 xrefs))
426 heller 1.1
427     (when (find-package :swank-loader)
428     (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
429     (lambda ()
430     (let ((home (user-homedir-pathname)))
431     (and (ext:probe-directory home)
432     (probe-file (format nil "~A/.swank.lisp"
433     (namestring (truename home)))))))))
434    
435     ;; Don't set *debugger-hook* to nil on break.
436     (ext:without-package-lock ()
437     (defun break (&optional (format-string "Break") &rest args)
438     (if (not sys::*use-clcs*)
439     (progn
440     (terpri *error-output*)
441     (apply #'format *error-output*
442     (concatenate 'string "*** - " format-string)
443     args)
444     (funcall ext:*break-driver* t))
445     (let ((condition
446     (make-condition 'simple-condition
447     :format-control format-string
448     :format-arguments args))
449     ;;(*debugger-hook* nil)
450     ;; Issue 91
451     )
452     (ext:with-restarts
453 heller 1.28 ((continue
454 heller 1.1 :report (lambda (stream)
455 heller 1.28 (format stream (sys::text "Return from ~S loop")
456 heller 1.1 'break))
457     ()))
458 heller 1.28 (with-condition-restarts condition (list (find-restart 'continue))
459 heller 1.1 (invoke-debugger condition)))))
460     nil))
461 heller 1.40
462 heller 1.23 ;;; Inspecting
463    
464 mbaringer 1.35 (defclass clisp-inspector (inspector)
465     ())
466    
467     (defimplementation make-default-inspector ()
468     (make-instance 'clisp-inspector))
469    
470     (defmethod inspect-for-emacs ((o t) (inspector clisp-inspector))
471     (declare (ignore inspector))
472 heller 1.23 (let* ((*print-array* nil) (*print-pretty* t)
473     (*print-circle* t) (*print-escape* t)
474     (*print-lines* custom:*inspect-print-lines*)
475     (*print-level* custom:*inspect-print-level*)
476     (*print-length* custom:*inspect-print-length*)
477     (sys::*inspect-all* (make-array 10 :fill-pointer 0 :adjustable t))
478     (tmp-pack (make-package (gensym "INSPECT-TMP-PACKAGE-")))
479     (*package* tmp-pack)
480     (sys::*inspect-unbound-value* (intern "#<unbound>" tmp-pack)))
481     (let ((inspection (sys::inspect-backend o)))
482     (values (format nil "~S~% ~A~{~%~A~}" o
483     (sys::insp-title inspection)
484     (sys::insp-blurb inspection))
485 mbaringer 1.35 (loop with count = (sys::insp-num-slots inspection)
486     for i upto count
487 heller 1.44 for (value name) = (multiple-value-list
488     (funcall (sys::insp-nth-slot
489     inspection) i))
490     collect `((:value ,name) " = " (:value ,value)
491     (:newline)))))))
492 heller 1.1
493 mbaringer 1.29 (defimplementation quit-lisp ()
494 heller 1.39 #+lisp=cl (ext:quit)
495     #-lisp=cl (lisp:quit))
496 mbaringer 1.29
497 heller 1.1 ;;; Local Variables:
498     ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
499 heller 1.25 ;;; eval: (put 'dynamic-flet 'common-lisp-indent-function 1)
500 heller 1.1 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5