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