搞定Hotspot-api
目的,搞这玩意儿,无非想学习下hotspot中的java对象体系。恩直接上代码吧。
做了一个工程:
待观察类:
package hotspot.api.test; /** * hotspot 观察类 * @author * */ public class Wukong { public static int wk_static_i = 7777777; private int wk_instance_i; public Wukong(int wk_instance_i) { this.wk_instance_i = wk_instance_i; } public int getWk_instance_i() { return wk_instance_i; } }
运行线程:
package hotspot.api.test; import java.io.IOException; public class WukongRun { public static void main(String args[]){ new Wukong(8888); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } } }
attach工具:
package hotspot.api.test; import sun.jvm.hotspot.oops.HeapVisitor; import sun.jvm.hotspot.oops.Klass; import sun.jvm.hotspot.oops.ObjectHeap; import sun.jvm.hotspot.oops.Oop; import sun.jvm.hotspot.oops.OopPrinter; import sun.jvm.hotspot.runtime.VM; public class HotspotApiTest extends sun.jvm.hotspot.tools.Tool { public static void main(String args[]) { HotspotApiTest ht = new HotspotApiTest(); ht.start(args); ht.stop(); } @Override public void run() { VM vm = VM.getVM(); final ObjectHeap objectHeap = vm.getObjectHeap(); objectHeap.iterate(new HeapVisitor() { @Override public void prologue(long l) { } @Override public boolean doObj(Oop oop) { System.out.println("////////////////////////////////////////"); System.out.println("OOP#" + oop); oop.iterate(new OopPrinter(System.out), true); System.out.println("////////////////////////////////////////"); System.out.println("OOP.KLASS#" + oop.getKlass()); oop.getKlass().iterate(new OopPrinter(System.out), true); System.out.println("////////////////////////////////////////"); System.out.println("OOP.KLASS.MIRROR#" + oop.getKlass().getJavaMirror()); oop.getKlass().getJavaMirror().iterate(new OopPrinter(System.out), true); System.out.println("////////////////////////////////////////"); System.out.println("OOP.KLASS.KLASS#" + oop.getKlass().getKlass()); oop.getKlass().getKlass().iterate(new OopPrinter(System.out), true); System.out.println("////////////////////////////////////////"); System.out.println("OOP.KLASS.KLASS.KLASS#" + oop.getKlass().getKlass().getKlass()); oop.getKlass().getKlass().getKlass().iterate(new OopPrinter(System.out), true); System.out.println("////////////////////////////////////////"); System.out.println("OOP.KLASS.KLASS.KLASS.KLASS#" + oop.getKlass().getKlass().getKlass().getKlass()); oop.getKlass().getKlass().getKlass().getKlass().iterate(new OopPrinter(System.out), true); return false; } @Override public void epilogue() { } }, new ObjectHeap.ObjectFilter() { @Override public boolean canInclude(Oop oop) { Klass klass = oop.getKlass(); return klass.getName() != null && "hotspot/api/test/Wukong".equals(klass.getName().asString()); } }); } }
首先把代码编译一下:
javac -cp /home/software/jdk1.7.0_80/lib/sa-jdi.jar -d bin -sourcepath src src/main/java/hotspot/api/test/*.java
这里边要用到sa-jdi.jar这个包;eclipse下需要专门在jre配置里导入这个包才能正常编译。
然后先到bin目录下执行:
java hotspot.api.test.WukongRun
新开窗口,执行:
java -classpath /home/software/jdk1.7.0_80/lib/sa-jdi.jar: hotspot.api.test.HotspotApiTest 3475
3475就是上边WukongRun的线程id。
输出结果:
//////////////////////////////////////// OOP#sun.jvm.hotspot.oops.Instance@ef44eb00 Oop for hotspot/api/test/Wukong @ 0x00000000ef44eb00 (object size = 16) - _mark: {0} :1 - _metadata._compressed_klass: {8} :InstanceKlass for hotspot/api/test/Wukong @ 0x00000000fb062e60 - wk_instance_i: {12} :8888 //////////////////////////////////////// OOP.KLASS#sun.jvm.hotspot.oops.InstanceKlass@fb062e60 InstanceKlass for hotspot/api/test/Wukong @ 0x00000000fb062e60 (object size = 568) - _mark: {0} :1 - _metadata._compressed_klass: {8} :InstanceKlassKlass @ 0x00000000fae00270 - _java_mirror: {120} :Oop for java/lang/Class @ 0x00000000ef44eaa0 - _super: {128} :InstanceKlass for java/lang/Object @ 0x00000000fae02bb0 - _layout_helper: {24} :16 - _access_flags: {156} :33 - _subklass: {136} :null - _next_sibling: {144} :InstanceKlass for java/lang/Void @ 0x00000000fb0627c0 - _alloc_count: {160} :0 - _array_klasses: {200} :null - _methods: {208} :ObjArray @ 0x00000000fb062bd0 - _method_ordering: {216} :[I @ 0x00000000fae01c30 - _local_interfaces: {224} :ObjArray @ 0x00000000fae01c40 - _transitive_interfaces: {232} :ObjArray @ 0x00000000fae01c40 - _fields: {240} :[S @ 0x00000000fb062ba8 - _constants: {248} :ConstantPool for hotspot/api/test/Wukong @ 0x00000000fb062a70 - _class_loader: {256} :Oop for sun/misc/Launcher$AppClassLoader @ 0x00000000ef442c58 - _protection_domain: {264} :Oop for java/security/ProtectionDomain @ 0x00000000ef44c568 - _signers: {272} :null - _inner_classes: {280} :[S @ 0x00000000fae01c20 - _nonstatic_field_size: {360} :1 - _static_field_size: {364} :1 - _static_oop_field_count: {368} :0 - _nonstatic_oop_map_size: {372} :0 - _is_marked_dependent: {376} :0 - _init_state: {498} :5 - _vtable_len: {392} :6 - _itable_len: {396} :2 //////////////////////////////////////// OOP.KLASS.MIRROR#sun.jvm.hotspot.oops.Instance@ef44eaa0 Oop for java/lang/Class @ 0x00000000ef44eaa0 (object size = 96) - _mark: {0} :1 - _metadata._compressed_klass: {8} :InstanceKlass for java/lang/Class @ 0x00000000fae160c0 - cachedConstructor: {12} :null - newInstanceCallerCache: {16} :null - name: {20} :null - classLoader: {24} :Oop for sun/misc/Launcher$AppClassLoader @ 0x00000000ef442c58 - reflectionData: {28} :null - classRedefinedCount: {72} :0 - genericInfo: {32} :null - enumConstants: {36} :null - enumConstantDirectory: {40} :null - annotations: {44} :null - declaredAnnotations: {48} :null - lastAnnotationsRedefinedCount: {76} :0 - annotationType: {52} :null - classValueMap: {56} :null Oop for java/lang/Class @ 0x00000000ef44eaa0 (object size = 96) - wk_static_i: {88} :7777777 //////////////////////////////////////// OOP.KLASS.KLASS#sun.jvm.hotspot.oops.InstanceKlassKlass@fae00270 InstanceKlassKlass @ 0x00000000fae00270 (object size = 208) - _mark: {0} :1 - _metadata._compressed_klass: {8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {120} :null - _super: {128} :null - _layout_helper: {24} :0 - _access_flags: {156} :0 - _subklass: {136} :null - _next_sibling: {144} :null - _alloc_count: {160} :0 //////////////////////////////////////// OOP.KLASS.KLASS.KLASS#sun.jvm.hotspot.oops.KlassKlass@fae00000 KlassKlass @ 0x00000000fae00000 (object size = 208) - _mark: {0} :1 - _metadata._compressed_klass: {8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {120} :null - _super: {128} :null - _layout_helper: {24} :0 - _access_flags: {156} :0 - _subklass: {136} :null - _next_sibling: {144} :null - _alloc_count: {160} :0 //////////////////////////////////////// OOP.KLASS.KLASS.KLASS.KLASS#sun.jvm.hotspot.oops.KlassKlass@fae00000 KlassKlass @ 0x00000000fae00000 (object size = 208) - _mark: {0} :1 - _metadata._compressed_klass: {8} :KlassKlass @ 0x00000000fae00000 - _java_mirror: {120} :null - _super: {128} :null - _layout_helper: {24} :0 - _access_flags: {156} :0 - _subklass: {136} :null - _next_sibling: {144} :null - _alloc_count: {160} :0
期待成为寂寞高手的武林老白