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