Exemplo n.º 1
0
 protected static String calcLabel(ValueDescriptor descriptor) {
   final ValueDescriptorImpl valueDescriptor = (ValueDescriptorImpl) descriptor;
   final Value value = valueDescriptor.getValue();
   if (value instanceof ObjectReference) {
     if (value instanceof StringReference) {
       return ((StringReference) value).value();
     } else if (value instanceof ClassObjectReference) {
       ReferenceType type = ((ClassObjectReference) value).reflectedType();
       return (type != null) ? type.name() : "{...}";
     } else {
       final ObjectReference objRef = (ObjectReference) value;
       final Type type = objRef.type();
       if (type instanceof ClassType && ((ClassType) type).isEnum()) {
         final String name = getEnumConstantName(objRef, (ClassType) type);
         if (name != null) {
           return name;
         } else {
           return type.name();
         }
       } else {
         return "";
       }
     }
   } else if (value == null) {
     //noinspection HardCodedStringLiteral
     return "null";
   } else {
     return DebuggerBundle.message("label.undefined");
   }
 }
Exemplo n.º 2
0
  public void exceptionEvent(ExceptionEvent event) {
    ObjectReference or = event.exception();
    ReferenceType rt = or.referenceType();
    String exceptionName = rt.name();
    // Field messageField = Throwable.class.getField("detailMessage");
    Field messageField = rt.fieldByName("detailMessage");
    //    System.out.println("field " + messageField);
    Value messageValue = or.getValue(messageField);
    //    System.out.println("mess val " + messageValue);

    // "java.lang.ArrayIndexOutOfBoundsException"
    int last = exceptionName.lastIndexOf('.');
    String message = exceptionName.substring(last + 1);
    if (messageValue != null) {
      String messageStr = messageValue.toString();
      if (messageStr.startsWith("\"")) {
        messageStr = messageStr.substring(1, messageStr.length() - 1);
      }
      message += ": " + messageStr;
    }
    //    System.out.println("mess type " + messageValue.type());
    // StringReference messageReference = (StringReference) messageValue.type();

    // First just report the exception and its placement
    reportException(message, or, event.thread());
    // Then try to pretty it up with a better message
    handleCommonErrors(exceptionName, message, listener);

    if (editor != null) {
      editor.deactivateRun();
    }
  }
Exemplo n.º 3
0
 private void commandMethods(StringTokenizer t) throws NoSessionException {
   if (!t.hasMoreTokens()) {
     env.error("No class specified.");
     return;
   }
   String idClass = t.nextToken();
   ReferenceType cls = findClass(idClass);
   if (cls != null) {
     List<Method> methods = cls.allMethods();
     OutputSink out = env.getOutputSink();
     for (int i = 0; i < methods.size(); i++) {
       Method method = methods.get(i);
       out.print(method.declaringType().name() + " " + method.name() + "(");
       Iterator<String> it = method.argumentTypeNames().iterator();
       if (it.hasNext()) {
         while (true) {
           out.print(it.next());
           if (!it.hasNext()) {
             break;
           }
           out.print(", ");
         }
       }
       out.println(")");
     }
     out.show();
   } else {
     // ### Should validate class name syntax.
     env.failure("\"" + idClass + "\" is not a valid id or class name.");
   }
 }
Exemplo n.º 4
0
 static ReferenceType getReferenceTypeFromToken(String idToken) {
   ReferenceType cls = null;
   if (Character.isDigit(idToken.charAt(0))) {
     cls = null;
   } else if (idToken.startsWith("*.")) {
     // This notation saves typing by letting the user omit leading
     // package names. The first
     // loaded class whose name matches this limited regular
     // expression is selected.
     idToken = idToken.substring(1);
     for (ReferenceType type : Env.vm().allClasses()) {
       if (type.name().endsWith(idToken)) {
         cls = type;
         break;
       }
     }
   } else {
     // It's a class name
     List<ReferenceType> classes = Env.vm().classesByName(idToken);
     if (classes.size() > 0) {
       // TO DO: handle multiples
       cls = classes.get(0);
     }
   }
   return cls;
 }
 /** The 'refType' is known to match, return the EventRequest. */
 EventRequest resolveEventRequest(ReferenceType refType) throws NoSuchFieldException {
   Field field = refType.fieldByName(fieldId);
   EventRequestManager em = refType.virtualMachine().eventRequestManager();
   EventRequest wp = em.createAccessWatchpointRequest(field);
   wp.setSuspendPolicy(suspendPolicy);
   wp.enable();
   return wp;
 }
