public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH v2 04/12] Handle case where Ada task is current but not listed
Date: Wed,  5 Aug 2020 13:08:33 -0600	[thread overview]
Message-ID: <20200805190841.2506771-5-tromey@adacore.com> (raw)
In-Reply-To: <20200805190841.2506771-1-tromey@adacore.com>

Currently, the ravenscar runtime can mark an Ada task as the current
task, before adding it to the list of tasks that can be read by gdb.
In this scenario, gdb can sometimes crash in
ravenscar_get_thread_base_cpu with:

../../src/gdb/ravenscar-thread.c:167: internal-error: int ravenscar_get_thread_base_cpu(ptid_t): Assertion `task_info != NULL' failed.

However, as ravenscar_get_thread_base_cpu is only called to find the
base CPU, we can simply record this when registering the thread, and
look this up later.

2019-03-26  Tom Tromey  <tromey@adacore.com>

	* ravenscar-thread.c (ravenscar_thread_target) <get_base_cpu,
	get_base_thread_from_ravenscar_task>: Now methods.
	<m_cpu_map>: New member.
	(ravenscar_thread_target::get_thread_base_cpu): Rename from
	ravenscar_get_thread_base_cpu.  Check m_cpu_map.
	(ravenscar_thread_target::task_is_currently_active): Update.
	(ravenscar_thread_target::get_base_thread_from_ravenscar_task):
	Now a method.
	(ravenscar_thread_target::add_active_thread): Put initial thread
	into the m_cpu_map.
---
 gdb/ChangeLog          | 13 +++++++++++++
 gdb/ravenscar-thread.c | 40 +++++++++++++++++++++++++++++-----------
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c
index b89b5f62e5a..ee95a1c4bb8 100644
--- a/gdb/ravenscar-thread.c
+++ b/gdb/ravenscar-thread.c
@@ -30,6 +30,7 @@
 #include "top.h"
 #include "regcache.h"
 #include "objfiles.h"
+#include <unordered_map>
 
 /* This module provides support for "Ravenscar" tasks (Ada) when
    debugging on bare-metal targets.
@@ -133,6 +134,14 @@ struct ravenscar_thread_target final : public target_ops
   ptid_t active_task (int cpu);
   bool task_is_currently_active (ptid_t ptid);
   bool runtime_initialized ();
+  int get_thread_base_cpu (ptid_t ptid);
+  ptid_t get_base_thread_from_ravenscar_task (ptid_t ptid);
+
+  /* This maps a TID to the CPU on which it was running.  This is
+     needed because sometimes the runtime will report an active task
+     that hasn't yet been put on the list of tasks that is read by
+     ada-tasks.c.  */
+  std::unordered_map<long, int> m_cpu_map;
 };
 
 /* Return true iff PTID corresponds to a ravenscar task.  */
@@ -156,8 +165,8 @@ is_ravenscar_task (ptid_t ptid)
    This assume that PTID is a valid ptid_t.  Otherwise, a gdb_assert
    will be triggered.  */
 
-static int
-ravenscar_get_thread_base_cpu (ptid_t ptid)
+int
+ravenscar_thread_target::get_thread_base_cpu (ptid_t ptid)
 {
   int base_cpu;
 
@@ -165,8 +174,15 @@ ravenscar_get_thread_base_cpu (ptid_t ptid)
     {
       struct ada_task_info *task_info = ada_get_task_info_from_ptid (ptid);
 
-      gdb_assert (task_info != NULL);
-      base_cpu = task_info->base_cpu;
+      if (task_info != NULL)
+	base_cpu = task_info->base_cpu;
+      else
+	{
+	  auto iter = m_cpu_map.find (ptid.tid ());
+
+	  gdb_assert (iter != m_cpu_map.end ());
+	  base_cpu = iter->second;
+	}
     }
   else
     {
@@ -190,8 +206,7 @@ ravenscar_get_thread_base_cpu (ptid_t ptid)
 bool
 ravenscar_thread_target::task_is_currently_active (ptid_t ptid)
 {
-  ptid_t active_task_ptid
-    = active_task (ravenscar_get_thread_base_cpu (ptid));
+  ptid_t active_task_ptid = active_task (get_thread_base_cpu (ptid));
 
   return ptid == active_task_ptid;
 }
@@ -202,15 +217,15 @@ ravenscar_thread_target::task_is_currently_active (ptid_t ptid)
    This is the thread that corresponds to the CPU on which the task
    is running.  */
 
-static ptid_t
-get_base_thread_from_ravenscar_task (ptid_t ptid)
+ptid_t
+ravenscar_thread_target::get_base_thread_from_ravenscar_task (ptid_t ptid)
 {
   int base_cpu;
 
   if (!is_ravenscar_task (ptid))
     return ptid;
 
-  base_cpu = ravenscar_get_thread_base_cpu (ptid);
+  base_cpu = get_thread_base_cpu (ptid);
   return ptid_t (ptid.pid (), base_cpu, 0);
 }
 
@@ -227,7 +242,7 @@ ravenscar_thread_target::add_active_thread ()
   int base_cpu;
 
   gdb_assert (!is_ravenscar_task (m_base_ptid));
-  base_cpu = ravenscar_get_thread_base_cpu (m_base_ptid);
+  base_cpu = get_thread_base_cpu (m_base_ptid);
 
   if (!runtime_initialized ())
     return nullptr;
@@ -242,7 +257,10 @@ ravenscar_thread_target::add_active_thread ()
      may not always add it to the thread list.  Add it here.  */
   thread_info *active_thr = find_thread_ptid (proc_target, active_ptid);
   if (active_thr == nullptr)
-    active_thr = add_thread (proc_target, active_ptid);
+    {
+      active_thr = ::add_thread (proc_target, active_ptid);
+      m_cpu_map[active_ptid.tid ()] = base_cpu;
+    }
   return active_thr;
 }
 
-- 
2.26.2


  parent reply	other threads:[~2020-08-05 19:08 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-05 19:08 [PATCH v2 00/12] Fix Ravenscar regressions Tom Tromey
2020-08-05 19:08 ` [PATCH v2 01/12] Call add_active_thread after pushing the ravenscar target Tom Tromey
2020-08-05 19:08 ` [PATCH v2 02/12] Avoid crash in ravenscar_thread_target::wait Tom Tromey
2020-08-05 19:08 ` [PATCH v2 03/12] Return event_ptid from ravenscar_thread_target::wait Tom Tromey
2020-08-05 19:08 ` Tom Tromey [this message]
2020-08-05 19:08 ` [PATCH v2 05/12] Change names given to Ravenscar threads Tom Tromey
2020-08-05 19:08 ` [PATCH v2 06/12] Use gdb::function_view in iterate_over_live_ada_tasks Tom Tromey
2020-08-05 19:08 ` [PATCH v2 07/12] Wrap xfer_partial and enable_btrace for Ravenscar Tom Tromey
2020-08-05 19:08 ` [PATCH v2 08/12] Update Ravenscar documentation Tom Tromey
2020-08-05 19:08 ` [PATCH v2 09/12] Fix Ravenscar "process" resume Tom Tromey
2020-08-05 19:08 ` [PATCH v2 10/12] Fetch registers from correct thread in ravenscar-thread.c Tom Tromey
2020-08-05 19:08 ` [PATCH v2 11/12] Set inferior_ptid in ravenscar_thread_target::update_thread_list Tom Tromey
2020-08-05 19:08 ` [PATCH v2 12/12] Fix remaining Ravenscar regressions Tom Tromey
2020-08-07 15:03 ` [PATCH v2 00/12] Fix " Joel Brobecker
2020-08-07 16:26   ` Tom Tromey
2020-08-07 17:11   ` Eli Zaretskii

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=20200805190841.2506771-5-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /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).