From a02d8e57101ba643c1b9978663cd43cea3caa85a Mon Sep 17 00:00:00 2001 From: Itamar Date: Mon, 20 Dec 2021 22:37:19 +0200 Subject: [PATCH] LibDebug: Add optional setup_child() callback to debugger If set, this callback gets called right after fork() in the child process. It can be used by the caller if it wants to perform some logic in the child process before it starts executing the debuggee program. --- Userland/Libraries/LibDebug/DebugSession.cpp | 12 +++++++++++- Userland/Libraries/LibDebug/DebugSession.h | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibDebug/DebugSession.cpp b/Userland/Libraries/LibDebug/DebugSession.cpp index 487132c2d2..b715a1620d 100644 --- a/Userland/Libraries/LibDebug/DebugSession.cpp +++ b/Userland/Libraries/LibDebug/DebugSession.cpp @@ -53,7 +53,9 @@ void DebugSession::for_each_loaded_library(Function DebugSession::exec_and_attach(String const& command, String source_root) +OwnPtr DebugSession::exec_and_attach(String const& command, + String source_root, + Function()> setup_child) { auto pid = fork(); @@ -63,6 +65,14 @@ OwnPtr DebugSession::exec_and_attach(String const& command, String } if (!pid) { + + if (setup_child) { + if (setup_child().is_error()) { + perror("DebugSession::setup_child"); + exit(1); + } + } + if (ptrace(PT_TRACE_ME, 0, 0, 0) < 0) { perror("PT_TRACE_ME"); exit(1); diff --git a/Userland/Libraries/LibDebug/DebugSession.h b/Userland/Libraries/LibDebug/DebugSession.h index 0949ca47f7..eaffaa82c4 100644 --- a/Userland/Libraries/LibDebug/DebugSession.h +++ b/Userland/Libraries/LibDebug/DebugSession.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -26,7 +27,7 @@ namespace Debug { class DebugSession : public ProcessInspector { public: - static OwnPtr exec_and_attach(String const& command, String source_root = {}); + static OwnPtr exec_and_attach(String const& command, String source_root = {}, Function()> setup_child = {}); virtual ~DebugSession() override;