I'd like to create a reverse debugger (a debugger where it is possible to go backwards in program execution) for Java and for this I need to store variable data alongside program execution. I will use a global cache for this and a static method which updates the cache.
I'd like to instrument loaded classes in such a way that after each field/variable modification, my static method will be called: e.g:
public static void updateCache(String fullVarName, Object value){...}
What I observed is that when a field is updated, a putfield
instruction is executed. When a local variable is updated, an (I)STORE
instruction is used. So I thought of instrumenting the classes and whenever such an opcode is found, I simply insert another getfield
or ILOAD
after, to get the value of the updated field/variable and then I use an invokestatic
to call my static method with all the necessary information.
The problem is that there are other use cases where variables are collections or arrays and they are updated with specific methods like when updating a HashMap
with map.put(key, value)
. So I need to intercept these calls as well, but there are a high number of such methods and I need to find and hardcode them all...
Is there a workaround? Or maybe I am missing something and there is a simpler solution.
Edit: I've also looked into JVMTI before and ran some benchmarks. It seemed that it is too slow for my use case e.g. adds an 7-100x slowdown to my program.....