1 /*
   2  * Copyright (c) 2010, 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 6426034
  27  *  @summary Instance filter doesn't filter event if it occurs in native method
  28  *
  29  *  @author Keith McGuigan
  30  *
  31  *  @library scaffold
  32  *  @modules jdk.jdi
  33  *  @run build JDIScaffold VMConnection
  34  *  @compile -XDignore.symbol.file NativeInstanceFilterTarg.java
  35  *  @run driver NativeInstanceFilter
  36  */
  37 
  38 /*
  39  *  This test tests instance filters for events generated from a native method
  40  */
  41 
  42 import java.util.*;
  43 import com.sun.jdi.*;
  44 import com.sun.jdi.event.*;
  45 import com.sun.jdi.request.*;
  46 
  47 public class NativeInstanceFilter extends JDIScaffold {
  48 
  49     static int unfilteredEvents = 0;
  50 
  51     public static void main(String args[]) throws Exception {
  52         new NativeInstanceFilter().startTests();
  53     }
  54 
  55     public NativeInstanceFilter() {
  56         super();
  57     }
  58 
  59     static EventRequestManager requestManager = null;
  60     static MethodExitRequest request = null;
  61     static ThreadReference mainThread = null;
  62 
  63     private void listen() {
  64         TargetAdapter adapter = new TargetAdapter() {
  65             EventSet set = null;
  66             ObjectReference instance = null;
  67 
  68             public boolean eventSetReceived(EventSet set) {
  69                 this.set = set;
  70                 return false;
  71             }
  72 
  73             public boolean methodExited(MethodExitEvent event) {
  74                 String name = event.method().name();
  75                 if (instance == null && name.equals("latch")) {
  76                     // Grab the instance (return value) and set up as filter
  77                     System.out.println("Setting up instance filter");
  78                     instance = (ObjectReference)event.returnValue();
  79                     requestManager.deleteEventRequest(request);
  80                     request = requestManager.createMethodExitRequest();
  81                     request.addInstanceFilter(instance);
  82                     request.addThreadFilter(mainThread);
  83                     request.enable();
  84                 } else if (instance != null && name.equals("intern")) {
  85                     // If not for the filter, this will be called twice
  86                     System.out.println("method exit event (String.intern())");
  87                     ++unfilteredEvents;
  88                 }
  89                 set.resume();
  90                 return false;
  91             }
  92         };
  93         addListener(adapter);
  94     }
  95 
  96 
  97     protected void runTests() throws Exception {
  98         String[] args = new String[2];
  99         args[0] = "-connect";
 100         args[1] = "com.sun.jdi.CommandLineLaunch:main=NativeInstanceFilterTarg";
 101 
 102         connect(args);
 103         waitForVMStart();
 104 
 105         // VM has started, but hasn't started running the test program yet.
 106         requestManager = vm().eventRequestManager();
 107         ClassPrepareEvent e = resumeToPrepareOf("NativeInstanceFilterTarg");
 108         ReferenceType referenceType = e.referenceType();
 109         mainThread = e.thread();
 110 
 111         request = requestManager.createMethodExitRequest();
 112         request.addThreadFilter(mainThread);
 113         request.enable();
 114 
 115         listen();
 116 
 117         vm().resume();
 118 
 119         waitForVMDeath();
 120 
 121         if (unfilteredEvents != 1) {
 122             throw new Exception(
 123                 "Failed: Event from native frame not filtered out.");
 124         }
 125         System.out.println("Passed: Event filtered out.");
 126     }
 127 }