本机java字节码检测 [英] native java bytecode instrumentation

查看:219
本文介绍了本机java字节码检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于java中的字节码检测,有asm框架和bcel和javaassist库。



但是我需要在本机代码中进行检测,因为有些java类已经在javaagent运行时加载了,例如java.lang.Thread,java.lang.Class等



是否有用于在本机代码中检测java类的库?



编辑:
似乎有点混乱。



我想要的是:
创建一个本机java代理,它使用JVMTI apis在加载时使用OnClassLoad事件挂钩更改类的字节码。

解决方案

我在博士研究期间遇到了这个问题。对我来说最有效的答案是使用java库(我使用ASM)在单独的JVM中执行字节码修改。



我使用了JVMTI类加载钩子捕获类文件并使用tcp连接将其传输到单独的JVM。在单独的JVM中修改了类后,我将其返回给JVMTI代理,后者将其复制到VM内存中,并将指向修改后的类文件的指针返回给JVM。



<我发现在同一个JVM中编写类很难被分析,因为我想要修改的系统类文件(例如java.lang.Object)必须在我需要的任何类文件之前加载执行编织。在确定最终使用的单独JVM方法之前,我没有成功地搜索c / c ++字节码库。



您可以使用主机名/端口参数化JVMTI代理weaver JVM,或者您可以使用某种形式的发现,具体取决于您的要求。


for bytecode instrumentation in java, there is the asm framework and the bcel and javaassist libraries.

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

is there any library for instrumenting java classes in native code?

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.

解决方案

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).

I used the JVMTI class load hook to capture the class file and transmit it to the separate JVM using a tcp connection. 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.

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. I hunted for c/c++ bytecode libraries without much success, before settling on the separate JVM approach I finally used.

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.

这篇关于本机java字节码检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