blob: 79c17d744f8a7ebdea31f54632975e6f0d5124a3 [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"
Alex Light49948e92016-08-11 15:35:28 -070040
41namespace art {
42
Andreas Gampecc13b222016-10-10 19:09:09 -070043jvmtiEnv* jvmti_env;
44
Alex Light49948e92016-08-11 15:35:28 -070045using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
46using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
47
48struct AgentLib {
49 const char* name;
50 OnLoad load;
51 OnAttach attach;
52};
53
54// A list of all the agents we have for testing.
55AgentLib agents[] = {
56 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light1e07ca62016-12-02 11:40:56 -080057 { "902-hello-transformation", common_redefine::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070058 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070059 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Andreas Gampecc13b222016-10-10 19:09:09 -070060 { "905-object-free", Test905ObjectFree::OnLoad, nullptr },
Andreas Gampee54d9922016-10-11 19:55:37 -070061 { "906-iterate-heap", Test906IterateHeap::OnLoad, nullptr },
Andreas Gampe9b8c5882016-10-21 15:27:46 -070062 { "907-get-loaded-classes", Test907GetLoadedClasses::OnLoad, nullptr },
63 { "908-gc-start-finish", Test908GcStartFinish::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070064 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Andreas Gampe3c252f02016-10-27 18:25:17 -070065 { "910-methods", Test910Methods::OnLoad, nullptr },
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070066 { "911-get-stack-trace", Test911GetStackTrace::OnLoad, nullptr },
Andreas Gampee492ae32016-10-28 19:34:57 -070067 { "912-classes", Test912Classes::OnLoad, nullptr },
Andreas Gampe8da6d032016-10-31 19:31:03 -070068 { "913-heaps", Test913Heaps::OnLoad, nullptr },
Alex Lightdba61482016-12-21 08:20:29 -080069 { "914-hello-obsolescence", common_redefine::OnLoad, nullptr },
70 { "915-obsolete-2", common_redefine::OnLoad, nullptr },
71 { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light200b9d72016-12-15 11:34:13 -080072 { "917-fields-transformation", common_redefine::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070073};
74
75static AgentLib* FindAgent(char* name) {
76 for (AgentLib& l : agents) {
77 if (strncmp(l.name, name, strlen(l.name)) == 0) {
78 return &l;
79 }
80 }
81 return nullptr;
82}
83
84static bool FindAgentNameAndOptions(char* options,
85 /*out*/char** name,
86 /*out*/char** other_options) {
87 // Name is the first element.
88 *name = options;
89 char* rest = options;
90 // name is the first thing in the options
91 while (*rest != '\0' && *rest != ',') {
92 rest++;
93 }
94 if (*rest == ',') {
95 *rest = '\0';
96 rest++;
97 }
98 *other_options = rest;
99 return true;
100}
101
Alex Light1e07ca62016-12-02 11:40:56 -0800102static void SetIsJVM(char* options) {
103 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
104}
105
Alex Light49948e92016-08-11 15:35:28 -0700106extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
107 char* remaining_options = nullptr;
108 char* name_option = nullptr;
109 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
110 printf("Unable to find agent name in options: %s\n", options);
111 return -1;
112 }
113 AgentLib* lib = FindAgent(name_option);
114 if (lib == nullptr) {
115 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
116 name_option);
117 return -2;
118 }
119 if (lib->load == nullptr) {
120 printf("agent: %s does not include an OnLoad method.\n", name_option);
121 return -3;
122 }
Alex Light1e07ca62016-12-02 11:40:56 -0800123 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700124 return lib->load(vm, remaining_options, reserved);
125}
126
Alex Light49948e92016-08-11 15:35:28 -0700127extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
128 char* remaining_options = nullptr;
129 char* name_option = nullptr;
130 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
131 printf("Unable to find agent name in options: %s\n", options);
132 return -1;
133 }
134 AgentLib* lib = FindAgent(name_option);
135 if (lib == nullptr) {
136 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
137 name_option);
138 return -2;
139 }
140 if (lib->attach == nullptr) {
141 printf("agent: %s does not include an OnAttach method.\n", name_option);
142 return -3;
143 }
Alex Light1e07ca62016-12-02 11:40:56 -0800144 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700145 return lib->attach(vm, remaining_options, reserved);
146}
147
148} // namespace art