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