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 /*
 25  * @test
 26  *
 27  * @summary converted from VM Testbase nsk/jvmti/GetStackTrace/getstacktr003.
 28  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 29  * VM Testbase readme:
 30  * DESCRIPTION
 31  *     The test exercises JVMTI function GetStackTrace for a non current thread.
 32  *     The test checks the following:
 33  *       - if function returns the expected frame of a Java method
 34  *       - if function returns the expected frame of a JNI method
 35  *       - if function returns the expected number of frames.
 36  * COMMENTS
 37  *     Ported from JVMDI.
 38  *
 39  * @library /test/lib
 40  * @compile --enable-preview -source ${jdk.version} getstacktr03.java
 41  * @run main/othervm/native --enable-preview -agentlib:getstacktr03 getstacktr03
 42  */
 43 
 44 public class getstacktr03 {
 45 
 46     static {
 47         System.loadLibrary("getstacktr03");
 48     }
 49 
 50     native static void chain();
 51     native static int check(Thread thread);
 52 
 53     public static Object lockIn = new Object();
 54     public static Object lockOut = new Object();
 55 
 56     public static void main(String args[]) {
 57         Thread thread = Thread.ofPlatform().unstarted(new Task());
 58         test(thread);
 59 
 60         Thread vthread = Thread.ofVirtual().unstarted(new Task());
 61         test(vthread);
 62     }
 63 
 64     public static void test(Thread thr) {
 65         synchronized (lockIn) {
 66             thr.start();
 67             try {
 68                 lockIn.wait();
 69             } catch (InterruptedException e) {
 70                 throw new Error("Unexpected " + e);
 71             }
 72         }
 73 
 74         synchronized (lockOut) {
 75             check(thr);
 76             lockOut.notify();
 77         }
 78 
 79         try {
 80             thr.join();
 81         } catch (InterruptedException e) {
 82             throw new Error("Unexpected " + e);
 83         }
 84     }
 85 
 86     static void dummy() {
 87         synchronized (lockOut) {
 88             synchronized (lockIn) {
 89                 lockIn.notify();
 90             }
 91             try {
 92                 lockOut.wait();
 93             } catch (InterruptedException e) {
 94                 throw new Error("Unexpected " + e);
 95             }
 96         }
 97     }
 98     static class Task implements Runnable {
 99         @Override
100         public void run() {
101             getstacktr03.chain();
102         }
103     }
104 }