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" |
| 18 | #include "java_vm_ext.h" |
| 19 | #include "runtime.h" |
| 20 | |
| 21 | namespace art { |
| 22 | namespace ti { |
| 23 | |
| 24 | const char* AGENT_ON_LOAD_FUNCTION_NAME = "Agent_OnLoad"; |
| 25 | const char* AGENT_ON_ATTACH_FUNCTION_NAME = "Agent_OnAttach"; |
| 26 | const char* AGENT_ON_UNLOAD_FUNCTION_NAME = "Agent_OnUnload"; |
| 27 | |
| 28 | Agent Agent::Create(std::string arg) { |
| 29 | size_t eq = arg.find_first_of('='); |
| 30 | if (eq == std::string::npos) { |
| 31 | return Agent(arg, ""); |
| 32 | } else { |
| 33 | return Agent(arg.substr(0, eq), arg.substr(eq + 1, arg.length())); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // TODO We need to acquire some locks probably. |
| 38 | Agent::LoadError Agent::Load(/*out*/jint* call_res, /*out*/ std::string* error_msg) { |
| 39 | DCHECK(call_res != nullptr); |
| 40 | DCHECK(error_msg != nullptr); |
| 41 | if (IsStarted()) { |
| 42 | *error_msg = StringPrintf("the agent at %s has already been started!", name_.c_str()); |
| 43 | VLOG(agents) << "err: " << *error_msg; |
| 44 | return kAlreadyStarted; |
| 45 | } |
| 46 | LoadError err = DoDlOpen(error_msg); |
| 47 | if (err != kNoError) { |
| 48 | VLOG(agents) << "err: " << *error_msg; |
| 49 | return err; |
| 50 | } |
| 51 | if (onload_ == nullptr) { |
| 52 | *error_msg = StringPrintf("Unable to start agent %s: No Agent_OnLoad function found", |
| 53 | name_.c_str()); |
| 54 | VLOG(agents) << "err: " << *error_msg; |
| 55 | return kLoadingError; |
| 56 | } |
| 57 | // TODO Need to do some checks that we are at a good spot etc. |
| 58 | *call_res = onload_(static_cast<JavaVM*>(Runtime::Current()->GetJavaVM()), |
| 59 | args_.c_str(), |
| 60 | nullptr); |
| 61 | if (*call_res != 0) { |
| 62 | *error_msg = StringPrintf("Initialization of %s returned non-zero value of %d", |
| 63 | name_.c_str(), *call_res); |
| 64 | VLOG(agents) << "err: " << *error_msg; |
| 65 | return kInitializationError; |
| 66 | } else { |
| 67 | return kNoError; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Agent::LoadError Agent::DoDlOpen(/*out*/std::string* error_msg) { |
| 72 | DCHECK(error_msg != nullptr); |
| 73 | dlopen_handle_ = dlopen(name_.c_str(), RTLD_LAZY); |
| 74 | if (dlopen_handle_ == nullptr) { |
| 75 | *error_msg = StringPrintf("Unable to dlopen %s: %s", name_.c_str(), dlerror()); |
| 76 | return kLoadingError; |
| 77 | } |
| 78 | |
| 79 | onload_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_, |
| 80 | AGENT_ON_LOAD_FUNCTION_NAME)); |
| 81 | if (onload_ == nullptr) { |
| 82 | VLOG(agents) << "Unable to find 'Agent_OnLoad' symbol in " << this; |
| 83 | } |
| 84 | onattach_ = reinterpret_cast<AgentOnAttachFunction>(dlsym(dlopen_handle_, |
| 85 | AGENT_ON_ATTACH_FUNCTION_NAME)); |
| 86 | if (onattach_ == nullptr) { |
| 87 | VLOG(agents) << "Unable to find 'Agent_OnAttach' symbol in " << this; |
| 88 | } |
| 89 | onunload_= reinterpret_cast<AgentOnUnloadFunction>(dlsym(dlopen_handle_, |
| 90 | AGENT_ON_UNLOAD_FUNCTION_NAME)); |
| 91 | if (onunload_ == nullptr) { |
| 92 | VLOG(agents) << "Unable to find 'Agent_OnUnload' symbol in " << this; |
| 93 | } |
| 94 | return kNoError; |
| 95 | } |
| 96 | |
| 97 | // TODO Lock some stuff probably. |
| 98 | void Agent::Unload() { |
| 99 | if (dlopen_handle_ != nullptr) { |
| 100 | if (onunload_ != nullptr) { |
| 101 | onunload_(Runtime::Current()->GetJavaVM()); |
| 102 | } |
| 103 | dlclose(dlopen_handle_); |
| 104 | dlopen_handle_ = nullptr; |
| 105 | } else { |
| 106 | VLOG(agents) << this << " is not currently loaded!"; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | Agent::Agent(const Agent& other) |
| 111 | : name_(other.name_), |
| 112 | args_(other.args_), |
| 113 | dlopen_handle_(other.dlopen_handle_), |
| 114 | onload_(other.onload_), |
| 115 | onattach_(other.onattach_), |
| 116 | onunload_(other.onunload_) { |
| 117 | if (other.dlopen_handle_ != nullptr) { |
| 118 | dlopen(other.name_.c_str(), 0); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | Agent::~Agent() { |
| 123 | if (dlopen_handle_ != nullptr) { |
| 124 | dlclose(dlopen_handle_); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | std::ostream& operator<<(std::ostream &os, const Agent* m) { |
| 129 | return os << *m; |
| 130 | } |
| 131 | |
| 132 | std::ostream& operator<<(std::ostream &os, Agent const& m) { |
| 133 | return os << "Agent { name=\"" << m.name_ << "\", args=\"" << m.args_ << "\", handle=" |
| 134 | << m.dlopen_handle_ << " }"; |
| 135 | } |
| 136 | |
| 137 | } // namespace ti |
| 138 | } // namespace art |