2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "java_vm_ext.h"
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";
28 // TODO We need to acquire some locks probably.
29 Agent::LoadError Agent::DoLoadHelper(bool attaching,
30 /*out*/jint* call_res,
31 /*out*/std::string* error_msg) {
32 DCHECK(call_res != nullptr);
33 DCHECK(error_msg != nullptr);
36 *error_msg = StringPrintf("the agent at %s has already been started!", name_.c_str());
37 VLOG(agents) << "err: " << *error_msg;
38 return kAlreadyStarted;
40 LoadError err = DoDlOpen(error_msg);
41 if (err != kNoError) {
42 VLOG(agents) << "err: " << *error_msg;
45 AgentOnLoadFunction callback = attaching ? onattach_ : onload_;
46 if (callback == nullptr) {
47 *error_msg = StringPrintf("Unable to start agent %s: No %s callback found",
48 (attaching ? "attach" : "load"),
50 VLOG(agents) << "err: " << *error_msg;
53 // Need to let the function fiddle with the array.
54 std::unique_ptr<char[]> copied_args(new char[args_.size() + 1]);
55 strcpy(copied_args.get(), args_.c_str());
56 // TODO Need to do some checks that we are at a good spot etc.
57 *call_res = callback(Runtime::Current()->GetJavaVM(),
61 *error_msg = StringPrintf("Initialization of %s returned non-zero value of %d",
62 name_.c_str(), *call_res);
63 VLOG(agents) << "err: " << *error_msg;
64 return kInitializationError;
70 Agent::LoadError Agent::DoDlOpen(/*out*/std::string* error_msg) {
71 DCHECK(error_msg != nullptr);
73 DCHECK(dlopen_handle_ == nullptr);
74 DCHECK(onload_ == nullptr);
75 DCHECK(onattach_ == nullptr);
76 DCHECK(onunload_ == nullptr);
78 dlopen_handle_ = dlopen(name_.c_str(), RTLD_LAZY);
79 if (dlopen_handle_ == nullptr) {
80 *error_msg = StringPrintf("Unable to dlopen %s: %s", name_.c_str(), dlerror());
84 onload_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_,
85 AGENT_ON_LOAD_FUNCTION_NAME));
86 if (onload_ == nullptr) {
87 VLOG(agents) << "Unable to find 'Agent_OnLoad' symbol in " << this;
89 onattach_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_,
90 AGENT_ON_ATTACH_FUNCTION_NAME));
91 if (onattach_ == nullptr) {
92 VLOG(agents) << "Unable to find 'Agent_OnAttach' symbol in " << this;
94 onunload_= reinterpret_cast<AgentOnUnloadFunction>(dlsym(dlopen_handle_,
95 AGENT_ON_UNLOAD_FUNCTION_NAME));
96 if (onunload_ == nullptr) {
97 VLOG(agents) << "Unable to find 'Agent_OnUnload' symbol in " << this;
102 // TODO Lock some stuff probably.
103 void Agent::Unload() {
104 if (dlopen_handle_ != nullptr) {
105 if (onunload_ != nullptr) {
106 onunload_(Runtime::Current()->GetJavaVM());
108 dlclose(dlopen_handle_);
109 dlopen_handle_ = nullptr;
114 VLOG(agents) << this << " is not currently loaded!";
118 Agent::Agent(std::string arg)
119 : dlopen_handle_(nullptr),
123 size_t eq = arg.find_first_of('=');
124 if (eq == std::string::npos) {
127 name_ = arg.substr(0, eq);
128 args_ = arg.substr(eq + 1, arg.length());
132 Agent::Agent(const Agent& other)
133 : dlopen_handle_(nullptr),
140 // Attempting to copy to/from loaded/started agents is a fatal error
141 Agent& Agent::operator=(const Agent& other) {
142 if (this != &other) {
143 if (other.dlopen_handle_ != nullptr) {
144 LOG(FATAL) << "Attempting to copy a loaded agent!";
147 if (dlopen_handle_ != nullptr) {
148 LOG(FATAL) << "Attempting to assign into a loaded agent!";
151 DCHECK(other.onload_ == nullptr);
152 DCHECK(other.onattach_ == nullptr);
153 DCHECK(other.onunload_ == nullptr);
155 DCHECK(onload_ == nullptr);
156 DCHECK(onattach_ == nullptr);
157 DCHECK(onunload_ == nullptr);
162 dlopen_handle_ = nullptr;
170 Agent::Agent(Agent&& other)
171 : dlopen_handle_(nullptr),
175 *this = std::move(other);
178 Agent& Agent::operator=(Agent&& other) {
179 if (this != &other) {
180 if (dlopen_handle_ != nullptr) {
181 dlclose(dlopen_handle_);
183 name_ = std::move(other.name_);
184 args_ = std::move(other.args_);
185 dlopen_handle_ = other.dlopen_handle_;
186 onload_ = other.onload_;
187 onattach_ = other.onattach_;
188 onunload_ = other.onunload_;
189 other.dlopen_handle_ = nullptr;
190 other.onload_ = nullptr;
191 other.onattach_ = nullptr;
192 other.onunload_ = nullptr;
198 if (dlopen_handle_ != nullptr) {
199 dlclose(dlopen_handle_);
203 std::ostream& operator<<(std::ostream &os, const Agent* m) {
207 std::ostream& operator<<(std::ostream &os, Agent const& m) {
208 return os << "Agent { name=\"" << m.name_ << "\", args=\"" << m.args_ << "\", handle="
209 << m.dlopen_handle_ << " }";