binutils-gdb/gdbserver/tdesc.cc
Andrew Burgess 0e26741636 gdb/riscv: delete target descriptions when gdb exits
It was pointed out on IRC that the RISC-V target allocates target
descriptions and stores them in a global map, and doesn't delete these
target descriptions when GDB shuts down.

This isn't a particular problem, the total number of target
descriptions we can create is very limited so creating these on demand
and holding them for the entire run on GDB seems reasonable.

However, not deleting these objects on GDB exit means extra warnings
are printed from tools like valgrind, and the address sanitiser,
making it harder to spot real issues.  As it's reasonably easy to have
GDB correctly delete these objects on exit, lets just do that.

I started by noticing that we already have a target_desc_up type, a
wrapper around unique_ptr that calls a function that will correctly
delete target descriptions, so I want to use that, but....

...that type is declared in gdb/target-descriptions.h.  If I try to
include that file in gdb/arch/riscv.c I run into a problem, that file
is compiled into both GDB and GDBServer.

OK, I could guard the include with #ifdef, but surely we can do
better.

So then I decided to move the target_desc_up type into
gdbsupport/tdesc.h, this is the interface file for generic code shared
between GDB and GDBserver (relating to target descriptions).  The
actual implementation for the delete function still lives in
gdb/target-description.c, but now gdb/arch/riscv.c can see the
declaration.  Problem solved....

... but, though RISC-V doesn't use it I've now exposed the
target_desc_up type to gdbserver, so in future someone _might_ start
using it, which is fine, except right now there's no definition of the
delete function - remember the delete I used is only defined in GDB
code.

No problem, I add an implementation of the delete operator into
gdbserver/tdesc.cc, and all is good..... except....

I start getting this error from GCC:

  tdesc.cc:109:10: error: deleting object of polymorphic class type ‘target_desc’ which has non-virtual destructor might cause undefined behavior [-Werror=delete-non-virtual-dtor]

Which is caused because gdbserver's target_desc type inherits from
tdesc_element which has a virtual method, and so GCC worries that
target_desc might be used as a base class.

The solution is to declare gdbserver's target_desc class as final.
This is fine so long as we never intent to inherit from
target_desc (in gdbserver).  But if we did then we'd want to make
target_desc's destructor virtual anyway, so the error above would be
resolved, and there wouldn't be an issue.

gdb/ChangeLog:

	* arch/riscv.c (riscv_tdesc_cache): Change map type.
	(riscv_lookup_target_description): Return pointer out of
	unique_ptr.
	* target-descriptions.c (allocate_target_description): Add
	comment.
	(target_desc_deleter::operator()): Likewise.
	* target-descriptions.h (struct target_desc_deleter): Moved to
	gdbsupport/tdesc.h.
	(target_desc_up): Likewise.

gdbserver/ChangeLog:

	* tdesc.cc (allocate_target_description): Add header comment.
	(target_desc_deleter::operator()): New function.
	* tdesc.h (struct target_desc): Declare as final.

gdbsupport/ChangeLog:

	* tdesc.h (struct target_desc_deleter): Moved here
	from gdb/target-descriptions.h, extend comment.
	(target_desc_up): Likewise.
2020-07-17 21:15:32 +01:00

236 lines
5.1 KiB
C++

