blob: cb732c74f125e26db058e9f36048d4030e54c7d3 [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
Andreas Gampe1bdaf732017-01-09 19:21:06 -080017#include <stdio.h>
18
19#include "base/macros.h"
20#include "jni.h"
21#include "openjdkjvmti/jvmti.h"
22#include "ScopedUtfChars.h"
23
24#include "ti-agent/common_helper.h"
25#include "ti-agent/common_load.h"
26
27namespace art {
28namespace Test922Properties {
29
30extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getSystemProperties(
31 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED) {
32 jint count;
33 char** properties;
34 jvmtiError result = jvmti_env->GetSystemProperties(&count, &properties);
35 if (JvmtiErrorToException(env, result)) {
36 return nullptr;
37 }
38
39 auto callback = [&](jint i) -> jstring {
40 char* data = properties[i];
41 if (data == nullptr) {
42 return nullptr;
43 }
44 jstring ret = env->NewStringUTF(data);
45 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(data));
46 return ret;
47 };
48 jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
49
50 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(properties));
51
52 return ret;
53}
54
55extern "C" JNIEXPORT jstring JNICALL Java_Main_getSystemProperty(
56 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key) {
57 ScopedUtfChars string(env, key);
58 if (string.c_str() == nullptr) {
59 return nullptr;
60 }
61
62 char* value = nullptr;
63 jvmtiError result = jvmti_env->GetSystemProperty(string.c_str(), &value);
64 if (JvmtiErrorToException(env, result)) {
65 return nullptr;
66 }
67
68 jstring ret = (value == nullptr) ? nullptr : env->NewStringUTF(value);
69
70 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(value));
71
72 return ret;
73}
74
75extern "C" JNIEXPORT void JNICALL Java_Main_setSystemProperty(
76 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jstring key, jstring value) {
77 ScopedUtfChars key_string(env, key);
78 if (key_string.c_str() == nullptr) {
79 return;
80 }
81 ScopedUtfChars value_string(env, value);
82 if (value_string.c_str() == nullptr) {
83 return;
84 }
85
86 jvmtiError result = jvmti_env->SetSystemProperty(key_string.c_str(), value_string.c_str());
87 if (JvmtiErrorToException(env, result)) {
88 return;
89 }
90}
91
Andreas Gampe1bdaf732017-01-09 19:21:06 -080092} // namespace Test922Properties
93} // namespace art