From: Philippe Waroquiers Date: Sun, 24 Mar 2019 10:44:00 +0000 (+0100) Subject: (re-)fix the regcache leaks when detaching from an executable. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=5783e150b2a9308e23262a5b62f5d51c8c932ece;p=binutils-gdb.git (re-)fix the regcache leaks when detaching from an executable. Commit 799efbe8e01ab8292c01f46ac59a6fb2349d4535 was supposed to fix the below leak. However, for this fix to work, it is critical to save the ptid before detach. This commit (pushed as OBVIOUS, as the change was already reviewed/approved) saves the ptid before the detach, as in the original reviewed patch (see https://sourceware.org/ml/gdb-patches/2019-02/msg00263.html). Re-tested on debian/amd64, natively and under valgrind. ==7426== 1,123 (72 direct, 1,051 indirect) bytes in 1 blocks are definitely lost in loss record 2,872 of 3,020 ==7426== at 0x4C2C4CC: operator new(unsigned long) (vg_replace_malloc.c:344) ==7426== by 0x5BD1E1: get_thread_arch_aspace_regcache(ptid_t, gdbarch*, address_space*) (regcache.c:330) ==7426== by 0x5BD39A: get_thread_regcache (regcache.c:366) ==7426== by 0x5BD39A: get_current_regcache() (regcache.c:372) ==7426== by 0x4B1EB4: get_current_frame() (frame.c:1588) ... --- diff --git a/gdb/target.c b/gdb/target.c index 5f596b6a3c9..6c05b6b83ef 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -2013,6 +2013,11 @@ target_preopen (int from_tty) void target_detach (inferior *inf, int from_tty) { + /* After we have detached, we will clear the register cache for this inferior + by calling registers_changed_ptid. We must save the pid_ptid before + detaching, as the target detach method will clear inf->pid. */ + ptid_t save_pid_ptid = ptid_t (inf->pid); + /* As long as some to_detach implementations rely on the current_inferior (either directly, or indirectly, like through target_gdbarch or by reading memory), INF needs to be the current inferior. When that @@ -2033,14 +2038,11 @@ target_detach (inferior *inf, int from_tty) 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); + registers_changed_ptid (save_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 + registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when + inferior_ptid matches save_pid_ptid, but in our case, it does not call it, as inferior_ptid has been reset. */ reinit_frame_cache (); }