[PATCH v2 04/11] gdbserver: convert init_register_cache and new_register_cache into constructors

Tankut Baris Aktemur
2024-12-30T10:54:26Z

This is a refactoring that converts

init_register_cache (struct regcache *regcache,
const struct target_desc *tdesc,
unsigned char *regbuf)

into the constructor

regcache (const target_desc *tdesc, unsigned char *regbuf)

and converts

new_register_cache (const struct target_desc *tdesc)

into the constructor

regcache (const target_desc *tdesc)

Also use DISABLE_COPY_AND_ASSIGN for additional compile-time safety.

Tested by rebuilding gdbserver with '--enable-inprocess-agent=no' and
with '--enable-inprocess-agent=yes'.
---
gdbserver/regcache.cc | 63 ++++++++++++++++++-------------------------------
gdbserver/regcache.h | 14 +++++------
gdbserver/server.cc | 2 +-
gdbserver/tracepoint.cc | 35 ++++++++++++---------------
4 files changed, 45 insertions(+), 69 deletions(-)

diff --git a/gdbserver/regcache.cc b/gdbserver/regcache.cc
index b5c3b7e9cbaf6c267bf1b4a3d3a5ecf4f8788135..11957af351d476d47eb4b9eacb810ac2f6aded40 100644
--- a/gdbserver/regcache.cc
+++ b/gdbserver/regcache.cc
@@ -42,7 +42,7 @@ get_thread_regcache (thread_info *thread, bool fetch)

gdb_assert (proc->tdesc != NULL);

- regcache = new_register_cache (proc->tdesc);
+ regcache = new struct regcache (proc->tdesc);
thread->set_regcache (regcache);
}

@@ -112,55 +112,38 @@ regcache_invalidate (void)

#endif

-struct regcache *
-init_register_cache (struct regcache *regcache,
- const struct target_desc *tdesc,
- unsigned char *regbuf)
+regcache::regcache (const target_desc *tdesc,
+ unsigned char *regbuf)
{
- if (regbuf == NULL)
- {
-#ifndef IN_PROCESS_AGENT
- /* Make sure to zero-initialize the register cache when it is
- created, in case there are registers the target never
- fetches. This way they'll read as zero instead of
- garbage. */
- regcache->tdesc = tdesc;
- regcache->registers
- = (unsigned char *) xcalloc (1, tdesc->registers_size);
- regcache->registers_owned = true;
- regcache->register_status
- = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
- memset ((void *) regcache->register_status, REG_UNAVAILABLE,
- tdesc->reg_defs.size ());
-#else
- gdb_assert_not_reached ("can't allocate memory from the heap");
-#endif
- }
- else
- {
- regcache->tdesc = tdesc;
- regcache->registers = regbuf;
- regcache->registers_owned = false;
+ gdb_assert (regbuf != nullptr);
+
+ this->tdesc = tdesc;
+ this->registers = regbuf;
+ this->registers_owned = false;
#ifndef IN_PROCESS_AGENT
- regcache->register_status = NULL;
+ this->register_status = nullptr;
#endif
- }
-
- regcache->registers_fetched = false;
-
- return regcache;
+ this->registers_fetched = false;
}

#ifndef IN_PROCESS_AGENT

-struct regcache *
-new_register_cache (const struct target_desc *tdesc)
+regcache::regcache (const target_desc *tdesc)
{
- struct regcache *regcache = new struct regcache;
-
gdb_assert (tdesc->registers_size != 0);

- return init_register_cache (regcache, tdesc, NULL);
+ /* Make sure to zero-initialize the register cache when it is
+ created, in case there are registers the target never
+ fetches. This way they'll read as zero instead of
+ garbage. */
+ this->tdesc = tdesc;
+ this->registers
+ = (unsigned char *) xcalloc (1, tdesc->registers_size);
+ this->registers_owned = true;
+ this->register_status
+ = (unsigned char *) xmalloc (tdesc->reg_defs.size ());
+ memset ((void *) this->register_status, REG_UNAVAILABLE,
+ tdesc->reg_defs.size ());
}

void
diff --git a/gdbserver/regcache.h b/gdbserver/regcache.h
index 12345a3439cd7dedc70cd06f13450dde58214124..ea6c26a37a07bf6d336d6bdf327d543fae0b6370 100644
--- a/gdbserver/regcache.h
+++ b/gdbserver/regcache.h
@@ -45,7 +45,13 @@ struct regcache : public reg_buffer_common
#ifndef IN_PROCESS_AGENT
/* One of REG_UNAVAILABLE or REG_VALID. */
unsigned char *register_status = nullptr;
+
+ /* Constructors. */
+ regcache (const target_desc *tdesc);
#endif
+ regcache (const target_desc *tdesc, unsigned char *regbuf);
+
+ DISABLE_COPY_AND_ASSIGN (regcache);

/* See gdbsupport/common-regcache.h. */
enum register_status get_register_status (int regnum) const override;
@@ -66,14 +72,6 @@ struct regcache : public reg_buffer_common
void copy_from (regcache *src);
};

