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 | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 67 | { "921-hello-failure", common_redefine::OnLoad, nullptr }, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 68 | { "926-multi-obsolescence", common_redefine::OnLoad, nullptr }, |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static AgentLib* FindAgent(char* name) { |
| 72 | for (AgentLib& l : agents) { |
| 73 | if (strncmp(l.name, name, strlen(l.name)) == 0) { |
| 74 | return &l; |
| 75 | } |
| 76 | } |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | static bool FindAgentNameAndOptions(char* options, |
| 81 | /*out*/char** name, |
| 82 | /*out*/char** other_options) { |
| 83 | // Name is the first element. |
| 84 | *name = options; |
| 85 | char* rest = options; |
| 86 | // name is the first thing in the options |
| 87 | while (*rest != '\0' && *rest != ',') { |
| 88 | rest++; |
| 89 | } |
| 90 | if (*rest == ',') { |
| 91 | *rest = '\0'; |
| 92 | rest++; |
| 93 | } |
| 94 | *other_options = rest; |
| 95 | return true; |
| 96 | } |
| 97 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 98 | static void SetIsJVM(char* options) { |
| 99 | RuntimeIsJVM = strncmp(options, "jvm", 3) == 0; |
| 100 | } |
| 101 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 102 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) { |
| 103 | char* remaining_options = nullptr; |
| 104 | char* name_option = nullptr; |
| 105 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 106 | printf("Unable to find agent name in options: %s\n", options); |
| 107 | return -1; |
| 108 | } |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 109 | |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 110 | SetIsJVM(remaining_options); |
Andreas Gampe | a8883a0 | 2017-01-11 19:53:50 -0800 | [diff] [blame] | 111 | |
| 112 | AgentLib* lib = FindAgent(name_option); |
| 113 | OnLoad fn = nullptr; |
| 114 | if (lib == nullptr) { |
| 115 | fn = &MinimalOnLoad; |
| 116 | } else { |
| 117 | if (lib->load == nullptr) { |
| 118 | printf("agent: %s does not include an OnLoad method.\n", name_option); |
| 119 | return -3; |
| 120 | } |
| 121 | fn = lib->load; |
| 122 | } |
| 123 | return fn(vm, remaining_options, reserved); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 126 | extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) { |
| 127 | char* remaining_options = nullptr; |
| 128 | char* name_option = nullptr; |
| 129 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 130 | printf("Unable to find agent name in options: %s\n", options); |
| 131 | return -1; |
| 132 | } |
| 133 | AgentLib* lib = FindAgent(name_option); |
| 134 | if (lib == nullptr) { |
| 135 | printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n", |
| 136 | name_option); |
| 137 | return -2; |
| 138 | } |
| 139 | if (lib->attach == nullptr) { |
| 140 | printf("agent: %s does not include an OnAttach method.\n", name_option); |
| 141 | return -3; |
| 142 | } |
Alex Light | 1e07ca6 | 2016-12-02 11:40:56 -0800 | [diff] [blame] | 143 | SetIsJVM(remaining_options); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 144 | return lib->attach(vm, remaining_options, reserved); |
| 145 | } |
| 146 | |
| 147 | } // namespace art |