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