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.StackFrame.visibleVariableByName;
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 visiblevarbyname002 JDI test.
33  */
34 
35 public class visiblevarbyname002a {
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                          Threadvisiblevarbyname002a test_thread =
98                              new Threadvisiblevarbyname002a("testedThread");
99                          log1("       thread2 is created");
100 
101                          label:
102                          synchronized (Threadvisiblevarbyname002a.lockingObject) {
103                              synchronized (Threadvisiblevarbyname002a.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                                      Threadvisiblevarbyname002a.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 Threadvisiblevarbyname002a extends Thread {
149 
Threadvisiblevarbyname002a(String threadName)150     public Threadvisiblevarbyname002a(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         int invi_i2 = 0;
170         i2--;
171         log("call to 'runt1'");
172         runt1();
173         log("returned from 'runt1'");
174         log("method 'run' exit");
175         return;
176     }
177     int invi_i0 = 0;
178 
runt1()179     public static void runt1() {
180 
181         boolean bl1 = true,  bl2 = false;
182         byte    bt1 = 0,     bt2 = 1;
183         char    ch1 = 0,     ch2 = 1;
184         double  db1 = 0.0d,  db2 = 1.0d;
185         float   fl1 = 0.0f,  fl2 = 1.0f;
186         int     in1 = 0,     in2 = 1;
187         long    ln1 = 0,     ln2 = 1;
188         short   sh1 = 0,     sh2 = 1;
189         int vi_i1 = 0;
190         log("method 'runt1' enter");
191         log("call to 'runt2' ");
192         runt2();
193         log("returned from 'runt2' ");
194         log("method 'runt1' body");
195         log("method 'runt1' exit");
196         int invi_i3 = 0;
197         return;
198     }
199 
runt2()200     public static void runt2() {
201 
202         boolean bl3 = true, bl4 = false;
203 
204         log("method 'runt2' enter");
205         bl3 = false;
206         bl4 = true;
207         log("method 'runt2' body");
208         log("method 'runt2' exit");
209         return;
210     }
211 
212     public static final int breakpointLineNumber1 = 12;
213     public static final int breakpointLineNumber2 = 2;
214 //    public static final int breakpointLineNumber3 = 7;
215 
216 
log(String str)217     static void log(String str) {
218         visiblevarbyname002a.log2("thread2: " + str);
219     }
220 
221 }
222