1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "jvmti.h" 18 19 // Test infrastructure 20 #include "jvmti_helper.h" 21 #include "scoped_local_ref.h" 22 #include "test_env.h" 23 24 namespace art { 25 namespace Test1909AgentTLS { 26 27 extern "C" JNIEXPORT void JNICALL Java_art_Test1909_setTLS(JNIEnv* env, 28 jclass, 29 jlong jvmti_env_ptr, 30 jthread thr, 31 jlong data) { 32 JvmtiErrorToException(env, 33 reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr), 34 reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr)->SetThreadLocalStorage( 35 thr, reinterpret_cast<const void*>(static_cast<intptr_t>(data)))); 36 } 37 38 extern "C" JNIEXPORT jlong JNICALL Java_art_Test1909_getTLS(JNIEnv* env, 39 jclass, 40 jlong jvmti_env_ptr, 41 jthread thr) { 42 void* res = nullptr; 43 JvmtiErrorToException( 44 env, 45 reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr), 46 reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr)->GetThreadLocalStorage(thr, &res)); 47 return static_cast<jlong>(reinterpret_cast<intptr_t>(res)); 48 } 49 50 extern "C" JNIEXPORT void Java_art_Test1909_destroyJvmtiEnv(JNIEnv* env, 51 jclass, 52 jlong jvmti_env_ptr) { 53 JvmtiErrorToException(env, 54 jvmti_env, 55 reinterpret_cast<jvmtiEnv*>(jvmti_env_ptr)->DisposeEnvironment()); 56 } 57 58 extern "C" JNIEXPORT jlong Java_art_Test1909_newJvmtiEnv(JNIEnv* env, jclass) { 59 JavaVM* vm = nullptr; 60 if (env->GetJavaVM(&vm) != 0) { 61 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException")); 62 env->ThrowNew(rt_exception.get(), "Unable to get JavaVM"); 63 return -1; 64 } 65 jvmtiEnv* new_env = nullptr; 66 if (vm->GetEnv(reinterpret_cast<void**>(&new_env), JVMTI_VERSION_1_0) != 0) { 67 ScopedLocalRef<jclass> rt_exception(env, env->FindClass("java/lang/RuntimeException")); 68 env->ThrowNew(rt_exception.get(), "Unable to create new jvmtiEnv"); 69 return -1; 70 } 71 return static_cast<jlong>(reinterpret_cast<intptr_t>(new_env)); 72 } 73 74 } // namespace Test1909AgentTLS 75 } // namespace art 76