コード例 #1
0
  private synchronized String getStackTrace() {
    if (stackTrace == null) {
      boolean gotoSourceAvailable =
          heapFragmentWalker.getHeapDumpProject() != null && GoToSource.isAvailable();
      StringBuilder sb = new StringBuilder();
      Heap h = heapFragmentWalker.getHeapFragment();
      Collection<GCRoot> roots = h.getGCRoots();
      Map<ThreadObjectGCRoot, Map<Integer, List<JavaFrameGCRoot>>> javaFrameMap =
          computeJavaFrameMap(roots);
      // Use this to enable VisualVM color scheme for threads dumps:
      // sw.append("<pre style='color: #cc3300;'>"); // NOI18N
      sb.append("<pre>"); // NOI18N
      for (GCRoot root : roots) {
        if (root.getKind().equals(GCRoot.THREAD_OBJECT)) {
          ThreadObjectGCRoot threadRoot = (ThreadObjectGCRoot) root;
          Instance threadInstance = threadRoot.getInstance();
          if (threadInstance != null) {
            String threadName = getThreadName(threadInstance);
            Boolean daemon = (Boolean) threadInstance.getValueOfField("daemon"); // NOI18N
            Integer priority = (Integer) threadInstance.getValueOfField("priority"); // NOI18N
            Long threadId = (Long) threadInstance.getValueOfField("tid"); // NOI18N
            Integer threadStatus =
                (Integer) threadInstance.getValueOfField("threadStatus"); // NOI18N
            StackTraceElement stack[] = threadRoot.getStackTrace();
            Map<Integer, List<JavaFrameGCRoot>> localsMap = javaFrameMap.get(threadRoot);
            String style = "";

            if (threadRoot.equals(oome)) {
              style = "style=\"color: #FF0000\"";
            }
            // --- Use this to enable VisualVM color scheme for threads dumps: ---
            // sw.append("&nbsp;&nbsp;<span style=\"color: #0033CC\">"); // NOI18N
            sb.append("&nbsp;&nbsp;<a name=")
                .append(threadInstance.getInstanceId())
                .append("></a><b ")
                .append(style)
                .append(">"); // NOI18N
            // -------------------------------------------------------------------
            sb.append("\"")
                .append(htmlize(threadName))
                .append("\"")
                .append(daemon.booleanValue() ? " daemon" : "")
                .append(" prio=")
                .append(priority); // NOI18N
            if (threadId != null) {
              sb.append(" tid=").append(threadId); // NOI18N
            }
            if (threadStatus != null) {
              State tState = sun.misc.VM.toThreadState(threadStatus.intValue());
              sb.append(" ").append(tState); // NOI18N
            }
            // --- Use this to enable VisualVM color scheme for threads dumps: ---
            // sw.append("</span><br>"); // NOI18N
            sb.append("</b><br>"); // NOI18N
            // -------------------------------------------------------------------
            if (stack != null) {
              for (int i = 0; i < stack.length; i++) {
                String stackElHref;
                StackTraceElement stackElement = stack[i];
                String stackElementText = htmlize(stackElement.toString());

                if (gotoSourceAvailable) {
                  String className = stackElement.getClassName();
                  String method = stackElement.getMethodName();
                  int lineNo = stackElement.getLineNumber();
                  String stackUrl =
                      OPEN_THREADS_URL + className + "|" + method + "|" + lineNo; // NOI18N

                  // --- Use this to enable VisualVM color scheme for threads dumps: ---
                  // stackElHref = "&nbsp;&nbsp;<a style=\"color: #CC3300;\"
                  // href=\""+stackUrl+"\">"+stackElement+"</a>"; // NOI18N
                  stackElHref =
                      "<a href=\"" + stackUrl + "\">" + stackElementText + "</a>"; // NOI18N
                  // -------------------------------------------------------------------
                } else {
                  stackElHref = stackElementText;
                }
                sb.append("\tat ").append(stackElHref).append("<br>"); // NOI18N
                if (localsMap != null) {
                  List<JavaFrameGCRoot> locals = localsMap.get(Integer.valueOf(i));

                  if (locals != null) {
                    for (JavaFrameGCRoot localVar : locals) {
                      Instance localInstance = localVar.getInstance();

                      if (localInstance != null) {
                        sb.append("\t   Local Variable: ")
                            .append(printInstance(localInstance))
                            .append("<br>"); // NOI18N
                      } else {
                        sb.append(
                            "\t   Unknown Local Variable<br>"); // NOI18N
                      }
                    }
                  }
                }
              }
            }
          } else {
            sb.append("&nbsp;&nbsp;Unknown thread"); // NOI18N
          }
          sb.append("<br>"); // NOI18N
        }
      }
      sb.append("</pre>"); // NOI18N
      stackTrace = sb.toString();
    }
    return stackTrace;
  }
