From ad69f9d11a8c1e046ff1157e69c6a0b519faaf85 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Thu, 2 Apr 2026 12:37:31 +0800 Subject: [PATCH] Impl: Data Structures and set/map --- src/libagent/callback/ClassFileLoadHook.cpp | 2 ++ src/libagent/core/global.cpp | 5 ++++ src/libagent/includes/global.hpp | 31 +++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/libagent/core/global.cpp create mode 100644 src/libagent/includes/global.hpp diff --git a/src/libagent/callback/ClassFileLoadHook.cpp b/src/libagent/callback/ClassFileLoadHook.cpp index 02a1841..19013f3 100644 --- a/src/libagent/callback/ClassFileLoadHook.cpp +++ b/src/libagent/callback/ClassFileLoadHook.cpp @@ -12,5 +12,7 @@ void JNICALL ClassFileLoadHook( const unsigned char* classbytes, jint* new_class_data_len, unsigned char** new_classbytes) { + // Delete this log when the hook is stable, or it will cause performance issues std::cout << "ClassFileLoadHook: " << name << std::endl; + } \ No newline at end of file diff --git a/src/libagent/core/global.cpp b/src/libagent/core/global.cpp new file mode 100644 index 0000000..9363c15 --- /dev/null +++ b/src/libagent/core/global.cpp @@ -0,0 +1,5 @@ +#include + +std::unordered_set classToCapture; +std::unordered_map classFileDataMap; +std::mutex classDataMutex; diff --git a/src/libagent/includes/global.hpp b/src/libagent/includes/global.hpp new file mode 100644 index 0000000..40107ae --- /dev/null +++ b/src/libagent/includes/global.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +// Data Structures +struct ClassFileData { + std::string className; + std::vector bytecode; + + // ======== Global references to prevent GC ======== + // Remember to release these references when they are no longer needed, otherwise it will cause memory leaks + jclass clazz; + jclass classloader; + jobject protection_domain; + +}; + +// Set of target class internal names to capture (e.g. java/lang/String) +extern std::unordered_set classToCapture; + +// Cache of captured class bytecode, keyed by internal class name +extern std::unordered_map classFileDataMap; + +// Mutex protecting access to classToCapture and classFileDataMap (thread-safe for JVMTI callbacks) +extern std::mutex classDataMutex; \ No newline at end of file