From: Philippe Waroquiers Date: Sat, 16 Feb 2019 13:11:38 +0000 (+0100) Subject: Fix regcache leak, and avoid possible regcache access after detach. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=799efbe8e01;p=binutils-gdb.git Fix regcache leak, and avoid possible regcache access after detach. Valgrind reports leaks like the below in various tests, e.g. gdb.threads/attach-slow-waitpid.exp, gdb.ada/task_switch_in_core.exp, ... Fix the leak by clearing the regcache when detaching from an inferior. Note that these leaks are 'created' when GDB exits, when the regcache::current_regcache is destroyed : the elements of the forward_list are pointers, and the 'pointed to' memory is not deleted by the forward_list destructor. Nevertheless, fixing this leak is good as it makes a bunch of tests 'leak clean'. Also, it seems strange to keep a register cache for a process from which GDB detached : it is not clear if this cache is still valid after detach. And effectively, when clearing only the regcache, (and not the frame cache), then the frame cache was still 'pointing' at this regcache and was used when switching to the child process in the test gdb.threads/watchpoint-fork.exp, which seems strange. So, we solve the leak and avoid possible accesses to the regcache and frame cache of the detached inferior, by clearing both the regcache and the frame cache. Tested on debian/amd64, natively, under Valgrind, and with make check RUNTESTFLAGS="--target_board=native-gdbserver". ==27679== VALGRIND_GDB_ERROR_BEGIN ==27679== 1,123 (72 direct, 1,051 indirect) bytes in 1 blocks are definitely lost in loss record 2,942 of 3,400 ==27679== at 0x4C2C4CC: operator new(unsigned long) (vg_replace_malloc.c:344) ==27679== by 0x5CDF71: get_thread_arch_aspace_regcache(ptid_t, gdbarch*, address_space*) (regcache.c:330) ==27679== by 0x5CE12A: get_thread_regcache (regcache.c:366) ==27679== by 0x5CE12A: get_current_regcache() (regcache.c:372) ==27679== by 0x4FF63D: post_create_inferior(target_ops*, int) (infcmd.c:452) ==27679== by 0x43AF62: core_target_open(char const*, int) (corelow.c:458) ==27679== by 0x408B68: cmd_func(cmd_list_element*, char const*, int) (cli-decode.c:1892) ... gdb/ChangeLog 2019-02-27 Philippe Waroquiers * target.c (target_detach): Clear the regcache and the frame cache. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 1dc63567842..f259763a45d 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2019-02-27 Philippe Waroquiers + + * target.c (target_detach): Clear the regcache and the + frame cache. + 2019-02-27 Pedro Alves * utils.c (set_screen_size): When we cap the height/width sizes, @@ -35,7 +40,7 @@ Python 2.4 workaround. 2019-02-27 Kevin Buettner - + * NEWS: Note minimum Python version. 2019-02-27 Kevin Buettner diff --git a/gdb/target.c b/gdb/target.c index 116510e8cb8..d5ff932c748 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -2029,6 +2029,17 @@ target_detach (inferior *inf, int from_tty) prepare_for_detach (); current_top_target ()->detach (inf, from_tty); + + /* After we have detached, clear the register cache for this inferior. */ + ptid_t pid_ptid = ptid_t (inf->pid); + + registers_changed_ptid (pid_ptid); + + /* We have to ensure we have no frame cache left. Normally, + registers_changed_ptid (pid_ptid) calls reinit_frame_cache when + inferior_ptid matches pid_ptid, but in our case, it does not + call it, as inferior_ptid has been reset. */ + reinit_frame_cache (); } void