4042dafafdeda23986643873853d0e60e71b8c3b
1 /******************************************************************************
4 * Event generator. XML to logging C code converter.
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.
15 * - C types : struct, union, enum, basic types.
16 * - Architectures : LP32, ILP32, ILP64, LLP64, LP64.
18 * Additionnal structures supported :
19 * - embedded variable size strings
20 * - embedded variable size arrays
21 * - embedded variable size sequences
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.
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.
36 * http://www.usenix.org/publications/login/standards/10.data.html
37 * (Andrew Josey <a.josey@opengroup.org>) :
39 * Data Type LP32 ILP32 ILP64 LLP64 LP64
41 * short 16 16 16 16 16
45 * long long (int64) 64
46 * pointer 32 32 64 64 64
48 * With these constraints :
49 * sizeof(char) <= sizeof(short) <= sizeof(int)
50 * <= sizeof(long) = sizeof(size_t)
52 * and therefore sizeof(long) <= sizeof(pointer) <= sizeof(size_t)
54 * Which means we only have to remember which is the biggest type in a structure
55 * to know the structure's alignment.
62 #include <sys/types.h>
77 /* Debugging printf */
79 #define dprintf(...) \
81 printf(__FILE__ ",%u,%s: ",\
89 void preset_field_type_size(event_t
*event_type
,
90 off_t offset_root
, off_t offset_parent
,
91 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
96 void print_tabs(unsigned int tabs
, FILE *fd
)
98 for(unsigned int i
= 0; i
<tabs
;i
++)
102 /* Type size checking */
103 /* Uses #error in the generated code to signal error and stop the compiler */
104 int print_check(int fd
);
109 * Copied from construct_types_and_fields in LTTV facility.c */
111 int print_type(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
112 char *nest_name
, char *field_name
)
114 char basename
[PATH_MAX
];
115 unsigned int basename_len
= 0;
117 strcpy(basename
, nest_name
);
118 basename_len
= strlen(basename
);
120 /* For a named type, we use the type_name directly */
121 if(td
->type_name
!= NULL
) {
122 strncpy(basename
, td
->type_name
, PATH_MAX
);
123 basename_len
= strlen(basename
);
125 /* For a unnamed type, there must be a field name */
126 if((basename_len
!= 0)
127 && (basename
[basename_len
-1] != '_')
128 && (field_name
[0] != '\0')) {
129 strncat(basename
, "_", PATH_MAX
- basename_len
);
130 basename_len
= strlen(basename
);
132 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
137 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
140 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
143 fprintf(fd
, "signed char");
146 fprintf(fd
, "unsigned char");
149 fprintf(fd
, "short");
152 fprintf(fd
, "unsigned short");
158 fprintf(fd
, "unsigned int");
161 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
164 fprintf(fd
, "void *");
170 fprintf(fd
, "unsigned long");
173 fprintf(fd
, "size_t");
176 fprintf(fd
, "ssize_t");
179 fprintf(fd
, "off_t");
182 fprintf(fd
, "char *");
185 fprintf(fd
, "enum lttng_%s", basename
);
188 fprintf(fd
, "lttng_array_%s", basename
);
191 fprintf(fd
, "lttng_sequence_%s", basename
);
194 fprintf(fd
, "struct lttng_%s", basename
);
197 fprintf(fd
, "union lttng_%s", basename
);
200 printf("print_type : unknown type\n");
207 /* Print logging function argument */
208 int print_arg(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
209 char *nest_name
, char *field_name
)
211 char basename
[PATH_MAX
];
212 unsigned int basename_len
= 0;
214 strcpy(basename
, nest_name
);
215 basename_len
= strlen(basename
);
217 /* For a named type, we use the type_name directly */
218 if(td
->type_name
!= NULL
) {
219 strncpy(basename
, td
->type_name
, PATH_MAX
);
220 basename_len
= strlen(basename
);
222 /* For a unnamed type, there must be a field name */
223 if((basename_len
!= 0)
224 && (basename
[basename_len
-1] != '_')
225 && (field_name
[0] != '\0')) {
226 strncat(basename
, "_", PATH_MAX
- basename_len
);
227 basename_len
= strlen(basename
);
229 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
232 print_tabs(tabs
, fd
);
236 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
237 fprintf(fd
, " %s", field_name
);
240 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
241 fprintf(fd
, " %s", field_name
);
244 fprintf(fd
, "signed char");
245 fprintf(fd
, " %s", field_name
);
248 fprintf(fd
, "unsigned char");
249 fprintf(fd
, " %s", field_name
);
252 fprintf(fd
, "short");
253 fprintf(fd
, " %s", field_name
);
256 fprintf(fd
, "unsigned short");
257 fprintf(fd
, " %s", field_name
);
261 fprintf(fd
, " %s", field_name
);
264 fprintf(fd
, "unsigned int");
265 fprintf(fd
, " %s", field_name
);
268 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
269 fprintf(fd
, " %s", field_name
);
272 fprintf(fd
, "void *");
273 fprintf(fd
, " %s", field_name
);
277 fprintf(fd
, " %s", field_name
);
280 fprintf(fd
, "unsigned long");
281 fprintf(fd
, " %s", field_name
);
284 fprintf(fd
, "size_t");
285 fprintf(fd
, " %s", field_name
);
288 fprintf(fd
, "ssize_t");
289 fprintf(fd
, " %s", field_name
);
292 fprintf(fd
, "off_t");
293 fprintf(fd
, " %s", field_name
);
296 fprintf(fd
, "char *");
297 fprintf(fd
, " %s", field_name
);
300 fprintf(fd
, "enum lttng_%s", basename
);
301 fprintf(fd
, " %s", field_name
);
304 fprintf(fd
, "lttng_array_%s", basename
);
305 fprintf(fd
, " %s", field_name
);
308 fprintf(fd
, "lttng_sequence_%s *", basename
);
309 fprintf(fd
, " %s", field_name
);
312 fprintf(fd
, "struct lttng_%s *", basename
);
313 fprintf(fd
, " %s", field_name
);
316 fprintf(fd
, "union lttng_%s *", basename
);
317 fprintf(fd
, " %s", field_name
);
320 printf("print_type : unknown type\n");
328 /* Does the type has a fixed size ? (as know from the compiler)
331 * 0 : variable length
333 int has_type_fixed_size(type_descriptor_t
*td
)
352 case UNION
: /* The union must have fixed size children. Must be checked by
362 int has_type_fixed
= 0;
363 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
364 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
365 type_descriptor_t
*type
= field
->type
;
367 has_type_fixed
= has_type_fixed_size(type
);
368 if(!has_type_fixed
) return 0;
374 assert(td
->size
>= 0);
375 return has_type_fixed(((field_t
*)td
->fields
.array
[0])->type
);
384 /* print type declaration.
386 * Copied from construct_types_and_fields in LTTV facility.c */
388 int print_type_declaration(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
389 char *nest_name
, char *field_name
)
391 char basename
[PATH_MAX
];
392 unsigned int basename_len
= 0;
394 strncpy(basename
, nest_name
, PATH_MAX
);
395 basename_len
= strlen(basename
);
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
);
402 /* For a unnamed type, there must be a field name, except for
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
);
410 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
415 fprintf(fd
, "enum lttng_%s", basename
);
417 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
419 fprintf(fd
, "LTTNG_%s", ((char*)(td
->labels
.array
[i
])));
427 dprintf("%s\n", basename
);
428 assert(td
->size
>= 0);
429 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
430 /* Not a named nested type : we must print its declaration first */
431 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
432 fd
, 0, basename
, "")) return 1;
434 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %llu\n", basename
,
436 fprintf(fd
, "typedef ");
437 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
438 fd
, tabs
, basename
, "")) return 1;
439 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
444 /* We assume that the sequence length type does not need to be declared.
446 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
447 /* Not a named nested type : we must print its declaration first */
448 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
449 fd
, 0, basename
, "")) return 1;
451 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
454 fprintf(fd
, "struct lttng_sequence_%s", basename
);
457 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
458 fd
, tabs
, basename
, "")) return 1;
459 fprintf(fd
, "len;\n");
461 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
462 fd
, tabs
, basename
, "")) return 1;
463 fprintf(fd
, " *array;\n");
469 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
470 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
471 type_descriptor_t
*type
= field
->type
;
472 if(type
->type_name
== NULL
) {
473 /* Not a named nested type : we must print its declaration first */
474 if(print_type_declaration(type
,
475 fd
, 0, basename
, field
->name
)) return 1;
478 fprintf(fd
, "struct lttng_%s", basename
);
480 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
481 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
482 type_descriptor_t
*type
= field
->type
;
484 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
486 fprintf(fd
, "%s", field
->name
);
493 /* TODO : Do not allow variable length fields in a union */
494 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
495 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
496 type_descriptor_t
*type
= field
->type
;
497 if(type
->type_name
== NULL
) {
498 /* Not a named nested type : we must print its declaration first */
499 if(print_type_declaration(type
,
500 fd
, 0, basename
, field
->name
)) return 1;
503 fprintf(fd
, "union lttng_%s", basename
);
505 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
506 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
507 type_descriptor_t
*type
= field
->type
;
509 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
511 fprintf(fd
, "%s", field
->name
);
518 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
526 /* print type alignment.
528 * Copied from construct_types_and_fields in LTTV facility.c
530 * basename is the name which identifies the type (along with a prefix
533 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
534 char *nest_name
, char *field_name
)
536 char basename
[PATH_MAX
];
537 unsigned int basename_len
= 0;
539 strncpy(basename
, nest_name
, PATH_MAX
);
540 basename_len
= strlen(basename
);
542 /* For a named type, we use the type_name directly */
543 if(td
->type_name
!= NULL
) {
544 strncpy(basename
, td
->type_name
, PATH_MAX
);
545 basename_len
= strlen(basename
);
547 /* For a unnamed type, there must be a field name, except for
549 if((basename_len
!= 0)
550 && (basename
[basename_len
-1] != '_'
551 && (field_name
[0] != '\0'))) {
552 strncat(basename
, "_", PATH_MAX
- basename_len
);
553 basename_len
= strlen(basename
);
555 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
575 fprintf(fd
, "sizeof(");
576 if(print_type(td
->type
,
577 fd
, 0, basename
, "")) return 1;
581 fprintf(fd
, "sizeof(char)");
584 fprintf(fd
, "lttng_get_alignment_sequence_%s(&obj->%s)", basename
,
588 fprintf(fd
, "lttng_get_alignment_struct_%s(&obj->%s)", basename
,
592 fprintf(fd
, "lttng_get_alignment_union_%s(&obj->%s)", basename
,
596 fprintf(fd
, "lttng_get_alignment_array_%s(obj->%s)", basename
,
606 * Copied from construct_types_and_fields in LTTV facility.c
608 * basename is the name which identifies the type (along with a prefix
611 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
612 char *nest_name
, char *field_name
)
614 char basename
[PATH_MAX
];
615 unsigned int basename_len
= 0;
617 strncpy(basename
, nest_name
, PATH_MAX
);
618 basename_len
= strlen(basename
);
620 /* For a named type, we use the type_name directly */
621 if(td
->type_name
!= NULL
) {
622 strncpy(basename
, td
->type_name
, PATH_MAX
);
623 basename_len
= strlen(basename
);
625 /* For a unnamed type, there must be a field name, except for
627 if((basename_len
!= 0)
628 && (basename
[basename_len
-1] != '_'
629 && (field_name
[0] != '\0'))) {
630 strncat(basename
, "_", PATH_MAX
- basename_len
);
631 basename_len
= strlen(basename
);
633 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
653 print_tabs(tabs
, fd
);
654 fprintf(fd
, "size = ");
655 fprintf(fd
, "sizeof(");
656 if(print_type(td
->type
,
657 fd
, 0, basename
, "")) return 1;
659 print_tabs(tabs
, fd
);
660 fprintf(fd
, "size += ltt_align(*to+*len, size) + size;");
661 print_tabs(tabs
, fd
);
662 fprintf(fd
, "*len += size;");
665 print_tabs(tabs
, fd
);
666 fprintf(fd
, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n");
669 print_tabs(tabs
, fd
);
670 fprintf(fd
, "lttng_write_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
674 print_tabs(tabs
, fd
);
675 fprintf(fd
, "lttng_write_struct_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
679 print_tabs(tabs
, fd
);
680 fprintf(fd
, "lttng_write_union_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
684 print_tabs(tabs
, fd
);
685 fprintf(fd
, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
695 /* print type alignment function.
697 * Copied from construct_types_and_fields in LTTV facility.c
699 * basename is the name which identifies the type (along with a prefix
702 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
704 char *nest_name
, char *field_name
)
706 char basename
[PATH_MAX
];
707 unsigned int basename_len
= 0;
709 strncpy(basename
, nest_name
, PATH_MAX
);
710 basename_len
= strlen(basename
);
712 /* For a named type, we use the type_name directly */
713 if(td
->type_name
!= NULL
) {
714 strncpy(basename
, td
->type_name
, PATH_MAX
);
715 basename_len
= strlen(basename
);
717 /* For a unnamed type, there must be a field name, except for
719 if((basename_len
!= 0)
720 && (basename
[basename_len
-1] != '_'
721 && (field_name
[0] != '\0'))) {
722 strncat(basename
, "_", PATH_MAX
- basename_len
);
723 basename_len
= strlen(basename
);
725 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
730 /* Function header */
731 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
734 if(print_type(td
->type
, fd
, 0, basename
, "")) return 1;
735 fprintf(fd
, " *obj)\n");
738 fprintf(fd
, "size_t align=0, localign;");
741 fprintf(fd
, "localign = ");
742 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
743 fd
, 0, basename
, field
->name
)) return 1;
746 fprintf(fd
, "align = max(align, localign);\n");
749 fprintf(fd
, "localign = ");
750 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
751 fd
, 0, basename
, field
->name
)) return 1;
754 fprintf(fd
, "align = max(align, localign);\n");
757 fprintf(fd
, "return align;\n");
760 /* Function header */
761 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
764 if(print_type(td
->type
, fd
, 0, basename
, "")) return 1;
765 fprintf(fd
, " *obj)\n");
768 fprintf(fd
, "size_t align=0, localign;");
770 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
771 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
772 type_descriptor_t
*type
= field
->type
;
774 fprintf(fd
, "localign = ");
775 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
778 fprintf(fd
, "align = max(align, localign);\n");
782 fprintf(fd
, "return align;\n");
786 /* Function header */
787 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
790 if(print_type(td
->type
, fd
, 0, basename
, "")) return 1;
791 fprintf(fd
, " *obj)\n");
794 fprintf(fd
, "size_t align=0, localign;");
796 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
797 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
798 type_descriptor_t
*type
= field
->type
;
800 fprintf(fd
, "localign = ");
801 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
804 fprintf(fd
, "align = max(align, localign);\n");
808 fprintf(fd
, "return align;\n");
812 /* Function header */
813 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
816 if(print_type(td
->type
, fd
, 0, basename
, "")) return 1;
817 fprintf(fd
, " obj)\n");
820 fprintf(fd
, "return \n");
821 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
822 fd
, 0, basename
, field
->name
)) return 1;
826 printf("print_type_alignment_fct : type has no alignment function.\n");
831 /* Function footer */
837 /* print type write function.
839 * Copied from construct_types_and_fields in LTTV facility.c
841 * basename is the name which identifies the type (along with a prefix
844 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
845 char *nest_name
, char *field_name
)
847 char basename
[PATH_MAX
];
848 unsigned int basename_len
= 0;
850 strncpy(basename
, nest_name
, PATH_MAX
);
851 basename_len
= strlen(basename
);
853 /* For a named type, we use the type_name directly */
854 if(td
->type_name
!= NULL
) {
855 strncpy(basename
, td
->type_name
, PATH_MAX
);
856 basename_len
= strlen(basename
);
858 /* For a unnamed type, there must be a field name, except for
860 if((basename_len
!= 0)
861 && (basename
[basename_len
-1] != '_'
862 && (field_name
[0] != '\0'))) {
863 strncat(basename
, "_", PATH_MAX
- basename_len
);
864 basename_len
= strlen(basename
);
866 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
872 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
875 fprintf(fd
, "lttng_get_alignment_sequence_%s(&obj->%s)", basename
,
879 fprintf(fd
, "lttng_get_alignment_struct_%s(&obj->%s)", basename
,
883 fprintf(fd
, "lttng_get_alignment_union_%s(&obj->%s)", basename
,
887 fprintf(fd
, "lttng_get_alignment_array_%s(obj->%s)", basename
,
891 printf("print_type_write_fct : type has no write function.\n");
896 fprintf(fd
, "void *buffer,\n");
898 fprintf(fd
, "size_t *to_base,\n");
900 fprintf(fd
, "size_t *to,\n");
902 fprintf(fd
, "void **from,\n");
904 fprintf(fd
, "size_t *len,\n");
906 if(print_type(td
->type
, fd
, 0, basename
, "")) return 1;
910 fprintf(fd
, " *obj)\n");
913 fprintf(fd
, " *obj)\n");
916 fprintf(fd
, " *obj)\n");
919 fprintf(fd
, " obj)\n");
922 printf("print_type_write_fct : type has no write function.\n");
928 fprintf(fd
, "size_t align, size;\n");
935 fprintf(fd
, "/* Flush pending memcpy */\n");
937 fprintf(fd
, "if(*len != 0) {\n");
939 fprintf(fd
, "if(buffer != NULL)\n");
941 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
945 fprintf(fd
, "*to += *len;\n");
947 fprintf(fd
, "*len = 0;\n");
955 printf("print_type_write_fct : type has no write function.\n");
960 fprintf(fd
, "align = \n");
961 if(print_type_alignment(td
, fd
, 0, basename
, field
->name
)) return 1;
965 fprintf(fd
, "if(*len == 0) {\n");
967 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
969 fprintf(fd
, "} else {\n");
971 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
976 /* First, check if the type has a fixed size. If it is the case, then the size
977 * to write is know by the compiler : simply use a sizeof() */
978 if(has_type_fixed_size(td
)) {
980 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
983 fprintf(fd
, "*len += sizeof(");
984 if(print_type(td
, fd
, 0, basename
, field
->name
)) return 1;
987 /* The type contains nested variable size subtypes :
988 * we must write field by field. */
990 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
996 fprintf(fd
, "/* Copy members */\n");
998 fprintf(fd
, "size = sizeof(\n");
999 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1000 fd
, 1, basename
, field
->name
)) return 1;
1001 fprintf(fd
, ");\n");
1003 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1005 fprintf(fd
, "if(buffer != NULL)\n");
1007 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, size);\n");
1009 fprintf(fd
, "*to += size;\n");
1012 /* Write the child : varlen child or not ? */
1013 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1014 /* Fixed size len child : use a multiplication of its size */
1016 fprintf(fd
, "size = sizeof(\n");
1017 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1018 fd
, 1, basename
, field
->name
)) return 1;
1019 fprintf(fd
, ");\n");
1021 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1023 fprintf(fd
, "size = obj->len * size;\n");
1025 fprintf(fd
, "if(buffer != NULL)\n");
1027 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, size);\n");
1029 fprintf(fd
, "*to += size;\n");
1033 fprintf(fd
, "/* Variable length child : iter. */\n");
1035 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1036 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1037 fd
, 2, basename
, field
->name
)) return 1;
1043 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1045 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1047 fprintf(fd
, "*to_base = *to_base+*to;\n");
1049 fprintf(fd
, "*to = 0;\n");
1051 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1053 fprintf(fd
, "*from = obj+1;\n");
1056 fprintf(fd
, "size = strlen(obj);\n");
1058 fprintf(fd
, "if(buffer != NULL)\n");
1060 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1062 fprintf(fd
, "*to += size;\n");
1065 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1067 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1069 fprintf(fd
, "*to_base = *to_base+*to;\n");
1071 fprintf(fd
, "*to = 0;\n");
1073 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1075 fprintf(fd
, "*from = obj+1;\n");
1078 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1079 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1080 type_descriptor_t
*type
= field
->type
;
1081 if(print_type_write(type
,
1082 fd
, 1, basename
, field
->name
)) return 1;
1087 printf("ERROR : A union CANNOT contain a variable size child.\n");
1091 /* Write the child : varlen child or not ? */
1092 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1093 /* Error : if an array has a variable size, then its child must also
1094 * have a variable size. */
1098 fprintf(fd
, "/* Variable length child : iter. */\n");
1100 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1101 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1102 fd
, 2, basename
, field
->name
)) return 1;
1108 printf("print_type_write_fct : type has no write function.\n");
1116 /* Function footer */
1124 /* Print the logging function of an event. This is the core of genevent */
1125 int print_event_logging_function(char *basename
, facility_t
*fac
,
1126 event_t
*event
, FILE *fd
)
1128 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1129 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1130 /* For each field, print the function argument */
1131 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1132 type_descriptor_t
*t
= f
->type
;
1133 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1134 if(j
< event
->fields
.position
-1) {
1139 if(event
->fields
.position
== 0) {
1141 fprintf(fd
, "void");
1144 fprintf(fd
, "#ifndef CONFIG_LTT\n");
1147 fprintf(fd
,"#else\n");
1149 /* Print the function variables */
1151 fprintf(fd
, "unsigned int index;\n");
1153 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1155 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1157 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1159 fprintf(fd
, "void *buffer = NULL;\n");
1161 fprintf(fd
, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1163 fprintf(fd
, "size_t to = 0;\n");
1165 fprintf(fd
, "void *from;");
1167 fprintf(fd
, "size_t len = 0;\n");
1169 fprintf(fd
, "size_t slot_size;\n");
1171 fprintf(fd
, "cycles_t tsc;\n");
1173 fprintf(fd
, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
1177 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1180 /* Calculate event variable len + event data alignment offset.
1181 * Assume that the padding for alignment starts at a void*
1183 * This excludes the header size and alignment. */
1186 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1188 fprintf(fd
, "/* size = to_base + to + len */\n");
1190 fprintf(fd
, "/* Assume that the padding for alignment starts at a void*\n");
1192 fprintf(fd
, "/* address. */\n");
1195 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1196 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1197 type_descriptor_t
*type
= field
->type
;
1198 if(print_type_write(type
,
1199 fd
, 1, basename
, field
->name
)) return 1;
1203 /* Take locks : make sure the trace does not vanish while we write on
1204 * it. A simple preemption disabling is enough (using rcu traces). */
1206 fprintf(fd
, "preempt_disable();\n");
1208 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1210 /* Get facility index */
1212 if(event
->per_tracefile
) {
1214 fprintf(fd
, "index = tracefile_index;\n");
1218 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1219 "\t\t\t\tevent_%s);\n",
1220 fac
->name
, fac
->checksum
, event
->name
);
1225 /* For each trace */
1227 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1229 fprintf(fd
, "if(!trace->active) continue;\n\n");
1231 if(event
->per_trace
) {
1233 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1237 fprintf(fp
, "channel = ltt_get_channel_from_index(trace, index);\n");
1239 fprintf(fd
, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1244 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1247 fprintf(fd
, "slot_size = 0;\n");
1249 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf, to_base + to + len, &slot_size, &tsc,\n"
1250 "\t\t&before_hdr_pad, &after_hdr_pad);\n");
1251 /* If error, return */
1253 fprintf(fd
, "if(!buffer) return;\n\n");
1255 /* write data : assume stack alignment is the same as struct alignment. */
1257 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1258 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1259 type_descriptor_t
*type
= field
->type
;
1263 fprintf(fd
, "from = %s;\n", f
->name
);
1265 if(print_type_write(type
,
1266 fd
, 2, basename
, field
->name
)) return 1;
1269 /* Don't forget to flush pending memcpy */
1271 fprintf(fd
, "/* Flush pending memcpy */\n");
1273 fprintf(fd
, "if(len != 0) {\n");
1275 fprintf(fd
, "memcpy(buffer+to_base+to, from, len);\n");
1277 fprintf(fd
, "to += len;\n");
1278 //print_tabs(3, fd);
1279 //fprintf(fd, "from += len;\n");
1281 fprintf(fd
, "len = 0;\n");
1290 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1293 fprintf(fd
, "}\n\n");
1297 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1299 fprintf(fd
, "preempt_enable_no_resched();\n");
1302 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1307 /* ltt-facility-name.h : main logging header.
1310 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1312 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1313 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1315 fprintf(fd
, "/* Facility activation at compile time. */\n");
1316 fprintf(fd
, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1321 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1323 sequence_t
*types
= &fac
->named_types
.values
;
1324 fprintf(fd
, "/* Named types */\n");
1327 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1328 /* For each named type, print the definition */
1329 if((print_type_declaration(types
->array
[i
], fd
,
1330 0, "", ""))) return 1;
1335 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1337 sequence_t
*events
= &fac
->events
;
1338 char basename
[PATH_MAX
];
1339 unsigned int facname_len
;
1341 strncpy(basename
, fac
->name
, PATH_MAX
);
1342 facname_len
= strlen(basename
);
1343 strncat(basename
, "_", PATH_MAX
-facname_len
);
1344 facname_len
= strlen(basename
);
1346 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1347 event_t
*event
= (event_t
*)events
->array
[i
];
1348 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1350 /* For each event, print structure, and then logging function */
1351 fprintf(fd
, "/* Event %s structures */\n",
1353 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1354 /* For each unnamed type, print the definition */
1355 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1356 type_descriptor_t
*t
= f
->type
;
1357 if(t
->type_name
== NULL
)
1358 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1362 fprintf(fd
, "/* Event %s logging function */\n",
1365 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1374 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1376 fprintf(fd
, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1377 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1380 int print_log_header(facility_t
*fac
)
1382 char filename
[PATH_MAX
];
1383 unsigned int filename_size
= 0;
1385 dprintf("%s\n", fac
->name
);
1387 strcpy(filename
, "ltt-facility-");
1388 filename_size
= strlen(filename
);
1390 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1391 filename_size
= strlen(filename
);
1393 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1394 filename_size
= strlen(filename
);
1397 fd
= fopen(filename
, "w");
1399 printf("Error opening file %s for writing : %s\n",
1400 filename
, strerror(errno
));
1404 /* Print file head */
1405 print_log_header_head(fac
, fd
);
1407 /* print named types in declaration order */
1408 if(print_log_header_types(fac
, fd
)) return 1;
1411 if(print_log_header_events(fac
, fd
)) return 1;
1413 /* Print file tail */
1414 print_log_header_tail(fac
, fd
);
1423 /* ltt-facility-id-name.h : facility id.
1425 int print_id_header(facility_t
*fac
)
1427 char filename
[PATH_MAX
];
1428 unsigned int filename_size
= 0;
1430 dprintf("%s\n", fac
->name
);
1432 strcpy(filename
, "ltt-facility-id-");
1433 filename_size
= strlen(filename
);
1435 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1436 filename_size
= strlen(filename
);
1438 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1439 filename_size
= strlen(filename
);
1442 fd
= fopen(filename
, "w");
1444 printf("Error opening file %s for writing : %s\n",
1445 filename
, strerror(errno
));
1455 /* ltt-facility-loader-name.h : facility specific loader info.
1457 int print_loader_header(facility_t
*fac
)
1459 char filename
[PATH_MAX
];
1460 unsigned int filename_size
= 0;
1462 dprintf("%s\n", fac
->name
);
1464 strcpy(filename
, "ltt-facility-loader-");
1465 filename_size
= strlen(filename
);
1467 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1468 filename_size
= strlen(filename
);
1470 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1471 filename_size
= strlen(filename
);
1474 fd
= fopen(filename
, "w");
1476 printf("Error opening file %s for writing : %s\n",
1477 filename
, strerror(errno
));
1486 /* ltt-facility-loader-name.c : generic faciilty loader
1488 int print_loader_c(facility_t
*fac
)
1490 char filename
[PATH_MAX
];
1491 unsigned int filename_size
= 0;
1493 dprintf("%s\n", fac
->name
);
1495 strcpy(filename
, "ltt-facility-loader-");
1496 filename_size
= strlen(filename
);
1498 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1499 filename_size
= strlen(filename
);
1501 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1502 filename_size
= strlen(filename
);
1505 fd
= fopen(filename
, "w");
1507 printf("Error opening file %s for writing : %s\n",
1508 filename
, strerror(errno
));
1521 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1523 /*****************************************************************************
1525 * ltt_facility_open : open facilities
1527 * pathname : the path name of the facility
1529 * Open the facility corresponding to the right checksum.
1531 *returns the facility on success, NULL on error.
1532 ****************************************************************************/
1533 facility_t
*ltt_facility_open(char * pathname
)
1538 facility_t
* fac
= NULL
;
1539 unsigned long checksum
;
1540 char buffer
[BUFFER_SIZE
];
1541 int generated
= FALSE
;
1543 in
.buffer
= &(buffer
[0]);
1545 in
.error
= error_callback
;
1549 in
.fp
= fopen(in
.name
, "r");
1556 token
= getToken(&in
);
1557 if(in
.type
== ENDFILE
) break;
1560 printf("More than one facility in the file. Only using the first one.\n");
1564 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1565 token
= getName(&in
);
1567 if(strcmp("facility",token
) == 0) {
1568 fac
= malloc(sizeof(facility_t
));
1570 fac
->description
= NULL
;
1571 sequence_init(&(fac
->events
));
1572 table_init(&(fac
->named_types
));
1573 sequence_init(&(fac
->unnamed_types
));
1575 parseFacility(&in
, fac
);
1577 //check if any namedType is not defined
1578 checkNamedTypesImplemented(&fac
->named_types
);
1580 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
1585 printf("facility token was expected in file %s\n", in
.name
);
1596 printf("Cannot find facility %s\n", pathname
);
1603 /* Close the facility */
1604 void ltt_facility_close(facility_t
*fac
)
1608 free(fac
->description
);
1609 freeEvents(&fac
->events
);
1610 sequence_dispose(&fac
->events
);
1611 freeNamedType(&fac
->named_types
);
1612 table_dispose(&fac
->named_types
);
1613 freeTypes(&fac
->unnamed_types
);
1614 sequence_dispose(&fac
->unnamed_types
);
1620 void show_help(int argc
, char ** argv
)
1622 printf("Genevent help : \n");
1624 printf("Use %s name.xml\n", argv
[0]);
1625 printf("to create :\n");
1626 printf("ltt-facility-name.h\n");
1627 printf("ltt-facility-id-name.h\n");
1628 printf("ltt-facility-loader-name.h\n");
1629 printf("ltt-facility-loader-name.c\n");
1630 printf("In the current directory.\n");
1634 /* Parse program arguments */
1636 * 0 : continue program
1637 * -1 : stop program, return 0
1638 * > 0 : stop program, return value as exit.
1640 int check_args(int argc
, char **argv
)
1643 printf("Not enough arguments\n");
1644 show_help(argc
, argv
);
1648 if(strcmp(argv
[1], "-h") == 0) {
1649 show_help(argc
, argv
);
1656 int main(int argc
, char **argv
)
1661 err
= check_args(argc
, argv
);
1662 if(err
> 0) return err
;
1663 else if(err
< 0) return 0;
1665 /* open the facility */
1666 fac
= ltt_facility_open(argv
[1]);
1668 printf("Error opening file %s for reading : %s\n",
1669 argv
[1], strerror(errno
));
1673 /* generate the output C files */
1676 /* ltt-facility-name.h : main logging header.
1678 err
= print_log_header(fac
);
1681 /* ltt-facility-id-name.h : facility id.
1683 err
= print_id_header(fac
);
1686 /* ltt-facility-loader-name.h : facility specific loader info.
1688 err
= print_loader_header(fac
);
1691 /* ltt-facility-loader-name.c : generic faciilty loader
1693 err
= print_loader_c(fac
);
1696 /* close the facility */
1697 ltt_facility_close(fac
);
This page took 0.107155 seconds and 4 git commands to generate.