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.frames_ii;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 
31 /**
32  * This class is used as debuggee application for the frames_ii002 JDI test.
33  */
34 
35 public class frames_ii002a {
36 
37     //----------------------------------------------------- templete section
38 
39     static final int PASSED = 0;
40     static final int FAILED = 2;
41     static final int PASS_BASE = 95;
42 
43     //--------------------------------------------------   log procedures
44 
45     static boolean verbMode = false;
46 
log1(String message)47     public static void log1(String message) {
48         if (verbMode)
49             System.err.println("**> mainThread: " + message);
50     }
log2(String message)51     public static void log2(String message) {
52         if (verbMode)
53             System.err.println("**> " + message);
54     }
55 
logErr(String message)56     private static void logErr(String message) {
57         if (verbMode)
58             System.err.println("!!**> mainThread: " + message);
59     }
60 
61     //====================================================== test program
62     //----------------------------------------------------   main method
63 
main(String argv[])64     public static void main (String argv[]) {
65 
66         for (int i=0; i<argv.length; i++) {
67             if ( argv[i].equals("-vbs") || argv[i].equals("-verbose") ) {
68                 verbMode = true;
69                 break;
70             }
71         }
72         log1("debuggee started!");
73 
74         // informing a debugger of readyness
75         ArgumentHandler argHandler = new ArgumentHandler(argv);
76         IOPipe pipe = argHandler.createDebugeeIOPipe();
77         pipe.println("ready");
78 
79 
80         int exitCode = PASSED;
81         for (int i = 0; ; i++) {
82 
83             String instruction;
84 
85             log1("waiting for an instruction from the debugger ...");
86             instruction = pipe.readln();
87             if (instruction.equals("quit")) {
88                 log1("'quit' recieved");
89                 break ;
90 
91             } else if (instruction.equals("newcheck")) {
92                 switch (i) {
93 
94     //------------------------------------------------------  section tested
95 
96                 case 0:
97                          Threadframes_ii002a test_thread =
98                              new Threadframes_ii002a("testedThread");
99                          log1("       thread2 is created");
100 
101                          label:
102                          synchronized (Threadframes_ii002a.lockingObject) {
103                              synchronized (Threadframes_ii002a.waitnotifyObj) {
104                                  log1("       synchronized (waitnotifyObj) { enter");
105                                  log1("       before: test_thread.start()");
106                                  test_thread.start();
107 
108                                  try {
109                                      log1("       before:   waitnotifyObj.wait();");
110                                      Threadframes_ii002a.waitnotifyObj.wait();
111                                      log1("       after:    waitnotifyObj.wait();");
112                                      pipe.println("checkready");
113                                      instruction = pipe.readln();
114                                      if (!instruction.equals("continue")) {
115                                          logErr("ERROR: unexpected instruction: " + instruction);
116                                          exitCode = FAILED;
117                                          break label;
118                                      }
119                                      pipe.println("docontinue");
120                                  } catch ( Exception e2) {
121                                      log1("       Exception e2 exception: " + e2 );
122                                      pipe.println("waitnotifyerr");
123                                  }
124                              }
125                          }
126                          log1("mainThread is out of: synchronized (lockingObject) {");
127 
128                          break ;
129 
130     //-------------------------------------------------    standard end section
131 
132                 default:
133                                 pipe.println("checkend");
134                                 break ;
135                 }
136 
137             } else {
138                 logErr("ERRROR: unexpected instruction: " + instruction);
139                 exitCode = FAILED;
140                 break ;
141             }
142         }
143 
144         System.exit(exitCode + PASS_BASE);
145     }
146 }
147 
148 class Threadframes_ii002a extends Thread {
149 
Threadframes_ii002a(String threadName)150     public Threadframes_ii002a(String threadName) {
151         super(threadName);
152     }
153 
154     public static Object waitnotifyObj = new Object();
155     public static Object lockingObject = new Object();
156 
157     private int i1 = 0, i2 = 10;
158 
run()159     public void run() {
160         log("method 'run' enter");
161         synchronized (waitnotifyObj)                                    {
162             log("entered into block:  synchronized (waitnotifyObj)");
163             waitnotifyObj.notify();                                     }
164         log("exited from block:  synchronized (waitnotifyObj)");
165         synchronized (lockingObject)                                    {
166             log("entered into block:  synchronized (lockingObject)");   }
167         log("exited from block:  synchronized (lockingObject)");
168         i1++;
169         i1++;
170         i1++;
171         i1++;
172         i1++;
173         i2--;
174 //        log("call to the method 'runt1'");
175 //        runt1();
176 //        log("returned from the method 'runt1'");
177         log("method 'run' exit");
178         return;
179     }
180 /*
181     public void runt1() {
182         log("method 'runt1' enter");
183         i1++;
184         i2--;
185         log("call to the method 'runt2'");
186         runt2();
187         log("returned from the method 'runt2'");
188         i1++;
189         i2--;
190         log("method 'runt1' exit");
191         return;
192     }
193 
194     public void runt2() {
195         log("method 'runt2' enter");
196         i1++;
197         i2--;
198         log("method 'run2t' exit");
199         return;
200     }
201 */
202     public static final int breakpointLineNumber0 = 9;
203     public static final int breakpointLineNumber1 = 3;
204     public static final int breakpointLineNumber3 = 7;
205 
206     public static final int breakpointLineNumber2 = 2;
207 
log(String str)208     void log(String str) {
209         frames_ii002a.log2("thread2: " + str);
210     }
211 
212 }
213