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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5