update compat
[lttv.git] / ltt / event.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2007 Mathieu Desnoyers
3 *
4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License Version 2.1 as published by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <glib.h>
26
27 #include <ltt/event.h>
28 #include <ltt/ltt-types.h>
29 #include <ltt/ltt-private.h>
30 #include <ltt/marker.h>
31 #include <ltt/marker-field.h>
32
33 /*****************************************************************************
34 *Function name
35 * ltt_event_position_get : get the event position data
36 *Input params
37 * e : an instance of an event type
38 * ep : a pointer to event's position structure
39 * tf : tracefile pointer
40 * block : current block
41 * offset : current offset
42 * tsc : current tsc
43 ****************************************************************************/
44 void ltt_event_position_get(LttEventPosition *ep, LttTracefile **tf,
45 guint *block, guint *offset, guint64 *tsc)
46 {
47 *tf = ep->tracefile;
48 *block = ep->block;
49 *offset = ep->offset;
50 *tsc = ep->tsc;
51 }
52
53
54 void ltt_event_position_set(LttEventPosition *ep, LttTracefile *tf,
55 guint block, guint offset, guint64 tsc)
56 {
57 ep->tracefile = tf;
58 ep->block = block;
59 ep->offset = offset;
60 ep->tsc = tsc;
61 }
62
63
64 /*****************************************************************************
65 *Function name
66 * ltt_event_position : get the event's position
67 *Input params
68 * e : an instance of an event type
69 * ep : a pointer to event's position structure
70 ****************************************************************************/
71
72 void ltt_event_position(LttEvent *e, LttEventPosition *ep)
73 {
74 ep->tracefile = e->tracefile;
75 ep->block = e->block;
76 ep->offset = e->offset;
77 ep->tsc = e->tsc;
78 }
79
80 LttEventPosition * ltt_event_position_new()
81 {
82 return g_new(LttEventPosition, 1);
83 }
84
85
86 /*****************************************************************************
87 * Function name
88 * ltt_event_position_compare : compare two positions
89 * A NULL value is infinite.
90 * Input params
91 * ep1 : a pointer to event's position structure
92 * ep2 : a pointer to event's position structure
93 * Return
94 * -1 is ep1 < ep2
95 * 1 if ep1 > ep2
96 * 0 if ep1 == ep2
97 ****************************************************************************/
98
99
100 gint ltt_event_position_compare(const LttEventPosition *ep1,
101 const LttEventPosition *ep2)
102 {
103 if(ep1 == NULL && ep2 == NULL)
104 return 0;
105 if(ep1 != NULL && ep2 == NULL)
106 return -1;
107 if(ep1 == NULL && ep2 != NULL)
108 return 1;
109
110 if(ep1->tracefile != ep2->tracefile)
111 g_error("ltt_event_position_compare on different tracefiles makes no sense");
112
113 if(ep1->block < ep2->block)
114 return -1;
115 if(ep1->block > ep2->block)
116 return 1;
117 if(ep1->offset < ep2->offset)
118 return -1;
119 if(ep1->offset > ep2->offset)
120 return 1;
121 return 0;
122 }
123
124 /*****************************************************************************
125 * Function name
126 * ltt_event_position_copy : copy position
127 * Input params
128 * src : a pointer to event's position structure source
129 * dest : a pointer to event's position structure dest
130 * Return
131 * void
132 ****************************************************************************/
133 void ltt_event_position_copy(LttEventPosition *dest,
134 const LttEventPosition *src)
135 {
136 if(src == NULL)
137 dest = NULL;
138 else
139 *dest = *src;
140 }
141
142
143
144 LttTracefile *ltt_event_position_tracefile(LttEventPosition *ep)
145 {
146 return ep->tracefile;
147 }
148
149 /*****************************************************************************
150 * These functions extract data from an event after architecture specific
151 * conversions
152 ****************************************************************************/
153 guint32 ltt_event_get_unsigned(LttEvent *e, struct marker_field *f)
154 {
155 gboolean reverse_byte_order;
156 struct LttField *eventfield;
157 int offset, size;
158
159 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
160 reverse_byte_order = (g_ntohs(0x1) != 0x1);
161 } else {
162 reverse_byte_order = LTT_GET_BO(e->tracefile);
163 }
164
165 eventfield = ltt_event_field(e, marker_field_get_index(f));
166 offset = eventfield->offset;
167 size = eventfield->size;
168 switch(size) {
169 case 1:
170 {
171 guint8 x = *(guint8 *)(e->data + offset);
172 return (guint32) x;
173 }
174 break;
175 case 2:
176 return (guint32)ltt_get_uint16(reverse_byte_order, e->data + offset);
177 break;
178 case 4:
179 return (guint32)ltt_get_uint32(reverse_byte_order, e->data + offset);
180 break;
181 case 8:
182 default:
183 g_critical("ltt_event_get_unsigned : field size %i unknown", size);
184 return 0;
185 break;
186 }
187 }
188
189 gint32 ltt_event_get_int(LttEvent *e, struct marker_field *f)
190 {
191 gboolean reverse_byte_order;
192 struct LttField *eventfield;
193 int offset, size;
194
195 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
196 reverse_byte_order = (g_ntohs(0x1) != 0x1);
197 } else {
198 reverse_byte_order = LTT_GET_BO(e->tracefile);
199 }
200
201 eventfield = ltt_event_field(e, marker_field_get_index(f));
202 offset = eventfield->offset;
203 size = eventfield->size;
204 switch(size) {
205 case 1:
206 {
207 gint8 x = *(gint8 *)(e->data + offset);
208 return (gint32) x;
209 }
210 break;
211 case 2:
212 return (gint32)ltt_get_int16(reverse_byte_order, e->data + offset);
213 break;
214 case 4:
215 return (gint32)ltt_get_int32(reverse_byte_order, e->data + offset);
216 break;
217 case 8:
218 default:
219 g_critical("ltt_event_get_int : field size %i unknown", size);
220 return 0;
221 break;
222 }
223 }
224
225 guint64 ltt_event_get_long_unsigned(LttEvent *e, struct marker_field *f)
226 {
227 gboolean reverse_byte_order;
228 struct LttField *eventfield;
229 int offset, size;
230
231 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
232 reverse_byte_order = (g_ntohs(0x1) != 0x1);
233 } else {
234 reverse_byte_order = LTT_GET_BO(e->tracefile);
235 }
236
237 eventfield = ltt_event_field(e, marker_field_get_index(f));
238 offset = eventfield->offset;
239 size = eventfield->size;
240 switch(size) {
241 case 1:
242 {
243 guint8 x = *(guint8 *)(e->data + offset);
244 return (guint64) x;
245 }
246 break;
247 case 2:
248 return (guint64)ltt_get_uint16(reverse_byte_order, e->data + offset);
249 break;
250 case 4:
251 return (guint64)ltt_get_uint32(reverse_byte_order, e->data + offset);
252 break;
253 case 8:
254 return ltt_get_uint64(reverse_byte_order, e->data + offset);
255 break;
256 default:
257 g_critical("ltt_event_get_long_unsigned : field size %i unknown", size);
258 return 0;
259 break;
260 }
261 }
262
263 gint64 ltt_event_get_long_int(LttEvent *e, struct marker_field *f)
264 {
265 gboolean reverse_byte_order;
266 struct LttField *eventfield;
267 int offset, size;
268
269 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
270 reverse_byte_order = (g_ntohs(0x1) != 0x1);
271 } else {
272 reverse_byte_order = LTT_GET_BO(e->tracefile);
273 }
274
275 eventfield = ltt_event_field(e, marker_field_get_index(f));
276 offset = eventfield->offset;
277 size = eventfield->size;
278 switch(size) {
279 case 1:
280 {
281 gint8 x = *(gint8 *)(e->data + offset);
282 return (gint64) x;
283 }
284 break;
285 case 2:
286 return (gint64)ltt_get_int16(reverse_byte_order, e->data + offset);
287 break;
288 case 4:
289 return (gint64)ltt_get_int32(reverse_byte_order, e->data + offset);
290 break;
291 case 8:
292 return ltt_get_int64(reverse_byte_order, e->data + offset);
293 break;
294 default:
295 g_critical("ltt_event_get_long_int : field size %i unknown", size);
296 return 0;
297 break;
298 }
299 }
300
301 #if 0
302 float ltt_event_get_float(LttEvent *e, struct marker_field *f)
303 {
304 gboolean reverse_byte_order;
305 struct LttField *eventfield;
306 int offset, size;
307
308 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
309 reverse_byte_order = (g_ntohs(0x1) != 0x1);
310 } else {
311 g_assert(LTT_HAS_FLOAT(e->tracefile));
312 reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
313 }
314
315 eventfield = ltt_event_field(e, marker_field_get_index(f));
316 offset = eventfield->offset;
317 size = eventfield->size;
318 g_assert(f->field_type.type_class == LTT_FLOAT && size == 4);
319
320 if(reverse_byte_order == 0) return *(float *)(e->data + offset);
321 else{
322 void *ptr = e->data + offset;
323 guint32 value = bswap_32(*(guint32*)ptr);
324 return *(float*)&value;
325 }
326 }
327
328 double ltt_event_get_double(LttEvent *e, struct marker_field *f)
329 {
330 gboolean reverse_byte_order;
331 struct LttField *eventfield;
332 int offset, size;
333
334 if(unlikely(f->attributes & LTT_ATTRIBUTE_NETWORK_BYTE_ORDER)) {
335 reverse_byte_order = (g_ntohs(0x1) != 0x1);
336 } else {
337 g_assert(LTT_HAS_FLOAT(e->tracefile));
338 reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
339 }
340
341 eventfield = ltt_event_field(e, marker_field_get_index(f));
342 offset = eventfield->offset;
343 size = eventfield->size;
344 if(size == 4)
345 return ltt_event_get_float(e, f);
346
347 g_assert(f->field_type.type_class == LTT_FLOAT && size == 8);
348
349 if(reverse_byte_order == 0) return *(double *)(e->data + offset);
350 else {
351 void *ptr = e->data + offset;
352 guint64 value = bswap_64(*(guint64*)ptr);
353 return *(double*)&value;
354 }
355 }
356 #endif
357
358 /*****************************************************************************
359 * The string obtained is only valid until the next read from
360 * the same tracefile. We reference directly the buffers.
361 ****************************************************************************/
362 gchar *ltt_event_get_string(LttEvent *e, struct marker_field *f)
363 {
364 struct LttField *eventfield;
365 int offset;
366
367 g_assert(f->type == LTT_TYPE_STRING);
368
369 //caused memory leaks
370 //return (gchar*)g_strdup((gchar*)(e->data + f->offset));
371 eventfield = ltt_event_field(e, marker_field_get_index(f));
372 offset = eventfield->offset;
373 return (gchar*)(e->data + offset);
374 }
375
376
This page took 0.036255 seconds and 4 git commands to generate.