| 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, "const 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, "const 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, "lttng_param_%s", field_name); |
| 228 | break; |
| 229 | case UINT_FIXED: |
| 230 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
| 231 | fprintf(fd, "lttng_param_%s", field_name); |
| 232 | break; |
| 233 | case CHAR: |
| 234 | fprintf(fd, "signed char"); |
| 235 | fprintf(fd, " lttng_param_%s", field_name); |
| 236 | break; |
| 237 | case UCHAR: |
| 238 | fprintf(fd, "unsigned char"); |
| 239 | fprintf(fd, " lttng_param_%s", field_name); |
| 240 | break; |
| 241 | case SHORT: |
| 242 | fprintf(fd, "short"); |
| 243 | fprintf(fd, " lttng_param_%s", field_name); |
| 244 | break; |
| 245 | case USHORT: |
| 246 | fprintf(fd, "unsigned short"); |
| 247 | fprintf(fd, " lttng_param_%s", field_name); |
| 248 | break; |
| 249 | case INT: |
| 250 | fprintf(fd, "int"); |
| 251 | fprintf(fd, " lttng_param_%s", field_name); |
| 252 | break; |
| 253 | case UINT: |
| 254 | fprintf(fd, "unsigned int"); |
| 255 | fprintf(fd, " lttng_param_%s", field_name); |
| 256 | break; |
| 257 | case FLOAT: |
| 258 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
| 259 | fprintf(fd, " lttng_param_%s", field_name); |
| 260 | break; |
| 261 | case POINTER: |
| 262 | fprintf(fd, "const void *"); |
| 263 | fprintf(fd, " lttng_param_%s", field_name); |
| 264 | break; |
| 265 | case LONG: |
| 266 | fprintf(fd, "long"); |
| 267 | fprintf(fd, " lttng_param_%s", field_name); |
| 268 | break; |
| 269 | case ULONG: |
| 270 | fprintf(fd, "unsigned long"); |
| 271 | fprintf(fd, " lttng_param_%s", field_name); |
| 272 | break; |
| 273 | case SIZE_T: |
| 274 | fprintf(fd, "size_t"); |
| 275 | fprintf(fd, " lttng_param_%s", field_name); |
| 276 | break; |
| 277 | case SSIZE_T: |
| 278 | fprintf(fd, "ssize_t"); |
| 279 | fprintf(fd, " lttng_param_%s", field_name); |
| 280 | break; |
| 281 | case OFF_T: |
| 282 | fprintf(fd, "off_t"); |
| 283 | fprintf(fd, " lttng_param_%s", field_name); |
| 284 | break; |
| 285 | case STRING: |
| 286 | fprintf(fd, "const char *"); |
| 287 | fprintf(fd, " lttng_param_%s", field_name); |
| 288 | break; |
| 289 | case ENUM: |
| 290 | fprintf(fd, "enum lttng_%s", basename); |
| 291 | fprintf(fd, " lttng_param_%s", field_name); |
| 292 | break; |
| 293 | case ARRAY: |
| 294 | fprintf(fd, "lttng_array_%s", basename); |
| 295 | fprintf(fd, " lttng_param_%s", field_name); |
| 296 | break; |
| 297 | case SEQUENCE: |
| 298 | fprintf(fd, "lttng_sequence_%s *", basename); |
| 299 | fprintf(fd, " lttng_param_%s", field_name); |
| 300 | break; |
| 301 | case STRUCT: |
| 302 | fprintf(fd, "struct lttng_%s *", basename); |
| 303 | fprintf(fd, " lttng_param_%s", field_name); |
| 304 | break; |
| 305 | case UNION: |
| 306 | fprintf(fd, "union lttng_%s *", basename); |
| 307 | fprintf(fd, " lttng_param_%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 | if(td->custom_write) return 0; /* Does print custom type */ |
| 390 | |
| 391 | strncpy(basename, nest_name, PATH_MAX); |
| 392 | basename_len = strlen(basename); |
| 393 | |
| 394 | /* For a named type, we use the type_name directly */ |
| 395 | if(td->type_name != NULL) { |
| 396 | strncpy(basename, td->type_name, PATH_MAX); |
| 397 | basename_len = strlen(basename); |
| 398 | } else { |
| 399 | /* For a unnamed type, there must be a field name, except for |
| 400 | * the array. */ |
| 401 | if((basename_len != 0) |
| 402 | && (basename[basename_len-1] != '_' |
| 403 | && (field_name[0] != '\0'))) { |
| 404 | strncat(basename, "_", PATH_MAX - basename_len); |
| 405 | basename_len = strlen(basename); |
| 406 | } |
| 407 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 408 | } |
| 409 | |
| 410 | switch(td->type) { |
| 411 | case ENUM: |
| 412 | fprintf(fd, "enum lttng_%s", basename); |
| 413 | fprintf(fd, " {\n"); |
| 414 | for(unsigned int i=0;i<td->labels.position;i++){ |
| 415 | print_tabs(1, fd); |
| 416 | fprintf(fd, "LTTNG_%s = %d", ((char*)td->labels.array[i]), |
| 417 | (*(int*)td->labels_values.array[i])); |
| 418 | fprintf(fd, ",\n"); |
| 419 | } |
| 420 | fprintf(fd, "};\n"); |
| 421 | fprintf(fd, "\n"); |
| 422 | break; |
| 423 | |
| 424 | case ARRAY: |
| 425 | dprintf("%s\n", basename); |
| 426 | assert(td->size >= 0); |
| 427 | if(((field_t*)td->fields.array[0])->type->type_name == NULL) { |
| 428 | /* Not a named nested type : we must print its declaration first */ |
| 429 | if(print_type_declaration(((field_t*)td->fields.array[0])->type, |
| 430 | fd, 0, basename, "")) return 1; |
| 431 | } |
| 432 | fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename, |
| 433 | td->size); |
| 434 | fprintf(fd, "typedef "); |
| 435 | if(print_type(((field_t*)td->fields.array[0])->type, |
| 436 | fd, tabs, basename, "")) return 1; |
| 437 | fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename, |
| 438 | basename); |
| 439 | fprintf(fd, "\n"); |
| 440 | break; |
| 441 | case SEQUENCE: |
| 442 | /* We assume that the sequence length type does not need to be declared. |
| 443 | */ |
| 444 | if(((field_t*)td->fields.array[1])->type->type_name == NULL) { |
| 445 | /* Not a named nested type : we must print its declaration first */ |
| 446 | if(print_type_declaration(((field_t*)td->fields.array[1])->type, |
| 447 | fd, 0, basename, "")) return 1; |
| 448 | } |
| 449 | fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n", |
| 450 | basename, |
| 451 | basename); |
| 452 | fprintf(fd, "struct lttng_sequence_%s", basename); |
| 453 | fprintf(fd, " {\n"); |
| 454 | print_tabs(1, fd); |
| 455 | if(print_type(((field_t*)td->fields.array[0])->type, |
| 456 | fd, tabs, basename, "")) return 1; |
| 457 | fprintf(fd, " len;\n"); |
| 458 | print_tabs(1, fd); |
| 459 | fprintf(fd, "const "); |
| 460 | if(print_type(((field_t*)td->fields.array[1])->type, |
| 461 | fd, tabs, basename, "")) return 1; |
| 462 | fprintf(fd, " *array;\n"); |
| 463 | fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy |
| 464 | it to the buffer directly. */ |
| 465 | fprintf(fd, "\n"); |
| 466 | break; |
| 467 | |
| 468 | case STRUCT: |
| 469 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 470 | field_t *field = (field_t*)(td->fields.array[i]); |
| 471 | type_descriptor_t *type = field->type; |
| 472 | if(type->type_name == NULL) { |
| 473 | /* Not a named nested type : we must print its declaration first */ |
| 474 | if(print_type_declaration(type, |
| 475 | fd, 0, basename, field->name)) return 1; |
| 476 | } |
| 477 | } |
| 478 | fprintf(fd, "struct lttng_%s", basename); |
| 479 | fprintf(fd, " {\n"); |
| 480 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 481 | field_t *field = (field_t*)(td->fields.array[i]); |
| 482 | type_descriptor_t *type = field->type; |
| 483 | print_tabs(1, fd); |
| 484 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
| 485 | fprintf(fd, " "); |
| 486 | fprintf(fd, "%s", field->name); |
| 487 | fprintf(fd, ";\n"); |
| 488 | } |
| 489 | fprintf(fd, "} LTT_ALIGN;\n"); |
| 490 | fprintf(fd, "\n"); |
| 491 | break; |
| 492 | case UNION: |
| 493 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 494 | field_t *field = (field_t*)(td->fields.array[i]); |
| 495 | type_descriptor_t *type = field->type; |
| 496 | if(type->type_name == NULL) { |
| 497 | /* Not a named nested type : we must print its declaration first */ |
| 498 | if(print_type_declaration(type, |
| 499 | fd, 0, basename, field->name)) return 1; |
| 500 | } |
| 501 | } |
| 502 | fprintf(fd, "union lttng_%s", basename); |
| 503 | fprintf(fd, " {\n"); |
| 504 | for(unsigned i=0;i<td->fields.position;i++){ |
| 505 | field_t *field = (field_t*)(td->fields.array[i]); |
| 506 | type_descriptor_t *type = field->type; |
| 507 | print_tabs(1, fd); |
| 508 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
| 509 | fprintf(fd, " "); |
| 510 | fprintf(fd, "%s", field->name); |
| 511 | fprintf(fd, ";\n"); |
| 512 | } |
| 513 | fprintf(fd, "} LTT_ALIGN;\n"); |
| 514 | fprintf(fd, "\n"); |
| 515 | break; |
| 516 | default: |
| 517 | dprintf("print_type_declaration : unknown type or nothing to declare.\n"); |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /* print type alignment. |
| 526 | * |
| 527 | * Copied from construct_types_and_fields in LTTV facility.c |
| 528 | * |
| 529 | * basename is the name which identifies the type (along with a prefix |
| 530 | * (possibly)). */ |
| 531 | |
| 532 | int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 533 | char *nest_name, char *field_name, char *obj_prefix) |
| 534 | { |
| 535 | char basename[PATH_MAX]; |
| 536 | unsigned int basename_len = 0; |
| 537 | |
| 538 | strncpy(basename, nest_name, PATH_MAX); |
| 539 | basename_len = strlen(basename); |
| 540 | |
| 541 | /* For a named type, we use the type_name directly */ |
| 542 | if(td->type_name != NULL) { |
| 543 | strncpy(basename, td->type_name, PATH_MAX); |
| 544 | basename_len = strlen(basename); |
| 545 | } else { |
| 546 | /* For a unnamed type, there must be a field name, except for |
| 547 | * the array. */ |
| 548 | if((basename_len != 0) |
| 549 | && (basename[basename_len-1] != '_' |
| 550 | && field_name != NULL |
| 551 | && (field_name[0] != '\0'))) { |
| 552 | strncat(basename, "_", PATH_MAX - basename_len); |
| 553 | basename_len = strlen(basename); |
| 554 | } |
| 555 | if(field_name != NULL) |
| 556 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 557 | } |
| 558 | |
| 559 | if(field_name[0] == '\0') { |
| 560 | /* We are in a write function : it's the obj that we must align. */ |
| 561 | switch(td->type) { |
| 562 | case SEQUENCE: |
| 563 | fprintf(fd, "lttng_get_alignment_sequence_%s(%s)", basename, |
| 564 | obj_prefix); |
| 565 | break; |
| 566 | case STRUCT: |
| 567 | fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename, |
| 568 | obj_prefix); |
| 569 | break; |
| 570 | case UNION: |
| 571 | fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename, |
| 572 | obj_prefix); |
| 573 | break; |
| 574 | case ARRAY: |
| 575 | fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename, |
| 576 | obj_prefix); |
| 577 | case STRING: |
| 578 | fprintf(fd, "sizeof(char)"); |
| 579 | break; |
| 580 | case INT_FIXED: |
| 581 | case UINT_FIXED: |
| 582 | case CHAR: |
| 583 | case UCHAR: |
| 584 | case SHORT: |
| 585 | case USHORT: |
| 586 | case INT: |
| 587 | case UINT: |
| 588 | case FLOAT: |
| 589 | case POINTER: |
| 590 | case LONG: |
| 591 | case ULONG: |
| 592 | case SIZE_T: |
| 593 | case SSIZE_T: |
| 594 | case OFF_T: |
| 595 | case ENUM: |
| 596 | fprintf(fd, "sizeof("); |
| 597 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 598 | fprintf(fd, ")"); |
| 599 | break; |
| 600 | |
| 601 | default: |
| 602 | printf("error : type unexpected\n"); |
| 603 | return 1; |
| 604 | break; |
| 605 | } |
| 606 | } else { |
| 607 | |
| 608 | switch(td->type) { |
| 609 | case INT_FIXED: |
| 610 | case UINT_FIXED: |
| 611 | case CHAR: |
| 612 | case UCHAR: |
| 613 | case SHORT: |
| 614 | case USHORT: |
| 615 | case INT: |
| 616 | case UINT: |
| 617 | case FLOAT: |
| 618 | case POINTER: |
| 619 | case LONG: |
| 620 | case ULONG: |
| 621 | case SIZE_T: |
| 622 | case SSIZE_T: |
| 623 | case OFF_T: |
| 624 | case ENUM: |
| 625 | fprintf(fd, "sizeof("); |
| 626 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 627 | fprintf(fd, ")"); |
| 628 | break; |
| 629 | case STRING: |
| 630 | fprintf(fd, "sizeof(char)"); |
| 631 | break; |
| 632 | case SEQUENCE: |
| 633 | fprintf(fd, "lttng_get_alignment_sequence_%s(&%s%s)", basename, |
| 634 | obj_prefix, field_name); |
| 635 | break; |
| 636 | case STRUCT: |
| 637 | fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename, |
| 638 | obj_prefix, field_name); |
| 639 | break; |
| 640 | case UNION: |
| 641 | fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename, |
| 642 | obj_prefix, field_name); |
| 643 | break; |
| 644 | case ARRAY: |
| 645 | fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename, |
| 646 | obj_prefix, field_name); |
| 647 | break; |
| 648 | case NONE: |
| 649 | printf("error : type NONE unexpected\n"); |
| 650 | return 1; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* print type write. |
| 659 | * |
| 660 | * Copied from construct_types_and_fields in LTTV facility.c |
| 661 | * |
| 662 | * basename is the name which identifies the type (along with a prefix |
| 663 | * (possibly)). */ |
| 664 | |
| 665 | int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 666 | char *nest_name, char *field_name, char *obj_prefix, int get_ptr) |
| 667 | { |
| 668 | char basename[PATH_MAX]; |
| 669 | unsigned int basename_len = 0; |
| 670 | char get_ptr_char[2] = ""; |
| 671 | char custom[PATH_MAX] = ""; |
| 672 | |
| 673 | strncpy(basename, nest_name, PATH_MAX); |
| 674 | basename_len = strlen(basename); |
| 675 | |
| 676 | /* For a named type, we use the type_name directly */ |
| 677 | if(td->type_name != NULL) { |
| 678 | strncpy(basename, td->type_name, PATH_MAX); |
| 679 | basename_len = strlen(basename); |
| 680 | } else { |
| 681 | /* For a unnamed type, there must be a field name, except for |
| 682 | * the array. */ |
| 683 | if((basename_len != 0) |
| 684 | && (basename[basename_len-1] != '_' |
| 685 | && (field_name[0] != '\0'))) { |
| 686 | strncat(basename, "_", PATH_MAX - basename_len); |
| 687 | basename_len = strlen(basename); |
| 688 | } |
| 689 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 690 | } |
| 691 | |
| 692 | if(get_ptr) { |
| 693 | strcpy(get_ptr_char, "&"); |
| 694 | } |
| 695 | |
| 696 | if(td->custom_write) { |
| 697 | strcpy(custom, "_custom"); |
| 698 | } |
| 699 | |
| 700 | switch(td->type) { |
| 701 | case INT_FIXED: |
| 702 | case UINT_FIXED: |
| 703 | case CHAR: |
| 704 | case UCHAR: |
| 705 | case SHORT: |
| 706 | case USHORT: |
| 707 | case INT: |
| 708 | case UINT: |
| 709 | case FLOAT: |
| 710 | case POINTER: |
| 711 | case LONG: |
| 712 | case ULONG: |
| 713 | case SIZE_T: |
| 714 | case SSIZE_T: |
| 715 | case OFF_T: |
| 716 | case ENUM: |
| 717 | print_tabs(tabs, fd); |
| 718 | fprintf(fd, "align = "); |
| 719 | if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1; |
| 720 | fprintf(fd, ";\n"); |
| 721 | fprintf(fd, "\n"); |
| 722 | print_tabs(tabs, fd); |
| 723 | fprintf(fd, "if(*len == 0) {\n"); |
| 724 | print_tabs(tabs+1, fd); |
| 725 | fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n"); |
| 726 | print_tabs(tabs, fd); |
| 727 | fprintf(fd, "} else {\n"); |
| 728 | print_tabs(tabs+1, fd); |
| 729 | fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n"); |
| 730 | print_tabs(tabs, fd); |
| 731 | fprintf(fd, "}\n"); |
| 732 | fprintf(fd, "\n"); |
| 733 | |
| 734 | print_tabs(tabs, fd); |
| 735 | fprintf(fd, "*len += "); |
| 736 | fprintf(fd, "sizeof("); |
| 737 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 738 | fprintf(fd, ");\n"); |
| 739 | |
| 740 | break; |
| 741 | case STRING: |
| 742 | print_tabs(tabs, fd); |
| 743 | fprintf(fd, |
| 744 | "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n", |
| 745 | custom, basename, obj_prefix, field_name); |
| 746 | break; |
| 747 | case SEQUENCE: |
| 748 | print_tabs(tabs, fd); |
| 749 | fprintf(fd, |
| 750 | "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);", |
| 751 | custom, basename, get_ptr_char, obj_prefix, field_name); |
| 752 | break; |
| 753 | case STRUCT: |
| 754 | print_tabs(tabs, fd); |
| 755 | fprintf(fd, |
| 756 | "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);", |
| 757 | custom, basename, get_ptr_char, obj_prefix, field_name); |
| 758 | break; |
| 759 | case UNION: |
| 760 | print_tabs(tabs, fd); |
| 761 | fprintf(fd, |
| 762 | "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);", |
| 763 | custom, basename, get_ptr_char, obj_prefix, field_name); |
| 764 | break; |
| 765 | case ARRAY: |
| 766 | print_tabs(tabs, fd); |
| 767 | fprintf(fd, |
| 768 | "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);", |
| 769 | custom, basename, obj_prefix, field_name); |
| 770 | break; |
| 771 | case NONE: |
| 772 | printf("Error : type NONE unexpected\n"); |
| 773 | return 1; |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* print need local vars ?. |
| 781 | * |
| 782 | * Copied from print_type_write |
| 783 | * |
| 784 | * Does the type_write call needs local size and from variables ? |
| 785 | * return value : 1 yes, 0 no. |
| 786 | */ |
| 787 | |
| 788 | int has_type_local(type_descriptor_t * td) |
| 789 | { |
| 790 | switch(td->type) { |
| 791 | case INT_FIXED: |
| 792 | case UINT_FIXED: |
| 793 | case CHAR: |
| 794 | case UCHAR: |
| 795 | case SHORT: |
| 796 | case USHORT: |
| 797 | case INT: |
| 798 | case UINT: |
| 799 | case FLOAT: |
| 800 | case POINTER: |
| 801 | case LONG: |
| 802 | case ULONG: |
| 803 | case SIZE_T: |
| 804 | case SSIZE_T: |
| 805 | case OFF_T: |
| 806 | case ENUM: |
| 807 | return 1; |
| 808 | break; |
| 809 | case STRING: |
| 810 | case SEQUENCE: |
| 811 | case STRUCT: |
| 812 | case UNION: |
| 813 | case ARRAY: |
| 814 | return 0; |
| 815 | break; |
| 816 | case NONE: |
| 817 | printf("Error : type NONE unexpected\n"); |
| 818 | return 1; |
| 819 | break; |
| 820 | } |
| 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | |
| 827 | /* print type alignment function. |
| 828 | * |
| 829 | * Copied from construct_types_and_fields in LTTV facility.c |
| 830 | * |
| 831 | * basename is the name which identifies the type (along with a prefix |
| 832 | * (possibly)). */ |
| 833 | |
| 834 | int print_type_alignment_fct(type_descriptor_t * td, FILE *fd, |
| 835 | unsigned int tabs, |
| 836 | char *nest_name, char *field_name) |
| 837 | { |
| 838 | char basename[PATH_MAX]; |
| 839 | unsigned int basename_len = 0; |
| 840 | |
| 841 | if(td->custom_write) return 0; /* Does print custom type */ |
| 842 | |
| 843 | strncpy(basename, nest_name, PATH_MAX); |
| 844 | basename_len = strlen(basename); |
| 845 | |
| 846 | /* For a named type, we use the type_name directly */ |
| 847 | if(td->type_name != NULL) { |
| 848 | strncpy(basename, td->type_name, PATH_MAX); |
| 849 | basename_len = strlen(basename); |
| 850 | } else { |
| 851 | /* For a unnamed type, there must be a field name, except for |
| 852 | * the array. */ |
| 853 | if((basename_len != 0) |
| 854 | && (basename[basename_len-1] != '_' |
| 855 | && (field_name[0] != '\0'))) { |
| 856 | strncat(basename, "_", PATH_MAX - basename_len); |
| 857 | basename_len = strlen(basename); |
| 858 | } |
| 859 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 860 | } |
| 861 | |
| 862 | switch(td->type) { |
| 863 | case SEQUENCE: |
| 864 | /* Function header */ |
| 865 | fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n", |
| 866 | basename); |
| 867 | print_tabs(2, fd); |
| 868 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 869 | fprintf(fd, " *obj)\n"); |
| 870 | fprintf(fd, "{\n"); |
| 871 | print_tabs(1, fd); |
| 872 | fprintf(fd, "size_t align=0, localign;"); |
| 873 | fprintf(fd, "\n"); |
| 874 | print_tabs(1, fd); |
| 875 | fprintf(fd, "localign = "); |
| 876 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
| 877 | fd, 0, basename, "len", "obj->")) return 1; |
| 878 | fprintf(fd, ";\n"); |
| 879 | print_tabs(1, fd); |
| 880 | fprintf(fd, "align = max(align, localign);\n"); |
| 881 | fprintf(fd, "\n"); |
| 882 | print_tabs(1, fd); |
| 883 | fprintf(fd, "localign = "); |
| 884 | if(print_type_alignment(((field_t*)td->fields.array[1])->type, |
| 885 | fd, 0, basename, "array[0]", "obj->")) return 1; |
| 886 | fprintf(fd, ";\n"); |
| 887 | print_tabs(1, fd); |
| 888 | fprintf(fd, "align = max(align, localign);\n"); |
| 889 | fprintf(fd, "\n"); |
| 890 | print_tabs(1, fd); |
| 891 | fprintf(fd, "return align;\n"); |
| 892 | break; |
| 893 | case STRUCT: |
| 894 | /* Function header */ |
| 895 | fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n", |
| 896 | basename); |
| 897 | print_tabs(2, fd); |
| 898 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 899 | fprintf(fd, " *obj)\n"); |
| 900 | fprintf(fd, "{\n"); |
| 901 | print_tabs(1, fd); |
| 902 | fprintf(fd, "size_t align=0, localign;"); |
| 903 | fprintf(fd, "\n"); |
| 904 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 905 | field_t *field = (field_t*)(td->fields.array[i]); |
| 906 | type_descriptor_t *type = field->type; |
| 907 | print_tabs(1, fd); |
| 908 | fprintf(fd, "localign = "); |
| 909 | if(print_type_alignment(type, fd, 0, basename, field->name, "obj->")) |
| 910 | return 1; |
| 911 | fprintf(fd, ";\n"); |
| 912 | print_tabs(1, fd); |
| 913 | fprintf(fd, "align = max(align, localign);\n"); |
| 914 | fprintf(fd, "\n"); |
| 915 | } |
| 916 | print_tabs(1, fd); |
| 917 | fprintf(fd, "return align;\n"); |
| 918 | |
| 919 | break; |
| 920 | case UNION: |
| 921 | /* Function header */ |
| 922 | fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n", |
| 923 | basename); |
| 924 | print_tabs(2, fd); |
| 925 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 926 | fprintf(fd, " *obj)\n"); |
| 927 | fprintf(fd, "{\n"); |
| 928 | print_tabs(1, fd); |
| 929 | fprintf(fd, "size_t align=0, localign;"); |
| 930 | fprintf(fd, "\n"); |
| 931 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 932 | field_t *field = (field_t*)(td->fields.array[i]); |
| 933 | type_descriptor_t *type = field->type; |
| 934 | print_tabs(1, fd); |
| 935 | fprintf(fd, "localign = "); |
| 936 | if(print_type_alignment(type, fd, 0, basename, field->name, "obj->")) |
| 937 | return 1; |
| 938 | fprintf(fd, ";\n"); |
| 939 | print_tabs(1, fd); |
| 940 | fprintf(fd, "align = max(align, localign);\n"); |
| 941 | fprintf(fd, "\n"); |
| 942 | } |
| 943 | print_tabs(1, fd); |
| 944 | fprintf(fd, "return align;\n"); |
| 945 | |
| 946 | break; |
| 947 | case ARRAY: |
| 948 | /* Function header */ |
| 949 | fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n", |
| 950 | basename); |
| 951 | print_tabs(2, fd); |
| 952 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 953 | fprintf(fd, " obj)\n"); |
| 954 | fprintf(fd, "{\n"); |
| 955 | print_tabs(1, fd); |
| 956 | fprintf(fd, "return \n"); |
| 957 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
| 958 | fd, 0, basename, "", "obj[0]")) |
| 959 | return 1; |
| 960 | fprintf(fd, ";\n"); |
| 961 | break; |
| 962 | default: |
| 963 | dprintf("print_type_alignment_fct : type has no alignment function.\n"); |
| 964 | return 0; |
| 965 | break; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | /* Function footer */ |
| 970 | fprintf(fd, "}\n"); |
| 971 | fprintf(fd, "\n"); |
| 972 | |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | /* print type write function. |
| 977 | * |
| 978 | * Copied from construct_types_and_fields in LTTV facility.c |
| 979 | * |
| 980 | * basename is the name which identifies the type (along with a prefix |
| 981 | * (possibly)). */ |
| 982 | |
| 983 | int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
| 984 | char *nest_name, char *field_name) |
| 985 | { |
| 986 | char basename[PATH_MAX]; |
| 987 | unsigned int basename_len = 0; |
| 988 | |
| 989 | if(td->custom_write) return 0; /* Does print custom type */ |
| 990 | |
| 991 | strncpy(basename, nest_name, PATH_MAX); |
| 992 | basename_len = strlen(basename); |
| 993 | |
| 994 | /* For a named type, we use the type_name directly */ |
| 995 | if(td->type_name != NULL) { |
| 996 | strncpy(basename, td->type_name, PATH_MAX); |
| 997 | basename_len = strlen(basename); |
| 998 | } else { |
| 999 | /* For a unnamed type, there must be a field name, except for |
| 1000 | * the array. */ |
| 1001 | if((basename_len != 0) |
| 1002 | && (basename[basename_len-1] != '_' |
| 1003 | && (field_name[0] != '\0'))) { |
| 1004 | strncat(basename, "_", PATH_MAX - basename_len); |
| 1005 | basename_len = strlen(basename); |
| 1006 | } |
| 1007 | strncat(basename, field_name, PATH_MAX - basename_len); |
| 1008 | } |
| 1009 | |
| 1010 | switch(td->type) { |
| 1011 | case SEQUENCE: |
| 1012 | case STRUCT: |
| 1013 | case UNION: |
| 1014 | case ARRAY: |
| 1015 | case STRING: |
| 1016 | break; |
| 1017 | default: |
| 1018 | dprintf("print_type_write_fct : type has no write function.\n"); |
| 1019 | return 0; |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | /* Print header */ |
| 1024 | switch(td->type) { |
| 1025 | case SEQUENCE: |
| 1026 | fprintf(fd, "static inline void lttng_write_sequence_%s(\n", |
| 1027 | basename); |
| 1028 | break; |
| 1029 | case STRUCT: |
| 1030 | fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename); |
| 1031 | break; |
| 1032 | case UNION: |
| 1033 | fprintf(fd, "static inline void lttng_write_union_%s(\n", basename); |
| 1034 | break; |
| 1035 | case ARRAY: |
| 1036 | fprintf(fd, "static inline void lttng_write_array_%s(\n", basename); |
| 1037 | break; |
| 1038 | case STRING: |
| 1039 | fprintf(fd, "static inline void lttng_write_string_%s(\n", basename); |
| 1040 | break; |
| 1041 | default: |
| 1042 | printf("print_type_write_fct : type has no write function.\n"); |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | print_tabs(2, fd); |
| 1047 | fprintf(fd, "void *buffer,\n"); |
| 1048 | print_tabs(2, fd); |
| 1049 | fprintf(fd, "size_t *to_base,\n"); |
| 1050 | print_tabs(2, fd); |
| 1051 | fprintf(fd, "size_t *to,\n"); |
| 1052 | print_tabs(2, fd); |
| 1053 | fprintf(fd, "const void **from,\n"); |
| 1054 | print_tabs(2, fd); |
| 1055 | fprintf(fd, "size_t *len,\n"); |
| 1056 | print_tabs(2, fd); |
| 1057 | if(print_type(td, fd, 0, basename, "")) return 1; |
| 1058 | |
| 1059 | switch(td->type) { |
| 1060 | case SEQUENCE: |
| 1061 | fprintf(fd, " *obj)\n"); |
| 1062 | break; |
| 1063 | case STRUCT: |
| 1064 | fprintf(fd, " *obj)\n"); |
| 1065 | break; |
| 1066 | case UNION: |
| 1067 | fprintf(fd, " *obj)\n"); |
| 1068 | break; |
| 1069 | case ARRAY: |
| 1070 | fprintf(fd, " obj)\n"); |
| 1071 | break; |
| 1072 | case STRING: |
| 1073 | fprintf(fd, " obj)\n"); |
| 1074 | break; |
| 1075 | default: |
| 1076 | printf("print_type_write_fct : type has no write function.\n"); |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | fprintf(fd, "{\n"); |
| 1081 | |
| 1082 | switch(td->type) { |
| 1083 | case STRING: |
| 1084 | print_tabs(1, fd); |
| 1085 | fprintf(fd, "size_t size;\n"); |
| 1086 | break; |
| 1087 | default: |
| 1088 | break; |
| 1089 | } |
| 1090 | |
| 1091 | print_tabs(1, fd); |
| 1092 | fprintf(fd, "size_t align;\n"); |
| 1093 | fprintf(fd, "\n"); |
| 1094 | |
| 1095 | switch(td->type) { |
| 1096 | case SEQUENCE: |
| 1097 | case STRING: |
| 1098 | print_tabs(1, fd); |
| 1099 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 1100 | print_tabs(1, fd); |
| 1101 | fprintf(fd, "if(*len != 0) {\n"); |
| 1102 | print_tabs(2, fd); |
| 1103 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1104 | print_tabs(3, fd); |
| 1105 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
| 1106 | print_tabs(1, fd); |
| 1107 | fprintf(fd, "}\n"); |
| 1108 | print_tabs(1, fd); |
| 1109 | fprintf(fd, "*to += *len;\n"); |
| 1110 | print_tabs(1, fd); |
| 1111 | fprintf(fd, "*len = 0;\n"); |
| 1112 | fprintf(fd, "\n"); |
| 1113 | break; |
| 1114 | case STRUCT: |
| 1115 | case UNION: |
| 1116 | case ARRAY: |
| 1117 | break; |
| 1118 | default: |
| 1119 | printf("print_type_write_fct : type has no write function.\n"); |
| 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | print_tabs(1, fd); |
| 1124 | fprintf(fd, "align = "); |
| 1125 | if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1; |
| 1126 | fprintf(fd, ";\n"); |
| 1127 | fprintf(fd, "\n"); |
| 1128 | print_tabs(1, fd); |
| 1129 | fprintf(fd, "if(*len == 0) {\n"); |
| 1130 | print_tabs(2, fd); |
| 1131 | fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n"); |
| 1132 | print_tabs(1, fd); |
| 1133 | fprintf(fd, "} else {\n"); |
| 1134 | print_tabs(2, fd); |
| 1135 | fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n"); |
| 1136 | print_tabs(1, fd); |
| 1137 | fprintf(fd, "}\n"); |
| 1138 | fprintf(fd, "\n"); |
| 1139 | |
| 1140 | /* First, check if the type has a fixed size. If it is the case, then the size |
| 1141 | * to write is know by the compiler : simply use a sizeof() */ |
| 1142 | if(has_type_fixed_size(td)) { |
| 1143 | print_tabs(1, fd); |
| 1144 | fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n"); |
| 1145 | fprintf(fd, "\n"); |
| 1146 | print_tabs(1, fd); |
| 1147 | fprintf(fd, "*len += sizeof("); |
| 1148 | if(print_type(td, fd, 0, basename, field_name)) return 1; |
| 1149 | fprintf(fd, ");\n"); |
| 1150 | } else { |
| 1151 | /* The type contains nested variable size subtypes : |
| 1152 | * we must write field by field. */ |
| 1153 | print_tabs(1, fd); |
| 1154 | fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n"); |
| 1155 | fprintf(fd, "\n"); |
| 1156 | |
| 1157 | switch(td->type) { |
| 1158 | case SEQUENCE: |
| 1159 | print_tabs(1, fd); |
| 1160 | fprintf(fd, "/* Copy members */\n"); |
| 1161 | // print_tabs(1, fd); |
| 1162 | // fprintf(fd, "size = sizeof(\n"); |
| 1163 | if(print_type_write(((field_t*)td->fields.array[0])->type, |
| 1164 | fd, 1, basename, "len", "obj->", 1)) return 1; |
| 1165 | fprintf(fd, "\n"); |
| 1166 | // fprintf(fd, ");\n"); |
| 1167 | // print_tabs(1, fd); |
| 1168 | // fprintf(fd, "*to += ltt_align(*to, size);\n"); |
| 1169 | print_tabs(1, fd); |
| 1170 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1171 | print_tabs(2, fd); |
| 1172 | fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n"); |
| 1173 | print_tabs(1, fd); |
| 1174 | fprintf(fd, "*to += *len;\n"); |
| 1175 | print_tabs(1, fd); |
| 1176 | fprintf(fd, "*len = 0;\n"); |
| 1177 | fprintf(fd, "\n"); |
| 1178 | |
| 1179 | /* Write the child : varlen child or not ? */ |
| 1180 | if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) { |
| 1181 | /* Fixed size len child : use a multiplication of its size */ |
| 1182 | // print_tabs(1, fd); |
| 1183 | // fprintf(fd, "size = sizeof(\n"); |
| 1184 | |
| 1185 | //print_tabs(1, fd); |
| 1186 | /* We know that *len does not contain alignment because of the |
| 1187 | * previous align output. len is always 0 here. */ |
| 1188 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
| 1189 | fd, 1, basename, "array[0]", "obj->", 1)) |
| 1190 | return 1; |
| 1191 | // fprintf(fd, ");\n"); |
| 1192 | fprintf(fd, "\n"); |
| 1193 | print_tabs(1, fd); |
| 1194 | fprintf(fd, "*len = obj->len * (*len);\n"); |
| 1195 | print_tabs(1, fd); |
| 1196 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1197 | print_tabs(2, fd); |
| 1198 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, *len);\n"); |
| 1199 | print_tabs(1, fd); |
| 1200 | fprintf(fd, "*to += *len;\n"); |
| 1201 | print_tabs(1, fd); |
| 1202 | fprintf(fd, "*len = 0;\n"); |
| 1203 | fprintf(fd, "\n"); |
| 1204 | } else { |
| 1205 | print_tabs(1, fd); |
| 1206 | fprintf(fd, "/* Variable length child : iter. */\n"); |
| 1207 | print_tabs(1, fd); |
| 1208 | fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n"); |
| 1209 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
| 1210 | fd, 2, basename, "array[i]", "obj->", 1)) return 1; |
| 1211 | print_tabs(1, fd); |
| 1212 | fprintf(fd, "}\n"); |
| 1213 | } |
| 1214 | fprintf(fd, "\n"); |
| 1215 | print_tabs(1, fd); |
| 1216 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
| 1217 | print_tabs(1, fd); |
| 1218 | fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n"); |
| 1219 | print_tabs(1, fd); |
| 1220 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
| 1221 | print_tabs(1, fd); |
| 1222 | fprintf(fd, "*to = 0;\n"); |
| 1223 | fprintf(fd, "\n"); |
| 1224 | print_tabs(1, fd); |
| 1225 | fprintf(fd, "/* Put source *from just after the C sequence */\n"); |
| 1226 | print_tabs(1, fd); |
| 1227 | fprintf(fd, "*from = obj+1;\n"); |
| 1228 | break; |
| 1229 | case STRING: |
| 1230 | print_tabs(1, fd); |
| 1231 | fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n"); |
| 1232 | print_tabs(1, fd); |
| 1233 | fprintf(fd, "if(buffer != NULL)\n"); |
| 1234 | print_tabs(2, fd); |
| 1235 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n"); |
| 1236 | print_tabs(1, fd); |
| 1237 | fprintf(fd, "*to += size;\n"); |
| 1238 | fprintf(fd, "\n"); |
| 1239 | print_tabs(1, fd); |
| 1240 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
| 1241 | print_tabs(1, fd); |
| 1242 | fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n"); |
| 1243 | print_tabs(1, fd); |
| 1244 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
| 1245 | print_tabs(1, fd); |
| 1246 | fprintf(fd, "*to = 0;\n"); |
| 1247 | fprintf(fd, "\n"); |
| 1248 | print_tabs(1, fd); |
| 1249 | fprintf(fd, "/* Put source *from just after the C string */\n"); |
| 1250 | print_tabs(1, fd); |
| 1251 | fprintf(fd, "*from += size;\n"); |
| 1252 | break; |
| 1253 | case STRUCT: |
| 1254 | for(unsigned int i=0;i<td->fields.position;i++){ |
| 1255 | field_t *field = (field_t*)(td->fields.array[i]); |
| 1256 | type_descriptor_t *type = field->type; |
| 1257 | if(print_type_write(type, |
| 1258 | fd, 1, basename, field->name, "obj->", 1)) return 1; |
| 1259 | fprintf(fd, "\n"); |
| 1260 | } |
| 1261 | break; |
| 1262 | case UNION: |
| 1263 | printf("ERROR : A union CANNOT contain a variable size child.\n"); |
| 1264 | return 1; |
| 1265 | break; |
| 1266 | case ARRAY: |
| 1267 | /* Write the child : varlen child or not ? */ |
| 1268 | if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) { |
| 1269 | /* Error : if an array has a variable size, then its child must also |
| 1270 | * have a variable size. */ |
| 1271 | assert(0); |
| 1272 | } else { |
| 1273 | print_tabs(1, fd); |
| 1274 | fprintf(fd, "/* Variable length child : iter. */\n"); |
| 1275 | print_tabs(1, fd); |
| 1276 | fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename); |
| 1277 | if(print_type_write(((field_t*)td->fields.array[0])->type, |
| 1278 | fd, 2, basename, "", "obj->array[i]", 1)) return 1; |
| 1279 | print_tabs(1, fd); |
| 1280 | fprintf(fd, "}\n"); |
| 1281 | } |
| 1282 | break; |
| 1283 | default: |
| 1284 | printf("print_type_write_fct : type has no write function.\n"); |
| 1285 | break; |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | /* Function footer */ |
| 1293 | fprintf(fd, "}\n"); |
| 1294 | fprintf(fd, "\n"); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | |
| 1300 | /* Print the logging function of an event. This is the core of genevent */ |
| 1301 | int print_event_logging_function(char *basename, facility_t *fac, |
| 1302 | event_t *event, FILE *fd) |
| 1303 | { |
| 1304 | fprintf(fd, "static inline void trace_%s(\n", basename); |
| 1305 | int has_argument = 0; |
| 1306 | int has_type_fixed = 0; |
| 1307 | |
| 1308 | /* Does it support per trace tracing ? */ |
| 1309 | if(event->per_trace) { |
| 1310 | print_tabs(2, fd); |
| 1311 | fprintf(fd, "struct ltt_trace_struct *dest_trace"); |
| 1312 | has_argument = 1; |
| 1313 | } |
| 1314 | |
| 1315 | /* Does it support per tracefile tracing ? */ |
| 1316 | if(event->per_tracefile) { |
| 1317 | if(has_argument) { |
| 1318 | fprintf(fd, ","); |
| 1319 | fprintf(fd, "\n"); |
| 1320 | } |
| 1321 | fprintf(fd, "unsigned int tracefile_index"); |
| 1322 | has_argument = 1; |
| 1323 | } |
| 1324 | |
| 1325 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 1326 | /* For each field, print the function argument */ |
| 1327 | field_t *f = (field_t*)event->fields.array[j]; |
| 1328 | type_descriptor_t *t = f->type; |
| 1329 | if(has_argument) { |
| 1330 | fprintf(fd, ","); |
| 1331 | fprintf(fd, "\n"); |
| 1332 | } |
| 1333 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
| 1334 | has_argument = 1; |
| 1335 | } |
| 1336 | if(!has_argument) { |
| 1337 | print_tabs(2, fd); |
| 1338 | fprintf(fd, "void"); |
| 1339 | } |
| 1340 | fprintf(fd,")\n"); |
| 1341 | fprintf(fd, |
| 1342 | "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n", |
| 1343 | fac->capname); |
| 1344 | fprintf(fd, "{\n"); |
| 1345 | fprintf(fd, "}\n"); |
| 1346 | fprintf(fd,"#else\n"); |
| 1347 | fprintf(fd, "{\n"); |
| 1348 | /* Print the function variables */ |
| 1349 | print_tabs(1, fd); |
| 1350 | fprintf(fd, "unsigned int index;\n"); |
| 1351 | print_tabs(1, fd); |
| 1352 | fprintf(fd, "struct ltt_channel_struct *channel;\n"); |
| 1353 | print_tabs(1, fd); |
| 1354 | fprintf(fd, "struct ltt_trace_struct *trace;\n"); |
| 1355 | print_tabs(1, fd); |
| 1356 | fprintf(fd, "struct rchan_buf *relayfs_buf;\n"); |
| 1357 | print_tabs(1, fd); |
| 1358 | fprintf(fd, "void *buffer = NULL;\n"); |
| 1359 | print_tabs(1, fd); |
| 1360 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
| 1361 | print_tabs(1, fd); |
| 1362 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
| 1363 | print_tabs(1, fd); |
| 1364 | fprintf(fd, "size_t real_to = 0;\n"); |
| 1365 | print_tabs(1, fd); |
| 1366 | fprintf(fd, "size_t *to = &real_to;\n"); |
| 1367 | print_tabs(1, fd); |
| 1368 | fprintf(fd, "size_t real_len = 0;\n"); |
| 1369 | print_tabs(1, fd); |
| 1370 | fprintf(fd, "size_t *len = &real_len;\n"); |
| 1371 | print_tabs(1, fd); |
| 1372 | fprintf(fd, "size_t reserve_size;\n"); |
| 1373 | print_tabs(1, fd); |
| 1374 | fprintf(fd, "size_t slot_size;\n"); |
| 1375 | print_tabs(1, fd); |
| 1376 | |
| 1377 | if(event->fields.position > 0) { |
| 1378 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1379 | /* Search for at least one child with fixed size. It means |
| 1380 | * we need local variables.*/ |
| 1381 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1382 | type_descriptor_t *type = field->type; |
| 1383 | has_type_fixed = has_type_local(type); |
| 1384 | if(has_type_fixed) break; |
| 1385 | } |
| 1386 | |
| 1387 | if(has_type_fixed) { |
| 1388 | fprintf(fd, "size_t align;\n"); |
| 1389 | print_tabs(1, fd); |
| 1390 | } |
| 1391 | |
| 1392 | fprintf(fd, "const void *real_from;\n"); |
| 1393 | print_tabs(1, fd); |
| 1394 | fprintf(fd, "const void **from = &real_from;\n"); |
| 1395 | print_tabs(1, fd); |
| 1396 | } |
| 1397 | fprintf(fd, "u64 tsc;\n"); |
| 1398 | print_tabs(1, fd); |
| 1399 | fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n"); |
| 1400 | fprintf(fd, "\n"); |
| 1401 | |
| 1402 | print_tabs(1, fd); |
| 1403 | fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n"); |
| 1404 | fprintf(fd, "\n"); |
| 1405 | |
| 1406 | /* Calculate event variable len + event data alignment offset. |
| 1407 | * Assume that the padding for alignment starts at a void* |
| 1408 | * address. |
| 1409 | * This excludes the header size and alignment. */ |
| 1410 | |
| 1411 | print_tabs(1, fd); |
| 1412 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
| 1413 | print_tabs(1, fd); |
| 1414 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
| 1415 | print_tabs(1, fd); |
| 1416 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
| 1417 | print_tabs(1, fd); |
| 1418 | fprintf(fd, " * sizeof(void *) address. */\n"); |
| 1419 | fprintf(fd, "\n"); |
| 1420 | |
| 1421 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1422 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1423 | type_descriptor_t *type = field->type; |
| 1424 | /* Set from */ |
| 1425 | print_tabs(1, fd); |
| 1426 | switch(type->type) { |
| 1427 | case SEQUENCE: |
| 1428 | case UNION: |
| 1429 | case ARRAY: |
| 1430 | case STRUCT: |
| 1431 | case STRING: |
| 1432 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 1433 | break; |
| 1434 | default: |
| 1435 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 1436 | break; |
| 1437 | } |
| 1438 | |
| 1439 | if(print_type_write(type, |
| 1440 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
| 1441 | fprintf(fd, "\n"); |
| 1442 | } |
| 1443 | print_tabs(1, fd); |
| 1444 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
| 1445 | |
| 1446 | /* Take locks : make sure the trace does not vanish while we write on |
| 1447 | * it. A simple preemption disabling is enough (using rcu traces). */ |
| 1448 | print_tabs(1, fd); |
| 1449 | fprintf(fd, "preempt_disable();\n"); |
| 1450 | print_tabs(1, fd); |
| 1451 | fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n"); |
| 1452 | |
| 1453 | /* Get facility index */ |
| 1454 | |
| 1455 | if(event->per_tracefile) { |
| 1456 | print_tabs(1, fd); |
| 1457 | fprintf(fd, "index = tracefile_index;\n"); |
| 1458 | } else { |
| 1459 | print_tabs(1, fd); |
| 1460 | fprintf(fd, |
| 1461 | "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\ |
| 1462 | "\t\t\t\t\t\tevent_%s_%s);\n", |
| 1463 | fac->name, fac->checksum, fac->name, event->name); |
| 1464 | } |
| 1465 | fprintf(fd,"\n"); |
| 1466 | |
| 1467 | |
| 1468 | /* For each trace */ |
| 1469 | print_tabs(1, fd); |
| 1470 | fprintf(fd, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n"); |
| 1471 | print_tabs(2, fd); |
| 1472 | fprintf(fd, "if(!trace->active) continue;\n\n"); |
| 1473 | |
| 1474 | if(event->per_trace) { |
| 1475 | print_tabs(2, fd); |
| 1476 | fprintf(fd, "if(dest_trace != trace) continue;\n\n"); |
| 1477 | } |
| 1478 | |
| 1479 | print_tabs(2, fd); |
| 1480 | fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n"); |
| 1481 | print_tabs(2, fd); |
| 1482 | fprintf(fd, "relayfs_buf = channel->rchan->buf[smp_processor_id()];\n"); |
| 1483 | fprintf(fd, "\n"); |
| 1484 | |
| 1485 | |
| 1486 | /* Relay reserve */ |
| 1487 | /* If error, increment event lost counter (done by ltt_reserve_slot) and |
| 1488 | * return */ |
| 1489 | print_tabs(2, fd); |
| 1490 | fprintf(fd, "slot_size = 0;\n"); |
| 1491 | print_tabs(2, fd); |
| 1492 | fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n"); |
| 1493 | print_tabs(3, fd); |
| 1494 | fprintf(fd, "reserve_size, &slot_size, &tsc,\n"); |
| 1495 | print_tabs(3, fd); |
| 1496 | fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n"); |
| 1497 | /* If error, return */ |
| 1498 | print_tabs(2, fd); |
| 1499 | fprintf(fd, "if(!buffer) continue; /* buffer full */\n\n"); |
| 1500 | //print_tabs(2, fd); |
| 1501 | // for DEBUG only |
| 1502 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
| 1503 | print_tabs(2, fd); |
| 1504 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
| 1505 | fprintf(fd, "\n"); |
| 1506 | |
| 1507 | /* Write event header */ |
| 1508 | print_tabs(2, fd); |
| 1509 | fprintf(fd, "ltt_write_event_header(trace, channel, buffer,\n"); |
| 1510 | print_tabs(3, fd); |
| 1511 | fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum, |
| 1512 | fac->name, event->name); |
| 1513 | print_tabs(3, fd); |
| 1514 | fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n"); |
| 1515 | print_tabs(2, fd); |
| 1516 | fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n"); |
| 1517 | fprintf(fd, "\n"); |
| 1518 | |
| 1519 | /* write data. */ |
| 1520 | |
| 1521 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1522 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1523 | type_descriptor_t *type = field->type; |
| 1524 | |
| 1525 | /* Set from */ |
| 1526 | print_tabs(2, fd); |
| 1527 | switch(type->type) { |
| 1528 | case SEQUENCE: |
| 1529 | case UNION: |
| 1530 | case ARRAY: |
| 1531 | case STRUCT: |
| 1532 | case STRING: |
| 1533 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 1534 | break; |
| 1535 | default: |
| 1536 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 1537 | break; |
| 1538 | } |
| 1539 | |
| 1540 | |
| 1541 | if(print_type_write(type, |
| 1542 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
| 1543 | fprintf(fd, "\n"); |
| 1544 | |
| 1545 | /* Don't forget to flush pending memcpy */ |
| 1546 | print_tabs(2, fd); |
| 1547 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 1548 | print_tabs(2, fd); |
| 1549 | fprintf(fd, "if(*len != 0) {\n"); |
| 1550 | print_tabs(3, fd); |
| 1551 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
| 1552 | print_tabs(3, fd); |
| 1553 | fprintf(fd, "*to += *len;\n"); |
| 1554 | //print_tabs(3, fd); |
| 1555 | //fprintf(fd, "from += len;\n"); |
| 1556 | print_tabs(3, fd); |
| 1557 | fprintf(fd, "*len = 0;\n"); |
| 1558 | print_tabs(2, fd); |
| 1559 | fprintf(fd, "}\n"); |
| 1560 | fprintf(fd, "\n"); |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | /* commit */ |
| 1565 | // for DEBUG only. |
| 1566 | //fprintf(fd, "commit:\n"); /* DEBUG! */ |
| 1567 | print_tabs(2, fd); |
| 1568 | fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n"); |
| 1569 | |
| 1570 | print_tabs(1, fd); |
| 1571 | fprintf(fd, "}\n\n"); |
| 1572 | |
| 1573 | /* Release locks */ |
| 1574 | print_tabs(1, fd); |
| 1575 | fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n"); |
| 1576 | print_tabs(1, fd); |
| 1577 | fprintf(fd, "preempt_enable_no_resched();\n"); |
| 1578 | |
| 1579 | fprintf(fd, "}\n"); |
| 1580 | fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n", |
| 1581 | fac->capname); |
| 1582 | return 0; |
| 1583 | } |
| 1584 | |
| 1585 | /* print_event_logging_function_user_generic |
| 1586 | * Print the logging function of an event for userspace tracing. This is the |
| 1587 | * core of genevent */ |
| 1588 | int print_event_logging_function_user_generic(char *basename, facility_t *fac, |
| 1589 | event_t *event, FILE *fd) |
| 1590 | { |
| 1591 | char *attrib; |
| 1592 | |
| 1593 | fprintf(fd, "#ifndef LTT_TRACE_FAST\n"); |
| 1594 | |
| 1595 | if(event->no_instrument_function) { |
| 1596 | attrib = "__attribute__((no_instrument_function)) "; |
| 1597 | } else { |
| 1598 | attrib = ""; |
| 1599 | } |
| 1600 | if(event->param_buffer) { |
| 1601 | fprintf(fd, "static inline %sint trace_%s_param_buffer(\n", attrib, basename); |
| 1602 | } else { |
| 1603 | fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename); |
| 1604 | } |
| 1605 | int has_argument = 0; |
| 1606 | int has_type_fixed = 0; |
| 1607 | |
| 1608 | if(event->param_buffer) { |
| 1609 | if(has_argument) { |
| 1610 | fprintf(fd, ","); |
| 1611 | fprintf(fd, "\n"); |
| 1612 | } |
| 1613 | print_tabs(2, fd); |
| 1614 | fprintf(fd, "void *buffer"); |
| 1615 | has_argument = 1; |
| 1616 | fprintf(fd, ","); |
| 1617 | fprintf(fd, "\n"); |
| 1618 | print_tabs(2, fd); |
| 1619 | fprintf(fd, "size_t reserve_size"); |
| 1620 | } else { |
| 1621 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 1622 | /* For each field, print the function argument */ |
| 1623 | field_t *f = (field_t*)event->fields.array[j]; |
| 1624 | type_descriptor_t *t = f->type; |
| 1625 | if(has_argument) { |
| 1626 | fprintf(fd, ","); |
| 1627 | fprintf(fd, "\n"); |
| 1628 | } |
| 1629 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
| 1630 | has_argument = 1; |
| 1631 | } |
| 1632 | } |
| 1633 | if(!has_argument) { |
| 1634 | print_tabs(2, fd); |
| 1635 | fprintf(fd, "void"); |
| 1636 | } |
| 1637 | fprintf(fd,")\n"); |
| 1638 | fprintf(fd, |
| 1639 | "#ifndef LTT_TRACE\n"); |
| 1640 | fprintf(fd, "{\n"); |
| 1641 | fprintf(fd, "}\n"); |
| 1642 | fprintf(fd,"#else\n"); |
| 1643 | fprintf(fd, "{\n"); |
| 1644 | /* Print the function variables */ |
| 1645 | print_tabs(1, fd); |
| 1646 | fprintf(fd, "int ret = 0;\n"); |
| 1647 | if(event->param_buffer) { |
| 1648 | print_tabs(1, fd); |
| 1649 | fprintf(fd, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n"); |
| 1650 | print_tabs(1, fd); |
| 1651 | fprintf(fd, "{\n"); |
| 1652 | goto do_syscall; |
| 1653 | } |
| 1654 | print_tabs(1, fd); |
| 1655 | fprintf(fd, "void *buffer = NULL;\n"); |
| 1656 | print_tabs(1, fd); |
| 1657 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
| 1658 | print_tabs(1, fd); |
| 1659 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
| 1660 | print_tabs(1, fd); |
| 1661 | fprintf(fd, "size_t real_to = 0;\n"); |
| 1662 | print_tabs(1, fd); |
| 1663 | fprintf(fd, "size_t *to = &real_to;\n"); |
| 1664 | print_tabs(1, fd); |
| 1665 | fprintf(fd, "size_t real_len = 0;\n"); |
| 1666 | print_tabs(1, fd); |
| 1667 | fprintf(fd, "size_t *len = &real_len;\n"); |
| 1668 | print_tabs(1, fd); |
| 1669 | fprintf(fd, "size_t reserve_size;\n"); |
| 1670 | print_tabs(1, fd); |
| 1671 | fprintf(fd, "size_t slot_size;\n"); |
| 1672 | print_tabs(1, fd); |
| 1673 | |
| 1674 | if(event->fields.position > 0) { |
| 1675 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1676 | /* Search for at least one child with fixed size. It means |
| 1677 | * we need local variables.*/ |
| 1678 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1679 | type_descriptor_t *type = field->type; |
| 1680 | has_type_fixed = has_type_local(type); |
| 1681 | if(has_type_fixed) break; |
| 1682 | } |
| 1683 | |
| 1684 | if(has_type_fixed) { |
| 1685 | fprintf(fd, "size_t align;\n"); |
| 1686 | print_tabs(1, fd); |
| 1687 | } |
| 1688 | |
| 1689 | fprintf(fd, "const void *real_from;\n"); |
| 1690 | print_tabs(1, fd); |
| 1691 | fprintf(fd, "const void **from = &real_from;\n"); |
| 1692 | print_tabs(1, fd); |
| 1693 | } |
| 1694 | |
| 1695 | /* Calculate event variable len + event data alignment offset. |
| 1696 | * Assume that the padding for alignment starts at a void* |
| 1697 | * address. |
| 1698 | * This excludes the header size and alignment. */ |
| 1699 | |
| 1700 | print_tabs(1, fd); |
| 1701 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
| 1702 | print_tabs(1, fd); |
| 1703 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
| 1704 | print_tabs(1, fd); |
| 1705 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
| 1706 | print_tabs(1, fd); |
| 1707 | fprintf(fd, " * sizeof(void *) address. */\n"); |
| 1708 | fprintf(fd, "\n"); |
| 1709 | |
| 1710 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1711 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1712 | type_descriptor_t *type = field->type; |
| 1713 | /* Set from */ |
| 1714 | print_tabs(1, fd); |
| 1715 | switch(type->type) { |
| 1716 | case SEQUENCE: |
| 1717 | case UNION: |
| 1718 | case ARRAY: |
| 1719 | case STRUCT: |
| 1720 | case STRING: |
| 1721 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 1722 | break; |
| 1723 | default: |
| 1724 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 1725 | break; |
| 1726 | } |
| 1727 | |
| 1728 | if(print_type_write(type, |
| 1729 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
| 1730 | fprintf(fd, "\n"); |
| 1731 | } |
| 1732 | print_tabs(1, fd); |
| 1733 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
| 1734 | |
| 1735 | print_tabs(1, fd); |
| 1736 | fprintf(fd, "{\n"); |
| 1737 | print_tabs(2, fd); |
| 1738 | fprintf(fd, "char stack_buffer[reserve_size];\n"); |
| 1739 | print_tabs(2, fd); |
| 1740 | fprintf(fd, "buffer = stack_buffer;\n"); |
| 1741 | fprintf(fd, "\n"); |
| 1742 | |
| 1743 | |
| 1744 | //print_tabs(2, fd); |
| 1745 | // for DEBUG only |
| 1746 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
| 1747 | print_tabs(2, fd); |
| 1748 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
| 1749 | fprintf(fd, "\n"); |
| 1750 | |
| 1751 | /* write data. */ |
| 1752 | |
| 1753 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1754 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1755 | type_descriptor_t *type = field->type; |
| 1756 | |
| 1757 | /* Set from */ |
| 1758 | print_tabs(2, fd); |
| 1759 | switch(type->type) { |
| 1760 | case SEQUENCE: |
| 1761 | case UNION: |
| 1762 | case ARRAY: |
| 1763 | case STRUCT: |
| 1764 | case STRING: |
| 1765 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 1766 | break; |
| 1767 | default: |
| 1768 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 1769 | break; |
| 1770 | } |
| 1771 | |
| 1772 | |
| 1773 | if(print_type_write(type, |
| 1774 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
| 1775 | fprintf(fd, "\n"); |
| 1776 | |
| 1777 | /* Don't forget to flush pending memcpy */ |
| 1778 | print_tabs(2, fd); |
| 1779 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 1780 | print_tabs(2, fd); |
| 1781 | fprintf(fd, "if(*len != 0) {\n"); |
| 1782 | print_tabs(3, fd); |
| 1783 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
| 1784 | print_tabs(3, fd); |
| 1785 | fprintf(fd, "*to += *len;\n"); |
| 1786 | //print_tabs(3, fd); |
| 1787 | //fprintf(fd, "from += len;\n"); |
| 1788 | print_tabs(3, fd); |
| 1789 | fprintf(fd, "*len = 0;\n"); |
| 1790 | print_tabs(2, fd); |
| 1791 | fprintf(fd, "}\n"); |
| 1792 | fprintf(fd, "\n"); |
| 1793 | } |
| 1794 | |
| 1795 | do_syscall: |
| 1796 | print_tabs(2, fd); |
| 1797 | fprintf(fd, "ret = ltt_trace_generic(ltt_facility_%s_%X, event_%s_%s, buffer, reserve_size, LTT_BLOCKING);\n", fac->name, fac->checksum, fac->name, event->name); |
| 1798 | |
| 1799 | print_tabs(1, fd); |
| 1800 | fprintf(fd, "}\n\n"); |
| 1801 | |
| 1802 | print_tabs(1, fd); |
| 1803 | fprintf(fd, "return ret;\n\n"); |
| 1804 | |
| 1805 | fprintf(fd, "}\n"); |
| 1806 | fprintf(fd, |
| 1807 | "#endif //LTT_TRACE\n"); |
| 1808 | fprintf(fd, "#endif //!LTT_TRACE_FAST\n\n"); |
| 1809 | |
| 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | /* print_event_logging_function_user_fast |
| 1814 | * Print the logging function of an event for userspace tracing. This is the |
| 1815 | * core of genevent */ |
| 1816 | int print_event_logging_function_user_fast(char *basename, facility_t *fac, |
| 1817 | event_t *event, FILE *fd) |
| 1818 | { |
| 1819 | char *attrib; |
| 1820 | |
| 1821 | fprintf(fd, "#ifdef LTT_TRACE_FAST\n"); |
| 1822 | |
| 1823 | if(event->no_instrument_function) { |
| 1824 | attrib = "__attribute__((no_instrument_function)) "; |
| 1825 | } else { |
| 1826 | attrib = ""; |
| 1827 | } |
| 1828 | fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename); |
| 1829 | |
| 1830 | int has_argument = 0; |
| 1831 | int has_type_fixed = 0; |
| 1832 | |
| 1833 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 1834 | /* For each field, print the function argument */ |
| 1835 | field_t *f = (field_t*)event->fields.array[j]; |
| 1836 | type_descriptor_t *t = f->type; |
| 1837 | if(has_argument) { |
| 1838 | fprintf(fd, ","); |
| 1839 | fprintf(fd, "\n"); |
| 1840 | } |
| 1841 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
| 1842 | has_argument = 1; |
| 1843 | } |
| 1844 | if(!has_argument) { |
| 1845 | print_tabs(2, fd); |
| 1846 | fprintf(fd, "void"); |
| 1847 | } |
| 1848 | fprintf(fd,")\n"); |
| 1849 | fprintf(fd, |
| 1850 | "#ifndef LTT_TRACE\n"); |
| 1851 | fprintf(fd, "{\n"); |
| 1852 | fprintf(fd, "}\n"); |
| 1853 | fprintf(fd,"#else\n"); |
| 1854 | fprintf(fd, "{\n"); |
| 1855 | /* Print the function variables */ |
| 1856 | print_tabs(1, fd); |
| 1857 | fprintf(fd, "unsigned int index;\n"); |
| 1858 | print_tabs(1, fd); |
| 1859 | fprintf(fd, "struct ltt_trace_info *trace = thread_trace_info;\n"); |
| 1860 | print_tabs(1, fd); |
| 1861 | fprintf(fd, "struct ltt_buf *ltt_buf;\n"); |
| 1862 | print_tabs(1, fd); |
| 1863 | fprintf(fd, "void *buffer = NULL;\n"); |
| 1864 | print_tabs(1, fd); |
| 1865 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
| 1866 | print_tabs(1, fd); |
| 1867 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
| 1868 | print_tabs(1, fd); |
| 1869 | fprintf(fd, "size_t real_to = 0;\n"); |
| 1870 | print_tabs(1, fd); |
| 1871 | fprintf(fd, "size_t *to = &real_to;\n"); |
| 1872 | print_tabs(1, fd); |
| 1873 | fprintf(fd, "size_t real_len = 0;\n"); |
| 1874 | print_tabs(1, fd); |
| 1875 | fprintf(fd, "size_t *len = &real_len;\n"); |
| 1876 | print_tabs(1, fd); |
| 1877 | fprintf(fd, "size_t reserve_size;\n"); |
| 1878 | print_tabs(1, fd); |
| 1879 | fprintf(fd, "size_t slot_size;\n"); |
| 1880 | print_tabs(1, fd); |
| 1881 | |
| 1882 | if(event->fields.position > 0) { |
| 1883 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1884 | /* Search for at least one child with fixed size. It means |
| 1885 | * we need local variables.*/ |
| 1886 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1887 | type_descriptor_t *type = field->type; |
| 1888 | has_type_fixed = has_type_local(type); |
| 1889 | if(has_type_fixed) break; |
| 1890 | } |
| 1891 | |
| 1892 | if(has_type_fixed) { |
| 1893 | fprintf(fd, "size_t align;\n"); |
| 1894 | print_tabs(1, fd); |
| 1895 | } |
| 1896 | |
| 1897 | fprintf(fd, "const void *real_from;\n"); |
| 1898 | print_tabs(1, fd); |
| 1899 | fprintf(fd, "const void **from = &real_from;\n"); |
| 1900 | print_tabs(1, fd); |
| 1901 | } |
| 1902 | fprintf(fd, "uint64_t tsc;\n"); |
| 1903 | print_tabs(1, fd); |
| 1904 | fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n"); |
| 1905 | fprintf(fd, "\n"); |
| 1906 | |
| 1907 | print_tabs(1, fd); |
| 1908 | fprintf(fd, "if(!trace) {\n"); |
| 1909 | print_tabs(2, fd); |
| 1910 | fprintf(fd, "ltt_thread_init();\n"); |
| 1911 | print_tabs(2, fd); |
| 1912 | fprintf(fd, "trace = thread_trace_info;\n"); |
| 1913 | print_tabs(1, fd); |
| 1914 | fprintf(fd, "}\n\n"); |
| 1915 | fprintf(fd, "\n"); |
| 1916 | |
| 1917 | /* Calculate event variable len + event data alignment offset. |
| 1918 | * Assume that the padding for alignment starts at a void* |
| 1919 | * address. |
| 1920 | * This excludes the header size and alignment. */ |
| 1921 | |
| 1922 | print_tabs(1, fd); |
| 1923 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
| 1924 | print_tabs(1, fd); |
| 1925 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
| 1926 | print_tabs(1, fd); |
| 1927 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
| 1928 | print_tabs(1, fd); |
| 1929 | fprintf(fd, " * sizeof(void *) address. */\n"); |
| 1930 | fprintf(fd, "\n"); |
| 1931 | |
| 1932 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 1933 | field_t *field = (field_t*)(event->fields.array[i]); |
| 1934 | type_descriptor_t *type = field->type; |
| 1935 | /* Set from */ |
| 1936 | print_tabs(1, fd); |
| 1937 | switch(type->type) { |
| 1938 | case SEQUENCE: |
| 1939 | case UNION: |
| 1940 | case ARRAY: |
| 1941 | case STRUCT: |
| 1942 | case STRING: |
| 1943 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 1944 | break; |
| 1945 | default: |
| 1946 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 1947 | break; |
| 1948 | } |
| 1949 | |
| 1950 | if(print_type_write(type, |
| 1951 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
| 1952 | fprintf(fd, "\n"); |
| 1953 | } |
| 1954 | print_tabs(1, fd); |
| 1955 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
| 1956 | |
| 1957 | print_tabs(1, fd); |
| 1958 | fprintf(fd, "trace->nesting++;\n"); |
| 1959 | |
| 1960 | /* Get facility index */ |
| 1961 | |
| 1962 | print_tabs(1, fd); |
| 1963 | fprintf(fd, |
| 1964 | "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\ |
| 1965 | "\t\t\t\t\t\tevent_%s_%s);\n", |
| 1966 | fac->name, fac->checksum, fac->name, event->name); |
| 1967 | fprintf(fd,"\n"); |
| 1968 | |
| 1969 | |
| 1970 | print_tabs(1, fd); |
| 1971 | fprintf(fd, "{\n"); |
| 1972 | |
| 1973 | if(event->per_trace) { |
| 1974 | print_tabs(2, fd); |
| 1975 | fprintf(fd, "if(dest_trace != trace) continue;\n\n"); |
| 1976 | } |
| 1977 | |
| 1978 | print_tabs(2, fd); |
| 1979 | fprintf(fd, "ltt_buf = ltt_get_channel_from_index(trace, index);\n"); |
| 1980 | print_tabs(2, fd); |
| 1981 | |
| 1982 | |
| 1983 | /* Relay reserve */ |
| 1984 | /* If error, increment event lost counter (done by ltt_reserve_slot) and |
| 1985 | * return */ |
| 1986 | print_tabs(2, fd); |
| 1987 | fprintf(fd, "slot_size = 0;\n"); |
| 1988 | print_tabs(2, fd); |
| 1989 | fprintf(fd, "buffer = ltt_reserve_slot(trace, ltt_buf,\n"); |
| 1990 | print_tabs(3, fd); |
| 1991 | fprintf(fd, "reserve_size, &slot_size, &tsc,\n"); |
| 1992 | print_tabs(3, fd); |
| 1993 | fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n"); |
| 1994 | /* If error, return */ |
| 1995 | print_tabs(2, fd); |
| 1996 | fprintf(fd, "if(!buffer) goto end; /* buffer full */\n\n"); |
| 1997 | //print_tabs(2, fd); |
| 1998 | // for DEBUG only |
| 1999 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
| 2000 | print_tabs(2, fd); |
| 2001 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
| 2002 | fprintf(fd, "\n"); |
| 2003 | |
| 2004 | /* Write event header */ |
| 2005 | print_tabs(2, fd); |
| 2006 | fprintf(fd, "ltt_write_event_header(trace, ltt_buf, buffer,\n"); |
| 2007 | print_tabs(3, fd); |
| 2008 | fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum, |
| 2009 | fac->name, event->name); |
| 2010 | print_tabs(3, fd); |
| 2011 | fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n"); |
| 2012 | print_tabs(2, fd); |
| 2013 | fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n"); |
| 2014 | fprintf(fd, "\n"); |
| 2015 | |
| 2016 | /* write data. */ |
| 2017 | |
| 2018 | for(unsigned int i=0;i<event->fields.position;i++){ |
| 2019 | field_t *field = (field_t*)(event->fields.array[i]); |
| 2020 | type_descriptor_t *type = field->type; |
| 2021 | |
| 2022 | /* Set from */ |
| 2023 | print_tabs(2, fd); |
| 2024 | switch(type->type) { |
| 2025 | case SEQUENCE: |
| 2026 | case UNION: |
| 2027 | case ARRAY: |
| 2028 | case STRUCT: |
| 2029 | case STRING: |
| 2030 | fprintf(fd, "*from = lttng_param_%s;\n", field->name); |
| 2031 | break; |
| 2032 | default: |
| 2033 | fprintf(fd, "*from = <tng_param_%s;\n", field->name); |
| 2034 | break; |
| 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | if(print_type_write(type, |
| 2039 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
| 2040 | fprintf(fd, "\n"); |
| 2041 | |
| 2042 | /* Don't forget to flush pending memcpy */ |
| 2043 | print_tabs(2, fd); |
| 2044 | fprintf(fd, "/* Flush pending memcpy */\n"); |
| 2045 | print_tabs(2, fd); |
| 2046 | fprintf(fd, "if(*len != 0) {\n"); |
| 2047 | print_tabs(3, fd); |
| 2048 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
| 2049 | print_tabs(3, fd); |
| 2050 | fprintf(fd, "*to += *len;\n"); |
| 2051 | //print_tabs(3, fd); |
| 2052 | //fprintf(fd, "from += len;\n"); |
| 2053 | print_tabs(3, fd); |
| 2054 | fprintf(fd, "*len = 0;\n"); |
| 2055 | print_tabs(2, fd); |
| 2056 | fprintf(fd, "}\n"); |
| 2057 | fprintf(fd, "\n"); |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | /* commit */ |
| 2062 | // for DEBUG only. |
| 2063 | //fprintf(fd, "commit:\n"); /* DEBUG! */ |
| 2064 | print_tabs(2, fd); |
| 2065 | fprintf(fd, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n"); |
| 2066 | |
| 2067 | fprintf(fd, "}\n\n"); |
| 2068 | |
| 2069 | fprintf(fd, "end:\n"); |
| 2070 | /* Release locks */ |
| 2071 | print_tabs(1, fd); |
| 2072 | fprintf(fd, "trace->nesting--;\n"); |
| 2073 | |
| 2074 | |
| 2075 | fprintf(fd, "}\n"); |
| 2076 | fprintf(fd, |
| 2077 | "#endif //LTT_TRACE\n"); |
| 2078 | fprintf(fd, "#endif //LTT_TRACE_FAST\n"); |
| 2079 | |
| 2080 | return 0; |
| 2081 | } |
| 2082 | |
| 2083 | |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | /* ltt-facility-name.h : main logging header. |
| 2089 | * log_header */ |
| 2090 | |
| 2091 | void print_log_header_head(facility_t *fac, FILE *fd) |
| 2092 | { |
| 2093 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
| 2094 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
| 2095 | fprintf(fd, "#include <linux/types.h>\n"); |
| 2096 | if(!fac->arch) |
| 2097 | fprintf(fd, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac->name); |
| 2098 | else |
| 2099 | fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n", |
| 2100 | fac->name, |
| 2101 | fac->arch); |
| 2102 | fprintf(fd, "#include <linux/ltt-core.h>\n"); |
| 2103 | fprintf(fd, "\n"); |
| 2104 | } |
| 2105 | |
| 2106 | /* ltt-facility-name.h : main logging header. |
| 2107 | * log_header */ |
| 2108 | |
| 2109 | void print_log_header_head_user(facility_t *fac, FILE *fd) |
| 2110 | { |
| 2111 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
| 2112 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
| 2113 | fprintf(fd, "#include <sys/types.h>\n"); |
| 2114 | if(!fac->arch) |
| 2115 | fprintf(fd, "#include <ltt/ltt-facility-id-%s.h>\n", fac->name); |
| 2116 | else |
| 2117 | fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n", |
| 2118 | fac->name, |
| 2119 | fac->arch); |
| 2120 | fprintf(fd, "#include <ltt/ltt-usertrace.h>\n"); |
| 2121 | fprintf(fd, "\n"); |
| 2122 | } |
| 2123 | |
| 2124 | |
| 2125 | int print_log_header_types(facility_t *fac, FILE *fd) |
| 2126 | { |
| 2127 | sequence_t *types = &fac->named_types.values; |
| 2128 | fprintf(fd, "/* Named types */\n"); |
| 2129 | fprintf(fd, "\n"); |
| 2130 | |
| 2131 | for(unsigned int i = 0; i < types->position; i++) { |
| 2132 | /* For each named type, print the definition */ |
| 2133 | if(print_type_declaration(types->array[i], fd, |
| 2134 | 0, "", "")) return 1; |
| 2135 | /* Print also the align function */ |
| 2136 | if(print_type_alignment_fct(types->array[i], fd, |
| 2137 | 0, "", "")) return 1; |
| 2138 | /* Print also the write function */ |
| 2139 | if(print_type_write_fct(types->array[i], fd, |
| 2140 | 0, "", "")) return 1; |
| 2141 | } |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | int print_log_header_events(facility_t *fac, FILE *fd) |
| 2146 | { |
| 2147 | sequence_t *events = &fac->events; |
| 2148 | char basename[PATH_MAX]; |
| 2149 | unsigned int facname_len; |
| 2150 | |
| 2151 | strncpy(basename, fac->name, PATH_MAX); |
| 2152 | facname_len = strlen(basename); |
| 2153 | strncat(basename, "_", PATH_MAX-facname_len); |
| 2154 | facname_len = strlen(basename); |
| 2155 | |
| 2156 | for(unsigned int i = 0; i < events->position; i++) { |
| 2157 | event_t *event = (event_t*)events->array[i]; |
| 2158 | strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len); |
| 2159 | |
| 2160 | /* For each event, print structure, and then logging function */ |
| 2161 | fprintf(fd, "/* Event %s structures */\n", |
| 2162 | event->name); |
| 2163 | for(unsigned int j = 0; j < event->fields.position; j++) { |
| 2164 | /* For each unnamed type, print the definition */ |
| 2165 | field_t *f = (field_t*)event->fields.array[j]; |
| 2166 | type_descriptor_t *t = f->type; |
| 2167 | if(t->type_name == NULL) { |
| 2168 | if((print_type_declaration(t, fd, 0, basename, f->name))) return 1; |
| 2169 | /* Print also the align function */ |
| 2170 | if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1; |
| 2171 | /* Print also the write function */ |
| 2172 | if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1; |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | fprintf(fd, "\n"); |
| 2177 | |
| 2178 | fprintf(fd, "/* Event %s logging function */\n", |
| 2179 | event->name); |
| 2180 | |
| 2181 | if(!fac->user) { |
| 2182 | if(print_event_logging_function(basename, fac, event, fd)) return 1; |
| 2183 | } else { |
| 2184 | if(print_event_logging_function_user_generic(basename, fac, event, fd)) |
| 2185 | return 1; |
| 2186 | if(print_event_logging_function_user_fast(basename, fac, event, fd)) |
| 2187 | return 1; |
| 2188 | } |
| 2189 | |
| 2190 | fprintf(fd, "\n"); |
| 2191 | } |
| 2192 | |
| 2193 | return 0; |
| 2194 | } |
| 2195 | |
| 2196 | |
| 2197 | void print_log_header_tail(facility_t *fac, FILE *fd) |
| 2198 | { |
| 2199 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
| 2200 | } |
| 2201 | |
| 2202 | void print_log_header_tail_user(facility_t *fac, FILE *fd) |
| 2203 | { |
| 2204 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
| 2205 | } |
| 2206 | |
| 2207 | int print_log_header(facility_t *fac) |
| 2208 | { |
| 2209 | char filename[PATH_MAX]; |
| 2210 | unsigned int filename_size = 0; |
| 2211 | FILE *fd; |
| 2212 | dprintf("%s\n", fac->name); |
| 2213 | |
| 2214 | strcpy(filename, "ltt-facility-"); |
| 2215 | filename_size = strlen(filename); |
| 2216 | |
| 2217 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2218 | filename_size = strlen(filename); |
| 2219 | |
| 2220 | if(fac->arch) { |
| 2221 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2222 | filename_size = strlen(filename); |
| 2223 | |
| 2224 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2225 | filename_size = strlen(filename); |
| 2226 | } |
| 2227 | |
| 2228 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 2229 | filename_size = strlen(filename); |
| 2230 | |
| 2231 | |
| 2232 | fd = fopen(filename, "w"); |
| 2233 | if(fd == NULL) { |
| 2234 | printf("Error opening file %s for writing : %s\n", |
| 2235 | filename, strerror(errno)); |
| 2236 | return errno; |
| 2237 | } |
| 2238 | |
| 2239 | /* Print file head */ |
| 2240 | if(!fac->user) |
| 2241 | print_log_header_head(fac, fd); |
| 2242 | else |
| 2243 | print_log_header_head_user(fac, fd); |
| 2244 | |
| 2245 | /* print named types in declaration order */ |
| 2246 | if(print_log_header_types(fac, fd)) return 1; |
| 2247 | |
| 2248 | /* Print events */ |
| 2249 | if(print_log_header_events(fac, fd)) return 1; |
| 2250 | |
| 2251 | /* Print file tail */ |
| 2252 | if(!fac->user) |
| 2253 | print_log_header_tail(fac, fd); |
| 2254 | else |
| 2255 | print_log_header_tail_user(fac, fd); |
| 2256 | |
| 2257 | |
| 2258 | |
| 2259 | fclose(fd); |
| 2260 | |
| 2261 | return 0; |
| 2262 | } |
| 2263 | |
| 2264 | |
| 2265 | /* ltt-facility-id-name.h : facility id. |
| 2266 | * log_id_header */ |
| 2267 | int print_id_header(facility_t *fac) |
| 2268 | { |
| 2269 | char filename[PATH_MAX]; |
| 2270 | unsigned int filename_size = 0; |
| 2271 | FILE *fd; |
| 2272 | char basename[PATH_MAX]; |
| 2273 | char basename_len = 0; |
| 2274 | |
| 2275 | dprintf("%s\n", fac->name); |
| 2276 | |
| 2277 | strcpy(filename, "ltt-facility-id-"); |
| 2278 | filename_size = strlen(filename); |
| 2279 | |
| 2280 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2281 | filename_size = strlen(filename); |
| 2282 | |
| 2283 | if(fac->arch) { |
| 2284 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2285 | filename_size = strlen(filename); |
| 2286 | |
| 2287 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2288 | filename_size = strlen(filename); |
| 2289 | } |
| 2290 | |
| 2291 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 2292 | filename_size = strlen(filename); |
| 2293 | |
| 2294 | |
| 2295 | fd = fopen(filename, "w"); |
| 2296 | if(fd == NULL) { |
| 2297 | printf("Error opening file %s for writing : %s\n", |
| 2298 | filename, strerror(errno)); |
| 2299 | return errno; |
| 2300 | } |
| 2301 | |
| 2302 | if(!fac->user) { |
| 2303 | fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 2304 | fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname); |
| 2305 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
| 2306 | |
| 2307 | fprintf(fd,"#include <linux/ltt-facilities.h>\n\n"); |
| 2308 | |
| 2309 | fprintf(fd,"/**** facility handle ****/\n\n"); |
| 2310 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n", |
| 2311 | fac->name, fac->checksum); |
| 2312 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name); |
| 2313 | |
| 2314 | strncpy(basename, fac->name, PATH_MAX); |
| 2315 | basename_len = strlen(basename); |
| 2316 | strncat(basename, "_", PATH_MAX - basename_len); |
| 2317 | basename_len++; |
| 2318 | |
| 2319 | fprintf(fd,"/**** event index ****/\n\n"); |
| 2320 | fprintf(fd,"enum %s_event {\n",fac->name); |
| 2321 | |
| 2322 | for(unsigned int i = 0; i < fac->events.position; i++) { |
| 2323 | event_t *event = (event_t*)fac->events.array[i]; |
| 2324 | strncpy(basename+basename_len, event->name, PATH_MAX-basename_len); |
| 2325 | print_tabs(1, fd); |
| 2326 | fprintf(fd, "event_%s,\n", basename); |
| 2327 | } |
| 2328 | print_tabs(1, fd); |
| 2329 | fprintf(fd, "facility_%s_num_events\n", fac->name); |
| 2330 | fprintf(fd, "};\n"); |
| 2331 | fprintf(fd, "\n"); |
| 2332 | |
| 2333 | |
| 2334 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
| 2335 | fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 2336 | } else { |
| 2337 | fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 2338 | fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname); |
| 2339 | fprintf(fd, "#ifdef LTT_TRACE\n"); |
| 2340 | |
| 2341 | fprintf(fd,"#include <ltt/ltt-usertrace.h>\n\n"); |
| 2342 | |
| 2343 | fprintf(fd,"/**** facility handle ****/\n\n"); |
| 2344 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n", |
| 2345 | fac->name, fac->checksum); |
| 2346 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name); |
| 2347 | |
| 2348 | strncpy(basename, fac->name, PATH_MAX); |
| 2349 | basename_len = strlen(basename); |
| 2350 | strncat(basename, "_", PATH_MAX - basename_len); |
| 2351 | basename_len++; |
| 2352 | |
| 2353 | fprintf(fd,"/**** event index ****/\n\n"); |
| 2354 | fprintf(fd,"enum %s_event {\n",fac->name); |
| 2355 | |
| 2356 | for(unsigned int i = 0; i < fac->events.position; i++) { |
| 2357 | event_t *event = (event_t*)fac->events.array[i]; |
| 2358 | strncpy(basename+basename_len, event->name, PATH_MAX-basename_len); |
| 2359 | print_tabs(1, fd); |
| 2360 | fprintf(fd, "event_%s,\n", basename); |
| 2361 | } |
| 2362 | print_tabs(1, fd); |
| 2363 | fprintf(fd, "facility_%s_num_events\n", fac->name); |
| 2364 | fprintf(fd, "};\n"); |
| 2365 | fprintf(fd, "\n"); |
| 2366 | |
| 2367 | |
| 2368 | fprintf(fd, "#endif //LTT_TRACE\n"); |
| 2369 | fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname); |
| 2370 | } |
| 2371 | |
| 2372 | |
| 2373 | fclose(fd); |
| 2374 | |
| 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | |
| 2379 | /* ltt-facility-loader-name.h : facility specific loader info. |
| 2380 | * loader_header */ |
| 2381 | int print_loader_header(facility_t *fac) |
| 2382 | { |
| 2383 | char filename[PATH_MAX]; |
| 2384 | unsigned int filename_size = 0; |
| 2385 | FILE *fd; |
| 2386 | dprintf("%s\n", fac->name); |
| 2387 | |
| 2388 | strcpy(filename, "ltt-facility-loader-"); |
| 2389 | filename_size = strlen(filename); |
| 2390 | |
| 2391 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2392 | filename_size = strlen(filename); |
| 2393 | |
| 2394 | if(fac->arch) { |
| 2395 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2396 | filename_size = strlen(filename); |
| 2397 | |
| 2398 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2399 | filename_size = strlen(filename); |
| 2400 | } |
| 2401 | |
| 2402 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 2403 | filename_size = strlen(filename); |
| 2404 | |
| 2405 | |
| 2406 | fd = fopen(filename, "w"); |
| 2407 | if(fd == NULL) { |
| 2408 | printf("Error opening file %s for writing : %s\n", |
| 2409 | filename, strerror(errno)); |
| 2410 | return errno; |
| 2411 | } |
| 2412 | |
| 2413 | fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 2414 | fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname); |
| 2415 | fprintf(fd, "#ifdef CONFIG_LTT\n\n"); |
| 2416 | fprintf(fd,"#include <linux/ltt-facilities.h>\n"); |
| 2417 | if(!fac->arch) |
| 2418 | fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n", |
| 2419 | fac->name); |
| 2420 | else |
| 2421 | fprintf(fd,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n", |
| 2422 | fac->name, |
| 2423 | fac->arch); |
| 2424 | fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name); |
| 2425 | fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n", |
| 2426 | fac->name, fac->checksum); |
| 2427 | |
| 2428 | fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n", |
| 2429 | fac->name); |
| 2430 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n", |
| 2431 | fac->name, fac->checksum); |
| 2432 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum); |
| 2433 | fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name); |
| 2434 | fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n", |
| 2435 | fac->name); |
| 2436 | fprintf(fd, "#endif //CONFIG_LTT\n\n"); |
| 2437 | fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 2438 | |
| 2439 | fclose(fd); |
| 2440 | |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | int print_loader_header_user(facility_t *fac) |
| 2445 | { |
| 2446 | char filename[PATH_MAX]; |
| 2447 | unsigned int filename_size = 0; |
| 2448 | FILE *fd; |
| 2449 | dprintf("%s\n", fac->name); |
| 2450 | |
| 2451 | strcpy(filename, "ltt-facility-loader-"); |
| 2452 | filename_size = strlen(filename); |
| 2453 | |
| 2454 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2455 | filename_size = strlen(filename); |
| 2456 | |
| 2457 | if(fac->arch) { |
| 2458 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2459 | filename_size = strlen(filename); |
| 2460 | |
| 2461 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2462 | filename_size = strlen(filename); |
| 2463 | } |
| 2464 | |
| 2465 | strncat(filename, ".h", PATH_MAX - filename_size); |
| 2466 | filename_size = strlen(filename); |
| 2467 | |
| 2468 | |
| 2469 | fd = fopen(filename, "w"); |
| 2470 | if(fd == NULL) { |
| 2471 | printf("Error opening file %s for writing : %s\n", |
| 2472 | filename, strerror(errno)); |
| 2473 | return errno; |
| 2474 | } |
| 2475 | |
| 2476 | fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 2477 | fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname); |
| 2478 | fprintf(fd,"#include <ltt/ltt-usertrace.h>\n"); |
| 2479 | if(!fac->arch) |
| 2480 | fprintf(fd,"#include <ltt/ltt-facility-id-%s.h>\n\n", |
| 2481 | fac->name); |
| 2482 | else |
| 2483 | fprintf(fd,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n", |
| 2484 | fac->name, |
| 2485 | fac->arch); |
| 2486 | fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name); |
| 2487 | fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n", |
| 2488 | fac->name, fac->checksum); |
| 2489 | |
| 2490 | fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n", |
| 2491 | fac->name); |
| 2492 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n", |
| 2493 | fac->name, fac->checksum); |
| 2494 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum); |
| 2495 | fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name); |
| 2496 | fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n", |
| 2497 | fac->name); |
| 2498 | fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
| 2499 | |
| 2500 | fclose(fd); |
| 2501 | |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
| 2505 | |
| 2506 | |
| 2507 | /* ltt-facility-loader-name.c : generic facility loader |
| 2508 | * loader_c */ |
| 2509 | int print_loader_c(facility_t *fac) |
| 2510 | { |
| 2511 | char filename[PATH_MAX]; |
| 2512 | unsigned int filename_size = 0; |
| 2513 | FILE *fd; |
| 2514 | dprintf("%s\n", fac->name); |
| 2515 | |
| 2516 | strcpy(filename, "ltt-facility-loader-"); |
| 2517 | filename_size = strlen(filename); |
| 2518 | |
| 2519 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2520 | filename_size = strlen(filename); |
| 2521 | |
| 2522 | if(fac->arch) { |
| 2523 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2524 | filename_size = strlen(filename); |
| 2525 | |
| 2526 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2527 | filename_size = strlen(filename); |
| 2528 | } |
| 2529 | |
| 2530 | strncat(filename, ".c", PATH_MAX - filename_size); |
| 2531 | filename_size = strlen(filename); |
| 2532 | |
| 2533 | |
| 2534 | fd = fopen(filename, "w"); |
| 2535 | if(fd == NULL) { |
| 2536 | printf("Error opening file %s for writing : %s\n", |
| 2537 | filename, strerror(errno)); |
| 2538 | return errno; |
| 2539 | } |
| 2540 | |
| 2541 | fprintf(fd, "/*\n"); |
| 2542 | if(!fac->arch) |
| 2543 | fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name); |
| 2544 | else |
| 2545 | fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch); |
| 2546 | fprintf(fd, " *\n"); |
| 2547 | fprintf(fd, " * (C) Copyright 2005 - \n"); |
| 2548 | fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n"); |
| 2549 | fprintf(fd, " *\n"); |
| 2550 | fprintf(fd, " * Contains the LTT facility loader.\n"); |
| 2551 | fprintf(fd, " *\n"); |
| 2552 | fprintf(fd, " */\n"); |
| 2553 | fprintf(fd, "\n"); |
| 2554 | fprintf(fd, "\n"); |
| 2555 | fprintf(fd, "#include <linux/ltt-facilities.h>\n"); |
| 2556 | fprintf(fd, "#include <linux/module.h>\n"); |
| 2557 | fprintf(fd, "#include <linux/init.h>\n"); |
| 2558 | fprintf(fd, "#include <linux/config.h>\n"); |
| 2559 | if(!fac->arch) |
| 2560 | fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name); |
| 2561 | else |
| 2562 | fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n", |
| 2563 | fac->name, fac->arch); |
| 2564 | fprintf(fd, "\n"); |
| 2565 | fprintf(fd, "\n"); |
| 2566 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
| 2567 | fprintf(fd, "\n"); |
| 2568 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n"); |
| 2569 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n"); |
| 2570 | fprintf(fd, "\n"); |
| 2571 | fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n"); |
| 2572 | fprintf(fd, "\n"); |
| 2573 | fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n"); |
| 2574 | fprintf(fd, "\n"); |
| 2575 | fprintf(fd, "static struct ltt_facility facility = {\n"); |
| 2576 | fprintf(fd, "\t.name = ltt_facility_name,\n"); |
| 2577 | fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n"); |
| 2578 | fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n"); |
| 2579 | fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n"); |
| 2580 | fprintf(fd, "};\n"); |
| 2581 | fprintf(fd, "\n"); |
| 2582 | fprintf(fd, "static int __init facility_init(void)\n"); |
| 2583 | fprintf(fd, "{\n"); |
| 2584 | fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name); |
| 2585 | fprintf(fd, "\n"); |
| 2586 | fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n"); |
| 2587 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
| 2588 | fprintf(fd, "\t\n"); |
| 2589 | fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n"); |
| 2590 | fprintf(fd, "}\n"); |
| 2591 | fprintf(fd, "__initcall(facility_init);\n"); |
| 2592 | fprintf(fd, "\n"); |
| 2593 | fprintf(fd, "\n"); |
| 2594 | fprintf(fd, "#ifndef MODULE\n"); |
| 2595 | fprintf(fd, "\n"); |
| 2596 | fprintf(fd, "static void __exit facility_exit(void)\n"); |
| 2597 | fprintf(fd, "{\n"); |
| 2598 | fprintf(fd, "\tint err;\n"); |
| 2599 | fprintf(fd, "\n"); |
| 2600 | fprintf(fd, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n"); |
| 2601 | fprintf(fd, "\tif(err != 0)\n"); |
| 2602 | fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n"); |
| 2603 | fprintf(fd, "\n"); |
| 2604 | fprintf(fd, "}\n"); |
| 2605 | fprintf(fd, "\n"); |
| 2606 | fprintf(fd, "module_exit(facility_exit)\n"); |
| 2607 | fprintf(fd, "\n"); |
| 2608 | fprintf(fd, "\n"); |
| 2609 | fprintf(fd, "MODULE_LICENSE(\"GPL\");\n"); |
| 2610 | fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n"); |
| 2611 | fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n"); |
| 2612 | fprintf(fd, "\n"); |
| 2613 | fprintf(fd, "#endif //MODULE\n"); |
| 2614 | fprintf(fd, "\n"); |
| 2615 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
| 2616 | |
| 2617 | fclose(fd); |
| 2618 | |
| 2619 | return 0; |
| 2620 | } |
| 2621 | |
| 2622 | int print_loader_c_user(facility_t *fac) |
| 2623 | { |
| 2624 | char filename[PATH_MAX]; |
| 2625 | unsigned int filename_size = 0; |
| 2626 | FILE *fd; |
| 2627 | dprintf("%s\n", fac->name); |
| 2628 | |
| 2629 | strcpy(filename, "ltt-facility-loader-"); |
| 2630 | filename_size = strlen(filename); |
| 2631 | |
| 2632 | strncat(filename, fac->name, PATH_MAX - filename_size); |
| 2633 | filename_size = strlen(filename); |
| 2634 | |
| 2635 | if(fac->arch) { |
| 2636 | strncat(filename, "_", PATH_MAX - filename_size); |
| 2637 | filename_size = strlen(filename); |
| 2638 | |
| 2639 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
| 2640 | filename_size = strlen(filename); |
| 2641 | } |
| 2642 | |
| 2643 | strncat(filename, ".c", PATH_MAX - filename_size); |
| 2644 | filename_size = strlen(filename); |
| 2645 | |
| 2646 | |
| 2647 | fd = fopen(filename, "w"); |
| 2648 | if(fd == NULL) { |
| 2649 | printf("Error opening file %s for writing : %s\n", |
| 2650 | filename, strerror(errno)); |
| 2651 | return errno; |
| 2652 | } |
| 2653 | |
| 2654 | fprintf(fd, "/*\n"); |
| 2655 | if(!fac->arch) |
| 2656 | fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name); |
| 2657 | else |
| 2658 | fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch); |
| 2659 | fprintf(fd, " *\n"); |
| 2660 | fprintf(fd, " * (C) Copyright 2005 - \n"); |
| 2661 | fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n"); |
| 2662 | fprintf(fd, " *\n"); |
| 2663 | fprintf(fd, " * Contains the LTT user space facility loader.\n"); |
| 2664 | fprintf(fd, " *\n"); |
| 2665 | fprintf(fd, " */\n"); |
| 2666 | fprintf(fd, "\n"); |
| 2667 | fprintf(fd, "\n"); |
| 2668 | fprintf(fd, "#define LTT_TRACE\n"); |
| 2669 | fprintf(fd, "#include <error.h>\n"); |
| 2670 | fprintf(fd, "#include <stdio.h>\n"); |
| 2671 | fprintf(fd, "#include <ltt/ltt-usertrace.h>\n"); |
| 2672 | if(!fac->arch) |
| 2673 | fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name); |
| 2674 | else |
| 2675 | fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n", |
| 2676 | fac->name, fac->arch); |
| 2677 | fprintf(fd, "\n"); |
| 2678 | fprintf(fd, "static struct user_facility_info facility = {\n"); |
| 2679 | fprintf(fd, "\t.name = LTT_FACILITY_NAME,\n"); |
| 2680 | fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n"); |
| 2681 | fprintf(fd, "#ifndef LTT_PACK\n"); |
| 2682 | fprintf(fd, "\t.alignment = sizeof(void*),\n"); |
| 2683 | fprintf(fd, "#else\n"); |
| 2684 | fprintf(fd, "\t.alignment = 0,\n"); |
| 2685 | fprintf(fd, "#endif //LTT_PACK\n"); |
| 2686 | fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n"); |
| 2687 | fprintf(fd, "\t.int_size = sizeof(int),\n"); |
| 2688 | fprintf(fd, "\t.long_size = sizeof(long),\n"); |
| 2689 | fprintf(fd, "\t.pointer_size = sizeof(void*),\n"); |
| 2690 | fprintf(fd, "\t.size_t_size = sizeof(size_t)\n"); |
| 2691 | fprintf(fd, "};\n"); |
| 2692 | fprintf(fd, "\n"); |
| 2693 | fprintf(fd, "static void __attribute__((constructor)) __ltt_user_init(void)\n"); |
| 2694 | fprintf(fd, "{\n"); |
| 2695 | fprintf(fd, "\tint err;\n"); |
| 2696 | fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n"); |
| 2697 | fprintf(fd, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac->name); |
| 2698 | fprintf(fd, "#endif //LTT_SHOW_DEBUG\n"); |
| 2699 | fprintf(fd, "\n"); |
| 2700 | fprintf(fd, "\terr = ltt_register_generic(<T_FACILITY_SYMBOL, &facility);\n"); |
| 2701 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
| 2702 | fprintf(fd, "\t\n"); |
| 2703 | fprintf(fd, "\tif(err) {\n"); |
| 2704 | fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n"); |
| 2705 | fprintf(fd, "\t\tperror(\"Error in ltt_register_generic\");\n"); |
| 2706 | fprintf(fd, "#endif //LTT_SHOW_DEBUG\n"); |
| 2707 | fprintf(fd, "\t}\n"); |
| 2708 | fprintf(fd, "}\n"); |
| 2709 | fprintf(fd, "\n"); |
| 2710 | |
| 2711 | fclose(fd); |
| 2712 | |
| 2713 | return 0; |
| 2714 | } |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | /* open facility */ |
| 2719 | /* code taken from ltt_facility_open in ltt/facility.c in lttv */ |
| 2720 | |
| 2721 | /***************************************************************************** |
| 2722 | *Function name |
| 2723 | * ltt_facility_open : open facilities |
| 2724 | *Input params |
| 2725 | * pathname : the path name of the facility |
| 2726 | * |
| 2727 | * Open the facility corresponding to the right checksum. |
| 2728 | * |
| 2729 | *returns the facility on success, NULL on error. |
| 2730 | ****************************************************************************/ |
| 2731 | facility_t *ltt_facility_open(char * pathname) |
| 2732 | { |
| 2733 | int ret = 0; |
| 2734 | char *token; |
| 2735 | parse_file_t in; |
| 2736 | facility_t * fac = NULL; |
| 2737 | char buffer[BUFFER_SIZE]; |
| 2738 | int generated = FALSE; |
| 2739 | |
| 2740 | in.buffer = &(buffer[0]); |
| 2741 | in.lineno = 0; |
| 2742 | in.error = error_callback; |
| 2743 | in.name = pathname; |
| 2744 | in.unget = 0; |
| 2745 | |
| 2746 | in.fp = fopen(in.name, "r"); |
| 2747 | if(in.fp == NULL) { |
| 2748 | ret = 1; |
| 2749 | goto open_error; |
| 2750 | } |
| 2751 | |
| 2752 | while(1){ |
| 2753 | token = getToken(&in); |
| 2754 | if(in.type == ENDFILE) break; |
| 2755 | |
| 2756 | if(generated) { |
| 2757 | printf("More than one facility in the file. Only using the first one.\n"); |
| 2758 | break; |
| 2759 | } |
| 2760 | |
| 2761 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
| 2762 | token = getName(&in); |
| 2763 | if(strcmp(token, "?")) in.error(&in,"not a facility file"); |
| 2764 | token = getName(&in); |
| 2765 | if(strcmp(token, "xml")) in.error(&in,"not a facility file"); |
| 2766 | token = getName(&in); |
| 2767 | if(strcmp(token, "version")) in.error(&in,"not a facility file"); |
| 2768 | token = getName(&in); |
| 2769 | if(strcmp(token, "=")) in.error(&in,"not a facility file"); |
| 2770 | token = getQuotedString(&in); |
| 2771 | if(strcmp(token, "1.0")) in.error(&in,"not a facility file"); |
| 2772 | token = getName(&in); |
| 2773 | if(strcmp(token, "?")) in.error(&in,"not a facility file"); |
| 2774 | token = getToken(&in); |
| 2775 | if(strcmp(token, ">")) in.error(&in,"not a facility file"); |
| 2776 | |
| 2777 | token = getName(&in); |
| 2778 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
| 2779 | token = getName(&in); |
| 2780 | if(strcmp("facility",token) == 0) { |
| 2781 | fac = malloc(sizeof(facility_t)); |
| 2782 | fac->name = NULL; |
| 2783 | fac->description = NULL; |
| 2784 | sequence_init(&(fac->events)); |
| 2785 | table_init(&(fac->named_types)); |
| 2786 | sequence_init(&(fac->unnamed_types)); |
| 2787 | |
| 2788 | parseFacility(&in, fac); |
| 2789 | |
| 2790 | //check if any namedType is not defined |
| 2791 | checkNamedTypesImplemented(&fac->named_types); |
| 2792 | |
| 2793 | generateChecksum(fac->name, &fac->checksum, &fac->events); |
| 2794 | |
| 2795 | generated = TRUE; |
| 2796 | } |
| 2797 | else { |
| 2798 | printf("facility token was expected in file %s\n", in.name); |
| 2799 | ret = 1; |
| 2800 | goto parse_error; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | parse_error: |
| 2805 | fclose(in.fp); |
| 2806 | open_error: |
| 2807 | |
| 2808 | if(!generated) { |
| 2809 | printf("Cannot find facility %s\n", pathname); |
| 2810 | fac = NULL; |
| 2811 | } |
| 2812 | |
| 2813 | return fac; |
| 2814 | } |
| 2815 | |
| 2816 | /* Close the facility */ |
| 2817 | void ltt_facility_close(facility_t *fac) |
| 2818 | { |
| 2819 | free(fac->name); |
| 2820 | free(fac->capname); |
| 2821 | free(fac->description); |
| 2822 | freeEvents(&fac->events); |
| 2823 | sequence_dispose(&fac->events); |
| 2824 | freeNamedType(&fac->named_types); |
| 2825 | table_dispose(&fac->named_types); |
| 2826 | freeTypes(&fac->unnamed_types); |
| 2827 | sequence_dispose(&fac->unnamed_types); |
| 2828 | free(fac); |
| 2829 | } |
| 2830 | |
| 2831 | |
| 2832 | /* Show help */ |
| 2833 | void show_help(int argc, char ** argv) |
| 2834 | { |
| 2835 | printf("Genevent help : \n"); |
| 2836 | printf("\n"); |
| 2837 | printf("Use %s name.xml\n", argv[0]); |
| 2838 | printf("to create :\n"); |
| 2839 | printf("ltt-facility-name.h\n"); |
| 2840 | printf("ltt-facility-id-name.h\n"); |
| 2841 | printf("ltt-facility-loader-name.h\n"); |
| 2842 | printf("ltt-facility-loader-name.c\n"); |
| 2843 | printf("In the current directory.\n"); |
| 2844 | printf("\n"); |
| 2845 | } |
| 2846 | |
| 2847 | /* Parse program arguments */ |
| 2848 | /* Return values : |
| 2849 | * 0 : continue program |
| 2850 | * -1 : stop program, return 0 |
| 2851 | * > 0 : stop program, return value as exit. |
| 2852 | */ |
| 2853 | int check_args(int argc, char **argv) |
| 2854 | { |
| 2855 | if(argc < 2) { |
| 2856 | printf("Not enough arguments\n"); |
| 2857 | show_help(argc, argv); |
| 2858 | return EINVAL; |
| 2859 | } |
| 2860 | |
| 2861 | if(strcmp(argv[1], "-h") == 0) { |
| 2862 | show_help(argc, argv); |
| 2863 | return -1; |
| 2864 | } |
| 2865 | |
| 2866 | return 0; |
| 2867 | } |
| 2868 | |
| 2869 | int main(int argc, char **argv) |
| 2870 | { |
| 2871 | int err = 0; |
| 2872 | facility_t *fac; |
| 2873 | |
| 2874 | err = check_args(argc, argv); |
| 2875 | if(err > 0) return err; |
| 2876 | else if(err < 0) return 0; |
| 2877 | |
| 2878 | /* open the facility */ |
| 2879 | fac = ltt_facility_open(argv[1]); |
| 2880 | if(fac == NULL) { |
| 2881 | printf("Error opening file %s for reading : %s\n", |
| 2882 | argv[1], strerror(errno)); |
| 2883 | return errno; |
| 2884 | } |
| 2885 | |
| 2886 | /* generate the output C files */ |
| 2887 | |
| 2888 | |
| 2889 | /* ltt-facility-name.h : main logging header. |
| 2890 | * log_header */ |
| 2891 | err = print_log_header(fac); |
| 2892 | if(err) return err; |
| 2893 | |
| 2894 | /* ltt-facility-id-name.h : facility id. |
| 2895 | * log_id_header */ |
| 2896 | err = print_id_header(fac); |
| 2897 | if(err) return err; |
| 2898 | |
| 2899 | /* ltt-facility-loader-name.h : facility specific loader info. |
| 2900 | * loader_header */ |
| 2901 | if(!fac->user) |
| 2902 | err = print_loader_header(fac); |
| 2903 | else |
| 2904 | err = print_loader_header_user(fac); |
| 2905 | if(err) return err; |
| 2906 | |
| 2907 | /* ltt-facility-loader-name.c : generic faciilty loader |
| 2908 | * loader_c */ |
| 2909 | if(!fac->user) |
| 2910 | err = print_loader_c(fac); |
| 2911 | else |
| 2912 | err = print_loader_c_user(fac); |
| 2913 | if(err) return err; |
| 2914 | |
| 2915 | /* close the facility */ |
| 2916 | ltt_facility_close(fac); |
| 2917 | |
| 2918 | return 0; |
| 2919 | } |
| 2920 | |
| 2921 | |