blob: 0b17656303303e0940f094d6144ebd59c6295f94 [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
Andreas Gampe3a7eb142017-01-19 21:59:22 -080031static void EnableEvent(jvmtiEnv* env, jvmtiEvent evt) {
32 jvmtiError error = env->SetEventNotificationMode(JVMTI_ENABLE, evt, nullptr);
33 if (error != JVMTI_ERROR_NONE) {
34 printf("Failed to enable event");
35 }
36}
37
38static void JNICALL VMStartCallback(jvmtiEnv *jenv ATTRIBUTE_UNUSED,
39 JNIEnv* jni_env ATTRIBUTE_UNUSED) {
40 printf("VMStart\n");
41}
42
43static void JNICALL VMInitCallback(jvmtiEnv *jvmti_env ATTRIBUTE_UNUSED,
44 JNIEnv* jni_env ATTRIBUTE_UNUSED,
45 jthread thread ATTRIBUTE_UNUSED) {
46 printf("VMInit\n");
47}
48
49static void JNICALL VMDeatchCallback(jvmtiEnv *jenv ATTRIBUTE_UNUSED,
50 JNIEnv* jni_env ATTRIBUTE_UNUSED) {
51 printf("VMDeath\n");
52}
53
54
55static void InstallVMEvents(jvmtiEnv* env) {
56 jvmtiEventCallbacks callbacks;
57 memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
58 callbacks.VMStart = VMStartCallback;
59 callbacks.VMInit = VMInitCallback;
60 callbacks.VMDeath = VMDeatchCallback;
61 jvmtiError ret = env->SetEventCallbacks(&callbacks, sizeof(callbacks));
62 if (ret != JVMTI_ERROR_NONE) {
63 printf("Failed to install callbacks");
64 }
65
66 EnableEvent(env, JVMTI_EVENT_VM_START);
67 EnableEvent(env, JVMTI_EVENT_VM_INIT);
68 EnableEvent(env, JVMTI_EVENT_VM_DEATH);
69}
70
Alex Light49948e92016-08-11 15:35:28 -070071jint OnLoad(JavaVM* vm,
72 char* options ATTRIBUTE_UNUSED,
73 void* reserved ATTRIBUTE_UNUSED) {
74 printf("Loaded Agent for test 901-hello-ti-agent\n");
75 fsync(1);
76 jvmtiEnv* env = nullptr;
77 jvmtiEnv* env2 = nullptr;
78
79#define CHECK_CALL_SUCCESS(c) \
80 do { \
Chih-Hung Hsieh1a0de6a2016-08-26 15:06:11 -070081 if ((c) != JNI_OK) { \
Alex Light49948e92016-08-11 15:35:28 -070082 printf("call " #c " did not succeed\n"); \
83 return -1; \
84 } \
85 } while (false)
86
87 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env), JVMTI_VERSION_1_0));
88 CHECK_CALL_SUCCESS(vm->GetEnv(reinterpret_cast<void**>(&env2), JVMTI_VERSION_1_0));
89 if (env == env2) {
90 printf("GetEnv returned same environment twice!\n");
91 return -1;
92 }
93 unsigned char* local_data = nullptr;
94 CHECK_CALL_SUCCESS(env->Allocate(8, &local_data));
95 strcpy(reinterpret_cast<char*>(local_data), "hello!!");
96 CHECK_CALL_SUCCESS(env->SetEnvironmentLocalStorage(local_data));
97 unsigned char* get_data = nullptr;
98 CHECK_CALL_SUCCESS(env->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data)));
99 if (get_data != local_data) {
100 printf("Got different data from local storage then what was set!\n");
101 return -1;
102 }
103 CHECK_CALL_SUCCESS(env2->GetEnvironmentLocalStorage(reinterpret_cast<void**>(&get_data)));
104 if (get_data != nullptr) {
105 printf("env2 did not have nullptr local storage.\n");
106 return -1;
107 }
108 CHECK_CALL_SUCCESS(env->Deallocate(local_data));
109 jint version = 0;
110 CHECK_CALL_SUCCESS(env->GetVersionNumber(&version));
111 if ((version & JVMTI_VERSION_1) != JVMTI_VERSION_1) {
112 printf("Unexpected version number!\n");
113 return -1;
114 }
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800115
116 InstallVMEvents(env);
117 InstallVMEvents(env2);
118
Alex Light49948e92016-08-11 15:35:28 -0700119 CHECK_CALL_SUCCESS(env->DisposeEnvironment());
120 CHECK_CALL_SUCCESS(env2->DisposeEnvironment());
121#undef CHECK_CALL_SUCCESS
Andreas Gampef37e3022017-01-13 17:54:46 -0800122
123 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
124 printf("Unable to get jvmti env!\n");
125 return 1;
126 }
127 SetAllCapabilities(jvmti_env);
128
Andreas Gampe96eca782017-01-19 19:45:30 -0800129 jvmtiPhase current_phase;
130 jvmtiError phase_result = jvmti_env->GetPhase(&current_phase);
131 if (phase_result != JVMTI_ERROR_NONE) {
132 printf("Could not get phase");
133 return 1;
134 }
135 if (current_phase != JVMTI_PHASE_ONLOAD) {
136 printf("Wrong phase");
137 return 1;
138 }
139
Andreas Gampe3a7eb142017-01-19 21:59:22 -0800140 InstallVMEvents(jvmti_env);
141
Alex Light49948e92016-08-11 15:35:28 -0700142 return JNI_OK;
143}
144
Andreas Gampef37e3022017-01-13 17:54:46 -0800145extern "C" JNIEXPORT void JNICALL Java_Main_setVerboseFlag(
146 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jint iflag, jboolean val) {
147 jvmtiVerboseFlag flag = static_cast<jvmtiVerboseFlag>(iflag);
148 jvmtiError result = jvmti_env->SetVerboseFlag(flag, val);
149 JvmtiErrorToException(env, result);
150}
Alex Light49948e92016-08-11 15:35:28 -0700151
Andreas Gampe96eca782017-01-19 19:45:30 -0800152extern "C" JNIEXPORT jboolean JNICALL Java_Main_checkLivePhase(
153 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
154 jvmtiPhase current_phase;
155 jvmtiError phase_result = jvmti_env->GetPhase(&current_phase);
156 if (JvmtiErrorToException(env, phase_result)) {
157 return JNI_FALSE;
158 }
159 return (current_phase == JVMTI_PHASE_LIVE) ? JNI_TRUE : JNI_FALSE;
160}
161
Alex Light49948e92016-08-11 15:35:28 -0700162} // namespace Test901HelloTi
163} // namespace art