关闭jvm

时间:2016-10-30 16:06:28

标签: java javaagents jvmti

我写了简单的代理人:

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {    
printf("Agent start");
}

简单的客户:

public class Agent {
//java -agentpath:/path_to_lib Agent

public static void main(String[] args) throws InterruptedException {
    System.out.println("Main");
}}

如果我运行程序,我会看到下面的输出:

  

主要

     

代理商开始

为什么方法主要在Agent_OnLoad之前调用?

1 个答案:

答案 0 :(得分:1)

在VM启动期间正在调用var code = ["alert('first');n=1;" , "alert('second');" , "alert('third');IWantThisVariableChanged = true;" , "alert('fourth');"]; var obj = { IWantThisVariableChanged: false, n: null }; function doStuff(o) { let props = Object.keys(o); for (let n = 0; n != 4; n++) { for (var i = 0, prop = void 0; i < props.length; i++) { if (new RegExp(props[i] + "(?=\\s+=|=)").test(code[n])) { prop = props[i]; break; } }; if (prop) { var value = code[n].split(prop).pop().replace(/=|;/g, ""); o[prop] = !isNaN(value) ? Number(value) : Boolean(value); } new Function(code[n])(); } } doStuff(obj); alert("Variable changed? " + obj.IWantThisVariableChanged); console.log(obj);函数,但Agent_OnLoad缓冲区在到达stdout后正在打印。在newline末尾添加\n即可查看其是否有效。以下答案详细解释:

Why does printf not flush after the call unless a newline is in the format string?

此外,您可以通过在printf(如果您正在使用调试器)上设置断点来确认这一点,以确定它在开始时被调用但是不会打印直到缓冲区达到printf个字符。