Exemplo n.º 6
0
 private void commandClasses() throws NoSessionException {
   OutputSink out = env.getOutputSink();
   // out.println("** classes list **");
   for (ReferenceType refType : runtime.allClasses()) {
     out.println(refType.name());
   }
   out.show();
 }
 /** Does the specified ReferenceType match this spec. */
 @Override
 public boolean matches(ReferenceType refType) {
   if (isWild) {
     return refType.name().endsWith(classId);
   } else {
     return refType.name().equals(classId);
   }
 }
Exemplo n.º 8
0
 public static String getLocationMethodQName(@NotNull Location location) {
   StringBuilder res = new StringBuilder();
   ReferenceType type = location.declaringType();
   if (type != null) {
     res.append(type.name()).append('.');
   }
   res.append(location.method().name());
   return res.toString();
 }
Exemplo n.º 9
0
 /** Return a description of an object. */
 static String description(ObjectReference ref) {
   ReferenceType clazz = ref.referenceType();
   long id = ref.uniqueID();
   if (clazz == null) {
     return toHex(id);
   } else {
     return MessageOutput.format(
         "object description and hex id", new Object[] {clazz.name(), toHex(id)});
   }
 }
Exemplo n.º 10
0
  @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);
  }
Exemplo n.º 11
0
  /**
   * Associates breakpoint with class. Create requests for loaded class and registers callback for
   * loading classes
   *
   * @param debugProcess the requesting process
   */
  protected void createOrWaitPrepare(DebugProcessImpl debugProcess, String classToBeLoaded) {
    debugProcess.getRequestsManager().callbackOnPrepareClasses(this, classToBeLoaded);

    List list = debugProcess.getVirtualMachineProxy().classesByName(classToBeLoaded);
    for (final Object aList : list) {
      ReferenceType refType = (ReferenceType) aList;
      if (refType.isPrepared()) {
        processClassPrepare(debugProcess, refType);
      }
    }
  }
Exemplo n.º 12
0
  protected void createOrWaitPrepare(
      final DebugProcessImpl debugProcess, final SourcePosition classPosition) {
    debugProcess.getRequestsManager().callbackOnPrepareClasses(this, classPosition);

    List list = debugProcess.getPositionManager().getAllClasses(classPosition);
    for (final Object aList : list) {
      ReferenceType refType = (ReferenceType) aList;
      if (refType.isPrepared()) {
        processClassPrepare(debugProcess, refType);
      }
    }
  }
Exemplo n.º 13
0
 /** ******** test assist ********* */
 Method getMethod(String className, String methodName) {
   List refs = vm().classesByName(className);
   if (refs.size() != 1) {
     failure("Test failure: " + refs.size() + " ReferenceTypes named: " + className);
     return null;
   }
   ReferenceType refType = (ReferenceType) refs.get(0);
   List meths = refType.methodsByName(methodName);
   if (meths.size() != 1) {
     failure("Test failure: " + meths.size() + " methods named: " + methodName);
     return null;
   }
   return (Method) meths.get(0);
 }
Exemplo n.º 14
0
  public List<ReferenceType> nestedTypes(ReferenceType refType) {
    List<ReferenceType> nestedTypes = myNestedClassesCache.get(refType);
    if (nestedTypes == null) {
      List<ReferenceType> list = Collections.emptyList();
      try {
        list = refType.nestedTypes();
      } catch (Throwable e) {
        // sometimes some strange errors are thrown from JDI. Do not crash debugger because of this.
        // Example:
        // java.lang.StringIndexOutOfBoundsException: String index out of range: 487700285
        //	at java.lang.String.checkBounds(String.java:375)
        //	at java.lang.String.<init>(String.java:415)
        //	at com.sun.tools.jdi.PacketStream.readString(PacketStream.java:392)
        //	at
        // com.sun.tools.jdi.JDWP$VirtualMachine$AllClassesWithGeneric$ClassInfo.<init>(JDWP.java:1644)
        LOG.info(e);
      }
      if (!list.isEmpty()) {
        final Set<ReferenceType> candidates = new HashSet<>();
        final ClassLoaderReference outerLoader = refType.classLoader();
        for (ReferenceType nested : list) {
          try {
            if (outerLoader == null
                ? nested.classLoader() == null
                : outerLoader.equals(nested.classLoader())) {
              candidates.add(nested);
            }
          } catch (ObjectCollectedException ignored) {
          }
        }

        if (!candidates.isEmpty()) {
          // keep only direct nested types
          final Set<ReferenceType> nested2 = new HashSet<>();
          for (final ReferenceType candidate : candidates) {
            nested2.addAll(nestedTypes(candidate));
          }
          candidates.removeAll(nested2);
        }

        nestedTypes = candidates.isEmpty() ? Collections.emptyList() : new ArrayList<>(candidates);
      } else {
        nestedTypes = Collections.emptyList();
      }
      myNestedClassesCache.put(refType, nestedTypes);
    }
    return nestedTypes;
  }
