blob: b1e7fce3b5f8ac80c212e2d3f9d47053b0b92bb7 [file] [log] [blame]
Andreas Gampe1bdaf732017-01-09 19:21:06 -08001/*
2 * Copyright (C) 2017 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 "properties.h"
18
19#include <stdio.h>
20
21#include "base/macros.h"
22#include "jni.h"
23#include "openjdkjvmti/jvmti.h"
24#include "ScopedUtfChars.h"
25
26#include "ti-agent/common_helper.h"
27#include "ti-agent/common_load.h"
28
29namespace art {
30namespace Test922Properties {
31
32extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getSystemProperties(
33 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
34 jint count;
35 char** properties;
36 jvmtiError result = jvmti_env->GetSystemProperties(&count, &properties);
37 if (JvmtiErrorToException(env, result)) {
38 return nullptr;
39 }
40
41 auto callback = [&](jint i) -> jstring {
42 char* data = properties[i];
43 if (data == nullptr) {
44 return nullptr;
45 }
46 jstring ret = env->NewStringUTF(data);
47 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(data));
48 return ret;
49 };
50 jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
51
52 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(properties));
53
54 return ret;
55}
56
57extern "C" JNIEXPORT jstring JNICALL Java_Main_getSystemProperty(
58 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key) {
59 ScopedUtfChars string(env, key);
60 if (string.c_str() == nullptr) {
61 return nullptr;
62 }
63
64 char* value = nullptr;
65 jvmtiError result = jvmti_env->GetSystemProperty(string.c_str(), &value);
66 if (JvmtiErrorToException(env, result)) {
67 return nullptr;
68 }
69
70 jstring ret = (value == nullptr) ? nullptr : env->NewStringUTF(value);
71
72 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(value));
73
74 return ret;
75}
76
77extern "C" JNIEXPORT void JNICALL Java_Main_setSystemProperty(
78 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key, jstring value) {
79 ScopedUtfChars key_string(env, key);
80 if (key_string.c_str() == nullptr) {
81 return;
82 }
83 ScopedUtfChars value_string(env, value);
84 if (value_string.c_str() == nullptr) {
85 return;
86 }
87
88 jvmtiError result = jvmti_env->SetSystemProperty(key_string.c_str(), value_string.c_str());
89 if (JvmtiErrorToException(env, result)) {
90 return;
91 }
92}
93
94// Don't do anything
95jint OnLoad(JavaVM* vm,
96 char* options ATTRIBUTE_UNUSED,
97 void* reserved ATTRIBUTE_UNUSED) {
98 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
99 printf("Unable to get jvmti env!\n");
100 return 1;
101 }
102 SetAllCapabilities(jvmti_env);
103 return 0;
104}
105
106} // namespace Test922Properties
107} // namespace art