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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.5