1 /*
  2  * Copyright (c) 2018, 2023, 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  * @bug 8174994 8200613
 27  * @summary Test the clhsdb commands 'printmdo', 'printall', 'jstack' on a CDS enabled corefile.
 28  * @requires vm.cds
 29  * @requires vm.hasSA
 30  * @requires vm.flavor == "server"
 31  * @library /test/lib
 32  * @modules java.base/jdk.internal.misc
 33  * @run driver/timeout=2400 ClhsdbCDSCore
 34  */
 35 
 36 import java.io.File;
 37 import java.io.IOException;
 38 import java.nio.file.Files;
 39 import java.nio.file.Paths;
 40 import java.util.ArrayList;
 41 import java.util.Arrays;
 42 import java.util.HashMap;
 43 import java.util.List;
 44 import java.util.Map;
 45 
 46 import jdk.internal.misc.Unsafe;
 47 
 48 import jdk.test.lib.Asserts;
 49 import jdk.test.lib.Platform;
 50 import jdk.test.lib.cds.CDSOptions;
 51 import jdk.test.lib.cds.CDSTestUtils;
 52 import jdk.test.lib.process.OutputAnalyzer;
 53 import jdk.test.lib.process.ProcessTools;
 54 import jdk.test.lib.util.CoreUtils;
 55 import jdk.test.lib.Utils;
 56 
 57 import jtreg.SkippedException;
 58 
 59 class CrashApp {
 60     public static void main(String[] args) {
 61         Unsafe.getUnsafe().putInt(0L, 0);
 62     }
 63 }
 64 
 65 public class ClhsdbCDSCore {
 66     private static final String SHARED_ARCHIVE_NAME = "ArchiveForClhsdbCDSCore.jsa";
 67     private static String coreFileName;
 68 
 69     public static void main(String[] args) throws Exception {
 70         System.out.println("Starting ClhsdbCDSCore test");
 71         cleanup();
 72 
 73         try {
 74             CDSOptions opts = (new CDSOptions()).setArchiveName(SHARED_ARCHIVE_NAME);
 75             CDSTestUtils.createArchiveAndCheck(opts);
 76 
 77             String[] jArgs = {
 78                 "-Xmx512m",
 79                 "-XX:+UnlockDiagnosticVMOptions",
 80                 "-XX:SharedArchiveFile=" + SHARED_ARCHIVE_NAME,
 81                 "-XX:+CreateCoredumpOnCrash",
 82                 "-Xshare:auto",
 83                 "-XX:+ProfileInterpreter",
 84                 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
 85                 CoreUtils.getAlwaysPretouchArg(true),
 86                 CrashApp.class.getName()
 87             };
 88 
 89             OutputAnalyzer crashOutput;
 90             try {
 91                List<String> options = new ArrayList<>();
 92                options.addAll(Arrays.asList(jArgs));
 93                ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(options);
 94                // Add "ulimit -c unlimited" if we can since we are generating a core file.
 95                pb = CoreUtils.addCoreUlimitCommand(pb);
 96                crashOutput = ProcessTools.executeProcess(pb);
 97             } catch (Throwable t) {
 98                throw new Error("Can't execute the java cds process.", t);
 99             }
100 
101             try {
102                 coreFileName = CoreUtils.getCoreFileLocation(crashOutput.getStdout(), crashOutput.pid());
103             } catch (Exception e) {
104                 cleanup();
105                 throw e;
106             }
107 
108             ClhsdbLauncher test = new ClhsdbLauncher();
109 
110             // Ensure that UseSharedSpaces is turned on.
111             List<String> cmds = List.of("flags UseSharedSpaces");
112 
113             String useSharedSpacesOutput = test.runOnCore(coreFileName, cmds, null, null);
114 
115             if (useSharedSpacesOutput == null) {
116                 // Output could be null due to attach permission issues.
117                 cleanup();
118                 throw new SkippedException("Could not determine the UseSharedSpaces value");
119             }
120 
121             if (useSharedSpacesOutput.contains("UseSharedSpaces = false")) {
122                 // CDS archive is not mapped. Skip the rest of the test.
123                 cleanup();
124                 throw new SkippedException("The CDS archive is not mapped");
125             }
126 
127             List testJavaOpts = Arrays.asList(Utils.getTestJavaOpts());
128 
129             if (testJavaOpts.contains("-XX:TieredStopAtLevel=1")) {
130                 // No MDOs are allocated in -XX:TieredStopAtLevel=1
131                 // The reason is methods being compiled aren't hot enough
132                 // Let's not call printmdo in such scenario
133                 cmds = List.of("printall", "jstack -v");
134             } else {
135                 cmds = List.of("printmdo -a", "printall", "jstack -v");
136             }
137 
138             Map<String, List<String>> expStrMap = new HashMap<>();
139             Map<String, List<String>> unExpStrMap = new HashMap<>();
140             expStrMap.put("printmdo -a", List.of(
141                 "CounterData",
142                 "BranchData"));
143             unExpStrMap.put("printmdo -a", List.of(
144                 "No suitable match for type of address"));
145             expStrMap.put("printall", List.of(
146                 "aload_0",
147                 "_nofast_aload_0",
148                 "_nofast_getfield",
149                 "_nofast_putfield",
150                 "Constant Pool of",
151                 "public static void main\\(java.lang.String\\[\\]\\)",
152                 "Bytecode",
153                 "invokevirtual",
154                 "checkcast",
155                 "Exception Table",
156                 "invokedynamic"));
157             unExpStrMap.put("printall", List.of(
158                 "sun.jvm.hotspot.types.WrongTypeException",
159                 "illegal code",
160                 "Failure occurred at bci",
161                 "No suitable match for type of address"));
162             expStrMap.put("jstack -v", List.of(
163                 "Common-Cleaner",
164                 "Method*"));
165             unExpStrMap.put("jstack -v", List.of(
166                 "sun.jvm.hotspot.debugger.UnmappedAddressException"));
167             test.runOnCore(coreFileName, cmds, expStrMap, unExpStrMap);
168         } catch (SkippedException e) {
169             throw e;
170         } catch (Exception ex) {
171             throw new RuntimeException("Test ERROR " + ex, ex);
172         }
173         cleanup();
174         System.out.println("Test PASSED");
175     }
176 
177     private static void cleanup() {
178         remove(SHARED_ARCHIVE_NAME);
179     }
180 
181     private static void remove(String item) {
182         File toDelete = new File(item);
183         toDelete.delete();
184     }
185 }