简体   繁体   English

本机java字节码检测

[英]native java bytecode instrumentation

for bytecode instrumentation in java, there is the asm framework and the bcel and javaassist libraries. 对于java中的字节码检测,有asm框架和bcel和javaassist库。

However I need to do instrumentation in native code, since some java classes are already loaded by the time the javaagent runs, eg java.lang.Thread, java.lang.Class, etc 但是我需要在本机代码中进行检测,因为javaagent运行时已经加载了一些java类,例如java.lang.Thread,java.lang.Class等。

is there any library for instrumenting java classes in native code? 是否有任何库用于在本机代码中检测java类?

Edit: Seems there is a bit of confusion. 编辑:似乎有点混乱。

What I want is: Create a native java agent, which uses JVMTI apis to change the bytecode of a class while its being loaded, using the OnClassLoad event hook. 我想要的是:创建一个本机java代理,它使用JVMTI apis在加载类时使用OnClassLoad事件挂钩更改类的字节码。

I encountered this problem during my doctoral research. 我在博士研究期间遇到了这个问题。 The answer that worked best for me was to perform the byte-code modification in a separate JVM using a java library (I used ASM). 对我来说最有效的答案是使用java库(我使用ASM)在单独的JVM中执行字节码修改。

I used the JVMTI class load hook to capture the class file and transmit it to the separate JVM using a tcp connection. 我使用JVMTI类加载挂钩来捕获类文件,并使用tcp连接将其传输到单独的JVM。 Once the class had been modified within the separate JVM I returned it to the JVMTI Agent, which copies it into VM memory and returns a pointer to the modified class file to the JVM. 在单独的JVM中修改了类后,我将其返回给JVMTI代理,后者将其复制到VM内存中,并将指向修改后的类文件的指针返回给JVM。

I found that it was too difficult to weave classes within the same JVM as was being profiled as the system class files I wanted to modify (java.lang.Object, for example) had to be loaded before any class files I needed to perform weaving. 我发现在同一个JVM中编写类很难被分析,因为我想要修改的系统类文件(例如java.lang.Object)必须在我需要执行编织的任何类文件之前加载。 I hunted for c/c++ bytecode libraries without much success, before settling on the separate JVM approach I finally used. 在确定我最终使用的单独JVM方法之前,我没有成功地寻找c / c ++字节码库。

You can parameterize the JVMTI agent with the hostname/port of the weaver JVM, or you could use some form of discovery, depending on your requirements. 您可以使用weaver JVM的主机名/端口参数化JVMTI代理,也可以使用某种形式的发现,具体取决于您的要求。

The JIT will turn byte code into native code. JIT会将字节代码转换为本机代码。 If you want to produce native code, you need to let the JIT do it or write native code which is called via JNI. 如果要生成本机代码,则需要让JIT执行此操作或编写通过JNI调用的本机代码。

Perhaps what you are trying to achieve can be done simpler another way. 也许你想要实现的目标可以通过另一种方式更简单地完成。

Create a native java agent, which uses JVMTI apis to change the bytecode of a class while its being loaded, using the OnClassLoad event hook. 创建一个本机java代理,它使用JVMTI apis在加载类时使用OnClassLoad事件挂钩更改类的字节码。

Though you don't need to do what you want. 虽然你不需要做你想做的事。 Why make the solution more complicated (and less likely to work) than it needs to be? 为什么使解决方案比需要的更复杂(并且不太可能工作)?

You cannot change the byte code of a class once it has been loaded. 加载后,您无法更改类的字节代码。 You can either make sure your instrumentation runs before it is loaded, or you can create a new ClassLoader, and re-load the classes inside of it by not asking the parent class. 您可以确保在加载之前运行检测,也可以创建新的ClassLoader,并通过不询问父类重新加载其中的类。 You can't use those classes with code loaded outside of the ClassLoader though, as that code will refer to the earlier loaded, non-altered class. 但是,您不能将这些类与在ClassLoader之外加载的代码一起使用,因为该代码将引用先前加载的,未更改的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM