blob: d7579ca0220f4aa5b9d7b05bec5fb47f9bfb2b6e [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"
Andreas Gampe50a4e492017-01-06 18:00:20 -080041#include "920-objects/objects.h"
Alex Light49948e92016-08-11 15:35:28 -070042
43namespace art {
44
Andreas Gampecc13b222016-10-10 19:09:09 -070045jvmtiEnv* jvmti_env;
46
Alex Light49948e92016-08-11 15:35:28 -070047using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
48using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
49
50struct AgentLib {
51 const char* name;
52 OnLoad load;
53 OnAttach attach;
54};
55
56// A list of all the agents we have for testing.
57AgentLib agents[] = {
58 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light1e07ca62016-12-02 11:40:56 -080059 { "902-hello-transformation", common_redefine::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070060 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070061 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Andreas Gampecc13b222016-10-10 19:09:09 -070062 { "905-object-free", Test905ObjectFree::OnLoad, nullptr },
Andreas Gampee54d9922016-10-11 19:55:37 -070063 { "906-iterate-heap", Test906IterateHeap::OnLoad, nullptr },
Andreas Gampe9b8c5882016-10-21 15:27:46 -070064 { "907-get-loaded-classes", Test907GetLoadedClasses::OnLoad, nullptr },
65 { "908-gc-start-finish", Test908GcStartFinish::OnLoad, nullptr },
Leonard Mosescueb842212016-10-06 17:26:36 -070066 { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach },
Andreas Gampe3c252f02016-10-27 18:25:17 -070067 { "910-methods", Test910Methods::OnLoad, nullptr },
Andreas Gampeb5eb94a2016-10-27 19:23:09 -070068 { "911-get-stack-trace", Test911GetStackTrace::OnLoad, nullptr },
Andreas Gampee492ae32016-10-28 19:34:57 -070069 { "912-classes", Test912Classes::OnLoad, nullptr },
Andreas Gampe8da6d032016-10-31 19:31:03 -070070 { "913-heaps", Test913Heaps::OnLoad, nullptr },
Alex Lightdba61482016-12-21 08:20:29 -080071 { "914-hello-obsolescence", common_redefine::OnLoad, nullptr },
72 { "915-obsolete-2", common_redefine::OnLoad, nullptr },
73 { "916-obsolete-jit", common_redefine::OnLoad, nullptr },
Alex Light200b9d72016-12-15 11:34:13 -080074 { "917-fields-transformation", common_redefine::OnLoad, nullptr },
Andreas Gampeab2f0d02017-01-05 17:23:45 -080075 { "918-fields", Test918Fields::OnLoad, nullptr },
Alex Light32a2fba2017-01-06 16:58:19 +000076 { "919-obsolete-fields", common_redefine::OnLoad, nullptr },
Andreas Gampe50a4e492017-01-06 18:00:20 -080077 { "920-objects", Test920Objects::OnLoad, nullptr },
Alex Light460d1b42017-01-10 15:37:17 +000078 { "921-hello-failure", common_redefine::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070079};
80
81static AgentLib* FindAgent(char* name) {
82 for (AgentLib& l : agents) {
83 if (strncmp(l.name, name, strlen(l.name)) == 0) {
84 return &l;
85 }
86 }
87 return nullptr;
88}
89
90static bool FindAgentNameAndOptions(char* options,
91 /*out*/char** name,
92 /*out*/char** other_options) {
93 // Name is the first element.
94 *name = options;
95 char* rest = options;
96 // name is the first thing in the options
97 while (*rest != '\0' && *rest != ',') {
98 rest++;
99 }
100 if (*rest == ',') {
101 *rest = '\0';
102 rest++;
103 }
104 *other_options = rest;
105 return true;
106}
107
Alex Light1e07ca62016-12-02 11:40:56 -0800108static void SetIsJVM(char* options) {
109 RuntimeIsJVM = strncmp(options, "jvm", 3) == 0;
110}
111
Alex Light49948e92016-08-11 15:35:28 -0700112extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
113 char* remaining_options = nullptr;
114 char* name_option = nullptr;
115 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
116 printf("Unable to find agent name in options: %s\n", options);
117 return -1;
118 }
119 AgentLib* lib = FindAgent(name_option);
120 if (lib == nullptr) {
121 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
122 name_option);
123 return -2;
124 }
125 if (lib->load == nullptr) {
126 printf("agent: %s does not include an OnLoad method.\n", name_option);
127 return -3;
128 }
Alex Light1e07ca62016-12-02 11:40:56 -0800129 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700130 return lib->load(vm, remaining_options, reserved);
131}
132
Alex Light49948e92016-08-11 15:35:28 -0700133extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
134 char* remaining_options = nullptr;
135 char* name_option = nullptr;
136 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
137 printf("Unable to find agent name in options: %s\n", options);
138 return -1;
139 }
140 AgentLib* lib = FindAgent(name_option);
141 if (lib == nullptr) {
142 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
143 name_option);
144 return -2;
145 }
146 if (lib->attach == nullptr) {
147 printf("agent: %s does not include an OnAttach method.\n", name_option);
148 return -3;
149 }
Alex Light1e07ca62016-12-02 11:40:56 -0800150 SetIsJVM(remaining_options);
Alex Light49948e92016-08-11 15:35:28 -0700151 return lib->attach(vm, remaining_options, reserved);
152}
153
154} // namespace art