コード例 #1
0
 public BreakpointRequest createBreakpointRequest(Location loc) {
   BreakpointRequest bpr = this.eventRequestManager.createBreakpointRequest(loc);
   // this could be SUSPEND_EVENT_THREAD
   bpr.setSuspendPolicy(EventRequest.SUSPEND_ALL);
   bpr.enable();
   return bpr;
 }
コード例 #2
0
ファイル: JavaDebugger.java プロジェクト: yanxch/che
 @Override
 public List<Breakpoint> getAllBreakpoints() throws DebuggerException {
   List<BreakpointRequest> breakpointRequests;
   try {
     breakpointRequests = getEventManager().breakpointRequests();
   } catch (DebuggerException e) {
     Throwable cause = e.getCause();
     if (cause instanceof VMCannotBeModifiedException) {
       // If target VM in read-only state then list of break point always empty.
       return Collections.emptyList();
     }
     throw e;
   }
   List<Breakpoint> breakPoints = new ArrayList<>(breakpointRequests.size());
   for (BreakpointRequest breakpointRequest : breakpointRequests) {
     com.sun.jdi.Location location = breakpointRequest.location();
     // Breakpoint always enabled at the moment. Managing states of breakpoint is not supported for
     // now.
     breakPoints.add(
         newDto(BreakpointDto.class)
             .withEnabled(true)
             .withLocation(
                 newDto(LocationDto.class)
                     .withTarget(location.declaringType().name())
                     .withLineNumber(location.lineNumber())));
   }
   Collections.sort(breakPoints, BREAKPOINT_COMPARATOR);
   return breakPoints;
 }
コード例 #3
0
ファイル: JavaDebugger.java プロジェクト: yanxch/che
  @Override
  public void addBreakpoint(Breakpoint breakpoint) throws DebuggerException {
    final String className = breakpoint.getLocation().getTarget();
    final int lineNumber = breakpoint.getLocation().getLineNumber();
    List<ReferenceType> classes = vm.classesByName(className);
    // it may mean that class doesn't loaded by a target JVM yet
    if (classes.isEmpty()) {
      deferBreakpoint(breakpoint);
      throw new DebuggerException("Class not loaded");
    }

    ReferenceType clazz = classes.get(0);
    List<com.sun.jdi.Location> locations;
    try {
      locations = clazz.locationsOfLine(lineNumber);
    } catch (AbsentInformationException | ClassNotPreparedException e) {
      throw new DebuggerException(e.getMessage(), e);
    }

    if (locations.isEmpty()) {
      throw new DebuggerException("Line " + lineNumber + " not found in class " + className);
    }

    com.sun.jdi.Location location = locations.get(0);
    if (location.method() == null) {
      // Line is out of method.
      throw new DebuggerException("Invalid line " + lineNumber + " in class " + className);
    }

    // Ignore new breakpoint if already have breakpoint at the same location.
    EventRequestManager requestManager = getEventManager();
    for (BreakpointRequest breakpointRequest : requestManager.breakpointRequests()) {
      if (location.equals(breakpointRequest.location())) {
        LOG.debug("Breakpoint at {} already set", location);
        return;
      }
    }

    try {
      EventRequest breakPointRequest = requestManager.createBreakpointRequest(location);
      breakPointRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
      String expression = breakpoint.getCondition();
      if (!(expression == null || expression.isEmpty())) {
        ExpressionParser parser = ExpressionParser.newInstance(expression);
        breakPointRequest.putProperty(
            "org.eclipse.che.ide.java.debug.condition.expression.parser", parser);
      }
      breakPointRequest.setEnabled(true);
    } catch (NativeMethodException | IllegalThreadStateException | InvalidRequestStateException e) {
      throw new DebuggerException(e.getMessage(), e);
    }

    debuggerCallback.onEvent(new BreakpointActivatedEventImpl(breakpoint));
    LOG.debug("Add breakpoint: {}", location);
  }
コード例 #4
0
ファイル: JavaDebugger.java プロジェクト: yanxch/che
 @Override
 public void deleteBreakpoint(Location location) throws DebuggerException {
   final String className = location.getTarget();
   final int lineNumber = location.getLineNumber();
   EventRequestManager requestManager = getEventManager();
   List<BreakpointRequest> snapshot = new ArrayList<>(requestManager.breakpointRequests());
   for (BreakpointRequest breakpointRequest : snapshot) {
     com.sun.jdi.Location jdiLocation = breakpointRequest.location();
     if (jdiLocation.declaringType().name().equals(className)
         && jdiLocation.lineNumber() == lineNumber) {
       requestManager.deleteEventRequest(breakpointRequest);
       LOG.debug("Delete breakpoint: {}", location);
     }
   }
 }
