File: CLETest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (449 lines) | stat: -rw-r--r-- 18,187 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 8292217
 * @summary Test co-located events (CLE) for MethodEntry, SingleStep, and Breakpoint events.
 * @run build TestScaffold VMConnection TargetListener TargetAdapter
 * @run compile -g CLETest.java
 * @run driver CLETest
 */

import com.sun.jdi.*;
import com.sun.jdi.event.*;
import com.sun.jdi.request.*;
import java.util.*;

class CLEClass1 {
    public static void foo() {
    }
}
class CLEClass2 {
    public static void foo() {
    }
}

/*
 * The debuggee has a large number of breakpoints pre-setup to help control the test.
 * They are each hit just once, and in the order of their number. No instructions in the
 * debuggee are ever executed more than once.
 *
 * NOTE: the breakpoints are sensitive to the their line number within the method.
 * If that changes, then the "breakpoints" table needs to be updated.
 */
class CLEDebugee {
    public static void main(String[] args) {
        runTests();
    }

    public static void runTests() {
        test1();
        test2();
        test3(); // BREAKPOINT_3
        test4(); // BREAKPOINT_5
        test5(); // BREAKPOINT_7
        test6(); // BREAKPOINT_9
    }

    // test1 and test2 are testing for the bug described in 8292217. For this test MethodEntry
    // events are enabled when we hit the breakpoint, and we single step OVER (test1) or
    // INTO (test2) an instruction with an unresolved contant pool entry. The Debugger will
    // verify that the generated MethodEntry events during class loading are not improperly
    // co-located as described the the CR.
    public static void test1() {
        CLEClass1.foo();  // BREAKPOINT_1
    }
    public static void test2() {
        CLEClass2.foo();  // BREAKPOINT_2
    }

    // Tests that MethodEntry, Step, and Breakpoint events that occur at the same
    // location are properly co-located in the same EventSet. MethodEntry and Step
    // are enabled when we hit BREAKPOINT_3 above. When the BreakpointEvent for
    // BREAKPOINT_4 is generated, the EventSet should also include a StepEvent
    // and a MethodEntryEvent.
    public static void test3() {
        int x = 1;   // BREAKPOINT_4
    }

    // Same as test3 but only check for co-located MethodEntry and Breakpoint events.
    // MethodEntry is enabled when we hit BREAKPOINT_5 above. StepEvent is not enabled.
    // When the BreakpointEvent for BREAKPOINT_6 is generated, the EventSet should also
    // include a MethodEntryEvent.
    public static void test4() {
        int x = 1;   // BREAKPOINT_6
    }

    // Same as test3 but only check for co-located Step and Breakpoint events.
    // StepEvents are enabled when we hit BREAKPOINT_7 above. When the BreakpointEvent
    // for BREAKPOINT_8 is generated, the EventSet should also include a StepEvent,
    public static void test5() {
        int x = 1;    // BREAKPOINT_8
    }

    // Same as test3 but only check for co-located MethodEntry and Step events.
    // MethodEntry and Step events are enabled when we hit BREAKPOINT_9 above. When
    // the StepEvent is received, the EventSet should also include the MethodEntryEvent.
    public static void test6() {
        int x = 1;
    }
}

public class CLETest extends TestScaffold {
    ClassType targetClass;
    EventRequestManager erm;
    StepRequest stepRequest;
    MethodEntryRequest entryRequest;
    MethodExitRequest exitRequest;
    int methodEntryCount = 0;
    int breakpointCount = 0;
    boolean testcaseFailed = false;
    int testcase = 0;

    CLETest(String args[]) {
        super(args);
    }

    public static void main(String[] args) throws Exception {
        CLETest cle = new CLETest(args);
        cle.startTests();
    }

    static class MethodBreakpointData {
        final String method;
        final String signature;
        final int lineNumber;
        public MethodBreakpointData(String method, String signature, int lineNumber) {
            this.method     = method;
            this.signature  = signature;
            this.lineNumber = lineNumber;
        }
    }

    // Table of all breakpoints based on method name and sig, plus the line number within the method.
    static MethodBreakpointData[] breakpoints = new MethodBreakpointData[] {
        new MethodBreakpointData("runTests", "()V", 3), // BREAKPOINT_3
        new MethodBreakpointData("runTests", "()V", 4), // BREAKPOINT_5
        new MethodBreakpointData("runTests", "()V", 5), // BREAKPOINT_7
        new MethodBreakpointData("runTests", "()V", 6), // BREAKPOINT_9
        new MethodBreakpointData("test1", "()V", 1), // BREAKPOINT_1
        new MethodBreakpointData("test2", "()V", 1), // BREAKPOINT_2
        new MethodBreakpointData("test3", "()V", 1), // BREAKPOINT_4
        new MethodBreakpointData("test4", "()V", 1), // BREAKPOINT_6
        new MethodBreakpointData("test5", "()V", 1)  // BREAKPOINT_8
    };

