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(unlikely(!event_type
)) return NULL
;
231 field
= event_type
->root_field
;
232 if(unlikely(!field
)) return NULL
;
234 //check if the field need refresh
235 if(likely(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(unlikely(field
->field_fixed
!= 1))
242 ltt_event_refresh_fields(0, 0, field
, e
->data
);
247 /*****************************************************************************
249 * ltt_event_time : get the time of the event
251 * e : an instance of an event type
253 * LttTime : the time of the event
254 ****************************************************************************/
256 LttTime
ltt_event_time(LttEvent
*e
)
258 return e
->event_time
;
261 /*****************************************************************************
263 * ltt_event_time : get the cycle count of the event
265 * e : an instance of an event type
267 * LttCycleCount : the cycle count of the event
268 ****************************************************************************/
270 LttCycleCount
ltt_event_cycle_count(LttEvent
*e
)
272 return e
->event_cycle_count
;
275 /*****************************************************************************
277 * ltt_event_position : get the event's position
279 * e : an instance of an event type
280 * ep : a pointer to event's position structure
281 ****************************************************************************/
283 void ltt_event_position(LttEvent
*e
, LttEventPosition
*ep
)
285 ep
->block_num
= e
->which_block
;
286 ep
->event_num
= e
->which_event
;
287 ep
->event_time
= e
->event_time
;
288 ep
->event_cycle_count
= e
->event_cycle_count
;
289 ep
->heart_beat_number
= e
->tracefile
->cur_heart_beat_number
;
290 ep
->old_position
= TRUE
;
291 ep
->event_offset
= e
->data
- e
->tracefile
->buffer
- EVENT_HEADER_SIZE
;
292 ep
->tf
= e
->tracefile
;
294 /* This is a workaround for fast position seek */
295 ep
->last_event_pos
= e
->last_event_pos
;
296 ep
->prev_block_end_time
= e
->prev_block_end_time
;
297 ep
->prev_event_time
= e
->prev_event_time
;
298 ep
->pre_cycle_count
= e
->pre_cycle_count
;
299 ep
->count
= e
->count
;
300 /* end of workaround */
303 LttEventPosition
* ltt_event_position_new()
305 return g_new(LttEventPosition
, 1);
308 /*****************************************************************************
310 * ltt_event_position_get : get the block number and index of the event
312 * ep : a pointer to event's position structure
313 * block_number : the block number of the event
314 * index_in_block : the index of the event within the block
315 ****************************************************************************/
317 void ltt_event_position_get(LttEventPosition
*ep
,
318 unsigned *block_number
, unsigned *index_in_block
, LttTracefile
** tf
)
320 *block_number
= ep
->block_num
;
321 *index_in_block
= ep
->event_num
;
325 /*****************************************************************************
327 * ltt_event_position_set : set the block number and index of the event
328 * It does put the old_position gboolean to FALSE, as it is impossible
329 * to know the quick position to seek in the tracefile.
331 * ep : a pointer to event's position structure
332 * block_number : the block number of the event
333 * index_in_block : the index of the event within the block
334 ****************************************************************************/
336 void ltt_event_position_set(LttEventPosition
*ep
,
337 unsigned block_number
, unsigned index_in_block
)
339 if(ep
->block_num
!= block_number
|| ep
->event_num
!= index_in_block
)
340 ep
->old_position
= FALSE
;
342 ep
->block_num
= block_number
;
343 ep
->event_num
= index_in_block
;
347 /*****************************************************************************
349 * ltt_event_position_compare : compare two positions
350 * A NULL value is infinite.
352 * ep1 : a pointer to event's position structure
353 * ep2 : a pointer to event's position structure
358 ****************************************************************************/
361 gint
ltt_event_position_compare(const LttEventPosition
*ep1
,
362 const LttEventPosition
*ep2
)
364 if(ep1
->tf
!= ep2
->tf
)
365 g_error("ltt_event_position_compare on different tracefiles makes no sense");
366 if(ep1
== NULL
&& ep2
== NULL
)
368 if(ep1
!= NULL
&& ep2
== NULL
)
370 if(ep1
== NULL
&& ep2
!= NULL
)
373 if(ep1
->block_num
< ep2
->block_num
)
375 if(ep1
->block_num
> ep2
->block_num
)
377 if(ep1
->event_num
< ep2
->event_num
)
379 if(ep1
->event_num
> ep2
->event_num
)
384 /*****************************************************************************
386 * ltt_event_event_position_compare : compare two positions, one in event,
387 * other in position opaque structure.
389 * event : a pointer to event structure
390 * ep : a pointer to event's position structure
395 ****************************************************************************/
397 gint
ltt_event_event_position_compare(const LttEvent
*event
,
398 const LttEventPosition
*ep
)
400 if(event
== NULL
&& ep
== NULL
)
402 if(event
!= NULL
&& ep
== NULL
)
404 if(event
== NULL
&& ep
!= NULL
)
407 g_assert(event
->tracefile
== ep
->tf
);
409 if(event
->which_block
< ep
->block_num
)
411 if(event
->which_block
> ep
->block_num
)
413 if(event
->which_event
< ep
->event_num
)
415 if(event
->which_event
> ep
->event_num
)
420 /*****************************************************************************
422 * ltt_event_position_copy : copy position
424 * src : a pointer to event's position structure source
425 * dest : a pointer to event's position structure dest
428 ****************************************************************************/
429 void ltt_event_position_copy(LttEventPosition
*dest
,
430 const LttEventPosition
*src
)
439 /*****************************************************************************
441 * ltt_event_cpu_i: get the cpu id where the event happens
443 * e : an instance of an event type
445 * unsigned : the cpu id
446 ****************************************************************************/
448 unsigned ltt_event_cpu_id(LttEvent
*e
)
450 char * c1
, * c2
, * c3
;
451 c1
= strrchr(e
->tracefile
->name
,'\\');
452 c2
= strrchr(e
->tracefile
->name
,'/');
453 if(c1
== NULL
&& c2
== NULL
){
454 return (unsigned)atoi(e
->tracefile
->name
);
455 }else if(c1
== NULL
){
457 return (unsigned)atoi(c2
);
458 }else if(c2
== NULL
){
460 return (unsigned)atoi(c1
);
462 c3
= (c1
> c2
) ? c1
: c2
;
464 return (unsigned)atoi(c3
);
468 /*****************************************************************************
470 * ltt_event_data : get the raw data for the event
472 * e : an instance of an event type
474 * void * : pointer to the raw data for the event
475 ****************************************************************************/
477 void *ltt_event_data(LttEvent
*e
)
482 /*****************************************************************************
484 * ltt_event_field_element_number
485 * : The number of elements in a sequence field is specific
486 * to each event. This function returns the number of
487 * elements for an array or sequence field in an event.
489 * e : an instance of an event type
490 * f : a field of the instance
492 * unsigned : the number of elements for an array/sequence field
493 ****************************************************************************/
495 unsigned ltt_event_field_element_number(LttEvent
*e
, LttField
*f
)
497 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
498 f
->field_type
->type_class
!= LTT_SEQUENCE
)
501 if(f
->field_type
->type_class
== LTT_ARRAY
)
502 return f
->field_type
->element_number
;
503 return (unsigned) getIntNumber(f
->sequ_number_size
, e
+ f
->offset_root
);
506 /*****************************************************************************
508 * ltt_event_field_element_select
509 * : Set the currently selected element for a sequence or
512 * e : an instance of an event type
513 * f : a field of the instance
514 * i : the ith element
515 ****************************************************************************/
517 void ltt_event_field_element_select(LttEvent
*e
, LttField
*f
, unsigned i
)
519 unsigned element_number
;
525 if(f
->field_type
->type_class
!= LTT_ARRAY
&&
526 f
->field_type
->type_class
!= LTT_SEQUENCE
)
529 element_number
= ltt_event_field_element_number(e
,f
);
530 /* Sanity check for i : 1..n only, and must be lower or equal element_number
532 if(element_number
< i
|| i
== 0) return;
536 evD
= e
->data
+ f
->offset_root
;
539 size
+= ltt_event_refresh_fields(f
->offset_root
+size
,size
, fld
, evD
+size
);
541 f
->current_element
= i
- 1;
544 /*****************************************************************************
545 * These functions extract data from an event after architecture specific
547 ****************************************************************************/
549 unsigned ltt_event_get_unsigned(LttEvent
*e
, LttField
*f
)
551 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
552 e
->tracefile
->trace
->system_description
->endian
? 0:1;
553 LttTypeEnum t
= f
->field_type
->type_class
;
555 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
557 if(f
->field_size
== 1){
558 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
559 return (unsigned int) x
;
560 }else if(f
->field_size
== 2){
561 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
562 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
563 return (unsigned int) (revFlag
? GUINT16_FROM_BE(x
): x
);
565 return (unsigned int) (revFlag
? GUINT16_FROM_LE(x
): x
);
566 }else if(f
->field_size
== 4){
567 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
568 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
569 return (unsigned int) (revFlag
? GUINT32_FROM_BE(x
): x
);
571 return (unsigned int) (revFlag
? GUINT32_FROM_LE(x
): x
);
572 }else if(f
->field_size
== 8){
573 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
574 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
575 return (unsigned int) (revFlag
? GUINT64_FROM_BE(x
): x
);
577 return (unsigned int) (revFlag
? GUINT64_FROM_LE(x
): x
);
579 g_critical("ltt_event_get_unsigned : field size %i unknown", f
->field_size
);
583 int ltt_event_get_int(LttEvent
*e
, LttField
*f
)
585 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
586 e
->tracefile
->trace
->system_description
->endian
? 0:1;
588 g_assert(f
->field_type
->type_class
== LTT_INT
);
590 if(f
->field_size
== 1){
591 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
593 }else if(f
->field_size
== 2){
594 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
595 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
596 return (int) (revFlag
? GINT16_FROM_BE(x
): x
);
598 return (int) (revFlag
? GINT16_FROM_LE(x
): x
);
599 }else if(f
->field_size
== 4){
600 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
601 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
602 return (int) (revFlag
? GINT32_FROM_BE(x
): x
);
604 return (int) (revFlag
? GINT32_FROM_LE(x
): x
);
605 }else if(f
->field_size
== 8){
606 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
607 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
608 return (int) (revFlag
? GINT64_FROM_BE(x
): x
);
610 return (int) (revFlag
? GINT64_FROM_LE(x
): x
);
612 g_critical("ltt_event_get_int : field size %i unknown", f
->field_size
);
616 unsigned long ltt_event_get_long_unsigned(LttEvent
*e
, LttField
*f
)
618 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
619 e
->tracefile
->trace
->system_description
->endian
? 0:1;
620 LttTypeEnum t
= f
->field_type
->type_class
;
622 g_assert(t
== LTT_UINT
|| t
== LTT_ENUM
);
624 if(f
->field_size
== 1){
625 guint8 x
= *(guint8
*)(e
->data
+ f
->offset_root
);
626 return (unsigned long) x
;
627 }else if(f
->field_size
== 2){
628 guint16 x
= *(guint16
*)(e
->data
+ f
->offset_root
);
629 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
630 return (unsigned long) (revFlag
? GUINT16_FROM_BE(x
): x
);
632 return (unsigned long) (revFlag
? GUINT16_FROM_LE(x
): x
);
633 }else if(f
->field_size
== 4){
634 guint32 x
= *(guint32
*)(e
->data
+ f
->offset_root
);
635 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
636 return (unsigned long) (revFlag
? GUINT32_FROM_BE(x
): x
);
638 return (unsigned long) (revFlag
? GUINT32_FROM_LE(x
): x
);
639 }else if(f
->field_size
== 8){
640 guint64 x
= *(guint64
*)(e
->data
+ f
->offset_root
);
641 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
642 return (unsigned long) (revFlag
? GUINT64_FROM_BE(x
): x
);
644 return (unsigned long) (revFlag
? GUINT64_FROM_LE(x
): x
);
646 g_critical("ltt_event_get_long_unsigned : field size %i unknown", f
->field_size
);
650 long int ltt_event_get_long_int(LttEvent
*e
, LttField
*f
)
652 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
653 e
->tracefile
->trace
->system_description
->endian
? 0:1;
655 g_assert( f
->field_type
->type_class
== LTT_INT
);
657 if(f
->field_size
== 1){
658 gint8 x
= *(gint8
*)(e
->data
+ f
->offset_root
);
660 }else if(f
->field_size
== 2){
661 gint16 x
= *(gint16
*)(e
->data
+ f
->offset_root
);
662 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
663 return (long) (revFlag
? GINT16_FROM_BE(x
): x
);
665 return (long) (revFlag
? GINT16_FROM_LE(x
): x
);
666 }else if(f
->field_size
== 4){
667 gint32 x
= *(gint32
*)(e
->data
+ f
->offset_root
);
668 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
669 return (long) (revFlag
? GINT32_FROM_BE(x
): x
);
671 return (long) (revFlag
? GINT32_FROM_LE(x
): x
);
672 }else if(f
->field_size
== 8){
673 gint64 x
= *(gint64
*)(e
->data
+ f
->offset_root
);
674 if(e
->tracefile
->trace
->my_arch_endian
== LTT_LITTLE_ENDIAN
)
675 return (long) (revFlag
? GINT64_FROM_BE(x
): x
);
677 return (long) (revFlag
? GINT64_FROM_LE(x
): x
);
679 g_critical("ltt_event_get_long_int : field size %i unknown", f
->field_size
);
683 float ltt_event_get_float(LttEvent
*e
, LttField
*f
)
685 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
686 e
->tracefile
->trace
->system_description
->endian
? 0:1;
688 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 4);
690 if(revFlag
== 0) return *(float *)(e
->data
+ f
->offset_root
);
693 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 4);
694 aInt
= ___swab32(aInt
);
695 return ((float)aInt
);
699 double ltt_event_get_double(LttEvent
*e
, LttField
*f
)
701 int revFlag
= e
->tracefile
->trace
->my_arch_endian
==
702 e
->tracefile
->trace
->system_description
->endian
? 0:1;
704 g_assert(f
->field_type
->type_class
== LTT_FLOAT
&& f
->field_size
== 8);
706 if(revFlag
== 0) return *(double *)(e
->data
+ f
->offset_root
);
709 memcpy((void*)&aInt
, e
->data
+ f
->offset_root
, 8);
710 aInt
= ___swab64(aInt
);
711 return ((double)aInt
);
715 /*****************************************************************************
716 * The string obtained is only valid until the next read from
717 * the same tracefile.
718 ****************************************************************************/
720 char *ltt_event_get_string(LttEvent
*e
, LttField
*f
)
722 g_assert(f
->field_type
->type_class
== LTT_STRING
);
724 return (char*)g_strdup((char*)(e
->data
+ f
->offset_root
));
This page took 0.046327 seconds and 4 git commands to generate.