LibCore+Ladybird: Extract helper into generic Process::is_being_debugged

This commit is contained in:
Sebastian Zaha 2023-08-02 10:27:58 +02:00 committed by Andrew Kaster
parent 002e206af0
commit 216667368d
Notes: sideshowbarker 2024-07-17 02:55:44 +09:00
3 changed files with 28 additions and 14 deletions

View File

@ -15,6 +15,7 @@
#include <Browser/Database.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Process.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <LibGfx/Font/FontDatabase.h>
@ -48,20 +49,9 @@ static ErrorOr<void> 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<u32>();
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 {};

View File

@ -10,6 +10,7 @@
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/File.h>
#include <LibCore/Process.h>
#include <LibCore/System.h>
#include <errno.h>
@ -144,4 +145,25 @@ ErrorOr<void> Process::set_name([[maybe_unused]] StringView name, [[maybe_unused
#endif
}
ErrorOr<bool> 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<u32>();
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);
}
}

View File

@ -30,6 +30,8 @@ public:
Yes,
};
static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
static ErrorOr<bool> is_being_debugged();
};
}