-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrimitiveVisitor.java
151 lines (141 loc) · 3.61 KB
/
PrimitiveVisitor.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
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.*;
import sun.jvm.hotspot.debugger.*;
public class PrimitiveVisitor implements Runnable,Constants
{
private String className;
private String fieldName;
private String type;
private boolean verbose;
public PrimitiveVisitor(String className,String fieldName,String type,boolean verbose)
{
this.className=className;
this.fieldName=fieldName;
this.type=type;
this.verbose=verbose;
}
public void run()
{
try
{
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 Field field=getField(klass);
heap.iterateObjectsOfKlass(new DefaultHeapVisitor(){
public boolean doObj(Oop obj/*className instance*/)
{
if(verbose)
{
obj.print();
}
countHolder[0]++;
Object r=getFieldValue(field,obj);
System.out.println(className+" @ "+obj.getHandle()+" (object size = "+obj.getObjectSize()+")");
System.out.print(fieldName+":"+r);
System.out.println();
return false;
}
},klass,false);
System.out.println("total "+className+" instance count:"+countHolder[0]);
}
catch(IllegalArgumentException e)
{
e.printStackTrace();
}
catch (AddressException e)
{
System.err.println("Error accessing address 0x"
+ Long.toHexString(e.getAddress()));
e.printStackTrace();
}
}
public Object getFieldValue(Field field,Oop obj) throws IllegalArgumentException
{
Object ret=null;
if(BYTE.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJByteAt(field.getOffset());
}
else if(CHAR.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJCharAt(field.getOffset());
}
else if(DOUBLE.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJDoubleAt(field.getOffset());
}
else if(FLOAT.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJFloatAt(field.getOffset());
}
else if(INT.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJIntAt(field.getOffset());
}
else if(LONG.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJLongAt(field.getOffset());
}
else if(SHORT.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJShortAt(field.getOffset());
}
else if(BOOLEAN.equalsIgnoreCase(this.type))
{
ret=obj.getHandle().getJBooleanAt(field.getOffset());
}
else
{
throw new IllegalArgumentException("illegal type");
}
return ret;
}
public Field getField(InstanceKlass klass) throws IllegalArgumentException
{
String signiture="";
if(BYTE.equalsIgnoreCase(this.type))
{
signiture=BYTE_VM;
}
else if(CHAR.equalsIgnoreCase(this.type))
{
signiture=CHAR_VM;
}
else if(DOUBLE.equalsIgnoreCase(this.type))
{
signiture=DOUBLE_VM;
}
else if(FLOAT.equalsIgnoreCase(this.type))
{
signiture=FLOAT_VM;
}
else if(INT.equalsIgnoreCase(this.type))
{
signiture=INT_VM;
}
else if(LONG.equalsIgnoreCase(this.type))
{
signiture=LONG_VM;
}
else if(SHORT.equalsIgnoreCase(this.type))
{
signiture=SHORT_VM;
}
else if(BOOLEAN.equalsIgnoreCase(this.type))
{
signiture=BOOLEAN_VM;
}
else
{
throw new IllegalArgumentException("illegal type");
}
return klass.findField(this.fieldName,signiture);
}
}