Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2015
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdlib.h>
  3.  
  4. #include "jni.h"
  5. #include "jvmti.h"
  6.  
  7. void check_jvmti_error(jvmtiEnv *jvmti, jvmtiError errnum, const char *str) {
  8.   if (errnum != JVMTI_ERROR_NONE) {
  9.     char *errnum_str;
  10.  
  11.     errnum_str = NULL;
  12.     (void)(*jvmti)->GetErrorName(jvmti, errnum, &errnum_str);
  13.  
  14.     printf("ERROR: JVMTI: %d(%s): %s\n", errnum,
  15.       (errnum_str == NULL ? "Unknown" : errnum_str),
  16.       (str == NULL ? "" : str));
  17.   }
  18. }
  19.  
  20. void printProperty(jvmtiEnv *jvmti, const char *prop) {
  21.   char *value;
  22.   jvmtiError err = (*jvmti)->GetSystemProperty(jvmti, prop, &value);
  23.   check_jvmti_error(jvmti, err, prop);
  24.   printf("%s = %s\n", prop, value);
  25.   (*jvmti)->Deallocate(jvmti, (unsigned char*)value);
  26. }
  27.  
  28. JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
  29.   jvmtiError err;
  30.   jvmtiEnv *jvmti;
  31.   size_t len;
  32.   char *bcp;
  33.   char *temp;
  34.  
  35.   (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1_1);
  36.  
  37.   err = (*jvmti)->GetSystemProperty(jvmti, "sun.boot.class.path", &bcp);
  38.   check_jvmti_error(jvmti, err, "GetSystemProperty");
  39.  
  40.   len = strlen(bcp) + 20;
  41.   temp = (char*)malloc(len);
  42.   sprintf_s(temp, len, "C:\\patch\\java.base;%s", bcp);
  43.   (*jvmti)->Deallocate(jvmti, (unsigned char*)bcp);
  44.  
  45.   err = (*jvmti)->SetSystemProperty(jvmti, "sun.boot.class.path", temp);
  46.   check_jvmti_error(jvmti, err, "SetSystemProperty");
  47.   free(temp);
  48.  
  49.   printProperty(jvmti, "sun.boot.class.path");
  50.   printProperty(jvmti, "jdk.boot.class.path.append");
  51.  
  52.   printf("Calling AddToBootstrapClassLoaderSearch\n");
  53.   err = (*jvmti)->AddToBootstrapClassLoaderSearch(jvmti, "C:\\XWork\\MinAgent\\target\\MinAgent.jar");
  54.   check_jvmti_error(jvmti, err, "AddToBootstrapClassLoaderSearch");
  55.  
  56.   printProperty(jvmti, "sun.boot.class.path");
  57.   printProperty(jvmti, "jdk.boot.class.path.append");
  58.  
  59.   return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement