jdk/src/share/classes/com/sun/tools/example/debug/gui/StackTraceTool.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.gui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;  // Must import explicitly due to conflict with javax.awt.List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.tree.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.example.debug.bdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class StackTraceTool extends JPanel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private Environment env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private ExecutionManager runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private ContextManager context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private ThreadInfo tinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private JList list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private ListModel stackModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public StackTraceTool(Environment env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        super(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.env = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.runtime = env.getExecutionManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.context = env.getContextManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        stackModel = new DefaultListModel();  // empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        list = new JList(stackModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        list.setCellRenderer(new StackFrameRenderer());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        JScrollPane listView = new JScrollPane(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        add(listView);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Create listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        StackTraceToolListener listener = new StackTraceToolListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        context.addContextListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        list.addListSelectionListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        //### remove listeners on exit!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private class StackTraceToolListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        implements ContextListener, ListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // ContextListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // If the user selects a new current frame, display it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        //### I suspect we handle the case badly that the VM is not interrupted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        public void currentFrameChanged(CurrentFrameChangedEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // If the current frame of the thread appearing in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // view is changed, move the selection to track it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            int frameIndex = e.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            ThreadInfo ti = e.getThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (e.getInvalidate() || tinfo != ti) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                tinfo = ti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                showStack(ti, frameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (frameIndex < stackModel.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    list.setSelectedIndex(frameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    list.ensureIndexIsVisible(frameIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // ListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int index = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                //### should use listener?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    context.setCurrentFrameIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                } catch (VMNotInterruptedException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private class StackFrameRenderer extends DefaultListCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public Component getListCellRendererComponent(JList list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                                      Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                                      int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                                      boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                                      boolean cellHasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            //### We should indicate the current thread independently of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            //### selection, e.g., with an icon, because the user may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            //### the selection graphically without affecting the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            //### thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            super.getListCellRendererComponent(list, value, index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                               isSelected, cellHasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                this.setText("<unavailable>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                StackFrame frame = (StackFrame)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                Location loc = frame.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                Method meth = loc.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                String methName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    meth.declaringType().name() + '.' + meth.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                String position = "";
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   142
                if (meth.isNative()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    position = " (native method)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } else if (loc.lineNumber() != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    position = ":" + loc.lineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    long pc = loc.codeIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (pc != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        position = ", pc = " + pc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // Indices are presented to the user starting from 1, not 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                this.setText("[" + (index+1) +"] " + methName + position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // Point this view at the given thread and frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private void showStack(ThreadInfo tinfo, int selectFrame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        StackTraceListModel model = new StackTraceListModel(tinfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        stackModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        list.setModel(stackModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        list.setSelectedIndex(selectFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        list.ensureIndexIsVisible(selectFrame);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private static class StackTraceListModel extends AbstractListModel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        private final ThreadInfo tinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        public StackTraceListModel(ThreadInfo tinfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            this.tinfo = tinfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        public Object getElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return tinfo == null? null : tinfo.getFrame(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                //### Is this the right way to handle this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                //### Would happen if user scrolled stack trace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                //### while not interrupted -- should probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                //### block user interaction in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                return tinfo == null? 1 : tinfo.getFrameCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (VMNotInterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                //### Is this the right way to handle this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}