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=" + (settings != null ? Arrays.asList(settings) : "(none)") + 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 if (disk != null) { 150 recording.setToDisk(disk.booleanValue()); 151 } 152 recording.setSettings(s); 153 SafePath safePath = null; 154 155 if (path != null) { 156 try { 157 if (dumpOnExit == null) { 158 // default to dumponexit=true if user specified filename 159 dumpOnExit = Boolean.TRUE; 160 } 161 Path p = Paths.get(path); 162 if (Files.isDirectory(p) && Boolean.TRUE.equals(dumpOnExit)) { 163 // Decide destination filename at dump time 164 // Purposely avoid generating filename in Recording#setDestination due to 165 // security concerns 166 PrivateAccess.getInstance().getPlatformRecording(recording).setDumpOnExitDirectory(new SafePath(p)); 167 } else { 168 safePath = resolvePath(recording, path); 169 recording.setDestination(safePath.toPath()); 170 } 171 } catch (IOException | InvalidPathException e) { 172 recording.close(); 173 throw new DCmdException("Could not start recording, not able to write to file %s. %s ", path, e.getMessage()); 174 } 175 } 176 177 if (maxAge != null) { 178 recording.setMaxAge(Duration.ofNanos(maxAge)); 179 } 180 181 if (maxSize != null) { 182 recording.setMaxSize(maxSize); 183 } 184 185 if (duration != null) { 186 recording.setDuration(Duration.ofNanos(duration)); 187 } 188 189 if (dumpOnExit != null) { 190 recording.setDumpOnExit(dumpOnExit); 191 } 192 193 if (delay != null) { 194 Duration dDelay = Duration.ofNanos(delay); 195 recording.scheduleStart(dDelay); 196 print("Recording " + recording.getId() + " scheduled to start in "); 197 printTimespan(dDelay, " "); 198 print("."); 199 } else { 200 recording.start(); 201 print("Started recording " + recording.getId() + "."); 202 } 203 204 if (recording.isToDisk() && duration == null && maxAge == null && maxSize == null) { 205 print(" No limit specified, using maxsize=250MB as default."); 206 recording.setMaxSize(250*1024L*1024L); 207 } 208 209 if (safePath != null && duration != null) { 210 println(" The result will be written to:"); 211 println(); 212 printPath(safePath); 213 } else { 214 println(); 215 println(); 216 String cmd = duration == null ? "dump" : "stop"; 217 String fileOption = path == null ? "filename=FILEPATH " : ""; 218 String recordingspecifier = "name=" + recording.getId(); 219 // if user supplied a name, use it. 220 if (name != null) { 221 recordingspecifier = "name=" + quoteIfNeeded(name); 222 } 223 print("Use jcmd " + getPid() + " JFR." + cmd + " " + recordingspecifier + " " + fileOption + "to copy recording data to file."); 224 println(); 225 } 226 return getResult(); 227 } 228 229 230 // Instruments JDK-events on class load to reduce startup time 231 private void initializeWithForcedInstrumentation(Map<String, String> settings) { 232 if (!hasJDKEvents(settings)) { 233 return; 234 } 235 JVM jvm = JVM.getJVM(); 236 try { 237 jvm.setForceInstrumentation(true); 238 FlightRecorder.getFlightRecorder(); 239 } finally { 240 jvm.setForceInstrumentation(false); 241 } 242 } 243 244 private boolean hasJDKEvents(Map<String, String> settings) { 245 String[] eventNames = new String[7]; 246 eventNames[0] = "FileRead"; 247 eventNames[1] = "FileWrite"; 248 eventNames[2] = "SocketRead"; 249 eventNames[3] = "SocketWrite"; 250 eventNames[4] = "JavaErrorThrow"; 251 eventNames[5] = "JavaExceptionThrow"; 252 eventNames[6] = "FileForce"; 253 for (String eventName : eventNames) { 254 if ("true".equals(settings.get(Type.EVENT_NAME_PREFIX + eventName + "#enabled"))) { 255 return true; 256 } 257 } 258 return false; 259 } 260 }