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             opts.addSuffix("-XX:-PreloadSharedClasses");
 76             CDSTestUtils.createArchiveAndCheck(opts);
 77 
 78             String[] jArgs = {
 79                 "-Xmx512m",
 80                 "-XX:+UnlockDiagnosticVMOptions",
 81                 "-XX:SharedArchiveFile=" + SHARED_ARCHIVE_NAME,
 82                 "-XX:+CreateCoredumpOnCrash",
 83                 "-Xshare:auto",
 84                 "-XX:+ProfileInterpreter",
 85                 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
 86                 CoreUtils.getAlwaysPretouchArg(true),
 87                 CrashApp.class.getName()
 88             };
 89 
 90             OutputAnalyzer crashOutput;
 91             try {
 92                List<String> options = new ArrayList<>();
 93                options.addAll(Arrays.asList(jArgs));
 94                ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(options);
 95                // Add "ulimit -c unlimited" if we can since we are generating a core file.
 96                pb = CoreUtils.addCoreUlimitCommand(pb);
 97                crashOutput = ProcessTools.executeProcess(pb);
 98             } catch (Throwable t) {
 99                throw new Error("Can't execute the java cds process.", t);
100             }
101 
102             try {
103                 coreFileName = CoreUtils.getCoreFileLocation(crashOutput.getStdout(), crashOutput.pid());
104             } catch (Exception e) {
105                 cleanup();
106                 throw e;
107             }
108 
109             ClhsdbLauncher test = new ClhsdbLauncher();
110 
111             // Ensure that UseSharedSpaces is turned on.
112             List<String> cmds = List.of("flags UseSharedSpaces");
113 
114             String useSharedSpacesOutput = test.runOnCore(coreFileName, cmds, null, null);
115 
116             if (useSharedSpacesOutput == null) {
117                 // Output could be null due to attach permission issues.
118                 cleanup();
119                 throw new SkippedException("Could not determine the UseSharedSpaces value");
120             }
121 
122             if (useSharedSpacesOutput.contains("UseSharedSpaces = false")) {
123                 // CDS archive is not mapped. Skip the rest of the test.
124                 cleanup();
125                 throw new SkippedException("The CDS archive is not mapped");
126             }
127 
128             List testJavaOpts = Arrays.asList(Utils.getTestJavaOpts());
129 
130             if (testJavaOpts.contains("-XX:TieredStopAtLevel=1")) {
131                 // No MDOs are allocated in -XX:TieredStopAtLevel=1
132                 // The reason is methods being compiled aren't hot enough
133                 // Let's not call printmdo in such scenario
134                 cmds = List.of("printall", "jstack -v");
135             } else {
136                 cmds = List.of("printmdo -a", "printall", "jstack -v");
137             }
138 
139             Map<String, List<String>> expStrMap = new HashMap<>();
140             Map<String, List<String>> unExpStrMap = new HashMap<>();
141             expStrMap.put("printmdo -a", List.of(
142                 "CounterData",
143                 "BranchData"));
144             unExpStrMap.put("printmdo -a", List.of(
145                 "No suitable match for type of address"));
146             expStrMap.put("printall", List.of(
147                 "aload_0",
148                 "_nofast_aload_0",
149                 "_nofast_getfield",
150                 "_nofast_putfield",
151                 "Constant Pool of",
152                 "public static void main\\(java.lang.String\\[\\]\\)",
153                 "Bytecode",
154                 "invokevirtual",
155                 "checkcast",
156                 "Exception Table",
157                 "invokedynamic"));
158             unExpStrMap.put("printall", List.of(
159                 "sun.jvm.hotspot.types.WrongTypeException",
160                 "illegal code",
161                 "Failure occurred at bci",
162                 "No suitable match for type of address"));
163             expStrMap.put("jstack -v", List.of(
164                 "Common-Cleaner",
165                 "Method*"));
166             unExpStrMap.put("jstack -v", List.of(
167                 "sun.jvm.hotspot.debugger.UnmappedAddressException"));
168             test.runOnCore(coreFileName, cmds, expStrMap, unExpStrMap);
169         } catch (SkippedException e) {
170             throw e;
171         } catch (Exception ex) {
172             throw new RuntimeException("Test ERROR " + ex, ex);
173         }
174         cleanup();
175         System.out.println("Test PASSED");
176     }
177 
178     private static void cleanup() {
179         remove(SHARED_ARCHIVE_NAME);
180     }
181 
182     private static void remove(String item) {
183         File toDelete = new File(item);
184         toDelete.delete();
185     }
186 }