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.InvalidPathException;
  29 import java.nio.file.Paths;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.internal.LogLevel;
  33 import jdk.jfr.internal.LogTag;
  34 import jdk.jfr.internal.Logger;
  35 import jdk.jfr.internal.SecuritySupport.SafePath;
  36 
  37 /**
  38  * JFR.stop
  39  *
  40  */
  41 // Instantiated by native
  42 final class DCmdStop extends AbstractDCmd {
  43 
  44     /**
  45      * Execute JFR.stop
  46      *
  47      * Requires that either <code>name or <code>id</code> is set.
  48      *
  49      * @param name name or id of the recording to stop.
  50      *
  51      * @param filename file path where data should be written after recording has
  52      *        been stopped, or <code>null</code> if recording shouldn't be written
  53      *        to disk.
  54      * @return result text
  55      *
  56      * @throws DCmdException if recording could not be stopped
  57      */
  58     public String execute(String name, String filename) throws DCmdException {
  59         if (Logger.shouldLog(LogTag.JFR_DCMD, LogLevel.DEBUG)) {
  60             Logger.log(LogTag.JFR_DCMD, LogLevel.DEBUG, "Executing DCmdStart: name=" + name + ", filename=" + filename);
  61         }
  62 
  63         try {
  64             SafePath safePath = null;
  65             Recording recording = findRecording(name);
  66             if (filename != null) {
  67                 try {
  68                     // Ensure path is valid. Don't generate safePath if filename == null, as a user may
  69                     // want to stop recording without a dump
  70                     safePath = resolvePath(null, filename);
  71                     recording.setDestination(Paths.get(filename));
  72                 } catch (IOException | InvalidPathException  e) {
  73                     throw new DCmdException("Failed to stop %s. Could not set destination for \"%s\" to file %s", recording.getName(), filename, e.getMessage());
  74                 }
  75             }
  76             recording.stop();
  77             reportOperationComplete("Stopped", recording.getName(), safePath);
  78             recording.close();
  79             return getResult();
  80         } catch (InvalidPathException | DCmdException e) {
  81             if (filename != null) {
  82                 throw new DCmdException("Could not write recording \"%s\" to file. %s", name, e.getMessage());
  83             }
  84             throw new DCmdException(e, "Could not stop recording \"%s\".", name, e.getMessage());
  85         }
  86     }
  87 }