diff mbox

[OBVIOUS/PUSHED] (re-)fix the regcache leaks when detaching from an executable.

Message ID 20190324130237.19835-1-philippe.waroquiers@skynet.be
State New
Headers show

Commit Message

Philippe Waroquiers March 24, 2019, 1:02 p.m. UTC
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)
...
---
 gdb/target.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
diff mbox

Patch

diff --git a/gdb/target.c b/gdb/target.c
index 5f596b6a3c..6c05b6b83e 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 ();
 }