| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 | #ifndef ART_RUNTIME_TI_AGENT_H_ |
| 18 | #define ART_RUNTIME_TI_AGENT_H_ |
| 19 | |
| 20 | #include <dlfcn.h> |
| 21 | #include <jni.h> // for jint, JavaVM* etc declarations |
| 22 | |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 23 | #include <memory> |
| 24 | |
| Andreas Gampe | 85f1c57 | 2018-11-21 13:52:48 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/macros.h> |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace ti { |
| 30 | |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 31 | class Agent; |
| 32 | |
| 33 | enum LoadError { |
| 34 | kNoError, // No error occurred.. |
| 35 | kLoadingError, // dlopen or dlsym returned an error. |
| 36 | kInitializationError, // The entrypoint did not return 0. This might require an abort. |
| 37 | }; |
| 38 | |
| 39 | class AgentSpec { |
| 40 | public: |
| 41 | explicit AgentSpec(const std::string& arg); |
| 42 | |
| 43 | const std::string& GetName() const { |
| 44 | return name_; |
| 45 | } |
| 46 | |
| 47 | const std::string& GetArgs() const { |
| 48 | return args_; |
| 49 | } |
| 50 | |
| 51 | bool HasArgs() const { |
| 52 | return !GetArgs().empty(); |
| 53 | } |
| 54 | |
| 55 | std::unique_ptr<Agent> Load(/*out*/jint* call_res, |
| 56 | /*out*/LoadError* error, |
| 57 | /*out*/std::string* error_msg); |
| 58 | |
| 59 | // Tries to attach the agent using its OnAttach method. Returns true on success. |
| Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 60 | std::unique_ptr<Agent> Attach(JNIEnv* env, |
| 61 | jobject class_loader, |
| 62 | /*out*/jint* call_res, |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 63 | /*out*/LoadError* error, |
| 64 | /*out*/std::string* error_msg); |
| 65 | |
| 66 | private: |
| Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 67 | std::unique_ptr<Agent> DoDlOpen(JNIEnv* env, |
| 68 | jobject class_loader, |
| 69 | /*out*/LoadError* error, |
| 70 | /*out*/std::string* error_msg); |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 71 | |
| Andreas Gampe | 7b38e69 | 2017-12-28 19:18:28 -0800 | [diff] [blame] | 72 | std::unique_ptr<Agent> DoLoadHelper(JNIEnv* env, |
| 73 | bool attaching, |
| 74 | jobject class_loader, |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 75 | /*out*/jint* call_res, |
| 76 | /*out*/LoadError* error, |
| 77 | /*out*/std::string* error_msg); |
| 78 | |
| 79 | std::string name_; |
| 80 | std::string args_; |
| 81 | |
| 82 | friend std::ostream& operator<<(std::ostream &os, AgentSpec const& m); |
| 83 | }; |
| 84 | |
| 85 | std::ostream& operator<<(std::ostream &os, AgentSpec const& m); |
| 86 | |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 87 | using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*); |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 88 | using AgentOnUnloadFunction = void (*)(JavaVM*); |
| 89 | |
| Alex Light | 65af20b | 2017-04-20 09:15:08 -0700 | [diff] [blame] | 90 | // Agents are native libraries that will be loaded by the runtime for the purpose of |
| 91 | // instrumentation. They will be entered by Agent_OnLoad or Agent_OnAttach depending on whether the |
| 92 | // agent is being attached during runtime startup or later. |
| 93 | // |
| 94 | // The agent's Agent_OnUnload function will be called during runtime shutdown. |
| 95 | // |
| Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 96 | // TODO: consider splitting ti::Agent into command line, agent and shared library handler classes |
| Alex Light | 65af20b | 2017-04-20 09:15:08 -0700 | [diff] [blame] | 97 | // TODO Support native-bridge. Currently agents can only be the actual runtime ISA of the device. |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 98 | class Agent { |
| 99 | public: |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 100 | const std::string& GetName() const { |
| 101 | return name_; |
| 102 | } |
| 103 | |
| Alex Light | 65af20b | 2017-04-20 09:15:08 -0700 | [diff] [blame] | 104 | void* FindSymbol(const std::string& name) const; |
| 105 | |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 106 | // TODO We need to acquire some locks probably. |
| 107 | void Unload(); |
| 108 | |
| Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 109 | Agent(Agent&& other) noexcept; |
| 110 | Agent& operator=(Agent&& other) noexcept; |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 111 | |
| 112 | ~Agent(); |
| 113 | |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 114 | private: |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 115 | Agent(const std::string& name, void* dlopen_handle) : name_(name), |
| 116 | dlopen_handle_(dlopen_handle), |
| 117 | onload_(nullptr), |
| 118 | onattach_(nullptr), |
| 119 | onunload_(nullptr) { |
| 120 | DCHECK(dlopen_handle != nullptr); |
| 121 | } |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 122 | |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 123 | void PopulateFunctions(); |
| Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 124 | |
| 125 | std::string name_; |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 126 | void* dlopen_handle_; |
| 127 | |
| 128 | // The entrypoints. |
| 129 | AgentOnLoadFunction onload_; |
| Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 130 | AgentOnLoadFunction onattach_; |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 131 | AgentOnUnloadFunction onunload_; |
| 132 | |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 133 | friend class AgentSpec; |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 134 | friend std::ostream& operator<<(std::ostream &os, Agent const& m); |
| Andreas Gampe | aadcbc6 | 2017-12-28 14:05:42 -0800 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(Agent); |
| Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | std::ostream& operator<<(std::ostream &os, Agent const& m); |
| 140 | std::ostream& operator<<(std::ostream &os, const Agent* m); |
| 141 | |
| 142 | } // namespace ti |
| 143 | } // namespace art |
| 144 | |
| 145 | #endif // ART_RUNTIME_TI_AGENT_H_ |
| 146 | |