1 /*
  2  * Copyright (c) 2012, 2026, 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/vmSymbols.hpp"
 27 #include "jfr/dcmd/jfrDcmds.hpp"
 28 #include "jfr/jfr.hpp"
 29 #include "jfr/jni/jfrJavaSupport.hpp"
 30 #include "jfr/periodic/jfrRedactedEvents.hpp"
 31 #include "jfr/recorder/jfrRecorder.hpp"
 32 #include "jfr/recorder/service/jfrOptionSet.hpp"
 33 #include "jfr/support/jfrThreadLocal.hpp"
 34 #include "logging/log.hpp"
 35 #include "logging/logConfiguration.hpp"
 36 #include "logging/logMessage.hpp"
 37 #include "memory/arena.hpp"
 38 #include "memory/resourceArea.hpp"
 39 #include "oops/objArrayOop.inline.hpp"
 40 #include "oops/oop.inline.hpp"
 41 #include "oops/symbol.hpp"
 42 #include "runtime/handles.inline.hpp"
 43 #include "runtime/jniHandles.hpp"
 44 #include "services/diagnosticArgument.hpp"
 45 #include "services/diagnosticFramework.hpp"
 46 #include "utilities/globalDefinitions.hpp"
 47 
 48 
 49 bool register_jfr_dcmds() {
 50   uint32_t full_export = DCmd_Source_Internal | DCmd_Source_AttachAPI | DCmd_Source_MBean;
 51   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrCheckFlightRecordingDCmd>(full_export));
 52   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrDumpFlightRecordingDCmd>(full_export));
 53   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrStartFlightRecordingDCmd>(full_export));
 54   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrStopFlightRecordingDCmd>(full_export));
 55   // JFR.query Uncomment when developing new queries for the JFR.view command
 56   // DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrQueryFlightRecordingDCmd>(full_export, /*hidden=*/true));
 57   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrViewFlightRecordingDCmd>(full_export));
 58   DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<JfrConfigureFlightRecorderDCmd>(full_export));
 59   return true;
 60 }
 61 
 62 static bool is_disabled(outputStream* output) {
 63   if (Jfr::is_disabled()) {
 64     if (output != nullptr) {
 65       output->print_cr("Flight Recorder is disabled.\n");
 66     }
 67     return true;
 68   }
 69   return false;
 70 }
 71 
 72 static bool invalid_state(outputStream* out, TRAPS) {
 73   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
 74   if (is_disabled(out)) {
 75     return true;
 76   }
 77   if (!JfrJavaSupport::is_jdk_jfr_module_available()) {
 78     JfrJavaSupport::load_jdk_jfr_module(THREAD);
 79     if (HAS_PENDING_EXCEPTION) {
 80       // Log exception here, but let is_jdk_jfr_module_available(out, THREAD)
 81       // handle output to the user.
 82       ResourceMark rm(THREAD);
 83       oop throwable = PENDING_EXCEPTION;
 84       assert(throwable != nullptr, "invariant");
 85       oop msg = java_lang_Throwable::message(throwable);
 86       if (msg != nullptr) {
 87         char* text = java_lang_String::as_utf8_string(msg);
 88         if (text != nullptr) {
 89           log_debug(jfr, startup)("Flight Recorder can not be enabled. %s", text);
 90         }
 91       }
 92       CLEAR_PENDING_EXCEPTION;
 93     }
 94   }
 95   return !JfrJavaSupport::is_jdk_jfr_module_available(out, THREAD);
 96 }
 97 
 98 static void handle_pending_exception(outputStream* output, bool startup, oop throwable) {
 99   assert(throwable != nullptr, "invariant");
100 
101   oop msg = java_lang_Throwable::message(throwable);
102   if (msg == nullptr) {
103     return;
104   }
105   char* text = java_lang_String::as_utf8_string(msg);
106   if (text != nullptr) {
107     if (startup) {
108       log_error(jfr,startup)("%s", text);
109     } else {
110       output->print_cr("%s", text);
111     }
112   }
113 }
114 
115 static void print_message(outputStream* output, oop content, TRAPS) {
116   objArrayOop lines = objArrayOop(content);
117   assert(lines != nullptr, "invariant");
118   assert(lines->is_array(), "must be array");
119   const int length = lines->length();
120   for (int i = 0; i < length; ++i) {
121     const char* text = JfrJavaSupport::c_str(lines->obj_at(i), THREAD);
122     if (text == nullptr) {
123       // An oome has been thrown and is pending.
124       break;
125     }
126     output->print_cr("%s", text);
127   }
128 }
129 
130 static void log(oop content, TRAPS) {
131   LogMessage(jfr,startup) msg;
132   objArrayOop lines = objArrayOop(content);
133   assert(lines != nullptr, "invariant");
134   assert(lines->is_array(), "must be array");
135   const int length = lines->length();
136   for (int i = 0; i < length; ++i) {
137     const char* text = JfrJavaSupport::c_str(lines->obj_at(i), THREAD);
138     if (text == nullptr) {
139       // An oome has been thrown and is pending.
140       break;
141     }
142     msg.info("%s", text);
143   }
144 }
145 
146 static void handle_dcmd_result(outputStream* output,
147                                const oop result,
148                                const DCmdSource source,
149                                TRAPS) {
150   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
151   assert(output != nullptr, "invariant");
152   ResourceMark rm(THREAD);
153   const bool startup = DCmd_Source_Internal == source;
154   if (HAS_PENDING_EXCEPTION) {
155     handle_pending_exception(output, startup, PENDING_EXCEPTION);
156     // Don't clear exception on startup, JVM should fail initialization.
157     if (!startup) {
158       CLEAR_PENDING_EXCEPTION;
159     }
160     return;
161   }
162 
163   assert(!HAS_PENDING_EXCEPTION, "invariant");
164 
165   if (startup) {
166     if (log_is_enabled(Warning, jfr, startup))  {
167       // if warning is set, assume user hasn't configured log level
168       // Log to Info and reset to Warning. This way user can disable
169       // default output by setting -Xlog:jfr+startup=error/off
170       LogConfiguration::configure_stdout(LogLevel::Info, true, LOG_TAGS(jfr, startup));
171       log(result, THREAD);
172       LogConfiguration::configure_stdout(LogLevel::Warning, true, LOG_TAGS(jfr, startup));
173     } else {
174       log(result, THREAD);
175     }
176   } else {
177       // Print output for jcmd or MXBean
178       print_message(output, result, THREAD);
179   }
180 }
181 
182 static oop construct_dcmd_instance(JfrJavaArguments* args, TRAPS) {
183   assert(args != nullptr, "invariant");
184   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
185   assert(args->klass() != nullptr, "invariant");
186   args->set_name("<init>");
187   args->set_signature("()V");
188   JfrJavaSupport::new_object(args, CHECK_NULL);
189   return args->result()->get_oop();
190 }
191 
192 JfrDCmd::JfrDCmd(outputStream* output, bool heap, int num_arguments) : DCmd(output, heap), _args(nullptr), _num_arguments(num_arguments), _delimiter('\0') {}
193 
194 void JfrDCmd::invoke(JfrJavaArguments& method, TRAPS) const {
195   JavaValue constructor_result(T_OBJECT);
196   JfrJavaArguments constructor_args(&constructor_result);
197   constructor_args.set_klass(javaClass(), CHECK);
198 
199   HandleMark hm(THREAD);
200   JNIHandleMark jni_handle_management(THREAD);
201 
202   const oop dcmd = construct_dcmd_instance(&constructor_args, CHECK);
203 
204   Handle h_dcmd_instance(THREAD, dcmd);
205   assert(h_dcmd_instance.not_null(), "invariant");
206 
207   method.set_receiver(h_dcmd_instance);
208   JfrJavaSupport::call_virtual(&method, THREAD);
209 }
210 
211 void JfrDCmd::parse(CmdLine* line, char delim, TRAPS) {
212   _args = line->args_addr();
213   _delimiter = delim;
214   // Error checking done in execute.
215   // Will not matter from DCmdFactory perspective
216   // where parse and execute are called consecutively.
217 }
218 
219 void JfrDCmd::execute(DCmdSource source, TRAPS) {
220   if (invalid_state(output(), THREAD)) {
221     return;
222   }
223   if (source == DCmd_Source_Internal && _args != nullptr && strcmp(_args, "help") == 0) {
224      print_java_help("getStartupHelp");
225      vm_exit(0);
226   }
227 
228   static const char signature[] = "(Ljava/lang/String;Ljava/lang/String;C)[Ljava/lang/String;";
229   JavaValue result(T_OBJECT);
230   JfrJavaArguments execute(&result, javaClass(), "execute", signature, CHECK);
231   jstring argument = JfrJavaSupport::new_string(_args, CHECK);
232   jstring s = nullptr;
233   if (source == DCmd_Source_Internal) {
234     s = JfrJavaSupport::new_string("internal", CHECK);
235   }
236   if (source == DCmd_Source_MBean) {
237     s = JfrJavaSupport::new_string("mbean", CHECK);
238   }
239   if (source == DCmd_Source_AttachAPI) {
240     s = JfrJavaSupport::new_string("attach", CHECK);
241   }
242   execute.push_jobject(s);
243   execute.push_jobject(argument);
244   execute.push_int(_delimiter);
245   invoke(execute, THREAD);
246   handle_dcmd_result(output(), result.get_oop(), source, THREAD);
247 }
248 
249 void JfrDCmd::print_java_help(const char* get_help_method) const {
250   static const char signature[] = "()[Ljava/lang/String;";
251   JavaThread* thread = JavaThread::current();
252   JavaValue result(T_OBJECT);
253   JfrJavaArguments java_method(&result, javaClass(), get_help_method, signature, thread);
254   invoke(java_method, thread);
255   handle_dcmd_result(output(), result.get_oop(), DCmd_Source_MBean, thread);
256 }
257 
258 void JfrDCmd::print_help(const char* name) const {
259   print_java_help("getHelp");
260 }
261 
262 static void initialize_dummy_descriptors(GrowableArray<DCmdArgumentInfo*>* array) {
263   assert(array != nullptr, "invariant");
264   DCmdArgumentInfo * const dummy = new DCmdArgumentInfo(nullptr,
265                                                         nullptr,
266                                                         nullptr,
267                                                         nullptr,
268                                                         false,
269                                                         true, // a DcmdFramework "option"
270                                                         false);
271   for (int i = 0; i < array->capacity(); ++i) {
272     array->append(dummy);
273   }
274 }
275 
276 // Since the DcmdFramework does not support dynamically allocated strings,
277 // we keep them in a thread local arena. The arena is reset between invocations.
278 static THREAD_LOCAL Arena* dcmd_arena = nullptr;
279 
280 static void prepare_dcmd_string_arena(JavaThread* jt) {
281   dcmd_arena = JfrThreadLocal::dcmd_arena(jt);
282   assert(dcmd_arena != nullptr, "invariant");
283   dcmd_arena->destruct_contents(); // will grow on next allocation
284 }
285 
286 static char* dcmd_arena_allocate(size_t size) {
287   assert(dcmd_arena != nullptr, "invariant");
288   return (char*)dcmd_arena->Amalloc(size);
289 }
290 
291 static const char* get_as_dcmd_arena_string(oop string) {
292   char* str = nullptr;
293   const typeArrayOop value = java_lang_String::value(string);
294   if (value != nullptr) {
295     const size_t length = java_lang_String::utf8_length(string, value) + 1;
296     str = dcmd_arena_allocate(length);
297     assert(str != nullptr, "invariant");
298     java_lang_String::as_utf8_string(string, value, str, length);
299   }
300   return str;
301 }
302 
303 static const char* read_string_field(oop argument, const char* field_name, TRAPS) {
304   JavaValue result(T_OBJECT);
305   JfrJavaArguments args(&result);
306   args.set_klass(argument->klass());
307   args.set_name(field_name);
308   args.set_signature("Ljava/lang/String;");
309   args.set_receiver(argument);
310   JfrJavaSupport::get_field(&args, THREAD);
311   const oop string_oop = result.get_oop();
312   return string_oop != nullptr ? get_as_dcmd_arena_string(string_oop) : nullptr;
313 }
314 
315 static bool read_boolean_field(oop argument, const char* field_name, TRAPS) {
316   JavaValue result(T_BOOLEAN);
317   JfrJavaArguments args(&result);
318   args.set_klass(argument->klass());
319   args.set_name(field_name);
320   args.set_signature("Z");
321   args.set_receiver(argument);
322   JfrJavaSupport::get_field(&args, THREAD);
323   return (result.get_jint() & 1) == 1;
324 }
325 
326 static DCmdArgumentInfo* create_info(oop argument, TRAPS) {
327   return new DCmdArgumentInfo(
328     read_string_field(argument, "name", THREAD),
329     read_string_field(argument, "description", THREAD),
330     read_string_field(argument, "type", THREAD),
331     read_string_field(argument, "defaultValue", THREAD),
332     read_boolean_field(argument, "mandatory", THREAD),
333     read_boolean_field(argument, "option", THREAD),
334     read_boolean_field(argument, "allowMultiple", THREAD));
335 }
336 
337 GrowableArray<DCmdArgumentInfo*>* JfrDCmd::argument_info_array() const {
338   static const char signature[] = "()[Ljdk/jfr/internal/dcmd/Argument;";
339   JavaThread* thread = JavaThread::current();
340   GrowableArray<DCmdArgumentInfo*>* const array = new GrowableArray<DCmdArgumentInfo*>(_num_arguments);
341   JavaValue result(T_OBJECT);
342   JfrJavaArguments getArgumentInfos(&result, javaClass(), "getArgumentInfos", signature, thread);
343   invoke(getArgumentInfos, thread);
344   if (thread->has_pending_exception()) {
345     // Most likely an OOME, but the DCmdFramework is not the best place to handle it.
346     // We handle it locally by clearing the exception and returning an array with dummy descriptors.
347     // This lets the MBean server initialization routine complete successfully,
348     // but this particular command will have no argument descriptors exposed.
349     // Hence we postpone, or delegate, handling of OOME's to code that is better suited.
350     log_debug(jfr, system)("Exception in DCmd getArgumentInfos");
351     thread->clear_pending_exception();
352     initialize_dummy_descriptors(array);
353     assert(array->length() == _num_arguments, "invariant");
354     return array;
355   }
356   objArrayOop arguments = objArrayOop(result.get_oop());
357   assert(arguments != nullptr, "invariant");
358   assert(arguments->is_array(), "must be array");
359   const int num_arguments = arguments->length();
360   assert(num_arguments == _num_arguments, "invariant");
361   prepare_dcmd_string_arena(thread);
362   for (int i = 0; i < num_arguments; ++i) {
363     DCmdArgumentInfo* const dai = create_info(arguments->obj_at(i), thread);
364     assert(dai != nullptr, "invariant");
365     array->append(dai);
366   }
367   return array;
368 }
369 
370 GrowableArray<const char*>* JfrDCmd::argument_name_array() const {
371   GrowableArray<DCmdArgumentInfo*>* argument_infos = argument_info_array();
372   GrowableArray<const char*>* array = new GrowableArray<const char*>(argument_infos->length());
373   for (int i = 0; i < argument_infos->length(); i++) {
374     array->append(argument_infos->at(i)->name());
375   }
376   return array;
377 }
378 
379 JfrConfigureFlightRecorderDCmd::JfrConfigureFlightRecorderDCmd(outputStream* output,
380                                                                bool heap) : DCmdWithParser(output, heap),
381   _repository_path("repositorypath", "Path to repository,.e.g \\\"My Repository\\\"", "STRING", false, nullptr),
382   _dump_path("dumppath", "Path to dump, e.g. \\\"My Dump path\\\"", "STRING", false, nullptr),
383   _stack_depth("stackdepth", "Stack depth", "JULONG", false, "64"),
384   _global_buffer_count("globalbuffercount", "Number of global buffers,", "JULONG", false, "20"),
385   _global_buffer_size("globalbuffersize", "Size of a global buffers,", "MEMORY SIZE", false, "512k"),
386   _thread_buffer_size("thread_buffer_size", "Size of a thread buffer", "MEMORY SIZE", false, "8k"),
387   _memory_size("memorysize", "Overall memory size, ", "MEMORY SIZE", false, "10m"),
388   _max_chunk_size("maxchunksize", "Size of an individual disk chunk", "MEMORY SIZE", false, "12m"),
389   _sample_threads("samplethreads", "Activate thread sampling", "BOOLEAN", false, "true"),
390   _preserve_repository("preserve-repository", "Preserve the disk repository after JVM exit", "BOOLEAN", false, "false"),
391   _verbose(true) {
392   _dcmdparser.add_dcmd_option(&_repository_path);
393   _dcmdparser.add_dcmd_option(&_dump_path);
394   _dcmdparser.add_dcmd_option(&_stack_depth);
395   _dcmdparser.add_dcmd_option(&_global_buffer_count);
396   _dcmdparser.add_dcmd_option(&_global_buffer_size);
397   _dcmdparser.add_dcmd_option(&_thread_buffer_size);
398   _dcmdparser.add_dcmd_option(&_memory_size);
399   _dcmdparser.add_dcmd_option(&_max_chunk_size);
400   _dcmdparser.add_dcmd_option(&_sample_threads);
401   _dcmdparser.add_dcmd_option(&_preserve_repository);
402 };
403 
404 void JfrConfigureFlightRecorderDCmd::print_help(const char* name) const {
405   print_help(output(), false);
406 }
407 
408 static void print_filters(outputStream* out, JfrRedactedEvents::StringArray* filters) {
409   for (int i = 0; i < filters->length(); i++) {
410     out->print_cr("                        %s", filters->at(i)->text());
411   }
412 }
413 
414 void JfrConfigureFlightRecorderDCmd::print_help(outputStream* out, bool startup) {
415               // 0123456789001234567890012345678900123456789001234567890012345678900123456789001234567890
416   if (startup) {
417     out->print_cr("Syntax : -XX:FlightRecorderOptions:[options]");
418     out->print_cr("");
419   }
420   out->print_cr("Options:");
421   out->print_cr("");
422   out->print_cr(              "  %-19s (Optional) Number of global buffers. This option is a legacy",
423                                        startup ? "numglobalbuffers": "globalbuffercount");
424   out->print_cr("                      option: change the memorysize parameter to alter the number of");
425   out->print_cr("                      global buffers. This value cannot be changed once JFR has been");
426   out->print_cr("                      initialized. (STRING, default determined by the value for");
427   out->print_cr("                      memorysize)");
428   out->print_cr("");
429   out->print_cr("  globalbuffersize    (Optional) Size of the global buffers, in bytes. This option is a");
430   out->print_cr("                      legacy option: change the memorysize parameter to alter the size");
431   out->print_cr("                      of the global buffers. This value cannot be changed once JFR has");
432   out->print_cr("                      been initialized. (STRING, default determined by the value for");
433   out->print_cr("                      memorysize)");
434   out->print_cr("");
435   out->print_cr("  maxchunksize        (Optional) Maximum size of an individual data chunk in bytes if");
436   out->print_cr("                      one of the following suffixes is not used: 'm' or 'M' for");
437   out->print_cr("                      megabytes OR 'g' or 'G' for gigabytes. This value cannot be");
438   out->print_cr("                      changed once JFR has been initialized. (STRING, 12M)");
439   out->print_cr("");
440   out->print_cr("  memorysize          (Optional) Overall memory size, in bytes if one of the following");
441   out->print_cr("                      suffixes is not used: 'm' or 'M' for megabytes OR 'g' or 'G' for");
442   out->print_cr("                      gigabytes. This value cannot be changed once JFR has been");
443   out->print_cr("                      initialized. (STRING, 10M)");
444   out->print_cr("");
445   out->print_cr(              "  %-19s (Optional) Path to the location where recordings are stored until",
446                                        startup ? "repository" : "repositorypath");
447   out->print_cr("                      they are written to a permanent file. (STRING, The default");
448   out->print_cr("                      location is the temporary directory for the operating system. On");
449   out->print_cr("                      Linux operating systems, the temporary directory is /tmp. On");
450   out->print_cr("                      Windows, the temporary directory is specified by the TMP");
451   out->print_cr("                      environment variable)");
452   out->print_cr("");
453   out->print_cr("  dumppath            (Optional) Path to the location where a recording file is written");
454   out->print_cr("                      in case the VM runs into a critical error, such as a system");
455   out->print_cr("                      crash. (STRING, The default location is the current directory)");
456   out->print_cr("");
457   out->print_cr("  stackdepth          (Optional) Stack depth for stack traces. Setting this value");
458   out->print_cr("                      greater than the default of 64 may cause a performance");
459   out->print_cr("                      degradation. This value cannot be changed once JFR has been");
460   out->print_cr("                      initialized. (LONG, 64)");
461   out->print_cr("");
462   out->print_cr(              "  %-19s (Optional) Local buffer size for each thread in bytes if one of",
463                                        startup ? "threadbuffersize" : "thread_buffer_size");
464   out->print_cr("                      the following suffixes is not used: 'k' or 'K' for kilobytes or");
465   out->print_cr("                      'm' or 'M' for megabytes. Overriding this parameter could reduce");
466   out->print_cr("                      performance and is not recommended. This value cannot be changed");
467   out->print_cr("                      once JFR has been initialized. (STRING, 8k)");
468   out->print_cr("");
469   out->print_cr("  preserve-repository (Optional) Preserve files stored in the disk repository after the");
470   out->print_cr("                      Java Virtual Machine has exited. (BOOLEAN, false)");
471   out->print_cr("");
472   if (startup) {
473     out->print_cr("  old-object-queue-size (Optional) Maximum number of old objects to track. By default,");
474     out->print_cr("                        the number of objects is set to 256. (LONG, 256)");
475     out->print_cr("");
476     out->print_cr("  redact-argument     (Optional) Replace command-line arguments that match a");
477     out->print_cr("                      semicolon-separated list of glob patterns, for example,");
478     out->print_cr("                      *secret*;password*. Matching is case-insensitive, and the");
479     out->print_cr("                      supported wildcards are '*' and '?'. To redact multiple arguments,");
480     out->print_cr("                      use a literal space (' ') as a separator. For example, to match");
481     out->print_cr("                      the two arguments --auth username:token, use the filter");
482     out->print_cr("                      --auth *:*. Filters containing spaces must be quoted as a single");
483     out->print_cr("                      command-line argument, for example,");
484     out->print_cr("                      -XX:FlightRecorderOptions:'redact-argument=--auth *:*'.");
485     out->print_cr("                      Arguments containing spaces might not be matched as expected.");
486     out->print_cr("                      The option redact-argument is best-effort and applies only to");
487     out->print_cr("                      command-line arguments in the jdk.JVMInformation event and to");
488     out->print_cr("                      the java.command system property in the jdk.InitialSystemProperty");
489     out->print_cr("                      event. Other events, such as jdk.ProcessStart (child processes),");
490     out->print_cr("                      are not redacted.");
491     out->print_cr("");
492     out->print_cr("                      If the redact-argument option is not specified, the following");
493     out->print_cr("                      filters are used by default:");
494     out->print_cr("");
495     print_filters(out, JfrRedactedEvents::argument_filters());
496     out->print_cr("");
497     out->print_cr("                      To load patterns from a file (one per line), use @<filename>.");
498     out->print_cr("                      To add to the default patterns instead of replacing them, prefix");
499     out->print_cr("                      the whole list with '+', for example, +*foo*;@redact.txt.");
500     out->print_cr("                      Use 'none' (lowercase) to disable all redaction filters for");
501     out->print_cr("                      command-line arguments. Redacted arguments will be replaced");
502     out->print_cr("                      with '[REDACTED]'. (STRING, default filters)");
503     out->print_cr("");
504     out->print_cr("  redact-key          (Optional) Replace the value of environment variables and system");
505     out->print_cr("                      properties whose key matches a semicolon-separated list of glob");
506     out->print_cr("                      patterns, for example, *password*;*token*. Matching is");
507     out->print_cr("                      case-insensitive, and the supported wildcards are '*' and '?'.");
508     out->print_cr("                      The option redact-key is best-effort and applies only to the");
509     out->print_cr("                      jdk.InitialSystemProperty, jdk.InitialEnvironmentVariable and");
510     out->print_cr("                      jdk.JVMInformation (-Dkey...) events. Other events, such as");
511     out->print_cr("                      jdk.InitialSecurityProperty, are not redacted.");
512     out->print_cr("");
513     out->print_cr("                      If the redact-key option is not specified, the");
514     out->print_cr("                      following filters are used by default:");
515     out->print_cr("");
516     print_filters(out, JfrRedactedEvents::key_filters());
517     out->print_cr("");
518     out->print_cr("                      To load patterns from a file (one per line), use @<filename>.");
519     out->print_cr("                      To add to the default patterns instead of replacing them, prefix");
520     out->print_cr("                      the whole list with '+', for example, +*cred*;@keys.txt.");
521     out->print_cr("                      Use 'none' (lowercase) to disable all redaction filters for key");
522     out->print_cr("                      matching. Redacted values will be replaced with '[REDACTED]'.");
523     out->print_cr("                      (STRING, default filters)");
524     out->print_cr("");
525     out->print_cr("Options must be specified using the <key>=<value> syntax. Multiple options are separated");
526     out->print_cr("with a comma.");
527     out->print_cr("");
528     out->print_cr("Example usage:");
529     out->print_cr("");
530     out->print_cr(" -XX:FlightRecorderOptions:repository=/temporary,stackdepth=256");
531     out->print_cr(" -XX:FlightRecorderOptions:'redact-key=+*confidential*;*private*,redact-argument=+https://*:*@*'");
532     out->print_cr(" -XX:FlightRecorderOptions:'redact-argument=+-*private *'");
533   } else {
534     out->print_cr("Options must be specified using the <key> or <key>=<value> syntax.");
535     out->print_cr("");
536     out->print_cr("Example usage:");
537     out->print_cr("");
538     out->print_cr(" $ jcmd <pid> JFR.configure");
539     out->print_cr(" $ jcmd <pid> JFR.configure repositorypath=/temporary");
540     out->print_cr(" $ jcmd <pid> JFR.configure stackdepth=256");
541     out->print_cr(" $ jcmd <pid> JFR.configure memorysize=100M");
542   }
543   out->print_cr("");
544 }
545 
546 void JfrConfigureFlightRecorderDCmd::execute(DCmdSource source, TRAPS) {
547   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
548 
549   if (invalid_state(output(), THREAD)) {
550     return;
551   }
552 
553   HandleMark hm(THREAD);
554   JNIHandleMark jni_handle_management(THREAD);
555 
556   JavaValue result(T_OBJECT);
557   JfrJavaArguments constructor_args(&result);
558   constructor_args.set_klass("jdk/jfr/internal/dcmd/DCmdConfigure", CHECK);
559   const oop dcmd = construct_dcmd_instance(&constructor_args, CHECK);
560   Handle h_dcmd_instance(THREAD, dcmd);
561   assert(h_dcmd_instance.not_null(), "invariant");
562 
563   jstring repository_path = nullptr;
564   if (_repository_path.is_set() && _repository_path.value() != nullptr) {
565     repository_path = JfrJavaSupport::new_string(_repository_path.value(), CHECK);
566   }
567 
568   jstring dump_path = nullptr;
569   if (_dump_path.is_set() && _dump_path.value() != nullptr) {
570     dump_path = JfrJavaSupport::new_string(_dump_path.value(), CHECK);
571   }
572 
573   jobject stack_depth = nullptr;
574   jobject global_buffer_count = nullptr;
575   jobject global_buffer_size = nullptr;
576   jobject thread_buffer_size = nullptr;
577   jobject max_chunk_size = nullptr;
578   jobject memory_size = nullptr;
579   jobject preserve_repository = nullptr;
580 
581   if (!JfrRecorder::is_created()) {
582     if (_stack_depth.is_set()) {
583       stack_depth = JfrJavaSupport::new_java_lang_Integer((jint)_stack_depth.value(), CHECK);
584     }
585     if (_global_buffer_count.is_set()) {
586       global_buffer_count = JfrJavaSupport::new_java_lang_Long(_global_buffer_count.value(), CHECK);
587     }
588     if (_global_buffer_size.is_set()) {
589       global_buffer_size = JfrJavaSupport::new_java_lang_Long(_global_buffer_size.value()._size, CHECK);
590     }
591     if (_thread_buffer_size.is_set()) {
592       thread_buffer_size = JfrJavaSupport::new_java_lang_Long(_thread_buffer_size.value()._size, CHECK);
593     }
594     if (_max_chunk_size.is_set()) {
595       max_chunk_size = JfrJavaSupport::new_java_lang_Long(_max_chunk_size.value()._size, CHECK);
596     }
597     if (_memory_size.is_set()) {
598       memory_size = JfrJavaSupport::new_java_lang_Long(_memory_size.value()._size, CHECK);
599     }
600     if (_sample_threads.is_set()) {
601       bool startup = DCmd_Source_Internal == source;
602       if (startup) {
603         log_warning(jfr,startup)("%s", "Option samplethreads is deprecated. Use -XX:StartFlightRecording:method-profiling=<off|normal|high|max>");
604       } else {
605         output()->print_cr("%s", "Option samplethreads is deprecated. Use JFR.start method-profiling=<off|normal|high|max>");
606         output()->print_cr("");
607       }
608     }
609   }
610   if (_preserve_repository.is_set()) {
611     preserve_repository = JfrJavaSupport::new_java_lang_Boolean(_preserve_repository.value(), CHECK);
612   }
613 
614   static const char klass[] = "jdk/jfr/internal/dcmd/DCmdConfigure";
615   static const char method[] = "execute";
616   static const char signature[] = "(ZLjava/lang/String;Ljava/lang/String;Ljava/lang/Integer;"
617     "Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;Ljava/lang/Long;"
618     "Ljava/lang/Long;Ljava/lang/Boolean;)[Ljava/lang/String;";
619 
620   JfrJavaArguments execute_args(&result, klass, method, signature, CHECK);
621   execute_args.set_receiver(h_dcmd_instance);
622 
623   // params
624   execute_args.push_int(_verbose ? 1 : 0);
625   execute_args.push_jobject(repository_path);
626   execute_args.push_jobject(dump_path);
627   execute_args.push_jobject(stack_depth);
628   execute_args.push_jobject(global_buffer_count);
629   execute_args.push_jobject(global_buffer_size);
630   execute_args.push_jobject(thread_buffer_size);
631   execute_args.push_jobject(memory_size);
632   execute_args.push_jobject(max_chunk_size);
633   execute_args.push_jobject(preserve_repository);
634 
635   JfrJavaSupport::call_virtual(&execute_args, THREAD);
636   handle_dcmd_result(output(), result.get_oop(), source, THREAD);
637 }