public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Aditya Kamath1 <Aditya.Kamath1@ibm.com>
To: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
	Aditya Kamath1 via Gdb-patches <gdb-patches@sourceware.org>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: [Patch] Fix Assertion pid 0 failure in AIX while running gdb.threads/foll-fork-other-thread
Date: Thu, 27 Apr 2023 09:45:04 +0000	[thread overview]
Message-ID: <CH2PR15MB35443DC1D9D5A5C88F9DEAF7D66A9@CH2PR15MB3544.namprd15.prod.outlook.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 4424 bytes --]

Hi all,

Please find attached a patch. {See: 0001-Fix-Assertion-pid-0-failure-in-AIX.patch}

Consider the example foll-fork-other-thread.c code from the gdb.threads testsuite. I have pasted the same below along with the output before and after applying the patch.

Once the thread completes execution and goes to pd_disable () in aix-thread.c , since the process exits, the inferior_ptid.pid () is 0. Hence in pd_disable () when GDB goes to fetch the aix thread data for process 0, this assertion failure occurs.

This patch is a fix for the same. Kindly let me know if there are any changes needed. If not kindly push this patch.

Have a nice day ahead.

Thanks and regards,
Aditya.

-------------------------------------------------------
Code:- { Program Credits: GDB threads testsuite}

#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>

/* Set by GDB.  */
volatile int stop_looping = 0;

static void *
gdb_forker_thread (void *arg)
{
  int ret;
  int stat;
  pid_t pid = FORK_FUNC ();

  if (pid == 0)
    _exit (0);

  assert (pid > 0);

  /* Wait for child to exit.  */
  do
    {
      ret = waitpid (pid, &stat, 0);
    }
  while (ret == -1 && errno == EINTR);

  assert (ret == pid);
  assert (WIFEXITED (stat));
  assert (WEXITSTATUS (stat) == 0);

  stop_looping = 1;

  return NULL;
}

static void
sleep_a_bit (void)
{
  usleep (1000 * 50);
}

int
main (void)
{
  int i;
  int ret;
  pthread_t thread;

  alarm (60);

  ret = pthread_create (&thread, NULL, gdb_forker_thread, NULL);
  assert (ret == 0);

  while (!stop_looping)  /* while loop */
    {
      sleep_a_bit ();    /* break here */
      sleep_a_bit ();    /* other line */
    }

  pthread_join (thread, NULL);

  return 0; /* exiting here */
}

----------------------------------------

Output before patch:-

./gdb ~/gdb_tests/foll-fork-other-thread
GNU gdb (GDB) 14.0.50.20230327-git
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc64-ibm-aix7.2.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/aditya/gdb_tests/foll-fork-other-thread...
(gdb) r
Starting program: /home/aditya/gdb_tests/foll-fork-other-thread
[New Thread 258]
[Detaching after fork from child process 10944778]
[Inferior 1 (process 7209300) exited normally]
inferior.c:350: internal-error: find_inferior_pid: Assertion `pid != 0' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
----- Backtrace -----
0x100f7d307 ???
0x100f7d4cf ???

--------------------------------------------------------
Output after applying patch:-

bash-5.1$ ./gdb ~/gdb_tests/foll-fork-other-thread
GNU gdb (GDB) 14.0.50.20230327-git
Copyright (C) 2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc64-ibm-aix7.2.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/aditya/gdb_tests/foll-fork-other-thread...
(gdb) r
Starting program: /home/aditya/gdb_tests/foll-fork-other-thread
[New Thread 258]
[Detaching after fork from child process 7209320]
[Inferior 1 (process 10944912) exited normally]
(gdb)

[-- Attachment #2: 0001-Fix-Assertion-pid-0-failure-in-AIX.patch --]
[-- Type: application/octet-stream, Size: 1107 bytes --]

From b0aa9687fd29a4a2f4def647fb1875da3fd97d89 Mon Sep 17 00:00:00 2001
From: Aditya Vidyadhar Kamath <Aditya.Kamath1@ibm.com>
Date: Thu, 27 Apr 2023 04:28:21 -0500
Subject: [PATCH] Fix Assertion pid != 0 failure in AIX.

In AIX if there is a main and a thread created from it , then once the
program completed execution and goes to pd_disable () inferior_ptid
had pid 0 leading to an assertion failure while finding the thread's data
in aix-thread.c file.

This patch is a fix for the same.
---
 gdb/aix-thread.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index df843d3c487..63d8c5179b1 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -1106,6 +1106,13 @@ static void
 pd_disable (void)
 {
   struct aix_thread_variables *data;
+  
+  /* If a thread exits and it is the only thread apart from 
+     main, inferior_ptid.pid () will be 0. Hence the below
+     condition to avoid pid != 0 assertion.  */
+  if (inferior_ptid.pid () == 0)
+    return;
+
   data = get_thread_data_helper_for_ptid (inferior_ptid);
 
   if (!data->pd_able)
-- 
2.38.3


             reply	other threads:[~2023-04-27  9:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27  9:45 Aditya Kamath1 [this message]
2023-04-27 12:20 ` Ulrich Weigand
2023-05-02 11:49   ` Aditya Kamath1
2023-05-02 12:53     ` Ulrich Weigand
2023-05-02 13:55       ` Aditya Kamath1
2023-05-02 14:06         ` Ulrich Weigand
2023-05-02 14:40           ` Aditya Kamath1
2023-05-02 14:52             ` Ulrich Weigand
2023-05-02 15:22               ` Aditya Kamath1
2023-05-02 15:33                 ` Ulrich Weigand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CH2PR15MB35443DC1D9D5A5C88F9DEAF7D66A9@CH2PR15MB3544.namprd15.prod.outlook.com \
    --to=aditya.kamath1@ibm.com \
    --cc=Ulrich.Weigand@de.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=sangamesh.swamy@in.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).