mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Refactor: bytecode.cpp
This commit is contained in:
1 parent
34dbd82a59
commit
71965165b9
1 file changed
+63
-44
@@ -1,69 +1,95 @@
|
||||
#include <jni_impl.hpp>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
|
||||
// ============================
|
||||
// Get class bytecode (already captured)
|
||||
// ============================
|
||||
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytes
|
||||
(JNIEnv *env, jclass, jclass clazz) {
|
||||
|
||||
auto& agent = JuiceAgent::Agent::instance();
|
||||
if (!check_env(agent))
|
||||
return nullptr;
|
||||
if (!check_env(agent)) return nullptr;
|
||||
|
||||
char* signature = nullptr;
|
||||
if (agent.get_jvmti()->GetClassSignature(clazz, &signature, nullptr) != JVMTI_ERROR_NONE || !signature) {
|
||||
if (agent.get_jvmti()->GetClassSignature(clazz, &signature, nullptr) != JVMTI_ERROR_NONE || !signature)
|
||||
return nullptr;
|
||||
|
||||
// Convert Lxxx; -> xxx
|
||||
std::string internalName(signature + 1, strlen(signature) - 2);
|
||||
agent.get_jvmti()->Deallocate(reinterpret_cast<unsigned char*>(signature));
|
||||
|
||||
// ============================
|
||||
// 1. Mark class for capture
|
||||
// ============================
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(classDataMutex);
|
||||
classToCapture.insert(internalName);
|
||||
}
|
||||
|
||||
// Lxxx; -> xxx
|
||||
std::string internalName(signature + 1, strlen(signature) - 2);
|
||||
agent.get_jvmti()->Deallocate((unsigned char*)signature);
|
||||
|
||||
classToCapture.insert(internalName); // Add to capture list
|
||||
|
||||
// ============================
|
||||
// 2. Trigger class retransform
|
||||
// ============================
|
||||
jvmtiError err = agent.get_jvmti()->RetransformClasses(1, &clazz);
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
PLOGE << "Retransform class failed: " << err;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ClassFileData data = classFileDataMap[internalName];
|
||||
if (data.classname.empty()) {
|
||||
PLOGE << "Failed to capture class [classname null]: " << internalName;
|
||||
// ============================
|
||||
// 3. Retrieve bytecode from cache
|
||||
// ============================
|
||||
ClassFileData data;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(classDataMutex);
|
||||
auto it = classFileDataMap.find(internalName);
|
||||
if (it != classFileDataMap.end()) {
|
||||
data = it->second;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.bytecode.empty()) {
|
||||
PLOGE << "Failed to capture bytecode for class [byetcode null]: " << internalName;
|
||||
PLOGE << "Failed to capture bytecode for class: " << internalName;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
jbyteArray out = env->NewByteArray(data.bytecode.size());
|
||||
env->SetByteArrayRegion(out, 0, data.bytecode.size(), (jbyte*)data.bytecode.data());
|
||||
jbyteArray out = env->NewByteArray(static_cast<jsize>(data.bytecode.size()));
|
||||
if (!out || env->ExceptionCheck()) {
|
||||
env->ExceptionClear();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(out, 0, static_cast<jsize>(data.bytecode.size()), reinterpret_cast<const jbyte*>(data.bytecode.data()));
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================
|
||||
// Get class bytecode by class name
|
||||
// ============================
|
||||
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytesByName
|
||||
(JNIEnv *env, jclass loader_class, jstring name) {
|
||||
|
||||
if (!name) return nullptr;
|
||||
auto& agent = JuiceAgent::Agent::instance();
|
||||
if (!check_env(agent) || !name)
|
||||
return nullptr;
|
||||
if (!check_env(agent)) return nullptr;
|
||||
|
||||
// =========================
|
||||
// 1. jstring -> internal name
|
||||
// =========================
|
||||
// ============================
|
||||
// 1. Convert jstring -> internal name
|
||||
// ============================
|
||||
const char* utf = env->GetStringUTFChars(name, nullptr);
|
||||
if (!utf)
|
||||
return nullptr;
|
||||
|
||||
if (!utf) return nullptr;
|
||||
std::string internalName(utf);
|
||||
env->ReleaseStringUTFChars(name, utf);
|
||||
|
||||
// a.b.C -> a/b/C
|
||||
// Convert dots to slashes: a.b.C -> a/b/C
|
||||
std::replace(internalName.begin(), internalName.end(), '.', '/');
|
||||
|
||||
// =========================
|
||||
// 2. Find loaded class via JVMTI
|
||||
// =========================
|
||||
// ============================
|
||||
// 2. Iterate loaded classes
|
||||
// ============================
|
||||
jint count = 0;
|
||||
jclass* classes = nullptr;
|
||||
|
||||
jvmtiEnv* jvmti = agent.get_jvmti();
|
||||
|
||||
if (jvmti->GetLoadedClasses(&count, &classes) != JVMTI_ERROR_NONE || count == 0) {
|
||||
@@ -71,28 +97,23 @@ JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getCl
|
||||
}
|
||||
|
||||
jclass targetClass = nullptr;
|
||||
|
||||
for (jint i = 0; i < count; i++) {
|
||||
char* signature = nullptr;
|
||||
|
||||
if (jvmti->GetClassSignature(classes[i], &signature, nullptr) == JVMTI_ERROR_NONE && signature) {
|
||||
size_t len = strlen(signature);
|
||||
|
||||
// Lxxx; -> xxx
|
||||
if (len > 2 && signature[0] == 'L' && signature[len - 1] == ';') {
|
||||
if (internalName == std::string(signature + 1, len - 2)) {
|
||||
if (internalName == std::string_view(signature + 1, len - 2)) {
|
||||
targetClass = classes[i];
|
||||
jvmti->Deallocate((unsigned char*)signature);
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
jvmti->Deallocate((unsigned char*)signature);
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
|
||||
}
|
||||
}
|
||||
|
||||
if (classes) {
|
||||
jvmti->Deallocate((unsigned char*)classes);
|
||||
jvmti->Deallocate(reinterpret_cast<unsigned char*>(classes));
|
||||
}
|
||||
|
||||
if (!targetClass) {
|
||||
@@ -100,10 +121,8 @@ JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getCl
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// 3. Reuse existing logic
|
||||
// =========================
|
||||
return Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytes(
|
||||
env, loader_class, targetClass
|
||||
);
|
||||
// ============================
|
||||
// 3. Reuse getClassBytes logic
|
||||
// ============================
|
||||
return Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytes(env, loader_class, targetClass);
|
||||
}
|
||||
Reference in new issue
Block a user