From 953cff563058831ab0bf863c7655d23b5e6a5989 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 15 Oct 2019 10:57:40 -0600 Subject: [PATCH] Change gcc_target_options to return std::string This patch was inspired by a recent review that recommended using std::string in a new implementation of the gcc_target_options gdbarch function. It changes this function to return std::string rather than an ordinary xmalloc'd string. I believe this caught a latent memory leak in compile.c:get_args. Tested on x86-64 Fedora 29. gdb/ChangeLog 2019-10-15 Tom Tromey * gdbarch.h, gdbarch.c: Rebuild. * gdbarch.sh (gcc_target_options): Change return type to std::string. * compile/compile.c (get_args): Update. * nios2-tdep.c (nios2_gcc_target_options): Return std::string. * arm-linux-tdep.c (arm_linux_gcc_target_options): Return std::string. * aarch64-linux-tdep.c (aarch64_linux_gcc_target_options): Return std::string. * arch-utils.c (default_gcc_target_options): Return std::string. * arch-utils.h (default_gcc_target_options): Return std::string. * s390-tdep.c (s390_gcc_target_options): Return std::string. Change-Id: I51f61703426a323089e646da8f22320a2cafbc1f --- gdb/ChangeLog | 15 +++++++++++++++ gdb/aarch64-linux-tdep.c | 4 ++-- gdb/arch-utils.c | 7 ++++--- gdb/arch-utils.h | 2 +- gdb/arm-linux-tdep.c | 4 ++-- gdb/compile/compile.c | 2 +- gdb/gdbarch.c | 2 +- gdb/gdbarch.h | 6 +++--- gdb/gdbarch.sh | 4 ++-- gdb/nios2-tdep.c | 4 ++-- gdb/s390-tdep.c | 4 ++-- 11 files changed, 35 insertions(+), 19 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 9d3ec1fae5f..e52ec3f307c 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,18 @@ +2019-10-15 Tom Tromey + + * gdbarch.h, gdbarch.c: Rebuild. + * gdbarch.sh (gcc_target_options): Change return type to + std::string. + * compile/compile.c (get_args): Update. + * nios2-tdep.c (nios2_gcc_target_options): Return std::string. + * arm-linux-tdep.c (arm_linux_gcc_target_options): Return + std::string. + * aarch64-linux-tdep.c (aarch64_linux_gcc_target_options): Return + std::string. + * arch-utils.c (default_gcc_target_options): Return std::string. + * arch-utils.h (default_gcc_target_options): Return std::string. + * s390-tdep.c (s390_gcc_target_options): Return std::string. + 2019-10-15 Christian Biesinger * breakpoint.c (breakpoint_chain): Make static. diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c index a375c3b137d..3ec08d1c76e 100644 --- a/gdb/aarch64-linux-tdep.c +++ b/gdb/aarch64-linux-tdep.c @@ -1429,11 +1429,11 @@ aarch64_linux_syscall_record (struct regcache *regcache, /* Implement the "gcc_target_options" gdbarch method. */ -static char * +static std::string aarch64_linux_gcc_target_options (struct gdbarch *gdbarch) { /* GCC doesn't know "-m64". */ - return NULL; + return {}; } static void diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index c61fa6f0519..571646e4c8d 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -903,11 +903,12 @@ default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size) /* -mcmodel=large is used so that no GOT (Global Offset Table) is needed to be created in inferior memory by GDB (normally it is set by ld.so). */ -char * +std::string default_gcc_target_options (struct gdbarch *gdbarch) { - return xstrprintf ("-m%d%s", gdbarch_ptr_bit (gdbarch), - gdbarch_ptr_bit (gdbarch) == 64 ? " -mcmodel=large" : ""); + return string_printf ("-m%d%s", gdbarch_ptr_bit (gdbarch), + (gdbarch_ptr_bit (gdbarch) == 64 + ? " -mcmodel=large" : "")); } /* gdbarch gnu_triplet_regexp method. */ diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h index e5bbcd1f951..48ff3bb9a16 100644 --- a/gdb/arch-utils.h +++ b/gdb/arch-utils.h @@ -247,7 +247,7 @@ extern void default_skip_permanent_breakpoint (struct regcache *regcache); extern CORE_ADDR default_infcall_mmap (CORE_ADDR size, unsigned prot); extern void default_infcall_munmap (CORE_ADDR addr, CORE_ADDR size); -extern char *default_gcc_target_options (struct gdbarch *gdbarch); +extern std::string default_gcc_target_options (struct gdbarch *gdbarch); extern const char *default_gnu_triplet_regexp (struct gdbarch *gdbarch); extern int default_addressable_memory_unit_size (struct gdbarch *gdbarch); diff --git a/gdb/arm-linux-tdep.c b/gdb/arm-linux-tdep.c index 54ea8511cba..32c1ef3a770 100644 --- a/gdb/arm-linux-tdep.c +++ b/gdb/arm-linux-tdep.c @@ -1710,11 +1710,11 @@ arm_linux_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc) /* Implement the gcc_target_options gdbarch method. */ -static char * +static std::string arm_linux_gcc_target_options (struct gdbarch *gdbarch) { /* GCC doesn't know "-m32". */ - return NULL; + return {}; } static void diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c index 5c3400ffec8..94942db10e1 100644 --- a/gdb/compile/compile.c +++ b/gdb/compile/compile.c @@ -635,7 +635,7 @@ get_args (const compile_instance *compiler, struct gdbarch *gdbarch, int argc_compiler; char **argv_compiler; - build_argc_argv (gdbarch_gcc_target_options (gdbarch), + build_argc_argv (gdbarch_gcc_target_options (gdbarch).c_str (), argcp, argvp); cs_producer_options = get_selected_pc_producer_options (); diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c index 7b93d003a72..fa6be50730b 100644 --- a/gdb/gdbarch.c +++ b/gdb/gdbarch.c @@ -5043,7 +5043,7 @@ set_gdbarch_infcall_munmap (struct gdbarch *gdbarch, gdbarch->infcall_munmap = infcall_munmap; } -char * +std::string gdbarch_gcc_target_options (struct gdbarch *gdbarch) { gdb_assert (gdbarch != NULL); diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h index 3c6efae895f..01b5aef8f80 100644 --- a/gdb/gdbarch.h +++ b/gdb/gdbarch.h @@ -1596,10 +1596,10 @@ extern void set_gdbarch_infcall_munmap (struct gdbarch *gdbarch, gdbarch_infcall /* Return string (caller has to use xfree for it) with options for GCC to produce code for this target, typically "-m64", "-m32" or "-m31". These options are put before CU's DW_AT_producer compilation options so that - they can override it. Method may also return NULL. */ + they can override it. */ -typedef char * (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch); -extern char * gdbarch_gcc_target_options (struct gdbarch *gdbarch); +typedef std::string (gdbarch_gcc_target_options_ftype) (struct gdbarch *gdbarch); +extern std::string gdbarch_gcc_target_options (struct gdbarch *gdbarch); extern void set_gdbarch_gcc_target_options (struct gdbarch *gdbarch, gdbarch_gcc_target_options_ftype *gcc_target_options); /* Return a regular expression that matches names used by this diff --git a/gdb/gdbarch.sh b/gdb/gdbarch.sh index d589b2c49a0..62f68dc8add 100755 --- a/gdb/gdbarch.sh +++ b/gdb/gdbarch.sh @@ -1184,8 +1184,8 @@ f;void;infcall_munmap;CORE_ADDR addr, CORE_ADDR size;addr, size;;default_infcall # Return string (caller has to use xfree for it) with options for GCC # to produce code for this target, typically "-m64", "-m32" or "-m31". # These options are put before CU's DW_AT_producer compilation options so that -# they can override it. Method may also return NULL. -m;char *;gcc_target_options;void;;;default_gcc_target_options;;0 +# they can override it. +m;std::string;gcc_target_options;void;;;default_gcc_target_options;;0 # Return a regular expression that matches names used by this # architecture in GNU configury triplets. The result is statically diff --git a/gdb/nios2-tdep.c b/gdb/nios2-tdep.c index cb17e4f3750..c2683b5e3a7 100644 --- a/gdb/nios2-tdep.c +++ b/gdb/nios2-tdep.c @@ -2260,11 +2260,11 @@ nios2_type_align (struct gdbarch *gdbarch, struct type *type) } /* Implement the gcc_target_options gdbarch method. */ -static char * +static std::string nios2_gcc_target_options (struct gdbarch *gdbarch) { /* GCC doesn't know "-m32". */ - return NULL; + return {}; } /* Initialize the Nios II gdbarch. */ diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c index 6bd0528cf44..329a1223963 100644 --- a/gdb/s390-tdep.c +++ b/gdb/s390-tdep.c @@ -6779,10 +6779,10 @@ UNKNOWN_OP: /* Implement gdbarch_gcc_target_options. GCC does not know "-m32" or "-mcmodel=large". */ -static char * +static std::string s390_gcc_target_options (struct gdbarch *gdbarch) { - return xstrdup (gdbarch_ptr_bit (gdbarch) == 64 ? "-m64" : "-m31"); + return gdbarch_ptr_bit (gdbarch) == 64 ? "-m64" : "-m31"; } /* Implement gdbarch_gnu_triplet_regexp. Target triplets are "s390-*" -- 2.30.2