[PATCH 3/9] gdb: add all_tracepoints function

Simon Marchi via Gdb-patches
2021-05-27T15:36:20Z

Same idea as the previous patches, but to replace the ALL_TRACEPOINTS
macro. Define a new filtered_iterator that only keeps the breakpoints
for which is_tracepoint returns true (just like the macro did).

I would have like to make it so tracepoint_range yields some
`tracepoint *` instead of some `breakpoint *`, that would help simplify
the callers, who wouldn't have to do the cast themselves. But I didn't
find an obvious way to do it. It can always be added later.

It turns out there is already an all_tracepoints function, which returns
a vector containing all the breakpoints that are tracepoint. Remove it,
most users will just work seamlessly with the new function. The
exception is start_tracing, which iterated multiple times on the vector.
Adapt this one so it iterates multiple times on the returned range.

Since the existing users of all_tracepoints are outside of breakpoint.c,
this requires defining all_tracepoints and a few supporting types in
breakpoint.h. So, move breakpoint_iterator from breakpoint.c to
breakpoint.h.

gdb/ChangeLog:

* breakpoint.h (all_tracepoints): Remove.
(breakpoint_iterator): Move here.
(struct tracepoint_filter): New.
(tracepoint_iterator): New.
(tracepoint_range): New.
(all_tracepoints): New.
* breakpoint.c (ALL_TRACEPOINTS): Remove, replace all users with
all_tracepoints.
(breakpoint_iterator): Move to header.
(all_tracepoints): New.
* tracepoint.c (start_tracing): Adjust.

Change-Id: I76b1bba4215dbec7a03846c568368aeef7f1e05a
---
gdb/breakpoint.c | 55 ++++++++++++------------------------------------
gdb/breakpoint.h | 29 ++++++++++++++++++++++---
gdb/tracepoint.c | 8 +++----
3 files changed, 44 insertions(+), 48 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 06012c7374ab..eb7fc553f9c4 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -514,20 +514,10 @@ bool target_exact_watchpoints = false;
&& (*BP_LOCP_TMP)->address == ADDRESS); \
BP_LOCP_TMP++)

-/* Iterator for tracepoints only. */
-
-#define ALL_TRACEPOINTS(B) \
- for (B = breakpoint_chain; B; B = B->next) \
- if (is_tracepoint (B))
-
/* Chains of all breakpoints defined. */

static struct breakpoint *breakpoint_chain;

-/* Breakpoint linked list iterator. */
-
-using breakpoint_iterator = next_iterator<breakpoint>;
-
/* Breakpoint linked list range. */

using breakpoint_range = next_adapter<breakpoint, breakpoint_iterator>;
@@ -554,6 +544,14 @@ all_breakpoints_safe ()
return breakpoint_safe_range (all_breakpoints ());
}

+/* See breakpoint.h. */
+
+tracepoint_range
+all_tracepoints ()
+{
+ return tracepoint_range (breakpoint_chain);
+}
+
/* Array is sorted by bp_location_is_less_than - primarily by the ADDRESS. */

