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