Blame SOURCES/gdb-stale-frame_info.patch

01917d
http://sourceware.org/ml/gdb-patches/2012-04/msg00058.html
01917d
Subject: [downstream patch FYI] workaround stale frame_info * (PR 13866)
01917d
01917d
Hi,
01917d
01917d
I did not look at which commit caused this regression but apparently it was
01917d
introduced at least with multi-inferiors.
01917d
01917d
I understand this fix is not right fix of the crash; but in most GDB cases one
01917d
does not use multi-inferior so why to regress single-inferior by it.
01917d
Some more simple solutions still fix the single-inferior mode but they
01917d
regressed the multi-inferior mode
01917d
	gdb.threads/no-unwaited-for-left.exp
01917d
	gdb.multi/base.exp
01917d
so I had to put there that sorting magic.
01917d
01917d
With proper C++ sanity check of stale live frame_info references the testcase
01917d
would be simple without the "frame_garbage_collection" reproducer below.
01917d
It is also reproducible just with valgrind but regularly running the whole
01917d
testsuite under valgrind I did not find feasible.
01917d
01917d
No regressions on {x86_64,x86_64-m32,i686}-fedora17-linux-gnu.
01917d
01917d
01917d
Thanks,
01917d
Jan
01917d
01917d
01917d
gdb/
01917d
2012-04-04  Jan Kratochvil  <jan.kratochvil@redhat.com>
01917d
01917d
	Workaround PR backtrace/13866.
01917d
	* progspace.c (switch_to_program_space_and_thread): Try not to call
01917d
	switch_to_thread.
01917d
01917d
--- a/gdb/progspace.c
01917d
+++ b/gdb/progspace.c
01917d
@@ -481,17 +481,28 @@ save_current_space_and_thread (void)
01917d
 void
01917d
 switch_to_program_space_and_thread (struct program_space *pspace)
01917d
 {
01917d
-  struct inferior *inf;
01917d
+  struct inferior *inf = current_inferior ();
01917d
 
01917d
-  inf = find_inferior_for_program_space (pspace);
01917d
+  if (inf->pspace != pspace)
01917d
+    inf = find_inferior_for_program_space (pspace);
01917d
   if (inf != NULL)
01917d
     {
01917d
-      struct thread_info *tp;
01917d
+      struct thread_info *tp, *current_tp = NULL;
01917d
+
01917d
+      if (ptid_get_pid (inferior_ptid) == inf->pid)
01917d
+	current_tp = find_thread_ptid (inferior_ptid);
01917d
 
01917d
       tp = any_live_thread_of_process (inf->pid);
01917d
       if (tp != NULL)
01917d
 	{
01917d
-	  switch_to_thread (tp->ptid);
01917d
+	  /* Prefer primarily thread not THREAD_EXITED and secondarily thread
01917d
+	     not EXECUTING.  */
01917d
+	  if (current_tp == NULL
01917d
+	      || (tp->state != THREAD_EXITED
01917d
+		  && current_tp->state == THREAD_EXITED)
01917d
+	      || (!tp->executing && current_tp->executing))
01917d
+	    switch_to_thread (tp->ptid);
01917d
+
01917d
 	  /* Switching thread switches pspace implicitly.  We're
01917d
 	     done.  */
01917d
 	  return;
01917d
01917d
01917d
Reproducer with:
01917d
./gdb -nx ~/t/thread -ex 'b 24' -ex r -ex 'until 25'
01917d
Breakpoint 1, main () at /home/jkratoch/t/thread.c:24
01917d
24	  v++;
01917d
Segmentation fault (core dumped)
01917d
01917d
#include <pthread.h>
01917d
#include <assert.h>
01917d
#include <unistd.h>
01917d
01917d
static int v;
01917d
01917d
static void *start (void *arg)
01917d
{
01917d
  v++;
01917d
  v++;
01917d
  v++;
01917d
  v++;
01917d
  sleep (100);
01917d
  return arg;
01917d
}
01917d
01917d
int main (void)
01917d
{
01917d
  pthread_t thread1;
01917d
  int i;
01917d
01917d
  i = pthread_create (&thread1, NULL, start, NULL);
01917d
  assert (i == 0);
01917d
  v++;
01917d
  v++;
01917d
  v++;
01917d
  v++;
01917d
  i = pthread_join (thread1, NULL);
01917d
  assert (i == 0);
01917d
01917d
  return 0;
01917d
}
01917d
### --- a/gdb/frame.c
01917d
### +++ b/gdb/frame.c
01917d
### @@ -1522,12 +1522,30 @@ frame_observer_target_changed (struct target_ops *target)
01917d
###    reinit_frame_cache ();
01917d
###  }
01917d
###  
01917d
### +typedef struct obstack obstack_s;
01917d
### +DEF_VEC_O (obstack_s);
01917d
### +static VEC (obstack_s) *frame_poison_vec;
01917d
### +
01917d
### +void frame_garbage_collection (void);
01917d
### +void
01917d
### +frame_garbage_collection (void)
01917d
### +{
01917d
### +  struct obstack *obstack_p;
01917d
### +  int ix;
01917d
### +
01917d
### +  for (ix = 0; VEC_iterate (obstack_s, frame_poison_vec, ix, obstack_p); ix++)
01917d
### +    obstack_free (obstack_p, 0);
01917d
### +
01917d
### +  VEC_free (obstack_s, frame_poison_vec);
01917d
### +  frame_poison_vec = NULL;
01917d
### +}
01917d
### +
01917d
###  /* Flush the entire frame cache.  */
01917d
###  
01917d
###  void
01917d
###  reinit_frame_cache (void)
01917d
###  {
01917d
### -  struct frame_info *fi;
01917d
### +  struct frame_info *fi, *fi_prev;
01917d
###  
01917d
###    /* Tear down all frame caches.  */
01917d
###    for (fi = current_frame; fi != NULL; fi = fi->prev)
01917d
### @@ -1538,8 +1556,14 @@ reinit_frame_cache (void)
01917d
###  	fi->base->unwind->dealloc_cache (fi, fi->base_cache);
01917d
###      }
01917d
###  
01917d
### +  for (fi = current_frame; fi != NULL; fi = fi_prev)
01917d
### +    {
01917d
### +      fi_prev = fi->prev;
01917d
### +      memset (fi, 0, sizeof (*fi));
01917d
### +    }
01917d
### +  VEC_safe_push (obstack_s, frame_poison_vec, &frame_cache_obstack);
01917d
### +
01917d
###    /* Since we can't really be sure what the first object allocated was.  */
01917d
### -  obstack_free (&frame_cache_obstack, 0);
01917d
###    obstack_init (&frame_cache_obstack);
01917d
###  
01917d
###    if (current_frame != NULL)
01917d
### --- a/gdb/top.c
01917d
### +++ b/gdb/top.c
01917d
### @@ -359,6 +359,11 @@ prepare_execute_command (void)
01917d
###    if (non_stop)
01917d
###      target_dcache_invalidate ();
01917d
###  
01917d
### +  {
01917d
### +    extern void frame_garbage_collection (void);
01917d
### +    frame_garbage_collection ();
01917d
### +  }
01917d
### +
01917d
###    return cleanup;
01917d
###  }
01917d
###