コード例 #5
0
  public BreakpointRequest setBreakpoint(
      String clsName, String methodName, String methodSignature) {
    ReferenceType rt = findReferenceType(clsName);
    if (rt == null) {
      rt = resumeToPrepareOf(clsName).referenceType();
    }

    Method method = findMethod(rt, methodName, methodSignature);
    if (method == null) {
      throw new IllegalArgumentException("Bad method name/signature");
    }
    BreakpointRequest bpr = eventRequestManager().createBreakpointRequest(method.location());
    bpr.setSuspendPolicy(EventRequest.SUSPEND_ALL);
    bpr.enable();
    return bpr;
  }
コード例 #6
0
 @Override
 protected void setRequestThreadFilter(EventRequest request, ThreadReference thread) {
   if (request instanceof MethodEntryRequest) {
     ((MethodEntryRequest) request).addThreadFilter(thread);
   } else if (request instanceof MethodExitRequest) {
     ((MethodExitRequest) request).addThreadFilter(thread);
   } else if (request instanceof BreakpointRequest) {
     ((BreakpointRequest) request).addThreadFilter(thread);
   }
 }
コード例 #7
0
  protected ThreadReference getDebuggedThread(DebugEvaluationTest test) {
    try {
      // desintall previous breakpoints
      this.jdiVM.eventRequestManager().deleteAllBreakpoints();

      // install a breakpoint at the breakpointLine
      List classes = this.jdiVM.classesByName(this.breakpointClassName);
      if (classes.size() == 0) {
        if (this.breakpointClassName.equals("_JDIStackFrame_")) {
          // install special class
          String source =
              "public class _JDIStackFrame_ {\n"
                  + "  public int foo() {\n"
                  + "    return -1;\n"
                  + "  }\n"
                  + "}";
          test.compileAndDeploy(source, "_JDIStackFrame_");
        }

        // force load of class
        test.evaluateWithExpectedDisplayString(
            ("return Class.forName(\"" + this.breakpointClassName + "\");").toCharArray(),
            ("class " + this.breakpointClassName).toCharArray());
        classes = this.jdiVM.classesByName(this.breakpointClassName);
        if (classes.size() == 0) {
          // workaround bug in Standard VM
          Iterator iterator = this.jdiVM.allClasses().iterator();
          while (iterator.hasNext()) {
            ReferenceType type = (ReferenceType) iterator.next();
            if (type.name().equals(this.breakpointClassName)) {
              classes = new ArrayList(1);
              classes.add(type);
              break;
            }
          }
          if (classes.size() == 0) {
            throw new Error("JDI could not retrieve class for " + this.breakpointClassName);
          }
        }
      }
      ClassType clazz = (ClassType) classes.get(0);
      Method method = (Method) clazz.methodsByName(this.breakpointMethodName).get(0);
      Location location;
      if (this.breakpointLine < 0 || this.breakpointLine == Integer.MAX_VALUE) {
        location = method.location();
      } else {
        location = (Location) method.locationsOfLine(this.breakpointLine).get(0);
      }
      BreakpointRequest request =
          this.jdiVM.eventRequestManager().createBreakpointRequest(location);
      request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
      request.enable();

      // create a new thread that hit the breakpoint
      EvaluationTest.Requestor requestor = test.new Requestor();
      try {
        test.resetEnv();
        test.context.evaluate(
            ("(new Thread() {\n"
                    + "  public void run() {\n"
                    + userCode
                    + "\n"
                    + (this.breakpointClassName.equals("_JDIStackFrame_")
                        ? "    new _JDIStackFrame_().foo();\n"
                        : "")
                    + "  }\n"
                    + "  public String toString() {\n"
                    + "    return \"my thread\";\n"
                    + "  }\n"
                    + "}).start();\n")
                .toCharArray(),
            test.getEnv(),
            test.getCompilerOptions(),
            requestor,
            test.getProblemFactory());
      } catch (InstallException e) {
        Assert.assertTrue("Target exception " + e.getMessage(), false);
      }
      EvaluationResult[] results = requestor.results;
      for (int i = 0; i < requestor.resultIndex + 1; i++) {
        if (results[i].hasErrors()) {
          Assert.assertTrue("Compilation error in user code", false);
        }
      }

      // Wait for the breakpoint event
      Event event = null;
      do {
        EventSet set = this.jdiVM.eventQueue().remove();
        Iterator iterator = set.eventIterator();
        while (iterator.hasNext()) {
          event = (Event) iterator.next();
          if (event instanceof BreakpointEvent) break;
        }
      } while (!(event instanceof BreakpointEvent));

      // Return the suspended thread
      return ((BreakpointEvent) event).thread();
    } catch (AbsentInformationException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return null;
  }
コード例 #8
0
 public void deleteAllBreakpoints() {
   for (BreakpointRequest req : this.eventRequestManager.breakpointRequests()) {
     req.disable();
   }
 }