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 package nsk.jvmti.unit.MethodBind;
25 
26 import java.io.PrintStream;
27 
28 public class JvmtiTest {
29 
30     final static int JCK_STATUS_BASE = 95;
31     final static int THREADS_LIMIT = 200;
32     final static String NAME_PREFIX = "JvmtiTest-";
33     static int fail_id = 0;
34 
35     static {
36         try {
37             System.loadLibrary("MethodBind");
38         } catch (UnsatisfiedLinkError ule) {
39             System.err.println("Could not load MethodBind library");
40             System.err.println("java.library.path:"
41                 + System.getProperty("java.library.path"));
42             throw ule;
43         }
44     }
45 
GetResult()46     native static int GetResult();
CreateRawMonitor(int i)47     native static void CreateRawMonitor(int i);
RawMonitorEnter(int i)48     native static void RawMonitorEnter(int i);
RawMonitorExit(int i)49     native static void RawMonitorExit(int i);
RawMonitorWait(int i)50     native static void RawMonitorWait(int i);
GetStackTrace(TestThread t)51     native static void GetStackTrace(TestThread t);
GetFrameCount(TestThread t)52     native static int GetFrameCount(TestThread t);
53 
54 
55     static volatile int thrCount = 0;
56 
main(String args[])57     public static void main(String args[]) {
58         args = nsk.share.jvmti.JVMTITest.commonInit(args);
59 
60         // produce JCK-like exit status.
61         System.exit(run(args, System.out) + JCK_STATUS_BASE);
62     }
63 
run(String args[], PrintStream out)64     public static int run(String args[], PrintStream out) {
65         CreateRawMonitor(0);
66         CreateRawMonitor(4);
67         TestThread t[] = new TestThread[THREADS_LIMIT];
68 
69         RawMonitorEnter(4);
70 
71         for (int i=0; i < THREADS_LIMIT; i++) {
72             t[i] = new TestThread(NAME_PREFIX + thrCount);
73             t[i].start();
74         }
75 
76 
77         for (int i=0; i < THREADS_LIMIT-1; i++) {
78             GetStackTrace(t[i]);
79             GetFrameCount(t[i]);
80         }
81 
82         RawMonitorExit(4);
83 
84         try {
85           for (int i=0; i < THREADS_LIMIT; i++) {
86               t[i].join();
87           }
88 
89         } catch (InterruptedException e) {
90             throw new Error("Unexpected: " + e);
91         }
92         return GetResult() + fail_id;
93     }
94 
95     static class TestThread extends Thread {
96         static int counter=0;
97         int ind;
TestThread(String name)98         public TestThread(String name) {
99             super(name);
100         }
run()101         public void run() {
102             thrCount++;
103             foo(0);
104         }
foo(int i)105         public void foo(int i) {
106             i++;
107             if ( i < 10 ) {
108                 foo(i);
109             } else {
110                 RawMonitorEnter(4);
111                 RawMonitorExit(4);
112             }
113 
114         }
115     }
116 }
117