Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/art/art/runtime/ti/   (Android Betriebssystem Version 17©)  Datei vom 26.5.2026 mit Größe 8 kB image not shown  

Quelle  agent.cc

  Sprache: C
 

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "agent.h"

#include "android-base/stringprintf.h"
#include "base/logging.h"
#include "base/strlcpy.h"
#include "jni/java_vm_ext.h"
#include "nativebridge/native_bridge.h"
#include "nativehelper/scoped_local_ref.h"
#include "nativeloader/native_loader.h"
#include "runtime.h"
#include "scoped_thread_state_change-inl.h"
#include "thread-current-inl.h"

namespace art HIDDEN {
namespace ti {

using android::base::StringPrintf;

const char* AGENT_ON_LOAD_FUNCTION_NAME = "Agent_OnLoad";
const char* AGENT_ON_ATTACH_FUNCTION_NAME = "Agent_OnAttach";
const char* AGENT_ON_UNLOAD_FUNCTION_NAME = "Agent_OnUnload";

AgentSpec::AgentSpec(const std::string& arg) {
  size_t eq = arg.find_first_of('=');
  if (eq == std::string::npos) {
    name_ = arg;
  } else {
    name_ = arg.substr(0, eq);
    args_ = arg.substr(eq + 1, arg.length());
  }
}

std::unique_ptr<Agent> AgentSpec::Load(/*out*/jint* call_res,
                                       /*out*/LoadError* error,
                                       /*out*/std::string* error_msg) {
  VLOG(agents) << "Loading agent: " << name_ << " " << args_;
  return DoLoadHelper(nullptr, false, nullptr, call_res, error, error_msg);
}

// 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(new char[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;
}

std::unique_ptr<Agent> AgentSpec::DoDlOpen(JNIEnv* env,
                                           jobject class_loader,
                                           /*out*/LoadError* error,
                                           /*out*/std::string* error_msg) {
  DCHECK(error_msg != nullptr);

  ScopedLocalRef<jstring> library_path(env,
                                       class_loader == nullptr
                                           ? nullptr
                                           : JavaVMExt::GetLibrarySearchPath(env, class_loader));

  bool needs_native_bridge = false;
  char* nativeloader_error_msg = nullptr;
  void* dlopen_handle = android::OpenNativeLibrary(env,
                                                   Runtime::Current()->GetTargetSdkVersion(),
                                                   name_.c_str(),
                                                   class_loader,
                                                   nullptr,
                                                   library_path.get(),
                                                   &needs_native_bridge,
                                                   &nativeloader_error_msg);
  if (dlopen_handle == nullptr) {
    *error_msg = StringPrintf("Unable to dlopen %s: %s",
                              name_.c_str(),
                              nativeloader_error_msg);
    android::NativeLoaderFreeErrorMessage(nativeloader_error_msg);
    *error = kLoadingError;
    return nullptr;
  }

  std::unique_ptr<Agent> agent{new Agent(name_, dlopen_handle, needs_native_bridge)};
  agent->PopulateFunctions();
  *error = kNoError;
  return agent;
}

std::ostream& operator<<(std::ostream &os, AgentSpec const& m) {
  return os << "AgentSpec { name=\"" << m.name_ << "\", args=\"" << m.args_ << "\" }";
}

void* Agent::FindSymbol(const std::string& name,
                        const char* shorty,
                        android::JNICallType jni_call_type) const {
  CHECK(dlopen_handle_ != nullptr) << "Cannot find symbols in an unloaded agent library " << this;

  if (needs_native_bridge_) {
    size_t shorty_len = shorty != nullptr ? strlen(shorty) : 0;
    return android::NativeBridgeGetTrampoline2(
        dlopen_handle_, name.c_str(), shorty, shorty_len, jni_call_type);
  } else {
    return dlsym(dlopen_handle_, name.c_str());
  }
}

// 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!";
  }
}

Agent::Agent(Agent&& other) noexcept
    : dlopen_handle_(nullptr),
      onload_(nullptr),
      onattach_(nullptr),
      onunload_(nullptr) {
  *this = std::move(other);
}

Agent& Agent::operator=(Agent&& other) noexcept {
  if (this != &other) {
    if (dlopen_handle_ != nullptr) {
      Unload();
    }
    name_ = std::move(other.name_);
    dlopen_handle_ = other.dlopen_handle_;
    onload_ = other.onload_;
    onattach_ = other.onattach_;
    onunload_ = other.onunload_;
    other.dlopen_handle_ = nullptr;
    other.onload_ = nullptr;
    other.onattach_ = nullptr;
    other.onunload_ = nullptr;
  }
  return *this;
}

void Agent::PopulateFunctions() {
  onload_ = reinterpret_cast<AgentOnLoadFunction>(
      FindSymbol(AGENT_ON_LOAD_FUNCTION_NAME, nullptr, android::kJNICallTypeRegular));
  if (onload_ == nullptr) {
    VLOG(agents) << "Unable to find 'Agent_OnLoad' symbol in " << this;
  }
  onattach_ = reinterpret_cast<AgentOnLoadFunction>(
      FindSymbol(AGENT_ON_ATTACH_FUNCTION_NAME, nullptr, android::kJNICallTypeRegular));
  if (onattach_ == nullptr) {
    VLOG(agents) << "Unable to find 'Agent_OnAttach' symbol in " << this;
  }
  onunload_ = reinterpret_cast<AgentOnUnloadFunction>(
      FindSymbol(AGENT_ON_UNLOAD_FUNCTION_NAME, nullptr, android::kJNICallTypeRegular));
  if (onunload_ == nullptr) {
    VLOG(agents) << "Unable to find 'Agent_OnUnload' symbol in " << this;
  }
}

Agent::~Agent() {
  if (dlopen_handle_ != nullptr) {
    Unload();
  }
}

std::ostream& operator<<(std::ostream &os, const Agent* m) {
  return os << *m;
}

std::ostream& operator<<(std::ostream &os, Agent const& m) {
  return os << "Agent { name=\"" << m.name_ << "\", handle=" << m.dlopen_handle_ << " }";
}

}  // namespace ti
}  // namespace art

Messung V0.5 in Prozent
C=88 H=95 G=91

¤ Dauer der Verarbeitung: 0.10 Sekunden  (vorverarbeitet am  2026-06-29) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

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.