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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.46 - (show annotations)
Sun Mar 27 18:40:51 2005 UTC (9 years ago) by heller
Branch: MAIN
CVS Tags: SLIME-1-2, SLIME-1-2-1
Changes since 1.45: +5 -2 lines
*** empty log message ***
1 ;;;; SWANK support for CLISP.
2
3 ;;;; Copyright (C) 2003, 2004 W. Jenkner, V. Sedach
4
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 ;;; 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 ;;; containing the "SOCKET", "REGEXP", and "LINUX" packages. The
19 ;;; portable xref from the CMU AI repository and metering.lisp from
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
25 (in-package :swank-backend)
26
27 (eval-when (:compile-toplevel :load-toplevel :execute)
28 ;;(use-package "SOCKET")
29 (use-package "GRAY"))
30
31 (eval-when (:compile-toplevel :execute)
32 (when (find-package "LINUX")
33 (pushnew :linux *features*)))
34
35 ;;;; 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 (and (find-package :clos)
42 (eql :external
43 (nth-value 1 (find-symbol (string ':standard-slot-definition)
44 :clos))))
45 "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 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
50
51 (defun swank-mop:slot-definition-documentation (slot)
52 (clos::slot-definition-documentation slot)))
53
54 #-#.(cl:if swank-backend::*have-mop* '(and) '(or))
55 (defclass swank-mop:standard-slot-definition ()
56 ()
57 (:documentation
58 "Dummy class created so that swank.lisp will compile and load."))
59
60 #+linux
61 (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
74 ;; XXX currently only works in CVS version. 2.32 breaks.
75 ;; #+linux
76 ;; (defimplementation call-without-interrupts (fn)
77 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
78 ;;
79 ;; #-linux
80 (defimplementation call-without-interrupts (fn)
81 (funcall fn))
82
83 #+unix
84 (defmethod getpid ()
85 (funcall (or (find-symbol "PROGRAM-ID" :system)
86 (find-symbol "PROCESS-ID" :system)
87 (error "getpid not implemented"))))
88
89 #+win32
90 (defmethod getpid ()
91 (cond ((find-package :win32)
92 (funcall (find-symbol "GetCurrentProcessId" :win32)))
93 (t
94 (system::getenv "PID"))))
95
96 ;; the above is likely broken; we need windows NT users!
97
98 (defimplementation lisp-implementation-type-name ()
99 "clisp")
100
101 (defimplementation set-default-directory (directory)
102 (setf (ext:default-directory) directory)
103 (namestring (setf *default-pathname-defaults* (ext:default-directory))))
104
105
106 ;;; TCP Server
107
108 (defimplementation create-socket (host port)
109 (declare (ignore host))
110 (socket:socket-server port))
111
112 (defimplementation local-port (socket)
113 (socket:socket-server-port socket))
114
115 (defimplementation close-socket (socket)
116 (socket:socket-server-close socket))
117
118 (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 (defimplementation accept-connection (socket
126 &key (external-format :iso-latin-1-unix))
127 (socket:socket-accept socket
128 :buffered nil ;; XXX should be t
129 :element-type 'character
130 :external-format (find-encoding external-format)))
131
132 ;;; Swank functions
133
134 (defimplementation arglist (fname)
135 (block nil
136 (or (ignore-errors (return (ext:arglist fname)))
137 :not-available)))
138
139 (defimplementation macroexpand-all (form)
140 (ext:expand-form form))
141
142 (defimplementation describe-symbol-for-emacs (symbol)
143 "Return a plist describing SYMBOL.
144 Return NIL if the symbol is unbound."
145 (let ((result ()))
146 (labels ((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 (when (fboundp symbol)
152 (if (macro-function symbol)
153 (setf (getf result :macro) (doc 'function))
154 (setf (getf result :function) (doc 'function))))
155 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
156 (maybe-push :class (when (find-class symbol nil)
157 (doc 'type))) ;this should be fixed
158 result)))
159
160 (defimplementation describe-definition (symbol namespace)
161 (ecase namespace
162 (:variable (describe symbol))
163 (:macro (describe (macro-function symbol)))
164 (:function (describe (symbol-function symbol)))
165 (:class (describe (find-class symbol)))))
166
167 (defun fspec-pathname (symbol)
168 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
169 (if (and path
170 (member (pathname-type path)
171 custom:*compiled-file-types* :test #'string=))
172 (loop for suffix in custom:*source-file-types*
173 thereis (make-pathname :defaults path :type suffix))
174 path)))
175
176 (defun fspec-location (fspec)
177 (let ((file (fspec-pathname fspec)))
178 (cond (file
179 (multiple-value-bind (truename c) (ignore-errors (truename file))
180 (cond (truename
181 (make-location (list :file (namestring truename))
182 (list :function-name (string fspec))))
183 (t (list :error (princ-to-string c))))))
184 (t (list :error (format nil "No source information available for: ~S"
185 fspec))))))
186
187 (defimplementation find-definitions (name)
188 (list (list name (fspec-location name))))
189
190 (defvar *sldb-topframe*)
191 (defvar *sldb-botframe*)
192 (defvar *sldb-source*)
193 (defvar *sldb-debugmode* 4)
194
195 (defun frame-down (frame)
196 (sys::frame-down-1 frame sys::*debug-mode*))
197
198 (defun frame-up (frame)
199 (sys::frame-up-1 frame sys::*debug-mode*))
200
201 (defimplementation call-with-debugging-environment (debugger-loop-fn)
202 (let* ((sys::*break-count* (1+ sys::*break-count*))
203 (sys::*driver* debugger-loop-fn)
204 (sys::*fasoutput-stream* nil)
205 (sys::*frame-limit1* (sys::frame-limit1 0))
206 (sys::*frame-limit2* (sys::frame-limit2))
207 (sys::*debug-mode* *sldb-debugmode*)
208 (*sldb-topframe* sys::*frame-limit1*))
209 (funcall debugger-loop-fn)))
210
211 (defun nth-frame (index)
212 (loop for frame = *sldb-topframe* then (frame-up frame)
213 repeat index
214 finally (return frame)))
215
216 (defimplementation compute-backtrace (start end)
217 (let ((end (or end most-positive-fixnum)))
218 (loop for last = nil then frame
219 for frame = (nth-frame start) then (frame-up frame)
220 for i from start below end
221 until (or (eq frame last) (system::driver-frame-p frame))
222 collect frame)))
223
224 (defimplementation print-frame (frame stream)
225 (write-string (string-left-trim '(#\Newline)
226 (with-output-to-string (stream)
227 (sys::describe-frame stream frame)))
228 stream))
229
230 (defimplementation eval-in-frame (form frame-number)
231 (sys::eval-at (nth-frame frame-number) form))
232
233 (defimplementation frame-locals (frame-number)
234 (let* ((frame (nth-frame frame-number))
235 (frame-env (sys::eval-at frame '(sys::the-environment))))
236 (append
237 (frame-do-venv frame (svref frame-env 0))
238 (frame-do-fenv frame (svref frame-env 1))
239 (frame-do-benv frame (svref frame-env 2))
240 (frame-do-genv frame (svref frame-env 3))
241 (frame-do-denv frame (svref frame-env 4)))))
242
243 (defimplementation frame-var-value (frame var)
244 (getf (nth var (frame-locals frame)) :value))
245
246 ;; Interpreter-Variablen-Environment has the shape
247 ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
248
249 (defun frame-do-venv (frame venv)
250 (loop for i from 1 below (length venv) by 2
251 as symbol = (svref venv (1- i))
252 and value = (svref venv i)
253 collect (list :name symbol :id 0
254 :value (if (eq sys::specdecl value)
255 ;; special variable
256 (sys::eval-at frame symbol)
257 ;; lexical variable or symbol macro
258 value))))
259
260 (defun frame-do-fenv (frame fenv)
261 (declare (ignore frame fenv))
262 nil)
263
264 (defun frame-do-benv (frame benv)
265 (declare (ignore frame benv))
266 nil)
267
268 (defun frame-do-genv (frame genv)
269 (declare (ignore frame genv))
270 nil)
271
272 (defun frame-do-denv (frame denv)
273 (declare (ignore frame denv))
274 nil)
275
276 (defimplementation frame-catch-tags (index)
277 (declare (ignore index))
278 nil)
279
280 (defimplementation return-from-frame (index form)
281 (sys::return-from-eval-frame (nth-frame index) form))
282
283 (defimplementation restart-frame (index)
284 (sys::redo-eval-frame (nth-frame index)))
285
286 (defimplementation frame-source-location-for-emacs (index)
287 (let ((f (nth-frame index)))
288 (list :error (format nil "Cannot find source for frame: ~A ~A ~A"
289 f
290 (sys::eval-frame-p f)
291 (sys::the-frame)))))
292
293 ;;; Profiling
294
295 (defimplementation profile (fname)
296 (eval `(mon:monitor ,fname))) ;monitor is a macro
297
298 (defimplementation profiled-functions ()
299 mon:*monitored-functions*)
300
301 (defimplementation unprofile (fname)
302 (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
303
304 (defimplementation unprofile-all ()
305 (mon:unmonitor))
306
307 (defimplementation profile-report ()
308 (mon:report-monitoring))
309
310 (defimplementation profile-reset ()
311 (mon:reset-all-monitoring))
312
313 (defimplementation profile-package (package callers-p methods)
314 (declare (ignore callers-p methods))
315 (mon:monitor-all package))
316
317 ;;; Handle compiler conditions (find out location of error etc.)
318
319 (defmacro compile-file-frobbing-notes ((&rest args) &body body)
320 "Pass ARGS to COMPILE-FILE, send the compiler notes to
321 *STANDARD-INPUT* and frob them in BODY."
322 `(let ((*error-output* (make-string-output-stream))
323 (*compile-verbose* t))
324 (multiple-value-prog1
325 (compile-file ,@args)
326 (handler-case
327 (with-input-from-string
328 (*standard-input* (get-output-stream-string *error-output*))
329 ,@body)
330 (sys::simple-end-of-file () nil)))))
331
332 (defvar *orig-c-warn* (symbol-function 'system::c-warn))
333 (defvar *orig-c-style-warn* (symbol-function 'system::c-style-warn))
334 (defvar *orig-c-error* (symbol-function 'system::c-error))
335 (defvar *orig-c-report-problems* (symbol-function 'system::c-report-problems))
336
337 (defmacro dynamic-flet (names-functions &body body)
338 "(dynamic-flet ((NAME FUNCTION) ...) BODY ...)
339 Execute BODY with NAME's function slot set to FUNCTION."
340 `(ext:letf* ,(loop for (name function) in names-functions
341 collect `((symbol-function ',name) ,function))
342 ,@body))
343
344 (defvar *buffer-name* nil)
345 (defvar *buffer-offset*)
346
347 (defun compiler-note-location ()
348 "Return the current compiler location."
349 (let ((lineno1 sys::*compile-file-lineno1*)
350 (lineno2 sys::*compile-file-lineno2*)
351 (file sys::*compile-file-truename*))
352 (cond ((and file lineno1 lineno2)
353 (make-location (list ':file (namestring file))
354 (list ':line lineno1)))
355 (*buffer-name*
356 (make-location (list ':buffer *buffer-name*)
357 (list ':position *buffer-offset*)))
358 (t
359 (list :error "No error location available")))))
360
361 (defun signal-compiler-warning (cstring args severity orig-fn)
362 (signal (make-condition 'compiler-condition
363 :severity severity
364 :message (apply #'format nil cstring args)
365 :location (compiler-note-location)))
366 (apply orig-fn cstring args))
367
368 (defun c-warn (cstring &rest args)
369 (signal-compiler-warning cstring args :warning *orig-c-warn*))
370
371 (defun c-style-warn (cstring &rest args)
372 (dynamic-flet ((sys::c-warn *orig-c-warn*))
373 (signal-compiler-warning cstring args :style-warning *orig-c-style-warn*)))
374
375 (defun c-error (cstring &rest args)
376 (signal-compiler-warning cstring args :error *orig-c-error*))
377
378 (defimplementation call-with-compilation-hooks (function)
379 (handler-bind ((warning #'handle-notification-condition))
380 (dynamic-flet ((system::c-warn #'c-warn)
381 (system::c-style-warn #'c-style-warn)
382 (system::c-error #'c-error))
383 (funcall function))))
384
385 (defun handle-notification-condition (condition)
386 "Handle a condition caused by a compiler warning."
387 (signal (make-condition 'compiler-condition
388 :original-condition condition
389 :severity :warning
390 :message (princ-to-string condition)
391 :location (compiler-note-location))))
392
393 (defimplementation swank-compile-file (filename load-p)
394 (with-compilation-hooks ()
395 (with-compilation-unit ()
396 (let ((fasl-file (compile-file filename)))
397 (when (and load-p fasl-file)
398 (load fasl-file))
399 nil))))
400
401 (defimplementation swank-compile-string (string &key buffer position directory)
402 (declare (ignore directory))
403 (with-compilation-hooks ()
404 (let ((*buffer-name* buffer)
405 (*buffer-offset* position))
406 (funcall (compile nil (read-from-string
407 (format nil "(~S () ~A)" 'lambda string)))))))
408
409 ;;; Portable XREF from the CMU AI repository.
410
411 (setq pxref::*handle-package-forms* '(cl:in-package))
412
413 (defmacro defxref (name function)
414 `(defimplementation ,name (name)
415 (xref-results (,function name))))
416
417 (defxref who-calls pxref:list-callers)
418 (defxref who-references pxref:list-readers)
419 (defxref who-binds pxref:list-setters)
420 (defxref who-sets pxref:list-setters)
421 (defxref list-callers pxref:list-callers)
422 (defxref list-callees pxref:list-callees)
423
424 (defun xref-results (symbols)
425 (let ((xrefs '()))
426 (dolist (symbol symbols)
427 (push (list symbol (fspec-location symbol)) xrefs))
428 xrefs))
429
430 (when (find-package :swank-loader)
431 (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
432 (lambda ()
433 (let ((home (user-homedir-pathname)))
434 (and (ext:probe-directory home)
435 (probe-file (format nil "~A/.swank.lisp"
436 (namestring (truename home)))))))))
437
438 ;; Don't set *debugger-hook* to nil on break.
439 (ext:without-package-lock ()
440 (defun break (&optional (format-string "Break") &rest args)
441 (if (not sys::*use-clcs*)
442 (progn
443 (terpri *error-output*)
444 (apply #'format *error-output*
445 (concatenate 'string "*** - " format-string)
446 args)
447 (funcall ext:*break-driver* t))
448 (let ((condition
449 (make-condition 'simple-condition
450 :format-control format-string
451 :format-arguments args))
452 ;;(*debugger-hook* nil)
453 ;; Issue 91
454 )
455 (ext:with-restarts
456 ((continue
457 :report (lambda (stream)
458 (format stream (sys::text "Return from ~S loop")
459 'break))
460 ()))
461 (with-condition-restarts condition (list (find-restart 'continue))
462 (invoke-debugger condition)))))
463 nil))
464
465 ;;; Inspecting
466
467 (defclass clisp-inspector (inspector)
468 ())
469
470 (defimplementation make-default-inspector ()
471 (make-instance 'clisp-inspector))
472
473 (defmethod inspect-for-emacs ((o t) (inspector clisp-inspector))
474 (declare (ignore inspector))
475 (let* ((*print-array* nil) (*print-pretty* t)
476 (*print-circle* t) (*print-escape* t)
477 (*print-lines* custom:*inspect-print-lines*)
478 (*print-level* custom:*inspect-print-level*)
479 (*print-length* custom:*inspect-print-length*)
480 (sys::*inspect-all* (make-array 10 :fill-pointer 0 :adjustable t))
481 (tmp-pack (make-package (gensym "INSPECT-TMP-PACKAGE-")))
482 (*package* tmp-pack)
483 (sys::*inspect-unbound-value* (intern "#<unbound>" tmp-pack)))
484 (let ((inspection (sys::inspect-backend o)))
485 (values (format nil "~S~% ~A~{~%~A~}" o
486 (sys::insp-title inspection)
487 (sys::insp-blurb inspection))
488 (loop with count = (sys::insp-num-slots inspection)
489 for i upto count
490 for (value name) = (multiple-value-list
491 (funcall (sys::insp-nth-slot
492 inspection) i))
493 collect `((:value ,name) " = " (:value ,value)
494 (:newline)))))))
495
496 (defimplementation quit-lisp ()
497 #+lisp=cl (ext:quit)
498 #-lisp=cl (lisp:quit))
499
500 ;;; Local Variables:
501 ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
502 ;;; eval: (put 'dynamic-flet 'common-lisp-indent-function 1)
503 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5