1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright 2016 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include <jni.h>
18*795d594fSAndroid Build Coastguard Worker #include <stdio.h>
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <android-base/logging.h>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker #include "jni_binder.h"
25*795d594fSAndroid Build Coastguard Worker #include "jvmti_helper.h"
26*795d594fSAndroid Build Coastguard Worker #include "test_env.h"
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker #include "901-hello-ti-agent/basics.h"
29*795d594fSAndroid Build Coastguard Worker #include "909-attach-agent/attach.h"
30*795d594fSAndroid Build Coastguard Worker #include "936-search-onload/search_onload.h"
31*795d594fSAndroid Build Coastguard Worker #include "1919-vminit-thread-start-timing/vminit.h"
32*795d594fSAndroid Build Coastguard Worker #include "993-breakpoints-non-debuggable/onload.h"
33*795d594fSAndroid Build Coastguard Worker
34*795d594fSAndroid Build Coastguard Worker namespace art {
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Worker namespace common_redefine {
37*795d594fSAndroid Build Coastguard Worker jint OnLoad(JavaVM* vm, char* options, void* reserved);
38*795d594fSAndroid Build Coastguard Worker } // namespace common_redefine
39*795d594fSAndroid Build Coastguard Worker
40*795d594fSAndroid Build Coastguard Worker namespace common_retransform {
41*795d594fSAndroid Build Coastguard Worker jint OnLoad(JavaVM* vm, char* options, void* reserved);
42*795d594fSAndroid Build Coastguard Worker } // namespace common_retransform
43*795d594fSAndroid Build Coastguard Worker
44*795d594fSAndroid Build Coastguard Worker namespace common_transform {
45*795d594fSAndroid Build Coastguard Worker jint OnLoad(JavaVM* vm, char* options, void* reserved);
46*795d594fSAndroid Build Coastguard Worker } // namespace common_transform
47*795d594fSAndroid Build Coastguard Worker
48*795d594fSAndroid Build Coastguard Worker namespace {
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Worker using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
51*795d594fSAndroid Build Coastguard Worker using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
52*795d594fSAndroid Build Coastguard Worker
53*795d594fSAndroid Build Coastguard Worker struct AgentLib {
54*795d594fSAndroid Build Coastguard Worker const char* name;
55*795d594fSAndroid Build Coastguard Worker OnLoad load;
56*795d594fSAndroid Build Coastguard Worker OnAttach attach;
57*795d594fSAndroid Build Coastguard Worker };
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker // A trivial OnLoad implementation that only initializes the global jvmti_env.
MinimalOnLoad(JavaVM * vm,char * options,void * reserved)60*795d594fSAndroid Build Coastguard Worker static jint MinimalOnLoad(JavaVM* vm,
61*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] char* options,
62*795d594fSAndroid Build Coastguard Worker [[maybe_unused]] void* reserved) {
63*795d594fSAndroid Build Coastguard Worker if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) {
64*795d594fSAndroid Build Coastguard Worker printf("Unable to get jvmti env!\n");
65*795d594fSAndroid Build Coastguard Worker return 1;
66*795d594fSAndroid Build Coastguard Worker }
67*795d594fSAndroid Build Coastguard Worker SetStandardCapabilities(jvmti_env);
68*795d594fSAndroid Build Coastguard Worker return 0;
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker // A list of all non-standard the agents we have for testing. All other agents will use
72*795d594fSAndroid Build Coastguard Worker // MinimalOnLoad.
73*795d594fSAndroid Build Coastguard Worker static AgentLib agents[] = {
74*795d594fSAndroid Build Coastguard Worker { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
75*795d594fSAndroid Build Coastguard Worker { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
76*795d594fSAndroid Build Coastguard Worker { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
77*795d594fSAndroid Build Coastguard Worker { "921-hello-failure", common_retransform::OnLoad, nullptr },
78*795d594fSAndroid Build Coastguard Worker { "934-load-transform", common_retransform::OnLoad, nullptr },
79*795d594fSAndroid Build Coastguard Worker { "935-non-retransformable", common_transform::OnLoad, nullptr },
80*795d594fSAndroid Build Coastguard Worker { "936-search-onload", Test936SearchOnload::OnLoad, nullptr },
81*795d594fSAndroid Build Coastguard Worker { "937-hello-retransform-package", common_retransform::OnLoad, nullptr },
82*795d594fSAndroid Build Coastguard Worker { "938-load-transform-bcp", common_retransform::OnLoad, nullptr },
83*795d594fSAndroid Build Coastguard Worker { "939-hello-transformation-bcp", common_redefine::OnLoad, nullptr },
84*795d594fSAndroid Build Coastguard Worker { "941-recursive-obsolete-jit", common_redefine::OnLoad, nullptr },
85*795d594fSAndroid Build Coastguard Worker { "943-private-recursive-jit", common_redefine::OnLoad, nullptr },
86*795d594fSAndroid Build Coastguard Worker { "993-non-debuggable", nullptr, Test993BreakpointsNonDebuggable::OnLoad },
87*795d594fSAndroid Build Coastguard Worker { "1919-vminit-thread-start-timing", Test1919VMInitThreadStart::OnLoad, nullptr },
88*795d594fSAndroid Build Coastguard Worker { "2031-zygote-compiled-frame-deopt", nullptr, MinimalOnLoad },
89*795d594fSAndroid Build Coastguard Worker { "2039-load-transform-larger", common_retransform::OnLoad, nullptr },
90*795d594fSAndroid Build Coastguard Worker };
91*795d594fSAndroid Build Coastguard Worker
FindAgent(char * name)92*795d594fSAndroid Build Coastguard Worker static AgentLib* FindAgent(char* name) {
93*795d594fSAndroid Build Coastguard Worker for (AgentLib& l : agents) {
94*795d594fSAndroid Build Coastguard Worker if (strncmp(l.name, name, strlen(l.name)) == 0) {
95*795d594fSAndroid Build Coastguard Worker return &l;
96*795d594fSAndroid Build Coastguard Worker }
97*795d594fSAndroid Build Coastguard Worker }
98*795d594fSAndroid Build Coastguard Worker return nullptr;
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker
FindAgentNameAndOptions(char * options,char ** name,char ** other_options)101*795d594fSAndroid Build Coastguard Worker static bool FindAgentNameAndOptions(char* options,
102*795d594fSAndroid Build Coastguard Worker /*out*/char** name,
103*795d594fSAndroid Build Coastguard Worker /*out*/char** other_options) {
104*795d594fSAndroid Build Coastguard Worker // Name is the first element.
105*795d594fSAndroid Build Coastguard Worker *name = options;
106*795d594fSAndroid Build Coastguard Worker char* rest = options;
107*795d594fSAndroid Build Coastguard Worker // name is the first thing in the options
108*795d594fSAndroid Build Coastguard Worker while (*rest != '\0' && *rest != ',') {
109*795d594fSAndroid Build Coastguard Worker rest++;
110*795d594fSAndroid Build Coastguard Worker }
111*795d594fSAndroid Build Coastguard Worker if (*rest == ',') {
112*795d594fSAndroid Build Coastguard Worker *rest = '\0';
113*795d594fSAndroid Build Coastguard Worker rest++;
114*795d594fSAndroid Build Coastguard Worker }
115*795d594fSAndroid Build Coastguard Worker *other_options = rest;
116*795d594fSAndroid Build Coastguard Worker return true;
117*795d594fSAndroid Build Coastguard Worker }
118*795d594fSAndroid Build Coastguard Worker
SetIsJVM(const char * options)119*795d594fSAndroid Build Coastguard Worker static void SetIsJVM(const char* options) {
120*795d594fSAndroid Build Coastguard Worker SetJVM(strncmp(options, "jvm", 3) == 0);
121*795d594fSAndroid Build Coastguard Worker }
122*795d594fSAndroid Build Coastguard Worker
123*795d594fSAndroid Build Coastguard Worker } // namespace
124*795d594fSAndroid Build Coastguard Worker
Agent_OnLoad(JavaVM * vm,char * options,void * reserved)125*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
126*795d594fSAndroid Build Coastguard Worker char* remaining_options = nullptr;
127*795d594fSAndroid Build Coastguard Worker char* name_option = nullptr;
128*795d594fSAndroid Build Coastguard Worker if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
129*795d594fSAndroid Build Coastguard Worker printf("Unable to find agent name in options: %s\n", options);
130*795d594fSAndroid Build Coastguard Worker return -1;
131*795d594fSAndroid Build Coastguard Worker }
132*795d594fSAndroid Build Coastguard Worker
133*795d594fSAndroid Build Coastguard Worker SetIsJVM(remaining_options);
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Worker AgentLib* lib = FindAgent(name_option);
136*795d594fSAndroid Build Coastguard Worker OnLoad fn = nullptr;
137*795d594fSAndroid Build Coastguard Worker if (lib == nullptr) {
138*795d594fSAndroid Build Coastguard Worker fn = &MinimalOnLoad;
139*795d594fSAndroid Build Coastguard Worker } else {
140*795d594fSAndroid Build Coastguard Worker if (lib->load == nullptr) {
141*795d594fSAndroid Build Coastguard Worker printf("agent: %s does not include an OnLoad method.\n", name_option);
142*795d594fSAndroid Build Coastguard Worker return -3;
143*795d594fSAndroid Build Coastguard Worker }
144*795d594fSAndroid Build Coastguard Worker fn = lib->load;
145*795d594fSAndroid Build Coastguard Worker }
146*795d594fSAndroid Build Coastguard Worker return fn(vm, remaining_options, reserved);
147*795d594fSAndroid Build Coastguard Worker }
148*795d594fSAndroid Build Coastguard Worker
Agent_OnAttach(JavaVM * vm,char * options,void * reserved)149*795d594fSAndroid Build Coastguard Worker extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
150*795d594fSAndroid Build Coastguard Worker char* remaining_options = nullptr;
151*795d594fSAndroid Build Coastguard Worker char* name_option = nullptr;
152*795d594fSAndroid Build Coastguard Worker if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
153*795d594fSAndroid Build Coastguard Worker printf("Unable to find agent name in options: %s\n", options);
154*795d594fSAndroid Build Coastguard Worker return -1;
155*795d594fSAndroid Build Coastguard Worker }
156*795d594fSAndroid Build Coastguard Worker
157*795d594fSAndroid Build Coastguard Worker AgentLib* lib = FindAgent(name_option);
158*795d594fSAndroid Build Coastguard Worker if (lib == nullptr) {
159*795d594fSAndroid Build Coastguard Worker printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
160*795d594fSAndroid Build Coastguard Worker name_option);
161*795d594fSAndroid Build Coastguard Worker return -2;
162*795d594fSAndroid Build Coastguard Worker }
163*795d594fSAndroid Build Coastguard Worker if (lib->attach == nullptr) {
164*795d594fSAndroid Build Coastguard Worker printf("agent: %s does not include an OnAttach method.\n", name_option);
165*795d594fSAndroid Build Coastguard Worker return -3;
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker SetIsJVM(remaining_options);
168*795d594fSAndroid Build Coastguard Worker return lib->attach(vm, remaining_options, reserved);
169*795d594fSAndroid Build Coastguard Worker }
170*795d594fSAndroid Build Coastguard Worker
171*795d594fSAndroid Build Coastguard Worker } // namespace art
172