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