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