Blame SOURCES/gdb-stale-frame_info.patch

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