Skip to content

Commit 466db42

Browse files
captain5050namhyung
authored andcommitted
perf parse-events: Add parse_uid_filter helper
Add parse_uid_filter filter as a helper to parse_filter, that constructs a uid filter string. As uid filters don't work with tracepoint filters, add a is_possible_tp_filter function so the tracepoint filter isn't attempted for tracepoint evsels. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 5ddf4c3 commit 466db42

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

tools/perf/util/parse-events.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "pmu.h"
2626
#include "pmus.h"
2727
#include "asm/bug.h"
28+
#include "ui/ui.h"
2829
#include "util/parse-branch-options.h"
2930
#include "util/evsel_config.h"
3031
#include "util/event.h"
@@ -2561,6 +2562,12 @@ foreach_evsel_in_last_glob(struct evlist *evlist,
25612562
return 0;
25622563
}
25632564

2565+
/* Will a tracepoint filter work for str or should a BPF filter be used? */
2566+
static bool is_possible_tp_filter(const char *str)
2567+
{
2568+
return strstr(str, "uid") == NULL;
2569+
}
2570+
25642571
static int set_filter(struct evsel *evsel, const void *arg)
25652572
{
25662573
const char *str = arg;
@@ -2573,7 +2580,7 @@ static int set_filter(struct evsel *evsel, const void *arg)
25732580
return -1;
25742581
}
25752582

2576-
if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2583+
if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT && is_possible_tp_filter(str)) {
25772584
if (evsel__append_tp_filter(evsel, str) < 0) {
25782585
fprintf(stderr,
25792586
"not enough memory to hold filter string\n");
@@ -2609,6 +2616,30 @@ int parse_filter(const struct option *opt, const char *str,
26092616
(const void *)str);
26102617
}
26112618

2619+
int parse_uid_filter(struct evlist *evlist, uid_t uid)
2620+
{
2621+
struct option opt = {
2622+
.value = &evlist,
2623+
};
2624+
char buf[128];
2625+
int ret;
2626+
2627+
snprintf(buf, sizeof(buf), "uid == %d", uid);
2628+
ret = parse_filter(&opt, buf, /*unset=*/0);
2629+
if (ret) {
2630+
if (use_browser >= 1) {
2631+
/*
2632+
* Use ui__warning so a pop up appears above the
2633+
* underlying BPF error message.
2634+
*/
2635+
ui__warning("Failed to add UID filtering that uses BPF filtering.\n");
2636+
} else {
2637+
fprintf(stderr, "Failed to add UID filtering that uses BPF filtering.\n");
2638+
}
2639+
}
2640+
return ret;
2641+
}
2642+
26122643
static int add_exclude_perf_filter(struct evsel *evsel,
26132644
const void *arg __maybe_unused)
26142645
{

tools/perf/util/parse-events.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/perf_event.h>
1212
#include <stdio.h>
1313
#include <string.h>
14+
#include <sys/types.h>
1415

1516
struct evsel;
1617
struct evlist;
@@ -45,6 +46,7 @@ static inline int parse_events(struct evlist *evlist, const char *str,
4546
int parse_event(struct evlist *evlist, const char *str);
4647

4748
int parse_filter(const struct option *opt, const char *str, int unset);
49+
int parse_uid_filter(struct evlist *evlist, uid_t uid);
4850
int exclude_perf(const struct option *opt, const char *arg, int unset);
4951

5052
enum parse_events__term_val_type {

0 commit comments

Comments
 (0)