1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4  *
5  */
6 
7 #include "event-parse.h"
8 #include "event-parse-local.h"
9 #include "event-utils.h"
10 
11 /**
12  * tep_get_event - returns the event with the given index
13  * @tep: a handle to the tep_handle
14  * @index: index of the requested event, in the range 0 .. nr_events
15  *
16  * This returns pointer to the element of the events array with the given index
17  * If @tep is NULL, or @index is not in the range 0 .. nr_events, NULL is returned.
18  */
tep_get_event(struct tep_handle * tep,int index)19 struct tep_event *tep_get_event(struct tep_handle *tep, int index)
20 {
21 	if (tep && tep->events && index < tep->nr_events)
22 		return tep->events[index];
23 
24 	return NULL;
25 }
26 
27 /**
28  * tep_get_first_event - returns the first event in the events array
29  * @tep: a handle to the tep_handle
30  *
31  * This returns pointer to the first element of the events array
32  * If @tep is NULL, NULL is returned.
33  */
tep_get_first_event(struct tep_handle * tep)34 struct tep_event *tep_get_first_event(struct tep_handle *tep)
35 {
36 	return tep_get_event(tep, 0);
37 }
38 
39 /**
40  * tep_get_events_count - get the number of defined events
41  * @tep: a handle to the tep_handle
42  *
43  * This returns number of elements in event array
44  * If @tep is NULL, 0 is returned.
45  */
tep_get_events_count(struct tep_handle * tep)46 int tep_get_events_count(struct tep_handle *tep)
47 {
48 	if (tep)
49 		return tep->nr_events;
50 	return 0;
51 }
52 
53 /**
54  * tep_set_flag - set event parser flag
55  * @tep: a handle to the tep_handle
56  * @flag: flag, or combination of flags to be set
57  * can be any combination from enum tep_flag
58  *
59  * This sets a flag or combination of flags from enum tep_flag
60  */
tep_set_flag(struct tep_handle * tep,int flag)61 void tep_set_flag(struct tep_handle *tep, int flag)
62 {
63 	if (tep)
64 		tep->flags |= flag;
65 }
66 
67 /**
68  * tep_clear_flag - clear event parser flag
69  * @tep: a handle to the tep_handle
70  * @flag: flag to be cleared
71  *
72  * This clears a tep flag
73  */
tep_clear_flag(struct tep_handle * tep,enum tep_flag flag)74 void tep_clear_flag(struct tep_handle *tep, enum tep_flag flag)
75 {
76 	if (tep)
77 		tep->flags &= ~flag;
78 }
79 
80 /**
81  * tep_test_flag - check the state of event parser flag
82  * @tep: a handle to the tep_handle
83  * @flag: flag to be checked
84  *
85  * This returns the state of the requested tep flag.
86  * Returns: true if the flag is set, false otherwise.
87  */
tep_test_flag(struct tep_handle * tep,enum tep_flag flag)88 bool tep_test_flag(struct tep_handle *tep, enum tep_flag flag)
89 {
90 	if (tep)
91 		return tep->flags & flag;
92 	return false;
93 }
94 
tep_data2host2(struct tep_handle * tep,unsigned short data)95 unsigned short tep_data2host2(struct tep_handle *tep, unsigned short data)
96 {
97 	unsigned short swap;
98 
99 	if (!tep || tep->host_bigendian == tep->file_bigendian)
100 		return data;
101 
102 	swap = ((data & 0xffULL) << 8) |
103 		((data & (0xffULL << 8)) >> 8);
104 
105 	return swap;
106 }
107 
tep_data2host4(struct tep_handle * tep,unsigned int data)108 unsigned int tep_data2host4(struct tep_handle *tep, unsigned int data)
109 {
110 	unsigned int swap;
111 
112 	if (!tep || tep->host_bigendian == tep->file_bigendian)
113 		return data;
114 
115 	swap = ((data & 0xffULL) << 24) |
116 		((data & (0xffULL << 8)) << 8) |
117 		((data & (0xffULL << 16)) >> 8) |
118 		((data & (0xffULL << 24)) >> 24);
119 
120 	return swap;
121 }
122 
123 unsigned long long
tep_data2host8(struct tep_handle * tep,unsigned long long data)124 tep_data2host8(struct tep_handle *tep, unsigned long long data)
125 {
126 	unsigned long long swap;
127 
128 	if (!tep || tep->host_bigendian == tep->file_bigendian)
129 		return data;
130 
131 	swap = ((data & 0xffULL) << 56) |
132 		((data & (0xffULL << 8)) << 40) |
133 		((data & (0xffULL << 16)) << 24) |
134 		((data & (0xffULL << 24)) << 8) |
135 		((data & (0xffULL << 32)) >> 8) |
136 		((data & (0xffULL << 40)) >> 24) |
137 		((data & (0xffULL << 48)) >> 40) |
138 		((data & (0xffULL << 56)) >> 56);
139 
140 	return swap;
141 }
142 
143 /**
144  * tep_get_header_page_size - get size of the header page
145  * @tep: a handle to the tep_handle
146  *
147  * This returns size of the header page
148  * If @tep is NULL, 0 is returned.
149  */
tep_get_header_page_size(struct tep_handle * tep)150 int tep_get_header_page_size(struct tep_handle *tep)
151 {
152 	if (tep)
153 		return tep->header_page_size_size;
154 	return 0;
155 }
156 
157 /**
158  * tep_get_header_timestamp_size - get size of the timestamp in the header page
159  * @tep: a handle to the tep_handle
160  *
161  * This returns size of the timestamp in the header page
162  * If @tep is NULL, 0 is returned.
163  */
tep_get_header_timestamp_size(struct tep_handle * tep)164 int tep_get_header_timestamp_size(struct tep_handle *tep)
165 {
166 	if (tep)
167 		return tep->header_page_ts_size;
168 	return 0;
169 }
170 
171 /**
172  * tep_get_cpus - get the number of CPUs
173  * @tep: a handle to the tep_handle
174  *
175  * This returns the number of CPUs
176  * If @tep is NULL, 0 is returned.
177  */
tep_get_cpus(struct tep_handle * tep)178 int tep_get_cpus(struct tep_handle *tep)
179 {
180 	if (tep)
181 		return tep->cpus;
182 	return 0;
183 }
184 
185 /**
186  * tep_set_cpus - set the number of CPUs
187  * @tep: a handle to the tep_handle
188  *
189  * This sets the number of CPUs
190  */
tep_set_cpus(struct tep_handle * tep,int cpus)191 void tep_set_cpus(struct tep_handle *tep, int cpus)
192 {
193 	if (tep)
194 		tep->cpus = cpus;
195 }
196 
197 /**
198  * tep_get_long_size - get the size of a long integer on the traced machine
199  * @tep: a handle to the tep_handle
200  *
201  * This returns the size of a long integer on the traced machine
202  * If @tep is NULL, 0 is returned.
203  */
tep_get_long_size(struct tep_handle * tep)204 int tep_get_long_size(struct tep_handle *tep)
205 {
206 	if (tep)
207 		return tep->long_size;
208 	return 0;
209 }
210 
211 /**
212  * tep_set_long_size - set the size of a long integer on the traced machine
213  * @tep: a handle to the tep_handle
214  * @size: size, in bytes, of a long integer
215  *
216  * This sets the size of a long integer on the traced machine
217  */
tep_set_long_size(struct tep_handle * tep,int long_size)218 void tep_set_long_size(struct tep_handle *tep, int long_size)
219 {
220 	if (tep)
221 		tep->long_size = long_size;
222 }
223 
224 /**
225  * tep_get_page_size - get the size of a memory page on the traced machine
226  * @tep: a handle to the tep_handle
227  *
228  * This returns the size of a memory page on the traced machine
229  * If @tep is NULL, 0 is returned.
230  */
tep_get_page_size(struct tep_handle * tep)231 int tep_get_page_size(struct tep_handle *tep)
232 {
233 	if (tep)
234 		return tep->page_size;
235 	return 0;
236 }
237 
238 /**
239  * tep_set_page_size - set the size of a memory page on the traced machine
240  * @tep: a handle to the tep_handle
241  * @_page_size: size of a memory page, in bytes
242  *
243  * This sets the size of a memory page on the traced machine
244  */
tep_set_page_size(struct tep_handle * tep,int _page_size)245 void tep_set_page_size(struct tep_handle *tep, int _page_size)
246 {
247 	if (tep)
248 		tep->page_size = _page_size;
249 }
250 
251 /**
252  * tep_is_file_bigendian - return the endian of the file
253  * @tep: a handle to the tep_handle
254  *
255  * This returns true if the file is in big endian order
256  * If @tep is NULL, false is returned.
257  */
tep_is_file_bigendian(struct tep_handle * tep)258 bool tep_is_file_bigendian(struct tep_handle *tep)
259 {
260 	if (tep)
261 		return (tep->file_bigendian == TEP_BIG_ENDIAN);
262 	return false;
263 }
264 
265 /**
266  * tep_set_file_bigendian - set if the file is in big endian order
267  * @tep: a handle to the tep_handle
268  * @endian: non zero, if the file is in big endian order
269  *
270  * This sets if the file is in big endian order
271  */
tep_set_file_bigendian(struct tep_handle * tep,enum tep_endian endian)272 void tep_set_file_bigendian(struct tep_handle *tep, enum tep_endian endian)
273 {
274 	if (tep)
275 		tep->file_bigendian = endian;
276 }
277 
278 /**
279  * tep_is_local_bigendian - return the endian of the saved local machine
280  * @tep: a handle to the tep_handle
281  *
282  * This returns true if the saved local machine in @tep is big endian.
283  * If @tep is NULL, false is returned.
284  */
tep_is_local_bigendian(struct tep_handle * tep)285 bool tep_is_local_bigendian(struct tep_handle *tep)
286 {
287 	if (tep)
288 		return (tep->host_bigendian == TEP_BIG_ENDIAN);
289 	return 0;
290 }
291 
292 /**
293  * tep_set_local_bigendian - set the stored local machine endian order
294  * @tep: a handle to the tep_handle
295  * @endian: non zero, if the local host has big endian order
296  *
297  * This sets the endian order for the local machine.
298  */
tep_set_local_bigendian(struct tep_handle * tep,enum tep_endian endian)299 void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian)
300 {
301 	if (tep)
302 		tep->host_bigendian = endian;
303 }
304 
305 /**
306  * tep_is_old_format - get if an old kernel is used
307  * @tep: a handle to the tep_handle
308  *
309  * This returns true, if an old kernel is used to generate the tracing events or
310  * false if a new kernel is used. Old kernels did not have header page info.
311  * If @tep is NULL, false is returned.
312  */
tep_is_old_format(struct tep_handle * tep)313 bool tep_is_old_format(struct tep_handle *tep)
314 {
315 	if (tep)
316 		return tep->old_format;
317 	return false;
318 }
319 
320 /**
321  * tep_set_test_filters - set a flag to test a filter string
322  * @tep: a handle to the tep_handle
323  * @test_filters: the new value of the test_filters flag
324  *
325  * This sets a flag to test a filter string. If this flag is set, when
326  * tep_filter_add_filter_str() API as called,it will print the filter string
327  * instead of adding it.
328  */
tep_set_test_filters(struct tep_handle * tep,int test_filters)329 void tep_set_test_filters(struct tep_handle *tep, int test_filters)
330 {
331 	if (tep)
332 		tep->test_filters = test_filters;
333 }
334