15518d80507f4c592d38f9b350cd5f9b0e277e4b
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;");
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 void*\n");
1194 fprintf(fd
, "/* 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;
1205 /* Take locks : make sure the trace does not vanish while we write on
1206 * it. A simple preemption disabling is enough (using rcu traces). */
1208 fprintf(fd
, "preempt_disable();\n");
1210 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1212 /* Get facility index */
1214 if(event
->per_tracefile
) {
1216 fprintf(fd
, "index = tracefile_index;\n");
1220 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1221 "\t\t\t\tevent_%s);\n",
1222 fac
->name
, fac
->checksum
, event
->name
);
1227 /* For each trace */
1229 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1231 fprintf(fd
, "if(!trace->active) continue;\n\n");
1233 if(event
->per_trace
) {
1235 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1239 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1241 fprintf(fd
, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1246 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1249 fprintf(fd
, "slot_size = 0;\n");
1251 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf, to_base + to + len, &slot_size, &tsc,\n"
1252 "\t\t&before_hdr_pad, &after_hdr_pad);\n");
1253 /* If error, return */
1255 fprintf(fd
, "if(!buffer) return;\n\n");
1257 /* write data : assume stack alignment is the same as struct alignment. */
1259 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1260 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1261 type_descriptor_t
*type
= field
->type
;
1265 fprintf(fd
, "from = %s;\n", field
->name
);
1267 if(print_type_write(type
,
1268 fd
, 2, basename
, field
->name
)) return 1;
1271 /* Don't forget to flush pending memcpy */
1273 fprintf(fd
, "/* Flush pending memcpy */\n");
1275 fprintf(fd
, "if(len != 0) {\n");
1277 fprintf(fd
, "memcpy(buffer+to_base+to, from, len);\n");
1279 fprintf(fd
, "to += len;\n");
1280 //print_tabs(3, fd);
1281 //fprintf(fd, "from += len;\n");
1283 fprintf(fd
, "len = 0;\n");
1292 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1295 fprintf(fd
, "}\n\n");
1299 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1301 fprintf(fd
, "preempt_enable_no_resched();\n");
1304 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1309 /* ltt-facility-name.h : main logging header.
1312 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1314 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1315 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1317 fprintf(fd
, "/* Facility activation at compile time. */\n");
1318 fprintf(fd
, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1323 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1325 sequence_t
*types
= &fac
->named_types
.values
;
1326 fprintf(fd
, "/* Named types */\n");
1329 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1330 /* For each named type, print the definition */
1331 if((print_type_declaration(types
->array
[i
], fd
,
1332 0, "", ""))) return 1;
1337 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1339 sequence_t
*events
= &fac
->events
;
1340 char basename
[PATH_MAX
];
1341 unsigned int facname_len
;
1343 strncpy(basename
, fac
->name
, PATH_MAX
);
1344 facname_len
= strlen(basename
);
1345 strncat(basename
, "_", PATH_MAX
-facname_len
);
1346 facname_len
= strlen(basename
);
1348 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1349 event_t
*event
= (event_t
*)events
->array
[i
];
1350 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1352 /* For each event, print structure, and then logging function */
1353 fprintf(fd
, "/* Event %s structures */\n",
1355 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1356 /* For each unnamed type, print the definition */
1357 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1358 type_descriptor_t
*t
= f
->type
;
1359 if(t
->type_name
== NULL
)
1360 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1364 fprintf(fd
, "/* Event %s logging function */\n",
1367 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1376 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1378 fprintf(fd
, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1379 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1382 int print_log_header(facility_t
*fac
)
1384 char filename
[PATH_MAX
];
1385 unsigned int filename_size
= 0;
1387 dprintf("%s\n", fac
->name
);
1389 strcpy(filename
, "ltt-facility-");
1390 filename_size
= strlen(filename
);
1392 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1393 filename_size
= strlen(filename
);
1395 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1396 filename_size
= strlen(filename
);
1399 fd
= fopen(filename
, "w");
1401 printf("Error opening file %s for writing : %s\n",
1402 filename
, strerror(errno
));
1406 /* Print file head */
1407 print_log_header_head(fac
, fd
);
1409 /* print named types in declaration order */
1410 if(print_log_header_types(fac
, fd
)) return 1;
1413 if(print_log_header_events(fac
, fd
)) return 1;
1415 /* Print file tail */
1416 print_log_header_tail(fac
, fd
);
1425 /* ltt-facility-id-name.h : facility id.
1427 int print_id_header(facility_t
*fac
)
1429 char filename
[PATH_MAX
];
1430 unsigned int filename_size
= 0;
1432 dprintf("%s\n", fac
->name
);
1434 strcpy(filename
, "ltt-facility-id-");
1435 filename_size
= strlen(filename
);
1437 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1438 filename_size
= strlen(filename
);
1440 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1441 filename_size
= strlen(filename
);
1444 fd
= fopen(filename
, "w");
1446 printf("Error opening file %s for writing : %s\n",
1447 filename
, strerror(errno
));
1457 /* ltt-facility-loader-name.h : facility specific loader info.
1459 int print_loader_header(facility_t
*fac
)
1461 char filename
[PATH_MAX
];
1462 unsigned int filename_size
= 0;
1464 dprintf("%s\n", fac
->name
);
1466 strcpy(filename
, "ltt-facility-loader-");
1467 filename_size
= strlen(filename
);
1469 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1470 filename_size
= strlen(filename
);
1472 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1473 filename_size
= strlen(filename
);
1476 fd
= fopen(filename
, "w");
1478 printf("Error opening file %s for writing : %s\n",
1479 filename
, strerror(errno
));
1488 /* ltt-facility-loader-name.c : generic faciilty loader
1490 int print_loader_c(facility_t
*fac
)
1492 char filename
[PATH_MAX
];
1493 unsigned int filename_size
= 0;
1495 dprintf("%s\n", fac
->name
);
1497 strcpy(filename
, "ltt-facility-loader-");
1498 filename_size
= strlen(filename
);
1500 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1501 filename_size
= strlen(filename
);
1503 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1504 filename_size
= strlen(filename
);
1507 fd
= fopen(filename
, "w");
1509 printf("Error opening file %s for writing : %s\n",
1510 filename
, strerror(errno
));
1523 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1525 /*****************************************************************************
1527 * ltt_facility_open : open facilities
1529 * pathname : the path name of the facility
1531 * Open the facility corresponding to the right checksum.
1533 *returns the facility on success, NULL on error.
1534 ****************************************************************************/
1535 facility_t
*ltt_facility_open(char * pathname
)
1540 facility_t
* fac
= NULL
;
1541 char buffer
[BUFFER_SIZE
];
1542 int generated
= FALSE
;
1544 in
.buffer
= &(buffer
[0]);
1546 in
.error
= error_callback
;
1550 in
.fp
= fopen(in
.name
, "r");
1557 token
= getToken(&in
);
1558 if(in
.type
== ENDFILE
) break;
1561 printf("More than one facility in the file. Only using the first one.\n");
1565 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1566 token
= getName(&in
);
1568 if(strcmp("facility",token
) == 0) {
1569 fac
= malloc(sizeof(facility_t
));
1571 fac
->description
= NULL
;
1572 sequence_init(&(fac
->events
));
1573 table_init(&(fac
->named_types
));
1574 sequence_init(&(fac
->unnamed_types
));
1576 parseFacility(&in
, fac
);
1578 //check if any namedType is not defined
1579 checkNamedTypesImplemented(&fac
->named_types
);
1581 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
1586 printf("facility token was expected in file %s\n", in
.name
);
1597 printf("Cannot find facility %s\n", pathname
);
1604 /* Close the facility */
1605 void ltt_facility_close(facility_t
*fac
)
1609 free(fac
->description
);
1610 freeEvents(&fac
->events
);
1611 sequence_dispose(&fac
->events
);
1612 freeNamedType(&fac
->named_types
);
1613 table_dispose(&fac
->named_types
);
1614 freeTypes(&fac
->unnamed_types
);
1615 sequence_dispose(&fac
->unnamed_types
);
1621 void show_help(int argc
, char ** argv
)
1623 printf("Genevent help : \n");
1625 printf("Use %s name.xml\n", argv
[0]);
1626 printf("to create :\n");
1627 printf("ltt-facility-name.h\n");
1628 printf("ltt-facility-id-name.h\n");
1629 printf("ltt-facility-loader-name.h\n");
1630 printf("ltt-facility-loader-name.c\n");
1631 printf("In the current directory.\n");
1635 /* Parse program arguments */
1637 * 0 : continue program
1638 * -1 : stop program, return 0
1639 * > 0 : stop program, return value as exit.
1641 int check_args(int argc
, char **argv
)
1644 printf("Not enough arguments\n");
1645 show_help(argc
, argv
);
1649 if(strcmp(argv
[1], "-h") == 0) {
1650 show_help(argc
, argv
);
1657 int main(int argc
, char **argv
)
1662 err
= check_args(argc
, argv
);
1663 if(err
> 0) return err
;
1664 else if(err
< 0) return 0;
1666 /* open the facility */
1667 fac
= ltt_facility_open(argv
[1]);
1669 printf("Error opening file %s for reading : %s\n",
1670 argv
[1], strerror(errno
));
1674 /* generate the output C files */
1677 /* ltt-facility-name.h : main logging header.
1679 err
= print_log_header(fac
);
1682 /* ltt-facility-id-name.h : facility id.
1684 err
= print_id_header(fac
);
1687 /* ltt-facility-loader-name.h : facility specific loader info.
1689 err
= print_loader_header(fac
);
1692 /* ltt-facility-loader-name.c : generic faciilty loader
1694 err
= print_loader_c(fac
);
1697 /* close the facility */
1698 ltt_facility_close(fac
);
This page took 0.102937 seconds and 4 git commands to generate.