3
votes

I am using asm to do byte code instrumentation into java function. Is there a way to do the same for jre native functions? Maybe a way to wrap the native function with a java function?

My purpose at the end is to get a list of all functions that ran with their arguments.

1

1 Answers

3
votes

Since the native methods are declared in the class file, you can replace them just like any other method. The tricky part is how to invoke the original method. To achieve this you can create another native method which will be linked to the original native code.

This must happen at loading time as most JVMs do not support adding methods afterwards. There is a feature to help linking your new helper method to the original code, the native method prefix. The documentation of Instrumentation.setNativeMethodPrefix explains the details.