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/GetFrameLocation/frameloc001.
 28  * VM Testbase keywords: [jpda, jvmti, noras]
 29  * VM Testbase readme:
 30  * DESCRIPTION
 31  *     The test exercise JVMTI function GetFrameLocation.
 32  *     The test debug agent calls the function from the hook
 33  *     function and native method for the test cases:
 34  *     - current and other threads;
 35  *     - currently executing instructions:
 36  *         1) astore_2 after catching exception (in frameloc01a.meth01)
 37  *         2) monitorenter (in frameloc01a.run)
 38  * COMMENTS
 39  *     Fixed according to the rfe 4388972.
 40  *     Tested bytecode was relocated to frameloc01a.jasm
 41  *     adjusting the expected locations.
 42  *     Fixed according to 4669812 bug.
 43  *     Ported from JVMDI.
 44  *
 45  * @library /test/lib
 46  * @compile frameloc01a.jasm
 47  * @run main/othervm/native -agentlib:frameloc01 frameloc01
 48  */
 49 
 50 import java.io.PrintStream;
 51 
 52 public class frameloc01 {
 53 
 54     final static int JCK_STATUS_BASE = 95;
 55 
 56     static {
 57         try {
 58             System.loadLibrary("frameloc01");
 59         } catch (UnsatisfiedLinkError ule) {
 60             System.err.println("Could not load frameloc01 library");
 61             System.err.println("java.library.path:"
 62                 + System.getProperty("java.library.path"));
 63             throw ule;
 64         }
 65     }
 66 
 67     native static void getReady(Class cls);
 68     native static boolean checkFrame01(Thread thr, Class cls, boolean mustPass);
 69     native static int getRes();
 70 
 71     static int fld = 0;
 72     static Object lock1 = new Object();
 73     static Object lock2 = new Object();
 74 
 75     public static void main(String args[]) {
 76 
 77 
 78         // produce JCK-like exit status.
 79         System.exit(run(args, System.out) + JCK_STATUS_BASE);
 80     }
 81 
 82     public static int run(String argv[], PrintStream ref) {
 83         frameloc01a thr = new frameloc01a();
 84 
 85         getReady(frameloc01a.class);
 86         thr.meth01(2000);
 87 
 88         synchronized (lock2) {
 89             synchronized (lock1) {
 90                 thr.start();
 91                 try {
 92                     lock1.wait();
 93                 } catch (InterruptedException e) {
 94                     throw new Error("Unexpected " + e);
 95                 }
 96             }
 97             waitForChildThread(thr);
 98             checkFrame01(thr, frameloc01a.class, true);
 99         }
100 
101         return getRes();
102     }
103 
104     private static void waitForChildThread(frameloc01a thr) {
105         // Wait for child thread to reach expected position. Wait up to 5 seconds.
106         final int MAX_WAIT_MS = 5000;
107         int sumWaitMs = 0;
108         while (!checkFrame01(thr, frameloc01a.class, false) && sumWaitMs <= MAX_WAIT_MS) {
109             try {
110                 System.out.println("Waited: " + sumWaitMs);
111                 final int sleepMs = 20;
112                 sumWaitMs += sleepMs;
113                 Thread.sleep(sleepMs);
114             } catch (InterruptedException e) {
115                 e.printStackTrace();
116                 throw new Error("Unexpected " + e);
117             }
118         }
119     }
120 }