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