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