xref: /OK3568_Linux_fs/kernel/tools/perf/util/trace-event.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include <stdio.h>
4*4882a593Smuzhiyun #include <unistd.h>
5*4882a593Smuzhiyun #include <stdlib.h>
6*4882a593Smuzhiyun #include <errno.h>
7*4882a593Smuzhiyun #include <sys/types.h>
8*4882a593Smuzhiyun #include <sys/stat.h>
9*4882a593Smuzhiyun #include <fcntl.h>
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <traceevent/event-parse.h>
13*4882a593Smuzhiyun #include <api/fs/tracing_path.h>
14*4882a593Smuzhiyun #include <api/fs/fs.h>
15*4882a593Smuzhiyun #include "trace-event.h"
16*4882a593Smuzhiyun #include "machine.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * global trace_event object used by trace_event__tp_format
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * TODO There's no cleanup call for this. Add some sort of
22*4882a593Smuzhiyun  * __exit function support and call trace_event__cleanup
23*4882a593Smuzhiyun  * there.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun static struct trace_event tevent;
26*4882a593Smuzhiyun static bool tevent_initialized;
27*4882a593Smuzhiyun 
trace_event__init(struct trace_event * t)28*4882a593Smuzhiyun int trace_event__init(struct trace_event *t)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct tep_handle *pevent = tep_alloc();
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	if (pevent) {
33*4882a593Smuzhiyun 		t->plugin_list = tep_load_plugins(pevent);
34*4882a593Smuzhiyun 		t->pevent  = pevent;
35*4882a593Smuzhiyun 	}
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	return pevent ? 0 : -1;
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
trace_event__init2(void)40*4882a593Smuzhiyun static int trace_event__init2(void)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	int be = tep_is_bigendian();
43*4882a593Smuzhiyun 	struct tep_handle *pevent;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	if (trace_event__init(&tevent))
46*4882a593Smuzhiyun 		return -1;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	pevent = tevent.pevent;
49*4882a593Smuzhiyun 	tep_set_flag(pevent, TEP_NSEC_OUTPUT);
50*4882a593Smuzhiyun 	tep_set_file_bigendian(pevent, be);
51*4882a593Smuzhiyun 	tep_set_local_bigendian(pevent, be);
52*4882a593Smuzhiyun 	tevent_initialized = true;
53*4882a593Smuzhiyun 	return 0;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
trace_event__register_resolver(struct machine * machine,tep_func_resolver_t * func)56*4882a593Smuzhiyun int trace_event__register_resolver(struct machine *machine,
57*4882a593Smuzhiyun 				   tep_func_resolver_t *func)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	if (!tevent_initialized && trace_event__init2())
60*4882a593Smuzhiyun 		return -1;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	return tep_set_function_resolver(tevent.pevent, func, machine);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
trace_event__cleanup(struct trace_event * t)65*4882a593Smuzhiyun void trace_event__cleanup(struct trace_event *t)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	tep_unload_plugins(t->plugin_list, t->pevent);
68*4882a593Smuzhiyun 	tep_free(t->pevent);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun  * Returns pointer with encoded error via <linux/err.h> interface.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun static struct tep_event*
tp_format(const char * sys,const char * name)75*4882a593Smuzhiyun tp_format(const char *sys, const char *name)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	char *tp_dir = get_events_file(sys);
78*4882a593Smuzhiyun 	struct tep_handle *pevent = tevent.pevent;
79*4882a593Smuzhiyun 	struct tep_event *event = NULL;
80*4882a593Smuzhiyun 	char path[PATH_MAX];
81*4882a593Smuzhiyun 	size_t size;
82*4882a593Smuzhiyun 	char *data;
83*4882a593Smuzhiyun 	int err;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (!tp_dir)
86*4882a593Smuzhiyun 		return ERR_PTR(-errno);
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	scnprintf(path, PATH_MAX, "%s/%s/format", tp_dir, name);
89*4882a593Smuzhiyun 	put_events_file(tp_dir);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	err = filename__read_str(path, &data, &size);
92*4882a593Smuzhiyun 	if (err)
93*4882a593Smuzhiyun 		return ERR_PTR(err);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	tep_parse_format(pevent, &event, data, size, sys);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	free(data);
98*4882a593Smuzhiyun 	return event;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun  * Returns pointer with encoded error via <linux/err.h> interface.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun struct tep_event*
trace_event__tp_format(const char * sys,const char * name)105*4882a593Smuzhiyun trace_event__tp_format(const char *sys, const char *name)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	if (!tevent_initialized && trace_event__init2())
108*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return tp_format(sys, name);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
trace_event__tp_format_id(int id)113*4882a593Smuzhiyun struct tep_event *trace_event__tp_format_id(int id)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	if (!tevent_initialized && trace_event__init2())
116*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	return tep_find_event(tevent.pevent, id);
119*4882a593Smuzhiyun }
120