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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.114 - (hide annotations)
Wed Sep 17 06:19:48 2008 UTC (5 years, 7 months ago) by heller
Branch: MAIN
Changes since 1.113: +6 -5 lines
Adjust positions in files with CRLF-style end-on-line markers.

* slime.el (slime-eol-conversion-fixup): New function.
(slime-goto-location-position): Use it.  Also add a new position
type :offset, so that we don't adjust offsets in strings that were
sent over the wire (which uses LF eol-convention).

* swank-abcl.lisp
* swank-allegro.lisp
* swank-clisp.lisp
* swank-cmucl.lisp
* swank-corman.lisp
* swank-ecl.lisp
* swank-lispworks.lisp
* swank-openmcl.lisp
* swank-sbcl.lisp
* swank-scl.lisp: Create :offset style positions where needed.

* swank-lispworks.lisp (skip-comments): New function.
(dspec-stream-position): Use it.
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.79 (require :process))
16 heller 1.1
17 heller 1.79 (import-from :excl *gray-stream-symbols* :swank-backend)
18 heller 1.1
19 mbaringer 1.53 ;;; swank-mop
20    
21 heller 1.60 (import-swank-mop-symbols :clos '(:slot-definition-documentation))
22 mbaringer 1.53
23 mbaringer 1.54 (defun swank-mop:slot-definition-documentation (slot)
24 heller 1.62 (documentation slot t))
25 mbaringer 1.53
26 heller 1.79
27 heller 1.8 ;;;; TCP Server
28 heller 1.1
29 heller 1.21 (defimplementation preferred-communication-style ()
30     :spawn)
31 heller 1.12
32 heller 1.14 (defimplementation create-socket (host port)
33     (socket:make-socket :connect :passive :local-port port
34     :local-host host :reuse-address t))
35 heller 1.5
36 heller 1.10 (defimplementation local-port (socket)
37 heller 1.6 (socket:local-port socket))
38    
39 heller 1.10 (defimplementation close-socket (socket)
40 heller 1.6 (close socket))
41    
42 dcrosher 1.85 (defimplementation accept-connection (socket &key external-format buffering
43     timeout)
44     (declare (ignore buffering timeout))
45 heller 1.94 (let ((s (socket:accept-connection socket :wait t)))
46     (when external-format
47     (setf (stream-external-format s) external-format))
48 heller 1.63 s))
49 heller 1.6
50 heller 1.94 (defvar *external-format-to-coding-system*
51     '((:iso-8859-1
52     "latin-1" "latin-1-unix" "iso-latin-1-unix"
53     "iso-8859-1" "iso-8859-1-unix")
54     (:utf-8 "utf-8" "utf-8-unix")
55     (:euc-jp "euc-jp" "euc-jp-unix")
56     (:us-ascii "us-ascii" "us-ascii-unix")
57     (:emacs-mule "emacs-mule" "emacs-mule-unix")))
58    
59     (defimplementation find-external-format (coding-system)
60     (let ((e (rassoc-if (lambda (x) (member coding-system x :test #'equal))
61     *external-format-to-coding-system*)))
62     (and e (excl:crlf-base-ef
63     (excl:find-external-format (car e)
64     :try-variant t)))))
65 heller 1.64
66 heller 1.34 (defimplementation format-sldb-condition (c)
67     (princ-to-string c))
68    
69 heller 1.39 (defimplementation call-with-syntax-hooks (fn)
70     (funcall fn))
71    
72 heller 1.9 ;;;; Unix signals
73    
74 heller 1.10 (defimplementation call-without-interrupts (fn)
75 heller 1.9 (excl:without-interrupts (funcall fn)))
76    
77 heller 1.10 (defimplementation getpid ()
78 heller 1.8 (excl::getpid))
79 heller 1.6
80 heller 1.15 (defimplementation lisp-implementation-type-name ()
81     "allegro")
82    
83 pseibel 1.28 (defimplementation set-default-directory (directory)
84 mkoeppe 1.86 (let* ((dir (namestring (truename (merge-pathnames directory)))))
85     (setf *default-pathname-defaults* (pathname (excl:chdir dir)))
86 heller 1.66 dir))
87 pseibel 1.28
88 heller 1.35 (defimplementation default-directory ()
89 heller 1.66 (namestring (excl:current-directory)))
90 heller 1.35
91 heller 1.8 ;;;; Misc
92 heller 1.1
93 heller 1.21 (defimplementation arglist (symbol)
94 lgorrie 1.30 (handler-case (excl:arglist symbol)
95     (simple-error () :not-available)))
96 heller 1.21
97     (defimplementation macroexpand-all (form)
98     (excl::walk form))
99 heller 1.1
100 heller 1.10 (defimplementation describe-symbol-for-emacs (symbol)
101 heller 1.1 (let ((result '()))
102     (flet ((doc (kind &optional (sym symbol))
103     (or (documentation sym kind) :not-documented))
104     (maybe-push (property value)
105     (when value
106     (setf result (list* property value result)))))
107     (maybe-push
108     :variable (when (boundp symbol)
109     (doc 'variable)))
110     (maybe-push
111     :function (if (fboundp symbol)
112     (doc 'function)))
113     (maybe-push
114     :class (if (find-class symbol nil)
115     (doc 'class)))
116     result)))
117    
118 heller 1.20 (defimplementation describe-definition (symbol namespace)
119     (ecase namespace
120     (:variable
121     (describe symbol))
122     ((:function :generic-function)
123     (describe (symbol-function symbol)))
124     (:class
125     (describe (find-class symbol)))))
126 heller 1.10
127 heller 1.8 ;;;; Debugger
128    
129 heller 1.1 (defvar *sldb-topframe*)
130 heller 1.4
131 heller 1.10 (defimplementation call-with-debugging-environment (debugger-loop-fn)
132 heller 1.71 (let ((*sldb-topframe* (find-topframe))
133 heller 1.20 (excl::*break-hook* nil))
134 heller 1.4 (funcall debugger-loop-fn)))
135 heller 1.1
136 mkoeppe 1.92 (defimplementation sldb-break-at-start (fname)
137     ;; :print-before is kind of mis-used but we just want to stuff our break form
138     ;; somewhere. This does not work for setf, :before and :after methods, which
139     ;; need special syntax in the trace call, see ACL's doc/debugging.htm chapter 10.
140     (eval `(trace (,fname
141     :print-before
142     ((break "Function start breakpoint of ~A" ',fname)))))
143     `(:ok ,(format nil "Set breakpoint at start of ~S" fname)))
144    
145 heller 1.71 (defun find-topframe ()
146 heller 1.73 (let ((skip-frames 3))
147     (do ((f (excl::int-newest-frame) (next-frame f))
148     (i 0 (1+ i)))
149     ((= i skip-frames) f))))
150 heller 1.71
151 heller 1.42 (defun next-frame (frame)
152     (let ((next (excl::int-next-older-frame frame)))
153     (cond ((not next) nil)
154     ((debugger:frame-visible-p next) next)
155     (t (next-frame next)))))
156    
157 heller 1.1 (defun nth-frame (index)
158 heller 1.42 (do ((frame *sldb-topframe* (next-frame frame))
159 heller 1.1 (i index (1- i)))
160     ((zerop i) frame)))
161    
162 heller 1.20 (defimplementation compute-backtrace (start end)
163 heller 1.1 (let ((end (or end most-positive-fixnum)))
164 heller 1.42 (loop for f = (nth-frame start) then (next-frame f)
165 heller 1.1 for i from start below end
166 trittweiler 1.113 while f collect (make-swank-frame :%frame f :restartable :unknown))))
167 heller 1.1
168 trittweiler 1.113 (defimplementation print-swank-frame (frame stream)
169     (debugger:output-frame stream (swank-frame.%frame frame) :moderate))
170 heller 1.4
171 heller 1.10 (defimplementation frame-locals (index)
172 heller 1.1 (let ((frame (nth-frame index)))
173     (loop for i from 0 below (debugger:frame-number-vars frame)
174 mbaringer 1.19 collect (list :name (debugger:frame-var-name frame i)
175 heller 1.1 :id 0
176 mbaringer 1.19 :value (debugger:frame-var-value frame i)))))
177 heller 1.1
178 heller 1.39 (defimplementation frame-var-value (frame var)
179     (let ((frame (nth-frame frame)))
180     (debugger:frame-var-value frame var)))
181    
182 heller 1.10 (defimplementation frame-catch-tags (index)
183 heller 1.1 (declare (ignore index))
184     nil)
185    
186 heller 1.21 (defimplementation disassemble-frame (index)
187     (disassemble (debugger:frame-function (nth-frame index))))
188    
189 heller 1.10 (defimplementation frame-source-location-for-emacs (index)
190 lgorrie 1.37 (let* ((frame (nth-frame index))
191     (expr (debugger:frame-expression frame))
192     (fspec (first expr)))
193     (second (first (fspec-definition-locations fspec)))))
194 heller 1.4
195 heller 1.10 (defimplementation eval-in-frame (form frame-number)
196 heller 1.73 (let ((frame (nth-frame frame-number)))
197     ;; let-bind lexical variables
198     (let ((vars (loop for i below (debugger:frame-number-vars frame)
199     for name = (debugger:frame-var-name frame i)
200     if (symbolp name)
201     collect `(,name ',(debugger:frame-var-value frame i)))))
202     (debugger:eval-form-in-context
203     `(let* ,vars ,form)
204     (debugger:environment-of-frame frame)))))
205 heller 1.10
206 heller 1.11 (defimplementation return-from-frame (frame-number form)
207     (let ((frame (nth-frame frame-number)))
208     (multiple-value-call #'debugger:frame-return
209     frame (debugger:eval-form-in-context
210 heller 1.20 form
211     (debugger:environment-of-frame frame)))))
212 heller 1.67
213 heller 1.11 (defimplementation restart-frame (frame-number)
214     (let ((frame (nth-frame frame-number)))
215 heller 1.69 (cond ((debugger:frame-retryable-p frame)
216     (apply #'debugger:frame-retry frame (debugger:frame-function frame)
217     (cdr (debugger:frame-expression frame))))
218     (t "Frame is not retryable"))))
219 heller 1.67
220 heller 1.8 ;;;; Compiler hooks
221    
222 heller 1.1 (defvar *buffer-name* nil)
223     (defvar *buffer-start-position*)
224     (defvar *buffer-string*)
225 lgorrie 1.33 (defvar *compile-filename* nil)
226 heller 1.1
227 heller 1.66 (defun compiler-note-p (object)
228     (member (type-of object) '(excl::compiler-note compiler::compiler-note)))
229    
230     (defun compiler-undefined-functions-called-warning-p (object)
231     (typep object 'excl:compiler-undefined-functions-called-warning))
232 heller 1.64
233     (deftype compiler-note ()
234     `(satisfies compiler-note-p))
235    
236 heller 1.66 (defun signal-compiler-condition (&rest args)
237     (signal (apply #'make-condition 'compiler-condition args)))
238    
239 heller 1.1 (defun handle-compiler-warning (condition)
240 heller 1.66 (declare (optimize (debug 3) (speed 0) (space 0)))
241     (cond ((and (not *buffer-name*)
242     (compiler-undefined-functions-called-warning-p condition))
243     (handle-undefined-functions-warning condition))
244     (t
245     (signal-compiler-condition
246     :original-condition condition
247     :severity (etypecase condition
248     (warning :warning)
249     (compiler-note :note))
250     :message (format nil "~A" condition)
251     :location (location-for-warning condition)))))
252    
253     (defun location-for-warning (condition)
254 heller 1.1 (let ((loc (getf (slot-value condition 'excl::plist) :loc)))
255 heller 1.66 (cond (*buffer-name*
256     (make-location
257     (list :buffer *buffer-name*)
258 heller 1.114 (list :offset *buffer-start-position* 0)))
259 heller 1.66 (loc
260     (destructuring-bind (file . pos) loc
261     (make-location
262     (list :file (namestring (truename file)))
263     (list :position (1+ pos)))))
264     (t
265     (list :error "No error location available.")))))
266    
267     (defun handle-undefined-functions-warning (condition)
268     (let ((fargs (slot-value condition 'excl::format-arguments)))
269 heller 1.67 (loop for (fname . pos-file) in (car fargs) do
270     (loop for (pos file) in pos-file do
271     (signal-compiler-condition
272     :original-condition condition
273     :severity :warning
274     :message (format nil "Undefined function referenced: ~S"
275     fname)
276     :location (make-location (list :file file)
277     (list :position (1+ pos))))))))
278 lgorrie 1.33
279     (defimplementation call-with-compilation-hooks (function)
280 heller 1.64 (handler-bind ((warning #'handle-compiler-warning)
281 heller 1.70 ;;(compiler-note #'handle-compiler-warning)
282     )
283 lgorrie 1.33 (funcall function)))
284 heller 1.1
285 heller 1.94 (defimplementation swank-compile-file (filename load-p external-format)
286 lgorrie 1.33 (with-compilation-hooks ()
287 heller 1.74 (let ((*buffer-name* nil)
288 heller 1.94 (*compile-filename* filename))
289 heller 1.74 (compile-file *compile-filename* :load-after-compile load-p
290 heller 1.94 :external-format external-format))))
291 heller 1.1
292 heller 1.46 (defun call-with-temp-file (fn)
293     (let ((tmpname (system:make-temp-file-name)))
294     (unwind-protect
295     (with-open-file (file tmpname :direction :output :if-exists :error)
296     (funcall fn file tmpname))
297     (delete-file tmpname))))
298    
299     (defun compile-from-temp-file (string)
300     (call-with-temp-file
301     (lambda (stream filename)
302     (write-string string stream)
303     (finish-output stream)
304 mkoeppe 1.83 (let ((binary-filename
305     (excl:without-redefinition-warnings
306     ;; Suppress Allegro's redefinition warnings; they are
307     ;; pointless when we are compiling via a temporary
308     ;; file.
309     (compile-file filename :load-after-compile t))))
310 heller 1.46 (when binary-filename
311     (delete-file binary-filename))))))
312    
313 heller 1.102 (defimplementation swank-compile-string (string &key buffer position directory
314     debug)
315     (declare (ignore debug))
316 heller 1.46 ;; We store the source buffer in excl::*source-pathname* as a string
317 heller 1.50 ;; of the form <buffername>;<start-offset>. Quite ugly encoding, but
318 heller 1.46 ;; the fasl file is corrupted if we use some other datatype.
319 lgorrie 1.33 (with-compilation-hooks ()
320 heller 1.20 (let ((*buffer-name* buffer)
321 heller 1.1 (*buffer-start-position* position)
322 pseibel 1.51 (*buffer-string* string)
323     (*default-pathname-defaults*
324     (if directory (merge-pathnames (pathname directory))
325     *default-pathname-defaults*)))
326 heller 1.46 (compile-from-temp-file
327     (format nil "~S ~S~%~A"
328     `(in-package ,(package-name *package*))
329     `(eval-when (:compile-toplevel :load-toplevel)
330     (setq excl::*source-pathname*
331 heller 1.52 ',(format nil "~A;~D" buffer position)))
332 heller 1.46 string)))))
333 heller 1.1
334 heller 1.8 ;;;; Definition Finding
335    
336 heller 1.32 (defun fspec-primary-name (fspec)
337     (etypecase fspec
338 heller 1.62 (symbol fspec)
339     (list (fspec-primary-name (second fspec)))))
340 heller 1.32
341 heller 1.62 ;; If Emacs uses DOS-style eol conventions, \n\r are considered as a
342     ;; single character, but file-position counts them as two. Here we do
343     ;; our own conversion.
344     (defun count-cr (file pos)
345     (let* ((bufsize 256)
346 heller 1.63 (type '(unsigned-byte 8))
347     (buf (make-array bufsize :element-type type))
348 heller 1.62 (cr-count 0))
349 heller 1.63 (with-open-file (stream file :direction :input :element-type type)
350     (loop for bytes-read = (read-sequence buf stream) do
351     (incf cr-count (count (char-code #\return) buf
352     :end (min pos bytes-read)))
353     (decf pos bytes-read)
354     (when (<= pos 0)
355     (return cr-count))))))
356 heller 1.62
357 mkoeppe 1.81 (defun find-definition-in-file (fspec type file top-level)
358     (let* ((part
359     (or (scm::find-definition-in-definition-group
360     fspec type (scm:section-file :file file)
361     :top-level top-level)
362     (scm::find-definition-in-definition-group
363     (fspec-primary-name fspec)
364     type (scm:section-file :file file)
365     :top-level top-level)))
366     (start (and part
367     (scm::source-part-start part)))
368 heller 1.60 (pos (if start
369 heller 1.114 (list :position (1+ start))
370 heller 1.62 (list :function-name (string (fspec-primary-name fspec))))))
371     (make-location (list :file (namestring (truename file)))
372     pos)))
373 heller 1.60
374     (defun find-definition-in-buffer (filename)
375     (let ((pos (position #\; filename :from-end t)))
376     (make-location
377     (list :buffer (subseq filename 0 pos))
378 heller 1.114 (list :offset (parse-integer (subseq filename (1+ pos))) 0))))
379 heller 1.60
380 mkoeppe 1.81 (defun find-fspec-location (fspec type file top-level)
381     (etypecase file
382     (pathname
383     (find-definition-in-file fspec type file top-level))
384     ((member :top-level)
385     (list :error (format nil "Defined at toplevel: ~A"
386     (fspec->string fspec))))
387     (string
388     (find-definition-in-buffer file))))
389 lgorrie 1.49
390 heller 1.50 (defun fspec->string (fspec)
391 lgorrie 1.49 (etypecase fspec
392 heller 1.50 (symbol (let ((*package* (find-package :keyword)))
393     (prin1-to-string fspec)))
394     (list (format nil "(~A ~A)"
395     (prin1-to-string (first fspec))
396     (let ((*package* (find-package :keyword)))
397     (prin1-to-string (second fspec)))))))
398 heller 1.21
399     (defun fspec-definition-locations (fspec)
400 mkoeppe 1.87 (cond
401     ((and (listp fspec)
402     (eql (car fspec) :top-level-form))
403 mkoeppe 1.98 (destructuring-bind (top-level-form file &optional position) fspec
404 heller 1.111 (declare (ignore top-level-form))
405 mkoeppe 1.87 (list
406     (list (list nil fspec)
407 heller 1.114 (make-location (list :buffer file) ; FIXME: should use :file
408     (list :position position)
409     (list :align t))))))
410 heller 1.90 ((and (listp fspec) (eq (car fspec) :internal))
411     (destructuring-bind (_internal next _n) fspec
412 heller 1.111 (declare (ignore _internal _n))
413 heller 1.90 (fspec-definition-locations next)))
414 mkoeppe 1.87 (t
415     (let ((defs (excl::find-source-file fspec)))
416 trittweiler 1.103 (when (and (null defs)
417     (listp fspec)
418     (string= (car fspec) '#:method))
419     ;; If methods are defined in a defgeneric form, the source location is
420     ;; recorded for the gf but not for the methods. Therefore fall back to
421     ;; the gf as the likely place of definition.
422     (setq defs (excl::find-source-file (second fspec))))
423 mkoeppe 1.87 (if (null defs)
424     (list
425     (list (list nil fspec)
426     (list :error
427     (format nil "Unknown source location for ~A"
428     (fspec->string fspec)))))
429 mkoeppe 1.81 (loop for (fspec type file top-level) in defs
430 mkoeppe 1.87 collect (list (list type fspec)
431     (find-fspec-location fspec type file top-level))))))))
432 heller 1.21
433     (defimplementation find-definitions (symbol)
434     (fspec-definition-locations symbol))
435 heller 1.1
436 heller 1.8 ;;;; XREF
437    
438 heller 1.21 (defmacro defxref (name relation name1 name2)
439     `(defimplementation ,name (x)
440     (xref-result (xref:get-relation ,relation ,name1 ,name2))))
441    
442     (defxref who-calls :calls :wild x)
443 heller 1.70 (defxref calls-who :calls x :wild)
444 heller 1.21 (defxref who-references :uses :wild x)
445     (defxref who-binds :binds :wild x)
446     (defxref who-macroexpands :macro-calls :wild x)
447     (defxref who-sets :sets :wild x)
448    
449     (defun xref-result (fspecs)
450     (loop for fspec in fspecs
451     append (fspec-definition-locations fspec)))
452 heller 1.46
453     ;; list-callers implemented by groveling through all fbound symbols.
454     ;; Only symbols are considered. Functions in the constant pool are
455 heller 1.71 ;; searched recursively. Closure environments are ignored at the
456 heller 1.46 ;; moment (constants in methods are therefore not found).
457    
458     (defun map-function-constants (function fn depth)
459     "Call FN with the elements of FUNCTION's constant pool."
460     (do ((i 0 (1+ i))
461     (max (excl::function-constant-count function)))
462     ((= i max))
463     (let ((c (excl::function-constant function i)))
464     (cond ((and (functionp c)
465     (not (eq c function))
466     (plusp depth))
467     (map-function-constants c fn (1- depth)))
468     (t
469     (funcall fn c))))))
470    
471 heller 1.64 (defun in-constants-p (fun symbol)
472     (map-function-constants fun
473     (lambda (c)
474     (when (eq c symbol)
475     (return-from in-constants-p t)))
476     3))
477 heller 1.70
478 heller 1.46 (defun function-callers (name)
479     (let ((callers '()))
480     (do-all-symbols (sym)
481     (when (fboundp sym)
482     (let ((fn (fdefinition sym)))
483     (when (in-constants-p fn name)
484     (push sym callers)))))
485     callers))
486    
487     (defimplementation list-callers (name)
488     (xref-result (function-callers name)))
489 heller 1.4
490 heller 1.70 (defimplementation list-callees (name)
491     (let ((result '()))
492     (map-function-constants (fdefinition name)
493     (lambda (c)
494     (when (fboundp c)
495     (push c result)))
496     2)
497     (xref-result result)))
498    
499 mkoeppe 1.83 ;;;; Profiling
500    
501 heller 1.89 ;; Per-function profiling based on description in
502     ;; http://www.franz.com/support/documentation/8.0/doc/runtime-analyzer.htm#data-collection-control-2
503    
504     (defvar *profiled-functions* ())
505     (defvar *profile-depth* 0)
506    
507     (defmacro with-redirected-y-or-n-p (&body body)
508     ;; If the profiler is restarted when the data from the previous
509     ;; session is not reported yet, the user is warned via Y-OR-N-P.
510     ;; As the CL:Y-OR-N-P question is (for some reason) not directly
511     ;; sent to the Slime user, the function CL:Y-OR-N-P is temporarily
512     ;; overruled.
513     `(let* ((pkg (find-package "common-lisp"))
514     (saved-pdl (excl::package-definition-lock pkg))
515     (saved-ynp (symbol-function 'cl:y-or-n-p)))
516    
517     (setf (excl::package-definition-lock pkg) nil
518     (symbol-function 'cl:y-or-n-p) (symbol-function
519     (find-symbol "y-or-n-p-in-emacs"
520     "swank")))
521     (unwind-protect
522     (progn ,@body)
523    
524     (setf (symbol-function 'cl:y-or-n-p) saved-ynp
525     (excl::package-definition-lock pkg) saved-pdl))))
526    
527     (defun start-acl-profiler ()
528     (with-redirected-y-or-n-p
529     (prof:start-profiler :type :time :count t
530     :start-sampling-p nil :verbose nil)))
531     (defun acl-profiler-active-p ()
532     (not (eq (prof:profiler-status :verbose nil) :inactive)))
533    
534     (defun stop-acl-profiler ()
535     (prof:stop-profiler :verbose nil))
536    
537     (excl:def-fwrapper profile-fwrapper (&rest args)
538     ;; Ensures sampling is done during the execution of the function,
539     ;; taking into account recursion.
540     (declare (ignore args))
541     (cond ((zerop *profile-depth*)
542     (let ((*profile-depth* (1+ *profile-depth*)))
543     (prof:start-sampling)
544     (unwind-protect (excl:call-next-fwrapper)
545     (prof:stop-sampling))))
546     (t
547     (excl:call-next-fwrapper))))
548    
549     (defimplementation profile (fname)
550     (unless (acl-profiler-active-p)
551     (start-acl-profiler))
552     (excl:fwrap fname 'profile-fwrapper 'profile-fwrapper)
553     (push fname *profiled-functions*))
554    
555     (defimplementation profiled-functions ()
556     *profiled-functions*)
557    
558     (defimplementation unprofile (fname)
559     (excl:funwrap fname 'profile-fwrapper)
560     (setq *profiled-functions* (remove fname *profiled-functions*)))
561    
562 mkoeppe 1.83 (defimplementation profile-report ()
563 heller 1.89 (prof:show-flat-profile :verbose nil)
564     (when *profiled-functions*
565     (start-acl-profiler)))
566    
567     (defimplementation profile-reset ()
568     (when (acl-profiler-active-p)
569     (stop-acl-profiler)
570     (start-acl-profiler))
571     "Reset profiling counters.")
572 mkoeppe 1.83
573 heller 1.18 ;;;; Inspecting
574    
575 heller 1.111 (excl:without-redefinition-warnings
576 heller 1.100 (defmethod emacs-inspect ((o t))
577 heller 1.111 (allegro-inspect o)))
578 heller 1.62
579 heller 1.100 (defmethod emacs-inspect ((o function))
580 heller 1.101 (allegro-inspect o))
581 heller 1.62
582 heller 1.100 (defmethod emacs-inspect ((o standard-object))
583 heller 1.101 (allegro-inspect o))
584 heller 1.82
585 heller 1.62 (defun allegro-inspect (o)
586     (loop for (d dd) on (inspect::inspect-ctl o)
587 heller 1.73 append (frob-allegro-field-def o d)
588     until (eq d dd)))
589 heller 1.62
590 heller 1.73 (defun frob-allegro-field-def (object def)
591 heller 1.62 (with-struct (inspect::field-def- name type access) def
592 heller 1.73 (ecase type
593 heller 1.79 ((:unsigned-word :unsigned-byte :unsigned-natural
594     :unsigned-long :unsigned-half-long
595     :unsigned-3byte)
596 heller 1.73 (label-value-line name (inspect::component-ref-v object access type)))
597     ((:lisp :value)
598     (label-value-line name (inspect::component-ref object access)))
599     (:indirect
600     (destructuring-bind (prefix count ref set) access
601     (declare (ignore set prefix))
602     (loop for i below (funcall count object)
603     append (label-value-line (format nil "~A-~D" name i)
604     (funcall ref object i))))))))
605 mbaringer 1.55
606 heller 1.18 ;;;; Multithreading
607 heller 1.8
608 mbaringer 1.91 (defimplementation initialize-multiprocessing (continuation)
609     (mp:start-scheduler)
610     (funcall continuation))
611 heller 1.8
612 heller 1.10 (defimplementation spawn (fn &key name)
613 lgorrie 1.47 (mp:process-run-function name fn))
614 heller 1.8
615 heller 1.40 (defvar *id-lock* (mp:make-process-lock :name "id lock"))
616     (defvar *thread-id-counter* 0)
617    
618     (defimplementation thread-id (thread)
619     (mp:with-process-lock (*id-lock*)
620     (or (getf (mp:process-property-list thread) 'id)
621     (setf (getf (mp:process-property-list thread) 'id)
622     (incf *thread-id-counter*)))))
623    
624     (defimplementation find-thread (id)
625     (find id mp:*all-processes*
626     :key (lambda (p) (getf (mp:process-property-list p) 'id))))
627    
628 heller 1.13 (defimplementation thread-name (thread)
629     (mp:process-name thread))
630 heller 1.8
631 heller 1.13 (defimplementation thread-status (thread)
632     (format nil "~A ~D" (mp:process-whostate thread)
633     (mp:process-priority thread)))
634 heller 1.8
635 heller 1.10 (defimplementation make-lock (&key name)
636 heller 1.8 (mp:make-process-lock :name name))
637    
638 heller 1.10 (defimplementation call-with-lock-held (lock function)
639 heller 1.8 (mp:with-process-lock (lock) (funcall function)))
640 heller 1.12
641     (defimplementation current-thread ()
642     mp:*current-process*)
643    
644     (defimplementation all-threads ()
645 heller 1.13 (copy-list mp:*all-processes*))
646 heller 1.12
647     (defimplementation interrupt-thread (thread fn)
648     (mp:process-interrupt thread fn))
649    
650 heller 1.16 (defimplementation kill-thread (thread)
651     (mp:process-kill thread))
652    
653 heller 1.12 (defvar *mailbox-lock* (mp:make-process-lock :name "mailbox lock"))
654    
655     (defstruct (mailbox (:conc-name mailbox.))
656 heller 1.107 (lock (mp:make-process-lock :name "process mailbox"))
657     (queue '() :type list)
658 heller 1.108 (gate (mp:make-gate nil)))
659 heller 1.12
660     (defun mailbox (thread)
661     "Return THREAD's mailbox."
662     (mp:with-process-lock (*mailbox-lock*)
663     (or (getf (mp:process-property-list thread) 'mailbox)
664     (setf (getf (mp:process-property-list thread) 'mailbox)
665     (make-mailbox)))))
666    
667     (defimplementation send (thread message)
668 heller 1.107 (let* ((mbox (mailbox thread)))
669     (mp:with-process-lock ((mailbox.lock mbox))
670     (setf (mailbox.queue mbox)
671     (nconc (mailbox.queue mbox) (list message)))
672     (mp:open-gate (mailbox.gate mbox)))))
673 heller 1.12
674 heller 1.110 (defimplementation receive-if (test &optional timeout)
675 heller 1.104 (let ((mbox (mailbox mp:*current-process*)))
676 heller 1.110 (assert (or (not timeout) (eq timeout t)))
677 heller 1.107 (loop
678     (check-slime-interrupts)
679     (mp:with-process-lock ((mailbox.lock mbox))
680     (let* ((q (mailbox.queue mbox))
681     (tail (member-if test q)))
682     (when tail
683     (setf (mailbox.queue mbox) (nconc (ldiff q tail) (cdr tail)))
684     (return (car tail)))
685     (mp:close-gate (mailbox.gate mbox))))
686 heller 1.110 (when (eq timeout t) (return (values nil t)))
687     (mp:process-wait-with-timeout "receive-if" 0.5
688     #'mp:gate-open-p (mailbox.gate mbox)))))
689 heller 1.104
690 mbaringer 1.27 (defimplementation quit-lisp ()
691     (excl:exit 0 :quiet t))
692 mbaringer 1.68
693 heller 1.69
694 mbaringer 1.68 ;;Trace implementations
695     ;;In Allegro 7.0, we have:
696     ;; (trace <name>)
697     ;; (trace ((method <name> <qualifier>? (<specializer>+))))
698     ;; (trace ((labels <name> <label-name>)))
699     ;; (trace ((labels (method <name> (<specializer>+)) <label-name>)))
700     ;; <name> can be a normal name or a (setf name)
701    
702 heller 1.70 (defimplementation toggle-trace (spec)
703 lgorrie 1.72 (ecase (car spec)
704     ((setf)
705     (toggle-trace-aux spec))
706 heller 1.70 (:defgeneric (toggle-trace-generic-function-methods (second spec)))
707 heller 1.71 ((setf :defmethod :labels :flet)
708 heller 1.70 (toggle-trace-aux (process-fspec-for-allegro spec)))
709 lgorrie 1.72 (:call
710 heller 1.70 (destructuring-bind (caller callee) (cdr spec)
711     (toggle-trace-aux callee
712     :inside (list (process-fspec-for-allegro caller)))))))
713    
714     (defun tracedp (fspec)
715 heller 1.71 (member fspec (eval '(trace)) :test #'equal))
716 heller 1.70
717     (defun toggle-trace-aux (fspec &rest args)
718     (cond ((tracedp fspec)
719     (eval `(untrace ,fspec))
720     (format nil "~S is now untraced." fspec))
721     (t
722     (eval `(trace (,fspec ,@args)))
723     (format nil "~S is now traced." fspec))))
724    
725     (defun toggle-trace-generic-function-methods (name)
726 mbaringer 1.68 (let ((methods (mop:generic-function-methods (fdefinition name))))
727 heller 1.70 (cond ((tracedp name)
728 mbaringer 1.68 (eval `(untrace ,name))
729     (dolist (method methods (format nil "~S is now untraced." name))
730     (excl:funtrace (mop:method-function method))))
731     (t
732 lgorrie 1.72 (eval `(trace (,name)))
733 heller 1.70 (dolist (method methods (format nil "~S is now traced." name))
734 mbaringer 1.68 (excl:ftrace (mop:method-function method)))))))
735    
736     (defun process-fspec-for-allegro (fspec)
737     (cond ((consp fspec)
738     (ecase (first fspec)
739 heller 1.71 ((setf) fspec)
740 mbaringer 1.68 ((:defun :defgeneric) (second fspec))
741     ((:defmethod) `(method ,@(rest fspec)))
742 heller 1.69 ((:labels) `(labels ,(process-fspec-for-allegro (second fspec))
743     ,(third fspec)))
744     ((:flet) `(flet ,(process-fspec-for-allegro (second fspec))
745     ,(third fspec)))))
746 mbaringer 1.68 (t
747     fspec)))
748 heller 1.88
749    
750     ;;;; Weak hashtables
751    
752     (defimplementation make-weak-key-hash-table (&rest args)
753     (apply #'make-hash-table :weak-keys t args))
754    
755     (defimplementation make-weak-value-hash-table (&rest args)
756     (apply #'make-hash-table :values :weak args))
757 mkoeppe 1.93
758 alendvai 1.95 (defimplementation hash-table-weakness (hashtable)
759     (cond ((excl:hash-table-weak-keys hashtable) :key)
760     ((eq (excl:hash-table-values hashtable) :weak) :value)))
761    
762    
763 mkoeppe 1.93
764     ;;;; Character names
765    
766     (defimplementation character-completion-set (prefix matchp)
767     (loop for name being the hash-keys of excl::*name-to-char-table*
768     when (funcall matchp prefix name)
769     collect (string-capitalize name)))

  ViewVC Help
Powered by ViewVC 1.1.5