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: ",\
91 void print_tabs(unsigned int tabs
, FILE *fd
)
93 for(unsigned int i
= 0; i
<tabs
;i
++)
99 * Copied from construct_types_and_fields in LTTV facility.c */
101 int print_type(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
102 char *nest_name
, char *field_name
)
104 char basename
[PATH_MAX
];
105 unsigned int basename_len
= 0;
107 strcpy(basename
, nest_name
);
108 basename_len
= strlen(basename
);
110 /* For a named type, we use the type_name directly */
111 if(td
->type_name
!= NULL
) {
112 strncpy(basename
, td
->type_name
, PATH_MAX
);
113 basename_len
= strlen(basename
);
115 /* For a unnamed type, there must be a field name */
116 if((basename_len
!= 0)
117 && (basename
[basename_len
-1] != '_')
118 && (field_name
[0] != '\0')) {
119 strncat(basename
, "_", PATH_MAX
- basename_len
);
120 basename_len
= strlen(basename
);
122 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
127 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
130 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
133 fprintf(fd
, "signed char");
136 fprintf(fd
, "unsigned char");
139 fprintf(fd
, "short");
142 fprintf(fd
, "unsigned short");
148 fprintf(fd
, "unsigned int");
151 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
154 fprintf(fd
, "void *");
160 fprintf(fd
, "unsigned long");
163 fprintf(fd
, "size_t");
166 fprintf(fd
, "ssize_t");
169 fprintf(fd
, "off_t");
172 fprintf(fd
, "char *");
175 fprintf(fd
, "enum lttng_%s", basename
);
178 fprintf(fd
, "lttng_array_%s", basename
);
181 fprintf(fd
, "lttng_sequence_%s", basename
);
184 fprintf(fd
, "struct lttng_%s", basename
);
187 fprintf(fd
, "union lttng_%s", basename
);
190 printf("print_type : unknown type\n");
197 /* Print logging function argument */
198 int print_arg(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
199 char *nest_name
, char *field_name
)
201 char basename
[PATH_MAX
];
202 unsigned int basename_len
= 0;
204 strcpy(basename
, nest_name
);
205 basename_len
= strlen(basename
);
207 /* For a named type, we use the type_name directly */
208 if(td
->type_name
!= NULL
) {
209 strncpy(basename
, td
->type_name
, PATH_MAX
);
210 basename_len
= strlen(basename
);
212 /* For a unnamed type, there must be a field name */
213 if((basename_len
!= 0)
214 && (basename
[basename_len
-1] != '_')
215 && (field_name
[0] != '\0')) {
216 strncat(basename
, "_", PATH_MAX
- basename_len
);
217 basename_len
= strlen(basename
);
219 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
222 print_tabs(tabs
, fd
);
226 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
227 fprintf(fd
, " %s", field_name
);
230 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
231 fprintf(fd
, " %s", field_name
);
234 fprintf(fd
, "signed char");
235 fprintf(fd
, " %s", field_name
);
238 fprintf(fd
, "unsigned char");
239 fprintf(fd
, " %s", field_name
);
242 fprintf(fd
, "short");
243 fprintf(fd
, " %s", field_name
);
246 fprintf(fd
, "unsigned short");
247 fprintf(fd
, " %s", field_name
);
251 fprintf(fd
, " %s", field_name
);
254 fprintf(fd
, "unsigned int");
255 fprintf(fd
, " %s", field_name
);
258 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
259 fprintf(fd
, " %s", field_name
);
262 fprintf(fd
, "void *");
263 fprintf(fd
, " %s", field_name
);
267 fprintf(fd
, " %s", field_name
);
270 fprintf(fd
, "unsigned long");
271 fprintf(fd
, " %s", field_name
);
274 fprintf(fd
, "size_t");
275 fprintf(fd
, " %s", field_name
);
278 fprintf(fd
, "ssize_t");
279 fprintf(fd
, " %s", field_name
);
282 fprintf(fd
, "off_t");
283 fprintf(fd
, " %s", field_name
);
286 fprintf(fd
, "char *");
287 fprintf(fd
, " %s", field_name
);
290 fprintf(fd
, "enum lttng_%s", basename
);
291 fprintf(fd
, " %s", field_name
);
294 fprintf(fd
, "lttng_array_%s", basename
);
295 fprintf(fd
, " %s", field_name
);
298 fprintf(fd
, "lttng_sequence_%s *", basename
);
299 fprintf(fd
, " %s", field_name
);
302 fprintf(fd
, "struct lttng_%s *", basename
);
303 fprintf(fd
, " %s", field_name
);
306 fprintf(fd
, "union lttng_%s *", basename
);
307 fprintf(fd
, " %s", field_name
);
310 printf("print_type : unknown type\n");
318 /* Does the type has a fixed size ? (as know from the compiler)
321 * 0 : variable length
323 int has_type_fixed_size(type_descriptor_t
*td
)
342 case UNION
: /* The union must have fixed size children. Must be checked by
352 int has_type_fixed
= 0;
353 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
354 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
355 type_descriptor_t
*type
= field
->type
;
357 has_type_fixed
= has_type_fixed_size(type
);
358 if(!has_type_fixed
) return 0;
364 assert(td
->size
>= 0);
365 return has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
);
368 printf("There is a type defined to NONE : bad.\n");
372 return 0; //make gcc happy.
379 /* print type declaration.
381 * Copied from construct_types_and_fields in LTTV facility.c */
383 int print_type_declaration(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
384 char *nest_name
, char *field_name
)
386 char basename
[PATH_MAX
];
387 unsigned int basename_len
= 0;
389 strncpy(basename
, nest_name
, PATH_MAX
);
390 basename_len
= strlen(basename
);
392 /* For a named type, we use the type_name directly */
393 if(td
->type_name
!= NULL
) {
394 strncpy(basename
, td
->type_name
, PATH_MAX
);
395 basename_len
= strlen(basename
);
397 /* For a unnamed type, there must be a field name, except for
399 if((basename_len
!= 0)
400 && (basename
[basename_len
-1] != '_'
401 && (field_name
[0] != '\0'))) {
402 strncat(basename
, "_", PATH_MAX
- basename_len
);
403 basename_len
= strlen(basename
);
405 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
410 fprintf(fd
, "enum lttng_%s", basename
);
412 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
414 fprintf(fd
, "LTTNG_%s", ((char*)(td
->labels
.array
[i
])));
422 dprintf("%s\n", basename
);
423 assert(td
->size
>= 0);
424 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
425 /* Not a named nested type : we must print its declaration first */
426 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
427 fd
, 0, basename
, "")) return 1;
429 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename
,
431 fprintf(fd
, "typedef ");
432 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
433 fd
, tabs
, basename
, "")) return 1;
434 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
439 /* We assume that the sequence length type does not need to be declared.
441 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
442 /* Not a named nested type : we must print its declaration first */
443 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
444 fd
, 0, basename
, "")) return 1;
446 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
449 fprintf(fd
, "struct lttng_sequence_%s", basename
);
452 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
453 fd
, tabs
, basename
, "")) return 1;
454 fprintf(fd
, "len;\n");
456 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
457 fd
, tabs
, basename
, "")) return 1;
458 fprintf(fd
, " *array;\n");
464 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
465 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
466 type_descriptor_t
*type
= field
->type
;
467 if(type
->type_name
== NULL
) {
468 /* Not a named nested type : we must print its declaration first */
469 if(print_type_declaration(type
,
470 fd
, 0, basename
, field
->name
)) return 1;
473 fprintf(fd
, "struct lttng_%s", basename
);
475 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
476 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
477 type_descriptor_t
*type
= field
->type
;
479 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
481 fprintf(fd
, "%s", field
->name
);
488 /* TODO : Do not allow variable length fields in a union */
489 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
490 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
491 type_descriptor_t
*type
= field
->type
;
492 if(type
->type_name
== NULL
) {
493 /* Not a named nested type : we must print its declaration first */
494 if(print_type_declaration(type
,
495 fd
, 0, basename
, field
->name
)) return 1;
498 fprintf(fd
, "union lttng_%s", basename
);
500 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
501 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
502 type_descriptor_t
*type
= field
->type
;
504 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
506 fprintf(fd
, "%s", field
->name
);
513 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
521 /* print type alignment.
523 * Copied from construct_types_and_fields in LTTV facility.c
525 * basename is the name which identifies the type (along with a prefix
528 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
529 char *nest_name
, char *field_name
)
531 char basename
[PATH_MAX
];
532 unsigned int basename_len
= 0;
534 strncpy(basename
, nest_name
, PATH_MAX
);
535 basename_len
= strlen(basename
);
537 /* For a named type, we use the type_name directly */
538 if(td
->type_name
!= NULL
) {
539 strncpy(basename
, td
->type_name
, PATH_MAX
);
540 basename_len
= strlen(basename
);
542 /* For a unnamed type, there must be a field name, except for
544 if((basename_len
!= 0)
545 && (basename
[basename_len
-1] != '_'
546 && (field_name
[0] != '\0'))) {
547 strncat(basename
, "_", PATH_MAX
- basename_len
);
548 basename_len
= strlen(basename
);
550 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
570 fprintf(fd
, "sizeof(");
571 if(print_type(td
, fd
, 0, basename
, "")) return 1;
575 fprintf(fd
, "sizeof(char)");
578 fprintf(fd
, "lttng_get_alignment_sequence_%s(&obj->%s)", basename
,
582 fprintf(fd
, "lttng_get_alignment_struct_%s(&obj->%s)", basename
,
586 fprintf(fd
, "lttng_get_alignment_union_%s(&obj->%s)", basename
,
590 fprintf(fd
, "lttng_get_alignment_array_%s(obj->%s)", basename
,
594 printf("error : type NONE unexpected\n");
604 * Copied from construct_types_and_fields in LTTV facility.c
606 * basename is the name which identifies the type (along with a prefix
609 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
610 char *nest_name
, char *field_name
)
612 char basename
[PATH_MAX
];
613 unsigned int basename_len
= 0;
615 strncpy(basename
, nest_name
, PATH_MAX
);
616 basename_len
= strlen(basename
);
618 /* For a named type, we use the type_name directly */
619 if(td
->type_name
!= NULL
) {
620 strncpy(basename
, td
->type_name
, PATH_MAX
);
621 basename_len
= strlen(basename
);
623 /* For a unnamed type, there must be a field name, except for
625 if((basename_len
!= 0)
626 && (basename
[basename_len
-1] != '_'
627 && (field_name
[0] != '\0'))) {
628 strncat(basename
, "_", PATH_MAX
- basename_len
);
629 basename_len
= strlen(basename
);
631 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
651 print_tabs(tabs
, fd
);
652 fprintf(fd
, "size = ");
653 fprintf(fd
, "sizeof(");
654 if(print_type(td
, fd
, 0, basename
, "")) return 1;
656 print_tabs(tabs
, fd
);
657 fprintf(fd
, "size += ltt_align(*to+*len, size) + size;\n");
658 print_tabs(tabs
, fd
);
659 fprintf(fd
, "*len += size;");
662 print_tabs(tabs
, fd
);
663 fprintf(fd
, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n", basename
, field_name
);
666 print_tabs(tabs
, fd
);
667 fprintf(fd
, "lttng_write_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
671 print_tabs(tabs
, fd
);
672 fprintf(fd
, "lttng_write_struct_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
676 print_tabs(tabs
, fd
);
677 fprintf(fd
, "lttng_write_union_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
681 print_tabs(tabs
, fd
);
682 fprintf(fd
, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
686 printf("Error : type NONE unexpected\n");
696 /* print type alignment function.
698 * Copied from construct_types_and_fields in LTTV facility.c
700 * basename is the name which identifies the type (along with a prefix
703 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
705 char *nest_name
, char *field_name
)
707 char basename
[PATH_MAX
];
708 unsigned int basename_len
= 0;
710 strncpy(basename
, nest_name
, PATH_MAX
);
711 basename_len
= strlen(basename
);
713 /* For a named type, we use the type_name directly */
714 if(td
->type_name
!= NULL
) {
715 strncpy(basename
, td
->type_name
, PATH_MAX
);
716 basename_len
= strlen(basename
);
718 /* For a unnamed type, there must be a field name, except for
720 if((basename_len
!= 0)
721 && (basename
[basename_len
-1] != '_'
722 && (field_name
[0] != '\0'))) {
723 strncat(basename
, "_", PATH_MAX
- basename_len
);
724 basename_len
= strlen(basename
);
726 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
731 /* Function header */
732 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
735 if(print_type(td
, fd
, 0, basename
, "")) return 1;
736 fprintf(fd
, " *obj)\n");
739 fprintf(fd
, "size_t align=0, localign;");
742 fprintf(fd
, "localign = ");
743 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
744 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
747 fprintf(fd
, "align = max(align, localign);\n");
750 fprintf(fd
, "localign = ");
751 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
752 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
755 fprintf(fd
, "align = max(align, localign);\n");
758 fprintf(fd
, "return align;\n");
761 /* Function header */
762 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
765 if(print_type(td
, fd
, 0, basename
, "")) return 1;
766 fprintf(fd
, " *obj)\n");
769 fprintf(fd
, "size_t align=0, localign;");
771 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
772 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
773 type_descriptor_t
*type
= field
->type
;
775 fprintf(fd
, "localign = ");
776 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
779 fprintf(fd
, "align = max(align, localign);\n");
783 fprintf(fd
, "return align;\n");
787 /* Function header */
788 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
791 if(print_type(td
, fd
, 0, basename
, "")) return 1;
792 fprintf(fd
, " *obj)\n");
795 fprintf(fd
, "size_t align=0, localign;");
797 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
798 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
799 type_descriptor_t
*type
= field
->type
;
801 fprintf(fd
, "localign = ");
802 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
805 fprintf(fd
, "align = max(align, localign);\n");
809 fprintf(fd
, "return align;\n");
813 /* Function header */
814 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
817 if(print_type(td
, fd
, 0, basename
, "")) return 1;
818 fprintf(fd
, " obj)\n");
821 fprintf(fd
, "return \n");
822 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
823 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
827 printf("print_type_alignment_fct : type has no alignment function.\n");
832 /* Function footer */
839 /* print type write function.
841 * Copied from construct_types_and_fields in LTTV facility.c
843 * basename is the name which identifies the type (along with a prefix
846 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
847 char *nest_name
, char *field_name
)
849 char basename
[PATH_MAX
];
850 unsigned int basename_len
= 0;
852 strncpy(basename
, nest_name
, PATH_MAX
);
853 basename_len
= strlen(basename
);
855 /* For a named type, we use the type_name directly */
856 if(td
->type_name
!= NULL
) {
857 strncpy(basename
, td
->type_name
, PATH_MAX
);
858 basename_len
= strlen(basename
);
860 /* For a unnamed type, there must be a field name, except for
862 if((basename_len
!= 0)
863 && (basename
[basename_len
-1] != '_'
864 && (field_name
[0] != '\0'))) {
865 strncat(basename
, "_", PATH_MAX
- basename_len
);
866 basename_len
= strlen(basename
);
868 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
874 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
877 fprintf(fd
, "lttng_get_alignment_sequence_%s(&obj->%s)", basename
,
881 fprintf(fd
, "lttng_get_alignment_struct_%s(&obj->%s)", basename
,
885 fprintf(fd
, "lttng_get_alignment_union_%s(&obj->%s)", basename
,
889 fprintf(fd
, "lttng_get_alignment_array_%s(obj->%s)", basename
,
893 printf("print_type_write_fct : type has no write function.\n");
898 fprintf(fd
, "void *buffer,\n");
900 fprintf(fd
, "size_t *to_base,\n");
902 fprintf(fd
, "size_t *to,\n");
904 fprintf(fd
, "void **from,\n");
906 fprintf(fd
, "size_t *len,\n");
908 if(print_type(td
, fd
, 0, basename
, "")) return 1;
912 fprintf(fd
, " *obj)\n");
915 fprintf(fd
, " *obj)\n");
918 fprintf(fd
, " *obj)\n");
921 fprintf(fd
, " obj)\n");
924 printf("print_type_write_fct : type has no write function.\n");
930 fprintf(fd
, "size_t align, size;\n");
937 fprintf(fd
, "/* Flush pending memcpy */\n");
939 fprintf(fd
, "if(*len != 0) {\n");
941 fprintf(fd
, "if(buffer != NULL)\n");
943 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
947 fprintf(fd
, "*to += *len;\n");
949 fprintf(fd
, "*len = 0;\n");
957 printf("print_type_write_fct : type has no write function.\n");
962 fprintf(fd
, "align = \n");
963 if(print_type_alignment(td
, fd
, 0, basename
, field_name
)) return 1;
967 fprintf(fd
, "if(*len == 0) {\n");
969 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
971 fprintf(fd
, "} else {\n");
973 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
978 /* First, check if the type has a fixed size. If it is the case, then the size
979 * to write is know by the compiler : simply use a sizeof() */
980 if(has_type_fixed_size(td
)) {
982 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
985 fprintf(fd
, "*len += sizeof(");
986 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
989 /* The type contains nested variable size subtypes :
990 * we must write field by field. */
992 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
998 fprintf(fd
, "/* Copy members */\n");
1000 fprintf(fd
, "size = sizeof(\n");
1001 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1002 fd
, 1, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
1003 fprintf(fd
, ");\n");
1005 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1007 fprintf(fd
, "if(buffer != NULL)\n");
1009 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, size);\n");
1011 fprintf(fd
, "*to += size;\n");
1014 /* Write the child : varlen child or not ? */
1015 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1016 /* Fixed size len child : use a multiplication of its size */
1018 fprintf(fd
, "size = sizeof(\n");
1019 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1020 fd
, 1, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1021 fprintf(fd
, ");\n");
1023 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1025 fprintf(fd
, "size = obj->len * size;\n");
1027 fprintf(fd
, "if(buffer != NULL)\n");
1029 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, size);\n");
1031 fprintf(fd
, "*to += size;\n");
1035 fprintf(fd
, "/* Variable length child : iter. */\n");
1037 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1038 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1039 fd
, 2, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1045 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1047 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1049 fprintf(fd
, "*to_base = *to_base+*to;\n");
1051 fprintf(fd
, "*to = 0;\n");
1053 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1055 fprintf(fd
, "*from = obj+1;\n");
1058 fprintf(fd
, "size = strlen(obj);\n");
1060 fprintf(fd
, "if(buffer != NULL)\n");
1062 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1064 fprintf(fd
, "*to += size;\n");
1067 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1069 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1071 fprintf(fd
, "*to_base = *to_base+*to;\n");
1073 fprintf(fd
, "*to = 0;\n");
1075 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1077 fprintf(fd
, "*from = obj+1;\n");
1080 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1081 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1082 type_descriptor_t
*type
= field
->type
;
1083 if(print_type_write(type
,
1084 fd
, 1, basename
, field
->name
)) return 1;
1089 printf("ERROR : A union CANNOT contain a variable size child.\n");
1093 /* Write the child : varlen child or not ? */
1094 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1095 /* Error : if an array has a variable size, then its child must also
1096 * have a variable size. */
1100 fprintf(fd
, "/* Variable length child : iter. */\n");
1102 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1103 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1104 fd
, 2, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1110 printf("print_type_write_fct : type has no write function.\n");
1118 /* Function footer */
1126 /* Print the logging function of an event. This is the core of genevent */
1127 int print_event_logging_function(char *basename
, facility_t
*fac
,
1128 event_t
*event
, FILE *fd
)
1130 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1131 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1132 /* For each field, print the function argument */
1133 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1134 type_descriptor_t
*t
= f
->type
;
1135 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1136 if(j
< event
->fields
.position
-1) {
1141 if(event
->fields
.position
== 0) {
1143 fprintf(fd
, "void");
1146 fprintf(fd
, "#ifndef CONFIG_LTT\n");
1149 fprintf(fd
,"#else\n");
1151 /* Print the function variables */
1153 fprintf(fd
, "unsigned int index;\n");
1155 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1157 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1159 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1161 fprintf(fd
, "void *buffer = NULL;\n");
1163 fprintf(fd
, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1165 fprintf(fd
, "size_t to = 0;\n");
1167 fprintf(fd
, "void *from;");
1169 fprintf(fd
, "size_t len = 0;\n");
1171 fprintf(fd
, "size_t slot_size;\n");
1173 fprintf(fd
, "cycles_t tsc;\n");
1175 fprintf(fd
, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
1179 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1182 /* Calculate event variable len + event data alignment offset.
1183 * Assume that the padding for alignment starts at a void*
1185 * This excludes the header size and alignment. */
1188 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1190 fprintf(fd
, "/* size = to_base + to + len */\n");
1192 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1194 fprintf(fd
, " * sizeof(void *) address. */\n");
1197 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1198 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1199 type_descriptor_t
*type
= field
->type
;
1200 if(print_type_write(type
,
1201 fd
, 1, basename
, field
->name
)) return 1;
1206 /* Take locks : make sure the trace does not vanish while we write on
1207 * it. A simple preemption disabling is enough (using rcu traces). */
1209 fprintf(fd
, "preempt_disable();\n");
1211 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1213 /* Get facility index */
1215 if(event
->per_tracefile
) {
1217 fprintf(fd
, "index = tracefile_index;\n");
1221 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1222 "\t\t\t\t\t\tevent_%s);\n",
1223 fac
->name
, fac
->checksum
, event
->name
);
1228 /* For each trace */
1230 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1232 fprintf(fd
, "if(!trace->active) continue;\n\n");
1234 if(event
->per_trace
) {
1236 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1240 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1242 fprintf(fd
, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1247 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1250 fprintf(fd
, "slot_size = 0;\n");
1252 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1254 fprintf(fd
, "to_base + to + len, &slot_size, &tsc,\n");
1256 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad);\n");
1257 /* If error, return */
1259 fprintf(fd
, "if(!buffer) return;\n\n");
1261 /* write data : assume stack alignment is the same as struct alignment. */
1263 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1264 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1265 type_descriptor_t
*type
= field
->type
;
1269 switch(type
->type
) {
1275 fprintf(fd
, "from = %s;\n", field
->name
);
1278 fprintf(fd
, "from = &%s;\n", field
->name
);
1283 if(print_type_write(type
,
1284 fd
, 2, basename
, field
->name
)) return 1;
1287 /* Don't forget to flush pending memcpy */
1289 fprintf(fd
, "/* Flush pending memcpy */\n");
1291 fprintf(fd
, "if(len != 0) {\n");
1293 fprintf(fd
, "memcpy(buffer+to_base+to, from, len);\n");
1295 fprintf(fd
, "to += len;\n");
1296 //print_tabs(3, fd);
1297 //fprintf(fd, "from += len;\n");
1299 fprintf(fd
, "len = 0;\n");
1308 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1311 fprintf(fd
, "}\n\n");
1315 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1317 fprintf(fd
, "preempt_enable_no_resched();\n");
1320 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1325 /* ltt-facility-name.h : main logging header.
1328 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1330 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1331 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1333 fprintf(fd
, "/* Facility activation at compile time. */\n");
1334 fprintf(fd
, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1339 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1341 sequence_t
*types
= &fac
->named_types
.values
;
1342 fprintf(fd
, "/* Named types */\n");
1345 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1346 /* For each named type, print the definition */
1347 if((print_type_declaration(types
->array
[i
], fd
,
1348 0, "", ""))) return 1;
1353 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1355 sequence_t
*events
= &fac
->events
;
1356 char basename
[PATH_MAX
];
1357 unsigned int facname_len
;
1359 strncpy(basename
, fac
->name
, PATH_MAX
);
1360 facname_len
= strlen(basename
);
1361 strncat(basename
, "_", PATH_MAX
-facname_len
);
1362 facname_len
= strlen(basename
);
1364 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1365 event_t
*event
= (event_t
*)events
->array
[i
];
1366 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1368 /* For each event, print structure, and then logging function */
1369 fprintf(fd
, "/* Event %s structures */\n",
1371 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1372 /* For each unnamed type, print the definition */
1373 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1374 type_descriptor_t
*t
= f
->type
;
1375 if(t
->type_name
== NULL
)
1376 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1380 fprintf(fd
, "/* Event %s logging function */\n",
1383 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1392 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1394 fprintf(fd
, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1395 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1398 int print_log_header(facility_t
*fac
)
1400 char filename
[PATH_MAX
];
1401 unsigned int filename_size
= 0;
1403 dprintf("%s\n", fac
->name
);
1405 strcpy(filename
, "ltt-facility-");
1406 filename_size
= strlen(filename
);
1408 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1409 filename_size
= strlen(filename
);
1411 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1412 filename_size
= strlen(filename
);
1415 fd
= fopen(filename
, "w");
1417 printf("Error opening file %s for writing : %s\n",
1418 filename
, strerror(errno
));
1422 /* Print file head */
1423 print_log_header_head(fac
, fd
);
1425 /* print named types in declaration order */
1426 if(print_log_header_types(fac
, fd
)) return 1;
1429 if(print_log_header_events(fac
, fd
)) return 1;
1431 /* Print file tail */
1432 print_log_header_tail(fac
, fd
);
1441 /* ltt-facility-id-name.h : facility id.
1443 int print_id_header(facility_t
*fac
)
1445 char filename
[PATH_MAX
];
1446 unsigned int filename_size
= 0;
1448 dprintf("%s\n", fac
->name
);
1450 strcpy(filename
, "ltt-facility-id-");
1451 filename_size
= strlen(filename
);
1453 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1454 filename_size
= strlen(filename
);
1456 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1457 filename_size
= strlen(filename
);
1460 fd
= fopen(filename
, "w");
1462 printf("Error opening file %s for writing : %s\n",
1463 filename
, strerror(errno
));
1473 /* ltt-facility-loader-name.h : facility specific loader info.
1475 int print_loader_header(facility_t
*fac
)
1477 char filename
[PATH_MAX
];
1478 unsigned int filename_size
= 0;
1480 dprintf("%s\n", fac
->name
);
1482 strcpy(filename
, "ltt-facility-loader-");
1483 filename_size
= strlen(filename
);
1485 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1486 filename_size
= strlen(filename
);
1488 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1489 filename_size
= strlen(filename
);
1492 fd
= fopen(filename
, "w");
1494 printf("Error opening file %s for writing : %s\n",
1495 filename
, strerror(errno
));
1504 /* ltt-facility-loader-name.c : generic faciilty loader
1506 int print_loader_c(facility_t
*fac
)
1508 char filename
[PATH_MAX
];
1509 unsigned int filename_size
= 0;
1511 dprintf("%s\n", fac
->name
);
1513 strcpy(filename
, "ltt-facility-loader-");
1514 filename_size
= strlen(filename
);
1516 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1517 filename_size
= strlen(filename
);
1519 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1520 filename_size
= strlen(filename
);
1523 fd
= fopen(filename
, "w");
1525 printf("Error opening file %s for writing : %s\n",
1526 filename
, strerror(errno
));
1539 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1541 /*****************************************************************************
1543 * ltt_facility_open : open facilities
1545 * pathname : the path name of the facility
1547 * Open the facility corresponding to the right checksum.
1549 *returns the facility on success, NULL on error.
1550 ****************************************************************************/
1551 facility_t
*ltt_facility_open(char * pathname
)
1556 facility_t
* fac
= NULL
;
1557 char buffer
[BUFFER_SIZE
];
1558 int generated
= FALSE
;
1560 in
.buffer
= &(buffer
[0]);
1562 in
.error
= error_callback
;
1566 in
.fp
= fopen(in
.name
, "r");
1573 token
= getToken(&in
);
1574 if(in
.type
== ENDFILE
) break;
1577 printf("More than one facility in the file. Only using the first one.\n");
1581 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1582 token
= getName(&in
);
1584 if(strcmp("facility",token
) == 0) {
1585 fac
= malloc(sizeof(facility_t
));
1587 fac
->description
= NULL
;
1588 sequence_init(&(fac
->events
));
1589 table_init(&(fac
->named_types
));
1590 sequence_init(&(fac
->unnamed_types
));
1592 parseFacility(&in
, fac
);
1594 //check if any namedType is not defined
1595 checkNamedTypesImplemented(&fac
->named_types
);
1597 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
1602 printf("facility token was expected in file %s\n", in
.name
);
1613 printf("Cannot find facility %s\n", pathname
);
1620 /* Close the facility */
1621 void ltt_facility_close(facility_t
*fac
)
1625 free(fac
->description
);
1626 freeEvents(&fac
->events
);
1627 sequence_dispose(&fac
->events
);
1628 freeNamedType(&fac
->named_types
);
1629 table_dispose(&fac
->named_types
);
1630 freeTypes(&fac
->unnamed_types
);
1631 sequence_dispose(&fac
->unnamed_types
);
1637 void show_help(int argc
, char ** argv
)
1639 printf("Genevent help : \n");
1641 printf("Use %s name.xml\n", argv
[0]);
1642 printf("to create :\n");
1643 printf("ltt-facility-name.h\n");
1644 printf("ltt-facility-id-name.h\n");
1645 printf("ltt-facility-loader-name.h\n");
1646 printf("ltt-facility-loader-name.c\n");
1647 printf("In the current directory.\n");
1651 /* Parse program arguments */
1653 * 0 : continue program
1654 * -1 : stop program, return 0
1655 * > 0 : stop program, return value as exit.
1657 int check_args(int argc
, char **argv
)
1660 printf("Not enough arguments\n");
1661 show_help(argc
, argv
);
1665 if(strcmp(argv
[1], "-h") == 0) {
1666 show_help(argc
, argv
);
1673 int main(int argc
, char **argv
)
1678 err
= check_args(argc
, argv
);
1679 if(err
> 0) return err
;
1680 else if(err
< 0) return 0;
1682 /* open the facility */
1683 fac
= ltt_facility_open(argv
[1]);
1685 printf("Error opening file %s for reading : %s\n",
1686 argv
[1], strerror(errno
));
1690 /* generate the output C files */
1693 /* ltt-facility-name.h : main logging header.
1695 err
= print_log_header(fac
);
1698 /* ltt-facility-id-name.h : facility id.
1700 err
= print_id_header(fac
);
1703 /* ltt-facility-loader-name.h : facility specific loader info.
1705 err
= print_loader_header(fac
);
1708 /* ltt-facility-loader-name.c : generic faciilty loader
1710 err
= print_loader_c(fac
);
1713 /* close the facility */
1714 ltt_facility_close(fac
);
This page took 0.104893 seconds and 4 git commands to generate.