Skip to content

Commit 5783e15

Browse files
author
Philippe Waroquiers
committed
(re-)fix the regcache leaks when detaching from an executable.
Commit 799efbe 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) ...
1 parent 83bfc77 commit 5783e15

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

gdb/target.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,11 @@ target_preopen (int from_tty)
20132013
void
20142014
target_detach (inferior *inf, int from_tty)
20152015
{
2016+
/* After we have detached, we will clear the register cache for this inferior
2017+
by calling registers_changed_ptid. We must save the pid_ptid before
2018+
detaching, as the target detach method will clear inf->pid. */
2019+
ptid_t save_pid_ptid = ptid_t (inf->pid);
2020+
20162021
/* As long as some to_detach implementations rely on the current_inferior
20172022
(either directly, or indirectly, like through target_gdbarch or by
20182023
reading memory), INF needs to be the current inferior. When that
@@ -2033,14 +2038,11 @@ target_detach (inferior *inf, int from_tty)
20332038

20342039
current_top_target ()->detach (inf, from_tty);
20352040

2036-
/* After we have detached, clear the register cache for this inferior. */
2037-
ptid_t pid_ptid = ptid_t (inf->pid);
2038-
2039-
registers_changed_ptid (pid_ptid);
2041+
registers_changed_ptid (save_pid_ptid);
20402042

20412043
/* We have to ensure we have no frame cache left. Normally,
2042-
registers_changed_ptid (pid_ptid) calls reinit_frame_cache when
2043-
inferior_ptid matches pid_ptid, but in our case, it does not
2044+
registers_changed_ptid (save_pid_ptid) calls reinit_frame_cache when
2045+
inferior_ptid matches save_pid_ptid, but in our case, it does not
20442046
call it, as inferior_ptid has been reset. */
20452047
reinit_frame_cache ();
20462048
}

0 commit comments

Comments
 (0)