Exemplo n.º 15
0
  public static ReferenceType getSuperClass(
      final String baseQualifiedName, ReferenceType checkedType) {
    if (baseQualifiedName.equals(checkedType.name())) {
      return checkedType;
    }

    if (checkedType instanceof ClassType) {
      ClassType classType = (ClassType) checkedType;
      ClassType superClassType = classType.superclass();
      if (superClassType != null) {
        ReferenceType superClass = getSuperClass(baseQualifiedName, superClassType);
        if (superClass != null) {
          return superClass;
        }
      }
      List<InterfaceType> interfaces = classType.allInterfaces();
      for (InterfaceType iface : interfaces) {
        ReferenceType superClass = getSuperClass(baseQualifiedName, iface);
        if (superClass != null) {
          return superClass;
        }
      }
    }

    if (checkedType instanceof InterfaceType) {
      List<InterfaceType> list = ((InterfaceType) checkedType).superinterfaces();
      for (InterfaceType superInterface : list) {
        ReferenceType superClass = getSuperClass(baseQualifiedName, superInterface);
        if (superClass != null) {
          return superClass;
        }
      }
    }
    return null;
  }
Exemplo n.º 16
0
  @Override
  public void buildChildren(
      final Value value, final ChildrenBuilder builder, final EvaluationContext evaluationContext) {
    DebuggerManagerThreadImpl.assertIsManagerThread();
    final ValueDescriptorImpl parentDescriptor =
        (ValueDescriptorImpl) builder.getParentDescriptor();
    final NodeManager nodeManager = builder.getNodeManager();
    final NodeDescriptorFactory nodeDescriptorFactory = builder.getDescriptorManager();

    List<DebuggerTreeNode> children = new ArrayList<>();
    if (value instanceof ObjectReference) {
      final ObjectReference objRef = (ObjectReference) value;
      final ReferenceType refType = objRef.referenceType();
      // default ObjectReference processing
      List<Field> fields = refType.allFields();
      if (!fields.isEmpty()) {
        Set<String> names = new HashSet<>();
        for (Field field : fields) {
          if (shouldDisplay(evaluationContext, objRef, field)) {
            FieldDescriptor fieldDescriptor =
                createFieldDescriptor(
                    parentDescriptor, nodeDescriptorFactory, objRef, field, evaluationContext);
            String name = fieldDescriptor.getName();
            if (names.contains(name)) {
              fieldDescriptor.putUserData(FieldDescriptor.SHOW_DECLARING_TYPE, Boolean.TRUE);
            } else {
              names.add(name);
            }
            children.add(nodeManager.createNode(fieldDescriptor, evaluationContext));
          }
        }

        if (children.isEmpty()) {
          children.add(
              nodeManager.createMessageNode(
                  DebuggerBundle.message("message.node.class.no.fields.to.display")));
        } else if (XDebuggerSettingsManager.getInstance().getDataViewSettings().isSortValues()) {
          children.sort(NodeManagerImpl.getNodeComparator());
        }
      } else {
        children.add(
            nodeManager.createMessageNode(MessageDescriptor.CLASS_HAS_NO_FIELDS.getLabel()));
      }
    }
    builder.setChildren(children);
  }
 /** Does the specified ReferenceType match this spec. */
 public boolean matches(ReferenceType refType) {
   try {
     if (refType.sourceName().equals(sourceName)) {
       try {
         refType.locationsOfLine(linenumber);
         // if we don't throw an exception then it was found
         return true;
       } catch (AbsentInformationException exc) {
       } catch (ObjectCollectedException exc) {
       } catch (InvalidLineNumberException exc) {
         //          } catch(ClassNotPreparedException  exc) {
         //               -- should not happen, so don't catch this ---
       }
     }
   } catch (AbsentInformationException exc) {
     // for sourceName(), fall through
   }
   return false;
 }
