Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdb/JdbArgumentHandler.java
41161 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share.jdb;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.ArgumentHandler;2829import java.io.*;30import java.util.*;3132/**33* Parser for <code>jdb</code> test's specific command-line arguments.34* This class also parses JDI specific command-line arguments.35* <p>36* <code>JdbArgumentHandler</code> handles <code>jdb</code> test's specific37* arguments related to launching of <code>jdb</code> to debugged VM38* in addition to general arguments recognized by <code>ArgumentHandler</code>,39* <code>DebugeeArgumentHandler</code> and <code>ArgumentParser</code>.40* <p>41* Following is the list of specific options recognized by <code>AgrumentHandler</code>:42* <ul>43* <li> <code>-jdb=JAVA_SDK_HOME_PATH/bin/$JDB</code> - <br>44* full path to <code>jdb</code> binaries. There is no default value. <br>45* <li> <code>-workdir=CURRENT_TEST_DIRECTORY_PATH</code> -46* full path to current test directory. There is no default value.47* <li> <code>-jdb.option=JDB_COMMAND_LINE_OPTIONS</code> -48* jdb options (see <code>jdb -help</code>) except these ones:49* <ul>50* <li><code>-attach [address] </code></li>51* <li><code>-listen [address] </code></li>52* <li><code>-connect [connector-name]:[name1]=[value1],... </code></li>53* </ul>54* </ul>55* <p>56* See also list of arguments recognized by the base <code>ArgumentHandler</code>,57* <code>DebugeeArgumentHandler</code> and <code>ArgumentParser</code> classes.58* <p>59* See also description of <code>ArgumentParser</code> how to work with60* command line arguments and options.61*62* @see nsk.share.ArgumentParser63* @see nsk.share.jpda.DebugeeArgumentHandler64* @see nsk.share.jdi.ArgumentHandler65*/66public class JdbArgumentHandler extends nsk.share.jdi.ArgumentHandler {6768/**69* Keep a copy of raw command-line arguments and parse them;70* but throw an exception on parsing error.71*72* @param args Array of the raw command-line arguments.73*74* @throws NullPointerException If <code>args==null</code>.75* @throws IllegalArgumentException If Binder or Log options76* are set incorrectly.77*78* @see #setRawArguments(String[])79*/80public JdbArgumentHandler(String args[]) {81super(args);82}838485/**86* Checks if an option is admissible and has proper value.87* This method is invoked by <code>parseArguments()</code>88*89* @param option option name90* @param value string representation of value (could be an empty string)91* null if this option has no value92* @return <i>true</i> if option is admissible and has proper value93* <i>false</i> if option is not admissible94*95* @throws <i>BadOption</i> if option has illegal value96*97* @see #parseArguments()98*/99protected boolean checkOption(String option, String value) {100101if (option.equals("jdb")) {102if (value == null) {103throw new BadOption(option + ": value must be not null");104}105return true;106}107108if (option.equals("workdir")) {109if (value == null) {110throw new BadOption(option + ": value must be not null");111}112return true;113}114115if (option.equals("java.options")) {116return true;117}118119if (option.equals("jdb.option")) {120if (value != null) {121if (value.indexOf("-attach") > 0) {122throw new BadOption("jdb option -attach is not admissible: " + value);123}124if (value.indexOf("-listen") > 0) {125throw new BadOption("jdb option -listen is not admissible: " + value);126}127if (value.indexOf("-connect") > 0) {128throw new BadOption("jdb option -connect is not admissible: " + value);129}130if (value.indexOf("-help") > 0) {131throw new BadOption("jdb option -help is not admissible: " + value);132}133if (value.indexOf("-listenany") > 0) {134throw new BadOption("jdb option -listenany is not admissible: " + value);135}136if (value.indexOf("-launch") > 0) {137throw new BadOption("jdb option -launch is not admissible: " + value);138}139if (value.indexOf("-tclient") > 0) {140throw new BadOption("jdb option -tclient is not admissible: " + value);141}142if (value.indexOf("-tserver") > 0) {143throw new BadOption("jdb option -tserver is not admissible: " + value);144}145}146return true;147}148149return super.checkOption(option, value);150}151152/**153* Checks options against inconcistence.154* This method is invoked by <code>parseArguments()</code>155*156* @see #parseArguments()157*/158protected void checkOptions() {159160super.checkOptions();161}162163/**164* Returns the path to current test directory.165*/166167public String getWorkDir() {168return options.getProperty("workdir", "");169}170171/**172* Return sfull path to jdb executable.173*174*/175public String getJdbExecPath() {176return options.getProperty("jdb");177}178179/**180* Returns command line options <code>jdb</code> was launched with.181*/182public String getJdbOptions() {183String value = options.getProperty("jdb.option", "").trim();184if (value.length() > 1 && value.startsWith("\"") && value.endsWith("\"")) {185value = value.substring(1, value.length() - 1).trim();186}187return value;188}189190/**191* Adds "<code>-J</code>" prefix to each Java option, so that192* <code>jdb</code> could apply them to the target Java VM.193*/194public static List<String> enwrapJavaOptions(String javaOptions) {195List<String> result = new ArrayList<String>();196for (String option : javaOptions.split("\\s+"))197if (option.length() > 0)198result.add("-J" + option);199return result;200}201202/**203* Returns adjusted additional options for debuggee VM with launching connector.204*/205public String getDebuggeeOptions() {206StringBuilder sb = new StringBuilder();207String value = options.getProperty("debugee.vmkeys", "").trim();208if (value.length() > 1 && value.startsWith("\"") && value.endsWith("\"")) {209value = value.substring(1, value.length() - 1).trim();210}211for (String option : value.split("\\s+")) {212if (option.length() > 0) {213sb.append(checkAndQuote(option, ","));214sb.append(" ");215}216}217return sb.toString();218}219220/**221* Returns options for debugger VM.222*/223public String getJavaOptions() {224String value = options.getProperty("java.options", "").trim();225if (value.length() > 1 && value.startsWith("\"") && value.endsWith("\"")) {226value = value.substring(1, value.length() - 1).trim();227}228return value;229}230231/**232* Remove "<code>-server</code>" or "<code>-client</code>" from options string,233* if anything of them are presented.234*/235public static String removeVMFlavorOption(String javaOptions) {236StringBuffer result = new StringBuffer();237StringTokenizer tokenizer = new StringTokenizer(javaOptions);238while (tokenizer.hasMoreTokens()) {239String option = tokenizer.nextToken();240if (!option.equals("-server") && !option.equals("-client")) {241result.append( (result.length() > 0 ? " " : "") + option);242}243};244return result.toString();245}246247/**248* @return "<code>-tserver</code>" if "<code>-server</code>" is presented in options string.249* @return "<code>-tclient</code>" if "<code>-client</code>" is presented in options string.250* @return empty string otherwise.251*/252public static String stripVMFlavorOption(String javaOptions) {253String result = "";254StringTokenizer tokenizer = new StringTokenizer(javaOptions);255while (tokenizer.hasMoreTokens()) {256String option = tokenizer.nextToken();257if (option.equals("-server")) {258result = "-tserver";259break;260} else if (option.equals("-client")) {261result = "-tclient";262break;263}264};265return result;266}267268// check if target substring exists and quote value if needed269// ex for subString == "," and value == disk=true,dumponexit=true270// return 'disk=true,dumponexit=true'271private static String checkAndQuote(String value, String subString) {272if (NO_SUBSTR_MATCH == value.indexOf(subString)) {273// return as-is274return value;275}276// already single quoted 'value' ?277if (isEnclosed(value, "'")) {278return value;279}280// already double quoted "value" ?281if (isEnclosed(value, "\"")) {282// change to 'value'283return value.replaceAll("\"", "'");284}285// else single quote the value286return "'" + value + "'";287}288289private static boolean isEnclosed(String value,290String enclosingChar) {291int firstEnclosePos = value.indexOf(enclosingChar);292if (0 == firstEnclosePos) {293int lastEnclosePos = value.lastIndexOf(enclosingChar);294if (lastEnclosePos > firstEnclosePos && lastEnclosePos == value.length() - 1) {295//already quoted296return true;297}298//only a single quote? subString outside quotes? Wrongly quoted, needs fix299throw new BadOption(value + " not correctly quoted");300}301return false;302}303304private static final int NO_SUBSTR_MATCH = -1;305}306307308