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