Просмотр исходного кода

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

Sebastian Zaha 2 лет назад
Родитель
Сommit
216667368d
3 измененных файлов с 28 добавлено и 14 удалено
  1. 4 14
      Ladybird/main.cpp
  2. 22 0
      Userland/Libraries/LibCore/Process.cpp
  3. 2 0
      Userland/Libraries/LibCore/Process.h

+ 4 - 14
Ladybird/main.cpp

@@ -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 {};

+ 22 - 0
Userland/Libraries/LibCore/Process.cpp

@@ -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);
+}
+
 }

+ 2 - 0
Userland/Libraries/LibCore/Process.h

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