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 size
= fld
->field_size
;
118 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
119 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
120 size
= fld
->field_size
;
121 }else if(type
->type_class
== LTT_ARRAY
){
122 element_number
= (int) type
->element_number
;
123 if(fld
->field_fixed
== 0){// has string or sequence
125 for(i
=0;i
<element_number
;i
++){
126 size
+= ltt_event_refresh_fields(offsetRoot
+size
,size
,
127 fld
->child
[0], evD
+size
);
129 }else size
= fld
->field_size
;
130 }else if(type
->type_class
== LTT_SEQUENCE
){
131 size1
= fld
->sequ_number_size
;
132 element_number
= getIntNumber(size1
,evD
);
133 type
->element_number
= element_number
;
134 if(fld
->element_size
> 0){
135 size
= element_number
* fld
->element_size
;
136 }else{//sequence has string or sequence
138 for(i
=0;i
<element_number
;i
++){
139 size
+= ltt_event_refresh_fields(offsetRoot
+size
+size1
,size
+size1
,
140 fld
->child
[0], evD
+size
+size1
);
144 }else if(type
->type_class
== LTT_STRING
){
145 size
= strlen((char*)evD
) + 1; //include end : '\0'
146 }else if(type
->type_class
== LTT_STRUCT
){
147 element_number
= (int) type
->element_number
;
148 if(fld
->field_fixed
== 0){
149 offset1
= offsetRoot
;
151 for(i
=0;i
<element_number
;i
++){
152 size
=ltt_event_refresh_fields(offset1
,offset2
,
153 fld
->child
[i
],evD
+offset2
);
158 }else size
= fld
->field_size
;
161 fld
->offset_root
= offsetRoot
;
162 fld
->offset_parent
= offsetParent
;
163 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
164 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
165 fld
->field_size
= size
;
170 /*****************************************************************************
172 * ltt_event_eventtype_id: get event type id
173 * (base id + position of the event)
175 * e : an instance of an event type
177 * unsigned : event type id
178 ****************************************************************************/
180 unsigned ltt_event_eventtype_id(LttEvent
*e
)
182 return (unsigned) e
->event_id
;
185 /*****************************************************************************
187 * ltt_event_facility : get the facility of the event
189 * e : an instance of an event type
191 * LttFacility * : the facility of the event
192 ****************************************************************************/
194 LttFacility
*ltt_event_facility(LttEvent
*e
)
196 LttTrace
* trace
= e
->tracefile
->trace
;
197 unsigned id
= e
->event_id
;
198 return ltt_trace_facility_by_id(trace
,id
);
201 /*****************************************************************************
203 * ltt_event_eventtype : get the event type of the event
205 * e : an instance of an event type
207 * LttEventType * : the event type of the event
208 ****************************************************************************/
210 LttEventType
*ltt_event_eventtype(LttEvent
*e
)
212 LttFacility
* facility
= ltt_event_facility(e
);
213 if(!facility
) return NULL
;
214 return facility
->events
[e
->event_id
- facility
->base_id
];
217 /*****************************************************************************
219 * ltt_event_field : get the root field of the event
221 * e : an instance of an event type
223 * LttField * : the root field of the event
224 ****************************************************************************/
226 LttField
*ltt_event_field(LttEvent
*e
)
229 LttEventType
* event_type
= ltt_event_eventtype(e
);
230 if(!event_type
) return NULL
;
231 field
= event_type
->root_field
;
232 if(!field
) return NULL
;
234 //check if the field need refresh
235 if(e
->which_block
!= event_type
->latest_block
||
236 e
->which_event
!= event_type
->latest_event
){
238 event_type
->latest_block
= e
->which_block
;
239 event_type
->latest_event
= e
->which_event
;
241 if(field
->field_fixed
== 1)return field
;
244 ltt_event_refresh_fields(0, 0, field
, e
->data
);
249 /*****************************************************************************
251 * ltt_event_time : get the time of the event
253 * e : an instance of an event type
255 * LttTime : the time of the event
256 ****************************************************************************/
258 LttTime
ltt_event_time(LttEvent
*e
)
260 return e
->event_time
;
263 /*****************************************************************************
265 * ltt_event_time : get the cycle count of the event
267 * e : an instance of an event type
269 * LttCycleCount : the cycle count of the event
270 ****************************************************************************/
272 LttCycleCount
ltt_event_cycle_count(LttEvent
*e
)
274 return e
->event_cycle_count
;
277 /*****************************************************************************
279 * ltt_event_position : get the event's position
281 * e : an instance of an event type
282 * ep : a pointer to event's position structure
283 ****************************************************************************/
285 void ltt_event_position(LttEvent
*e
, LttEventPosition
*ep
)
287 ep
->block_num
= e
->which_block
;
288 ep
->event_num
= e
->which_event
;
289 ep
->event_time
= e
->event_time
;
290 ep
->event_cycle_count
= e
->event_cycle_count
;
291 ep
->heart_beat_number
= e
->tracefile
->cur_heart_beat_number
;
292 ep
->old_position
= TRUE
;
293 ep
->event_offset
= e
->data
- e
->tracefile
->buffer
- EVENT_HEADER_SIZE
;
294 ep
->tf
= e
->tracefile
;
296 /* This is a workaround for fast position seek */
297 ep
->last_event_pos
= e
->last_event_pos
;
298 ep
->prev_block_end_time
= e
->prev_block_end_time
;
299 ep
->prev_event_time
= e
->prev_event_time
;
300 ep
->pre_cycle_count
= e
->pre_cycle_count
;
301 ep
->count
= e
->count
;
302 /* end of workaround */
305 LttEventPosition
* ltt_event_position_new()
307 return g_new(LttEventPosition
, 1);
310 /*****************************************************************************
312 * ltt_event_position_get : get the block number and index of the event
314 * ep : a pointer to event's position structure
315 * block_number : the block number of the event
316 * index_in_block : the index of the event within the block
317 ****************************************************************************/
319 void ltt_event_position_get(LttEventPosition
*ep
,
320 unsigned *block_number
, unsigned *index_in_block
, LttTracefile
** tf
)
322 *block_number
= ep
->block_num
;
323 *index_in_block
= ep
->event_num
;
327 /*****************************************************************************
329 * ltt_event_position_set : set the block number and index of the event
330 * It does put the old_position gboolean to FALSE, as it is impossible
331 * to know the quick position to seek in the tracefile.
333 * ep : a pointer to event's position structure
334 * block_number : the block number of the event
335 * index_in_block : the index of the event within the block
336 ****************************************************************************/
338 void ltt_event_position_set(LttEventPosition
*ep
,
339 unsigned block_number
, unsigned index_in_block
)
341 if(ep
->block_num
!= block_number
|| ep
->event_num
!= index_in_block
)
342 ep
->old_position
= FALSE
;
344 ep
->block_num
= block_number
;
345 ep
->event_num
= index_in_block
;
349 /*****************************************************************************
351 * ltt_event_position_compare : compare two positions
352 * A NULL value is infinite.
354 * ep1 : a pointer to event's position structure
355 * ep2 : a pointer to event's position structure
360 ****************************************************************************/
363 gint
ltt_event_position_compare(const LttEventPosition
*ep1
,
364 const LttEventPosition
*ep2
)
366 if(ep1
->tf
!= ep2
->tf
)
367 g_error("ltt_event_position_compare on different tracefiles makes no sense");
368 if(ep1
== NULL
&& ep2
== NULL
)
370 if(ep1
!= NULL
&& ep2
== NULL
)
372 if(ep1
== NULL
&& ep2
!= NULL
)
375 if(ep1
->block_num
< ep2
->block_num
)
377 if(ep1
->block_num
> ep2
->block_num
)
379 if(ep1
->event_num
< ep2
->event_num
)
381 if(ep1
->event_num
> ep2
->event_num
)
386 /*****************************************************************************
388 * ltt_event_event_position_compare : compare two positions, one in event,
389 * other in position opaque structure.
391 * event : a pointer to event structure
392 * ep : a pointer to event's position structure
397 ****************************************************************************/
399 gint
ltt_event_event_position_compare(const LttEvent
*event
,
400 const LttEventPosition
*ep
)
402 if(event
== NULL
&& ep
== NULL
)
404 if(event
!= NULL
&& ep
== NULL
)
406 if(event
== NULL
&& ep
!= NULL
)
409 g_assert(event
->tracefile
== ep
->tf
);
411 if(event
->which_block
< ep
->block_num
)
413 if(event
->which_block
> ep
->block_num
)
415 if(event
->which_event
< ep
->event_num
)
417 if(event
->which_event
> ep
->event_num
)
422 /*****************************************************************************
424 * ltt_event_position_copy : copy position
426 * src : a pointer to event's position structure source
427 * dest : a pointer to event's position structure dest
430 ****************************************************************************/
431 void ltt_event_position_copy(LttEventPosition
*dest
,
432 const LttEventPosition
*src
)
441 /*****************************************************************************
443 * ltt_event_cpu_i: get the cpu id where the event happens
445 * e : an instance of an event type
447 * unsigned : the cpu id
448 ****************************************************************************/
450 unsigned ltt_event_cpu_id(LttEvent
*e
)
452 char * c1
, * c2
, * c3
;
453 c1
= strrchr(e
->tracefile
->name
,'\\');
454 c2
= strrchr(e
->tracefile
->name
,'/');
455 if(c1
== NULL
&& c2
== NULL
){
456 return (unsigned)atoi(e
->tracefile
->name
);
457 }else if(c1
== NULL
){
459 return (unsigned)atoi(c2
);
460 }else if(c2
== NULL
){
462 return (unsigned)atoi(c1
);
464 c3
= (c1
> c2
) ? c1
: c2
;
466 return (unsigned)atoi(c3
);
470 /*****************************************************************************
472 * ltt_event_data : get the raw data for the event
474 * e : an instance of an event type
476 * void * : pointer to the raw data for the event
477 ****************************************************************************/
479 void *ltt_event_data(LttEvent
*e
)
484 /*****************************************************************************
486 * ltt_event_field_element_number
487 * : The number of elements in a sequence field is specific
488 * to each event. This function returns the number of
489 * elements for an array or sequence field in an event.
491 * e : an instance of an event type
492 * f : a field of the instance
494 * unsigned : the number of elements for an array/sequence field
495 ****************************************************************************/
497 unsigned ltt_event_field_element_number(LttEvent
*e
, LttField
*f
)
499 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
500 f
->field_type
->type_class
!= LTT_SEQUENCE
)
503 if(f
->field_type
->type_class
== LTT_ARRAY
)
504 return f
->field_type
->element_number
;
505 return (unsigned) getIntNumber(f
->sequ_number_size
, e
+ f
->offset_root
);
508 /*****************************************************************************
510 * ltt_event_field_element_select
511 * : Set the currently selected element for a sequence or
514 * e : an instance of an event type
515 * f : a field of the instance
516 * i : the ith element
517 ****************************************************************************/
519 void ltt_event_field_element_select(LttEvent
*e
, LttField
*f
, unsigned i
)
521 unsigned element_number
;
527 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
528 f
->field_type
->type_class
!= LTT_SEQUENCE
)
531 element_number
= ltt_event_field_element_number(e
,f
);
532 /* Sanity check for i : 1..n only, and must be lower or equal element_number
534 if(element_number
< i
|| i
== 0) return;
538 evD
= e
->data
+ f
->offset_root
;
541 size
+= ltt_event_refresh_fields(f
->offset_root
+size
,size
, fld
, evD
+size
);
543 f
->current_element
= i
- 1;
546 /*****************************************************************************
547 * These functions extract data from an event after architecture specific
549 ****************************************************************************/
551 unsigned ltt_event_get_unsigned(LttEvent
*e
, LttField
*f
)
553 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
554 e
->tracefile
->trace
->system_description
->endian
? 0:1;
555 LttTypeEnum t
= f
->field_type
->type_class
;
557 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
559 if(f
->field_size
== 1){
560 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
561 return (unsigned int) x
;
562 }else if(f
->field_size
== 2){
563 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
564 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
565 return (unsigned int) (revFlag
? GUINT16_FROM_BE(x
): x
);
567 return (unsigned int) (revFlag
? GUINT16_FROM_LE(x
): x
);
568 }else if(f
->field_size
== 4){
569 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
570 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
571 return (unsigned int) (revFlag
? GUINT32_FROM_BE(x
): x
);
573 return (unsigned int) (revFlag
? GUINT32_FROM_LE(x
): x
);
574 }else if(f
->field_size
== 8){
575 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
576 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
577 return (unsigned int) (revFlag
? GUINT64_FROM_BE(x
): x
);
579 return (unsigned int) (revFlag
? GUINT64_FROM_LE(x
): x
);
581 g_critical("ltt_event_get_unsigned : field size %i unknown", f
->field_size
);
585 int ltt_event_get_int(LttEvent
*e
, LttField
*f
)
587 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
588 e
->tracefile
->trace
->system_description
->endian
? 0:1;
590 g_assert(f
->field_type
->type_class
== LTT_INT
);
592 if(f
->field_size
== 1){
593 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
595 }else if(f
->field_size
== 2){
596 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
597 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
598 return (int) (revFlag
? GINT16_FROM_BE(x
): x
);
600 return (int) (revFlag
? GINT16_FROM_LE(x
): x
);
601 }else if(f
->field_size
== 4){
602 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
603 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
604 return (int) (revFlag
? GINT32_FROM_BE(x
): x
);
606 return (int) (revFlag
? GINT32_FROM_LE(x
): x
);
607 }else if(f
->field_size
== 8){
608 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
609 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
610 return (int) (revFlag
? GINT64_FROM_BE(x
): x
);
612 return (int) (revFlag
? GINT64_FROM_LE(x
): x
);
614 g_critical("ltt_event_get_int : field size %i unknown", f
->field_size
);
618 unsigned long ltt_event_get_long_unsigned(LttEvent
*e
, LttField
*f
)
620 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
621 e
->tracefile
->trace
->system_description
->endian
? 0:1;
622 LttTypeEnum t
= f
->field_type
->type_class
;
624 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
626 if(f
->field_size
== 1){
627 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
628 return (unsigned long) x
;
629 }else if(f
->field_size
== 2){
630 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
631 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
632 return (unsigned long) (revFlag
? GUINT16_FROM_BE(x
): x
);
634 return (unsigned long) (revFlag
? GUINT16_FROM_LE(x
): x
);
635 }else if(f
->field_size
== 4){
636 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
637 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
638 return (unsigned long) (revFlag
? GUINT32_FROM_BE(x
): x
);
640 return (unsigned long) (revFlag
? GUINT32_FROM_LE(x
): x
);
641 }else if(f
->field_size
== 8){
642 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
643 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
644 return (unsigned long) (revFlag
? GUINT64_FROM_BE(x
): x
);
646 return (unsigned long) (revFlag
? GUINT64_FROM_LE(x
): x
);
648 g_critical("ltt_event_get_long_unsigned : field size %i unknown", f
->field_size
);
652 long int ltt_event_get_long_int(LttEvent
*e
, LttField
*f
)
654 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
655 e
->tracefile
->trace
->system_description
->endian
? 0:1;
657 g_assert( f
->field_type
->type_class
== LTT_INT
);
659 if(f
->field_size
== 1){
660 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
662 }else if(f
->field_size
== 2){
663 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
664 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
665 return (long) (revFlag
? GINT16_FROM_BE(x
): x
);
667 return (long) (revFlag
? GINT16_FROM_LE(x
): x
);
668 }else if(f
->field_size
== 4){
669 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
670 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
671 return (long) (revFlag
? GINT32_FROM_BE(x
): x
);
673 return (long) (revFlag
? GINT32_FROM_LE(x
): x
);
674 }else if(f
->field_size
== 8){
675 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
676 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
677 return (long) (revFlag
? GINT64_FROM_BE(x
): x
);
679 return (long) (revFlag
? GINT64_FROM_LE(x
): x
);
681 g_critical("ltt_event_get_long_int : field size %i unknown", f
->field_size
);
685 float ltt_event_get_float(LttEvent
*e
, LttField
*f
)
687 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
688 e
->tracefile
->trace
->system_description
->endian
? 0:1;
690 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 4);
692 if(revFlag
== 0) return *(float *)(e
->data
+ f
->offset_root
);
695 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 4);
696 aInt
= ___swab32(aInt
);
697 return ((float)aInt
);
701 double ltt_event_get_double(LttEvent
*e
, LttField
*f
)
703 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
704 e
->tracefile
->trace
->system_description
->endian
? 0:1;
706 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 8);
708 if(revFlag
== 0) return *(double *)(e
->data
+ f
->offset_root
);
711 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 8);
712 aInt
= ___swab64(aInt
);
713 return ((double)aInt
);
717 /*****************************************************************************
718 * The string obtained is only valid until the next read from
719 * the same tracefile.
720 ****************************************************************************/
722 char *ltt_event_get_string(LttEvent
*e
, LttField
*f
)
724 g_assert(f
->field_type
->type_class
== LTT_STRING
);
726 return (char*)g_strdup((char*)(e
->data
+ f
->offset_root
));
This page took 0.046874 seconds and 4 git commands to generate.