Exemplo n.º 18
0
 public Method findMethodInClass(ReferenceType clazz, String methodName, List<String> argTypes) {
   Method toReturn = null;
   for (Method m : clazz.methodsByName(methodName)) {
     if (m.argumentTypeNames().equals(argTypes)) {
       toReturn = m;
       break;
     }
   }
   return toReturn;
 }
Exemplo n.º 19
0
 @Override
 protected boolean createRequest(JDXDebugTarget target, ReferenceType type) throws CoreException {
   if (!type.name().equals(getTypeName()) || shouldSkipBreakpoint()) {
     return false;
   }
   EventRequest entryRequest = createMethodEntryRequest(target, type);
   EventRequest exitRequest = createMethodExitRequest(target, type);
   registerRequest(entryRequest, target);
   registerRequest(exitRequest, target);
   return true;
 }
Exemplo n.º 20
0
 /**
  * Return a list of ReferenceType objects for all currently loaded classes and interfaces whose
  * name matches the given pattern. The pattern syntax is open to some future revision, but
  * currently consists of a fully-qualified class name in which the first component may optionally
  * be a "*" character, designating an arbitrary prefix.
  */
 public List findClassesMatchingPattern(String pattern) throws JDEException {
   if (vm == null) return null;
   List result = new ArrayList(); // ### Is default size OK?
   if (pattern.startsWith("*.")) {
     // Wildcard matches any leading package name.
     pattern = pattern.substring(1);
     List classes = vm.allClasses();
     Iterator iter = classes.iterator();
     while (iter.hasNext()) {
       ReferenceType type = ((ReferenceType) iter.next());
       if (type.name().endsWith(pattern)) {
         result.add(type);
       }
     }
     return result;
   } else {
     // It's a class name.
     return vm.classesByName(pattern);
   }
 }
Exemplo n.º 21
0
 // 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;
 }
Exemplo n.º 22
0
 /**
  * Determine the location at which to set the breakpoint using the given class type.
  *
  * @param clazz ClassType against which to resolve.
  * @return Location at which to create breakpoint.
  * @throws ResolveException if the location failed to resolve.
  */
 protected Location resolveLocation(ClassType clazz) throws ResolveException {
   List locs = null;
   try {
     locs = clazz.locationsOfLine(lineNumber);
     if (locs.isEmpty()) {
       List inners = clazz.nestedTypes();
       Iterator iter = inners.iterator();
       while (iter.hasNext() && locs.isEmpty()) {
         ReferenceType type = (ReferenceType) iter.next();
         locs = type.locationsOfLine(lineNumber);
       }
       if (locs.isEmpty()) {
         throw new ResolveException(new LineNotFoundException());
       }
     }
   } catch (AbsentInformationException aie) {
     throw new ResolveException(aie);
   }
   // We assume the first location for this line is good enough.
   return (Location) locs.get(0);
 }
Exemplo n.º 23
0
 /** Get source object associated with a class or interface. Returns null if not available. */
 public SourceModel sourceForClass(ReferenceType refType) {
   SourceModel sm = classToSource.get(refType);
   if (sm != null) {
     return sm;
   }
   try {
     String filename = refType.sourceName();
     String refName = refType.name();
     int iDot = refName.lastIndexOf('.');
     String pkgName = (iDot >= 0) ? refName.substring(0, iDot + 1) : "";
     String full = pkgName.replace('.', File.separatorChar) + filename;
     File path = sourcePath.resolve(full);
     if (path != null) {
       sm = sourceForFile(path);
       classToSource.put(refType, sm);
       return sm;
     }
     return null;
   } catch (AbsentInformationException e) {
     return null;
   }
 }
