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>,
	"gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: Re: [Patch] Fix Assertion pid 0 failure in AIX while running gdb.threads/foll-fork-other-thread
Date: Tue, 2 May 2023 13:55:15 +0000	[thread overview]
Message-ID: <CH2PR15MB3544CE8EBA1A777CD079FD07D66F9@CH2PR15MB3544.namprd15.prod.outlook.com> (raw)
In-Reply-To: <540716db6567b15aa2b3c61b5e85ecbb51511f2a.camel@de.ibm.com>


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

Hi Ulrich and community,

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

>Ah, right. There's indeed no inf argument (unfortunately), but you can
>just use "current_inferior ()" instead.  This is guaranteed to be
>set correctly at this point.

This makes it easy. Thanks Ulrich. It fixes the issue and no memory leaks. Kindly push this small patch if there are no further changes.

>warning: "/usr/lib/libpthread.a": member "shr_xpg5.o" missing.
>>warning: "/usr/lib/libc.a": member "shr.o" missing.
>>warning: Could not load shared library symbols for 3 libraries, e.g. /usr/lib/libpthreads.a(shr_comm.o).
>>Use the "info sharedlibrary" command to see the complete listing.
[snip]
>>Did this API change in the last few days?? Until Thursday all was fine
>>and I was not seeing these warnings. I updated my GDB to the latest
>>development branch today.

>I'm not aware of anything.  But if you noticed this when updating the
>GDB sources recently, you might be able to find the specific commit
>that introduced the breakage via bisect (binary search).

I will dig this deeper and figure out. Thanks.

Thanks and regards,
Aditya.

----------------------------
Output after patch application:-

bash-5.1$ ./gdb foll-fork-other-thread
GNU gdb (GDB) 14.0.50.20230502-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 foll-fork-other-thread...
(gdb) r
Starting program: /home/aditya/latest_gdb/binutils-gdb/gdb/foll-fork-other-thread
[New Thread 258]
[Detaching after fork from child process 20906416]
[Inferior 1 (process 21365186) exited normally]
(gdb)


Output without applying the patch:-

(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 ???

The code :-

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 */
}

From: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
Date: Tuesday, 2 May 2023 at 6:23 PM
To: gdb-patches@sourceware.org <gdb-patches@sourceware.org>, Aditya Kamath1 <Aditya.Kamath1@ibm.com>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>
Subject: Re: [Patch] Fix Assertion pid 0 failure in AIX while running gdb.threads/foll-fork-other-thread
Aditya Kamath1 <Aditya.Kamath1@ibm.com> wrote:

>Yes it does cause leaks. Currently there are two places from where
>pd_disable () is called. One from aix_thread_tarhet::detach () where
>we have the inferior. Unfortunately this is not getting called at all
>and hence we are never actually detaching. We can take the case of the
>program pasted in this mail below. But even in complex programs
>detach has never been called. This makes me worried that I am missing
>something major. Am I??

::detach() is called when you detach from an inferior that GDB
attached to (i.e. when using the "detach" command).  It is not
called for an inferior that was actually started under GDB;
::mourn_inferior() is used in those cases.

>And when pd_disable () is called it is from the mourn inferior ().
>Here we do not have the inferior_ptid.pid nor the inf to catch hold
>of the exited thread/process..

Ah, right. There's indeed no inf argument (unfortunately), but you can
just use "current_inferior ()" instead.  This is guaranteed to be
set correctly at this point.

>Also I begin to see these warnings…
>
>Starting program: /home/aditya/latest_gdb/binutils-gdb/gdb/foll-fork-other-thread
>[New Thread 258]
>[New inferior 2 (process 30278140)]
>warning: "/usr/lib/libpthreads.a": member "shr_comm.o" missing.
>warning: "/usr/lib/libpthread.a": member "shr_xpg5.o" missing.
>warning: "/usr/lib/libc.a": member "shr.o" missing.
>warning: Could not load shared library symbols for 3 libraries, e.g. /usr/lib/libpthreads.a(shr_comm.o).
>Use the "info sharedlibrary" command to see the complete listing.
[snip]
>Did this API change in the last few days?? Until Thursday all was fine
>and I was not seeing these warnings. I updated my GDB to the latest
>development branch today.

I'm not aware of anything.  But if you noticed this when updating the
GDB sources recently, you might be able to find the specific commit
that introduced the breakage via bisect (binary search).

Bye,
Ulrich

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

From 985bf8ca70a56393034d99047df2697e4fde4dfd Mon Sep 17 00:00:00 2001
From: Aditya Kamath <Aditya.Kamath@ibm.com>
Date: Tue, 2 May 2023 08:43: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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/aix-thread.c b/gdb/aix-thread.c
index c587027fb6d..1af26c66bc3 100644
--- a/gdb/aix-thread.c
+++ b/gdb/aix-thread.c
@@ -1106,7 +1106,7 @@ static void
 pd_disable (void)
 {
   struct aix_thread_variables *data;
-  data = get_thread_data_helper_for_ptid (inferior_ptid);
+  data = get_thread_data_helper_for_pid (current_inferior ()->pid);
 
   if (!data->pd_able)
     return;
-- 
2.38.3


  reply	other threads:[~2023-05-02 13:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-27  9:45 Aditya Kamath1
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 [this message]
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=CH2PR15MB3544CE8EBA1A777CD079FD07D66F9@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).