1 /*
2  * Copyright (c) 2001, 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.jdi.ThreadReference.popFrames;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 /**
31  * This class is used as debuggee application for the popframes003 JDI test.
32  */
33 
34 public class popframes003a {
35 
36     //----------------------------------------------------- templete section
37 
38     static final int PASSED = 0;
39     static final int FAILED = 2;
40     static final int PASS_BASE = 95;
41 
42     static ArgumentHandler argHandler;
43     static Log log;
44 
45     //--------------------------------------------------   log procedures
46 
log1(String message)47     private static void log1(String message) {
48         log.display("**> debuggee: " + message);
49     }
50 
logErr(String message)51     private static void logErr(String message) {
52         log.complain("**> debuggee: " + message);
53     }
54 
55     //====================================================== test program
56 
57     static Thread2popframes003a thread2 = null;
58     static Thread3popframes003a thread3 = null;
59 
60     //------------------------------------------------------ common section
61     static int instruction = 1;
62     static int end         = 0;
63                                    //    static int quit        = 0;
64                                    //    static int continue    = 2;
65     static int maxInstr    = 1;    // 2;
66 
67     static int lineForComm = 2;
methodForCommunication()68     private static void methodForCommunication() {
69             int i1 = instruction;
70             int i2 = i1;
71             int i3 = i2;
72     }
73 
74     //----------------------------------------------------   main method
75 
main(String argv[])76     public static void main (String argv[]) {
77 
78         argHandler = new ArgumentHandler(argv);
79         log = argHandler.createDebugeeLog();
80 
81         log1("debuggee started!");
82 
83         int exitCode = PASSED;
84 
85 
86         label0:
87             {
88                  thread2 =  new Thread2popframes003a("thread2");
89                  log1("       thread2 is created");
90                  thread3 =  new Thread3popframes003a("thread3");
91                  log1("       thread3 is created");
92 
93                  synchronized (lockingObject1) {
94                      synchronized (lockingObject3) {
95                          synchronized (lockingObject2) {
96                              log1("      thread2.start()");
97                              if ( threadStart(thread2) != PASSED )
98                                  break label0;
99 
100                              log1("       thread3.start()");
101                              if ( threadStart(thread3) != PASSED )
102                                  break label0;
103 
104                              // to get classes Popped&ForCommunication prepared
105                              Popped.poppedMethod();
106                              ForCommunication.methodForCommunication();
107 
108                              log1("      methodForCommunication();");
109                              methodForCommunication();
110                          }
111 
112                          log1("      before: lockingObject1.wait();");
113                          try {
114                               lockingObject1.wait();
115                          } catch ( InterruptedException e ) {
116                               logErr("       Exception : " + e );
117                               break label0;
118                          }
119                          log1("      after: lockingObject1.wait();");
120                      }
121 
122                      log1("      ForCommunication.methodForCommunication(); --1");
123                      ForCommunication.methodForCommunication();
124                  }
125                  log1("      ForCommunication.methodForCommunication(); --2");
126                  ForCommunication.methodForCommunication();
127             }
128 
129         System.exit(exitCode + PASS_BASE);
130     }
131 
132 
133     static int testVar1 = 0;
134     static int testVar2 = 0;
135 
136     static class Popped {
137 
138     static int breakpointLine = 4;
139 
poppedMethod()140         static synchronized void poppedMethod() {
141             log1("poppedMethod entered by the thread : " + Thread.currentThread().getName() );
142             synchronized (lockingObject1) { lockingObject1.notify(); }
143             testVar1 +=1;
144             testVar2 +=1;
145             testVar2 +=1;
146 
147             log1("poppedMethod:  exit");
148             return;
149         }
150     }
151 
152     static class ForCommunication {
153 
methodForCommunication()154         public static void methodForCommunication() {
155             int i1 = instruction;
156             int i2 = i1;
157             int i3 = i2;
158         }
159     }
160 
161     static Object waitnotifyObj = new Object();
162     static Object lockingObject = new Object();
163 
threadStart(Thread t)164     static int threadStart(Thread t) {
165         synchronized (waitnotifyObj) {
166             t.start();
167             try {
168                 log1("       before:   waitnotifyObj.wait();");
169                 waitnotifyObj.wait();
170                 log1("       after:    waitnotifyObj.wait();");
171             } catch ( Exception e) {
172                 logErr("       Exception : " + e );
173                 return FAILED;
174             }
175         }
176         return PASSED;
177     }
178 
179     static Object lockingObject1 = new Object();
180 
181     static Object lockingObject2 = new Object();
182 
183     static class Thread2popframes003a extends Thread {
184 
Thread2popframes003a(String threadName)185         public Thread2popframes003a(String threadName) {
186             super(threadName);
187         }
188 
run()189         public void run() {
190                 log1("thread2: method 'run' enter");
191                 synchronized (waitnotifyObj) {
192                     log1("thread2: entered into block:  synchronized (waitnotifyObj)");
193                     waitnotifyObj.notify();
194                 }
195                 log1("thread2: exited from block:  synchronized (waitnotifyObj)");
196 
197             synchronized (lockingObject2) {
198                 log1("thread2: before: 'poppedMethod()'");
199                 Popped.poppedMethod();
200                 log1("thread2: after:  'poppedMethod()'");
201             }
202             return;
203         }
204     }
205 
206     static Object lockingObject3 = new Object();
207 
208     static class Thread3popframes003a extends Thread {
209 
Thread3popframes003a(String threadName)210         public Thread3popframes003a(String threadName) {
211             super(threadName);
212         }
213 
run()214         public void run() {
215                 log1("thread3: method 'run' enter");
216                 synchronized (waitnotifyObj) {
217                     log1("thread3: entered into block:  synchronized (waitnotifyObj)");
218                     waitnotifyObj.notify();
219                 }
220                 log1("thread3: exited from block:  synchronized (waitnotifyObj)");
221 
222             synchronized (lockingObject3) {
223                 log1("thread3: before: 'poppedMethod()'");
224                 Popped.poppedMethod();
225                 log1("thread3: after:  'poppedMethod()'");
226             }
227             return;
228         }
229     }
230 
231 }
232