1 | ;;; disassemble.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2005 Peter Graves |
---|
4 | ;;; $Id: disassemble.lisp 14859 2016-09-04 07:01:06Z 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 | (defun disassemble (arg) |
---|
82 | (require-type arg '(OR FUNCTION |
---|
83 | SYMBOL |
---|
84 | (CONS (EQL SETF) (CONS SYMBOL NULL)) |
---|
85 | (CONS (EQL LAMBDA) LIST))) |
---|
86 | (let ((function (cond ((functionp arg) |
---|
87 | arg) |
---|
88 | ((symbolp arg) |
---|
89 | (or (macro-function arg) (symbol-function arg)))))) |
---|
90 | (when (typep function 'generic-function) |
---|
91 | (setf function (mop::funcallable-instance-function function))) |
---|
92 | (when (functionp function) |
---|
93 | (unless (compiled-function-p function) |
---|
94 | (setf function (compile nil function))) |
---|
95 | (let ((class-bytes (or (function-class-bytes function) |
---|
96 | (class-bytes (java:jcall-raw "getClass" function))))) |
---|
97 | (if class-bytes |
---|
98 | (let ((disassembler (or *disassembler-function* |
---|
99 | (choose-disassembler)))) |
---|
100 | (and disassembler (funcall disassembler class-bytes))) |
---|
101 | (%format t "; Disassembly is not available.~%")))))) |
---|
102 | |
---|
103 | (defun print-lines-with-prefix (string) |
---|
104 | (with-input-from-string (stream string) |
---|
105 | (loop |
---|
106 | (let ((line (read-line stream nil))) |
---|
107 | (unless line (return)) |
---|
108 | (write-string "; ") |
---|
109 | (write-string line) |
---|
110 | (terpri))))) |
---|
111 | |
---|
112 | (defun external-disassemble (object) |
---|
113 | (print-lines-with-prefix (disassemble-class-bytes object))) |
---|
114 | |
---|
115 | (defun external-test () |
---|
116 | (ignore-errors |
---|
117 | (and (disassemble-class-bytes #'cons) #'external-disassemble))) |
---|
118 | |
---|
119 | (defun objectweb-disassemble (object) |
---|
120 | (let* ((reader (java:jnew "org.objectweb.asm.ClassReader" object)) |
---|
121 | (writer (java:jnew "java.io.StringWriter")) |
---|
122 | (printer (java:jnew "java.io.PrintWriter" writer)) |
---|
123 | (tracer (java:jnew "org.objectweb.asm.util.TraceClassVisitor" java:+null+ printer)) |
---|
124 | ;; this is to support both the 1.X and subsequent releases |
---|
125 | (flags (ignore-errors (java:jfield "org.objectweb.asm.ClassReader" "SKIP_DEBUG")))) |
---|
126 | (java:jcall-raw "accept" reader tracer (or flags java:+false+)) |
---|
127 | (print-lines-with-prefix (java:jcall "toString" writer)))) |
---|
128 | |
---|
129 | (defun objectweb-test () |
---|
130 | (ignore-errors |
---|
131 | (and (java:jclass "org.objectweb.asm.ClassReader") #'objectweb-disassemble))) |
---|