1 /*
   2  * Copyright (c) 2018, 2019, 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 os.family != "windows"
  31  * @requires vm.flavor == "server"
  32  * @library /test/lib
  33  * @modules java.base/jdk.internal.misc
  34  * @run main/othervm/timeout=2400 -Xmx1g ClhsdbCDSCore
  35  */
  36 
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.nio.file.Files;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.HashMap;
  45 import java.util.List;
  46 import java.util.Map;
  47 import java.util.Scanner;
  48 import java.util.regex.Matcher;
  49 import java.util.regex.Pattern;
  50 
  51 import jdk.internal.misc.Unsafe;
  52 
  53 import jdk.test.lib.Asserts;
  54 import jdk.test.lib.Platform;
  55 import jdk.test.lib.cds.CDSOptions;
  56 import jdk.test.lib.cds.CDSTestUtils;
  57 import jdk.test.lib.process.OutputAnalyzer;
  58 import jdk.test.lib.process.ProcessTools;
  59 import jdk.test.lib.SA.SATestUtils;
  60 
  61 import jtreg.SkippedException;
  62 
  63 class CrashApp {
  64     public static void main(String[] args) {
  65         Unsafe.getUnsafe().putInt(0L, 0);
  66     }
  67 }
  68 
  69 public class ClhsdbCDSCore {
  70 
  71     private static final String TEST_CDS_CORE_FILE_NAME = "cds_core_file";
  72     private static final String LOCATIONS_STRING = "location: ";
  73     private static final String RUN_SHELL_NO_LIMIT = "ulimit -c unlimited && ";
  74     private static final String SHARED_ARCHIVE_NAME = "ArchiveForClhsdbCDSCore.jsa";
  75     private static final String CORE_PATTERN_FILE_NAME = "/proc/sys/kernel/core_pattern";
  76 
  77     public static void main(String[] args) throws Exception {
  78         System.out.println("Starting ClhsdbCDSCore test");
  79         cleanup();
  80 
  81         try {
  82             CDSOptions opts = (new CDSOptions()).setArchiveName(SHARED_ARCHIVE_NAME);
  83             CDSTestUtils.createArchiveAndCheck(opts);
  84 
  85             String[] jArgs = {
  86                 "-Xmx512m",
  87                 "-XX:+UnlockDiagnosticVMOptions",
  88                 "-XX:SharedArchiveFile=" + SHARED_ARCHIVE_NAME,
  89                 "-XX:+CreateCoredumpOnCrash",
  90                 "-Xshare:auto",
  91                 "-XX:+ProfileInterpreter",
  92                 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED",
  93                 CrashApp.class.getName()
  94             };
  95 
  96             OutputAnalyzer crashOut;
  97             try {
  98                List<String> options = new ArrayList<>();
  99                options.addAll(Arrays.asList(jArgs));
 100                crashOut =
 101                    ProcessTools.executeProcess(getTestJavaCommandlineWithPrefix(
 102                    RUN_SHELL_NO_LIMIT, options.toArray(new String[0])));
 103             } catch (Throwable t) {
 104                throw new Error("Can't execute the java cds process.", t);
 105             }
 106 
 107             System.out.println(crashOut.getOutput());
 108             String crashOutputString = crashOut.getOutput();
 109             SATestUtils.unzipCores(new File("."));
 110             String coreFileLocation = getCoreFileLocation(crashOutputString);
 111             if (coreFileLocation == null) {
 112                 if (Platform.isOSX()) {
 113                     File coresDir = new File("/cores");
 114                     if (!coresDir.isDirectory()) {
 115                         throw new Error("cores is not a directory");
 116                     }
 117                     // the /cores directory is usually not writable on macOS 10.15
 118                     final String osVersion = System.getProperty("os.version");
 119                     if (osVersion == null) {
 120                         throw new Error("Cannot query the 'os.version' property!");
 121                     }
 122                     if (!coresDir.canWrite()) {
 123                         if (osVersion.startsWith("10.15")) {
 124                             System.out.println("on macOS 10.15 /cores is usually not writable, skipping test.");
 125                             return;
 126                         } else {
 127                             throw new Error("cores does not have write permissions");
 128                         }
 129                     }
 130                 } else if (Platform.isLinux()) {
 131                     // Check if a crash report tool is installed.
 132                     File corePatternFile = new File(CORE_PATTERN_FILE_NAME);
 133                     Scanner scanner = new Scanner(corePatternFile);
 134                     while (scanner.hasNextLine()) {
 135                         String line = scanner.nextLine();
 136                         line = line.trim();
 137                         System.out.println(line);
 138                         if (line.startsWith("|")) {
 139                             System.out.println(
 140                                 "\nThis system uses a crash report tool ($cat /proc/sys/kernel/core_pattern).\n" +
 141                                 "Core files might not be generated. Please reset /proc/sys/kernel/core_pattern\n" +
 142                                 "to enable core generation. Skipping this test.");
 143                             cleanup();
 144                             throw new SkippedException("This system uses a crash report tool");
 145                         }
 146                     }
 147                 }
 148                 throw new Error("Couldn't find core file location in: '" + crashOutputString + "'");
 149             }
 150             try {
 151                 Asserts.assertGT(new File(coreFileLocation).length(), 0L, "Unexpected core size");
 152                 Files.move(Paths.get(coreFileLocation), Paths.get(TEST_CDS_CORE_FILE_NAME));
 153             } catch (IOException ioe) {
 154                 throw new Error("Can't move core file: " + ioe, ioe);
 155             }
 156 
 157             ClhsdbLauncher test = new ClhsdbLauncher();
 158 
 159             // Ensure that UseSharedSpaces is turned on.
 160             List<String> cmds = List.of("flags UseSharedSpaces");
 161 
 162             String useSharedSpacesOutput = test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds,
 163                                                           null, null);
 164 
 165             if (useSharedSpacesOutput == null) {
 166                 // Output could be null due to attach permission issues.
 167                 cleanup();
 168                 throw new SkippedException("Could not determine the UseSharedSpaces value");
 169             }
 170 
 171             if (!useSharedSpacesOutput.contains("true")) {
 172                 // CDS archive is not mapped. Skip the rest of the test.
 173                 cleanup();
 174                 throw new SkippedException("The CDS archive is not mapped");
 175             }
 176 
 177             cmds = List.of("printmdo -a", "printall", "jstack -v");
 178 
 179             Map<String, List<String>> expStrMap = new HashMap<>();
 180             Map<String, List<String>> unExpStrMap = new HashMap<>();
 181             expStrMap.put("printmdo -a", List.of(
 182                 "CounterData",
 183                 "BranchData"));
 184             unExpStrMap.put("printmdo -a", List.of(
 185                 "No suitable match for type of address"));
 186             expStrMap.put("printall", List.of(
 187                 "aload_0",
 188                 "_nofast_aload_0",
 189                 "_nofast_getfield",
 190                 "_nofast_putfield",
 191                 "Constant Pool of",
 192                 "public static void main\\(java.lang.String\\[\\]\\)",
 193                 "Bytecode",
 194                 "invokevirtual",
 195                 "checkcast",
 196                 "Exception Table",
 197                 "invokedynamic"));
 198             unExpStrMap.put("printall", List.of(
 199                 "sun.jvm.hotspot.types.WrongTypeException",
 200                 "illegal code",
 201                 "Failure occurred at bci",
 202                 "No suitable match for type of address"));
 203             expStrMap.put("jstack -v", List.of(
 204                 "Common-Cleaner",
 205                 "Method*"));
 206             unExpStrMap.put("jstack -v", List.of(
 207                 "sun.jvm.hotspot.debugger.UnmappedAddressException"));
 208             test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds, expStrMap, unExpStrMap);
 209         } catch (SkippedException e) {
 210             throw e;
 211         } catch (Exception ex) {
 212             throw new RuntimeException("Test ERROR " + ex, ex);
 213         }
 214         cleanup();
 215         System.out.println("Test PASSED");
 216     }
 217 
 218     // lets search for a few possible locations using process output and return existing location
 219     private static String getCoreFileLocation(String crashOutputString) {
 220         Asserts.assertTrue(crashOutputString.contains(LOCATIONS_STRING),
 221             "Output doesn't contain the location of core file.");
 222         String stringWithLocation = Arrays.stream(crashOutputString.split("\\r?\\n"))
 223             .filter(str -> str.contains(LOCATIONS_STRING))
 224             .findFirst()
 225             .get();
 226         stringWithLocation = stringWithLocation.substring(stringWithLocation
 227             .indexOf(LOCATIONS_STRING) + LOCATIONS_STRING.length());
 228         System.out.println("getCoreFileLocation found stringWithLocation = " + stringWithLocation);
 229         String coreWithPid;
 230         if (stringWithLocation.contains("or ")) {
 231             Matcher m = Pattern.compile("or.* ([^ ]+[^\\)])\\)?").matcher(stringWithLocation);
 232             if (!m.find()) {
 233                 throw new Error("Couldn't find path to core inside location string");
 234             }
 235             coreWithPid = m.group(1);
 236         } else {
 237             coreWithPid = stringWithLocation.trim();
 238         }
 239         if (new File(coreWithPid).exists()) {
 240             return coreWithPid;
 241         }
 242         String justCore = Paths.get("core").toString();
 243         if (new File(justCore).exists()) {
 244             return justCore;
 245         }
 246         Path coreWithPidPath = Paths.get(coreWithPid);
 247         String justFile = coreWithPidPath.getFileName().toString();
 248         if (new File(justFile).exists()) {
 249             return justFile;
 250         }
 251         Path parent = coreWithPidPath.getParent();
 252         if (parent != null) {
 253             String coreWithoutPid = parent.resolve("core").toString();
 254             if (new File(coreWithoutPid).exists()) {
 255                 return coreWithoutPid;
 256             }
 257         }
 258         return null;
 259     }
 260 
 261     private static String[] getTestJavaCommandlineWithPrefix(String prefix, String... args) {
 262         try {
 263             String cmd = ProcessTools.getCommandLine(ProcessTools.createJavaProcessBuilder(true, args));
 264             return new String[]{"sh", "-c", prefix + cmd};
 265         } catch (Throwable t) {
 266             throw new Error("Can't create process builder: " + t, t);
 267         }
 268     }
 269 
 270     private static void cleanup() {
 271         remove(TEST_CDS_CORE_FILE_NAME);
 272         remove(SHARED_ARCHIVE_NAME);
 273     }
 274 
 275     private static void remove(String item) {
 276         File toDelete = new File(item);
 277         toDelete.delete();
 278     }
 279 }