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

Contents of /slime/swank-allegro.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.136 - (hide annotations)
Mon Mar 8 16:20:10 2010 UTC (4 years, 1 month ago) by heller
Branch: MAIN
Changes since 1.135: +22 -12 lines
Fix some of the brokeness in the last change.

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

  ViewVC Help
Powered by ViewVC 1.1.5