feat(debug): 添加调试窗口和类监控功能

- 新增 DebugWindow 类用于显示调试信息
- 在主程序中添加调试窗口的创建和显示逻辑
- 新增 ClassDebug 类提供类监控和调试功能
- 更新 build.gradle 添加 byte-buddy 依赖
This commit is contained in:
tzdwindows 7
2025-06-27 09:09:55 +08:00
parent 4dfdec9055
commit 06fe2bedf4
50 changed files with 4638 additions and 65 deletions

View File

@@ -1,6 +1,5 @@
package com.axis.innovators.box;
import com.axis.innovators.box.decompilation.gui.ModernJarViewer;
import com.axis.innovators.box.events.GlobalEventBus;
import com.axis.innovators.box.events.OpenFileEvents;
import com.axis.innovators.box.events.StartupEvent;
@@ -8,10 +7,10 @@ import com.axis.innovators.box.gui.*;
import com.axis.innovators.box.plugins.PluginDescriptor;
import com.axis.innovators.box.plugins.PluginLoader;
import com.axis.innovators.box.plugins.PluginPyLoader;
import com.axis.innovators.box.register.LanguageManager;
import com.axis.innovators.box.register.RegistrationSettingsItem;
import com.axis.innovators.box.register.RegistrationTool;
import com.axis.innovators.box.register.RegistrationTopic;
import com.axis.innovators.box.register.LanguageManager;
import com.axis.innovators.box.tools.*;
import com.axis.innovators.box.util.PythonResult;
import com.axis.innovators.box.util.Tray;
@@ -27,19 +26,20 @@ import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.api.dog.agent.VirtualMachine;
import org.jetbrains.annotations.NotNull;
import org.tzd.lm.LM;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.lang.instrument.Instrumentation;
import java.lang.management.*;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -72,9 +72,20 @@ public class AxisInnovatorsBox {
private final List<WindowsJDialog> windowsJDialogList = new ArrayList<>();
private final StateManager stateManager = new StateManager();
private UserTags userTags;
private final boolean isDebug;
private static DebugWindow debugWindow;
public AxisInnovatorsBox(String[] args) {
public AxisInnovatorsBox(String[] args, boolean isDebug) {
this.args = args;
this.isDebug = isDebug;
}
/**
* 获取调试状态
* @return 调试状态
*/
public boolean isDebugEnvironment(){
return isDebug;
}
@@ -82,7 +93,7 @@ public class AxisInnovatorsBox {
try {
LibraryLoad.loadLibrary("FridaNative");
LibraryLoad.loadLibrary("ThrowSafely");
LibraryLoad.loadLibrary("DogAgent");
LibraryLoad.loadLibrary("DogAgent");
} catch (Exception e) {
logger.error("Failed to load the 'FridaNative' library", e);
}
@@ -325,21 +336,8 @@ public class AxisInnovatorsBox {
// 播放错误音效的方法
private void playErrorSound() {
try {
// 使用系统默认的错误音效
Toolkit.getDefaultToolkit().beep();
// 或者播放自定义音效
/*
File soundFile = new File("error_sound.wav");
if (soundFile.exists()) {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
}
*/
} catch (Exception ex) {
// 忽略音效播放错误
} catch (Exception ignored) {
}
}
@@ -357,10 +355,8 @@ public class AxisInnovatorsBox {
private void processAppender(ZipOutputStream zos, Set<String> addedFiles,
Appender appender) throws IOException {
if (appender instanceof FileAppender) {
FileAppender fileAppender = (FileAppender) appender;
if (appender instanceof FileAppender fileAppender) {
String fileName = fileAppender.getFileName();
if (fileName != null && !addedFiles.contains(fileName)) {
addFileToZip(zos, new File(fileName), "logs/");
addedFiles.add(fileName);
@@ -410,7 +406,9 @@ public class AxisInnovatorsBox {
filePattern = filePattern.substring(lastSlash + 1);
}
int patternStart = filePattern.indexOf('%');
if (patternStart == -1) patternStart = filePattern.indexOf('$');
if (patternStart == -1) {
patternStart = filePattern.indexOf('$');
}
if (patternStart > 0) {
baseName = filePattern.substring(0, patternStart);
@@ -531,7 +529,9 @@ public class AxisInnovatorsBox {
}
private String formatMemory(long bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(1024));
char unit = "KMGTPE".charAt(exp - 1);
return String.format("%.1f %sB", bytes / Math.pow(1024, exp), unit);
@@ -601,22 +601,10 @@ public class AxisInnovatorsBox {
return tempFile;
}
// 添加目录到ZIP递归
private void addDirectoryToZip(ZipOutputStream zos, File dir, String basePath) throws IOException {
if (!dir.exists() || !dir.isDirectory()) return;
for (File file : dir.listFiles()) {
String entryPath = basePath + "/" + file.getName();
if (file.isDirectory()) {
addDirectoryToZip(zos, file, entryPath);
} else {
addFileToZip(zos, file, entryPath);
}
}
}
private void addFileToZip(ZipOutputStream zos, File file, String entryPath) throws IOException {
if (!file.exists()) return;
if (!file.exists()) {
return;
}
String entryName = entryPath + file.getName();
ZipEntry zipEntry = new ZipEntry(entryName);
@@ -755,7 +743,7 @@ public class AxisInnovatorsBox {
* @param windowsJDialog 窗口
*/
public boolean isWindowStartup(WindowsJDialog windowsJDialog) {
return windowsJDialogList.contains(windowsJDialog);
return !windowsJDialogList.contains(windowsJDialog);
}
/**
@@ -790,8 +778,34 @@ public class AxisInnovatorsBox {
isWindow = true;
}
public static void run(String[] args) {
main = new AxisInnovatorsBox(args);
private void createDebugWindow() {
debugWindow = new DebugWindow();
// 关键配置:设置为独立窗口,不受模态对话框影响
//debugWindow.setAlwaysOnTop(true);
//debugWindow.setFocusableWindowState(true);
//debugWindow.setFocusable(true);
// 当主窗口关闭时关闭调试窗口
ex.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
debugWindow.dispose();
}
});
ex.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
debugWindow.setVisible(true);
}
});
debugWindow.setVisible(true);
}
public static void run(String[] args, boolean isDebug) {
main = new AxisInnovatorsBox(args,isDebug);
try {
main.initLog4j2();
main.setTopic();
@@ -828,7 +842,15 @@ public class AxisInnovatorsBox {
SwingUtilities.invokeLater(() -> {
try {
main.ex = new MainWindow();
main.ex = new MainWindow(){
@Override
public void setVisible(boolean b) {
if (debugWindow != null) {
debugWindow.setVisible(b);
}
super.setVisible(b);
}
};
GlobalEventBus.EVENT_BUS.post(new StartupEvent(main));
PluginPyLoader.getLoadedPlugins().forEach((pluginId, pyPluginDescriptor) -> {
if (pyPluginDescriptor.getLastResult().getInterpreter() != null){
@@ -843,8 +865,9 @@ public class AxisInnovatorsBox {
main.runWindow();
} catch (Exception e) {
logger.error("There was a problem starting the main thread", e);
if (main.ex != null)
if (main.ex != null) {
main.ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
main.organizingCrashReports(e);
throw new RuntimeException(e);
}
@@ -860,8 +883,9 @@ public class AxisInnovatorsBox {
} catch (Exception e) {
logger.error("Failed to load plugins", e);
if (main.ex != null)
if (main.ex != null) {
main.ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
main.organizingCrashReports(e);
throw new RuntimeException(e);
}
@@ -870,8 +894,9 @@ public class AxisInnovatorsBox {
main.thread.start();
} catch (Exception e) {
logger.error("In unexpected errors", e);
if (main.ex != null)
if (main.ex != null) {
main.ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
main.organizingCrashReports(e);
throw new RuntimeException(e);
}
@@ -894,6 +919,10 @@ public class AxisInnovatorsBox {
ex.initUI();
isWindow = true;
ex.setVisible(true);
if (isDebug) {
SwingUtilities.invokeLater(this::createDebugWindow);
}
}
/**

View File

@@ -1,12 +1,11 @@
package com.axis.innovators.box;
import com.axis.innovators.box.browser.MainApplication;
import com.axis.innovators.box.browser.WindowRegistry;
import com.axis.innovators.box.decompilation.gui.ModernJarViewer;
import com.axis.innovators.box.register.LanguageManager;
import com.axis.innovators.box.tools.ArgsParser;
import com.axis.innovators.box.tools.FolderCleaner;
import com.axis.innovators.box.tools.FolderCreator;
import com.axis.innovators.box.register.LanguageManager;
import javax.swing.*;
import java.io.File;
@@ -46,6 +45,20 @@ public class Main {
LanguageManager.loadLanguage("system:zh_CN");
}
// 检查是否包含调试控制台参数
boolean debugWindowEnabled = false;
for (int i = 0; i < args.length; i++) {
if ("-debugControlWindow-on".equals(args[i])) {
debugWindowEnabled = true;
// 移除此参数避免干扰后续处理
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 0, newArgs, 0, i);
System.arraycopy(args, i + 1, newArgs, i, args.length - i - 1);
args = newArgs;
break;
}
}
List<Map<String, String>> validFiles = ArgsParser.parseArgs(args);
for (Map<String, String> fileInfo : validFiles) {
String extension = fileInfo.get("extension");
@@ -66,12 +79,12 @@ public class Main {
if (".html".equals(extension)) {
MainApplication.popupHTMLWindow(path);
releaseLock(); // 释放锁(窗口模式)
releaseLock();
return;
}
}
AxisInnovatorsBox.run(args);
AxisInnovatorsBox.run(args, debugWindowEnabled);
}
/**

File diff suppressed because it is too large Load Diff

View File

@@ -1,30 +1,27 @@
package com.axis.innovators.box.gui;
import com.axis.innovators.box.AxisInnovatorsBox;
import com.axis.innovators.box.decompilation.gui.ModernJarViewer;
import com.axis.innovators.box.events.*;
import com.axis.innovators.box.register.RegistrationSettingsItem;
import com.axis.innovators.box.register.LanguageManager;
import com.axis.innovators.box.register.RegistrationSettingsItem;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLaf;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.PanelUI;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.*;
import java.util.List;
import java.util.*;
/**
* 显示窗口(显示的主窗口)
@@ -350,7 +347,7 @@ public class MainWindow extends JFrame {
private WindowsJDialog dialog;
public void showSettings() {
if (dialog == null || !AxisInnovatorsBox.getMain().isWindowStartup(dialog)) {
if (dialog == null || AxisInnovatorsBox.getMain().isWindowStartup(dialog)) {
dialog = new WindowsJDialog(this,
LanguageManager.getLoadedLanguages().getText("mainWindow.settings.title"), true);
}
@@ -373,7 +370,7 @@ public class MainWindow extends JFrame {
GlobalEventBus.EVENT_BUS.post(new SettingsLoadEvents(dialog, content));
dialog.add(content);
dialog.revalidate();
if (!AxisInnovatorsBox.getMain().isWindowStartup(dialog)) {
if (AxisInnovatorsBox.getMain().isWindowStartup(dialog)) {
AxisInnovatorsBox.getMain().popupWindow(dialog);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -2,8 +2,10 @@ package com.axis.innovators.box.tools;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.api.dog.agent.VirtualMachine;
import java.io.File;
import java.lang.management.ManagementFactory;
/**
* 在当前jar下创建文件夹
@@ -85,6 +87,15 @@ public class FolderCreator {
return folder;
}
public static void main(String[] args) throws Exception {
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
String pid = jvmName.split("@")[0];
System.load("C:\\Users\\Administrator\\MCreatorWorkspaces\\mineralluminescence\\DogAgent.dll");
VirtualMachine vm = VirtualMachine.getVirtualMachine(Float.parseFloat(pid), true);
//vm.getInstrumentation();
vm.getInstrumentation();
}
public static String getLibraryFolder() {
String folder = createFolder(LIBRARY_NAME);
if (folder == null) {

View 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);
}
}

View 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);
}

View 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;
}
}
}

View 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();
}

View File

@@ -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 消息列表