blob: 1b1144209219f9e866356a4a8ef2f9d5d28623f1 [file] [log] [blame]
Alex Light49948e92016-08-11 15:35:28 -07001/*
2 * Copyright 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 <jni.h>
18#include <stdio.h>
19// TODO I don't know?
20#include "openjdkjvmti/jvmti.h"
21
22#include "art_method-inl.h"
23#include "base/logging.h"
24#include "base/macros.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070025#include "common_load.h"
Alex Light1e07ca62016-12-02 11:40:56 -080026#include "common_helper.h"
Alex Light49948e92016-08-11 15:35:28 -070027
28#include "901-hello-ti-agent/basics.h"
Leonard Mosescueb842212016-10-06 17:26:36 -070029#include "909-attach-agent/attach.h"
Alex Light49948e92016-08-11 15:35:28 -070030
31namespace art {
32
Andreas Gampecc13b222016-10-10 19:09:09 -070033jvmtiEnv* jvmti_env;
34
Alex Light49948e92016-08-11 15:35:28 -070035using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
36using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
37
38struct AgentLib {
39 const char* name;
40 OnLoad load;
41 OnAttach attach;
42};
43
Andreas Gampea8883a02017-01-11 19:53:50 -080044// A trivial OnLoad implementation that only initializes the global jvmti_env.
45static jint MinimalOnLoad(JavaVM* vm,
46 char* options ATTRIBUTE_UNUSED,
47 void* reserved ATTRIBUTE_UNUSED) {
48 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
49 printf("Unable to get jvmti env!\n");
50 return 1;
51 }
52 SetAllCapabilities(jvmti_env);
53 return 0;
54}
55
56// A list of all non-standard the agents we have for testing. All other agents will use
57// MinimalOnLoad.
Alex Light49948e92016-08-11 15:35:28 -070058AgentLib agents[] = {
59 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light1e07ca62016-12-02 11:40:56 -080060 { "902-hello-transformation", common_redefine::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070061 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Alex Lightdba61482016-12-21 08:20:29 -080062 { "914-hello-obsolescence", common_redefine::OnLoad, nullptr },
63 { "915-obsolete-2", common_redefine::OnLoad, nullptr },
64 { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light200b9d72016-12-15 11:34:13 -080065 { "917-fields-transformation", common_redefine::OnLoad, nullptr },
Alex Light32a2fba2017-01-06 16:58:19 +000066 { "919-obsolete-fields", common_redefine::OnLoad, nullptr },
Alex Light6ac57502017-01-19 15:05:06 -080067 { "921-hello-failure", common_retransform::OnLoad, nullptr },
Alex Light0e692732017-01-10 15:00:05 -080068 { "926-multi-obsolescence", common_redefine::OnLoad, nullptr },
Alex Light6ac57502017-01-19 15:05:06 -080069 { "930-hello-retransform", common_retransform::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070070};
71
72static AgentLib* FindAgent(char* name) {
73 for (AgentLib& l : agents) {
74 if (strncmp(l.name, name, strlen(l.name)) == 0) {
75 return &l;
76 }
77 }
78 return nullptr;
79}
80
81static bool FindAgentNameAndOptions(char* options,
82 /*out*/char** name,
83 /*out*/char** other_options) {
84 // Name is the first element.
85 *name = options;
86 char* rest = options;
87 // name is the first thing in the options
88 while (*rest != '\0' && *rest != ',') {
89 rest++;
90 }
91 if (*rest == ',') {
92 *rest = '\0';
93 rest++;
94 }
95 *other_options = rest;
96 return true;
97}
98
Alex Light1e07ca62016-12-02 11:40:56 -080099static void SetIsJVM(char* options) {
100 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
101}
102
Alex Light49948e92016-08-11 15:35:28 -0700103extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
104 char* remaining_options = nullptr;
105 char* name_option = nullptr;
106 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
107 printf("Unable to find agent name in options: %s\n", options);
108 return -1;
109 }
Andreas Gampea8883a02017-01-11 19:53:50 -0800110
Alex Light1e07ca62016-12-02 11:40:56 -0800111 SetIsJVM(remaining_options);
Andreas Gampea8883a02017-01-11 19:53:50 -0800112
113 AgentLib* lib = FindAgent(name_option);
114 OnLoad fn = nullptr;
115 if (lib == nullptr) {
116 fn = &MinimalOnLoad;
117 } else {
118 if (lib->load == nullptr) {
119 printf("agent: %s does not include an OnLoad method.\n", name_option);
120 return -3;
121 }
122 fn = lib->load;
123 }
124 return fn(vm, remaining_options, reserved);
Alex Light49948e92016-08-11 15:35:28 -0700125}
126
Alex Light49948e92016-08-11 15:35:28 -0700127extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
128 char* remaining_options = nullptr;
129 char* name_option = nullptr;
130 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
131 printf("Unable to find agent name in options: %s\n", options);
132 return -1;
133 }
134 AgentLib* lib = FindAgent(name_option);
135 if (lib == nullptr) {
136 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
137 name_option);
138 return -2;
139 }
140 if (lib->attach == nullptr) {
141 printf("agent: %s does not include an OnAttach method.\n", name_option);
142 return -3;
143 }
Alex Light1e07ca62016-12-02 11:40:56 -0800144 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700145 return lib->attach(vm, remaining_options, reserved);
146}
147
148} // namespace art