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