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