-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringVisitor.java
94 lines (86 loc) · 2.7 KB
/
StringVisitor.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
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.utilities.*;
public class StringVisitor implements Runnable,Constants
{
private String className;
private String fieldName;
private String type;
private boolean verbose;
public StringVisitor(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();
String signiture="Ljava/lang/String;";
final OopField field=(OopField)klass.findField(this.fieldName,signiture);
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+":");
}
//string instance
final Oop oop=field.getValue(obj);
InstanceKlass k=(InstanceKlass)oop.getKlass();
OopField valueField = (OopField)k.findField("value", "[C");
//char[] instance
final TypeArray arrayInstance=(TypeArray)valueField.getValue(oop);
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 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);
System.out.println();
return false;
}
},klass,false);
System.out.println("total "+className+" instance count:"+countHolder[0]);
}
}