1 /*
   2  * Copyright (c) 2000, 2015, 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 or when a
 108             // static method is called. Setting an instance
 109             // filter does not prevent this event from being
 110             // emitted with a this that is null.
 111             methodEntryRequest.disable();
 112             return;
 113         }
 114 
 115         if (!theThis.equals(theInstance)) {
 116             failure("FAILED: Got a hit on a non-selected instance");
 117         }
 118 
 119         // fail if we don't get called for each of the expected methods
 120         {
 121             String methodStr = event.location().method().name();
 122 
 123             if (methodCount >= expectedMethods.length) {
 124                 failure("FAILED: Got too many methodEntryEvents");
 125             } else if (methodStr.indexOf(expectedMethods[methodCount]) == -1) {
 126                 failure("FAILED: Expected method: " + expectedMethods[methodCount]);
 127             }
 128             methodCount++;
 129             println("Method: " + methodStr);
 130         }
 131     }
 132 
 133     protected void runTests() throws Exception {
 134 
 135         BreakpointEvent bpe = startTo("InstanceFilterTarg", "go", "()V");
 136         targetClass = bpe.location().declaringType();
 137 
 138         Field field = targetClass.fieldByName("second");
 139         theInstance = (ObjectReference)(targetClass.getValue(field));
 140 
 141         EventRequestManager mgr = vm().eventRequestManager();
 142         methodEntryRequest = mgr.createMethodEntryRequest();
 143         methodEntryRequest.addInstanceFilter(theInstance);
 144         // Thread filter is needed to prevent MethodEntry events
 145         // to be emitted by the debugee when a static method
 146         // is called on any thread.
 147         methodEntryRequest.addThreadFilter(bpe.thread());
 148         methodEntryRequest.enable();
 149 
 150         listenUntilVMDisconnect();
 151 
 152         if (!testFailed && methodCount < expectedMethods.length) {
 153             failure("FAILED: Expected " + expectedMethods.length + " events, only got "
 154                     + methodCount);
 155         }
 156         if (!testFailed) {
 157             println("InstanceFilter: passed");
 158         } else {
 159             throw new Exception("InstanceFilter: failed");
 160         }
 161     }
 162 }