[v2,03/11] gdbserver: use inheritance more to define tracepoint contexts

Message ID 20241230-upstream-gdbserver-regcache-v2-3-020a9514fcf0@intel.com
State New
Headers
Series gdbserver: refactor regcache |

Commit Message

Tankut Baris Aktemur Dec. 30, 2024, 10:49 a.m. UTC
  This is a continuation of the previous refactoring to use inheritance
in the definition of tracepoints contexts.  Again, no behavioral change
is intended.

Different tracepoint contexts are identified by the `type` field.  The
field is used only in `get_context_regcache`, where we essentially
have 3 cases, each corresponding to a tracepoint context type.  Remove
the `type` field and split the `get_context_regcache` function into 3
virtual method implementations.

Tested by rebuilding gdbserver with '--enable-inprocess-agent=no' and
'--enable-inprocess-agent=yes'.
---
 gdbserver/tracepoint.cc | 95 +++++++++++++++++++++++++------------------------
 1 file changed, 48 insertions(+), 47 deletions(-)
  

Comments

Simon Marchi Jan. 7, 2025, 4:40 a.m. UTC | #1
On 2024-12-30 05:49, Tankut Baris Aktemur wrote:
> This is a continuation of the previous refactoring to use inheritance
> in the definition of tracepoints contexts.  Again, no behavioral change
> is intended.
> 
> Different tracepoint contexts are identified by the `type` field.  The
> field is used only in `get_context_regcache`, where we essentially
> have 3 cases, each corresponding to a tracepoint context type.  Remove
> the `type` field and split the `get_context_regcache` function into 3
> virtual method implementations.
> 
> Tested by rebuilding gdbserver with '--enable-inprocess-agent=no' and
> '--enable-inprocess-agent=yes'.
> ---
>  gdbserver/tracepoint.cc | 95 +++++++++++++++++++++++++------------------------
>  1 file changed, 48 insertions(+), 47 deletions(-)
> 
> diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
> index fc0586a5c5ace6edc1fc72d24511c6f36e97f759..1b6e9d05273ea7a709f1880473c070c59169ddd5 100644
> --- a/gdbserver/tracepoint.cc
> +++ b/gdbserver/tracepoint.cc
> @@ -1278,7 +1278,7 @@ static char *tracing_stop_note;
>     collect_data_at_tracepoint.  */
>  struct tracepoint_hit_ctx
>  {
> -  enum tracepoint_type type;
> +  virtual struct regcache *get_regcache () = 0;

I would name this method just `regcache ()`, I try to push not to have
the "get_" prefix for getters.  Methods without a verb are implicitly
getters.

>  };
>  
>  #ifdef IN_PROCESS_AGENT
> @@ -1290,9 +1290,10 @@ struct fast_tracepoint_ctx : public tracepoint_hit_ctx
>    fast_tracepoint_ctx (unsigned char *_regs)
>      : regcache_initted (0), regs (_regs)
>    {
> -    type = fast_tracepoint;
>    }
>  
> +  virtual struct regcache *get_regcache () override;

I think you could move the implementation of those methods here, it's
all in the same .cc file anyway.  And it would help with readability,
given the mess of ifdefs below.

> +struct regcache *
> +static_tracepoint_ctx::get_regcache ()
> +{
>  #ifdef HAVE_UST
> -  if (ctx->type == static_tracepoint)
> -    {
> -      auto sctx = static_cast <static_tracepoint_ctx *> (ctx);
> +  const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
>  
> -      if (!sctx->regcache_initted)
> -	{
> -	  sctx->regcache_initted = 1;
> -	  init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
> -	  supply_regblock (&sctx->regcache, NULL);
> -	  /* Pass down the tracepoint address, because REGS doesn't
> -	     include the PC, but we know what it must have been.  */
> -	  supply_static_tracepoint_registers (&sctx->regcache,
> -					      (const unsigned char *)
> -					      sctx->regs,
> -					      sctx->tpoint->address);
> -	}
> -      regcache = &sctx->regcache;
> -    }
> -#endif
> -#else
> -  if (ctx->type == trap_tracepoint)
> +  if (!this->regcache_initted)
>      {
> -      auto tctx = static_cast<trap_tracepoint_ctx *> (ctx);
> -      regcache = tctx->regcache;
> +      this->regcache_initted = 1;
> +      init_register_cache (&this->regcache, ipa_tdesc, this->regspace);
> +      supply_regblock (&this->regcache, nullptr);
> +      /* Pass down the tracepoint address, because REGS doesn't
> +	 include the PC, but we know what it must have been.  */
> +      supply_static_tracepoint_registers (&this->regcache,
> +					  (const unsigned char *)
> +					  this->regs,
> +					  this->tpoint->address);
>      }
> +
> +  return &this->regcache;
> +#else
> +  gdb_assert_not_reached ("static tracepoint feature requires UST");
>  #endif

I think that static_tracepoint_ctx (and therefore this method) could
simply not exist at all if !HAVE_UST?  Although if you remove UST
support, the question won't matter anymore.

Simon
  

Patch

diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index fc0586a5c5ace6edc1fc72d24511c6f36e97f759..1b6e9d05273ea7a709f1880473c070c59169ddd5 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -1278,7 +1278,7 @@  static char *tracing_stop_note;
    collect_data_at_tracepoint.  */
 struct tracepoint_hit_ctx
 {
-  enum tracepoint_type type;
+  virtual struct regcache *get_regcache () = 0;
 };
 
 #ifdef IN_PROCESS_AGENT
@@ -1290,9 +1290,10 @@  struct fast_tracepoint_ctx : public tracepoint_hit_ctx
   fast_tracepoint_ctx (unsigned char *_regs)
     : regcache_initted (0), regs (_regs)
   {
-    type = fast_tracepoint;
   }
 
+  virtual struct regcache *get_regcache () override;
+
   /* The regcache corresponding to the registers state at the time of
      the tracepoint hit.  Initialized lazily, from REGS.  */
   struct regcache regcache;
@@ -1318,9 +1319,10 @@  struct static_tracepoint_ctx : public fast_tracepoint_ctx
 			 const char *_fmt, va_list *_args)
     : fast_tracepoint_ctx (_regs), fmt (_fmt), args (_args)
   {
-    type = static_tracepoint;
   }
 
