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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.29 - (hide annotations)
Tue Apr 6 10:42:53 2004 UTC (10 years ago) by mbaringer
Branch: MAIN
CVS Tags: SLIME-0-12
Changes since 1.28: +3 -0 lines
See ChangeLog entry 2004-04-06  Marco Baringer
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     (use-package "SOCKET")
29     (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 vsedach 1.3 #+linux
36 wjenkner 1.10 (defmacro with-blocked-signals ((&rest signals) &body body)
37     (ext:with-gensyms ("SIGPROCMASK" ret mask)
38     `(multiple-value-bind (,ret ,mask)
39     (linux:sigprocmask-set-n-save
40     ,linux:SIG_BLOCK
41     ,(do ((sigset (linux:sigset-empty)
42     (linux:sigset-add sigset (the fixnum (pop signals)))))
43     ((null signals) sigset)))
44     (linux:check-res ,ret 'linux:sigprocmask-set-n-save)
45     (unwind-protect
46     (progn ,@body)
47     (linux:sigprocmask-set ,linux:SIG_SETMASK ,mask nil)))))
48 heller 1.25
49     ;; XXX currently only works in CVS version. 2.32 breaks.
50 heller 1.15 ;; #+linux
51 heller 1.27 ;; (defimplementation call-without-interrupts (fn)
52 heller 1.15 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
53     ;;
54     ;; #-linux
55 heller 1.27 (defimplementation call-without-interrupts (fn)
56 heller 1.12 (funcall fn))
57 vsedach 1.3
58 heller 1.12 #+unix (defmethod getpid () (system::program-id))
59     #+win32 (defmethod getpid () (or (system::getenv "PID") -1))
60 vsedach 1.3 ;; the above is likely broken; we need windows NT users!
61 heller 1.1
62 heller 1.21 (defimplementation lisp-implementation-type-name ()
63     "clisp")
64    
65 heller 1.28 (defimplementation set-default-directory (directory)
66     (setf (ext:default-directory) directory)
67     (namestring (setf *default-pathname-defaults* (ext:default-directory))))
68    
69 wjenkner 1.4
70 heller 1.1 ;;; TCP Server
71    
72 heller 1.17 (defimplementation create-socket (host port)
73     (declare (ignore host))
74 heller 1.8 (socket:socket-server port))
75 lgorrie 1.7
76 wjenkner 1.14 (defimplementation local-port (socket)
77 heller 1.8 (socket:socket-server-port socket))
78 vsedach 1.5
79 wjenkner 1.14 (defimplementation close-socket (socket)
80 heller 1.8 (socket:socket-server-close socket))
81 vsedach 1.5
82 wjenkner 1.14 (defimplementation accept-connection (socket)
83 heller 1.8 (socket:socket-accept socket
84     :buffered nil ;; XXX should be t
85     :element-type 'character
86     :external-format (ext:make-encoding
87     :charset 'charset:iso-8859-1
88     :line-terminator :unix)))
89 vsedach 1.3
90     ;;; Swank functions
91 heller 1.1
92 heller 1.25 (defimplementation arglist (fname)
93     (ext:arglist fname))
94 heller 1.1
95 wjenkner 1.14 (defimplementation macroexpand-all (form)
96 heller 1.1 (ext:expand-form form))
97    
98 wjenkner 1.14 (defimplementation describe-symbol-for-emacs (symbol)
99 heller 1.1 "Return a plist describing SYMBOL.
100     Return NIL if the symbol is unbound."
101     (let ((result ()))
102     (labels ((doc (kind)
103     (or (documentation symbol kind) :not-documented))
104     (maybe-push (property value)
105     (when value
106     (setf result (list* property value result)))))
107     (when (fboundp symbol)
108     (if (macro-function symbol)
109     (setf (getf result :macro) (doc 'function))
110 heller 1.25 (setf (getf result :function) (doc 'function))))
111 heller 1.1 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
112     (maybe-push :class (when (find-class symbol nil)
113     (doc 'type))) ;this should be fixed
114     result)))
115    
116 heller 1.25 (defimplementation describe-definition (symbol namespace)
117     (ecase namespace
118     (:variable (describe symbol))
119     (:macro (describe (macro-function symbol)))
120     (:function (describe (symbol-function symbol)))
121     (:class (describe (find-class symbol)))))
122    
123 heller 1.26 (defun fspec-pathname (symbol)
124 heller 1.1 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
125     (if (and path
126     (member (pathname-type path)
127     custom:*compiled-file-types* :test #'string=))
128 heller 1.26 (loop for suffix in custom:*source-file-types*
129     thereis (make-pathname :defaults path :type suffix))
130 heller 1.1 path)))
131    
132 heller 1.26 (defun fspec-location (fspec)
133     (let ((file (fspec-pathname fspec)))
134     (cond (file
135     (multiple-value-bind (truename c) (ignore-errors (truename file))
136     (cond (truename
137     (make-location (list :file (namestring truename))
138     (list :function-name (string fspec))))
139     (t (list :error (princ-to-string c))))))
140     (t (list :error (format nil "No source information available for: ~S"
141     fspec))))))
142 heller 1.25
143     (defimplementation find-definitions (name)
144 heller 1.26 (list (list name (fspec-location name))))
145 heller 1.1
146     (defvar *sldb-topframe*)
147     (defvar *sldb-botframe*)
148     (defvar *sldb-source*)
149     (defvar *sldb-debugmode* 4)
150    
151 wjenkner 1.14 (defimplementation call-with-debugging-environment (debugger-loop-fn)
152 heller 1.1 (let* ((sys::*break-count* (1+ sys::*break-count*))
153     (sys::*driver* debugger-loop-fn)
154     (sys::*fasoutput-stream* nil)
155     ;;; (sys::*frame-limit1* (sys::frame-limit1 43))
156     (sys::*frame-limit1* (sys::frame-limit1 0))
157     ;;; (sys::*frame-limit2* (sys::frame-limit2))
158     (sys::*debug-mode* *sldb-debugmode*)
159     (*sldb-topframe*
160     (sys::frame-down-1
161     (sys::frame-up-1 sys::*frame-limit1* sys::*debug-mode*)
162     sys::*debug-mode*))
163 heller 1.25 (*sldb-botframe* (sys::frame-up *sldb-topframe* sys::*debug-mode*)))
164 heller 1.1 (funcall debugger-loop-fn)))
165    
166     (defun nth-frame (index)
167     (loop for frame = *sldb-topframe* then (sys::frame-up-1 frame
168     sys::*debug-mode*)
169     repeat index
170     never (eq frame *sldb-botframe*)
171 heller 1.15 finally (return frame)))
172 heller 1.1
173 heller 1.25 (defimplementation compute-backtrace (start end)
174 heller 1.1 (let ((end (or end most-positive-fixnum)))
175     (loop for f = (nth-frame start)
176     then (sys::frame-up-1 f sys::*debug-mode*)
177     for i from start below end
178     until (eq f *sldb-botframe*)
179     collect f)))
180    
181 heller 1.25 (defimplementation print-frame (frame stream)
182     (write-string (string-left-trim '(#\Newline)
183     (with-output-to-string (stream)
184     (sys::describe-frame stream frame)))
185     stream))
186 heller 1.1
187 wjenkner 1.14 (defimplementation eval-in-frame (form frame-number)
188 heller 1.1 (sys::eval-at (nth-frame frame-number) form))
189    
190 wjenkner 1.14 (defimplementation frame-locals (frame-number)
191 heller 1.1 (let* ((frame (nth-frame frame-number))
192     (frame-env (sys::eval-at frame '(sys::the-environment))))
193     (append
194     (frame-do-venv frame (svref frame-env 0))
195     (frame-do-fenv frame (svref frame-env 1))
196     (frame-do-benv frame (svref frame-env 2))
197     (frame-do-genv frame (svref frame-env 3))
198     (frame-do-denv frame (svref frame-env 4)))))
199    
200     ;; Interpreter-Variablen-Environment has the shape
201     ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
202    
203     (defun frame-do-venv (frame venv)
204 heller 1.2 (loop for i from 1 below (length venv) by 2
205     as symbol = (svref venv (1- i))
206     and value = (svref venv i)
207 mbaringer 1.24 collect (list :name symbol :id 0
208     :value (if (eq sys::specdecl value)
209     ;; special variable
210     (sys::eval-at frame symbol)
211     ;; lexical variable or symbol macro
212     value))))
213 heller 1.1
214     (defun frame-do-fenv (frame fenv)
215     (declare (ignore frame fenv))
216     nil)
217    
218     (defun frame-do-benv (frame benv)
219     (declare (ignore frame benv))
220     nil)
221    
222     (defun frame-do-genv (frame genv)
223     (declare (ignore frame genv))
224     nil)
225    
226     (defun frame-do-denv (frame denv)
227     (declare (ignore frame denv))
228     nil)
229    
230 wjenkner 1.14 (defimplementation frame-catch-tags (index)
231 heller 1.1 (declare (ignore index))
232     nil)
233    
234 wjenkner 1.14 (defimplementation return-from-frame (index form)
235 heller 1.25 (sys::return-from-eval-frame (nth-frame index) form))
236 wjenkner 1.14
237     (defimplementation restart-frame (index)
238     (sys::redo-eval-frame (nth-frame index)))
239    
240     (defimplementation frame-source-location-for-emacs (index)
241 heller 1.1 (list :error (format nil "Cannot find source for frame: ~A"
242     (nth-frame index))))
243    
244 wjenkner 1.16 ;;; Profiling
245    
246     (defimplementation profile (fname)
247     (eval `(mon:monitor ,fname))) ;monitor is a macro
248    
249     (defimplementation profiled-functions ()
250     mon:*monitored-functions*)
251    
252     (defimplementation unprofile (fname)
253     (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
254    
255     (defimplementation unprofile-all ()
256     (mon:unmonitor))
257    
258     (defimplementation profile-report ()
259     (mon:report-monitoring))
260    
261     (defimplementation profile-reset ()
262     (mon:reset-all-monitoring))
263    
264     (defimplementation profile-package (package callers-p methods)
265     (declare (ignore callers-p methods))
266     (mon:monitor-all package))
267 heller 1.1
268     ;;; Handle compiler conditions (find out location of error etc.)
269    
270     (defmacro compile-file-frobbing-notes ((&rest args) &body body)
271     "Pass ARGS to COMPILE-FILE, send the compiler notes to
272     *STANDARD-INPUT* and frob them in BODY."
273     `(let ((*error-output* (make-string-output-stream))
274     (*compile-verbose* t))
275     (multiple-value-prog1
276 vsedach 1.6 (compile-file ,@args)
277     (handler-case
278 heller 1.1 (with-input-from-string
279 vsedach 1.6 (*standard-input* (get-output-stream-string *error-output*))
280     ,@body)
281     (sys::simple-end-of-file () nil)))))
282 heller 1.1
283 heller 1.25 (defvar *orig-c-warn* (symbol-function 'system::c-warn))
284     (defvar *orig-c-style-warn* (symbol-function 'system::c-style-warn))
285     (defvar *orig-c-error* (symbol-function 'system::c-error))
286     (defvar *orig-c-report-problems* (symbol-function 'system::c-report-problems))
287    
288     (defmacro dynamic-flet (names-functions &body body)
289     "(dynamic-flet ((NAME FUNCTION) ...) BODY ...)
290 heller 1.27 Execute BODY with NAME's funtion slot set to FUNCTION."
291 heller 1.25 `(ext:letf* ,(loop for (name function) in names-functions
292     collect `((symbol-function ',name) ,function))
293     ,@body))
294    
295     (defun compiler-note-location ()
296 heller 1.27 "Return the current compiler location."
297 heller 1.25 (let ((lineno1 sys::*compile-file-lineno1*)
298     (lineno2 sys::*compile-file-lineno2*)
299     (file sys::*compile-file-truename*))
300     (cond ((and file lineno1 lineno2)
301     `(:location (:file ,(namestring file)) (:line ,lineno1)))
302     (*buffer-name*
303     `(:location (:buffer ,*buffer-name*) (:position ,*buffer-offset*)))
304     (t
305     (list :error "No error location available")))))
306    
307     (defun signal-compiler-warning (cstring args severity orig-fn)
308     (signal (make-condition 'compiler-condition
309     :severity severity
310     :message (apply #'format nil cstring args)
311     :location (compiler-note-location)))
312     (apply orig-fn cstring args))
313    
314     (defun c-warn (cstring &rest args)
315     (signal-compiler-warning cstring args :warning *orig-c-warn*))
316    
317     (defun c-style-warn (cstring &rest args)
318     (dynamic-flet ((sys::c-warn *orig-c-warn*))
319     (signal-compiler-warning cstring args :style-warning *orig-c-style-warn*)))
320    
321     (defun c-error (cstring &rest args)
322     (signal-compiler-warning cstring args :error *orig-c-error*))
323    
324 wjenkner 1.14 (defimplementation call-with-compilation-hooks (function)
325 heller 1.25 (handler-bind ((warning #'handle-notification-condition))
326     (dynamic-flet ((system::c-warn #'c-warn)
327     (system::c-style-warn #'c-style-warn)
328     (system::c-error #'c-error))
329     (funcall function))))
330 heller 1.1
331     (defun handle-notification-condition (condition)
332     "Handle a condition caused by a compiler warning."
333 heller 1.25 (signal (make-condition 'compiler-condition
334     :original-condition condition
335     :severity :warning
336     :message (princ-to-string condition)
337     :location (compiler-note-location))))
338 heller 1.1
339     (defvar *buffer-name* nil)
340     (defvar *buffer-offset*)
341    
342 heller 1.25 (defimplementation swank-compile-file (filename load-p)
343 heller 1.1 (with-compilation-hooks ()
344 heller 1.25 (with-compilation-unit ()
345     (let ((fasl-file (compile-file filename)))
346     (when (and load-p fasl-file)
347     (load fasl-file))
348     nil))))
349 heller 1.1
350 heller 1.25 (defimplementation swank-compile-string (string &key buffer position)
351 heller 1.1 (with-compilation-hooks ()
352 heller 1.25 (let ((*buffer-name* buffer)
353 heller 1.1 (*buffer-offset* position))
354 heller 1.25 (funcall (compile nil (read-from-string
355 heller 1.27 (format nil "(~S () ~A)" 'lambda string)))))))
356 heller 1.1
357     ;;; Portable XREF from the CMU AI repository.
358    
359     (setq xref::*handle-package-forms* '(cl:in-package))
360    
361 heller 1.25 (defmacro defxref (name function)
362     `(defimplementation ,name (name)
363     (xref-results (,function name))))
364    
365     (defxref who-calls xref:list-callers)
366     (defxref who-references xref:list-readers)
367     (defxref who-binds xref:list-setters)
368     (defxref who-sets xref:list-setters)
369     (defxref list-callers xref:list-callers)
370     (defxref list-callees xref:list-callees)
371 heller 1.1
372 heller 1.28 (defun xref-results (symbols)
373 heller 1.1 (let ((xrefs '()))
374 heller 1.28 (dolist (symbol symbols)
375     (push (list symbol (fspec-location symbol)) xrefs))
376 heller 1.25 xrefs))
377 heller 1.1
378     (when (find-package :swank-loader)
379     (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
380     (lambda ()
381     (let ((home (user-homedir-pathname)))
382     (and (ext:probe-directory home)
383     (probe-file (format nil "~A/.swank.lisp"
384     (namestring (truename home)))))))))
385    
386     ;; Don't set *debugger-hook* to nil on break.
387     (ext:without-package-lock ()
388     (defun break (&optional (format-string "Break") &rest args)
389     (if (not sys::*use-clcs*)
390     (progn
391     (terpri *error-output*)
392     (apply #'format *error-output*
393     (concatenate 'string "*** - " format-string)
394     args)
395     (funcall ext:*break-driver* t))
396     (let ((condition
397     (make-condition 'simple-condition
398     :format-control format-string
399     :format-arguments args))
400     ;;(*debugger-hook* nil)
401     ;; Issue 91
402     )
403     (ext:with-restarts
404 heller 1.28 ((continue
405 heller 1.1 :report (lambda (stream)
406 heller 1.28 (format stream (sys::text "Return from ~S loop")
407 heller 1.1 'break))
408     ()))
409 heller 1.28 (with-condition-restarts condition (list (find-restart 'continue))
410 heller 1.1 (invoke-debugger condition)))))
411     nil))
412 heller 1.23
413     ;;; Inspecting
414    
415     (defmethod inspected-parts (o)
416     (let* ((*print-array* nil) (*print-pretty* t)
417     (*print-circle* t) (*print-escape* t)
418     (*print-lines* custom:*inspect-print-lines*)
419     (*print-level* custom:*inspect-print-level*)
420     (*print-length* custom:*inspect-print-length*)
421     (sys::*inspect-all* (make-array 10 :fill-pointer 0 :adjustable t))
422     (tmp-pack (make-package (gensym "INSPECT-TMP-PACKAGE-")))
423     (*package* tmp-pack)
424     (sys::*inspect-unbound-value* (intern "#<unbound>" tmp-pack)))
425     (let ((inspection (sys::inspect-backend o)))
426     (values (format nil "~S~% ~A~{~%~A~}" o
427     (sys::insp-title inspection)
428     (sys::insp-blurb inspection))
429     (let ((count (sys::insp-num-slots inspection))
430     (pairs '()))
431     (dotimes (i count)
432     (multiple-value-bind (value name)
433     (funcall (sys::insp-nth-slot inspection) i)
434 heller 1.25 (push (cons (princ-to-string (or name i)) value)
435 heller 1.23 pairs)))
436     (nreverse pairs))))))
437 heller 1.1
438 mbaringer 1.29 (defimplementation quit-lisp ()
439     (#+lisp=cl ext:quit #-lisp=cl lisp:quit code))
440    
441 heller 1.1 ;;; Local Variables:
442     ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
443 heller 1.25 ;;; eval: (put 'dynamic-flet 'common-lisp-indent-function 1)
444 heller 1.1 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5