1 /*
 2  * Copyright (c) 2020, 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  * @summary Test that the stack traces for carrier threads are hidden by
27  *     exceptions and the StackWalker API
28  * @modules java.management
29  * @compile --enable-preview -source ${jdk.version} StackTraces.java
30  * @run testng/othervm --enable-preview StackTraces
31  * @run testng/othervm --enable-preview -XX:+UnlockDiagnosticVMOptions -XX:+ShowCarrierFrames StackTraces
32  */
33 
34 import java.lang.management.ManagementFactory;
35 import java.util.Arrays;
36 import java.util.Set;
37 import java.util.concurrent.ForkJoinPool;
38 import static java.lang.StackWalker.Option.*;
39 
40 import org.testng.annotations.Test;
41 import static org.testng.Assert.*;
42 
43 public class StackTraces {
44 
45     /**
46      * Test that the stack trace in exceptions does not include the carrier thread
47      * frames, except when running with -XX:+ShowCarrierFrames.
48      */
49     @Test
50     public void testStackTrace() throws Exception {
51         TestHelper.runInVirtualThread(() -> {
52             Exception e = new Exception();
53             boolean found = Arrays.stream(e.getStackTrace())
54                     .map(StackTraceElement::getClassName)
55                     .anyMatch("java.util.concurrent.ForkJoinPool"::equals);
56             assertTrue(found == hasJvmArgument("-XX:+ShowCarrierFrames"));
57         });
58     }
59 
60     /**
61      * Test that StackWalker does not include carrier thread frames.
62      */
63     @Test
64     public void testStackWalker() throws Exception {
65         TestHelper.runInVirtualThread(() -> {
66             StackWalker walker = StackWalker.getInstance(Set.of(RETAIN_CLASS_REFERENCE));
67             boolean found = walker.walk(sf ->
68                     sf.map(StackWalker.StackFrame::getDeclaringClass)
69                             .anyMatch(c -> c == ForkJoinPool.class));
70             assertFalse(found);
71         });
72     }
73 
74     private static boolean hasJvmArgument(String arg) {
75         for (String argument : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
76             if (argument.equals(arg)) return true;
77         }
78         return false;
79     }
80 }