03d7fdf3 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Xiangxiu Yang |
3 | * 2005 Mathieu Desnoyers |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License Version 2.1 as published by the Free Software Foundation. |
8 | * |
9 | * This library 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 GNU |
12 | * Lesser General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the |
16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | * Boston, MA 02111-1307, USA. |
18 | */ |
19 | |
20 | #ifdef HAVE_CONFIG_H |
21 | #include <config.h> |
22 | #endif |
23 | |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <stdio.h> |
27 | #include <glib.h> |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
30 | #include <fcntl.h> |
31 | |
32 | |
33 | |
34 | #include "parser.h" |
35 | #include <ltt/ltt.h> |
36 | #include "ltt-private.h" |
37 | #include <ltt/facility.h> |
38 | |
39 | #ifndef g_open |
40 | #define g_open open |
41 | #endif |
42 | |
43 | #define g_close close |
44 | |
45 | /* search for the (named) type in the table, if it does not exist |
46 | create a new one */ |
47 | LttType * lookup_named_type(LttFacility *fac, type_descriptor_t * td); |
48 | |
49 | /* construct directed acyclic graph for types, and tree for fields */ |
50 | void construct_fields(LttFacility *fac, |
51 | LttField *field, |
52 | field_t *fld); |
53 | |
54 | /* generate the facility according to the events belongin to it */ |
55 | void generateFacility(LttFacility * f, facility_t * fac, |
56 | guint32 checksum); |
57 | |
58 | /* functions to release the memory occupied by a facility */ |
59 | void freeFacility(LttFacility * facility); |
60 | void freeEventtype(LttEventType * evType); |
61 | void freeLttType(LttType * type); |
62 | void freeLttField(LttField * fld); |
63 | void freeLttNamedType(LttType * type); |
64 | |
65 | |
66 | /***************************************************************************** |
67 | *Function name |
68 | * ltt_facility_open : open facilities |
69 | *Input params |
70 | * t : the trace containing the facilities |
71 | * pathname : the path name of the facility |
72 | * |
73 | * Open the facility corresponding to the right checksum. |
74 | * |
75 | *returns 0 on success, 1 on error. |
76 | ****************************************************************************/ |
77 | |
78 | int ltt_facility_open(LttFacility *f, LttTrace * t, gchar * pathname) |
79 | { |
80 | int ret = 0; |
81 | gchar *token; |
82 | parse_file_t in; |
83 | facility_t * fac; |
84 | unsigned int checksum; |
85 | gchar buffer[BUFFER_SIZE]; |
86 | gboolean generated = FALSE; |
87 | |
88 | in.buffer = &(buffer[0]); |
89 | in.lineno = 0; |
90 | in.error = error_callback; |
91 | in.name = pathname; |
92 | in.unget = 0; |
93 | |
94 | in.fp = fopen(in.name, "r"); |
95 | if(in.fp == NULL) { |
96 | g_warning("cannot open facility description file %s", |
97 | in.name); |
98 | ret = 1; |
99 | goto open_error; |
100 | } |
101 | |
102 | while(1){ |
103 | token = getToken(&in); |
104 | if(in.type == ENDFILE) break; |
105 | |
106 | if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file"); |
107 | token = getName(&in); |
108 | if(g_ascii_strcasecmp(token, "?")) in.error(&in,"not a facility file"); |
109 | token = getName(&in); |
110 | if(g_ascii_strcasecmp(token, "xml")) in.error(&in,"not a facility file"); |
111 | token = getName(&in); |
112 | if(g_ascii_strcasecmp(token, "version")) in.error(&in,"not a facility file"); |
113 | token = getName(&in); |
114 | if(g_ascii_strcasecmp(token, "=")) in.error(&in,"not a facility file"); |
115 | token = getQuotedString(&in); |
116 | if(g_ascii_strcasecmp(token, "1.0")) in.error(&in,"not a facility file"); |
117 | token = getName(&in); |
118 | if(g_ascii_strcasecmp(token, "?")) in.error(&in,"not a facility file"); |
119 | token = getToken(&in); |
120 | if(g_ascii_strcasecmp(token, ">")) in.error(&in,"not a facility file"); |
121 | |
122 | token = getToken(&in); |
123 | |
124 | if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file"); |
125 | token = getName(&in); |
126 | |
127 | if(g_ascii_strcasecmp("facility",token) == 0) { |
128 | fac = g_new(facility_t, 1); |
129 | fac->name = NULL; |
130 | fac->description = NULL; |
131 | sequence_init(&(fac->events)); |
132 | table_init(&(fac->named_types)); |
133 | sequence_init(&(fac->unnamed_types)); |
134 | |
135 | parseFacility(&in, fac); |
136 | |
137 | //check if any namedType is not defined |
138 | checkNamedTypesImplemented(&fac->named_types); |
139 | |
140 | generateChecksum(fac->name, &checksum, &fac->events); |
141 | |
142 | if(checksum == f->checksum) { |
143 | generateFacility(f, fac, checksum); |
144 | generated = TRUE; |
145 | } |
146 | |
147 | g_free(fac->name); |
148 | free(fac->capname); |
149 | g_free(fac->description); |
150 | freeEvents(&fac->events); |
151 | sequence_dispose(&fac->events); |
152 | freeNamedType(&fac->named_types); |
153 | table_dispose(&fac->named_types); |
154 | freeTypes(&fac->unnamed_types); |
155 | sequence_dispose(&fac->unnamed_types); |
156 | g_free(fac); |
157 | if(generated) break; /* use the first good match */ |
158 | } |
159 | else { |
160 | g_warning("facility token was expected in file %s", in.name); |
161 | ret = 1; |
162 | goto parse_error; |
163 | } |
164 | } |
165 | |
166 | parse_error: |
167 | fclose(in.fp); |
168 | open_error: |
169 | |
170 | if(!generated) { |
171 | g_warning("Cannot find facility %s, checksum 0x%X", |
172 | g_quark_to_string(f->name), f->checksum); |
173 | ret = 1; |
174 | } |
175 | |
176 | return ret; |
177 | } |
178 | |
179 | |
180 | /***************************************************************************** |
181 | *Function name |
182 | * generateFacility : generate facility, internal function |
183 | *Input params |
184 | * facility : LttFacilty structure |
185 | * fac : facility structure |
186 | * checksum : checksum of the facility |
187 | ****************************************************************************/ |
188 | |
189 | void generateFacility(LttFacility *f, facility_t *fac, guint32 checksum) |
190 | { |
191 | char * facilityName = fac->name; |
192 | sequence_t * events = &fac->events; |
193 | unsigned int i, j; |
194 | LttType * type; |
195 | table_t *named_types = &fac->named_types; |
196 | |
197 | g_assert(f->name == g_quark_from_string(facilityName)); |
198 | g_assert(f->checksum == checksum); |
199 | |
200 | //f->event_number = events->position; |
201 | |
202 | //initialize inner structures |
203 | f->events = g_array_sized_new (FALSE, TRUE, sizeof(LttEventType), |
204 | events->position); |
205 | //f->events = g_new(LttEventType*,f->event_number); |
206 | f->events = g_array_set_size(f->events, events->position); |
207 | |
208 | g_datalist_init(&f->events_by_name); |
209 | // g_datalist_init(&f->named_types); |
210 | #if 0 |
211 | /* The first day, he created the named types */ |
212 | |
213 | for(i=0; i<named_types->keys.position; i++) { |
214 | GQuark name = g_quark_from_string((char*)named_types->keys.array[i]); |
215 | type_descriptor_t *td = (type_descriptor_t*)named_types->values.array[i]; |
216 | |
217 | /* Create the type */ |
218 | type = g_new(LttType,1); |
219 | type->type_name = name; |
220 | type->type_class = td->type; |
221 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
222 | else type->fmt = NULL; |
223 | type->size = td->size; |
224 | type->enum_strings = NULL; |
225 | type->element_type = NULL; |
226 | type->element_number = 0; |
227 | |
228 | construct_types_and_fields(type, td, NULL, NULL, ...); |
229 | |
230 | g_datalist_id_set_data_full(&fac->named_types, name, |
231 | type, (GDestroyNotify)freeLttNamedType); |
232 | |
233 | } |
234 | #endif //0 |
235 | /* The second day, he created the event fields and types */ |
236 | //for each event, construct field and type acyclic graph |
237 | for(i=0;i<events->position;i++){ |
238 | event_t *parser_event = (event_t*)events->array[i]; |
239 | LttEventType *event_type = &g_array_index(f->events, LttEventType, i); |
240 | |
241 | event_type->name = |
242 | g_quark_from_string(parser_event->name); |
243 | |
244 | g_datalist_id_set_data(&f->events_by_name, event_type->name, |
245 | event_type); |
246 | |
247 | event_type->description = |
248 | g_strdup(parser_event->description); |
249 | |
250 | event_type->index = i; |
251 | event_type->facility = f; |
252 | |
253 | event_type->fields = g_array_sized_new(FALSE, TRUE, |
254 | sizeof(LttField), parser_event->fields.position); |
255 | event_type->fields = |
256 | g_array_set_size(event_type->fields, parser_event->fields.position); |
257 | g_datalist_init(&event_type->fields_by_name); |
258 | |
259 | for(j=0; j<parser_event->fields.position; j++) { |
260 | LttField *field = &g_array_index(event_type->fields, LttField, j); |
261 | field_t *parser_field = (field_t*)parser_event->fields.array[j]; |
262 | |
263 | construct_fields(f, field, parser_field); |
264 | g_datalist_id_set_data(&event_type->fields_by_name, |
265 | field->name, |
266 | field); |
267 | } |
268 | } |
269 | |
270 | /* What about 2 days weeks ? */ |
271 | } |
272 | |
273 | |
274 | /***************************************************************************** |
275 | *Function name |
276 | * construct_types_and_fields : construct field tree and type graph, |
277 | * internal recursion function |
278 | *Input params |
279 | * fac : facility struct |
280 | * field : destination lttv field |
281 | * fld : source parser field |
282 | ****************************************************************************/ |
283 | |
284 | //DONE |
285 | //make the change for arrays and sequences |
286 | //no more root field. -> change this for an array of fields. |
287 | // Compute the field size here. |
288 | // Flag fields as "VARIABLE OFFSET" or "FIXED OFFSET" : as soon as |
289 | // a field with a variable size is found, all the following fields must |
290 | // be flagged with "VARIABLE OFFSET", this will be done by the offset |
291 | // precomputation. |
292 | |
293 | |
294 | void construct_fields(LttFacility *fac, |
295 | LttField *field, |
296 | field_t *fld) |
297 | { |
298 | guint len; |
299 | type_descriptor_t *td; |
300 | LttType *type; |
301 | |
302 | if(fld->name) |
303 | field->name = g_quark_from_string(fld->name); |
304 | else |
305 | fld->name = 0; |
306 | |
307 | if(fld->description) { |
308 | len = strlen(fld->description); |
309 | field->description = g_new(gchar, len+1); |
310 | strcpy(field->description, fld->description); |
311 | } |
312 | field->dynamic_offsets = NULL; |
313 | type = &field->field_type; |
314 | td = fld->type; |
315 | |
316 | type->enum_map = NULL; |
317 | type->fields = NULL; |
318 | type->fields_by_name = NULL; |
319 | type->network = td->network; |
320 | |
321 | switch(td->type) { |
322 | case INT_FIXED: |
323 | type->type_class = LTT_INT_FIXED; |
324 | type->size = td->size; |
325 | break; |
326 | case UINT_FIXED: |
327 | type->type_class = LTT_UINT_FIXED; |
328 | type->size = td->size; |
329 | break; |
330 | case POINTER: |
331 | type->type_class = LTT_POINTER; |
332 | type->size = fac->pointer_size; |
333 | break; |
334 | case CHAR: |
335 | type->type_class = LTT_CHAR; |
336 | type->size = td->size; |
337 | break; |
338 | case UCHAR: |
339 | type->type_class = LTT_UCHAR; |
340 | type->size = td->size; |
341 | g_assert(type->size != 0); |
342 | break; |
343 | case SHORT: |
344 | type->type_class = LTT_SHORT; |
345 | type->size = td->size; |
346 | break; |
347 | case USHORT: |
348 | type->type_class = LTT_USHORT; |
349 | type->size = td->size; |
350 | break; |
351 | case INT: |
352 | type->type_class = LTT_INT; |
353 | type->size = fac->int_size; |
354 | break; |
355 | case UINT: |
356 | type->type_class = LTT_UINT; |
357 | type->size = fac->int_size; |
358 | g_assert(type->size != 0); |
359 | break; |
360 | case LONG: |
361 | type->type_class = LTT_LONG; |
362 | type->size = fac->long_size; |
363 | break; |
364 | case ULONG: |
365 | type->type_class = LTT_ULONG; |
366 | type->size = fac->long_size; |
367 | break; |
368 | case SIZE_T: |
369 | type->type_class = LTT_SIZE_T; |
370 | type->size = fac->size_t_size; |
371 | break; |
372 | case SSIZE_T: |
373 | type->type_class = LTT_SSIZE_T; |
374 | type->size = fac->size_t_size; |
375 | break; |
376 | case OFF_T: |
377 | type->type_class = LTT_OFF_T; |
378 | type->size = fac->size_t_size; |
379 | break; |
380 | case FLOAT: |
381 | type->type_class = LTT_FLOAT; |
382 | type->size = td->size; |
383 | break; |
384 | case STRING: |
385 | type->type_class = LTT_STRING; |
386 | type->size = 0; |
387 | break; |
388 | case ENUM: |
389 | type->type_class = LTT_ENUM; |
390 | type->size = fac->int_size; |
391 | { |
392 | guint i; |
393 | type->enum_map = g_hash_table_new(g_direct_hash, g_direct_equal); |
394 | for(i=0; i<td->labels.position; i++) { |
395 | GQuark value = g_quark_from_string((char*)td->labels.array[i]); |
396 | gint key = *(int*)td->labels_values.array[i]; |
397 | g_hash_table_insert(type->enum_map, (gpointer)key, (gpointer)value); |
398 | } |
399 | } |
400 | g_assert(type->size != 0); |
401 | break; |
402 | case ARRAY: |
403 | type->type_class = LTT_ARRAY; |
404 | type->size = td->size; |
405 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
406 | td->fields.position); |
407 | type->fields = g_array_set_size(type->fields, td->fields.position); |
408 | { |
409 | guint i; |
410 | |
411 | for(i=0; i<td->fields.position; i++) { |
412 | field_t *schild = (field_t*)td->fields.array[i]; |
413 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
414 | |
415 | construct_fields(fac, dchild, schild); |
416 | } |
417 | } |
418 | break; |
419 | case SEQUENCE: |
420 | type->type_class = LTT_SEQUENCE; |
421 | type->size = 0; |
422 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
423 | td->fields.position); |
424 | type->fields = g_array_set_size(type->fields, td->fields.position); |
425 | { |
426 | guint i; |
427 | |
428 | for(i=0; i<td->fields.position; i++) { |
429 | field_t *schild = (field_t*)td->fields.array[i]; |
430 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
431 | |
432 | construct_fields(fac, dchild, schild); |
433 | } |
434 | } |
435 | break; |
436 | case STRUCT: |
437 | type->type_class = LTT_STRUCT; |
438 | type->size = 0; // Size not calculated by the parser. |
439 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
440 | td->fields.position); |
441 | type->fields = g_array_set_size(type->fields, td->fields.position); |
442 | g_datalist_init(&type->fields_by_name); |
443 | { |
444 | guint i; |
445 | |
446 | for(i=0; i<td->fields.position; i++) { |
447 | field_t *schild = (field_t*)td->fields.array[i]; |
448 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
449 | |
450 | construct_fields(fac, dchild, schild); |
451 | g_datalist_id_set_data(&type->fields_by_name, |
452 | dchild->name, |
453 | dchild); |
454 | } |
455 | } |
456 | break; |
457 | case UNION: |
458 | type->type_class = LTT_UNION; |
459 | type->size = 0; // Size not calculated by the parser. |
460 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
461 | td->fields.position); |
462 | type->fields = g_array_set_size(type->fields, td->fields.position); |
463 | g_datalist_init(&type->fields_by_name); |
464 | { |
465 | guint i; |
466 | |
467 | for(i=0; i<td->fields.position; i++) { |
468 | field_t *schild = (field_t*)td->fields.array[i]; |
469 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
470 | |
471 | construct_fields(fac, dchild, schild); |
472 | g_datalist_id_set_data(&type->fields_by_name, |
473 | dchild->name, |
474 | dchild); |
475 | } |
476 | } |
477 | break; |
478 | case NONE: |
479 | default: |
480 | g_error("construct_fields : unknown type"); |
481 | } |
482 | |
483 | field->field_size = type->size; |
484 | |
485 | /* Put the fields as "variable" offset to root first. Then, |
486 | * the offset precomputation will only have to set the FIELD_FIXED until |
487 | * it reaches the first variable length field, then stop. |
488 | */ |
489 | field->fixed_root = FIELD_VARIABLE; |
490 | |
491 | if(td->fmt) { |
492 | len = strlen(td->fmt); |
493 | type->fmt = g_new(gchar, len+1); |
494 | strcpy(type->fmt, td->fmt); |
495 | } |
496 | } |
497 | |
498 | |
499 | |
500 | #if 0 |
501 | void construct_types_and_fields(LttFacility * fac, type_descriptor_t * td, |
502 | LttField * fld) |
503 | { |
504 | int i; |
505 | type_descriptor_t * tmpTd; |
506 | |
507 | switch(td->type) { |
508 | case INT: |
509 | case UINT: |
510 | case FLOAT: |
511 | fld->field_type->size = td->size; |
512 | break; |
513 | case POINTER: |
514 | case LONG: |
515 | case ULONG: |
516 | case SIZE_T: |
517 | case SSIZE_T: |
518 | case OFF_T: |
519 | fld->field_type->size = 0; |
520 | break; |
521 | case STRING: |
522 | fld->field_type->size = 0; |
523 | break; |
524 | case ENUM: |
525 | fld->field_type->element_number = td->labels.position; |
526 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
527 | for(i=0;i<td->labels.position;i++){ |
528 | fld->field_type->enum_strings[i] |
529 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
530 | } |
531 | fld->field_type->size = td->size; |
532 | break; |
533 | |
534 | case ARRAY: |
535 | fld->field_type->element_number = (unsigned)td->size; |
536 | case SEQUENCE: |
537 | fld->field_type->element_type = g_new(LttType*,1); |
538 | tmpTd = td->nested_type; |
539 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
540 | fld->child = g_new(LttField*, 1); |
541 | fld->child[0] = g_new(LttField, 1); |
542 | |
543 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
544 | fld->child[0]->offset_root = 0; |
545 | fld->child[0]->fixed_root = FIELD_UNKNOWN; |
546 | fld->child[0]->offset_parent = 0; |
547 | fld->child[0]->fixed_parent = FIELD_UNKNOWN; |
548 | fld->child[0]->field_size = 0; |
549 | fld->child[0]->fixed_size = FIELD_UNKNOWN; |
550 | fld->child[0]->parent = fld; |
551 | fld->child[0]->child = NULL; |
552 | fld->child[0]->current_element = 0; |
553 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
554 | break; |
555 | |
556 | case STRUCT: |
557 | case UNION: |
558 | fld->field_type->element_number = td->fields.position; |
559 | |
560 | g_assert(fld->field_type->element_type == NULL); |
561 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
562 | |
563 | fld->child = g_new(LttField*, td->fields.position); |
564 | for(i=0;i<td->fields.position;i++){ |
565 | tmpTd = ((field_t*)(td->fields.array[i]))->type; |
566 | |
567 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
568 | fld->child[i] = g_new(LttField,1); |
569 | |
570 | // fld->child[i]->field_pos = i; |
571 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
572 | |
573 | fld->child[i]->field_type->element_name |
574 | = g_quark_from_string(((field_t*)(td->fields.array[i]))->name); |
575 | |
576 | fld->child[i]->offset_root = 0; |
577 | fld->child[i]->fixed_root = FIELD_UNKNOWN; |
578 | fld->child[i]->offset_parent = 0; |
579 | fld->child[i]->fixed_parent = FIELD_UNKNOWN; |
580 | fld->child[i]->field_size = 0; |
581 | fld->child[i]->fixed_size = FIELD_UNKNOWN; |
582 | fld->child[i]->parent = fld; |
583 | fld->child[i]->child = NULL; |
584 | fld->child[i]->current_element = 0; |
585 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
586 | } |
587 | break; |
588 | |
589 | default: |
590 | g_error("construct_types_and_fields : unknown type"); |
591 | } |
592 | |
593 | |
594 | } |
595 | |
596 | #endif //0 |
597 | |
598 | #if 0 |
599 | void construct_types_and_fields(LttFacility * fac, type_descriptor * td, |
600 | LttField * fld) |
601 | { |
602 | int i, flag; |
603 | type_descriptor * tmpTd; |
604 | |
605 | // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE) |
606 | // fld->field_size = 0; |
607 | // else fld->field_size = -1; |
608 | |
609 | if(td->type == LTT_ENUM){ |
610 | fld->field_type->element_number = td->labels.position; |
611 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
612 | for(i=0;i<td->labels.position;i++){ |
613 | fld->field_type->enum_strings[i] |
614 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
615 | } |
616 | }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){ |
617 | if(td->type == LTT_ARRAY) |
618 | fld->field_type->element_number = (unsigned)td->size; |
619 | fld->field_type->element_type = g_new(LttType*,1); |
620 | tmpTd = td->nested_type; |
621 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
622 | fld->child = g_new(LttField*, 1); |
623 | fld->child[0] = g_new(LttField, 1); |
624 | |
625 | // fld->child[0]->field_pos = 0; |
626 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
627 | fld->child[0]->offset_root = fld->offset_root; |
628 | fld->child[0]->fixed_root = fld->fixed_root; |
629 | fld->child[0]->offset_parent = 0; |
630 | fld->child[0]->fixed_parent = 1; |
631 | // fld->child[0]->base_address = NULL; |
632 | fld->child[0]->field_size = 0; |
633 | fld->child[0]->field_fixed = -1; |
634 | fld->child[0]->parent = fld; |
635 | fld->child[0]->child = NULL; |
636 | fld->child[0]->current_element = 0; |
637 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
638 | }else if(td->type == LTT_STRUCT){ |
639 | fld->field_type->element_number = td->fields.position; |
640 | |
641 | if(fld->field_type->element_type == NULL){ |
642 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
643 | flag = 1; |
644 | }else{ |
645 | flag = 0; |
646 | } |
647 | |
648 | fld->child = g_new(LttField*, td->fields.position); |
649 | for(i=0;i<td->fields.position;i++){ |
650 | tmpTd = ((type_fields*)(td->fields.array[i]))->type; |
651 | |
652 | if(flag) |
653 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
654 | fld->child[i] = g_new(LttField,1); |
655 | |
656 | fld->child[i]->field_pos = i; |
657 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
658 | |
659 | if(flag){ |
660 | fld->child[i]->field_type->element_name |
661 | = g_quark_from_string(((type_fields*)(td->fields.array[i]))->name); |
662 | } |
663 | |
664 | fld->child[i]->offset_root = -1; |
665 | fld->child[i]->fixed_root = -1; |
666 | fld->child[i]->offset_parent = -1; |
667 | fld->child[i]->fixed_parent = -1; |
668 | // fld->child[i]->base_address = NULL; |
669 | fld->child[i]->field_size = 0; |
670 | fld->child[i]->field_fixed = -1; |
671 | fld->child[i]->parent = fld; |
672 | fld->child[i]->child = NULL; |
673 | fld->child[i]->current_element = 0; |
674 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
675 | } |
676 | } |
677 | } |
678 | #endif //0 |
679 | |
680 | #if 0 |
681 | /***************************************************************************** |
682 | *Function name |
683 | * lookup_named_type: search named type in the table |
684 | * internal function |
685 | *Input params |
686 | * fac : facility struct |
687 | * name : type name |
688 | *Return value |
689 | * : either find the named type, or create a new LttType |
690 | ****************************************************************************/ |
691 | |
692 | LttType * lookup_named_type(LttFacility *fac, GQuark type_name) |
693 | { |
694 | LttType *type = NULL; |
695 | |
696 | /* Named type */ |
697 | type = g_datalist_id_get_data(&fac->named_types, name); |
698 | |
699 | g_assert(type != NULL); |
700 | #if 0 |
701 | if(type == NULL){ |
702 | /* Create the type */ |
703 | type = g_new(LttType,1); |
704 | type->type_name = name; |
705 | type->type_class = td->type; |
706 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
707 | else type->fmt = NULL; |
708 | type->size = td->size; |
709 | type->enum_strings = NULL; |
710 | type->element_type = NULL; |
711 | type->element_number = 0; |
712 | |
713 | if(td->type_name != NULL) |
714 | g_datalist_id_set_data_full(&fac->named_types, name, |
715 | type, (GDestroyNotify)freeLttNamedType); |
716 | } |
717 | #endif //0 |
718 | return type; |
719 | } |
720 | #endif //0 |
721 | |
722 | /***************************************************************************** |
723 | *Function name |
724 | * ltt_facility_close : close a facility, decrease its usage count, |
725 | * if usage count = 0, release the memory |
726 | *Input params |
727 | * f : facility that will be closed |
728 | ****************************************************************************/ |
729 | |
730 | void ltt_facility_close(LttFacility *f) |
731 | { |
732 | //release the memory it occupied |
733 | freeFacility(f); |
734 | } |
735 | |
736 | /***************************************************************************** |
737 | * Functions to release the memory occupied by the facility |
738 | ****************************************************************************/ |
739 | |
740 | void freeFacility(LttFacility * fac) |
741 | { |
742 | guint i; |
743 | LttEventType *et; |
744 | |
745 | for(i=0; i<fac->events->len; i++) { |
746 | et = &g_array_index (fac->events, LttEventType, i); |
747 | freeEventtype(et); |
748 | } |
749 | g_array_free(fac->events, TRUE); |
750 | |
751 | g_datalist_clear(&fac->events_by_name); |
752 | |
753 | // g_datalist_clear(&fac->named_types); |
754 | } |
755 | |
756 | void freeEventtype(LttEventType * evType) |
757 | { |
758 | unsigned int i; |
759 | LttType * root_type; |
760 | if(evType->description) |
761 | g_free(evType->description); |
762 | |
763 | for(i=0; i<evType->fields->len;i++) { |
764 | LttField *field = &g_array_index(evType->fields, LttField, i); |
765 | freeLttField(field); |
766 | } |
767 | g_array_free(evType->fields, TRUE); |
768 | g_datalist_clear(&evType->fields_by_name); |
769 | } |
770 | |
771 | void freeLttType(LttType * type) |
772 | { |
773 | unsigned int i; |
774 | |
775 | if(type->fmt) |
776 | g_free(type->fmt); |
777 | |
778 | if(type->enum_map) |
779 | g_hash_table_destroy(type->enum_map); |
780 | |
781 | if(type->fields) { |
782 | for(i=0; i<type->fields->len; i++) { |
783 | freeLttField(&g_array_index(type->fields, LttField, i)); |
784 | } |
785 | g_array_free(type->fields, TRUE); |
786 | } |
787 | if(type->fields_by_name) |
788 | g_datalist_clear(&type->fields_by_name); |
789 | } |
790 | |
791 | void freeLttNamedType(LttType * type) |
792 | { |
793 | freeLttType(type); |
794 | } |
795 | |
796 | void freeLttField(LttField * field) |
797 | { |
798 | if(field->description) |
799 | g_free(field->description); |
800 | if(field->dynamic_offsets) |
801 | g_array_free(field->dynamic_offsets, TRUE); |
802 | freeLttType(&field->field_type); |
803 | } |
804 | |
805 | /***************************************************************************** |
806 | *Function name |
807 | * ltt_facility_name : obtain the facility's name |
808 | *Input params |
809 | * f : the facility |
810 | *Return value |
811 | * GQuark : the facility's name |
812 | ****************************************************************************/ |
813 | |
814 | GQuark ltt_facility_name(LttFacility *f) |
815 | { |
816 | return f->name; |
817 | } |
818 | |
819 | /***************************************************************************** |
820 | *Function name |
821 | * ltt_facility_checksum : obtain the facility's checksum |
822 | *Input params |
823 | * f : the facility |
824 | *Return value |
825 | * : the checksum of the facility |
826 | ****************************************************************************/ |
827 | |
828 | guint32 ltt_facility_checksum(LttFacility *f) |
829 | { |
830 | return f->checksum; |
831 | } |
832 | |
833 | /***************************************************************************** |
834 | *Function name |
835 | * ltt_facility_base_id : obtain the facility base id |
836 | *Input params |
837 | * f : the facility |
838 | *Return value |
839 | * : the base id of the facility |
840 | ****************************************************************************/ |
841 | |
842 | guint ltt_facility_id(LttFacility *f) |
843 | { |
844 | return f->id; |
845 | } |
846 | |
847 | /***************************************************************************** |
848 | *Function name |
849 | * ltt_facility_eventtype_number: obtain the number of the event types |
850 | *Input params |
851 | * f : the facility that will be closed |
852 | *Return value |
853 | * : the number of the event types |
854 | ****************************************************************************/ |
855 | |
856 | guint8 ltt_facility_eventtype_number(LttFacility *f) |
857 | { |
858 | return (f->events->len); |
859 | } |
860 | |
861 | /***************************************************************************** |
862 | *Function name |
863 | * ltt_facility_eventtype_get: obtain the event type according to event id |
864 | * from 0 to event_number - 1 |
865 | *Input params |
866 | * f : the facility that will be closed |
867 | *Return value |
868 | * LttEventType * : the event type required |
869 | ****************************************************************************/ |
870 | |
871 | LttEventType *ltt_facility_eventtype_get(LttFacility *f, guint8 i) |
872 | { |
873 | if(!f->exists) return NULL; |
874 | |
875 | g_assert(i < f->events->len); |
876 | return &g_array_index(f->events, LttEventType, i); |
877 | } |
878 | |
879 | /***************************************************************************** |
880 | *Function name |
881 | * ltt_facility_eventtype_get_by_name |
882 | * : obtain the event type according to event name |
883 | * event name is unique in the facility |
884 | *Input params |
885 | * f : the facility |
886 | * name : the name of the event |
887 | *Return value |
888 | * LttEventType * : the event type required |
889 | ****************************************************************************/ |
890 | |
891 | LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, GQuark name) |
892 | { |
893 | LttEventType *et = g_datalist_id_get_data(&f->events_by_name, name); |
894 | return et; |
895 | } |
896 | |