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 24.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.JDWPConstants;
31 import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
32 import org.apache.harmony.jpda.tests.framework.jdwp.TaggedObject;
33 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
34 
35 
36 /**
37  * JDWP Unit test for StackFrame.ThisObject command.
38  */
39 public class ThisObjectTest extends JDWPStackFrameTestCase {
40 
41     public static String[] KNOWN_METHOD_NAMES = {
42         "nestledMethod1",
43         "nestledMethod2",
44         "nestledMethod3",
45     };
46 
47     /**
48      * This testcase exercises StackFrame.ThisObject command.
49      * <BR>The test starts StackTraceDebuggee and
50      * checks if StackFrame.ThisObject command returns correct data for each stack frame
51      * of main thread in debuggee, taking into account calls to known methods.
52      */
testThisObjectTest001()53     public void testThisObjectTest001() {
54         logWriter.println("==> ThisObjectTestTest001 started");
55         //boolean success = true;
56 
57         // select main thread
58         String mainThreadName = synchronizer.receiveMessage();
59         logWriter.println("==> Searching for main thread by name: " + mainThreadName);
60         long mainThread = debuggeeWrapper.vmMirror.getThreadID(mainThreadName);
61         logWriter.println("==> Found main thread: " + mainThread);
62 
63         // release on run()
64         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
65 
66         // pass nestledMethod1()
67         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
68         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
69 
70         // pass nestledMethod2()
71         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
72         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
73 
74         // enter nestledMethod3()
75         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
76 
77         if ( mainThread == -1 ) {
78             logWriter.println("## FAILURE: main thread is not found!");
79             //assert True(false);
80             fail("main thread is not found!");
81         }
82 
83         // suspend thread
84         logWriter.println("==> Suspending main thread");
85         jdwpSuspendThread(mainThread);
86 
87         logWriter.println("==> Getting frames count");
88         int frameCount = jdwpGetFrameCount(mainThread);
89         logWriter.println("==> frames count = " + frameCount);
90 
91         logWriter.println("==> Getting frames");
92         FrameInfo[] frameIDs = jdwpGetFrames(mainThread, 0, frameCount);
93         logWriter.println("==> frames count = " + frameIDs.length);
94         assertEquals("Invalid number of frames,", frameCount, frameIDs.length);
95         //assertTrue(frameIDs.length == frameCount);
96 
97         for (int i = 0; i < frameCount; i++) {
98             logWriter.println("\n==> frame #" + i);
99 
100             long frameID = frameIDs[i].frameID;
101             logWriter.println("==> frameID=" + frameID);
102             if (frameID == 0) {
103                 logWriter.println("## FAILURE: ThreadReference.Frames returned NULL FrameID for frame #" + i);
104                 //success = false;
105                 fail("ThreadReference.Frames returned NULL FrameID for frame #" + i);
106                 continue;
107             }
108 
109             // logWriter.println("  location=" + frameIDs[i].location);
110 
111             String methodName = debuggeeWrapper.vmMirror.getMethodName(frameIDs[i].location.classID, frameIDs[i].location.methodID);
112             logWriter.println("==> method name=" + methodName);
113 
114             String methodSig = debuggeeWrapper.vmMirror.getMethodSignature(frameIDs[i].location.classID, frameIDs[i].location.methodID);
115             logWriter.println("==> method signature=" + methodSig);
116 
117             String classSig = debuggeeWrapper.vmMirror.getClassSignature(frameIDs[i].location.classID);
118             logWriter.println("==> class signature=" + classSig);
119 
120             // get ThisObject
121             logWriter.println("==> Send StackFrame::ThisObject command...");
122             CommandPacket packet = new CommandPacket(
123                     JDWPCommands.StackFrameCommandSet.CommandSetID,
124                     JDWPCommands.StackFrameCommandSet.ThisObjectCommand);
125             packet.setNextValueAsThreadID(mainThread);
126             packet.setNextValueAsLong(frameIDs[i].getFrameID());
127 
128             ReplyPacket reply = debuggeeWrapper.vmMirror.performCommand(packet);
129             long knownMethodsThisObject = 0;
130             if (reply.getErrorCode() == JDWPConstants.Error.NONE) {
131                 TaggedObject thisObject = reply.getNextValueAsTaggedObject();
132                 logWriter.println("==> thisObject:");
133                 logWriter.println("==> tag=" + thisObject.tag + "("
134                         + JDWPConstants.Tag.getName(thisObject.tag) + ")");
135                 logWriter.println("==> id=" + thisObject.objectID);
136                 if (thisObject.objectID != 0) {
137                     long classID = getObjectReferenceType(thisObject.objectID);
138                     logWriter.println("==> class=" + debuggeeWrapper.vmMirror.getClassSignature(classID));
139                 }
140 
141                 for (int j = 0; j < KNOWN_METHOD_NAMES.length; j++) {
142                     if (KNOWN_METHOD_NAMES[j].equals(methodName)) {
143                         logWriter.println("==> frame for known method: " + KNOWN_METHOD_NAMES[j]);
144                         if (thisObject.objectID == 0) {
145                             logWriter.println
146                             ("## FAILURE: StackFrame.ThisObject returned NULL ObjectID for known method: " + methodName);
147                             //success = false;
148                             fail("StackFrame.ThisObject returned NULL ObjectID for known method: " + methodName);
149                         } else {
150                             if ( knownMethodsThisObject != 0 ) {
151                                 if ( knownMethodsThisObject != thisObject.objectID ) {
152                                     logWriter.println
153                                     ("## FAILURE: Returned unexpected ObjectID for known method: " + methodName);
154                                     logWriter.println
155                                     ("## Expected ObjectID: " + knownMethodsThisObject);
156                                     //success = false;
157                                     fail("Returned unexpected ObjectID for known method: " + methodName);
158                                 }
159                             } else {
160                                 knownMethodsThisObject = thisObject.objectID;
161                             }
162                         }
163                         if (thisObject.tag != JDWPConstants.Tag.OBJECT_TAG) {
164                             logWriter.println
165                             ("## FAILURE: StackFrame.ThisObject returned not OBJECT_TAG for known method: " + methodName);
166                             //success = false;
167                             fail("StackFrame.ThisObject returned not OBJECT_TAG for known method: " + methodName);
168                         }
169                     }
170                 }
171 
172                 String mainMethod = "main";
173                 if (mainMethod.equals(methodName)) {
174                     logWriter.println("==> frame for method: " + mainMethod);
175                     if (thisObject.objectID != 0) {
176                         logWriter.println
177                         ("## FAILURE: Returned unexpected ObjectID for method: " + mainMethod);
178                         logWriter.println
179                         ("## Expected ObjectID: " + 0);
180                         //success = false;
181                         fail("Returned unexpected ObjectID for method: " + mainMethod);
182                     }
183                     if (thisObject.tag != JDWPConstants.Tag.OBJECT_TAG) {
184                         logWriter.println
185                         ("## FAILURE: StackFrame.ThisObject returned not OBJECT_TAG for method: " + mainMethod);
186                         //success = false;
187                         fail("StackFrame.ThisObject returned not OBJECT_TAG for method: " + mainMethod);
188                     }
189                 }
190 
191                 assertAllDataRead(reply);
192 
193             } else {
194                 logWriter.println
195                 ("## FAILURE: StackFrame::ThisObject command returns unexpected ERROR = "
196                         + reply.getErrorCode()
197                         + "(" + JDWPConstants.Error.getName(reply.getErrorCode()) + ")");
198                 logWriter.println("## Expected ERROR = " + JDWPConstants.Error.NONE
199                         + "(" + JDWPConstants.Error.getName(JDWPConstants.Error.NONE) + ")");
200                 //success = false;
201                 fail("StackFrame::ThisObject command returns unexpected ERROR = " + reply.getErrorCode()
202                         + "(" + JDWPConstants.Error.getName(reply.getErrorCode()) + ")"
203                         + ", Expected ERROR = " + JDWPConstants.Error.NONE
204                         + "(" + JDWPConstants.Error.getName(JDWPConstants.Error.NONE) + ")");
205             }
206         }
207 
208         // resume thread
209         logWriter.println("==> Resuming main thread");
210         jdwpResumeThread(mainThread);
211 
212         // release nestledMethod3()
213         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
214 
215         //assertTrue(success);
216         logWriter.println("==> ThisObjectTestTest001 finished");
217     }
218 }
219