Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "901-hello-ti-agent/basics.h" |
| 18 | |
| 19 | #include <jni.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include "base/macros.h" |
| 23 | #include "openjdkjvmti/jvmti.h" |
| 24 | |
| 25 | namespace art { |
| 26 | namespace Test901HelloTi { |
| 27 | |
| 28 | jint OnLoad(JavaVM* vm, |
| 29 | char* options ATTRIBUTE_UNUSED, |
| 30 | void* reserved ATTRIBUTE_UNUSED) { |
| 31 | printf("Loaded Agent for test 901-hello-ti-agent\n"); |
| 32 | fsync(1); |
| 33 | jvmtiEnv* env = nullptr; |
| 34 | jvmtiEnv* env2 = nullptr; |
| 35 | |
| 36 | #define CHECK_CALL_SUCCESS(c) \ |
| 37 | do { \ |
Chih-Hung Hsieh | 1a0de6a | 2016-08-26 15:06:11 -0700 | [diff] [blame] | 38 | if ((c) != JNI_OK) { \ |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 39 | printf("call " #c " did not succeed\n"); \ |
| 40 | return -1; \ |
| 41 | } \ |
| 42 | } while (false) |
| 43 | |
| 44 | CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env), JVMTI_VERSION_1_0)); |
| 45 | CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env2), JVMTI_VERSION_1_0)); |
| 46 | if (env == env2) { |
| 47 | printf("GetEnv returned same environment twice!\n"); |
| 48 | return -1; |
| 49 | } |
| 50 | unsigned char* local_data = nullptr; |
| 51 | CHECK_CALL_SUCCESS(env->Allocate(8, &local_data)); |
| 52 | strcpy(reinterpret_cast<char*>(local_data), "hello!!"); |
| 53 | CHECK_CALL_SUCCESS(env->SetEnvironmentLocalStorage(local_data)); |
| 54 | unsigned char* get_data = nullptr; |
| 55 | CHECK_CALL_SUCCESS(env->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data))); |
| 56 | if (get_data != local_data) { |
| 57 | printf("Got different data from local storage then what was set!\n"); |
| 58 | return -1; |
| 59 | } |
| 60 | CHECK_CALL_SUCCESS(env2->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data))); |
| 61 | if (get_data != nullptr) { |
| 62 | printf("env2 did not have nullptr local storage.\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | CHECK_CALL_SUCCESS(env->Deallocate(local_data)); |
| 66 | jint version = 0; |
| 67 | CHECK_CALL_SUCCESS(env->GetVersionNumber(&version)); |
| 68 | if ((version & JVMTI_VERSION_1) != JVMTI_VERSION_1) { |
| 69 | printf("Unexpected version number!\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | CHECK_CALL_SUCCESS(env->DisposeEnvironment()); |
| 73 | CHECK_CALL_SUCCESS(env2->DisposeEnvironment()); |
| 74 | #undef CHECK_CALL_SUCCESS |
| 75 | return JNI_OK; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | } // namespace Test901HelloTi |
| 80 | } // namespace art |