[PATCH v4 12/44] gdb, remote: do 'remote_add_inferior' in 'remote_notice_new_inferior' earlier

Markus Metzger <[email protected]>
Newsgroups gmane.comp.gdb.patches
Message-ID <[email protected]>
From: Tankut Baris Aktemur <[email protected]>

In remote_target::remote_notice_new_inferior, we check if the inferior
for the reported ptid exists, and if not, we add that inferior via
'remote_add_inferior'.  However, before adding the new inferior, we
may have already added a new thread, because the thread is not found
via 'in_thread_list'.  This may bring the odd situation that a new
thread is being added before its inferior exists.  Fix this problem by
moving the check and creation of a new inferior to an earlier spot in
the execution flow.

Note that the code has a check for inferior_ptid.is_pid, but this is
not useful to avoid the problem described above because the core
infrun clears inferior_ptid (i.e. sets to null_ptid) before waiting on
targets for stop events.
---
 gdb/remote.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/gdb/remote.c b/gdb/remote.c
index 194c4cbd9bb..05cda9d0b0c 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -3223,6 +3223,23 @@ remote_target::remote_notice_new_inferior (ptid_t currthread,
       struct inferior *inf = NULL;
       int pid = currthread.pid ();
 
+      /* When connecting to a target remote, or to a target
+	 extended-remote which already was debugging an inferior, we
+	 may not know about it yet.  Add it before adding its child
+	 thread, so notifications are emitted in a sensible order.  */
+      if (find_inferior_pid (this, pid) == nullptr)
+	{
+	  bool fake_pid_p = !m_features.remote_multi_process_p ();
+
+	  inf = remote_add_inferior (fake_pid_p, pid, -1, 1);
+
+	  /* Fetch the target description for this inferior.  Make sure to
+	     leave the currently selected inferior unchanged.  */
+	  scoped_restore_current_thread restore_thread;
+	  switch_to_inferior_no_thread (inf);
+	  target_find_description ();
+	}
+
       if (inferior_ptid.is_pid ()
 	  && pid == inferior_ptid.pid ())
 	{
@@ -3253,24 +3270,6 @@ remote_target::remote_notice_new_inferior (ptid_t currthread,
 	  return;
 	}
 
-      /* When connecting to a target remote, or to a target
-	 extended-remote which already was debugging an inferior, we
-	 may not know about it yet.  Add it before adding its child
-	 thread, so notifications are emitted in a sensible order.  */
-      if (find_inferior_pid (this, currthread.pid ()) == NULL)
-	{
-	  bool fake_pid_p = !m_features.remote_multi_process_p ();
-
-	  inf = remote_add_inferior (fake_pid_p,
-				     currthread.pid (), -1, 1);
-
-	  /* Fetch the target description for this inferior.  Make sure to
-	     leave the currently selected inferior unchanged.  */
-	  scoped_restore_current_thread restore_thread;
-	  switch_to_inferior_no_thread (inf);
-	  target_find_description ();
-	}
-
       /* This is really a new thread.  Add it.  */
       thread_info *new_thr
 	= remote_add_thread (currthread, state, internal_state, false);
-- 
2.43.0

________________________________________
Intel Deutschland GmbH 

Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany 

Tel: +49 (89) 99143-0 

www.intel.de 

Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman

Chairperson of the Supervisory Board: Sonja Pierer

Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.