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