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