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.StepRequest.addClassFilter_rt;
25 
26 import nsk.share.*;
27 import nsk.share.jpda.*;
28 import nsk.share.jdi.*;
29 import nsk.share.jdi.ThreadState;
30 
31 /**
32  * This class is used as debuggee application for the filter_rt001 JDI test.
33  */
34 
35 public class filter_rt001a {
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     static final long THREAD_STATE_TIMEOUT_MS = 30000;
44     static final String STATE_INIT = "init";
45     static final String STATE_THREAD_STARTED = "threadStarted";
46     static final String STATE_JDI_INITED = "jdiInited";
47 
48     static ArgumentHandler argHandler;
49     static Log log;
50 
51     //--------------------------------------------------   log procedures
52 
log1(String message)53     public static void log1(String message) {
54         log.display("**> debuggee: " + message);
55     }
56 
logErr(String message)57     private static void logErr(String message) {
58         log.complain("**> debuggee: " + message);
59     }
60 
61     //====================================================== test program
62 
63     static Thread1filter_rt001a thread1 = new Thread1filter_rt001a(
64             "thread1", new ThreadState(STATE_INIT, THREAD_STATE_TIMEOUT_MS));
65     static Thread2filter_rt001a thread2 = new Thread2filter_rt001a(
66             "thread2", new ThreadState(STATE_INIT, THREAD_STATE_TIMEOUT_MS));
67 
68     static TestClass11 obj = new TestClass11();
69     //------------------------------------------------------ common section
70 
71     static int exitCode = PASSED;
72 
73     static int instruction = 1;
74     static int end         = 0;
75                                    //    static int quit        = 0;
76                                    //    static int continue    = 2;
77     static int maxInstr    = 1;    // 2;
78 
79     static int lineForComm = 2;
80 
methodForCommunication()81     private static void methodForCommunication() {
82         int i1 = instruction;
83         int i2 = i1;
84         int i3 = i2;
85     }
86     //----------------------------------------------------   main method
87 
main(String argv[])88     public static void main (String argv[]) {
89 
90         argHandler = new ArgumentHandler(argv);
91         log = argHandler.createDebugeeLog();
92 
93         thread1.start();
94         thread2.start();
95         thread1.getThreadState().waitForState(STATE_THREAD_STARTED);
96         thread2.getThreadState().waitForState(STATE_THREAD_STARTED);
97 
98         log1("debuggee started!");
99 
100         for (int i = 0; ; i++) {
101 
102             log1("methodForCommunication();");
103             methodForCommunication();
104             if (instruction == end)
105                 break;
106 
107             if (instruction > maxInstr) {
108                 logErr("ERROR: unexpected instruction: " + instruction);
109                 exitCode = FAILED;
110                 break ;
111             }
112 
113             switch (i) {
114 
115 //------------------------------------------------------  section tested
116 
117                 case 0:
118                 thread1.getThreadState().setState(STATE_JDI_INITED);
119                 thread2.getThreadState().setState(STATE_JDI_INITED);
120                 waitForThreadJoin ( thread1, "thread1" );
121                 waitForThreadJoin ( thread2, "thread2" );
122 
123 //-------------------------------------------------    standard end section
124 
125                 default:
126                 instruction = end;
127                 break;
128             }
129         }
130 
131         log1("debuggee exits");
132         System.exit(exitCode + PASS_BASE);
133     }
134 
waitForThreadJoin(Thread thread, String threadName)135     static void waitForThreadJoin (Thread thread, String threadName) {
136         log1("waiting for " + threadName + " join");
137         int waitMs = argHandler.getWaitTime() * 60 * 1000;
138         if (thread.isAlive()) {
139             try {
140                 thread.join(waitMs);
141             } catch (InterruptedException e) {
142                 throw new Failure("catched unexpected InterruptedException while waiting of " + threadName + " join:" + e);
143             };
144         }
145         if (thread.isAlive()) {
146             throw new Failure(threadName + " is still alive");
147         } else {
148             log1(threadName + " joined");
149         }
150     }
151 }
152 
153 class TestClass10{
m10()154     static void m10() {
155         filter_rt001a.log1("entered: m10");
156     }
157 }
158 class TestClass11 extends TestClass10{
m11()159     static void m11() {
160         filter_rt001a.log1("entered: m11");
161         TestClass10.m10();
162     }
163 }
164 
165 class Thread1filter_rt001a extends Thread {
166 
167     private String tName = null;
168     private ThreadState threadState = null;
169 
Thread1filter_rt001a(String threadName, ThreadState threadState)170     public Thread1filter_rt001a(String threadName, ThreadState threadState) {
171         super(threadName);
172         tName = threadName;
173         this.threadState = threadState;
174     }
175 
getThreadState()176     public ThreadState getThreadState() {
177         return threadState;
178     }
179 
run()180     public void run() {
181         filter_rt001a.log1("  'run': enter  :: threadName == " + tName);
182         threadState.setAndWait(filter_rt001a.STATE_THREAD_STARTED, filter_rt001a.STATE_JDI_INITED);
183         TestClass11.m11();
184         filter_rt001a.log1("  'run': exit   :: threadName == " + tName);
185         return;
186     }
187 }
188 
189 class TestClass20{
m20()190     static void m20() {
191         filter_rt001a.log1("entered: m20");
192     }
193 }
194 class TestClass21 extends TestClass20{
m21()195     static void m21() {
196         filter_rt001a.log1("entered: m21");
197         TestClass20.m20();
198     }
199 }
200 
201 class Thread2filter_rt001a extends Thread {
202 
203     private String tName = null;
204     private ThreadState threadState = null;
205 
Thread2filter_rt001a(String threadName, ThreadState threadState)206     public Thread2filter_rt001a(String threadName, ThreadState threadState) {
207         super(threadName);
208         tName = threadName;
209         this.threadState = threadState;
210     }
211 
getThreadState()212     public ThreadState getThreadState() {
213         return threadState;
214     }
215 
run()216     public void run() {
217         filter_rt001a.log1("  'run': enter  :: threadName == " + tName);
218         threadState.setAndWait(filter_rt001a.STATE_THREAD_STARTED, filter_rt001a.STATE_JDI_INITED);
219         TestClass21.m21();
220         filter_rt001a.log1("  'run': exit   :: threadName == " + tName);
221         return;
222     }
223 }
224