blob: d60cff364a1901d51b17351bff886691464384f4 [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 Light1e07ca62016-12-02 11:40:56 -080026#include "common_helper.h"
Alex Light49948e92016-08-11 15:35:28 -070027
28#include "901-hello-ti-agent/basics.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"
Andreas Gampe8da6d032016-10-31 19:31:03 -070039#include "913-heaps/heaps.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080040#include "918-fields/fields.h"
Alex Light49948e92016-08-11 15:35:28 -070041
42namespace art {
43
Andreas Gampecc13b222016-10-10 19:09:09 -070044jvmtiEnv* jvmti_env;
45
Alex Light49948e92016-08-11 15:35:28 -070046using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
47using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
48
49struct AgentLib {
50 const char* name;
51 OnLoad load;
52 OnAttach attach;
53};
54
55// A list of all the agents we have for testing.
56AgentLib agents[] = {
57 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light1e07ca62016-12-02 11:40:56 -080058 { "902-hello-transformation", common_redefine::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070059 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070060 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Andreas Gampecc13b222016-10-10 19:09:09 -070061 { "905-object-free", Test905ObjectFree::OnLoad, nullptr },
Andreas Gampee54d9922016-10-11 19:55:37 -070062 { "906-iterate-heap", Test906IterateHeap::OnLoad, nullptr },
Andreas Gampe9b8c5882016-10-21 15:27:46 -070063 { "907-get-loaded-classes", Test907GetLoadedClasses::OnLoad, nullptr },
64 { "908-gc-start-finish", Test908GcStartFinish::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070065 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Andreas Gampe3c252f02016-10-27 18:25:17 -070066 { "910-methods", Test910Methods::OnLoad, nullptr },
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070067 { "911-get-stack-trace", Test911GetStackTrace::OnLoad, nullptr },
Andreas Gampee492ae32016-10-28 19:34:57 -070068 { "912-classes", Test912Classes::OnLoad, nullptr },
Andreas Gampe8da6d032016-10-31 19:31:03 -070069 { "913-heaps", Test913Heaps::OnLoad, nullptr },
Alex Lightdba61482016-12-21 08:20:29 -080070 { "914-hello-obsolescence", common_redefine::OnLoad, nullptr },
71 { "915-obsolete-2", common_redefine::OnLoad, nullptr },
72 { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light200b9d72016-12-15 11:34:13 -080073 { "917-fields-transformation", common_redefine::OnLoad, nullptr },
Andreas Gampeab2f0d02017-01-05 17:23:45 -080074 { "918-fields", Test918Fields::OnLoad, nullptr },
Alex Light32a2fba2017-01-06 16:58:19 +000075 { "919-obsolete-fields", common_redefine::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070076};
77
78static AgentLib* FindAgent(char* name) {
79 for (AgentLib& l : agents) {
80 if (strncmp(l.name, name, strlen(l.name)) == 0) {
81 return &l;
82 }
83 }
84 return nullptr;
85}
86
87static bool FindAgentNameAndOptions(char* options,
88 /*out*/char** name,
89 /*out*/char** other_options) {
90 // Name is the first element.
91 *name = options;
92 char* rest = options;
93 // name is the first thing in the options
94 while (*rest != '\0' && *rest != ',') {
95 rest++;
96 }
97 if (*rest == ',') {
98 *rest = '\0';
99 rest++;
100 }
101 *other_options = rest;
102 return true;
103}
104
Alex Light1e07ca62016-12-02 11:40:56 -0800105static void SetIsJVM(char* options) {
106 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
107}
108
Alex Light49948e92016-08-11 15:35:28 -0700109extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(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->load == nullptr) {
123 printf("agent: %s does not include an OnLoad method.\n", name_option);
124 return -3;
125 }
Alex Light1e07ca62016-12-02 11:40:56 -0800126 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700127 return lib->load(vm, remaining_options, reserved);
128}
129
Alex Light49948e92016-08-11 15:35:28 -0700130extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
131 char* remaining_options = nullptr;
132 char* name_option = nullptr;
133 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
134 printf("Unable to find agent name in options: %s\n", options);
135 return -1;
136 }
137 AgentLib* lib = FindAgent(name_option);
138 if (lib == nullptr) {
139 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
140 name_option);
141 return -2;
142 }
143 if (lib->attach == nullptr) {
144 printf("agent: %s does not include an OnAttach method.\n", name_option);
145 return -3;
146 }
Alex Light1e07ca62016-12-02 11:40:56 -0800147 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700148 return lib->attach(vm, remaining_options, reserved);
149}
150
151} // namespace art