-struct regcache *init_register_cache (struct regcache *regcache,
- const struct target_desc *tdesc,
- unsigned char *regbuf);
-
-/* Create a new register cache for INFERIOR. */
-
-struct regcache *new_register_cache (const struct target_desc *tdesc);
-
regcache *get_thread_regcache (thread_info *thread, bool fetch = true);

/* Release all memory associated with the register cache for INFERIOR. */
diff --git a/gdbserver/server.cc b/gdbserver/server.cc
index 55898f59556b2d16bebe319e48321cdfb0cc9f89..a4d654de00adf014e28b0ce022ba8a5886ee77ce 100644
--- a/gdbserver/server.cc
+++ b/gdbserver/server.cc
@@ -4704,7 +4704,7 @@ process_serial_event (void)
if (cs.current_traceframe >= 0)
{
struct regcache *regcache
- = new_register_cache (current_target_desc ());
+ = new struct regcache (current_target_desc ());

if (fetch_traceframe_registers (cs.current_traceframe,
regcache, -1) == 0)
diff --git a/gdbserver/tracepoint.cc b/gdbserver/tracepoint.cc
index 1b6e9d05273ea7a709f1880473c070c59169ddd5..9a00d1f272f905cadbc87265ed0be3d87f7b54df 100644
--- a/gdbserver/tracepoint.cc
+++ b/gdbserver/tracepoint.cc
@@ -1288,7 +1288,7 @@ struct tracepoint_hit_ctx
struct fast_tracepoint_ctx : public tracepoint_hit_ctx
{
fast_tracepoint_ctx (unsigned char *_regs)
- : regcache_initted (0), regs (_regs)
+ : regcache (), regs (_regs)
{
}

@@ -1296,8 +1296,7 @@ struct fast_tracepoint_ctx : public tracepoint_hit_ctx

/* The regcache corresponding to the registers state at the time of
the tracepoint hit. Initialized lazily, from REGS. */
- struct regcache regcache;
- int regcache_initted;
+ std::optional<struct regcache> regcache;

/* The buffer space REGCACHE above uses. We use a separate buffer
instead of letting the regcache malloc for both signal safety and
@@ -4695,15 +4694,15 @@ fast_tracepoint_ctx::get_regcache ()
{
const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);

- if (!this->regcache_initted)
+ if (!this->regcache.has_value ())
{
- 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);
+ this->regcache.emplace (ipa_tdesc, this->regspace);
+ supply_regblock (&this->regcache.value (), nullptr);
+ supply_fast_tracepoint_registers (&this->regcache.value (),
+ this->regs);
}

- return &this->regcache;
+ return &this->regcache.value ();
}

struct regcache *
@@ -4712,20 +4711,19 @@ static_tracepoint_ctx::get_regcache ()
#ifdef HAVE_UST
const struct target_desc *ipa_tdesc = get_ipa_tdesc (ipa_tdesc_idx);

- if (!this->regcache_initted)
+ if (!this->regcache.has_value ())
{
- this->regcache_initted = 1;
- init_register_cache (&this->regcache, ipa_tdesc, this->regspace);
- supply_regblock (&this->regcache, nullptr);
+ this->regcache.emplace (ipa_tdesc, this->regspace);
+ supply_regblock (&this->regcache.value (), 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,
+ supply_static_tracepoint_registers (&this->regcache.value (),
(const unsigned char *)
this->regs,
this->tpoint->address);
}

- return &this->regcache;
+ return &this->regcache.value ();
#else
gdb_assert_not_reached ("static tracepoint feature requires UST");
#endif
@@ -4773,7 +4771,6 @@ do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,
case 'R':
{
unsigned char *regspace;
- struct regcache tregcache;
int regcache_size;

trace_debug ("Want to collect registers");
@@ -4793,8 +4790,7 @@ do_action_at_tracepoint (struct tracepoint_hit_ctx *ctx,

/* Wrap the regblock in a register cache (in the stack, we
don't want to malloc here). */
- init_register_cache (&tregcache, context_regcache->tdesc,
- regspace + 1);
+ regcache tregcache (context_regcache->tdesc, regspace + 1);

/* Copy the register data to the regblock. */
tregcache.copy_from (context_regcache);
@@ -5190,7 +5186,6 @@ fetch_traceframe_registers (int tfnum, struct regcache *regcache, int regnum)
static CORE_ADDR
traceframe_get_pc (struct traceframe *tframe)
{
- struct regcache regcache;
unsigned char *dataptr;
const struct target_desc *tdesc = current_target_desc ();

@@ -5198,7 +5193,7 @@ traceframe_get_pc (struct traceframe *tframe)
if (dataptr == NULL)
return 0;

- init_register_cache (&regcache, tdesc, dataptr);
+ regcache regcache (tdesc, dataptr);
return regcache_read_pc (&regcache);
}

--
2.34.1

Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva