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 | |
| 23 | #include "base/stringprintf.h" |
| 24 | #include "runtime.h" |
| 25 | #include "utils.h" |
| 26 | |
| 27 | namespace art { |
| 28 | namespace ti { |
| 29 | |
| 30 | using AgentOnLoadFunction = jint (*)(JavaVM*, const char*, void*); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 31 | using AgentOnUnloadFunction = void (*)(JavaVM*); |
| 32 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 33 | // TODO: consider splitting ti::Agent into command line, agent and shared library handler classes |
| 34 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 35 | class Agent { |
| 36 | public: |
| 37 | enum LoadError { |
| 38 | kNoError, // No error occurred.. |
| 39 | kAlreadyStarted, // The agent has already been loaded. |
| 40 | kLoadingError, // dlopen or dlsym returned an error. |
| 41 | kInitializationError, // The entrypoint did not return 0. This might require an abort. |
| 42 | }; |
| 43 | |
| 44 | bool IsStarted() const { |
| 45 | return dlopen_handle_ != nullptr; |
| 46 | } |
| 47 | |
| 48 | const std::string& GetName() const { |
| 49 | return name_; |
| 50 | } |
| 51 | |
| 52 | const std::string& GetArgs() const { |
| 53 | return args_; |
| 54 | } |
| 55 | |
| 56 | bool HasArgs() const { |
| 57 | return !GetArgs().empty(); |
| 58 | } |
| 59 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 60 | LoadError Load(/*out*/jint* call_res, /*out*/std::string* error_msg) { |
| 61 | VLOG(agents) << "Loading agent: " << name_ << " " << args_; |
| 62 | return DoLoadHelper(false, call_res, error_msg); |
| 63 | } |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 64 | |
| 65 | // TODO We need to acquire some locks probably. |
| 66 | void Unload(); |
| 67 | |
| 68 | // Tries to attach the agent using its OnAttach method. Returns true on success. |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 69 | LoadError Attach(/*out*/jint* call_res, /*out*/std::string* error_msg) { |
| 70 | VLOG(agents) << "Attaching agent: " << name_ << " " << args_; |
| 71 | return DoLoadHelper(true, call_res, error_msg); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 74 | explicit Agent(std::string arg); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 75 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 76 | Agent(const Agent& other); |
| 77 | Agent& operator=(const Agent& other); |
| 78 | |
| 79 | Agent(Agent&& other); |
| 80 | Agent& operator=(Agent&& other); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 81 | |
| 82 | ~Agent(); |
| 83 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 84 | private: |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 85 | LoadError DoDlOpen(/*out*/std::string* error_msg); |
| 86 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 87 | LoadError DoLoadHelper(bool attaching, |
| 88 | /*out*/jint* call_res, |
| 89 | /*out*/std::string* error_msg); |
| 90 | |
| 91 | std::string name_; |
| 92 | std::string args_; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 93 | void* dlopen_handle_; |
| 94 | |
| 95 | // The entrypoints. |
| 96 | AgentOnLoadFunction onload_; |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 97 | AgentOnLoadFunction onattach_; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 98 | AgentOnUnloadFunction onunload_; |
| 99 | |
| 100 | friend std::ostream& operator<<(std::ostream &os, Agent const& m); |
| 101 | }; |
| 102 | |
| 103 | std::ostream& operator<<(std::ostream &os, Agent const& m); |
| 104 | std::ostream& operator<<(std::ostream &os, const Agent* m); |
| 105 | |
| 106 | } // namespace ti |
| 107 | } // namespace art |
| 108 | |
| 109 | #endif // ART_RUNTIME_TI_AGENT_H_ |
| 110 | |