I am trying to track allocations of all objects in JVM.
In the several documents about allocation profilers it was mentioned that the easiest way to do so is this: add
invokestatic Tracker.trackAllocation()V instruction to
java/lang/Object.<init> (normally it consists of a single
return instruction, we add
invokestatic before it, so it is 2 instructions now).
(I know that this approach is slow and will not track array allocations, but I wanted to start with simplest solution. Also I don't pass reference to the allocated object to tracker, but this will be added later.)
Class file is instrumented with JVM TI agent in onClassLoaded hook.
However, after adding
invokestatic instruction JVM crashes with segfault. Tracker object is added to bootstrap classloader, so it should be visible at any stage. I tried adding
nop instead of
invokestatic, and JVM works fine with the modified Object class. So the problem is specifically with the invocation of some static method.
I also tried to instrument application (not part of base) classes and it worked fine - tracker was called and no crashes happened. Also I tried redefining Object at 2 points: when it is initially loaded (first loaded class), or after vmInit event (when all base classes are loaded and restrictions on jni are lifted).
Is there anything I am missing about instrumenting java.lang.Object?
Code for the agent is here: https://gist.github.com/Korobochka/3bf2f906f6ab85b22dec (Error checking is stripped, code for changing classes is also not included, but it works well enough for other classes)