1 /*
   2  * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 #include <string.h>
  24 #include <stdlib.h>
  25 #include <jvmti_aod.h>
  26 
  27 extern "C" {
  28 
  29 void nsk_jvmti_aod_disableEventAndFinish(const char* agentName, jvmtiEvent event, int success, jvmtiEnv *jvmti, JNIEnv* jni) {
  30     if (!nsk_jvmti_aod_disableEvent(jvmti, event))
  31         success = 0;
  32 
  33     nsk_aod_agentFinished(jni, agentName, success);
  34 }
  35 
  36 void nsk_jvmti_aod_disableEventsAndFinish(const char* agentName, jvmtiEvent events[], int eventsNumber, int success, jvmtiEnv *jvmti, JNIEnv* jni) {
  37     if (!nsk_jvmti_aod_disableEvents(jvmti, events, eventsNumber))
  38         success = 0;
  39 
  40     nsk_aod_agentFinished(jni, agentName, success);
  41 }
  42 
  43 /*
  44  * Work with agent options
  45  */
  46 
  47 struct {
  48     jvmtiEnv *jvmti;
  49     Options *options;
  50 } multiagentsOptions[MAX_MULTIPLE_AGENTS];
  51 
  52 static volatile int multiagentsCount = 0;
  53 
  54 int nsk_jvmti_aod_addMultiagentsOptions(jvmtiEnv *jvmti, Options *options) {
  55     if (multiagentsCount >= MAX_MULTIPLE_AGENTS) {
  56         NSK_COMPLAIN1("To many agents, max agents count is %d\n", MAX_MULTIPLE_AGENTS);
  57         return NSK_FALSE;
  58     }
  59 
  60     multiagentsOptions[multiagentsCount].jvmti = jvmti;
  61     multiagentsOptions[multiagentsCount].options = options;
  62     multiagentsCount++;
  63 
  64     NSK_DISPLAY3("Options for agent %s were added (jvmtiEnv: %p, agentsCount: %d)\n",
  65             nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION),
  66             jvmti,
  67             multiagentsCount);
  68 
  69     return NSK_TRUE;
  70 }
  71 
  72 Options* nsk_jvmti_aod_getMultiagentsOptions(jvmtiEnv *jvmti) {
  73     int i;
  74     for (i = 0; i < multiagentsCount; i++) {
  75         if (multiagentsOptions[i].jvmti == jvmti) {
  76             return multiagentsOptions[i].options;
  77         }
  78     }
  79 
  80     NSK_COMPLAIN1("Options for jvmtiEnv %p weren't found\n", jvmti);
  81 
  82     return NULL;
  83 }
  84 
  85 /*
  86  * Auxiliary functions
  87  */
  88 
  89 void nsk_jvmti_aod_deallocate(jvmtiEnv *jvmti, unsigned char* mem) {
  90     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, jvmti, mem))) {
  91         NSK_COMPLAIN0("Deallocate failed\n");
  92 
  93         /*
  94          * if deallocate fails it isn't critical and test execution can continue without problems,
  95          * just call nsk_aod_internal_error to inform framework about this error
  96          */
  97         nsk_aod_internal_error();
  98     }
  99 }
 100 
 101 int nsk_jvmti_aod_getClassName(jvmtiEnv *jvmti, jclass klass, char classNameBuffer[]) {
 102     char* className;
 103 
 104     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti, klass, &className, NULL))) {
 105         NSK_COMPLAIN0("Failed to get class name\n");
 106         classNameBuffer[0] = '\0';
 107         return NSK_FALSE;
 108     } else {
 109         strcpy(classNameBuffer, className);
 110 
 111         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)className);
 112 
 113         return NSK_TRUE;
 114     }
 115 }
 116 
 117 int nsk_jvmti_aod_getThreadName(jvmtiEnv * jvmti, jthread thread, char threadNameBuffer[]) {
 118     jvmtiThreadInfo info;
 119     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadInfo, jvmti, thread, &info))){
 120         NSK_COMPLAIN0("Failed to get thread info\n");
 121         threadNameBuffer[0] = '\0';
 122         return NSK_FALSE;
 123     } else {
 124         strcpy(threadNameBuffer, info.name);
 125 
 126         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)info.name);
 127 
 128         return NSK_TRUE;
 129     }
 130 }
 131 
 132 // events enabling/disabling
 133 
 134 int nsk_jvmti_aod_disableEvents(jvmtiEnv* jvmti, jvmtiEvent events[], int eventsNumber) {
 135     int i;
 136     int status = NSK_TRUE;
 137 
 138     for (i = 0; i < eventsNumber; i++) {
 139         if (!nsk_jvmti_aod_disableEvent(jvmti, events[i])) {
 140             status = NSK_FALSE;
 141         }
 142     }
 143 
 144     return status;
 145 }
 146 
 147 int nsk_jvmti_aod_enableEvents(jvmtiEnv* jvmti, jvmtiEvent events[], int eventsNumber) {
 148     int i;
 149     for (i = 0; i < eventsNumber; i++) {
 150         if (!nsk_jvmti_aod_enableEvent(jvmti, events[i]))
 151             return NSK_FALSE;
 152     }
 153 
 154     return NSK_TRUE;
 155 }
 156 
 157 // java threads creation
 158 
 159 jthread nsk_jvmti_aod_createThread(JNIEnv *jni) {
 160     jclass klass ;
 161     jmethodID threadConstructor;
 162     jthread thread;
 163 
 164     if (!NSK_JNI_VERIFY(jni,
 165             (klass = NSK_CPP_STUB2(FindClass, jni, "java/lang/Thread")) != NULL )) {
 166         NSK_COMPLAIN0("Failed to get the java.lang.Thread class\n");
 167         return NULL;
 168     }
 169     if (!NSK_JNI_VERIFY(jni,
 170             (threadConstructor = NSK_CPP_STUB4(GetMethodID, jni, klass, "<init>", "()V") )  != NULL )) {
 171         NSK_COMPLAIN0("Failed to get java.lang.Thread constructor\n");
 172         return NULL;
 173     }
 174 
 175     if (!NSK_JNI_VERIFY (jni,
 176             (thread = NSK_CPP_STUB4(NewObject, jni, klass, threadConstructor, NULL)) != NULL ) ) {
 177         NSK_COMPLAIN0("Failed to create Thread object\n");
 178         return NULL;
 179     }
 180 
 181     if (!NSK_JNI_VERIFY(jni, (thread =
 182         NSK_CPP_STUB2(NewGlobalRef, jni, thread)) != NULL)) {
 183         NSK_COMPLAIN0("Failed to create global reference\n");
 184         return NULL;
 185     }
 186 
 187     return thread;
 188 }
 189 
 190 jthread nsk_jvmti_aod_createThreadWithName(JNIEnv *jni, const char* threadName) {
 191     jclass klass ;
 192     jmethodID threadConstructor;
 193     jthread thread;
 194     jstring threadNameString;
 195 
 196     if (!NSK_JNI_VERIFY(jni, (threadNameString =
 197         NSK_CPP_STUB2(NewStringUTF, jni, threadName)) != NULL))
 198         return NULL;
 199 
 200     if (!NSK_JNI_VERIFY(jni,
 201             (klass = NSK_CPP_STUB2(FindClass, jni, "java/lang/Thread")) != NULL )) {
 202         NSK_COMPLAIN0("Failed to get the java.lang.Thread class\n");
 203         return NULL;
 204     }
 205     if (!NSK_JNI_VERIFY(jni,
 206             (threadConstructor = NSK_CPP_STUB4(GetMethodID, jni, klass, "<init>", "(Ljava/lang/String;)V") )  != NULL )) {
 207         NSK_COMPLAIN0("Failed to get java.lang.Thread constructor\n");
 208         return NULL;
 209     }
 210 
 211     if (!NSK_JNI_VERIFY(jni,
 212             (thread = NSK_CPP_STUB4(NewObject, jni, klass, threadConstructor, threadNameString)) != NULL ) ) {
 213         NSK_COMPLAIN0("Failed to create Thread object\n");
 214         return NULL;
 215     }
 216 
 217     if (!NSK_JNI_VERIFY(jni, (thread =
 218         NSK_CPP_STUB2(NewGlobalRef, jni, thread)) != NULL)) {
 219         NSK_COMPLAIN0("Failed to create global reference\n");
 220         return NULL;
 221     }
 222 
 223     return thread;
 224 }
 225 
 226 // class redefinition
 227 
 228 int nsk_jvmti_aod_redefineClass(
 229         Options * options,
 230         jvmtiEnv * jvmti,
 231         jclass classToRedefine,
 232         const char * fileName) {
 233 
 234     if (!nsk_aod_optionSpecified(options, PATH_TO_NEW_BYTE_CODE_OPTION)) {
 235         NSK_COMPLAIN1("Option '%s' isn't specified\n", PATH_TO_NEW_BYTE_CODE_OPTION);
 236         return NSK_FALSE;
 237     }
 238     if (fileName == NULL) {
 239         NSK_COMPLAIN0("File name is NULL\n");
 240         return NSK_FALSE;
 241     }
 242     {
 243         char file [1024];
 244 
 245         sprintf(file,"%s/%s.class",
 246                 nsk_aod_getOptionValue(options, PATH_TO_NEW_BYTE_CODE_OPTION),
 247                 fileName);
 248         NSK_DISPLAY1("File with new bytecode: '%s'\n", file);
 249 
 250         {
 251             FILE *bytecode;
 252             unsigned char * classBytes;
 253             jvmtiError error;
 254             jint size;
 255 
 256             bytecode = fopen(file, "rb");
 257             error= JVMTI_ERROR_NONE;
 258             if ( bytecode == NULL ) {
 259                 NSK_COMPLAIN1("Error opening file '%s'\n", file);
 260                 return NSK_FALSE;
 261             }
 262 
 263             NSK_DISPLAY1("Opening file '%s' \n", file);
 264             fseek(bytecode, 0, SEEK_END);
 265             size = ftell(bytecode);
 266             NSK_DISPLAY1("File size= %ld\n", ftell(bytecode));
 267             rewind(bytecode);
 268             error = jvmti->Allocate(size, &classBytes);
 269             if ( error != JVMTI_ERROR_NONE) {
 270                 NSK_DISPLAY1("Failed to create memory %s\n", TranslateError(error));
 271                 return NSK_FALSE;
 272             }
 273 
 274             if ( ((jint) fread( classBytes, 1, size, bytecode )) != size ) {
 275                 NSK_COMPLAIN0("Failed to read all the bytes, could be less or more\n");
 276                 fclose(bytecode);
 277                 return NSK_FALSE;
 278             } else {
 279                 NSK_DISPLAY0("File read completely \n");
 280             }
 281             fclose(bytecode);
 282             {
 283                 jvmtiClassDefinition classDef;
 284                 classDef.klass = classToRedefine;
 285                 classDef.class_byte_count= size;
 286                 classDef.class_bytes = classBytes;
 287                 NSK_DISPLAY0("Redefining\n");
 288                 error = jvmti->RedefineClasses(1, &classDef);
 289                 if ( error != JVMTI_ERROR_NONE ) {
 290                     NSK_DISPLAY1("# error occured while redefining %s ",
 291                             TranslateError(error) );
 292                     return NSK_FALSE;
 293                 }
 294             }
 295         }
 296     }
 297     return NSK_TRUE;
 298 }
 299 
 300 // capabilities
 301 
 302 void printCapabilities(jvmtiCapabilities caps) {
 303     #define printCap(name) NSK_DISPLAY1(#name ": %d\n", caps.name)
 304 
 305     printCap(can_tag_objects);
 306     printCap(can_generate_field_modification_events);
 307     printCap(can_generate_field_access_events);
 308     printCap(can_get_bytecodes);
 309     printCap(can_get_synthetic_attribute);
 310     printCap(can_get_owned_monitor_info);
 311     printCap(can_get_current_contended_monitor);
 312     printCap(can_get_monitor_info);
 313     printCap(can_pop_frame);
 314     printCap(can_redefine_classes);
 315     printCap(can_signal_thread);
 316     printCap(can_get_source_file_name);
 317     printCap(can_get_line_numbers);
 318     printCap(can_get_source_debug_extension);
 319     printCap(can_access_local_variables);
 320     printCap(can_maintain_original_method_order);
 321     printCap(can_generate_single_step_events);
 322     printCap(can_generate_exception_events);
 323     printCap(can_generate_frame_pop_events);
 324     printCap(can_generate_breakpoint_events);
 325     printCap(can_suspend);
 326     printCap(can_redefine_any_class);
 327     printCap(can_get_current_thread_cpu_time);
 328     printCap(can_get_thread_cpu_time);
 329     printCap(can_generate_method_entry_events);
 330     printCap(can_generate_method_exit_events);
 331     printCap(can_generate_all_class_hook_events);
 332     printCap(can_generate_compiled_method_load_events);
 333     printCap(can_generate_monitor_events);
 334     printCap(can_generate_vm_object_alloc_events);
 335     printCap(can_generate_native_method_bind_events);
 336     printCap(can_generate_garbage_collection_events);
 337     printCap(can_generate_object_free_events);
 338     printCap(can_force_early_return);
 339     printCap(can_get_owned_monitor_stack_depth_info);
 340     printCap(can_get_constant_pool);
 341     printCap(can_set_native_method_prefix);
 342     printCap(can_retransform_classes);
 343     printCap(can_retransform_any_class);
 344 
 345     #undef printCap
 346 }
 347 
 348 }