diff --git a/common/settings.h b/common/settings.h index 48b9ed700346e5f43a0759bb69326304b8326404..f8bc41750acaa5dc4a8307c51bc779ac3fe347e1 100644 --- a/common/settings.h +++ b/common/settings.h @@ -17,6 +17,8 @@ struct Settings { // Port on target will be auto selected by the OS. A message will be printed // on the target with the port after it has been selected. uint32_t observatory_port = 0; + bool enable_diagnostic = false; + uint32_t diagnostic_port = 0; bool start_paused = false; bool trace_startup = false; bool endless_trace_buffer = false; diff --git a/shell/common/diagnostic/diagnostic_server.cc b/shell/common/diagnostic/diagnostic_server.cc index 2fc0c3cfc415acff104525829600beea4fe5a0a7..a7a3f02224ff4a10547a6d46d411eb745dd31d1f 100644 --- a/shell/common/diagnostic/diagnostic_server.cc +++ b/shell/common/diagnostic/diagnostic_server.cc @@ -31,6 +31,7 @@ using tonic::DartLibraryNatives; using blink::EmbedderResources; using tonic::DartInvokeField; using tonic::LogIfError; +using tonic::ToDart; namespace { @@ -60,7 +61,7 @@ void SendNull(Dart_Port port_id) { DART_NATIVE_CALLBACK_STATIC(DiagnosticServer, HandleSkiaPictureRequest); -void DiagnosticServer::Start() { +void DiagnosticServer::Start(uint32_t port) { if (!g_natives) { g_natives = new DartLibraryNatives(); g_natives->Register({ @@ -91,7 +92,7 @@ void DiagnosticServer::Start() { FTL_CHECK(!LogIfError(Dart_FinalizeLoading(false))); - DartInvokeField(Dart_RootLibrary(), "diagnosticServerStart", {}); + DartInvokeField(Dart_RootLibrary(), "diagnosticServerStart", {ToDart(port)}); } void DiagnosticServer::HandleSkiaPictureRequest(Dart_Handle send_port) { diff --git a/shell/common/diagnostic/diagnostic_server.dart b/shell/common/diagnostic/diagnostic_server.dart index cc7aeaec7266dac2b8f8c5a2593fe76873ea6c5e..6779713f7ff9e6e4e1f5449a794f076e255b9a90 100644 --- a/shell/common/diagnostic/diagnostic_server.dart +++ b/shell/common/diagnostic/diagnostic_server.dart @@ -11,8 +11,8 @@ import 'dart:typed_data'; void handleSkiaPictureRequest(SendPort sendPort) native 'DiagnosticServer_HandleSkiaPictureRequest'; -void diagnosticServerStart() { - HttpServer.bind('127.0.0.1', 0).then((HttpServer server) { +void diagnosticServerStart(int port) { + HttpServer.bind('127.0.0.1', port).then((HttpServer server) { server.listen(dispatchRequest, cancelOnError: true); String ip = server.address.address.toString(); diff --git a/shell/common/diagnostic/diagnostic_server.h b/shell/common/diagnostic/diagnostic_server.h index 00553ca2e35962b206a488fbc425e8a08e2f4fda..b5f9e1d00bb5f7c233e353aaa0993f873e832b7e 100644 --- a/shell/common/diagnostic/diagnostic_server.h +++ b/shell/common/diagnostic/diagnostic_server.h @@ -11,7 +11,7 @@ namespace shell { class DiagnosticServer { public: - static void Start(); + static void Start(uint32_t port); static void HandleSkiaPictureRequest(Dart_Handle send_port); private: diff --git a/shell/common/shell.cc b/shell/common/shell.cc index b60999b9559f2f273d1a5edb5b30d584343d0fc4..9c64802c0eedf312255b3d936cd6ad3971306afe 100644 --- a/shell/common/shell.cc +++ b/shell/common/shell.cc @@ -42,6 +42,18 @@ bool IsViewInvalid(const ftl::WeakPtr& platform_view) { return !platform_view; } +template bool GetSwitchValue(const base::CommandLine& command_line, + Switch sw, T* result) { + auto port_string = command_line.GetSwitchValueASCII(FlagForSwitch(sw)); + std::stringstream stream(port_string); + T value = 0; + if (stream >> value) { + *result = value; + return true; + } + return false; +} + class NonDiscardableMemory : public base::DiscardableMemory { public: explicit NonDiscardableMemory(size_t size) : data_(new uint8_t[size]) {} @@ -66,8 +78,8 @@ base::LazyInstance g_discardable; void ServiceIsolateHook(bool running_precompiled) { if (!running_precompiled) { const blink::Settings& settings = blink::Settings::Get(); - if (settings.enable_observatory) - DiagnosticServer::Start(); + if (settings.enable_diagnostic) + DiagnosticServer::Start(settings.diagnostic_port); } } @@ -135,19 +147,26 @@ void Shell::InitStandalone(std::string icu_data_path, // Set Observatory Port if (command_line.HasSwitch(FlagForSwitch(Switch::DeviceObservatoryPort))) { - auto port_string = command_line.GetSwitchValueASCII( - FlagForSwitch(Switch::DeviceObservatoryPort)); - std::stringstream stream(port_string); - uint32_t port = 0; - if (stream >> port) { - settings.observatory_port = port; - } else { + if (!GetSwitchValue(command_line, Switch::DeviceObservatoryPort, + &settings.observatory_port)) { FTL_LOG(INFO) << "Observatory port specified was malformed. Will default to " << settings.observatory_port; } } + settings.enable_diagnostic = + !command_line.HasSwitch(FlagForSwitch(Switch::DisableDiagnostic)); + + if (command_line.HasSwitch(FlagForSwitch(Switch::DeviceDiagnosticPort))) { + if (!GetSwitchValue(command_line, Switch::DeviceDiagnosticPort, + &settings.diagnostic_port)) { + FTL_LOG(INFO) + << "Diagnostic port specified was malformed. Will default to " + << settings.diagnostic_port; + } + } + settings.start_paused = command_line.HasSwitch(FlagForSwitch(Switch::StartPaused)); diff --git a/shell/common/switches.h b/shell/common/switches.h index 33692a5b1a21c22eb70b483ec27e8f912be92ef7..631b4ed8c099ac158c29ce8b2e4d18f6f9010b0a 100644 --- a/shell/common/switches.h +++ b/shell/common/switches.h @@ -43,6 +43,13 @@ DEF_SWITCH(DisableObservatory, "disable-observatory", "Disable the Dart Observatory. The observatory is never available " "in release mode.") +DEF_SWITCH(DeviceDiagnosticPort, + "diagnostic-port", + "A custom diagnostic server port.") +DEF_SWITCH(DisableDiagnostic, + "disable-diagnostic", + "Disable the diagnostic server. The diagnostic server is never " + "available in release mode.") DEF_SWITCH(EnableDartProfiling, "enable-dart-profiling", "Enable Dart profiling. Profiling information can be viewed from "