1 | ;;; dump-class.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2003-2005 Peter Graves |
---|
4 | ;;; $Id: dump-class.lisp 14144 2012-09-01 21:03:05Z ehuelsmann $ |
---|
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 | (require '#:jvm-instructions) |
---|
33 | |
---|
34 | (in-package #:jvm) |
---|
35 | |
---|
36 | (defvar *pool* nil) |
---|
37 | |
---|
38 | (defun read-u1 (stream) |
---|
39 | (read-byte stream)) |
---|
40 | |
---|
41 | (defun read-u2 (stream) |
---|
42 | (+ (ash (read-byte stream) 8) (read-byte stream))) |
---|
43 | |
---|
44 | (defun read-u4 (stream) |
---|
45 | (+ (ash (read-u2 stream) 16) (read-u2 stream))) |
---|
46 | |
---|
47 | (defun lookup-utf8 (index) |
---|
48 | (let ((entry (svref *pool* index))) |
---|
49 | (when (eql (car entry) 1) |
---|
50 | (caddr entry)))) |
---|
51 | |
---|
52 | (defun read-constant-pool-entry (stream) |
---|
53 | (let ((tag (read-u1 stream))) |
---|
54 | (case tag |
---|
55 | ((7 8) |
---|
56 | (list tag (read-u2 stream))) |
---|
57 | (1 |
---|
58 | ` (let* ((len (read-u2 stream)) |
---|
59 | (s (make-string len))) |
---|
60 | (dotimes (i len) |
---|
61 | (setf (char s i) (code-char (read-u1 stream)))) |
---|
62 | (list tag len s))) |
---|
63 | ((3 4) |
---|
64 | (list tag (read-u4 stream))) |
---|
65 | ((5 6) |
---|
66 | (list tag (read-u4 stream) (read-u4 stream))) |
---|
67 | ((12 9 10 11) |
---|
68 | (list tag (read-u2 stream) (read-u2 stream))) |
---|
69 | (t |
---|
70 | (error "READ-CONSTANT-POOL-ENTRY unhandled tag ~D" tag))))) |
---|
71 | |
---|
72 | (defvar *indent* 0) |
---|
73 | |
---|
74 | (defparameter *spaces* (make-string 256 :initial-element #\space)) |
---|
75 | |
---|
76 | (defmacro out (&rest args) |
---|
77 | `(progn (format t (subseq *spaces* 0 *indent*)) (format t ,@args))) |
---|
78 | |
---|
79 | (defun dump-code (code) |
---|
80 | (let ((code-length (length code))) |
---|
81 | (do ((i 0)) |
---|
82 | ((>= i code-length)) |
---|
83 | (let* ((opcode (svref code i)) |
---|
84 | (size (opcode-size opcode))) |
---|
85 | (out "~D: ~D (#x~X) ~A~%" i opcode opcode (opcode-name opcode)) |
---|
86 | (incf i) |
---|
87 | (dotimes (j (1- size)) |
---|
88 | (let ((byte (svref code i))) |
---|
89 | (out "~D: ~D (#x~X)~%" i byte byte)) |
---|
90 | (incf i)))))) |
---|
91 | |
---|
92 | (defun dump-code-attribute (stream) |
---|
93 | (let ((*indent* (+ *indent* 2))) |
---|
94 | (out "Stack: ~D~%" (read-u2 stream)) |
---|
95 | (out "Locals: ~D~%" (read-u2 stream)) |
---|
96 | (let* ((code-length (read-u4 stream)) |
---|
97 | (code (make-array code-length))) |
---|
98 | (out "Code length: ~D~%" code-length) |
---|
99 | (out "Code:~%") |
---|
100 | (dotimes (i code-length) |
---|
101 | (setf (svref code i) (read-u1 stream))) |
---|
102 | (let ((*indent* (+ *indent* 2))) |
---|
103 | (dump-code code))) |
---|
104 | (let ((exception-table-length (read-u2 stream))) |
---|
105 | (out "Exception table length: ~D~%" exception-table-length) |
---|
106 | (let ((*indent* (+ *indent* 2))) |
---|
107 | (dotimes (i exception-table-length) |
---|
108 | (out "Start PC: ~D~%" (read-u2 stream)) |
---|
109 | (out "End PC: ~D~%" (read-u2 stream)) |
---|
110 | (out "Handler PC: ~D~%" (read-u2 stream)) |
---|
111 | (out "Catch type: ~D~%" (read-u2 stream))))) |
---|
112 | (let ((attributes-count (read-u2 stream))) |
---|
113 | (out "Number of attributes: ~D~%" attributes-count) |
---|
114 | (let ((*indent* (+ *indent* 2))) |
---|
115 | (dotimes (i attributes-count) |
---|
116 | (read-attribute i stream)))))) |
---|
117 | |
---|
118 | (defun dump-exceptions (stream) |
---|
119 | (declare (ignore stream)) |
---|
120 | ) |
---|
121 | |
---|
122 | (defun read-attribute (index stream) |
---|
123 | (let* ((name-index (read-u2 stream)) |
---|
124 | (name (lookup-utf8 name-index)) |
---|
125 | (length (read-u4 stream)) |
---|
126 | (*indent* (+ *indent* 2))) |
---|
127 | (out "Attribute ~D: Name index: ~D (~S)~%" index name-index name) |
---|
128 | (out "Attribute ~D: Length: ~D~%" index length) |
---|
129 | (cond ((string= name "Code") |
---|
130 | (dump-code-attribute stream)) |
---|
131 | ((string= name "Exceptions") |
---|
132 | (let ((count (read-u2 stream))) |
---|
133 | (out "Attribute ~D: Number of exceptions: ~D~%" index count) |
---|
134 | (let ((*indent* (+ *indent* 2))) |
---|
135 | (dotimes (i count) |
---|
136 | (out "Exception ~D: ~D~%" i (read-u2 stream)))))) |
---|
137 | ((string= name "SourceFile") |
---|
138 | (let ((source-file-index (read-u2 stream))) |
---|
139 | (out "Attribute ~D: Source file index: ~D (~S)~%" |
---|
140 | index source-file-index (lookup-utf8 source-file-index)))) |
---|
141 | (t |
---|
142 | (dotimes (i length) |
---|
143 | (read-u1 stream)))))) |
---|
144 | |
---|
145 | (defun read-info (index stream type) |
---|
146 | (let* ((access-flags (read-u2 stream)) |
---|
147 | (name-index (read-u2 stream)) |
---|
148 | (descriptor-index (read-u2 stream)) |
---|
149 | (attributes-count (read-u2 stream)) |
---|
150 | (*indent* (+ *indent* 2)) |
---|
151 | (type (case type |
---|
152 | ('field "Field") |
---|
153 | ('method "Method")))) |
---|
154 | (out "~A ~D: Access flags: #x~X~%" type index access-flags) |
---|
155 | (out "~A ~D: Name index: ~D (~S)~%" type index name-index (lookup-utf8 name-index)) |
---|
156 | (out "~A ~D: Descriptor index: ~D~%" type index descriptor-index) |
---|
157 | (out "~A ~D: Number of attributes: ~D~%" type index attributes-count) |
---|
158 | (let ((*indent* (+ *indent* 2))) |
---|
159 | (dotimes (i attributes-count) |
---|
160 | (read-attribute i stream))))) |
---|
161 | |
---|
162 | (defun dump-class (filename) |
---|
163 | (let ((*indent* 0) |
---|
164 | (*pool* nil)) |
---|
165 | (with-open-file (stream filename :direction :input :element-type 'unsigned-byte) |
---|
166 | (handler-bind ((end-of-file |
---|
167 | #'(lambda (c) (return-from dump-class c)))) |
---|
168 | (out "Magic number: #x~X~%" (read-u4 stream)) |
---|
169 | (let ((minor (read-u2 stream)) |
---|
170 | (major (read-u2 stream))) |
---|
171 | (out "Version: ~D.~D~%" major minor)) |
---|
172 | ;; Constant pool. |
---|
173 | (let ((count (read-u2 stream)) |
---|
174 | entry type) |
---|
175 | (out "Constant pool (~D entries):~%" count) |
---|
176 | (setq *pool* (make-array count)) |
---|
177 | (let ((*indent* (+ *indent* 2))) |
---|
178 | (dotimes (index (1- count)) |
---|
179 | (setq entry (read-constant-pool-entry stream)) |
---|
180 | (setf (svref *pool* (1+ index)) entry) |
---|
181 | (setq type (case (car entry) |
---|
182 | (7 'class) |
---|
183 | (9 'field) |
---|
184 | (10 'method) |
---|
185 | (11 'interface) |
---|
186 | (8 'string) |
---|
187 | (3 'integer) |
---|
188 | (4 'float) |
---|
189 | (5 'long) |
---|
190 | (6 'double) |
---|
191 | (12 'name-and-type) |
---|
192 | (1 'utf8))) |
---|
193 | (out "~D: ~A ~S~%" (1+ index) type entry)))) |
---|
194 | (out "Access flags: #x~X~%" (read-u2 stream)) |
---|
195 | (out "This class: ~D~%" (read-u2 stream)) |
---|
196 | (out "Superclass: ~D~%" (read-u2 stream)) |
---|
197 | ;; Interfaces. |
---|
198 | (let ((count (read-u2 stream))) |
---|
199 | (cond ((zerop count) |
---|
200 | (out "No interfaces~%")) |
---|
201 | (t |
---|
202 | (out "Interfaces (~D):~%" count) |
---|
203 | (dotimes (i count) |
---|
204 | (out " ~D: ~D~%" i (read-u2 stream)))))) |
---|
205 | ;; Fields. |
---|
206 | (let ((count (read-u2 stream))) |
---|
207 | (cond ((zerop count) |
---|
208 | (out "No fields~%")) |
---|
209 | (t |
---|
210 | (out "Fields (~D):~%" count))) |
---|
211 | (dotimes (index count) |
---|
212 | (read-info index stream 'field))) |
---|
213 | ;; Methods. |
---|
214 | (let ((count (read-u2 stream))) |
---|
215 | (cond ((zerop count) |
---|
216 | (out "No methods~%")) |
---|
217 | (t |
---|
218 | (out "Methods (~D):~%" count))) |
---|
219 | (dotimes (index count) |
---|
220 | (read-info index stream 'method))) |
---|
221 | ;; Attributes. |
---|
222 | (let ((count (read-u2 stream))) |
---|
223 | (cond ((zerop count) |
---|
224 | (out "No attributes~%")) |
---|
225 | (t |
---|
226 | (out "Attributes (~D):~%" count))) |
---|
227 | (dotimes (index count) |
---|
228 | (read-attribute index stream)))))) |
---|
229 | t) |
---|