1 /*
2  * Copyright (c) 2007, 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 /*
25  * @test
26  * @modules java.base/jdk.internal.misc:+open
27  *
28  * @summary converted from VM Testbase nsk/jdi/StepEvent/_itself_/stepEvent004.
29  * VM Testbase keywords: [jpda, jdi, feature_sde, vm6]
30  * VM Testbase readme:
31  * DESCRIPTION
32  *     Test scenario:
33  *     There is class 'TestClass1' defined in test package wich has 10 method with 100 locations in each method.
34  *     Debugger creates copy of class file for this class with SourceDebugExtension attribute which contains informations for 3
35  *     stratums('TestStratum1'-'TestStratum3') and for all this stratums following line mapping is defined:
36  *         "Java"          "TestStratum"
37  *         <init>
38  *         31      -->     1001, source1
39  *         sde_testMethod1
40  *         35      -->     2000, source1
41  *         ...             ...
42  *         135     -->     2100, source1
43  *         sde_testMethod2
44  *         139     -->     3000, source1
45  *         ...             ...
46  *         239     -->     3100, source1
47  *         ...             ...
48  *         sde_testMethod10
49  *         971     -->    11000, source1
50  *         ...             ...
51  *         1071    -->    11100, source1
52  *     Then debugger forces debuggee to load 'TestClass1' from updated class file, starts event listener thread which saves all received StepEvents
53  *     and enables StepEvent request(class filter is used to receive events only for 'TestClass1').
54  *     for TestStratum in 'TestStratum1'-'TestStratum3'
55  *     do
56  *         - set TestStratum as VM default
57  *         - force debuggee to execute all methods defined in 'TestClass1'
58  *         - when all methods was executed check up that StepEvents was generated for each location specified for TestStratum
59  *         Described event generation is performed 10 times.
60  *     done
61  *
62  * @library /vmTestbase
63  *          /test/lib
64  * @run driver jdk.test.lib.FileInstaller . .
65  * @build nsk.jdi.StepEvent._itself_.stepEvent004.stepEvent004
66  * @run main/othervm PropertyResolvingWrapper
67  *      nsk.jdi.StepEvent._itself_.stepEvent004.stepEvent004
68  *      -verbose
69  *      -arch=${os.family}-${os.simpleArch}
70  *      -waittime=5
71  *      -debugee.vmkind=java
72  *      -transport.address=dynamic
73  *      "-debugee.vmkeys=${test.vm.opts} ${test.java.opts}"
74  *      -testClassPath ${test.class.path}
75  *      -testWorkDir .
76  *      -testStratumCount 3
77  */
78 
79 package nsk.jdi.StepEvent._itself_.stepEvent004;
80 
81 import java.io.*;
82 import java.util.*;
83 import com.sun.jdi.request.StepRequest;
84 import nsk.share.Consts;
85 import nsk.share.jdi.EventHandler;
86 import nsk.share.jdi.sde.*;
87 
88 public class stepEvent004 extends SDEDebugger {
89     private static final int INIT_LINE = 31;
90     private static final int METHOD1_LINE = 35;
91     private static final int METHOD2_LINE = 139;
92     private static final int METHOD3_LINE = 243;
93     private static final int METHOD4_LINE = 347;
94     private static final int METHOD5_LINE = 451;
95     private static final int METHOD6_LINE = 555;
96     private static final int METHOD7_LINE = 659;
97     private static final int METHOD8_LINE = 763;
98     private static final int METHOD9_LINE = 867;
99     private static final int METHOD10_LINE = 971;
100 
main(String argv[])101     public static void main(String argv[]) {
102         System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
103     }
104 
run(String argv[], PrintStream out)105     public static int run(String argv[], PrintStream out) {
106         return new stepEvent004().runIt(argv, out);
107     }
108 
doInit(String args[], PrintStream out)109     protected String[] doInit(String args[], PrintStream out) {
110         args = super.doInit(args, out);
111 
112         ArrayList<String> standardArgs = new ArrayList<String>();
113 
114         for (int i = 0; i < args.length; i++) {
115             if (args[i].equals("-testStratumCount") && (i < args.length - 1)) {
116                 testStratumCount = Integer.parseInt(args[i + 1]);
117                 i++;
118             } else
119                 standardArgs.add(args[i]);
120         }
121 
122         return standardArgs.toArray(new String[] {});
123     }
124 
125     private int testStratumCount = 1;
126 
127     private EventHandler eventHandler;
128 
preparePatchedClassFile(String className, int testStratumCount)129     protected Map<String, LocationsData> preparePatchedClassFile(String className, int testStratumCount) {
130         /*
131          * Create file with following line mapping for each test stratum:
132          *
133          * "Java"  "TestStratum"
134          * <init>
135          * 31   --> 1001, source1
136          * sde_testMethod1
137          * 35   --> 2000, source1
138          * ...             ...
139          * 135  --> 2100, source1
140          * sde_testMethod2
141          * 139  --> 3000, source1
142          * ...             ...
143          * 239  --> 3100, source1
144          * ...             ...
145          * sde_testMethod10
146          * 971  --> 11000, source1
147          * ...             ...
148          * 1071 --> 11100, source1
149          */
150         String smapFileName = "TestSMAP.smap";
151         SmapGenerator smapGenerator = new SmapGenerator();
152 
153         Map<String, LocationsData> testStratumData = new TreeMap<String, LocationsData>();
154 
155         for (int i = 0; i < testStratumCount; i++) {
156             String stratumName = testStratumName + (i + 1);
157 
158             LocationsData locationsData = new LocationsData(stratumName);
159 
160             String sourceName = testStratumSourceName + (i + 1);
161             String sourcePath = testStratumSourcePath + (i + 1);
162 
163             locationsData.paths.add(sourcePath);
164 
165             SmapStratum smapStratum = new SmapStratum(stratumName);
166 
167             List<DebugLocation> sourceLocations = new ArrayList<DebugLocation>();
168 
169             sourceLocations.add(new DebugLocation(sourceName, sourcePath,
170                     "<init>", 1001, INIT_LINE));
171 
172             for (int j = 0; j < 100; j++) {
173                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
174                         "sde_testMethod1",   2000 + j, METHOD1_LINE  + j));
175                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
176                         "sde_testMethod2",   3000 + j, METHOD2_LINE  + j));
177                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
178                         "sde_testMethod3",   4000 + j, METHOD3_LINE  + j));
179                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
180                         "sde_testMethod4",   5000 + j, METHOD4_LINE  + j));
181                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
182                         "sde_testMethod5",   6000 + j, METHOD5_LINE  + j));
183                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
184                         "sde_testMethod6",   7000 + j, METHOD6_LINE  + j));
185                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
186                         "sde_testMethod7",   8000 + j, METHOD7_LINE  + j));
187                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
188                         "sde_testMethod8",   9000 + j, METHOD8_LINE  + j));
189                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
190                         "sde_testMethod9",  10000 + j, METHOD9_LINE  + j));
191                 sourceLocations.add(new DebugLocation(sourceName, sourcePath,
192                         "sde_testMethod10", 11000 + j, METHOD10_LINE + j));
193             }
194 
195             locationsData.sourceLocations.put(sourceName, sourceLocations);
196             locationsData.allLocations.addAll(sourceLocations);
197             testStratumData.put(stratumName, locationsData);
198 
199             smapStratum.addFile(sourceName, sourcePath);
200 
201             for (DebugLocation debugLocation : sourceLocations) {
202                 smapStratum.addLineData(
203                         debugLocation.inputLine,
204                         debugLocation.sourceName,
205                         1,
206                         debugLocation.outputLine,
207                         1);
208             }
209 
210             smapGenerator.addStratum(smapStratum, false);
211         }
212 
213         savePathcedClassFile(className, smapGenerator, smapFileName);
214 
215         return testStratumData;
216     }
217 
doTest()218     public void doTest() {
219         String className = TestClass1.class.getName();
220 
221         Map<String, LocationsData> testStratumData = preparePatchedClassFile(className, testStratumCount);
222 
223         initDefaultBreakpoint();
224 
225         eventHandler = new EventHandler(debuggee, log);
226         eventHandler.startListening();
227 
228         StepEventListener stepEventListener = new StepEventListener();
229         eventHandler.addListener(stepEventListener);
230 
231         StepRequest stepRequest = debuggee.getEventRequestManager().createStepRequest(
232                 debuggee.threadByName(SDEDebuggee.mainThreadName),
233                 StepRequest.STEP_LINE,
234                 StepRequest.STEP_INTO);
235 
236         stepRequest.setSuspendPolicy(StepRequest.SUSPEND_EVENT_THREAD);
237         stepRequest.addClassFilter(TestClass1.class.getName());
238         stepRequest.enable();
239 
240         // for each stratum defined for class
241         for (String stratumName : testStratumData.keySet()) {
242             log.display("Generate events for stratum: " + stratumName);
243 
244             vm.setDefaultStratum(stratumName);
245 
246             // perform event generation 10 times
247             for (int i = 0; i < 10; i++) {
248                 stepEventListener.clearLocations();
249 
250                 pipe.println(SDEDebuggee.COMMAND_EXECUTE_TEST_METHODS + ":" + className);
251 
252                 if (!isDebuggeeReady())
253                     return;
254 
255                 stepEventListener.waitBreakpointEvent();
256 
257                 compareLocations(
258                         stepEventListener.stepLocations(),
259                         testStratumData.get(stratumName).allLocations,
260                         stratumName);
261             }
262         }
263 
264     }
265 }
266