1 /* 2 * Copyright (c) 2012, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.jfr.internal.dcmd; 26 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 import java.nio.file.Files; 30 import java.nio.file.InvalidPathException; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.text.ParseException; 34 import java.time.Duration; 35 import java.util.Arrays; 36 import java.util.HashMap; 37 import java.util.Map; 38 39 import jdk.jfr.FlightRecorder; 40 import jdk.jfr.Recording; 41 import jdk.jfr.internal.JVM; 42 import jdk.jfr.internal.LogLevel; 43 import jdk.jfr.internal.LogTag; 44 import jdk.jfr.internal.Logger; 45 import jdk.jfr.internal.OldObjectSample; 46 import jdk.jfr.internal.PrivateAccess; 47 import jdk.jfr.internal.SecuritySupport.SafePath; 48 import jdk.jfr.internal.Type; 49 import jdk.jfr.internal.jfc.JFC; 50 51 /** 52 * JFR.start 53 * 54 */ 55 //Instantiated by native 56 final class DCmdStart extends AbstractDCmd { 57 58 /** 59 * Execute JFR.start. 60 * 61 * @param name optional name that can be used to identify recording. 62 * @param settings names of settings files to use, i.e. "default" or 63 * "default.jfc". 64 * @param delay delay before recording is started, in nanoseconds. Must be 65 * at least 1 second. 66 * @param duration duration of the recording, in nanoseconds. Must be at 67 * least 1 second. 68 * @param disk if recording should be persisted to disk 69 * @param path file path where recording data should be written 70 * @param maxAge how long recording data should be kept in the disk 71 * repository, or <code>0</code> if no limit should be set. 72 * 73 * @param maxSize the minimum amount data to keep in the disk repository 74 * before it is discarded, or <code>0</code> if no limit should be 75 * set. 76 * 77 * @param dumpOnExit if recording should dump on exit 78 * 79 * @return result output 80 * 81 * @throws DCmdException if recording could not be started 82 */ 83 @SuppressWarnings("resource") 84 public String execute(String name, String[] settings, Long delay, Long duration, Boolean disk, String path, Long maxAge, Long maxSize, Boolean dumpOnExit, Boolean pathToGcRoots) throws DCmdException { 85 if (LogTag.JFR_DCMD.shouldLog(LogLevel.DEBUG)) { 86 Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG, "Executing DCmdStart: name=" + name + 87 ", settings=" + (settings != null ? Arrays.asList(settings) : "(none)") + 88 ", delay=" + delay + 89 ", duration=" + duration + 90 ", disk=" + disk+ 91 ", filename=" + path + 92 ", maxage=" + maxAge + 93 ", maxsize=" + maxSize + 94 ", dumponexit =" + dumpOnExit + 95 ", path-to-gc-roots=" + pathToGcRoots); 96 } 97 if (name != null) { 98 try { 99 Integer.parseInt(name); 100 throw new DCmdException("Name of recording can't be numeric"); 101 } catch (NumberFormatException nfe) { 102 // ok, can't be mixed up with name 103 } 104 } 105 106 if (duration == null && Boolean.FALSE.equals(dumpOnExit) && path != null) { 107 throw new DCmdException("Filename can only be set for a time bound recording or if dumponexit=true. Set duration/dumponexit or omit filename."); 108 } 109 if (settings.length == 1 && settings[0].length() == 0) { 110 throw new DCmdException("No settings specified. Use settings=none to start without any settings"); 111 } 112 Map<String, String> s = new HashMap<>(); 113 114 if (settings == null || settings.length == 0) { 115 settings = new String[] { "default" }; 116 } 117 118 for (String configName : settings) { 119 try { 120 s.putAll(JFC.createKnown(configName).getSettings()); 121 } catch(FileNotFoundException e) { 122 throw new DCmdException("Could not find settings file'" + configName + "'", e); 123 } catch (IOException | ParseException e) { 124 throw new DCmdException("Could not parse settings file '" + settings[0] + "'", e); 125 } 126 } 127 128 OldObjectSample.updateSettingPathToGcRoots(s, pathToGcRoots); 129 130 if (duration != null) { 131 if (duration < 1000L * 1000L * 1000L) { 132 // to avoid typo, duration below 1s makes no sense 133 throw new DCmdException("Could not start recording, duration must be at least 1 second."); 134 } 135 } 136 137 if (delay != null) { 138 if (delay < 1000L * 1000L * 1000) { 139 // to avoid typo, delay shorter than 1s makes no sense. 140 throw new DCmdException("Could not start recording, delay must be at least 1 second."); 141 } 142 } 143 144 if (!FlightRecorder.isInitialized() && delay == null) { 145 initializeWithForcedInstrumentation(s); 146 } 147 148 Recording recording = new Recording(); 149 if (name != null) { 150 recording.setName(name); 151 } 152 153 if (disk != null) { 154 recording.setToDisk(disk.booleanValue()); 155 } 156 recording.setSettings(s); 157 SafePath safePath = null; 158 159 if (path != null) { 160 try { 161 if (dumpOnExit == null) { 162 // default to dumponexit=true if user specified filename 163 dumpOnExit = Boolean.TRUE; 164 } 165 Path p = Paths.get(path); 166 if (Files.isDirectory(p) && Boolean.TRUE.equals(dumpOnExit)) { 167 // Decide destination filename at dump time 168 // Purposely avoid generating filename in Recording#setDestination due to 169 // security concerns 170 PrivateAccess.getInstance().getPlatformRecording(recording).setDumpOnExitDirectory(new SafePath(p)); 171 } else { 172 safePath = resolvePath(recording, path); 173 recording.setDestination(safePath.toPath()); 174 } 175 } catch (IOException | InvalidPathException e) { 176 recording.close(); 177 throw new DCmdException("Could not start recording, not able to write to file %s. %s ", path, e.getMessage()); 178 } 179 } 180 181 if (maxAge != null) { 182 recording.setMaxAge(Duration.ofNanos(maxAge)); 183 } 184 185 if (maxSize != null) { 186 recording.setMaxSize(maxSize); 187 } 188 189 if (duration != null) { 190 recording.setDuration(Duration.ofNanos(duration)); 191 } 192 193 if (dumpOnExit != null) { 194 recording.setDumpOnExit(dumpOnExit); 195 } 196 197 if (delay != null) { 198 Duration dDelay = Duration.ofNanos(delay); 199 recording.scheduleStart(dDelay); 200 print("Recording " + recording.getId() + " scheduled to start in "); 201 printTimespan(dDelay, " "); 202 print("."); 203 } else { 204 recording.start(); 205 print("Started recording " + recording.getId() + "."); 206 } 207 208 if (recording.isToDisk() && duration == null && maxAge == null && maxSize == null) { 209 print(" No limit specified, using maxsize=250MB as default."); 210 recording.setMaxSize(250*1024L*1024L); 211 } 212 213 if (safePath != null && duration != null) { 214 println(" The result will be written to:"); 215 println(); 216 printPath(safePath); 217 } else { 218 println(); 219 println(); 220 String cmd = duration == null ? "dump" : "stop"; 221 String fileOption = path == null ? "filename=FILEPATH " : ""; 222 String recordingspecifier = "name=" + recording.getId(); 223 // if user supplied a name, use it. 224 if (name != null) { 225 recordingspecifier = "name=" + quoteIfNeeded(name); 226 } 227 print("Use jcmd " + getPid() + " JFR." + cmd + " " + recordingspecifier + " " + fileOption + "to copy recording data to file."); 228 println(); 229 } 230 return getResult(); 231 } 232 233 234 // Instruments JDK-events on class load to reduce startup time 235 private void initializeWithForcedInstrumentation(Map<String, String> settings) { 236 if (!hasJDKEvents(settings)) { 237 return; 238 } 239 JVM jvm = JVM.getJVM(); 240 try { 241 jvm.setForceInstrumentation(true); 242 FlightRecorder.getFlightRecorder(); 243 } finally { 244 jvm.setForceInstrumentation(false); 245 } 246 } 247 248 private boolean hasJDKEvents(Map<String, String> settings) { 249 String[] eventNames = new String[7]; 250 eventNames[0] = "FileRead"; 251 eventNames[1] = "FileWrite"; 252 eventNames[2] = "SocketRead"; 253 eventNames[3] = "SocketWrite"; 254 eventNames[4] = "JavaErrorThrow"; 255 eventNames[5] = "JavaExceptionThrow"; 256 eventNames[6] = "FileForce"; 257 for (String eventName : eventNames) { 258 if ("true".equals(settings.get(Type.EVENT_NAME_PREFIX + eventName + "#enabled"))) { 259 return true; 260 } 261 } 262 return false; 263 } 264 }