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: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Anatoly F. Bondarenko
020: * @version $Revision: 1.1 $
021: */
022:
023: /**
024: * Created on 26.10.2006
025: */package org.apache.harmony.jpda.tests.jdwp.Events;
026:
027: import org.apache.harmony.jpda.tests.framework.jdwp.CommandPacket;
028: import org.apache.harmony.jpda.tests.framework.jdwp.EventMod;
029: import org.apache.harmony.jpda.tests.framework.jdwp.EventPacket;
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.Location;
033: import org.apache.harmony.jpda.tests.framework.jdwp.ParsedEvent;
034: import org.apache.harmony.jpda.tests.framework.jdwp.ReplyPacket;
035: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
036:
037: /**
038: * JDWP Unit test for possible combined (co-located) events:
039: * METHOD_ENTRY, SINGLE_STEP, BREAKPOINT, METHOD_EXIT.
040: */
041: public class CombinedEvents003Test extends CombinedEventsTestCase {
042:
043: private String debuggeeSignature = "Lorg/apache/harmony/jpda/tests/jdwp/Events/CombinedEvents003Debuggee;";
044:
045: private String methodForEvents = "emptyMethod";
046:
047: private String methodEntryClassNameRegexp = "org.apache.harmony.jpda.tests.jdwp.Events.CombinedEvents003Debuggee";
048:
049: private boolean eventVmDeathReceived = false;
050:
051: protected String getDebuggeeClassName() {
052: return CombinedEvents003Debuggee.class.getName();
053: }
054:
055: public static void main(String[] args) {
056: junit.textui.TestRunner.run(CombinedEvents003Test.class);
057: }
058:
059: /**
060: * This test case checks events:
061: * METHOD_ENTRY, SINGLE_STEP, BREAKPOINT, METHOD_EXIT
062: * for empty method.
063: */
064:
065: public void testCombinedEvents003_01() {
066: logWriter.println("==> testCombinedEvents003_01 started");
067:
068: byte[] EXPECTED_EVENTS_ARRAY = {
069: JDWPConstants.EventKind.METHOD_ENTRY,
070: JDWPConstants.EventKind.BREAKPOINT,
071: JDWPConstants.EventKind.SINGLE_STEP,
072: JDWPConstants.EventKind.METHOD_EXIT };
073:
074: String debuggeeMainThreadName = synchronizer.receiveMessage();
075:
076: long debuggeeClassID = debuggeeWrapper.vmMirror
077: .getClassID(debuggeeSignature);
078: logWriter.println("=> debuggeeClassID = " + debuggeeClassID);
079:
080: long threadID = debuggeeWrapper.vmMirror
081: .getThreadID(debuggeeMainThreadName);
082: logWriter.println("=> threadID = " + threadID);
083:
084: long runMethodID = debuggeeWrapper.vmMirror.getMethodID(
085: debuggeeClassID, "run");
086: logWriter.println("=> runMethodID = " + runMethodID);
087:
088: long dummyMethodID = debuggeeWrapper.vmMirror.getMethodID(
089: debuggeeClassID, "dummyMethod");
090: logWriter.println("=> dummyMethodID = " + dummyMethodID);
091:
092: logWriter.println("");
093: logWriter.println("=> Info for tested method '"
094: + methodForEvents + "':");
095: long testedMethodID = debuggeeWrapper.vmMirror.getMethodID(
096: debuggeeClassID, methodForEvents);
097: if (testedMethodID == -1) {
098: String failureMessage = "## FAILURE: Can NOT get MethodID for class '"
099: + getDebuggeeClassName()
100: + "'; Method name = "
101: + methodForEvents;
102: printErrorAndFail(failureMessage);
103: }
104: logWriter.println("=> testedMethodID = " + testedMethodID);
105: printMethodLineTable(debuggeeClassID, null, methodForEvents);
106:
107: // set requests for events that will be checked
108: logWriter.println("");
109: logWriter
110: .println("=> Set request for BREAKPOINT event in debuggee: "
111: + getDebuggeeClassName()
112: + ", beginning of method: " + methodForEvents);
113: Location combinedEventsLocation = getMethodEntryLocation(
114: debuggeeClassID, methodForEvents);
115: if (combinedEventsLocation == null) {
116: String failureMessage = "## FAILURE: Can NOT get MethodEntryLocation for method '"
117: + methodForEvents + "'";
118: printErrorAndFail(failureMessage);
119: }
120: ReplyPacket reply = debuggeeWrapper.vmMirror
121: .setBreakpoint(combinedEventsLocation);
122: int breakpointRequestID = reply.getNextValueAsInt();
123: logWriter.println("=> Breakpoint requestID = "
124: + breakpointRequestID);
125:
126: logWriter
127: .println("=> Set request for METHOD_ENTRY event in debuggee: "
128: + getDebuggeeClassName());
129: reply = debuggeeWrapper.vmMirror
130: .setMethodEntry(methodEntryClassNameRegexp);
131: checkReplyPacket(reply, "Set METHOD_ENTRY event");
132: int methodEntryRequestID = reply.getNextValueAsInt();
133: logWriter.println("=> METHOD_ENTRY requestID = "
134: + methodEntryRequestID);
135: logWriter
136: .println("=> Set request for METHOD_EXIT event in debuggee: "
137: + getDebuggeeClassName());
138: reply = debuggeeWrapper.vmMirror
139: .setMethodExit(methodEntryClassNameRegexp);
140: checkReplyPacket(reply, "Set METHOD_EXIT event");
141: int methodExitRequestID = reply.getNextValueAsInt();
142: logWriter.println("=> METHOD_EXIT requestID = "
143: + methodExitRequestID);
144:
145: logWriter
146: .println("=> Set request for SINGLE_STEP event in class "
147: + debuggeeSignature);
148: CommandPacket setRequestCommand = new CommandPacket(
149: JDWPCommands.EventRequestCommandSet.CommandSetID,
150: JDWPCommands.EventRequestCommandSet.SetCommand);
151: setRequestCommand
152: .setNextValueAsByte(JDWPConstants.EventKind.SINGLE_STEP);
153: setRequestCommand
154: .setNextValueAsByte(JDWPConstants.SuspendPolicy.ALL);
155: setRequestCommand.setNextValueAsInt(2);
156: setRequestCommand.setNextValueAsByte(EventMod.ModKind.Step);
157: setRequestCommand.setNextValueAsThreadID(threadID);
158: setRequestCommand.setNextValueAsInt(JDWPConstants.StepSize.MIN);
159: setRequestCommand
160: .setNextValueAsInt(JDWPConstants.StepDepth.INTO);
161: setRequestCommand
162: .setNextValueAsByte(EventMod.ModKind.ClassOnly);
163: setRequestCommand
164: .setNextValueAsReferenceTypeID(debuggeeClassID);
165:
166: ReplyPacket setRequestReply = debuggeeWrapper.vmMirror
167: .performCommand(setRequestCommand);
168: checkReplyPacket(setRequestReply, "EventRequest::Set command");
169: int stepRequestID = setRequestReply.getNextValueAsInt();
170: logWriter
171: .println("=> SINGLE_STEP requestID = " + stepRequestID);
172:
173: logWriter.println("");
174: logWriter
175: .println("=> Send SGNL_CONTINUE signal to debuggee...");
176: synchronizer
177: .sendMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
178:
179: logWriter
180: .println("=> Try to receive and check combined events: "
181: + " METHOD_ENTRY, SINGLE_STEP, BREAKPOINT, METHOD_EXIT events; ignore single SINGLE_STEP event");
182: receiveAndCheckEvents(EXPECTED_EVENTS_ARRAY,
183: combinedEventsLocation);
184: if (eventVmDeathReceived) {
185: logWriter
186: .println("==> testCombinedEvents001 is FINISHing as VM_DEATH is received!");
187: return;
188: }
189:
190: logWriter.println("");
191: logWriter.println("=> Clean request for METHOD_ENTRY event...");
192: ReplyPacket clearReply = debuggeeWrapper.vmMirror.clearEvent(
193: JDWPConstants.EventKind.METHOD_ENTRY,
194: methodEntryRequestID);
195: checkReplyPacket(clearReply, "EventRequest::Clear");
196:
197: logWriter.println("");
198: logWriter.println("=> Clean request for SINGLE_STEP event...");
199: clearReply = debuggeeWrapper.vmMirror.clearEvent(
200: JDWPConstants.EventKind.SINGLE_STEP, stepRequestID);
201: checkReplyPacket(clearReply, "EventRequest::Clear");
202:
203: logWriter.println("=> Resume debuggee");
204: debuggeeWrapper.vmMirror.resume();
205:
206: // check that no other events, except VM_DEATH, will be received
207: checkVMDeathEvent();
208:
209: logWriter.println("");
210: logWriter.println("==> testCombinedEvents003_01 PASSED");
211: }
212:
213: void receiveAndCheckEvents(byte[] EXPECTED_EVENTS_ARRAY,
214: Location expectedLocation) {
215: int EXPECTED_EVENTS_COUNT = EXPECTED_EVENTS_ARRAY.length;
216: for (;;) {
217: logWriter.println("=>");
218: logWriter.println("=> Receiving events...");
219: EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
220: ParsedEvent[] parsedEvents = ParsedEvent
221: .parseEventPacket(event);
222:
223: // print all received events
224: logWriter
225: .println("=> Received event packet with events number = "
226: + parsedEvents.length + " :");
227:
228: for (int i = 0; i < parsedEvents.length; i++) {
229: logWriter.println("");
230: logWriter.println("=> Event #" + (i + 1)
231: + " in packet -");
232: logWriter
233: .println("=> EventKind: "
234: + parsedEvents[i].getEventKind()
235: + "["
236: + JDWPConstants.EventKind
237: .getName(parsedEvents[i]
238: .getEventKind()) + "]");
239: logWriter.println("=> RequestID: "
240: + parsedEvents[i].getRequestID());
241: if (parsedEvents[i].getEventKind() == JDWPConstants.EventKind.VM_DEATH) {
242: eventVmDeathReceived = true;
243: }
244: }
245: if (eventVmDeathReceived) {
246: return;
247: }
248: checkEventsLocation(parsedEvents, null); // DBG__
249:
250: // the following code checks received events
251: if (parsedEvents.length == 1) {
252: debuggeeWrapper.vmMirror.resume();
253: continue;
254: /* DBG__
255: if (parsedEvents[0].getEventKind() == JDWPConstants.EventKind.SINGLE_STEP) {
256: logWriter.println("");
257: logWriter.println("=> Resume debuggee");
258: logWriter.println("");
259: debuggeeWrapper.vmMirror.resume();
260: continue;
261: } else {
262: logWriter.println("##FAILURE: received unexpected event: "
263: + parsedEvents[0].getEventKind()
264: + "["
265: + JDWPConstants.EventKind.getName(parsedEvents[0]
266: .getEventKind())
267: + "] instead of SINGLE_STEP");
268: fail("received event is not SINGLE_STEP event: "
269: + parsedEvents[0].getEventKind()
270: + "["
271: + JDWPConstants.EventKind.getName(parsedEvents[0]
272: .getEventKind()) + "]");
273: }
274: // DBG__ */
275: //DBG__} else if (parsedEvents.length == EXPECTED_EVENTS_COUNT) {
276: } else if (parsedEvents.length > 1) {
277: logWriter.println("");
278: logWriter
279: .println("=> Combined events are received. Check events..");
280: boolean success = true;
281: for (int i = 0; i < parsedEvents.length; i++) {
282: boolean isFound = false;
283: for (int j = 0; j < EXPECTED_EVENTS_COUNT; j++) {
284: if (parsedEvents[i].getEventKind() == EXPECTED_EVENTS_ARRAY[j]) {
285: EXPECTED_EVENTS_ARRAY[j] = 0;
286: isFound = true;
287: break;
288: }
289: }
290: if (!isFound) {
291: logWriter
292: .println("##FAILURE: received unexpected event: "
293: + parsedEvents[i]
294: .getEventKind()
295: + "["
296: + JDWPConstants.EventKind
297: .getName(parsedEvents[0]
298: .getEventKind())
299: + "]");
300: success = false;
301: }
302: }
303: if (!success) {
304: logWriter.println("");
305: logWriter
306: .println("##FAILURE: the following expected events were not received: ");
307: for (int k = 0; k < EXPECTED_EVENTS_COUNT; k++) {
308: if (EXPECTED_EVENTS_ARRAY[k] != 0)
309: logWriter
310: .println(" #"
311: + k
312: + ": "
313: + EXPECTED_EVENTS_ARRAY[k]
314: + "["
315: + JDWPConstants.EventKind
316: .getName(EXPECTED_EVENTS_ARRAY[k])
317: + "]");
318: }
319: fail("not all expected events were received");
320: }
321: for (int i = 0; i < parsedEvents.length; i++) {
322: byte eventKind = parsedEvents[i].getEventKind();
323: long eventThreadID = ((ParsedEvent.EventThread) parsedEvents[i])
324: .getThreadID();
325: logWriter.println("");
326: logWriter
327: .println("=> Chcek location for combined event N "
328: + (i + 1)
329: + ": Event kind = "
330: + eventKind
331: + "("
332: + JDWPConstants.EventKind
333: .getName(eventKind)
334: + "); eventThreadID = "
335: + eventThreadID);
336: Location eventLocation = null;
337: switch (eventKind) {
338: case JDWPConstants.EventKind.METHOD_ENTRY:
339: eventLocation = ((ParsedEvent.Event_METHOD_ENTRY) parsedEvents[i])
340: .getLocation();
341: break;
342: case JDWPConstants.EventKind.SINGLE_STEP:
343: eventLocation = ((ParsedEvent.Event_SINGLE_STEP) parsedEvents[i])
344: .getLocation();
345: break;
346: case JDWPConstants.EventKind.BREAKPOINT:
347: eventLocation = ((ParsedEvent.Event_BREAKPOINT) parsedEvents[i])
348: .getLocation();
349: break;
350: case JDWPConstants.EventKind.METHOD_EXIT:
351: eventLocation = ((ParsedEvent.Event_METHOD_EXIT) parsedEvents[i])
352: .getLocation();
353: break;
354: }
355: long eventClassID = eventLocation.classID;
356: logWriter.println("=> ClassID in event = "
357: + eventClassID);
358: if (expectedLocation.classID != eventClassID) {
359: logWriter
360: .println("## FAILURE: Unexpected ClassID in event!");
361: logWriter
362: .println("## Expected ClassID = "
363: + expectedLocation.classID);
364: success = false;
365: } else {
366: logWriter
367: .println("=> OK - it is expected ClassID");
368: }
369: long eventMethodID = eventLocation.methodID;
370: logWriter.println("=> MethodID in event = "
371: + eventMethodID);
372: if (expectedLocation.methodID != eventMethodID) {
373: logWriter
374: .println("## FAILURE: Unexpected MethodID in event!");
375: logWriter
376: .println("## Expected MethodID = "
377: + expectedLocation.methodID);
378: success = false;
379: } else {
380: logWriter
381: .println("=> OK - it is expected MethodID");
382: }
383: long eventCodeIndex = eventLocation.index;
384: logWriter.println("=> CodeIndex in event = "
385: + eventCodeIndex);
386: if (expectedLocation.index != eventCodeIndex) {
387: logWriter
388: .println("## FAILURE: Unexpected CodeIndex in event!");
389: logWriter
390: .println("## Expected CodeIndex = "
391: + expectedLocation.index);
392: success = false;
393: } else {
394: logWriter
395: .println("=> OK - it is expected CodeIndex)");
396: }
397: }
398: logWriter.println("");
399: if (!success) {
400: String failureMessage = "## FAILURE: Unexpected events' locations are found out!";
401: logWriter.println(failureMessage);
402: //DBG__printErrorAndFail(failureMessage);
403: } else {
404: logWriter
405: .println("=> OK - all combined events have expected location!");
406: }
407: break;
408: } else {
409: logWriter
410: .println("##FAILURE: received unexpected number of events: "
411: + parsedEvents.length
412: + " instead of 1 or "
413: + EXPECTED_EVENTS_COUNT);
414: fail("received unexpected number of events: "
415: + parsedEvents.length);
416: }
417: }
418: }
419:
420: void checkVMDeathEvent() {
421: if (eventVmDeathReceived) {
422: return;
423: }
424: logWriter.println("=> Wait for VM_DEATH event...");
425: while (true) { //DBG_
426: logWriter.println("=> Receiving events...");
427: EventPacket event = debuggeeWrapper.vmMirror.receiveEvent();
428: ParsedEvent[] parsedEvents = ParsedEvent
429: .parseEventPacket(event);
430: if (parsedEvents.length != 1
431: || parsedEvents[0].getEventKind() != JDWPConstants.EventKind.VM_DEATH) {
432: // print all received events
433: logWriter
434: .println("##FAILURE: Received unexpected events");
435: logWriter.println("=> Events received: "
436: + parsedEvents.length);
437: for (int i = 0; i < parsedEvents.length; i++) {
438: logWriter.println("");
439: logWriter.println("=> Event #" + i + ";");
440: logWriter.println("=> EventKind: "
441: + parsedEvents[i].getEventKind()
442: + "["
443: + JDWPConstants.EventKind
444: .getName(parsedEvents[i]
445: .getEventKind()) + "]");
446: logWriter.println("=> RequestID: "
447: + parsedEvents[i].getRequestID());
448: }
449: checkEventsLocation(parsedEvents, null); // DBG__
450: logWriter.println("=> Resume debuggee"); //DBG__
451: debuggeeWrapper.vmMirror.resume(); //DBG__
452: continue; //DBG__
453: //DBG__fail("unexpected events received");
454: }
455: logWriter.println("=> OK - VM_DEATH event was received. ");
456: break; //DBG__
457: } // DBG__
458: }
459: }
|