    public static void printStack(ThreadReference thread) {
        try {
            List<StackFrame> frames = thread.frames();
            Iterator<StackFrame> iter = frames.iterator();
            while (iter.hasNext()) {
                StackFrame frame = iter.next();
                System.out.println(getLocationString(frame.location()));
            }
        } catch (Exception e) {
            System.out.println("printStack: exception " + e);
        }
    }

    public static String getLocationString(Location loc) {
        return
            loc.declaringType().name() + "." +
            loc.method().name() + ":" +
            loc.lineNumber();
    }

    /*
     * Returns true if the specified event types are all co-located in this EventSet,
     * and no other events are included. Note that the order of the events (when present)
     * is required to be: MethodEntryEvent, StepEvent, BreakpointEvent.
     */
    public boolean isColocated(EventSet set, boolean needEntry, boolean needStep, boolean needBreakpoint) {
        int expectedSize = (needEntry ? 1 : 0) + (needStep ? 1 : 0) + (needBreakpoint ? 1 : 0);
        if (set.size() != expectedSize) {
            return false;
        }
        EventIterator iter = set.eventIterator();
        if (needEntry) {
            Event meEvent = iter.next();
            if (!(meEvent instanceof MethodEntryEvent)) {
                return false;
            }
        }
        if (needStep) {
            Event ssEvent = iter.next();
            if (!(ssEvent instanceof StepEvent)) {
                return false;
            }
        }
        if (needBreakpoint) {
            Event bpEvent = iter.next();
            if (!(bpEvent instanceof BreakpointEvent)) {
                return false;
            }
        }
        return true;
    }

    public void eventSetReceived(EventSet set) {
        System.out.println("\nEventSet for test case #" + testcase + ": " + set);
        switch (testcase) {
        case 1:
        case 2: {
            // During the first two test cases we should never receive an EventSet with
            // more than one Event in it.
            if (set.size() != 1) {
                testcaseFailed = true;
                // For now, we expect the 2nd test cases to fail due to 8292217,
                // so don't fail the overall test run as a result of this failure.
                // There is a workaround in place that allows the 1st test case to pass.
                if (testcase == 1) {
                    testFailed = true;
                }
                System.out.println("TESTCASE #" + testcase + " FAILED" +
                                   (testcase == 2 ? "(ignoring)" : "") +
                                   ": too many events in EventSet: " + set.size());
            }
            break;
        }
        case 3: {
            // At some point during test3 we should receive co-located MethodEntry, Step, and Breakpoint events.
            if (isColocated(set, true, true, true)) {
                testcaseFailed = false;
            }
            break;
        }
        case 4: {
            // At some point during test4 we should receive co-located MethodEntry and Breakpoint events.
            if (isColocated(set, true, false, true)) {
                testcaseFailed = false;
            }
            break;
        }
        case 5: {
            // At some point during test5 we should receive co-located Step and Breakpoint events.
            if (isColocated(set, false, true, true)) {
                testcaseFailed = false;
            }
            break;
        }
        case 6: {
            // At some point during test6 we should receive co-located MethodEntry and Step events.
            if (isColocated(set, true, true, false)) {
                testcaseFailed = false;
            }
            break;
        }
        }
    }

