feat(debug): 添加调试窗口和类监控功能
- 新增 DebugWindow 类用于显示调试信息 - 在主程序中添加调试窗口的创建和显示逻辑 - 新增 ClassDebug 类提供类监控和调试功能 - 更新 build.gradle 添加 byte-buddy 依赖
This commit is contained in:
227
src/main/java/org/tzd/debug/ClassDebug.java
Normal file
227
src/main/java/org/tzd/debug/ClassDebug.java
Normal file
@@ -0,0 +1,227 @@
|
||||
package org.tzd.debug;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.api.dog.agent.VirtualMachine;
|
||||
import org.tzd.debug.util.GlobalObjects;
|
||||
|
||||
import javax.tools.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.IllegalClassFormatException;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.ProtectionDomain;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 对类进行监控和debug操作
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class ClassDebug {
|
||||
private static final Logger logger = LogManager.getLogger(ClassDebug.class);
|
||||
private static Instrumentation instrumentation;
|
||||
|
||||
/**
|
||||
* 监控类加载
|
||||
* @param monitor 监控器
|
||||
*/
|
||||
public static void monitoringLoading(LoadingMonitor monitor) {
|
||||
if (instrumentation != null) {
|
||||
instrumentation.addTransformer(new ClassFileTransformer() {
|
||||
@Override
|
||||
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)
|
||||
throws IllegalClassFormatException {
|
||||
monitor.load(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
|
||||
return classfileBuffer;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
logger.error("Instrumentation is null");
|
||||
try {
|
||||
VirtualMachine vm = VirtualMachine.getVirtualMachine(ProcessHandle.current().pid(), true);
|
||||
instrumentation = vm.getInstrumentation();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to attach to VM: {}", e.getMessage());
|
||||
}
|
||||
monitoringLoading(monitor);
|
||||
}
|
||||
}
|
||||
|
||||
public static Instrumentation getInstrumentation() {
|
||||
if (instrumentation == null) {
|
||||
try {
|
||||
VirtualMachine vm = VirtualMachine.getVirtualMachine(ProcessHandle.current().pid(), true);
|
||||
instrumentation = vm.getInstrumentation();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to attach to VM: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
return instrumentation;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行动态代码
|
||||
* @param code 要执行的代码
|
||||
* @return 执行结果(空字符串表示成功,否则为错误信息)
|
||||
*/
|
||||
public static String executeCode(String code) {
|
||||
GlobalObjects globalObjects = new GlobalObjects();
|
||||
globalObjects.instrumentation = instrumentation;
|
||||
|
||||
|
||||
// 1. 预处理代码:替换print/printf为System.out
|
||||
String processedCode = code.trim();
|
||||
if (processedCode.startsWith("print(") && processedCode.endsWith(");")) {
|
||||
processedCode = "System.out.println" + processedCode.substring("print".length());
|
||||
} else if (processedCode.startsWith("printf(") && processedCode.endsWith(");")) {
|
||||
processedCode = "System.out.printf" + processedCode.substring("printf".length());
|
||||
}
|
||||
|
||||
// 2. 构建完整类代码
|
||||
String className = "DynamicExecutedCode";
|
||||
String fullCode = "package dynamic;\n" + // 添加包声明避免冲突
|
||||
"import org.tzd.debug.util.GlobalObjects;import java.util.*;\n" +
|
||||
"public class " + className + " {\n" +
|
||||
" public static GlobalObjects global;\n" + // 静态字段接收全局对象
|
||||
" public static void run() {\n" +
|
||||
" try {\n" + // 添加try-catch捕获用户代码异常
|
||||
processedCode + "\n" +
|
||||
" } catch (Throwable t) {\n" +
|
||||
" throw new RuntimeException(t);\n" + // 包装异常以保持堆栈
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
|
||||
Path tempDir = null;
|
||||
try {
|
||||
// 3. 创建临时目录和源文件
|
||||
tempDir = Files.createTempDirectory("dynamicCode");
|
||||
Path sourceDir = tempDir.resolve("dynamic");
|
||||
Files.createDirectories(sourceDir);
|
||||
Path sourceFile = sourceDir.resolve(className + ".java");
|
||||
Files.write(sourceFile, fullCode.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
// 4. 编译代码
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
if (compiler == null) {
|
||||
return "错误:找不到Java编译器。请使用JDK运行此程序。";
|
||||
}
|
||||
|
||||
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
|
||||
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
|
||||
String[] compileOptions = {"-d", tempDir.toString(), "-cp", System.getProperty("java.class.path")};
|
||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(sourceFile);
|
||||
|
||||
JavaCompiler.CompilationTask task = compiler.getTask(
|
||||
null, fileManager, diagnostics,
|
||||
Arrays.asList(compileOptions), null, compilationUnits
|
||||
);
|
||||
|
||||
boolean success = task.call();
|
||||
if (!success) {
|
||||
StringBuilder errorMsg = new StringBuilder("编译错误:\n");
|
||||
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
|
||||
errorMsg.append(String.format(
|
||||
"Line %d: %s\n",
|
||||
diagnostic.getLineNumber(),
|
||||
diagnostic.getMessage(Locale.getDefault())
|
||||
));
|
||||
}
|
||||
return errorMsg.toString();
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 加载并执行
|
||||
URLClassLoader classLoader = new URLClassLoader(
|
||||
new URL[]{tempDir.toUri().toURL()},
|
||||
ClassDebug.class.getClassLoader()
|
||||
);
|
||||
Class<?> loadedClass = classLoader.loadClass("dynamic." + className);
|
||||
|
||||
// 注入全局对象
|
||||
Field globalField = loadedClass.getDeclaredField("global");
|
||||
globalField.set(null, globalObjects);
|
||||
|
||||
Method runMethod = loadedClass.getMethod("run");
|
||||
runMethod.invoke(null);
|
||||
return "";
|
||||
} catch (Throwable e) {
|
||||
// 6. 异常处理
|
||||
Throwable cause = e;
|
||||
while (cause.getCause() != null) {
|
||||
cause = cause.getCause();
|
||||
}
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
cause.printStackTrace(new PrintWriter(sw));
|
||||
return "运行时错误:" + cause.getMessage() + "\n堆栈跟踪:\n" + sw.toString();
|
||||
} finally {
|
||||
// 7. 清理资源
|
||||
if (tempDir != null) {
|
||||
try {
|
||||
Files.walk(tempDir)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(Path::toFile)
|
||||
.forEach(File::delete);
|
||||
} catch (IOException ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
executeCode("JOptionPane.showMessageDialog(null, \"普通对话框\");");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已加载的类
|
||||
* @return 已加载的类
|
||||
*/
|
||||
public static Class<?>[] getLoadedClasses() {
|
||||
if (instrumentation == null){
|
||||
try {
|
||||
VirtualMachine vm = VirtualMachine.getVirtualMachine(ProcessHandle.current().pid(), true);
|
||||
instrumentation = vm.getInstrumentation();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to attach to VM: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
return instrumentation.getAllLoadedClasses();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类的方法与字段
|
||||
* @param clazz 类名
|
||||
* @return 类的方法与字段
|
||||
*/
|
||||
public static String getMethodAndField(String clazz) {
|
||||
try {
|
||||
Class<?> aClass = Class.forName(clazz);
|
||||
String methods = Arrays.stream(aClass.getDeclaredMethods())
|
||||
.map(Method::toString)
|
||||
.collect(Collectors.joining("\n"));
|
||||
String fields = Arrays.stream(aClass.getDeclaredFields())
|
||||
.map(Field::toString)
|
||||
.collect(Collectors.joining("\n"));
|
||||
return "方法:\n" + methods + "\n\n字段:\n" + fields;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("Class not found: " + clazz, e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface LoadingMonitor {
|
||||
void load(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer);
|
||||
}
|
||||
}
|
||||
18
src/main/java/org/tzd/debug/GetInstance.java
Normal file
18
src/main/java/org/tzd/debug/GetInstance.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.tzd.debug;
|
||||
|
||||
/**
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class GetInstance {
|
||||
|
||||
static {
|
||||
System.load("C:\\Users\\Administrator\\source\\repos\\GetInstance\\x64\\Release\\GetInstance.dll");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类的所有实例
|
||||
* @param clazz 类
|
||||
* @return 类的所有实例
|
||||
*/
|
||||
public static native Object[] getInstance(Class<?> clazz);
|
||||
}
|
||||
217
src/main/java/org/tzd/debug/MethodDebug.java
Normal file
217
src/main/java/org/tzd/debug/MethodDebug.java
Normal file
@@ -0,0 +1,217 @@
|
||||
package org.tzd.debug;
|
||||
|
||||
import net.bytebuddy.agent.builder.AgentBuilder;
|
||||
import net.bytebuddy.asm.Advice;
|
||||
import net.bytebuddy.implementation.MethodDelegation;
|
||||
import net.bytebuddy.implementation.bind.annotation.AllArguments;
|
||||
import net.bytebuddy.implementation.bind.annotation.Origin;
|
||||
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
|
||||
import net.bytebuddy.implementation.bind.annotation.SuperCall;
|
||||
import net.bytebuddy.implementation.bytecode.assign.Assigner;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.api.dog.agent.VirtualMachine;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class MethodDebug {
|
||||
private static final Logger logger = LogManager.getLogger(MethodDebug.class);
|
||||
private static Instrumentation instrumentation;
|
||||
private static final List<MethodMonitorCallback> callbacks = new CopyOnWriteArrayList<>();
|
||||
|
||||
static {
|
||||
try {
|
||||
VirtualMachine vm = VirtualMachine.getVirtualMachine(ProcessHandle.current().pid(), true);
|
||||
instrumentation = vm.getInstrumentation();
|
||||
} catch (Exception e) {
|
||||
logger.error("Failed to attach to VM: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
try {
|
||||
new AgentBuilder.Default()
|
||||
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
|
||||
// 只拦截你的应用代码,排除系统类
|
||||
.ignore(ElementMatchers.nameStartsWith("java.")
|
||||
.or(ElementMatchers.nameStartsWith("javax."))
|
||||
.or(ElementMatchers.nameStartsWith("sun."))
|
||||
.or(ElementMatchers.nameStartsWith("com.sun."))
|
||||
.or(ElementMatchers.nameStartsWith("jdk."))
|
||||
.or(ElementMatchers.nameStartsWith("org.tzd.debug.")))
|
||||
.or(ElementMatchers.nameStartsWith("com.axis.innovators.box.tools.RegisterTray"))
|
||||
.or(ElementMatchers.nameStartsWith("com.formdev.flatlaf."))
|
||||
.or(ElementMatchers.nameStartsWith("com.axis.innovators.box.gui."))
|
||||
.or(ElementMatchers.nameStartsWith("org.apache."))
|
||||
.or(ElementMatchers.nameStartsWith("jdk."))
|
||||
.or(ElementMatchers.nameStartsWith("org.fife."))
|
||||
.or(ElementMatchers.nameStartsWith("java.awt."))
|
||||
.or(ElementMatchers.nameStartsWith("javax.swing."))
|
||||
.type(ElementMatchers.any())
|
||||
.transform((builder, type, classLoader, module, pd) ->
|
||||
builder.method(ElementMatchers.any().
|
||||
and(ElementMatchers.not(ElementMatchers.isNative()))
|
||||
.and(ElementMatchers.not(ElementMatchers.isAbstract()))
|
||||
)
|
||||
.intercept(MethodDelegation.to(
|
||||
MethodInterceptor.class))
|
||||
).installOn(instrumentation);
|
||||
} catch (Exception e) {
|
||||
logger.error("Agent installation failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addCallback(MethodMonitorCallback callback) {
|
||||
callbacks.add(callback);
|
||||
}
|
||||
|
||||
public static void removeCallback(MethodMonitorCallback callback) {
|
||||
callbacks.remove(callback);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
logger.info("Starting MethodDebug main method");
|
||||
|
||||
// 先添加回调
|
||||
addCallback(new MethodMonitorCallback() {
|
||||
@Override
|
||||
public void onMethodEnter(String className, String methodName, Object[] args) {
|
||||
logger.info("ENTER: {}.{}", className, methodName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMethodExit(String className, String methodName, Object returnValue, Throwable exception, long durationNanos) {
|
||||
String status = exception != null ? "FAILED" : "SUCCESS";
|
||||
logger.info("EXIT: {}.{} - {} ({} ns)", className, methodName, status, durationNanos);
|
||||
}
|
||||
});
|
||||
|
||||
// 然后安装Agent
|
||||
load();
|
||||
|
||||
// 测试监控
|
||||
logger.info("Starting test loop...");
|
||||
while (true) {
|
||||
try {
|
||||
logger.info("Calling ds.ada()...");
|
||||
//ds.ada();
|
||||
Thread.sleep(1000); // 添加延迟避免过度占用CPU
|
||||
} catch (Exception e) {
|
||||
logger.error("Error in test loop", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface MethodMonitorCallback {
|
||||
void onMethodEnter(String className, String methodName, Object[] args);
|
||||
void onMethodExit(String className, String methodName, Object returnValue, Throwable exception, long durationNanos);
|
||||
}
|
||||
|
||||
public static class MethodInterceptor {
|
||||
@RuntimeType
|
||||
public static Object intercept(@Origin Method method,
|
||||
@SuperCall Callable<?> callable,
|
||||
@AllArguments Object[] args) throws Exception {
|
||||
String className = method.getDeclaringClass().getName();
|
||||
String methodName = method.getName();
|
||||
|
||||
// 通知进入
|
||||
for (MethodMonitorCallback callback : callbacks) {
|
||||
try {
|
||||
callback.onMethodEnter(className, methodName, args);
|
||||
} catch (Throwable t) {
|
||||
logger.error("Callback error in onMethodEnter", t);
|
||||
}
|
||||
}
|
||||
|
||||
long start = System.nanoTime();
|
||||
Object result = null;
|
||||
Throwable exception = null;
|
||||
|
||||
try {
|
||||
if (callable != null) {
|
||||
result = callable.call();
|
||||
}
|
||||
return result;
|
||||
} catch (Throwable t) {
|
||||
exception = t;
|
||||
throw t;
|
||||
} finally {
|
||||
long duration = System.nanoTime() - start;
|
||||
|
||||
// 通知退出
|
||||
for (MethodMonitorCallback callback : callbacks) {
|
||||
try {
|
||||
callback.onMethodExit(className, methodName, result, exception, duration);
|
||||
} catch (Throwable t) {
|
||||
logger.error("Callback error in onMethodExit", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class MethodTimerAdvice {
|
||||
@Advice.OnMethodEnter
|
||||
static MethodContext enter(@Advice.Origin Method method,
|
||||
@Advice.AllArguments Object[] args) {
|
||||
System.out.println("MethodTimerAdvice.enter");
|
||||
if (callbacks.isEmpty()) return null;
|
||||
|
||||
MethodContext context = new MethodContext();
|
||||
context.startTime = System.nanoTime();
|
||||
context.className = method.getDeclaringClass().getName();
|
||||
context.methodName = method.getName();
|
||||
context.args = args;
|
||||
|
||||
// 通知所有回调
|
||||
for (MethodMonitorCallback callback : callbacks) {
|
||||
try {
|
||||
callback.onMethodEnter(context.className, context.methodName, args);
|
||||
} catch (Throwable t) {
|
||||
logger.error("Callback execution error in onMethodEnter", t);
|
||||
}
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
@Advice.OnMethodExit(onThrowable = Throwable.class)
|
||||
static void exit(@Advice.Enter MethodContext context,
|
||||
@Advice.Thrown Throwable exception,
|
||||
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue) {
|
||||
System.out.println("MethodTimerAdvice.exit");
|
||||
if (context == null || callbacks.isEmpty()) return;
|
||||
|
||||
long duration = System.nanoTime() - context.startTime;
|
||||
|
||||
// 通知所有回调
|
||||
for (MethodMonitorCallback callback : callbacks) {
|
||||
try {
|
||||
callback.onMethodExit(
|
||||
context.className,
|
||||
context.methodName,
|
||||
returnValue,
|
||||
exception,
|
||||
duration
|
||||
);
|
||||
} catch (Throwable t) {
|
||||
logger.error("Callback execution error in onMethodExit", t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 上下文对象,用于在方法进入和退出之间传递数据
|
||||
static class MethodContext {
|
||||
long startTime;
|
||||
String className;
|
||||
String methodName;
|
||||
Object[] args;
|
||||
}
|
||||
}
|
||||
}
|
||||
18
src/main/java/org/tzd/debug/util/GlobalObjects.java
Normal file
18
src/main/java/org/tzd/debug/util/GlobalObjects.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package org.tzd.debug.util;
|
||||
|
||||
import com.axis.innovators.box.AxisInnovatorsBox;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
/**
|
||||
* 调试控制台的全局对象
|
||||
* @author tzdwindows 7
|
||||
*/
|
||||
public class GlobalObjects {
|
||||
/**
|
||||
* Instrumentation对象
|
||||
*/
|
||||
public Instrumentation instrumentation;
|
||||
|
||||
public AxisInnovatorsBox axisInnovatorsBox = AxisInnovatorsBox.getMain();
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
package org.tzd.lm;
|
||||
|
||||
import java.io.*;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
@@ -9,9 +15,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
/**
|
||||
* 使用AI接口获取回复
|
||||
* @author tzdwindows 7
|
||||
@@ -35,6 +38,7 @@ public class LMApi {
|
||||
public void setContent(String content) { this.content = content; }
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调用AI接口获取回复
|
||||
* @param messages 消息列表
|
||||
|
||||
Reference in New Issue
Block a user