| 1 | /****************************************************************************** |
| 2 | * Genevent |
| 3 | * |
| 4 | * Event generator. XML to logging C code converter. |
| 5 | * |
| 6 | * Program parameters : |
| 7 | * ./genevent name.xml |
| 8 | * |
| 9 | * Will generate ltt-facility-name.h, ltt-facility-id-name.h |
| 10 | * ltt-facility-loader-name.c, ltt-facility-loader-name.h |
| 11 | * in the current directory. |
| 12 | * |
| 13 | * Supports : |
| 14 | * - C Alignment |
| 15 | * - C types : struct, union, enum, basic types. |
| 16 | * - Architectures : LP32, ILP32, ILP64, LLP64, LP64. |
| 17 | * |
| 18 | * Additionnal structures supported : |
| 19 | * - embedded variable size strings |
| 20 | * - embedded variable size arrays |
| 21 | * - embedded variable size sequences |
| 22 | * |
| 23 | * Notes : |
| 24 | * (1) |
| 25 | * enums are limited to integer type, as this is what is used in C. Note, |
| 26 | * however, that ISO/IEC 9899:TC2 specify that the type of enum can be char, |
| 27 | * unsigned int or int. This is implementation defined (compiler). That's why we |
| 28 | * add a check for sizeof enum. |
| 29 | * |
| 30 | * (2) |
| 31 | * Because of archtecture defined type sizes, we need to ask for ltt_align |
| 32 | * (which gives the alignment) by passing basic types, not their actual sizes. |
| 33 | * It's up to ltt_align to determine sizes of types. |
| 34 | * |
| 35 | * Note that, from |
| 36 | * http://www.usenix.org/publications/login/standards/10.data.html |
| 37 | * (Andrew Josey <a.josey@opengroup.org>) : |
| 38 | * |
| 39 | * Data Type LP32 ILP32 ILP64 LLP64 LP64 |
| 40 | * char 8 8 8 8 8 |
| 41 | * short 16 16 16 16 16 |
| 42 | * int32 32 |
| 43 | * int 16 32 64 32 32 |
| 44 | * long 32 32 64 32 64 |
| 45 | * long long (int64) 64 |
| 46 | * pointer 32 32 64 64 64 |
| 47 | * |
| 48 | * With these constraints : |
| 49 | * sizeof(char) <= sizeof(short) <= sizeof(int) |
| 50 | * <= sizeof(long) = sizeof(size_t) |
| 51 | * |
| 52 | * and therefore sizeof(long) <= sizeof(pointer) <= sizeof(size_t) |
| 53 | * |
| 54 | * Which means we only have to remember which is the biggest type in a structure |
| 55 | * to know the structure's alignment. |
| 56 | */ |
| 57 | |
| 58 | #define _GNU_SOURCE |
| 59 | #include <limits.h> |
| 60 | #include <stdlib.h> |
| 61 | #include <errno.h> |
| 62 | #include <sys/types.h> |
| 63 | #include <sys/stat.h> |
| 64 | #include <fcntl.h> |
| 65 | #include <stdio.h> |
| 66 | #include <string.h> |
| 67 | #include <unistd.h> |
| 68 | #include <assert.h> |
| 69 | |
| 70 | #include "genevent.h" |
| 71 | #include "parser.h" |
| 72 | |
| 73 | |
| 74 | #define TRUE 1 |
| 75 | #define FALSE (!TRUE) |
| 76 | |
| 77 | /* Debugging printf */ |
| 78 | #ifdef DEBUG |
| 79 | #define dprintf(...) \ |
| 80 | do {\ |
| 81 | printf(__FILE__ ",%u,%s: ",\ |
| 82 | __LINE__, __func__);\ |
| 83 | printf(__VA_ARGS__);\ |
| 84 | } while(0) |
| 85 | #else |
| 86 | #define dprintf(...) |
| 87 | #endif |
| 88 | |
| 89 | /* Code printing */ |
| 90 | |
| 91 | void print_tabs(unsigned int tabs, FILE *fd) |
| 92 | { |
| 93 | for(unsigned int i = 0; i<tabs;i++) |
| 94 | fprintf(fd, "\t"); |
| 95 | } |
| 96 | |
| 97 | /* print type. |
| 98 | * |
| 99 | * Copied from construct_types_and_fields in LTTV facility.c */ |
| 100 | |
| 101 | int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 102 | char *nest_name, char *field_name) |
| 103 | { |
| 104 | char basename[PATH_MAX]; |
| 105 | unsigned int basename_len = 0; |
| 106 | |
| 107 | strcpy(basename, nest_name); |
| 108 | basename_len = strlen(basename); |
| 109 | |
| 110 | /* For a named type, we use the type_name directly */ |
| 111 | if(td->type_name != NULL) { |
| 112 | strncpy(basename, td->type_name, PATH_MAX); |
| 113 | basename_len = strlen(basename); |
| 114 | } else { |
| 115 | /* For a unnamed type, there must be a field name */ |
| 116 | if((basename_len != 0) |
| 117 | && (basename[basename_len-1] != '_') |
| 118 | && (field_name[0] != '\0')) { |
| 119 | strncat(basename, "_", PATH_MAX - basename_len); |
| 120 | basename_len = strlen(basename); |
| 121 | } |
| 122 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 123 | } |
| 124 | |
| 125 | switch(td->type) { |
| 126 | case INT_FIXED: |
| 127 | fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]); |
| 128 | break; |
| 129 | case UINT_FIXED: |
| 130 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
| 131 | break; |
| 132 | case CHAR: |
| 133 | fprintf(fd, "signed char"); |
| 134 | break; |
| 135 | case UCHAR: |
| 136 | fprintf(fd, "unsigned char"); |
| 137 | break; |
| 138 | case SHORT: |
| 139 | fprintf(fd, "short"); |
| 140 | break; |
| 141 | case USHORT: |
| 142 | fprintf(fd, "unsigned short"); |
| 143 | break; |
| 144 | case INT: |
| 145 | fprintf(fd, "int"); |
| 146 | break; |
| 147 | case UINT: |
| 148 | fprintf(fd, "unsigned int"); |
| 149 | break; |
| 150 | case FLOAT: |
| 151 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
| 152 | break; |
| 153 | case POINTER: |
| 154 | fprintf(fd, "void *"); |
| 155 | break; |
| 156 | case LONG: |
| 157 | fprintf(fd, "long"); |
| 158 | break; |
| 159 | case ULONG: |
| 160 | fprintf(fd, "unsigned long"); |
| 161 | break; |
| 162 | case SIZE_T: |
| 163 | fprintf(fd, "size_t"); |
| 164 | break; |
| 165 | case SSIZE_T: |
| 166 | fprintf(fd, "ssize_t"); |
| 167 | break; |
| 168 | case OFF_T: |
| 169 | fprintf(fd, "off_t"); |
| 170 | break; |
| 171 | case STRING: |
| 172 | fprintf(fd, "char *"); |
| 173 | break; |
| 174 | case ENUM: |
| 175 | fprintf(fd, "enum lttng_%s", basename); |
| 176 | break; |
| 177 | case ARRAY: |
| 178 | fprintf(fd, "lttng_array_%s", basename); |
| 179 | break; |
| 180 | case SEQUENCE: |
| 181 | fprintf(fd, "lttng_sequence_%s", basename); |
| 182 | break; |
| 183 | case STRUCT: |
| 184 | fprintf(fd, "struct lttng_%s", basename); |
| 185 | break; |
| 186 | case UNION: |
| 187 | fprintf(fd, "union lttng_%s", basename); |
| 188 | break; |
| 189 | default: |
| 190 | printf("print_type : unknown type\n"); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* Print logging function argument */ |
| 198 | int print_arg(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 199 | char *nest_name, char *field_name) |
| 200 | { |
| 201 | char basename[PATH_MAX]; |
| 202 | unsigned int basename_len = 0; |
| 203 | |
| 204 | strcpy(basename, nest_name); |
| 205 | basename_len = strlen(basename); |
| 206 | |
| 207 | /* For a named type, we use the type_name directly */ |
| 208 | if(td->type_name != NULL) { |
| 209 | strncpy(basename, td->type_name, PATH_MAX); |
| 210 | basename_len = strlen(basename); |
| 211 | } else { |
| 212 | /* For a unnamed type, there must be a field name */ |
| 213 | if((basename_len != 0) |
| 214 | && (basename[basename_len-1] != '_') |
| 215 | && (field_name[0] != '\0')) { |
| 216 | strncat(basename, "_", PATH_MAX - basename_len); |
| 217 | basename_len = strlen(basename); |
| 218 | } |
| 219 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 220 | } |
| 221 | |
| 222 | print_tabs(tabs, fd); |
| 223 | |
| 224 | switch(td->type) { |
| 225 | case INT_FIXED: |
| 226 | fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]); |
| 227 | fprintf(fd, " %s", field_name); |
| 228 | break; |
| 229 | case UINT_FIXED: |
| 230 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
| 231 | fprintf(fd, " %s", field_name); |
| 232 | break; |
| 233 | case CHAR: |
| 234 | fprintf(fd, "signed char"); |
| 235 | fprintf(fd, " %s", field_name); |
| 236 | break; |
| 237 | case UCHAR: |
| 238 | fprintf(fd, "unsigned char"); |
| 239 | fprintf(fd, " %s", field_name); |
| 240 | break; |
| 241 | case SHORT: |
| 242 | fprintf(fd, "short"); |
| 243 | fprintf(fd, " %s", field_name); |
| 244 | break; |
| 245 | case USHORT: |
| 246 | fprintf(fd, "unsigned short"); |
| 247 | fprintf(fd, " %s", field_name); |
| 248 | break; |
| 249 | case INT: |
| 250 | fprintf(fd, "int"); |
| 251 | fprintf(fd, " %s", field_name); |
| 252 | break; |
| 253 | case UINT: |
| 254 | fprintf(fd, "unsigned int"); |
| 255 | fprintf(fd, " %s", field_name); |
| 256 | break; |
| 257 | case FLOAT: |
| 258 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
| 259 | fprintf(fd, " %s", field_name); |
| 260 | break; |
| 261 | case POINTER: |
| 262 | fprintf(fd, "void *"); |
| 263 | fprintf(fd, " %s", field_name); |
| 264 | break; |
| 265 | case LONG: |
| 266 | fprintf(fd, "long"); |
| 267 | fprintf(fd, " %s", field_name); |
| 268 | break; |
| 269 | case ULONG: |
| 270 | fprintf(fd, "unsigned long"); |
| 271 | fprintf(fd, " %s", field_name); |
| 272 | break; |
| 273 | case SIZE_T: |
| 274 | fprintf(fd, "size_t"); |
| 275 | fprintf(fd, " %s", field_name); |
| 276 | break; |
| 277 | case SSIZE_T: |
| 278 | fprintf(fd, "ssize_t"); |
| 279 | fprintf(fd, " %s", field_name); |
| 280 | break; |
| 281 | case OFF_T: |
| 282 | fprintf(fd, "off_t"); |
| 283 | fprintf(fd, " %s", field_name); |
| 284 | break; |
| 285 | case STRING: |
| 286 | fprintf(fd, "char *"); |
| 287 | fprintf(fd, " %s", field_name); |
| 288 | break; |
| 289 | case ENUM: |
| 290 | fprintf(fd, "enum lttng_%s", basename); |
| 291 | fprintf(fd, " %s", field_name); |
| 292 | break; |
| 293 | case ARRAY: |
| 294 | fprintf(fd, "lttng_array_%s", basename); |
| 295 | fprintf(fd, " %s", field_name); |
| 296 | break; |
| 297 | case SEQUENCE: |
| 298 | fprintf(fd, "lttng_sequence_%s *", basename); |
| 299 | fprintf(fd, " %s", field_name); |
| 300 | break; |
| 301 | case STRUCT: |
| 302 | fprintf(fd, "struct lttng_%s *", basename); |
| 303 | fprintf(fd, " %s", field_name); |
| 304 | break; |
| 305 | case UNION: |
| 306 | fprintf(fd, "union lttng_%s *", basename); |
| 307 | fprintf(fd, " %s", field_name); |
| 308 | break; |
| 309 | default: |
| 310 | printf("print_type : unknown type\n"); |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* Does the type has a fixed size ? (as know from the compiler) |
| 319 | * |
| 320 | * 1 : fixed size |
| 321 | * 0 : variable length |
| 322 | */ |
| 323 | int has_type_fixed_size(type_descriptor_t *td) |
| 324 | { |
| 325 | switch(td->type) { |
| 326 | case INT_FIXED: |
| 327 | case UINT_FIXED: |
| 328 | case CHAR: |
| 329 | case UCHAR: |
| 330 | case SHORT: |
| 331 | case USHORT: |
| 332 | case INT: |
| 333 | case UINT: |
| 334 | case FLOAT: |
| 335 | case POINTER: |
| 336 | case LONG: |
| 337 | case ULONG: |
| 338 | case SIZE_T: |
| 339 | case SSIZE_T: |
| 340 | case OFF_T: |
| 341 | case ENUM: |
| 342 | case UNION: /* The union must have fixed size children. Must be checked by |
| 343 | the parser */ |
| 344 | return 1; |
| 345 | break; |
| 346 | case STRING: |
| 347 | case SEQUENCE: |
| 348 | return 0; |
| 349 | break; |
| 350 | case STRUCT: |
| 351 | { |
| 352 | int has_type_fixed = 0; |
| 353 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 354 | field_t *field = (field_t*)(td->fields.array[i]); |
| 355 | type_descriptor_t *type = field->type; |
| 356 | |
| 357 | has_type_fixed = has_type_fixed_size(type); |
| 358 | if(!has_type_fixed) return 0; |
| 359 | } |
| 360 | return 1; |
| 361 | } |
| 362 | break; |
| 363 | case ARRAY: |
| 364 | assert(td->size >= 0); |
| 365 | return has_type_fixed_size(((field_t*)td->fields.array[0])->type); |
| 366 | break; |
| 367 | case NONE: |
| 368 | printf("There is a type defined to NONE : bad.\n"); |
| 369 | assert(0); |
| 370 | break; |
| 371 | } |
| 372 | return 0; //make gcc happy. |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | /* print type declaration. |
| 380 | * |
| 381 | * Copied from construct_types_and_fields in LTTV facility.c */ |
| 382 | |
| 383 | int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 384 | char *nest_name, char *field_name) |
| 385 | { |
| 386 | char basename[PATH_MAX]; |
| 387 | unsigned int basename_len = 0; |
| 388 | |
| 389 | strncpy(basename, nest_name, PATH_MAX); |
| 390 | basename_len = strlen(basename); |
| 391 | |
| 392 | /* For a named type, we use the type_name directly */ |
| 393 | if(td->type_name != NULL) { |
| 394 | strncpy(basename, td->type_name, PATH_MAX); |
| 395 | basename_len = strlen(basename); |
| 396 | } else { |
| 397 | /* For a unnamed type, there must be a field name, except for |
| 398 | * the array. */ |
| 399 | if((basename_len != 0) |
| 400 | && (basename[basename_len-1] != '_' |
| 401 | && (field_name[0] != '\0'))) { |
| 402 | strncat(basename, "_", PATH_MAX - basename_len); |
| 403 | basename_len = strlen(basename); |
| 404 | } |
| 405 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 406 | } |
| 407 | |
| 408 | switch(td->type) { |
| 409 | case ENUM: |
| 410 | fprintf(fd, "enum lttng_%s", basename); |
| 411 | fprintf(fd, " {\n"); |
| 412 | for(unsigned int i=0;i<td->labels.position;i++){ |
| 413 | print_tabs(1, fd); |
| 414 | fprintf(fd, "LTTNG_%s", ((char*)(td->labels.array[i]))); |
| 415 | fprintf(fd, ",\n"); |
| 416 | } |
| 417 | fprintf(fd, "};\n"); |
| 418 | fprintf(fd, "\n"); |
| 419 | break; |
| 420 | |
| 421 | case ARRAY: |
| 422 | dprintf("%s\n", basename); |
| 423 | assert(td->size >= 0); |
| 424 | if(((field_t*)td->fields.array[0])->type->type_name == NULL) { |
| 425 | /* Not a named nested type : we must print its declaration first */ |
| 426 | if(print_type_declaration(((field_t*)td->fields.array[0])->type, |
| 427 | fd, 0, basename, "")) return 1; |
| 428 | } |
| 429 | fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename, |
| 430 | td->size); |
| 431 | fprintf(fd, "typedef "); |
| 432 | if(print_type(((field_t*)td->fields.array[0])->type, |
| 433 | fd, tabs, basename, "")) return 1; |
| 434 | fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename, |
| 435 | basename); |
| 436 | fprintf(fd, "\n"); |
| 437 | break; |
| 438 | case SEQUENCE: |
| 439 | /* We assume that the sequence length type does not need to be declared. |
| 440 | */ |
| 441 | if(((field_t*)td->fields.array[1])->type->type_name == NULL) { |
| 442 | /* Not a named nested type : we must print its declaration first */ |
| 443 | if(print_type_declaration(((field_t*)td->fields.array[1])->type, |
| 444 | fd, 0, basename, "")) return 1; |
| 445 | } |
| 446 | fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n", |
| 447 | basename, |
| 448 | basename); |
| 449 | fprintf(fd, "struct lttng_sequence_%s", basename); |
| 450 | fprintf(fd, " {\n"); |
| 451 | print_tabs(1, fd); |
| 452 | if(print_type(((field_t*)td->fields.array[0])->type, |
| 453 | fd, tabs, basename, "")) return 1; |
| 454 | fprintf(fd, " len;\n"); |
| 455 | print_tabs(1, fd); |
| 456 | if(print_type(((field_t*)td->fields.array[1])->type, |
| 457 | fd, tabs, basename, "")) return 1; |
| 458 | fprintf(fd, " *array;\n"); |
| 459 | fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy |
| 460 | it to the buffer directly. */ |
| 461 | fprintf(fd, "\n"); |
| 462 | break; |
| 463 | |
| 464 | case STRUCT: |
| 465 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 466 | field_t *field = (field_t*)(td->fields.array[i]); |
| 467 | type_descriptor_t *type = field->type; |
| 468 | if(type->type_name == NULL) { |
| 469 | /* Not a named nested type : we must print its declaration first */ |
| 470 | if(print_type_declaration(type, |
| 471 | fd, 0, basename, field->name)) return 1; |
| 472 | } |
| 473 | } |
| 474 | fprintf(fd, "struct lttng_%s", basename); |
| 475 | fprintf(fd, " {\n"); |
| 476 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 477 | field_t *field = (field_t*)(td->fields.array[i]); |
| 478 | type_descriptor_t *type = field->type; |
| 479 | print_tabs(1, fd); |
| 480 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
| 481 | fprintf(fd, " "); |
| 482 | fprintf(fd, "%s", field->name); |
| 483 | fprintf(fd, ";\n"); |
| 484 | } |
| 485 | fprintf(fd, "} LTT_ALIGN;\n"); |
| 486 | fprintf(fd, "\n"); |
| 487 | break; |
| 488 | case UNION: |
| 489 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 490 | field_t *field = (field_t*)(td->fields.array[i]); |
| 491 | type_descriptor_t *type = field->type; |
| 492 | if(type->type_name == NULL) { |
| 493 | /* Not a named nested type : we must print its declaration first */ |
| 494 | if(print_type_declaration(type, |
| 495 | fd, 0, basename, field->name)) return 1; |
| 496 | } |
| 497 | } |
| 498 | fprintf(fd, "union lttng_%s", basename); |
| 499 | fprintf(fd, " {\n"); |
| 500 | for(unsigned i=0;i<td->fields.position;i++){ |
| 501 | field_t *field = (field_t*)(td->fields.array[i]); |
| 502 | type_descriptor_t *type = field->type; |
| 503 | print_tabs(1, fd); |
| 504 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
| 505 | fprintf(fd, " "); |
| 506 | fprintf(fd, "%s", field->name); |
| 507 | fprintf(fd, ";\n"); |
| 508 | } |
| 509 | fprintf(fd, "} LTT_ALIGN;\n"); |
| 510 | fprintf(fd, "\n"); |
| 511 | break; |
| 512 | default: |
| 513 | dprintf("print_type_declaration : unknown type or nothing to declare.\n"); |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | /* print type alignment. |
| 522 | * |
| 523 | * Copied from construct_types_and_fields in LTTV facility.c |
| 524 | * |
| 525 | * basename is the name which identifies the type (along with a prefix |
| 526 | * (possibly)). */ |
| 527 | |
| 528 | int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 529 | char *nest_name, char *field_name) |
| 530 | { |
| 531 | char basename[PATH_MAX]; |
| 532 | unsigned int basename_len = 0; |
| 533 | |
| 534 | strncpy(basename, nest_name, PATH_MAX); |
| 535 | basename_len = strlen(basename); |
| 536 | |
| 537 | /* For a named type, we use the type_name directly */ |
| 538 | if(td->type_name != NULL) { |
| 539 | strncpy(basename, td->type_name, PATH_MAX); |
| 540 | basename_len = strlen(basename); |
| 541 | } else { |
| 542 | /* For a unnamed type, there must be a field name, except for |
| 543 | * the array. */ |
| 544 | if((basename_len != 0) |
| 545 | && (basename[basename_len-1] != '_' |
| 546 | && field_name != NULL |
| 547 | && (field_name[0] != '\0'))) { |
| 548 | strncat(basename, "_", PATH_MAX - basename_len); |
| 549 | basename_len = strlen(basename); |
| 550 | } |
| 551 | if(field_name != NULL) |
| 552 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 553 | } |
| 554 | |
| 555 | switch(td->type) { |
| 556 | case INT_FIXED: |
| 557 | case UINT_FIXED: |
| 558 | case CHAR: |
| 559 | case UCHAR: |
| 560 | case SHORT: |
| 561 | case USHORT: |
| 562 | case INT: |
| 563 | case UINT: |
| 564 | case FLOAT: |
| 565 | case POINTER: |
| 566 | case LONG: |
| 567 | case ULONG: |
| 568 | case SIZE_T: |
| 569 | case SSIZE_T: |
| 570 | case OFF_T: |
| 571 | case ENUM: |
| 572 | fprintf(fd, "sizeof("); |
| 573 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 574 | fprintf(fd, ")"); |
| 575 | break; |
| 576 | case STRING: |
| 577 | fprintf(fd, "sizeof(char)"); |
| 578 | break; |
| 579 | case SEQUENCE: |
| 580 | fprintf(fd, "lttng_get_alignment_sequence_%s(&obj->%s)", basename, |
| 581 | field_name); |
| 582 | break; |
| 583 | case STRUCT: |
| 584 | fprintf(fd, "lttng_get_alignment_struct_%s(&obj->%s)", basename, |
| 585 | field_name); |
| 586 | break; |
| 587 | case UNION: |
| 588 | fprintf(fd, "lttng_get_alignment_union_%s(&obj->%s)", basename, |
| 589 | field_name); |
| 590 | break; |
| 591 | case ARRAY: |
| 592 | fprintf(fd, "lttng_get_alignment_array_%s(obj->%s)", basename, |
| 593 | field_name); |
| 594 | break; |
| 595 | case NONE: |
| 596 | printf("error : type NONE unexpected\n"); |
| 597 | return 1; |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | /* print type write. |
| 605 | * |
| 606 | * Copied from construct_types_and_fields in LTTV facility.c |
| 607 | * |
| 608 | * basename is the name which identifies the type (along with a prefix |
| 609 | * (possibly)). */ |
| 610 | |
| 611 | int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 612 | char *nest_name, char *field_name) |
| 613 | { |
| 614 | char basename[PATH_MAX]; |
| 615 | unsigned int basename_len = 0; |
| 616 | |
| 617 | strncpy(basename, nest_name, PATH_MAX); |
| 618 | basename_len = strlen(basename); |
| 619 | |
| 620 | /* For a named type, we use the type_name directly */ |
| 621 | if(td->type_name != NULL) { |
| 622 | strncpy(basename, td->type_name, PATH_MAX); |
| 623 | basename_len = strlen(basename); |
| 624 | } else { |
| 625 | /* For a unnamed type, there must be a field name, except for |
| 626 | * the array. */ |
| 627 | if((basename_len != 0) |
| 628 | && (basename[basename_len-1] != '_' |
| 629 | && (field_name[0] != '\0'))) { |
| 630 | strncat(basename, "_", PATH_MAX - basename_len); |
| 631 | basename_len = strlen(basename); |
| 632 | } |
| 633 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 634 | } |
| 635 | |
| 636 | switch(td->type) { |
| 637 | case INT_FIXED: |
| 638 | case UINT_FIXED: |
| 639 | case CHAR: |
| 640 | case UCHAR: |
| 641 | case SHORT: |
| 642 | case USHORT: |
| 643 | case INT: |
| 644 | case UINT: |
| 645 | case FLOAT: |
| 646 | case POINTER: |
| 647 | case LONG: |
| 648 | case ULONG: |
| 649 | case SIZE_T: |
| 650 | case SSIZE_T: |
| 651 | case OFF_T: |
| 652 | case ENUM: |
| 653 | print_tabs(tabs, fd); |
| 654 | fprintf(fd, "size = "); |
| 655 | fprintf(fd, "sizeof("); |
| 656 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 657 | fprintf(fd, ");\n"); |
| 658 | print_tabs(tabs, fd); |
| 659 | fprintf(fd, "size += ltt_align(*to+*len, size) + size;\n"); |
| 660 | print_tabs(tabs, fd); |
| 661 | fprintf(fd, "*len += size;"); |
| 662 | break; |
| 663 | case STRING: |
| 664 | print_tabs(tabs, fd); |
| 665 | fprintf(fd, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n", basename, field_name); |
| 666 | break; |
| 667 | case SEQUENCE: |
| 668 | print_tabs(tabs, fd); |
| 669 | fprintf(fd, "lttng_write_%s(buffer, to_base, to, from, len, &obj->%s)", basename, |
| 670 | field_name); |
| 671 | break; |
| 672 | case STRUCT: |
| 673 | print_tabs(tabs, fd); |
| 674 | fprintf(fd, "lttng_write_struct_%s(buffer, to_base, to, from, len, &obj->%s)", basename, |
| 675 | field_name); |
| 676 | break; |
| 677 | case UNION: |
| 678 | print_tabs(tabs, fd); |
| 679 | fprintf(fd, "lttng_write_union_%s(buffer, to_base, to, from, len, &obj->%s)", basename, |
| 680 | field_name); |
| 681 | break; |
| 682 | case ARRAY: |
| 683 | print_tabs(tabs, fd); |
| 684 | fprintf(fd, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename, |
| 685 | field_name); |
| 686 | break; |
| 687 | case NONE: |
| 688 | printf("Error : type NONE unexpected\n"); |
| 689 | return 1; |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | |
| 697 | |
| 698 | /* print type alignment function. |
| 699 | * |
| 700 | * Copied from construct_types_and_fields in LTTV facility.c |
| 701 | * |
| 702 | * basename is the name which identifies the type (along with a prefix |
| 703 | * (possibly)). */ |
| 704 | |
| 705 | int print_type_alignment_fct(type_descriptor_t * td, FILE *fd, |
| 706 | unsigned int tabs, |
| 707 | char *nest_name, char *field_name) |
| 708 | { |
| 709 | char basename[PATH_MAX]; |
| 710 | unsigned int basename_len = 0; |
| 711 | |
| 712 | strncpy(basename, nest_name, PATH_MAX); |
| 713 | basename_len = strlen(basename); |
| 714 | |
| 715 | /* For a named type, we use the type_name directly */ |
| 716 | if(td->type_name != NULL) { |
| 717 | strncpy(basename, td->type_name, PATH_MAX); |
| 718 | basename_len = strlen(basename); |
| 719 | } else { |
| 720 | /* For a unnamed type, there must be a field name, except for |
| 721 | * the array. */ |
| 722 | if((basename_len != 0) |
| 723 | && (basename[basename_len-1] != '_' |
| 724 | && (field_name[0] != '\0'))) { |
| 725 | strncat(basename, "_", PATH_MAX - basename_len); |
| 726 | basename_len = strlen(basename); |
| 727 | } |
| 728 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 729 | } |
| 730 | |
| 731 | switch(td->type) { |
| 732 | case SEQUENCE: |
| 733 | /* Function header */ |
| 734 | fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n", |
| 735 | basename); |
| 736 | print_tabs(2, fd); |
| 737 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 738 | fprintf(fd, " *obj)\n"); |
| 739 | fprintf(fd, "{\n"); |
| 740 | print_tabs(1, fd); |
| 741 | fprintf(fd, "size_t align=0, localign;"); |
| 742 | fprintf(fd, "\n"); |
| 743 | print_tabs(1, fd); |
| 744 | fprintf(fd, "localign = "); |
| 745 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
| 746 | fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1; |
| 747 | fprintf(fd, ";\n"); |
| 748 | print_tabs(1, fd); |
| 749 | fprintf(fd, "align = max(align, localign);\n"); |
| 750 | fprintf(fd, "\n"); |
| 751 | print_tabs(1, fd); |
| 752 | fprintf(fd, "localign = "); |
| 753 | if(print_type_alignment(((field_t*)td->fields.array[1])->type, |
| 754 | fd, 0, basename, ((field_t*)td->fields.array[1])->name)) return 1; |
| 755 | fprintf(fd, ";\n"); |
| 756 | print_tabs(1, fd); |
| 757 | fprintf(fd, "align = max(align, localign);\n"); |
| 758 | fprintf(fd, "\n"); |
| 759 | print_tabs(1, fd); |
| 760 | fprintf(fd, "return align;\n"); |
| 761 | break; |
| 762 | case STRUCT: |
| 763 | /* Function header */ |
| 764 | fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n", |
| 765 | basename); |
| 766 | print_tabs(2, fd); |
| 767 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 768 | fprintf(fd, " *obj)\n"); |
| 769 | fprintf(fd, "{\n"); |
| 770 | print_tabs(1, fd); |
| 771 | fprintf(fd, "size_t align=0, localign;"); |
| 772 | fprintf(fd, "\n"); |
| 773 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 774 | field_t *field = (field_t*)(td->fields.array[i]); |
| 775 | type_descriptor_t *type = field->type; |
| 776 | print_tabs(1, fd); |
| 777 | fprintf(fd, "localign = "); |
| 778 | if(print_type_alignment(type, fd, 0, basename, field->name)) return 1; |
| 779 | fprintf(fd, ";\n"); |
| 780 | print_tabs(1, fd); |
| 781 | fprintf(fd, "align = max(align, localign);\n"); |
| 782 | fprintf(fd, "\n"); |
| 783 | } |
| 784 | print_tabs(1, fd); |
| 785 | fprintf(fd, "return align;\n"); |
| 786 | |
| 787 | break; |
| 788 | case UNION: |
| 789 | /* Function header */ |
| 790 | fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n", |
| 791 | basename); |
| 792 | print_tabs(2, fd); |
| 793 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 794 | fprintf(fd, " *obj)\n"); |
| 795 | fprintf(fd, "{\n"); |
| 796 | print_tabs(1, fd); |
| 797 | fprintf(fd, "size_t align=0, localign;"); |
| 798 | fprintf(fd, "\n"); |
| 799 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 800 | field_t *field = (field_t*)(td->fields.array[i]); |
| 801 | type_descriptor_t *type = field->type; |
| 802 | print_tabs(1, fd); |
| 803 | fprintf(fd, "localign = "); |
| 804 | if(print_type_alignment(type, fd, 0, basename, field->name)) return 1; |
| 805 | fprintf(fd, ";\n"); |
| 806 | print_tabs(1, fd); |
| 807 | fprintf(fd, "align = max(align, localign);\n"); |
| 808 | fprintf(fd, "\n"); |
| 809 | } |
| 810 | print_tabs(1, fd); |
| 811 | fprintf(fd, "return align;\n"); |
| 812 | |
| 813 | break; |
| 814 | case ARRAY: |
| 815 | /* Function header */ |
| 816 | fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n", |
| 817 | basename); |
| 818 | print_tabs(2, fd); |
| 819 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 820 | fprintf(fd, " obj)\n"); |
| 821 | fprintf(fd, "{\n"); |
| 822 | print_tabs(1, fd); |
| 823 | fprintf(fd, "return \n"); |
| 824 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
| 825 | fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1; |
| 826 | fprintf(fd, ";\n"); |
| 827 | break; |
| 828 | default: |
| 829 | dprintf("print_type_alignment_fct : type has no alignment function.\n"); |
| 830 | return 0; |
| 831 | break; |
| 832 | } |
| 833 | |
| 834 | |
| 835 | /* Function footer */ |
| 836 | fprintf(fd, "}\n"); |
| 837 | fprintf(fd, "\n"); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | /* print type write function. |
| 843 | * |
| 844 | * Copied from construct_types_and_fields in LTTV facility.c |
| 845 | * |
| 846 | * basename is the name which identifies the type (along with a prefix |
| 847 | * (possibly)). */ |
| 848 | |
| 849 | int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 850 | char *nest_name, char *field_name) |
| 851 | { |
| 852 | char basename[PATH_MAX]; |
| 853 | unsigned int basename_len = 0; |
| 854 | |
| 855 | strncpy(basename, nest_name, PATH_MAX); |
| 856 | basename_len = strlen(basename); |
| 857 | |
| 858 | /* For a named type, we use the type_name directly */ |
| 859 | if(td->type_name != NULL) { |
| 860 | strncpy(basename, td->type_name, PATH_MAX); |
| 861 | basename_len = strlen(basename); |
| 862 | } else { |
| 863 | /* For a unnamed type, there must be a field name, except for |
| 864 | * the array. */ |
| 865 | if((basename_len != 0) |
| 866 | && (basename[basename_len-1] != '_' |
| 867 | && (field_name[0] != '\0'))) { |
| 868 | strncat(basename, "_", PATH_MAX - basename_len); |
| 869 | basename_len = strlen(basename); |
| 870 | } |
| 871 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 872 | } |
| 873 | |
| 874 | switch(td->type) { |
| 875 | case SEQUENCE: |
| 876 | case STRUCT: |
| 877 | case UNION: |
| 878 | case ARRAY: |
| 879 | break; |
| 880 | default: |
| 881 | dprintf("print_type_write_fct : type has no write function.\n"); |
| 882 | return 0; |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | /* Print header */ |
| 887 | switch(td->type) { |
| 888 | case SEQUENCE: |
| 889 | fprintf(fd, "static inline void lttng_write_sequence_%s(\n", |
| 890 | basename); |
| 891 | break; |
| 892 | case STRUCT: |
| 893 | fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename); |
| 894 | break; |
| 895 | case UNION: |
| 896 | fprintf(fd, "static inline void lttng_write_union_%s(\n", basename); |
| 897 | break; |
| 898 | case ARRAY: |
| 899 | fprintf(fd, "static inline void lttng_write_array_%s(\n", basename); |
| 900 | break; |
| 901 | default: |
| 902 | printf("print_type_write_fct : type has no write function.\n"); |
| 903 | break; |
| 904 | } |
| 905 | |
| 906 | print_tabs(2, fd); |
| 907 | fprintf(fd, "void *buffer,\n"); |
| 908 | print_tabs(2, fd); |
| 909 | fprintf(fd, "size_t *to_base,\n"); |
| 910 | print_tabs(2, fd); |
| 911 | fprintf(fd, "size_t *to,\n"); |
| 912 | print_tabs(2, fd); |
| 913 | fprintf(fd, "void **from,\n"); |
| 914 | print_tabs(2, fd); |
| 915 | fprintf(fd, "size_t *len,\n"); |
| 916 | print_tabs(2, fd); |
| 917 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 918 | |
| 919 | switch(td->type) { |
| 920 | case SEQUENCE: |
| 921 | fprintf(fd, " *obj)\n"); |
| 922 | break; |
| 923 | case STRUCT: |
| 924 | fprintf(fd, " *obj)\n"); |
| 925 | break; |
| 926 | case UNION: |
| 927 | fprintf(fd, " *obj)\n"); |
| 928 | break; |
| 929 | case ARRAY: |
| 930 | fprintf(fd, " obj)\n"); |
| 931 | break; |
| 932 | default: |
| 933 | printf("print_type_write_fct : type has no write function.\n"); |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | fprintf(fd, "{\n"); |
| 938 | print_tabs(1, fd); |
| 939 | fprintf(fd, "size_t align, size;\n"); |
| 940 | fprintf(fd, "\n"); |
| 941 | |
| 942 | switch(td->type) { |
| 943 | case SEQUENCE: |
| 944 | case STRING: |
| 945 | print_tabs(1, fd); |
| 946 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 947 | print_tabs(1, fd); |
| 948 | fprintf(fd, "if(*len != 0) {\n"); |
| 949 | print_tabs(2, fd); |
| 950 | fprintf(fd, "if(buffer != NULL)\n"); |
| 951 | print_tabs(3, fd); |
| 952 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
| 953 | print_tabs(1, fd); |
| 954 | fprintf(fd, "}\n"); |
| 955 | print_tabs(1, fd); |
| 956 | fprintf(fd, "*to += *len;\n"); |
| 957 | print_tabs(1, fd); |
| 958 | fprintf(fd, "*len = 0;\n"); |
| 959 | fprintf(fd, "\n"); |
| 960 | break; |
| 961 | case STRUCT: |
| 962 | case UNION: |
| 963 | case ARRAY: |
| 964 | break; |
| 965 | default: |
| 966 | printf("print_type_write_fct : type has no write function.\n"); |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | print_tabs(1, fd); |
| 971 | fprintf(fd, "align = "); |
| 972 | if(print_type_alignment(td, fd, 0, basename, field_name)) return 1; |
| 973 | fprintf(fd, ";\n"); |
| 974 | fprintf(fd, "\n"); |
| 975 | print_tabs(1, fd); |
| 976 | fprintf(fd, "if(*len == 0) {\n"); |
| 977 | print_tabs(2, fd); |
| 978 | fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n"); |
| 979 | print_tabs(1, fd); |
| 980 | fprintf(fd, "} else {\n"); |
| 981 | print_tabs(2, fd); |
| 982 | fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n"); |
| 983 | print_tabs(1, fd); |
| 984 | fprintf(fd, "}\n"); |
| 985 | fprintf(fd, "\n"); |
| 986 | |
| 987 | /* First, check if the type has a fixed size. If it is the case, then the size |
| 988 | * to write is know by the compiler : simply use a sizeof() */ |
| 989 | if(has_type_fixed_size(td)) { |
| 990 | print_tabs(1, fd); |
| 991 | fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n"); |
| 992 | fprintf(fd, "\n"); |
| 993 | print_tabs(1, fd); |
| 994 | fprintf(fd, "*len += sizeof("); |
| 995 | if(print_type(td, fd, 0, basename, field_name)) return 1; |
| 996 | fprintf(fd, ");\n"); |
| 997 | } else { |
| 998 | /* The type contains nested variable size subtypes : |
| 999 | * we must write field by field. */ |
| 1000 | print_tabs(1, fd); |
| 1001 | fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n"); |
| 1002 | fprintf(fd, "\n"); |
| 1003 | |
| 1004 | switch(td->type) { |
| 1005 | case SEQUENCE: |
| 1006 | print_tabs(1, fd); |
| 1007 | fprintf(fd, "/* Copy members */\n"); |
| 1008 | print_tabs(1, fd); |
| 1009 | fprintf(fd, "size = sizeof(\n"); |
| 1010 | if(print_type_write(((field_t*)td->fields.array[0])->type, |
| 1011 | fd, 1, basename, ((field_t*)td->fields.array[0])->name)) return 1; |
| 1012 | fprintf(fd, ");\n"); |
| 1013 | print_tabs(1, fd); |
| 1014 | fprintf(fd, "*to += ltt_align(*to, size);\n"); |
| 1015 | print_tabs(1, fd); |
| 1016 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1017 | print_tabs(2, fd); |
| 1018 | fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, size);\n"); |
| 1019 | print_tabs(1, fd); |
| 1020 | fprintf(fd, "*to += size;\n"); |
| 1021 | fprintf(fd, "\n"); |
| 1022 | |
| 1023 | /* Write the child : varlen child or not ? */ |
| 1024 | if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) { |
| 1025 | /* Fixed size len child : use a multiplication of its size */ |
| 1026 | print_tabs(1, fd); |
| 1027 | fprintf(fd, "size = sizeof(\n"); |
| 1028 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
| 1029 | fd, 1, basename, ((field_t*)td->fields.array[1])->name)) return 1; |
| 1030 | fprintf(fd, ");\n"); |
| 1031 | print_tabs(1, fd); |
| 1032 | fprintf(fd, "*to += ltt_align(*to, size);\n"); |
| 1033 | print_tabs(1, fd); |
| 1034 | fprintf(fd, "size = obj->len * size;\n"); |
| 1035 | print_tabs(1, fd); |
| 1036 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1037 | print_tabs(2, fd); |
| 1038 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, size);\n"); |
| 1039 | print_tabs(1, fd); |
| 1040 | fprintf(fd, "*to += size;\n"); |
| 1041 | fprintf(fd, "\n"); |
| 1042 | } else { |
| 1043 | print_tabs(1, fd); |
| 1044 | fprintf(fd, "/* Variable length child : iter. */\n"); |
| 1045 | print_tabs(1, fd); |
| 1046 | fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n"); |
| 1047 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
| 1048 | fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1; |
| 1049 | print_tabs(1, fd); |
| 1050 | fprintf(fd, "}\n"); |
| 1051 | } |
| 1052 | fprintf(fd, "\n"); |
| 1053 | print_tabs(1, fd); |
| 1054 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
| 1055 | print_tabs(1, fd); |
| 1056 | fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n"); |
| 1057 | print_tabs(1, fd); |
| 1058 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
| 1059 | print_tabs(1, fd); |
| 1060 | fprintf(fd, "*to = 0;\n"); |
| 1061 | fprintf(fd, "\n"); |
| 1062 | fprintf(fd, "/* Put source *from just after the C sequence */\n"); |
| 1063 | print_tabs(1, fd); |
| 1064 | fprintf(fd, "*from = obj+1;\n"); |
| 1065 | break; |
| 1066 | case STRING: |
| 1067 | fprintf(fd, "size = strlen(obj);\n"); |
| 1068 | print_tabs(1, fd); |
| 1069 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1070 | print_tabs(2, fd); |
| 1071 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n"); |
| 1072 | print_tabs(1, fd); |
| 1073 | fprintf(fd, "*to += size;\n"); |
| 1074 | fprintf(fd, "\n"); |
| 1075 | print_tabs(1, fd); |
| 1076 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
| 1077 | print_tabs(1, fd); |
| 1078 | fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n"); |
| 1079 | print_tabs(1, fd); |
| 1080 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
| 1081 | print_tabs(1, fd); |
| 1082 | fprintf(fd, "*to = 0;\n"); |
| 1083 | fprintf(fd, "\n"); |
| 1084 | fprintf(fd, "/* Put source *from just after the C sequence */\n"); |
| 1085 | print_tabs(1, fd); |
| 1086 | fprintf(fd, "*from = obj+1;\n"); |
| 1087 | break; |
| 1088 | case STRUCT: |
| 1089 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 1090 | field_t *field = (field_t*)(td->fields.array[i]); |
| 1091 | type_descriptor_t *type = field->type; |
| 1092 | if(print_type_write(type, |
| 1093 | fd, 1, basename, field->name)) return 1; |
| 1094 | fprintf(fd, "\n"); |
| 1095 | } |
| 1096 | break; |
| 1097 | case UNION: |
| 1098 | printf("ERROR : A union CANNOT contain a variable size child.\n"); |
| 1099 | return 1; |
| 1100 | break; |
| 1101 | case ARRAY: |
| 1102 | /* Write the child : varlen child or not ? */ |
| 1103 | if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) { |
| 1104 | /* Error : if an array has a variable size, then its child must also |
| 1105 | * have a variable size. */ |
| 1106 | assert(0); |
| 1107 | } else { |
| 1108 | print_tabs(1, fd); |
| 1109 | fprintf(fd, "/* Variable length child : iter. */\n"); |
| 1110 | print_tabs(1, fd); |
| 1111 | fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename); |
| 1112 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
| 1113 | fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1; |
| 1114 | print_tabs(1, fd); |
| 1115 | fprintf(fd, "}\n"); |
| 1116 | } |
| 1117 | break; |
| 1118 | default: |
| 1119 | printf("print_type_write_fct : type has no write function.\n"); |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | /* Function footer */ |
| 1128 | fprintf(fd, "}\n"); |
| 1129 | fprintf(fd, "\n"); |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | |
| 1134 | |
| 1135 | /* Print the logging function of an event. This is the core of genevent */ |
| 1136 | int print_event_logging_function(char *basename, facility_t *fac, |
| 1137 | event_t *event, FILE *fd) |
| 1138 | { |
| 1139 | fprintf(fd, "static inline void trace_%s(\n", basename); |
| 1140 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 1141 | /* For each field, print the function argument */ |
| 1142 | field_t *f = (field_t*)event->fields.array[j]; |
| 1143 | type_descriptor_t *t = f->type; |
| 1144 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
| 1145 | if(j < event->fields.position-1) { |
| 1146 | fprintf(fd, ","); |
| 1147 | fprintf(fd, "\n"); |
| 1148 | } |
| 1149 | } |
| 1150 | if(event->fields.position == 0) { |
| 1151 | print_tabs(2, fd); |
| 1152 | fprintf(fd, "void"); |
| 1153 | } |
| 1154 | fprintf(fd,")\n"); |
| 1155 | fprintf(fd, |
| 1156 | "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n", |
| 1157 | fac->capname); |
| 1158 | fprintf(fd, "{\n"); |
| 1159 | fprintf(fd, "}\n"); |
| 1160 | fprintf(fd,"#else\n"); |
| 1161 | fprintf(fd, "{\n"); |
| 1162 | /* Print the function variables */ |
| 1163 | print_tabs(1, fd); |
| 1164 | fprintf(fd, "unsigned int index;\n"); |
| 1165 | print_tabs(1, fd); |
| 1166 | fprintf(fd, "struct ltt_channel_struct *channel;\n"); |
| 1167 | print_tabs(1, fd); |
| 1168 | fprintf(fd, "struct ltt_trace_struct *trace;\n"); |
| 1169 | print_tabs(1, fd); |
| 1170 | fprintf(fd, "struct rchan_buf *relayfs_buf;\n"); |
| 1171 | print_tabs(1, fd); |
| 1172 | fprintf(fd, "void *buffer = NULL;\n"); |
| 1173 | print_tabs(1, fd); |
| 1174 | fprintf(fd, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
| 1175 | print_tabs(1, fd); |
| 1176 | fprintf(fd, "size_t to = 0;\n"); |
| 1177 | print_tabs(1, fd); |
| 1178 | fprintf(fd, "void *from;"); |
| 1179 | print_tabs(1, fd); |
| 1180 | fprintf(fd, "size_t len = 0;\n"); |
| 1181 | print_tabs(1, fd); |
| 1182 | fprintf(fd, "size_t reserve_size;\n"); |
| 1183 | print_tabs(1, fd); |
| 1184 | fprintf(fd, "size_t slot_size;\n"); |
| 1185 | print_tabs(1, fd); |
| 1186 | fprintf(fd, "cycles_t tsc;\n"); |
| 1187 | print_tabs(1, fd); |
| 1188 | fprintf(fd, "size_t before_hdr_pad, size_t after_hdr_pad;\n"); |
| 1189 | fprintf(fd, "\n"); |
| 1190 | |
| 1191 | print_tabs(1, fd); |
| 1192 | fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n"); |
| 1193 | fprintf(fd, "\n"); |
| 1194 | |
| 1195 | /* Calculate event variable len + event data alignment offset. |
| 1196 | * Assume that the padding for alignment starts at a void* |
| 1197 | * address. |
| 1198 | * This excludes the header size and alignment. */ |
| 1199 | |
| 1200 | print_tabs(1, fd); |
| 1201 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
| 1202 | print_tabs(1, fd); |
| 1203 | fprintf(fd, "/* size = to_base + to + len */\n"); |
| 1204 | print_tabs(1, fd); |
| 1205 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
| 1206 | print_tabs(1, fd); |
| 1207 | fprintf(fd, " * sizeof(void *) address. */\n"); |
| 1208 | fprintf(fd, "\n"); |
| 1209 | |
| 1210 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1211 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1212 | type_descriptor_t *type = field->type; |
| 1213 | if(print_type_write(type, |
| 1214 | fd, 1, basename, field->name)) return 1; |
| 1215 | fprintf(fd, "\n"); |
| 1216 | } |
| 1217 | print_tabs(1, fd); |
| 1218 | fprintf(fd, "reserve_size = to_base + to + len;\n"); |
| 1219 | print_tabs(1, fd); |
| 1220 | fprintf(fd, "to_base = to = len = 0;\n"); |
| 1221 | fprintf(fd, "\n"); |
| 1222 | |
| 1223 | /* Take locks : make sure the trace does not vanish while we write on |
| 1224 | * it. A simple preemption disabling is enough (using rcu traces). */ |
| 1225 | print_tabs(1, fd); |
| 1226 | fprintf(fd, "preempt_disable();\n"); |
| 1227 | print_tabs(1, fd); |
| 1228 | fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n"); |
| 1229 | |
| 1230 | /* Get facility index */ |
| 1231 | |
| 1232 | if(event->per_tracefile) { |
| 1233 | print_tabs(1, fd); |
| 1234 | fprintf(fd, "index = tracefile_index;\n"); |
| 1235 | } else { |
| 1236 | print_tabs(1, fd); |
| 1237 | fprintf(fd, |
| 1238 | "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\ |
| 1239 | "\t\t\t\t\t\tevent_%s);\n", |
| 1240 | fac->name, fac->checksum, event->name); |
| 1241 | } |
| 1242 | fprintf(fd,"\n"); |
| 1243 | |
| 1244 | |
| 1245 | /* For each trace */ |
| 1246 | print_tabs(1, fd); |
| 1247 | fprintf(fd, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n"); |
| 1248 | print_tabs(2, fd); |
| 1249 | fprintf(fd, "if(!trace->active) continue;\n\n"); |
| 1250 | |
| 1251 | if(event->per_trace) { |
| 1252 | print_tabs(2, fd); |
| 1253 | fprintf(fd, "if(dest_trace != trace) continue;\n\n"); |
| 1254 | } |
| 1255 | |
| 1256 | print_tabs(2, fd); |
| 1257 | fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n"); |
| 1258 | print_tabs(2, fd); |
| 1259 | fprintf(fd, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n"); |
| 1260 | fprintf(fd, "\n"); |
| 1261 | |
| 1262 | |
| 1263 | /* Relay reserve */ |
| 1264 | /* If error, increment event lost counter (done by ltt_reserve_slot) and |
| 1265 | * return */ |
| 1266 | print_tabs(2, fd); |
| 1267 | fprintf(fd, "slot_size = 0;\n"); |
| 1268 | print_tabs(2, fd); |
| 1269 | fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n"); |
| 1270 | print_tabs(3, fd); |
| 1271 | fprintf(fd, "reserve_size, &slot_size, &tsc,\n"); |
| 1272 | print_tabs(3, fd); |
| 1273 | fprintf(fd, "&before_hdr_pad, &after_hdr_pad);\n"); |
| 1274 | /* If error, return */ |
| 1275 | print_tabs(2, fd); |
| 1276 | fprintf(fd, "if(!buffer) return;\n\n"); |
| 1277 | |
| 1278 | /* write data : assume stack alignment is the same as struct alignment. */ |
| 1279 | |
| 1280 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1281 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1282 | type_descriptor_t *type = field->type; |
| 1283 | |
| 1284 | /* Set from */ |
| 1285 | print_tabs(2, fd); |
| 1286 | switch(type->type) { |
| 1287 | case SEQUENCE: |
| 1288 | case UNION: |
| 1289 | case ARRAY: |
| 1290 | case STRUCT: |
| 1291 | case STRING: |
| 1292 | fprintf(fd, "from = %s;\n", field->name); |
| 1293 | break; |
| 1294 | default: |
| 1295 | fprintf(fd, "from = &%s;\n", field->name); |
| 1296 | break; |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | if(print_type_write(type, |
| 1301 | fd, 2, basename, field->name)) return 1; |
| 1302 | fprintf(fd, "\n"); |
| 1303 | |
| 1304 | /* Don't forget to flush pending memcpy */ |
| 1305 | print_tabs(2, fd); |
| 1306 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 1307 | print_tabs(2, fd); |
| 1308 | fprintf(fd, "if(len != 0) {\n"); |
| 1309 | print_tabs(3, fd); |
| 1310 | fprintf(fd, "memcpy(buffer+to_base+to, from, len);\n"); |
| 1311 | print_tabs(3, fd); |
| 1312 | fprintf(fd, "to += len;\n"); |
| 1313 | //print_tabs(3, fd); |
| 1314 | //fprintf(fd, "from += len;\n"); |
| 1315 | print_tabs(3, fd); |
| 1316 | fprintf(fd, "len = 0;\n"); |
| 1317 | print_tabs(2, fd); |
| 1318 | fprintf(fd, "}\n"); |
| 1319 | fprintf(fd, "\n"); |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | /* commit */ |
| 1324 | print_tabs(2, fd); |
| 1325 | fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n"); |
| 1326 | |
| 1327 | print_tabs(1, fd); |
| 1328 | fprintf(fd, "}\n\n"); |
| 1329 | |
| 1330 | /* Release locks */ |
| 1331 | print_tabs(1, fd); |
| 1332 | fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n"); |
| 1333 | print_tabs(1, fd); |
| 1334 | fprintf(fd, "preempt_enable_no_resched();\n"); |
| 1335 | |
| 1336 | fprintf(fd, "}\n"); |
| 1337 | fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n", |
| 1338 | fac->capname); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* ltt-facility-name.h : main logging header. |
| 1344 | * log_header */ |
| 1345 | |
| 1346 | void print_log_header_head(facility_t *fac, FILE *fd) |
| 1347 | { |
| 1348 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
| 1349 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
| 1350 | fprintf(fd, "\n"); |
| 1351 | } |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | int print_log_header_types(facility_t *fac, FILE *fd) |
| 1356 | { |
| 1357 | sequence_t *types = &fac->named_types.values; |
| 1358 | fprintf(fd, "/* Named types */\n"); |
| 1359 | fprintf(fd, "\n"); |
| 1360 | |
| 1361 | for(unsigned int i = 0; i < types->position; i++) { |
| 1362 | /* For each named type, print the definition */ |
| 1363 | if((print_type_declaration(types->array[i], fd, |
| 1364 | 0, "", ""))) return 1; |
| 1365 | /* Print also the align function */ |
| 1366 | if((print_type_alignment_fct(types->array[i], fd, |
| 1367 | 0, "", ""))) return 1; |
| 1368 | /* Print also the write function */ |
| 1369 | if((print_type_write_fct(types->array[i], fd, |
| 1370 | 0, "", ""))) return 1; |
| 1371 | } |
| 1372 | return 0; |
| 1373 | } |
| 1374 | |
| 1375 | int print_log_header_events(facility_t *fac, FILE *fd) |
| 1376 | { |
| 1377 | sequence_t *events = &fac->events; |
| 1378 | char basename[PATH_MAX]; |
| 1379 | unsigned int facname_len; |
| 1380 | |
| 1381 | strncpy(basename, fac->name, PATH_MAX); |
| 1382 | facname_len = strlen(basename); |
| 1383 | strncat(basename, "_", PATH_MAX-facname_len); |
| 1384 | facname_len = strlen(basename); |
| 1385 | |
| 1386 | for(unsigned int i = 0; i < events->position; i++) { |
| 1387 | event_t *event = (event_t*)events->array[i]; |
| 1388 | strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len); |
| 1389 | |
| 1390 | /* For each event, print structure, and then logging function */ |
| 1391 | fprintf(fd, "/* Event %s structures */\n", |
| 1392 | event->name); |
| 1393 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 1394 | /* For each unnamed type, print the definition */ |
| 1395 | field_t *f = (field_t*)event->fields.array[j]; |
| 1396 | type_descriptor_t *t = f->type; |
| 1397 | if(t->type_name == NULL) { |
| 1398 | if((print_type_declaration(t, fd, 0, basename, f->name))) return 1; |
| 1399 | /* Print also the align function */ |
| 1400 | if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1; |
| 1401 | /* Print also the write function */ |
| 1402 | if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | fprintf(fd, "\n"); |
| 1407 | |
| 1408 | fprintf(fd, "/* Event %s logging function */\n", |
| 1409 | event->name); |
| 1410 | |
| 1411 | if(print_event_logging_function(basename, fac, event, fd)) return 1; |
| 1412 | |
| 1413 | fprintf(fd, "\n"); |
| 1414 | } |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | void print_log_header_tail(facility_t *fac, FILE *fd) |
| 1421 | { |
| 1422 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
| 1423 | } |
| 1424 | |
| 1425 | int print_log_header(facility_t *fac) |
| 1426 | { |
| 1427 | char filename[PATH_MAX]; |
| 1428 | unsigned int filename_size = 0; |
| 1429 | FILE *fd; |
| 1430 | dprintf("%s\n", fac->name); |
| 1431 | |
| 1432 | strcpy(filename, "ltt-facility-"); |
| 1433 | filename_size = strlen(filename); |
| 1434 | |
| 1435 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 1436 | filename_size = strlen(filename); |
| 1437 | |
| 1438 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 1439 | filename_size = strlen(filename); |
| 1440 | |
| 1441 | |
| 1442 | fd = fopen(filename, "w"); |
| 1443 | if(fd == NULL) { |
| 1444 | printf("Error opening file %s for writing : %s\n", |
| 1445 | filename, strerror(errno)); |
| 1446 | return errno; |
| 1447 | } |
| 1448 | |
| 1449 | /* Print file head */ |
| 1450 | print_log_header_head(fac, fd); |
| 1451 | |
| 1452 | /* print named types in declaration order */ |
| 1453 | if(print_log_header_types(fac, fd)) return 1; |
| 1454 | |
| 1455 | /* Print events */ |
| 1456 | if(print_log_header_events(fac, fd)) return 1; |
| 1457 | |
| 1458 | /* Print file tail */ |
| 1459 | print_log_header_tail(fac, fd); |
| 1460 | |
| 1461 | |
| 1462 | fclose(fd); |
| 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | /* ltt-facility-id-name.h : facility id. |
| 1469 | * log_id_header */ |
| 1470 | int print_id_header(facility_t *fac) |
| 1471 | { |
| 1472 | char filename[PATH_MAX]; |
| 1473 | unsigned int filename_size = 0; |
| 1474 | FILE *fd; |
| 1475 | char basename[PATH_MAX]; |
| 1476 | char basename_len = 0; |
| 1477 | |
| 1478 | dprintf("%s\n", fac->name); |
| 1479 | |
| 1480 | strcpy(filename, "ltt-facility-id-"); |
| 1481 | filename_size = strlen(filename); |
| 1482 | |
| 1483 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 1484 | filename_size = strlen(filename); |
| 1485 | |
| 1486 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 1487 | filename_size = strlen(filename); |
| 1488 | |
| 1489 | |
| 1490 | fd = fopen(filename, "w"); |
| 1491 | if(fd == NULL) { |
| 1492 | printf("Error opening file %s for writing : %s\n", |
| 1493 | filename, strerror(errno)); |
| 1494 | return errno; |
| 1495 | } |
| 1496 | |
| 1497 | fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 1498 | fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname); |
| 1499 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
| 1500 | |
| 1501 | fprintf(fd,"#include <linux/ltt-facilities.h>\n\n"); |
| 1502 | |
| 1503 | fprintf(fd,"/**** facility handle ****/\n\n"); |
| 1504 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n", |
| 1505 | fac->name, fac->checksum); |
| 1506 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name); |
| 1507 | |
| 1508 | strncpy(basename, fac->name, PATH_MAX); |
| 1509 | basename_len = strlen(basename); |
| 1510 | strncat(basename, "_", PATH_MAX - basename_len); |
| 1511 | basename_len++; |
| 1512 | |
| 1513 | fprintf(fd,"/**** event index ****/\n\n"); |
| 1514 | fprintf(fd,"enum %s_event {\n",fac->name); |
| 1515 | |
| 1516 | for(unsigned int i = 0; i < fac->events.position; i++) { |
| 1517 | event_t *event = (event_t*)fac->events.array[i]; |
| 1518 | strncpy(basename+basename_len, event->name, PATH_MAX-basename_len); |
| 1519 | print_tabs(1, fd); |
| 1520 | fprintf(fd, "event_%s,\n", basename); |
| 1521 | } |
| 1522 | print_tabs(1, fd); |
| 1523 | fprintf(fd, "facility_%s_num_events\n", fac->name); |
| 1524 | fprintf(fd, "};\n"); |
| 1525 | fprintf(fd, "\n"); |
| 1526 | |
| 1527 | |
| 1528 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
| 1529 | fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 1530 | |
| 1531 | |
| 1532 | fclose(fd); |
| 1533 | |
| 1534 | return 0; |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | /* ltt-facility-loader-name.h : facility specific loader info. |
| 1539 | * loader_header */ |
| 1540 | int print_loader_header(facility_t *fac) |
| 1541 | { |
| 1542 | char filename[PATH_MAX]; |
| 1543 | unsigned int filename_size = 0; |
| 1544 | FILE *fd; |
| 1545 | dprintf("%s\n", fac->name); |
| 1546 | |
| 1547 | strcpy(filename, "ltt-facility-loader-"); |
| 1548 | filename_size = strlen(filename); |
| 1549 | |
| 1550 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 1551 | filename_size = strlen(filename); |
| 1552 | |
| 1553 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 1554 | filename_size = strlen(filename); |
| 1555 | |
| 1556 | |
| 1557 | fd = fopen(filename, "w"); |
| 1558 | if(fd == NULL) { |
| 1559 | printf("Error opening file %s for writing : %s\n", |
| 1560 | filename, strerror(errno)); |
| 1561 | return errno; |
| 1562 | } |
| 1563 | |
| 1564 | fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 1565 | fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname); |
| 1566 | fprintf(fd, "#ifdef CONFIG_LTT\n\n"); |
| 1567 | fprintf(fd,"#include <linux/ltt-facilities.h>\n"); |
| 1568 | fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n", |
| 1569 | fac->name); |
| 1570 | fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name); |
| 1571 | fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n", |
| 1572 | fac->name, fac->checksum); |
| 1573 | |
| 1574 | fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n", |
| 1575 | fac->name); |
| 1576 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n", |
| 1577 | fac->name, fac->checksum); |
| 1578 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum); |
| 1579 | fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name); |
| 1580 | fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n", |
| 1581 | fac->name); |
| 1582 | fprintf(fd, "#endif //CONFIG_LTT\n\n"); |
| 1583 | fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 1584 | |
| 1585 | fclose(fd); |
| 1586 | |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | /* ltt-facility-loader-name.c : generic faciilty loader |
| 1591 | * loader_c */ |
| 1592 | int print_loader_c(facility_t *fac) |
| 1593 | { |
| 1594 | char filename[PATH_MAX]; |
| 1595 | unsigned int filename_size = 0; |
| 1596 | FILE *fd; |
| 1597 | dprintf("%s\n", fac->name); |
| 1598 | |
| 1599 | strcpy(filename, "ltt-facility-loader-"); |
| 1600 | filename_size = strlen(filename); |
| 1601 | |
| 1602 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 1603 | filename_size = strlen(filename); |
| 1604 | |
| 1605 | strncat(filename, ".c", PATH_MAX - filename_size); |
| 1606 | filename_size = strlen(filename); |
| 1607 | |
| 1608 | |
| 1609 | fd = fopen(filename, "w"); |
| 1610 | if(fd == NULL) { |
| 1611 | printf("Error opening file %s for writing : %s\n", |
| 1612 | filename, strerror(errno)); |
| 1613 | return errno; |
| 1614 | } |
| 1615 | |
| 1616 | fprintf(fd, "/*\n"); |
| 1617 | fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name); |
| 1618 | fprintf(fd, " *\n"); |
| 1619 | fprintf(fd, " * (C) Copyright 2005 - \n"); |
| 1620 | fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n"); |
| 1621 | fprintf(fd, " *\n"); |
| 1622 | fprintf(fd, " * Contains the LTT facility loader.\n"); |
| 1623 | fprintf(fd, " *\n"); |
| 1624 | fprintf(fd, " */\n"); |
| 1625 | fprintf(fd, "\n"); |
| 1626 | fprintf(fd, "\n"); |
| 1627 | fprintf(fd, "#include <linux/ltt-facilities.h>\n"); |
| 1628 | fprintf(fd, "#include <linux/module.h>\n"); |
| 1629 | fprintf(fd, "#include <linux/init.h>\n"); |
| 1630 | fprintf(fd, "#include <linux/config.h>\n"); |
| 1631 | fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name); |
| 1632 | fprintf(fd, "\n"); |
| 1633 | fprintf(fd, "\n"); |
| 1634 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
| 1635 | fprintf(fd, "\n"); |
| 1636 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n"); |
| 1637 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n"); |
| 1638 | fprintf(fd, "\n"); |
| 1639 | fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n"); |
| 1640 | fprintf(fd, "\n"); |
| 1641 | fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n"); |
| 1642 | fprintf(fd, "\n"); |
| 1643 | fprintf(fd, "static struct ltt_facility facility = {\n"); |
| 1644 | fprintf(fd, "\t.name = ltt_facility_name,\n"); |
| 1645 | fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n"); |
| 1646 | fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n"); |
| 1647 | fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n"); |
| 1648 | fprintf(fd, "};\n"); |
| 1649 | fprintf(fd, "\n"); |
| 1650 | fprintf(fd, "#ifndef MODULE\n"); |
| 1651 | fprintf(fd, "\n"); |
| 1652 | fprintf(fd, "/* Built-in facility. */\n"); |
| 1653 | fprintf(fd, "\n"); |
| 1654 | fprintf(fd, "static int __init facility_init(void)\n"); |
| 1655 | fprintf(fd, "{\n"); |
| 1656 | fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name); |
| 1657 | fprintf(fd, "\n"); |
| 1658 | fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n"); |
| 1659 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
| 1660 | fprintf(fd, "\t\n"); |
| 1661 | fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n"); |
| 1662 | fprintf(fd, "}\n"); |
| 1663 | fprintf(fd, "__initcall(facility_init);\n"); |
| 1664 | fprintf(fd, "\n"); |
| 1665 | fprintf(fd, "\n"); |
| 1666 | fprintf(fd, "\n"); |
| 1667 | fprintf(fd, "#else \n"); |
| 1668 | fprintf(fd, "\n"); |
| 1669 | fprintf(fd, "/* Dynamic facility. */\n"); |
| 1670 | fprintf(fd, "\n"); |
| 1671 | fprintf(fd, "static int __init facility_init(void)\n"); |
| 1672 | fprintf(fd, "{\n"); |
| 1673 | fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac->name); |
| 1674 | fprintf(fd, "\n"); |
| 1675 | fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n"); |
| 1676 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
| 1677 | fprintf(fd, "\n"); |
| 1678 | fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n"); |
| 1679 | fprintf(fd, "}\n"); |
| 1680 | fprintf(fd, "\n"); |
| 1681 | fprintf(fd, "static void __exit facility_exit(void)\n"); |
| 1682 | fprintf(fd, "{\n"); |
| 1683 | fprintf(fd, "\tint err;\n"); |
| 1684 | fprintf(fd, "\n"); |
| 1685 | fprintf(fd, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n"); |
| 1686 | fprintf(fd, "\tif(err != 0)\n"); |
| 1687 | fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n"); |
| 1688 | fprintf(fd, "\n"); |
| 1689 | fprintf(fd, "}\n"); |
| 1690 | fprintf(fd, "\n"); |
| 1691 | fprintf(fd, "module_init(facility_init)\n"); |
| 1692 | fprintf(fd, "module_exit(facility_exit)\n"); |
| 1693 | fprintf(fd, "\n"); |
| 1694 | fprintf(fd, "\n"); |
| 1695 | fprintf(fd, "MODULE_LICENSE(\"GPL\");\n"); |
| 1696 | fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n"); |
| 1697 | fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n"); |
| 1698 | fprintf(fd, "\n"); |
| 1699 | fprintf(fd, "#endif //MODULE\n"); |
| 1700 | fprintf(fd, "\n"); |
| 1701 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
| 1702 | |
| 1703 | fclose(fd); |
| 1704 | |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | |
| 1710 | /* open facility */ |
| 1711 | /* code taken from ltt_facility_open in ltt/facility.c in lttv */ |
| 1712 | |
| 1713 | /***************************************************************************** |
| 1714 | *Function name |
| 1715 | * ltt_facility_open : open facilities |
| 1716 | *Input params |
| 1717 | * pathname : the path name of the facility |
| 1718 | * |
| 1719 | * Open the facility corresponding to the right checksum. |
| 1720 | * |
| 1721 | *returns the facility on success, NULL on error. |
| 1722 | ****************************************************************************/ |
| 1723 | facility_t *ltt_facility_open(char * pathname) |
| 1724 | { |
| 1725 | int ret = 0; |
| 1726 | char *token; |
| 1727 | parse_file_t in; |
| 1728 | facility_t * fac = NULL; |
| 1729 | char buffer[BUFFER_SIZE]; |
| 1730 | int generated = FALSE; |
| 1731 | |
| 1732 | in.buffer = &(buffer[0]); |
| 1733 | in.lineno = 0; |
| 1734 | in.error = error_callback; |
| 1735 | in.name = pathname; |
| 1736 | in.unget = 0; |
| 1737 | |
| 1738 | in.fp = fopen(in.name, "r"); |
| 1739 | if(in.fp == NULL) { |
| 1740 | ret = 1; |
| 1741 | goto open_error; |
| 1742 | } |
| 1743 | |
| 1744 | while(1){ |
| 1745 | token = getToken(&in); |
| 1746 | if(in.type == ENDFILE) break; |
| 1747 | |
| 1748 | if(generated) { |
| 1749 | printf("More than one facility in the file. Only using the first one.\n"); |
| 1750 | break; |
| 1751 | } |
| 1752 | |
| 1753 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
| 1754 | token = getName(&in); |
| 1755 | |
| 1756 | if(strcmp("facility",token) == 0) { |
| 1757 | fac = malloc(sizeof(facility_t)); |
| 1758 | fac->name = NULL; |
| 1759 | fac->description = NULL; |
| 1760 | sequence_init(&(fac->events)); |
| 1761 | table_init(&(fac->named_types)); |
| 1762 | sequence_init(&(fac->unnamed_types)); |
| 1763 | |
| 1764 | parseFacility(&in, fac); |
| 1765 | |
| 1766 | //check if any namedType is not defined |
| 1767 | checkNamedTypesImplemented(&fac->named_types); |
| 1768 | |
| 1769 | generateChecksum(fac->name, &fac->checksum, &fac->events); |
| 1770 | |
| 1771 | generated = TRUE; |
| 1772 | } |
| 1773 | else { |
| 1774 | printf("facility token was expected in file %s\n", in.name); |
| 1775 | ret = 1; |
| 1776 | goto parse_error; |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | parse_error: |
| 1781 | fclose(in.fp); |
| 1782 | open_error: |
| 1783 | |
| 1784 | if(!generated) { |
| 1785 | printf("Cannot find facility %s\n", pathname); |
| 1786 | fac = NULL; |
| 1787 | } |
| 1788 | |
| 1789 | return fac; |
| 1790 | } |
| 1791 | |
| 1792 | /* Close the facility */ |
| 1793 | void ltt_facility_close(facility_t *fac) |
| 1794 | { |
| 1795 | free(fac->name); |
| 1796 | free(fac->capname); |
| 1797 | free(fac->description); |
| 1798 | freeEvents(&fac->events); |
| 1799 | sequence_dispose(&fac->events); |
| 1800 | freeNamedType(&fac->named_types); |
| 1801 | table_dispose(&fac->named_types); |
| 1802 | freeTypes(&fac->unnamed_types); |
| 1803 | sequence_dispose(&fac->unnamed_types); |
| 1804 | free(fac); |
| 1805 | } |
| 1806 | |
| 1807 | |
| 1808 | /* Show help */ |
| 1809 | void show_help(int argc, char ** argv) |
| 1810 | { |
| 1811 | printf("Genevent help : \n"); |
| 1812 | printf("\n"); |
| 1813 | printf("Use %s name.xml\n", argv[0]); |
| 1814 | printf("to create :\n"); |
| 1815 | printf("ltt-facility-name.h\n"); |
| 1816 | printf("ltt-facility-id-name.h\n"); |
| 1817 | printf("ltt-facility-loader-name.h\n"); |
| 1818 | printf("ltt-facility-loader-name.c\n"); |
| 1819 | printf("In the current directory.\n"); |
| 1820 | printf("\n"); |
| 1821 | } |
| 1822 | |
| 1823 | /* Parse program arguments */ |
| 1824 | /* Return values : |
| 1825 | * 0 : continue program |
| 1826 | * -1 : stop program, return 0 |
| 1827 | * > 0 : stop program, return value as exit. |
| 1828 | */ |
| 1829 | int check_args(int argc, char **argv) |
| 1830 | { |
| 1831 | if(argc < 2) { |
| 1832 | printf("Not enough arguments\n"); |
| 1833 | show_help(argc, argv); |
| 1834 | return EINVAL; |
| 1835 | } |
| 1836 | |
| 1837 | if(strcmp(argv[1], "-h") == 0) { |
| 1838 | show_help(argc, argv); |
| 1839 | return -1; |
| 1840 | } |
| 1841 | |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | int main(int argc, char **argv) |
| 1846 | { |
| 1847 | int err = 0; |
| 1848 | facility_t *fac; |
| 1849 | |
| 1850 | err = check_args(argc, argv); |
| 1851 | if(err > 0) return err; |
| 1852 | else if(err < 0) return 0; |
| 1853 | |
| 1854 | /* open the facility */ |
| 1855 | fac = ltt_facility_open(argv[1]); |
| 1856 | if(fac == NULL) { |
| 1857 | printf("Error opening file %s for reading : %s\n", |
| 1858 | argv[1], strerror(errno)); |
| 1859 | return errno; |
| 1860 | } |
| 1861 | |
| 1862 | /* generate the output C files */ |
| 1863 | |
| 1864 | |
| 1865 | /* ltt-facility-name.h : main logging header. |
| 1866 | * log_header */ |
| 1867 | err = print_log_header(fac); |
| 1868 | if(err) return err; |
| 1869 | |
| 1870 | /* ltt-facility-id-name.h : facility id. |
| 1871 | * log_id_header */ |
| 1872 | err = print_id_header(fac); |
| 1873 | if(err) return err; |
| 1874 | |
| 1875 | /* ltt-facility-loader-name.h : facility specific loader info. |
| 1876 | * loader_header */ |
| 1877 | err = print_loader_header(fac); |
| 1878 | if(err) return err; |
| 1879 | |
| 1880 | /* ltt-facility-loader-name.c : generic faciilty loader |
| 1881 | * loader_c */ |
| 1882 | err = print_loader_c(fac); |
| 1883 | if(err) return err; |
| 1884 | |
| 1885 | /* close the facility */ |
| 1886 | ltt_facility_close(fac); |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
| 1891 | |