Java – how to debug bytecode injected into VM when class is loaded?
Context:
Problem: at runtime, the value of the instance variable is not set as expected - which means that my bytecode injection obviously doesn't work properly
Question: 1) how to check the contents of the (newly injected and modified) classes loaded in the JVM at runtime? (javap helps existing courses perform this operation) 2) can I debug and inject bytecode code through eclipse? Is there a plug-in?
Any suggestions are appreciated
Solution
You can use Java assist
Let's go step by step:
>Gets the contents of the class file (for example, point. Class) to be modified by bytecode injection
BufferedInputStream fin = new BufferedInputStream(new FileInputStream(“Point.class”)); ClassFile cf = new ClassFile(new DataInputStream(fin));
>Classfile provides addfield () and Addmethod () to add fields or methods (note that constructors are treated as bytecode level methods) It also provides addAttribute () for adding attributes to class files
Note that fieldinfo, methodinfo, and attributeinfo objects contain links to constpool (constant pool table) objects The constpool object must be the same as the classfile object and the fieldinfo (or methodinfo, etc.) object added to the classfile object In other words, fieldinfo (or methodinfo, etc.) objects cannot be shared between different classfile objects
To delete a field or method from a classfile object, you must first get the Java. Net that contains all the fields of the class util. List object Getfields () and getmethods () return lists You can delete fields or methods by calling remove () on the list object You can delete attributes in a similar way Call getAttributes () in FieldInfo or MethodInfo to get the list of attributes and delete one from the list. > Now, check if the injection is effective:
MethodInfo minfo = cf.getmethod(“move”); // We assume that the movement is not overloaded
CodeAttribute ca = minfo. getCodeAttribute();
There are many methods to check in methodinfo / codeattribute
If you like, please let me know I'll be on that case http://puspendu.wordpress.com/ Post a more detailed blog
Reference: here