1 /*
2 * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
356 }
357 /* Add this item to the list */
358 next = get_token(next, ",=", gdata->exclude+used, maxlen);
359 /* Check for token scan error */
360 if ( next==NULL ) {
361 fatal_error("ERROR: exclude option error\n");
362 }
363 } else if ( token[0]!=0 ) {
364 /* We got a non-empty token and we don't know what it is. */
365 fatal_error("ERROR: Unknown option: %s\n", token);
366 }
367 /* Get the next token (returns NULL if there are no more) */
368 next = get_token(next, ",=", token, sizeof(token));
369 }
370 }
371
372 /* Agent_OnLoad: This is called immediately after the shared library is
373 * loaded. This is the first code executed.
374 */
375 JNIEXPORT jint JNICALL
376 Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
377 {
378 static GlobalAgentData data;
379 jvmtiEnv *jvmti;
380 jvmtiError error;
381 jint res;
382 jvmtiCapabilities capabilities;
383 jvmtiEventCallbacks callbacks;
384
385 /* Setup initial global agent data area
386 * Use of static/extern data should be handled carefully here.
387 * We need to make sure that we are able to cleanup after ourselves
388 * so anything allocated in this library needs to be freed in
389 * the Agent_OnUnload() function.
390 */
391 (void)memset((void*)&data, 0, sizeof(data));
392 gdata = &data;
393
394 /* First thing we need to do is get the jvmtiEnv* or JVMTI environment */
395 res = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1);
396 if (res != JNI_OK) {
450 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread)NULL);
451 check_jvmti_error(jvmti, error, "Cannot set event notification");
452
453 /* Here we create a raw monitor for our use in this agent to
454 * protect critical sections of code.
455 */
456 error = (*jvmti)->CreateRawMonitor(jvmti, "agent data", &(gdata->lock));
457 check_jvmti_error(jvmti, error, "Cannot create raw monitor");
458
459 /* Add demo jar file to boot classpath */
460 add_demo_jar_to_bootclasspath(jvmti, "minst");
461
462 /* We return JNI_OK to signify success */
463 return JNI_OK;
464 }
465
466 /* Agent_OnUnload: This is called immediately before the shared library is
467 * unloaded. This is the last code executed.
468 */
469 JNIEXPORT void JNICALL
470 Agent_OnUnload(JavaVM *vm)
471 {
472 /* Make sure all malloc/calloc/strdup space is freed */
473 if ( gdata->include != NULL ) {
474 (void)free((void*)gdata->include);
475 gdata->include = NULL;
476 }
477 if ( gdata->exclude != NULL ) {
478 (void)free((void*)gdata->exclude);
479 gdata->exclude = NULL;
480 }
481 }
|
1 /*
2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of Oracle nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
356 }
357 /* Add this item to the list */
358 next = get_token(next, ",=", gdata->exclude+used, maxlen);
359 /* Check for token scan error */
360 if ( next==NULL ) {
361 fatal_error("ERROR: exclude option error\n");
362 }
363 } else if ( token[0]!=0 ) {
364 /* We got a non-empty token and we don't know what it is. */
365 fatal_error("ERROR: Unknown option: %s\n", token);
366 }
367 /* Get the next token (returns NULL if there are no more) */
368 next = get_token(next, ",=", token, sizeof(token));
369 }
370 }
371
372 /* Agent_OnLoad: This is called immediately after the shared library is
373 * loaded. This is the first code executed.
374 */
375 JNIEXPORT jint JNICALL
376 DEF_Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
377 {
378 static GlobalAgentData data;
379 jvmtiEnv *jvmti;
380 jvmtiError error;
381 jint res;
382 jvmtiCapabilities capabilities;
383 jvmtiEventCallbacks callbacks;
384
385 /* Setup initial global agent data area
386 * Use of static/extern data should be handled carefully here.
387 * We need to make sure that we are able to cleanup after ourselves
388 * so anything allocated in this library needs to be freed in
389 * the Agent_OnUnload() function.
390 */
391 (void)memset((void*)&data, 0, sizeof(data));
392 gdata = &data;
393
394 /* First thing we need to do is get the jvmtiEnv* or JVMTI environment */
395 res = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1);
396 if (res != JNI_OK) {
450 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread)NULL);
451 check_jvmti_error(jvmti, error, "Cannot set event notification");
452
453 /* Here we create a raw monitor for our use in this agent to
454 * protect critical sections of code.
455 */
456 error = (*jvmti)->CreateRawMonitor(jvmti, "agent data", &(gdata->lock));
457 check_jvmti_error(jvmti, error, "Cannot create raw monitor");
458
459 /* Add demo jar file to boot classpath */
460 add_demo_jar_to_bootclasspath(jvmti, "minst");
461
462 /* We return JNI_OK to signify success */
463 return JNI_OK;
464 }
465
466 /* Agent_OnUnload: This is called immediately before the shared library is
467 * unloaded. This is the last code executed.
468 */
469 JNIEXPORT void JNICALL
470 DEF_Agent_OnUnload(JavaVM *vm)
471 {
472 /* Make sure all malloc/calloc/strdup space is freed */
473 if ( gdata->include != NULL ) {
474 (void)free((void*)gdata->include);
475 gdata->include = NULL;
476 }
477 if ( gdata->exclude != NULL ) {
478 (void)free((void*)gdata->exclude);
479 gdata->exclude = NULL;
480 }
481 }
|