Blame SOURCES/gdb-stale-frame_info.patch

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