1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <asm/types.h>
24 #include <linux/byteorder/swab.h>
28 #include "ltt-private.h"
29 #include <ltt/event.h>
30 #include <ltt/trace.h>
33 LttEvent
*ltt_event_new()
35 return g_new(LttEvent
, 1);
38 void ltt_event_destroy(LttEvent
*event
)
44 /*****************************************************************************
46 * ltt_event_refresh_fields : refresh fields of an event
48 * offsetRoot : offset from the root
49 * offsetParent : offset from the parent
53 * int : size of the field
54 ****************************************************************************/
56 int ltt_event_refresh_fields(int offsetRoot
,int offsetParent
,
57 LttField
* fld
, void *evD
)
59 int size
, size1
, element_number
, i
, offset1
, offset2
;
60 LttType
* type
= fld
->field_type
;
62 switch(type
->type_class
) {
64 element_number
= (int) type
->element_number
;
65 if(fld
->field_fixed
== 0){// has string or sequence
67 for(i
=0;i
<element_number
;i
++){
68 size
+= ltt_event_refresh_fields(offsetRoot
+size
,size
,
69 fld
->child
[0], evD
+size
);
71 }else size
= fld
->field_size
;
75 size1
= fld
->sequ_number_size
;
76 element_number
= getIntNumber(size1
,evD
);
77 type
->element_number
= element_number
;
78 if(fld
->element_size
> 0){
79 size
= element_number
* fld
->element_size
;
80 }else{//sequence has string or sequence
82 for(i
=0;i
<element_number
;i
++){
83 size
+= ltt_event_refresh_fields(offsetRoot
+size
+size1
,size
+size1
,
84 fld
->child
[0], evD
+size
+size1
);
91 size
= strlen((char*)evD
) + 1; //include end : '\0'
95 element_number
= (int) type
->element_number
;
96 if(fld
->field_fixed
== 0){
99 for(i
=0;i
<element_number
;i
++){
100 size
=ltt_event_refresh_fields(offset1
,offset2
,
101 fld
->child
[i
],evD
+offset2
);
106 }else size
= fld
->field_size
;
110 size
= fld
->field_size
;
114 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
115 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
116 size
= fld
->field_size
;
117 }else if(type
->type_class
== LTT_ARRAY
){
118 element_number
= (int) type
->element_number
;
119 if(fld
->field_fixed
== 0){// has string or sequence
121 for(i
=0;i
<element_number
;i
++){
122 size
+= ltt_event_refresh_fields(offsetRoot
+size
,size
,
123 fld
->child
[0], evD
+size
);
125 }else size
= fld
->field_size
;
126 }else if(type
->type_class
== LTT_SEQUENCE
){
127 size1
= fld
->sequ_number_size
;
128 element_number
= getIntNumber(size1
,evD
);
129 type
->element_number
= element_number
;
130 if(fld
->element_size
> 0){
131 size
= element_number
* fld
->element_size
;
132 }else{//sequence has string or sequence
134 for(i
=0;i
<element_number
;i
++){
135 size
+= ltt_event_refresh_fields(offsetRoot
+size
+size1
,size
+size1
,
136 fld
->child
[0], evD
+size
+size1
);
140 }else if(type
->type_class
== LTT_STRING
){
141 size
= strlen((char*)evD
) + 1; //include end : '\0'
142 }else if(type
->type_class
== LTT_STRUCT
){
143 element_number
= (int) type
->element_number
;
144 if(fld
->field_fixed
== 0){
145 offset1
= offsetRoot
;
147 for(i
=0;i
<element_number
;i
++){
148 size
=ltt_event_refresh_fields(offset1
,offset2
,
149 fld
->child
[i
],evD
+offset2
);
154 }else size
= fld
->field_size
;
157 fld
->offset_root
= offsetRoot
;
158 fld
->offset_parent
= offsetParent
;
159 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
160 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
161 fld
->field_size
= size
;
166 /*****************************************************************************
168 * ltt_event_eventtype_id: get event type id
169 * (base id + position of the event)
171 * e : an instance of an event type
173 * unsigned : event type id
174 ****************************************************************************/
176 unsigned ltt_event_eventtype_id(LttEvent
*e
)
178 return (unsigned) e
->event_id
;
181 /*****************************************************************************
183 * ltt_event_facility : get the facility of the event
185 * e : an instance of an event type
187 * LttFacility * : the facility of the event
188 ****************************************************************************/
190 LttFacility
*ltt_event_facility(LttEvent
*e
)
192 LttTrace
* trace
= e
->tracefile
->trace
;
193 unsigned id
= e
->event_id
;
194 return ltt_trace_facility_by_id(trace
,id
);
197 /*****************************************************************************
199 * ltt_event_eventtype : get the event type of the event
201 * e : an instance of an event type
203 * LttEventType * : the event type of the event
204 ****************************************************************************/
206 LttEventType
*ltt_event_eventtype(LttEvent
*e
)
208 LttFacility
* facility
= ltt_event_facility(e
);
209 if(!facility
) return NULL
;
210 return facility
->events
[e
->event_id
- facility
->base_id
];
213 /*****************************************************************************
215 * ltt_event_field : get the root field of the event
217 * e : an instance of an event type
219 * LttField * : the root field of the event
220 ****************************************************************************/
222 LttField
*ltt_event_field(LttEvent
*e
)
225 LttEventType
* event_type
= ltt_event_eventtype(e
);
226 if(!event_type
) return NULL
;
227 field
= event_type
->root_field
;
228 if(!field
) return NULL
;
230 //check if the field need refresh
231 if(e
->which_block
!= event_type
->latest_block
||
232 e
->which_event
!= event_type
->latest_event
){
234 event_type
->latest_block
= e
->which_block
;
235 event_type
->latest_event
= e
->which_event
;
237 if(field
->field_fixed
== 1)return field
;
240 ltt_event_refresh_fields(0, 0, field
, e
->data
);
245 /*****************************************************************************
247 * ltt_event_time : get the time of the event
249 * e : an instance of an event type
251 * LttTime : the time of the event
252 ****************************************************************************/
254 LttTime
ltt_event_time(LttEvent
*e
)
256 return e
->event_time
;
259 /*****************************************************************************
261 * ltt_event_time : get the cycle count of the event
263 * e : an instance of an event type
265 * LttCycleCount : the cycle count of the event
266 ****************************************************************************/
268 LttCycleCount
ltt_event_cycle_count(LttEvent
*e
)
270 return e
->event_cycle_count
;
273 /*****************************************************************************
275 * ltt_event_position : get the event's position
277 * e : an instance of an event type
278 * ep : a pointer to event's position structure
279 ****************************************************************************/
281 void ltt_event_position(LttEvent
*e
, LttEventPosition
*ep
)
283 ep
->block_num
= e
->which_block
;
284 ep
->event_num
= e
->which_event
;
285 ep
->event_time
= e
->event_time
;
286 ep
->event_cycle_count
= e
->event_cycle_count
;
287 ep
->heart_beat_number
= e
->tracefile
->cur_heart_beat_number
;
288 ep
->old_position
= TRUE
;
289 ep
->event_offset
= e
->data
- e
->tracefile
->buffer
- EVENT_HEADER_SIZE
;
290 ep
->tf
= e
->tracefile
;
292 /* This is a workaround for fast position seek */
293 ep
->last_event_pos
= e
->last_event_pos
;
294 ep
->prev_block_end_time
= e
->prev_block_end_time
;
295 ep
->prev_event_time
= e
->prev_event_time
;
296 ep
->pre_cycle_count
= e
->pre_cycle_count
;
297 ep
->count
= e
->count
;
298 /* end of workaround */
301 LttEventPosition
* ltt_event_position_new()
303 return g_new(LttEventPosition
, 1);
306 /*****************************************************************************
308 * ltt_event_position_get : get the block number and index of the event
310 * ep : a pointer to event's position structure
311 * block_number : the block number of the event
312 * index_in_block : the index of the event within the block
313 ****************************************************************************/
315 void ltt_event_position_get(LttEventPosition
*ep
,
316 unsigned *block_number
, unsigned *index_in_block
, LttTracefile
** tf
)
318 *block_number
= ep
->block_num
;
319 *index_in_block
= ep
->event_num
;
323 /*****************************************************************************
325 * ltt_event_position_set : set the block number and index of the event
326 * It does put the old_position gboolean to FALSE, as it is impossible
327 * to know the quick position to seek in the tracefile.
329 * ep : a pointer to event's position structure
330 * block_number : the block number of the event
331 * index_in_block : the index of the event within the block
332 ****************************************************************************/
334 void ltt_event_position_set(LttEventPosition
*ep
,
335 unsigned block_number
, unsigned index_in_block
)
337 if(ep
->block_num
!= block_number
|| ep
->event_num
!= index_in_block
)
338 ep
->old_position
= FALSE
;
340 ep
->block_num
= block_number
;
341 ep
->event_num
= index_in_block
;
345 /*****************************************************************************
347 * ltt_event_position_compare : compare two positions
348 * A NULL value is infinite.
350 * ep1 : a pointer to event's position structure
351 * ep2 : a pointer to event's position structure
356 ****************************************************************************/
359 gint
ltt_event_position_compare(const LttEventPosition
*ep1
,
360 const LttEventPosition
*ep2
)
362 if(ep1
->tf
!= ep2
->tf
)
363 g_error("ltt_event_position_compare on different tracefiles makes no sense");
364 if(ep1
== NULL
&& ep2
== NULL
)
366 if(ep1
!= NULL
&& ep2
== NULL
)
368 if(ep1
== NULL
&& ep2
!= NULL
)
371 if(ep1
->block_num
< ep2
->block_num
)
373 if(ep1
->block_num
> ep2
->block_num
)
375 if(ep1
->event_num
< ep2
->event_num
)
377 if(ep1
->event_num
> ep2
->event_num
)
382 /*****************************************************************************
384 * ltt_event_event_position_compare : compare two positions, one in event,
385 * other in position opaque structure.
387 * event : a pointer to event structure
388 * ep : a pointer to event's position structure
393 ****************************************************************************/
395 gint
ltt_event_event_position_compare(const LttEvent
*event
,
396 const LttEventPosition
*ep
)
398 if(event
== NULL
&& ep
== NULL
)
400 if(event
!= NULL
&& ep
== NULL
)
402 if(event
== NULL
&& ep
!= NULL
)
405 g_assert(event
->tracefile
== ep
->tf
);
407 if(event
->which_block
< ep
->block_num
)
409 if(event
->which_block
> ep
->block_num
)
411 if(event
->which_event
< ep
->event_num
)
413 if(event
->which_event
> ep
->event_num
)
418 /*****************************************************************************
420 * ltt_event_position_copy : copy position
422 * src : a pointer to event's position structure source
423 * dest : a pointer to event's position structure dest
426 ****************************************************************************/
427 void ltt_event_position_copy(LttEventPosition
*dest
,
428 const LttEventPosition
*src
)
437 /*****************************************************************************
439 * ltt_event_cpu_i: get the cpu id where the event happens
441 * e : an instance of an event type
443 * unsigned : the cpu id
444 ****************************************************************************/
446 unsigned ltt_event_cpu_id(LttEvent
*e
)
448 char * c1
, * c2
, * c3
;
449 c1
= strrchr(e
->tracefile
->name
,'\\');
450 c2
= strrchr(e
->tracefile
->name
,'/');
451 if(c1
== NULL
&& c2
== NULL
){
452 return (unsigned)atoi(e
->tracefile
->name
);
453 }else if(c1
== NULL
){
455 return (unsigned)atoi(c2
);
456 }else if(c2
== NULL
){
458 return (unsigned)atoi(c1
);
460 c3
= (c1
> c2
) ? c1
: c2
;
462 return (unsigned)atoi(c3
);
466 /*****************************************************************************
468 * ltt_event_data : get the raw data for the event
470 * e : an instance of an event type
472 * void * : pointer to the raw data for the event
473 ****************************************************************************/
475 void *ltt_event_data(LttEvent
*e
)
480 /*****************************************************************************
482 * ltt_event_field_element_number
483 * : The number of elements in a sequence field is specific
484 * to each event. This function returns the number of
485 * elements for an array or sequence field in an event.
487 * e : an instance of an event type
488 * f : a field of the instance
490 * unsigned : the number of elements for an array/sequence field
491 ****************************************************************************/
493 unsigned ltt_event_field_element_number(LttEvent
*e
, LttField
*f
)
495 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
496 f
->field_type
->type_class
!= LTT_SEQUENCE
)
499 if(f
->field_type
->type_class
== LTT_ARRAY
)
500 return f
->field_type
->element_number
;
501 return (unsigned) getIntNumber(f
->sequ_number_size
, e
+ f
->offset_root
);
504 /*****************************************************************************
506 * ltt_event_field_element_select
507 * : Set the currently selected element for a sequence or
510 * e : an instance of an event type
511 * f : a field of the instance
512 * i : the ith element
513 ****************************************************************************/
515 void ltt_event_field_element_select(LttEvent
*e
, LttField
*f
, unsigned i
)
517 unsigned element_number
;
523 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
524 f
->field_type
->type_class
!= LTT_SEQUENCE
)
527 element_number
= ltt_event_field_element_number(e
,f
);
528 /* Sanity check for i : 1..n only, and must be lower or equal element_number
530 if(element_number
< i
|| i
== 0) return;
534 evD
= e
->data
+ f
->offset_root
;
537 size
+= ltt_event_refresh_fields(f
->offset_root
+size
,size
, fld
, evD
+size
);
539 f
->current_element
= i
- 1;
542 /*****************************************************************************
543 * These functions extract data from an event after architecture specific
545 ****************************************************************************/
547 unsigned ltt_event_get_unsigned(LttEvent
*e
, LttField
*f
)
549 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
550 e
->tracefile
->trace
->system_description
->endian
? 0:1;
551 LttTypeEnum t
= f
->field_type
->type_class
;
553 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
555 if(f
->field_size
== 1){
556 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
557 return (unsigned int) x
;
558 }else if(f
->field_size
== 2){
559 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
560 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
561 return (unsigned int) (revFlag
? GUINT16_FROM_BE(x
): x
);
563 return (unsigned int) (revFlag
? GUINT16_FROM_LE(x
): x
);
564 }else if(f
->field_size
== 4){
565 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
566 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
567 return (unsigned int) (revFlag
? GUINT32_FROM_BE(x
): x
);
569 return (unsigned int) (revFlag
? GUINT32_FROM_LE(x
): x
);
570 }else if(f
->field_size
== 8){
571 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
572 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
573 return (unsigned int) (revFlag
? GUINT64_FROM_BE(x
): x
);
575 return (unsigned int) (revFlag
? GUINT64_FROM_LE(x
): x
);
577 g_critical("ltt_event_get_unsigned : field size %i unknown", f
->field_size
);
581 int ltt_event_get_int(LttEvent
*e
, LttField
*f
)
583 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
584 e
->tracefile
->trace
->system_description
->endian
? 0:1;
586 g_assert(f
->field_type
->type_class
== LTT_INT
);
588 if(f
->field_size
== 1){
589 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
591 }else if(f
->field_size
== 2){
592 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
593 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
594 return (int) (revFlag
? GINT16_FROM_BE(x
): x
);
596 return (int) (revFlag
? GINT16_FROM_LE(x
): x
);
597 }else if(f
->field_size
== 4){
598 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
599 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
600 return (int) (revFlag
? GINT32_FROM_BE(x
): x
);
602 return (int) (revFlag
? GINT32_FROM_LE(x
): x
);
603 }else if(f
->field_size
== 8){
604 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
605 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
606 return (int) (revFlag
? GINT64_FROM_BE(x
): x
);
608 return (int) (revFlag
? GINT64_FROM_LE(x
): x
);
610 g_critical("ltt_event_get_int : field size %i unknown", f
->field_size
);
614 unsigned long ltt_event_get_long_unsigned(LttEvent
*e
, LttField
*f
)
616 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
617 e
->tracefile
->trace
->system_description
->endian
? 0:1;
618 LttTypeEnum t
= f
->field_type
->type_class
;
620 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
622 if(f
->field_size
== 1){
623 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
624 return (unsigned long) x
;
625 }else if(f
->field_size
== 2){
626 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
627 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
628 return (unsigned long) (revFlag
? GUINT16_FROM_BE(x
): x
);
630 return (unsigned long) (revFlag
? GUINT16_FROM_LE(x
): x
);
631 }else if(f
->field_size
== 4){
632 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
633 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
634 return (unsigned long) (revFlag
? GUINT32_FROM_BE(x
): x
);
636 return (unsigned long) (revFlag
? GUINT32_FROM_LE(x
): x
);
637 }else if(f
->field_size
== 8){
638 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
639 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
640 return (unsigned long) (revFlag
? GUINT64_FROM_BE(x
): x
);
642 return (unsigned long) (revFlag
? GUINT64_FROM_LE(x
): x
);
644 g_critical("ltt_event_get_long_unsigned : field size %i unknown", f
->field_size
);
648 long int ltt_event_get_long_int(LttEvent
*e
, LttField
*f
)
650 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
651 e
->tracefile
->trace
->system_description
->endian
? 0:1;
653 g_assert( f
->field_type
->type_class
== LTT_INT
);
655 if(f
->field_size
== 1){
656 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
658 }else if(f
->field_size
== 2){
659 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
660 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
661 return (long) (revFlag
? GINT16_FROM_BE(x
): x
);
663 return (long) (revFlag
? GINT16_FROM_LE(x
): x
);
664 }else if(f
->field_size
== 4){
665 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
666 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
667 return (long) (revFlag
? GINT32_FROM_BE(x
): x
);
669 return (long) (revFlag
? GINT32_FROM_LE(x
): x
);
670 }else if(f
->field_size
== 8){
671 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
672 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
673 return (long) (revFlag
? GINT64_FROM_BE(x
): x
);
675 return (long) (revFlag
? GINT64_FROM_LE(x
): x
);
677 g_critical("ltt_event_get_long_int : field size %i unknown", f
->field_size
);
681 float ltt_event_get_float(LttEvent
*e
, LttField
*f
)
683 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
684 e
->tracefile
->trace
->system_description
->endian
? 0:1;
686 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 4);
688 if(revFlag
== 0) return *(float *)(e
->data
+ f
->offset_root
);
691 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 4);
692 aInt
= ___swab32(aInt
);
693 return ((float)aInt
);
697 double ltt_event_get_double(LttEvent
*e
, LttField
*f
)
699 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
700 e
->tracefile
->trace
->system_description
->endian
? 0:1;
702 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 8);
704 if(revFlag
== 0) return *(double *)(e
->data
+ f
->offset_root
);
707 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 8);
708 aInt
= ___swab64(aInt
);
709 return ((double)aInt
);
713 /*****************************************************************************
714 * The string obtained is only valid until the next read from
715 * the same tracefile.
716 ****************************************************************************/
718 char *ltt_event_get_string(LttEvent
*e
, LttField
*f
)
720 g_assert(f
->field_type
->type_class
== LTT_STRING
);
722 return (char*)g_strdup((char*)(e
->data
+ f
->offset_root
));
This page took 0.047188 seconds and 4 git commands to generate.