Exemplo n.º 24
0
 private void dump(
     OutputSink out, ObjectReference obj, ReferenceType refType, ReferenceType refTypeBase) {
   for (Field field : refType.fields()) {
     out.print("    ");
     if (!refType.equals(refTypeBase)) {
       out.print(refType.name() + ".");
     }
     out.print(field.name() + ": ");
     Object o = obj.getValue(field);
     out.println((o == null) ? "null" : o.toString()); // Bug ID 4374471
   }
   if (refType instanceof ClassType) {
     ClassType sup = ((ClassType) refType).superclass();
     if (sup != null) {
       dump(out, obj, sup, refTypeBase);
     }
   } else if (refType instanceof InterfaceType) {
     for (InterfaceType sup : ((InterfaceType) refType).superinterfaces()) {
       dump(out, obj, sup, refTypeBase);
     }
   }
 }
Exemplo n.º 25
0
 @Override
 protected void createRequestForPreparedClass(
     DebugVMEventsProcessor debugProcess, final ReferenceType classType) {
   RequestManager requestManager = debugProcess.getRequestManager();
   try {
     int lineIndex = myLocation.getLineIndexInFile();
     List<Location> locs = classType.locationsOfLine(lineIndex);
     if (locs.size() > 0) {
       for (final Location location : locs) {
         BreakpointRequest request = requestManager.createBreakpointRequest(this, location);
         requestManager.enableRequest(request);
       }
     } else {
       //  there's no executable code in this class
       requestManager.setInvalid(this, "no executable code found");
       String message =
           "No locations of type "
               + classType.name()
               + " found at line "
               + myLocation.getLineIndexInFile();
       LOG.warning(message);
     }
   } catch (ClassNotPreparedException ex) {
     LOG.warning("ClassNotPreparedException: " + ex.getMessage());
     //  there's a chance to add a breakpoint when the class is prepared
   } catch (ObjectCollectedException ex) {
     LOG.warning("ObjectCollectedException: " + ex.getMessage());
     //  there's a chance to add a breakpoint when the class is prepared
   } catch (InvalidLineNumberException ex) {
     requestManager.setInvalid(this, "no executable code found");
     LOG.warning("InvalidLineNumberException: " + ex.getMessage());
   } catch (InternalException ex) {
     LOG.error(ex);
   } catch (Exception ex) {
     LOG.error(ex);
   }
 }
