mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-21 04:42:53 +08:00
0e26741636
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.
118 lines
3.4 KiB
C
118 lines
3.4 KiB
C
/* Copyright (C) 2018-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 "gdbsupport/common-defs.h"
|
|
#include "riscv.h"
|
|
#include <stdlib.h>
|
|
#include <unordered_map>
|
|
|
|
#include "../features/riscv/32bit-cpu.c"
|
|
#include "../features/riscv/64bit-cpu.c"
|
|
#include "../features/riscv/32bit-fpu.c"
|
|
#include "../features/riscv/64bit-fpu.c"
|
|
|
|
#ifndef GDBSERVER
|
|
#define STATIC_IN_GDB static
|
|
#else
|
|
#define STATIC_IN_GDB
|
|
#endif
|
|
|
|
/* See arch/riscv.h. */
|
|
|
|
STATIC_IN_GDB target_desc *
|
|
riscv_create_target_description (const struct riscv_gdbarch_features features)
|
|
{
|
|
/* Now we should create a new target description. */
|
|
target_desc *tdesc = allocate_target_description ();
|
|
|
|
#ifndef IN_PROCESS_AGENT
|
|
std::string arch_name = "riscv";
|
|
|
|
if (features.xlen == 4)
|
|
arch_name.append (":rv32i");
|
|
else if (features.xlen == 8)
|
|
arch_name.append (":rv64i");
|
|
else if (features.xlen == 16)
|
|
arch_name.append (":rv128i");
|
|
|
|
if (features.flen == 4)
|
|
arch_name.append ("f");
|
|
else if (features.flen == 8)
|
|
arch_name.append ("d");
|
|
else if (features.flen == 16)
|
|
arch_name.append ("q");
|
|
|
|
set_tdesc_architecture (tdesc, arch_name.c_str ());
|
|
#endif
|
|
|
|
long regnum = 0;
|
|
|
|
/* For now we only support creating 32-bit or 64-bit x-registers. */
|
|
if (features.xlen == 4)
|
|
regnum = create_feature_riscv_32bit_cpu (tdesc, regnum);
|
|
else if (features.xlen == 8)
|
|
regnum = create_feature_riscv_64bit_cpu (tdesc, regnum);
|
|
|
|
/* For now we only support creating 32-bit or 64-bit f-registers. */
|
|
if (features.flen == 4)
|
|
regnum = create_feature_riscv_32bit_fpu (tdesc, regnum);
|
|
else if (features.flen == 8)
|
|
regnum = create_feature_riscv_64bit_fpu (tdesc, regnum);
|
|
|
|
return tdesc;
|
|
}
|
|
|
|
#ifndef GDBSERVER
|
|
|
|
/* Wrapper used by std::unordered_map to generate hash for feature set. */
|
|
struct riscv_gdbarch_features_hasher
|
|
{
|
|
std::size_t
|
|
operator() (const riscv_gdbarch_features &features) const noexcept
|
|
{
|
|
return features.hash ();
|
|
}
|
|
};
|
|
|
|
/* Cache of previously seen target descriptions, indexed by the feature set
|
|
that created them. */
|
|
static std::unordered_map<riscv_gdbarch_features,
|
|
const target_desc_up,
|
|
riscv_gdbarch_features_hasher> riscv_tdesc_cache;
|
|
|
|
/* See arch/riscv.h. */
|
|
|
|
const target_desc *
|
|
riscv_lookup_target_description (const struct riscv_gdbarch_features features)
|
|
{
|
|
/* Lookup in the cache. If we find it then return the pointer out of
|
|
the target_desc_up (which is a unique_ptr). This is safe as the
|
|
riscv_tdesc_cache will exist until GDB exits. */
|
|
const auto it = riscv_tdesc_cache.find (features);
|
|
if (it != riscv_tdesc_cache.end ())
|
|
return it->second.get ();
|
|
|
|
target_desc *tdesc = riscv_create_target_description (features);
|
|
|
|
/* Add to the cache. */
|
|
riscv_tdesc_cache.emplace (features, tdesc);
|
|
|
|
return tdesc;
|
|
}
|
|
|
|
#endif /* !GDBSERVER */
|