src/jdk.jfr/share/classes/jdk/jfr/internal/dcmd/AbstractDCmd.java
author egahlin
Tue, 15 May 2018 20:24:34 +0200
changeset 50113 caf115bb98ad
child 50226 408021edf22f
permissions -rw-r--r--
8199712: Flight Recorder Reviewed-by: coleenp, ihse, erikj, dsamersoff, mseledtsov, egahlin, mgronlun Contributed-by: erik.gahlin@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
package jdk.jfr.internal.dcmd;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
import java.io.IOException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
import java.io.PrintWriter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
import java.io.StringWriter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
import java.nio.file.InvalidPathException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
import java.nio.file.Path;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
import java.time.Duration;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
import java.util.ArrayList;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
import java.util.Collections;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
import jdk.jfr.FlightRecorder;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
import jdk.jfr.Recording;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
import jdk.jfr.internal.SecuritySupport;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
import jdk.jfr.internal.SecuritySupport.SafePath;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
import jdk.jfr.internal.Utils;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
/**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
 * Base class for JFR diagnostic commands
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    46
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
abstract class AbstractDCmd {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
    private final StringWriter result;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
    private final PrintWriter log;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
    protected AbstractDCmd() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
        result = new StringWriter();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
        log = new PrintWriter(result);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
    protected final FlightRecorder getFlightRecorder() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
        return FlightRecorder.getFlightRecorder();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
    public final String getResult() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
        return result.toString();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
    protected final SafePath resolvePath(String path, String errorMsg) throws DCmdException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
        if (path == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
            return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    69
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
            return new SafePath(path);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
        } catch (InvalidPathException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
            throw new DCmdException(e, errorMsg, ", invalid path \"" + path + "\".");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
    protected final Recording findRecording(String name) throws DCmdException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
            return findRecordingById(Integer.parseInt(name));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
        } catch (NumberFormatException nfe) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
            // User specified a name, not an id.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
            return findRecordingByName(name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
    protected final void reportOperationComplete(String actionPrefix, Recording r, SafePath file) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
        print(actionPrefix);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
        print(" recording ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
        print("\"" + r.getName() + "\"");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
        if (file != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
            print(",");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
            try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
                print(" ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
                long bytes = SecuritySupport.getFileSize(file);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
                printBytes(bytes, " ");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
            } catch (IOException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
                // Ignore, not essential
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
            println(" written to:");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
            println();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
            printPath(file);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
        } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
            println(".");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
    protected final List<Recording> getRecordings() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   107
        List<Recording> list = new ArrayList<>(getFlightRecorder().getRecordings());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   108
        Collections.sort(list, (a, b) -> a.getName().compareTo(b.getName()));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   109
        return list;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   110
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   111
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   112
    static String quoteIfNeeded(String text) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   113
        if (text.contains(" ")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   114
            return "\\\"" + text + "\\\"";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   115
        } else {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   116
            return text;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   117
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   118
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   119
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   120
    protected final void println() {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   121
        log.println();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   122
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   123
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   124
    protected final void print(String s) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   125
        log.print(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   126
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   127
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   128
    protected final void print(String s, Object... args) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   129
        log.printf(s, args);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   130
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   131
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   132
    protected final void println(String s, Object... args) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   133
        print(s, args);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   134
        println();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
    protected final void printBytes(long bytes, String separation) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
       print(Utils.formatBytes(bytes, separation));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   141
    protected final void printTimespan(Duration timespan, String separator) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   142
        print(Utils.formatTimespan(timespan, separator));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   143
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   144
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   145
    protected final void printPath(SafePath path) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   146
        if (path == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   147
            print("N/A");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   148
            return;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   149
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   150
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   151
            printPath(SecuritySupport.getAbsolutePath(path).toPath());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   152
        } catch (IOException ioe) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
            printPath(path.toPath());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
    protected final void printPath(Path path) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
            println(path.toAbsolutePath().toString());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
        } catch (SecurityException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
            // fall back on filename
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
            println(path.toString());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
    private Recording findRecordingById(int id) throws DCmdException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
        for (Recording r : getFlightRecorder().getRecordings()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
            if (r.getId() == id) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
                return r;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   171
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   172
        throw new DCmdException("Could not find %d.\n\nUse JFR.check without options to see list of all available recordings.", id);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   173
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   174
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   175
    private Recording findRecordingByName(String name) throws DCmdException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   176
        for (Recording recording : getFlightRecorder().getRecordings()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
            if (name.equals(recording.getName())) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
                return recording;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
        throw new DCmdException("Could not find %s.\n\nUse JFR.check without options to see list of all available recordings.", name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
}