Exemplo n.º 26
0
 /** Does the specified ReferenceType match this spec. */
 public boolean matches(ReferenceType refType) {
   String refName = refType.name().replace('$', '.');
   String compName;
   compName = refName;
   if (anonymous) {
     String anonymousBase = anonymousBaseName(refName);
     if (anonymousBase != null) compName = anonymousBase;
     else return false;
   }
   if (classId.startsWith("*")) {
     return compName.endsWith(stem);
   } else if (classId.endsWith("*")) {
     return compName.startsWith(stem);
   } else if (checkJavaLang
       && compName.startsWith("java.lang.")
       && compName.substring(10).equals(stem)) {
     return true;
   } else {
     return compName.equals(transClassId);
   }
 }
 private static boolean isAnonymousClass(ReferenceType classType) {
   if (classType instanceof ClassType) {
     return ourAnonymousPattern.matcher(classType.name()).matches();
   }
   return false;
 }
  private static List<ThreadState> buildThreadStates(VirtualMachineProxyImpl vmProxy) {
    final List<ThreadReference> threads = vmProxy.getVirtualMachine().allThreads();
    final List<ThreadState> result = new ArrayList<ThreadState>();
    final Map<String, ThreadState> nameToThreadMap = new HashMap<String, ThreadState>();
    final Map<String, String> waitingMap = new HashMap<String, String>(); // key 'waits_for' value
    for (ThreadReference threadReference : threads) {
      final StringBuilder buffer = new StringBuilder();
      boolean hasEmptyStack = true;
      final int threadStatus = threadReference.status();
      if (threadStatus == ThreadReference.THREAD_STATUS_ZOMBIE) {
        continue;
      }
      final String threadName = threadName(threadReference);
      final ThreadState threadState =
          new ThreadState(threadName, threadStatusToState(threadStatus));
      nameToThreadMap.put(threadName, threadState);
      result.add(threadState);
      threadState.setJavaThreadState(threadStatusToJavaThreadState(threadStatus));

      buffer.append("\"").append(threadName).append("\"");
      ReferenceType referenceType = threadReference.referenceType();
      if (referenceType != null) {
        //noinspection HardCodedStringLiteral
        Field daemon = referenceType.fieldByName("daemon");
        if (daemon != null) {
          Value value = threadReference.getValue(daemon);
          if (value instanceof BooleanValue && ((BooleanValue) value).booleanValue()) {
            buffer
                .append(" ")
                .append(DebuggerBundle.message("threads.export.attribute.label.daemon"));
            threadState.setDaemon(true);
          }
        }

        //noinspection HardCodedStringLiteral
        Field priority = referenceType.fieldByName("priority");
        if (priority != null) {
          Value value = threadReference.getValue(priority);
          if (value instanceof IntegerValue) {
            buffer
                .append(" ")
                .append(
                    DebuggerBundle.message(
                        "threads.export.attribute.label.priority",
                        ((IntegerValue) value).intValue()));
          }
        }

        Field tid = referenceType.fieldByName("tid");
        if (tid != null) {
          Value value = threadReference.getValue(tid);
          if (value instanceof LongValue) {
            buffer
                .append(" ")
                .append(
                    DebuggerBundle.message(
                        "threads.export.attribute.label.tid",
                        Long.toHexString(((LongValue) value).longValue())));
            buffer.append(" nid=NA");
          }
        }
      }
      // ThreadGroupReference groupReference = threadReference.threadGroup();
      // if (groupReference != null) {
      //  buffer.append(", ").append(DebuggerBundle.message("threads.export.attribute.label.group",
      // groupReference.name()));
      // }
      final String state = threadState.getState();
      if (state != null) {
        buffer.append(" ").append(state);
      }

      buffer.append("\n  java.lang.Thread.State: ").append(threadState.getJavaThreadState());

      try {
        if (vmProxy.canGetOwnedMonitorInfo() && vmProxy.canGetMonitorInfo()) {
          List<ObjectReference> list = threadReference.ownedMonitors();
          for (ObjectReference reference : list) {
            if (!vmProxy.canGetMonitorFrameInfo()) { // java 5 and earlier
              buffer.append("\n\t ").append(renderLockedObject(reference));
            }
            final List<ThreadReference> waiting = reference.waitingThreads();
            for (ThreadReference thread : waiting) {
              final String waitingThreadName = threadName(thread);
              waitingMap.put(waitingThreadName, threadName);
              buffer
                  .append("\n\t ")
                  .append(
                      DebuggerBundle.message(
                          "threads.export.attribute.label.blocks.thread", waitingThreadName));
            }
          }
        }

        ObjectReference waitedMonitor =
            vmProxy.canGetCurrentContendedMonitor()
                ? threadReference.currentContendedMonitor()
                : null;
        if (waitedMonitor != null) {
          if (vmProxy.canGetMonitorInfo()) {
            ThreadReference waitedMonitorOwner = waitedMonitor.owningThread();
            if (waitedMonitorOwner != null) {
              final String monitorOwningThreadName = threadName(waitedMonitorOwner);
              waitingMap.put(threadName, monitorOwningThreadName);
              buffer
                  .append("\n\t ")
                  .append(
                      DebuggerBundle.message(
                          "threads.export.attribute.label.waiting.for.thread",
                          monitorOwningThreadName,
                          renderObject(waitedMonitor)));
            }
          }
        }

        final List<StackFrame> frames = threadReference.frames();
        hasEmptyStack = frames.size() == 0;

        final TIntObjectHashMap<List<ObjectReference>> lockedAt =
            new TIntObjectHashMap<List<ObjectReference>>();
        if (vmProxy.canGetMonitorFrameInfo()) {
          for (MonitorInfo info : threadReference.ownedMonitorsAndFrames()) {
            final int stackDepth = info.stackDepth();
            List<ObjectReference> monitors;
            if ((monitors = lockedAt.get(stackDepth)) == null) {
              lockedAt.put(stackDepth, monitors = new SmartList<ObjectReference>());
            }
            monitors.add(info.monitor());
          }
        }

        for (int i = 0, framesSize = frames.size(); i < framesSize; i++) {
          final StackFrame stackFrame = frames.get(i);
          try {
            final Location location = stackFrame.location();
            buffer.append("\n\t  ").append(renderLocation(location));

            final List<ObjectReference> monitors = lockedAt.get(i);
            if (monitors != null) {
              for (ObjectReference monitor : monitors) {
                buffer.append("\n\t  - ").append(renderLockedObject(monitor));
              }
            }
          } catch (InvalidStackFrameException e) {
            buffer.append("\n\t  Invalid stack frame: ").append(e.getMessage());
          }
        }
      } catch (IncompatibleThreadStateException e) {
        buffer
            .append("\n\t ")
            .append(DebuggerBundle.message("threads.export.attribute.error.incompatible.state"));
      }
      threadState.setStackTrace(buffer.toString(), hasEmptyStack);
      ThreadDumpParser.inferThreadStateDetail(threadState);
    }

    for (String waiting : waitingMap.keySet()) {
      final ThreadState waitingThread = nameToThreadMap.get(waiting);
      final ThreadState awaitedThread = nameToThreadMap.get(waitingMap.get(waiting));
      awaitedThread.addWaitingThread(waitingThread);
    }

    // detect simple deadlocks
    for (ThreadState thread : result) {
      for (ThreadState awaitingThread : thread.getAwaitingThreads()) {
        if (awaitingThread.isAwaitedBy(thread)) {
          thread.addDeadlockedThread(awaitingThread);
          awaitingThread.addDeadlockedThread(thread);
        }
      }
    }

    ThreadDumpParser.sortThreads(result);
    return result;
  }
