-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimitiveArrayVisitor.java
228 lines (210 loc) · 5.79 KB
/
PrimitiveArrayVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.*;
public class PrimitiveArrayVisitor implements Runnable,Constants
{
private String className;
private String fieldName;
private String type;
private boolean verbose;
public PrimitiveArrayVisitor(String className,String fieldName,String type,boolean verbose)
{
this.className=className;
this.fieldName=fieldName;
this.type=type;
this.verbose=verbose;
}
public void run()
{
final int[] countHolder=new int[1];
final InstanceKlass klass=SystemDictionaryHelper.findInstanceKlass(className);
if(null==klass)
{
System.out.println(className+" klass is null");
return ;
}
ObjectHeap heap=VM.getVM().getObjectHeap();
final OopField field=getField(klass);
heap.iterateObjectsOfKlass(new DefaultHeapVisitor(){
public boolean doObj(Oop obj/*className instance*/)
{
countHolder[0]++;
System.out.println(klass.getName().asString()+" @ "+ obj.getHandle()+" (object size = "+obj.getObjectSize()+")");
if(verbose)
{
obj.print();
}
else
{
System.out.print(fieldName+":[");
}
//TypeArray
final TypeArray arrayInstance=(TypeArray)field.getValue(obj);
arrayInstance.iterate(new DefaultOopVisitor(){
public void doChar(CharField field, boolean isVMField)
{
char v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doByte(ByteField field, boolean isVMField)
{
byte v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doBoolean(BooleanField field, boolean isVMField)
{
boolean v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doShort(ShortField field, boolean isVMField)
{
short v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doInt(IntField field, boolean isVMField)
{
int v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doLong(LongField field, boolean isVMField)
{
long v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doFloat(FloatField field, boolean isVMField)
{
float v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doDouble(DoubleField field, boolean isVMField)
{
double v=field.getValue(arrayInstance);
if(verbose)
{
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
else
{
System.out.print(v+" ");
}
}
public void doCInt(CIntField field, boolean isVMField)
{
if(verbose)
{
long v=field.getValue(arrayInstance);
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"+v);
}
}
public void doOop(OopField field, boolean isVMField)
{
if(verbose)
{
TypeArrayKlass pp=(TypeArrayKlass)field.getValue(arrayInstance);
System.out.println(field.getID().getName()+": {"+field.getOffset()+"} :"
+pp.getClass().getSimpleName()+" for "+pp.signature()+" @ "
+pp.getHandle());
}
}
},true);
if(!verbose)
{
System.out.print("]");
}
System.out.println();
return false;
}
},klass,false);
System.out.println("total "+className+" instance count:"+countHolder[0]);
}
public OopField getField(InstanceKlass klass) throws IllegalArgumentException
{
String signiture="";
if(BYTE_ARRAY.equalsIgnoreCase(this.type))
{
signiture=BYTE_ARRAY_VM;
}
else if(CHAR_ARRAY.equalsIgnoreCase(this.type))
{
signiture=CHAR_ARRAY_VM;
}
else if(DOUBLE_ARRAY.equalsIgnoreCase(this.type))
{
signiture=DOUBLE_ARRAY_VM;
}
else if(FLOAT_ARRAY.equalsIgnoreCase(this.type))
{
signiture=FLOAT_ARRAY_VM;
}
else if(INT_ARRAY.equalsIgnoreCase(this.type))
{
signiture=INT_ARRAY_VM;
}
else if(LONG_ARRAY.equalsIgnoreCase(this.type))
{
signiture=LONG_ARRAY_VM;
}
else if(SHORT_ARRAY.equalsIgnoreCase(this.type))
{
signiture=SHORT_ARRAY_VM;
}
else if(BOOLEAN_ARRAY.equalsIgnoreCase(this.type))
{
signiture=BOOLEAN_ARRAY_VM;
}
else
{
throw new IllegalArgumentException("illegal type");
}
return (OopField)klass.findField(this.fieldName,signiture);
}
}