1 /*
  2  * Copyright (c) 2003, 2018, 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 import java.io.*;
 25 
 26 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/SingleStep/singlestep003.
 30  * VM Testbase keywords: [quick, jpda, jvmti, onload_only_caps, noras]
 31  * VM Testbase readme:
 32  * DESCRIPTION
 33  *     This test exercises the JVMTI event SingleStep.
 34  *     It verifies that no single step event will be generated from
 35  *     within native methods.
 36  *     The test works as follows. Breakpoint is set at special method
 37  *     'bpMethod()'. Upon reaching the breakpoint, agent enables
 38  *     SingleStep event generation and checks the events. The java part
 39  *     calls native method 'nativeMethod()' which calls another native
 40  *     'anotherNativeMethod()' in order to provoke the SingleStep events
 41  *     from within native methods. When 'bpMethod()' is leaved and
 42  *     accordingly, the program returns to the calling method 'runThis()',
 43  *     the agent disables the event generation.
 44  * COMMENTS
 45  *
 46  * @library /test/lib
 47  * @compile --enable-preview -source ${jdk.version} singlestep03.java
 48  * @run main/othervm/native --enable-preview -agentlib:singlestep03 singlestep03 platform
 49  * @run main/othervm/native --enable-preview -agentlib:singlestep03 singlestep03 virtual
 50  */
 51 
 52 public class singlestep03 {
 53     static {
 54         try {
 55             System.loadLibrary("singlestep03");
 56         } catch (UnsatisfiedLinkError ule) {
 57             System.err.println("Could not load \"singlestep03\" library");
 58             System.err.println("java.library.path:"
 59                 + System.getProperty("java.library.path"));
 60             throw ule;
 61         }
 62     }
 63 
 64     static volatile int result;
 65     native void nativeMethod();
 66     native void anotherNativeMethod(int i);
 67 
 68     native int check();
 69 
 70     public static void main(String[] args) throws Exception {
 71         Thread.Builder builder;
 72         if ("virtual".equals(args[0])) {
 73             builder = Thread.ofVirtual();
 74         } else {
 75             builder = Thread.ofPlatform();
 76         }
 77         Thread thread = builder.start(() -> {
 78             result = new singlestep03().runThis();
 79         });
 80         thread.join();
 81         if (result != 0) {
 82             throw new RuntimeException("Unexpected status: " + result);
 83         }
 84     }
 85 
 86     private int runThis() {
 87 
 88         System.out.println("\nReaching a breakpoint method ...\n");
 89         bpMethod();
 90         System.out.println("The breakpoint method leaved ...");
 91 
 92         return check();
 93     }
 94 
 95     /**
 96      * dummy method used to reach breakpoint, enable the SingleStep
 97      * event in the agent and provoke SingleStep for native method
 98      */
 99     private void bpMethod() {
100         nativeMethod();
101     }
102 }