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 os.family != "windows" 30 * @requires vm.flavor == "server" 31 * @library /test/lib 32 * @modules java.base/jdk.internal.misc 33 * @run main/othervm/timeout=2400 -Xmx1g ClhsdbCDSCore 34 */ 35 36 import java.util.List; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Map; 40 import java.util.HashMap; 41 import jdk.test.lib.process.ProcessTools; 42 import jdk.test.lib.Platform; 43 import jdk.test.lib.process.OutputAnalyzer; 44 import jdk.test.lib.cds.CDSTestUtils; 45 import jdk.test.lib.cds.CDSOptions; 46 import java.io.IOException; 47 import java.io.File; 48 import java.nio.file.Files; 49 import java.nio.file.Path; 50 import java.nio.file.Paths; 51 import jdk.test.lib.Asserts; 52 import java.util.regex.Pattern; 53 import java.util.regex.Matcher; 54 import jdk.internal.misc.Unsafe; 55 56 class CrashApp { 57 public static void main(String[] args) { 58 Unsafe.getUnsafe().putInt(0L, 0); 59 } 60 } 61 62 public class ClhsdbCDSCore { 63 64 private static final String TEST_CDS_CORE_FILE_NAME = "cds_core_file"; 65 private static final String LOCATIONS_STRING = "location: "; 66 private static final String RUN_SHELL_NO_LIMIT = "ulimit -c unlimited && "; 67 private static final String SHARED_ARCHIVE_NAME = "ArchiveForClhsdbCDSCore.jsa"; 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 "-XX:+UnlockDiagnosticVMOptions", 79 "-XX:SharedArchiveFile=" + SHARED_ARCHIVE_NAME, 80 "-XX:+CreateCoredumpOnCrash", 81 "-Xshare:auto", 82 "-XX:+ProfileInterpreter", 83 "--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED", 84 CrashApp.class.getName() 85 }; 86 87 OutputAnalyzer crashOut; 88 try { 89 List<String> options = new ArrayList<>(); 90 options.addAll(Arrays.asList(jArgs)); 91 crashOut = 92 ProcessTools.executeProcess(getTestJavaCommandlineWithPrefix( 93 RUN_SHELL_NO_LIMIT, options.toArray(new String[0]))); 94 } catch (Throwable t) { 95 throw new Error("Can't execute the java cds process."); 96 } 97 System.out.println(crashOut.getOutput()); 98 String crashOutputString = crashOut.getOutput(); 99 String coreFileLocation = getCoreFileLocation(crashOutputString); 100 if (coreFileLocation == null) { 101 if (Platform.isOSX()) { 102 File coresDir = new File("/cores"); 103 if (!coresDir.isDirectory() || !coresDir.canWrite()) { 104 throw new Error("cores is not a directory or does not have write permissions"); 105 } 106 } 107 throw new Error("Couldn't find core file location in: '" + crashOutputString + "'"); 108 } 109 try { 110 Asserts.assertGT(new File(coreFileLocation).length(), 0L, "Unexpected core size"); 111 Files.move(Paths.get(coreFileLocation), Paths.get(TEST_CDS_CORE_FILE_NAME)); 112 } catch (IOException ioe) { 113 throw new Error("Can't move core file: " + ioe, ioe); 114 } 115 116 ClhsdbLauncher test = new ClhsdbLauncher(); 117 118 // Ensure that UseSharedSpaces is turned on. 119 List<String> cmds = List.of("flags UseSharedSpaces"); 120 121 String useSharedSpacesOutput = test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds, 122 null, null); 123 124 if (useSharedSpacesOutput == null) { 125 // Output could be null due to attach permission issues. 126 System.out.println("Could not determine the UseSharedSpaces value - test skipped."); 127 cleanup(); 128 return; 129 } 130 131 if (useSharedSpacesOutput.contains("true") == false) { 132 // CDS archive is not mapped. Skip the rest of the test. 133 System.out.println("The CDS archive is not mapped - test skipped."); 134 cleanup(); 135 return; 136 } 137 138 cmds = List.of("printmdo -a", "printall"); 139 140 Map<String, List<String>> expStrMap = new HashMap<>(); 141 Map<String, List<String>> unExpStrMap = new HashMap<>(); 142 expStrMap.put("printmdo -a", List.of( 143 "CounterData", 144 "BranchData")); 145 unExpStrMap.put("printmdo -a", List.of( 146 "No suitable match for type of address")); 147 expStrMap.put("printall", List.of( 148 "aload_0", 149 "Constant Pool of", 150 "public static void main(java.lang.String[])", 151 "Bytecode", 152 "invokevirtual", 153 "checkcast", 154 "Exception Table", 155 "invokedynamic")); 156 unExpStrMap.put("printall", List.of( 157 "sun.jvm.hotspot.types.WrongTypeException", 158 "No suitable match for type of address")); 159 test.runOnCore(TEST_CDS_CORE_FILE_NAME, cmds, expStrMap, unExpStrMap); 160 } catch (Exception ex) { 161 throw new RuntimeException("Test ERROR " + ex, ex); 162 } 163 cleanup(); 164 System.out.println("Test PASSED"); 165 } 166 167 // lets search for a few possible locations using process output and return existing location 168 private static String getCoreFileLocation(String crashOutputString) { 169 Asserts.assertTrue(crashOutputString.contains(LOCATIONS_STRING), 170 "Output doesn't contain the location of core file."); 171 String stringWithLocation = Arrays.stream(crashOutputString.split("\\r?\\n")) 172 .filter(str -> str.contains(LOCATIONS_STRING)) 173 .findFirst() 174 .get(); 175 stringWithLocation = stringWithLocation.substring(stringWithLocation 176 .indexOf(LOCATIONS_STRING) + LOCATIONS_STRING.length()); 177 String coreWithPid; 178 if (stringWithLocation.contains("or ")) { 179 Matcher m = Pattern.compile("or.* ([^ ]+[^\\)])\\)?").matcher(stringWithLocation); 180 if (!m.find()) { 181 throw new Error("Couldn't find path to core inside location string"); 182 } 183 coreWithPid = m.group(1); 184 } else { 185 coreWithPid = stringWithLocation.trim(); 186 } 187 if (new File(coreWithPid).exists()) { 188 return coreWithPid; 189 } 190 String justCore = Paths.get("core").toString(); 191 if (new File(justCore).exists()) { 192 return justCore; 193 } 194 Path coreWithPidPath = Paths.get(coreWithPid); 195 String justFile = coreWithPidPath.getFileName().toString(); 196 if (new File(justFile).exists()) { 197 return justFile; 198 } 199 Path parent = coreWithPidPath.getParent(); 200 if (parent != null) { 201 String coreWithoutPid = parent.resolve("core").toString(); 202 if (new File(coreWithoutPid).exists()) { 203 return coreWithoutPid; 204 } 205 } 206 return null; 207 } 208 209 private static String[] getTestJavaCommandlineWithPrefix(String prefix, String... args) { 210 try { 211 String cmd = ProcessTools.getCommandLine(ProcessTools.createJavaProcessBuilder(true, args)); 212 return new String[]{"sh", "-c", prefix + cmd}; 213 } catch (Throwable t) { 214 throw new Error("Can't create process builder: " + t, t); 215 } 216 } 217 218 private static void cleanup() { 219 remove(TEST_CDS_CORE_FILE_NAME); 220 remove(SHARED_ARCHIVE_NAME); 221 } 222 223 private static void remove(String item) { 224 File toDelete = new File(item); 225 toDelete.delete(); 226 } 227 }