162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __PERF_PARSE_EVENTS_H
362306a36Sopenharmony_ci#define __PERF_PARSE_EVENTS_H
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * Parse symbolic events/counts passed in as options:
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/list.h>
962306a36Sopenharmony_ci#include <stdbool.h>
1062306a36Sopenharmony_ci#include <linux/types.h>
1162306a36Sopenharmony_ci#include <linux/perf_event.h>
1262306a36Sopenharmony_ci#include <stdio.h>
1362306a36Sopenharmony_ci#include <string.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_cistruct evsel;
1662306a36Sopenharmony_cistruct evlist;
1762306a36Sopenharmony_cistruct parse_events_error;
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct option;
2062306a36Sopenharmony_cistruct perf_pmu;
2162306a36Sopenharmony_cistruct strbuf;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ciconst char *event_type(int type);
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/* Arguments encoded in opt->value. */
2662306a36Sopenharmony_cistruct parse_events_option_args {
2762306a36Sopenharmony_ci	struct evlist **evlistp;
2862306a36Sopenharmony_ci	const char *pmu_filter;
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ciint parse_events_option(const struct option *opt, const char *str, int unset);
3162306a36Sopenharmony_ciint parse_events_option_new_evlist(const struct option *opt, const char *str, int unset);
3262306a36Sopenharmony_ci__attribute__((nonnull(1, 2, 4)))
3362306a36Sopenharmony_ciint __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
3462306a36Sopenharmony_ci		   struct parse_events_error *error, struct perf_pmu *fake_pmu,
3562306a36Sopenharmony_ci		   bool warn_if_reordered);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci__attribute__((nonnull(1, 2, 3)))
3862306a36Sopenharmony_cistatic inline int parse_events(struct evlist *evlist, const char *str,
3962306a36Sopenharmony_ci			       struct parse_events_error *err)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	return __parse_events(evlist, str, /*pmu_filter=*/NULL, err, /*fake_pmu=*/NULL,
4262306a36Sopenharmony_ci			      /*warn_if_reordered=*/true);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciint parse_event(struct evlist *evlist, const char *str);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciint parse_events_terms(struct list_head *terms, const char *str, FILE *input);
4862306a36Sopenharmony_ciint parse_filter(const struct option *opt, const char *str, int unset);
4962306a36Sopenharmony_ciint exclude_perf(const struct option *opt, const char *arg, int unset);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cienum parse_events__term_val_type {
5262306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_NUM,
5362306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_STR,
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cienum parse_events__term_type {
5762306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_USER,
5862306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_CONFIG,
5962306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_CONFIG1,
6062306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_CONFIG2,
6162306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_CONFIG3,
6262306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_NAME,
6362306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD,
6462306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ,
6562306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
6662306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_TIME,
6762306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_CALLGRAPH,
6862306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_STACKSIZE,
6962306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_NOINHERIT,
7062306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_INHERIT,
7162306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_MAX_STACK,
7262306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_MAX_EVENTS,
7362306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_NOOVERWRITE,
7462306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_OVERWRITE,
7562306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_DRV_CFG,
7662306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_PERCORE,
7762306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT,
7862306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE,
7962306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_METRIC_ID,
8062306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_RAW,
8162306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
8262306a36Sopenharmony_ci	PARSE_EVENTS__TERM_TYPE_HARDWARE,
8362306a36Sopenharmony_ci#define	__PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_HARDWARE + 1)
8462306a36Sopenharmony_ci};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_cistruct parse_events_term {
8762306a36Sopenharmony_ci	/** @list: The term list the term is a part of. */
8862306a36Sopenharmony_ci	struct list_head list;
8962306a36Sopenharmony_ci	/**
9062306a36Sopenharmony_ci	 * @config: The left-hand side of a term assignment, so the term
9162306a36Sopenharmony_ci	 * "event=8" would have the config be "event"
9262306a36Sopenharmony_ci	 */
9362306a36Sopenharmony_ci	const char *config;
9462306a36Sopenharmony_ci	/**
9562306a36Sopenharmony_ci	 * @val: The right-hand side of a term assignment that can either be a
9662306a36Sopenharmony_ci	 * string or a number depending on type_val.
9762306a36Sopenharmony_ci	 */
9862306a36Sopenharmony_ci	union {
9962306a36Sopenharmony_ci		char *str;
10062306a36Sopenharmony_ci		u64  num;
10162306a36Sopenharmony_ci	} val;
10262306a36Sopenharmony_ci	/** @type_val: The union variable in val to be used for the term. */
10362306a36Sopenharmony_ci	enum parse_events__term_val_type type_val;
10462306a36Sopenharmony_ci	/**
10562306a36Sopenharmony_ci	 * @type_term: A predefined term type or PARSE_EVENTS__TERM_TYPE_USER
10662306a36Sopenharmony_ci	 * when not inbuilt.
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci	enum parse_events__term_type type_term;
10962306a36Sopenharmony_ci	/**
11062306a36Sopenharmony_ci	 * @err_term: The column index of the term from parsing, used during
11162306a36Sopenharmony_ci	 * error output.
11262306a36Sopenharmony_ci	 */
11362306a36Sopenharmony_ci	int err_term;
11462306a36Sopenharmony_ci	/**
11562306a36Sopenharmony_ci	 * @err_val: The column index of the val from parsing, used during error
11662306a36Sopenharmony_ci	 * output.
11762306a36Sopenharmony_ci	 */
11862306a36Sopenharmony_ci	int err_val;
11962306a36Sopenharmony_ci	/** @used: Was the term used during parameterized-eval. */
12062306a36Sopenharmony_ci	bool used;
12162306a36Sopenharmony_ci	/**
12262306a36Sopenharmony_ci	 * @weak: A term from the sysfs or json encoding of an event that
12362306a36Sopenharmony_ci	 * shouldn't override terms coming from the command line.
12462306a36Sopenharmony_ci	 */
12562306a36Sopenharmony_ci	bool weak;
12662306a36Sopenharmony_ci	/**
12762306a36Sopenharmony_ci	 * @no_value: Is there no value. If a numeric term has no value then the
12862306a36Sopenharmony_ci	 * value is assumed to be 1. An event name also has no value.
12962306a36Sopenharmony_ci	 */
13062306a36Sopenharmony_ci	bool no_value;
13162306a36Sopenharmony_ci};
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistruct parse_events_error {
13462306a36Sopenharmony_ci	int   num_errors;       /* number of errors encountered */
13562306a36Sopenharmony_ci	int   idx;	/* index in the parsed string */
13662306a36Sopenharmony_ci	char *str;      /* string to display at the index */
13762306a36Sopenharmony_ci	char *help;	/* optional help string */
13862306a36Sopenharmony_ci	int   first_idx;/* as above, but for the first encountered error */
13962306a36Sopenharmony_ci	char *first_str;
14062306a36Sopenharmony_ci	char *first_help;
14162306a36Sopenharmony_ci};
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_cistruct parse_events_state {
14462306a36Sopenharmony_ci	/* The list parsed events are placed on. */
14562306a36Sopenharmony_ci	struct list_head	   list;
14662306a36Sopenharmony_ci	/* The updated index used by entries as they are added. */
14762306a36Sopenharmony_ci	int			   idx;
14862306a36Sopenharmony_ci	/* Error information. */
14962306a36Sopenharmony_ci	struct parse_events_error *error;
15062306a36Sopenharmony_ci	/* Holds returned terms for term parsing. */
15162306a36Sopenharmony_ci	struct list_head	  *terms;
15262306a36Sopenharmony_ci	/* Start token. */
15362306a36Sopenharmony_ci	int			   stoken;
15462306a36Sopenharmony_ci	/* Special fake PMU marker for testing. */
15562306a36Sopenharmony_ci	struct perf_pmu		  *fake_pmu;
15662306a36Sopenharmony_ci	/* If non-null, when wildcard matching only match the given PMU. */
15762306a36Sopenharmony_ci	const char		  *pmu_filter;
15862306a36Sopenharmony_ci	/* Should PE_LEGACY_NAME tokens be generated for config terms? */
15962306a36Sopenharmony_ci	bool			   match_legacy_cache_terms;
16062306a36Sopenharmony_ci	/* Were multiple PMUs scanned to find events? */
16162306a36Sopenharmony_ci	bool			   wild_card_pmus;
16262306a36Sopenharmony_ci};
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cibool parse_events__filter_pmu(const struct parse_events_state *parse_state,
16562306a36Sopenharmony_ci			      const struct perf_pmu *pmu);
16662306a36Sopenharmony_civoid parse_events__shrink_config_terms(void);
16762306a36Sopenharmony_ciint parse_events__is_hardcoded_term(struct parse_events_term *term);
16862306a36Sopenharmony_ciint parse_events_term__num(struct parse_events_term **term,
16962306a36Sopenharmony_ci			   enum parse_events__term_type type_term,
17062306a36Sopenharmony_ci			   const char *config, u64 num,
17162306a36Sopenharmony_ci			   bool novalue,
17262306a36Sopenharmony_ci			   void *loc_term, void *loc_val);
17362306a36Sopenharmony_ciint parse_events_term__str(struct parse_events_term **term,
17462306a36Sopenharmony_ci			   enum parse_events__term_type type_term,
17562306a36Sopenharmony_ci			   char *config, char *str,
17662306a36Sopenharmony_ci			   void *loc_term, void *loc_val);
17762306a36Sopenharmony_ciint parse_events_term__term(struct parse_events_term **term,
17862306a36Sopenharmony_ci			    enum parse_events__term_type term_lhs,
17962306a36Sopenharmony_ci			    enum parse_events__term_type term_rhs,
18062306a36Sopenharmony_ci			    void *loc_term, void *loc_val);
18162306a36Sopenharmony_ciint parse_events_term__clone(struct parse_events_term **new,
18262306a36Sopenharmony_ci			     struct parse_events_term *term);
18362306a36Sopenharmony_civoid parse_events_term__delete(struct parse_events_term *term);
18462306a36Sopenharmony_civoid parse_events_terms__delete(struct list_head *terms);
18562306a36Sopenharmony_civoid parse_events_terms__purge(struct list_head *terms);
18662306a36Sopenharmony_ciint parse_events_term__to_strbuf(struct list_head *term_list, struct strbuf *sb);
18762306a36Sopenharmony_ciint parse_events__modifier_event(struct list_head *list, char *str, bool add);
18862306a36Sopenharmony_ciint parse_events__modifier_group(struct list_head *list, char *event_mod);
18962306a36Sopenharmony_ciint parse_events_name(struct list_head *list, const char *name);
19062306a36Sopenharmony_ciint parse_events_add_tracepoint(struct list_head *list, int *idx,
19162306a36Sopenharmony_ci				const char *sys, const char *event,
19262306a36Sopenharmony_ci				struct parse_events_error *error,
19362306a36Sopenharmony_ci				struct list_head *head_config, void *loc);
19462306a36Sopenharmony_ciint parse_events_add_numeric(struct parse_events_state *parse_state,
19562306a36Sopenharmony_ci			     struct list_head *list,
19662306a36Sopenharmony_ci			     u32 type, u64 config,
19762306a36Sopenharmony_ci			     struct list_head *head_config,
19862306a36Sopenharmony_ci			     bool wildcard);
19962306a36Sopenharmony_ciint parse_events_add_tool(struct parse_events_state *parse_state,
20062306a36Sopenharmony_ci			  struct list_head *list,
20162306a36Sopenharmony_ci			  int tool_event);
20262306a36Sopenharmony_ciint parse_events_add_cache(struct list_head *list, int *idx, const char *name,
20362306a36Sopenharmony_ci			   struct parse_events_state *parse_state,
20462306a36Sopenharmony_ci			   struct list_head *head_config);
20562306a36Sopenharmony_ciint parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *config);
20662306a36Sopenharmony_ciint parse_events_add_breakpoint(struct parse_events_state *parse_state,
20762306a36Sopenharmony_ci				struct list_head *list,
20862306a36Sopenharmony_ci				u64 addr, char *type, u64 len,
20962306a36Sopenharmony_ci				struct list_head *head_config);
21062306a36Sopenharmony_ciint parse_events_add_pmu(struct parse_events_state *parse_state,
21162306a36Sopenharmony_ci			 struct list_head *list, const char *name,
21262306a36Sopenharmony_ci			 struct list_head *head_config,
21362306a36Sopenharmony_ci			bool auto_merge_stats, void *loc);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_cistruct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
21662306a36Sopenharmony_ci				      const char *name, const char *metric_id,
21762306a36Sopenharmony_ci				      struct perf_pmu *pmu);
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ciint parse_events_multi_pmu_add(struct parse_events_state *parse_state,
22062306a36Sopenharmony_ci			       char *str,
22162306a36Sopenharmony_ci			       struct list_head *head_config,
22262306a36Sopenharmony_ci			       struct list_head **listp, void *loc);
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciint parse_events_copy_term_list(struct list_head *old,
22562306a36Sopenharmony_ci				 struct list_head **new);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_civoid parse_events__set_leader(char *name, struct list_head *list);
22862306a36Sopenharmony_civoid parse_events_update_lists(struct list_head *list_event,
22962306a36Sopenharmony_ci			       struct list_head *list_all);
23062306a36Sopenharmony_civoid parse_events_evlist_error(struct parse_events_state *parse_state,
23162306a36Sopenharmony_ci			       int idx, const char *str);
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistruct event_symbol {
23462306a36Sopenharmony_ci	const char	*symbol;
23562306a36Sopenharmony_ci	const char	*alias;
23662306a36Sopenharmony_ci};
23762306a36Sopenharmony_ciextern struct event_symbol event_symbols_hw[];
23862306a36Sopenharmony_ciextern struct event_symbol event_symbols_sw[];
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cichar *parse_events_formats_error_string(char *additional_terms);
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_civoid parse_events_error__init(struct parse_events_error *err);
24362306a36Sopenharmony_civoid parse_events_error__exit(struct parse_events_error *err);
24462306a36Sopenharmony_civoid parse_events_error__handle(struct parse_events_error *err, int idx,
24562306a36Sopenharmony_ci				char *str, char *help);
24662306a36Sopenharmony_civoid parse_events_error__print(struct parse_events_error *err,
24762306a36Sopenharmony_ci			       const char *event);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci#ifdef HAVE_LIBELF_SUPPORT
25062306a36Sopenharmony_ci/*
25162306a36Sopenharmony_ci * If the probe point starts with '%',
25262306a36Sopenharmony_ci * or starts with "sdt_" and has a ':' but no '=',
25362306a36Sopenharmony_ci * then it should be a SDT/cached probe point.
25462306a36Sopenharmony_ci */
25562306a36Sopenharmony_cistatic inline bool is_sdt_event(char *str)
25662306a36Sopenharmony_ci{
25762306a36Sopenharmony_ci	return (str[0] == '%' ||
25862306a36Sopenharmony_ci		(!strncmp(str, "sdt_", 4) &&
25962306a36Sopenharmony_ci		 !!strchr(str, ':') && !strchr(str, '=')));
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci#else
26262306a36Sopenharmony_cistatic inline bool is_sdt_event(char *str __maybe_unused)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	return false;
26562306a36Sopenharmony_ci}
26662306a36Sopenharmony_ci#endif /* HAVE_LIBELF_SUPPORT */
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci#endif /* __PERF_PARSE_EVENTS_H */
269