blob: 5e8ef94334fa5a06eb9c9e300e456215f633d0e6 [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 Light49948e92016-08-11 15:35:28 -070026
27#include "901-hello-ti-agent/basics.h"
Alex Light9c20a142016-08-23 15:05:12 -070028#include "902-hello-transformation/transform.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070029#include "903-hello-tagging/tagging.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070030#include "904-object-allocation/tracking.h"
Andreas Gampecc13b222016-10-10 19:09:09 -070031#include "905-object-free/tracking_free.h"
Andreas Gampee54d9922016-10-11 19:55:37 -070032#include "906-iterate-heap/iterate_heap.h"
Andreas Gampe9b8c5882016-10-21 15:27:46 -070033#include "907-get-loaded-classes/get_loaded_classes.h"
34#include "908-gc-start-finish/gc_callbacks.h"
Leonard Mosescueb842212016-10-06 17:26:36 -070035#include "909-attach-agent/attach.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070036#include "910-methods/methods.h"
Alex Light49948e92016-08-11 15:35:28 -070037
38namespace art {
39
Andreas Gampecc13b222016-10-10 19:09:09 -070040jvmtiEnv* jvmti_env;
41
Alex Light49948e92016-08-11 15:35:28 -070042using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
43using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
44
45struct AgentLib {
46 const char* name;
47 OnLoad load;
48 OnAttach attach;
49};
50
51// A list of all the agents we have for testing.
52AgentLib agents[] = {
53 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light9c20a142016-08-23 15:05:12 -070054 { "902-hello-transformation", Test902HelloTransformation::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070055 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070056 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Andreas Gampecc13b222016-10-10 19:09:09 -070057 { "905-object-free", Test905ObjectFree::OnLoad, nullptr },
Andreas Gampee54d9922016-10-11 19:55:37 -070058 { "906-iterate-heap", Test906IterateHeap::OnLoad, nullptr },
Andreas Gampe9b8c5882016-10-21 15:27:46 -070059 { "907-get-loaded-classes", Test907GetLoadedClasses::OnLoad, nullptr },
60 { "908-gc-start-finish", Test908GcStartFinish::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070061 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Andreas Gampe3c252f02016-10-27 18:25:17 -070062 { "910-methods", Test910Methods::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070063};
64
65static AgentLib* FindAgent(char* name) {
66 for (AgentLib& l : agents) {
67 if (strncmp(l.name, name, strlen(l.name)) == 0) {
68 return &l;
69 }
70 }
71 return nullptr;
72}
73
74static bool FindAgentNameAndOptions(char* options,
75 /*out*/char** name,
76 /*out*/char** other_options) {
77 // Name is the first element.
78 *name = options;
79 char* rest = options;
80 // name is the first thing in the options
81 while (*rest != '\0' && *rest != ',') {
82 rest++;
83 }
84 if (*rest == ',') {
85 *rest = '\0';
86 rest++;
87 }
88 *other_options = rest;
89 return true;
90}
91
92extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
93 char* remaining_options = nullptr;
94 char* name_option = nullptr;
95 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
96 printf("Unable to find agent name in options: %s\n", options);
97 return -1;
98 }
99 AgentLib* lib = FindAgent(name_option);
100 if (lib == nullptr) {
101 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
102 name_option);
103 return -2;
104 }
105 if (lib->load == nullptr) {
106 printf("agent: %s does not include an OnLoad method.\n", name_option);
107 return -3;
108 }
109 return lib->load(vm, remaining_options, reserved);
110}
111
Alex Light49948e92016-08-11 15:35:28 -0700112extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
113 char* remaining_options = nullptr;
114 char* name_option = nullptr;
115 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
116 printf("Unable to find agent name in options: %s\n", options);
117 return -1;
118 }
119 AgentLib* lib = FindAgent(name_option);
120 if (lib == nullptr) {
121 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
122 name_option);
123 return -2;
124 }
125 if (lib->attach == nullptr) {
126 printf("agent: %s does not include an OnAttach method.\n", name_option);
127 return -3;
128 }
129 return lib->attach(vm, remaining_options, reserved);
130}
131
132} // namespace art