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