1 /*
   2  * Copyright (c) 2016, 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 
  26 package jdk.jfr.internal.tool;
  27 
  28 import java.util.Arrays;
  29 import java.util.Deque;
  30 import java.util.LinkedList;
  31 
  32 /**
  33  * Launcher class for the JDK_HOME\bin\jfr tool
  34  *
  35  */
  36 public final class Main {
  37 
  38     private static final int EXIT_OK = 0;
  39     private static final int EXIT_FAILED = 1;
  40     private static final int EXIT_WRONG_ARGUMENTS = 2;
  41 
  42     public static void main(String... args) {
  43         Deque<String> argList = new LinkedList<>(Arrays.asList(args));
  44         if (argList.isEmpty()) {
  45             System.out.println(Command.title);
  46             System.out.println();
  47             System.out.println("Before using this tool, you must have a recording file.");
  48             System.out.println("A file can be created by starting a recording from command line:");
  49             System.out.println();
  50             System.out.println(" java -XX:StartFlightRecording:filename=recording.jfr,duration=30s ... ");
  51             System.out.println();
  52             System.out.println("A recording can also be started on already running Java Virtual Machine:");
  53             System.out.println();
  54             System.out.println(" jcmd (to list available pids)");
  55             System.out.println(" jcmd <pid> JFR.start");
  56             System.out.println();
  57             System.out.println("Recording data can be dumped to file using the JFR.dump command:");
  58             System.out.println();
  59             System.out.println(" jcmd <pid> JFR.dump filename=recording.jfr");
  60             System.out.println();
  61             System.out.println("The contents of the recording can then be printed, for example:");
  62             System.out.println();
  63             System.out.println(" jfr print recording.jfr");
  64             System.out.println();
  65             System.out.println(" jfr print --events CPULoad,GarbageCollection recording.jfr");
  66             System.out.println();
  67             System.out.println(" jfr print --json --events CPULoad recording.jfr");
  68             System.out.println();
  69             System.out.println(" jfr print --categories \"GC,JVM,Java*\" recording.jfr");
  70             System.out.println();
  71             System.out.println(" jfr print --events \"jdk.*\" --stack-depth 64 recording.jfr");
  72             System.out.println();
  73             System.out.println(" jfr summary recording.jfr");
  74             System.out.println();
  75             System.out.println(" jfr metadata recording.jfr");
  76             System.out.println();
  77             System.out.println("For more information about available commands, use 'jfr help'");
  78             System.exit(EXIT_OK);
  79         }
  80         String command = argList.remove();
  81         for (Command c : Command.getCommands()) {
  82             if (c.matches(command)) {
  83                 try {
  84                     c.execute(argList);
  85                     System.exit(EXIT_OK);
  86                 } catch (UserDataException ude) {
  87                     System.err.println("jfr " + c.getName() + ": " + ude.getMessage());
  88                     System.exit(EXIT_FAILED);
  89                 } catch (UserSyntaxException use) {
  90                     System.err.println("jfr " + c.getName() + ": " + use.getMessage());
  91                     System.err.println();
  92                     System.err.println("Usage:");
  93                     System.err.println();
  94                     c.displayUsage(System.err);
  95                     System.exit(EXIT_WRONG_ARGUMENTS);
  96                 } catch (Throwable e) {
  97                     System.err.println("jfr " + c.getName() + ": unexpected internal error, " + e.getMessage());
  98                     e.printStackTrace();
  99                     System.exit(EXIT_FAILED);
 100                 }
 101             }
 102         }
 103         System.err.println("jfr: unknown command '" + command + "'");
 104         System.err.println();
 105         System.err.println("List of available commands:");
 106         System.err.println();
 107         Command.displayAvailableCommands(System.err);
 108         System.exit(EXIT_WRONG_ARGUMENTS);
 109     }
 110 }