blob: 0bba44c988040f348766b5e4748d59bd5665abce [file] [log] [blame]
Alex Light7233c7e2016-07-28 10:07:45 -07001/*
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 Gampe46ee31b2016-12-14 10:11:49 -080018
19#include "android-base/stringprintf.h"
20
Alex Light7233c7e2016-07-28 10:07:45 -070021#include "java_vm_ext.h"
22#include "runtime.h"
23
24namespace art {
25namespace ti {
26
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027using android::base::StringPrintf;
28
Alex Light7233c7e2016-07-28 10:07:45 -070029const char* AGENT_ON_LOAD_FUNCTION_NAME = "Agent_OnLoad";
30const char* AGENT_ON_ATTACH_FUNCTION_NAME = "Agent_OnAttach";
31const char* AGENT_ON_UNLOAD_FUNCTION_NAME = "Agent_OnUnload";
32
Alex Light7233c7e2016-07-28 10:07:45 -070033// TODO We need to acquire some locks probably.
Leonard Mosescueb842212016-10-06 17:26:36 -070034Agent::LoadError Agent::DoLoadHelper(bool attaching,
35 /*out*/jint* call_res,
36 /*out*/std::string* error_msg) {
Alex Light7233c7e2016-07-28 10:07:45 -070037 DCHECK(call_res != nullptr);
38 DCHECK(error_msg != nullptr);
Alex Light49948e92016-08-11 15:35:28 -070039
Alex Light7233c7e2016-07-28 10:07:45 -070040 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 Mosescueb842212016-10-06 17:26:36 -070050 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 Light7233c7e2016-07-28 10:07:45 -070054 name_.c_str());
55 VLOG(agents) << "err: " << *error_msg;
56 return kLoadingError;
57 }
Alex Light49948e92016-08-11 15:35:28 -070058 // 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 Light7233c7e2016-07-28 10:07:45 -070061 // TODO Need to do some checks that we are at a good spot etc.
Leonard Mosescueb842212016-10-06 17:26:36 -070062 *call_res = callback(Runtime::Current()->GetJavaVM(),
63 copied_args.get(),
64 nullptr);
Alex Light7233c7e2016-07-28 10:07:45 -070065 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
75Agent::LoadError Agent::DoDlOpen(/*out*/std::string* error_msg) {
76 DCHECK(error_msg != nullptr);
Leonard Mosescueb842212016-10-06 17:26:36 -070077
78 DCHECK(dlopen_handle_ == nullptr);
79 DCHECK(onload_ == nullptr);
80 DCHECK(onattach_ == nullptr);
81 DCHECK(onunload_ == nullptr);
82
Alex Light7233c7e2016-07-28 10:07:45 -070083 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 Mosescueb842212016-10-06 17:26:36 -070094 onattach_ = reinterpret_cast<AgentOnLoadFunction>(dlsym(dlopen_handle_,
Alex Light7233c7e2016-07-28 10:07:45 -070095 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.
108void 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 Mosescueb842212016-10-06 17:26:36 -0700115 onload_ = nullptr;
116 onattach_ = nullptr;
117 onunload_ = nullptr;
Alex Light7233c7e2016-07-28 10:07:45 -0700118 } else {
119 VLOG(agents) << this << " is not currently loaded!";
120 }
121}
122
Leonard Mosescueb842212016-10-06 17:26:36 -0700123Agent::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 Light7233c7e2016-07-28 10:07:45 -0700134 }
135}
136
Leonard Mosescueb842212016-10-06 17:26:36 -0700137Agent::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
146Agent& 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
175Agent::Agent(Agent&& other)
176 : dlopen_handle_(nullptr),
177 onload_(nullptr),
178 onattach_(nullptr),
179 onunload_(nullptr) {
180 *this = std::move(other);
181}
182
183Agent& 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 Light7233c7e2016-07-28 10:07:45 -0700202Agent::~Agent() {
203 if (dlopen_handle_ != nullptr) {
204 dlclose(dlopen_handle_);
205 }
206}
207
208std::ostream& operator<<(std::ostream &os, const Agent* m) {
209 return os << *m;
210}
211
212std::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