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  */
26 package org.apache.harmony.jpda.tests.jdwp.StackFrame;
27 
28 import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
29 import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
30 import org.apache.harmony.jpda.tests.framework.jdwp.Location;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.jdwp.share.JDWPSyncTestCase;
33 
34 
35 public class JDWPStackFrameTestCase extends JDWPSyncTestCase {
36 
37     public class FrameInfo {
38         long frameID;
39         Location location;
40 
FrameInfo(long frameID, Location location)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          */
getFrameID()50         public long getFrameID() {
51             return frameID;
52         }
53         /**
54          * @return Returns the location.
55          */
getLocation()56         public Location getLocation() {
57             return location;
58         }
59     }
60 
61     public class VarInfo {
62         int slot;
63         String signature, name;
64 
65 
VarInfo(String name, int slot, String signature)66         public VarInfo(String name, int slot, String signature) {
67             this.slot = slot;
68             this.signature = signature;
69             this.name = name;
70         }
71 
getSlot()72         public int getSlot() {
73             return slot;
74         }
75 
getSignature()76         public String getSignature() {
77             return signature;
78         }
79 
getName()80         public String getName() {
81             return name;
82         }
83 
84     }
85 
getDebuggeeClassName()86     protected String getDebuggeeClassName() {
87         return StackTraceDebuggee.class.getName();
88     }
89 
jdwpGetFrameCount(long threadID)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 
jdwpGetFrames(long threadID, int startFrame, int length)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 
jdwpGetVariableTable(long classID, long methodID)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 
jdwpGetAllThreads()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 
jdwpGetThreadName(long threadID)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 
jdwpSuspendThread(long threadID)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 
jdwpResumeThread(long threadID)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 
jdwpPopFrames(long threadID, long frameID)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