0N/A/*
2362N/A * Copyright (c) 2001, 2007, Oracle and/or its affiliates. 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * 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);
0N/A stepReq.addCountFilter(1); // next step only
0N/A stepReq.setSuspendPolicy(EventRequest.SUSPEND_ALL);
0N/A }
0N/A stepReq.enable();
0N/A }
0N/A public void stepCompleted(StepEvent event) {
0N/A System.out.println("stepCompleted: line#=" +
0N/A event.location().lineNumber() +
0N/A " event=" + event);
0N/A // disable the step and then run to completion
0N/A //eventRequestManager().deleteEventRequest(event.request());
0N/A StepRequest str= (StepRequest)event.request();
0N/A str.disable();
0N/A }
0N/A public void methodEntered(MethodEntryEvent event) {
0N/A if (! finishedCounting) {
0N/A // We have to count the entry to loopComplete, but
0N/A // not the exit
0N/A methodEntryCount++;
0N/A System.out.print (" Method entry number: ");
0N/A System.out.print (methodEntryCount);
0N/A System.out.print (" : ");
0N/A System.out.println(event);
0N/A if ("loopComplete".equals(event.method().name())) {
0N/A finishedCounting = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void methodExited(MethodExitEvent event) {
0N/A if (! finishedCounting){
0N/A methodExitCount++;
0N/A System.out.print (" Method exit number: ");
0N/A System.out.print (methodExitCount);
0N/A System.out.print (" : ");
0N/A System.out.println(event);
0N/A }
0N/A }
0N/A
0N/A protected void runTests() throws Exception {
0N/A if (args.length < 1) {
0N/A usage(args);
0N/A }
0N/A //Pick up the SUSPEND_xxx in first argument
0N/A if ("SUSPEND_NONE".equals(args[0])) {
0N/A sessionSuspendPolicy = EventRequest.SUSPEND_NONE;
0N/A } else if ("SUSPEND_EVENT_THREAD".equals(args[0])) {
0N/A sessionSuspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
0N/A } else if ("SUSPEND_ALL".equals(args[0])) {
0N/A sessionSuspendPolicy = EventRequest.SUSPEND_ALL;
0N/A } else {
0N/A usage(args);
0N/A }
0N/A System.out.print("Suspend policy is: ");
0N/A System.out.println(args[0]);
0N/A
0N/A // Skip the test arg
0N/A String[] args2 = new String[args.length - 1];
0N/A System.arraycopy(args, 1, args2, 0, args.length - 1);
0N/A
0N/A if (args2.length < 1) {
0N/A usage(args2);
0N/A }
0N/A List argList = new ArrayList(Arrays.asList(args2));
0N/A System.out.println("run args: " + argList);
0N/A connect((String[]) argList.toArray(args2));
0N/A waitForVMStart();
0N/A
0N/A try {
0N/A
0N/A /*
0N/A * Ask for Exception events
0N/A */
0N/A ExceptionRequest exceptionRequest =
0N/A eventRequestManager().createExceptionRequest(null, // refType (null == all instances)
0N/A true, // notifyCaught
0N/A true);// notifyUncaught
0N/A exceptionRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
0N/A exceptionRequest.enable();
0N/A
0N/A /*
0N/A * Ask for method entry events
0N/A */
0N/A MethodEntryRequest entryRequest =
0N/A eventRequestManager().createMethodEntryRequest();
0N/A for (int i=0; i<excludes.length; ++i) {
0N/A entryRequest.addClassExclusionFilter(excludes[i]);
0N/A }
0N/A entryRequest.setSuspendPolicy(sessionSuspendPolicy);
0N/A entryRequest.enable();
0N/A
0N/A /*
0N/A * Ask for method exit events
0N/A */
0N/A MethodExitRequest exitRequest =
0N/A eventRequestManager().createMethodExitRequest();
0N/A
0N/A for (int i=0; i<excludes.length; ++i) {
0N/A exitRequest.addClassExclusionFilter(excludes[i]);
0N/A }
0N/A exitRequest.setSuspendPolicy(sessionSuspendPolicy);
0N/A exitRequest.enable();
0N/A
0N/A /*
0N/A * We are now set up to receive the notifications we want.
0N/A * Here we go. This adds 'this' as a listener so
0N/A * that our handlers above will be called.
0N/A */
0N/A
0N/A listenUntilVMDisconnect();
0N/A System.out.println("All done...");
0N/A
0N/A } catch (Exception ex){
0N/A ex.printStackTrace();
0N/A testFailed = true;
0N/A }
0N/A
0N/A if ((methodEntryCount != expectedEntryCount) ||
0N/A (methodExitCount != expectedExitCount)) {
0N/A testFailed = true;
0N/A }
0N/A if (!testFailed) {
0N/A System.out.println();
0N/A System.out.println("MethodEntryExitEvents: passed");
0N/A System.out.print (" Method entry count: ");
0N/A System.out.println(methodEntryCount);
0N/A System.out.print (" Method exit count: ");
0N/A System.out.println(methodExitCount);
0N/A } else {
0N/A System.out.println();
0N/A System.out.println("MethodEntryExitEvents: failed");
0N/A System.out.print (" expected method entry count: ");
0N/A System.out.println(expectedEntryCount);
0N/A System.out.print (" observed method entry count: ");
0N/A System.out.println(methodEntryCount);
0N/A System.out.print (" expected method exit count: ");
0N/A System.out.println(expectedExitCount);
0N/A System.out.print (" observed method exit count: ");
0N/A System.out.println(methodExitCount);
0N/A throw new Exception("MethodEntryExitEvents: failed");
0N/A }
0N/A }
0N/A}