[25/26] gdbserver: refuse null argument in regcache::supply_regblock

Message ID 39a4774140afc2f7253756971398fc2cabceb289.1677582745.git.tankut.baris.aktemur@intel.com
State New
Headers
Series gdbserver: refactor regcache and allow gradually populating |

Commit Message

Aktemur, Tankut Baris Feb. 28, 2023, 11:28 a.m. UTC
  The regcache::supply_regblock method implements two different
behaviors based on whether its 'buf' argument is null.  The null
behavior is used only in tracepoint.cc.  Refuse the null pointer
argument and use the 'discard' method to obtain that second behavior.

Note that the original code resets register statuses to
REG_UNAVAILABLE, but 'discard' sets them to REG_UNKNOWN.  For the
current purposes of resetting the regcache, the difference does not
seem to be important.
---
 gdbserver/regcache.cc   | 19 +++++--------------
 gdbserver/regcache.h    |  3 +--
 gdbserver/tracepoint.cc |  8 ++++----
 3 files changed, 10 insertions(+), 20 deletions(-)
  

Comments

Simon Marchi Dec. 22, 2023, 4:54 a.m. UTC | #1
On 2023-02-28 06:28, Tankut Baris Aktemur via Gdb-patches wrote:
> The regcache::supply_regblock method implements two different
> behaviors based on whether its 'buf' argument is null.  The null
> behavior is used only in tracepoint.cc.  Refuse the null pointer
> argument and use the 'discard' method to obtain that second behavior.
> 
> Note that the original code resets register statuses to
> REG_UNAVAILABLE, but 'discard' sets them to REG_UNKNOWN.  For the
> current purposes of resetting the regcache, the difference does not
> seem to be important.

Ah, glad to see this, it matches one of my comment on an earlier patch
(and as you can see, I read patch series in a very linear way with no
lookahead).

In the spots inside the in-process agent, the regcache doesn't even
track statuses, so it doesn't make a difference indeed.

> diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
> index 9833e7c3b0f..9622d53cd82 100644
> --- a/gdbserver/tracepoint.cc
> +++ b/gdbserver/tracepoint.cc
> @@ -4707,7 +4707,7 @@ get_context_regcache (struct tracepoint_hit_ctx *ctx)
>  	{
>  	  fctx->regcache_initted = 1;
>  	  fctx->regcache.initialize (ipa_tdesc, fctx->regspace);
> -	  fctx->regcache.supply_regblock (nullptr);
> +	  fctx->regcache.discard ();
>  	  supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);

I think the call to discard is pointless.  The call to discard does (and
should) pretty much bring the regcache back to its state just after
initialization.  In this case, it was initialized at the line just before.

>  	}
>        regcache = &fctx->regcache;
> @@ -4722,7 +4722,7 @@ get_context_regcache (struct tracepoint_hit_ctx *ctx)
>  	{
>  	  sctx->regcache_initted = 1;
>  	  sctx->regcache.initialize (ipa_tdesc, sctx->regspace);
> -	  sctx->regcache.supply_regblock (nullptr);
> +	  sctx->regcache.discard ();

Same here.

>  	  /* 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,
> @@ -5179,8 +5179,8 @@ fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
>    dataptr = traceframe_find_regblock (tframe, tfnum);
>    if (dataptr == NULL)
>      {
> -      /* Mark registers unavailable.  */
> -      regcache->supply_regblock (nullptr);
> +      /* Wipe out the cache.  */
> +      regcache->discard ();

So, I think this is a spot where you would actually want to make the
registers REG_UNAVAILABLE.  We are filling a regcache meant to represent
the registers that were collected when hitting a given tracepoint.  If
traceframe_find_regblock returns nullptr, I guess it means that we
didn't collect the registers.  So all registers are unavailable.

In practice, it won't make a difference, because that regcache is then
fed to registers_to_string, which will have the same behavior for
REG_UNAVAILABLE and REG_UNKNOWN.  But if you want to be accurate, I
think that REG_UNAVAILABLE makes more sense here.

Simon
  

Patch

diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index 4533e9e9b12..cfb68774402 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -390,22 +390,13 @@  supply_register_by_name_zeroed (struct regcache *regcache,
 void
 regcache::supply_regblock (const void *buf)
 {
-  if (buf != nullptr)
-    {
-      memcpy (registers, buf, tdesc->registers_size);
-#ifndef IN_PROCESS_AGENT
-      for (int i = 0; i < tdesc->reg_defs.size (); i++)
-	set_register_status (i, REG_VALID);
-#endif
-    }
-  else
-    {
-      memset (registers, 0, tdesc->registers_size);
+  gdb_assert (buf != nullptr);
+
+  memcpy (registers, buf, tdesc->registers_size);
 #ifndef IN_PROCESS_AGENT
-      for (int i = 0; i < tdesc->reg_defs.size (); i++)
-	set_register_status (i, REG_UNAVAILABLE);
+  for (int i = 0; i < tdesc->reg_defs.size (); i++)
+    set_register_status (i, REG_VALID);
 #endif
-    }
 }
 
 #ifndef IN_PROCESS_AGENT
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index be412bc3765..216044889ec 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -103,8 +103,7 @@  struct regcache : public reg_buffer_common
   void registers_from_string (const char *buf);
 
   /* Supply the whole register set whose contents are stored in BUF,
-     to this regcache.  If BUF is NULL, all the registers' values are
-     recorded as unavailable.  */
+     to this regcache.  BUF cannot be NULL.  */
   void supply_regblock (const void *buf);
 
   /* Return the pointer to the register with number REGNUM.  */
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 9833e7c3b0f..9622d53cd82 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -4707,7 +4707,7 @@  get_context_regcache (struct tracepoint_hit_ctx *ctx)
 	{
 	  fctx->regcache_initted = 1;
 	  fctx->regcache.initialize (ipa_tdesc, fctx->regspace);
-	  fctx->regcache.supply_regblock (nullptr);
+	  fctx->regcache.discard ();
 	  supply_fast_tracepoint_registers (&fctx->regcache, fctx->regs);
 	}
       regcache = &fctx->regcache;
@@ -4722,7 +4722,7 @@  get_context_regcache (struct tracepoint_hit_ctx *ctx)
 	{
 	  sctx->regcache_initted = 1;
 	  sctx->regcache.initialize (ipa_tdesc, sctx->regspace);
-	  sctx->regcache.supply_regblock (nullptr);
+	  sctx->regcache.discard ();
 	  /* 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,
@@ -5179,8 +5179,8 @@  fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
   dataptr = traceframe_find_regblock (tframe, tfnum);
   if (dataptr == NULL)
     {
-      /* Mark registers unavailable.  */
-      regcache->supply_regblock (nullptr);
+      /* Wipe out the cache.  */
+      regcache->discard ();
 
       /* We can generally guess at a PC, although this will be
 	 misleading for while-stepping frames and multi-location