xref: /openbmc/linux/tools/perf/util/trace-event.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
297978b3eSJiri Olsa #include <stdio.h>
397978b3eSJiri Olsa #include <unistd.h>
497978b3eSJiri Olsa #include <stdlib.h>
597978b3eSJiri Olsa #include <errno.h>
697978b3eSJiri Olsa #include <sys/types.h>
797978b3eSJiri Olsa #include <sys/stat.h>
897978b3eSJiri Olsa #include <fcntl.h>
997978b3eSJiri Olsa #include <linux/kernel.h>
108dd2a131SJiri Olsa #include <linux/err.h>
1129f5ffd3SJiri Olsa #include <traceevent/event-parse.h>
12592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
13607bfbd7SJiri Olsa #include <api/fs/fs.h>
1429f5ffd3SJiri Olsa #include "trace-event.h"
15c3168b0dSArnaldo Carvalho de Melo #include "machine.h"
1697978b3eSJiri Olsa 
1797978b3eSJiri Olsa /*
1897978b3eSJiri Olsa  * global trace_event object used by trace_event__tp_format
1997978b3eSJiri Olsa  *
2097978b3eSJiri Olsa  * TODO There's no cleanup call for this. Add some sort of
2197978b3eSJiri Olsa  * __exit function support and call trace_event__cleanup
2297978b3eSJiri Olsa  * there.
2397978b3eSJiri Olsa  */
2497978b3eSJiri Olsa static struct trace_event tevent;
25c3168b0dSArnaldo Carvalho de Melo static bool tevent_initialized;
2629f5ffd3SJiri Olsa 
trace_event__init(struct trace_event * t)2729f5ffd3SJiri Olsa int trace_event__init(struct trace_event *t)
2829f5ffd3SJiri Olsa {
294d5c58b1STzvetomir Stoyanov (VMware) 	struct tep_handle *pevent = tep_alloc();
3029f5ffd3SJiri Olsa 
3129f5ffd3SJiri Olsa 	if (pevent) {
32fc9b6971STzvetomir Stoyanov (VMware) 		t->plugin_list = tep_load_plugins(pevent);
3329f5ffd3SJiri Olsa 		t->pevent  = pevent;
3429f5ffd3SJiri Olsa 	}
3529f5ffd3SJiri Olsa 
3629f5ffd3SJiri Olsa 	return pevent ? 0 : -1;
3729f5ffd3SJiri Olsa }
3829f5ffd3SJiri Olsa 
trace_event__init2(void)39c3168b0dSArnaldo Carvalho de Melo static int trace_event__init2(void)
40c3168b0dSArnaldo Carvalho de Melo {
41*55c34ae0STzvetomir Stoyanov 	int be = tep_is_bigendian();
42096177a8STzvetomir Stoyanov (VMware) 	struct tep_handle *pevent;
43c3168b0dSArnaldo Carvalho de Melo 
44c3168b0dSArnaldo Carvalho de Melo 	if (trace_event__init(&tevent))
45c3168b0dSArnaldo Carvalho de Melo 		return -1;
46c3168b0dSArnaldo Carvalho de Melo 
47c3168b0dSArnaldo Carvalho de Melo 	pevent = tevent.pevent;
486fed932eSTzvetomir Stoyanov (VMware) 	tep_set_flag(pevent, TEP_NSEC_OUTPUT);
49ece2a4f4STzvetomir Stoyanov (VMware) 	tep_set_file_bigendian(pevent, be);
50*55c34ae0STzvetomir Stoyanov 	tep_set_local_bigendian(pevent, be);
51c3168b0dSArnaldo Carvalho de Melo 	tevent_initialized = true;
52c3168b0dSArnaldo Carvalho de Melo 	return 0;
53c3168b0dSArnaldo Carvalho de Melo }
54c3168b0dSArnaldo Carvalho de Melo 
trace_event__register_resolver(struct machine * machine,tep_func_resolver_t * func)55959c2199SArnaldo Carvalho de Melo int trace_event__register_resolver(struct machine *machine,
564d5c58b1STzvetomir Stoyanov (VMware) 				   tep_func_resolver_t *func)
57c3168b0dSArnaldo Carvalho de Melo {
58c3168b0dSArnaldo Carvalho de Melo 	if (!tevent_initialized && trace_event__init2())
59c3168b0dSArnaldo Carvalho de Melo 		return -1;
60c3168b0dSArnaldo Carvalho de Melo 
61ece2a4f4STzvetomir Stoyanov (VMware) 	return tep_set_function_resolver(tevent.pevent, func, machine);
62c3168b0dSArnaldo Carvalho de Melo }
63c3168b0dSArnaldo Carvalho de Melo 
trace_event__cleanup(struct trace_event * t)6429f5ffd3SJiri Olsa void trace_event__cleanup(struct trace_event *t)
6529f5ffd3SJiri Olsa {
66fc9b6971STzvetomir Stoyanov (VMware) 	tep_unload_plugins(t->plugin_list, t->pevent);
674d5c58b1STzvetomir Stoyanov (VMware) 	tep_free(t->pevent);
6829f5ffd3SJiri Olsa }
6997978b3eSJiri Olsa 
708dd2a131SJiri Olsa /*
718dd2a131SJiri Olsa  * Returns pointer with encoded error via <linux/err.h> interface.
728dd2a131SJiri Olsa  */
7397fbf3f0STzvetomir Stoyanov static struct tep_event*
tp_format(const char * sys,const char * name)7497978b3eSJiri Olsa tp_format(const char *sys, const char *name)
7597978b3eSJiri Olsa {
7625a7d914SArnaldo Carvalho de Melo 	char *tp_dir = get_events_file(sys);
77096177a8STzvetomir Stoyanov (VMware) 	struct tep_handle *pevent = tevent.pevent;
7897fbf3f0STzvetomir Stoyanov 	struct tep_event *event = NULL;
7997978b3eSJiri Olsa 	char path[PATH_MAX];
8097978b3eSJiri Olsa 	size_t size;
8197978b3eSJiri Olsa 	char *data;
828dd2a131SJiri Olsa 	int err;
8397978b3eSJiri Olsa 
8425a7d914SArnaldo Carvalho de Melo 	if (!tp_dir)
8525a7d914SArnaldo Carvalho de Melo 		return ERR_PTR(-errno);
8625a7d914SArnaldo Carvalho de Melo 
8725a7d914SArnaldo Carvalho de Melo 	scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
8825a7d914SArnaldo Carvalho de Melo 	put_events_file(tp_dir);
8997978b3eSJiri Olsa 
908dd2a131SJiri Olsa 	err = filename__read_str(path, &data, &size);
918dd2a131SJiri Olsa 	if (err)
928dd2a131SJiri Olsa 		return ERR_PTR(err);
9397978b3eSJiri Olsa 
94c60167c1STzvetomir Stoyanov (VMware) 	tep_parse_format(pevent, &event, data, size, sys);
9597978b3eSJiri Olsa 
9697978b3eSJiri Olsa 	free(data);
9797978b3eSJiri Olsa 	return event;
9897978b3eSJiri Olsa }
9997978b3eSJiri Olsa 
1008dd2a131SJiri Olsa /*
1018dd2a131SJiri Olsa  * Returns pointer with encoded error via <linux/err.h> interface.
1028dd2a131SJiri Olsa  */
10397fbf3f0STzvetomir Stoyanov struct tep_event*
trace_event__tp_format(const char * sys,const char * name)10497978b3eSJiri Olsa trace_event__tp_format(const char *sys, const char *name)
10597978b3eSJiri Olsa {
106c3168b0dSArnaldo Carvalho de Melo 	if (!tevent_initialized && trace_event__init2())
1078dd2a131SJiri Olsa 		return ERR_PTR(-ENOMEM);
10897978b3eSJiri Olsa 
10997978b3eSJiri Olsa 	return tp_format(sys, name);
11097978b3eSJiri Olsa }
11171fe1052SJiri Olsa 
trace_event__tp_format_id(int id)11297fbf3f0STzvetomir Stoyanov struct tep_event *trace_event__tp_format_id(int id)
11371fe1052SJiri Olsa {
11471fe1052SJiri Olsa 	if (!tevent_initialized && trace_event__init2())
11571fe1052SJiri Olsa 		return ERR_PTR(-ENOMEM);
11671fe1052SJiri Olsa 
117af85cd19STzvetomir Stoyanov (VMware) 	return tep_find_event(tevent.pevent, id);
11871fe1052SJiri Olsa }
119