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