From 259ec8fe8943ff079d1177c7d13587ac808baed3 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Fri, 7 Jul 2023 17:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/net/educoder/debugger/Debugger.java | 26 ++-- .../educoder/debugger/DebuggerController.java | 109 ++++++++++---- .../java/net/educoder/model/DebugResult.java | 140 ++++++++++++++++++ 3 files changed, 241 insertions(+), 34 deletions(-) create mode 100644 src/main/java/net/educoder/model/DebugResult.java diff --git a/src/main/java/net/educoder/debugger/Debugger.java b/src/main/java/net/educoder/debugger/Debugger.java index 2ca84c7..d2cce01 100644 --- a/src/main/java/net/educoder/debugger/Debugger.java +++ b/src/main/java/net/educoder/debugger/Debugger.java @@ -37,8 +37,8 @@ public class Debugger { private StepRequest stepRequest; private Boolean isRunning; - public Debugger(String paramString1, String paramString2) { - debugSession = getDebugSession(paramString1, paramString2); + public Debugger(String mainClass, String classPath) { + debugSession = getDebugSession(mainClass, classPath); this.isRunning = Boolean.valueOf(true); debugSession.getEventHub().events().subscribe(paramDebugEvent -> { @@ -67,7 +67,7 @@ public class Debugger { debugSession.getEventHub().events().subscribe(paramDebugEvent -> { if (paramDebugEvent.event instanceof com.sun.jdi.event.VMDisconnectEvent) { try { -// debugSession.getEventHub().close(); + debugSession.getEventHub().close(); } catch (Exception exception) { } } @@ -140,18 +140,26 @@ public class Debugger { this.breakpoints.remove(str); } - public ArrayList> getBreakpoints() { - ArrayList> arrayList = new ArrayList(); + public Map> getBreakpoints() { + Map> listMap = new HashMap<>(); Set set = this.breakpoints.keySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { String str1 = iterator.next(); String[] arrayOfString = str1.split(":"); - String str2 = arrayOfString[0]; - String str3 = arrayOfString[1]; - arrayList.add(new ArrayList(Arrays.asList((Object[]) new String[]{str2, str3}))); + String className = arrayOfString[0]; + Integer lineNo = Integer.valueOf(arrayOfString[1]); + + if (listMap.containsKey(className)) { + List lineNoList = listMap.get(className); + lineNoList.add(lineNo); + }else{ + List lineNoList = new ArrayList<>(); + lineNoList.add(lineNo); + listMap.put(className, lineNoList); + } } - return arrayList; + return listMap; } public void runContinue() throws Exception { diff --git a/src/main/java/net/educoder/debugger/DebuggerController.java b/src/main/java/net/educoder/debugger/DebuggerController.java index 5a85f0b..471dcbe 100644 --- a/src/main/java/net/educoder/debugger/DebuggerController.java +++ b/src/main/java/net/educoder/debugger/DebuggerController.java @@ -1,11 +1,14 @@ package net.educoder.debugger; import com.google.gson.Gson; -import com.microsoft.java.debug.core.DebugUtility; +import net.educoder.model.DebugResult; +import org.apache.commons.io.FileUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.io.File; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -19,11 +22,16 @@ import java.util.Map; @RestController public class DebuggerController { - public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src:/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src/precompiled.jar"; - public static final String MAIN_CLASS = "__Driver__"; - private static final Debugger debugger = new Debugger(MAIN_CLASS, CLASS_PATHS); + // public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src:/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src/precompiled.jar"; +// public static final String MAIN_CLASS = "__Driver__"; + public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java"; + public static final String MAIN_CLASS = "Debug"; + // private static Debugger debugger = new Debugger(MAIN_CLASS, CLASS_PATHS); + private static Debugger debugger = null; private static final Gson gson = new Gson(); + private String outputPath; + @GetMapping("/") public String home() { @@ -32,17 +40,32 @@ public class DebuggerController { /** * 启动程序 + * * @return * @throws Exception */ @GetMapping("/start-program") - public String startProgram() throws Exception { + public String startProgram(@RequestParam String classPath, + @RequestParam String mainClass, + @RequestParam String breakPoints, + @RequestParam("fileInputPath") String fileInputPath, + @RequestParam("fileOutPath") String fileOutPath) throws Exception { + + initDebuggerInstance(mainClass, classPath); + outputPath = fileOutPath; + debugger.setInputFile(fileInputPath); + debugger.setOutputFile(fileOutPath); + for (String breakPoint : breakPoints.split(",")) { + String[] segmentation = breakPoint.split(":"); + debugger.setBreakPoint(segmentation[0], Integer.valueOf(segmentation[1])); + } debugger.startProgram(); - return "OK"; + return getCurrentState(); } /** * 设置输入 + * * @param filePath * @return */ @@ -54,19 +77,22 @@ public class DebuggerController { /** * 设置输出 + * * @param filePath * @return */ @GetMapping("/set-output-file") public String setOutputFile(@RequestParam("filepath") String filePath) { debugger.setOutputFile(filePath); + outputPath = filePath; return "OK"; } /** * 设置断点 - * @param className - * @param lineNo + * + * @param className Debug.java -> Debug + * @param lineNo 10 * @return * @throws Exception */ @@ -74,11 +100,12 @@ public class DebuggerController { public String setBreakpoint(@RequestParam("className") String className, @RequestParam("lineNo") Integer lineNo) throws Exception { debugger.setBreakPoint(className, lineNo); - return "OK"; + return getCurrentState(); } /** * 移除断点 + * * @param className * @param lineNo * @return @@ -88,21 +115,22 @@ public class DebuggerController { public String removeBreakpoint(@RequestParam("className") String className, @RequestParam("lineNo") Integer lineNo) throws Exception { debugger.removeBreakPoint(className, lineNo); - return "OK"; + return getCurrentState(); } /** * 获取断点 + * * @return */ @GetMapping("/get-breakpoints") public String getBreakpoints() { - ArrayList> breakpoints = debugger.getBreakpoints(); - return gson.toJson(breakpoints); + return gson.toJson(debugger.getBreakpoints()); } /** * 跳过断点(下一个) + * * @return * @throws Exception */ @@ -110,11 +138,12 @@ public class DebuggerController { public String runContinue() throws Exception { debugger.waitForVMPause(); debugger.runContinue(); - return "OK"; + return getCurrentState(); } /** * 单步调试 + * * @return * @throws Exception */ @@ -122,11 +151,12 @@ public class DebuggerController { public String runStepInto() throws Exception { debugger.waitForVMPause(); debugger.runStepInto(); - return "OK"; + return getCurrentState(); } /** * 单步跳出 + * * @return * @throws Exception */ @@ -134,11 +164,12 @@ public class DebuggerController { public String runStepOut() throws Exception { debugger.waitForVMPause(); debugger.runStepOut(); - return "OK"; + return getCurrentState(); } /** * 单步跳过(下一步) + * * @return * @throws Exception */ @@ -146,12 +177,13 @@ public class DebuggerController { public String runStepOver() throws Exception { debugger.waitForVMPause(); debugger.runStepOver(); - return "OK"; + return getCurrentState(); } /** * 获取当前状态 + * * @return * @throws Exception */ @@ -159,18 +191,45 @@ public class DebuggerController { public String getCurrentState() throws Exception { debugger.waitForVMPause(); - Map map = new HashMap<>(2); + DebugResult result = new DebugResult(); if (!debugger.isRunning()) { - return gson.toJson(map); + result.setExpressions(new HashMap<>()); + result.setLocals(new HashMap<>()); + result.setOut(FileUtils.readFileToString(new File(outputPath), Charset.defaultCharset())); + return gson.toJson(result); } - ArrayList localVariables = debugger.getLocalVariables(); - String locationFilename = debugger.getLocationFilename(); - Integer locationLineNumber = debugger.getLocationLineNumber(); +// result.setExpressions(); + result.setFilename(debugger.getLocationFilename()); + result.setCurrent_line(debugger.getLocationLineNumber()); - map.put("location", locationFilename + ":" + locationLineNumber); - map.put("variables", localVariables); - map.put("status", debugger.getStatus()); - return gson.toJson(map); + ArrayList localVariables = debugger.getLocalVariables(); + if (localVariables != null) { + Map locals = new HashMap<>(); + for (Variable localVariable : localVariables) { + locals.put(localVariable.name, localVariable.value); + } + result.setLocals(locals); + } + result.setBreakpoints(debugger.getBreakpoints()); + result.setOut(FileUtils.readFileToString(new File(outputPath), Charset.defaultCharset())); + + return gson.toJson(result); + } + + /** + * 初始化 + * + * @param mainClass 执行类 + * @param classPath class path 路径 + */ + private static void initDebuggerInstance(String mainClass, String classPath) { + if (debugger == null) { + synchronized (DebuggerController.class) { + if (debugger == null) { + debugger = new Debugger(mainClass, classPath); + } + } + } } } diff --git a/src/main/java/net/educoder/model/DebugResult.java b/src/main/java/net/educoder/model/DebugResult.java new file mode 100644 index 0000000..44af7bf --- /dev/null +++ b/src/main/java/net/educoder/model/DebugResult.java @@ -0,0 +1,140 @@ +package net.educoder.model; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.List; +import java.util.Map; + +/** + * @Author: youys + * @Date: 2023/7/7 + * @Description: + */ +public class DebugResult { + + /** + * 当前行 + */ + private int current_line = -1; + /** + * 当前文件 + */ + private String filename = ""; + + + /** + * 调试唯一标识 + */ + @JsonInclude(JsonInclude.Include.NON_NULL) + private String unique; + /** + * 断点信息 + */ + private Map> breakpoints; + + /** + * 表达式 + */ + private Map expressions; + + /** + * 本地变量 + */ + private Map locals; + + /** + * 输出 + */ + private String out; + + /** + * 是否结束 true 已结束 false 调试中 + */ + private boolean is_over; + + /** + * 是否需要跳转文件 true 跳转 false 不跳转 + */ + private boolean is_jump; + + + public int getCurrent_line() { + return current_line; + } + + public void setCurrent_line(int current_line) { + this.current_line = current_line; + } + + public String getFilename() { + return filename; + } + + public void setFilename(String filename) { + this.filename = filename; + } + + public boolean isIs_over() { + return is_over; + } + + public void setIs_over(boolean is_over) { + this.is_over = is_over; + } + + public String getUnique() { + return unique; + } + + public void setUnique(String unique) { + this.unique = unique; + } + + public Map> getBreakpoints() { + return breakpoints; + } + + public void setBreakpoints(Map> breakpoints) { + this.breakpoints = breakpoints; + } + + public Map getExpressions() { + return expressions; + } + + public void setExpressions(Map expressions) { + unicodeToCn(expressions); + this.expressions = expressions; + } + + public Map getLocals() { + return locals; + } + + public void setLocals(Map locals) { + unicodeToCn(locals); + this.locals = locals; + } + + public String getOut() { + return out; + } + + public void setOut(String out) { + this.out = out; + } + + public boolean isIs_jump() { + return is_jump; + } + + public void setIs_jump(boolean is_jump) { + this.is_jump = is_jump; + } + + private void unicodeToCn(Map map){ + for (Map.Entry entry : map.entrySet()) { +// map.put(entry.getKey(), UnicodeUtil.toString(entry.getValue())); + } + } +}