mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Perf & Fix: reduce ClassFileLoadHook overhead and fix retransform patch path
- Skip mutable event dispatch when no mutable listeners exist (hot path) - Wire retransformClass bytecode into pendingRetransform so patches are applied - Guard pendingRetransform with its mutex; check jvmti Allocate result - Store global refs in captured ClassFileData and release on re-capture - Add null/exception checks in defineClass/getLoadedClasses - Extract shared find_class_by_internal_name helper - Use CONFIGURE_DEPENDS for CMake globs; guard InitLogger; delete leaked jArgs
This commit is contained in:
1 parent
8cf2b8330c
commit
bb5b215a33
10 files changed
+198
-130
No files matched your search
@@ -64,6 +64,29 @@ public:
|
||||
return token;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Listeners presence check
|
||||
// =========================
|
||||
template<typename Event>
|
||||
bool has_listeners() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto it = listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == listeners_.end()) return false;
|
||||
|
||||
return !std::static_pointer_cast<std::vector<Node<Event>>>(it->second)->empty();
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
bool has_mutable_listeners() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto it = mutable_listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == mutable_listeners_.end()) return false;
|
||||
|
||||
return !std::static_pointer_cast<std::vector<MutableNode<Event>>>(it->second)->empty();
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Unsubscribe
|
||||
// =========================
|
||||
@@ -161,7 +184,7 @@ private:
|
||||
MutableListener<Event> callback;
|
||||
};
|
||||
|
||||
std::mutex mutex_;
|
||||
mutable std::mutex mutex_;
|
||||
std::atomic<Token> nextToken_{1};
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> listeners_;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <JuiceAgent.hpp>
|
||||
#include <cstring>
|
||||
|
||||
inline bool check_env(JuiceAgent::Agent& agent) {
|
||||
if (agent.get_jvm() == nullptr) {
|
||||
@@ -14,6 +15,41 @@ inline bool check_env(JuiceAgent::Agent& agent) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Search all loaded classes for one whose internal name (e.g. "a/b/C") matches.
|
||||
// Returns a jclass reference owned by JVMTI (must not be deleted); nullptr if not found.
|
||||
inline jclass find_class_by_internal_name(jvmtiEnv* jvmti, const std::string& internal_name) {
|
||||
if (!jvmti || internal_name.empty()) return nullptr;
|
||||
|
||||
jint count = 0;
|
||||
jclass* classes = nullptr;
|
||||
if (jvmti->GetLoadedClasses(&count, &classes) != JVMTI_ERROR_NONE || count == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
jclass result = nullptr;
|
||||
for (jint i = 0; i < count; i++) {
|
||||
char* signature = nullptr;
|
||||
if (jvmti->GetClassSignature(classes[i], &signature, nullptr) != JVMTI_ERROR_NONE || !signature) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t len = strlen(signature);
|
||||
// Signature format: "La/b/C;"
|
||||
if (len > 2 && signature[0] == 'L' && signature[len - 1] == ';' &&
|
||||
internal_name.size() == len - 2 &&
|
||||
memcmp(signature + 1, internal_name.data(), len - 2) == 0) {
|
||||
result = classes[i];
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
|
||||
break;
|
||||
}
|
||||
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
|
||||
}
|
||||
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(classes));
|
||||
return result;
|
||||
}
|
||||
|
||||
inline std::string get_class_name(JNIEnv* env, jclass clazz) {
|
||||
if (!env || !clazz) return "";
|
||||
|
||||
|
||||
Reference in new issue
Block a user