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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.150 - (hide annotations)
Fri Jan 6 09:02:43 2012 UTC (2 years, 3 months ago) by heller
Branch: MAIN
Changes since 1.149: +19 -0 lines
Add a "sentinel thread" to protect access to global lists.

* swank.lisp (start-sentinel, sentinel, send-to-sentinel)
(sentinel-serve, sentinel-stop-server, sentinel-maybe-exit): New.
(make-connection, close-connection, setup-server, stop-server):
Use the sentinel.
(close-connection%): Factored out.
* swank-backend.lisp (register-thread, find-registered): New.
* swank-allegro.lisp: Implement it.
* swank-ccl.lisp:
* swank-lispworks.lisp:
* swank-sbcl.lisp:
1 heller 1.79 ;;;; -*- indent-tabs-mode: nil; outline-regexp: ";;;;;* "; -*-
2 heller 1.1 ;;;
3     ;;; swank-allegro.lisp --- Allegro CL specific code for SLIME.
4     ;;;
5 heller 1.60 ;;; Created 2003
6 heller 1.1 ;;;
7     ;;; This code has been placed in the Public Domain. All warranties
8 heller 1.74 ;;; are disclaimed.
9 heller 1.1 ;;;
10    
11 heller 1.21 (in-package :swank-backend)
12    
13 heller 1.1 (eval-when (:compile-toplevel :load-toplevel :execute)
14     (require :sock)
15 heller 1.135 (require :process)
16     #+(version>= 8 2)
17     (require 'lldb)
18     )
19    
20 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
21 heller 1.1
22 mbaringer 1.53 ;;; swank-mop
23    
24 heller 1.60 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
25 mbaringer 1.53
26 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
27 heller 1.62 (documentation slot t))
28 mbaringer 1.53
29 heller 1.79
30 heller 1.146 ;;;; UTF8
31    
32     (defimplementation string-to-utf8 (s)
33 heller 1.147 (let ((ef (load-time-value (excl:find-external-format :utf-8) t)))
34     (excl:string-to-octets s :external-format ef)))
35 heller 1.146
36     (defimplementation utf8-to-string (u)
37 heller 1.147 (let ((ef (load-time-value (excl:find-external-format :utf-8) t)))
38     (excl:octets-to-string u :external-format ef)))
39 heller 1.146
40    
41 heller 1.8 ;;;; TCP Server
42 heller 1.1
43 heller 1.21 (defimplementation preferred-communication-style ()
44     :spawn)
45 heller 1.12
46 heller 1.148 (defimplementation create-socket (host port &key backlog)
47 heller 1.14 (socket:make-socket :connect :passive :local-port port
48 heller 1.148 :local-host host :reuse-address t
49     :backlog (or backlog 5)))
50 heller 1.5
51 heller 1.10 (defimplementation local-port (socket)
52 heller 1.6 (socket:local-port socket))
53    
54 heller 1.10 (defimplementation close-socket (socket)
55 heller 1.6 (close socket))
56    
57 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
58     timeout)
59     (declare (ignore buffering timeout))
60 heller 1.94 (let ((s (socket:accept-connection socket :wait t)))
61     (when external-format
62     (setf (stream-external-format s) external-format))
63 heller 1.63 s))
64 heller 1.6
65 heller 1.141 (defimplementation socket-fd (stream)
66     (excl::stream-input-handle stream))
67    
68 heller 1.94 (defvar *external-format-to-coding-system*
69     '((:iso-8859-1
70     "latin-1" "latin-1-unix" "iso-latin-1-unix"
71     "iso-8859-1" "iso-8859-1-unix")
72     (:utf-8 "utf-8" "utf-8-unix")
73     (:euc-jp "euc-jp" "euc-jp-unix")
74     (:us-ascii "us-ascii" "us-ascii-unix")
75     (:emacs-mule "emacs-mule" "emacs-mule-unix")))
76    
77     (defimplementation find-external-format (coding-system)
78     (let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
79     *external-format-to-coding-system*)))
80     (and e (excl:crlf-base-ef
81     (excl:find-external-format (car e)
82     :try-variant t)))))
83 heller 1.64
84 heller 1.34 (defimplementation format-sldb-condition (c)
85     (princ-to-string c))
86    
87 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
88     (funcall fn))
89    
90 heller 1.9 ;;;; Unix signals
91    
92 heller 1.10 (defimplementation getpid ()
93 heller 1.8 (excl::getpid))
94 heller 1.6
95 heller 1.15 (defimplementation lisp-implementation-type-name ()
96     "allegro")
97    
98 pseibel 1.28 (defimplementation set-default-directory (directory)
99 mkoeppe 1.86 (let* ((dir (namestring (truename (merge-pathnames directory)))))
100     (setf *default-pathname-defaults* (pathname (excl:chdir dir)))
101 heller 1.66 dir))
102 pseibel 1.28
103 heller 1.35 (defimplementation default-directory ()
104 heller 1.66 (namestring (excl:current-directory)))
105 heller 1.35
106 heller 1.8 ;;;; Misc
107 heller 1.1
108 heller 1.21 (defimplementation arglist (symbol)
109 lgorrie 1.30 (handler-case (excl:arglist symbol)
110     (simple-error () :not-available)))
111 heller 1.21
112     (defimplementation macroexpand-all (form)
113     (excl::walk form))
114 heller 1.1
115 heller 1.10 (defimplementation describe-symbol-for-emacs (symbol)
116 heller 1.1 (let ((result '()))
117     (flet ((doc (kind &optional (sym symbol))
118     (or (documentation sym kind) :not-documented))
119     (maybe-push (property value)
120     (when value
121     (setf result (list* property value result)))))
122     (maybe-push
123     :variable (when (boundp symbol)
124     (doc 'variable)))
125     (maybe-push
126     :function (if (fboundp symbol)
127     (doc 'function)))
128     (maybe-push
129     :class (if (find-class symbol nil)
130     (doc 'class)))
131     result)))
132    
133 heller 1.20 (defimplementation describe-definition (symbol namespace)
134     (ecase namespace
135     (:variable
136     (describe symbol))
137     ((:function :generic-function)
138     (describe (symbol-function symbol)))
139     (:class
140     (describe (find-class symbol)))))
141 heller 1.10
142 heller 1.8 ;;;; Debugger
143    
144 heller 1.1 (defvar *sldb-topframe*)
145 heller 1.4
146 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
147 heller 1.71 (let ((*sldb-topframe* (find-topframe))
148 heller 1.20 (excl::*break-hook* nil))
149 heller 1.4 (funcall debugger-loop-fn)))
150 heller 1.1
151 mkoeppe 1.92 (defimplementation sldb-break-at-start (fname)
152 heller 1.135 ;; :print-before is kind of mis-used but we just want to stuff our
153     ;; break form somewhere. This does not work for setf, :before and
154     ;; :after methods, which need special syntax in the trace call, see
155     ;; ACL's doc/debugging.htm chapter 10.
156 mkoeppe 1.92 (eval `(trace (,fname
157     :print-before
158     ((break "Function start breakpoint of ~A" ',fname)))))
159     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
160    
161 heller 1.71 (defun find-topframe ()
162 trittweiler 1.125 (let ((magic-symbol (intern (symbol-name :swank-debugger-hook)
163     (find-package :swank)))
164 sboukarev 1.143 (top-frame (excl::int-newest-frame (excl::current-thread))))
165 trittweiler 1.125 (loop for frame = top-frame then (next-frame frame)
166     for name = (debugger:frame-name frame)
167     for i from 0
168     when (eq name magic-symbol)
169     return (next-frame frame)
170     until (= i 10) finally (return top-frame))))
171 heller 1.71
172 heller 1.42 (defun next-frame (frame)
173     (let ((next (excl::int-next-older-frame frame)))
174     (cond ((not next) nil)
175     ((debugger:frame-visible-p next) next)
176     (t (next-frame next)))))
177    
178 heller 1.1 (defun nth-frame (index)
179 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
180 heller 1.1 (i index (1- i)))
181     ((zerop i) frame)))
182    
183 heller 1.20 (defimplementation compute-backtrace (start end)
184 heller 1.1 (let ((end (or end most-positive-fixnum)))
185 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
186 heller 1.1 for i from start below end
187 heller 1.117 while f collect f)))
188 heller 1.1
189 heller 1.117 (defimplementation print-frame (frame stream)
190     (debugger:output-frame stream frame :moderate))
191 heller 1.4
192 heller 1.10 (defimplementation frame-locals (index)
193 heller 1.1 (let ((frame (nth-frame index)))
194     (loop for i from 0 below (debugger:frame-number-vars frame)
195 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
196 heller 1.1 :id 0
197 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
198 heller 1.1
199 heller 1.39 (defimplementation frame-var-value (frame var)
200     (let ((frame (nth-frame frame)))
201     (debugger:frame-var-value frame var)))
202 heller 1.1
203 heller 1.21 (defimplementation disassemble-frame (index)
204 heller 1.135 (let ((frame (nth-frame index)))
205     (multiple-value-bind (x fun xx xxx pc) (debugger::dyn-fd-analyze frame)
206     (format t "pc: ~d (~s ~s ~s)~%fun: ~a~%" pc x xx xxx fun)
207     (disassemble (debugger:frame-function frame)))))
208 heller 1.21
209 heller 1.128 (defimplementation frame-source-location (index)
210 heller 1.135 (let* ((frame (nth-frame index)))
211     (multiple-value-bind (x fun xx xxx pc) (debugger::dyn-fd-analyze frame)
212     (declare (ignore x xx xxx))
213 heller 1.136 (cond (pc
214     #+(version>= 8 2)
215     (pc-source-location fun pc)
216     #-(version>= 8 2)
217     (function-source-location fun))
218     (t ; frames for unbound functions etc end up here
219     (cadr (car (fspec-definition-locations
220     (car (debugger:frame-expression frame))))))))))
221 heller 1.135
222     (defun function-source-location (fun)
223 heller 1.137 (cadr (car (fspec-definition-locations (xref::object-to-function-name fun)))))
224 heller 1.135
225     #+(version>= 8 2)
226     (defun pc-source-location (fun pc)
227     (let* ((debug-info (excl::function-source-debug-info fun)))
228     (cond ((not debug-info)
229     (function-source-location fun))
230     (t
231 heller 1.139 (let* ((code-loc (find-if (lambda (c)
232     (<= (- pc (sys::natural-width))
233     (excl::ldb-code-pc c)
234     pc))
235     debug-info)))
236     (cond ((not code-loc)
237 heller 1.135 (ldb-code-to-src-loc (aref debug-info 0)))
238     (t
239 heller 1.139 (ldb-code-to-src-loc code-loc))))))))
240 heller 1.135
241     #+(version>= 8 2)
242     (defun ldb-code-to-src-loc (code)
243     (let* ((start (excl::ldb-code-start-char code))
244     (func (excl::ldb-code-func code))
245 heller 1.136 (src-file (excl:source-file func)))
246     (cond (start
247     (buffer-or-file-location src-file start))
248 heller 1.135 (t
249     (let* ((debug-info (excl::function-source-debug-info func))
250     (whole (aref debug-info 0))
251     (paths (source-paths-of (excl::ldb-code-source whole)
252     (excl::ldb-code-source code)))
253     (path (longest-common-prefix paths))
254 heller 1.138 (start (excl::ldb-code-start-char whole)))
255     (buffer-or-file
256     src-file
257     (lambda (file)
258     (make-location `(:file ,file)
259     `(:source-path (0 . ,path) ,start)))
260     (lambda (buffer bstart)
261     (make-location `(:buffer ,buffer)
262     `(:source-path (0 . ,path)
263     ,(+ bstart start))))))))))
264    
265 heller 1.135 (defun longest-common-prefix (sequences)
266     (assert sequences)
267     (flet ((common-prefix (s1 s2)
268     (let ((diff-pos (mismatch s1 s2)))
269     (if diff-pos (subseq s1 0 diff-pos) s1))))
270     (reduce #'common-prefix sequences)))
271    
272     (defun source-paths-of (whole part)
273     (let ((result '()))
274     (labels ((walk (form path)
275     (cond ((eq form part)
276     (push (reverse path) result))
277     ((consp form)
278     (loop for i from 0 while (consp form) do
279     (walk (pop form) (cons i path)))))))
280     (walk whole '())
281     (reverse result))))
282 heller 1.4
283 heller 1.10 (defimplementation eval-in-frame (form frame-number)
284 heller 1.73 (let ((frame (nth-frame frame-number)))
285     ;; let-bind lexical variables
286     (let ((vars (loop for i below (debugger:frame-number-vars frame)
287     for name = (debugger:frame-var-name frame i)
288     if (symbolp name)
289     collect `(,name ',(debugger:frame-var-value frame i)))))
290     (debugger:eval-form-in-context
291     `(let* ,vars ,form)
292     (debugger:environment-of-frame frame)))))
293 heller 1.10
294 heller 1.11 (defimplementation return-from-frame (frame-number form)
295     (let ((frame (nth-frame frame-number)))
296     (multiple-value-call #'debugger:frame-return
297     frame (debugger:eval-form-in-context
298 heller 1.20 form
299     (debugger:environment-of-frame frame)))))
300 heller 1.67
301 heller 1.117 (defimplementation frame-restartable-p (frame)
302 heller 1.119 (handler-case (debugger:frame-retryable-p frame)
303     (serious-condition (c)
304     (funcall (read-from-string "swank::background-message")
305     "~a ~a" frame (princ-to-string c))
306     nil)))
307 heller 1.117
308 heller 1.11 (defimplementation restart-frame (frame-number)
309     (let ((frame (nth-frame frame-number)))
310 heller 1.69 (cond ((debugger:frame-retryable-p frame)
311     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
312     (cdr (debugger:frame-expression frame))))
313     (t "Frame is not retryable"))))
314 heller 1.67
315 heller 1.8 ;;;; Compiler hooks
316    
317 heller 1.1 (defvar *buffer-name* nil)
318     (defvar *buffer-start-position*)
319     (defvar *buffer-string*)
320 lgorrie 1.33 (defvar *compile-filename* nil)
321 heller 1.1
322 heller 1.66 (defun compiler-note-p (object)
323     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
324    
325 trittweiler 1.131 (defun redefinition-p (condition)
326     (and (typep condition 'style-warning)
327     (every #'char-equal "redefin" (princ-to-string condition))))
328    
329 heller 1.66 (defun compiler-undefined-functions-called-warning-p (object)
330     (typep object 'excl:compiler-undefined-functions-called-warning))
331 heller 1.64
332     (deftype compiler-note ()
333     `(satisfies compiler-note-p))
334    
335 trittweiler 1.131 (deftype redefinition ()
336     `(satisfies redefinition-p))
337    
338 heller 1.66 (defun signal-compiler-condition (&rest args)
339     (signal (apply #'make-condition 'compiler-condition args)))
340    
341 heller 1.1 (defun handle-compiler-warning (condition)
342 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
343 heller 1.133 (cond ((and (not *buffer-name*)
344 heller 1.66 (compiler-undefined-functions-called-warning-p condition))
345     (handle-undefined-functions-warning condition))
346     (t
347     (signal-compiler-condition
348     :original-condition condition
349     :severity (etypecase condition
350 trittweiler 1.131 (redefinition :redefinition)
351     (style-warning :style-warning)
352     (warning :warning)
353 trittweiler 1.126 (compiler-note :note)
354 trittweiler 1.131 (reader-error :read-error)
355     (error :error))
356 heller 1.66 :message (format nil "~A" condition)
357 trittweiler 1.126 :location (if (typep condition 'reader-error)
358     (location-for-reader-error condition)
359     (location-for-warning condition))))))
360 heller 1.66
361     (defun location-for-warning (condition)
362 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
363 heller 1.66 (cond (*buffer-name*
364     (make-location
365     (list :buffer *buffer-name*)
366 heller 1.114 (list :offset *buffer-start-position* 0)))
367 heller 1.66 (loc
368     (destructuring-bind (file . pos) loc
369 heller 1.133 (let ((start (cond ((consp pos) ; 8.2 and newer
370     (car pos))
371     (t pos))))
372     (make-location
373     (list :file (namestring (truename file)))
374     (list :position (1+ start))))))
375 heller 1.66 (t
376 sboukarev 1.130 (make-error-location "No error location available.")))))
377 heller 1.66
378 trittweiler 1.126 (defun location-for-reader-error (condition)
379     (let ((pos (car (last (slot-value condition 'excl::format-arguments))))
380     (file (pathname (stream-error-stream condition))))
381     (if (integerp pos)
382     (if *buffer-name*
383     (make-location `(:buffer ,*buffer-name*)
384 heller 1.135 `(:offset ,*buffer-start-position* ,pos))
385 trittweiler 1.126 (make-location `(:file ,(namestring (truename file)))
386     `(:position ,pos)))
387 sboukarev 1.130 (make-error-location "No error location available."))))
388 trittweiler 1.126
389 heller 1.133 ;; TODO: report it as a bug to Franz that the condition's plist
390     ;; slot contains (:loc nil).
391 heller 1.66 (defun handle-undefined-functions-warning (condition)
392     (let ((fargs (slot-value condition 'excl::format-arguments)))
393 heller 1.133 (loop for (fname . locs) in (car fargs) do
394     (dolist (loc locs)
395     (multiple-value-bind (pos file) (ecase (length loc)
396     (2 (values-list loc))
397     (3 (destructuring-bind
398     (start end file) loc
399     (declare (ignore end))
400     (values start file))))
401     (signal-compiler-condition
402     :original-condition condition
403     :severity :warning
404     :message (format nil "Undefined function referenced: ~S"
405     fname)
406     :location (make-location (list :file file)
407     (list :position (1+ pos)))))))))
408    
409 lgorrie 1.33 (defimplementation call-with-compilation-hooks (function)
410 trittweiler 1.126 (handler-bind ((warning #'handle-compiler-warning)
411     (compiler-note #'handle-compiler-warning)
412     (reader-error #'handle-compiler-warning))
413 lgorrie 1.33 (funcall function)))
414 heller 1.1
415 heller 1.124 (defimplementation swank-compile-file (input-file output-file
416 sboukarev 1.132 load-p external-format
417     &key policy)
418     (declare (ignore policy))
419 trittweiler 1.126 (handler-case
420     (with-compilation-hooks ()
421     (let ((*buffer-name* nil)
422     (*compile-filename* input-file))
423     (compile-file *compile-filename*
424     :output-file output-file
425     :load-after-compile load-p
426     :external-format external-format)))
427     (reader-error () (values nil nil t))))
428 heller 1.1
429 heller 1.46 (defun call-with-temp-file (fn)
430     (let ((tmpname (system:make-temp-file-name)))
431     (unwind-protect
432     (with-open-file (file tmpname :direction :output :if-exists :error)
433     (funcall fn file tmpname))
434     (delete-file tmpname))))
435    
436 heller 1.135 (defvar *temp-file-map* (make-hash-table :test #'equal)
437     "A mapping from tempfile names to Emacs buffer names.")
438    
439     (defun compile-from-temp-file (string buffer offset file)
440 heller 1.46 (call-with-temp-file
441     (lambda (stream filename)
442 heller 1.135 (let ((excl:*load-source-file-info* t)
443     (sys:*source-file-types* '(nil)) ; suppress .lisp extension
444     #+(version>= 8 2)
445     (compiler:save-source-level-debug-info-switch t)
446     #+(version>= 8 2)
447     (excl:*load-source-debug-info* t) ; NOTE: requires lldb
448     )
449 heller 1.46 (write-string string stream)
450     (finish-output stream)
451 heller 1.116 (multiple-value-bind (binary-filename warnings? failure?)
452 trittweiler 1.126 (excl:without-redefinition-warnings
453 heller 1.116 ;; Suppress Allegro's redefinition warnings; they are
454     ;; pointless when we are compiling via a temporary
455     ;; file.
456     (compile-file filename :load-after-compile t))
457     (declare (ignore warnings?))
458 heller 1.46 (when binary-filename
459 heller 1.135 (setf (gethash (pathname stream) *temp-file-map*)
460     (list buffer offset file))
461 heller 1.116 (delete-file binary-filename))
462 trittweiler 1.126 (not failure?))))))
463    
464     (defimplementation swank-compile-string (string &key buffer position filename
465     policy)
466     (declare (ignore policy))
467     (handler-case
468     (with-compilation-hooks ()
469     (let ((*buffer-name* buffer)
470     (*buffer-start-position* position)
471     (*buffer-string* string)
472     (*default-pathname-defaults*
473     (if filename
474     (merge-pathnames (pathname filename))
475     *default-pathname-defaults*)))
476 heller 1.135 (compile-from-temp-file string buffer position filename)))
477 heller 1.147 (reader-error () nil)))
478 heller 1.46
479 heller 1.8 ;;;; Definition Finding
480    
481 heller 1.137 (defun buffer-or-file (file file-fun buffer-fun)
482 heller 1.135 (let* ((probe (gethash file *temp-file-map*)))
483 heller 1.137 (cond (probe
484 heller 1.135 (destructuring-bind (buffer start file) probe
485     (declare (ignore file))
486 heller 1.137 (funcall buffer-fun buffer start)))
487     (t (funcall file-fun (namestring (truename file)))))))
488    
489     (defun buffer-or-file-location (file offset)
490     (buffer-or-file file
491     (lambda (filename)
492     (make-location `(:file ,filename)
493     `(:position ,(1+ offset))))
494     (lambda (buffer start)
495     (make-location `(:buffer ,buffer)
496     `(:offset ,start ,offset)))))
497 heller 1.135
498 heller 1.32 (defun fspec-primary-name (fspec)
499     (etypecase fspec
500 heller 1.62 (symbol fspec)
501     (list (fspec-primary-name (second fspec)))))
502 heller 1.135
503 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
504     (let* ((part
505     (or (scm::find-definition-in-definition-group
506     fspec type (scm:section-file :file file)
507     :top-level top-level)
508     (scm::find-definition-in-definition-group
509     (fspec-primary-name fspec)
510     type (scm:section-file :file file)
511     :top-level top-level)))
512     (start (and part
513     (scm::source-part-start part)))
514 heller 1.60 (pos (if start
515 heller 1.114 (list :position (1+ start))
516 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
517     (make-location (list :file (namestring (truename file)))
518     pos)))
519 heller 1.60
520 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
521 sboukarev 1.130 (handler-case
522     (etypecase file
523     (pathname
524 heller 1.135 (let ((probe (gethash file *temp-file-map*)))
525     (cond (probe
526     (destructuring-bind (buffer offset file) probe
527     (declare (ignore file))
528     (make-location `(:buffer ,buffer)
529     `(:offset ,offset 0))))
530     (t
531     (find-definition-in-file fspec type file top-level)))))
532 sboukarev 1.130 ((member :top-level)
533 heller 1.135 (make-error-location "Defined at toplevel: ~A" (fspec->string fspec))))
534 sboukarev 1.130 (error (e)
535     (make-error-location "Error: ~A" e))))
536 lgorrie 1.49
537 heller 1.50 (defun fspec->string (fspec)
538 heller 1.135 (typecase fspec
539 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
540     (prin1-to-string fspec)))
541     (list (format nil "(~A ~A)"
542     (prin1-to-string (first fspec))
543     (let ((*package* (find-package :keyword)))
544 heller 1.135 (prin1-to-string (second fspec)))))
545     (t (princ-to-string fspec))))
546 heller 1.21
547     (defun fspec-definition-locations (fspec)
548 mkoeppe 1.87 (cond
549 sboukarev 1.130 ((and (listp fspec)
550     (eql (car fspec) :top-level-form))
551 sboukarev 1.142 (destructuring-bind (top-level-form file &optional (position 0)) fspec
552 sboukarev 1.130 (declare (ignore top-level-form))
553 heller 1.137 `((,fspec
554     ,(buffer-or-file-location file position)))))
555 sboukarev 1.130 ((and (listp fspec) (eq (car fspec) :internal))
556     (destructuring-bind (_internal next _n) fspec
557     (declare (ignore _internal _n))
558     (fspec-definition-locations next)))
559     (t
560     (let ((defs (excl::find-source-file fspec)))
561     (when (and (null defs)
562     (listp fspec)
563     (string= (car fspec) '#:method))
564     ;; If methods are defined in a defgeneric form, the source location is
565     ;; recorded for the gf but not for the methods. Therefore fall back to
566     ;; the gf as the likely place of definition.
567     (setq defs (excl::find-source-file (second fspec))))
568     (if (null defs)
569     (list
570     (list fspec
571     (make-error-location "Unknown source location for ~A"
572     (fspec->string fspec))))
573 heller 1.133 (loop for (fspec type file top-level) in defs collect
574     (list (list type fspec)
575     (find-fspec-location fspec type file top-level))))))))
576 heller 1.21
577     (defimplementation find-definitions (symbol)
578     (fspec-definition-locations symbol))
579 heller 1.1
580 heller 1.8 ;;;; XREF
581    
582 heller 1.21 (defmacro defxref (name relation name1 name2)
583     `(defimplementation ,name (x)
584     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
585    
586     (defxref who-calls :calls :wild x)
587 heller 1.70 (defxref calls-who :calls x :wild)
588 heller 1.21 (defxref who-references :uses :wild x)
589     (defxref who-binds :binds :wild x)
590     (defxref who-macroexpands :macro-calls :wild x)
591     (defxref who-sets :sets :wild x)
592    
593     (defun xref-result (fspecs)
594     (loop for fspec in fspecs
595     append (fspec-definition-locations fspec)))
596 heller 1.46
597     ;; list-callers implemented by groveling through all fbound symbols.
598     ;; Only symbols are considered. Functions in the constant pool are
599 heller 1.71 ;; searched recursively. Closure environments are ignored at the
600 heller 1.46 ;; moment (constants in methods are therefore not found).
601    
602     (defun map-function-constants (function fn depth)
603     "Call FN with the elements of FUNCTION's constant pool."
604     (do ((i 0 (1+ i))
605     (max (excl::function-constant-count function)))
606     ((= i max))
607     (let ((c (excl::function-constant function i)))
608     (cond ((and (functionp c)
609     (not (eq c function))
610     (plusp depth))
611     (map-function-constants c fn (1- depth)))
612     (t
613     (funcall fn c))))))
614    
615 heller 1.64 (defun in-constants-p (fun symbol)
616     (map-function-constants fun
617     (lambda (c)
618     (when (eq c symbol)
619     (return-from in-constants-p t)))
620     3))
621 heller 1.70
622 heller 1.46 (defun function-callers (name)
623     (let ((callers '()))
624     (do-all-symbols (sym)
625     (when (fboundp sym)
626     (let ((fn (fdefinition sym)))
627     (when (in-constants-p fn name)
628     (push sym callers)))))
629     callers))
630    
631     (defimplementation list-callers (name)
632     (xref-result (function-callers name)))
633 heller 1.4
634 heller 1.70 (defimplementation list-callees (name)
635     (let ((result '()))
636     (map-function-constants (fdefinition name)
637     (lambda (c)
638     (when (fboundp c)
639     (push c result)))
640     2)
641     (xref-result result)))
642    
643 mkoeppe 1.83 ;;;; Profiling
644    
645 heller 1.89 ;; Per-function profiling based on description in
646     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
647    
648     (defvar *profiled-functions* ())
649     (defvar *profile-depth* 0)
650    
651     (defmacro with-redirected-y-or-n-p (&body body)
652     ;; If the profiler is restarted when the data from the previous
653     ;; session is not reported yet, the user is warned via Y-OR-N-P.
654     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
655     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
656     ;; overruled.
657 sboukarev 1.144 `(let* ((pkg (find-package :common-lisp))
658 heller 1.89 (saved-pdl (excl::package-definition-lock pkg))
659     (saved-ynp (symbol-function 'cl:y-or-n-p)))
660     (setf (excl::package-definition-lock pkg) nil
661 sboukarev 1.144 (symbol-function 'cl:y-or-n-p)
662     (symbol-function (read-from-string "swank:y-or-n-p-in-emacs")))
663 heller 1.89 (unwind-protect
664 sboukarev 1.144 (progn ,@body)
665 heller 1.89 (setf (symbol-function 'cl:y-or-n-p) saved-ynp
666     (excl::package-definition-lock pkg) saved-pdl))))
667    
668     (defun start-acl-profiler ()
669     (with-redirected-y-or-n-p
670     (prof:start-profiler :type :time :count t
671     :start-sampling-p nil :verbose nil)))
672     (defun acl-profiler-active-p ()
673     (not (eq (prof:profiler-status :verbose nil) :inactive)))
674    
675     (defun stop-acl-profiler ()
676     (prof:stop-profiler :verbose nil))
677    
678     (excl:def-fwrapper profile-fwrapper (&rest args)
679     ;; Ensures sampling is done during the execution of the function,
680     ;; taking into account recursion.
681     (declare (ignore args))
682     (cond ((zerop *profile-depth*)
683     (let ((*profile-depth* (1+ *profile-depth*)))
684     (prof:start-sampling)
685     (unwind-protect (excl:call-next-fwrapper)
686     (prof:stop-sampling))))
687     (t
688     (excl:call-next-fwrapper))))
689    
690     (defimplementation profile (fname)
691     (unless (acl-profiler-active-p)
692     (start-acl-profiler))
693     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
694     (push fname *profiled-functions*))
695    
696     (defimplementation profiled-functions ()
697     *profiled-functions*)
698    
699     (defimplementation unprofile (fname)
700     (excl:funwrap fname 'profile-fwrapper)
701     (setq *profiled-functions* (remove fname *profiled-functions*)))
702    
703 mkoeppe 1.83 (defimplementation profile-report ()
704 heller 1.89 (prof:show-flat-profile :verbose nil)
705     (when *profiled-functions*
706     (start-acl-profiler)))
707    
708     (defimplementation profile-reset ()
709     (when (acl-profiler-active-p)
710     (stop-acl-profiler)
711     (start-acl-profiler))
712     "Reset profiling counters.")
713 mkoeppe 1.83
714 heller 1.18 ;;;; Inspecting
715    
716 heller 1.111 (excl:without-redefinition-warnings
717 heller 1.100 (defmethod emacs-inspect ((o t))
718 heller 1.111 (allegro-inspect o)))
719 heller 1.62
720 heller 1.100 (defmethod emacs-inspect ((o function))
721 heller 1.101 (allegro-inspect o))
722 heller 1.62
723 heller 1.100 (defmethod emacs-inspect ((o standard-object))
724 heller 1.101 (allegro-inspect o))
725 heller 1.82
726 heller 1.62 (defun allegro-inspect (o)
727     (loop for (d dd) on (inspect::inspect-ctl o)
728 heller 1.73 append (frob-allegro-field-def o d)
729     until (eq d dd)))
730 heller 1.62
731 heller 1.73 (defun frob-allegro-field-def (object def)
732 heller 1.62 (with-struct (inspect::field-def- name type access) def
733 heller 1.73 (ecase type
734 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
735 heller 1.145 :unsigned-long :unsigned-half-long
736     :unsigned-3byte :unsigned-long32)
737 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
738 heller 1.115 ((:lisp :value :func)
739 heller 1.73 (label-value-line name (inspect::component-ref object access)))
740     (:indirect
741     (destructuring-bind (prefix count ref set) access
742     (declare (ignore set prefix))
743     (loop for i below (funcall count object)
744     append (label-value-line (format nil "~A-~D" name i)
745     (funcall ref object i))))))))
746 mbaringer 1.55
747 heller 1.18 ;;;; Multithreading
748 heller 1.8
749 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
750     (mp:start-scheduler)
751     (funcall continuation))
752 heller 1.8
753 heller 1.10 (defimplementation spawn (fn &key name)
754 lgorrie 1.47 (mp:process-run-function name fn))
755 heller 1.8
756 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
757     (defvar *thread-id-counter* 0)
758    
759     (defimplementation thread-id (thread)
760     (mp:with-process-lock (*id-lock*)
761     (or (getf (mp:process-property-list thread) 'id)
762     (setf (getf (mp:process-property-list thread) 'id)
763     (incf *thread-id-counter*)))))
764    
765     (defimplementation find-thread (id)
766     (find id mp:*all-processes*
767     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
768    
769 heller 1.13 (defimplementation thread-name (thread)
770     (mp:process-name thread))
771 heller 1.8
772 heller 1.13 (defimplementation thread-status (thread)
773 sboukarev 1.140 (princ-to-string (mp:process-whostate thread)))
774    
775     (defimplementation thread-attributes (thread)
776     (list :priority (mp:process-priority thread)
777     :times-resumed (mp:process-times-resumed thread)))
778 heller 1.8
779 heller 1.10 (defimplementation make-lock (&key name)
780 heller 1.8 (mp:make-process-lock :name name))
781    
782 heller 1.10 (defimplementation call-with-lock-held (lock function)
783 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
784 heller 1.12
785     (defimplementation current-thread ()
786     mp:*current-process*)
787    
788     (defimplementation all-threads ()
789 heller 1.13 (copy-list mp:*all-processes*))
790 heller 1.12
791     (defimplementation interrupt-thread (thread fn)
792     (mp:process-interrupt thread fn))
793    
794 heller 1.16 (defimplementation kill-thread (thread)
795     (mp:process-kill thread))
796    
797 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
798    
799     (defstruct (mailbox (:conc-name mailbox.))
800 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
801     (queue '() :type list)
802 heller 1.108 (gate (mp:make-gate nil)))
803 heller 1.12
804     (defun mailbox (thread)
805     "Return THREAD's mailbox."
806     (mp:with-process-lock (*mailbox-lock*)
807     (or (getf (mp:process-property-list thread) 'mailbox)
808     (setf (getf (mp:process-property-list thread) 'mailbox)
809     (make-mailbox)))))
810    
811     (defimplementation send (thread message)
812 heller 1.107 (let* ((mbox (mailbox thread)))
813     (mp:with-process-lock ((mailbox.lock mbox))
814     (setf (mailbox.queue mbox)
815     (nconc (mailbox.queue mbox) (list message)))
816     (mp:open-gate (mailbox.gate mbox)))))
817 heller 1.12
818 heller 1.110 (defimplementation receive-if (test &optional timeout)
819 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
820 heller 1.110 (assert (or (not timeout) (eq timeout t)))
821 heller 1.107 (loop
822     (check-slime-interrupts)
823     (mp:with-process-lock ((mailbox.lock mbox))
824     (let* ((q (mailbox.queue mbox))
825     (tail (member-if test q)))
826     (when tail
827     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
828     (return (car tail)))
829     (mp:close-gate (mailbox.gate mbox))))
830 heller 1.110 (when (eq timeout t) (return (values nil t)))
831     (mp:process-wait-with-timeout "receive-if" 0.5
832     #'mp:gate-open-p (mailbox.gate mbox)))))
833 heller 1.104
834 heller 1.150 (let ((alist '())
835     (lock (mp:make-process-lock :name "register-thread")))
836    
837     (defimplementation register-thread (name thread)
838     (declare (type symbol name))
839     (mp:with-process-lock (lock)
840     (etypecase thread
841     (null
842     (setf alist (delete name alist :key #'car)))
843     (mp:process
844     (let ((probe (assoc name alist)))
845     (cond (probe (setf (cdr probe) thread))
846     (t (setf alist (acons name thread alist))))))))
847     nil)
848    
849     (defimplementation find-registered (name)
850     (mp:with-process-lock (lock)
851     (cdr (assoc name alist)))))
852    
853 heller 1.123 (defimplementation set-default-initial-binding (var form)
854 heller 1.149 (push (cons var form)
855     #+(version>= 9 0)
856     excl:*required-thread-bindings*
857     #-(version>= 9 0)
858     excl::required-thread-bindings))
859 heller 1.123
860 mbaringer 1.27 (defimplementation quit-lisp ()
861     (excl:exit 0 :quiet t))
862 mbaringer 1.68
863 heller 1.69
864 mbaringer 1.68 ;;Trace implementations
865     ;;In Allegro 7.0, we have:
866     ;; (trace <name>)
867     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
868     ;; (trace ((labels <name> <label-name>)))
869     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
870     ;; <name> can be a normal name or a (setf name)
871    
872 heller 1.70 (defimplementation toggle-trace (spec)
873 lgorrie 1.72 (ecase (car spec)
874     ((setf)
875     (toggle-trace-aux spec))
876 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
877 heller 1.71 ((setf :defmethod :labels :flet)
878 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
879 lgorrie 1.72 (:call
880 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
881     (toggle-trace-aux callee
882     :inside (list (process-fspec-for-allegro caller)))))))
883    
884     (defun tracedp (fspec)
885 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
886 heller 1.70
887     (defun toggle-trace-aux (fspec &rest args)
888     (cond ((tracedp fspec)
889     (eval `(untrace ,fspec))
890     (format nil "~S is now untraced." fspec))
891     (t
892     (eval `(trace (,fspec ,@args)))
893     (format nil "~S is now traced." fspec))))
894    
895     (defun toggle-trace-generic-function-methods (name)
896 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
897 heller 1.70 (cond ((tracedp name)
898 mbaringer 1.68 (eval `(untrace ,name))
899     (dolist (method methods (format nil "~S is now untraced." name))
900     (excl:funtrace (mop:method-function method))))
901     (t
902 lgorrie 1.72 (eval `(trace (,name)))
903 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
904 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
905    
906     (defun process-fspec-for-allegro (fspec)
907     (cond ((consp fspec)
908     (ecase (first fspec)
909 heller 1.71 ((setf) fspec)
910 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
911     ((:defmethod) `(method ,@(rest fspec)))
912 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
913     ,(third fspec)))
914     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
915     ,(third fspec)))))
916 mbaringer 1.68 (t
917     fspec)))
918 heller 1.88
919    
920     ;;;; Weak hashtables
921    
922     (defimplementation make-weak-key-hash-table (&rest args)
923     (apply #'make-hash-table :weak-keys t args))
924    
925     (defimplementation make-weak-value-hash-table (&rest args)
926     (apply #'make-hash-table :values :weak args))
927 mkoeppe 1.93
928 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
929     (cond ((excl:hash-table-weak-keys hashtable) :key)
930     ((eq (excl:hash-table-values hashtable) :weak) :value)))
931    
932    
933 mkoeppe 1.93
934     ;;;; Character names
935    
936     (defimplementation character-completion-set (prefix matchp)
937     (loop for name being the hash-keys of excl::*name-to-char-table*
938     when (funcall matchp prefix name)
939     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5