test/hotspot/jtreg/testlibrary/jvmti/libSimpleClassFileLoadHook.c
changeset 52319 625f6c742392
parent 47216 71c04702a3d5
equal deleted inserted replaced
52318:124af9276e44 52319:625f6c742392
     1 /*
     1 /*
     2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    89         }
    89         }
    90 
    90 
    91         *new_class_data_len = class_data_len;
    91         *new_class_data_len = class_data_len;
    92         *new_class_data = new_data;
    92         *new_class_data = new_data;
    93 
    93 
    94         fprintf(stderr, "Rewriting done. Replaced %d occurrence(s)\n", count);
    94         fprintf(stderr, "Rewriting done. Replaced %d occurrence(s) of \"%s\" to \"%s\"\n", count, FROM, TO);
    95       }
    95       }
    96     }
    96     }
    97 }
    97 }
       
    98 
       
    99 static int early = 0;
    98 
   100 
    99 static jint init_options(char *options) {
   101 static jint init_options(char *options) {
   100   char* class_name;
   102   char* class_name;
   101   char* from;
   103   char* from;
   102   char* to;
   104   char* to;
   103 
   105 
   104   fprintf(stderr, "Agent library loaded with options = %s\n", options);
   106   fprintf(stderr, "Agent library loaded with options = %s\n", options);
       
   107   if (options != NULL && strncmp(options, "-early,", 7) == 0) {
       
   108     early = 1;
       
   109     options += 7;
       
   110   }
   105   if ((class_name = options) != NULL &&
   111   if ((class_name = options) != NULL &&
   106       (from = strchr(class_name, ',')) != NULL && (from[1] != 0)) {
   112       (from = strchr(class_name, ',')) != NULL && (from[1] != 0)) {
   107     *from = 0;
   113     *from = 0;
   108     from++;
   114     from++;
   109     if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) {
   115     if ((to = strchr(from, ',')) != NULL && (to[1] != 0)) {
   130   return JNI_ERR;
   136   return JNI_ERR;
   131 }
   137 }
   132 
   138 
   133 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
   139 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
   134   int rc;
   140   int rc;
       
   141   jvmtiCapabilities caps;
   135 
   142 
   136   if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {
   143   if ((rc = (*jvm)->GetEnv(jvm, (void **)&jvmti, JVMTI_VERSION_1_1)) != JNI_OK) {
   137     fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc);
   144     fprintf(stderr, "Unable to create jvmtiEnv, GetEnv failed, error = %d\n", rc);
   138     return JNI_ERR;
   145     return JNI_ERR;
   139   }
   146   }
   140   if ((rc = init_options(options)) != JNI_OK) {
   147   if ((rc = init_options(options)) != JNI_OK) {
       
   148     return JNI_ERR;
       
   149   }
       
   150 
       
   151   memset(&caps, 0, sizeof(caps));
       
   152 
       
   153   caps.can_redefine_classes = 1;
       
   154   if (early) {
       
   155     fprintf(stderr, "can_generate_all_class_hook_events/can_generate_early_vmstart/can_generate_early_class_hook_events == 1\n");
       
   156     caps.can_generate_all_class_hook_events = 1;
       
   157     caps.can_generate_early_class_hook_events = 1;
       
   158   }
       
   159   if ((rc = (*jvmti)->AddCapabilities(jvmti, &caps)) != JNI_OK) {
       
   160     fprintf(stderr, "AddCapabilities failed, error = %d\n", rc);
   141     return JNI_ERR;
   161     return JNI_ERR;
   142   }
   162   }
   143 
   163 
   144   (void) memset(&callbacks, 0, sizeof(callbacks));
   164   (void) memset(&callbacks, 0, sizeof(callbacks));
   145   callbacks.ClassFileLoadHook = &ClassFileLoadHook;
   165   callbacks.ClassFileLoadHook = &ClassFileLoadHook;