1 /*
  2  * Copyright (c) 2011, 2025, 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 
 25 #include "classfile/javaClasses.inline.hpp"
 26 #include "classfile/symbolTable.hpp"
 27 #include "classfile/vmClasses.hpp"
 28 #include "jni.h"
 29 #include "memory/oopFactory.hpp"
 30 #include "memory/resourceArea.hpp"
 31 #include "oops/objArrayOop.inline.hpp"
 32 #include "prims/wbtestmethods/parserTests.hpp"
 33 #include "prims/whitebox.inline.hpp"
 34 #include "runtime/interfaceSupport.inline.hpp"
 35 #include "runtime/jniHandles.inline.hpp"
 36 #include "services/diagnosticArgument.hpp"
 37 #include "services/diagnosticFramework.hpp"
 38 
 39 //There's no way of beforeahnd knowing an upper size
 40 //Of the length of a string representation of
 41 //the value of an argument.
 42 #define VALUE_MAXLEN 256
 43 
 44 // DiagnosticFramework test utility methods
 45 
 46 /*
 47  * The DiagnosticArgumentType class contains an enum that says which type
 48  * this argument represents. (JLONG, BOOLEAN etc).
 49  * This method Returns a char* representation of that enum value.
 50  */
 51 static const char* lookup_diagnosticArgumentEnum(const char* field_name, oop object) {
 52   const char* enum_sig = "Ljdk/test/whitebox/parser/DiagnosticCommand$DiagnosticArgumentType;";
 53   TempNewSymbol enumSigSymbol = SymbolTable::new_symbol(enum_sig);
 54   int offset = WhiteBox::offset_for_field(field_name, object, enumSigSymbol);
 55   oop enumOop = object->obj_field(offset);
 56 
 57   const char* ret = WhiteBox::lookup_jstring("name", enumOop);
 58   return ret;
 59 }
 60 
 61 /*
 62  * Takes an oop to a DiagnosticArgumentType-instance and
 63  * reads the fields from it. Fills an native DCmdParser with
 64  * this info.
 65  */
 66 static void fill_in_parser(DCmdParser* parser, oop argument)
 67 {
 68   const char* name = WhiteBox::lookup_jstring("name", argument);
 69   const char* desc = WhiteBox::lookup_jstring("desc", argument);
 70   const char* default_value = WhiteBox::lookup_jstring("defaultValue", argument);
 71   bool mandatory = WhiteBox::lookup_bool("mandatory", argument);
 72   bool isarg = WhiteBox::lookup_bool("argument", argument);
 73   const char*  type = lookup_diagnosticArgumentEnum("type", argument);
 74 
 75    if (strcmp(type, "STRING") == 0) {
 76      DCmdArgument<char*>* argument = new DCmdArgument<char*>(
 77      name, desc,
 78      "STRING", mandatory, default_value);
 79      if (isarg) {
 80       parser->add_dcmd_argument(argument);
 81      } else {
 82       parser->add_dcmd_option(argument);
 83      }
 84    } else if (strcmp(type, "NANOTIME") == 0) {
 85      DCmdArgument<NanoTimeArgument>* argument = new DCmdArgument<NanoTimeArgument>(
 86      name, desc,
 87      "NANOTIME", mandatory, default_value);
 88      if (isarg) {
 89       parser->add_dcmd_argument(argument);
 90      } else {
 91       parser->add_dcmd_option(argument);
 92      }
 93    } else if (strcmp(type, "JLONG") == 0) {
 94      DCmdArgument<jlong>* argument = new DCmdArgument<jlong>(
 95      name, desc,
 96      "JLONG", mandatory, default_value);
 97      if (isarg) {
 98       parser->add_dcmd_argument(argument);
 99      } else {
100       parser->add_dcmd_option(argument);
101      }
102    } else if (strcmp(type, "BOOLEAN") == 0) {
103      DCmdArgument<bool>* argument = new DCmdArgument<bool>(
104      name, desc,
105      "BOOLEAN", mandatory, default_value);
106      if (isarg) {
107       parser->add_dcmd_argument(argument);
108      } else {
109       parser->add_dcmd_option(argument);
110      }
111    } else if (strcmp(type, "MEMORYSIZE") == 0) {
112      DCmdArgument<MemorySizeArgument>* argument = new DCmdArgument<MemorySizeArgument>(
113      name, desc,
114      "MEMORY SIZE", mandatory, default_value);
115      if (isarg) {
116       parser->add_dcmd_argument(argument);
117      } else {
118       parser->add_dcmd_option(argument);
119      }
120    } else if (strcmp(type, "STRINGARRAY") == 0) {
121      DCmdArgument<StringArrayArgument*>* argument = new DCmdArgument<StringArrayArgument*>(
122      name, desc,
123      "STRING SET", mandatory);
124      if (isarg) {
125       parser->add_dcmd_argument(argument);
126      } else {
127       parser->add_dcmd_option(argument);
128      }
129    } else if (strcmp(type, "FILE") == 0) {
130       DCmdArgument<char*>* argument =
131           new DCmdArgument<char*>(name, desc, "FILE", mandatory);
132       if (isarg) {
133         parser->add_dcmd_argument(argument);
134       } else {
135         parser->add_dcmd_option(argument);
136       }
137    }
138 }
139 
140 /*
141  * Will Fill in a java object array with alternating names of parsed command line options and
142  * the value that has been parsed for it:
143  * { name, value, name, value ... }
144  * This can then be checked from java.
145  */
146 WB_ENTRY(jobjectArray, WB_ParseCommandLine(JNIEnv* env, jobject o, jstring j_cmdline, jchar j_delim, jobjectArray arguments))
147   ResourceMark rm;
148   DCmdParser parser;
149 
150   const char* c_cmdline = java_lang_String::as_utf8_string(JNIHandles::resolve(j_cmdline));
151   const char c_delim = (char)(j_delim & 0xff);
152   objArrayOop argumentArray = objArrayOop(JNIHandles::resolve_non_null(arguments));
153   objArrayHandle argumentArray_ah(THREAD, argumentArray);
154 
155   int length = argumentArray_ah->length();
156 
157   for (int i = 0; i < length; i++) {
158     oop argument_oop = argumentArray_ah->obj_at(i);
159     fill_in_parser(&parser, argument_oop);
160   }
161 
162   CmdLine cmdline(c_cmdline, strlen(c_cmdline), true);
163   parser.parse(&cmdline,c_delim,CHECK_NULL);
164 
165   Klass* k = vmClasses::Object_klass();
166   objArrayOop returnvalue_array = oopFactory::new_objArray(k, parser.num_arguments() * 2, CHECK_NULL);
167   objArrayHandle returnvalue_array_ah(THREAD, returnvalue_array);
168 
169   GrowableArray<const char *>*parsedArgNames = parser.argument_name_array();
170   GenDCmdArgument* arglist = parser.arguments_list();
171 
172   for (int i = 0; i < parser.num_arguments(); i++) {
173     oop parsedName = java_lang_String::create_oop_from_str(parsedArgNames->at(i), CHECK_NULL);
174     returnvalue_array_ah->obj_at_put(i*2, parsedName);
175     GenDCmdArgument* arg = parser.lookup_dcmd_option(parsedArgNames->at(i), strlen(parsedArgNames->at(i)));
176     if (!arg) {
177       arg = arglist;
178       arglist = arglist->next();
179     }
180     char buf[VALUE_MAXLEN];
181     if (arg) {
182       arg->value_as_str(buf, sizeof(buf));
183     } else {
184       os::snprintf_checked(buf, sizeof(buf), "<null>");
185     }
186     oop parsedValue = java_lang_String::create_oop_from_str(buf, CHECK_NULL);
187     returnvalue_array_ah->obj_at_put(i*2+1, parsedValue);
188   }
189 
190   return (jobjectArray) JNIHandles::make_local(returnvalue_array_ah());
191 
192 WB_END