1 /*
  2  * Copyright (c) 2003, 2021, 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/getstacktr008.
 28  * VM Testbase keywords: [quick, jpda, jvmti, noras, redefine]
 29  * VM Testbase readme:
 30  * DESCRIPTION
 31  *     The test exercises JVMTI function GetStackTrace.
 32  *     The test starts a new thread, does some nested calls with a native
 33  *     call in the middle, and stops at breakpoint.
 34  *     Then the test does the following:
 35  *         - checks the stack on expected frames
 36  *         - steps
 37  *         - checks the stack on expected frames
 38  *         - pops frame
 39  *         - checks the stack on expected frames
 40  *         - redefines class
 41  *         - checks the stack on expected frames
 42  *         - checks the stack on expected frames just before
 43  *           returning from the native call.
 44  * COMMENTS
 45  *     Ported from JVMDI.
 46  *
 47  * @library /test/lib
 48  * @compile --enable-preview -source ${jdk.version} getstacktr08.java
 49  * @run main/othervm/native --enable-preview -agentlib:getstacktr08 getstacktr08
 50  */
 51 
 52 
 53 import java.io.File;
 54 import java.io.InputStream;
 55 
 56 public class getstacktr08 {
 57 
 58     final static String fileName =
 59         TestThread.class.getName().replace('.', File.separatorChar) + ".class";
 60 
 61     static {
 62         System.loadLibrary("getstacktr08");
 63     }
 64 
 65     native static void getReady(Class clz, byte bytes[]);
 66     native static void nativeChain(Class clz);
 67 
 68     public static void main(String args[]) throws Exception {
 69         ClassLoader cl = getstacktr08.class.getClassLoader();
 70         Thread thread = Thread.ofPlatform().unstarted(new TestThread());
 71 
 72         InputStream in = cl.getSystemResourceAsStream(fileName);
 73         byte[] bytes = new byte[in.available()];
 74         in.read(bytes);
 75         in.close();
 76 
 77         getReady(TestThread.class, bytes);
 78 
 79         thread.start();
 80         thread.join();
 81         /* PopFrame not implemented for virtual threads yet.
 82         Thread vThread = Thread.ofVirtual().unstarted(new TestThread());
 83         getReady(TestThread.class, bytes);
 84         vThread.start();
 85         vThread.join();
 86         */
 87 
 88     }
 89 
 90     static class TestThread implements Runnable {
 91         public void run() {
 92             chain1();
 93         }
 94 
 95         static void chain1() {
 96             chain2();
 97         }
 98 
 99         static void chain2() {
100             chain3();
101         }
102 
103         static void chain3() {
104             nativeChain(TestThread.class);
105         }
106 
107         static void chain4() {
108             chain5();
109         }
110         static void chain5() {
111             checkPoint();
112         }
113 
114         // dummy method to be breakpointed in agent
115         static void checkPoint() {
116         }
117     }
118 }