示例#1
0
 private EventRequest createMethodRequest(JDXDebugTarget target, Object classFilter, boolean entry)
     throws CoreException {
   EventRequest request = null;
   EventRequestManager manager = target.getEventRequestManager();
   if (manager == null) {
     target.requestFailed(
         JDXMessages.JDXMethodBreakpoint_Unable_to_create_breakpoint_request___VM_disconnected__1,
         null); //$NON-NLS-1$
   }
   try {
     if (entry) {
       if (classFilter instanceof ClassType
           && getMethodName() != null
           && getMethodSignature() != null) {
         ClassType clazz = (ClassType) classFilter;
         if (clazz.name().equals(getTypeName())) {
           Method method = clazz.concreteMethodByName(getMethodName(), getMethodSignature());
           if (method != null && !method.isNative()) {
             Location location = method.location();
             if (location != null && location.codeIndex() != -1) {
               request = manager.createBreakpointRequest(location);
             }
           }
         }
       }
       if (request == null) {
         request = manager.createMethodEntryRequest();
         if (classFilter instanceof String) {
           ((MethodEntryRequest) request).addClassFilter((String) classFilter);
         } else if (classFilter instanceof ReferenceType) {
           ((MethodEntryRequest) request).addClassFilter((ReferenceType) classFilter);
         }
       }
     } else {
       request = manager.createMethodExitRequest();
       if (classFilter instanceof String) {
         ((MethodExitRequest) request).addClassFilter((String) classFilter);
       } else if (classFilter instanceof ReferenceType) {
         ((MethodExitRequest) request).addClassFilter((ReferenceType) classFilter);
       }
     }
     configureRequest(request, target);
   } catch (VMDisconnectedException e) {
     if (!target.isAvailable()) {
       return null;
     }
     Plugin.log(e);
   } catch (RuntimeException e) {
     target.internalError(e);
   }
   return request;
 }
示例#2
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;
  }
 // TODO this is not sufficient only does method names not line locations
 public Location resolveLocation(String location) {
   DalvikUtils.LOGGER.warn("line locations not yet implemented!");
   location = location.trim();
   Location loc = null;
   int endIdx = location.lastIndexOf(".");
   if (endIdx != -1) {
     String className = location.substring(0, endIdx);
     ReferenceType cr = this.findClassType(className);
     if (cr != null) {
       for (Method m : cr.allMethods()) {
         // TODO need to think on this comparison ...
         if (m.toString().contains(location)) {
           loc = m.location();
           break;
         }
       }
     }
   }
   return loc;
 }
示例#4
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;
  }