[Pdbg] [PATCH 1/2] libpdbg: Add api for iterating over all targets
Amitay Isaacs
amitay at ozlabs.org
Thu May 14 16:26:39 AEST 2020
Signed-off-by: Amitay Isaacs <amitay at ozlabs.org>
---
libpdbg/libpdbg.c | 30 ++++++++++++++++++++++++++++++
libpdbg/libpdbg.h | 24 ++++++++++++++++++++++++
2 files changed, 54 insertions(+)
diff --git a/libpdbg/libpdbg.c b/libpdbg/libpdbg.c
index 8af6a02..bca727e 100644
--- a/libpdbg/libpdbg.c
+++ b/libpdbg/libpdbg.c
@@ -170,6 +170,36 @@ struct pdbg_target *__pdbg_next_child_target(struct pdbg_target *parent, struct
return NULL;
}
+static int _pdbg_target_traverse(struct pdbg_target *parent,
+ pdbg_target_traverse_callback_fn func,
+ void *priv)
+{
+ struct pdbg_target *target;
+ int ret;
+
+ ret = func(parent, priv);
+ if (ret)
+ return ret;
+
+ pdbg_for_each_child_target(parent, target) {
+ ret = _pdbg_target_traverse(target, func, priv);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
+int pdbg_target_traverse(struct pdbg_target *parent,
+ pdbg_target_traverse_callback_fn func,
+ void *priv)
+{
+ if (!parent)
+ parent = pdbg_target_root();
+
+ return _pdbg_target_traverse(parent, func, priv);
+}
+
enum pdbg_target_status pdbg_target_status(struct pdbg_target *target)
{
return target->status;
diff --git a/libpdbg/libpdbg.h b/libpdbg/libpdbg.h
index 3d4c185..f7a74de 100644
--- a/libpdbg/libpdbg.h
+++ b/libpdbg/libpdbg.h
@@ -262,6 +262,30 @@ enum pdbg_backend {
target; \
target = __pdbg_next_child_target(parent, target, true))
+/**
+ * @brief Callback function for pdbg target traverse
+ *
+ * @param[in] target current target
+ * @param[in] priv private data assoicated with callback function
+ * @return 0 to continue traverse, non-zero to stop traverse
+ */
+typedef int (*pdbg_target_traverse_callback_fn)(struct pdbg_target *target, void *priv);
+
+/**
+ * @brief Loop over all children of the given target
+ *
+ * @param[in] parent the head pdbg_target
+ * @param[in] func callback function to call on each target
+ * @param[in] priv private data for callback function
+ * @return 0 on successful traverse of all children, non-zero otherwise
+ *
+ * If the callback function returns non-zero value, the traverse will stop and
+ * return the same non-zero value.
+ */
+int pdbg_target_traverse(struct pdbg_target *parent,
+ pdbg_target_traverse_callback_fn func,
+ void *priv);
+
/**
* @brief Find a target parent of the given class/type
* @param[in] klass the desired parent class
--
2.25.4
More information about the Pdbg
mailing list