source: trunk/abcl/src/org/armedbear/lisp/disassemble.lisp @ 14944

Last change on this file since 14944 was 14944, checked in by Mark Evenson, 5 years ago

make *disassembler-function* work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.6 KB
Line 
1;;; disassemble.lisp
2;;;
3;;; Copyright (C) 2005 Peter Graves
4;;; $Id: disassemble.lisp 14944 2017-01-11 21:04:41Z mevenson $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19;;;
20;;; As a special exception, the copyright holders of this library give you
21;;; permission to link this library with independent modules to produce an
22;;; executable, regardless of the license terms of these independent
23;;; modules, and to copy and distribute the resulting executable under
24;;; terms of your choice, provided that you also meet, for each linked
25;;; independent module, the terms and conditions of the license of that
26;;; module.  An independent module is a module which is not derived from
27;;; or based on this library.  If you modify this library, you may extend
28;;; this exception to your version of the library, but you are not
29;;; obligated to do so.  If you do not wish to do so, delete this
30;;; exception statement from your version.
31
32(in-package #:system)
33
34(require '#:clos)
35
36(defvar *disassembler-function* NIL)
37
38(defvar *disassemblers*
39  `((:objectweb . objectweb-test)
40    (:external . external-test)))
41
42(defun choose-disassembler (&optional name)
43  (setf *disassembler-function*
44        (if name
45            (or (funcall (cdr (assoc name *disassemblers*)))
46                (error "Can't find suitable disassembler."))
47            (loop
48              for (NIL . test) in *disassemblers*
49              for result = (funcall test)
50              when result
51                do (return result)
52              finally (warn "Can't find suitable disassembler.")))))
53
54(eval-when (:compile-toplevel :load-toplevel :execute)
55  (defmacro with-open ((name value) &body body)
56    `(let ((,name ,value))
57       (unwind-protect
58           (progn ,@body)
59         (java:jcall-raw "close" ,name)))))
60
61(defun read-byte-array-from-stream (stream)
62  (let ((buffer (java:jnew-array (java:jclass "byte") 4096)))
63    (with-open (output (java:jnew "java.io.ByteArrayOutputStream"))
64      (loop
65        for length = (java:jcall "read" stream buffer)
66        until (eql length -1)
67        do (java:jcall-raw "write" output buffer 0 length))
68      (java:jcall-raw "flush" output)
69      (java:jcall-raw "toByteArray" output))))
70
71(defun class-resource-path (class)
72  (format NIL "~A.class" (substitute #\/ #\. (java:jcall "getName" class))))
73
74(defun class-bytes (class)
75  (with-open (stream (java:jcall-raw
76                      "getResourceAsStream"
77                      (java:jcall-raw "getClassLoader" class)
78                      (class-resource-path class)))
79    (read-byte-array-from-stream stream)))
80
81;; disassemble more things
82(defun disassemble-function (arg)
83  (flet ((disassemble-bytes (bytes) (funcall (or *disassembler-function* 'system::disassemble-class-bytes) bytes)))
84    (let ((function (cond ((java:jcall "isInstance" (java:jclass "org.armedbear.lisp.CompiledClosure") arg)
85         (return-from disassemble-function "don't know how to disassemble CompiledClosure"))
86        ((java::java-object-p arg) 
87         (cond ((java::jinstance-of-p arg "java.lang.Class")
88          arg)
89         ((java::jinstance-of-p arg "java.lang.reflect.Method")
90          (java::jmethod-declaring-class arg))
91         ;; use isInstance instead of jinstance-of-p
92         ;; because the latter checked java-object-p
93         ;; which fails since its a lisp object
94         ((and (java:jcall "isInstance"  (java:jclass "org.armedbear.lisp.Closure") arg)
95               (not (java:jcall "isInstance"  (java:jclass "org.armedbear.lisp.CompiledClosure") arg)))
96          (return-from disassemble-function 
97            (with-output-to-string (s)
98              (format s "Not a compiled function: ~%")
99              (pprint (java:jcall "getBody" arg) s))))
100         ))
101        ((functionp arg)
102         arg)
103        ((symbolp arg)
104         (or (macro-function arg) (symbol-function arg)))
105        (t arg))))
106      (when (typep function 'generic-function)
107  (setf function (mop::funcallable-instance-function function)))
108      (print function)
109      (let ((bytes (and nil (and function (not (java::java-object-p function)) (system::function-class-bytes  function)))))
110  ;; we've got bytes here then we've covered the case that the diassembler already handled
111  ;; If not then we've either got a primitive (in function) or we got passed a method object as arg.
112  (if bytes
113      (disassemble-bytes bytes)
114      (let ((class (if (java:java-object-p function) function (java:jcall "getClass" function))))
115        (let ((classloader (java:jcall "getClassLoader" class)))
116    (if (or (java:jinstance-of-p classloader "org.armedbear.lisp.MemoryClassLoader")
117      (java:jinstance-of-p classloader "org.armedbear.lisp.FaslClassLoader"))
118        (disassemble-bytes 
119         (java:jcall "getFunctionClassBytes" classloader class))
120        (disassemble-bytes 
121         (java:jstatic "toByteArray" "com.google.common.io.ByteStreams" 
122           (java:jcall-raw
123            "getResourceAsStream"
124            (java:jcall-raw "getClassLoader" class)
125            (class-resource-path class))))))))))))
126
127(defun disassemble (arg)
128  (print-lines-with-prefix (disassemble-function arg)))
129
130(defun print-lines-with-prefix (string)
131  (with-input-from-string (stream string)
132    (loop
133      (let ((line (read-line stream nil)))
134        (unless line (return))
135        (write-string "; ")
136        (write-string line)
137        (terpri)))))
138
139(defun external-disassemble (object)
140  (disassemble-class-bytes object))
141
142(defun external-test ()
143  (ignore-errors
144    (and (disassemble-class-bytes #'cons) #'external-disassemble)))
145
146(defun objectweb-disassemble (object)
147  (let* ((reader (java:jnew "org.objectweb.asm.ClassReader" object))
148         (writer (java:jnew "java.io.StringWriter"))
149         (printer (java:jnew "java.io.PrintWriter" writer))
150         (tracer (java:jnew "org.objectweb.asm.util.TraceClassVisitor" java:+null+ printer))
151         ;; this is to support both the 1.X and subsequent releases
152         (flags (ignore-errors (java:jfield "org.objectweb.asm.ClassReader" "SKIP_DEBUG"))))
153    (java:jcall-raw "accept" reader tracer (or flags java:+false+))
154    (java:jcall "toString" writer)))
155
156(defun objectweb-test ()
157  (ignore-errors
158    (and (java:jclass "org.objectweb.asm.ClassReader") #'objectweb-disassemble)))
Note: See TracBrowser for help on using the repository browser.