1 /*
   2  * Copyright (c) 2013, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.oracle.java.testlibrary.gc;
  25 
  26 import java.util.List;
  27 import java.util.ArrayList;
  28 import com.oracle.java.testlibrary.*;
  29 
  30 /**
  31  * A helper class for creating a command that starts a {@code jmap} process.
  32  *
  33  * The JmapLauncher can in particular be combined with a
  34  * java.lang.ProcessBuilder to easily start a jmap process. For example, the
  35  * following code run {@code jmap -heap} against a process with GC logging
  36  * turned on for the {@code jmap} process:
  37  *
  38  * <pre>
  39  * {@code
  40  * JmapLauncher jmap = new JmapLauncher("-XX:+PrintGC");
  41  * jmap.setJmapArguments("-heap", pid);
  42  * ProcessBuilder pb = new ProcessBuilder(jmap.getCommand());
  43  * Process p = pb.start();
  44  * }
  45  * </pre>
  46  */
  47 public class JmapLauncher {
  48     private final String executable;
  49     private final List<String> vmArgs = new ArrayList<String>();
  50     private final List<String> jmapArgs = new ArrayList<String>();
  51 
  52     /**
  53      * Creates a new {@code jmap} launcher and also sets the Java virtual
  54      * machine (JVM) arguments of the command.
  55      *
  56      * The JVM arguments are the arguments that are prefixed {@code -J} and
  57      * are passed to the underlying JVM that runs the {@code jmap} program.
  58      *
  59      * Any platform specific arguments that are needed to run the {@code jmap}
  60      * command will automatically be added.
  61      *
  62      * @param args The arguments to the JVM. Will be automatically prefix with
  63      *             {@code J}.
  64      */
  65     public JmapLauncher(String ... args) {
  66         executable = JDKToolFinder.getJDKTool("jmap");
  67         setVMArgs(ProcessTools.getPlatformSpecificVMArgs());
  68         setVMArgs(args);
  69     }
  70 
  71     /**
  72      * Sets the arguments for {@code jmap}. Note that any JVM specific
  73      * arguments must be passed via the constructor.
  74      *
  75      * @param args The arguments to {@code jmap}.
  76      */
  77     public void setJmapArguments(String ... args) {
  78         for (String arg : args) {
  79             jmapArgs.add(arg);
  80         }
  81     }
  82 
  83     /**
  84      * Returns the command that can be used for starting the {@code jmap}
  85      * process.
  86      *
  87      * @return An array whose elements are the arguments of the command.
  88      */
  89     public String[] getCommand() {
  90         List<String> command = new ArrayList<String>();
  91         command.add(executable);
  92         command.addAll(vmArgs);
  93         command.addAll(jmapArgs);
  94         return command.toArray(new String[command.size()]);
  95     }
  96 
  97     private void setVMArgs(String[]  args) {
  98         for (String arg : args) {
  99             vmArgs.add("-J" + arg);
 100         }
 101     }
 102 }