From 7106a7bd58512391aa0298f561d191c6bbc29901 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 1 Aug 2024 07:03:03 -0400 Subject: [PATCH] LibWebView+UI: Allow debugging any helper process This removes the --debug-web-content flag, and replaces it with a --debug-process= flag. For example: ladybird --debug-process=WebContent ladybird --debug-process=RequestServer This allows attaching gdb to any helper process. --- Ladybird/HelperProcess.cpp | 10 +++++++--- Ladybird/ImageDecoder/main.cpp | 7 +++++++ Ladybird/RequestServer/main.cpp | 6 ++++++ Ladybird/WebWorker/main.cpp | 6 ++++++ Userland/Libraries/LibWebView/Application.cpp | 10 +++++++--- Userland/Libraries/LibWebView/Options.h | 8 ++------ 6 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Ladybird/HelperProcess.cpp b/Ladybird/HelperProcess.cpp index 212c961ea2d..39e7c38bfc9 100644 --- a/Ladybird/HelperProcess.cpp +++ b/Ladybird/HelperProcess.cpp @@ -18,6 +18,9 @@ static ErrorOr> launch_server_process( WebView::EnableCallgrindProfiling enable_callgrind_profiling, ClientArguments&&... client_arguments) { + auto process_type = WebView::process_type_from_name(server_name); + auto const& chrome_options = WebView::Application::chrome_options(); + if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::Yes) { arguments.prepend({ "--tool=callgrind"sv, @@ -26,6 +29,9 @@ static ErrorOr> launch_server_process( }); } + if (chrome_options.debug_helper_process == process_type) + arguments.append("--wait-for-debugger"sv); + for (auto [i, path] : enumerate(candidate_server_paths)) { Core::ProcessSpawnOptions options { .name = server_name, .arguments = arguments }; @@ -45,7 +51,7 @@ static ErrorOr> launch_server_process( if constexpr (requires { process.client->set_pid(pid_t {}); }) process.client->set_pid(process.process.pid()); - WebView::Application::the().add_child_process(WebView::Process { WebView::process_type_from_name(server_name), process.client, move(process.process) }); + WebView::Application::the().add_child_process(WebView::Process { process_type, process.client, move(process.process) }); if (enable_callgrind_profiling == WebView::EnableCallgrindProfiling::Yes) { dbgln(); @@ -89,8 +95,6 @@ ErrorOr> launch_web_content_process( arguments.append("--layout-test-mode"sv); if (web_content_options.use_lagom_networking == WebView::UseLagomNetworking::Yes) arguments.append("--use-lagom-networking"sv); - if (web_content_options.wait_for_debugger == WebView::WaitForDebugger::Yes) - arguments.append("--wait-for-debugger"sv); if (web_content_options.log_all_js_exceptions == WebView::LogAllJSExceptions::Yes) arguments.append("--log-all-js-exceptions"sv); if (web_content_options.enable_idl_tracing == WebView::EnableIDLTracing::Yes) diff --git a/Ladybird/ImageDecoder/main.cpp b/Ladybird/ImageDecoder/main.cpp index 983b2ce20c0..8585ad512eb 100644 --- a/Ladybird/ImageDecoder/main.cpp +++ b/Ladybird/ImageDecoder/main.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -22,9 +23,15 @@ ErrorOr serenity_main(Main::Arguments arguments) Core::ArgsParser args_parser; StringView mach_server_name; + bool wait_for_debugger = false; + args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name"); + args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger"); args_parser.parse(arguments); + if (wait_for_debugger) + Core::Process::wait_for_debugger_and_break(); + Core::EventLoop event_loop; #if defined(AK_OS_MACOS) diff --git a/Ladybird/RequestServer/main.cpp b/Ladybird/RequestServer/main.cpp index 47a894fbf69..cb565d14c9b 100644 --- a/Ladybird/RequestServer/main.cpp +++ b/Ladybird/RequestServer/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -38,13 +39,18 @@ ErrorOr serenity_main(Main::Arguments arguments) StringView serenity_resource_root; Vector certificates; StringView mach_server_name; + bool wait_for_debugger = false; Core::ArgsParser args_parser; args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root"); args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name"); + args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger"); args_parser.parse(arguments); + if (wait_for_debugger) + Core::Process::wait_for_debugger_and_break(); + // Ensure the certificates are read out here. if (certificates.is_empty()) certificates.append(TRY(find_certificates(serenity_resource_root))); diff --git a/Ladybird/WebWorker/main.cpp b/Ladybird/WebWorker/main.cpp index b1bf2924c22..62ec2c760a0 100644 --- a/Ladybird/WebWorker/main.cpp +++ b/Ladybird/WebWorker/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -41,14 +42,19 @@ ErrorOr serenity_main(Main::Arguments arguments) StringView serenity_resource_root; Vector certificates; bool use_lagom_networking { false }; + bool wait_for_debugger = false; Core::ArgsParser args_parser; args_parser.add_option(request_server_socket, "File descriptor of the request server socket", "request-server-socket", 's', "request-server-socket"); args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root"); args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking"); args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); + args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger"); args_parser.parse(arguments); + if (wait_for_debugger) + Core::Process::wait_for_debugger_and_break(); + #if defined(HAVE_QT) QCoreApplication app(arguments.argc, arguments.argv); Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt); diff --git a/Userland/Libraries/LibWebView/Application.cpp b/Userland/Libraries/LibWebView/Application.cpp index 3abe73b73e8..cc8d7289123 100644 --- a/Userland/Libraries/LibWebView/Application.cpp +++ b/Userland/Libraries/LibWebView/Application.cpp @@ -38,9 +38,9 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_ bool force_new_process = false; bool allow_popups = false; bool disable_sql_database = false; + Optional debug_process; Optional webdriver_content_ipc_path; bool enable_callgrind_profiling = false; - bool debug_web_content = false; bool log_all_js_exceptions = false; bool enable_idl_tracing = false; bool enable_http_cache = false; @@ -54,9 +54,9 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_ args_parser.add_option(force_new_process, "Force creation of new browser/chrome process", "force-new-process"); args_parser.add_option(allow_popups, "Disable popup blocking by default", "allow-popups"); args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database"); + args_parser.add_option(debug_process, "Wait for a debugger to attach to the given process name (WebContent, RequestServer, etc.)", "debug-process", 0, "process-name"); args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown); args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P'); - args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content"); args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions"); args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing"); args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache"); @@ -65,6 +65,10 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_ create_platform_arguments(args_parser); args_parser.parse(arguments); + Optional debug_process_type; + if (debug_process.has_value()) + debug_process_type = process_type_from_name(*debug_process); + m_chrome_options = { .urls = sanitize_urls(raw_urls, new_tab_page_url), .raw_urls = move(raw_urls), @@ -74,6 +78,7 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_ .force_new_process = force_new_process ? ForceNewProcess::Yes : ForceNewProcess::No, .allow_popups = allow_popups ? AllowPopups::Yes : AllowPopups::No, .disable_sql_database = disable_sql_database ? DisableSQLDatabase::Yes : DisableSQLDatabase::No, + .debug_helper_process = move(debug_process_type), }; if (webdriver_content_ipc_path.has_value()) @@ -83,7 +88,6 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_ .command_line = MUST(String::join(' ', arguments.strings)), .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))), .enable_callgrind_profiling = enable_callgrind_profiling ? EnableCallgrindProfiling::Yes : EnableCallgrindProfiling::No, - .wait_for_debugger = debug_web_content ? WaitForDebugger::Yes : WaitForDebugger::No, .log_all_js_exceptions = log_all_js_exceptions ? LogAllJSExceptions::Yes : LogAllJSExceptions::No, .enable_idl_tracing = enable_idl_tracing ? EnableIDLTracing::Yes : EnableIDLTracing::No, .enable_http_cache = enable_http_cache ? EnableHTTPCache::Yes : EnableHTTPCache::No, diff --git a/Userland/Libraries/LibWebView/Options.h b/Userland/Libraries/LibWebView/Options.h index 768589a99c6..e0987eb6146 100644 --- a/Userland/Libraries/LibWebView/Options.h +++ b/Userland/Libraries/LibWebView/Options.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace WebView { @@ -43,6 +44,7 @@ struct ChromeOptions { ForceNewProcess force_new_process { ForceNewProcess::No }; AllowPopups allow_popups { AllowPopups::No }; DisableSQLDatabase disable_sql_database { DisableSQLDatabase::No }; + Optional debug_helper_process {}; Optional webdriver_content_ipc_path {}; }; @@ -61,11 +63,6 @@ enum class UseLagomNetworking { Yes, }; -enum class WaitForDebugger { - No, - Yes, -}; - enum class LogAllJSExceptions { No, Yes, @@ -93,7 +90,6 @@ struct WebContentOptions { EnableCallgrindProfiling enable_callgrind_profiling { EnableCallgrindProfiling::No }; IsLayoutTestMode is_layout_test_mode { IsLayoutTestMode::No }; UseLagomNetworking use_lagom_networking { UseLagomNetworking::Yes }; - WaitForDebugger wait_for_debugger { WaitForDebugger::No }; LogAllJSExceptions log_all_js_exceptions { LogAllJSExceptions::No }; EnableIDLTracing enable_idl_tracing { EnableIDLTracing::No }; EnableHTTPCache enable_http_cache { EnableHTTPCache::No };