1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Anton V. Karnachuk
21 */
22
23/**
24 * Created on 16.02.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.StackFrame;
27
28import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30import org.apache.harmony.jpda.tests.framework.jdwp.Location;
31import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33
34
35public class JDWPStackFrameTestCase extends JDWPSyncTestCase {
36
37    public class FrameInfo {
38        long frameID;
39        Location location;
40
41        public FrameInfo(long frameID, Location location) {
42            super();
43            this.frameID = frameID;
44            this.location = location;
45        }
46
47        /**
48         * @return Returns the frameID.
49         */
50        public long getFrameID() {
51            return frameID;
52        }
53        /**
54         * @return Returns the location.
55         */
56        public Location getLocation() {
57            return location;
58        }
59    }
60
61    public class VarInfo {
62        int slot;
63        String signature, name;
64
65
66        public VarInfo(String name, int slot, String signature) {
67            this.slot = slot;
68            this.signature = signature;
69            this.name = name;
70        }
71
72        public int getSlot() {
73            return slot;
74        }
75
76        public String getSignature() {
77            return signature;
78        }
79
80        public String getName() {
81            return name;
82        }
83
84    }
85
86    protected String getDebuggeeClassName() {
87        return StackTraceDebuggee.class.getName();
88    }
89
90    protected int jdwpGetFrameCount(long threadID) {
91        CommandPacket packet = new CommandPacket(
92                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
93                JDWPCommands.ThreadReferenceCommandSet.FrameCountCommand);
94        packet.setNextValueAsThreadID(threadID);
95
96        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
97        checkReplyPacket(reply, "ThreadReference::FrameCount command");
98
99        int frameCount = reply.getNextValueAsInt();
100        return frameCount;
101    }
102
103    protected FrameInfo[] jdwpGetFrames(long threadID, int startFrame, int length) {
104        CommandPacket packet = new CommandPacket(
105                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
106                JDWPCommands.ThreadReferenceCommandSet.FramesCommand);
107        packet.setNextValueAsThreadID(threadID);
108        packet.setNextValueAsInt(startFrame);
109        packet.setNextValueAsInt(length);
110
111        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
112        checkReplyPacket(reply, "ThreadReference::FramesCommand command");
113
114        int frames = reply.getNextValueAsInt();
115        FrameInfo[] frameInfos = new FrameInfo[frames];
116        for (int i = 0; i < frames; i++) {
117            long frameID = reply.getNextValueAsLong();
118            Location location = reply.getNextValueAsLocation();
119            frameInfos[i] = new FrameInfo(frameID, location);
120        }
121        return frameInfos;
122    }
123
124    protected VarInfo[] jdwpGetVariableTable(long classID, long methodID) {
125        CommandPacket packet = new CommandPacket(
126                JDWPCommands.MethodCommandSet.CommandSetID,
127                JDWPCommands.MethodCommandSet.VariableTableCommand);
128        packet.setNextValueAsClassID(classID);
129        packet.setNextValueAsMethodID(methodID);
130
131        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
132        checkReplyPacket(reply, "Method::VariableTable command");
133
134        reply.getNextValueAsInt();
135        int varNumber = reply.getNextValueAsInt();
136
137        VarInfo[] varInfos = new VarInfo[varNumber];
138        for (int i = 0; i < varNumber; i++) {
139            reply.getNextValueAsLong();
140            String name = reply.getNextValueAsString();
141            String sign = reply.getNextValueAsString();
142            reply.getNextValueAsInt();
143
144            int slot = reply.getNextValueAsInt();
145            varInfos[i] = new VarInfo(name, slot, sign);
146        }
147        return varInfos;
148    }
149
150    protected long[] jdwpGetAllThreads() {
151        CommandPacket packet = new CommandPacket(
152                JDWPCommands.VirtualMachineCommandSet.CommandSetID,
153                JDWPCommands.VirtualMachineCommandSet.AllThreadsCommand);
154
155        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
156        checkReplyPacket(reply, "VirtualMachine::AllThreads command");
157
158        int frames = reply.getNextValueAsInt();
159
160        long[] frameIDs = new long[frames];
161        for (int i = 0; i < frames; i++) {
162            frameIDs[i] = reply.getNextValueAsLong();
163        }
164
165        return frameIDs;
166    }
167
168    protected String jdwpGetThreadName(long threadID) {
169        CommandPacket packet = new CommandPacket(
170                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
171                JDWPCommands.ThreadReferenceCommandSet.NameCommand);
172        packet.setNextValueAsThreadID(threadID);
173
174        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
175        checkReplyPacket(reply, "ThreadReference::Name command");
176
177        String name= reply.getNextValueAsString();
178        return name;
179    }
180
181    protected void jdwpSuspendThread(long threadID) {
182        CommandPacket packet = new CommandPacket(
183                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
184                JDWPCommands.ThreadReferenceCommandSet.SuspendCommand);
185        packet.setNextValueAsThreadID(threadID);
186
187        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
188        checkReplyPacket(reply, "ThreadReference::Suspend command");
189    }
190
191    protected void jdwpResumeThread(long threadID) {
192        CommandPacket packet = new CommandPacket(
193                JDWPCommands.ThreadReferenceCommandSet.CommandSetID,
194                JDWPCommands.ThreadReferenceCommandSet.ResumeCommand);
195        packet.setNextValueAsThreadID(threadID);
196
197        ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
198        checkReplyPacket(reply, "ThreadReference::Resume command");
199    }
200
201    protected void jdwpPopFrames(long threadID, long frameID) {
202        CommandPacket popFramesCommand = new CommandPacket(
203                JDWPCommands.StackFrameCommandSet.CommandSetID,
204                JDWPCommands.StackFrameCommandSet.PopFramesCommand);
205        popFramesCommand.setNextValueAsThreadID(threadID);
206        popFramesCommand.setNextValueAsFrameID(frameID);
207
208        ReplyPacket reply = debuggeeWrapper.vmMirror
209                .performCommand(popFramesCommand);
210        checkReplyPacket(reply, "StackFrame::PopFramesCommand command");
211    }
212
213}
214