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