aboutsummaryrefslogtreecommitdiff
path: root/gdb/gdbserver/inferiors.c
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/gdbserver/inferiors.c')
-rw-r--r--gdb/gdbserver/inferiors.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/gdb/gdbserver/inferiors.c b/gdb/gdbserver/inferiors.c
index 7888f3c..574a7ba 100644
--- a/gdb/gdbserver/inferiors.c
+++ b/gdb/gdbserver/inferiors.c
@@ -248,6 +248,51 @@ find_inferior (struct inferior_list *list,
return NULL;
}
+/* Find the random inferior_list_entry E in LIST for which FUNC (E, ARG)
+ returns non-zero. If no entry is found then return NULL. */
+
+struct inferior_list_entry *
+find_inferior_in_random (struct inferior_list *list,
+ int (*func) (struct inferior_list_entry *, void *),
+ void *arg)
+{
+ struct inferior_list_entry *inf = list->head;
+ int count = 0;
+ int random_selector;
+
+ /* First count how many interesting entries we have. */
+ while (inf != NULL)
+ {
+ struct inferior_list_entry *next;
+
+ next = inf->next;
+ if ((*func) (inf, arg))
+ count++;
+ inf = next;
+ }
+
+ if (count == 0)
+ return NULL;
+
+ /* Now randomly pick an entry out of those. */
+ random_selector = (int)
+ ((count * (double) rand ()) / (RAND_MAX + 1.0));
+
+ inf = list->head;
+ while (inf != NULL)
+ {
+ struct inferior_list_entry *next;
+
+ next = inf->next;
+ if ((*func) (inf, arg) && (random_selector-- == 0))
+ return inf;
+ inf = next;
+ }
+
+ gdb_assert_not_reached ("failed to find an inferior in random.");
+ return NULL;
+}
+
struct inferior_list_entry *
find_inferior_id (struct inferior_list *list, ptid_t id)
{