blob: bfd165db106977c6765fcc0b254cd68b33cc9cc8 [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>
Alex Light49948e92016-08-11 15:35:28 -070019
Andreas Gampe57943812017-12-06 21:39:13 -080020#include <android-base/logging.h>
21
Alex Light49948e92016-08-11 15:35:28 -070022#include "base/macros.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070023
Andreas Gampe3f46c962017-03-30 10:26:59 -070024#include "jni_binder.h"
25#include "jvmti_helper.h"
26#include "test_env.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"
Andreas Gampecefaa142017-01-23 15:04:59 -080030#include "936-search-onload/search_onload.h"
Alex Light1d8a9742017-08-17 11:12:06 -070031#include "1919-vminit-thread-start-timing/vminit.h"
Alex Light49948e92016-08-11 15:35:28 -070032
33namespace art {
34
Alex Lightd0d65962017-06-30 11:13:33 -070035namespace common_redefine {
36jint OnLoad(JavaVM* vm, char* options, void* reserved);
37} // namespace common_redefine
38
39namespace common_retransform {
40jint OnLoad(JavaVM* vm, char* options, void* reserved);
41} // namespace common_retransform
42
43namespace common_transform {
44jint OnLoad(JavaVM* vm, char* options, void* reserved);
45} // namespace common_transform
46
Andreas Gampe53ae7802017-01-19 21:13:46 -080047namespace {
48
Alex Light49948e92016-08-11 15:35:28 -070049using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
50using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
51
52struct AgentLib {
53 const char* name;
54 OnLoad load;
55 OnAttach attach;
56};
57
Andreas Gampea8883a02017-01-11 19:53:50 -080058// A trivial OnLoad implementation that only initializes the global jvmti_env.
59static jint MinimalOnLoad(JavaVM* vm,
60 char* options ATTRIBUTE_UNUSED,
61 void* reserved ATTRIBUTE_UNUSED) {
Andreas Gampe53ae7802017-01-19 21:13:46 -080062 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
Andreas Gampea8883a02017-01-11 19:53:50 -080063 printf("Unable to get jvmti env!\n");
64 return 1;
65 }
Alex Light3d324fd2017-07-20 15:38:52 -070066 SetStandardCapabilities(jvmti_env);
Andreas Gampea8883a02017-01-11 19:53:50 -080067 return 0;
68}
69
70// A list of all non-standard the agents we have for testing. All other agents will use
71// MinimalOnLoad.
Andreas Gampe53ae7802017-01-19 21:13:46 -080072static AgentLib agents[] = {
Alex Light49948e92016-08-11 15:35:28 -070073 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070074 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Alex Lightdba61482016-12-21 08:20:29 -080075 { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light6ac57502017-01-19 15:05:06 -080076 { "921-hello-failure", common_retransform::OnLoad, nullptr },
Alex Light440b5d92017-01-24 15:32:25 -080077 { "934-load-transform", common_retransform::OnLoad, nullptr },
78 { "935-non-retransformable", common_transform::OnLoad, nullptr },
Andreas Gampecefaa142017-01-23 15:04:59 -080079 { "936-search-onload", Test936SearchOnload::OnLoad, nullptr },
Alex Light28027122017-01-26 17:21:51 -080080 { "937-hello-retransform-package", common_retransform::OnLoad, nullptr },
Alex Light7916f202017-01-27 09:00:15 -080081 { "938-load-transform-bcp", common_retransform::OnLoad, nullptr },
82 { "939-hello-transformation-bcp", common_redefine::OnLoad, nullptr },
Alex Light1ebe4fe2017-01-30 14:57:11 -080083 { "941-recursive-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light1ebe4fe2017-01-30 14:57:11 -080084 { "943-private-recursive-jit", common_redefine::OnLoad, nullptr },
Alex Light1d8a9742017-08-17 11:12:06 -070085 { "1919-vminit-thread-start-timing", Test1919VMInitThreadStart::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070086};
87
88static AgentLib* FindAgent(char* name) {
89 for (AgentLib& l : agents) {
90 if (strncmp(l.name, name, strlen(l.name)) == 0) {
91 return &l;
92 }
93 }
94 return nullptr;
95}
96
97static bool FindAgentNameAndOptions(char* options,
98 /*out*/char** name,
99 /*out*/char** other_options) {
100 // Name is the first element.
101 *name = options;
102 char* rest = options;
103 // name is the first thing in the options
104 while (*rest != '\0' && *rest != ',') {
105 rest++;
106 }
107 if (*rest == ',') {
108 *rest = '\0';
109 rest++;
110 }
111 *other_options = rest;
112 return true;
113}
114
Andreas Gampe3f46c962017-03-30 10:26:59 -0700115static void SetIsJVM(const char* options) {
116 SetJVM(strncmp(options, "jvm", 3) == 0);
Alex Light1e07ca62016-12-02 11:40:56 -0800117}
118
Andreas Gampe53ae7802017-01-19 21:13:46 -0800119} // namespace
120
Alex Light49948e92016-08-11 15:35:28 -0700121extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
122 char* remaining_options = nullptr;
123 char* name_option = nullptr;
124 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
125 printf("Unable to find agent name in options: %s\n", options);
126 return -1;
127 }
Andreas Gampea8883a02017-01-11 19:53:50 -0800128
Alex Light1e07ca62016-12-02 11:40:56 -0800129 SetIsJVM(remaining_options);
Andreas Gampea8883a02017-01-11 19:53:50 -0800130
131 AgentLib* lib = FindAgent(name_option);
132 OnLoad fn = nullptr;
133 if (lib == nullptr) {
134 fn = &MinimalOnLoad;
135 } else {
136 if (lib->load == nullptr) {
137 printf("agent: %s does not include an OnLoad method.\n", name_option);
138 return -3;
139 }
140 fn = lib->load;
141 }
142 return fn(vm, remaining_options, reserved);
Alex Light49948e92016-08-11 15:35:28 -0700143}
144
Alex Light49948e92016-08-11 15:35:28 -0700145extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
146 char* remaining_options = nullptr;
147 char* name_option = nullptr;
148 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
149 printf("Unable to find agent name in options: %s\n", options);
150 return -1;
151 }
Andreas Gampe53ae7802017-01-19 21:13:46 -0800152
Alex Light49948e92016-08-11 15:35:28 -0700153 AgentLib* lib = FindAgent(name_option);
154 if (lib == nullptr) {
155 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
156 name_option);
157 return -2;
158 }
159 if (lib->attach == nullptr) {
160 printf("agent: %s does not include an OnAttach method.\n", name_option);
161 return -3;
162 }
Alex Light1e07ca62016-12-02 11:40:56 -0800163 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700164 return lib->attach(vm, remaining_options, reserved);
165}
166
167} // namespace art