1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <errno.h> 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <string.h> 7 #include <sys/param.h> 8 9 #include <api/fs/tracing_path.h> 10 #include <linux/stddef.h> 11 #include <linux/perf_event.h> 12 #include <linux/zalloc.h> 13 #include <subcmd/pager.h> 14 15 #include "build-id.h" 16 #include "debug.h" 17 #include "evsel.h" 18 #include "metricgroup.h" 19 #include "parse-events.h" 20 #include "pmu.h" 21 #include "print-events.h" 22 #include "probe-file.h" 23 #include "string2.h" 24 #include "strlist.h" 25 #include "tracepoint.h" 26 #include "pfm.h" 27 #include "pmu-hybrid.h" 28 29 #define MAX_NAME_LEN 100 30 31 static const char * const event_type_descriptors[] = { 32 "Hardware event", 33 "Software event", 34 "Tracepoint event", 35 "Hardware cache event", 36 "Raw hardware event descriptor", 37 "Hardware breakpoint", 38 }; 39 40 static const struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = { 41 [PERF_TOOL_DURATION_TIME] = { 42 .symbol = "duration_time", 43 .alias = "", 44 }, 45 [PERF_TOOL_USER_TIME] = { 46 .symbol = "user_time", 47 .alias = "", 48 }, 49 [PERF_TOOL_SYSTEM_TIME] = { 50 .symbol = "system_time", 51 .alias = "", 52 }, 53 }; 54 55 /* 56 * Print the events from <debugfs_mount_point>/tracing/events 57 */ 58 void print_tracepoint_events(const char *subsys_glob, 59 const char *event_glob, bool name_only) 60 { 61 struct dirent **sys_namelist = NULL; 62 bool printed = false; 63 int sys_items = tracing_events__scandir_alphasort(&sys_namelist); 64 65 for (int i = 0; i < sys_items; i++) { 66 struct dirent *sys_dirent = sys_namelist[i]; 67 struct dirent **evt_namelist = NULL; 68 char *dir_path; 69 int evt_items; 70 71 if (sys_dirent->d_type != DT_DIR || 72 !strcmp(sys_dirent->d_name, ".") || 73 !strcmp(sys_dirent->d_name, "..")) 74 continue; 75 76 if (subsys_glob != NULL && 77 !strglobmatch(sys_dirent->d_name, subsys_glob)) 78 continue; 79 80 dir_path = get_events_file(sys_dirent->d_name); 81 if (!dir_path) 82 continue; 83 84 evt_items = scandir(dir_path, &evt_namelist, NULL, alphasort); 85 for (int j = 0; j < evt_items; j++) { 86 struct dirent *evt_dirent = evt_namelist[j]; 87 char evt_path[MAXPATHLEN]; 88 89 if (evt_dirent->d_type != DT_DIR || 90 !strcmp(evt_dirent->d_name, ".") || 91 !strcmp(evt_dirent->d_name, "..")) 92 continue; 93 94 if (tp_event_has_id(dir_path, evt_dirent) != 0) 95 continue; 96 97 if (event_glob != NULL && 98 !strglobmatch(evt_dirent->d_name, event_glob)) 99 continue; 100 101 snprintf(evt_path, MAXPATHLEN, "%s:%s", 102 sys_dirent->d_name, evt_dirent->d_name); 103 if (name_only) 104 printf("%s ", evt_path); 105 else { 106 printf(" %-50s [%s]\n", evt_path, 107 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 108 } 109 printed = true; 110 } 111 free(dir_path); 112 free(evt_namelist); 113 } 114 free(sys_namelist); 115 if (printed && pager_in_use()) 116 printf("\n"); 117 } 118 119 void print_sdt_events(const char *subsys_glob, const char *event_glob, 120 bool name_only) 121 { 122 struct probe_cache *pcache; 123 struct probe_cache_entry *ent; 124 struct strlist *bidlist, *sdtlist; 125 struct strlist_config cfg = {.dont_dupstr = true}; 126 struct str_node *nd, *nd2; 127 char *buf, *path, *ptr = NULL; 128 bool show_detail = false; 129 int ret; 130 131 sdtlist = strlist__new(NULL, &cfg); 132 if (!sdtlist) { 133 pr_debug("Failed to allocate new strlist for SDT\n"); 134 return; 135 } 136 bidlist = build_id_cache__list_all(true); 137 if (!bidlist) { 138 pr_debug("Failed to get buildids: %d\n", errno); 139 return; 140 } 141 strlist__for_each_entry(nd, bidlist) { 142 pcache = probe_cache__new(nd->s, NULL); 143 if (!pcache) 144 continue; 145 list_for_each_entry(ent, &pcache->entries, node) { 146 if (!ent->sdt) 147 continue; 148 if (subsys_glob && 149 !strglobmatch(ent->pev.group, subsys_glob)) 150 continue; 151 if (event_glob && 152 !strglobmatch(ent->pev.event, event_glob)) 153 continue; 154 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, 155 ent->pev.event, nd->s); 156 if (ret > 0) 157 strlist__add(sdtlist, buf); 158 } 159 probe_cache__delete(pcache); 160 } 161 strlist__delete(bidlist); 162 163 strlist__for_each_entry(nd, sdtlist) { 164 buf = strchr(nd->s, '@'); 165 if (buf) 166 *(buf++) = '\0'; 167 if (name_only) { 168 printf("%s ", nd->s); 169 continue; 170 } 171 nd2 = strlist__next(nd); 172 if (nd2) { 173 ptr = strchr(nd2->s, '@'); 174 if (ptr) 175 *ptr = '\0'; 176 if (strcmp(nd->s, nd2->s) == 0) 177 show_detail = true; 178 } 179 if (show_detail) { 180 path = build_id_cache__origname(buf); 181 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); 182 if (ret > 0) { 183 printf(" %-50s [%s]\n", buf, "SDT event"); 184 free(buf); 185 } 186 free(path); 187 } else 188 printf(" %-50s [%s]\n", nd->s, "SDT event"); 189 if (nd2) { 190 if (strcmp(nd->s, nd2->s) != 0) 191 show_detail = false; 192 if (ptr) 193 *ptr = '@'; 194 } 195 } 196 strlist__delete(sdtlist); 197 } 198 199 int print_hwcache_events(const char *event_glob, bool name_only) 200 { 201 struct strlist *evt_name_list = strlist__new(NULL, NULL); 202 struct str_node *nd; 203 204 if (!evt_name_list) { 205 pr_debug("Failed to allocate new strlist for hwcache events\n"); 206 return -ENOMEM; 207 } 208 for (int type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 209 for (int op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 210 /* skip invalid cache type */ 211 if (!evsel__is_cache_op_valid(type, op)) 212 continue; 213 214 for (int i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 215 struct perf_pmu *pmu = NULL; 216 char name[64]; 217 218 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); 219 if (event_glob != NULL && !strglobmatch(name, event_glob)) 220 continue; 221 222 if (!perf_pmu__has_hybrid()) { 223 if (is_event_supported(PERF_TYPE_HW_CACHE, 224 type | (op << 8) | (i << 16))) 225 strlist__add(evt_name_list, name); 226 continue; 227 } 228 perf_pmu__for_each_hybrid_pmu(pmu) { 229 if (is_event_supported(PERF_TYPE_HW_CACHE, 230 type | (op << 8) | (i << 16) | 231 ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT))) { 232 char new_name[128]; 233 snprintf(new_name, sizeof(new_name), 234 "%s/%s/", pmu->name, name); 235 strlist__add(evt_name_list, new_name); 236 } 237 } 238 } 239 } 240 } 241 242 strlist__for_each_entry(nd, evt_name_list) { 243 if (name_only) { 244 printf("%s ", nd->s); 245 continue; 246 } 247 printf(" %-50s [%s]\n", nd->s, event_type_descriptors[PERF_TYPE_HW_CACHE]); 248 } 249 if (!strlist__empty(evt_name_list) && pager_in_use()) 250 printf("\n"); 251 252 strlist__delete(evt_name_list); 253 return 0; 254 } 255 256 static void print_tool_event(const struct event_symbol *syms, const char *event_glob, 257 bool name_only) 258 { 259 if (syms->symbol == NULL) 260 return; 261 262 if (event_glob && !(strglobmatch(syms->symbol, event_glob) || 263 (syms->alias && strglobmatch(syms->alias, event_glob)))) 264 return; 265 266 if (name_only) 267 printf("%s ", syms->symbol); 268 else { 269 char name[MAX_NAME_LEN]; 270 271 if (syms->alias && strlen(syms->alias)) 272 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 273 else 274 strlcpy(name, syms->symbol, MAX_NAME_LEN); 275 printf(" %-50s [%s]\n", name, "Tool event"); 276 } 277 } 278 279 void print_tool_events(const char *event_glob, bool name_only) 280 { 281 // Start at 1 because the first enum entry means no tool event. 282 for (int i = 1; i < PERF_TOOL_MAX; ++i) 283 print_tool_event(event_symbols_tool + i, event_glob, name_only); 284 285 if (pager_in_use()) 286 printf("\n"); 287 } 288 289 void print_symbol_events(const char *event_glob, unsigned int type, 290 struct event_symbol *syms, unsigned int max, 291 bool name_only) 292 { 293 struct strlist *evt_name_list = strlist__new(NULL, NULL); 294 struct str_node *nd; 295 296 if (!evt_name_list) { 297 pr_debug("Failed to allocate new strlist for symbol events\n"); 298 return; 299 } 300 for (unsigned int i = 0; i < max; i++) { 301 /* 302 * New attr.config still not supported here, the latest 303 * example was PERF_COUNT_SW_CGROUP_SWITCHES 304 */ 305 if (syms[i].symbol == NULL) 306 continue; 307 308 if (event_glob != NULL && !(strglobmatch(syms[i].symbol, event_glob) || 309 (syms[i].alias && strglobmatch(syms[i].alias, event_glob)))) 310 continue; 311 312 if (!is_event_supported(type, i)) 313 continue; 314 315 if (strlen(syms[i].alias)) { 316 char name[MAX_NAME_LEN]; 317 318 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms[i].symbol, syms[i].alias); 319 strlist__add(evt_name_list, name); 320 } else 321 strlist__add(evt_name_list, syms[i].symbol); 322 } 323 324 strlist__for_each_entry(nd, evt_name_list) { 325 if (name_only) { 326 printf("%s ", nd->s); 327 continue; 328 } 329 printf(" %-50s [%s]\n", nd->s, event_type_descriptors[type]); 330 } 331 if (!strlist__empty(evt_name_list) && pager_in_use()) 332 printf("\n"); 333 334 strlist__delete(evt_name_list); 335 } 336 337 /* 338 * Print the help text for the event symbols: 339 */ 340 void print_events(const char *event_glob, bool name_only, bool quiet_flag, 341 bool long_desc, bool details_flag, bool deprecated, 342 const char *pmu_name) 343 { 344 print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 345 event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 346 347 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 348 event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 349 print_tool_events(event_glob, name_only); 350 351 print_hwcache_events(event_glob, name_only); 352 353 print_pmu_events(event_glob, name_only, quiet_flag, long_desc, 354 details_flag, deprecated, pmu_name); 355 356 if (event_glob != NULL) 357 return; 358 359 if (!name_only) { 360 printf(" %-50s [%s]\n", 361 "rNNN", 362 event_type_descriptors[PERF_TYPE_RAW]); 363 printf(" %-50s [%s]\n", 364 "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 365 event_type_descriptors[PERF_TYPE_RAW]); 366 if (pager_in_use()) 367 printf(" (see 'man perf-list' on how to encode it)\n\n"); 368 369 printf(" %-50s [%s]\n", 370 "mem:<addr>[/len][:access]", 371 event_type_descriptors[PERF_TYPE_BREAKPOINT]); 372 if (pager_in_use()) 373 printf("\n"); 374 } 375 376 print_tracepoint_events(NULL, NULL, name_only); 377 378 print_sdt_events(NULL, NULL, name_only); 379 380 metricgroup__print(true, true, NULL, name_only, details_flag, 381 pmu_name); 382 383 print_libpfm_events(name_only, long_desc); 384 } 385