Fix regcache leak, and avoid possible regcache access after detach.
authorPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Sat, 16 Feb 2019 13:11:38 +0000 (14:11 +0100)
committerPhilippe Waroquiers <philippe.waroquiers@skynet.be>
Wed, 27 Feb 2019 21:41:16 +0000 (22:41 +0100)
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  <philippe.waroquiers@skynet.be>

* target.c (target_detach): Clear the regcache and the
frame cache.

gdb/ChangeLog
gdb/target.c

index 1dc635678422e0e847b7c2f622868ab63c931523..f259763a45d0471d2d0dd1a0acf2246186d61705 100644 (file)
@@ -1,3 +1,8 @@
+2019-02-27  Philippe Waroquiers  <philippe.waroquiers@skynet.be>
+
+       * target.c (target_detach): Clear the regcache and the
+       frame cache.
+
 2019-02-27  Pedro Alves  <palves@redhat.com>
 
        * 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  <kevinb@redhat.com>
-    
+
        * NEWS: Note minimum Python version.
 
 2019-02-27  Kevin Buettner  <kevinb@redhat.com>
index 116510e8cb8bf491a5bbb904f5144a5e5c3e7b87..d5ff932c7483d70fe27a337ef58441864977d99b 100644 (file)
@@ -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
This page took 0.029114 seconds and 4 git commands to generate.