コード例 #2
0
  private JPopupMenu createTablePopup() {
    JPopupMenu popup = new JPopupMenu();

    showInstanceItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_ShowInstanceItemText());
    showInstanceItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            performDefaultAction();
          }
        });
    showInstanceItem.setFont(popup.getFont().deriveFont(Font.BOLD));

    //        showClassItem = new JMenuItem(SHOW_IN_CLASSES_ITEM_TEXT);
    //        showClassItem.addActionListener(new ActionListener() {
    //                public void actionPerformed(ActionEvent e) {
    //                    int row = fieldsListTable.getSelectedRow();
    //
    //                    if (row != -1) {
    //                        HeapWalkerNode node = (HeapWalkerNode)
    // fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();
    //
    //                        if (node instanceof HeapWalkerInstanceNode) {
    //                            HeapWalkerInstanceNode instanceNode = (HeapWalkerInstanceNode)
    // node;
    //
    //                            if (instanceNode instanceof ObjectFieldNode && ((ObjectFieldNode)
    // instanceNode).isStatic()) {
    //                                referencesBrowserController.navigateToClass(((ObjectFieldNode)
    // instanceNode).getFieldValue()
    //
    // .getField().getDeclaringClass());
    //                            } else if (instanceNode.hasInstance()) {
    //
    // referencesBrowserController.navigateToClass(instanceNode.getInstance().getJavaClass());
    //                            }
    //                        }
    //                    }
    //                }
    //            });

    showGcRootItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_ShowGcRootItemText());
    showGcRootItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int row = fieldsListTable.getSelectedRow();

            if (row != -1) {
              HeapWalkerNode node =
                  (HeapWalkerNode)
                      fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();

              if (node instanceof InstanceNode) {
                InstanceNode instanceNode = (InstanceNode) node;

                if (instanceNode.hasInstance()) {
                  referencesBrowserController.navigateToNearestGCRoot(instanceNode);
                }
              }
            }
          }
        });

    showLoopOriginItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_ShowLoopItemText());
    showLoopOriginItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int row = fieldsListTable.getSelectedRow();

            if (row != -1) {
              HeapWalkerNode node =
                  (HeapWalkerNode)
                      fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();

              if (node instanceof HeapWalkerInstanceNode
                  && ((HeapWalkerInstanceNode) node).isLoop()) {
                selectNode(((HeapWalkerInstanceNode) node).getLoopTo());
              }
            }
          }
        });

    // Copy Path From Root
    copyPathFromRootItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_CopyPathFromRoot());
    copyPathFromRootItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int row = fieldsListTable.getSelectedRow();
            if (row != -1) {
              TreePath path = fieldsListTable.getTree().getPathForRow(row);
              BrowserUtils.copyPathFromRoot(path);
            }
          };
        });

    if (GoToSource.isAvailable()) {
      showSourceItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_GoToSourceItemText());
      showSourceItem.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              int row = fieldsListTable.getSelectedRow();

              if (row != -1) {
                HeapWalkerNode node =
                    (HeapWalkerNode)
                        fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();
                String className = node.getType();

                while (className.endsWith("[]")) {
                  className = className.substring(0, className.length() - 2); // NOI18N
                }
                Lookup.Provider p =
                    referencesBrowserController
                        .getReferencesControllerHandler()
                        .getHeapFragmentWalker()
                        .getHeapDumpProject();
                GoToSource.openSource(p, className, null, null);
              }
            }
          });
    }

    showInThreadsItem = new JMenuItem(Bundle.ReferencesBrowserControllerUI_ShowInThreadsItemText());
    showInThreadsItem.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            int row = fieldsListTable.getSelectedRow();

            if (row != -1) {
              HeapWalkerNode node =
                  (HeapWalkerNode)
                      fieldsListTable.getTree().getPathForRow(row).getLastPathComponent();
              if (node instanceof HeapWalkerInstanceNode) {
                Instance instance = ((HeapWalkerInstanceNode) node).getInstance();
                referencesBrowserController.showInThreads(instance);
              }
            }
          }
        });

    popup.add(showInstanceItem);
    //        popup.add(showClassItem);
    popup.add(showGcRootItem);
    popup.add(showInThreadsItem);
    popup.addSeparator();
    popup.add(copyPathFromRootItem);
    popup.addSeparator();
    popup.add(showLoopOriginItem);
    if (showSourceItem != null) popup.add(showSourceItem);

    return popup;
  }