1 /*
   2  * Copyright (c) 2012, 2019, 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.util.HashMap;
  30 import java.util.Map;
  31 
  32 import jdk.jfr.Recording;
  33 import jdk.jfr.internal.PlatformRecording;
  34 import jdk.jfr.internal.PrivateAccess;
  35 import jdk.jfr.internal.SecuritySupport.SafePath;
  36 import jdk.jfr.internal.Utils;
  37 import jdk.jfr.internal.WriteableUserPath;
  38 
  39 /**
  40  * JFR.dump
  41  *
  42  */
  43 //Instantiated by native
  44 final class DCmdDump extends AbstractDCmd {
  45     /**
  46      * Execute JFR.dump.
  47      *
  48      * @param recordingText name or id of the recording to dump, or
  49      *        <code>null</code>
  50      *
  51      * @param textPath file path where recording should be written.
  52      *
  53      * @return result output
  54      *
  55      * @throws DCmdException if the dump could not be completed
  56      */
  57     public String execute(String recordingText, String textPath,  Boolean pathToGcRoots) throws DCmdException {
  58         if (textPath == null) {
  59             throw new DCmdException("Failed to dump %s, missing filename.", recordingText);
  60         }
  61         Recording recording = findRecording(recordingText);
  62         try {
  63             SafePath dumpFile = resolvePath(textPath, "Failed to dump %s");
  64             // create file for JVM
  65             Utils.touch(dumpFile.toPath());
  66             PlatformRecording r = PrivateAccess.getInstance().getPlatformRecording(recording);
  67             WriteableUserPath wup = new WriteableUserPath(dumpFile.toPath());
  68 
  69             Map<String, String> overlay = new HashMap<>();
  70             Utils.updateSettingPathToGcRoots(overlay, pathToGcRoots);
  71 
  72             r.copyTo(wup, "Dumped by user", overlay);
  73             reportOperationComplete("Dumped", recording, dumpFile);
  74         } catch (IOException | InvalidPathException e) {
  75             throw new DCmdException("Failed to dump %s. Could not copy recording for dump. %s", recordingText, e.getMessage());
  76         }
  77         return getResult();
  78     }
  79 
  80 }