#include "debugger.c"
+#define TIMEOUT_SECS 4
+#undef LOOPS_MIN
+#define LOOPS_MIN 1000
+
+
#define tkill(tid, sig) syscall (SYS_tkill, (tid), (sig))
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));
- attach_state = attach (pid);
- if (redelivered_expect != attach_state_redelivered_get (attach_state))
+ do
{
- fprintf (stderr, "Expecting redelivery of %d but found %d\n",
- redelivered_expect, attach_state_redelivered_get (attach_state));
- abort ();
+ 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;
+ /* 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);
}
- assert (spawned_threads_count == attach_state_threads_count_get (attach_state));
+ while (loops++ < LOOPS_MIN || time (NULL) < timeout);
+
/* FIXME: Why also STATE_STOPPED? */
STATE (pid, (1 << STATE_PTRACED) | (1 << STATE_STOPPED));
return attach_state;