1 /*
 2  * Copyright (c) 2021, Alibaba Group Holding Limited. 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 runtime.valhalla.inlinetypes;
25 
26 import jdk.test.lib.process.ProcessTools;
27 import jdk.test.lib.process.OutputAnalyzer;
28 import jdk.test.lib.JDKToolFinder;
29 import jdk.internal.vm.annotation.LooselyConsistentValue;
30 import jdk.internal.vm.annotation.NullRestricted;
31 import jdk.internal.vm.annotation.Strict;
32 
33 
34 /*
35  * @test
36  * @summary Test the VM.class_print_layout command
37  * @library /test/lib
38  * @modules java.base/jdk.internal.vm.annotation
39  * @enablePreview
40  * @compile ClassPrintLayoutDcmd.java
41  * @run main/othervm runtime.valhalla.inlinetypes.ClassPrintLayoutDcmd
42  */
43 
44 public value class ClassPrintLayoutDcmd {
45 
46     @LooselyConsistentValue
47     static value class Point {
48         int i = 0;
49         int j = 0;
50     }
51 
52     @LooselyConsistentValue
53     static value class Line {
54         @Strict
55         @NullRestricted
56         Point p1;
57         @Strict
58         @NullRestricted
59         Point p2;
60         Line() {
61             this.p1 = this.p2 = new Point();
62         }
63     }
64     static {
65         try {
66             Class.forName(Line.class.getName());
67         } catch(Throwable e) {
68             throw new RuntimeException("failed to find class");
69         }
70     }
71     static ProcessBuilder pb = new ProcessBuilder();
72 
73     static void testCmd(String arg, int expectExitCode, String... expectStrings) throws Exception {
74         // Grab my own PID
75         String pid = Long.toString(ProcessTools.getProcessId());
76 
77         pb.command(new String[] { JDKToolFinder.getJDKTool("jcmd"), pid, "VM.class_print_layout", arg });
78         OutputAnalyzer output = new OutputAnalyzer(pb.start());
79         output.shouldHaveExitValue(expectExitCode);
80         for (String expectString : expectStrings) {
81             output.shouldContain(expectString);
82         }
83     }
84 
85     public static void main(String args[]) throws Exception {
86         testCmd("foo/bar", 0, "");
87         testCmd("", 1, "IllegalArgumentException", "mandatory");
88         testCmd("java/lang/Object", 0, "java/lang/Object", "@bootstrap");
89         testCmd("java/lang/Class", 0, "java/lang/Class", "@bootstrap");
90         testCmd("runtime/valhalla/inlinetypes/ClassPrintLayoutDcmd$Line", 0, "@app", "p1", "p2");
91     }
92 }