From dcdab4314652b04a259ee4a34f16bf8ebc9c0aea Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Thu, 24 Aug 2023 10:09:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(Debugger)=20list,map,=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=E5=8F=98=E9=87=8F=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 13 +++++++++ .../java/net/educoder/debugger/Debugger.java | 29 ++++++++++++++++--- .../educoder/debugger/DebuggerController.java | 7 +++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pom.xml b/pom.xml index c94c866..cbd3ce9 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,19 @@ + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + + --add-exports + jdk.jdi/com.sun.tools.jdi=ALL-UNNAMED + + + diff --git a/src/main/java/net/educoder/debugger/Debugger.java b/src/main/java/net/educoder/debugger/Debugger.java index d2cce01..5a1e885 100644 --- a/src/main/java/net/educoder/debugger/Debugger.java +++ b/src/main/java/net/educoder/debugger/Debugger.java @@ -10,6 +10,7 @@ import com.sun.jdi.event.BreakpointEvent; import com.sun.jdi.event.Event; import com.sun.jdi.event.StepEvent; import com.sun.jdi.request.StepRequest; +import com.sun.tools.jdi.ClassObjectReferenceImpl; import java.io.IOException; import java.io.OutputStream; @@ -36,8 +37,10 @@ public class Debugger { private String outputFilePath = ""; private StepRequest stepRequest; private Boolean isRunning; + private String mainClass; public Debugger(String mainClass, String classPath) { + this.mainClass = mainClass; debugSession = getDebugSession(mainClass, classPath); this.isRunning = Boolean.valueOf(true); @@ -140,8 +143,8 @@ public class Debugger { this.breakpoints.remove(str); } - public Map> getBreakpoints() { - Map> listMap = new HashMap<>(); + public Map> getBreakpoints() { + Map> listMap = new HashMap<>(); Set set = this.breakpoints.keySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { @@ -153,7 +156,7 @@ public class Debugger { if (listMap.containsKey(className)) { List lineNoList = listMap.get(className); lineNoList.add(lineNo); - }else{ + } else { List lineNoList = new ArrayList<>(); lineNoList.add(lineNo); listMap.put(className, lineNoList); @@ -226,6 +229,22 @@ public class Debugger { VirtualMachine virtualMachine = debugSession.getVM(); List list = virtualMachine.classesByName(SERIALIZER_CLASS); + if (list.size() == 0) { + ClassLoaderReference reference = virtualMachine.classesByName(mainClass).get(0).classLoader(); + Method method = virtualMachine.classesByName("jdk.internal.loader.ClassLoaders$AppClassLoader").get(0).methodsByName("loadClass").get(1); + List arguments = new ArrayList<>(); + arguments.add(virtualMachine.mirrorOf(SERIALIZER_CLASS)); + + ThreadReference threadReference = getThreadFromLastEvent(); + Value value = reference.invokeMethod(threadReference, method, arguments, 1); + + ReferenceType referenceType = ((ClassObjectReferenceImpl) value).referenceType(); + ClassType classType = (ClassType) referenceType; + method = referenceType.methodsByName("forName").get(0); + classType.invokeMethod(threadReference, method, arguments, 1); + } + + list = virtualMachine.classesByName(SERIALIZER_CLASS); ClassType classType = (ClassType) list.get(0); List list1 = classType.methodsByName(paramString); @@ -246,6 +265,7 @@ public class Debugger { List list = Arrays.asList(new Value[]{paramValue}); return callSerializerMethod(SERIALIZE_DYNAMIC_RESOLVE, list); } catch (Exception exception) { + exception.printStackTrace(); return null; } } @@ -255,6 +275,7 @@ public class Debugger { List list = Arrays.asList(new Value[]{paramValue}); return callSerializerMethod(OBJECT_TO_CLASS, list); } catch (Exception exception) { + exception.printStackTrace(); try { return paramValue.toString(); @@ -363,7 +384,7 @@ public class Debugger { ThreadReference threadReference = getThreadFromLastEvent(); if (threadReference.frameCount() > 0) { return "PAUSED"; - }else{ + } else { return "RUNNING"; } } catch (Exception exception) { diff --git a/src/main/java/net/educoder/debugger/DebuggerController.java b/src/main/java/net/educoder/debugger/DebuggerController.java index ef8d547..478467c 100644 --- a/src/main/java/net/educoder/debugger/DebuggerController.java +++ b/src/main/java/net/educoder/debugger/DebuggerController.java @@ -32,6 +32,7 @@ public class DebuggerController { private static final Gson gson = new Gson(); private String outputPath; + private static final String workspace = "/data/workspace"; private final Logger logger = LoggerFactory.getLogger(DebuggerController.class); @@ -256,12 +257,12 @@ public class DebuggerController { * @param originalEvaluateFile 原始评测文件 */ private void compileCode(String originalEvaluateFile) { - String compilePath = "/data/workspace" + File.separator + originalEvaluateFile.substring(0, originalEvaluateFile.lastIndexOf("/") + 1); - String compileCommand = StringUtils.join("javac -g -cp ", compilePath, " ", compilePath+"*.java"); + String compilePath = workspace + File.separator + originalEvaluateFile.substring(0, originalEvaluateFile.lastIndexOf("/") + 1); + String compileCommand = StringUtils.join("javac -g -cp ", compilePath,":", workspace,"/precompiled.jar:", workspace,"/minimal-json.jar ", " ", compilePath+"*.java"); ShellResult shellResult = ShellUtil.executeAndGetExitStatus(compileCommand); logger.info("编译代码command:{}, status:{}, out:{}", compileCommand, shellResult.getExitStatus(), shellResult.getOut()); if(shellResult.getExitStatus() != 0){ - throw new RuntimeException(shellResult.getOut().replaceAll("/data/workspace/", "")); + throw new RuntimeException(shellResult.getOut().replaceAll(workspace + "/", "")); } } }