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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5