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

Contents of /slime/swank-clisp.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.20 - (show annotations)
Wed Feb 18 07:32:44 2004 UTC (10 years, 2 months ago) by heller
Branch: MAIN
Changes since 1.19: +4 -10 lines
Update comments about metering package.
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")
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 #+linux
36 (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
49 ;; #+linux
50 ;; (defmethod call-without-interrupts (fn)
51 ;; (with-blocked-signals (#.linux:SIGINT) (funcall fn)))
52 ;;
53 ;; #-linux
54 (defmethod call-without-interrupts (fn)
55 (funcall fn))
56
57 #+unix (defmethod getpid () (system::program-id))
58 #+win32 (defmethod getpid () (or (system::getenv "PID") -1))
59 ;; the above is likely broken; we need windows NT users!
60
61
62 ;;; TCP Server
63
64 (setq *swank-in-background* nil)
65
66 (defimplementation create-socket (host port)
67 (declare (ignore host))
68 (socket:socket-server port))
69
70 (defimplementation local-port (socket)
71 (socket:socket-server-port socket))
72
73 (defimplementation close-socket (socket)
74 (socket:socket-server-close socket))
75
76 (defimplementation accept-connection (socket)
77 (socket:socket-accept socket
78 :buffered nil ;; XXX should be t
79 :element-type 'character
80 :external-format (ext:make-encoding
81 :charset 'charset:iso-8859-1
82 :line-terminator :unix)))
83
84 (defvar *sigio-handlers* '()
85 "List of (key . fn) pairs to be called on SIGIO.")
86
87 (defun sigio-handler (signal)
88 (mapc (lambda (handler) (funcall (cdr handler))) *sigio-handlers*))
89
90 ;(trace sigio-handler)
91
92 (defvar *saved-sigio-handler*)
93
94 #+(or)
95 (progn
96 (defun set-sigio-handler ()
97 (setf *saved-sigio-handler*
98 (linux:set-signal-handler linux:SIGIO
99 (lambda (signal) (sigio-handler signal))))
100 (let* ((action (linux:signal-action-retrieve linux:SIGIO))
101 (flags (linux:sa-flags action)))
102 (setf (linux:sa-flags action) (logior flags linux:SA_NODEFER))
103 (linux:signal-action-install linux:SIGIO action)))
104
105 (defimplementation add-input-handler (socket fn)
106 (set-sigio-handler)
107 (let ((fd (socket:socket-stream-handle socket)))
108 (format *debug-io* "Adding input handler: ~S ~%" fd)
109 ;; XXX error checking
110 (linux:fcntl3l fd linux:F_SETOWN (getpid))
111 (linux:fcntl3l fd linux:F_SETFL linux:O_ASYNC)
112 (push (cons fd fn) *sigio-handlers*)))
113
114 (defimplementation remove-input-handlers (socket)
115 (let ((fd (socket:socket-stream-handle socket)))
116 (remove-sigio-handler fd)
117 (setf *sigio-handlers* (delete fd *sigio-handlers* :key #'car)))
118 (close socket))
119 )
120
121 ;;; Swank functions
122
123 (defimplementation arglist-string (fname)
124 (format-arglist fname #'ext:arglist))
125
126 (defimplementation macroexpand-all (form)
127 (ext:expand-form form))
128
129 (defimplementation describe-symbol-for-emacs (symbol)
130 "Return a plist describing SYMBOL.
131 Return NIL if the symbol is unbound."
132 (let ((result ()))
133 (labels ((doc (kind)
134 (or (documentation symbol kind) :not-documented))
135 (maybe-push (property value)
136 (when value
137 (setf result (list* property value result)))))
138 (when (fboundp symbol)
139 (if (macro-function symbol)
140 (setf (getf result :macro) (doc 'function))
141 (setf (getf result :function) (doc 'function))))
142 (maybe-push :variable (when (boundp symbol) (doc 'variable)))
143 (maybe-push :class (when (find-class symbol nil)
144 (doc 'type))) ;this should be fixed
145 result)))
146
147 (defun fspec-pathname (symbol &optional type)
148 (declare (ignore type))
149 (let ((path (getf (gethash symbol sys::*documentation*) 'sys::file)))
150 (if (and path
151 (member (pathname-type path)
152 custom:*compiled-file-types* :test #'string=))
153 (loop
154 for suffix in custom:*source-file-types*
155 thereis (make-pathname :defaults path :type suffix))
156 path)))
157
158 (defun find-multiple-definitions (fspec)
159 (list `(,fspec t)))
160
161 (defun find-definition-in-file (fspec type file)
162 (declare (ignore fspec type file))
163 ;; FIXME
164 0)
165
166 (defun fspec-source-locations (fspec)
167 (let ((defs (find-multiple-definitions fspec)))
168 (let ((locations '()))
169 (loop for (fspec type) in defs do
170 (let ((file (fspec-pathname fspec type)))
171 (etypecase file
172 (pathname
173 (let ((start (find-definition-in-file fspec type file)))
174 (push (make-location
175 (list :file (namestring (truename file)))
176 (if start
177 (list :position (1+ start))
178 (list :function-name (string fspec))))
179 locations)))
180 ((member :top-level)
181 (push (list :error (format nil "Defined at toplevel: ~A"
182 fspec))
183 locations))
184 (null
185 (push (list :error (format nil
186 "Unkown source location for ~A"
187 fspec))
188 locations))
189 )))
190 locations)))
191
192 (defimplementation find-function-locations (symbol-name)
193 (multiple-value-bind (symbol foundp) (find-symbol-designator symbol-name)
194 (cond ((not foundp)
195 (list (list :error (format nil "Unkown symbol: ~A" symbol-name))))
196 ((macro-function symbol)
197 (fspec-source-locations symbol))
198 ((special-operator-p symbol)
199 (list (list :error (format nil "~A is a special-operator" symbol))))
200 ((fboundp symbol)
201 (fspec-source-locations symbol))
202 (t (list (list :error
203 (format nil "Symbol not fbound: ~A" symbol-name))))
204 )))
205
206 (defvar *sldb-topframe*)
207 (defvar *sldb-botframe*)
208 (defvar *sldb-source*)
209 (defvar *sldb-restarts*)
210 (defvar *sldb-debugmode* 4)
211
212 (defimplementation call-with-debugging-environment (debugger-loop-fn)
213 (let* ((sys::*break-count* (1+ sys::*break-count*))
214 (sys::*driver* debugger-loop-fn)
215 (sys::*fasoutput-stream* nil)
216 ;;; (sys::*frame-limit1* (sys::frame-limit1 43))
217 (sys::*frame-limit1* (sys::frame-limit1 0))
218 ;;; (sys::*frame-limit2* (sys::frame-limit2))
219 (sys::*debug-mode* *sldb-debugmode*)
220 (*sldb-topframe*
221 (sys::frame-down-1
222 (sys::frame-up-1 sys::*frame-limit1* sys::*debug-mode*)
223 sys::*debug-mode*))
224 (*sldb-botframe* (sys::frame-up *sldb-topframe* sys::*debug-mode*))
225 (*debugger-hook* nil)
226 (*package* *buffer-package*)
227 (*sldb-restarts*
228 (compute-restarts *swank-debugger-condition*))
229 (*print-pretty* nil)
230 (*print-readably* nil))
231 ;;; (*print-level* 3)
232 ;;; (*print-length* 10))
233 (funcall debugger-loop-fn)))
234
235 (defun format-restarts-for-emacs ()
236 (loop for restart in *sldb-restarts*
237 collect (list (princ-to-string (restart-name restart))
238 (princ-to-string restart))))
239
240 (defun nth-frame (index)
241 (loop for frame = *sldb-topframe* then (sys::frame-up-1 frame
242 sys::*debug-mode*)
243 repeat index
244 never (eq frame *sldb-botframe*)
245 finally (return frame)))
246
247 (defun compute-backtrace (start end)
248 (let ((end (or end most-positive-fixnum)))
249 (loop for f = (nth-frame start)
250 then (sys::frame-up-1 f sys::*debug-mode*)
251 for i from start below end
252 until (eq f *sldb-botframe*)
253 collect f)))
254
255 (defimplementation backtrace (start-frame-number end-frame-number)
256 (flet ((format-frame (f i)
257 (print-with-frame-label
258 i (lambda (s)
259 (princ (string-left-trim
260 '(#\Newline)
261 (with-output-to-string (stream)
262 (sys::describe-frame stream f)))
263 s)))))
264 (loop for i from start-frame-number
265 for f in (compute-backtrace start-frame-number end-frame-number)
266 collect (list i (format-frame f i)))))
267
268 (defimplementation eval-in-frame (form frame-number)
269 (sys::eval-at (nth-frame frame-number) form))
270
271 (defimplementation frame-locals (frame-number)
272 (let* ((frame (nth-frame frame-number))
273 (frame-env (sys::eval-at frame '(sys::the-environment))))
274 (append
275 (frame-do-venv frame (svref frame-env 0))
276 (frame-do-fenv frame (svref frame-env 1))
277 (frame-do-benv frame (svref frame-env 2))
278 (frame-do-genv frame (svref frame-env 3))
279 (frame-do-denv frame (svref frame-env 4)))))
280
281 ;; Interpreter-Variablen-Environment has the shape
282 ;; NIL or #(v1 val1 ... vn valn NEXT-ENV).
283
284 (defun frame-do-venv (frame venv)
285 (loop for i from 1 below (length venv) by 2
286 as symbol = (svref venv (1- i))
287 and value = (svref venv i)
288 collect (list :name (to-string symbol) :id 0
289 :value-string (to-string
290 (if (eq sys::specdecl value)
291 ;; special variable
292 (sys::eval-at frame symbol)
293 ;; lexical variable or symbol macro
294 value)))))
295
296 (defun frame-do-fenv (frame fenv)
297 (declare (ignore frame fenv))
298 nil)
299
300 (defun frame-do-benv (frame benv)
301 (declare (ignore frame benv))
302 nil)
303
304 (defun frame-do-genv (frame genv)
305 (declare (ignore frame genv))
306 nil)
307
308 (defun frame-do-denv (frame denv)
309 (declare (ignore frame denv))
310 nil)
311
312 (defimplementation frame-catch-tags (index)
313 (declare (ignore index))
314 nil)
315
316 (defimplementation return-from-frame (index form)
317 (sys::return-from-eval-frame (nth-frame index) (from-string form)))
318
319 (defimplementation restart-frame (index)
320 (sys::redo-eval-frame (nth-frame index)))
321
322 (defimplementation frame-source-location-for-emacs (index)
323 (list :error (format nil "Cannot find source for frame: ~A"
324 (nth-frame index))))
325
326 (defimplementation debugger-info-for-emacs (start end)
327 (list (debugger-condition-for-emacs)
328 (format-restarts-for-emacs)
329 (backtrace start end)))
330
331 (defun nth-restart (index)
332 (nth index *sldb-restarts*))
333
334 (defslimefun invoke-nth-restart (index)
335 (invoke-restart-interactively (nth-restart index)))
336
337 (defslimefun sldb-abort ()
338 (invoke-restart (find 'abort *sldb-restarts* :key #'restart-name)))
339
340 ;;; Profiling
341
342 (defimplementation profile (fname)
343 (eval `(mon:monitor ,fname))) ;monitor is a macro
344
345 (defimplementation profiled-functions ()
346 mon:*monitored-functions*)
347
348 (defimplementation unprofile (fname)
349 (eval `(mon:unmonitor ,fname))) ;unmonitor is a macro
350
351 (defimplementation unprofile-all ()
352 (mon:unmonitor))
353
354 (defimplementation profile-report ()
355 (mon:report-monitoring))
356
357 (defimplementation profile-reset ()
358 (mon:reset-all-monitoring))
359
360 (defimplementation profile-package (package callers-p methods)
361 (declare (ignore callers-p methods))
362 (mon:monitor-all package))
363
364 ;;; Handle compiler conditions (find out location of error etc.)
365
366 (defmacro compile-file-frobbing-notes ((&rest args) &body body)
367 "Pass ARGS to COMPILE-FILE, send the compiler notes to
368 *STANDARD-INPUT* and frob them in BODY."
369 `(let ((*error-output* (make-string-output-stream))
370 (*compile-verbose* t))
371 (multiple-value-prog1
372 (compile-file ,@args)
373 (handler-case
374 (with-input-from-string
375 (*standard-input* (get-output-stream-string *error-output*))
376 ,@body)
377 (sys::simple-end-of-file () nil)))))
378
379 (defimplementation call-with-compilation-hooks (function)
380 (handler-bind ((compiler-condition #'handle-notification-condition))
381 (funcall function)))
382
383 (defun handle-notification-condition (condition)
384 "Handle a condition caused by a compiler warning."
385 (declare (ignore condition)))
386
387 (defvar *buffer-name* nil)
388 (defvar *buffer-offset*)
389
390 (defvar *compiler-note-line-regexp*
391 (regexp:regexp-compile
392 "^(WARNING|ERROR) .* in lines ([0-9]+)\\.\\.[0-9]+ :$"
393 :extended t))
394
395 (defun split-compiler-note-line (line)
396 (multiple-value-bind (all head tail)
397 (regexp:regexp-exec *compiler-note-line-regexp* line)
398 (declare (ignore all))
399 (if head
400 (list (let ((*package* (find-package :keyword)))
401 (read-from-string (regexp:match-string line head)))
402 (read-from-string (regexp:match-string line tail)))
403 (list nil line))))
404
405 ;;; Ugly but essentially working.
406 ;;; TODO: Do something with the summary about undefined functions etc.
407
408 (defimplementation compile-file-for-emacs (filename load-p)
409 (with-compilation-hooks ()
410 (multiple-value-bind (fas-file w-p f-p)
411 (compile-file-frobbing-notes (filename)
412 (read-line) ;""
413 (read-line) ;"Compiling file ..."
414 (loop
415 with condition
416 for (severity message) = (split-compiler-note-line (read-line))
417 until (and (stringp message) (string= message ""))
418 if severity
419 do (when condition
420 (signal condition))
421 (setq condition
422 (make-condition 'compiler-condition
423 :severity severity
424 :message ""
425 :location `(:location (:file ,filename)
426 (:line ,message))))
427 else do (setf (message condition)
428 (format nil "~a~&~a" (message condition) message))
429 finally (when condition
430 (signal condition))))
431 ;; w-p = errors + warnings, f-p = errors + warnings - style warnings,
432 ;; where a result of 0 is replaced by NIL. It follows that w-p
433 ;; is non-NIL iff there was any note whatsoever and that f-p is
434 ;; non-NIL iff there was anything more severe than a style
435 ;; warning. This is completely ANSI compliant.
436 (declare (ignore w-p f-p))
437 (if (and fas-file load-p)
438 (load fas-file)
439 fas-file))))
440
441 (defimplementation compile-string-for-emacs (string &key buffer position)
442 (with-compilation-hooks ()
443 (let ((*package* *buffer-package*)
444 (*buffer-name* buffer)
445 (*buffer-offset* position))
446 (eval (from-string
447 (format nil "(funcall (compile nil '(lambda () ~A)))"
448 string))))))
449
450 ;;; Portable XREF from the CMU AI repository.
451
452 (setq xref::*handle-package-forms* '(cl:in-package))
453
454 (defun lookup-xrefs (finder name)
455 (xref-results-for-emacs (funcall finder (from-string name))))
456
457 (defimplementation who-calls (function-name)
458 (lookup-xrefs #'xref:list-callers function-name))
459
460 (defimplementation who-references (variable)
461 (lookup-xrefs #'xref:list-readers variable))
462
463 (defimplementation who-binds (variable)
464 (lookup-xrefs #'xref:list-setters variable))
465
466 (defimplementation who-sets (variable)
467 (lookup-xrefs #'xref:list-setters variable))
468
469 (defimplementation list-callers (symbol-name)
470 (lookup-xrefs #'xref:who-calls symbol-name))
471
472 (defimplementation list-callees (symbol-name)
473 (lookup-xrefs #'xref:list-callees symbol-name))
474
475 (defun xref-results-for-emacs (fspecs)
476 (let ((xrefs '()))
477 (dolist (fspec fspecs)
478 (dolist (location (fspec-source-locations fspec))
479 (push (cons (to-string fspec) location) xrefs)))
480 (group-xrefs xrefs)))
481
482 (when (find-package :swank-loader)
483 (setf (symbol-function (intern "USER-INIT-FILE" :swank-loader))
484 (lambda ()
485 (let ((home (user-homedir-pathname)))
486 (and (ext:probe-directory home)
487 (probe-file (format nil "~A/.swank.lisp"
488 (namestring (truename home)))))))))
489
490 ;; Don't set *debugger-hook* to nil on break.
491 (ext:without-package-lock ()
492 (defun break (&optional (format-string "Break") &rest args)
493 (if (not sys::*use-clcs*)
494 (progn
495 (terpri *error-output*)
496 (apply #'format *error-output*
497 (concatenate 'string "*** - " format-string)
498 args)
499 (funcall ext:*break-driver* t))
500 (let ((condition
501 (make-condition 'simple-condition
502 :format-control format-string
503 :format-arguments args))
504 ;;(*debugger-hook* nil)
505 ;; Issue 91
506 )
507 (ext:with-restarts
508 ((CONTINUE
509 :report (lambda (stream)
510 (format stream (sys::TEXT "Return from ~S loop")
511 'break))
512 ()))
513 (with-condition-restarts condition (list (find-restart 'CONTINUE))
514 (invoke-debugger condition)))))
515 nil))
516
517 ;;; Local Variables:
518 ;;; eval: (put 'compile-file-frobbing-notes 'lisp-indent-function 1)
519 ;;; End:

  ViewVC Help
Powered by ViewVC 1.1.5