Exemplo n.º 29
0
 private void commandList(StringTokenizer t) throws NoSessionException {
   ThreadReference current = context.getCurrentThread();
   if (current == null) {
     env.error("No thread specified.");
     return;
   }
   Location loc;
   try {
     StackFrame frame = context.getCurrentFrame(current);
     if (frame == null) {
       env.failure("Thread has not yet begun execution.");
       return;
     }
     loc = frame.location();
   } catch (VMNotInterruptedException e) {
     env.failure("Target VM must be in interrupted state.");
     return;
   }
   SourceModel source = sourceManager.sourceForLocation(loc);
   if (source == null) {
     if (loc.method().isNative()) {
       env.failure("Current method is native.");
       return;
     }
     env.failure("No source available for " + Utils.locationString(loc) + ".");
     return;
   }
   ReferenceType refType = loc.declaringType();
   int lineno = loc.lineNumber();
   if (t.hasMoreTokens()) {
     String id = t.nextToken();
     // See if token is a line number.
     try {
       lineno = Integer.valueOf(id).intValue();
     } catch (NumberFormatException nfe) {
       // It isn't -- see if it's a method name.
       List<Method> meths = refType.methodsByName(id);
       if (meths == null || meths.size() == 0) {
         env.failure(
             id + " is not a valid line number or " + "method name for class " + refType.name());
         return;
       } else if (meths.size() > 1) {
         env.failure(id + " is an ambiguous method name in" + refType.name());
         return;
       }
       loc = meths.get(0).location();
       lineno = loc.lineNumber();
     }
   }
   int startLine = (lineno > 4) ? lineno - 4 : 1;
   int endLine = startLine + 9;
   String sourceLine = source.sourceLine(lineno);
   if (sourceLine == null) {
     env.failure("" + lineno + " is an invalid line number for " + refType.name());
   } else {
     OutputSink out = env.getOutputSink();
     for (int i = startLine; i <= endLine; i++) {
       sourceLine = source.sourceLine(i);
       if (sourceLine == null) {
         break;
       }
       out.print(i);
       out.print("\t");
       if (i == lineno) {
         out.print("=> ");
       } else {
         out.print("   ");
       }
       out.println(sourceLine);
     }
     out.show();
   }
 }
