Blame SOURCES/gdb-stale-frame_info.patch

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