1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Builtin evlist command: Show the list of event selectors present
4  * in a perf.data file.
5  */
6 #include "builtin.h"
7 
8 #include <linux/list.h>
9 
10 #include "perf.h"
11 #include "util/evlist.h"
12 #include "util/evsel.h"
13 #include "util/evsel_fprintf.h"
14 #include "util/parse-events.h"
15 #include <subcmd/parse-options.h>
16 #include "util/session.h"
17 #include "util/data.h"
18 #include "util/debug.h"
19 #include <linux/err.h>
20 #include "util/tool.h"
21 
process_header_feature(struct perf_session * session __maybe_unused,union perf_event * event __maybe_unused)22 static int process_header_feature(struct perf_session *session __maybe_unused,
23 				  union perf_event *event __maybe_unused)
24 {
25 	session_done = 1;
26 	return 0;
27 }
28 
__cmd_evlist(const char * file_name,struct perf_attr_details * details)29 static int __cmd_evlist(const char *file_name, struct perf_attr_details *details)
30 {
31 	struct perf_session *session;
32 	struct evsel *pos;
33 	struct perf_data data = {
34 		.path      = file_name,
35 		.mode      = PERF_DATA_MODE_READ,
36 		.force     = details->force,
37 	};
38 	struct perf_tool tool = {
39 		/* only needed for pipe mode */
40 		.attr = perf_event__process_attr,
41 		.feature = process_header_feature,
42 	};
43 	bool has_tracepoint = false;
44 
45 	session = perf_session__new(&data, 0, &tool);
46 	if (IS_ERR(session))
47 		return PTR_ERR(session);
48 
49 	if (data.is_pipe)
50 		perf_session__process_events(session);
51 
52 	evlist__for_each_entry(session->evlist, pos) {
53 		evsel__fprintf(pos, details, stdout);
54 
55 		if (pos->core.attr.type == PERF_TYPE_TRACEPOINT)
56 			has_tracepoint = true;
57 	}
58 
59 	if (has_tracepoint && !details->trace_fields)
60 		printf("# Tip: use 'perf evlist --trace-fields' to show fields for tracepoint events\n");
61 
62 	perf_session__delete(session);
63 	return 0;
64 }
65 
cmd_evlist(int argc,const char ** argv)66 int cmd_evlist(int argc, const char **argv)
67 {
68 	struct perf_attr_details details = { .verbose = false, };
69 	const struct option options[] = {
70 	OPT_STRING('i', "input", &input_name, "file", "Input file name"),
71 	OPT_BOOLEAN('F', "freq", &details.freq, "Show the sample frequency"),
72 	OPT_BOOLEAN('v', "verbose", &details.verbose,
73 		    "Show all event attr details"),
74 	OPT_BOOLEAN('g', "group", &details.event_group,
75 		    "Show event group information"),
76 	OPT_BOOLEAN('f', "force", &details.force, "don't complain, do it"),
77 	OPT_BOOLEAN(0, "trace-fields", &details.trace_fields, "Show tracepoint fields"),
78 	OPT_END()
79 	};
80 	const char * const evlist_usage[] = {
81 		"perf evlist [<options>]",
82 		NULL
83 	};
84 
85 	argc = parse_options(argc, argv, options, evlist_usage, 0);
86 	if (argc)
87 		usage_with_options(evlist_usage, options);
88 
89 	if (details.event_group && (details.verbose || details.freq)) {
90 		usage_with_options_msg(evlist_usage, options,
91 			"--group option is not compatible with other options\n");
92 	}
93 
94 	return __cmd_evlist(input_name, &details);
95 }
96