Skip to content

Commit 2166673

Browse files
SebastianZahaADKaster
authored andcommitted
LibCore+Ladybird: Extract helper into generic Process::is_being_debugged
1 parent 002e206 commit 2166673

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Ladybird/main.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <Browser/Database.h>
1616
#include <LibCore/ArgsParser.h>
1717
#include <LibCore/EventLoop.h>
18+
#include <LibCore/Process.h>
1819
#include <LibCore/System.h>
1920
#include <LibFileSystem/FileSystem.h>
2021
#include <LibGfx/Font/FontDatabase.h>
@@ -48,20 +49,9 @@ static ErrorOr<void> handle_attached_debugger()
4849
// incorrectly forwards the signal to us even when it's set to
4950
// "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
5051
// for details.
51-
auto unbuffered_status_file = TRY(Core::File::open("/proc/self/status"sv, Core::File::OpenMode::Read));
52-
auto status_file = TRY(Core::InputBufferedFile::create(move(unbuffered_status_file)));
53-
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
54-
while (TRY(status_file->can_read_line())) {
55-
auto line = TRY(status_file->read_line(buffer));
56-
auto const parts = line.split_view(':');
57-
if (parts.size() < 2 || parts[0] != "TracerPid"sv)
58-
continue;
59-
auto tracer_pid = parts[1].to_uint<u32>();
60-
if (tracer_pid != 0UL) {
61-
dbgln("Debugger is attached, ignoring SIGINT");
62-
TRY(Core::System::signal(SIGINT, SIG_IGN));
63-
}
64-
break;
52+
if (TRY(Core::Process::is_being_debugged())) {
53+
dbgln("Debugger is attached, ignoring SIGINT");
54+
TRY(Core::System::signal(SIGINT, SIG_IGN));
6555
}
6656
#endif
6757
return {};

Userland/Libraries/LibCore/Process.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <AK/ScopeGuard.h>
1111
#include <AK/String.h>
1212
#include <AK/Vector.h>
13+
#include <LibCore/File.h>
1314
#include <LibCore/Process.h>
1415
#include <LibCore/System.h>
1516
#include <errno.h>
@@ -144,4 +145,25 @@ ErrorOr<void> Process::set_name([[maybe_unused]] StringView name, [[maybe_unused
144145
#endif
145146
}
146147

148+
ErrorOr<bool> Process::is_being_debugged()
149+
{
150+
#ifdef AK_OS_LINUX
151+
auto unbuffered_status_file = TRY(Core::File::open("/proc/self/status"sv, Core::File::OpenMode::Read));
152+
auto status_file = TRY(Core::InputBufferedFile::create(move(unbuffered_status_file)));
153+
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
154+
while (TRY(status_file->can_read_line())) {
155+
auto line = TRY(status_file->read_line(buffer));
156+
auto const parts = line.split_view(':');
157+
if (parts.size() < 2 || parts[0] != "TracerPid"sv)
158+
continue;
159+
auto tracer_pid = parts[1].to_uint<u32>();
160+
return (tracer_pid != 0UL);
161+
}
162+
return false;
163+
#endif
164+
// FIXME: Implement this for more platforms.
165+
// MacOS version: https://developer.apple.com/library/archive/qa/qa1361/_index.html
166+
return Error::from_string_view("Platform does not support checking for debugger"sv);
167+
}
168+
147169
}

Userland/Libraries/LibCore/Process.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Process {
3030
Yes,
3131
};
3232
static ErrorOr<void> set_name(StringView, SetThreadName = SetThreadName::No);
33+
34+
static ErrorOr<bool> is_being_debugged();
3335
};
3436

3537
}

0 commit comments

Comments
 (0)