static struct bp_location **bp_locations;
@@ -11714,12 +11712,11 @@ bp_locations_target_extensions_update (void)
static void
download_tracepoint_locations (void)
{
- struct breakpoint *b;
enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;

scoped_restore_current_pspace_and_thread restore_pspace_thread;

- ALL_TRACEPOINTS (b)
+ for (breakpoint *b : all_tracepoints ())
{
struct bp_location *bl;
struct tracepoint *t;
@@ -14910,13 +14907,12 @@ delete_trace_command (const char *arg, int from_tty)
if (arg == 0)
{
int breaks_to_delete = 0;
- breakpoint *tp;

/* Delete all breakpoints if no argument.
Do not delete internal or call-dummy breakpoints, these
have to be deleted with an explicit breakpoint number
argument. */
- ALL_TRACEPOINTS (tp)
+ for (breakpoint *tp : all_tracepoints ())
if (is_tracepoint (tp) && user_breakpoint_p (tp))
{
breaks_to_delete = 1;
@@ -14973,13 +14969,11 @@ trace_pass_command (const char *args, int from_tty)
args = skip_spaces (args);
if (*args && strncasecmp (args, "all", 3) == 0)
{
- struct breakpoint *b;
-
args += 3; /* Skip special argument "all". */
if (*args)
error (_("Junk at end of arguments."));

- ALL_TRACEPOINTS (b)
+ for (breakpoint *b : all_tracepoints ())
{
t1 = (struct tracepoint *) b;
trace_pass_set_count (t1, count, from_tty);
@@ -15006,9 +15000,7 @@ trace_pass_command (const char *args, int from_tty)
struct tracepoint *
get_tracepoint (int num)
{
- struct breakpoint *t;
-
- ALL_TRACEPOINTS (t)
+ for (breakpoint *t : all_tracepoints ())
if (t->number == num)
return (struct tracepoint *) t;

@@ -15022,9 +15014,7 @@ get_tracepoint (int num)
struct tracepoint *
get_tracepoint_by_number_on_target (int num)
{
- struct breakpoint *b;
-
- ALL_TRACEPOINTS (b)
+ for (breakpoint *b : all_tracepoints ())
{
struct tracepoint *t = (struct tracepoint *) b;

@@ -15044,7 +15034,6 @@ struct tracepoint *
get_tracepoint_by_number (const char **arg,
number_or_range_parser *parser)
{
- struct breakpoint *t;
int tpnum;
const char *instring = arg == NULL ? NULL : *arg;

@@ -15068,7 +15057,7 @@ get_tracepoint_by_number (const char **arg,
return NULL;
}

- ALL_TRACEPOINTS (t)
+ for (breakpoint *t : all_tracepoints ())
if (t->number == tpnum)
{
return (struct tracepoint *) t;
@@ -15225,22 +15214,6 @@ save_tracepoints_command (const char *args, int from_tty)
save_breakpoints (args, from_tty, is_tracepoint);
}

-/* Create a vector of all tracepoints. */
-
-std::vector<breakpoint *>
-all_tracepoints (void)
-{
- std::vector<breakpoint *> tp_vec;
- struct breakpoint *tp;
-
- ALL_TRACEPOINTS (tp)
- {
- tp_vec.push_back (tp);
- }
-
- return tp_vec;
-}
-
\f
/* This help string is used to consolidate all the help string for specifying
locations used by several commands. */
diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h
index 54c5e423e10a..5a10839603d7 100644
--- a/gdb/breakpoint.h
+++ b/gdb/breakpoint.h
@@ -28,6 +28,7 @@
#include "location.h"
#include <vector>
#include "gdbsupport/array-view.h"
+#include "gdbsupport/filtered-iterator.h"
#include "gdbsupport/function-view.h"
#include "gdbsupport/refcounted-object.h"
#include "cli/cli-script.h"
@@ -1683,9 +1684,6 @@ extern struct tracepoint *
get_tracepoint_by_number (const char **arg,
number_or_range_parser *parser);

-/* Return a vector of all tracepoints currently defined. */
-extern std::vector<breakpoint *> all_tracepoints (void);
-
/* Return true if B is of tracepoint kind. */

extern bool is_tracepoint (const struct breakpoint *b);
@@ -1717,6 +1715,31 @@ class scoped_rbreak_breakpoints
extern struct breakpoint *iterate_over_breakpoints
(gdb::function_view<bool (breakpoint *)>);

+/* Breakpoint linked list iterator. */
+
+using breakpoint_iterator = next_iterator<breakpoint>;
+
+/* Breakpoint filter to only keep tracepoints. */
+
+struct tracepoint_filter
+{
+ bool operator() (breakpoint *b)
+ { return is_tracepoint (b); }
+};
+
+/* Breakpoint linked list iterator, filtering to only keep tracepoints. */
+
+using tracepoint_iterator
+ = filtered_iterator<breakpoint_iterator, tracepoint_filter>;
+
+/* Breakpoint linked list range, filtering to only keep tracepoints. */
+
+using tracepoint_range = next_adapter<breakpoint, tracepoint_iterator>;
+
+/* Return a range to iterate over all tracepoints. */
+
+tracepoint_range all_tracepoints ();
+
/* Nonzero if the specified PC cannot be a location where functions
have been inlined. */

diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index da0e976c8694..863fb6d34d8d 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -1603,13 +1603,13 @@ start_tracing (const char *notes)
int any_enabled = 0, num_to_download = 0;
int ret;

- std::vector<breakpoint *> tp_vec = all_tracepoints ();
+ auto tracepoint_range = all_tracepoints ();

/* No point in tracing without any tracepoints... */
- if (tp_vec.empty ())
+ if (tracepoint_range.begin () == tracepoint_range.end ())
error (_("No tracepoints defined, not starting trace"));

- for (breakpoint *b : tp_vec)
+ for (breakpoint *b : tracepoint_range)
{
if (b->enable_state == bp_enabled)
any_enabled = 1;
@@ -1640,7 +1640,7 @@ start_tracing (const char *notes)

target_trace_init ();

- for (breakpoint *b : tp_vec)
+ for (breakpoint *b : tracepoint_range)
{
struct tracepoint *t = (struct tracepoint *) b;
struct bp_location *loc;
--
2.31.1