new version of the reading API
[lttv.git] / ltt / branches / poly / ltt / facility.c
CommitLineData
6cd62ccf 1#include <stdlib.h>
2#include <string.h>
3#include <stdio.h>
4
fcdf0ec2 5#include <ltt/LTTTypes.h>
6cd62ccf 6#include "parser.h"
7#include <ltt/facility.h>
8
9/* search for the (named) type in the table, if it does not exist
10 create a new one */
963b5f2d 11LttType * lookup_named_type(LttFacility *fac, type_descriptor * td);
6cd62ccf 12
13/* construct directed acyclic graph for types, and tree for fields */
963b5f2d 14void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
15 LttField * fld);
6cd62ccf 16
17/* generate the facility according to the events belongin to it */
963b5f2d 18void generateFacility(LttFacility * f, facility * fac,
19 LttChecksum checksum);
6cd62ccf 20
21/* functions to release the memory occupied by a facility */
963b5f2d 22void freeFacility(LttFacility * facility);
23void freeEventtype(LttEventType * evType);
6cd62ccf 24void freeAllNamedTypes(table * named_types);
25void freeAllUnamedTypes(sequence * unnamed_types);
26void freeAllFields(sequence * all_fields);
963b5f2d 27void freeLttType(LttType * type);
28void freeLttField(LttField * fld);
6cd62ccf 29
30
31/*****************************************************************************
32 *Function name
963b5f2d 33 * ltt_facility_open : open facilities
6cd62ccf 34 *Input params
963b5f2d 35 * t : the trace containing the facilities
6cd62ccf 36 * pathname : the path name of the facility
6cd62ccf 37 ****************************************************************************/
38
963b5f2d 39void ltt_facility_open(LttTrace * t, char * pathname)
6cd62ccf 40{
41 char *token;
42 parse_file in;
43 char buffer[BUFFER_SIZE];
963b5f2d 44 facility * fac;
45 LttFacility * f;
46 LttChecksum checksum;
6cd62ccf 47
48 in.buffer = buffer;
49 in.lineno = 0;
50 in.error = error_callback;
963b5f2d 51 in.name = pathname;
6cd62ccf 52
53 in.fp = fopen(in.name, "r");
963b5f2d 54 if(!in.fp ) in.error(&in,"cannot open input file");
55
6cd62ccf 56 while(1){
57 token = getToken(&in);
58 if(in.type == ENDFILE) break;
59
963b5f2d 60 if(strcmp(token, "<")) in.error(&in,"not a facility file");
61 token = getName(&in);
62
63 if(strcmp("facility",token) == 0) {
64 fac = g_new(facility, 1);
65 fac->name = NULL;
66 fac->description = NULL;
67 sequence_init(&(fac->events));
68 table_init(&(fac->named_types));
69 sequence_init(&(fac->unnamed_types));
70
71 parseFacility(&in, fac);
72
73 //check if any namedType is not defined
74 checkNamedTypesImplemented(&fac->named_types);
75
76 generateChecksum(fac->name, &checksum, &fac->events);
77
78 f = g_new(LttFacility,1);
79 generateFacility(f, fac, checksum);
80
81 t->facility_number++;
82 g_ptr_array_add(t->facilities,f);
83
84 free(fac->name);
85 free(fac->description);
86 freeEvents(&fac->events);
87 sequence_dispose(&fac->events);
88 freeNamedType(&fac->named_types);
89 table_dispose(&fac->named_types);
90 freeTypes(&fac->unnamed_types);
91 sequence_dispose(&fac->unnamed_types);
92 free(fac);
6cd62ccf 93 }
963b5f2d 94 else in.error(&in,"facility token was expected");
6cd62ccf 95 }
6cd62ccf 96 fclose(in.fp);
6cd62ccf 97}
98
99
100/*****************************************************************************
101 *Function name
102 * generateFacility : generate facility, internal function
103 *Input params
963b5f2d 104 * facility : LttFacilty structure
105 * fac : facility structure
6cd62ccf 106 * checksum : checksum of the facility
6cd62ccf 107 ****************************************************************************/
108
963b5f2d 109void generateFacility(LttFacility *f, facility *fac,LttChecksum checksum)
6cd62ccf 110{
963b5f2d 111 char * facilityName = fac->name;
112 sequence * events = &fac->events;
6cd62ccf 113 int i;
963b5f2d 114 LttEventType * evType;
115 LttField * field;
116 LttType * type;
6cd62ccf 117
963b5f2d 118 f->name = g_strdup(facilityName);
119 f->event_number = events->position;
120 f->checksum = checksum;
6cd62ccf 121
122 //initialize inner structures
963b5f2d 123 f->events = g_new(LttEventType*,f->event_number);
124 sequence_init(&(f->all_fields));
125 sequence_init(&(f->all_unnamed_types));
126 table_init(&(f->all_named_types));
6cd62ccf 127
128 //for each event, construct field tree and type graph
129 for(i=0;i<events->position;i++){
963b5f2d 130 evType = g_new(LttEventType,1);
131 f->events[i] = evType;
6cd62ccf 132
133 evType->name = g_strdup(((event*)(events->array[i]))->name);
134 evType->description=g_strdup(((event*)(events->array[i]))->description);
135
963b5f2d 136 field = g_new(LttField, 1);
137 sequence_push(&(f->all_fields), field);
6cd62ccf 138 evType->root_field = field;
963b5f2d 139 evType->facility = f;
6cd62ccf 140 evType->index = i;
141
142 field->field_pos = 0;
963b5f2d 143 type = lookup_named_type(f,((event*)(events->array[i]))->type);
6cd62ccf 144 field->field_type = type;
145 field->offset_root = 0;
146 field->fixed_root = 1;
147 field->offset_parent = 0;
148 field->fixed_parent = 1;
149 // field->base_address = NULL;
150 field->field_size = 0;
151 field->field_fixed = -1;
152 field->parent = NULL;
153 field->child = NULL;
154 field->current_element = 0;
155
156 //construct field tree and type graph
963b5f2d 157 constructTypeAndFields(f,((event*)(events->array[i]))->type,field);
6cd62ccf 158 }
159}
160
161
162/*****************************************************************************
163 *Function name
164 * constructTypeAndFields : construct field tree and type graph,
165 * internal recursion function
166 *Input params
167 * fac : facility struct
168 * td : type descriptor
169 * root_field : root field of the event
170 ****************************************************************************/
171
963b5f2d 172void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
173 LttField * fld)
6cd62ccf 174{
175 int i;
176 type_descriptor * tmpTd;
177
178 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
179 // fld->field_size = 0;
180 // else fld->field_size = -1;
181
182 if(td->type == LTT_ENUM){
183 fld->field_type->element_number = td->labels.position;
184 fld->field_type->enum_strings = g_new(char*,td->labels.position);
185 for(i=0;i<td->labels.position;i++){
186 fld->field_type->enum_strings[i]
187 = g_strdup(((char*)(td->labels.array[i])));
188 }
189 }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){
190 if(td->type == LTT_ARRAY)
191 fld->field_type->element_number = (unsigned)td->size;
963b5f2d 192 fld->field_type->element_type = g_new(LttType*,1);
6cd62ccf 193 tmpTd = td->nested_type;
194 fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd);
963b5f2d 195 fld->child = g_new(LttField*, 1);
196 fld->child[0] = g_new(LttField, 1);
6cd62ccf 197 sequence_push(&(fac->all_fields), fld->child[0]);
198
199 fld->child[0]->field_pos = 0;
200 fld->child[0]->field_type = fld->field_type->element_type[0];
201 fld->child[0]->offset_root = fld->offset_root;
202 fld->child[0]->fixed_root = fld->fixed_root;
203 fld->child[0]->offset_parent = 0;
204 fld->child[0]->fixed_parent = 1;
205 // fld->child[0]->base_address = NULL;
206 fld->child[0]->field_size = 0;
207 fld->child[0]->field_fixed = -1;
208 fld->child[0]->parent = fld;
209 fld->child[0]->child = NULL;
210 fld->child[0]->current_element = 0;
211 constructTypeAndFields(fac, tmpTd, fld->child[0]);
212 }else if(td->type == LTT_STRUCT){
213 fld->field_type->element_number = td->fields.position;
963b5f2d 214 fld->field_type->element_type = g_new(LttType*, td->fields.position);
215 fld->child = g_new(LttField*, td->fields.position);
6cd62ccf 216 for(i=0;i<td->fields.position;i++){
217 tmpTd = ((field*)(td->fields.array[i]))->type;
218 fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd);
963b5f2d 219 fld->child[i] = g_new(LttField,1);
6cd62ccf 220 sequence_push(&(fac->all_fields), fld->child[i]);
221
222 fld->child[i]->field_pos = i;
223 fld->child[i]->field_type = fld->field_type->element_type[i];
224 fld->child[i]->field_type->element_name
225 = g_strdup(((field*)(td->fields.array[i]))->name);
226 fld->child[i]->offset_root = -1;
227 fld->child[i]->fixed_root = -1;
228 fld->child[i]->offset_parent = -1;
229 fld->child[i]->fixed_parent = -1;
230 // fld->child[i]->base_address = NULL;
231 fld->child[i]->field_size = 0;
232 fld->child[i]->field_fixed = -1;
233 fld->child[i]->parent = fld;
234 fld->child[i]->child = NULL;
235 fld->child[i]->current_element = 0;
236 constructTypeAndFields(fac, tmpTd, fld->child[i]);
237 }
238 }
239}
240
241
242/*****************************************************************************
243 *Function name
244 * lookup_named_type: search named type in the table
245 * internal function
246 *Input params
247 * fac : facility struct
248 * td : type descriptor
249 *Return value
963b5f2d 250 * : either find the named type, or create a new LttType
6cd62ccf 251 ****************************************************************************/
252
963b5f2d 253LttType * lookup_named_type(LttFacility *fac, type_descriptor * td)
6cd62ccf 254{
963b5f2d 255 LttType * lttType = NULL;
6cd62ccf 256 int i;
257 char * name;
258 if(td->type_name){
259 for(i=0;i<fac->all_named_types.keys.position;i++){
260 name = (char *)(fac->all_named_types.keys.array[i]);
261 if(strcmp(name, td->type_name)==0){
963b5f2d 262 lttType = (LttType*)(fac->all_named_types.values.array[i]);
6cd62ccf 263 break;
264 }
265 }
266 }
267
268 if(!lttType){
963b5f2d 269 lttType = g_new(LttType,1);
6cd62ccf 270 lttType->type_class = td->type;
271 if(td->fmt) lttType->fmt = g_strdup(td->fmt);
272 else lttType->fmt = NULL;
273 lttType->size = td->size;
274 lttType->enum_strings = NULL;
275 lttType->element_type = NULL;
276 lttType->element_number = 0;
277 if(td->type_name){
278 name = g_strdup(td->type_name);
279 table_insert(&(fac->all_named_types),name,lttType);
280 lttType->element_name = name;
281 }
282 else{
283 sequence_push(&(fac->all_unnamed_types), lttType);
284 lttType->element_name = NULL;
285 }
286 }
287
288 return lttType;
289}
290
291
292/*****************************************************************************
293 *Function name
294 * ltt_facility_close : close a facility, decrease its usage count,
295 * if usage count = 0, release the memory
296 *Input params
297 * f : facility that will be closed
298 *Return value
299 * int : usage count ?? status
300 ****************************************************************************/
301
963b5f2d 302int ltt_facility_close(LttFacility *f)
6cd62ccf 303{
6cd62ccf 304 //release the memory it occupied
305 freeFacility(f);
306
307 return 0;
308}
309
310/*****************************************************************************
311 * Functions to release the memory occupied by the facility
312 ****************************************************************************/
313
963b5f2d 314void freeFacility(LttFacility * fac)
6cd62ccf 315{
316 int i;
317 g_free(fac->name); //free facility name
318
319 //free event types
320 for(i=0;i<fac->event_number;i++){
321 freeEventtype(fac->events[i]);
322 }
963b5f2d 323 g_free(fac->events);
6cd62ccf 324
325 //free all named types
326 freeAllNamedTypes(&(fac->all_named_types));
327
328 //free all unnamed types
329 freeAllUnamedTypes(&(fac->all_unnamed_types));
330
331 //free all fields
332 freeAllFields(&(fac->all_fields));
333
334 //free the facility itself
335 g_free(fac);
336}
337
963b5f2d 338void freeEventtype(LttEventType * evType)
6cd62ccf 339{
340 g_free(evType->name);
341 if(evType->description)
342 g_free(evType->description);
343 g_free(evType);
344}
345
346void freeAllNamedTypes(table * named_types)
347{
348 int i;
349 for(i=0;i<named_types->keys.position;i++){
350 //free the name of the type
351 g_free((char*)(named_types->keys.array[i]));
352
353 //free type
963b5f2d 354 freeLttType((LttType*)(named_types->values.array[i]));
6cd62ccf 355 }
356 table_dispose(named_types);
357}
358
359void freeAllUnamedTypes(sequence * unnamed_types)
360{
361 int i;
362 for(i=0;i<unnamed_types->position;i++){
963b5f2d 363 freeLttType((LttType*)(unnamed_types->array[i]));
6cd62ccf 364 }
365 sequence_dispose(unnamed_types);
366}
367
368void freeAllFields(sequence * all_fields)
369{
370 int i;
371 for(i=0;i<all_fields->position;i++){
963b5f2d 372 freeLttField((LttField*)(all_fields->array[i]));
6cd62ccf 373 }
374 sequence_dispose(all_fields);
375}
376
963b5f2d 377//only free current type, not child types
378void freeLttType(LttType * type)
6cd62ccf 379{
963b5f2d 380 int i;
6cd62ccf 381 if(type->element_name)
382 g_free(type->element_name);
383 if(type->fmt)
384 g_free(type->fmt);
963b5f2d 385 if(type->enum_strings){
386 for(i=0;i<type->element_number;i++)
387 g_free(type->enum_strings[i]);
6cd62ccf 388 g_free(type->enum_strings);
963b5f2d 389 }
390
391 if(type->element_type){
6cd62ccf 392 g_free(type->element_type);
963b5f2d 393 }
6cd62ccf 394 g_free(type);
395}
396
963b5f2d 397//only free the current field, not child fields
398void freeLttField(LttField * fld)
6cd62ccf 399{
400 if(fld->child)
401 g_free(fld->child);
402 g_free(fld);
403}
404
405/*****************************************************************************
406 *Function name
407 * ltt_facility_name : obtain the facility's name
408 *Input params
409 * f : the facility that will be closed
410 *Return value
411 * char * : the facility's name
412 ****************************************************************************/
413
963b5f2d 414char *ltt_facility_name(LttFacility *f)
6cd62ccf 415{
416 return f->name;
417}
418
419/*****************************************************************************
420 *Function name
421 * ltt_facility_checksum : obtain the facility's checksum
422 *Input params
423 * f : the facility that will be closed
424 *Return value
963b5f2d 425 * LttChecksum : the checksum of the facility
6cd62ccf 426 ****************************************************************************/
427
963b5f2d 428LttChecksum ltt_facility_checksum(LttFacility *f)
6cd62ccf 429{
430 return f->checksum;
431}
432
963b5f2d 433/*****************************************************************************
434 *Function name
435 * ltt_facility_base_id : obtain the facility base id
436 *Input params
437 * f : the facility
438 *Return value
439 * : the base id of the facility
440 ****************************************************************************/
441
442unsigned ltt_facility_base_id(LttFacility *f)
443{
444 return f->base_id;
445}
446
6cd62ccf 447/*****************************************************************************
448 *Function name
449 * ltt_facility_eventtype_number: obtain the number of the event types
450 *Input params
451 * f : the facility that will be closed
452 *Return value
453 * unsigned : the number of the event types
454 ****************************************************************************/
455
963b5f2d 456unsigned ltt_facility_eventtype_number(LttFacility *f)
6cd62ccf 457{
458 return (unsigned)(f->event_number);
459}
460
461/*****************************************************************************
462 *Function name
463 * ltt_facility_eventtype_get: obtain the event type according to event id
464 * from 0 to event_number - 1
465 *Input params
466 * f : the facility that will be closed
467 *Return value
963b5f2d 468 * LttEventType * : the event type required
6cd62ccf 469 ****************************************************************************/
470
963b5f2d 471LttEventType *ltt_facility_eventtype_get(LttFacility *f, unsigned i)
6cd62ccf 472{
473 return f->events[i];
474}
475
476/*****************************************************************************
477 *Function name
478 * ltt_facility_eventtype_get_by_name
479 * : obtain the event type according to event name
480 * event name is unique in the facility
481 *Input params
482 * f : the facility that will be closed
483 * name : the name of the event
484 *Return value
963b5f2d 485 * LttEventType * : the event type required
6cd62ccf 486 ****************************************************************************/
487
963b5f2d 488LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, char *name)
6cd62ccf 489{
490 int i;
963b5f2d 491 LttEventType * ev;
6cd62ccf 492 for(i=0;i<f->event_number;i++){
493 ev = f->events[i];
494 if(strcmp(ev->name, name) == 0)break;
495 }
496
497 if(i==f->event_number) return NULL;
498 else return ev;
499}
500
This page took 0.042276 seconds and 4 git commands to generate.