blob: 3a7d1a1e99802ce32f93db4a2910a4282fea91a6 [file] [log] [blame]
Alex Light49948e92016-08-11 15:35:28 -07001/*
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
Andreas Gampef37e3022017-01-13 17:54:46 -080025#include "ti-agent/common_helper.h"
26#include "ti-agent/common_load.h"
27
Alex Light49948e92016-08-11 15:35:28 -070028namespace art {
29namespace Test901HelloTi {
30
31jint OnLoad(JavaVM* vm,
32 char* options ATTRIBUTE_UNUSED,
33 void* reserved ATTRIBUTE_UNUSED) {
34 printf("Loaded Agent for test 901-hello-ti-agent\n");
35 fsync(1);
36 jvmtiEnv* env = nullptr;
37 jvmtiEnv* env2 = nullptr;
38
39#define CHECK_CALL_SUCCESS(c) \
40 do { \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070041 if ((c) != JNI_OK) { \
Alex Light49948e92016-08-11 15:35:28 -070042 printf("call " #c " did not succeed\n"); \
43 return -1; \
44 } \
45 } while (false)
46
47 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env), JVMTI_VERSION_1_0));
48 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env2), JVMTI_VERSION_1_0));
49 if (env == env2) {
50 printf("GetEnv returned same environment twice!\n");
51 return -1;
52 }
53 unsigned char* local_data = nullptr;
54 CHECK_CALL_SUCCESS(env->Allocate(8, &local_data));
55 strcpy(reinterpret_cast<char*>(local_data), "hello!!");
56 CHECK_CALL_SUCCESS(env->SetEnvironmentLocalStorage(local_data));
57 unsigned char* get_data = nullptr;
58 CHECK_CALL_SUCCESS(env->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data)));
59 if (get_data != local_data) {
60 printf("Got different data from local storage then what was set!\n");
61 return -1;
62 }
63 CHECK_CALL_SUCCESS(env2->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data)));
64 if (get_data != nullptr) {
65 printf("env2 did not have nullptr local storage.\n");
66 return -1;
67 }
68 CHECK_CALL_SUCCESS(env->Deallocate(local_data));
69 jint version = 0;
70 CHECK_CALL_SUCCESS(env->GetVersionNumber(&version));
71 if ((version & JVMTI_VERSION_1) != JVMTI_VERSION_1) {
72 printf("Unexpected version number!\n");
73 return -1;
74 }
75 CHECK_CALL_SUCCESS(env->DisposeEnvironment());
76 CHECK_CALL_SUCCESS(env2->DisposeEnvironment());
77#undef CHECK_CALL_SUCCESS
Andreas Gampef37e3022017-01-13 17:54:46 -080078
79 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
80 printf("Unable to get jvmti env!\n");
81 return 1;
82 }
83 SetAllCapabilities(jvmti_env);
84
Andreas Gampe96eca782017-01-19 19:45:30 -080085 jvmtiPhase current_phase;
86 jvmtiError phase_result = jvmti_env->GetPhase(&current_phase);
87 if (phase_result != JVMTI_ERROR_NONE) {
88 printf("Could not get phase");
89 return 1;
90 }
91 if (current_phase != JVMTI_PHASE_ONLOAD) {
92 printf("Wrong phase");
93 return 1;
94 }
95
Alex Light49948e92016-08-11 15:35:28 -070096 return JNI_OK;
97}
98
Andreas Gampef37e3022017-01-13 17:54:46 -080099extern "C" JNIEXPORT void JNICALL Java_Main_setVerboseFlag(
100 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jint iflag, jboolean val) {
101 jvmtiVerboseFlag flag = static_cast<jvmtiVerboseFlag>(iflag);
102 jvmtiError result = jvmti_env->SetVerboseFlag(flag, val);
103 JvmtiErrorToException(env, result);
104}
Alex Light49948e92016-08-11 15:35:28 -0700105
Andreas Gampe96eca782017-01-19 19:45:30 -0800106extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkLivePhase(
107 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
108 jvmtiPhase current_phase;
109 jvmtiError phase_result = jvmti_env->GetPhase(&current_phase);
110 if (JvmtiErrorToException(env, phase_result)) {
111 return JNI_FALSE;
112 }
113 return (current_phase == JVMTI_PHASE_LIVE) ? JNI_TRUE : JNI_FALSE;
114}
115
Alex Light49948e92016-08-11 15:35:28 -0700116} // namespace Test901HelloTi
117} // namespace art