    /*
     * Most of the control flow of the test is handled via breakpoints. There is one at the start
     * of each test case that is used to enable other events that we check for during the test case.
     * In some cases there is an additional Breakpoint enabled for the test cases that is
     * also used to determine when the test case is complete. Other test cases are completed
     * when a Step or MethodEntry event arrives.
     */
    public void breakpointReached(BreakpointEvent event) {
        breakpointCount++;
        if (breakpointCount != 4 && breakpointCount != 6 && breakpointCount != 8) {
            testcase++;
        }
        System.out.println("Got BreakpointEvent(" + breakpointCount + "): " + getLocationString(event.location()));
        event.request().disable();

        // Setup test1. Completion is checked for in stepCompleted().
        if (breakpointCount == 1) {
            testcaseFailed = false; // assume passing unless error detected
            entryRequest.enable();
            exitRequest.enable();
            stepRequest = erm.createStepRequest(mainThread,
                                                StepRequest.STEP_LINE,
                                                StepRequest.STEP_OVER);
            stepRequest.addCountFilter(1);
            stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
            stepRequest.enable();
        }

        // Setup test2. Completion is checked for in stepCompleted().
        if (breakpointCount == 2) {
            testcaseFailed = false; // assume passing unless error detected
            entryRequest.enable();
            exitRequest.enable();
            stepRequest = erm.createStepRequest(mainThread,
                                                StepRequest.STEP_LINE,
                                                StepRequest.STEP_INTO);
            stepRequest.addCountFilter(1);
            stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
            stepRequest.enable();
        }

        // Setup test3: MethodEntry, Step, and Breakpoint co-located events.
        // Completion is handled by the next breakpoint being hit.
        if (breakpointCount == 3) {
            testcaseFailed = true; // assume failing unless pass detected
            entryRequest.enable();
            stepRequest = erm.createStepRequest(mainThread,
                                                StepRequest.STEP_LINE,
                                                StepRequest.STEP_INTO);
            stepRequest.addCountFilter(1);
            stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
            stepRequest.enable();
        }
        // Complete test3. We fail if we never saw the expected co-located events.
        if (breakpointCount == 4) {
            if (testcaseFailed) {
                testFailed = true;
                System.out.println("TESTCASE #3 FAILED: did not get MethodEntry, Step, and Breakpoint co-located events");
            } else {
                System.out.println("TESTCASE #3 PASSED");
            }
        }

        // Setup test4: MethodEntry and Breakpoint co-located events.
        // Completion is handled by the next breakpoint being hit.
        if (breakpointCount == 5) {
            testcaseFailed = true; // assume failing unless pass detected
            entryRequest.enable();
        }
        // Complete test4. We fail if we never saw the expected co-located events.
        if (breakpointCount == 6) {
            entryRequest.disable();
            if (testcaseFailed) {
                testFailed = true;
                System.out.println("TESTCASE #4 FAILED: did not get MethodEntry and Breakpoint co-located events");
            } else {
                System.out.println("TESTCASE #4 PASSED");
            }
        }

        // Setup test5: Step and Breakpoint co-located events.
        // Completion is handled by the next breakpoint being hit.
        if (breakpointCount == 7) {
            testcaseFailed = true; // assume failing unless pass detected
            stepRequest = erm.createStepRequest(mainThread,
                                                StepRequest.STEP_LINE,
                                                StepRequest.STEP_INTO);
            stepRequest.addCountFilter(1);
            stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
            stepRequest.enable();
        }
        // Complete test5. We fail if we never saw the expected co-located events.
        if (breakpointCount == 8) {
            if (testcaseFailed) {
                testFailed = true;
                System.out.println("TESTCASE #5 FAILED: did not get Step and Breakpoint co-located events");
            } else {
                System.out.println("TESTCASE #5 PASSED");
            }
        }

        // Setup test: MethodEntry and Step co-located events
        // Completion is handled by the stepCompleted() since there is no additional breakpoint.
        if (breakpointCount == 9) {
            testcaseFailed = true; // assume failing unless pass detected
            entryRequest.enable();
            stepRequest = erm.createStepRequest(mainThread,
                                                StepRequest.STEP_LINE,
                                                StepRequest.STEP_INTO);
            stepRequest.addCountFilter(1);
            stepRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
            stepRequest.enable();
        }
    }

    public void stepCompleted(StepEvent event) {
        System.out.println("Got StepEvent: " + getLocationString(event.location()));
        event.request().disable();
        entryRequest.disable();
        if (testcase == 6 && testcaseFailed) {
            testFailed = true;
            System.out.println("TESTCASE #6 FAILED: did not get MethodEntry and Step co-located events");
        }
        if (testcase == 1 || testcase == 2 || testcase == 6) {
            exitRequest.disable();
            if (!testcaseFailed) {  // We already did a println if the test failed.
                System.out.println("TESTCASE #" + testcase + " PASSED");
            }
        }
    }

    public void methodEntered(MethodEntryEvent event) {
        System.out.println("Got MethodEntryEvent: " + getLocationString(event.location()));
        if (methodEntryCount++ == 25) {
            entryRequest.disable(); // Just in case the test loses control.
        }
    }

    public void methodExited(MethodExitEvent event) {
        System.out.println("Got MethodExitEvent: " + getLocationString(event.location()));
        //printStack(event.thread());
        exitRequest.disable();
        entryRequest.disable();
    }

    protected void runTests() throws Exception {
        System.out.println("Starting CLETest");
        BreakpointEvent bpe = startToMain("CLEDebugee");
        targetClass = (ClassType)bpe.location().declaringType();
        mainThread = bpe.thread();
        System.out.println("Got main thread: " + mainThread);
        erm = eventRequestManager();

        try {
            // Setup all breakpoints
            for (MethodBreakpointData bpData : breakpoints) {
                Location loc = findMethodLocation(targetClass, bpData.method,
                                                  bpData.signature, bpData.lineNumber);
                BreakpointRequest req = erm.createBreakpointRequest(loc);
                req.setSuspendPolicy(EventRequest.SUSPEND_ALL);
                req.enable();
            }

            // Ask for method entry events
            entryRequest = erm.createMethodEntryRequest();
            entryRequest.addThreadFilter(mainThread);
            entryRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);

            // Ask for method exit events
            exitRequest = erm.createMethodExitRequest();
            exitRequest.addThreadFilter(mainThread);
            exitRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);

            System.out.println("Waiting for events: ");

            listenUntilVMDisconnect();
            System.out.println("All done...");
        } catch (Exception ex){
            ex.printStackTrace();
            testFailed = true;
        }

        if (!testFailed) {
            println("CLETest: passed");
        } else {
            throw new Exception("CLETest: failed");
        }
    }
}