接口调整

This commit is contained in:
youys 2023-07-07 17:35:05 +08:00
parent 9cb11cbb57
commit 259ec8fe89
3 changed files with 241 additions and 34 deletions

View File

@ -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<ArrayList<String>> getBreakpoints() {
ArrayList<ArrayList<String>> arrayList = new ArrayList();
public Map<String,List<Integer>> getBreakpoints() {
Map<String,List<Integer>> listMap = new HashMap<>();
Set<String> set = this.breakpoints.keySet();
Iterator<String> 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<Integer> lineNoList = listMap.get(className);
lineNoList.add(lineNo);
}else{
List<Integer> lineNoList = new ArrayList<>();
lineNoList.add(lineNo);
listMap.put(className, lineNoList);
}
}
return arrayList;
return listMap;
}
public void runContinue() throws Exception {

View File

@ -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<ArrayList<String>> 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<String, Object> 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<Variable> 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<Variable> localVariables = debugger.getLocalVariables();
if (localVariables != null) {
Map<String, String> 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);
}
}
}
}
}

View File

@ -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<String, List<Integer>> breakpoints;
/**
* 表达式
*/
private Map<String, String> expressions;
/**
* 本地变量
*/
private Map<String, String> 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<String, List<Integer>> getBreakpoints() {
return breakpoints;
}
public void setBreakpoints(Map<String, List<Integer>> breakpoints) {
this.breakpoints = breakpoints;
}
public Map<String, String> getExpressions() {
return expressions;
}
public void setExpressions(Map<String, String> expressions) {
unicodeToCn(expressions);
this.expressions = expressions;
}
public Map<String, String> getLocals() {
return locals;
}
public void setLocals(Map<String, String> 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<String,String> map){
for (Map.Entry<String, String> entry : map.entrySet()) {
// map.put(entry.getKey(), UnicodeUtil.toString(entry.getValue()));
}
}
}