1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 * 2005 Mathieu Desnoyers
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #include <sys/types.h>
36 #include "ltt-private.h"
37 #include <ltt/facility.h>
45 /* search for the (named) type in the table, if it does not exist
47 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor_t
* td
);
49 /* construct directed acyclic graph for types, and tree for fields */
50 void construct_types_and_fields(LttFacility
* fac
, type_descriptor_t
* td
,
53 /* generate the facility according to the events belongin to it */
54 void generateFacility(LttFacility
* f
, facility_t
* fac
,
57 /* functions to release the memory occupied by a facility */
58 void freeFacility(LttFacility
* facility
);
59 void freeEventtype(LttEventType
* evType
);
60 void freeLttType(LttType
** type
);
61 void freeLttField(LttField
* fld
);
62 void freeLttNamedType(LttType
* type
);
65 /*****************************************************************************
67 * ltt_facility_open : open facilities
69 * t : the trace containing the facilities
70 * pathname : the path name of the facility
72 *returns 0 on success, 1 on error.
73 ****************************************************************************/
75 int ltt_facility_open(LttFacility
*f
, LttTrace
* t
, gchar
* pathname
)
81 unsigned long checksum
;
82 //GError * error = NULL;
83 gchar buffer
[BUFFER_SIZE
];
85 in
.buffer
= &(buffer
[0]);
87 in
.error
= error_callback
;
91 //in.fd = g_open(in.name, O_RDONLY, 0);
93 in
.fp
= fopen(in
.name
, "r");
95 g_warning("cannot open facility description file %s",
101 //in.channel = g_io_channel_unix_new(in.fd);
105 token
= getToken(&in
);
106 if(in
.type
== ENDFILE
) break;
108 if(g_ascii_strcasecmp(token
, "<")) in
.error(&in
,"not a facility file");
109 token
= getName(&in
);
111 if(g_ascii_strcasecmp("facility",token
) == 0) {
112 fac
= g_new(facility_t
, 1);
114 fac
->description
= NULL
;
115 sequence_init(&(fac
->events
));
116 table_init(&(fac
->named_types
));
117 sequence_init(&(fac
->unnamed_types
));
119 parseFacility(&in
, fac
);
121 //check if any namedType is not defined
122 checkNamedTypesImplemented(&fac
->named_types
);
124 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
126 generateFacility(f
, fac
, checksum
);
129 g_free(fac
->description
);
130 freeEvents(&fac
->events
);
131 sequence_dispose(&fac
->events
);
132 freeNamedType(&fac
->named_types
);
133 table_dispose(&fac
->named_types
);
134 freeTypes(&fac
->unnamed_types
);
135 sequence_dispose(&fac
->unnamed_types
);
139 g_warning("facility token was expected in file %s", in
.name
);
146 //g_io_channel_shutdown(in.channel, FALSE, &error); /* No flush */
147 //if(error != NULL) {
150 // g_warning("Can not close file: \n%s\n", error->message);
151 // g_error_free(error);
159 /*****************************************************************************
161 * generateFacility : generate facility, internal function
163 * facility : LttFacilty structure
164 * fac : facility structure
165 * checksum : checksum of the facility
166 ****************************************************************************/
168 void generateFacility(LttFacility
*f
, facility_t
*fac
, guint32 checksum
)
170 char * facilityName
= fac
->name
;
171 sequence_t
* events
= &fac
->events
;
173 //LttEventType * evType;
174 LttEventType
* event_type
;
178 g_assert(f
->name
== g_quark_from_string(facilityName
));
179 g_assert(f
->checksum
== checksum
);
181 //f->event_number = events->position;
183 //initialize inner structures
184 f
->events
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttEventType
),
186 //f->events = g_new(LttEventType*,f->event_number);
187 f
->events
= g_array_set_size(f
->events
, events
->position
);
189 g_datalist_init(&f
->events_by_name
);
190 g_datalist_init(&f
->named_types
);
192 //f->named_types_number = fac->named_types.keys.position;
193 //f->named_types = g_array_sized_new (FALSE, TRUE, sizeof(LttType),
194 // fac->named_types.keys.position);
195 //f->named_types = g_new(LttType*, fac->named_types.keys.position);
196 //f->named_types = g_array_set_size(f->named_types,
197 // fac->named_types.keys.position);
199 //for each event, construct field tree and type graph
200 for(i
=0;i
<events
->position
;i
++){
201 event_type
= &g_array_index(f
->events
, LttEventType
, i
);
202 //evType = g_new(LttEventType,1);
203 //f->events[i] = evType;
206 g_quark_from_string(((event_t
*)(events
->array
[i
]))->name
);
208 g_datalist_id_set_data(&f
->events_by_name
, event_type
->name
,
211 event_type
->description
=
212 g_strdup(((event_t
*)(events
->array
[i
]))->description
);
214 field
= g_new(LttField
, 1);
215 event_type
->root_field
= field
;
216 event_type
->facility
= f
;
217 event_type
->index
= i
;
219 if(((event_t
*)(events
->array
[i
]))->type
!= NULL
){
220 // field->field_pos = 0;
221 type
= lookup_named_type(f
,((event_t
*)(events
->array
[i
]))->type
);
222 field
->field_type
= type
;
223 field
->offset_root
= 0;
224 field
->fixed_root
= FIELD_UNKNOWN
;
225 field
->offset_parent
= 0;
226 field
->fixed_parent
= FIELD_UNKNOWN
;
227 // field->base_address = NULL;
228 field
->field_size
= 0;
229 field
->fixed_size
= FIELD_UNKNOWN
;
230 field
->parent
= NULL
;
232 field
->current_element
= 0;
234 //construct field tree and type graph
235 construct_types_and_fields(f
,((event_t
*)(events
->array
[i
]))->type
,field
);
237 event_type
->root_field
= NULL
;
244 /*****************************************************************************
246 * construct_types_and_fields : construct field tree and type graph,
247 * internal recursion function
249 * fac : facility struct
250 * td : type descriptor
251 * root_field : root field of the event
252 ****************************************************************************/
255 void construct_types_and_fields(LttFacility
* fac
, type_descriptor_t
* td
,
259 type_descriptor_t
* tmpTd
;
265 fld
->field_type
->size
= td
->size
;
273 fld
->field_type
->size
= 0;
276 fld
->field_type
->size
= 0;
279 fld
->field_type
->element_number
= td
->labels
.position
;
280 fld
->field_type
->enum_strings
= g_new(GQuark
,td
->labels
.position
);
281 for(i
=0;i
<td
->labels
.position
;i
++){
282 fld
->field_type
->enum_strings
[i
]
283 = g_quark_from_string(((char*)(td
->labels
.array
[i
])));
285 fld
->field_type
->size
= td
->size
;
289 fld
->field_type
->element_number
= (unsigned)td
->size
;
291 fld
->field_type
->element_type
= g_new(LttType
*,1);
292 tmpTd
= td
->nested_type
;
293 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
294 fld
->child
= g_new(LttField
*, 1);
295 fld
->child
[0] = g_new(LttField
, 1);
297 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
298 fld
->child
[0]->offset_root
= 0;
299 fld
->child
[0]->fixed_root
= FIELD_UNKNOWN
;
300 fld
->child
[0]->offset_parent
= 0;
301 fld
->child
[0]->fixed_parent
= FIELD_UNKNOWN
;
302 fld
->child
[0]->field_size
= 0;
303 fld
->child
[0]->fixed_size
= FIELD_UNKNOWN
;
304 fld
->child
[0]->parent
= fld
;
305 fld
->child
[0]->child
= NULL
;
306 fld
->child
[0]->current_element
= 0;
307 construct_types_and_fields(fac
, tmpTd
, fld
->child
[0]);
312 fld
->field_type
->element_number
= td
->fields
.position
;
314 g_assert(fld
->field_type
->element_type
== NULL
);
315 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
317 fld
->child
= g_new(LttField
*, td
->fields
.position
);
318 for(i
=0;i
<td
->fields
.position
;i
++){
319 tmpTd
= ((field_t
*)(td
->fields
.array
[i
]))->type
;
321 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
322 fld
->child
[i
] = g_new(LttField
,1);
324 // fld->child[i]->field_pos = i;
325 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
327 fld
->child
[i
]->field_type
->element_name
328 = g_quark_from_string(((field_t
*)(td
->fields
.array
[i
]))->name
);
330 fld
->child
[i
]->offset_root
= 0;
331 fld
->child
[i
]->fixed_root
= FIELD_UNKNOWN
;
332 fld
->child
[i
]->offset_parent
= 0;
333 fld
->child
[i
]->fixed_parent
= FIELD_UNKNOWN
;
334 fld
->child
[i
]->field_size
= 0;
335 fld
->child
[i
]->fixed_size
= FIELD_UNKNOWN
;
336 fld
->child
[i
]->parent
= fld
;
337 fld
->child
[i
]->child
= NULL
;
338 fld
->child
[i
]->current_element
= 0;
339 construct_types_and_fields(fac
, tmpTd
, fld
->child
[i
]);
344 g_error("construct_types_and_fields : unknown type");
353 void construct_types_and_fields(LttFacility
* fac
, type_descriptor
* td
,
357 type_descriptor
* tmpTd
;
359 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
360 // fld->field_size = 0;
361 // else fld->field_size = -1;
363 if(td
->type
== LTT_ENUM
){
364 fld
->field_type
->element_number
= td
->labels
.position
;
365 fld
->field_type
->enum_strings
= g_new(GQuark
,td
->labels
.position
);
366 for(i
=0;i
<td
->labels
.position
;i
++){
367 fld
->field_type
->enum_strings
[i
]
368 = g_quark_from_string(((char*)(td
->labels
.array
[i
])));
370 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
371 if(td
->type
== LTT_ARRAY
)
372 fld
->field_type
->element_number
= (unsigned)td
->size
;
373 fld
->field_type
->element_type
= g_new(LttType
*,1);
374 tmpTd
= td
->nested_type
;
375 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
376 fld
->child
= g_new(LttField
*, 1);
377 fld
->child
[0] = g_new(LttField
, 1);
379 // fld->child[0]->field_pos = 0;
380 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
381 fld
->child
[0]->offset_root
= fld
->offset_root
;
382 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
383 fld
->child
[0]->offset_parent
= 0;
384 fld
->child
[0]->fixed_parent
= 1;
385 // fld->child[0]->base_address = NULL;
386 fld
->child
[0]->field_size
= 0;
387 fld
->child
[0]->field_fixed
= -1;
388 fld
->child
[0]->parent
= fld
;
389 fld
->child
[0]->child
= NULL
;
390 fld
->child
[0]->current_element
= 0;
391 construct_types_and_fields(fac
, tmpTd
, fld
->child
[0]);
392 }else if(td
->type
== LTT_STRUCT
){
393 fld
->field_type
->element_number
= td
->fields
.position
;
395 if(fld
->field_type
->element_type
== NULL
){
396 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
402 fld
->child
= g_new(LttField
*, td
->fields
.position
);
403 for(i
=0;i
<td
->fields
.position
;i
++){
404 tmpTd
= ((type_fields
*)(td
->fields
.array
[i
]))->type
;
407 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
408 fld
->child
[i
] = g_new(LttField
,1);
410 fld
->child
[i
]->field_pos
= i
;
411 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
414 fld
->child
[i
]->field_type
->element_name
415 = g_quark_from_string(((type_fields
*)(td
->fields
.array
[i
]))->name
);
418 fld
->child
[i
]->offset_root
= -1;
419 fld
->child
[i
]->fixed_root
= -1;
420 fld
->child
[i
]->offset_parent
= -1;
421 fld
->child
[i
]->fixed_parent
= -1;
422 // fld->child[i]->base_address = NULL;
423 fld
->child
[i
]->field_size
= 0;
424 fld
->child
[i
]->field_fixed
= -1;
425 fld
->child
[i
]->parent
= fld
;
426 fld
->child
[i
]->child
= NULL
;
427 fld
->child
[i
]->current_element
= 0;
428 construct_types_and_fields(fac
, tmpTd
, fld
->child
[i
]);
434 /*****************************************************************************
436 * lookup_named_type: search named type in the table
439 * fac : facility struct
440 * td : type descriptor
442 * : either find the named type, or create a new LttType
443 ****************************************************************************/
445 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor_t
* td
)
447 LttType
*type
= NULL
;
450 if(td
->type_name
!= NULL
) {
452 name
= g_quark_from_string(td
->type_name
);
454 type
= g_datalist_id_get_data(&fac
->named_types
, name
);
458 /* Create the type */
459 type
= g_new(LttType
,1);
460 type
->type_name
= name
;
461 type
->type_class
= td
->type
;
462 if(td
->fmt
) type
->fmt
= g_strdup(td
->fmt
);
463 else type
->fmt
= NULL
;
464 type
->size
= td
->size
;
465 type
->enum_strings
= NULL
;
466 type
->element_type
= NULL
;
467 type
->element_number
= 0;
469 if(td
->type_name
!= NULL
)
470 g_datalist_id_set_data_full(&fac
->named_types
, name
,
471 type
, (GDestroyNotify
)freeLttNamedType
);
477 /*****************************************************************************
479 * ltt_facility_close : close a facility, decrease its usage count,
480 * if usage count = 0, release the memory
482 * f : facility that will be closed
483 ****************************************************************************/
485 void ltt_facility_close(LttFacility
*f
)
487 //release the memory it occupied
491 /*****************************************************************************
492 * Functions to release the memory occupied by the facility
493 ****************************************************************************/
495 void freeFacility(LttFacility
* fac
)
500 for(i
=0; i
<fac
->events
->len
; i
++) {
501 et
= &g_array_index (fac
->events
, LttEventType
, i
);
504 g_array_free(fac
->events
, TRUE
);
506 g_datalist_clear(&fac
->named_types
);
510 void freeEventtype(LttEventType
* evType
)
513 if(evType
->description
)
514 g_free(evType
->description
);
515 if(evType
->root_field
){
516 root_type
= evType
->root_field
->field_type
;
517 freeLttField(evType
->root_field
);
518 freeLttType(&root_type
);
522 void freeLttNamedType(LttType
* type
)
527 void freeLttType(LttType
** type
)
530 if(*type
== NULL
) return;
531 if((*type
)->type_name
!= 0) return; //this is a named type.
532 //if((*type)->type_name){
533 // return; //this is a named type
536 g_free((*type
)->fmt
);
537 if((*type
)->enum_strings
){
538 g_free((*type
)->enum_strings
);
541 if((*type
)->element_type
){
542 for(i
=0;i
<(*type
)->element_number
;i
++)
543 freeLttType(&((*type
)->element_type
[i
]));
544 g_free((*type
)->element_type
);
550 void freeLttField(LttField
* fld
)
556 if(fld
->field_type
->type_class
== LTT_ARRAY
||
557 fld
->field_type
->type_class
== LTT_SEQUENCE
){
559 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
560 size
= fld
->field_type
->element_number
;
565 for(i
=0; i
<size
; i
++){
566 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
573 /*****************************************************************************
575 * ltt_facility_name : obtain the facility's name
579 * GQuark : the facility's name
580 ****************************************************************************/
582 GQuark
ltt_facility_name(LttFacility
*f
)
587 /*****************************************************************************
589 * ltt_facility_checksum : obtain the facility's checksum
593 * : the checksum of the facility
594 ****************************************************************************/
596 guint32
ltt_facility_checksum(LttFacility
*f
)
601 /*****************************************************************************
603 * ltt_facility_base_id : obtain the facility base id
607 * : the base id of the facility
608 ****************************************************************************/
610 guint
ltt_facility_id(LttFacility
*f
)
615 /*****************************************************************************
617 * ltt_facility_eventtype_number: obtain the number of the event types
619 * f : the facility that will be closed
621 * : the number of the event types
622 ****************************************************************************/
624 guint8
ltt_facility_eventtype_number(LttFacility
*f
)
626 return (f
->events
->len
);
629 /*****************************************************************************
631 * ltt_facility_eventtype_get: obtain the event type according to event id
632 * from 0 to event_number - 1
634 * f : the facility that will be closed
636 * LttEventType * : the event type required
637 ****************************************************************************/
639 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, guint8 i
)
641 if(!f
->exists
) return NULL
;
643 g_assert(i
< f
->events
->len
);
644 return &g_array_index(f
->events
, LttEventType
, i
);
647 /*****************************************************************************
649 * ltt_facility_eventtype_get_by_name
650 * : obtain the event type according to event name
651 * event name is unique in the facility
654 * name : the name of the event
656 * LttEventType * : the event type required
657 ****************************************************************************/
659 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, GQuark name
)
661 LttEventType
*et
= g_datalist_id_get_data(&f
->events_by_name
, name
);
This page took 0.046054 seconds and 4 git commands to generate.