From 41134f21c63017f364353630d60f7fed106d9473 Mon Sep 17 00:00:00 2001
From: Aditya Vidyadhar Kamath <Aditya.Kamath1@ibm.com>
Date: Thu, 18 Aug 2022 13:50:22 -0500
Subject: [PATCH] Enable-multi-process-debugging-for-AIX

---
 gdb/rs6000-aix-nat.c | 80 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 3 deletions(-)

diff --git a/gdb/rs6000-aix-nat.c b/gdb/rs6000-aix-nat.c
index cb141427696..ad47df399d8 100644
--- a/gdb/rs6000-aix-nat.c
+++ b/gdb/rs6000-aix-nat.c
@@ -73,6 +73,8 @@
 # define ARCH64() (register_size (target_gdbarch (), 0) == 8)
 #endif
 
+# define AIX_FORK_EVENT 0x57e 
+
 class rs6000_nat_target final : public inf_ptrace_target
 {
 public:
@@ -91,10 +93,13 @@ class rs6000_nat_target final : public inf_ptrace_target
 
   ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
 
+  /* Fork detection related functions, For adding multi process debugging 
+     support.  */ 
+  void follow_fork (inferior *, ptid_t, target_waitkind, bool, bool) override;
+
 protected:
 
-  void post_startup_inferior (ptid_t ptid) override
-  { /* Nothing.  */ }
+  void post_startup_inferior (ptid_t ptid) override;
 
 private:
   enum target_xfer_status
@@ -187,6 +192,39 @@ rs6000_ptrace64 (int req, int id, long long addr, int data, void *buf)
   return ret;
 }
 
+void rs6000_nat_target::post_startup_inferior (ptid_t ptid)
+{
+ 
+  /* In AIX to turn on multi process debugging in ptrace
+     PT_MULTI is the option to be passed,
+     with the process ID which can fork () and 
+     the data parameter [fourth parameter] must be 1.  */
+
+  rs6000_ptrace64 (PT_MULTI, ptid.pid(), NULL, 1, NULL);
+}
+
+void 
+rs6000_nat_target::follow_fork (inferior *child_inf, ptid_t child_ptid,
+                          target_waitkind fork_kind, bool follow_child,
+                                                      bool detach_fork)
+{
+ 
+  /* Once the fork event is detected the infrun.c code
+     calls the target_follow_fork to take care of 
+     follow child and detach the child activity which is
+     done using the function below.  */
+  
+  inf_ptrace_target::follow_fork (child_inf, child_ptid, fork_kind,
+                                        follow_child, detach_fork);
+
+  /* If we detach fork and follow child we do not want the child
+     process to geneate events that ptrace can trace.  Hence we 
+     detach it.  */
+
+  if (detach_fork && !follow_child)
+    rs6000_ptrace64 (PT_DETACH, child_ptid.pid (), NULL, NULL, NULL);
+}
+
 /* Fetch register REGNO from the inferior.  */
 
 static void
@@ -539,8 +577,44 @@ rs6000_nat_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
   if (status == 0x57c)
     ourstatus->set_loaded ();
   /* signal 0.  I have no idea why wait(2) returns with this status word.  */
-  else if (status == 0x7f)
+  /* 0x17f and 0x137f in hexadecimal are status returned if 
+     if we follow parent,
+     a switch is made to a child post parent execution
+     and child continues its execution [user switches to child and 
+     presses continue].  */
+  else if (status == 0x7f || status == 0x17f || status == 0x137f)
     ourstatus->set_spurious ();
+  
+  /* In AIX when a fork event is detected the waitpid return 0x57e. 
+     Either the parent can raise a wait event first or the child. 
+     Since the child does not have an inferior and is a new process,
+     we use find_inferior_pid () to check if child process
+     reported the wait event first.  The next wait event will be 
+     raised by its parent process.  This is how we get the parent
+     and child process in AIX.  After this we set the status to 
+     a fork () event and return the parent ptid_t.  */
+ 
+  else if (status == AIX_FORK_EVENT && find_inferior_pid (this,pid) == nullptr)
+  {
+    int parent_pid;
+    parent_pid = waitpid (-1, &status, 0);
+    /* passing child pid as forked pid.  */
+    ourstatus->set_forked (ptid_t (pid));
+    return ptid_t (parent_pid);
+  }
+  
+  /* This is when the parent process reports wait event first after fork ()
+     and then the child.  */
+ 
+  else if (status == AIX_FORK_EVENT)
+  {
+    int parent_pid = pid;
+    int child_pid;
+    child_pid = waitpid (-1, &status, 0);
+    /* passing child pid as forked pid.  */
+    ourstatus->set_forked (ptid_t (child_pid));
+    return ptid_t (parent_pid);
+  }
   /* A normal waitstatus.  Let the usual macros deal with it.  */
   else
     *ourstatus = host_status_to_waitstatus (status);
-- 
2.31.1

