Handle the inferior's threads initialization to make its fail possible.
[debugger.git] / testsuite.c
index 065d0a9..cb11a53 100644 (file)
 #include "debugger.c"
 
 
-#define tkill(tid, sig) syscall (SYS_tkill, (tid), (sig))
-
+#define TIMEOUT_SECS 4
+#undef LOOPS_MIN
+#define LOOPS_MIN 1000
 
-static volatile int spawned_threads_count;     /* Set for the specific spawner.  */
 
-static struct attach_state_struct *attach_checked (pid_t pid, int redelivered_expect)
-{
-  struct attach_state_struct *attach_state;
+#define tkill(tid, sig) syscall (SYS_tkill, (tid), (sig))
 
-  STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING) | (1 << STATE_STOPPED));
-  attach_state = attach (pid);
-  if (redelivered_expect != attach_state_redelivered_get (attach_state))
-    {
-      fprintf (stderr, "Expecting redelivery of %d but found %d\n",
-              redelivered_expect, attach_state_redelivered_get (attach_state));
-      abort ();
-    }
-  assert (spawned_threads_count == attach_state_threads_count_get (attach_state));
-  /* FIXME: Why also STATE_STOPPED?  */
-  STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
-  return attach_state;
-}
 
 static void detach_checked (struct attach_state_struct *attach_state)
 {
@@ -61,6 +46,45 @@ static void detach_checked (struct attach_state_struct *attach_state)
     }
 }
 
+static volatile int spawned_threads_count;     /* Set for the specific spawner.  */
+
+static struct attach_state_struct *attach_checked (pid_t pid, int redelivered_expect)
+{
+  struct attach_state_struct *attach_state;
+  time_t timeout = time (NULL) + TIMEOUT_SECS;
+  unsigned loops = 0;
+
+  STATE (pid, (1 << STATE_SLEEPING) | (1 << STATE_RUNNING) | (1 << STATE_STOPPED));
+  do
+    {
+      attach_state = attach (pid);
+      if (redelivered_expect != attach_state_redelivered_get (attach_state))
+       {
+         fprintf (stderr, "Expecting redelivery of %d but found %d\n",
+                  redelivered_expect,
+                  attach_state_redelivered_get (attach_state));
+         abort ();
+       }
+      if (attach_state_threads_count_get (attach_state)
+         == spawned_threads_count)
+        break;
+      fprintf (stderr, "FIXME: Untested threads initialization - REMOVE.\n");
+      /* During the inferior's initialization we may catch less threads.  */
+      assert (attach_state_threads_count_get (attach_state)
+             < spawned_threads_count);
+      /* WARNING: Currently we never use REDELIVERED_EXPECT but we would have to
+         probably reset it back to 0 otherwise.  */
+      assert (redelivered_expect == 0);
+      detach_checked (attach_state);
+    }
+  while (loops++ < LOOPS_MIN || time (NULL) < timeout);
+  assert (attach_state_threads_count_get (attach_state) == spawned_threads_count);
+
+  /* FIXME: Why also STATE_STOPPED?  */
+  STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
+  return attach_state;
+}
+
 struct registry
   {
     struct registry *next;
@@ -584,7 +608,10 @@ int main (int argc, char **argv)
       pid_threaded = spawn (tests_threaded, NULL, NULL, -1);
       pid_threaded_got = waitpid (pid_threaded, &status, 0);
       assert (pid_threaded_got == pid_threaded);
-      assert (WIFEXITED (status) && WEXITSTATUS (status) == 0);
+      assert (!WIFSIGNALED (status));  /* Improve readability in the failed case.  */
+      assert (!WIFSTOPPED (status));   /* Improve readability in the failed case.  */
+      assert (WIFEXITED (status));
+      assert (WEXITSTATUS (status) == 0);
       STATE (pid_threaded, 1 << STATE_ENOENT);
       registry_remove (pid_threaded);