MethodEntryExitEvents.java revision 0
0N/A/*
0N/A * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4409241 4432820
0N/A * @summary Test the bug fix for: MethodExitEvents disappear when Object-Methods are called from main
0N/A * @author Tim Bell
0N/A *
0N/A * @run build TestScaffold VMConnection TargetListener TargetAdapter
0N/A * @run compile -g MethodEntryExitEvents.java
0N/A * @run main MethodEntryExitEvents SUSPEND_EVENT_THREAD MethodEntryExitEventsDebugee
0N/A * @run main MethodEntryExitEvents SUSPEND_NONE MethodEntryExitEventsDebugee
0N/A * @run main MethodEntryExitEvents SUSPEND_ALL MethodEntryExitEventsDebugee
0N/A */
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.request.*;
0N/Aimport java.util.*;
0N/A
0N/Aclass t2 {
0N/A public static void sayHello1(int i, int j) {
0N/A sayHello2(i, j);
0N/A }
0N/A public static void sayHello2(int i, int j) {
0N/A sayHello3(i, j);
0N/A }
0N/A public static void sayHello3(int i, int j) {
0N/A sayHello4(i, j);
0N/A }
0N/A public static void sayHello4(int i, int j) {
0N/A sayHello5(i, j);
0N/A }
0N/A public static void sayHello5(int i, int j) {
0N/A if (i < 2) {
0N/A sayHello1(++i, j);
0N/A } else {
0N/A System.out.print ("MethodEntryExitEventsDebugee: ");
0N/A System.out.print (" -->> Hello. j is: ");
0N/A System.out.print (j);
0N/A System.out.println(" <<--");
0N/A }
0N/A }
0N/A}
0N/A
0N/Aclass MethodEntryExitEventsDebugee {
0N/A public static void loopComplete () {
0N/A /*
0N/A * The implementation here is deliberately inefficient
0N/A * because the debugger is still watching this method.
0N/A */
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.append ("MethodEntryExitEventsDebugee: ");
0N/A sb.append ("Executing loopComplete method for a graceful shutdown...");
0N/A String s = sb.toString();
0N/A for (int i = 0; i < s.length(); i++) {
0N/A char c = s.charAt(i);
0N/A System.out.print(c);
0N/A }
0N/A System.out.println();
0N/A }
0N/A public static void main(String[] args) {
0N/A t2 test = new t2();
0N/A for (int j = 0; j < 3; j++) {
0N/A test.sayHello1(0, j);
0N/A }
0N/A loopComplete();
0N/A }
0N/A}
0N/A
0N/A
0N/Apublic class MethodEntryExitEvents extends TestScaffold {
0N/A int sessionSuspendPolicy = EventRequest.SUSPEND_ALL;
0N/A StepRequest stepReq = null; //Only one step request allowed per thread
0N/A boolean finishedCounting = false;
0N/A
0N/A /*
0N/A * Enter main() , then t2.<init>, then sayHello[1,2,3,4,5] 15 times 3 loops,
0N/A * then loopComplete()
0N/A */
0N/A final int expectedEntryCount = 1 + 1 + (15 * 3) + 1;
0N/A int methodEntryCount = 0;
0N/A
0N/A /*
0N/A * Exit t2.<init>, then sayHello[1,2,3,4,5] 15 times 3 loopa
0N/A * (event monitoring is cancelled before we exit loopComplete() or main())
0N/A */
0N/A final int expectedExitCount = 1 + (15 * 3);
0N/A int methodExitCount = 0;
0N/A
0N/A /*
0N/A * Class patterns for which we don't want events (copied
0N/A * from the "Trace.java" example):
0N/A * http://java.sun.com/javase/technologies/core/toolsapis/jpda/
0N/A */
0N/A private String[] excludes = {"java.*", "javax.*", "sun.*",
0N/A "com.sun.*"};
0N/A
0N/A MethodEntryExitEvents (String args[]) {
0N/A super(args);
0N/A }
0N/A
0N/A private void usage(String[] args) throws Exception {
0N/A StringBuffer sb = new StringBuffer("Usage: ");
0N/A sb.append(System.getProperty("line.separator"));
0N/A sb.append(" java ");
0N/A sb.append(getClass().getName());
0N/A sb.append(" [SUSPEND_NONE | SUSPEND_EVENT_THREAD | SUSPEND_ALL]");
0N/A sb.append(" [MethodEntryExitEventsDebugee | -connect <connector options...>] ");
0N/A throw new Exception (sb.toString());
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MethodEntryExitEvents meee = new MethodEntryExitEvents (args);
0N/A meee.startTests();
0N/A }
0N/A
0N/A public void exceptionThrown(ExceptionEvent event) {
0N/A System.out.println("Exception: " + event.exception());
0N/A System.out.println(" at catch location: " + event.catchLocation());
0N/A
0N/A // Step to the catch
0N/A if (stepReq == null) {
0N/A stepReq =
0N/A eventRequestManager().createStepRequest(event.thread(),
0N/A StepRequest.STEP_MIN,
0N/A StepRequest.STEP_INTO);
stepReq.addCountFilter(1); // next step only
stepReq.setSuspendPolicy(EventRequest.SUSPEND_ALL);
}
stepReq.enable();
}
public void stepCompleted(StepEvent event) {
System.out.println("stepCompleted: line#=" +
event.location().lineNumber() +
" event=" + event);
// disable the step and then run to completion
//eventRequestManager().deleteEventRequest(event.request());
StepRequest str= (StepRequest)event.request();
str.disable();
}
public void methodEntered(MethodEntryEvent event) {
if (! finishedCounting) {
// We have to count the entry to loopComplete, but
// not the exit
methodEntryCount++;
System.out.print (" Method entry number: ");
System.out.print (methodEntryCount);
System.out.print (" : ");
System.out.println(event);
if ("loopComplete".equals(event.method().name())) {
finishedCounting = true;
}
}
}
public void methodExited(MethodExitEvent event) {
if (! finishedCounting){
methodExitCount++;
System.out.print (" Method exit number: ");
System.out.print (methodExitCount);
System.out.print (" : ");
System.out.println(event);
}
}
protected void runTests() throws Exception {
if (args.length < 1) {
usage(args);
}
//Pick up the SUSPEND_xxx in first argument
if ("SUSPEND_NONE".equals(args[0])) {
sessionSuspendPolicy = EventRequest.SUSPEND_NONE;
} else if ("SUSPEND_EVENT_THREAD".equals(args[0])) {
sessionSuspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
} else if ("SUSPEND_ALL".equals(args[0])) {
sessionSuspendPolicy = EventRequest.SUSPEND_ALL;
} else {
usage(args);
}
System.out.print("Suspend policy is: ");
System.out.println(args[0]);
// Skip the test arg
String[] args2 = new String[args.length - 1];
System.arraycopy(args, 1, args2, 0, args.length - 1);
if (args2.length < 1) {
usage(args2);
}
List argList = new ArrayList(Arrays.asList(args2));
System.out.println("run args: " + argList);
connect((String[]) argList.toArray(args2));
waitForVMStart();
try {
/*
* Ask for Exception events
*/
ExceptionRequest exceptionRequest =
eventRequestManager().createExceptionRequest(null, // refType (null == all instances)
true, // notifyCaught
true);// notifyUncaught
exceptionRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
exceptionRequest.enable();
/*
* Ask for method entry events
*/
MethodEntryRequest entryRequest =
eventRequestManager().createMethodEntryRequest();
for (int i=0; i<excludes.length; ++i) {
entryRequest.addClassExclusionFilter(excludes[i]);
}
entryRequest.setSuspendPolicy(sessionSuspendPolicy);
entryRequest.enable();
/*
* Ask for method exit events
*/
MethodExitRequest exitRequest =
eventRequestManager().createMethodExitRequest();
for (int i=0; i<excludes.length; ++i) {
exitRequest.addClassExclusionFilter(excludes[i]);
}
exitRequest.setSuspendPolicy(sessionSuspendPolicy);
exitRequest.enable();
/*
* We are now set up to receive the notifications we want.
* Here we go. This adds 'this' as a listener so
* that our handlers above will be called.
*/
listenUntilVMDisconnect();
System.out.println("All done...");
} catch (Exception ex){
ex.printStackTrace();
testFailed = true;
}
if ((methodEntryCount != expectedEntryCount) ||
(methodExitCount != expectedExitCount)) {
testFailed = true;
}
if (!testFailed) {
System.out.println();
System.out.println("MethodEntryExitEvents: passed");
System.out.print (" Method entry count: ");
System.out.println(methodEntryCount);
System.out.print (" Method exit count: ");
System.out.println(methodExitCount);
} else {
System.out.println();
System.out.println("MethodEntryExitEvents: failed");
System.out.print (" expected method entry count: ");
System.out.println(expectedEntryCount);
System.out.print (" observed method entry count: ");
System.out.println(methodEntryCount);
System.out.print (" expected method exit count: ");
System.out.println(expectedExitCount);
System.out.print (" observed method exit count: ");
System.out.println(methodExitCount);
throw new Exception("MethodEntryExitEvents: failed");
}
}
}