1 /*
2  * Copyright (c) 2002, 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.getValues;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 
30 //    THIS TEST IS LINE NUMBER SENSITIVE
31 
32 /**
33  * This is a debuggee class.
34  */
35 public class getvalues003t {
36     private Log log;
37     private IOPipe pipe;
38     private OtherThr auxThr;
39 
main(String args[])40     public static void main(String args[]) {
41         System.exit(run(args) + Consts.JCK_STATUS_BASE);
42     }
43 
run(String args[])44     public static int run(String args[]) {
45         return new getvalues003t().runIt(args);
46     }
47 
runIt(String args[])48     private int runIt(String args[]) {
49         ArgumentHandler argHandler = new ArgumentHandler(args);
50 
51         log = argHandler.createDebugeeLog();
52         pipe = argHandler.createDebugeeIOPipe();
53 
54         Thread.currentThread().setName(getvalues003.DEBUGGEE_THRDNAMES[0]);
55         startThread();
56 
57         // dummy local vars used by debugger for testing
58         byte getvalues003tFindMe = 127;
59         short shortVar = -32768;
60         int intVar = 2147483647;
61         long longVar = 9223372036854775807L;
62         float floatVar = 5.1F;
63         double doubleVar = 6.2D;
64         char charVar = 'a';
65         boolean booleanVar = true;
66         String strVar =  "string var";
67 
68         // Now the debuggee is ready
69         pipe.println(getvalues003.COMMAND_READY);
70         String cmd = pipe.readln();
71         if (cmd.equals(getvalues003.COMMAND_QUIT)) {
72             killThread(argHandler.getWaitTime()*60000);
73             log.complain("Debuggee: exiting due to the command "
74                     + cmd);
75             return Consts.TEST_PASSED;
76         }
77 
78         int stopMeHere = 0; // getvalues003.DEBUGGEE_STOPATLINE
79 
80         cmd = pipe.readln();
81         killThread(argHandler.getWaitTime()*60000);
82         if (!cmd.equals(getvalues003.COMMAND_QUIT)) {
83             log.complain("TEST BUG: unknown debugger command: "
84                 + cmd);
85             return Consts.TEST_FAILED;
86         }
87         return Consts.TEST_PASSED;
88     }
89 
startThread()90     private void startThread() {
91         Object readyObj = new Object();
92 
93         auxThr = new OtherThr(readyObj,
94             getvalues003.DEBUGGEE_THRDNAMES[1]);
95         auxThr.setDaemon(true);
96 
97         log.display("Debuggee: starting thread \""
98             + auxThr.getName() + "\" ...");
99         synchronized(readyObj) {
100             auxThr.start();
101             try {
102                 readyObj.wait(); // wait for the thread's readiness
103             } catch (InterruptedException e) {
104                 log.complain("TEST FAILURE: Debuggee: waiting for the thread "
105                     + auxThr + " start: caught " + e);
106                 pipe.println("failed");
107                 System.exit(Consts.JCK_STATUS_BASE +
108                     Consts.TEST_FAILED);
109             }
110         }
111         log.display("Debuggee: the thread \""
112             + auxThr.getName() + "\" started");
113     }
114 
killThread(int waitTime)115     private void killThread(int waitTime) {
116         auxThr.doExit = true;
117         try {
118             auxThr.join(waitTime);
119             log.display("Debuggee: thread \""
120                 + auxThr.getName() + "\" done");
121         } catch (InterruptedException e) {
122             log.complain("TEST FAILURE: Debuggee: joining the thread \""
123                 + auxThr.getName() + "\": caught " + e);
124         }
125     }
126 
127    /**
128     * This is an auxiliary thread class used to check
129     * an IllegalArgumentException in debugger.
130     */
131     class OtherThr extends Thread {
132         volatile boolean doExit = false;
133         private Object readyObj;
134 
OtherThr(Object readyObj, String name)135         OtherThr(Object readyObj, String name) {
136             super(name);
137             this.readyObj = readyObj;
138         }
139 
run()140         public void run() {
141             // dummy local vars used by debugger for testing
142             byte getvalues003tFindMe = 127;
143             short shortVar = -32768;
144             int intVar = 2147483647;
145             long longVar = 9223372036854775807L;
146             float floatVar = 5.1F;
147             double doubleVar = 6.2D;
148             char charVar = 'a';
149             boolean booleanVar = true;
150             String strVar =  "string var";
151 
152             Thread thr = Thread.currentThread();
153 
154             synchronized(readyObj) {
155                 readyObj.notify(); // notify the main thread
156             }
157             log.display("Debuggee thread \""
158                     + thr.getName() + "\": going to loop");
159             while(!doExit) {
160                 int i = 0;
161                 i++; i--;
162 
163                 // reliable analogue of Thread.yield()
164                 synchronized(this) {
165                     try {
166                         this.wait(30);
167                     } catch (InterruptedException e) {
168                         e.printStackTrace(log.getOutStream());
169                         log.complain("TEST FAILURE: Debuggee thread \""
170                             + thr.getName()
171                             + "\" interrupted while sleeping:\n\t" + e);
172                         break;
173                     }
174                 }
175             }
176             log.display("Debuggee thread \""
177                 + thr.getName() + "\" exiting ...");
178         }
179     }
180 /////////////////////////////////////////////////////////////////////////////
181 
182 }
183