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