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, Aleksander V. Budniy
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.ReplyPacket;
31 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
32 
33 /**
34  * JDWP Unit test for StackFrame.PopFrames command.
35  */
36 public class PopFramesTest extends JDWPStackFrameTestCase {
37 
38     private String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/StackFrame/PopFramesDebuggee;";
39 
40     private String breakpointMethodName = "nestledMethod4";
41 
42     private String methodToPop = "nestledMethod4";
43 
getDebuggeeClassName()44     protected String getDebuggeeClassName() {
45         return PopFramesDebuggee.class.getName();
46     }
47 
48     /**
49      * This testcase exercises StackFrame.PopFrames command to discard one top frame.
50      * <BR>The test starts PopFramesDebuggee class, sets a breakpoint
51      * in 'nestledMethod4', stops at breakpoint and prints stack.
52      * <BR>Then the test performs StackFrame.PopFrame command for one top frame,
53      * prints stack and checks that discarded frame is not returned
54      * by ThreadReference.Frames command.
55      * <BR>Then the test resumes debuggee and checks stop on the same breakpoint.
56      */
testPopFramesTest001()57     public void testPopFramesTest001() {
58         logWriter.println("==> testPopFramesTest001 started");
59 
60         //check capability, relevant for this test
61         logWriter.println("=> Check capability: canPopFrames");
62         debuggeeWrapper.vmMirror.capabilities();
63         boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canPopFrames;
64         if (!isCapability) {
65             logWriter.println("##WARNING: this VM doesn't possess capability: canPopFrames");
66             return;
67         }
68 
69         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
70 
71         // find checked method
72         long refTypeID = getClassIDBySignature(debuggeeSignature);
73 
74         logWriter.println("=> Debuggee class = " + getDebuggeeClassName());
75 
76         logWriter.println("=> Set breakpoint at the beginning of " + breakpointMethodName);
77         int requestID = debuggeeWrapper.vmMirror.setBreakpointAtMethodBegin(
78                 refTypeID, breakpointMethodName);
79 
80         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
81 
82         // receive event
83         logWriter.println("=> Wait for breakpoint in " + breakpointMethodName);
84         long breakpointThreadID = debuggeeWrapper.vmMirror
85                 .waitForBreakpoint(requestID);
86 
87         logWriter.println("=> breakpointThreadID = " + breakpointThreadID);
88 
89         // print stack frames
90         logWriter.println("");
91         logWriter.println("=> Get frames before PopFrames command, thread = " + breakpointThreadID);
92         FrameInfo[] frameInfos = jdwpGetFrames(breakpointThreadID, 0, -1);
93         logWriter.println("=> Frames before popFrame");
94         printStackFrame(frameInfos.length, frameInfos);
95         logWriter.println("=> Number of frames before command: "
96                 + frameInfos.length);
97 
98         // find stack frame for popped method
99         logWriter.println("");
100         logWriter.println("=> Find frameID of method = " + methodToPop + " for PopFrames command");
101         long frameID = 0;
102         long methodID = getMethodID(refTypeID, methodToPop);
103         if (methodID == -1) {
104             logWriter.println("##FAILURE: error during getting methodID of " + methodToPop);
105         }
106         boolean isMethodFound = false;
107         for (int i = 0; i < frameInfos.length; i++) {
108             if (frameInfos[i].location.methodID == methodID) {
109                 frameID = frameInfos[i].getFrameID();
110                 isMethodFound = true;
111                 break;
112             }
113         }
114         if (!isMethodFound) {
115             logWriter
116                     .println("##FAILURE: there is no frame for checked method");
117             fail("There is no frame for checked method");
118         }
119 
120         logWriter.println("=> frameID for PopFrames command = " + frameID);
121 
122         logWriter.println("");
123         logWriter.println("=> Perform PopFrames command for method = " + methodToPop + " with frameID = " + frameID);
124         // pop stack frame
125         CommandPacket popFramesCommand = new CommandPacket(
126                 JDWPCommands.StackFrameCommandSet.CommandSetID,
127                 JDWPCommands.StackFrameCommandSet.PopFramesCommand);
128         popFramesCommand.setNextValueAsThreadID(breakpointThreadID);
129         popFramesCommand.setNextValueAsFrameID(frameID);
130 
131         ReplyPacket err = debuggeeWrapper.vmMirror
132                 .performCommand(popFramesCommand);
133         checkReplyPacket(err, "StackFrame::PopFrames command");
134 
135         logWriter.println("=> Get frames after PopFrames command, thread = " + breakpointThreadID);
136         FrameInfo[] newFrameInfos = jdwpGetFrames(breakpointThreadID, 0, -1);
137 
138         logWriter.println("");
139         logWriter.println("=> Frames after popFrame");
140         logWriter.println("=> newNumberOfFrames = " + newFrameInfos.length);
141 
142         printStackFrame(newFrameInfos.length, newFrameInfos);
143 
144         // check if expected frames are popped
145         logWriter.println("=> Check that only one frame was discarded: frameID = " + frameID + ", method = " + methodToPop);
146         int numberOfPoppedFrames = frameInfos.length - newFrameInfos.length;
147         assertEquals("frame is not discarded", numberOfPoppedFrames, 1);
148 
149         for (int i = numberOfPoppedFrames; i < (frameInfos.length); i++) {
150             if (frameInfos[i].location.methodID != newFrameInfos[i
151                     - numberOfPoppedFrames].location.methodID) {
152                 logWriter.println("## FAILURE: frames number " + i + " and "
153                         + (i - numberOfPoppedFrames) + " are not equal");
154                 fail("frames number are not equal");
155             }
156         }
157         logWriter.println("=> Ckeck PASSED");
158         logWriter.println("=> Resume debuggee");
159         debuggeeWrapper.vmMirror.resume();
160 
161         logWriter.println("=> Wait for breakpoint in " + breakpointMethodName);
162         breakpointThreadID = debuggeeWrapper.vmMirror
163                 .waitForBreakpoint(requestID);
164 
165         logWriter.println("=> Resume debuggee");
166         debuggeeWrapper.vmMirror.resume();
167 
168         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
169         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
170 
171         logWriter.println("==> TEST PASSED");
172 
173     }
174 
printStackFrame(int NumberOfFrames, FrameInfo[] frameInfos)175     void printStackFrame(int NumberOfFrames, FrameInfo[] frameInfos) {
176         for (int i = 0; i < NumberOfFrames; i++) {
177             logWriter.println(" ");
178             logWriter
179                     .println("=> #" + i + " frameID=" + frameInfos[i].frameID);
180             String methodName = getMethodName(frameInfos[i].location.classID,
181                     frameInfos[i].location.methodID);
182             logWriter.println("=> method name=" + methodName);
183         }
184         logWriter.println(" ");
185     }
186 }
187