Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 22 | #include "base/macros.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 23 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 24 | #include "jni_binder.h" |
| 25 | #include "jvmti_helper.h" |
| 26 | #include "test_env.h" |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 27 | |
| 28 | #include "901-hello-ti-agent/basics.h" |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 29 | #include "909-attach-agent/attach.h" |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame] | 30 | #include "936-search-onload/search_onload.h" |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 31 | #include "1919-vminit-thread-start-timing/vminit.h" |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
Alex Light | d0d6596 | 2017-06-30 11:13:33 -0700 | [diff] [blame] | 35 | namespace common_redefine { |
| 36 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 37 | } // namespace common_redefine |
| 38 | |
| 39 | namespace common_retransform { |
| 40 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 41 | } // namespace common_retransform |
| 42 | |
| 43 | namespace common_transform { |
| 44 | jint OnLoad(JavaVM* vm, char* options, void* reserved); |
| 45 | } // namespace common_transform |
| 46 | |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 47 | namespace { |
| 48 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 49 | using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 50 | using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 51 | |
| 52 | struct AgentLib { |
| 53 | const char* name; |
| 54 | OnLoad load; |
| 55 | OnAttach attach; |
| 56 | }; |
| 57 | |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 58 | // A trivial OnLoad implementation that only initializes the global jvmti_env. |
| 59 | static jint MinimalOnLoad(JavaVM* vm, |
| 60 | char* options ATTRIBUTE_UNUSED, |
| 61 | void* reserved ATTRIBUTE_UNUSED) { |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 62 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0) != 0) { |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 63 | printf("Unable to get jvmti env!\n"); |
| 64 | return 1; |
| 65 | } |
Alex Light | 3d324fd | 2017-07-20 15:38:52 -0700 | [diff] [blame] | 66 | SetStandardCapabilities(jvmti_env); |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | // A list of all non-standard the agents we have for testing. All other agents will use |
| 71 | // MinimalOnLoad. |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 72 | static AgentLib agents[] = { |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 73 | { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr }, |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 74 | { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach }, |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 75 | { "916-obsolete-jit", common_redefine::OnLoad, nullptr }, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 76 | { "921-hello-failure", common_retransform::OnLoad, nullptr }, |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 77 | { "934-load-transform", common_retransform::OnLoad, nullptr }, |
| 78 | { "935-non-retransformable", common_transform::OnLoad, nullptr }, |
Andreas Gampe | cefaa14 | 2017-01-23 15:04:59 -0800 | [diff] [blame] | 79 | { "936-search-onload", Test936SearchOnload::OnLoad, nullptr }, |
Alex Light | 2802712 | 2017-01-26 17:21:51 -0800 | [diff] [blame] | 80 | { "937-hello-retransform-package", common_retransform::OnLoad, nullptr }, |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 81 | { "938-load-transform-bcp", common_retransform::OnLoad, nullptr }, |
| 82 | { "939-hello-transformation-bcp", common_redefine::OnLoad, nullptr }, |
Alex Light | 1ebe4fe | 2017-01-30 14:57:11 -0800 | [diff] [blame] | 83 | { "941-recursive-obsolete-jit", common_redefine::OnLoad, nullptr }, |
Alex Light | 1ebe4fe | 2017-01-30 14:57:11 -0800 | [diff] [blame] | 84 | { "943-private-recursive-jit", common_redefine::OnLoad, nullptr }, |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 85 | { "1919-vminit-thread-start-timing", Test1919VMInitThreadStart::OnLoad, nullptr }, |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static AgentLib* FindAgent(char* name) { |
| 89 | for (AgentLib& l : agents) { |
| 90 | if (strncmp(l.name, name, strlen(l.name)) == 0) { |
| 91 | return &l; |
| 92 | } |
| 93 | } |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
| 97 | static bool FindAgentNameAndOptions(char* options, |
| 98 | /*out*/char** name, |
| 99 | /*out*/char** other_options) { |
| 100 | // Name is the first element. |
| 101 | *name = options; |
| 102 | char* rest = options; |
| 103 | // name is the first thing in the options |
| 104 | while (*rest != '\0' && *rest != ',') { |
| 105 | rest++; |
| 106 | } |
| 107 | if (*rest == ',') { |
| 108 | *rest = '\0'; |
| 109 | rest++; |
| 110 | } |
| 111 | *other_options = rest; |
| 112 | return true; |
| 113 | } |
| 114 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 115 | static void SetIsJVM(const char* options) { |
| 116 | SetJVM(strncmp(options, "jvm", 3) == 0); |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 119 | } // namespace |
| 120 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 121 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) { |
| 122 | char* remaining_options = nullptr; |
| 123 | char* name_option = nullptr; |
| 124 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 125 | printf("Unable to find agent name in options: %s\n", options); |
| 126 | return -1; |
| 127 | } |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 128 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 129 | SetIsJVM(remaining_options); |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 130 | |
| 131 | AgentLib* lib = FindAgent(name_option); |
| 132 | OnLoad fn = nullptr; |
| 133 | if (lib == nullptr) { |
| 134 | fn = &MinimalOnLoad; |
| 135 | } else { |
| 136 | if (lib->load == nullptr) { |
| 137 | printf("agent: %s does not include an OnLoad method.\n", name_option); |
| 138 | return -3; |
| 139 | } |
| 140 | fn = lib->load; |
| 141 | } |
| 142 | return fn(vm, remaining_options, reserved); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 145 | extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) { |
| 146 | char* remaining_options = nullptr; |
| 147 | char* name_option = nullptr; |
| 148 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 149 | printf("Unable to find agent name in options: %s\n", options); |
| 150 | return -1; |
| 151 | } |
Andreas Gampe | 53ae780 | 2017-01-19 21:13:46 -0800 | [diff] [blame] | 152 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 153 | AgentLib* lib = FindAgent(name_option); |
| 154 | if (lib == nullptr) { |
| 155 | printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n", |
| 156 | name_option); |
| 157 | return -2; |
| 158 | } |
| 159 | if (lib->attach == nullptr) { |
| 160 | printf("agent: %s does not include an OnAttach method.\n", name_option); |
| 161 | return -3; |
| 162 | } |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 163 | SetIsJVM(remaining_options); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 164 | return lib->attach(vm, remaining_options, reserved); |
| 165 | } |
| 166 | |
| 167 | } // namespace art |