binutils-gdb/gdb/arch
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
..
aarch32.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
aarch32.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
aarch64-insn.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
aarch64-insn.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
aarch64.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
aarch64.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
amd64.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
amd64.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
arc.c arc: Migrate to new target features 2020-03-16 22:53:10 +01:00
arc.h arc: Migrate to new target features 2020-03-16 22:53:10 +01:00
arm-get-next-pcs.c gnulib: import count-one-bits module and use it 2020-02-14 14:41:07 -05:00
arm-get-next-pcs.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
arm-linux.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
arm-linux.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
arm.c gnulib: import count-one-bits module and use it 2020-02-14 14:41:07 -05:00
arm.h gnulib: import count-one-bits module and use it 2020-02-14 14:41:07 -05:00
i386.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
i386.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
ppc-linux-common.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
ppc-linux-common.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
ppc-linux-tdesc.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
riscv.c gdb/riscv: delete target descriptions when gdb exits 2020-07-17 21:15:32 +01:00
riscv.h gdb/riscv: Update API for looking up target descriptions 2020-02-19 01:24:37 +00:00
tic6x.c Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
tic6x.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00
xtensa.h Update copyright year range in all GDB files. 2020-01-01 10:20:53 +04:00