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> |
| 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 Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 25 | #include "common_load.h" |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 26 | #include "common_helper.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" |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame] | 33 | jvmtiEnv* jvmti_env; |
| 34 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 35 | using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 36 | using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 37 | |
| 38 | struct AgentLib { |
| 39 | const char* name; |
| 40 | OnLoad load; |
| 41 | OnAttach attach; |
| 42 | }; |
| 43 | |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 44 | // A trivial OnLoad implementation that only initializes the global jvmti_env. |
| 45 | static jint MinimalOnLoad(JavaVM* vm, |
| 46 | char* options ATTRIBUTE_UNUSED, |
| 47 | void* reserved ATTRIBUTE_UNUSED) { |
| 48 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 49 | printf("Unable to get jvmti env!\n"); |
| 50 | return 1; |
| 51 | } |
| 52 | SetAllCapabilities(jvmti_env); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | // A list of all non-standard the agents we have for testing. All other agents will use |
| 57 | // MinimalOnLoad. |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 58 | AgentLib agents[] = { |
| 59 | { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr }, |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 60 | { "902-hello-transformation", common_redefine::OnLoad, nullptr }, |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 61 | { "909-attach-agent", nullptr, Test909AttachAgent::OnAttach }, |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 62 | { "914-hello-obsolescence", common_redefine::OnLoad, nullptr }, |
| 63 | { "915-obsolete-2", common_redefine::OnLoad, nullptr }, |
| 64 | { "916-obsolete-jit", common_redefine::OnLoad, nullptr }, |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 65 | { "917-fields-transformation", common_redefine::OnLoad, nullptr }, |
Alex Light | 32a2fba | 2017-01-06 16:58:19 +0000 | [diff] [blame] | 66 | { "919-obsolete-fields", common_redefine::OnLoad, nullptr }, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 67 | { "921-hello-failure", common_retransform::OnLoad, nullptr }, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 68 | { "926-multi-obsolescence", common_redefine::OnLoad, nullptr }, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame^] | 69 | { "930-hello-retransform", common_retransform::OnLoad, nullptr }, |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | static AgentLib* FindAgent(char* name) { |
| 73 | for (AgentLib& l : agents) { |
| 74 | if (strncmp(l.name, name, strlen(l.name)) == 0) { |
| 75 | return &l; |
| 76 | } |
| 77 | } |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
| 81 | static bool FindAgentNameAndOptions(char* options, |
| 82 | /*out*/char** name, |
| 83 | /*out*/char** other_options) { |
| 84 | // Name is the first element. |
| 85 | *name = options; |
| 86 | char* rest = options; |
| 87 | // name is the first thing in the options |
| 88 | while (*rest != '\0' && *rest != ',') { |
| 89 | rest++; |
| 90 | } |
| 91 | if (*rest == ',') { |
| 92 | *rest = '\0'; |
| 93 | rest++; |
| 94 | } |
| 95 | *other_options = rest; |
| 96 | return true; |
| 97 | } |
| 98 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 99 | static void SetIsJVM(char* options) { |
| 100 | RuntimeIsJVM = strncmp(options, "jvm", 3) == 0; |
| 101 | } |
| 102 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 103 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) { |
| 104 | char* remaining_options = nullptr; |
| 105 | char* name_option = nullptr; |
| 106 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 107 | printf("Unable to find agent name in options: %s\n", options); |
| 108 | return -1; |
| 109 | } |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 110 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 111 | SetIsJVM(remaining_options); |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 112 | |
| 113 | AgentLib* lib = FindAgent(name_option); |
| 114 | OnLoad fn = nullptr; |
| 115 | if (lib == nullptr) { |
| 116 | fn = &MinimalOnLoad; |
| 117 | } else { |
| 118 | if (lib->load == nullptr) { |
| 119 | printf("agent: %s does not include an OnLoad method.\n", name_option); |
| 120 | return -3; |
| 121 | } |
| 122 | fn = lib->load; |
| 123 | } |
| 124 | return fn(vm, remaining_options, reserved); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 127 | extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) { |
| 128 | char* remaining_options = nullptr; |
| 129 | char* name_option = nullptr; |
| 130 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 131 | printf("Unable to find agent name in options: %s\n", options); |
| 132 | return -1; |
| 133 | } |
| 134 | AgentLib* lib = FindAgent(name_option); |
| 135 | if (lib == nullptr) { |
| 136 | printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n", |
| 137 | name_option); |
| 138 | return -2; |
| 139 | } |
| 140 | if (lib->attach == nullptr) { |
| 141 | printf("agent: %s does not include an OnAttach method.\n", name_option); |
| 142 | return -3; |
| 143 | } |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 144 | SetIsJVM(remaining_options); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 145 | return lib->attach(vm, remaining_options, reserved); |
| 146 | } |
| 147 | |
| 148 | } // namespace art |