1 /* 2 * Copyright (c) 2015, 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 package org.graalvm.compiler.hotspot; 26 27 import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.GRAAL_OPTION_PROPERTY_PREFIX; 28 import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.defaultOptions; 29 30 import java.io.FileNotFoundException; 31 import java.io.FileOutputStream; 32 import java.io.IOException; 33 import java.io.OutputStream; 34 import java.io.PrintStream; 35 import java.util.List; 36 37 import org.graalvm.compiler.core.common.SuppressFBWarnings; 38 import org.graalvm.compiler.debug.TTYStreamProvider; 39 import org.graalvm.compiler.options.Option; 40 import org.graalvm.compiler.options.OptionKey; 41 import org.graalvm.compiler.options.OptionType; 42 import org.graalvm.compiler.serviceprovider.GraalServices; 43 import org.graalvm.compiler.serviceprovider.ServiceProvider; 44 45 import jdk.vm.ci.common.NativeImageReinitialize; 46 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 47 import jdk.vm.ci.services.Services; 48 49 @ServiceProvider(TTYStreamProvider.class) 50 public class HotSpotTTYStreamProvider implements TTYStreamProvider { 51 52 public static class Options { 53 54 // @formatter:off 55 @Option(help = "File to which logging is sent. A %p in the name will be replaced with a string identifying " + 56 "the process, usually the process id and %t will be replaced by System.currentTimeMillis(). " + 57 "Using %o as filename sends logging to System.out whereas %e sends logging to System.err.", type = OptionType.Expert) 58 public static final LogStreamOptionKey LogFile = new LogStreamOptionKey(); 59 // @formatter:on 60 } 61 62 @Override 63 public PrintStream getStream() { 64 return Options.LogFile.getStream(); 65 } 66 67 /** 68 * An option for a configurable file name that can also open a {@link PrintStream} on the file. 69 * If no value is given for the option, the stream will output to HotSpot's 70 * {@link HotSpotJVMCIRuntime#getLogStream() log} stream 71 */ 72 private static class LogStreamOptionKey extends OptionKey<String> { 73 74 LogStreamOptionKey() { 75 super(null); 76 } 77 78 /** 79 * @return {@code nameTemplate} with all instances of %p replaced by 80 * {@link GraalServices#getExecutionID()} and %t by 81 * {@link System#currentTimeMillis()}. Checks %o and %e are not combined with any 82 * other characters. 83 */ 84 private static String makeFilename(String nameTemplate) { 85 String name = nameTemplate; 86 if (name.contains("%p")) { 87 name = name.replaceAll("%p", GraalServices.getExecutionID()); 88 } 89 if (name.contains("%t")) { 90 name = name.replaceAll("%t", String.valueOf(System.currentTimeMillis())); 91 } 92 93 for (String subst : new String[]{"%o", "%e"}) { 94 if (name.contains(subst) && !name.equals(subst)) { 95 throw new IllegalArgumentException("LogFile substitution " + subst + " cannot be combined with any other characters"); 96 } 97 } 98 99 return name; 100 } 101 102 /** 103 * An output stream that redirects to {@link HotSpotJVMCIRuntime#getLogStream()}. The 104 * {@link HotSpotJVMCIRuntime#getLogStream()} value is only accessed the first time an IO 105 * operation is performed on the stream. This is required to break a deadlock in early JVMCI 106 * initialization. 107 */ 108 class DelayedOutputStream extends OutputStream { 109 @NativeImageReinitialize private volatile OutputStream lazy; 110 111 private OutputStream lazy() { 112 if (lazy == null) { 113 synchronized (this) { 114 if (lazy == null) { 115 String nameTemplate = LogStreamOptionKey.this.getValue(defaultOptions()); 116 if (nameTemplate != null) { 117 String name = makeFilename(nameTemplate); 118 switch (name) { 119 case "%o": 120 lazy = System.out; 121 break; 122 case "%e": 123 lazy = System.err; 124 break; 125 default: 126 try { 127 final boolean enableAutoflush = true; 128 FileOutputStream result = new FileOutputStream(name); 129 if (!Services.IS_IN_NATIVE_IMAGE) { 130 printVMConfig(enableAutoflush, result); 131 } else { 132 // There are no VM arguments for the libgraal 133 // library. 134 } 135 lazy = result; 136 } catch (FileNotFoundException e) { 137 throw new RuntimeException("couldn't open file: " + name, e); 138 } 139 } 140 return lazy; 141 } 142 143 lazy = HotSpotJVMCIRuntime.runtime().getLogStream(); 144 PrintStream ps = new PrintStream(lazy); 145 ps.printf("[Use -D%sLogFile=<path> to redirect Graal log output to a file.]%n", GRAAL_OPTION_PROPERTY_PREFIX); 146 ps.flush(); 147 } 148 } 149 } 150 return lazy; 151 } 152 153 @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "false positive on dead store to `ps`") 154 private void printVMConfig(final boolean enableAutoflush, FileOutputStream result) { 155 /* 156 * Add the JVM and Java arguments to the log file to help identity it. 157 */ 158 PrintStream ps = new PrintStream(result, enableAutoflush); 159 List<String> inputArguments = GraalServices.getInputArguments(); 160 if (inputArguments != null) { 161 ps.println("VM Arguments: " + String.join(" ", inputArguments)); 162 } 163 String cmd = Services.getSavedProperties().get("sun.java.command"); 164 if (cmd != null) { 165 ps.println("sun.java.command=" + cmd); 166 } 167 } 168 169 @Override 170 public void write(byte[] b, int off, int len) throws IOException { 171 lazy().write(b, off, len); 172 } 173 174 @Override 175 public void write(int b) throws IOException { 176 lazy().write(b); 177 } 178 179 @Override 180 public void flush() throws IOException { 181 lazy().flush(); 182 } 183 184 @Override 185 public void close() throws IOException { 186 lazy().close(); 187 } 188 } 189 190 /** 191 * Gets the print stream configured by this option. If no file is configured, the print 192 * stream will output to HotSpot's {@link HotSpotJVMCIRuntime#getLogStream() log} stream. 193 */ 194 public PrintStream getStream() { 195 return new PrintStream(new DelayedOutputStream()); 196 } 197 } 198 199 }