From dc0b57133569a421e6f006824d9fbc22c832f7cd Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Tue, 4 Jan 2022 19:53:11 +0000 Subject: [PATCH] [ VM / CLI ] Remove service flags from VM and the CLI when building in PRODUCT mode Fixes https://github.com/dart-lang/sdk/issues/47813 TEST=Manual testing, CQ Change-Id: I50e57fa653c45c892e748d4e283617200cee0c0a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/224860 Reviewed-by: Siva Annamalai Commit-Queue: Ben Konyi --- pkg/dartdev/lib/src/commands/run.dart | 265 ++++++++++++++------------ runtime/bin/main_options.cc | 20 ++ utils/application_snapshot.gni | 1 + 3 files changed, 161 insertions(+), 125 deletions(-) diff --git a/pkg/dartdev/lib/src/commands/run.dart b/pkg/dartdev/lib/src/commands/run.dart index a74fa84ffde..457f673b26c 100644 --- a/pkg/dartdev/lib/src/commands/run.dart +++ b/pkg/dartdev/lib/src/commands/run.dart @@ -20,6 +20,7 @@ import '../utils.dart'; import '../vm_interop_handler.dart'; class RunCommand extends DartdevCommand { + static const bool isProductMode = bool.fromEnvironment("dart.vm.product"); static const String cmdName = 'run'; // kErrorExitCode, as defined in runtime/bin/error_exit.h @@ -47,97 +48,103 @@ class RunCommand extends DartdevCommand { // the list of flags in Options::ProcessVMDebuggingOptions in // runtime/bin/main_options.cc. Failure to do so will result in those VM // options being ignored. - argParser - ..addSeparator( - 'Debugging options:', - ) - ..addOption( - 'observe', - help: 'The observe flag is a convenience flag used to run a program ' - 'with a set of common options useful for debugging.', - valueHelp: '[[/]]', - ) - ..addOption('launch-dds', hide: true, help: 'Launch DDS.') - ..addSeparator( - 'Options implied by --observe are currently:', - ) - ..addOption( - 'enable-vm-service', - help: 'Enables the VM service and listens on the specified port for ' - 'connections (default port number is 8181, default bind address ' - 'is localhost).', - valueHelp: '[[/]]', - ) - ..addFlag( - 'serve-devtools', - help: 'Serves an instance of the Dart DevTools debugger and profiler ' - 'via the VM service at /devtools.', - defaultsTo: true, - ) - ..addFlag( - 'pause-isolates-on-exit', - help: 'Pause isolates on exit when ' - 'running with --enable-vm-service.', - ) - ..addFlag( - 'pause-isolates-on-unhandled-exceptions', - help: 'Pause isolates when an unhandled exception is encountered ' - 'when running with --enable-vm-service.', - ) - ..addFlag( - 'warn-on-pause-with-no-debugger', - help: 'Print a warning when an isolate pauses with no attached debugger' - ' when running with --enable-vm-service.', - ) - ..addSeparator( - 'Other debugging options:', - ) - ..addFlag( - 'pause-isolates-on-start', - help: 'Pause isolates on start when ' - 'running with --enable-vm-service.', - ) - ..addFlag( - 'enable-asserts', - help: 'Enable assert statements.', - ) - ..addOption( - 'verbosity', - help: 'Sets the verbosity level of the compilation.', - defaultsTo: Verbosity.defaultValue, - allowed: Verbosity.allowedValues, - allowedHelp: Verbosity.allowedValuesHelp, - ); + argParser.addSeparator( + 'Debugging options:', + ); + if (!isProductMode) { + argParser + ..addOption( + 'observe', + help: 'The observe flag is a convenience flag used to run a program ' + 'with a set of common options useful for debugging.', + valueHelp: '[[/]]', + ) + ..addOption('launch-dds', hide: true, help: 'Launch DDS.') + ..addSeparator( + 'Options implied by --observe are currently:', + ) + ..addOption( + 'enable-vm-service', + help: 'Enables the VM service and listens on the specified port for ' + 'connections (default port number is 8181, default bind address ' + 'is localhost).', + valueHelp: '[[/]]', + ) + ..addFlag( + 'serve-devtools', + help: 'Serves an instance of the Dart DevTools debugger and profiler ' + 'via the VM service at /devtools.', + defaultsTo: true, + ) + ..addFlag( + 'pause-isolates-on-exit', + help: 'Pause isolates on exit when ' + 'running with --enable-vm-service.', + ) + ..addFlag( + 'pause-isolates-on-unhandled-exceptions', + help: 'Pause isolates when an unhandled exception is encountered ' + 'when running with --enable-vm-service.', + ) + ..addFlag( + 'warn-on-pause-with-no-debugger', + help: + 'Print a warning when an isolate pauses with no attached debugger' + ' when running with --enable-vm-service.', + ) + ..addSeparator( + 'Other debugging options:', + ) + ..addFlag( + 'pause-isolates-on-start', + help: 'Pause isolates on start when ' + 'running with --enable-vm-service.', + ) + ..addFlag( + 'enable-asserts', + help: 'Enable assert statements.', + ); + } + argParser.addOption( + 'verbosity', + help: 'Sets the verbosity level of the compilation.', + defaultsTo: Verbosity.defaultValue, + allowed: Verbosity.allowedValues, + allowedHelp: Verbosity.allowedValuesHelp, + ); if (verbose) { argParser.addSeparator( 'Advanced options:', ); } + argParser.addMultiOption( + 'define', + abbr: 'D', + valueHelp: 'key=value', + help: 'Define an environment declaration.', + ); + if (!isProductMode) { + argParser + ..addFlag( + 'disable-service-auth-codes', + hide: !verbose, + negatable: false, + help: 'Disables the requirement for an authentication code to ' + 'communicate with the VM service. Authentication codes help ' + 'protect against CSRF attacks, so it is not recommended to ' + 'disable them unless behind a firewall on a secure device.', + ) + ..addFlag( + 'enable-service-port-fallback', + hide: !verbose, + negatable: false, + help: 'When the VM service is told to bind to a particular port, ' + 'fallback to 0 if it fails to bind instread of failing to ' + 'start.', + ); + } argParser - ..addMultiOption( - 'define', - abbr: 'D', - valueHelp: 'key=value', - help: 'Define an environment declaration.', - ) - ..addFlag( - 'disable-service-auth-codes', - hide: !verbose, - negatable: false, - help: 'Disables the requirement for an authentication code to ' - 'communicate with the VM service. Authentication codes help ' - 'protect against CSRF attacks, so it is not recommended to ' - 'disable them unless behind a firewall on a secure device.', - ) - ..addFlag( - 'enable-service-port-fallback', - hide: !verbose, - negatable: false, - help: 'When the VM service is told to bind to a particular port, ' - 'fallback to 0 if it fails to bind instread of failing to ' - 'start.', - ) ..addOption( 'namespace', hide: !verbose, @@ -164,17 +171,22 @@ class RunCommand extends DartdevCommand { hide: !verbose, negatable: false, help: 'Enables tracing of library and script loading.', - ) - ..addFlag('dds', - hide: !verbose, - help: 'Use the Dart Development Service (DDS) for enhanced debugging ' - 'functionality. Note: Disabling DDS may break some functionality ' - 'in IDEs and other tooling.', - defaultsTo: true) - ..addFlag( - 'debug-dds', - hide: true, ); + + if (!isProductMode) { + argParser + ..addFlag('dds', + hide: !verbose, + help: + 'Use the Dart Development Service (DDS) for enhanced debugging ' + 'functionality. Note: Disabling DDS may break some functionality ' + 'in IDEs and other tooling.', + defaultsTo: true) + ..addFlag( + 'debug-dds', + hide: true, + ); + } addExperimentalFlags(argParser, verbose); } @@ -191,40 +203,43 @@ class RunCommand extends DartdevCommand { // The command line arguments after the command name. runArgs = argResults.rest.skip(1).toList(); } - // --launch-dds is provided by the VM if the VM service is to be enabled. In - // that case, we need to launch DDS as well. - String launchDdsArg = argResults['launch-dds']; - String ddsHost = ''; - String ddsPort = ''; - bool launchDevTools = argResults['serve-devtools']; - bool launchDds = false; - if (launchDdsArg != null) { - launchDds = true; - final ddsUrl = launchDdsArg.split('\\:'); - ddsHost = ddsUrl[0]; - ddsPort = ddsUrl[1]; - } - final bool debugDds = argResults['debug-dds']; + if (!isProductMode) { + // --launch-dds is provided by the VM if the VM service is to be enabled. In + // that case, we need to launch DDS as well. + String launchDdsArg = argResults['launch-dds']; + String ddsHost = ''; + String ddsPort = ''; - bool disableServiceAuthCodes = argResults['disable-service-auth-codes']; + bool launchDevTools = argResults['serve-devtools']; + bool launchDds = false; + if (launchDdsArg != null) { + launchDds = true; + final ddsUrl = launchDdsArg.split('\\:'); + ddsHost = ddsUrl[0]; + ddsPort = ddsUrl[1]; + } + final bool debugDds = argResults['debug-dds']; - // If the user wants to start a debugging session we need to do some extra - // work and spawn a Dart Development Service (DDS) instance. DDS is a VM - // service intermediary which implements the VM service protocol and - // provides non-VM specific extensions (e.g., log caching, client - // synchronization). - _DebuggingSession debugSession; - if (launchDds) { - debugSession = _DebuggingSession(); - if (!await debugSession.start( - ddsHost, - ddsPort, - disableServiceAuthCodes, - launchDevTools, - debugDds, - )) { - return errorExitCode; + bool disableServiceAuthCodes = argResults['disable-service-auth-codes']; + + // If the user wants to start a debugging session we need to do some extra + // work and spawn a Dart Development Service (DDS) instance. DDS is a VM + // service intermediary which implements the VM service protocol and + // provides non-VM specific extensions (e.g., log caching, client + // synchronization). + _DebuggingSession debugSession; + if (launchDds) { + debugSession = _DebuggingSession(); + if (!await debugSession.start( + ddsHost, + ddsPort, + disableServiceAuthCodes, + launchDevTools, + debugDds, + )) { + return errorExitCode; + } } } diff --git a/runtime/bin/main_options.cc b/runtime/bin/main_options.cc index e213e706b46..83c14082c25 100644 --- a/runtime/bin/main_options.cc +++ b/runtime/bin/main_options.cc @@ -141,8 +141,10 @@ void Options::PrintUsage() { if (!Options::verbose_option()) { Syslog::Print( "Common VM flags:\n" +#if !defined(PRODUCT) "--enable-asserts\n" " Enable assert statements.\n" +#endif // !defined(PRODUCT) "--help or -h\n" " Display this message (add -v or --verbose for information about\n" " all VM options).\n" @@ -151,6 +153,7 @@ void Options::PrintUsage() { "--define== or -D=\n" " Define an environment declaration. To specify multiple declarations,\n" " use multiple instances of this option.\n" +#if !defined(PRODUCT) "--observe[=[/]]\n" " The observe flag is a convenience flag used to run a program with a\n" " set of options which are often useful for debugging under Observatory.\n" @@ -166,6 +169,7 @@ void Options::PrintUsage() { " Outputs information necessary to connect to the VM service to the\n" " specified file in JSON format. Useful for clients which are unable to\n" " listen to stdout for the Observatory listening message.\n" +#endif // !defined(PRODUCT) "--snapshot-kind=\n" "--snapshot=\n" " These snapshot options are used to generate a snapshot of the loaded\n" @@ -178,8 +182,10 @@ void Options::PrintUsage() { } else { Syslog::Print( "Supported options:\n" +#if !defined(PRODUCT) "--enable-asserts\n" " Enable assert statements.\n" +#endif // !defined(PRODUCT) "--help or -h\n" " Display this message (add -v or --verbose for information about\n" " all VM options).\n" @@ -188,6 +194,7 @@ void Options::PrintUsage() { "--define== or -D=\n" " Define an environment declaration. To specify multiple declarations,\n" " use multiple instances of this option.\n" +#if !defined(PRODUCT) "--observe[=[/]]\n" " The observe flag is a convenience flag used to run a program with a\n" " set of options which are often useful for debugging under Observatory.\n" @@ -199,12 +206,14 @@ void Options::PrintUsage() { " --warn-on-pause-with-no-debugger\n" " This set is subject to change.\n" " Please see these options for further documentation.\n" +#endif // !defined(PRODUCT) "--version\n" " Print the VM version.\n" "\n" "--trace-loading\n" " enables tracing of library and script loading\n" "\n" +#if !defined(PRODUCT) "--enable-vm-service[=[/]]\n" " Enables the VM service and listens on specified port for connections\n" " (default port number is 8181, default bind address is localhost).\n" @@ -219,6 +228,7 @@ void Options::PrintUsage() { " When the VM service is told to bind to a particular port, fallback to 0 if\n" " it fails to bind instead of failing to start.\n" "\n" +#endif // !defined(PRODUCT) "--root-certs-file=\n" " The path to a file containing the trusted root certificates to use for\n" " secure socket connections.\n" @@ -321,6 +331,7 @@ const char* Options::vm_service_server_ip_ = DEFAULT_VM_SERVICE_SERVER_IP; int Options::vm_service_server_port_ = INVALID_VM_SERVICE_SERVER_PORT; bool Options::ProcessEnableVmServiceOption(const char* arg, CommandLineOptions* vm_options) { +#if !defined(PRODUCT) const char* value = OptionProcessor::ProcessOption(arg, "--enable-vm-service"); if (value == NULL) { @@ -339,10 +350,15 @@ bool Options::ProcessEnableVmServiceOption(const char* arg, #endif // !defined(DART_PRECOMPILED_RUNTIME) enable_vm_service_ = true; return true; +#else + // VM service not available in product mode. + return false; +#endif // !defined(PRODUCT) } bool Options::ProcessObserveOption(const char* arg, CommandLineOptions* vm_options) { +#if !defined(PRODUCT) const char* value = OptionProcessor::ProcessOption(arg, "--observe"); if (value == NULL) { return false; @@ -366,6 +382,10 @@ bool Options::ProcessObserveOption(const char* arg, #endif // !defined(DART_PRECOMPILED_RUNTIME) enable_vm_service_ = true; return true; +#else + // VM service not available in product mode. + return false; +#endif // !defined(PRODUCT) } // Explicitly handle VM flags that can be parsed by DartDev's run command. diff --git a/utils/application_snapshot.gni b/utils/application_snapshot.gni index 9c4cd3fbceb..2d6ea50f216 100644 --- a/utils/application_snapshot.gni +++ b/utils/application_snapshot.gni @@ -128,6 +128,7 @@ template("_application_snapshot") { # (Instead of ensuring every user of the "application_snapshot" / # "kernel_snapshot" passes this if needed, we always pass it) "-Dsdk_hash=$sdk_hash", + "-Ddart.vm.product=$is_product", ] args += [ rebase_path(main_dart) ] }