btraceio / btrace

BTrace - a safe, dynamic tracing tool for the Java platform
5.82k stars 961 forks source link

Fix remapping arrays and improve test coverage #704

Closed jbachorik closed 2 weeks ago

jbachorik commented 2 weeks ago

This is a rather large work to reimplement the indy linking to avoid deadlocks between tracing and linking.

Unfortunately, this involves some hackery - we need to instrument java.lang.invoke.MethodHandleNatives and track the start and end of the linking process. The reason is that we need to hook-in deeply into the JDK linking process, otherwise we are not able to prevent BTrace probes firing while in the indy linking.

That particular instrumentation will increment thread-local value of a linking flag when the linking starts and decrement once the linking is finished. Then, the injected code needs to be adjusted such that the BTrace action invocation is guarded by a check for the linking flag, skipping the action if the linking is in progress.

Once all of this is in place, it is possible to drastically reduce the exclusion filter for 'sensitive' classes, leaving just a bunch of classes that are crucial to BTrace. Here is the minimal list of excluded classes, still allowing full functionality of BTrace

"java/lang/Integer"
"java/lang/Number"
"java/lang/Object"
"java/lang/String"
"java/lang/StringUTF16"
"java/lang/ThreadLocal"
"java/lang/ThreadLocal$ThreadLocalMap"
"java/lang/WeakPairMap"
"java/lang/WeakPairMap$Pair$Weak"

"java/lang/instrument/*"
"java/lang/invoke/*"
"java/lang/ref/*"
"java/util/concurrent/locks/LockSupport"
"java/util/concurrent/locks/AbstractQueuedSynchronizer"
"java/util/concurrent/locks/AbstractQueuedSynchronizer$ExclusiveNode"
"java/util/concurrent/locks/AbstractQueuedSynchronizer$Node"
"java/util/concurrent/locks/AbstractOwnableSynchronizer"
"java/util/concurrent/locks/ReentrantLock"

"jdk/internal/*"
"sun/invoke/*"
"org/openjdk/btrace/*"

While working on this change, the instrumentor tests were converted to parametric tests and using golden files.