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 | #include "agent.h" |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 18 | |
| 19 | #include "android-base/stringprintf.h" |
| 20 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 21 | #include "java_vm_ext.h" |
| 22 | #include "runtime.h" |
| 23 | |
| 24 | namespace art { |
| 25 | namespace ti { |
| 26 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 27 | using android::base::StringPrintf; |
| 28 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 29 | const char* AGENT_ON_LOAD_FUNCTION_NAME = "Agent_OnLoad"; |
| 30 | const char* AGENT_ON_ATTACH_FUNCTION_NAME = "Agent_OnAttach"; |
| 31 | const char* AGENT_ON_UNLOAD_FUNCTION_NAME = "Agent_OnUnload"; |
| 32 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 33 | // TODO We need to acquire some locks probably. |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 34 | Agent::LoadError Agent::DoLoadHelper(bool attaching, |
| 35 | /*out*/jint* call_res, |
| 36 | /*out*/std::string* error_msg) { |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 37 | DCHECK(call_res != nullptr); |
| 38 | DCHECK(error_msg != nullptr); |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 39 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 40 | if (IsStarted()) { |
| 41 | *error_msg = StringPrintf("the agent at %s has already been started!", name_.c_str()); |
| 42 | VLOG(agents) << "err: " << *error_msg; |
| 43 | return kAlreadyStarted; |
| 44 | } |
| 45 | LoadError err = DoDlOpen(error_msg); |
| 46 | if (err != kNoError) { |
| 47 | VLOG(agents) << "err: " << *error_msg; |
| 48 | return err; |
| 49 | } |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 50 | AgentOnLoadFunction callback = attaching ? onattach_ : onload_; |
| 51 | if (callback == nullptr) { |
| 52 | *error_msg = StringPrintf("Unable to start agent %s: No %s callback found", |
| 53 | (attaching ? "attach" : "load"), |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 54 | name_.c_str()); |
| 55 | VLOG(agents) << "err: " << *error_msg; |
| 56 | return kLoadingError; |
| 57 | } |
Alex Light | 49948e9 | 2016-08-11 15:35:28 -0700 | [diff] [blame] | 58 | // Need to let the function fiddle with the array. |
| 59 | std::unique_ptr<char[]> copied_args(new char[args_.size() + 1]); |
| 60 | strcpy(copied_args.get(), args_.c_str()); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 61 | // TODO Need to do some checks that we are at a good spot etc. |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 62 | *call_res = callback(Runtime::Current()->GetJavaVM(), |
| 63 | copied_args.get(), |
| 64 | nullptr); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 65 | if (*call_res != 0) { |
| 66 | *error_msg = StringPrintf("Initialization of %s returned non-zero value of %d", |
| 67 | name_.c_str(), *call_res); |
| 68 | VLOG(agents) << "err: " << *error_msg; |
| 69 | return kInitializationError; |
| 70 | } else { |
| 71 | return kNoError; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | Agent::LoadError Agent::DoDlOpen(/*out*/std::string* error_msg) { |
| 76 | DCHECK(error_msg != nullptr); |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 77 | |
| 78 | DCHECK(dlopen_handle_ == nullptr); |
| 79 | DCHECK(onload_ == nullptr); |
| 80 | DCHECK(onattach_ == nullptr); |
| 81 | DCHECK(onunload_ == nullptr); |
| 82 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 83 | dlopen_handle_ = dlopen(name_.c_str(), RTLD_LAZY); |
| 84 | if (dlopen_handle_ == nullptr) { |
| 85 | *error_msg = StringPrintf("Unable to dlopen %s: %s", name_.c_str(), dlerror()); |
| 86 | return kLoadingError; |
| 87 | } |
| 88 | |
| 89 | onload_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_, |
| 90 | AGENT_ON_LOAD_FUNCTION_NAME)); |
| 91 | if (onload_ == nullptr) { |
| 92 | VLOG(agents) << "Unable to find 'Agent_OnLoad' symbol in " << this; |
| 93 | } |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 94 | onattach_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_, |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 95 | AGENT_ON_ATTACH_FUNCTION_NAME)); |
| 96 | if (onattach_ == nullptr) { |
| 97 | VLOG(agents) << "Unable to find 'Agent_OnAttach' symbol in " << this; |
| 98 | } |
| 99 | onunload_= reinterpret_cast<AgentOnUnloadFunction>(dlsym(dlopen_handle_, |
| 100 | AGENT_ON_UNLOAD_FUNCTION_NAME)); |
| 101 | if (onunload_ == nullptr) { |
| 102 | VLOG(agents) << "Unable to find 'Agent_OnUnload' symbol in " << this; |
| 103 | } |
| 104 | return kNoError; |
| 105 | } |
| 106 | |
| 107 | // TODO Lock some stuff probably. |
| 108 | void Agent::Unload() { |
| 109 | if (dlopen_handle_ != nullptr) { |
| 110 | if (onunload_ != nullptr) { |
| 111 | onunload_(Runtime::Current()->GetJavaVM()); |
| 112 | } |
| 113 | dlclose(dlopen_handle_); |
| 114 | dlopen_handle_ = nullptr; |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 115 | onload_ = nullptr; |
| 116 | onattach_ = nullptr; |
| 117 | onunload_ = nullptr; |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 118 | } else { |
| 119 | VLOG(agents) << this << " is not currently loaded!"; |
| 120 | } |
| 121 | } |
| 122 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 123 | Agent::Agent(std::string arg) |
| 124 | : dlopen_handle_(nullptr), |
| 125 | onload_(nullptr), |
| 126 | onattach_(nullptr), |
| 127 | onunload_(nullptr) { |
| 128 | size_t eq = arg.find_first_of('='); |
| 129 | if (eq == std::string::npos) { |
| 130 | name_ = arg; |
| 131 | } else { |
| 132 | name_ = arg.substr(0, eq); |
| 133 | args_ = arg.substr(eq + 1, arg.length()); |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Leonard Mosescu | eb84221 | 2016-10-06 17:26:36 -0700 | [diff] [blame] | 137 | Agent::Agent(const Agent& other) |
| 138 | : dlopen_handle_(nullptr), |
| 139 | onload_(nullptr), |
| 140 | onattach_(nullptr), |
| 141 | onunload_(nullptr) { |
| 142 | *this = other; |
| 143 | } |
| 144 | |
| 145 | // Attempting to copy to/from loaded/started agents is a fatal error |
| 146 | Agent& Agent::operator=(const Agent& other) { |
| 147 | if (this != &other) { |
| 148 | if (other.dlopen_handle_ != nullptr) { |
| 149 | LOG(FATAL) << "Attempting to copy a loaded agent!"; |
| 150 | } |
| 151 | |
| 152 | if (dlopen_handle_ != nullptr) { |
| 153 | LOG(FATAL) << "Attempting to assign into a loaded agent!"; |
| 154 | } |
| 155 | |
| 156 | DCHECK(other.onload_ == nullptr); |
| 157 | DCHECK(other.onattach_ == nullptr); |
| 158 | DCHECK(other.onunload_ == nullptr); |
| 159 | |
| 160 | DCHECK(onload_ == nullptr); |
| 161 | DCHECK(onattach_ == nullptr); |
| 162 | DCHECK(onunload_ == nullptr); |
| 163 | |
| 164 | name_ = other.name_; |
| 165 | args_ = other.args_; |
| 166 | |
| 167 | dlopen_handle_ = nullptr; |
| 168 | onload_ = nullptr; |
| 169 | onattach_ = nullptr; |
| 170 | onunload_ = nullptr; |
| 171 | } |
| 172 | return *this; |
| 173 | } |
| 174 | |
| 175 | Agent::Agent(Agent&& other) |
| 176 | : dlopen_handle_(nullptr), |
| 177 | onload_(nullptr), |
| 178 | onattach_(nullptr), |
| 179 | onunload_(nullptr) { |
| 180 | *this = std::move(other); |
| 181 | } |
| 182 | |
| 183 | Agent& Agent::operator=(Agent&& other) { |
| 184 | if (this != &other) { |
| 185 | if (dlopen_handle_ != nullptr) { |
| 186 | dlclose(dlopen_handle_); |
| 187 | } |
| 188 | name_ = std::move(other.name_); |
| 189 | args_ = std::move(other.args_); |
| 190 | dlopen_handle_ = other.dlopen_handle_; |
| 191 | onload_ = other.onload_; |
| 192 | onattach_ = other.onattach_; |
| 193 | onunload_ = other.onunload_; |
| 194 | other.dlopen_handle_ = nullptr; |
| 195 | other.onload_ = nullptr; |
| 196 | other.onattach_ = nullptr; |
| 197 | other.onunload_ = nullptr; |
| 198 | } |
| 199 | return *this; |
| 200 | } |
| 201 | |
Alex Light | 7233c7e | 2016-07-28 10:07:45 -0700 | [diff] [blame] | 202 | Agent::~Agent() { |
| 203 | if (dlopen_handle_ != nullptr) { |
| 204 | dlclose(dlopen_handle_); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | std::ostream& operator<<(std::ostream &os, const Agent* m) { |
| 209 | return os << *m; |
| 210 | } |
| 211 | |
| 212 | std::ostream& operator<<(std::ostream &os, Agent const& m) { |
| 213 | return os << "Agent { name=\"" << m.name_ << "\", args=\"" << m.args_ << "\", handle=" |
| 214 | << m.dlopen_handle_ << " }"; |
| 215 | } |
| 216 | |
| 217 | } // namespace ti |
| 218 | } // namespace art |