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