/* Copyright (C) 2012-2020 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "server.h"
#include "tdesc.h"
#include "regdef.h"
#ifndef IN_PROCESS_AGENT
target_desc::~target_desc ()
{
xfree ((char *) arch);
xfree ((char *) osabi);
}
bool target_desc::operator== (const target_desc &other) const
{
if (reg_defs != other.reg_defs)
return false;
/* Compare expedite_regs. */
int i = 0;
for (; expedite_regs[i] != NULL; i++)
{
if (strcmp (expedite_regs[i], other.expedite_regs[i]) != 0)
return false;
}
if (other.expedite_regs[i] != NULL)
return false;
return true;
}
#endif
void target_desc::accept (tdesc_element_visitor &v) const
{
#ifndef IN_PROCESS_AGENT
v.visit_pre (this);
for (const tdesc_feature_up &feature : features)
feature->accept (v);
v.visit_post (this);
#endif
}
void
init_target_desc (struct target_desc *tdesc,
const char **expedite_regs)
{
int offset = 0;
/* Go through all the features and populate reg_defs. */
for (const tdesc_feature_up &feature : tdesc->features)
for (const tdesc_reg_up &treg : feature->registers)
{
int regnum = treg->target_regnum;
/* Register number will increase (possibly with gaps) or be zero. */
gdb_assert (regnum == 0 || regnum >= tdesc->reg_defs.size ());
if (regnum != 0)
tdesc->reg_defs.resize (regnum, gdb::reg (offset));
tdesc->reg_defs.emplace_back (treg->name.c_str (), offset,
treg->bitsize);
offset += treg->bitsize;
}
tdesc->registers_size = offset / 8;
/* Make sure PBUFSIZ is large enough to hold a full register
packet. */
gdb_assert (2 * tdesc->registers_size + 32 <= PBUFSIZ);
#ifndef IN_PROCESS_AGENT
tdesc->expedite_regs = expedite_regs;
#endif
}
/* See gdbsupport/tdesc.h. */
struct target_desc *
allocate_target_description (void)
{
return new target_desc ();
}
/* See gdbsupport/tdesc.h. */
void
target_desc_deleter::operator() (struct target_desc *target_desc) const
{
delete target_desc;
}
#ifndef IN_PROCESS_AGENT
static const struct target_desc default_description {};
void
copy_target_description (struct target_desc *dest,
const struct target_desc *src)
{
dest->reg_defs = src->reg_defs;
dest->expedite_regs = src->expedite_regs;
dest->registers_size = src->registers_size;
dest->xmltarget = src->xmltarget;
}
const struct target_desc *
current_target_desc (void)
{
if (current_thread == NULL)
return &default_description;
return current_process ()->tdesc;
}
/* An empty structure. */
struct tdesc_compatible_info { };
/* See gdbsupport/tdesc.h. */
const std::vector<tdesc_compatible_info_up> &
tdesc_compatible_info_list (const target_desc *target_desc)
{
static std::vector<tdesc_compatible_info_up> empty;
return empty;
}
/* See gdbsupport/tdesc.h. */
const char *
tdesc_compatible_info_arch_name (const tdesc_compatible_info_up &c_info)
{
return nullptr;
}
/* See gdbsupport/tdesc.h. */
const char *
tdesc_architecture_name (const struct target_desc *target_desc)
{
return target_desc->arch;
}
/* See gdbsupport/tdesc.h. */
void
set_tdesc_architecture (struct target_desc *target_desc,
const char *name)
{
target_desc->arch = xstrdup (name);
}
/* See gdbsupport/tdesc.h. */
const char *
tdesc_osabi_name (const struct target_desc *target_desc)
{
return target_desc->osabi;
}
/* See gdbsupport/tdesc.h. */
void
set_tdesc_osabi (struct target_desc *target_desc, const char *name)
{
target_desc->osabi = xstrdup (name);
}
/* See gdbsupport/tdesc.h. */
const char *
tdesc_get_features_xml (const target_desc *tdesc)
{
/* Either .xmltarget or .features is not NULL. */
gdb_assert (tdesc->xmltarget != NULL
|| (!tdesc->features.empty ()
&& tdesc->arch != NULL));
if (tdesc->xmltarget == NULL)
{
std::string buffer ("@");
print_xml_feature v (&buffer);
tdesc->accept (v);
tdesc->xmltarget = xstrdup (buffer.c_str ());
}
return tdesc->xmltarget;
}
#endif
/* See gdbsupport/tdesc.h. */
struct tdesc_feature *
tdesc_create_feature (struct target_desc *tdesc, const char *name)
{
struct tdesc_feature *new_feature = new tdesc_feature (name);
tdesc->features.emplace_back (new_feature);
return new_feature;
}
/* See gdbsupport/tdesc.h. */
bool
tdesc_contains_feature (const target_desc *tdesc, const std::string &feature)
{
gdb_assert (tdesc != nullptr);
for (const tdesc_feature_up &f : tdesc->features)
{
if (f->name == feature)
return true;
}
return false;
}