xref: /openbmc/linux/tools/perf/util/tracepoint.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
19b7c7728SIan Rogers // SPDX-License-Identifier: GPL-2.0
29b7c7728SIan Rogers #include "tracepoint.h"
39b7c7728SIan Rogers 
49b7c7728SIan Rogers #include <errno.h>
59b7c7728SIan Rogers #include <fcntl.h>
69b7c7728SIan Rogers #include <stdio.h>
79b7c7728SIan Rogers #include <sys/param.h>
89b7c7728SIan Rogers #include <unistd.h>
99b7c7728SIan Rogers 
109b7c7728SIan Rogers #include <api/fs/tracing_path.h>
119b7c7728SIan Rogers 
tp_event_has_id(const char * dir_path,struct dirent * evt_dir)129b7c7728SIan Rogers int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
139b7c7728SIan Rogers {
149b7c7728SIan Rogers 	char evt_path[MAXPATHLEN];
159b7c7728SIan Rogers 	int fd;
169b7c7728SIan Rogers 
179b7c7728SIan Rogers 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
189b7c7728SIan Rogers 	fd = open(evt_path, O_RDONLY);
199b7c7728SIan Rogers 	if (fd < 0)
209b7c7728SIan Rogers 		return -EINVAL;
219b7c7728SIan Rogers 	close(fd);
229b7c7728SIan Rogers 
239b7c7728SIan Rogers 	return 0;
249b7c7728SIan Rogers }
259b7c7728SIan Rogers 
269b7c7728SIan Rogers /*
279b7c7728SIan Rogers  * Check whether event is in <debugfs_mount_point>/tracing/events
289b7c7728SIan Rogers  */
is_valid_tracepoint(const char * event_string)299b7c7728SIan Rogers int is_valid_tracepoint(const char *event_string)
309b7c7728SIan Rogers {
319b7c7728SIan Rogers 	DIR *sys_dir, *evt_dir;
329b7c7728SIan Rogers 	struct dirent *sys_dirent, *evt_dirent;
339b7c7728SIan Rogers 	char evt_path[MAXPATHLEN];
349b7c7728SIan Rogers 	char *dir_path;
359b7c7728SIan Rogers 
369b7c7728SIan Rogers 	sys_dir = tracing_events__opendir();
379b7c7728SIan Rogers 	if (!sys_dir)
389b7c7728SIan Rogers 		return 0;
399b7c7728SIan Rogers 
409b7c7728SIan Rogers 	for_each_subsystem(sys_dir, sys_dirent) {
419b7c7728SIan Rogers 		dir_path = get_events_file(sys_dirent->d_name);
429b7c7728SIan Rogers 		if (!dir_path)
439b7c7728SIan Rogers 			continue;
449b7c7728SIan Rogers 		evt_dir = opendir(dir_path);
459b7c7728SIan Rogers 		if (!evt_dir)
469b7c7728SIan Rogers 			goto next;
479b7c7728SIan Rogers 
489b7c7728SIan Rogers 		for_each_event(dir_path, evt_dir, evt_dirent) {
499b7c7728SIan Rogers 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
509b7c7728SIan Rogers 				 sys_dirent->d_name, evt_dirent->d_name);
519b7c7728SIan Rogers 			if (!strcmp(evt_path, event_string)) {
529b7c7728SIan Rogers 				closedir(evt_dir);
53*9b86c497SYang Jihong 				put_events_file(dir_path);
549b7c7728SIan Rogers 				closedir(sys_dir);
559b7c7728SIan Rogers 				return 1;
569b7c7728SIan Rogers 			}
579b7c7728SIan Rogers 		}
589b7c7728SIan Rogers 		closedir(evt_dir);
599b7c7728SIan Rogers next:
609b7c7728SIan Rogers 		put_events_file(dir_path);
619b7c7728SIan Rogers 	}
629b7c7728SIan Rogers 	closedir(sys_dir);
639b7c7728SIan Rogers 	return 0;
649b7c7728SIan Rogers }
65