xref: /aosp_15_r20/external/libtraceevent/src/event-parse-api.c (revision 436bf2bcd5202612ffffe471bbcc1f277cc8d28e)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <[email protected]>
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_get_events_count - get the number of defined events
55  * @tep: a handle to the tep_handle
56  *
57  * This returns number of elements in event array
58  * If @tep is NULL, 0 is returned.
59  */
tep_get_function_count(struct tep_handle * tep)60 int tep_get_function_count(struct tep_handle *tep)
61 {
62 	if (tep)
63 		return tep->func_count;
64 	return 0;
65 }
66 
67 /**
68  * tep_set_flag - set event parser flag
69  * @tep: a handle to the tep_handle
70  * @flag: flag, or combination of flags to be set
71  * can be any combination from enum tep_flag
72  *
73  * This sets a flag or combination of flags from enum tep_flag
74  */
tep_set_flag(struct tep_handle * tep,int flag)75 void tep_set_flag(struct tep_handle *tep, int flag)
76 {
77 	if (tep)
78 		tep->flags |= flag;
79 }
80 
81 /**
82  * tep_clear_flag - clear event parser flag
83  * @tep: a handle to the tep_handle
84  * @flag: flag to be cleared
85  *
86  * This clears a tep flag
87  */
tep_clear_flag(struct tep_handle * tep,enum tep_flag flag)88 void tep_clear_flag(struct tep_handle *tep, enum tep_flag flag)
89 {
90 	if (tep)
91 		tep->flags &= ~flag;
92 }
93 
94 /**
95  * tep_test_flag - check the state of event parser flag
96  * @tep: a handle to the tep_handle
97  * @flag: flag to be checked
98  *
99  * This returns the state of the requested tep flag.
100  * Returns: true if the flag is set, false otherwise.
101  */
tep_test_flag(struct tep_handle * tep,enum tep_flag flag)102 bool tep_test_flag(struct tep_handle *tep, enum tep_flag flag)
103 {
104 	if (tep)
105 		return tep->flags & flag;
106 	return false;
107 }
108 
data2host2(struct tep_handle * tep,unsigned short data)109 __hidden unsigned short data2host2(struct tep_handle *tep, unsigned short data)
110 {
111 	unsigned short swap;
112 
113 	if (!tep || tep->host_bigendian == tep->file_bigendian)
114 		return data;
115 
116 	swap = ((data & 0xffULL) << 8) |
117 		((data & (0xffULL << 8)) >> 8);
118 
119 	return swap;
120 }
121 
data2host4(struct tep_handle * tep,unsigned int data)122 __hidden unsigned int data2host4(struct tep_handle *tep, unsigned int data)
123 {
124 	unsigned int swap;
125 
126 	if (!tep || tep->host_bigendian == tep->file_bigendian)
127 		return data;
128 
129 	swap = ((data & 0xffULL) << 24) |
130 		((data & (0xffULL << 8)) << 8) |
131 		((data & (0xffULL << 16)) >> 8) |
132 		((data & (0xffULL << 24)) >> 24);
133 
134 	return swap;
135 }
136 
137 __hidden  unsigned long long
data2host8(struct tep_handle * tep,unsigned long long data)138 data2host8(struct tep_handle *tep, unsigned long long data)
139 {
140 	unsigned long long swap;
141 
142 	if (!tep || tep->host_bigendian == tep->file_bigendian)
143 		return data;
144 
145 	swap = ((data & 0xffULL) << 56) |
146 		((data & (0xffULL << 8)) << 40) |
147 		((data & (0xffULL << 16)) << 24) |
148 		((data & (0xffULL << 24)) << 8) |
149 		((data & (0xffULL << 32)) >> 8) |
150 		((data & (0xffULL << 40)) >> 24) |
151 		((data & (0xffULL << 48)) >> 40) |
152 		((data & (0xffULL << 56)) >> 56);
153 
154 	return swap;
155 }
156 
157 /**
158  * tep_get_header_page_size - get size of the header page
159  * @tep: a handle to the tep_handle
160  *
161  * This returns size of the header page
162  * If @tep is NULL, 0 is returned.
163  */
tep_get_header_page_size(struct tep_handle * tep)164 int tep_get_header_page_size(struct tep_handle *tep)
165 {
166 	if (tep)
167 		return tep->header_page_size_size;
168 	return 0;
169 }
170 
171 /**
172  * tep_get_header_timestamp_size - get size of the timestamp in the header page
173  * @tep: a handle to the tep_handle
174  *
175  * This returns size of the timestamp in the header page
176  * If @tep is NULL, 0 is returned.
177  */
tep_get_header_timestamp_size(struct tep_handle * tep)178 int tep_get_header_timestamp_size(struct tep_handle *tep)
179 {
180 	if (tep)
181 		return tep->header_page_ts_size;
182 	return 0;
183 }
184 
185 /**
186  * tep_get_cpus - get the number of CPUs
187  * @tep: a handle to the tep_handle
188  *
189  * This returns the number of CPUs
190  * If @tep is NULL, 0 is returned.
191  */
tep_get_cpus(struct tep_handle * tep)192 int tep_get_cpus(struct tep_handle *tep)
193 {
194 	if (tep)
195 		return tep->cpus;
196 	return 0;
197 }
198 
199 /**
200  * tep_set_cpus - set the number of CPUs
201  * @tep: a handle to the tep_handle
202  *
203  * This sets the number of CPUs
204  */
tep_set_cpus(struct tep_handle * tep,int cpus)205 void tep_set_cpus(struct tep_handle *tep, int cpus)
206 {
207 	if (tep)
208 		tep->cpus = cpus;
209 }
210 
211 /**
212  * tep_get_long_size - get the size of a long integer on the traced machine
213  * @tep: a handle to the tep_handle
214  *
215  * This returns the size of a long integer on the traced machine
216  * If @tep is NULL, 0 is returned.
217  */
tep_get_long_size(struct tep_handle * tep)218 int tep_get_long_size(struct tep_handle *tep)
219 {
220 	if (tep)
221 		return tep->long_size;
222 	return 0;
223 }
224 
225 /**
226  * tep_set_long_size - set the size of a long integer on the traced machine
227  * @tep: a handle to the tep_handle
228  * @size: size, in bytes, of a long integer
229  *
230  * This sets the size of a long integer on the traced machine
231  */
tep_set_long_size(struct tep_handle * tep,int long_size)232 void tep_set_long_size(struct tep_handle *tep, int long_size)
233 {
234 	if (tep)
235 		tep->long_size = long_size;
236 }
237 
238 /**
239  * tep_get_page_size - get the size of a memory page on the traced machine
240  * @tep: a handle to the tep_handle
241  *
242  * This returns the size of a memory page on the traced machine
243  * If @tep is NULL, 0 is returned.
244  */
tep_get_page_size(struct tep_handle * tep)245 int tep_get_page_size(struct tep_handle *tep)
246 {
247 	if (tep)
248 		return tep->page_size;
249 	return 0;
250 }
251 
252 /**
253  * tep_set_page_size - set the size of a memory page on the traced machine
254  * @tep: a handle to the tep_handle
255  * @_page_size: size of a memory page, in bytes
256  *
257  * This sets the size of a memory page on the traced machine
258  */
tep_set_page_size(struct tep_handle * tep,int _page_size)259 void tep_set_page_size(struct tep_handle *tep, int _page_size)
260 {
261 	if (tep)
262 		tep->page_size = _page_size;
263 }
264 
265 /**
266  * tep_get_sub_buffer_data_size - get the size of the data portion
267  * @tep: The handle to the tep to get the data size from
268  *
269  * Returns the size of the data portion of the sub buffer
270  */
tep_get_sub_buffer_data_size(struct tep_handle * tep)271 int tep_get_sub_buffer_data_size(struct tep_handle *tep)
272 {
273 	if (!tep)
274 		return -1;
275 
276 	return tep->header_page_data_size;
277 }
278 
279 /**
280  * tep_get_sub_buffer_size - get the size of a trace buffer page
281  * @tep: a handle to the tep_handle
282  *
283  * This returns the size of a trace buffer page on the traced machine.
284  * If @tep is NULL then -1 is returned.
285  */
tep_get_sub_buffer_size(struct tep_handle * tep)286 int tep_get_sub_buffer_size(struct tep_handle *tep)
287 {
288 	if (!tep)
289 		return -1;
290 
291 	return tep->header_page_data_size + tep->header_page_data_offset;
292 }
293 
294 /**
295  * tep_get_sub_buffer_commit_offset - return offset of the commit location
296  * @tep: the handle to the tep_handle
297  *
298  * Returns the offset of where to find the "commit" field of the offset.
299  * Use tep_get_header_page_size() to find the size of the commit field.
300  */
tep_get_sub_buffer_commit_offset(struct tep_handle * tep)301 int tep_get_sub_buffer_commit_offset(struct tep_handle *tep)
302 {
303 	if (!tep)
304 		return -1;
305 
306 	return tep->header_page_size_offset;
307 }
308 
309 /**
310  * tep_is_file_bigendian - return the endian of the file
311  * @tep: a handle to the tep_handle
312  *
313  * This returns true if the file is in big endian order
314  * If @tep is NULL, false is returned.
315  */
tep_is_file_bigendian(struct tep_handle * tep)316 bool tep_is_file_bigendian(struct tep_handle *tep)
317 {
318 	if (tep)
319 		return (tep->file_bigendian == TEP_BIG_ENDIAN);
320 	return false;
321 }
322 
323 /**
324  * tep_set_file_bigendian - set if the file is in big endian order
325  * @tep: a handle to the tep_handle
326  * @endian: non zero, if the file is in big endian order
327  *
328  * This sets if the file is in big endian order
329  */
tep_set_file_bigendian(struct tep_handle * tep,enum tep_endian endian)330 void tep_set_file_bigendian(struct tep_handle *tep, enum tep_endian endian)
331 {
332 	if (tep)
333 		tep->file_bigendian = endian;
334 }
335 
336 /**
337  * tep_is_local_bigendian - return the endian of the saved local machine
338  * @tep: a handle to the tep_handle
339  *
340  * This returns true if the saved local machine in @tep is big endian.
341  * If @tep is NULL, false is returned.
342  */
tep_is_local_bigendian(struct tep_handle * tep)343 bool tep_is_local_bigendian(struct tep_handle *tep)
344 {
345 	if (tep)
346 		return (tep->host_bigendian == TEP_BIG_ENDIAN);
347 	return 0;
348 }
349 
350 /**
351  * tep_set_local_bigendian - set the stored local machine endian order
352  * @tep: a handle to the tep_handle
353  * @endian: non zero, if the local host has big endian order
354  *
355  * This sets the endian order for the local machine.
356  */
tep_set_local_bigendian(struct tep_handle * tep,enum tep_endian endian)357 void tep_set_local_bigendian(struct tep_handle *tep, enum tep_endian endian)
358 {
359 	if (tep)
360 		tep->host_bigendian = endian;
361 }
362 
363 /**
364  * tep_is_old_format - get if an old kernel is used
365  * @tep: a handle to the tep_handle
366  *
367  * This returns true, if an old kernel is used to generate the tracing events or
368  * false if a new kernel is used. Old kernels did not have header page info.
369  * If @tep is NULL, false is returned.
370  */
tep_is_old_format(struct tep_handle * tep)371 bool tep_is_old_format(struct tep_handle *tep)
372 {
373 	if (tep)
374 		return tep->old_format;
375 	return false;
376 }
377 
378 /**
379  * tep_set_test_filters - set a flag to test a filter string
380  * @tep: a handle to the tep_handle
381  * @test_filters: the new value of the test_filters flag
382  *
383  * This sets a flag to test a filter string. If this flag is set, when
384  * tep_filter_add_filter_str() API as called,it will print the filter string
385  * instead of adding it.
386  */
tep_set_test_filters(struct tep_handle * tep,int test_filters)387 void tep_set_test_filters(struct tep_handle *tep, int test_filters)
388 {
389 	if (tep)
390 		tep->test_filters = test_filters;
391 }
392