blob: f4ce4c381e2f34c94868ececfcc22cfb69cc8582 [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 Lighta7e38d82017-01-19 14:57:28 -080070 { "932-transform-saves", common_retransform::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070071};
72
73static AgentLib* FindAgent(char* name) {
74 for (AgentLib& l : agents) {
75 if (strncmp(l.name, name, strlen(l.name)) == 0) {
76 return &l;
77 }
78 }
79 return nullptr;
80}
81
82static bool FindAgentNameAndOptions(char* options,
83 /*out*/char** name,
84 /*out*/char** other_options) {
85 // Name is the first element.
86 *name = options;
87 char* rest = options;
88 // name is the first thing in the options
89 while (*rest != '\0' && *rest != ',') {
90 rest++;
91 }
92 if (*rest == ',') {
93 *rest = '\0';
94 rest++;
95 }
96 *other_options = rest;
97 return true;
98}
99
Alex Light1e07ca62016-12-02 11:40:56 -0800100static void SetIsJVM(char* options) {
101 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
102}
103
Alex Light49948e92016-08-11 15:35:28 -0700104extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
105 char* remaining_options = nullptr;
106 char* name_option = nullptr;
107 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
108 printf("Unable to find agent name in options: %s\n", options);
109 return -1;
110 }
Andreas Gampea8883a02017-01-11 19:53:50 -0800111
Alex Light1e07ca62016-12-02 11:40:56 -0800112 SetIsJVM(remaining_options);
Andreas Gampea8883a02017-01-11 19:53:50 -0800113
114 AgentLib* lib = FindAgent(name_option);
115 OnLoad fn = nullptr;
116 if (lib == nullptr) {
117 fn = &MinimalOnLoad;
118 } else {
119 if (lib->load == nullptr) {
120 printf("agent: %s does not include an OnLoad method.\n", name_option);
121 return -3;
122 }
123 fn = lib->load;
124 }
125 return fn(vm, remaining_options, reserved);
Alex Light49948e92016-08-11 15:35:28 -0700126}
127
Alex Light49948e92016-08-11 15:35:28 -0700128extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
129 char* remaining_options = nullptr;
130 char* name_option = nullptr;
131 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
132 printf("Unable to find agent name in options: %s\n", options);
133 return -1;
134 }
135 AgentLib* lib = FindAgent(name_option);
136 if (lib == nullptr) {
137 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
138 name_option);
139 return -2;
140 }
141 if (lib->attach == nullptr) {
142 printf("agent: %s does not include an OnAttach method.\n", name_option);
143 return -3;
144 }
Alex Light1e07ca62016-12-02 11:40:56 -0800145 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700146 return lib->attach(vm, remaining_options, reserved);
147}
148
149} // namespace art