blob: fde3c6bb3846f29206891a3667864ab96830653a [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"
25
26#include "901-hello-ti-agent/basics.h"
Alex Light9c20a142016-08-23 15:05:12 -070027#include "902-hello-transformation/transform.h"
Andreas Gampe6dee92e2016-09-12 19:58:13 -070028#include "903-hello-tagging/tagging.h"
Andreas Gampe27fa96c2016-10-07 15:05:24 -070029#include "904-object-allocation/tracking.h"
Alex Light49948e92016-08-11 15:35:28 -070030
31namespace art {
32
33using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved);
34using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved);
35
36struct AgentLib {
37 const char* name;
38 OnLoad load;
39 OnAttach attach;
40};
41
42// A list of all the agents we have for testing.
43AgentLib agents[] = {
44 { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr },
Alex Light9c20a142016-08-23 15:05:12 -070045 { "902-hello-transformation", Test902HelloTransformation::OnLoad, nullptr },
Andreas Gampe6dee92e2016-09-12 19:58:13 -070046 { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr },
Andreas Gampe27fa96c2016-10-07 15:05:24 -070047 { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr },
Alex Light49948e92016-08-11 15:35:28 -070048};
49
50static AgentLib* FindAgent(char* name) {
51 for (AgentLib& l : agents) {
52 if (strncmp(l.name, name, strlen(l.name)) == 0) {
53 return &l;
54 }
55 }
56 return nullptr;
57}
58
59static bool FindAgentNameAndOptions(char* options,
60 /*out*/char** name,
61 /*out*/char** other_options) {
62 // Name is the first element.
63 *name = options;
64 char* rest = options;
65 // name is the first thing in the options
66 while (*rest != '\0' && *rest != ',') {
67 rest++;
68 }
69 if (*rest == ',') {
70 *rest = '\0';
71 rest++;
72 }
73 *other_options = rest;
74 return true;
75}
76
77extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
78 char* remaining_options = nullptr;
79 char* name_option = nullptr;
80 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
81 printf("Unable to find agent name in options: %s\n", options);
82 return -1;
83 }
84 AgentLib* lib = FindAgent(name_option);
85 if (lib == nullptr) {
86 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
87 name_option);
88 return -2;
89 }
90 if (lib->load == nullptr) {
91 printf("agent: %s does not include an OnLoad method.\n", name_option);
92 return -3;
93 }
94 return lib->load(vm, remaining_options, reserved);
95}
96
97
98extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) {
99 char* remaining_options = nullptr;
100 char* name_option = nullptr;
101 if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) {
102 printf("Unable to find agent name in options: %s\n", options);
103 return -1;
104 }
105 AgentLib* lib = FindAgent(name_option);
106 if (lib == nullptr) {
107 printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n",
108 name_option);
109 return -2;
110 }
111 if (lib->attach == nullptr) {
112 printf("agent: %s does not include an OnAttach method.\n", name_option);
113 return -3;
114 }
115 return lib->attach(vm, remaining_options, reserved);
116}
117
118} // namespace art