7f06edaa70fe8d0e5ed962ba4fcfedde72228cc2
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");
459 fprintf(fd
, "};\n"); /* We do not LTT_ALIGN, because we never copy
460 it to the buffer directly. */
465 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
466 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
467 type_descriptor_t
*type
= field
->type
;
468 if(type
->type_name
== NULL
) {
469 /* Not a named nested type : we must print its declaration first */
470 if(print_type_declaration(type
,
471 fd
, 0, basename
, field
->name
)) return 1;
474 fprintf(fd
, "struct lttng_%s", basename
);
476 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
477 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
478 type_descriptor_t
*type
= field
->type
;
480 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
482 fprintf(fd
, "%s", field
->name
);
485 fprintf(fd
, "} LTT_ALIGN;\n");
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
);
509 fprintf(fd
, "} LTT_ALIGN;\n");
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
!= NULL
547 && (field_name
[0] != '\0'))) {
548 strncat(basename
, "_", PATH_MAX
- basename_len
);
549 basename_len
= strlen(basename
);
551 if(field_name
!= NULL
)
552 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
572 fprintf(fd
, "sizeof(");
573 if(print_type(td
, fd
, 0, basename
, "")) return 1;
577 fprintf(fd
, "sizeof(char)");
580 fprintf(fd
, "lttng_get_alignment_sequence_%s(&obj->%s)", basename
,
584 fprintf(fd
, "lttng_get_alignment_struct_%s(&obj->%s)", basename
,
588 fprintf(fd
, "lttng_get_alignment_union_%s(&obj->%s)", basename
,
592 fprintf(fd
, "lttng_get_alignment_array_%s(obj->%s)", basename
,
596 printf("error : type NONE unexpected\n");
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
, fd
, 0, basename
, "")) return 1;
658 print_tabs(tabs
, fd
);
659 fprintf(fd
, "size += ltt_align(*to+*len, size) + size;\n");
660 print_tabs(tabs
, fd
);
661 fprintf(fd
, "*len += size;");
664 print_tabs(tabs
, fd
);
665 fprintf(fd
, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n", basename
, field_name
);
668 print_tabs(tabs
, fd
);
669 fprintf(fd
, "lttng_write_%s(buffer, to_base, to, from, len, &obj->%s)", basename
,
673 print_tabs(tabs
, fd
);
674 fprintf(fd
, "lttng_write_struct_%s(buffer, to_base, to, from, len, &obj->%s)", basename
,
678 print_tabs(tabs
, fd
);
679 fprintf(fd
, "lttng_write_union_%s(buffer, to_base, to, from, len, &obj->%s)", basename
,
683 print_tabs(tabs
, fd
);
684 fprintf(fd
, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename
,
688 printf("Error : type NONE unexpected\n");
698 /* print type alignment function.
700 * Copied from construct_types_and_fields in LTTV facility.c
702 * basename is the name which identifies the type (along with a prefix
705 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
707 char *nest_name
, char *field_name
)
709 char basename
[PATH_MAX
];
710 unsigned int basename_len
= 0;
712 strncpy(basename
, nest_name
, PATH_MAX
);
713 basename_len
= strlen(basename
);
715 /* For a named type, we use the type_name directly */
716 if(td
->type_name
!= NULL
) {
717 strncpy(basename
, td
->type_name
, PATH_MAX
);
718 basename_len
= strlen(basename
);
720 /* For a unnamed type, there must be a field name, except for
722 if((basename_len
!= 0)
723 && (basename
[basename_len
-1] != '_'
724 && (field_name
[0] != '\0'))) {
725 strncat(basename
, "_", PATH_MAX
- basename_len
);
726 basename_len
= strlen(basename
);
728 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
733 /* Function header */
734 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
737 if(print_type(td
, fd
, 0, basename
, "")) return 1;
738 fprintf(fd
, " *obj)\n");
741 fprintf(fd
, "size_t align=0, localign;");
744 fprintf(fd
, "localign = ");
745 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
746 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
749 fprintf(fd
, "align = max(align, localign);\n");
752 fprintf(fd
, "localign = ");
753 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
754 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
757 fprintf(fd
, "align = max(align, localign);\n");
760 fprintf(fd
, "return align;\n");
763 /* Function header */
764 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
767 if(print_type(td
, fd
, 0, basename
, "")) return 1;
768 fprintf(fd
, " *obj)\n");
771 fprintf(fd
, "size_t align=0, localign;");
773 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
774 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
775 type_descriptor_t
*type
= field
->type
;
777 fprintf(fd
, "localign = ");
778 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
781 fprintf(fd
, "align = max(align, localign);\n");
785 fprintf(fd
, "return align;\n");
789 /* Function header */
790 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
793 if(print_type(td
, fd
, 0, basename
, "")) return 1;
794 fprintf(fd
, " *obj)\n");
797 fprintf(fd
, "size_t align=0, localign;");
799 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
800 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
801 type_descriptor_t
*type
= field
->type
;
803 fprintf(fd
, "localign = ");
804 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
)) return 1;
807 fprintf(fd
, "align = max(align, localign);\n");
811 fprintf(fd
, "return align;\n");
815 /* Function header */
816 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
819 if(print_type(td
, fd
, 0, basename
, "")) return 1;
820 fprintf(fd
, " obj)\n");
823 fprintf(fd
, "return \n");
824 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
825 fd
, 0, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
829 printf("print_type_alignment_fct : type has no alignment function.\n");
835 /* Function footer */
842 /* print type write function.
844 * Copied from construct_types_and_fields in LTTV facility.c
846 * basename is the name which identifies the type (along with a prefix
849 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
850 char *nest_name
, char *field_name
)
852 char basename
[PATH_MAX
];
853 unsigned int basename_len
= 0;
855 strncpy(basename
, nest_name
, PATH_MAX
);
856 basename_len
= strlen(basename
);
858 /* For a named type, we use the type_name directly */
859 if(td
->type_name
!= NULL
) {
860 strncpy(basename
, td
->type_name
, PATH_MAX
);
861 basename_len
= strlen(basename
);
863 /* For a unnamed type, there must be a field name, except for
865 if((basename_len
!= 0)
866 && (basename
[basename_len
-1] != '_'
867 && (field_name
[0] != '\0'))) {
868 strncat(basename
, "_", PATH_MAX
- basename_len
);
869 basename_len
= strlen(basename
);
871 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
881 printf("print_type_write_fct : type has no write function.\n");
889 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
893 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
896 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
899 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
902 printf("print_type_write_fct : type has no write function.\n");
907 fprintf(fd
, "void *buffer,\n");
909 fprintf(fd
, "size_t *to_base,\n");
911 fprintf(fd
, "size_t *to,\n");
913 fprintf(fd
, "void **from,\n");
915 fprintf(fd
, "size_t *len,\n");
917 if(print_type(td
, fd
, 0, basename
, "")) return 1;
921 fprintf(fd
, " *obj)\n");
924 fprintf(fd
, " *obj)\n");
927 fprintf(fd
, " *obj)\n");
930 fprintf(fd
, " obj)\n");
933 printf("print_type_write_fct : type has no write function.\n");
939 fprintf(fd
, "size_t align, size;\n");
946 fprintf(fd
, "/* Flush pending memcpy */\n");
948 fprintf(fd
, "if(*len != 0) {\n");
950 fprintf(fd
, "if(buffer != NULL)\n");
952 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
956 fprintf(fd
, "*to += *len;\n");
958 fprintf(fd
, "*len = 0;\n");
966 printf("print_type_write_fct : type has no write function.\n");
971 fprintf(fd
, "align = ");
972 if(print_type_alignment(td
, fd
, 0, basename
, field_name
)) return 1;
976 fprintf(fd
, "if(*len == 0) {\n");
978 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
980 fprintf(fd
, "} else {\n");
982 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
987 /* First, check if the type has a fixed size. If it is the case, then the size
988 * to write is know by the compiler : simply use a sizeof() */
989 if(has_type_fixed_size(td
)) {
991 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
994 fprintf(fd
, "*len += sizeof(");
995 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
998 /* The type contains nested variable size subtypes :
999 * we must write field by field. */
1001 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1007 fprintf(fd
, "/* Copy members */\n");
1009 fprintf(fd
, "size = sizeof(\n");
1010 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1011 fd
, 1, basename
, ((field_t
*)td
->fields
.array
[0])->name
)) return 1;
1012 fprintf(fd
, ");\n");
1014 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1016 fprintf(fd
, "if(buffer != NULL)\n");
1018 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, size);\n");
1020 fprintf(fd
, "*to += size;\n");
1023 /* Write the child : varlen child or not ? */
1024 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1025 /* Fixed size len child : use a multiplication of its size */
1027 fprintf(fd
, "size = sizeof(\n");
1028 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1029 fd
, 1, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1030 fprintf(fd
, ");\n");
1032 fprintf(fd
, "*to += ltt_align(*to, size);\n");
1034 fprintf(fd
, "size = obj->len * size;\n");
1036 fprintf(fd
, "if(buffer != NULL)\n");
1038 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, size);\n");
1040 fprintf(fd
, "*to += size;\n");
1044 fprintf(fd
, "/* Variable length child : iter. */\n");
1046 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1047 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1048 fd
, 2, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1054 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1056 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1058 fprintf(fd
, "*to_base = *to_base+*to;\n");
1060 fprintf(fd
, "*to = 0;\n");
1062 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1064 fprintf(fd
, "*from = obj+1;\n");
1067 fprintf(fd
, "size = strlen(obj);\n");
1069 fprintf(fd
, "if(buffer != NULL)\n");
1071 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1073 fprintf(fd
, "*to += size;\n");
1076 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1078 fprintf(fd
, "*to = ltt_align(*to, sizeof(void *));\n");
1080 fprintf(fd
, "*to_base = *to_base+*to;\n");
1082 fprintf(fd
, "*to = 0;\n");
1084 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1086 fprintf(fd
, "*from = obj+1;\n");
1089 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1090 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1091 type_descriptor_t
*type
= field
->type
;
1092 if(print_type_write(type
,
1093 fd
, 1, basename
, field
->name
)) return 1;
1098 printf("ERROR : A union CANNOT contain a variable size child.\n");
1102 /* Write the child : varlen child or not ? */
1103 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1104 /* Error : if an array has a variable size, then its child must also
1105 * have a variable size. */
1109 fprintf(fd
, "/* Variable length child : iter. */\n");
1111 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1112 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1113 fd
, 2, basename
, ((field_t
*)td
->fields
.array
[1])->name
)) return 1;
1119 printf("print_type_write_fct : type has no write function.\n");
1127 /* Function footer */
1135 /* Print the logging function of an event. This is the core of genevent */
1136 int print_event_logging_function(char *basename
, facility_t
*fac
,
1137 event_t
*event
, FILE *fd
)
1139 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1140 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1141 /* For each field, print the function argument */
1142 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1143 type_descriptor_t
*t
= f
->type
;
1144 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1145 if(j
< event
->fields
.position
-1) {
1150 if(event
->fields
.position
== 0) {
1152 fprintf(fd
, "void");
1155 fprintf(fd
, "#ifndef CONFIG_LTT\n");
1158 fprintf(fd
,"#else\n");
1160 /* Print the function variables */
1162 fprintf(fd
, "unsigned int index;\n");
1164 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1166 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1168 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1170 fprintf(fd
, "void *buffer = NULL;\n");
1172 fprintf(fd
, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1174 fprintf(fd
, "size_t to = 0;\n");
1176 fprintf(fd
, "void *from;");
1178 fprintf(fd
, "size_t len = 0;\n");
1180 fprintf(fd
, "size_t reserve_size;\n");
1182 fprintf(fd
, "size_t slot_size;\n");
1184 fprintf(fd
, "cycles_t tsc;\n");
1186 fprintf(fd
, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
1190 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1193 /* Calculate event variable len + event data alignment offset.
1194 * Assume that the padding for alignment starts at a void*
1196 * This excludes the header size and alignment. */
1199 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1201 fprintf(fd
, "/* size = to_base + to + len */\n");
1203 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1205 fprintf(fd
, " * sizeof(void *) address. */\n");
1208 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1209 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1210 type_descriptor_t
*type
= field
->type
;
1211 if(print_type_write(type
,
1212 fd
, 1, basename
, field
->name
)) return 1;
1216 fprintf(fd
, "reserve_size = to_base + to + len;\n");
1218 fprintf(fd
, "to_base = to = len = 0;\n");
1221 /* Take locks : make sure the trace does not vanish while we write on
1222 * it. A simple preemption disabling is enough (using rcu traces). */
1224 fprintf(fd
, "preempt_disable();\n");
1226 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1228 /* Get facility index */
1230 if(event
->per_tracefile
) {
1232 fprintf(fd
, "index = tracefile_index;\n");
1236 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1237 "\t\t\t\t\t\tevent_%s);\n",
1238 fac
->name
, fac
->checksum
, event
->name
);
1243 /* For each trace */
1245 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1247 fprintf(fd
, "if(!trace->active) continue;\n\n");
1249 if(event
->per_trace
) {
1251 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1255 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1257 fprintf(fd
, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1262 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1265 fprintf(fd
, "slot_size = 0;\n");
1267 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1269 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1271 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad);\n");
1272 /* If error, return */
1274 fprintf(fd
, "if(!buffer) return;\n\n");
1276 /* write data : assume stack alignment is the same as struct alignment. */
1278 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1279 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1280 type_descriptor_t
*type
= field
->type
;
1284 switch(type
->type
) {
1290 fprintf(fd
, "from = %s;\n", field
->name
);
1293 fprintf(fd
, "from = &%s;\n", field
->name
);
1298 if(print_type_write(type
,
1299 fd
, 2, basename
, field
->name
)) return 1;
1302 /* Don't forget to flush pending memcpy */
1304 fprintf(fd
, "/* Flush pending memcpy */\n");
1306 fprintf(fd
, "if(len != 0) {\n");
1308 fprintf(fd
, "memcpy(buffer+to_base+to, from, len);\n");
1310 fprintf(fd
, "to += len;\n");
1311 //print_tabs(3, fd);
1312 //fprintf(fd, "from += len;\n");
1314 fprintf(fd
, "len = 0;\n");
1323 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1326 fprintf(fd
, "}\n\n");
1330 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1332 fprintf(fd
, "preempt_enable_no_resched();\n");
1335 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1340 /* ltt-facility-name.h : main logging header.
1343 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1345 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1346 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1348 fprintf(fd
, "/* Facility activation at compile time. */\n");
1349 fprintf(fd
, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1354 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1356 sequence_t
*types
= &fac
->named_types
.values
;
1357 fprintf(fd
, "/* Named types */\n");
1360 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1361 /* For each named type, print the definition */
1362 if((print_type_declaration(types
->array
[i
], fd
,
1363 0, "", ""))) return 1;
1364 /* Print also the align function */
1365 if((print_type_alignment_fct(types
->array
[i
], fd
,
1366 0, "", ""))) return 1;
1367 /* Print also the write function */
1368 if((print_type_write_fct(types
->array
[i
], fd
,
1369 0, "", ""))) return 1;
1374 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1376 sequence_t
*events
= &fac
->events
;
1377 char basename
[PATH_MAX
];
1378 unsigned int facname_len
;
1380 strncpy(basename
, fac
->name
, PATH_MAX
);
1381 facname_len
= strlen(basename
);
1382 strncat(basename
, "_", PATH_MAX
-facname_len
);
1383 facname_len
= strlen(basename
);
1385 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1386 event_t
*event
= (event_t
*)events
->array
[i
];
1387 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1389 /* For each event, print structure, and then logging function */
1390 fprintf(fd
, "/* Event %s structures */\n",
1392 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1393 /* For each unnamed type, print the definition */
1394 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1395 type_descriptor_t
*t
= f
->type
;
1396 if(t
->type_name
== NULL
) {
1397 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1398 /* Print also the align function */
1399 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1400 /* Print also the write function */
1401 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1407 fprintf(fd
, "/* Event %s logging function */\n",
1410 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1419 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1421 fprintf(fd
, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
1422 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1425 int print_log_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-");
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
));
1449 /* Print file head */
1450 print_log_header_head(fac
, fd
);
1452 /* print named types in declaration order */
1453 if(print_log_header_types(fac
, fd
)) return 1;
1456 if(print_log_header_events(fac
, fd
)) return 1;
1458 /* Print file tail */
1459 print_log_header_tail(fac
, fd
);
1468 /* ltt-facility-id-name.h : facility id.
1470 int print_id_header(facility_t
*fac
)
1472 char filename
[PATH_MAX
];
1473 unsigned int filename_size
= 0;
1475 dprintf("%s\n", fac
->name
);
1477 strcpy(filename
, "ltt-facility-id-");
1478 filename_size
= strlen(filename
);
1480 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1481 filename_size
= strlen(filename
);
1483 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1484 filename_size
= strlen(filename
);
1487 fd
= fopen(filename
, "w");
1489 printf("Error opening file %s for writing : %s\n",
1490 filename
, strerror(errno
));
1500 /* ltt-facility-loader-name.h : facility specific loader info.
1502 int print_loader_header(facility_t
*fac
)
1504 char filename
[PATH_MAX
];
1505 unsigned int filename_size
= 0;
1507 dprintf("%s\n", fac
->name
);
1509 strcpy(filename
, "ltt-facility-loader-");
1510 filename_size
= strlen(filename
);
1512 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1513 filename_size
= strlen(filename
);
1515 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1516 filename_size
= strlen(filename
);
1519 fd
= fopen(filename
, "w");
1521 printf("Error opening file %s for writing : %s\n",
1522 filename
, strerror(errno
));
1531 /* ltt-facility-loader-name.c : generic faciilty loader
1533 int print_loader_c(facility_t
*fac
)
1535 char filename
[PATH_MAX
];
1536 unsigned int filename_size
= 0;
1538 dprintf("%s\n", fac
->name
);
1540 strcpy(filename
, "ltt-facility-loader-");
1541 filename_size
= strlen(filename
);
1543 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1544 filename_size
= strlen(filename
);
1546 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1547 filename_size
= strlen(filename
);
1550 fd
= fopen(filename
, "w");
1552 printf("Error opening file %s for writing : %s\n",
1553 filename
, strerror(errno
));
1566 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1568 /*****************************************************************************
1570 * ltt_facility_open : open facilities
1572 * pathname : the path name of the facility
1574 * Open the facility corresponding to the right checksum.
1576 *returns the facility on success, NULL on error.
1577 ****************************************************************************/
1578 facility_t
*ltt_facility_open(char * pathname
)
1583 facility_t
* fac
= NULL
;
1584 char buffer
[BUFFER_SIZE
];
1585 int generated
= FALSE
;
1587 in
.buffer
= &(buffer
[0]);
1589 in
.error
= error_callback
;
1593 in
.fp
= fopen(in
.name
, "r");
1600 token
= getToken(&in
);
1601 if(in
.type
== ENDFILE
) break;
1604 printf("More than one facility in the file. Only using the first one.\n");
1608 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1609 token
= getName(&in
);
1611 if(strcmp("facility",token
) == 0) {
1612 fac
= malloc(sizeof(facility_t
));
1614 fac
->description
= NULL
;
1615 sequence_init(&(fac
->events
));
1616 table_init(&(fac
->named_types
));
1617 sequence_init(&(fac
->unnamed_types
));
1619 parseFacility(&in
, fac
);
1621 //check if any namedType is not defined
1622 checkNamedTypesImplemented(&fac
->named_types
);
1624 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
1629 printf("facility token was expected in file %s\n", in
.name
);
1640 printf("Cannot find facility %s\n", pathname
);
1647 /* Close the facility */
1648 void ltt_facility_close(facility_t
*fac
)
1652 free(fac
->description
);
1653 freeEvents(&fac
->events
);
1654 sequence_dispose(&fac
->events
);
1655 freeNamedType(&fac
->named_types
);
1656 table_dispose(&fac
->named_types
);
1657 freeTypes(&fac
->unnamed_types
);
1658 sequence_dispose(&fac
->unnamed_types
);
1664 void show_help(int argc
, char ** argv
)
1666 printf("Genevent help : \n");
1668 printf("Use %s name.xml\n", argv
[0]);
1669 printf("to create :\n");
1670 printf("ltt-facility-name.h\n");
1671 printf("ltt-facility-id-name.h\n");
1672 printf("ltt-facility-loader-name.h\n");
1673 printf("ltt-facility-loader-name.c\n");
1674 printf("In the current directory.\n");
1678 /* Parse program arguments */
1680 * 0 : continue program
1681 * -1 : stop program, return 0
1682 * > 0 : stop program, return value as exit.
1684 int check_args(int argc
, char **argv
)
1687 printf("Not enough arguments\n");
1688 show_help(argc
, argv
);
1692 if(strcmp(argv
[1], "-h") == 0) {
1693 show_help(argc
, argv
);
1700 int main(int argc
, char **argv
)
1705 err
= check_args(argc
, argv
);
1706 if(err
> 0) return err
;
1707 else if(err
< 0) return 0;
1709 /* open the facility */
1710 fac
= ltt_facility_open(argv
[1]);
1712 printf("Error opening file %s for reading : %s\n",
1713 argv
[1], strerror(errno
));
1717 /* generate the output C files */
1720 /* ltt-facility-name.h : main logging header.
1722 err
= print_log_header(fac
);
1725 /* ltt-facility-id-name.h : facility id.
1727 err
= print_id_header(fac
);
1730 /* ltt-facility-loader-name.h : facility specific loader info.
1732 err
= print_loader_header(fac
);
1735 /* ltt-facility-loader-name.c : generic faciilty loader
1737 err
= print_loader_c(fac
);
1740 /* close the facility */
1741 ltt_facility_close(fac
);
This page took 0.070307 seconds and 3 git commands to generate.