xref: /openbmc/linux/tools/perf/builtin-list.c (revision a6efaa2c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * builtin-list.c
4  *
5  * Builtin list command: list all event types
6  *
7  * Copyright (C) 2009, Thomas Gleixner <tglx@linutronix.de>
8  * Copyright (C) 2008-2009, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
9  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
10  */
11 #include "builtin.h"
12 
13 #include "util/print-events.h"
14 #include "util/pmu.h"
15 #include "util/pmu-hybrid.h"
16 #include "util/debug.h"
17 #include "util/metricgroup.h"
18 #include <subcmd/pager.h>
19 #include <subcmd/parse-options.h>
20 #include <stdio.h>
21 
22 static bool desc_flag = true;
23 static bool details_flag;
24 
25 int cmd_list(int argc, const char **argv)
26 {
27 	int i, ret = 0;
28 	bool raw_dump = false;
29 	bool long_desc_flag = false;
30 	bool deprecated = false;
31 	char *pmu_name = NULL;
32 	const char *hybrid_name = NULL;
33 	const char *unit_name = NULL;
34 	struct option list_options[] = {
35 		OPT_BOOLEAN(0, "raw-dump", &raw_dump, "Dump raw events"),
36 		OPT_BOOLEAN('d', "desc", &desc_flag,
37 			    "Print extra event descriptions. --no-desc to not print."),
38 		OPT_BOOLEAN('v', "long-desc", &long_desc_flag,
39 			    "Print longer event descriptions."),
40 		OPT_BOOLEAN(0, "details", &details_flag,
41 			    "Print information on the perf event names and expressions used internally by events."),
42 		OPT_BOOLEAN(0, "deprecated", &deprecated,
43 			    "Print deprecated events."),
44 		OPT_STRING(0, "cputype", &hybrid_name, "hybrid cpu type",
45 			   "Limit PMU or metric printing to the given hybrid PMU (e.g. core or atom)."),
46 		OPT_STRING(0, "unit", &unit_name, "PMU name",
47 			   "Limit PMU or metric printing to the specified PMU."),
48 		OPT_INCR(0, "debug", &verbose,
49 			     "Enable debugging output"),
50 		OPT_END()
51 	};
52 	const char * const list_usage[] = {
53 		"perf list [<options>] [hw|sw|cache|tracepoint|pmu|sdt|metric|metricgroup|event_glob]",
54 		NULL
55 	};
56 
57 	set_option_flag(list_options, 0, "raw-dump", PARSE_OPT_HIDDEN);
58 	/* Hide hybrid flag for the more generic 'unit' flag. */
59 	set_option_flag(list_options, 0, "cputype", PARSE_OPT_HIDDEN);
60 
61 	argc = parse_options(argc, argv, list_options, list_usage,
62 			     PARSE_OPT_STOP_AT_NON_OPTION);
63 
64 	setup_pager();
65 
66 	if (!raw_dump && pager_in_use())
67 		printf("\nList of pre-defined events (to be used in -e or -M):\n\n");
68 
69 	if (unit_name)
70 		pmu_name = strdup(unit_name);
71 	else if (hybrid_name) {
72 		pmu_name = perf_pmu__hybrid_type_to_pmu(hybrid_name);
73 		if (!pmu_name)
74 			pr_warning("WARNING: hybrid cputype is not supported!\n");
75 	}
76 
77 	if (argc == 0) {
78 		print_events(NULL, raw_dump, !desc_flag, long_desc_flag,
79 				details_flag, deprecated, pmu_name);
80 		goto out;
81 	}
82 
83 	for (i = 0; i < argc; ++i) {
84 		char *sep, *s;
85 
86 		if (strcmp(argv[i], "tracepoint") == 0)
87 			print_tracepoint_events(NULL, NULL, raw_dump);
88 		else if (strcmp(argv[i], "hw") == 0 ||
89 			 strcmp(argv[i], "hardware") == 0)
90 			print_symbol_events(NULL, PERF_TYPE_HARDWARE,
91 					event_symbols_hw, PERF_COUNT_HW_MAX, raw_dump);
92 		else if (strcmp(argv[i], "sw") == 0 ||
93 			 strcmp(argv[i], "software") == 0) {
94 			print_symbol_events(NULL, PERF_TYPE_SOFTWARE,
95 					event_symbols_sw, PERF_COUNT_SW_MAX, raw_dump);
96 			print_tool_events(NULL, raw_dump);
97 		} else if (strcmp(argv[i], "cache") == 0 ||
98 			 strcmp(argv[i], "hwcache") == 0)
99 			print_hwcache_events(NULL, raw_dump);
100 		else if (strcmp(argv[i], "pmu") == 0)
101 			print_pmu_events(NULL, raw_dump, !desc_flag,
102 						long_desc_flag, details_flag,
103 						deprecated, pmu_name);
104 		else if (strcmp(argv[i], "sdt") == 0)
105 			print_sdt_events(NULL, NULL, raw_dump);
106 		else if (strcmp(argv[i], "metric") == 0 || strcmp(argv[i], "metrics") == 0)
107 			metricgroup__print(true, false, NULL, raw_dump, details_flag, pmu_name);
108 		else if (strcmp(argv[i], "metricgroup") == 0 || strcmp(argv[i], "metricgroups") == 0)
109 			metricgroup__print(false, true, NULL, raw_dump, details_flag, pmu_name);
110 		else if ((sep = strchr(argv[i], ':')) != NULL) {
111 			int sep_idx;
112 
113 			sep_idx = sep - argv[i];
114 			s = strdup(argv[i]);
115 			if (s == NULL) {
116 				ret = -1;
117 				goto out;
118 			}
119 
120 			s[sep_idx] = '\0';
121 			print_tracepoint_events(s, s + sep_idx + 1, raw_dump);
122 			print_sdt_events(s, s + sep_idx + 1, raw_dump);
123 			metricgroup__print(true, true, s, raw_dump, details_flag, pmu_name);
124 			free(s);
125 		} else {
126 			if (asprintf(&s, "*%s*", argv[i]) < 0) {
127 				printf("Critical: Not enough memory! Trying to continue...\n");
128 				continue;
129 			}
130 			print_symbol_events(s, PERF_TYPE_HARDWARE,
131 					    event_symbols_hw, PERF_COUNT_HW_MAX, raw_dump);
132 			print_symbol_events(s, PERF_TYPE_SOFTWARE,
133 					    event_symbols_sw, PERF_COUNT_SW_MAX, raw_dump);
134 			print_tool_events(s, raw_dump);
135 			print_hwcache_events(s, raw_dump);
136 			print_pmu_events(s, raw_dump, !desc_flag,
137 						long_desc_flag,
138 						details_flag,
139 						deprecated,
140 						pmu_name);
141 			print_tracepoint_events(NULL, s, raw_dump);
142 			print_sdt_events(NULL, s, raw_dump);
143 			metricgroup__print(true, true, s, raw_dump, details_flag, pmu_name);
144 			free(s);
145 		}
146 	}
147 
148 out:
149 	free(pmu_name);
150 	return ret;
151 }
152