1 /* 2 * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4312961 27 * @summary Verify that an instance filter on a MethodEntryRequest works 28 * properly. 29 * 30 * @author Robert Field/Jim Holmlund 31 * 32 * @run build TestScaffold VMConnection TargetAdapter TargetListener 33 * @run compile -g InstanceFilter.java 34 * @run driver InstanceFilter 35 */ 36 import com.sun.jdi.*; 37 import com.sun.jdi.event.*; 38 import com.sun.jdi.request.*; 39 40 class InstanceFilterTarg { 41 static InstanceFilterTarg first = new InstanceFilterTarg(); 42 static InstanceFilterTarg second = new InstanceFilterTarg(); 43 static InstanceFilterTarg third = new InstanceFilterTarg(); 44 45 public static void main(String args[]) { 46 start(); 47 } 48 49 static void start() { 50 first.go(); 51 second.go(); 52 third.go(); 53 } 54 55 void go() { 56 one(); 57 two(); 58 three(); 59 } 60 61 void one() { 62 } 63 64 void two() { 65 } 66 67 void three() { 68 } 69 } 70 71 public class InstanceFilter extends TestScaffold { 72 ReferenceType targetClass; 73 74 ObjectReference theInstance; 75 MethodEntryRequest methodEntryRequest; 76 int methodCount = 0; 77 // These are the methods for which we expect to get MethodEntryEvents. 78 String[] expectedMethods = new String[] { "go", "one", "two", "three"}; 79 80 public static void main(String args[]) throws Exception { 81 new InstanceFilter(args).startTests(); 82 } 83 84 InstanceFilter(String args[]) throws Exception { 85 super(args); 86 } 87 88 /** 89 * Override TestScaffold.methodEntered. This should get called 90 * once for each method named in 'expectedMethods', and for 91 * the instance that we select to filter on. 92 */ 93 public void methodEntered(MethodEntryEvent event) { 94 if (testFailed) { 95 return; 96 } 97 // Find the instance and verify that it is 98 // the one we want. 99 ObjectReference theThis; 100 try { 101 theThis = event.thread().frame(0).thisObject(); 102 } catch (IncompatibleThreadStateException ee) { 103 failure("FAILED: Exception occured in methodEntered: " + ee); 104 return; 105 } 106 if (theThis == null) { 107 // This happens when the thread has exited. 108 methodEntryRequest.disable(); 109 return; 110 } 111 112 if (!theThis.equals(theInstance)) { 113 failure("FAILED: Got a hit on a non-selected instance"); 114 } 115 116 // fail if we don't get called for each of the expected methods 117 { 118 String methodStr = event.location().method().name(); 119 120 if (methodCount >= expectedMethods.length) { 121 failure("FAILED: Got too many methodEntryEvents"); 122 } else if (methodStr.indexOf(expectedMethods[methodCount]) == -1) { 123 failure("FAILED: Expected method: " + expectedMethods[methodCount]); 124 } 125 methodCount++; 126 println("Method: " + methodStr); 127 } 128 } 129 130 protected void runTests() throws Exception { 131 132 BreakpointEvent bpe = startTo("InstanceFilterTarg", "go", "()V"); 133 targetClass = bpe.location().declaringType(); 134 135 Field field = targetClass.fieldByName("second"); 136 theInstance = (ObjectReference)(targetClass.getValue(field)); 137 138 EventRequestManager mgr = vm().eventRequestManager(); 139 methodEntryRequest = mgr.createMethodEntryRequest(); 140 methodEntryRequest.addInstanceFilter(theInstance); 141 methodEntryRequest.enable(); 142 143 listenUntilVMDisconnect(); 144 145 if (!testFailed && methodCount < expectedMethods.length) { 146 failure("FAILED: Expected " + expectedMethods.length + " events, only got " 147 + methodCount); 148 } 149 if (!testFailed) { 150 println("InstanceFilter: passed"); 151 } else { 152 throw new Exception("InstanceFilter: failed"); 153 } 154 } 155 }