+/* This file is part of the Linux Trace Toolkit viewer
+ * Copyright (C) 2003-2004 Xiangxiu Yang
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <glib.h>
+
#include <asm/types.h>
-#include <linux/byteorder/swab.h>
+#include <byteswap.h>
#include "parser.h"
#include <ltt/ltt.h>
#include "ltt-private.h"
#include <ltt/event.h>
#include <ltt/trace.h>
+#include <ltt/ltt-types.h>
+
+
+
+void compute_fields_offsets(LttTracefile *tf, LttField *field, off_t *offset,
+ void *root);
+
+
+
+
+LttEvent *ltt_event_new()
+{
+ return g_new(LttEvent, 1);
+}
+
+void ltt_event_destroy(LttEvent *event)
+{
+ g_free(event);
+}
+
+#if 0
+/* Use get_field_type_size instead */
/*****************************************************************************
*Function name
* ltt_event_refresh_fields : refresh fields of an event
*Input params
* offsetRoot : offset from the root
- * offsetParent : offset from the parrent
+ * offsetParent : offset from the parent
* fld : field
* evD : event data
+ * reverse_byte_order : 1 or 0
*Return value
* int : size of the field
****************************************************************************/
int ltt_event_refresh_fields(int offsetRoot,int offsetParent,
- LttField * fld, void *evD)
+ LttField * fld, void *evD, gboolean reverse_byte_order)
{
int size, size1, element_number, i, offset1, offset2;
LttType * type = fld->field_type;
+ switch(type->type_class) {
+ case LTT_ARRAY:
+ element_number = (int) type->element_number;
+ if(fld->field_fixed == 0){// has string or sequence
+ size = 0;
+ for(i=0;i<element_number;i++){
+ size += ltt_event_refresh_fields(offsetRoot+size,size,
+ fld->child[0], evD+size, reverse_byte_order);
+ }
+ }else size = fld->field_size;
+ break;
+
+ case LTT_SEQUENCE:
+ size1 = fld->sequ_number_size;
+ element_number = getIntNumber(reverse_byte_order,size1,evD);
+ type->element_number = element_number;
+ if(fld->element_size > 0){
+ size = element_number * fld->element_size;
+ }else{//sequence has string or sequence
+ size = 0;
+ for(i=0;i<element_number;i++){
+ size += ltt_event_refresh_fields(offsetRoot+size+size1,size+size1,
+ fld->child[0], evD+size+size1, reverse_byte_order);
+ }
+ size += size1;
+ }
+ break;
+
+ case LTT_STRING:
+ size = strlen((gchar*)evD) + 1; //include end : '\0'
+ break;
+
+ case LTT_STRUCT:
+ element_number = (int) type->element_number;
+ if(fld->field_fixed == 0){
+ offset1 = offsetRoot;
+ offset2 = 0;
+ for(i=0;i<element_number;i++){
+ size=ltt_event_refresh_fields(offset1,offset2,
+ fld->child[i],evD+offset2, reverse_byte_order);
+ offset1 += size;
+ offset2 += size;
+ }
+ size = offset2;
+ }else size = fld->field_size;
+ break;
+
+ case LTT_UNION:
+ size = fld->field_size;
+ break;
+
+ default:
+ size = fld->field_size;
+ }
+
+#if 0
if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
size = fld->field_size;
if(fld->field_fixed == 0){// has string or sequence
size = 0;
for(i=0;i<element_number;i++){
- size += ltt_event_refresh_fields(offsetRoot+size,size,
+ size += ltt_event_refresh_fields(offsetRoot+size,size,
fld->child[0], evD+size);
- }
+ }
}else size = fld->field_size;
}else if(type->type_class == LTT_SEQUENCE){
size1 = fld->sequ_number_size;
}else{//sequence has string or sequence
size = 0;
for(i=0;i<element_number;i++){
- size += ltt_event_refresh_fields(offsetRoot+size+size1,size+size1,
- fld->child[0], evD+size+size1);
+ size += ltt_event_refresh_fields(offsetRoot+size+size1,size+size1,
+ fld->child[0], evD+size+size1);
}
size += size1;
}
offset1 = offsetRoot;
offset2 = 0;
for(i=0;i<element_number;i++){
- size=ltt_event_refresh_fields(offset1,offset2,fld->child[i],evD+offset2);
- offset1 += size;
- offset2 += size;
+ size=ltt_event_refresh_fields(offset1,offset2,
+ fld->child[i],evD+offset2);
+ offset1 += size;
+ offset2 += size;
}
size = offset2;
}else size = fld->field_size;
}
-
+#endif //0
fld->offset_root = offsetRoot;
fld->offset_parent = offsetParent;
fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
return size;
}
+#endif //0
+
/*****************************************************************************
*Function name
* unsigned : event type id
****************************************************************************/
-unsigned ltt_event_eventtype_id(LttEvent *e)
+unsigned ltt_event_eventtype_id(const LttEvent *e)
{
return (unsigned) e->event_id;
}
* LttFacility * : the facility of the event
****************************************************************************/
-LttFacility *ltt_event_facility(LttEvent *e)
+LttFacility *ltt_event_facility(const LttEvent *e)
{
LttTrace * trace = e->tracefile->trace;
- unsigned id = e->event_id;
- return ltt_trace_facility_by_id(trace,id);
+ unsigned id = e->facility_id;
+ LttFacility *facility = ltt_trace_facility_by_id(trace,id);
+
+ g_assert(facility->exists);
+
+ return facility;
}
/*****************************************************************************
*Function name
- * ltt_event_eventtype : get the event type of the event
+ * ltt_event_facility_id : get the facility id of the event
*Input params
- * e : an instance of an event type
+ * e : an instance of an event type
*Return value
- * LttEventType * : the event type of the event
+ * unsigned : the facility of the event
****************************************************************************/
-LttEventType *ltt_event_eventtype(LttEvent *e)
+unsigned ltt_event_facility_id(const LttEvent *e)
{
- LttFacility* facility = ltt_event_facility(e);
- if(!facility) return NULL;
- return facility->events[e->event_id - facility->base_id];
+ return e->facility_id;
}
/*****************************************************************************
*Function name
- * ltt_event_field : get the root field of the event
+ * ltt_event_eventtype : get the event type of the event
*Input params
- * e : an instance of an event type
+ * e : an instance of an event type
*Return value
- * LttField * : the root field of the event
+ * LttEventType * : the event type of the event
****************************************************************************/
-LttField *ltt_event_field(LttEvent *e)
+LttEventType *ltt_event_eventtype(const LttEvent *e)
{
- LttField * field;
- LttEventType * event_type = ltt_event_eventtype(e);
- if(!event_type) return NULL;
- field = event_type->root_field;
- if(!field) return NULL;
-
- //check if the field need refresh
- if(e->which_block != event_type->latest_block ||
- e->which_event != event_type->latest_event){
-
- event_type->latest_block = e->which_block;
- event_type->latest_event = e->which_event;
-
- if(field->field_fixed == 1)return field;
-
- //refresh the field
- ltt_event_refresh_fields(0, 0, field, e->data);
- }
- return field;
+ LttFacility* facility = ltt_event_facility(e);
+ if(!facility) return NULL;
+ return &g_array_index(facility->events, LttEventType, e->event_id);
}
+
/*****************************************************************************
*Function name
* ltt_event_time : get the time of the event
* LttTime : the time of the event
****************************************************************************/
-LttTime ltt_event_time(LttEvent *e)
+LttTime ltt_event_time(const LttEvent *e)
{
return e->event_time;
}
* LttCycleCount : the cycle count of the event
****************************************************************************/
-LttCycleCount ltt_event_cycle_count(LttEvent *e)
+LttCycleCount ltt_event_cycle_count(const LttEvent *e)
+{
+ return e->tsc;
+}
+
+
+
+/*****************************************************************************
+ *Function name
+ * ltt_event_position_get : get the event position data
+ *Input params
+ * e : an instance of an event type
+ * ep : a pointer to event's position structure
+ * tf : tracefile pointer
+ * block : current block
+ * offset : current offset
+ * tsc : current tsc
+ ****************************************************************************/
+void ltt_event_position_get(LttEventPosition *ep, LttTracefile **tf,
+ guint *block, guint *offset, guint64 *tsc)
{
- return e->event_cycle_count;
+ *tf = ep->tracefile;
+ *block = ep->block;
+ *offset = ep->offset;
+ *tsc = ep->tsc;
}
+
/*****************************************************************************
*Function name
* ltt_event_position : get the event's position
void ltt_event_position(LttEvent *e, LttEventPosition *ep)
{
- ep->block_num = e->which_block;
- ep->event_num = e->which_event;
- ep->event_time = e->event_time;
- ep->event_cycle_count = e->event_cycle_count;
- ep->heart_beat_number = e->tracefile->cur_heart_beat_number;
- ep->old_position = FALSE;
- ep->event_offset = e->data - e->tracefile->buffer - EVENT_HEADER_SIZE ;
- ep->tf = e->tracefile;
+ ep->tracefile = e->tracefile;
+ ep->block = e->block;
+ ep->offset = e->offset;
+ ep->tsc = e->tsc;
}
LttEventPosition * ltt_event_position_new()
return g_new(LttEventPosition, 1);
}
+
/*****************************************************************************
- *Function name
- * ltt_event_position_get : get the block number and index of the event
- *Input params
- * ep : a pointer to event's position structure
- * block_number : the block number of the event
- * index_in_block : the index of the event within the block
+ * Function name
+ * ltt_event_position_compare : compare two positions
+ * A NULL value is infinite.
+ * Input params
+ * ep1 : a pointer to event's position structure
+ * ep2 : a pointer to event's position structure
+ * Return
+ * -1 is ep1 < ep2
+ * 1 if ep1 > ep2
+ * 0 if ep1 == ep2
****************************************************************************/
-void ltt_event_position_get(LttEventPosition *ep,
- unsigned *block_number, unsigned *index_in_block, LttTracefile ** tf)
+
+gint ltt_event_position_compare(const LttEventPosition *ep1,
+ const LttEventPosition *ep2)
{
- *block_number = ep->block_num;
- *index_in_block = ep->event_num;
- *tf = ep->tf;
+ if(ep1 == NULL && ep2 == NULL)
+ return 0;
+ if(ep1 != NULL && ep2 == NULL)
+ return -1;
+ if(ep1 == NULL && ep2 != NULL)
+ return 1;
+
+ if(ep1->tracefile != ep2->tracefile)
+ g_error("ltt_event_position_compare on different tracefiles makes no sense");
+
+ if(ep1->block < ep2->block)
+ return -1;
+ if(ep1->block > ep2->block)
+ return 1;
+ if(ep1->offset < ep2->offset)
+ return -1;
+ if(ep1->offset > ep2->offset)
+ return 1;
+ return 0;
}
/*****************************************************************************
- *Function name
- * ltt_event_position_set : set the block number and index of the event
- *Input params
- * ep : a pointer to event's position structure
- * block_number : the block number of the event
- * index_in_block : the index of the event within the block
+ * Function name
+ * ltt_event_position_copy : copy position
+ * Input params
+ * src : a pointer to event's position structure source
+ * dest : a pointer to event's position structure dest
+ * Return
+ * void
****************************************************************************/
+void ltt_event_position_copy(LttEventPosition *dest,
+ const LttEventPosition *src)
+{
+ if(src == NULL)
+ dest = NULL;
+ else
+ *dest = *src;
+}
+
-void ltt_event_position_set(LttEventPosition *ep,
- unsigned block_number, unsigned index_in_block)
+
+LttTracefile *ltt_event_position_tracefile(LttEventPosition *ep)
{
- ep->block_num = block_number;
- ep->event_num = index_in_block;
+ return ep->tracefile;
}
/*****************************************************************************
****************************************************************************/
unsigned ltt_event_cpu_id(LttEvent *e)
-{
- char * c1, * c2, * c3;
- c1 = strrchr(e->tracefile->name,'\\');
- c2 = strrchr(e->tracefile->name,'/');
- if(c1 == NULL && c2 == NULL){
- return (unsigned)atoi(e->tracefile->name);
- }else if(c1 == NULL){
- c2++;
- return (unsigned)atoi(c2);
- }else if(c2 == NULL){
- c1++;
- return (unsigned)atoi(c1);
- }else{
- c3 = (c1 > c2) ? c1 : c2;
- c3++;
- return (unsigned)atoi(c3);
- }
+{
+ return e->tracefile->cpu_num;
}
/*****************************************************************************
*Return value
* unsigned : the number of elements for an array/sequence field
****************************************************************************/
-
-unsigned ltt_event_field_element_number(LttEvent *e, LttField *f)
+guint64 ltt_event_field_element_number(LttEvent *e, LttField *f)
{
- if(f->field_type->type_class != LTT_ARRAY &&
- f->field_type->type_class != LTT_SEQUENCE)
+ if(f->field_type.type_class != LTT_ARRAY &&
+ f->field_type.type_class != LTT_SEQUENCE)
return 0;
- if(f->field_type->type_class == LTT_ARRAY)
- return f->field_type->element_number;
- return (unsigned) getIntNumber(f->sequ_number_size, e + f->offset_root);
+ if(f->field_type.type_class == LTT_ARRAY)
+ return f->field_type.size;
+ return ltt_event_get_long_unsigned(e, &g_array_index(f->field_type.fields,
+ LttField, 0));
}
/*****************************************************************************
* ltt_event_field_element_select
* : Set the currently selected element for a sequence or
* array field
+ * O(1) because of offset array.
*Input params
* e : an instance of an event type
* f : a field of the instance
- * i : the ith element
+ * i : the ith element (0, ...)
+ *returns : the child field, at the right index, updated.
****************************************************************************/
-
-void ltt_event_field_element_select(LttEvent *e, LttField *f, unsigned i)
+LttField *ltt_event_field_element_select(LttEvent *e, LttField *f, gulong i)
{
- unsigned element_number;
- LttField *fld;
- int k, size;
- void *evD;
+ gulong element_number;
+ LttField *field;
+ unsigned int k;
+ size_t size;
+ LttEventType *event_type;
+ off_t new_offset;
- if(f->field_type->type_class != LTT_ARRAY &&
- f->field_type->type_class != LTT_SEQUENCE)
+ if(f->field_type.type_class != LTT_ARRAY &&
+ f->field_type.type_class != LTT_SEQUENCE)
return ;
element_number = ltt_event_field_element_number(e,f);
- if((element_number-1) < i || i < 0) return;
-
- fld = f->child[0];
-
- evD = e->data + f->offset_root;
- size = 0;
- for(k=0;k<i;k++){
- size += ltt_event_refresh_fields(f->offset_root+size,size, fld, evD+size);
+ event_type = ltt_event_eventtype(e);
+ /* Sanity check for i : 0..n-1 only, and must be lower or equal element_number
+ */
+ if(i >= element_number) return;
+
+ if(f->field_type.type_class == LTT_ARRAY) {
+ field = &g_array_index(f->field_type.fields, LttField, 0);
+ } else {
+ field = &g_array_index(f->field_type.fields, LttField, 1);
}
- f->current_element = i - 1;
+ if(field->field_size != 0) {
+ if(f->array_offset + (i * field->field_size) == field->offset_root)
+ return; /* fixed length child, already at the right offset */
+ else
+ new_offset = f->array_offset + (i * field->field_size);
+ } else {
+ /* Var. len. child */
+ new_offset = g_array_index(f->dynamic_offsets, off_t, i);
+ }
+ compute_fields_offsets(e->tracefile, field, &new_offset, e->data);
+
+ return field;
}
/*****************************************************************************
* These functions extract data from an event after architecture specific
* conversions
****************************************************************************/
-
-unsigned ltt_event_get_unsigned(LttEvent *e, LttField *f)
+guint32 ltt_event_get_unsigned(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
- LttTypeEnum t = f->field_type->type_class;
+ gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- if(t != LTT_UINT && t != LTT_ENUM)
- g_error("The type of the field is not unsigned int\n");
+ LttTypeEnum t = f->field_type.type_class;
if(f->field_size == 1){
guint8 x = *(guint8 *)(e->data + f->offset_root);
- return (unsigned int) x;
+ return (guint32) x;
}else if(f->field_size == 2){
- guint16 x = *(guint16 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (unsigned int) (revFlag ? GUINT16_FROM_BE(x): x);
- else
- return (unsigned int) (revFlag ? GUINT16_FROM_LE(x): x);
+ return (guint32)ltt_get_uint16(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 4){
- guint32 x = *(guint32 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (unsigned int) (revFlag ? GUINT32_FROM_BE(x): x);
- else
- return (unsigned int) (revFlag ? GUINT32_FROM_LE(x): x);
- }else if(f->field_size == 8){
+ return (guint32)ltt_get_uint32(reverse_byte_order, e->data + f->offset_root);
+ }
+#if 0
+ else if(f->field_size == 8){
guint64 x = *(guint64 *)(e->data + f->offset_root);
if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
return (unsigned int) (revFlag ? GUINT64_FROM_BE(x): x);
else
return (unsigned int) (revFlag ? GUINT64_FROM_LE(x): x);
}
+#endif //0
+ g_critical("ltt_event_get_unsigned : field size %i unknown", f->field_size);
+ return 0;
}
-int ltt_event_get_int(LttEvent *e, LttField *f)
+gint32 ltt_event_get_int(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
-
- if(f->field_type->type_class != LTT_INT)
- g_error("The type of the field is not int\n");
+ gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
if(f->field_size == 1){
gint8 x = *(gint8 *)(e->data + f->offset_root);
- return (int) x;
+ return (gint32) x;
}else if(f->field_size == 2){
- gint16 x = *(gint16 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (int) (revFlag ? GINT16_FROM_BE(x): x);
- else
- return (int) (revFlag ? GINT16_FROM_LE(x): x);
+ return (gint32)ltt_get_int16(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 4){
- gint32 x = *(gint32 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (int) (revFlag ? GINT32_FROM_BE(x): x);
- else
- return (int) (revFlag ? GINT32_FROM_LE(x): x);
- }else if(f->field_size == 8){
+ return (gint32)ltt_get_int32(reverse_byte_order, e->data + f->offset_root);
+ }
+#if 0
+ else if(f->field_size == 8){
gint64 x = *(gint64 *)(e->data + f->offset_root);
if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
return (int) (revFlag ? GINT64_FROM_BE(x): x);
else
return (int) (revFlag ? GINT64_FROM_LE(x): x);
}
+#endif //0
+ g_critical("ltt_event_get_int : field size %i unknown", f->field_size);
+ return 0;
}
-unsigned long ltt_event_get_long_unsigned(LttEvent *e, LttField *f)
+guint64 ltt_event_get_long_unsigned(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
- LttTypeEnum t = f->field_type->type_class;
+ gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
- if(t != LTT_UINT && t != LTT_ENUM)
- g_error("The type of the field is not unsigned long\n");
+ LttTypeEnum t = f->field_type.type_class;
if(f->field_size == 1){
guint8 x = *(guint8 *)(e->data + f->offset_root);
- return (unsigned long) x;
+ return (guint64) x;
}else if(f->field_size == 2){
- guint16 x = *(guint16 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (unsigned long) (revFlag ? GUINT16_FROM_BE(x): x);
- else
- return (unsigned long) (revFlag ? GUINT16_FROM_LE(x): x);
+ return (guint64)ltt_get_uint16(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 4){
- guint32 x = *(guint32 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (unsigned long) (revFlag ? GUINT32_FROM_BE(x): x);
- else
- return (unsigned long) (revFlag ? GUINT32_FROM_LE(x): x);
+ return (guint64)ltt_get_uint32(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 8){
- guint64 x = *(guint64 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (unsigned long) (revFlag ? GUINT64_FROM_BE(x): x);
- else
- return (unsigned long) (revFlag ? GUINT64_FROM_LE(x): x);
+ return ltt_get_uint64(reverse_byte_order, e->data + f->offset_root);
}
+ g_critical("ltt_event_get_long_unsigned : field size %i unknown", f->field_size);
+ return 0;
}
-long int ltt_event_get_long_int(LttEvent *e, LttField *f)
+gint64 ltt_event_get_long_int(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
-
- if( f->field_type->type_class != LTT_INT)
- g_error("The type of the field is not long int\n");
+ //int revFlag = e->tracefile->trace->my_arch_endian ==
+ // e->tracefile->trace->system_description->endian ? 0:1;
+ gboolean reverse_byte_order = LTT_GET_BO(e->tracefile);
if(f->field_size == 1){
gint8 x = *(gint8 *)(e->data + f->offset_root);
- return (long) x;
+ return (gint64) x;
}else if(f->field_size == 2){
- gint16 x = *(gint16 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (long) (revFlag ? GINT16_FROM_BE(x): x);
- else
- return (long) (revFlag ? GINT16_FROM_LE(x): x);
+ return (gint64)ltt_get_int16(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 4){
- gint32 x = *(gint32 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (long) (revFlag ? GINT32_FROM_BE(x): x);
- else
- return (long) (revFlag ? GINT32_FROM_LE(x): x);
+ return (gint64)ltt_get_int32(reverse_byte_order, e->data + f->offset_root);
}else if(f->field_size == 8){
- gint64 x = *(gint64 *)(e->data + f->offset_root);
- if(e->tracefile->trace->my_arch_endian == LTT_LITTLE_ENDIAN)
- return (long) (revFlag ? GINT64_FROM_BE(x): x);
- else
- return (long) (revFlag ? GINT64_FROM_LE(x): x);
+ return ltt_get_int64(reverse_byte_order, e->data + f->offset_root);
}
+ g_critical("ltt_event_get_long_int : field size %i unknown", f->field_size);
+ return 0;
}
float ltt_event_get_float(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
+ g_assert(LTT_HAS_FLOAT(e->tracefile));
+ gboolean reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
- if(f->field_type->type_class != LTT_FLOAT ||
- (f->field_type->type_class == LTT_FLOAT && f->field_size != 4))
- g_error("The type of the field is not float\n");
+ g_assert(f->field_type.type_class == LTT_FLOAT && f->field_size == 4);
- if(revFlag == 0) return *(float *)(e->data + f->offset_root);
+ if(reverse_byte_order == 0) return *(float *)(e->data + f->offset_root);
else{
- guint32 aInt;
- memcpy((void*)&aInt, e->data + f->offset_root, 4);
- aInt = ___swab32(aInt);
- return *((float*)&aInt);
+ void *ptr = e->data + f->offset_root;
+ guint32 value = bswap_32(*(guint32*)ptr);
+ return *(float*)&value;
}
}
double ltt_event_get_double(LttEvent *e, LttField *f)
{
- int revFlag = e->tracefile->trace->my_arch_endian ==
- e->tracefile->trace->system_description->endian ? 0:1;
+ g_assert(LTT_HAS_FLOAT(e->tracefile));
+ gboolean reverse_byte_order = LTT_GET_FLOAT_BO(e->tracefile);
- if(f->field_type->type_class != LTT_FLOAT ||
- (f->field_type->type_class == LTT_FLOAT && f->field_size != 8))
- g_error("The type of the field is not double\n");
+ if(f->field_size == 4)
+ return ltt_event_get_float(e, f);
+
+ g_assert(f->field_type.type_class == LTT_FLOAT && f->field_size == 8);
- if(revFlag == 0) return *(double *)(e->data + f->offset_root);
- else{
- guint64 aInt;
- memcpy((void*)&aInt, e->data + f->offset_root, 8);
- aInt = ___swab64(aInt);
- return *((double *)&aInt);
+ if(reverse_byte_order == 0) return *(double *)(e->data + f->offset_root);
+ else {
+ void *ptr = e->data + f->offset_root;
+ guint64 value = bswap_64(*(guint64*)ptr);
+ return *(double*)&value;
}
}
* The string obtained is only valid until the next read from
* the same tracefile.
****************************************************************************/
-
char *ltt_event_get_string(LttEvent *e, LttField *f)
{
- if(f->field_type->type_class != LTT_STRING)
- g_error("The field contains no string\n");
- return (char*)g_strdup((char*)(e->data + f->offset_root));
+ g_assert(f->field_type.type_class == LTT_STRING);
+
+ return (gchar*)g_strdup((gchar*)(e->data + f->offset_root));
+}
+
+
+/*****************************************************************************
+ *Function name
+ * get_field_type_size : set the fixed and dynamic sizes of the field type
+ * from the data read.
+ *Input params
+ * tf : tracefile
+ * event_type : event type
+ * offset_root : offset from the root
+ * offset_parent : offset from the parent
+ * field : field
+ * data : a pointer to the event data.
+ *Returns the field type size.
+ ****************************************************************************/
+ // TODO
+// Change this function so it uses a *to offset value incrementation, just like
+// genevent-new instead of returning a size. What is of interest here is the
+// offset needed to read each field.
+//
+// Precomputed ones can be returned directly. Otherwise, the field is flagged
+// "VARIABLE OFFSET" and must be computed dynamically. The dynamic processing
+// of an offset takes the last known fixed offset, and then dynamically
+// calculates all variable field offsets from it.
+//
+// After a VARIABLE SIZE element, all fields have a variable offset.
+// Also, is an array or a sequence has variable length child, we must pass
+// through all of them, saving the offsets in the dynamic_offsets array.
+
+#if 0
+size_t get_field_type_size(LttTracefile *tf, LttEventType *event_type,
+ off_t offset_root, off_t offset_parent,
+ LttField *field, void *data)
+{
+ size_t size = 0;
+ guint i;
+ LttType *type;
+ off_t align;
+
+ g_assert(field->fixed_root != FIELD_UNKNOWN);
+ g_assert(field->fixed_parent != FIELD_UNKNOWN);
+ g_assert(field->fixed_size != FIELD_UNKNOWN);
+
+ field->offset_root = offset_root;
+ field->offset_parent = offset_parent;
+
+ type = field->field_type;
+
+ switch(type->type_class) {
+ case LTT_INT:
+ case LTT_UINT:
+ case LTT_FLOAT:
+ case LTT_ENUM:
+ case LTT_POINTER:
+ case LTT_LONG:
+ case LTT_ULONG:
+ case LTT_SIZE_T:
+ case LTT_SSIZE_T:
+ case LTT_OFF_T:
+ g_assert(field->fixed_size == FIELD_FIXED);
+ size = field->field_size;
+ align = ltt_align(field->offset_root,
+ size, event_type->facility->has_alignment);
+ field->offset_root += align;
+ field->offset_parent += align;
+ size += align;
+ break;
+ case LTT_SEQUENCE:
+ {
+ /* FIXME : check the type of sequence identifier */
+ gint seqnum = ltt_get_uint(LTT_GET_BO(tf),
+ field->sequ_number_size,
+ data + offset_root);
+
+ if(field->child[0]->fixed_size == FIELD_FIXED) {
+ size = field->sequ_number_size +
+ (seqnum * get_field_type_size(tf, event_type,
+ offset_root, offset_parent,
+ field->child[0], data));
+ } else {
+ size += field->sequ_number_size;
+ for(i=0;i<seqnum;i++) {
+ size_t child_size;
+ child_size = get_field_type_size(tf, event_type,
+ offset_root, offset_parent,
+ field->child[0], data);
+ offset_root += child_size;
+ offset_parent += child_size;
+ size += child_size;
+ }
+ }
+ field->field_size = size;
+ }
+ break;
+ case LTT_STRING:
+ size = strlen((char*)(data+offset_root)) + 1;// length + \0
+ field->field_size = size;
+ break;
+ case LTT_ARRAY:
+ if(field->fixed_size == FIELD_FIXED)
+ size = field->field_size;
+ else {
+ for(i=0;i<field->field_type->element_number;i++) {
+ size_t child_size;
+ child_size = get_field_type_size(tf, event_type,
+ offset_root, offset_parent,
+ field->child[0], data);
+ offset_root += child_size;
+ offset_parent += child_size;
+ size += child_size;
+ }
+ field->field_size = size;
+ }
+ break;
+ case LTT_STRUCT:
+ if(field->fixed_size == FIELD_FIXED)
+ size = field->field_size;
+ else {
+ size_t current_root_offset = offset_root;
+ size_t current_offset = 0;
+ size_t child_size = 0;
+ for(i=0;i<type->element_number;i++) {
+ child_size = get_field_type_size(tf,
+ event_type, current_root_offset, current_offset,
+ field->child[i], data);
+ current_offset += child_size;
+ current_root_offset += child_size;
+
+ }
+ size = current_offset;
+ field->field_size = size;
+ }
+ break;
+ case LTT_UNION:
+ if(field->fixed_size == FIELD_FIXED)
+ size = field->field_size;
+ else {
+ size_t current_root_offset = field->offset_root;
+ size_t current_offset = 0;
+ for(i=0;i<type->element_number;i++) {
+ size = get_field_type_size(tf, event_type,
+ current_root_offset, current_offset,
+ field->child[i], data);
+ size = max(size, field->child[i]->field_size);
+ }
+ field->field_size = size;
+ }
+ break;
+ }
+
+ return size;
}
+#endif //0
+
+
+
+
+
+/*****************************************************************************
+ *Function name
+ * compute_fields_offsets : set the precomputable offset of the fields
+ *Input params
+ * tf : tracefile
+ * field : the field
+ * offset : pointer to the current offset, must be incremented
+ ****************************************************************************/
+
+
+void compute_fields_offsets(LttTracefile *tf, LttField *field, off_t *offset,
+ void *root)
+{
+ LttType *type = &field->field_type;
+
+ switch(type->type_class) {
+ case LTT_INT_FIXED:
+ case LTT_UINT_FIXED:
+ case LTT_POINTER:
+ case LTT_CHAR:
+ case LTT_UCHAR:
+ case LTT_SHORT:
+ case LTT_USHORT:
+ case LTT_INT:
+ case LTT_UINT:
+ case LTT_LONG:
+ case LTT_ULONG:
+ case LTT_SIZE_T:
+ case LTT_SSIZE_T:
+ case LTT_OFF_T:
+ case LTT_FLOAT:
+ case LTT_ENUM:
+ if(field->fixed_root == FIELD_VARIABLE) {
+ /* Align offset on type size */
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ /* Increment offset */
+ *offset += field->field_size;
+ }
+ /* None of these types has variable size, so we are sure that if
+ * this element has a fixed_root, then the following one will have
+ * a fixed root too, so it does not need the *offset at all.
+ */
+ break;
+ case LTT_STRING:
+ if(field->fixed_root == FIELD_VARIABLE) {
+ field->offset_root = *offset;
+ }
+ *offset += strlen((gchar*)(root+*offset)) + 1;
+ break;
+ case LTT_ARRAY:
+ g_assert(type->fields->len == 1);
+ {
+ off_t local_offset;
+ LttField *child = &g_array_index(type->fields, LttField, 0);
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ field->array_offset = *offset;
+ }
+
+ if(field->field_size != 0) {
+ /* Increment offset */
+ /* field_size is the array size in bytes */
+ *offset = field->offset_root + field->field_size;
+ } else {
+ guint i;
+ *offset = field->array_offset;
+ field->dynamic_offsets = g_array_set_size(field->dynamic_offsets,
+ 0);
+ for(i=0; i<type->size; i++) {
+ g_array_append_val(field->dynamic_offsets, *offset);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ }
+ // local_offset = field->array_offset;
+ // /* Set the offset at position 0 */
+ // compute_fields_offsets(tf, child, &local_offset, root);
+ }
+ break;
+ case LTT_SEQUENCE:
+ g_assert(type->fields->len == 2);
+ {
+ off_t local_offset;
+ LttField *child;
+ guint i;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+
+ child = &g_array_index(type->fields, LttField, 0);
+ compute_fields_offsets(tf, child, offset, root);
+ child = &g_array_index(type->fields, LttField, 1);
+ *offset += ltt_align(*offset, get_alignment(tf, child),
+ tf->has_alignment);
+ field->array_offset = *offset;
+
+ } else {
+ child = &g_array_index(type->fields, LttField, 1);
+ }
+ *offset = field->array_offset;
+ field->dynamic_offsets = g_array_set_size(field->dynamic_offsets,
+ 0);
+ for(i=0; i<ltt_event_field_element_number(&tf->event, field); i++) {
+ g_array_append_val(field->dynamic_offsets, *offset);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ // local_offset = field->array_offset;
+ // /* Set the offset at position 0 */
+ // compute_fields_offsets(tf, child, &local_offset, root);
+ }
+ break;
+ case LTT_STRUCT:
+ {
+ LttField *child;
+ guint i;
+ gint ret=0;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ } else {
+ *offset = field->offset_root;
+ }
+ for(i=0; i<type->fields->len; i++) {
+ child = &g_array_index(type->fields, LttField, i);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ }
+ break;
+ case LTT_UNION:
+ {
+ LttField *child;
+ guint i;
+ gint ret=0;
+ if(field->fixed_root == FIELD_VARIABLE) {
+ *offset += ltt_align(*offset, get_alignment(tf, field),
+ tf->has_alignment);
+ /* remember offset */
+ field->offset_root = *offset;
+ }
+ for(i=0; i<type->fields->len; i++) {
+ *offset = field->offset_root;
+ child = &g_array_index(type->fields, LttField, i);
+ compute_fields_offsets(tf, child, offset, root);
+ }
+ *offset = field->offset_root + field->field_size;
+ }
+ break;
+ case LTT_NONE:
+ default:
+ g_error("compute_fields_offsets : unknown type");
+ }
+
+}
+
+
+/*****************************************************************************
+ *Function name
+ * compute_offsets : set the dynamically computable offsets of an event type
+ *Input params
+ * tf : tracefile
+ * event : event type
+ *
+ ****************************************************************************/
+void compute_offsets(LttTracefile *tf, LttEventType *event, off_t *offset,
+ void *root)
+{
+ guint i;
+ gint ret;
+
+ /* compute all variable offsets */
+ for(i=0; i<event->fields->len; i++) {
+ LttField *field = &g_array_index(event->fields, LttField, i);
+ compute_fields_offsets(tf, field, offset, root);
+ if(ret) break;
+ }
+
+}
+