blob: 38861482d21f61580c26d08972530683a4cac5a1 [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 Light200b9d72016-12-15 11:34:13 -080069 { "917-fields-transformation", common_redefine::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070070};
71
72static AgentLib* FindAgent(char* name) {
73 for (AgentLib& l : agents) {
74 if (strncmp(l.name, name, strlen(l.name)) == 0) {
75 return &l;
76 }
77 }
78 return nullptr;
79}
80
81static bool FindAgentNameAndOptions(char* options,
82 /*out*/char** name,
83 /*out*/char** other_options) {
84 // Name is the first element.
85 *name = options;
86 char* rest = options;
87 // name is the first thing in the options
88 while (*rest != '\0' && *rest != ',') {
89 rest++;
90 }
91 if (*rest == ',') {
92 *rest = '\0';
93 rest++;
94 }
95 *other_options = rest;
96 return true;
97}
98
Alex Light1e07ca62016-12-02 11:40:56 -080099static void SetIsJVM(char* options) {
100 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
101}
102
Alex Light49948e92016-08-11 15:35:28 -0700103extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
104 char* remaining_options = nullptr;
105 char* name_option = nullptr;
106 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
107 printf("Unable to find agent name in options: %s\n", options);
108 return -1;
109 }
110 AgentLib* lib = FindAgent(name_option);
111 if (lib == nullptr) {
112 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
113 name_option);
114 return -2;
115 }
116 if (lib->load == nullptr) {
117 printf("agent: %s does not include an OnLoad method.\n", name_option);
118 return -3;
119 }
Alex Light1e07ca62016-12-02 11:40:56 -0800120 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700121 return lib->load(vm, remaining_options, reserved);
122}
123
Alex Light49948e92016-08-11 15:35:28 -0700124extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
125 char* remaining_options = nullptr;
126 char* name_option = nullptr;
127 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
128 printf("Unable to find agent name in options: %s\n", options);
129 return -1;
130 }
131 AgentLib* lib = FindAgent(name_option);
132 if (lib == nullptr) {
133 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
134 name_option);
135 return -2;
136 }
137 if (lib->attach == nullptr) {
138 printf("agent: %s does not include an OnAttach method.\n", name_option);
139 return -3;
140 }
Alex Light1e07ca62016-12-02 11:40:56 -0800141 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700142 return lib->attach(vm, remaining_options, reserved);
143}
144
145} // namespace art