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