static struct target_ops *the_dummy_target;
static struct target_ops *the_debug_target;
+/* The target stack. */
+
+static target_stack g_target_stack;
+
/* Top of target stack. */
/* The target structure we are currently using to talk to a process
or file or whatever "inferior" we have. */
-static target_ops *g_current_top_target;
-
target_ops *
current_top_target ()
{
- return g_current_top_target;
+ return g_target_stack.top ();
}
/* Command list for target. */
to_execution_direction must be implemented for reverse async");
}
-/* Push a new target type into the stack of the existing target accessors,
- possibly superseding some of the existing accessors.
-
- Rather than allow an empty stack, we always have the dummy target at
- the bottom stratum, so we can call the function vectors without
- checking them. */
+/* See target.h. */
void
-push_target (struct target_ops *t)
+target_stack::push (target_ops *t)
{
- struct target_ops **cur;
-
- /* Find the proper stratum to install this target in. */
- for (cur = &g_current_top_target; (*cur) != NULL; cur = &(*cur)->m_beneath)
+ /* If there's already a target at this stratum, remove it. */
+ if (m_stack[t->to_stratum] != NULL)
{
- if ((int) (t->to_stratum) >= (int) (*cur)->to_stratum)
- break;
+ target_ops *prev = m_stack[t->to_stratum];
+ m_stack[t->to_stratum] = NULL;
+ target_close (prev);
}
- /* If there's already targets at this stratum, remove them. */
- /* FIXME: cagney/2003-10-15: I think this should be popping all
- targets to CUR, and not just those at this stratum level. */
- while ((*cur) != NULL && t->to_stratum == (*cur)->to_stratum)
- {
- /* There's already something at this stratum level. Close it,
- and un-hook it from the stack. */
- struct target_ops *tmp = (*cur);
+ /* Now add the new one. */
+ m_stack[t->to_stratum] = t;
- (*cur) = (*cur)->m_beneath;
- tmp->m_beneath = NULL;
- target_close (tmp);
- }
+ if (m_top < t->to_stratum)
+ m_top = t->to_stratum;
+}
+
+/* See target.h. */
- /* We have removed all targets in our stratum, now add the new one. */
- t->m_beneath = (*cur);
- (*cur) = t;
+void
+push_target (struct target_ops *t)
+{
+ g_target_stack.push (t);
}
-/* Remove a target_ops vector from the stack, wherever it may be.
- Return how many times it was removed (0 or 1). */
+/* See target.h. */
int
unpush_target (struct target_ops *t)
{
+ return g_target_stack.unpush (t);
+}
+
+/* See target.h. */
+
+bool
+target_stack::unpush (target_ops *t)
+{
struct target_ops **cur;
struct target_ops *tmp;
internal_error (__FILE__, __LINE__,
_("Attempt to unpush the dummy target"));
- /* Look for the specified target. Note that we assume that a target
- can only occur once in the target stack. */
+ gdb_assert (t != NULL);
+
+ /* Look for the specified target. Note that a target can only occur
+ once in the target stack. */
- for (cur = &g_current_top_target; (*cur) != NULL; cur = &(*cur)->m_beneath)
+ if (m_stack[t->to_stratum] != t)
{
- if ((*cur) == t)
- break;
+ /* If T wasn't pushed, quit. Only open targets should be
+ closed. */
+ return false;
}
- /* If we don't find target_ops, quit. Only open targets should be
- closed. */
- if ((*cur) == NULL)
- return 0;
-
/* Unchain the target. */
- tmp = (*cur);
- (*cur) = (*cur)->m_beneath;
- tmp->m_beneath = NULL;
+ m_stack[t->to_stratum] = NULL;
+
+ if (m_top == t->to_stratum)
+ m_top = t->beneath ()->to_stratum;
/* Finally close the target. Note we do this after unchaining, so
any target method calls from within the target_close
implementation don't end up in T anymore. */
target_close (t);
- return 1;
+ return true;
}
/* Unpush TARGET and assert that it worked. */
int
target_is_pushed (struct target_ops *t)
{
- for (target_ops *cur = current_top_target ();
- cur != NULL;
- cur = cur->beneath ())
- if (cur == t)
- return 1;
-
- return 0;
+ return g_target_stack.is_pushed (t);
}
/* Default implementation of to_get_thread_local_address. */
target_ops *
target_ops::beneath () const
{
- return m_beneath;
+ return g_target_stack.find_beneath (this);
}
void
/* See target.h. */
-struct target_ops *
-find_target_at (enum strata stratum)
+target_ops *
+target_stack::find_beneath (const target_ops *t) const
{
- for (target_ops *t = current_top_target (); t != NULL; t = t->beneath ())
- if (t->to_stratum == stratum)
- return t;
+ /* Look for a non-empty slot at stratum levels beneath T's. */
+ for (int stratum = t->to_stratum - 1; stratum >= 0; --stratum)
+ if (m_stack[stratum] != NULL)
+ return m_stack[stratum];
return NULL;
}
+/* See target.h. */
+
+struct target_ops *
+find_target_at (enum strata stratum)
+{
+ return g_target_stack.at (stratum);
+}
+
\f
/* See target.h */
of variables any more (the file target is handling them and they
never get to the process target). So when you push a file target,
it goes into the file stratum, which is always below the process
- stratum. */
+ stratum.
+
+ Note that rather than allow an empty stack, we always have the
+ dummy target at the bottom stratum, so we can call the target
+ methods without checking them. */
#include "target/target.h"
#include "target/resume.h"
struct target_ops
{
/* To the target under this one. */
- target_ops *m_beneath;
target_ops *beneath () const;
/* Free resources associated with the target. Note that singleton
NULL. */
extern target_ops *get_native_target ();
+/* Type that manages a target stack. See description of target stacks
+ and strata at the top of the file. */
+
+class target_stack
+{
+public:
+ target_stack () = default;
+ DISABLE_COPY_AND_ASSIGN (target_stack);
+
+ /* Push a new target into the stack of the existing target
+ accessors, possibly superseding some existing accessor. */
+ void push (target_ops *t);
+
+ /* Remove a target from the stack, wherever it may be. Return true
+ if it was removed, false otherwise. */
+ bool unpush (target_ops *t);
+
+ /* Returns true if T is pushed on the target stack. */
+ bool is_pushed (target_ops *t) const
+ { return at (t->to_stratum) == t; }
+
+ /* Return the target at STRATUM. */
+ target_ops *at (strata stratum) const { return m_stack[stratum]; }
+
+ /* Return the target at the top of the stack. */
+ target_ops *top () const { return at (m_top); }
+
+ /* Find the next target down the stack from the specified target. */
+ target_ops *find_beneath (const target_ops *t) const;
+
+private:
+ /* The stratum of the top target. */
+ enum strata m_top {};
+
+ /* The stack, represented as an array, with one slot per stratum.
+ If no target is pushed at some stratum, the corresponding slot is
+ null. */
+ target_ops *m_stack[(int) debug_stratum + 1] {};
+};
+
/* The ops structure for our "current" target process. This should
never be NULL. If there is no target, it points to the dummy_target. */