|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, 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 org.graalvm.visualvm.jfr.impl; |
| 26 | + |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import org.graalvm.visualvm.application.Application; |
| 31 | +import org.graalvm.visualvm.application.ApplicationFinder; |
| 32 | +import org.graalvm.visualvm.jfr.JFRSnapshotSupport; |
| 33 | +import org.netbeans.api.sendopts.CommandException; |
| 34 | +import org.netbeans.spi.sendopts.Env; |
| 35 | +import org.netbeans.spi.sendopts.Option; |
| 36 | +import org.netbeans.spi.sendopts.OptionProcessor; |
| 37 | +import org.openide.DialogDisplayer; |
| 38 | +import org.openide.NotifyDescriptor; |
| 39 | +import org.openide.util.NbBundle; |
| 40 | +import org.openide.util.lookup.ServiceProvider; |
| 41 | + |
| 42 | +/** |
| 43 | + * |
| 44 | + * @author Jiri Sedlacek |
| 45 | + */ |
| 46 | +@ServiceProvider(service=OptionProcessor.class) |
| 47 | +public class JFRArguments extends OptionProcessor { |
| 48 | + |
| 49 | + private static final String START_LONG_NAME = "start-jfr"; // NOI18N |
| 50 | + private static final Option START_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, START_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Start_ShortDescr"); // NOI18N |
| 51 | + private static final String DUMP_LONG_NAME = "dump-jfr"; // NOI18N |
| 52 | + private static final Option DUMP_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, DUMP_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Dump_ShortDescr"); // NOI18N |
| 53 | + private static final String STOP_LONG_NAME = "stop-jfr"; // NOI18N |
| 54 | + private static final Option STOP_JFR_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, STOP_LONG_NAME), "org.graalvm.visualvm.jfr.impl", "Argument_Stop_ShortDescr"); // NOI18N |
| 55 | + |
| 56 | + |
| 57 | + @Override |
| 58 | + protected Set<Option> getOptions() { |
| 59 | + Set<Option> options = new HashSet(); |
| 60 | + options.add(START_JFR_ARGUMENT); |
| 61 | + options.add(DUMP_JFR_ARGUMENT); |
| 62 | + options.add(STOP_JFR_ARGUMENT); |
| 63 | + return options; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void process(Env env, Map<Option, String[]> maps) throws CommandException { |
| 68 | + String[] startJFR = maps.get(START_JFR_ARGUMENT); |
| 69 | + if (startJFR != null) { |
| 70 | + final String[] _startJFR = startJFR.length == 1 ? startJFR[0].split("@") : null; // NOI18N |
| 71 | + if (_startJFR != null && _startJFR.length == 2) startJFR[0] = _startJFR[0]; |
| 72 | + new Finder(startJFR, START_LONG_NAME) { |
| 73 | + @Override |
| 74 | + public void found(Application application) { |
| 75 | + if (_startJFR != null && _startJFR.length == 2) { |
| 76 | + JFRSnapshotSupport.jfrStartRecording(application, decode(_startJFR[1])); |
| 77 | + } else { |
| 78 | + JFRSnapshotSupport.jfrStartRecording(application); |
| 79 | + } |
| 80 | + } |
| 81 | + }.find(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + final String[] dumpJFR = maps.get(DUMP_JFR_ARGUMENT); |
| 86 | + final String[] stopJFR = maps.get(STOP_JFR_ARGUMENT); |
| 87 | + if (dumpJFR != null) { |
| 88 | + new Finder(dumpJFR, DUMP_LONG_NAME) { |
| 89 | + @Override |
| 90 | + public void found(Application application) { |
| 91 | + boolean stop = stopJFR != null && stopJFR.length == 1 && stopJFR[0].equals(dumpJFR[0]); |
| 92 | + JFRSnapshotSupport.takeJfrDump(application, stop, true); |
| 93 | + } |
| 94 | + }.find(); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (stopJFR != null) { |
| 99 | + new Finder(stopJFR, STOP_LONG_NAME) { |
| 100 | + @Override |
| 101 | + public void found(Application application) { |
| 102 | + JFRSnapshotSupport.jfrStopRecording(application); |
| 103 | + } |
| 104 | + }.find(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + private static String decode(String value) { |
| 110 | + value = value.replace("%27", "\'"); // NOI18N |
| 111 | + value = value.replace("%22", "\""); // NOI18N |
| 112 | + value = value.replace("%20", " "); // NOI18N |
| 113 | + return value; |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + private static abstract class Finder extends ApplicationFinder { |
| 118 | + |
| 119 | + Finder(String[] pids, String longName) throws CommandException { |
| 120 | + super(resolvePid(pids, longName)); |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + public final void notFound(int pid, String id) { |
| 125 | + NotifyDescriptor desc = new NotifyDescriptor.Message(NbBundle.getMessage(JFRArguments.class, "MSG_NO_APP_PID", new Object[] { Integer.toString(pid) }), NotifyDescriptor.WARNING_MESSAGE); // NOI18N |
| 126 | + DialogDisplayer.getDefault().notifyLater(desc); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + private static int resolvePid(String[] pids, String longName) throws CommandException { |
| 131 | + if (pids.length == 1) { |
| 132 | + try { |
| 133 | + return Integer.valueOf(pids[0]); |
| 134 | + } catch (NumberFormatException e) { |
| 135 | + throw new CommandException(0, "Incorrect pid format for --" + longName + ": " + e.getMessage()); // NOI18N |
| 136 | + } |
| 137 | + } else { |
| 138 | + throw new CommandException(0, "--" + longName + " requires exactly one value"); // NOI18N |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments