1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2007 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
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.
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.
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.
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>
33 /*****************************************************************************
35 * ltt_event_position_get : get the event position data
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
43 ****************************************************************************/
44 void ltt_event_position_get(LttEventPosition
*ep
, LttTracefile
**tf
,
45 guint
*block
, guint
*offset
, guint64
*tsc
)
54 void ltt_event_position_set(LttEventPosition
*ep
, LttTracefile
*tf
,
55 guint block
, guint offset
, guint64 tsc
)
64 /*****************************************************************************
66 * ltt_event_position : get the event's position
68 * e : an instance of an event type
69 * ep : a pointer to event's position structure
70 ****************************************************************************/
72 void ltt_event_position(LttEvent
*e
, LttEventPosition
*ep
)
74 ep
->tracefile
= e
->tracefile
;
76 ep
->offset
= e
->offset
;
80 LttEventPosition
* ltt_event_position_new()
82 return g_new(LttEventPosition
, 1);
86 /*****************************************************************************
88 * ltt_event_position_compare : compare two positions
89 * A NULL value is infinite.
91 * ep1 : a pointer to event's position structure
92 * ep2 : a pointer to event's position structure
97 ****************************************************************************/
100 gint
ltt_event_position_compare(const LttEventPosition
*ep1
,
101 const LttEventPosition
*ep2
)
103 if(ep1
== NULL
&& ep2
== NULL
)
105 if(ep1
!= NULL
&& ep2
== NULL
)
107 if(ep1
== NULL
&& ep2
!= NULL
)
110 if(ep1
->tracefile
!= ep2
->tracefile
)
111 g_error("ltt_event_position_compare on different tracefiles makes no sense");
113 if(ep1
->block
< ep2
->block
)
115 if(ep1
->block
> ep2
->block
)
117 if(ep1
->offset
< ep2
->offset
)
119 if(ep1
->offset
> ep2
->offset
)
124 /*****************************************************************************
126 * ltt_event_position_copy : copy position
128 * src : a pointer to event's position structure source
129 * dest : a pointer to event's position structure dest
132 ****************************************************************************/
133 void ltt_event_position_copy(LttEventPosition
*dest
,
134 const LttEventPosition
*src
)
144 LttTracefile
*ltt_event_position_tracefile(LttEventPosition
*ep
)
146 return ep
->tracefile
;
149 /*****************************************************************************
150 * These functions extract data from an event after architecture specific
152 ****************************************************************************/
153 guint32
ltt_event_get_unsigned(LttEvent
*e
, struct marker_field
*f
)
155 gboolean reverse_byte_order
;
156 struct LttField
*eventfield
;
159 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
160 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
162 reverse_byte_order
= LTT_GET_BO(e
->tracefile
);
165 eventfield
= ltt_event_field(e
, marker_field_get_index(f
));
166 offset
= eventfield
->offset
;
167 size
= eventfield
->size
;
171 guint8 x
= *(guint8
*)(e
->data
+ offset
);
176 return (guint32
)ltt_get_uint16(reverse_byte_order
, e
->data
+ offset
);
179 return (guint32
)ltt_get_uint32(reverse_byte_order
, e
->data
+ offset
);
183 g_critical("ltt_event_get_unsigned : field size %i unknown", size
);
189 gint32
ltt_event_get_int(LttEvent
*e
, struct marker_field
*f
)
191 gboolean reverse_byte_order
;
192 struct LttField
*eventfield
;
195 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
196 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
198 reverse_byte_order
= LTT_GET_BO(e
->tracefile
);
201 eventfield
= ltt_event_field(e
, marker_field_get_index(f
));
202 offset
= eventfield
->offset
;
203 size
= eventfield
->size
;
207 gint8 x
= *(gint8
*)(e
->data
+ offset
);
212 return (gint32
)ltt_get_int16(reverse_byte_order
, e
->data
+ offset
);
215 return (gint32
)ltt_get_int32(reverse_byte_order
, e
->data
+ offset
);
219 g_critical("ltt_event_get_int : field size %i unknown", size
);
225 guint64
ltt_event_get_long_unsigned(LttEvent
*e
, struct marker_field
*f
)
227 gboolean reverse_byte_order
;
228 struct LttField
*eventfield
;
231 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
232 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
234 reverse_byte_order
= LTT_GET_BO(e
->tracefile
);
237 eventfield
= ltt_event_field(e
, marker_field_get_index(f
));
238 offset
= eventfield
->offset
;
239 size
= eventfield
->size
;
243 guint8 x
= *(guint8
*)(e
->data
+ offset
);
248 return (guint64
)ltt_get_uint16(reverse_byte_order
, e
->data
+ offset
);
251 return (guint64
)ltt_get_uint32(reverse_byte_order
, e
->data
+ offset
);
254 return ltt_get_uint64(reverse_byte_order
, e
->data
+ offset
);
257 g_critical("ltt_event_get_long_unsigned : field size %i unknown", size
);
263 gint64
ltt_event_get_long_int(LttEvent
*e
, struct marker_field
*f
)
265 gboolean reverse_byte_order
;
266 struct LttField
*eventfield
;
269 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
270 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
272 reverse_byte_order
= LTT_GET_BO(e
->tracefile
);
275 eventfield
= ltt_event_field(e
, marker_field_get_index(f
));
276 offset
= eventfield
->offset
;
277 size
= eventfield
->size
;
281 gint8 x
= *(gint8
*)(e
->data
+ offset
);
286 return (gint64
)ltt_get_int16(reverse_byte_order
, e
->data
+ offset
);
289 return (gint64
)ltt_get_int32(reverse_byte_order
, e
->data
+ offset
);
292 return ltt_get_int64(reverse_byte_order
, e
->data
+ offset
);
295 g_critical("ltt_event_get_long_int : field size %i unknown", size
);
302 float ltt_event_get_float(LttEvent
*e
, struct marker_field
*f
)
304 gboolean reverse_byte_order
;
305 struct LttField
*eventfield
;
308 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
309 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
311 g_assert(LTT_HAS_FLOAT(e
->tracefile
));
312 reverse_byte_order
= LTT_GET_FLOAT_BO(e
->tracefile
);
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);
320 if(reverse_byte_order
== 0) return *(float *)(e
->data
+ offset
);
322 void *ptr
= e
->data
+ offset
;
323 guint32 value
= bswap_32(*(guint32
*)ptr
);
324 return *(float*)&value
;
328 double ltt_event_get_double(LttEvent
*e
, struct marker_field
*f
)
330 gboolean reverse_byte_order
;
331 struct LttField
*eventfield
;
334 if(unlikely(f
->attributes
& LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
)) {
335 reverse_byte_order
= (g_ntohs(0x1) != 0x1);
337 g_assert(LTT_HAS_FLOAT(e
->tracefile
));
338 reverse_byte_order
= LTT_GET_FLOAT_BO(e
->tracefile
);
341 eventfield
= ltt_event_field(e
, marker_field_get_index(f
));
342 offset
= eventfield
->offset
;
343 size
= eventfield
->size
;
345 return ltt_event_get_float(e
, f
);
347 g_assert(f
->field_type
.type_class
== LTT_FLOAT
&& size
== 8);
349 if(reverse_byte_order
== 0) return *(double *)(e
->data
+ offset
);
351 void *ptr
= e
->data
+ offset
;
352 guint64 value
= bswap_64(*(guint64
*)ptr
);
353 return *(double*)&value
;
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
)
364 struct LttField
*eventfield
;
367 g_assert(f
->type
== LTT_TYPE_STRING
);
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
);