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" |
| 25 | |
| 26 | #include "901-hello-ti-agent/basics.h" |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 27 | #include "902-hello-transformation/transform.h" |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 28 | #include "903-hello-tagging/tagging.h" |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame^] | 29 | #include "904-object-allocation/tracking.h" |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | using OnLoad = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 34 | using OnAttach = jint (*)(JavaVM* vm, char* options, void* reserved); |
| 35 | |
| 36 | struct AgentLib { |
| 37 | const char* name; |
| 38 | OnLoad load; |
| 39 | OnAttach attach; |
| 40 | }; |
| 41 | |
| 42 | // A list of all the agents we have for testing. |
| 43 | AgentLib agents[] = { |
| 44 | { "901-hello-ti-agent", Test901HelloTi::OnLoad, nullptr }, |
Alex Light | 9c20a14 | 2016-08-23 15:05:12 -0700 | [diff] [blame] | 45 | { "902-hello-transformation", Test902HelloTransformation::OnLoad, nullptr }, |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 46 | { "903-hello-tagging", Test903HelloTagging::OnLoad, nullptr }, |
Andreas Gampe | 27fa96c | 2016-10-07 15:05:24 -0700 | [diff] [blame^] | 47 | { "904-object-allocation", Test904ObjectAllocation::OnLoad, nullptr }, |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static AgentLib* FindAgent(char* name) { |
| 51 | for (AgentLib& l : agents) { |
| 52 | if (strncmp(l.name, name, strlen(l.name)) == 0) { |
| 53 | return &l; |
| 54 | } |
| 55 | } |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
| 59 | static bool FindAgentNameAndOptions(char* options, |
| 60 | /*out*/char** name, |
| 61 | /*out*/char** other_options) { |
| 62 | // Name is the first element. |
| 63 | *name = options; |
| 64 | char* rest = options; |
| 65 | // name is the first thing in the options |
| 66 | while (*rest != '\0' && *rest != ',') { |
| 67 | rest++; |
| 68 | } |
| 69 | if (*rest == ',') { |
| 70 | *rest = '\0'; |
| 71 | rest++; |
| 72 | } |
| 73 | *other_options = rest; |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) { |
| 78 | char* remaining_options = nullptr; |
| 79 | char* name_option = nullptr; |
| 80 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 81 | printf("Unable to find agent name in options: %s\n", options); |
| 82 | return -1; |
| 83 | } |
| 84 | AgentLib* lib = FindAgent(name_option); |
| 85 | if (lib == nullptr) { |
| 86 | printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n", |
| 87 | name_option); |
| 88 | return -2; |
| 89 | } |
| 90 | if (lib->load == nullptr) { |
| 91 | printf("agent: %s does not include an OnLoad method.\n", name_option); |
| 92 | return -3; |
| 93 | } |
| 94 | return lib->load(vm, remaining_options, reserved); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | extern "C" JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM* vm, char* options, void* reserved) { |
| 99 | char* remaining_options = nullptr; |
| 100 | char* name_option = nullptr; |
| 101 | if (!FindAgentNameAndOptions(options, &name_option, &remaining_options)) { |
| 102 | printf("Unable to find agent name in options: %s\n", options); |
| 103 | return -1; |
| 104 | } |
| 105 | AgentLib* lib = FindAgent(name_option); |
| 106 | if (lib == nullptr) { |
| 107 | printf("Unable to find agent named: %s, add it to the list in test/ti-agent/common_load.cc\n", |
| 108 | name_option); |
| 109 | return -2; |
| 110 | } |
| 111 | if (lib->attach == nullptr) { |
| 112 | printf("agent: %s does not include an OnAttach method.\n", name_option); |
| 113 | return -3; |
| 114 | } |
| 115 | return lib->attach(vm, remaining_options, reserved); |
| 116 | } |
| 117 | |
| 118 | } // namespace art |