+  virtual struct regcache *get_regcache () override;
+
   /* The "printf" formatter and the args the user passed to the marker
      call.  We use this to be able to collect "static trace data"
      ($_sdata).  */
@@ -1337,9 +1339,10 @@  struct trap_tracepoint_ctx : public tracepoint_hit_ctx
   trap_tracepoint_ctx (struct regcache *_regcache)
     : regcache (_regcache)
   {
-    type = trap_tracepoint;
   }
 
+  virtual struct regcache *get_regcache () override;
+
   struct regcache *regcache;
 };
 
@@ -4686,59 +4689,58 @@  IP_AGENT_EXPORT_VAR int ipa_tdesc_idx;
 }
 #endif
 
-static struct regcache *
-get_context_regcache (struct tracepoint_hit_ctx *ctx)
-{
-  struct regcache *regcache = NULL;
 #ifdef IN_PROCESS_AGENT
+struct regcache *
+fast_tracepoint_ctx::get_regcache ()
+{
   const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
 
-  if (ctx->type == fast_tracepoint)
+  if (!this->regcache_initted)
     {
-      auto fctx = static_cast<fast_tracepoint_ctx *> (ctx);
-
-      if (!fctx->regcache_initted)
-	{
-	  fctx->regcache_initted = 1;
-	  init_register_cache (&fctx->regcache, ipa_tdesc, fctx->regspace);
-	  supply_regblock (&fctx->regcache, NULL);
-	  supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
-	}
-      regcache = &fctx->regcache;
+      this->regcache_initted = 1;
+      init_register_cache (&this->regcache, ipa_tdesc, this->regspace);
+      supply_regblock (&this->regcache, nullptr);
+      supply_fast_tracepoint_registers (&this->regcache, this->regs);
     }
+
+  return &this->regcache;
+}
+
+struct regcache *
+static_tracepoint_ctx::get_regcache ()
+{
 #ifdef HAVE_UST
-  if (ctx->type == static_tracepoint)
-    {
-      auto sctx = static_cast <static_tracepoint_ctx *> (ctx);
+  const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);
 
-      if (!sctx->regcache_initted)
-	{
-	  sctx->regcache_initted = 1;
-	  init_register_cache (&sctx->regcache, ipa_tdesc, sctx->regspace);
-	  supply_regblock (&sctx->regcache, NULL);
-	  /* Pass down the tracepoint address, because REGS doesn't
-	     include the PC, but we know what it must have been.  */
-	  supply_static_tracepoint_registers (&sctx->regcache,
-					      (const unsigned char *)
-					      sctx->regs,
-					      sctx->tpoint->address);
-	}
-      regcache = &sctx->regcache;
-    }
-#endif
-#else
-  if (ctx->type == trap_tracepoint)
+  if (!this->regcache_initted)
     {
-      auto tctx = static_cast<trap_tracepoint_ctx *> (ctx);
-      regcache = tctx->regcache;
+      this->regcache_initted = 1;
+      init_register_cache (&this->regcache, ipa_tdesc, this->regspace);
+      supply_regblock (&this->regcache, nullptr);
+      /* Pass down the tracepoint address, because REGS doesn't
+	 include the PC, but we know what it must have been.  */
+      supply_static_tracepoint_registers (&this->regcache,
+					  (const unsigned char *)
+					  this->regs,
+					  this->tpoint->address);
     }
+
+  return &this->regcache;
+#else
+  gdb_assert_not_reached ("static tracepoint feature requires UST");
 #endif
+}
 
-  gdb_assert (regcache != NULL);
+#else
 
-  return regcache;
+struct regcache *
+trap_tracepoint_ctx::get_regcache ()
+{
+  return this->regcache;
 }
 
+#endif
+
 static void
 do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
 			 CORE_ADDR stop_pc,
@@ -4772,12 +4774,11 @@  do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
       {
 	unsigned char *regspace;
 	struct regcache tregcache;
-	struct regcache *context_regcache;
 	int regcache_size;
 
 	trace_debug ("Want to collect registers");
 
-	context_regcache = get_context_regcache (ctx);
+	regcache *context_regcache = ctx->get_regcache ();
 	regcache_size = register_cache_size (context_regcache->tdesc);
 
 	/* Collect all registers for now.  */
@@ -4824,7 +4825,7 @@  do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
 	struct eval_agent_expr_context ax_ctx;
 
 	eaction = (struct eval_expr_action *) taction;
-	ax_ctx.regcache = get_context_regcache (ctx);
+	ax_ctx.regcache = ctx->get_regcache ();
 	ax_ctx.tframe = tframe;
 	ax_ctx.tpoint = tpoint;
 
@@ -4888,7 +4889,7 @@  condition_true_at_tracepoint (struct tracepoint_hit_ctx *ctx,
     {
       struct eval_agent_expr_context ax_ctx;
 
-      ax_ctx.regcache = get_context_regcache (ctx);
+      ax_ctx.regcache = ctx->get_regcache ();
       ax_ctx.tframe = NULL;
       ax_ctx.tpoint = tpoint;