jdk/src/share/classes/com/sun/tools/jdi/StackFrameImpl.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.jdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class StackFrameImpl extends MirrorImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                            implements StackFrame, ThreadListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /* Once false, frame should not be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * access synchronized on (vm.state())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private boolean isValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final ThreadReferenceImpl thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private final long id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private final Location location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Map<String, LocalVariable> visibleVariables =  null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private ObjectReference thisObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    StackFrameImpl(VirtualMachine vm, ThreadReferenceImpl thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                   long id, Location location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super(vm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.thread = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.location = location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        thread.addListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * ThreadListener implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Must be synchronized since we must protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * sending defunct (isValid == false) stack ids to the back-end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public boolean threadResumable(ThreadAction action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                isValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                return false;   /* remove this stack frame as a listener */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                throw new InternalException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                  "Invalid stack frame thread listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    void validateStackFrame() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (!isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new InvalidStackFrameException("Thread has been resumed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Return the frame location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Need not be synchronized since it cannot be provably stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Location location() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return the thread holding the frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Need not be synchronized since it cannot be provably stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public ThreadReference thread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if ((obj != null) && (obj instanceof StackFrameImpl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            StackFrameImpl other = (StackFrameImpl)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return (id == other.id) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                   (thread().equals(other.thread())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                   (location().equals(other.location())) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    super.equals(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return (thread().hashCode() << 4) + ((int)id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public ObjectReference thisObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        MethodImpl currentMethod = (MethodImpl)location.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (currentMethod.isStatic() || currentMethod.isNative()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (thisObject == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                PacketStream ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                /* protect against defunct frame id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    ps = JDWP.StackFrame.ThisObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                      enqueueCommand(vm, thread, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                /* actually get it, now that order is guaranteed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    thisObject = JDWP.StackFrame.ThisObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                      waitForReply(vm, ps).objectThis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    case JDWP.Error.INVALID_FRAMEID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    case JDWP.Error.INVALID_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        throw new InvalidStackFrameException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return thisObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Build the visible variable map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Need not be synchronized since it cannot be provably stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private void createVisibleVariables() throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (visibleVariables == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            List<LocalVariable> allVariables = location.method().variables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            Map<String, LocalVariable> map = new HashMap<String, LocalVariable>(allVariables.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for (LocalVariable variable : allVariables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                String name = variable.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (variable.isVisible(this)) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   165
                    LocalVariable existing = map.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if ((existing == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        ((LocalVariableImpl)variable).hides(existing)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        map.put(name, variable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            visibleVariables = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Return the list of visible variable in the frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Need not be synchronized since it cannot be provably stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public List<LocalVariable> visibleVariables() throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        createVisibleVariables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        List<LocalVariable> mapAsList = new ArrayList<LocalVariable>(visibleVariables.values());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Collections.sort(mapAsList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return mapAsList;
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
     * Return a particular variable in the frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Need not be synchronized since it cannot be provably stale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public LocalVariable visibleVariableByName(String name) throws AbsentInformationException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        createVisibleVariables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return visibleVariables.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Value getValue(LocalVariable variable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        List<LocalVariable> list = new ArrayList<LocalVariable>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        list.add(variable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return getValues(list).get(variable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public Map<LocalVariable, Value> getValues(List<? extends LocalVariable> variables) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        validateMirrors(variables);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int count = variables.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        JDWP.StackFrame.GetValues.SlotInfo[] slots =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                           new JDWP.StackFrame.GetValues.SlotInfo[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        for (int i=0; i<count; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            LocalVariableImpl variable = (LocalVariableImpl)variables.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            if (!variable.isVisible(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                throw new IllegalArgumentException(variable.name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                 " is not valid at this frame location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            slots[i] = new JDWP.StackFrame.GetValues.SlotInfo(variable.slot(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                      (byte)variable.signature().charAt(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        PacketStream ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        /* protect against defunct frame id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            ps = JDWP.StackFrame.GetValues.enqueueCommand(vm, thread, id, slots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /* actually get it, now that order is guaranteed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        ValueImpl[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            values = JDWP.StackFrame.GetValues.waitForReply(vm, ps).values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                case JDWP.Error.INVALID_FRAMEID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                case JDWP.Error.INVALID_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    throw new InvalidStackFrameException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (count != values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new InternalException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                      "Wrong number of values returned from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Map<LocalVariable, Value> map = new HashMap<LocalVariable, Value>(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i=0; i<count; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            LocalVariableImpl variable = (LocalVariableImpl)variables.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            map.put(variable, values[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public void setValue(LocalVariable variableIntf, Value valueIntf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        throws InvalidTypeException, ClassNotLoadedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        validateMirror(variableIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        validateMirrorOrNull(valueIntf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        LocalVariableImpl variable = (LocalVariableImpl)variableIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        ValueImpl value = (ValueImpl)valueIntf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (!variable.isVisible(this)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new IllegalArgumentException(variable.name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                             " is not valid at this frame location");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // Validate and convert value if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            value = ValueImpl.prepareForAssignment(value, variable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            JDWP.StackFrame.SetValues.SlotInfo[] slotVals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                new JDWP.StackFrame.SetValues.SlotInfo[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            slotVals[0] = new JDWP.StackFrame.SetValues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                       SlotInfo(variable.slot(), value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            PacketStream ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            /* protect against defunct frame id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                ps = JDWP.StackFrame.SetValues.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                     enqueueCommand(vm, thread, id, slotVals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            /* actually set it, now that order is guaranteed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                JDWP.StackFrame.SetValues.waitForReply(vm, ps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                case JDWP.Error.INVALID_FRAMEID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                case JDWP.Error.INVALID_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    throw new InvalidStackFrameException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (ClassNotLoadedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
             * Since we got this exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
             * the variable type must be a reference type. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
             * we're trying to set is null, but if the variable's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             * class has not yet been loaded through the enclosing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             * class loader, then setting to null is essentially a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             * no-op, and we should allow it without an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public List<Value> getArgumentValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        MethodImpl mmm = (MethodImpl)location.method();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        List<String> argSigs = mmm.argumentSignatures();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int count = argSigs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        JDWP.StackFrame.GetValues.SlotInfo[] slots =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                           new JDWP.StackFrame.GetValues.SlotInfo[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (mmm.isStatic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            slot = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            slot = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        for (int ii = 0; ii < count; ++ii) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   333
            char sigChar = argSigs.get(ii).charAt(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            slots[ii] = new JDWP.StackFrame.GetValues.SlotInfo(slot++,(byte)sigChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (sigChar == 'J' || sigChar == 'D') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                slot++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        PacketStream ps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /* protect against defunct frame id */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        synchronized (vm.state()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            ps = JDWP.StackFrame.GetValues.enqueueCommand(vm, thread, id, slots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        ValueImpl[] values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            values = JDWP.StackFrame.GetValues.waitForReply(vm, ps).values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                case JDWP.Error.INVALID_FRAMEID:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                case JDWP.Error.INVALID_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    throw new InvalidStackFrameException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (count != values.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            throw new InternalException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                      "Wrong number of values returned from target VM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return Arrays.asList((Value[])values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    void pop() throws IncompatibleThreadStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        validateStackFrame();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // flush caches and disable caching until command completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        CommandSender sender =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            new CommandSender() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                public PacketStream send() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    return JDWP.StackFrame.PopFrames.enqueueCommand(vm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                 thread, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            PacketStream stream = thread.sendResumingCommand(sender);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            JDWP.StackFrame.PopFrames.waitForReply(vm, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (JDWPException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            switch (exc.errorCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            case JDWP.Error.THREAD_NOT_SUSPENDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                throw new IncompatibleThreadStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                         "Thread not current or suspended");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            case JDWP.Error.INVALID_THREAD:   /* zombie */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                throw new IncompatibleThreadStateException("zombie");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            case JDWP.Error.NO_MORE_FRAMES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                throw new InvalidStackFrameException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                         "No more frames on the stack");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                throw exc.toJDIException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // enable caching - suspended again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        vm.state().freeze();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
       return location.toString() + " in thread " + thread.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}