java agentlib 作用_javaagent加载机制分析

JNIEXPORT jint JNICALL

Agent_OnLoad(JavaVM *vm, char *tail, void * reserved) {

JPLISInitializationError initerror  = JPLIS_INIT_ERROR_NONE;

jint                     result     = JNI_OK;

JPLISAgent *             agent      = NULL;

initerror = createNewJPLISAgent(vm, &agent);

if ( initerror == JPLIS_INIT_ERROR_NONE ) {

int             oldLen, newLen;

char *          jarfile;

char *          options;

jarAttribute*   attributes;

char *          premainClass;

char *          agentClass;

char *          bootClassPath;

/*

* Parse [=options] into jarfile and options

*/

if (parseArgumentTail(tail, &jarfile, &options) != 0) {

fprintf(stderr, "-javaagent: memory allocation failure.\n");

return JNI_ERR;

}

/*

* Agent_OnLoad is specified to provide the agent options

* argument tail in modified UTF8. However for 1.5.0 this is

* actually in the platform encoding - see 5049313.

*

* Open zip/jar file and parse archive. If can‘t be opened or

* not a zip file return error. Also if Premain-Class attribute

* isn‘t present we return an error.

*/

attributes = readAttributes(jarfile);

if (attributes == NULL) {

fprintf(stderr, "Error opening zip file or JAR manifest missing : %s\n", jarfile);

free(jarfile);

if (options != NULL) free(options);

return JNI_ERR;

}

premainClass = getAttribute(attributes, "Premain-Class");

if (premainClass == NULL) {

fprintf(stderr, "Failed to find Premain-Class manifest attribute in %s\n",

jarfile);

free(jarfile);

if (options != NULL) free(options);

freeAttributes(attributes);

return JNI_ERR;

}

/*

* Add to the jarfile

*/

appendClassPath(agent, jarfile);

/*

* The value of the Premain-Class attribute becomes the agent

* class name. The manifest is in UTF8 so need to convert to

* modified UTF8 (see JNI spec).

*/

oldLen = (int)strlen(premainClass);

newLen = modifiedUtf8LengthOfUtf8(premainClass, oldLen);

if (newLen == oldLen) {

premainClass = strdup(premainClass);

} else {

char* str = (char*)malloc( newLen+1 );

if (str != NULL) {

convertUtf8ToModifiedUtf8(premainClass, oldLen, str, newLen);

}

premainClass = str;

}

if (premainClass == NULL) {

fprintf(stderr, "-javaagent: memory allocation failed\n");

free(jarfile);

if (options != NULL) free(options);

freeAttributes(attributes);

return JNI_ERR;

}

/*

* If the Boot-Class-Path attribute is specified then we process

* each relative URL and add it to the bootclasspath.

*/

bootClassPath = getAttribute(attributes, "Boot-Class-Path");

if (bootClassPath != NULL) {

appendBootClassPath(agent, jarfile, bootClassPath);

}

/*

* Convert JAR attributes into agent capabilities

*/

convertCapabilityAtrributes(attributes, agent);

/*

* Track (record) the agent class name and options data

*/

initerror = recordCommandLineData(agent, premainClass, options);

/*

* Clean-up

*/

free(jarfile);

if (options != NULL) free(options);

freeAttributes(attributes);

free(premainClass);

}

switch (initerror) {

case JPLIS_INIT_ERROR_NONE:

result = JNI_OK;

break;

case JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT:

result = JNI_ERR;

fprintf(stderr, "java.lang.instrument/-javaagent: cannot create native agent.\n");

break;

case JPLIS_INIT_ERROR_FAILURE:

result = JNI_ERR;

fprintf(stderr, "java.lang.instrument/-javaagent: initialization of native agent failed.\n");

break;

case JPLIS_INIT_ERROR_ALLOCATION_FAILURE:

result = JNI_ERR;

fprintf(stderr, "java.lang.instrument/-javaagent: allocation failure.\n");

break;

case JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED:

result = JNI_ERR;

fprintf(stderr, "-javaagent: agent class not specified.\n");

break;

default:

result = JNI_ERR;

fprintf(stderr, "java.lang.instrument/-javaagent: unknown error\n");

break;

}

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值