blob: 2b8947ec96949770c66da7df75febd147221b267 [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"
Alex Light49948e92016-08-11 15:35:28 -070032
33namespace art {
34
Andreas Gampecc13b222016-10-10 19:09:09 -070035jvmtiEnv* jvmti_env;
36
Alex Light49948e92016-08-11 15:35:28 -070037using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
38using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
39
40struct AgentLib {
41 const char* name;
42 OnLoad load;
43 OnAttach attach;
44};
45
46// A list of all the agents we have for testing.
47AgentLib agents[] = {
48 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light9c20a142016-08-23 15:05:12 -070049 { "902-hello-transformation", Test902HelloTransformation::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070050 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070051 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Andreas Gampecc13b222016-10-10 19:09:09 -070052 { "905-object-free", Test905ObjectFree::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070053};
54
55static AgentLib* FindAgent(char* name) {
56 for (AgentLib& l : agents) {
57 if (strncmp(l.name, name, strlen(l.name)) == 0) {
58 return &l;
59 }
60 }
61 return nullptr;
62}
63
64static bool FindAgentNameAndOptions(char* options,
65 /*out*/char** name,
66 /*out*/char** other_options) {
67 // Name is the first element.
68 *name = options;
69 char* rest = options;
70 // name is the first thing in the options
71 while (*rest != '\0' && *rest != ',') {
72 rest++;
73 }
74 if (*rest == ',') {
75 *rest = '\0';
76 rest++;
77 }
78 *other_options = rest;
79 return true;
80}
81
82extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
83 char* remaining_options = nullptr;
84 char* name_option = nullptr;
85 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
86 printf("Unable to find agent name in options: %s\n", options);
87 return -1;
88 }
89 AgentLib* lib = FindAgent(name_option);
90 if (lib == nullptr) {
91 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
92 name_option);
93 return -2;
94 }
95 if (lib->load == nullptr) {
96 printf("agent: %s does not include an OnLoad method.\n", name_option);
97 return -3;
98 }
99 return lib->load(vm, remaining_options, reserved);
100}
101
102
103extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(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->attach == nullptr) {
117 printf("agent: %s does not include an OnAttach method.\n", name_option);
118 return -3;
119 }
120 return lib->attach(vm, remaining_options, reserved);
121}
122
123} // namespace art