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 popframes002 JDI test.
32  */
33 
34 public class popframes002a {
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 Thread2popframes002a thread2 = null;
58     static Thread3popframes002a thread3 = null;
59 
60     //------------------------------------------------------ common section
61 
62     static int instruction = 1;
63     static int end         = 0;
64                                    //    static int quit        = 0;
65                                    //    static int continue    = 2;
66     static int maxInstr    = 1;    // 2;
67 
68     static int lineForComm = 2;
69 
methodForCommunication()70     private static void methodForCommunication() {
71         int i1 = instruction;
72         int i2 = i1;
73         int i3 = i2;
74     }
75     //----------------------------------------------------   main method
76 
main(String argv[])77     public static void main (String argv[]) {
78 
79         argHandler = new ArgumentHandler(argv);
80         log = argHandler.createDebugeeLog();
81 
82         log1("debuggee started!");
83 
84         int exitCode = PASSED;
85 
86 
87         label0:
88             {
89 
90     //------------------------------------------------------  section tested
91 
92                  thread2 =  new Thread2popframes002a("thread2");
93                  log1("       thread2 is created");
94                  thread3 =  new Thread3popframes002a("thread3");
95                  log1("       thread3 is created");
96 
97                  synchronized (lockingObject1) {
98                      synchronized (lockingObject3) {
99                          synchronized (lockingObject2) {
100                              log1("      thread2.start()");
101                              if ( threadStart(thread2) != PASSED )
102                                  break label0;
103 
104                              log1("       thread3.start()");
105                              if ( threadStart(thread3) != PASSED )
106                                  break label0;
107 
108                              log1("      methodForCommunication();  ----1");
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("      methodForCommunication();  ----2");
123                  methodForCommunication();
124             }
125     //-------------------------------------------------    standard end section
126 
127         System.exit(exitCode + PASS_BASE);
128     }
129 
130     static Object waitnotifyObj = new Object();
131     static Object lockingObject = new Object();
132 
threadStart(Thread t)133     static int threadStart(Thread t) {
134         synchronized (waitnotifyObj) {
135             t.start();
136             try {
137                 log1("       before:   waitnotifyObj.wait();");
138                 waitnotifyObj.wait();
139                 log1("       after:    waitnotifyObj.wait();");
140             } catch ( Exception e) {
141                 logErr("       Exception : " + e );
142                 return FAILED;
143             }
144         }
145         return PASSED;
146     }
147 
148     static Object lockingObject1 = new Object();
149 
150     static int testVar1 = 0;
151     static int testVar2 = 0;
152     static int breakpointLine = 4;
153 
poppedMethod()154     static synchronized void poppedMethod() {
155         log1("poppedMethod entered by the thread : " + Thread.currentThread().getName() );
156         synchronized (lockingObject1) { lockingObject1.notify(); }
157         testVar1 +=1;
158         testVar2 +=1;
159         testVar2 +=1;
160 
161         log1("poppedMethod:  exit");
162         return;
163     }
164 
165     static Object lockingObject2 = new Object();
166 
167     static class Thread2popframes002a extends Thread {
168 
Thread2popframes002a(String threadName)169         public Thread2popframes002a(String threadName) {
170             super(threadName);
171         }
172 
run()173         public void run() {
174                 log1("thread2: method 'run' enter");
175                 synchronized (waitnotifyObj) {
176                     log1("thread2: entered into block:  synchronized (waitnotifyObj)");
177                     waitnotifyObj.notify();
178                 }
179                 log1("thread2: exited from block:  synchronized (waitnotifyObj)");
180 
181             synchronized (lockingObject2) {
182                 log1("thread2: before: 'poppedMethod()'");
183                 poppedMethod();
184                 log1("thread2: after:  'poppedMethod()'");
185             }
186             return;
187         }
188     }
189 
190     static Object lockingObject3 = new Object();
191 
192     static class Thread3popframes002a extends Thread {
193 
Thread3popframes002a(String threadName)194         public Thread3popframes002a(String threadName) {
195             super(threadName);
196         }
197 
run()198         public void run() {
199                 log1("thread3: method 'run' enter");
200                 synchronized (waitnotifyObj) {
201                     log1("thread3: entered into block:  synchronized (waitnotifyObj)");
202                     waitnotifyObj.notify();
203                 }
204                 log1("thread3: exited from block:  synchronized (waitnotifyObj)");
205 
206             synchronized (lockingObject3) {
207                 log1("thread3: before: 'poppedMethod()'");
208                 poppedMethod();
209                 log1("thread3: after:  'poppedMethod()'");
210             }
211             return;
212         }
213     }
214 
215 }
216