1 /* 2 * Copyright (c) 2003, 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 24 #include <string.h> 25 #include "jvmti.h" 26 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 30 extern "C" { 31 32 /* ============================================================================= */ 33 34 /* scaffold objects */ 35 static jlong timeout = 0; 36 37 /* constant names */ 38 #define THREAD_NAME "TestedThread" 39 40 /* constants */ 41 #define STORAGE_DATA_SIZE 1024 42 #define STORAGE_DATA_CHAR 'X' 43 44 /* storage structure */ 45 typedef struct _StorageStructure { 46 char data[STORAGE_DATA_SIZE]; 47 } StorageStructure; 48 49 /* ============================================================================= */ 50 51 /** Agent algorithm. */ 52 static void JNICALL 53 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 54 55 NSK_DISPLAY0("Wait for thread to start\n"); 56 if (!nsk_jvmti_waitForSync(timeout)) 57 return; 58 59 /* perform testing */ 60 { 61 StorageStructure storageData; 62 StorageStructure* initialStorage = &storageData; 63 StorageStructure* obtainedStorage = NULL; 64 65 memset(storageData.data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE); 66 67 NSK_DISPLAY1("SetThreadLocalStorage() for current agent thread with pointer: %p\n", 68 (void*)initialStorage); 69 if (!NSK_JVMTI_VERIFY( 70 NSK_CPP_STUB3(SetThreadLocalStorage, jvmti, 71 NULL, (void*)initialStorage))) { 72 nsk_jvmti_setFailStatus(); 73 return; 74 } 75 76 NSK_DISPLAY0("Let debuggee to run\n"); 77 if (!nsk_jvmti_resumeSync()) 78 return; 79 80 NSK_DISPLAY0("Wait for debuggee to run\n"); 81 if (!nsk_jvmti_waitForSync(timeout)) 82 return; 83 84 NSK_DISPLAY0("GetThreadLocalStorage() for current agent thread\n"); 85 if (!NSK_JVMTI_VERIFY( 86 NSK_CPP_STUB3(GetThreadLocalStorage, jvmti, 87 NULL, (void**)&obtainedStorage))) { 88 nsk_jvmti_setFailStatus(); 89 return; 90 } 91 NSK_DISPLAY1(" ... got storage: %p\n", (void*)obtainedStorage); 92 93 NSK_DISPLAY0("Check storage data obtained for current agent thread\n"); 94 if (obtainedStorage != initialStorage) { 95 NSK_COMPLAIN2("Wrong storage pointer returned for tested thread:\n" 96 "# got pointer: %p\n" 97 "# expected: %p\n", 98 (void*)obtainedStorage, (void*)initialStorage); 99 nsk_jvmti_setFailStatus(); 100 } else { 101 int changed = 0; 102 int i; 103 104 for (i = 0; i < STORAGE_DATA_SIZE; i++) { 105 if (obtainedStorage->data[i] != STORAGE_DATA_CHAR) { 106 changed++; 107 } 108 } 109 110 if (changed > 0) { 111 NSK_COMPLAIN2("Data changed in returned storage for current agent thread:\n" 112 "# changed bytes: %d\n" 113 "# total bytes: %d\n", 114 changed, STORAGE_DATA_SIZE); 115 nsk_jvmti_setFailStatus(); 116 } 117 } 118 } 119 120 NSK_DISPLAY0("Let debugee to finish\n"); 121 if (!nsk_jvmti_resumeSync()) 122 return; 123 } 124 125 /* ============================================================================= */ 126 127 /** Agent library initialization. */ 128 #ifdef STATIC_BUILD 129 JNIEXPORT jint JNICALL Agent_OnLoad_setthrdstor002(JavaVM *jvm, char *options, void *reserved) { 130 return Agent_Initialize(jvm, options, reserved); 131 } 132 JNIEXPORT jint JNICALL Agent_OnAttach_setthrdstor002(JavaVM *jvm, char *options, void *reserved) { 133 return Agent_Initialize(jvm, options, reserved); 134 } 135 JNIEXPORT jint JNI_OnLoad_setthrdstor002(JavaVM *jvm, char *options, void *reserved) { 136 return JNI_VERSION_1_8; 137 } 138 #endif 139 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 140 jvmtiEnv* jvmti = NULL; 141 142 /* init framework and parse options */ 143 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 144 return JNI_ERR; 145 146 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 147 148 /* create JVMTI environment */ 149 if (!NSK_VERIFY((jvmti = 150 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 151 return JNI_ERR; 152 153 /* register agent proc and arg */ 154 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 155 return JNI_ERR; 156 157 return JNI_OK; 158 } 159 160 /* ============================================================================= */ 161 162 }