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