001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Aleksander V. Budniy
021: * @version $Revision: 1.5 $
022: */
023:
024: /**
025: * Created on 1.05.2006
026: */package org.apache.harmony.jpda.tests.jdwp.StackFrame;
027:
028: import org.apache.harmony.jpda.tests.framework.TestErrorException;
029: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
030: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPCommands;
031: import org.apache.harmony.jpda.tests.framework.jdwp.JDWPConstants;
032: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
033: import org.apache.harmony.jpda.tests.framework.jdwp.Value;
034: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
035:
036: /**
037: * JDWP Unit test for StackFrame.PopFrames command.
038: */
039: public class PopFrames002Test extends JDWPStackFrameTestCase {
040:
041: private String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/StackFrame/PopFramesDebuggee;";
042:
043: private String breakpointMethodName = "nestledMethod4";
044:
045: private String methodToPop = "nestledMethod2";
046:
047: private long timeoutToWait = 2000;
048:
049: private long timeOfMethodInvocation = timeoutToWait * 5;
050:
051: private static final byte NUMBER_OF_FRAMES_TO_POP = 3;
052:
053: protected String getDebuggeeClassName() {
054: return PopFramesDebuggee.class.getName();
055: }
056:
057: /**
058: * This testcase exercises StackFrame.PopFrames command to discard several frames at once.
059: * <BR>The test starts PopFramesDebuggee class, sets a breakpoint
060: * in 'nestledMethod4', stops at breakpoint and prints stack.
061: * <BR>Then the test performs StackFrame.PopFrame command to discard several frames at once,
062: * prints stack and checks that discarded frames are not returned
063: * by ThreadReference.Frames command.
064: * <BR>Then the test resumes debuggee and checks stop on the same breakpoint.
065: */
066: public void testPopSeveralFrames() {
067: logWriter.println("==> testPopSeveralFrames started");
068:
069: //check capability, relevant for this test
070: logWriter.println("=> Check capability: canPopFrames");
071: debuggeeWrapper.vmMirror.capabilities();
072: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canPopFrames;
073: if (!isCapability) {
074: logWriter
075: .println("##WARNING: this VM doesn't possess capability: canPopFrames");
076: return;
077: }
078:
079: synchronizer
080: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
081:
082: // find checked method
083: long refTypeID = getClassIDBySignature(debuggeeSignature);
084:
085: logWriter.println("=> Debuggee class = "
086: + getDebuggeeClassName());
087:
088: // long methodID = getMethodID(refTypeID, breakpointMethodName);
089: logWriter.println("=> Set breakpoint at the beginning of "
090: + breakpointMethodName);
091: long requestID = debuggeeWrapper.vmMirror
092: .setBreakpointAtMethodBegin(refTypeID,
093: breakpointMethodName);
094:
095: // release debuggee
096: synchronizer
097: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
098:
099: // receive event in nestledMethod4
100: logWriter.println("=> Wait for breakpoint in "
101: + breakpointMethodName);
102: long breakpointThreadID = debuggeeWrapper.vmMirror
103: .waitForBreakpoint(requestID);
104:
105: logWriter.println("=> breakpointThreadID = "
106: + breakpointThreadID);
107:
108: // print stack frames
109: logWriter.println("");
110: logWriter
111: .println("=> Get frames before PopFrames command, thread = "
112: + breakpointThreadID);
113: FrameInfo[] frameInfos = jdwpGetFrames(breakpointThreadID, 0,
114: -1);
115: logWriter.println("=> Frames before popFrame");
116: printStackFrame(frameInfos.length, frameInfos);
117: logWriter.println("=> Number of frames before command: "
118: + frameInfos.length);
119:
120: // find stack frame for popped method
121: logWriter.println("");
122: logWriter.println("=> Find frameID of method = " + methodToPop
123: + " for PopFrames command");
124: long frameID = 0;
125: long methodID = getMethodID(refTypeID, methodToPop);
126: if (methodID == -1) {
127: logWriter
128: .println("##FAILURE: error during getting methodID of "
129: + methodToPop);
130: }
131: boolean isMethodFound = false;
132: for (int j = 0; j < frameInfos.length; j++) {
133: if (frameInfos[j].location.methodID == methodID) {
134: frameID = frameInfos[j].getFrameID();
135: isMethodFound = true;
136: break;
137: }
138: }
139: if (!isMethodFound) {
140: logWriter
141: .println("##FAILURE: there is no frame for checked method");
142: fail("There is no frame for checked method");
143: }
144:
145: logWriter.println("=> frameID for PopFrames command = "
146: + frameID);
147:
148: // pop stack frames
149: logWriter.println("=> Pop " + NUMBER_OF_FRAMES_TO_POP
150: + " frames at once");
151:
152: logWriter.println("");
153: logWriter.println("=> Perform PopFrames command for method = "
154: + methodToPop + " with frameID = " + frameID);
155: jdwpPopFrames(breakpointThreadID, frameID);
156:
157: logWriter
158: .println("=> Get frames after PopFrames command, thread = "
159: + breakpointThreadID);
160: FrameInfo[] newFrameInfos = jdwpGetFrames(breakpointThreadID,
161: 0, -1);
162:
163: logWriter.println("");
164: logWriter.println("=> Frames after popFrame");
165: logWriter.println("=> newNumberOfFrames = "
166: + newFrameInfos.length);
167:
168: printStackFrame(newFrameInfos.length, newFrameInfos);
169: logWriter.println("=> Check that " + NUMBER_OF_FRAMES_TO_POP
170: + " were discarded");
171: int numberOfPoppedFrames = frameInfos.length
172: - newFrameInfos.length;
173: assertEquals("not all frames were discarded",
174: numberOfPoppedFrames, NUMBER_OF_FRAMES_TO_POP);
175:
176: for (int i = numberOfPoppedFrames; i < (frameInfos.length); i++) {
177: if (frameInfos[i].location.methodID != newFrameInfos[i
178: - numberOfPoppedFrames].location.methodID) {
179: logWriter.println("## FAILURE: frames number " + i
180: + " and " + (i - numberOfPoppedFrames)
181: + " are not equal");
182: fail("frames number are not equal");
183: }
184: }
185:
186: logWriter.println("=> Ckeck PASSED");
187: logWriter.println("=> Resume debuggee");
188: debuggeeWrapper.vmMirror.resume();
189:
190: synchronizer
191: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
192: synchronizer
193: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
194:
195: logWriter.println("=> Wait for breakpoint in "
196: + breakpointMethodName);
197: breakpointThreadID = debuggeeWrapper.vmMirror
198: .waitForBreakpoint(requestID);
199:
200: logWriter.println("=> Resume debuggee");
201: debuggeeWrapper.vmMirror.resume();
202:
203: synchronizer
204: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
205: synchronizer
206: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
207: logWriter.println("==> TEST popSeveralFrames PASSED");
208:
209: }
210:
211: /**
212: * This testcase exercises StackFrame.PopFrames command performing it several times.
213: * <BR>The test starts PopFramesDebuggee class, sets a breakpoint
214: * in 'nestledMethod4', stops at breakpoint and prints stack.
215: * <BR>Then the test performs StackFrame.PopFrame command several times
216: * to discard frames one after another,
217: * prints stack and checks that discarded frames are not returned
218: * by ThreadReference.Frames command.
219: * <BR>Then the test resumes debuggee and checks stop on the same breakpoint.
220: */
221: public void testPopSeveralTimes() {
222: logWriter.println("==> testPopSeveralTimes started");
223:
224: //check capability, relevant for this test
225: logWriter.println("=> Check capability: canPopFrames");
226: debuggeeWrapper.vmMirror.capabilities();
227: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canPopFrames;
228: if (!isCapability) {
229: logWriter
230: .println("##WARNING: this VM doesn't possess capability: canPopFrames");
231: return;
232: }
233: // pass nestledMethod1()
234: synchronizer
235: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
236:
237: // find checked method
238: long refTypeID = getClassIDBySignature(debuggeeSignature);
239:
240: logWriter.println("=> Debuggee class = "
241: + getDebuggeeClassName());
242:
243: logWriter.println("=> Set breakpoint at the beginning of "
244: + breakpointMethodName);
245: long requestID = debuggeeWrapper.vmMirror
246: .setBreakpointAtMethodBegin(refTypeID,
247: breakpointMethodName);
248:
249: synchronizer
250: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
251:
252: // receive event
253: logWriter.println("=> Wait for breakpoint in "
254: + breakpointMethodName);
255: long breakpointThreadID = debuggeeWrapper.vmMirror
256: .waitForBreakpoint(requestID);
257:
258: logWriter.println("=> breakpointThreadID = "
259: + breakpointThreadID);
260:
261: // print stack frames
262: logWriter.println("");
263: logWriter
264: .println("=> Get frames before PopFrames command, thread = "
265: + breakpointThreadID);
266: FrameInfo[] frameInfos = jdwpGetFrames(breakpointThreadID, 0,
267: -1);
268: logWriter.println("=> Frames before popFrame");
269: printStackFrame(frameInfos.length, frameInfos);
270: int framesBeforePop = frameInfos.length;
271: logWriter.println("=> Number of frames before command: "
272: + framesBeforePop);
273:
274: logWriter.println("");
275: logWriter.println("=> Pop " + NUMBER_OF_FRAMES_TO_POP
276: + " frames one after another");
277: logWriter.println("");
278: for (int i = 0; i < 3; i++) {
279: logWriter.println("=> Pop frame#" + i);
280: frameInfos = jdwpGetFrames(breakpointThreadID, 0, -1);
281: logWriter
282: .println("=> Perform PopFrames command with frameID = "
283: + frameInfos[0].getFrameID());
284: jdwpPopFrames(breakpointThreadID, frameInfos[0]
285: .getFrameID());
286: }
287:
288: logWriter
289: .println("=> Get frames after PopFrames command, thread = "
290: + breakpointThreadID);
291: FrameInfo[] newFrameInfos = jdwpGetFrames(breakpointThreadID,
292: 0, -1);
293:
294: logWriter.println("");
295: logWriter.println("=> Frames after popFrame");
296: logWriter.println("=> newNumberOfFrames = "
297: + newFrameInfos.length);
298:
299: printStackFrame(newFrameInfos.length, newFrameInfos);
300:
301: logWriter.println("=> Check that " + NUMBER_OF_FRAMES_TO_POP
302: + " frames were discarded");
303: int numberOfPoppedFrames = framesBeforePop
304: - newFrameInfos.length;
305: assertEquals("not all frames were discarded",
306: numberOfPoppedFrames, NUMBER_OF_FRAMES_TO_POP);
307:
308: logWriter.println("=> Resume debuggee");
309: debuggeeWrapper.vmMirror.resume();
310:
311: synchronizer
312: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
313: synchronizer
314: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
315:
316: logWriter.println("=> Wait for breakpoint in "
317: + breakpointMethodName);
318: breakpointThreadID = debuggeeWrapper.vmMirror
319: .waitForBreakpoint(requestID);
320:
321: logWriter.println("=> Resume debuggee");
322: debuggeeWrapper.vmMirror.resume();
323:
324: synchronizer
325: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
326: synchronizer
327: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
328:
329: logWriter.println("==> TEST popSeveralTimes PASSED");
330: }
331:
332: /**
333: * This testcase exercises StackFrame.PopFrames command when thread is not suspended.
334: * <BR>The test starts PopFramesDebuggee class, sets a breakpoint
335: * in 'nestledMethod4', stops at breakpoint and prints stack.
336: * <BR>Then the test performs ClassType.InvokeMethodCommand without waiting reply, and
337: * waits to ensure that method was started.
338: * <BR>During working of method the test performs StackFrame.PopFrames command.
339: * Then the test checks that StackFrame.PopFrames command
340: * returns error: THREAD_NOT_SUSPENDED or INVALID_FRAMEID.
341: * <BR>Next, the test receives reply from invoked method and resumes debuggee..
342: */
343: public void testPopFramesWithInvokeMethods() {
344: logWriter.println("==> testPopFramesWithInvokeMethods started");
345:
346: //check capability, relevant for this test
347: logWriter.println("=> Check capability: canPopFrames");
348: debuggeeWrapper.vmMirror.capabilities();
349: boolean isCapability = debuggeeWrapper.vmMirror.targetVMCapabilities.canPopFrames;
350: if (!isCapability) {
351: logWriter
352: .println("##WARNING: this VM doesn't possess capability: canPopFrames");
353: return;
354: }
355: synchronizer
356: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
357:
358: // find checked method
359: long refTypeID = getClassIDBySignature(debuggeeSignature);
360:
361: logWriter.println("=> Debuggee class = "
362: + getDebuggeeClassName());
363:
364: logWriter.println("=> Set breakpoint at the beginning of "
365: + breakpointMethodName);
366: long requestID = debuggeeWrapper.vmMirror
367: .setBreakpointAtMethodBegin(refTypeID,
368: breakpointMethodName);
369:
370: // release debuggee
371: synchronizer
372: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
373:
374: // receive event
375: logWriter.println("=> Wait for breakpoint in "
376: + breakpointMethodName);
377: long breakpointThreadID = debuggeeWrapper.vmMirror
378: .waitForBreakpoint(requestID);
379:
380: logWriter.println("=> breakpointThreadID = "
381: + breakpointThreadID);
382:
383: // print stack frames
384: logWriter.println("");
385: logWriter
386: .println("=> Get frames before PopFrames command, thread = "
387: + breakpointThreadID);
388: FrameInfo[] frameInfos = jdwpGetFrames(breakpointThreadID, 0,
389: -1);
390: printStackFrame(frameInfos.length, frameInfos);
391: logWriter.println("=> Number of frames before command: "
392: + frameInfos.length);
393:
394: // find frameID to pop
395: logWriter.println("");
396: logWriter.println("=> Find frameID of method = " + methodToPop
397: + " for PopFrames command");
398: long frameID = 0;
399: long methodID = getMethodID(refTypeID, methodToPop);
400: boolean isMethodFound = false;
401: for (int j = 0; j < frameInfos.length; j++) {
402: if (frameInfos[j].location.methodID == methodID) {
403: frameID = frameInfos[j].getFrameID();
404: isMethodFound = true;
405: break;
406: }
407: }
408: if (!isMethodFound) {
409: logWriter
410: .println("##FAILURE: there is no frame for checked method");
411: fail("There is no frame for checked method");
412: }
413:
414: logWriter.println("=> frameID for PopFrames command = "
415: + frameID);
416:
417: // create JDWP command for MethodInvoke with a long running method
418: long debuggeeRefTypeID = debuggeeWrapper.vmMirror
419: .getClassID(debuggeeSignature);
420: logWriter.println("=> Find toInvokeMethodID for method: "
421: + PopFramesDebuggee.METHOD_TO_INVOKE_NAME);
422: long toInvokeMethodID = debuggeeWrapper.vmMirror.getMethodID(
423: debuggeeRefTypeID,
424: PopFramesDebuggee.METHOD_TO_INVOKE_NAME);
425: if (toInvokeMethodID == -1) {
426: logWriter
427: .println("## FAILURE: Can NOT get toInvokeMethodID for method: "
428: + PopFramesDebuggee.METHOD_TO_INVOKE_NAME);
429: fail("## Can NOT get toInvokeMethodID");
430: }
431: logWriter.println("=> toInvokeMethodID = " + toInvokeMethodID);
432:
433: int invokeCommandID = 0;
434: CommandPacket invokeCommand = new CommandPacket(
435: JDWPCommands.ClassTypeCommandSet.CommandSetID,
436: JDWPCommands.ClassTypeCommandSet.InvokeMethodCommand);
437: invokeCommand.setNextValueAsClassID(debuggeeRefTypeID);
438: invokeCommand.setNextValueAsThreadID(breakpointThreadID);
439: invokeCommand.setNextValueAsMethodID(toInvokeMethodID);
440: invokeCommand.setNextValueAsInt(1); // args number
441: invokeCommand.setNextValueAsValue(new Value(
442: timeOfMethodInvocation));
443: invokeCommand
444: .setNextValueAsInt(JDWPConstants.InvokeOptions.INVOKE_SINGLE_THREADED);
445:
446: // send MethodInvoke command, but not wait for reply
447: try {
448: logWriter
449: .println("=> Send InvokeMethod command for method: "
450: + PopFramesDebuggee.METHOD_TO_INVOKE_NAME);
451: invokeCommandID = debuggeeWrapper.vmMirror
452: .sendCommand(invokeCommand);
453: } catch (Exception e) {
454: logWriter.println("Exception during invokeCommand: " + e);
455: throw new TestErrorException(e);
456: }
457:
458: // wait to ensure that method invocation started
459: logWriter.println("=> Wait " + timeoutToWait
460: + " mls to ensure that method invocation started");
461: Object waitObj = new Object();
462: synchronized (waitObj) {
463: try {
464: waitObj.wait(timeoutToWait);
465: } catch (InterruptedException e) {
466: logWriter
467: .println("##Exception while waiting on object: "
468: + e);
469: throw new TestErrorException(e);
470: }
471: }
472:
473: // perform PopFrame command
474: logWriter.println("=> Perform PopFrames command for method = "
475: + methodToPop + " with frameID = " + frameID
476: + " and expect errors");
477: CommandPacket popFramesCommand = new CommandPacket(
478: JDWPCommands.StackFrameCommandSet.CommandSetID,
479: JDWPCommands.StackFrameCommandSet.PopFramesCommand);
480: popFramesCommand.setNextValueAsThreadID(breakpointThreadID);
481: popFramesCommand.setNextValueAsFrameID(frameID);
482:
483: ReplyPacket popFrameReply = debuggeeWrapper.vmMirror
484: .performCommand(popFramesCommand);
485: int res = popFrameReply.getErrorCode();
486: logWriter.println("=> Returned error code: " + res + " ("
487: + JDWPConstants.Error.getName(res) + ")");
488:
489: // check that PopFrames returns error, because thread is resumed by
490: // InvokeMethod
491: if (res == JDWPConstants.Error.NONE) {
492: logWriter
493: .println("##PopFrames command returned no error for thread resumed by InvokeMethod");
494: fail("##PopFrames command returned no error for thread resumed by InvokeMethod");
495: }
496:
497: logWriter.println("=> Receive reply of invoked method: "
498: + PopFramesDebuggee.METHOD_TO_INVOKE_NAME);
499: try {
500: ReplyPacket reply = debuggeeWrapper.vmMirror
501: .receiveReply(invokeCommandID);
502: checkReplyPacket(reply, "ClassType::InvokeMethod command");
503: } catch (Exception e) {
504: logWriter
505: .println("##Exception while receiving reply for invoke command: "
506: + e);
507: throw new TestErrorException(e);
508: }
509:
510: logWriter.println("=> Resume debuggee");
511: debuggeeWrapper.vmMirror.resume();
512:
513: synchronizer
514: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_READY);
515: synchronizer
516: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
517: logWriter
518: .println("==> TEST testPopFramesWithInvokeMethods PASSED");
519: }
520:
521: void printStackFrame(int NumberOfFrames, FrameInfo[] frameInfos) {
522: for (int i = 0; i < NumberOfFrames; i++) {
523: logWriter.println(" ");
524: logWriter.println("=> #" + i + " frameID="
525: + frameInfos[i].frameID);
526: String methodName = getMethodName(
527: frameInfos[i].location.classID,
528: frameInfos[i].location.methodID);
529: logWriter.println("=> method name=" + methodName);
530: }
531: logWriter.println("");
532: }
533:
534: public static void main(String[] args) {
535: junit.textui.TestRunner.run(PopFrames002Test.class);
536: }
537: }
|