1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00
serenity/Ports/gdb/patches/0006-serenity-Implement-mourn_inferior-override-for-the-s.patch
Brian Gianforcaro e56262caed Ports/gdb: Implement wait and mourn_inferior overrides for our target
While troubleshooting why gdb wasn't working when attempting to debug
serenity programs I noticed two things:

 - The contract of serenity's `waitpid(..)` appears to be slightly
   different than the generic ptrace target expects. We need to make
   sure we pass `WSTOPPED`, and it can return different errno values
   that we would want to re-try on.

-  The contract of serenity's `ptrace(..)` implementation appears to
   diverge as well, as we are expected to call `PT_ATTACH` before we
   call `PT_CONTINUE`, otherwise `ptrace(..)` will just error out.

These two patches fix the behavior of wait and mourn_inferior so that
they work as expected on serenity and allow us to attach and then wait
for a process to exit while running under gdb.
2022-02-20 11:49:41 +01:00

51 lines
1.5 KiB
Diff

From 1285f88fcd5a8eabf5536e5af8015777f2a64117 Mon Sep 17 00:00:00 2001
From: Brian Gianforcaro <b.gianfo@gmail.com>
Date: Sun, 20 Feb 2022 00:44:08 -0800
Subject: [PATCH 6/6] serenity: Implement mourn_inferior override for the
serenity_nat_target
We need to pass `WNOHANG` to our `waitpid(..)` call on SerenityOS,
otherwise we will wait forever.
---
gdb/serenity-nat.c | 14 ++++++++++++++
gdb/serenity-nat.h | 2 ++
2 files changed, 16 insertions(+)
diff --git a/gdb/serenity-nat.c b/gdb/serenity-nat.c
index b1d39d4..7789950 100644
--- a/gdb/serenity-nat.c
+++ b/gdb/serenity-nat.c
@@ -71,3 +71,17 @@ ptid_t serenity_nat_target::wait (ptid_t ptid, struct target_waitstatus* ourstat
return ptid_t (pid);
}
+
+void serenity_nat_target::mourn_inferior ()
+{
+ int status;
+
+ /* Wait just one more time to collect the inferior's exit status.
+ * Do not check whether this succeeds though, since we may be
+ * dealing with a process that we attached to. Such a process will
+ * only report its exit status to its original parent.
+ */
+ waitpid (inferior_ptid.pid (), &status, WNOHANG);
+
+ inf_child_target::mourn_inferior ();
+}
diff --git a/gdb/serenity-nat.h b/gdb/serenity-nat.h
index 9ae3b87..35d7ffc 100644
--- a/gdb/serenity-nat.h
+++ b/gdb/serenity-nat.h
@@ -13,6 +13,8 @@ class serenity_nat_target : public inf_ptrace_target
{
ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
+ void mourn_inferior ();
+
private:
bool m_attach_before_continue_called { false };
};
--
2.32.0