--- ./gdb/rs6000-nat.c_orig	2022-11-12 09:42:29 +0000
+++ ./gdb/rs6000-nat.c	2022-11-12 09:53:40 +0000
@@ -58,6 +58,10 @@
 #include <sys/context.h>
 #include <sys/pthdebug.h>
 
+/* Header files for getting ppid in AIX of a child process.  */
+#include <procinfo.h>
+#include <sys/types.h>
+
 #include "features/rs6000/powerpc-vsx64.c"
 #include "features/rs6000/powerpc-vsx32.c"
 #include "features/rs6000/powerpc-altivec32.c"
@@ -105,8 +109,16 @@
 
   ptid_t wait (ptid_t, struct target_waitstatus *, int) override;
 
+  /* Fork detection related functions, For adding multi process debugging
+    support.  */
+ virtual  bool follow_fork (bool,  bool) override;
+
   const struct target_desc *read_description ()  override;
 
+ protected:
+ 
+   void post_startup_inferior (ptid_t ptid) override;
+
 private:
   enum target_xfer_status
     xfer_shared_libraries (enum target_object object,
@@ -118,6 +130,121 @@
 
 static rs6000_nat_target the_rs6000_nat_target;
 
+/* The below declaration is to track number of times, parent has
+   reported fork event before its children.  */
+
+static std::list<pid_t> aix_pending_parent;
+
+/* The below declaration is for a child process event that
+   is reported before its corresponding parent process in
+   the event of a fork ().  */
+
+static std::list<pid_t> aix_pending_children;
+
+static void
+aix_remember_child (pid_t pid)
+{
+  aix_pending_children.push_front (pid);
+}
+
+static void
+aix_remember_parent (pid_t pid)
+{
+  aix_pending_parent.push_front (pid);
+}
+
+/* This function returns a parent of a child process.  */
+
+static pid_t
+find_my_aix_parent (pid_t child_pid)
+{
+  struct procsinfo ProcessBuffer1;
+  int ProcessSize1;
+  struct fdsinfo FileBuffer1;
+ int FileSize1;
+  pid_t IndexPointer1 = 0;
+  int Count = 1;
+ while (1)
+  {
+    if (getprocs (&ProcessBuffer1, sizeof (ProcessBuffer1),
+        &FileBuffer1, sizeof (FileBuffer1),  &IndexPointer1,
+        Count) != 1)
+      break; 
+     
+    if (child_pid == ProcessBuffer1.pi_pid)
+      return ProcessBuffer1.pi_ppid;
+  }
+  return 0;
+}
+
+/* In the below function we check if there was any child
+   process pending.  If it exists we return it from the
+   list, otherwise we return a null.  */
+
+static pid_t
+has_my_aix_child_reported (pid_t parent_pid)
+{
+  auto it = aix_pending_children.begin ();
+  pid_t child;
+  pid_t my_parent;
+  while (it != aix_pending_children.end ())
+  {
+    child = *it;
+    my_parent = find_my_aix_parent (child);
+   if (my_parent == parent_pid)
+    {
+      aix_pending_children.erase (it);
+     return child;
+    }
+    it++;
+  }
+  return 0;
+}
+
+/* In the below function we check if there was any parent 
+   process pending.  If it exists we return it from the
+   list, otherwise we return a null.  */
+
+static pid_t
+has_my_aix_parent_reported (pid_t child_pid)
+{
+ auto it = aix_pending_parent.begin ();
+  pid_t parent;
+  pid_t my_parent = find_my_aix_parent (child_pid);
+  while (it != aix_pending_parent.end ())
+  {
+    parent = *it;
+   if (my_parent == parent)
+    {
+      aix_pending_parent.erase (it);
+      return parent;
+    }
+    it++;
+  }
+  return 0;
+}
+
+/* This function checks if there was a fork () event.  */
+
+static bool
+did_aix_inferior_fork (int status)
+{
+  /* If multi-process debug mode is enabled, the status
+     location is set to W_SFWTED.  */
+
+  status = status & 0xff;
+
+  /* Eliminate the last few bits. If the W_SFWTED is set
+     which is equal to 0x7e, it is a fork event otherwise
+     it is not.  */
+
+  if (status != W_SFWTED)
+    return false;
+  else
+    return true;
+}
+
+
 /* Given REGNO, a gdb register number, return the corresponding
    number suitable for use as a ptrace() parameter.  Return -1 if
    there's no suitable mapping.  Also, set the int pointed to by
@@ -198,6 +325,47 @@
   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.  */
+
+  if (!ARCH64 ())
+    rs6000_ptrace32 (PT_MULTI, ptid.pid(), 0, 1, 0);
+  else
+    rs6000_ptrace64 (PT_MULTI, ptid.pid(), 0, 1, 0);
+}
+
+bool
+rs6000_nat_target::follow_fork (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 (follow_child, detach_fork); */
+
+  if (detach_fork && !follow_child)
+  {
+      struct thread_info *tp = inferior_thread ();
+      pid_t child_pid = tp->pending_follow.value.related_pid.pid ();
+  /* 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 (ARCH64 ())
+      rs6000_ptrace64 (PT_DETACH, child_pid, 0, 0, 0);
+    else
+      rs6000_ptrace32 (PT_DETACH, child_pid, 0, 0, 0);
+  }
+}
+
+
 /* Fetch register REGNO from the inferior.  */
 
 static void
@@ -719,7 +887,7 @@
   pid_t pid;
   int status, save_errno;
 
-  do
+   while(1)
     {
       set_sigint_trap ();
 
@@ -730,6 +898,7 @@
 	}
       while (pid == -1 && errno == EINTR);
 
+
       clear_sigint_trap ();
 
       if (pid == -1)
@@ -746,22 +915,71 @@
       /* Ignore terminated detached child processes.  */
        if (!WIFSTOPPED (status) && find_inferior_pid (this, pid) == nullptr)
 	pid = -1;
-    }
-  while (pid == -1);
 
-  /* AIX has a couple of strange returns from wait().  */
 
-  /* stop after load" status.  */
-  if (status == 0x57c)
-    ourstatus->kind = TARGET_WAITKIND_LOADED;
-  /* signal 0.  I have no idea why wait(2) returns with this status word.  */
-  else if (status == 0x7f)
-    ourstatus->kind = TARGET_WAITKIND_SPURIOUS;
-  /* A normal waitstatus.  Let the usual macros deal with it.  */
-  else
-    store_waitstatus (ourstatus, status);
+      if (pid != -1)
+      {
+        /* AIX has a couple of strange returns from wait().  */
+
+        /* stop after load" status.  */
+        if (status == 0x57c)
+  	    ourstatus->kind = TARGET_WAITKIND_LOADED;
+        /* 0x7f is signal 0.  */
+        /* 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->kind = TARGET_WAITKIND_SPURIOUS;
+        /* Check for a fork () event.  */
+        else if (did_aix_inferior_fork (status))
+        {
+          /* Checking whether it is a parent or a child event.  */
 
-  return ptid_t (pid);
+          /* If the event is a child we check if there was a parent
+             event recorded before.  If yes we got the parent child
+             relationship.  If not we push this child and wait for
+             the next fork () event.  */
+
+          if (find_inferior_pid (this, pid) == nullptr)
+            {
+              pid_t parent_pid = has_my_aix_parent_reported (pid);
+              if (parent_pid > 0)
+              {
+                ourstatus->kind = TARGET_WAITKIND_FORKED;
+		ourstatus->value.related_pid = (ptid_t (pid));
+                return ptid_t (parent_pid);
+              }
+              aix_remember_child (pid);
+            }
+
+            /* If the event is a parent we check if there was a child
+               event recorded before.  If yes we got the parent child
+               relationship.  If not we push this parent and wait for
+               the next fork () event.  */
+
+          else
+            {
+              pid_t child_pid = has_my_aix_child_reported (pid);
+              if (child_pid > 0)
+              {
+                ourstatus->kind = TARGET_WAITKIND_FORKED;
+		ourstatus->value.related_pid = (ptid_t (child_pid));
+                return ptid_t (pid);
+              }
+              aix_remember_parent (pid);
+            }   
+          continue;
+        }
+      /* A normal waitstatus.  Let the usual macros deal with it.  */
+      else
+        store_waitstatus (ourstatus, status);
+      
+      return ptid_t (pid);
+     
+     }
+    }
 }
 
 
