diff --git a/Ladybird/main.cpp b/Ladybird/main.cpp index c1bb7bd03a..ca3acdee97 100644 --- a/Ladybird/main.cpp +++ b/Ladybird/main.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -48,20 +49,9 @@ static ErrorOr handle_attached_debugger() // incorrectly forwards the signal to us even when it's set to // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425 // for details. - auto unbuffered_status_file = TRY(Core::File::open("/proc/self/status"sv, Core::File::OpenMode::Read)); - auto status_file = TRY(Core::InputBufferedFile::create(move(unbuffered_status_file))); - auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); - while (TRY(status_file->can_read_line())) { - auto line = TRY(status_file->read_line(buffer)); - auto const parts = line.split_view(':'); - if (parts.size() < 2 || parts[0] != "TracerPid"sv) - continue; - auto tracer_pid = parts[1].to_uint(); - if (tracer_pid != 0UL) { - dbgln("Debugger is attached, ignoring SIGINT"); - TRY(Core::System::signal(SIGINT, SIG_IGN)); - } - break; + if (TRY(Core::Process::is_being_debugged())) { + dbgln("Debugger is attached, ignoring SIGINT"); + TRY(Core::System::signal(SIGINT, SIG_IGN)); } #endif return {}; diff --git a/Userland/Libraries/LibCore/Process.cpp b/Userland/Libraries/LibCore/Process.cpp index 1097cdf7aa..bad30c3cfe 100644 --- a/Userland/Libraries/LibCore/Process.cpp +++ b/Userland/Libraries/LibCore/Process.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -144,4 +145,25 @@ ErrorOr Process::set_name([[maybe_unused]] StringView name, [[maybe_unused #endif } +ErrorOr Process::is_being_debugged() +{ +#ifdef AK_OS_LINUX + auto unbuffered_status_file = TRY(Core::File::open("/proc/self/status"sv, Core::File::OpenMode::Read)); + auto status_file = TRY(Core::InputBufferedFile::create(move(unbuffered_status_file))); + auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); + while (TRY(status_file->can_read_line())) { + auto line = TRY(status_file->read_line(buffer)); + auto const parts = line.split_view(':'); + if (parts.size() < 2 || parts[0] != "TracerPid"sv) + continue; + auto tracer_pid = parts[1].to_uint(); + return (tracer_pid != 0UL); + } + return false; +#endif + // FIXME: Implement this for more platforms. + // MacOS version: https://developer.apple.com/library/archive/qa/qa1361/_index.html + return Error::from_string_view("Platform does not support checking for debugger"sv); +} + } diff --git a/Userland/Libraries/LibCore/Process.h b/Userland/Libraries/LibCore/Process.h index 3e0891afba..b25d6e4db8 100644 --- a/Userland/Libraries/LibCore/Process.h +++ b/Userland/Libraries/LibCore/Process.h @@ -30,6 +30,8 @@ public: Yes, }; static ErrorOr set_name(StringView, SetThreadName = SetThreadName::No); + + static ErrorOr is_being_debugged(); }; }