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