1 /* 2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 package sun.jvm.hotspot; 26 27 import java.io.*; 28 import java.awt.*; 29 import java.awt.event.*; 30 import java.math.*; 31 import javax.swing.*; 32 import javax.swing.tree.*; 33 import java.util.*; 34 35 import sun.jvm.hotspot.code.*; 36 import sun.jvm.hotspot.compiler.*; 37 import sun.jvm.hotspot.debugger.*; 38 import sun.jvm.hotspot.gc_implementation.parallelScavenge.*; 39 import sun.jvm.hotspot.gc_interface.*; 40 import sun.jvm.hotspot.interpreter.*; 41 import sun.jvm.hotspot.memory.*; 42 import sun.jvm.hotspot.oops.*; 43 import sun.jvm.hotspot.runtime.*; 44 import sun.jvm.hotspot.ui.*; 45 import sun.jvm.hotspot.ui.tree.*; 46 import sun.jvm.hotspot.ui.classbrowser.*; 47 import sun.jvm.hotspot.utilities.*; 48 49 /** The top-level HotSpot Debugger. FIXME: make this an embeddable 50 component! (Among other things, figure out what to do with the 51 menu bar...) */ 52 53 public class HSDB implements ObjectHistogramPanel.Listener, SAListener { 54 public static void main(String[] args) { 55 new HSDB(args).run(); 56 } 57 58 //-------------------------------------------------------------------------------- 59 // Internals only below this point 60 // 61 private HotSpotAgent agent; 62 private JVMDebugger jvmDebugger; 63 private JDesktopPane desktop; 64 private boolean attached; 65 private boolean argError; 66 private JFrame frame; 67 /** List <JMenuItem> */ 68 private java.util.List attachMenuItems; 69 /** List <JMenuItem> */ 70 private java.util.List detachMenuItems; 71 private JMenu toolsMenu; 72 private JMenuItem showDbgConsoleMenuItem; 73 private JMenuItem computeRevPtrsMenuItem; 74 private JInternalFrame attachWaitDialog; 75 private JInternalFrame threadsFrame; 76 private JInternalFrame consoleFrame; 77 private WorkerThread workerThread; 78 // These had to be made data members because they are referenced in inner classes. 79 private String pidText; 80 private int pid; 81 private String execPath; 82 private String coreFilename; 83 84 private void doUsage() { 85 System.out.println("Usage: java HSDB [[pid] | [path-to-java-executable [path-to-corefile]] | help ]"); 86 System.out.println(" pid: attach to the process whose id is 'pid'"); 87 System.out.println(" path-to-java-executable: Debug a core file produced by this program"); 88 System.out.println(" path-to-corefile: Debug this corefile. The default is 'core'"); 89 System.out.println(" If no arguments are specified, you can select what to do from the GUI.\n"); 90 HotSpotAgent.showUsage(); 91 argError = true; 92 } 93 94 public HSDB(JVMDebugger d) { 95 jvmDebugger = d; 96 } 97 98 private HSDB(String[] args) { 99 switch (args.length) { 100 case (0): 101 break; 102 103 case (1): 104 if (args[0].equals("help") || args[0].equals("-help")) { 105 doUsage(); 106 } 107 // If all numbers, it is a PID to attach to 108 // Else, it is a pathname to a .../bin/java for a core file. 109 try { 110 int unused = Integer.parseInt(args[0]); 111 // If we get here, we have a PID and not a core file name 112 pidText = args[0]; 113 } catch (NumberFormatException e) { 114 execPath = args[0]; 115 coreFilename = "core"; 116 } 117 break; 118 119 case (2): 120 execPath = args[0]; 121 coreFilename = args[1]; 122 break; 123 124 default: 125 System.out.println("HSDB Error: Too many options specified"); 126 doUsage(); 127 } 128 } 129 130 // close this tool without calling System.exit 131 protected void closeUI() { 132 workerThread.shutdown(); 133 frame.dispose(); 134 } 135 136 public void run() { 137 // Don't start the UI if there were bad arguments. 138 if (argError) { 139 return; 140 } 141 142 agent = new HotSpotAgent(); 143 workerThread = new WorkerThread(); 144 attachMenuItems = new java.util.ArrayList(); 145 detachMenuItems = new java.util.ArrayList(); 146 147 frame = new JFrame("HSDB - HotSpot Debugger"); 148 frame.setSize(800, 600); 149 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 150 151 JMenuBar menuBar = new JMenuBar(); 152 153 // 154 // File menu 155 // 156 157 JMenu menu = new JMenu("File"); 158 menu.setMnemonic(KeyEvent.VK_F); 159 JMenuItem item; 160 item = createMenuItem("Attach to HotSpot process...", 161 new ActionListener() { 162 public void actionPerformed(ActionEvent e) { 163 showAttachDialog(); 164 } 165 }); 166 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK)); 167 item.setMnemonic(KeyEvent.VK_A); 168 menu.add(item); 169 attachMenuItems.add(item); 170 171 item = createMenuItem("Open HotSpot core file...", 172 new ActionListener() { 173 public void actionPerformed(ActionEvent e) { 174 showOpenCoreFileDialog(); 175 } 176 }); 177 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.ALT_MASK)); 178 item.setMnemonic(KeyEvent.VK_O); 179 menu.add(item); 180 attachMenuItems.add(item); 181 182 item = createMenuItem("Connect to debug server...", 183 new ActionListener() { 184 public void actionPerformed(ActionEvent e) { 185 showConnectDialog(); 186 } 187 }); 188 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK)); 189 item.setMnemonic(KeyEvent.VK_S); 190 menu.add(item); 191 attachMenuItems.add(item); 192 193 item = createMenuItem("Detach", 194 new ActionListener() { 195 public void actionPerformed(ActionEvent e) { 196 detach(); 197 } 198 }); 199 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK)); 200 item.setMnemonic(KeyEvent.VK_S); 201 menu.add(item); 202 detachMenuItems.add(item); 203 204 // Disable detach menu items at first 205 setMenuItemsEnabled(detachMenuItems, false); 206 207 menu.addSeparator(); 208 209 item = createMenuItem("Exit", 210 new ActionListener() { 211 public void actionPerformed(ActionEvent e) { 212 closeUI(); 213 } 214 }); 215 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.ALT_MASK)); 216 item.setMnemonic(KeyEvent.VK_X); 217 menu.add(item); 218 menuBar.add(menu); 219 220 // 221 // Tools menu 222 // 223 224 toolsMenu = new JMenu("Tools"); 225 toolsMenu.setMnemonic(KeyEvent.VK_T); 226 227 item = createMenuItem("Class Browser", 228 new ActionListener() { 229 public void actionPerformed(ActionEvent e) { 230 showClassBrowser(); 231 } 232 }); 233 item.setMnemonic(KeyEvent.VK_B); 234 235 toolsMenu.add(item); 236 237 item = createMenuItem("Code Viewer", 238 new ActionListener() { 239 public void actionPerformed(ActionEvent e) { 240 showCodeViewer(); 241 } 242 }); 243 item.setMnemonic(KeyEvent.VK_C); 244 245 toolsMenu.add(item); 246 247 248 item = createMenuItem("Compute Reverse Ptrs", 249 new ActionListener() { 250 public void actionPerformed(ActionEvent e) { 251 fireComputeReversePtrs(); 252 } 253 }); 254 computeRevPtrsMenuItem = item; 255 item.setMnemonic(KeyEvent.VK_M); 256 toolsMenu.add(item); 257 258 item = createMenuItem("Deadlock Detection", 259 new ActionListener() { 260 public void actionPerformed(ActionEvent e) { 261 showDeadlockDetectionPanel(); 262 } 263 }); 264 item.setMnemonic(KeyEvent.VK_D); 265 toolsMenu.add(item); 266 267 item = createMenuItem("Find Object by Query", 268 new ActionListener() { 269 public void actionPerformed(ActionEvent e) { 270 showFindByQueryPanel(); 271 } 272 }); 273 item.setMnemonic(KeyEvent.VK_Q); 274 toolsMenu.add(item); 275 276 277 item = createMenuItem("Find Pointer", 278 new ActionListener() { 279 public void actionPerformed(ActionEvent e) { 280 showFindPanel(); 281 } 282 }); 283 item.setMnemonic(KeyEvent.VK_P); 284 toolsMenu.add(item); 285 286 item = createMenuItem("Find Value In Heap", 287 new ActionListener() { 288 public void actionPerformed(ActionEvent e) { 289 showFindInHeapPanel(); 290 } 291 }); 292 item.setMnemonic(KeyEvent.VK_V); 293 toolsMenu.add(item); 294 295 item = createMenuItem("Find Value In Code Cache", 296 new ActionListener() { 297 public void actionPerformed(ActionEvent e) { 298 showFindInCodeCachePanel(); 299 } 300 }); 301 item.setMnemonic(KeyEvent.VK_A); 302 toolsMenu.add(item); 303 304 item = createMenuItem("Heap Parameters", 305 new ActionListener() { 306 public void actionPerformed(ActionEvent e) { 307 showHeapParametersPanel(); 308 } 309 }); 310 item.setMnemonic(KeyEvent.VK_H); 311 toolsMenu.add(item); 312 313 item = createMenuItem("Inspector", 314 new ActionListener() { 315 public void actionPerformed(ActionEvent e) { 316 showInspector(null); 317 } 318 }); 319 item.setMnemonic(KeyEvent.VK_R); 320 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK)); 321 toolsMenu.add(item); 322 323 item = createMenuItem("Memory Viewer", 324 new ActionListener() { 325 public void actionPerformed(ActionEvent e) { 326 showMemoryViewer(); 327 } 328 }); 329 item.setMnemonic(KeyEvent.VK_M); 330 toolsMenu.add(item); 331 332 item = createMenuItem("Monitor Cache Dump", 333 new ActionListener() { 334 public void actionPerformed(ActionEvent e) { 335 showMonitorCacheDumpPanel(); 336 } 337 }); 338 item.setMnemonic(KeyEvent.VK_D); 339 toolsMenu.add(item); 340 341 item = createMenuItem("Object Histogram", 342 new ActionListener() { 343 public void actionPerformed(ActionEvent e) { 344 showObjectHistogram(); 345 } 346 }); 347 item.setMnemonic(KeyEvent.VK_O); 348 toolsMenu.add(item); 349 350 item = createMenuItem("Show System Properties", 351 new ActionListener() { 352 public void actionPerformed(ActionEvent e) { 353 showSystemProperties(); 354 } 355 }); 356 item.setMnemonic(KeyEvent.VK_S); 357 toolsMenu.add(item); 358 359 item = createMenuItem("Show VM Version", 360 new ActionListener() { 361 public void actionPerformed(ActionEvent e) { 362 showVMVersion(); 363 } 364 }); 365 item.setMnemonic(KeyEvent.VK_M); 366 toolsMenu.add(item); 367 368 item = createMenuItem("Show -XX flags", 369 new ActionListener() { 370 public void actionPerformed(ActionEvent e) { 371 showCommandLineFlags(); 372 } 373 }); 374 item.setMnemonic(KeyEvent.VK_X); 375 toolsMenu.add(item); 376 377 toolsMenu.setEnabled(false); 378 menuBar.add(toolsMenu); 379 380 // 381 // Windows menu 382 // 383 384 JMenu windowsMenu = new JMenu("Windows"); 385 windowsMenu.setMnemonic(KeyEvent.VK_W); 386 item = createMenuItem("Console", 387 new ActionListener() { 388 public void actionPerformed(ActionEvent e) { 389 showConsole(); 390 } 391 }); 392 item.setMnemonic(KeyEvent.VK_C); 393 windowsMenu.add(item); 394 showDbgConsoleMenuItem = createMenuItem("Debugger Console", 395 new ActionListener() { 396 public void actionPerformed(ActionEvent e) { 397 showDebuggerConsole(); 398 } 399 }); 400 showDbgConsoleMenuItem.setMnemonic(KeyEvent.VK_D); 401 windowsMenu.add(showDbgConsoleMenuItem); 402 showDbgConsoleMenuItem.setEnabled(false); 403 404 menuBar.add(windowsMenu); 405 406 407 frame.setJMenuBar(menuBar); 408 409 desktop = new JDesktopPane(); 410 frame.getContentPane().add(desktop); 411 GraphicsUtilities.reshapeToAspectRatio(frame, 4.0f/3.0f, 0.75f, Toolkit.getDefaultToolkit().getScreenSize()); 412 GraphicsUtilities.centerInContainer(frame, Toolkit.getDefaultToolkit().getScreenSize()); 413 frame.setVisible(true); 414 415 Runtime.getRuntime().addShutdownHook(new java.lang.Thread() { 416 public void run() { 417 detachDebugger(); 418 } 419 }); 420 421 // If jvmDebugger is already set, we have been given a JVMDebugger. 422 // Otherwise, if pidText != null we are supposed to attach to it. 423 // Finally, if execPath != null, it is the path of a jdk/bin/java 424 // and coreFilename is the pathname of a core file we are 425 // supposed to attach to. 426 427 if (jvmDebugger != null) { 428 attach(jvmDebugger); 429 } else if (pidText != null) { 430 attach(pidText); 431 } else if (execPath != null) { 432 attach(execPath, coreFilename); 433 } 434 } 435 436 // FIXME: merge showAttachDialog, showOpenCoreFileDialog, showConnectDialog 437 private void showAttachDialog() { 438 // FIXME: create filtered text field which only accepts numbers 439 setMenuItemsEnabled(attachMenuItems, false); 440 final JInternalFrame attachDialog = new JInternalFrame("Attach to HotSpot process"); 441 attachDialog.getContentPane().setLayout(new BorderLayout()); 442 443 JPanel panel = new JPanel(); 444 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 445 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 446 attachDialog.setBackground(panel.getBackground()); 447 448 panel.add(new JLabel("Enter process ID:")); 449 final JTextField pidTextField = new JTextField(10); 450 ActionListener attacher = new ActionListener() { 451 public void actionPerformed(ActionEvent e) { 452 attachDialog.setVisible(false); 453 desktop.remove(attachDialog); 454 workerThread.invokeLater(new Runnable() { 455 public void run() { 456 attach(pidTextField.getText()); 457 } 458 }); 459 } 460 }; 461 462 pidTextField.addActionListener(attacher); 463 panel.add(pidTextField); 464 attachDialog.getContentPane().add(panel, BorderLayout.NORTH); 465 466 Box vbox = Box.createVerticalBox(); 467 panel = new JPanel(); 468 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 469 panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); 470 JTextArea ta = new JTextArea( 471 "Enter the process ID of a currently-running HotSpot process. On " + 472 "Solaris and most Unix operating systems, this can be determined by " + 473 "typing \"ps -u <your username> | grep java\"; the process ID is the " + 474 "first number which appears on the resulting line. On Windows, the " + 475 "process ID is present in the Task Manager, which can be brought up " + 476 "while logged on to the desktop by pressing Ctrl-Alt-Delete."); 477 ta.setLineWrap(true); 478 ta.setWrapStyleWord(true); 479 ta.setEditable(false); 480 ta.setBackground(panel.getBackground()); 481 panel.add(ta); 482 vbox.add(panel); 483 484 Box hbox = Box.createHorizontalBox(); 485 hbox.add(Box.createGlue()); 486 JButton button = new JButton("OK"); 487 button.addActionListener(attacher); 488 hbox.add(button); 489 hbox.add(Box.createHorizontalStrut(20)); 490 button = new JButton("Cancel"); 491 button.addActionListener(new ActionListener() { 492 public void actionPerformed(ActionEvent e) { 493 attachDialog.setVisible(false); 494 desktop.remove(attachDialog); 495 setMenuItemsEnabled(attachMenuItems, true); 496 } 497 }); 498 hbox.add(button); 499 hbox.add(Box.createGlue()); 500 panel = new JPanel(); 501 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 502 panel.add(hbox); 503 vbox.add(panel); 504 505 attachDialog.getContentPane().add(vbox, BorderLayout.SOUTH); 506 507 desktop.add(attachDialog); 508 attachDialog.setSize(400, 300); 509 GraphicsUtilities.centerInContainer(attachDialog); 510 attachDialog.show(); 511 pidTextField.requestFocus(); 512 } 513 514 // FIXME: merge showAttachDialog, showOpenCoreFileDialog, showConnectDialog 515 private void showOpenCoreFileDialog() { 516 setMenuItemsEnabled(attachMenuItems, false); 517 final JInternalFrame dialog = new JInternalFrame("Open Core File"); 518 dialog.getContentPane().setLayout(new BorderLayout()); 519 520 JPanel panel = new JPanel(); 521 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 522 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 523 dialog.setBackground(panel.getBackground()); 524 525 Box hbox = Box.createHorizontalBox(); 526 Box vbox = Box.createVerticalBox(); 527 vbox.add(new JLabel("Path to core file:")); 528 vbox.add(new JLabel("Path to Java executable:")); 529 hbox.add(vbox); 530 531 vbox = Box.createVerticalBox(); 532 final JTextField corePathField = new JTextField(40); 533 final JTextField execPathField = new JTextField(40); 534 vbox.add(corePathField); 535 vbox.add(execPathField); 536 hbox.add(vbox); 537 538 final JButton browseCorePath = new JButton("Browse .."); 539 final JButton browseExecPath = new JButton("Browse .."); 540 browseCorePath.addActionListener(new ActionListener() { 541 public void actionPerformed(ActionEvent e) { 542 JFileChooser fileChooser = new JFileChooser(new File(".")); 543 int retVal = fileChooser.showOpenDialog(dialog); 544 if (retVal == JFileChooser.APPROVE_OPTION) { 545 corePathField.setText(fileChooser.getSelectedFile().getPath()); 546 } 547 } 548 }); 549 browseExecPath.addActionListener(new ActionListener() { 550 public void actionPerformed(ActionEvent e) { 551 JFileChooser fileChooser = new JFileChooser(new File(".")); 552 int retVal = fileChooser.showOpenDialog(dialog); 553 if (retVal == JFileChooser.APPROVE_OPTION) { 554 execPathField.setText(fileChooser.getSelectedFile().getPath()); 555 } 556 } 557 }); 558 vbox = Box.createVerticalBox(); 559 vbox.add(browseCorePath); 560 vbox.add(browseExecPath); 561 hbox.add(vbox); 562 563 panel.add(hbox); 564 dialog.getContentPane().add(panel, BorderLayout.NORTH); 565 566 ActionListener attacher = new ActionListener() { 567 public void actionPerformed(ActionEvent e) { 568 dialog.setVisible(false); 569 desktop.remove(dialog); 570 workerThread.invokeLater(new Runnable() { 571 public void run() { 572 attach(execPathField.getText(), corePathField.getText()); 573 } 574 }); 575 } 576 }; 577 corePathField.addActionListener(attacher); 578 execPathField.addActionListener(attacher); 579 580 vbox = Box.createVerticalBox(); 581 panel = new JPanel(); 582 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 583 panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); 584 JTextArea ta = new JTextArea( 585 "Enter the full path names to the core file from a HotSpot process " + 586 "and the Java executable from which it came. The latter is typically " + 587 "located in the JDK/JRE directory under the directory " + 588 "jre/bin/<arch>/native_threads."); 589 ta.setLineWrap(true); 590 ta.setWrapStyleWord(true); 591 ta.setEditable(false); 592 ta.setBackground(panel.getBackground()); 593 panel.add(ta); 594 vbox.add(panel); 595 596 hbox = Box.createHorizontalBox(); 597 hbox.add(Box.createGlue()); 598 JButton button = new JButton("OK"); 599 button.addActionListener(attacher); 600 hbox.add(button); 601 hbox.add(Box.createHorizontalStrut(20)); 602 button = new JButton("Cancel"); 603 button.addActionListener(new ActionListener() { 604 public void actionPerformed(ActionEvent e) { 605 dialog.setVisible(false); 606 desktop.remove(dialog); 607 setMenuItemsEnabled(attachMenuItems, true); 608 } 609 }); 610 hbox.add(button); 611 hbox.add(Box.createGlue()); 612 panel = new JPanel(); 613 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 614 panel.add(hbox); 615 vbox.add(panel); 616 617 dialog.getContentPane().add(vbox, BorderLayout.SOUTH); 618 619 desktop.add(dialog); 620 dialog.setSize(500, 300); 621 GraphicsUtilities.centerInContainer(dialog); 622 dialog.show(); 623 corePathField.requestFocus(); 624 } 625 626 // FIXME: merge showAttachDialog, showOpenCoreFileDialog, showConnectDialog 627 private void showConnectDialog() { 628 // FIXME: create filtered text field which only accepts numbers 629 setMenuItemsEnabled(attachMenuItems, false); 630 final JInternalFrame dialog = new JInternalFrame("Connect to HotSpot Debug Server"); 631 dialog.getContentPane().setLayout(new BorderLayout()); 632 633 JPanel panel = new JPanel(); 634 panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); 635 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 636 dialog.setBackground(panel.getBackground()); 637 638 panel.add(new JLabel("Enter machine name:")); 639 final JTextField pidTextField = new JTextField(40); 640 ActionListener attacher = new ActionListener() { 641 public void actionPerformed(ActionEvent e) { 642 dialog.setVisible(false); 643 desktop.remove(dialog); 644 workerThread.invokeLater(new Runnable() { 645 public void run() { 646 connect(pidTextField.getText()); 647 } 648 }); 649 } 650 }; 651 652 pidTextField.addActionListener(attacher); 653 panel.add(pidTextField); 654 dialog.getContentPane().add(panel, BorderLayout.NORTH); 655 656 Box vbox = Box.createVerticalBox(); 657 panel = new JPanel(); 658 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 659 panel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); 660 JTextArea ta = new JTextArea( 661 "Enter the name of a machine on which the HotSpot \"Debug Server\" is " + 662 "running and is attached to a process or core file."); 663 ta.setLineWrap(true); 664 ta.setWrapStyleWord(true); 665 ta.setEditable(false); 666 ta.setBackground(panel.getBackground()); 667 panel.add(ta); 668 vbox.add(panel); 669 670 Box hbox = Box.createHorizontalBox(); 671 hbox.add(Box.createGlue()); 672 JButton button = new JButton("OK"); 673 button.addActionListener(attacher); 674 hbox.add(button); 675 hbox.add(Box.createHorizontalStrut(20)); 676 button = new JButton("Cancel"); 677 button.addActionListener(new ActionListener() { 678 public void actionPerformed(ActionEvent e) { 679 dialog.setVisible(false); 680 desktop.remove(dialog); 681 setMenuItemsEnabled(attachMenuItems, true); 682 } 683 }); 684 hbox.add(button); 685 hbox.add(Box.createGlue()); 686 panel = new JPanel(); 687 panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 688 panel.add(hbox); 689 vbox.add(panel); 690 691 dialog.getContentPane().add(vbox, BorderLayout.SOUTH); 692 693 desktop.add(dialog); 694 dialog.setSize(400, 300); 695 GraphicsUtilities.centerInContainer(dialog); 696 dialog.show(); 697 pidTextField.requestFocus(); 698 } 699 700 public void showThreadOopInspector(JavaThread thread) { 701 showInspector(new OopTreeNodeAdapter(thread.getThreadObj(), null)); 702 } 703 704 public void showInspector(SimpleTreeNode adapter) { 705 showPanel("Inspector", new Inspector(adapter), 1.0f, 0.65f); 706 } 707 708 public void showLiveness(Oop oop, LivenessPathList liveness) { 709 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 710 PrintStream tty = new PrintStream(bos); 711 int numPaths = liveness.size(); 712 for (int i = 0; i < numPaths; i++) { 713 tty.println("Path " + (i + 1) + " of " + numPaths + ":"); 714 liveness.get(i).printOn(tty); 715 } 716 JTextArea ta = new JTextArea(bos.toString()); 717 ta.setLineWrap(true); 718 ta.setWrapStyleWord(true); 719 ta.setEditable(false); 720 721 JPanel panel = new JPanel(); 722 panel.setLayout(new BorderLayout()); 723 724 JScrollPane scroller = new JScrollPane(); 725 scroller.getViewport().add(ta); 726 727 panel.add(scroller, BorderLayout.CENTER); 728 729 bos = new ByteArrayOutputStream(); 730 tty = new PrintStream(bos); 731 tty.print("Liveness result for "); 732 Oop.printOopValueOn(oop, tty); 733 734 JInternalFrame frame = new JInternalFrame(bos.toString()); 735 frame.setResizable(true); 736 frame.setClosable(true); 737 frame.setIconifiable(true); 738 frame.getContentPane().setLayout(new BorderLayout()); 739 frame.getContentPane().add(panel, BorderLayout.CENTER); 740 frame.pack(); 741 desktop.add(frame); 742 GraphicsUtilities.reshapeToAspectRatio(frame, 0.5f / 0.2f, 0.5f, frame.getParent().getSize()); 743 frame.show(); 744 } 745 746 private void fireComputeReversePtrs() { 747 // Possible this might have been computed elsewhere 748 if (VM.getVM().getRevPtrs() != null) { 749 computeRevPtrsMenuItem.setEnabled(false); 750 return; 751 } 752 753 workerThread.invokeLater(new Runnable() { 754 public void run() { 755 HeapProgress progress = new HeapProgress("Reverse Pointers Analysis"); 756 try { 757 ReversePtrsAnalysis analysis = new ReversePtrsAnalysis(); 758 analysis.setHeapProgressThunk(progress); 759 analysis.run(); 760 computeRevPtrsMenuItem.setEnabled(false); 761 } catch (OutOfMemoryError e) { 762 final String errMsg = formatMessage(e.toString(), 80); 763 SwingUtilities.invokeLater(new Runnable() { 764 public void run() { 765 JOptionPane.showInternalMessageDialog(desktop, 766 "Error computing reverse pointers:" + errMsg, 767 "Error", 768 JOptionPane.WARNING_MESSAGE); 769 } 770 }); 771 } finally { 772 // make sure the progress bar goes away 773 progress.heapIterationComplete(); 774 } 775 } 776 }); 777 } 778 779 // Simple struct containing signal information 780 class SignalInfo { 781 public int sigNum; 782 public String sigName; 783 } 784 785 // Need to have mutable vframe as well as visible memory panel 786 abstract class StackWalker implements Runnable { 787 protected JavaVFrame vf; 788 protected AnnotatedMemoryPanel annoPanel; 789 790 StackWalker(JavaVFrame vf, AnnotatedMemoryPanel annoPanel) { 791 this.vf = vf; 792 this.annoPanel = annoPanel; 793 } 794 } 795 796 public void showThreadStackMemory(final JavaThread thread) { 797 // dumpStack(thread); 798 JavaVFrame vframe = getLastJavaVFrame(thread); 799 if (vframe == null) { 800 JOptionPane.showInternalMessageDialog(desktop, 801 "Thread \"" + thread.getThreadName() + 802 "\" has no Java frames on its stack", 803 "Show Stack Memory", 804 JOptionPane.INFORMATION_MESSAGE); 805 return; 806 } 807 808 JInternalFrame stackFrame = new JInternalFrame("Stack Memory for " + thread.getThreadName()); 809 stackFrame.getContentPane().setLayout(new BorderLayout()); 810 stackFrame.setResizable(true); 811 stackFrame.setClosable(true); 812 stackFrame.setIconifiable(true); 813 final long addressSize = agent.getTypeDataBase().getAddressSize(); 814 boolean is64Bit = (addressSize == 8); 815 // This is somewhat of a hack to guess a thread's stack limits since the 816 // JavaThread doesn't support this functionality. However it is nice in that 817 // it locks us into the active region of the thread's stack and not its 818 // theoretical limits. 819 // 820 sun.jvm.hotspot.runtime.Frame tmpFrame = thread.getCurrentFrameGuess(); 821 Address sp = tmpFrame.getSP(); 822 Address starting = sp; 823 Address maxSP = starting; 824 Address minSP = starting; 825 RegisterMap tmpMap = thread.newRegisterMap(false); 826 while ((tmpFrame != null) && (!tmpFrame.isFirstFrame())) { 827 tmpFrame = tmpFrame.sender(tmpMap); 828 if (tmpFrame != null) { 829 sp = tmpFrame.getSP(); 830 if (sp != null) { 831 maxSP = AddressOps.max(maxSP, sp); 832 minSP = AddressOps.min(minSP, sp); 833 } 834 } 835 836 } 837 // It is useful to be able to see say +/- 8K on the current stack range 838 AnnotatedMemoryPanel annoMemPanel = new AnnotatedMemoryPanel(agent.getDebugger(), is64Bit, starting, 839 minSP.addOffsetTo(-8192), 840 maxSP.addOffsetTo( 8192)); 841 842 stackFrame.getContentPane().add(annoMemPanel, BorderLayout.CENTER); 843 desktop.add(stackFrame); 844 GraphicsUtilities.reshapeToAspectRatio(stackFrame, 4.0f / 3.0f, 0.85f, stackFrame.getParent().getSize()); 845 stackFrame.show(); 846 847 // Stackmap computation for interpreted frames is expensive; do 848 // all stackwalking work in another thread for better GUI 849 // responsiveness 850 workerThread.invokeLater(new StackWalker(vframe, annoMemPanel) { 851 public void run() { 852 Address startAddr = null; 853 854 // As this is a debugger, we want to provide potential crash 855 // information to the user, i.e., by marking signal handler frames 856 // on the stack. Since this system is currently targeted at 857 // annotating the Java frames (interpreted or compiled) on the 858 // stack and not, for example, "external" frames (note the current 859 // absence of a PC-to-symbol lookup mechanism at the Debugger 860 // level), we want to mark any Java frames which were interrupted 861 // by a signal. We do this by making two passes over the stack, 862 // one which finds signal handler frames and puts the parent 863 // frames in a table and one which finds Java frames and if they 864 // are in the table indicates that they were interrupted by a signal. 865 866 Map interruptedFrameMap = new HashMap(); 867 { 868 sun.jvm.hotspot.runtime.Frame tmpFrame = thread.getCurrentFrameGuess(); 869 RegisterMap tmpMap = thread.newRegisterMap(false); 870 while ((tmpFrame != null) && (!tmpFrame.isFirstFrame())) { 871 if (tmpFrame.isSignalHandlerFrameDbg()) { 872 // Add some information to the map that we can extract later 873 sun.jvm.hotspot.runtime.Frame interruptedFrame = tmpFrame.sender(tmpMap); 874 SignalInfo info = new SignalInfo(); 875 info.sigNum = tmpFrame.getSignalNumberDbg(); 876 info.sigName = tmpFrame.getSignalNameDbg(); 877 interruptedFrameMap.put(interruptedFrame, info); 878 } 879 tmpFrame = tmpFrame.sender(tmpMap); 880 } 881 } 882 883 while (vf != null) { 884 String anno = null; 885 JavaVFrame curVFrame = vf; 886 sun.jvm.hotspot.runtime.Frame curFrame = curVFrame.getFrame(); 887 Method interpreterFrameMethod = null; 888 889 if (curVFrame.isInterpretedFrame()) { 890 anno = "Interpreted frame"; 891 } else { 892 anno = "Compiled frame"; 893 if (curVFrame.isDeoptimized()) { 894 anno += " (deoptimized)"; 895 } 896 } 897 if (curVFrame.mayBeImpreciseDbg()) { 898 anno += "; information may be imprecise"; 899 } 900 901 if (curVFrame.isInterpretedFrame()) { 902 // Find the codelet 903 InterpreterCodelet codelet = VM.getVM().getInterpreter().getCodeletContaining(curFrame.getPC()); 904 String description = null; 905 if (codelet != null) { 906 description = codelet.getDescription(); 907 } 908 if (description == null) { 909 anno += "\n(Unknown interpreter codelet)"; 910 } else { 911 anno += "\nExecuting in codelet \"" + description + "\" at PC = " + curFrame.getPC(); 912 } 913 } else if (curVFrame.isCompiledFrame()) { 914 anno += "\nExecuting at PC = " + curFrame.getPC(); 915 } 916 917 if (startAddr == null) { 918 startAddr = curFrame.getSP(); 919 } 920 921 // FIXME: some compiled frames with empty oop map sets have been 922 // found (for example, Vector's inner Enumeration class, method 923 // "hasMoreElements"). Not sure yet why these cases are showing 924 // up -- should be possible (though unlikely) for safepoint code 925 // to patch the return instruction of these methods and then 926 // later attempt to get an oop map for that instruction. For 927 // now, we warn if we find such a method. 928 boolean shouldSkipOopMaps = false; 929 if (curVFrame.isCompiledFrame()) { 930 CodeBlob cb = VM.getVM().getCodeCache().findBlob(curFrame.getPC()); 931 OopMapSet maps = cb.getOopMaps(); 932 if ((maps == null) || (maps.getSize() == 0)) { 933 shouldSkipOopMaps = true; 934 } 935 } 936 937 // Add signal information to annotation if necessary 938 SignalInfo sigInfo = (SignalInfo) interruptedFrameMap.get(curFrame); 939 if (sigInfo != null) { 940 // This frame took a signal and we need to report it. 941 anno = (anno + "\n*** INTERRUPTED BY SIGNAL " + Integer.toString(sigInfo.sigNum) + 942 " (" + sigInfo.sigName + ")"); 943 } 944 945 JavaVFrame nextVFrame = curVFrame; 946 sun.jvm.hotspot.runtime.Frame nextFrame = curFrame; 947 do { 948 curVFrame = nextVFrame; 949 curFrame = nextFrame; 950 951 try { 952 Method method = curVFrame.getMethod(); 953 if (interpreterFrameMethod == null && curVFrame.isInterpretedFrame()) { 954 interpreterFrameMethod = method; 955 } 956 int bci = curVFrame.getBCI(); 957 String lineNumberAnno = ""; 958 if (method.hasLineNumberTable()) { 959 if ((bci == DebugInformationRecorder.SYNCHRONIZATION_ENTRY_BCI) || 960 (bci >= 0 && bci < method.getCodeSize())) { 961 lineNumberAnno = ", line " + method.getLineNumberFromBCI(bci); 962 } else { 963 lineNumberAnno = " (INVALID BCI)"; 964 } 965 } 966 anno += "\n" + method.getMethodHolder().getName().asString() + "." + 967 method.getName().asString() + method.getSignature().asString() + 968 "\n@bci " + bci + lineNumberAnno; 969 } catch (Exception e) { 970 anno += "\n(ERROR while iterating vframes for frame " + curFrame + ")"; 971 } 972 973 nextVFrame = curVFrame.javaSender(); 974 if (nextVFrame != null) { 975 nextFrame = nextVFrame.getFrame(); 976 } 977 } while (nextVFrame != null && nextFrame.equals(curFrame)); 978 979 if (shouldSkipOopMaps) { 980 anno = anno + "\nNOTE: null or empty OopMapSet found for this CodeBlob"; 981 } 982 983 if (curFrame.getFP() != null) { 984 annoPanel.addAnnotation(new Annotation(curFrame.getSP(), 985 curFrame.getFP(), 986 anno)); 987 } else { 988 // For C2, which has null frame pointers on x86/amd64/aarch64 989 CodeBlob cb = VM.getVM().getCodeCache().findBlob(curFrame.getPC()); 990 Address sp = curFrame.getSP(); 991 if (Assert.ASSERTS_ENABLED) { 992 Assert.that(cb.getFrameSize() > 0, "CodeBlob must have non-zero frame size"); 993 } 994 annoPanel.addAnnotation(new Annotation(sp, 995 sp.addOffsetTo(cb.getFrameSize()), 996 anno)); 997 } 998 999 // Add interpreter frame annotations 1000 if (curFrame.isInterpretedFrame()) { 1001 annoPanel.addAnnotation(new Annotation(curFrame.addressOfInterpreterFrameExpressionStack(), 1002 curFrame.addressOfInterpreterFrameTOS(), 1003 "Interpreter expression stack")); 1004 Address monBegin = curFrame.interpreterFrameMonitorBegin().address(); 1005 Address monEnd = curFrame.interpreterFrameMonitorEnd().address(); 1006 if (!monBegin.equals(monEnd)) { 1007 annoPanel.addAnnotation(new Annotation(monBegin, monEnd, 1008 "BasicObjectLocks")); 1009 } 1010 if (interpreterFrameMethod != null) { 1011 // The offset is just to get the right stack slots highlighted in the output 1012 int offset = 1; 1013 annoPanel.addAnnotation(new Annotation(curFrame.addressOfInterpreterFrameLocal(offset), 1014 curFrame.addressOfInterpreterFrameLocal((int) interpreterFrameMethod.getMaxLocals() + offset), 1015 "Interpreter locals area for frame with SP = " + curFrame.getSP())); 1016 } 1017 String methodAnno = "Interpreter frame Method*"; 1018 if (interpreterFrameMethod == null) { 1019 methodAnno += " (BAD OOP)"; 1020 } 1021 Address a = curFrame.addressOfInterpreterFrameMethod(); 1022 annoPanel.addAnnotation(new Annotation(a, a.addOffsetTo(addressSize), methodAnno)); 1023 a = curFrame.addressOfInterpreterFrameCPCache(); 1024 annoPanel.addAnnotation(new Annotation(a, a.addOffsetTo(addressSize), "Interpreter constant pool cache")); 1025 } 1026 1027 RegisterMap rm = (RegisterMap) vf.getRegisterMap().clone(); 1028 if (!shouldSkipOopMaps) { 1029 try { 1030 curFrame.oopsDo(new AddressVisitor() { 1031 public void visitAddress(Address addr) { 1032 if (Assert.ASSERTS_ENABLED) { 1033 Assert.that(addr.andWithMask(VM.getVM().getAddressSize() - 1) == null, 1034 "Address " + addr + "should have been aligned"); 1035 } 1036 OopHandle handle = addr.getOopHandleAt(0); 1037 addAnnotation(addr, handle); 1038 } 1039 1040 public void visitCompOopAddress(Address addr) { 1041 if (Assert.ASSERTS_ENABLED) { 1042 Assert.that(addr.andWithMask(VM.getVM().getAddressSize() - 1) == null, 1043 "Address " + addr + "should have been aligned"); 1044 } 1045 OopHandle handle = addr.getCompOopHandleAt(0); 1046 addAnnotation(addr, handle); 1047 } 1048 1049 public void addAnnotation(Address addr, OopHandle handle) { 1050 // Check contents 1051 String anno = "null oop"; 1052 if (handle != null) { 1053 // Find location 1054 CollectedHeap collHeap = VM.getVM().getUniverse().heap(); 1055 boolean bad = true; 1056 anno = "BAD OOP"; 1057 if (collHeap instanceof GenCollectedHeap) { 1058 GenCollectedHeap heap = (GenCollectedHeap) collHeap; 1059 for (int i = 0; i < heap.nGens(); i++) { 1060 if (heap.getGen(i).isIn(handle)) { 1061 if (i == 0) { 1062 anno = "NewGen "; 1063 } else if (i == 1) { 1064 anno = "OldGen "; 1065 } else { 1066 anno = "Gen " + i + " "; 1067 } 1068 bad = false; 1069 break; 1070 } 1071 } 1072 1073 } else if (collHeap instanceof ParallelScavengeHeap) { 1074 ParallelScavengeHeap heap = (ParallelScavengeHeap) collHeap; 1075 if (heap.youngGen().isIn(handle)) { 1076 anno = "PSYoungGen "; 1077 bad = false; 1078 } else if (heap.oldGen().isIn(handle)) { 1079 anno = "PSOldGen "; 1080 bad = false; 1081 } 1082 } else { 1083 // Optimistically assume the oop isn't bad 1084 anno = "[Unknown generation] "; 1085 bad = false; 1086 } 1087 1088 if (!bad) { 1089 try { 1090 Oop oop = VM.getVM().getObjectHeap().newOop(handle); 1091 if (oop instanceof Instance) { 1092 // Java-level objects always have workable names 1093 anno = anno + oop.getKlass().getName().asString(); 1094 } else { 1095 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1096 Oop.printOopValueOn(oop, new PrintStream(bos)); 1097 anno = anno + bos.toString(); 1098 } 1099 } 1100 catch (AddressException e) { 1101 anno += "CORRUPT OOP"; 1102 } 1103 catch (NullPointerException e) { 1104 anno += "CORRUPT OOP (null pointer)"; 1105 } 1106 } 1107 } 1108 1109 annoPanel.addAnnotation(new Annotation(addr, addr.addOffsetTo(addressSize), anno)); 1110 } 1111 }, rm); 1112 } catch (Exception e) { 1113 System.err.println("Error while performing oopsDo for frame " + curFrame); 1114 e.printStackTrace(); 1115 } 1116 } 1117 1118 vf = nextVFrame; 1119 } 1120 1121 // This used to paint as we walked the frames. This caused the display to be refreshed 1122 // enough to be annoying on remote displays. It also would cause the annotations to 1123 // be displayed in varying order which caused some annotations to overwrite others 1124 // depending on the races between painting and adding annotations. This latter problem 1125 // still exists to some degree but moving this code here definitely seems to reduce it 1126 annoPanel.makeVisible(startAddr); 1127 annoPanel.repaint(); 1128 } 1129 }); 1130 } 1131 1132 // Attach to existing JVMDebugger, which should be already attached to a core/process. 1133 private void attach(JVMDebugger d) { 1134 attached = true; 1135 showThreadsDialog(); 1136 } 1137 1138 /** NOTE we are in a different thread here than either the main 1139 thread or the Swing/AWT event handler thread, so we must be very 1140 careful when creating or removing widgets */ 1141 private void attach(String pidText) { 1142 try { 1143 this.pidText = pidText; 1144 pid = Integer.parseInt(pidText); 1145 } 1146 catch (NumberFormatException e) { 1147 SwingUtilities.invokeLater(new Runnable() { 1148 public void run() { 1149 setMenuItemsEnabled(attachMenuItems, true); 1150 JOptionPane.showInternalMessageDialog(desktop, 1151 "Unable to parse process ID \"" + HSDB.this.pidText + "\".\nPlease enter a number.", 1152 "Parse error", 1153 JOptionPane.WARNING_MESSAGE); 1154 } 1155 }); 1156 return; 1157 } 1158 1159 // Try to attach to this process 1160 Runnable remover = new Runnable() { 1161 public void run() { 1162 attachWaitDialog.setVisible(false); 1163 desktop.remove(attachWaitDialog); 1164 attachWaitDialog = null; 1165 } 1166 }; 1167 1168 try { 1169 SwingUtilities.invokeLater(new Runnable() { 1170 public void run() { 1171 JOptionPane pane = new JOptionPane("Attaching to process " + pid + ", please wait...", JOptionPane.INFORMATION_MESSAGE); 1172 pane.setOptions(new Object[] {}); 1173 attachWaitDialog = pane.createInternalFrame(desktop, "Attaching to Process"); 1174 attachWaitDialog.show(); 1175 } 1176 }); 1177 1178 // FIXME: display exec'd debugger's output messages during this 1179 // lengthy call 1180 agent.attach(pid); 1181 if (agent.getDebugger().hasConsole()) { 1182 showDbgConsoleMenuItem.setEnabled(true); 1183 } 1184 attached = true; 1185 SwingUtilities.invokeLater(remover); 1186 } 1187 catch (DebuggerException e) { 1188 SwingUtilities.invokeLater(remover); 1189 final String errMsg = formatMessage(e.getMessage(), 80); 1190 SwingUtilities.invokeLater(new Runnable() { 1191 public void run() { 1192 setMenuItemsEnabled(attachMenuItems, true); 1193 JOptionPane.showInternalMessageDialog(desktop, 1194 "Unable to connect to process ID " + pid + ":\n\n" + errMsg, 1195 "Unable to Connect", 1196 JOptionPane.WARNING_MESSAGE); 1197 } 1198 }); 1199 agent.detach(); 1200 return; 1201 } 1202 1203 // OK, the VM should be available. Create the Threads dialog. 1204 showThreadsDialog(); 1205 } 1206 1207 /** NOTE we are in a different thread here than either the main 1208 thread or the Swing/AWT event handler thread, so we must be very 1209 careful when creating or removing widgets */ 1210 private void attach(final String executablePath, final String corePath) { 1211 // Try to open this core file 1212 Runnable remover = new Runnable() { 1213 public void run() { 1214 attachWaitDialog.setVisible(false); 1215 desktop.remove(attachWaitDialog); 1216 attachWaitDialog = null; 1217 } 1218 }; 1219 1220 try { 1221 SwingUtilities.invokeLater(new Runnable() { 1222 public void run() { 1223 JOptionPane pane = new JOptionPane("Opening core file, please wait...", JOptionPane.INFORMATION_MESSAGE); 1224 pane.setOptions(new Object[] {}); 1225 attachWaitDialog = pane.createInternalFrame(desktop, "Opening Core File"); 1226 attachWaitDialog.show(); 1227 } 1228 }); 1229 1230 // FIXME: display exec'd debugger's output messages during this 1231 // lengthy call 1232 agent.attach(executablePath, corePath); 1233 if (agent.getDebugger().hasConsole()) { 1234 showDbgConsoleMenuItem.setEnabled(true); 1235 } 1236 attached = true; 1237 SwingUtilities.invokeLater(remover); 1238 } 1239 catch (DebuggerException e) { 1240 SwingUtilities.invokeLater(remover); 1241 final String errMsg = formatMessage(e.getMessage(), 80); 1242 SwingUtilities.invokeLater(new Runnable() { 1243 public void run() { 1244 setMenuItemsEnabled(attachMenuItems, true); 1245 JOptionPane.showInternalMessageDialog(desktop, 1246 "Unable to open core file\n" + corePath + ":\n\n" + errMsg, 1247 "Unable to Open Core File", 1248 JOptionPane.WARNING_MESSAGE); 1249 } 1250 }); 1251 agent.detach(); 1252 return; 1253 } 1254 1255 // OK, the VM should be available. Create the Threads dialog. 1256 showThreadsDialog(); 1257 } 1258 1259 /** NOTE we are in a different thread here than either the main 1260 thread or the Swing/AWT event handler thread, so we must be very 1261 careful when creating or removing widgets */ 1262 private void connect(final String remoteMachineName) { 1263 // Try to open this core file 1264 Runnable remover = new Runnable() { 1265 public void run() { 1266 attachWaitDialog.setVisible(false); 1267 desktop.remove(attachWaitDialog); 1268 attachWaitDialog = null; 1269 } 1270 }; 1271 1272 try { 1273 SwingUtilities.invokeLater(new Runnable() { 1274 public void run() { 1275 JOptionPane pane = new JOptionPane("Connecting to debug server, please wait...", JOptionPane.INFORMATION_MESSAGE); 1276 pane.setOptions(new Object[] {}); 1277 attachWaitDialog = pane.createInternalFrame(desktop, "Connecting to Debug Server"); 1278 attachWaitDialog.show(); 1279 } 1280 }); 1281 1282 agent.attach(remoteMachineName); 1283 if (agent.getDebugger().hasConsole()) { 1284 showDbgConsoleMenuItem.setEnabled(true); 1285 } 1286 attached = true; 1287 SwingUtilities.invokeLater(remover); 1288 } 1289 catch (DebuggerException e) { 1290 SwingUtilities.invokeLater(remover); 1291 final String errMsg = formatMessage(e.getMessage(), 80); 1292 SwingUtilities.invokeLater(new Runnable() { 1293 public void run() { 1294 setMenuItemsEnabled(attachMenuItems, true); 1295 JOptionPane.showInternalMessageDialog(desktop, 1296 "Unable to connect to machine \"" + remoteMachineName + "\":\n\n" + errMsg, 1297 "Unable to Connect", 1298 JOptionPane.WARNING_MESSAGE); 1299 } 1300 }); 1301 agent.detach(); 1302 return; 1303 } 1304 1305 // OK, the VM should be available. Create the Threads dialog. 1306 showThreadsDialog(); 1307 } 1308 1309 private void detachDebugger() { 1310 if (!attached) { 1311 return; 1312 } 1313 agent.detach(); 1314 attached = false; 1315 } 1316 1317 private void detach() { 1318 detachDebugger(); 1319 attachWaitDialog = null; 1320 threadsFrame = null; 1321 consoleFrame = null; 1322 setMenuItemsEnabled(attachMenuItems, true); 1323 setMenuItemsEnabled(detachMenuItems, false); 1324 toolsMenu.setEnabled(false); 1325 showDbgConsoleMenuItem.setEnabled(false); 1326 // FIXME: is this sufficient, or will I have to do anything else 1327 // to the components to kill them off? What about WorkerThreads? 1328 desktop.removeAll(); 1329 desktop.invalidate(); 1330 desktop.validate(); 1331 desktop.repaint(); 1332 } 1333 1334 /** NOTE that this is called from another thread than the main or 1335 Swing thread and we have to be careful about synchronization */ 1336 private void showThreadsDialog() { 1337 SwingUtilities.invokeLater(new Runnable() { 1338 public void run() { 1339 threadsFrame = new JInternalFrame("Java Threads"); 1340 threadsFrame.setResizable(true); 1341 threadsFrame.setIconifiable(true); 1342 JavaThreadsPanel threadsPanel = new JavaThreadsPanel(); 1343 threadsPanel.addPanelListener(HSDB.this); 1344 threadsFrame.getContentPane().add(threadsPanel); 1345 threadsFrame.setSize(500, 300); 1346 threadsFrame.pack(); 1347 desktop.add(threadsFrame); 1348 GraphicsUtilities.moveToInContainer(threadsFrame, 0.75f, 0.25f, 0, 20); 1349 threadsFrame.show(); 1350 setMenuItemsEnabled(attachMenuItems, false); 1351 setMenuItemsEnabled(detachMenuItems, true); 1352 toolsMenu.setEnabled(true); 1353 VM.registerVMInitializedObserver(new Observer() { 1354 public void update(Observable o, Object data) { 1355 computeRevPtrsMenuItem.setEnabled(true); 1356 } 1357 }); 1358 } 1359 }); 1360 } 1361 1362 private void showObjectHistogram() { 1363 sun.jvm.hotspot.oops.ObjectHistogram histo = new sun.jvm.hotspot.oops.ObjectHistogram(); 1364 ObjectHistogramCleanupThunk cleanup = 1365 new ObjectHistogramCleanupThunk(histo); 1366 doHeapIteration("Object Histogram", 1367 "Generating histogram...", 1368 histo, 1369 cleanup); 1370 } 1371 1372 class ObjectHistogramCleanupThunk implements CleanupThunk { 1373 sun.jvm.hotspot.oops.ObjectHistogram histo; 1374 1375 ObjectHistogramCleanupThunk(sun.jvm.hotspot.oops.ObjectHistogram histo) { 1376 this.histo = histo; 1377 } 1378 1379 public void heapIterationComplete() { 1380 SwingUtilities.invokeLater(new Runnable() { 1381 public void run() { 1382 JInternalFrame histoFrame = new JInternalFrame("Object Histogram"); 1383 histoFrame.setResizable(true); 1384 histoFrame.setClosable(true); 1385 histoFrame.setIconifiable(true); 1386 histoFrame.getContentPane().setLayout(new BorderLayout()); 1387 ObjectHistogramPanel panel = new ObjectHistogramPanel(histo); 1388 panel.addPanelListener(HSDB.this); 1389 histoFrame.getContentPane().add(panel); 1390 desktop.add(histoFrame); 1391 GraphicsUtilities.reshapeToAspectRatio(histoFrame, 4.0f / 3.0f, 0.6f, 1392 histoFrame.getParent().getSize()); 1393 GraphicsUtilities.centerInContainer(histoFrame); 1394 histoFrame.show(); 1395 } 1396 }); 1397 } 1398 } 1399 1400 public void showObjectsOfType(Klass type) { 1401 FindObjectByType finder = new FindObjectByType(type); 1402 FindObjectByTypeCleanupThunk cleanup = 1403 new FindObjectByTypeCleanupThunk(finder); 1404 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1405 type.printValueOn(new PrintStream(bos)); 1406 String typeName = bos.toString(); 1407 doHeapIteration("Show Objects Of Type", 1408 "Finding instances of \"" + typeName + "\"", 1409 finder, 1410 cleanup); 1411 } 1412 1413 class FindObjectByTypeCleanupThunk implements CleanupThunk { 1414 FindObjectByType finder; 1415 1416 FindObjectByTypeCleanupThunk(FindObjectByType finder) { 1417 this.finder = finder; 1418 } 1419 1420 public void heapIterationComplete() { 1421 SwingUtilities.invokeLater(new Runnable() { 1422 public void run() { 1423 JInternalFrame finderFrame = new JInternalFrame("Show Objects of Type"); 1424 finderFrame.getContentPane().setLayout(new BorderLayout()); 1425 finderFrame.setResizable(true); 1426 finderFrame.setClosable(true); 1427 finderFrame.setIconifiable(true); 1428 ObjectListPanel panel = new ObjectListPanel(finder.getResults(), 1429 new HeapProgress("Reverse Pointers Analysis")); 1430 panel.addPanelListener(HSDB.this); 1431 finderFrame.getContentPane().add(panel); 1432 desktop.add(finderFrame); 1433 GraphicsUtilities.reshapeToAspectRatio(finderFrame, 4.0f / 3.0f, 0.6f, 1434 finderFrame.getParent().getSize()); 1435 GraphicsUtilities.centerInContainer(finderFrame); 1436 finderFrame.show(); 1437 } 1438 }); 1439 } 1440 } 1441 1442 private void showDebuggerConsole() { 1443 if (consoleFrame == null) { 1444 consoleFrame = new JInternalFrame("Debugger Console"); 1445 consoleFrame.setResizable(true); 1446 consoleFrame.setClosable(true); 1447 consoleFrame.setIconifiable(true); 1448 consoleFrame.getContentPane().setLayout(new BorderLayout()); 1449 consoleFrame.getContentPane().add(new DebuggerConsolePanel(agent.getDebugger()), BorderLayout.CENTER); 1450 GraphicsUtilities.reshapeToAspectRatio(consoleFrame, 5.0f, 0.9f, desktop.getSize()); 1451 } 1452 if (consoleFrame.getParent() == null) { 1453 desktop.add(consoleFrame); 1454 } 1455 consoleFrame.setVisible(true); 1456 consoleFrame.show(); 1457 consoleFrame.getContentPane().getComponent(0).requestFocus(); 1458 } 1459 1460 private void showConsole() { 1461 CommandProcessor.DebuggerInterface di = new CommandProcessor.DebuggerInterface() { 1462 public HotSpotAgent getAgent() { 1463 return agent; 1464 } 1465 public boolean isAttached() { 1466 return attached; 1467 } 1468 public void attach(String pid) { 1469 attach(pid); 1470 } 1471 public void attach(String java, String core) { 1472 } 1473 public void detach() { 1474 detachDebugger(); 1475 } 1476 public void reattach() { 1477 if (attached) { 1478 detachDebugger(); 1479 } 1480 if (pidText != null) { 1481 attach(pidText); 1482 } else { 1483 attach(execPath, coreFilename); 1484 } 1485 } 1486 }; 1487 1488 showPanel("Command Line", new CommandProcessorPanel(new CommandProcessor(di, null, null, null))); 1489 } 1490 1491 private void showFindByQueryPanel() { 1492 showPanel("Find Object by Query", new FindByQueryPanel()); 1493 } 1494 1495 private void showFindPanel() { 1496 showPanel("Find Pointer", new FindPanel()); 1497 } 1498 1499 private void showFindInHeapPanel() { 1500 showPanel("Find Address In Heap", new FindInHeapPanel()); 1501 } 1502 1503 private void showFindInCodeCachePanel() { 1504 showPanel("Find Address In Code Cache", new FindInCodeCachePanel()); 1505 } 1506 1507 private void showHeapParametersPanel() { 1508 showPanel("Heap Parameters", new HeapParametersPanel()); 1509 } 1510 1511 public void showThreadInfo(final JavaThread thread) { 1512 showPanel("Info for " + thread.getThreadName(), new ThreadInfoPanel(thread)); 1513 } 1514 1515 public void showJavaStackTrace(final JavaThread thread) { 1516 JavaStackTracePanel jstp = new JavaStackTracePanel(); 1517 showPanel("Java stack trace for " + thread.getThreadName(), jstp); 1518 jstp.setJavaThread(thread); 1519 } 1520 1521 private void showDeadlockDetectionPanel() { 1522 showPanel("Deadlock Detection", new DeadlockDetectionPanel()); 1523 } 1524 1525 private void showMonitorCacheDumpPanel() { 1526 showPanel("Monitor Cache Dump", new MonitorCacheDumpPanel()); 1527 } 1528 1529 public void showClassBrowser() { 1530 final JInternalFrame progressFrame = new JInternalFrame("Class Browser"); 1531 progressFrame.setResizable(true); 1532 progressFrame.setClosable(true); 1533 progressFrame.setIconifiable(true); 1534 progressFrame.getContentPane().setLayout(new BorderLayout()); 1535 final ProgressBarPanel bar = new ProgressBarPanel("Generating class list .."); 1536 bar.setIndeterminate(true); 1537 progressFrame.getContentPane().add(bar, BorderLayout.CENTER); 1538 desktop.add(progressFrame); 1539 progressFrame.pack(); 1540 GraphicsUtilities.centerInContainer(progressFrame); 1541 progressFrame.show(); 1542 1543 workerThread.invokeLater(new Runnable() { 1544 public void run() { 1545 HTMLGenerator htmlGen = new HTMLGenerator(); 1546 InstanceKlass[] klasses = SystemDictionaryHelper.getAllInstanceKlasses(); 1547 final String htmlText = htmlGen.genHTMLForKlassNames(klasses); 1548 SwingUtilities.invokeLater(new Runnable() { 1549 public void run() { 1550 JInternalFrame cbFrame = new JInternalFrame("Class Browser"); 1551 cbFrame.getContentPane().setLayout(new BorderLayout()); 1552 cbFrame.setResizable(true); 1553 cbFrame.setClosable(true); 1554 cbFrame.setIconifiable(true); 1555 ClassBrowserPanel cbPanel = new ClassBrowserPanel(); 1556 cbFrame.getContentPane().add(cbPanel, BorderLayout.CENTER); 1557 desktop.remove(progressFrame); 1558 desktop.repaint(); 1559 desktop.add(cbFrame); 1560 GraphicsUtilities.reshapeToAspectRatio(cbFrame, 1.25f, 0.85f, 1561 cbFrame.getParent().getSize()); 1562 cbFrame.show(); 1563 cbPanel.setClassesText(htmlText); 1564 } 1565 }); 1566 } 1567 }); 1568 } 1569 1570 public void showCodeViewer() { 1571 showPanel("Code Viewer", new CodeViewerPanel(), 1.25f, 0.85f); 1572 } 1573 1574 public void showCodeViewer(final Address address) { 1575 final CodeViewerPanel panel = new CodeViewerPanel(); 1576 showPanel("Code Viewer", panel, 1.25f, 0.85f); 1577 SwingUtilities.invokeLater(new Runnable() { 1578 public void run() { 1579 panel.viewAddress(address); 1580 } 1581 }); 1582 1583 } 1584 1585 public void showMemoryViewer() { 1586 showPanel("Memory Viewer", new MemoryViewer(agent.getDebugger(), agent.getTypeDataBase().getAddressSize() == 8)); 1587 } 1588 1589 public void showCommandLineFlags() { 1590 showPanel("Command Line Flags", new VMFlagsPanel()); 1591 } 1592 1593 public void showVMVersion() { 1594 showPanel("VM Version Info", new VMVersionInfoPanel()); 1595 } 1596 1597 public void showSystemProperties() { 1598 showPanel("System Properties", new SysPropsPanel()); 1599 } 1600 1601 private void showPanel(String name, JPanel panel) { 1602 showPanel(name, panel, 5.0f / 3.0f, 0.4f); 1603 } 1604 1605 private void showPanel(String name, JPanel panel, float aspectRatio, float fillRatio) { 1606 JInternalFrame frame = new JInternalFrame(name); 1607 frame.getContentPane().setLayout(new BorderLayout()); 1608 frame.setResizable(true); 1609 frame.setClosable(true); 1610 frame.setIconifiable(true); 1611 frame.setMaximizable(true); 1612 frame.getContentPane().add(panel, BorderLayout.CENTER); 1613 desktop.add(frame); 1614 GraphicsUtilities.reshapeToAspectRatio(frame, aspectRatio, fillRatio, frame.getParent().getSize()); 1615 GraphicsUtilities.randomLocation(frame); 1616 frame.show(); 1617 if (panel instanceof SAPanel) { 1618 ((SAPanel)panel).addPanelListener(this); 1619 } 1620 } 1621 1622 //-------------------------------------------------------------------------------- 1623 // Framework for heap iteration with progress bar 1624 // 1625 1626 interface CleanupThunk { 1627 public void heapIterationComplete(); 1628 } 1629 1630 class HeapProgress implements HeapProgressThunk { 1631 private JInternalFrame frame; 1632 private ProgressBarPanel bar; 1633 private String windowTitle; 1634 private String progressBarTitle; 1635 private CleanupThunk cleanup; 1636 1637 HeapProgress(String windowTitle) { 1638 this(windowTitle, "Percentage of heap visited", null); 1639 } 1640 1641 HeapProgress(String windowTitle, String progressBarTitle) { 1642 this(windowTitle, progressBarTitle, null); 1643 } 1644 1645 HeapProgress(String windowTitle, String progressBarTitle, CleanupThunk cleanup) { 1646 this.windowTitle = windowTitle; 1647 this.progressBarTitle = progressBarTitle; 1648 this.cleanup = cleanup; 1649 } 1650 1651 public void heapIterationFractionUpdate(final double fractionOfHeapVisited) { 1652 if (frame == null) { 1653 SwingUtilities.invokeLater(new Runnable() { 1654 public void run() { 1655 frame = new JInternalFrame(windowTitle); 1656 frame.setResizable(true); 1657 frame.setIconifiable(true); 1658 frame.getContentPane().setLayout(new BorderLayout()); 1659 bar = new ProgressBarPanel(progressBarTitle); 1660 frame.getContentPane().add(bar, BorderLayout.CENTER); 1661 desktop.add(frame); 1662 frame.pack(); 1663 GraphicsUtilities.constrainToSize(frame, frame.getParent().getSize()); 1664 GraphicsUtilities.centerInContainer(frame); 1665 frame.show(); 1666 } 1667 }); 1668 } 1669 1670 SwingUtilities.invokeLater(new Runnable() { 1671 public void run() { 1672 bar.setValue(fractionOfHeapVisited); 1673 } 1674 }); 1675 } 1676 1677 public void heapIterationComplete() { 1678 SwingUtilities.invokeLater(new Runnable() { 1679 public void run() { 1680 desktop.remove(frame); 1681 desktop.repaint(); 1682 if (VM.getVM().getRevPtrs() != null) { 1683 // Ended up computing reverse pointers as a side-effect 1684 computeRevPtrsMenuItem.setEnabled(false); 1685 } 1686 } 1687 }); 1688 1689 if (cleanup != null) { 1690 cleanup.heapIterationComplete(); 1691 } 1692 } 1693 } 1694 1695 class VisitHeap implements Runnable { 1696 HeapVisitor visitor; 1697 1698 VisitHeap(HeapVisitor visitor) { 1699 this.visitor = visitor; 1700 } 1701 1702 public void run() { 1703 VM.getVM().getObjectHeap().iterate(visitor); 1704 } 1705 } 1706 1707 private void doHeapIteration(String frameTitle, 1708 String progressBarText, 1709 HeapVisitor visitor, 1710 CleanupThunk cleanup) { 1711 sun.jvm.hotspot.oops.ObjectHistogram histo = new sun.jvm.hotspot.oops.ObjectHistogram(); 1712 HeapProgress progress = new HeapProgress(frameTitle, 1713 progressBarText, 1714 cleanup); 1715 HeapVisitor progVisitor = new ProgressiveHeapVisitor(visitor, progress); 1716 workerThread.invokeLater(new VisitHeap(progVisitor)); 1717 } 1718 1719 //-------------------------------------------------------------------------------- 1720 // Stack trace helper 1721 // 1722 1723 private static JavaVFrame getLastJavaVFrame(JavaThread cur) { 1724 RegisterMap regMap = cur.newRegisterMap(true); 1725 sun.jvm.hotspot.runtime.Frame f = cur.getCurrentFrameGuess(); 1726 if (f == null) return null; 1727 boolean imprecise = true; 1728 if (f.isInterpretedFrame() && !f.isInterpretedFrameValid()) { 1729 System.err.println("Correcting for invalid interpreter frame"); 1730 f = f.sender(regMap); 1731 imprecise = false; 1732 } 1733 VFrame vf = VFrame.newVFrame(f, regMap, cur, true, imprecise); 1734 if (vf == null) { 1735 System.err.println(" (Unable to create vframe for topmost frame guess)"); 1736 return null; 1737 } 1738 if (vf.isJavaFrame()) { 1739 return (JavaVFrame) vf; 1740 } 1741 return (JavaVFrame) vf.javaSender(); 1742 } 1743 1744 // Internal routine for debugging 1745 private static void dumpStack(JavaThread cur) { 1746 RegisterMap regMap = cur.newRegisterMap(true); 1747 sun.jvm.hotspot.runtime.Frame f = cur.getCurrentFrameGuess(); 1748 PrintStream tty = System.err; 1749 while (f != null) { 1750 tty.print("Found "); 1751 if (f.isInterpretedFrame()) { tty.print("interpreted"); } 1752 else if (f.isCompiledFrame()) { tty.print("compiled"); } 1753 else if (f.isEntryFrame()) { tty.print("entry"); } 1754 else if (f.isNativeFrame()) { tty.print("native"); } 1755 else if (f.isRuntimeFrame()) { tty.print("runtime"); } 1756 else { tty.print("external"); } 1757 tty.print(" frame with PC = " + f.getPC() + ", SP = " + f.getSP() + ", FP = " + f.getFP()); 1758 if (f.isSignalHandlerFrameDbg()) { 1759 tty.print(" (SIGNAL HANDLER)"); 1760 } 1761 tty.println(); 1762 1763 if (!f.isFirstFrame()) { 1764 f = f.sender(regMap); 1765 } else { 1766 f = null; 1767 } 1768 } 1769 } 1770 1771 //-------------------------------------------------------------------------------- 1772 // Component utilities 1773 // 1774 1775 private static JMenuItem createMenuItem(String name, ActionListener l) { 1776 JMenuItem item = new JMenuItem(name); 1777 item.addActionListener(l); 1778 return item; 1779 } 1780 1781 /** Punctuates the given string with \n's where necessary to not 1782 exceed the given number of characters per line. Strips 1783 extraneous whitespace. */ 1784 private String formatMessage(String message, int charsPerLine) { 1785 StringBuffer buf = new StringBuffer(message.length()); 1786 StringTokenizer tokenizer = new StringTokenizer(message); 1787 int curLineLength = 0; 1788 while (tokenizer.hasMoreTokens()) { 1789 String tok = tokenizer.nextToken(); 1790 if (curLineLength + tok.length() > charsPerLine) { 1791 buf.append('\n'); 1792 curLineLength = 0; 1793 } else { 1794 if (curLineLength != 0) { 1795 buf.append(' '); 1796 ++curLineLength; 1797 } 1798 } 1799 buf.append(tok); 1800 curLineLength += tok.length(); 1801 } 1802 return buf.toString(); 1803 } 1804 1805 private void setMenuItemsEnabled(java.util.List items, boolean enabled) { 1806 for (Iterator iter = items.iterator(); iter.hasNext(); ) { 1807 ((JMenuItem) iter.next()).setEnabled(enabled); 1808 } 1809 } 1810 }