Exemplo n.º 30
0
  private static List<ThreadState> buildThreadStates(VirtualMachineProxyImpl vmProxy) {
    final List<ThreadReference> threads = vmProxy.getVirtualMachine().allThreads();
    final List<ThreadState> result = new ArrayList<ThreadState>();
    final Map<String, ThreadState> nameToThreadMap = new HashMap<String, ThreadState>();
    final Map<String, String> waitingMap = new HashMap<String, String>(); // key 'waits_for' value
    for (ThreadReference threadReference : threads) {
      final StringBuilder buffer = new StringBuilder();
      boolean hasEmptyStack = true;
      final String threadName = threadName(threadReference);
      final int threadStatus = threadReference.status();
      final ThreadState threadState =
          new ThreadState(threadName, threadStatusToState(threadStatus));
      nameToThreadMap.put(threadName, threadState);
      result.add(threadState);
      threadState.setJavaThreadState(threadStatusToJavaThreadState(threadStatus));

      buffer.append(threadName);
      ReferenceType referenceType = threadReference.referenceType();
      if (referenceType != null) {
        //noinspection HardCodedStringLiteral
        Field daemon = referenceType.fieldByName("daemon");
        if (daemon != null) {
          Value value = threadReference.getValue(daemon);
          if (value instanceof BooleanValue && ((BooleanValue) value).booleanValue()) {
            buffer
                .append(" ")
                .append(DebuggerBundle.message("threads.export.attribute.label.daemon"));
          }
        }

        //noinspection HardCodedStringLiteral
        Field priority = referenceType.fieldByName("priority");
        if (priority != null) {
          Value value = threadReference.getValue(priority);
          if (value instanceof IntegerValue) {
            buffer
                .append(", ")
                .append(
                    DebuggerBundle.message(
                        "threads.export.attribute.label.priority",
                        ((IntegerValue) value).intValue()));
          }
        }
      }

      ThreadGroupReference groupReference = threadReference.threadGroup();
      if (groupReference != null) {
        buffer
            .append(", ")
            .append(
                DebuggerBundle.message(
                    "threads.export.attribute.label.group", groupReference.name()));
      }
      buffer
          .append(", ")
          .append(
              DebuggerBundle.message(
                  "threads.export.attribute.label.status", threadState.getState()));

      buffer.append("\n  java.lang.Thread.State: ").append(threadState.getJavaThreadState());

      try {
        if (vmProxy.canGetOwnedMonitorInfo() && vmProxy.canGetMonitorInfo()) {
          List<ObjectReference> list = threadReference.ownedMonitors();
          for (ObjectReference reference : list) {
            final List<ThreadReference> waiting = reference.waitingThreads();
            for (ThreadReference thread : waiting) {
              final String waitingThreadName = threadName(thread);
              waitingMap.put(waitingThreadName, threadName);
              buffer
                  .append("\n\t ")
                  .append(
                      DebuggerBundle.message(
                          "threads.export.attribute.label.blocks.thread", waitingThreadName));
            }
          }
        }

        ObjectReference waitedMonitor =
            vmProxy.canGetCurrentContendedMonitor()
                ? threadReference.currentContendedMonitor()
                : null;
        if (waitedMonitor != null) {
          if (vmProxy.canGetMonitorInfo()) {
            ThreadReference waitedMonitorOwner = waitedMonitor.owningThread();
            if (waitedMonitorOwner != null) {
              final String monitorOwningThreadName = threadName(waitedMonitorOwner);
              waitingMap.put(threadName, monitorOwningThreadName);
              buffer
                  .append("\n\t ")
                  .append(
                      DebuggerBundle.message(
                          "threads.export.attribute.label.waiting.for.thread",
                          monitorOwningThreadName));
            }
          }
        }

        final List<StackFrame> frames = threadReference.frames();
        hasEmptyStack = frames.size() == 0;
        for (StackFrame stackFrame : frames) {
          final Location location = stackFrame.location();
          buffer.append("\n\t  ").append(renderLocation(location));
        }
      } catch (IncompatibleThreadStateException e) {
        buffer
            .append("\n\t ")
            .append(DebuggerBundle.message("threads.export.attribute.error.incompatible.state"));
      }
      threadState.setStackTrace(buffer.toString(), hasEmptyStack);
      ThreadDumpParser.inferThreadStateDetail(threadState);
    }

    for (String waiting : waitingMap.keySet()) {
      final ThreadState waitingThread = nameToThreadMap.get(waiting);
      final ThreadState awaitedThread = nameToThreadMap.get(waitingMap.get(waiting));
      awaitedThread.addWaitingThread(waitingThread);
    }

    // detect simple deadlocks
    for (ThreadState thread : result) {
      for (ThreadState awaitingThread : thread.getAwaitingThreads()) {
        if (awaitingThread.isAwaitedBy(thread)) {
          thread.addDeadlockedThread(awaitingThread);
          awaitingThread.addDeadlockedThread(thread);
        }
      }
    }

    ThreadDumpParser.sortThreads(result);
    return result;
  }