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