// Tries to attach the agent using its OnAttach method. Returns true on success.
std::unique_ptr<Agent> AgentSpec::Attach(JNIEnv* env,
jobject class_loader, /*out*/jint* call_res, /*out*/LoadError* error, /*out*/std::string* error_msg) {
VLOG(agents) << "Attaching agent: " << name_ << " " << args_; return DoLoadHelper(env, true, class_loader, call_res, error, error_msg);
}
// TODO We need to acquire some locks probably.
std::unique_ptr<Agent> AgentSpec::DoLoadHelper(JNIEnv* env, bool attaching,
jobject class_loader, /*out*/jint* call_res, /*out*/LoadError* error, /*out*/std::string* error_msg) {
ScopedThreadStateChange stsc(Thread::Current(), ThreadState::kNative);
DCHECK(call_res != nullptr);
DCHECK(error_msg != nullptr);
std::unique_ptr<Agent> agent = DoDlOpen(env, class_loader, error, error_msg); if (agent == nullptr) {
VLOG(agents) << "err: " << *error_msg; return nullptr;
}
AgentOnLoadFunction callback = attaching ? agent->onattach_ : agent->onload_; if (callback == nullptr) {
*error_msg = StringPrintf("Unable to start agent %s: No %s callback found",
(attaching ? "attach" : "load"),
name_.c_str());
VLOG(agents) << "err: " << *error_msg;
*error = kLoadingError; return nullptr;
} // Need to let the function fiddle with the array.
std::unique_ptr<char[]> copied_args(newchar[args_.size() + 1]);
strlcpy(copied_args.get(), args_.c_str(), args_.size() + 1); // TODO Need to do some checks that we are at a good spot etc.
*call_res = callback(Runtime::Current()->GetJavaVM(),
copied_args.get(),
nullptr); if (*call_res != 0) {
*error_msg = StringPrintf("Initialization of %s returned non-zero value of %d",
name_.c_str(), *call_res);
VLOG(agents) << "err: " << *error_msg;
*error = kInitializationError; return nullptr;
} return agent;
}
// TODO Lock some stuff probably. void Agent::Unload() { if (dlopen_handle_ != nullptr) { if (onunload_ != nullptr) {
onunload_(Runtime::Current()->GetJavaVM());
} // Don't actually android::CloseNativeLibrary since some agents assume they will never get // unloaded. Since this only happens when the runtime is shutting down anyway this isn't a big // deal.
dlopen_handle_ = nullptr;
onload_ = nullptr;
onattach_ = nullptr;
onunload_ = nullptr;
} else {
VLOG(agents) << this << " is not currently loaded!";
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.