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: ",\
90 enum user_fct_types
{ USER_FCT_PROTO
, USER_FCT_DECLARATION
} ;
94 void print_tabs(unsigned int tabs
, FILE *fd
)
96 for(unsigned int i
= 0; i
<tabs
;i
++)
102 * Copied from construct_types_and_fields in LTTV facility.c */
104 int print_type(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
105 char *nest_name
, char *field_name
)
107 char basename
[PATH_MAX
];
108 unsigned int basename_len
= 0;
110 strcpy(basename
, nest_name
);
111 basename_len
= strlen(basename
);
113 /* For a named type, we use the type_name directly */
114 if(td
->type_name
!= NULL
) {
115 strncpy(basename
, td
->type_name
, PATH_MAX
);
116 basename_len
= strlen(basename
);
118 /* For a unnamed type, there must be a field name */
119 if((basename_len
!= 0)
120 && (basename
[basename_len
-1] != '_')
121 && (field_name
[0] != '\0')) {
122 strncat(basename
, "_", PATH_MAX
- basename_len
);
123 basename_len
= strlen(basename
);
125 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
130 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
133 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
136 fprintf(fd
, "signed char");
139 fprintf(fd
, "unsigned char");
142 fprintf(fd
, "short");
145 fprintf(fd
, "unsigned short");
151 fprintf(fd
, "unsigned int");
154 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
157 fprintf(fd
, "const void *");
163 fprintf(fd
, "unsigned long");
166 fprintf(fd
, "size_t");
169 fprintf(fd
, "ssize_t");
172 fprintf(fd
, "off_t");
175 fprintf(fd
, "const char *");
178 fprintf(fd
, "enum lttng_%s", basename
);
181 fprintf(fd
, "lttng_array_%s", basename
);
184 fprintf(fd
, "lttng_sequence_%s", basename
);
187 fprintf(fd
, "struct lttng_%s", basename
);
190 fprintf(fd
, "union lttng_%s", basename
);
193 printf("print_type : unknown type\n");
200 /* Print logging function argument */
201 int print_arg(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
202 char *nest_name
, char *field_name
)
204 char basename
[PATH_MAX
];
205 unsigned int basename_len
= 0;
207 strcpy(basename
, nest_name
);
208 basename_len
= strlen(basename
);
210 /* For a named type, we use the type_name directly */
211 if(td
->type_name
!= NULL
) {
212 strncpy(basename
, td
->type_name
, PATH_MAX
);
213 basename_len
= strlen(basename
);
215 /* For a unnamed type, there must be a field name */
216 if((basename_len
!= 0)
217 && (basename
[basename_len
-1] != '_')
218 && (field_name
[0] != '\0')) {
219 strncat(basename
, "_", PATH_MAX
- basename_len
);
220 basename_len
= strlen(basename
);
222 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
225 print_tabs(tabs
, fd
);
229 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
230 fprintf(fd
, " lttng_param_%s", field_name
);
233 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
234 fprintf(fd
, " lttng_param_%s", field_name
);
237 fprintf(fd
, "signed char");
238 fprintf(fd
, " lttng_param_%s", field_name
);
241 fprintf(fd
, "unsigned char");
242 fprintf(fd
, " lttng_param_%s", field_name
);
245 fprintf(fd
, "short");
246 fprintf(fd
, " lttng_param_%s", field_name
);
249 fprintf(fd
, "unsigned short");
250 fprintf(fd
, " lttng_param_%s", field_name
);
254 fprintf(fd
, " lttng_param_%s", field_name
);
257 fprintf(fd
, "unsigned int");
258 fprintf(fd
, " lttng_param_%s", field_name
);
261 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
262 fprintf(fd
, " lttng_param_%s", field_name
);
265 fprintf(fd
, "const void *");
266 fprintf(fd
, " lttng_param_%s", field_name
);
270 fprintf(fd
, " lttng_param_%s", field_name
);
273 fprintf(fd
, "unsigned long");
274 fprintf(fd
, " lttng_param_%s", field_name
);
277 fprintf(fd
, "size_t");
278 fprintf(fd
, " lttng_param_%s", field_name
);
281 fprintf(fd
, "ssize_t");
282 fprintf(fd
, " lttng_param_%s", field_name
);
285 fprintf(fd
, "off_t");
286 fprintf(fd
, " lttng_param_%s", field_name
);
289 fprintf(fd
, "const char *");
290 fprintf(fd
, " lttng_param_%s", field_name
);
293 fprintf(fd
, "enum lttng_%s", basename
);
294 fprintf(fd
, " lttng_param_%s", field_name
);
297 fprintf(fd
, "lttng_array_%s", basename
);
298 fprintf(fd
, " lttng_param_%s", field_name
);
301 fprintf(fd
, "lttng_sequence_%s *", basename
);
302 fprintf(fd
, " lttng_param_%s", field_name
);
305 fprintf(fd
, "struct lttng_%s *", basename
);
306 fprintf(fd
, " lttng_param_%s", field_name
);
309 fprintf(fd
, "union lttng_%s *", basename
);
310 fprintf(fd
, " lttng_param_%s", field_name
);
313 printf("print_type : unknown type\n");
321 /* Does the type has a fixed size ? (as know from the compiler)
324 * 0 : variable length
326 int has_type_fixed_size(type_descriptor_t
*td
)
345 case UNION
: /* The union must have fixed size children. Must be checked by
355 int has_type_fixed
= 0;
356 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
357 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
358 type_descriptor_t
*type
= field
->type
;
360 has_type_fixed
= has_type_fixed_size(type
);
361 if(!has_type_fixed
) return 0;
367 assert(td
->size
>= 0);
368 return has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
);
371 printf("There is a type defined to NONE : bad.\n");
375 return 0; //make gcc happy.
382 /* print type declaration.
384 * Copied from construct_types_and_fields in LTTV facility.c */
386 int print_type_declaration(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
387 char *nest_name
, char *field_name
)
389 char basename
[PATH_MAX
];
390 unsigned int basename_len
= 0;
392 if(td
->custom_write
) return 0; /* Does print custom type */
394 strncpy(basename
, nest_name
, PATH_MAX
);
395 basename_len
= strlen(basename
);
397 /* For a named type, we use the type_name directly */
398 if(td
->type_name
!= NULL
) {
399 strncpy(basename
, td
->type_name
, PATH_MAX
);
400 basename_len
= strlen(basename
);
402 /* For a unnamed type, there must be a field name, except for
404 if((basename_len
!= 0)
405 && (basename
[basename_len
-1] != '_'
406 && (field_name
[0] != '\0'))) {
407 strncat(basename
, "_", PATH_MAX
- basename_len
);
408 basename_len
= strlen(basename
);
410 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
415 fprintf(fd
, "enum lttng_%s", basename
);
417 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
419 fprintf(fd
, "LTTNG_%s = %d", ((char*)td
->labels
.array
[i
]),
420 (*(int*)td
->labels_values
.array
[i
]));
428 dprintf("%s\n", basename
);
429 assert(td
->size
>= 0);
430 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
431 /* Not a named nested type : we must print its declaration first */
432 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
433 fd
, 0, basename
, "")) return 1;
435 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename
,
437 fprintf(fd
, "typedef ");
438 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
439 fd
, tabs
, basename
, "")) return 1;
440 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
445 /* We assume that the sequence length type does not need to be declared.
447 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
448 /* Not a named nested type : we must print its declaration first */
449 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
450 fd
, 0, basename
, "")) return 1;
452 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
455 fprintf(fd
, "struct lttng_sequence_%s", basename
);
458 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
459 fd
, tabs
, basename
, "")) return 1;
460 fprintf(fd
, " len;\n");
462 fprintf(fd
, "const ");
463 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
464 fd
, tabs
, basename
, "")) return 1;
465 fprintf(fd
, " *array;\n");
466 fprintf(fd
, "};\n"); /* We do not LTT_ALIGN, because we never copy
467 it to the buffer directly. */
472 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
473 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
474 type_descriptor_t
*type
= field
->type
;
475 if(type
->type_name
== NULL
) {
476 /* Not a named nested type : we must print its declaration first */
477 if(print_type_declaration(type
,
478 fd
, 0, basename
, field
->name
)) return 1;
481 fprintf(fd
, "struct lttng_%s", basename
);
483 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
484 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
485 type_descriptor_t
*type
= field
->type
;
487 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
489 fprintf(fd
, "%s", field
->name
);
493 fprintf(fd
, "} LTT_ALIGN;\n");
499 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
500 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
501 type_descriptor_t
*type
= field
->type
;
502 if(type
->type_name
== NULL
) {
503 /* Not a named nested type : we must print its declaration first */
504 if(print_type_declaration(type
,
505 fd
, 0, basename
, field
->name
)) return 1;
508 fprintf(fd
, "union lttng_%s", basename
);
510 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
511 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
512 type_descriptor_t
*type
= field
->type
;
514 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
516 fprintf(fd
, "%s", field
->name
);
520 fprintf(fd
, "} LTT_ALIGN;\n");
526 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
534 /* print type alignment.
536 * Copied from construct_types_and_fields in LTTV facility.c
538 * basename is the name which identifies the type (along with a prefix
541 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
542 char *nest_name
, char *field_name
, char *obj_prefix
)
544 char basename
[PATH_MAX
];
545 unsigned int basename_len
= 0;
547 strncpy(basename
, nest_name
, PATH_MAX
);
548 basename_len
= strlen(basename
);
550 /* For a named type, we use the type_name directly */
551 if(td
->type_name
!= NULL
) {
552 strncpy(basename
, td
->type_name
, PATH_MAX
);
553 basename_len
= strlen(basename
);
555 /* For a unnamed type, there must be a field name, except for
557 if((basename_len
!= 0)
558 && (basename
[basename_len
-1] != '_'
559 && field_name
!= NULL
560 && (field_name
[0] != '\0'))) {
561 strncat(basename
, "_", PATH_MAX
- basename_len
);
562 basename_len
= strlen(basename
);
564 if(field_name
!= NULL
)
565 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
568 if(field_name
[0] == '\0') {
569 /* We are in a write function : it's the obj that we must align. */
572 fprintf(fd
, "lttng_get_alignment_sequence_%s(%s)", basename
,
576 fprintf(fd
, "lttng_get_alignment_struct_%s(%s)", basename
,
580 fprintf(fd
, "lttng_get_alignment_union_%s(%s)", basename
,
584 fprintf(fd
, "lttng_get_alignment_array_%s(%s)", basename
,
587 fprintf(fd
, "sizeof(char)");
605 fprintf(fd
, "sizeof(");
606 if(print_type(td
, fd
, 0, basename
, "")) return 1;
611 printf("error : type unexpected\n");
634 fprintf(fd
, "sizeof(");
635 if(print_type(td
, fd
, 0, basename
, "")) return 1;
639 fprintf(fd
, "sizeof(char)");
642 fprintf(fd
, "lttng_get_alignment_sequence_%s(&%s%s)", basename
,
643 obj_prefix
, field_name
);
646 fprintf(fd
, "lttng_get_alignment_struct_%s(&%s%s)", basename
,
647 obj_prefix
, field_name
);
650 fprintf(fd
, "lttng_get_alignment_union_%s(&%s%s)", basename
,
651 obj_prefix
, field_name
);
654 fprintf(fd
, "lttng_get_alignment_array_%s(%s%s)", basename
,
655 obj_prefix
, field_name
);
658 printf("error : type NONE unexpected\n");
669 * Copied from construct_types_and_fields in LTTV facility.c
671 * basename is the name which identifies the type (along with a prefix
674 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
675 char *nest_name
, char *field_name
, char *obj_prefix
, int get_ptr
)
677 char basename
[PATH_MAX
];
678 unsigned int basename_len
= 0;
679 char get_ptr_char
[2] = "";
680 char custom
[PATH_MAX
] = "";
682 strncpy(basename
, nest_name
, PATH_MAX
);
683 basename_len
= strlen(basename
);
685 /* For a named type, we use the type_name directly */
686 if(td
->type_name
!= NULL
) {
687 strncpy(basename
, td
->type_name
, PATH_MAX
);
688 basename_len
= strlen(basename
);
690 /* For a unnamed type, there must be a field name, except for
692 if((basename_len
!= 0)
693 && (basename
[basename_len
-1] != '_'
694 && (field_name
[0] != '\0'))) {
695 strncat(basename
, "_", PATH_MAX
- basename_len
);
696 basename_len
= strlen(basename
);
698 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
702 strcpy(get_ptr_char
, "&");
705 if(td
->custom_write
) {
706 strcpy(custom
, "_custom");
726 print_tabs(tabs
, fd
);
727 fprintf(fd
, "align = ");
729 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
734 print_tabs(tabs
, fd
);
735 fprintf(fd
, "if (*len == 0) {\n");
736 print_tabs(tabs
+1, fd
);
737 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
738 print_tabs(tabs
, fd
);
739 fprintf(fd
, "} else {\n");
740 print_tabs(tabs
+1, fd
);
741 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
742 print_tabs(tabs
, fd
);
746 print_tabs(tabs
, fd
);
747 fprintf(fd
, "*len += ");
748 fprintf(fd
, "sizeof(");
749 if(print_type(td
, fd
, 0, basename
, "")) return 1;
754 print_tabs(tabs
, fd
);
756 "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n",
757 custom
, basename
, obj_prefix
, field_name
);
760 print_tabs(tabs
, fd
);
762 "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
763 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
766 print_tabs(tabs
, fd
);
768 "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
769 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
772 print_tabs(tabs
, fd
);
774 "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);",
775 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
778 print_tabs(tabs
, fd
);
780 "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);",
781 custom
, basename
, obj_prefix
, field_name
);
784 printf("Error : type NONE unexpected\n");
792 /* print need local vars ?.
794 * Copied from print_type_write
796 * Does the type_write call needs local size and from variables ?
797 * return value : 1 yes, 0 no.
800 int has_type_local(type_descriptor_t
* td
)
829 printf("Error : type NONE unexpected\n");
839 /* print type alignment 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_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
848 char *nest_name
, char *field_name
)
850 char basename
[PATH_MAX
];
851 unsigned int basename_len
= 0;
853 if(td
->custom_write
) return 0; /* Does print custom type */
854 if(td
->fac
->align
== 0) return 0;
856 strncpy(basename
, nest_name
, PATH_MAX
);
857 basename_len
= strlen(basename
);
859 /* For a named type, we use the type_name directly */
860 if(td
->type_name
!= NULL
) {
861 strncpy(basename
, td
->type_name
, PATH_MAX
);
862 basename_len
= strlen(basename
);
864 /* For a unnamed type, there must be a field name, except for
866 if((basename_len
!= 0)
867 && (basename
[basename_len
-1] != '_'
868 && (field_name
[0] != '\0'))) {
869 strncat(basename
, "_", PATH_MAX
- basename_len
);
870 basename_len
= strlen(basename
);
872 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
877 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
878 /* Not a named nested type : we must print its align fct */
879 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
880 0, basename
, "")) return 1;
882 /* Function header */
883 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
886 if(print_type(td
, fd
, 0, basename
, "")) return 1;
887 fprintf(fd
, " *obj)\n");
890 fprintf(fd
, "size_t align=0, localign;");
893 fprintf(fd
, "localign = ");
894 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
895 fd
, 0, basename
, "len", "obj->")) return 1;
898 fprintf(fd
, "align = max(align, localign);\n");
901 fprintf(fd
, "localign = ");
902 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
903 fd
, 0, basename
, "array[0]", "obj->")) return 1;
906 fprintf(fd
, "align = max(align, localign);\n");
909 fprintf(fd
, "return align;\n");
912 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
913 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
914 type_descriptor_t
*type
= field
->type
;
915 if(type
->type_name
== NULL
) {
916 /* Not a named nested type : we must print its align fct */
917 if(print_type_alignment_fct(type
, fd
,
918 0, basename
, field
->name
)) return 1;
921 /* Function header */
922 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
925 if(print_type(td
, fd
, 0, basename
, "")) return 1;
926 fprintf(fd
, " *obj)\n");
929 fprintf(fd
, "size_t align=0, localign;");
931 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
932 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
933 type_descriptor_t
*type
= field
->type
;
935 fprintf(fd
, "localign = ");
936 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
940 fprintf(fd
, "align = max(align, localign);\n");
944 fprintf(fd
, "return align;\n");
948 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
949 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
950 type_descriptor_t
*type
= field
->type
;
951 if(type
->type_name
== NULL
) {
952 /* Not a named nested type : we must print its align fct */
953 if(print_type_alignment_fct(type
, fd
,
954 0, basename
, field
->name
)) return 1;
957 /* Function header */
958 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
961 if(print_type(td
, fd
, 0, basename
, "")) return 1;
962 fprintf(fd
, " *obj)\n");
965 fprintf(fd
, "size_t align=0, localign;");
967 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
968 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
969 type_descriptor_t
*type
= field
->type
;
971 fprintf(fd
, "localign = ");
972 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
976 fprintf(fd
, "align = max(align, localign);\n");
980 fprintf(fd
, "return align;\n");
984 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
985 /* Not a named nested type : we must print its align fct */
986 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
987 0, basename
, "")) return 1;
989 /* Function header */
990 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
993 if(print_type(td
, fd
, 0, basename
, "")) return 1;
994 fprintf(fd
, " obj)\n");
997 fprintf(fd
, "return \n");
998 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
999 fd
, 0, basename
, "", "obj[0]"))
1004 dprintf("print_type_alignment_fct : type has no alignment function.\n");
1010 /* Function footer */
1017 /* print type write function.
1019 * Copied from construct_types_and_fields in LTTV facility.c
1021 * basename is the name which identifies the type (along with a prefix
1024 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
1025 char *nest_name
, char *field_name
)
1027 char basename
[PATH_MAX
];
1028 unsigned int basename_len
= 0;
1030 if(td
->custom_write
) return 0; /* Does print custom type */
1032 strncpy(basename
, nest_name
, PATH_MAX
);
1033 basename_len
= strlen(basename
);
1035 /* For a named type, we use the type_name directly */
1036 if(td
->type_name
!= NULL
) {
1037 strncpy(basename
, td
->type_name
, PATH_MAX
);
1038 basename_len
= strlen(basename
);
1040 /* For a unnamed type, there must be a field name, except for
1042 if((basename_len
!= 0)
1043 && (basename
[basename_len
-1] != '_'
1044 && (field_name
[0] != '\0'))) {
1045 strncat(basename
, "_", PATH_MAX
- basename_len
);
1046 basename_len
= strlen(basename
);
1048 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
1053 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
1054 /* Not a named nested type : we must print its write fct */
1055 if(print_type_write_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
1056 0, basename
, "")) return 1;
1060 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1061 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1062 type_descriptor_t
*type
= field
->type
;
1063 if(type
->type_name
== NULL
) {
1064 /* Not a named nested type : we must print its write fct */
1065 if(print_type_write_fct(type
, fd
,
1066 0, basename
, field
->name
)) return 1;
1071 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1072 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1073 type_descriptor_t
*type
= field
->type
;
1074 if(type
->type_name
== NULL
) {
1075 /* Not a named nested type : we must print its write fct */
1076 if(print_type_write_fct(type
, fd
,
1077 0, basename
, field
->name
)) return 1;
1082 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
1083 /* Not a named nested type : we must print its write fct */
1084 if(print_type_write_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
1085 0, basename
, "")) return 1;
1091 dprintf("print_type_write_fct : type has no write function.\n");
1099 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
1103 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
1106 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
1109 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
1112 fprintf(fd
, "static inline void lttng_write_string_%s(\n", basename
);
1115 printf("print_type_write_fct : type has no write function.\n");
1120 fprintf(fd
, "char *buffer,\n");
1122 fprintf(fd
, "size_t *to_base,\n");
1124 fprintf(fd
, "size_t *to,\n");
1126 fprintf(fd
, "const char **from,\n");
1128 fprintf(fd
, "size_t *len,\n");
1130 if(print_type(td
, fd
, 0, basename
, "")) return 1;
1134 fprintf(fd
, " *obj)\n");
1137 fprintf(fd
, " *obj)\n");
1140 fprintf(fd
, " *obj)\n");
1143 fprintf(fd
, " obj)\n");
1146 fprintf(fd
, " obj)\n");
1149 printf("print_type_write_fct : type has no write function.\n");
1158 fprintf(fd
, "size_t size;\n");
1165 fprintf(fd
, "size_t align;\n");
1172 fprintf(fd
, "/* Flush pending memcpy */\n");
1174 fprintf(fd
, "if (*len != 0) {\n");
1176 fprintf(fd
, "if (buffer != NULL)\n");
1178 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1182 fprintf(fd
, "*to += *len;\n");
1184 fprintf(fd
, "*len = 0;\n");
1192 printf("print_type_write_fct : type has no write function.\n");
1197 fprintf(fd
, "align = ");
1198 if(td
->fac
->align
) {
1199 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
1205 fprintf(fd
, "if (*len == 0) {\n");
1207 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
1209 fprintf(fd
, "} else {\n");
1211 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1216 /* First, check if the type has a fixed size. If it is the case, then the size
1217 * to write is know by the compiler : simply use a sizeof() */
1218 if(has_type_fixed_size(td
)) {
1220 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1223 fprintf(fd
, "*len += sizeof(");
1224 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
1225 fprintf(fd
, ");\n");
1227 /* The type contains nested variable size subtypes :
1228 * we must write field by field. */
1230 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1236 fprintf(fd
, "/* Copy members */\n");
1237 // print_tabs(1, fd);
1238 // fprintf(fd, "size = sizeof(\n");
1239 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1240 fd
, 1, basename
, "len", "obj->", 1)) return 1;
1242 // fprintf(fd, ");\n");
1243 // print_tabs(1, fd);
1244 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1246 fprintf(fd
, "if (buffer != NULL)\n");
1248 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1250 fprintf(fd
, "*to += *len;\n");
1252 fprintf(fd
, "*len = 0;\n");
1255 /* Write the child : varlen child or not ? */
1256 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1257 /* Fixed size len child : use a multiplication of its size */
1258 // print_tabs(1, fd);
1259 // fprintf(fd, "size = sizeof(\n");
1261 //print_tabs(1, fd);
1262 /* We know that *len does not contain alignment because of the
1263 * previous align output. len is always 0 here. */
1264 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1265 fd
, 1, basename
, "array[0]", "obj->", 1))
1267 // fprintf(fd, ");\n");
1270 fprintf(fd
, "*len = obj->len * (*len);\n");
1272 fprintf(fd
, "if (buffer != NULL)\n");
1274 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1276 fprintf(fd
, "*to += *len;\n");
1278 fprintf(fd
, "*len = 0;\n");
1282 fprintf(fd
, "/* Variable length child : iter. */\n");
1284 fprintf(fd
, "for (unsigned int i = 0; i < obj->len; i++) {\n");
1285 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1286 fd
, 2, basename
, "array[i]", "obj->", 1)) return 1;
1292 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1295 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1297 fprintf(fd
, "*to_base = *to_base+*to;\n");
1299 fprintf(fd
, "*to = 0;\n");
1302 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1304 fprintf(fd
, "*from = (const char*)(obj+1);\n");
1308 fprintf(fd
, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1310 fprintf(fd
, "if (buffer != NULL)\n");
1312 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1314 fprintf(fd
, "*to += size;\n");
1317 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1320 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1322 fprintf(fd
, "*to_base = *to_base+*to;\n");
1324 fprintf(fd
, "*to = 0;\n");
1327 fprintf(fd
, "/* Put source *from just after the C string */\n");
1329 fprintf(fd
, "*from += size;\n");
1332 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1333 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1334 type_descriptor_t
*type
= field
->type
;
1335 if(print_type_write(type
,
1336 fd
, 1, basename
, field
->name
, "obj->", 1)) return 1;
1341 printf("ERROR : A union CANNOT contain a variable size child.\n");
1345 /* Write the child : varlen child or not ? */
1346 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1347 /* Error : if an array has a variable size, then its child must also
1348 * have a variable size. */
1352 fprintf(fd
, "/* Variable length child : iter. */\n");
1354 fprintf(fd
, "for (unsigned int i = 0; i < LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1355 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1356 fd
, 2, basename
, "", "obj->array[i]", 1)) return 1;
1362 printf("print_type_write_fct : type has no write function.\n");
1370 /* Function footer */
1378 /* Print the logging function of an event. This is the core of genevent */
1379 int print_event_logging_function(char *basename
, facility_t
*fac
,
1380 event_t
*event
, FILE *fd
)
1382 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1383 int has_argument
= 0;
1384 int has_type_fixed
= 0;
1386 /* Does it support per trace tracing ? */
1387 if(event
->per_trace
) {
1389 fprintf(fd
, "struct ltt_trace_struct *dest_trace");
1393 /* Does it support per tracefile tracing ? */
1394 if(event
->per_tracefile
) {
1399 fprintf(fd
, "unsigned int tracefile_index");
1403 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1404 /* For each field, print the function argument */
1405 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1406 type_descriptor_t
*t
= f
->type
;
1411 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1416 fprintf(fd
, "void");
1421 "#if (!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n",
1422 fac
->capname
, fac
->capname
);
1425 fprintf(fd
,"#else\n");
1428 /* Print the function variables */
1430 fprintf(fd
, "unsigned int index;\n");
1432 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1434 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1436 fprintf(fd
, "void *transport_data;\n");
1438 fprintf(fd
, "char *buffer = NULL;\n");
1440 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1442 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1444 fprintf(fd
, "size_t real_to = 0;\n");
1446 fprintf(fd
, "size_t *to = &real_to;\n");
1448 fprintf(fd
, "size_t real_len = 0;\n");
1450 fprintf(fd
, "size_t *len = &real_len;\n");
1452 fprintf(fd
, "size_t reserve_size;\n");
1454 fprintf(fd
, "size_t slot_size;\n");
1457 if(event
->fields
.position
> 0) {
1458 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1459 /* Search for at least one child with fixed size. It means
1460 * we need local variables.*/
1461 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1462 type_descriptor_t
*type
= field
->type
;
1463 has_type_fixed
= has_type_local(type
);
1464 if(has_type_fixed
) break;
1467 if(has_type_fixed
) {
1468 fprintf(fd
, "size_t align;\n");
1472 fprintf(fd
, "const char *real_from;\n");
1474 fprintf(fd
, "const char **from = &real_from;\n");
1477 fprintf(fd
, "u64 tsc;\n");
1479 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1484 fprintf(fd
, "if (ltt_traces.num_active_traces == 0)\n");
1486 fprintf(fd
, "return;\n");
1490 /* Calculate event variable len + event data alignment offset.
1491 * Assume that the padding for alignment starts at a void*
1493 * This excludes the header size and alignment. */
1496 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1498 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1500 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1502 fprintf(fd
, " * sizeof(void *) address. */\n");
1505 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1506 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1507 type_descriptor_t
*type
= field
->type
;
1510 switch(type
->type
) {
1516 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1519 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1523 if(print_type_write(type
,
1524 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1528 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1530 /* Take locks : make sure the trace does not vanish while we write on
1531 * it. A simple preemption disabling is enough (using rcu traces). */
1533 fprintf(fd
, "preempt_disable();\n");
1535 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1537 /* Get facility index */
1539 if(event
->per_tracefile
) {
1541 fprintf(fd
, "index = tracefile_index;\n");
1545 "index = ltt_get_index_from_facility_%s(" \
1546 "\t\t\t\t\t\tevent_%s_%s);\n",
1547 fac
->name
, fac
->name
, event
->name
);
1552 /* For each trace */
1554 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1557 fprintf(fd
, "if (!trace->active)\n");
1559 fprintf(fd
, "continue;\n\n");
1562 if(event
->per_trace
) {
1564 fprintf(fd
, "if (dest_trace != trace)\n");
1566 fprintf(fd
, "continue;\n\n");
1570 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1575 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1578 fprintf(fd
, "slot_size = 0;\n");
1580 fprintf(fd
, "buffer = ltt_reserve_slot(trace, channel, &transport_data,\n");
1582 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1584 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1585 /* If error, return */
1587 fprintf(fd
, "if (!buffer)\n");
1589 fprintf(fd
, "continue; /* buffer full */\n\n");
1590 //print_tabs(2, fd);
1592 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1594 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1597 /* Write event header */
1598 if(strcmp("compact", fac
->name
) != 0) {
1600 fprintf(fd
, "ltt_write_event_header(trace, channel, buffer,\n");
1602 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1603 fac
->name
, event
->name
);
1605 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
1608 fprintf(fd
, "ltt_write_compact_header(trace, channel, buffer,\n");
1610 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1611 fac
->name
, event
->name
);
1613 if(event
->compact_data
) {
1614 assert(event
->fields
.position
> 0);
1615 field_t
*field
= (field_t
*)(event
->fields
.array
[0]);
1616 fprintf(fd
, "reserve_size, before_hdr_pad, tsc, lttng_param_%s);\n",
1619 fprintf(fd
, "reserve_size, before_hdr_pad, tsc, 0);\n");
1622 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1627 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1628 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1629 type_descriptor_t
*type
= field
->type
;
1631 /* First param is compacted in the header */
1632 if(event
->compact_data
&& i
== 0)
1636 switch(type
->type
) {
1642 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1645 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1650 if(print_type_write(type
,
1651 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1654 /* Don't forget to flush pending memcpy */
1656 fprintf(fd
, "/* Flush pending memcpy */\n");
1658 fprintf(fd
, "if (*len != 0) {\n");
1660 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1662 fprintf(fd
, "*to += *len;\n");
1663 //print_tabs(3, fd);
1664 //fprintf(fd, "from += len;\n");
1666 fprintf(fd
, "*len = 0;\n");
1675 //fprintf(fd, "commit:\n"); /* DEBUG! */
1677 fprintf(fd
, "ltt_commit_slot(channel, &transport_data, buffer, slot_size);\n\n");
1680 fprintf(fd
, "}\n\n");
1684 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1686 fprintf(fd
, "preempt_enable();\n");
1690 fprintf(fd
, "#endif //(!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n\n",
1691 fac
->capname
, fac
->capname
);
1696 int print_event_logging_function_header_user_generic(char *basename
, facility_t
*fac
,
1697 event_t
*event
, FILE *fd
, enum user_fct_types fct_type
)
1701 if(event
->no_instrument_function
&& fct_type
== USER_FCT_PROTO
) {
1702 attrib
= "__attribute__((no_instrument_function)) ";
1706 if(event
->param_buffer
) {
1707 fprintf(fd
, "static inline %sint trace_%s_param_buffer(\n", attrib
, basename
);
1709 fprintf(fd
, "static inline %sint trace_%s(\n",attrib
, basename
);
1711 int has_argument
= 0;
1713 if(event
->param_buffer
) {
1719 fprintf(fd
, "char *buffer");
1724 fprintf(fd
, "size_t reserve_size");
1726 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1727 /* For each field, print the function argument */
1728 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1729 type_descriptor_t
*t
= f
->type
;
1734 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1740 fprintf(fd
, "void");
1747 /* print_event_logging_function_user_generic
1748 * Print the logging function of an event for userspace tracing. This is the
1749 * core of genevent */
1750 int print_event_logging_function_user_generic(char *basename
, facility_t
*fac
,
1751 event_t
*event
, FILE *fd
)
1753 int has_type_fixed
= 0;
1755 if(print_event_logging_function_header_user_generic(basename
, fac
, event
, fd
, USER_FCT_PROTO
)) return 1;
1758 fprintf(fd
, "#ifndef LTT_TRACE_FAST\n");
1759 if(print_event_logging_function_header_user_generic(basename
, fac
, event
, fd
, USER_FCT_DECLARATION
)) return 1;
1762 "#ifndef LTT_TRACE\n");
1765 fprintf(fd
,"#else\n");
1767 /* Print the function variables */
1769 fprintf(fd
, "int ret = 0;\n");
1770 if(event
->param_buffer
) {
1771 //FIX print_tabs(1, fd);
1772 //fprintf(fd, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n");
1778 fprintf(fd
, "char *buffer = NULL;\n");
1780 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1782 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1784 fprintf(fd
, "size_t real_to = 0;\n");
1786 fprintf(fd
, "size_t *to = &real_to;\n");
1788 fprintf(fd
, "size_t real_len = 0;\n");
1790 fprintf(fd
, "size_t *len = &real_len;\n");
1792 fprintf(fd
, "size_t reserve_size;\n");
1794 fprintf(fd
, "size_t slot_size;\n");
1797 if(event
->fields
.position
> 0) {
1798 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1799 /* Search for at least one child with fixed size. It means
1800 * we need local variables.*/
1801 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1802 type_descriptor_t
*type
= field
->type
;
1803 has_type_fixed
= has_type_local(type
);
1804 if(has_type_fixed
) break;
1807 if(has_type_fixed
) {
1808 fprintf(fd
, "size_t align;\n");
1812 fprintf(fd
, "const char *real_from;\n");
1814 fprintf(fd
, "const char **from = &real_from;\n");
1818 /* Calculate event variable len + event data alignment offset.
1819 * Assume that the padding for alignment starts at a void*
1821 * This excludes the header size and alignment. */
1824 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1826 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1828 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1830 fprintf(fd
, " * sizeof(void *) address. */\n");
1833 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1834 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1835 type_descriptor_t
*type
= field
->type
;
1838 switch(type
->type
) {
1844 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1847 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1851 if(print_type_write(type
,
1852 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1856 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1861 fprintf(fd
, "char stack_buffer[reserve_size];\n");
1863 fprintf(fd
, "buffer = stack_buffer;\n");
1867 //print_tabs(2, fd);
1869 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1871 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1876 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1877 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1878 type_descriptor_t
*type
= field
->type
;
1882 switch(type
->type
) {
1888 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1891 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1896 if(print_type_write(type
,
1897 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1900 /* Don't forget to flush pending memcpy */
1902 fprintf(fd
, "/* Flush pending memcpy */\n");
1904 fprintf(fd
, "if (*len != 0) {\n");
1906 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1908 fprintf(fd
, "*to += *len;\n");
1909 //print_tabs(3, fd);
1910 //fprintf(fd, "from += len;\n");
1912 fprintf(fd
, "*len = 0;\n");
1920 fprintf(fd
, "ret = ltt_trace_generic(ltt_facility_%s_%X, event_%s_%s, buffer, reserve_size, LTT_BLOCKING, %u);\n", fac
->name
, fac
->checksum
, fac
->name
, event
->name
, event
->high_priority
);
1923 fprintf(fd
, "}\n\n");
1926 fprintf(fd
, "return ret;\n\n");
1930 "#endif //LTT_TRACE\n");
1931 fprintf(fd
, "#endif //!LTT_TRACE_FAST\n\n");
1936 /* print_event_logging_function_user_fast
1937 * Print the logging function of an event for userspace tracing. This is the
1938 * core of genevent */
1939 int print_event_logging_function_user_fast(char *basename
, facility_t
*fac
,
1940 event_t
*event
, FILE *fd
)
1944 fprintf(fd
, "#ifdef LTT_TRACE_FAST\n");
1946 if(event
->no_instrument_function
) {
1947 attrib
= "__attribute__((no_instrument_function)) ";
1951 fprintf(fd
, "static inline %sint trace_%s(\n",attrib
, basename
);
1953 int has_argument
= 0;
1954 int has_type_fixed
= 0;
1956 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1957 /* For each field, print the function argument */
1958 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1959 type_descriptor_t
*t
= f
->type
;
1964 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1969 fprintf(fd
, "void");
1973 "#ifndef LTT_TRACE\n");
1976 fprintf(fd
,"#else\n");
1978 /* Print the function variables */
1980 fprintf(fd
, "unsigned int index;\n");
1982 fprintf(fd
, "struct ltt_trace_info *trace = thread_trace_info;\n");
1984 fprintf(fd
, "struct ltt_buf *ltt_buf;\n");
1986 fprintf(fd
, "char *buffer = NULL;\n");
1988 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1990 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1992 fprintf(fd
, "size_t real_to = 0;\n");
1994 fprintf(fd
, "size_t *to = &real_to;\n");
1996 fprintf(fd
, "size_t real_len = 0;\n");
1998 fprintf(fd
, "size_t *len = &real_len;\n");
2000 fprintf(fd
, "size_t reserve_size;\n");
2002 fprintf(fd
, "size_t slot_size;\n");
2005 if(event
->fields
.position
> 0) {
2006 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2007 /* Search for at least one child with fixed size. It means
2008 * we need local variables.*/
2009 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2010 type_descriptor_t
*type
= field
->type
;
2011 has_type_fixed
= has_type_local(type
);
2012 if(has_type_fixed
) break;
2015 if(has_type_fixed
) {
2016 fprintf(fd
, "size_t align;\n");
2020 fprintf(fd
, "const char *real_from;\n");
2022 fprintf(fd
, "const char **from = &real_from;\n");
2025 fprintf(fd
, "uint64_t tsc;\n");
2027 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
2031 fprintf(fd
, "if (!trace) {\n");
2033 fprintf(fd
, "ltt_thread_init();\n");
2035 fprintf(fd
, "trace = thread_trace_info;\n");
2037 fprintf(fd
, "}\n\n");
2040 /* Calculate event variable len + event data alignment offset.
2041 * Assume that the padding for alignment starts at a void*
2043 * This excludes the header size and alignment. */
2046 fprintf(fd
, "/* For each field, calculate the field size. */\n");
2048 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
2050 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
2052 fprintf(fd
, " * sizeof(void *) address. */\n");
2055 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2056 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2057 type_descriptor_t
*type
= field
->type
;
2060 switch(type
->type
) {
2066 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
2069 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
2073 if(print_type_write(type
,
2074 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
2078 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
2081 fprintf(fd
, "trace->nesting++;\n");
2083 /* Get facility index */
2087 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
2088 "\t\t\t\t\t\tevent_%s_%s);\n",
2089 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
2096 if(event
->per_trace
) {
2098 fprintf(fd
, "if (dest_trace != trace) continue;\n\n");
2102 fprintf(fd
, "ltt_buf = ltt_get_channel_from_index(trace, index);\n");
2107 /* If error, increment event lost counter (done by ltt_reserve_slot) and
2110 fprintf(fd
, "slot_size = 0;\n");
2112 fprintf(fd
, "buffer = ltt_reserve_slot(trace, ltt_buf,\n");
2114 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
2116 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
2117 /* If error, return */
2119 fprintf(fd
, "if (!buffer)\n");
2121 fprintf(fd
, "goto end; /* buffer full */\n\n");
2122 //print_tabs(2, fd);
2124 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
2126 fprintf(fd
, "*to_base = *to = *len = 0;\n");
2129 /* Write event header */
2131 fprintf(fd
, "ltt_write_event_header(trace, ltt_buf, buffer,\n");
2133 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
2134 fac
->name
, event
->name
);
2136 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
2138 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
2143 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2144 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2145 type_descriptor_t
*type
= field
->type
;
2149 switch(type
->type
) {
2155 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
2158 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
2163 if(print_type_write(type
,
2164 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
2167 /* Don't forget to flush pending memcpy */
2169 fprintf(fd
, "/* Flush pending memcpy */\n");
2171 fprintf(fd
, "if (*len != 0) {\n");
2173 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
2175 fprintf(fd
, "*to += *len;\n");
2176 //print_tabs(3, fd);
2177 //fprintf(fd, "from += len;\n");
2179 fprintf(fd
, "*len = 0;\n");
2188 //fprintf(fd, "commit:\n"); /* DEBUG! */
2190 fprintf(fd
, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n");
2192 fprintf(fd
, "}\n\n");
2194 fprintf(fd
, "end:\n");
2197 fprintf(fd
, "trace->nesting--;\n");
2202 "#endif //LTT_TRACE\n");
2203 fprintf(fd
, "#endif //LTT_TRACE_FAST\n");
2213 /* ltt-facility-name.h : main logging header.
2216 void print_log_header_head(facility_t
*fac
, FILE *fd
)
2218 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2219 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2220 fprintf(fd
, "#include <linux/types.h>\n");
2222 fprintf(fd
, "#include <ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2224 fprintf(fd
, "#include <ltt/ltt-facility-id-%s_%s.h>\n",
2227 fprintf(fd
, "#include <ltt/ltt-tracer.h>\n");
2231 /* ltt-facility-name.h : main logging header.
2234 void print_log_header_head_user(facility_t
*fac
, FILE *fd
)
2236 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2237 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2238 fprintf(fd
, "#include <sys/types.h>\n");
2240 fprintf(fd
, "#include <ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2242 fprintf(fd
, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2245 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2247 fprintf(fd
, "#ifdef __cplusplus\n");
2248 fprintf(fd
, "extern \"C\" {\n");
2249 fprintf(fd
, "#endif\n");
2254 int print_log_header_types(facility_t
*fac
, FILE *fd
)
2256 sequence_t
*types
= &fac
->named_types
.values
;
2257 fprintf(fd
, "/* Named types */\n");
2260 for(unsigned int i
= 0; i
< types
->position
; i
++) {
2261 /* For each named type, print the definition */
2262 if(print_type_declaration(types
->array
[i
], fd
,
2263 0, "", "")) return 1;
2264 /* Print also the align function */
2265 if(((type_descriptor_t
*)types
->array
[i
])->fac
->align
)
2266 if(print_type_alignment_fct(types
->array
[i
], fd
,
2267 0, "", "")) return 1;
2268 /* Print also the write function */
2269 if(print_type_write_fct(types
->array
[i
], fd
,
2270 0, "", "")) return 1;
2275 int print_log_header_events(facility_t
*fac
, FILE *fd
)
2277 sequence_t
*events
= &fac
->events
;
2278 char basename
[PATH_MAX
];
2279 unsigned int facname_len
;
2281 strncpy(basename
, fac
->name
, PATH_MAX
);
2282 facname_len
= strlen(basename
);
2283 strncat(basename
, "_", PATH_MAX
-facname_len
);
2284 facname_len
= strlen(basename
);
2286 for(unsigned int i
= 0; i
< events
->position
; i
++) {
2287 event_t
*event
= (event_t
*)events
->array
[i
];
2288 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
2290 /* For each event, print structure, and then logging function */
2291 fprintf(fd
, "/* Event %s structures */\n",
2293 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
2294 /* For each unnamed type, print the definition */
2295 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
2296 type_descriptor_t
*t
= f
->type
;
2297 if(t
->type_name
== NULL
) {
2298 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
2299 /* Print also the align function */
2301 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2303 /* Print also the write function */
2304 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2310 fprintf(fd
, "/* Event %s logging function */\n",
2314 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
2316 if(print_event_logging_function_user_generic(basename
, fac
, event
, fd
))
2318 if(print_event_logging_function_user_fast(basename
, fac
, event
, fd
))
2329 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
2331 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2334 void print_log_header_tail_user(facility_t
*fac
, FILE *fd
)
2336 fprintf(fd
, "#ifdef __cplusplus\n");
2337 fprintf(fd
, "} /* end of extern \"C\" */\n");
2338 fprintf(fd
, "#endif\n");
2340 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2343 int print_log_header(facility_t
*fac
)
2345 char filename
[PATH_MAX
];
2346 unsigned int filename_size
= 0;
2348 dprintf("%s\n", fac
->name
);
2350 strcpy(filename
, "ltt-facility-");
2351 filename_size
= strlen(filename
);
2353 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2354 filename_size
= strlen(filename
);
2357 strncat(filename
, "_", PATH_MAX
- filename_size
);
2358 filename_size
= strlen(filename
);
2360 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2361 filename_size
= strlen(filename
);
2364 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2365 filename_size
= strlen(filename
);
2368 fd
= fopen(filename
, "w");
2370 printf("Error opening file %s for writing : %s\n",
2371 filename
, strerror(errno
));
2375 /* Print file head */
2377 print_log_header_head(fac
, fd
);
2379 print_log_header_head_user(fac
, fd
);
2381 /* print named types in declaration order */
2382 if(print_log_header_types(fac
, fd
)) return 1;
2385 if(print_log_header_events(fac
, fd
)) return 1;
2387 /* Print file tail */
2389 print_log_header_tail(fac
, fd
);
2391 print_log_header_tail_user(fac
, fd
);
2401 /* ltt-facility-id-name.h : facility id.
2403 int print_id_header(facility_t
*fac
)
2405 char filename
[PATH_MAX
];
2406 unsigned int filename_size
= 0;
2408 char basename
[PATH_MAX
];
2409 char basename_len
= 0;
2411 dprintf("%s\n", fac
->name
);
2413 strcpy(filename
, "ltt-facility-id-");
2414 filename_size
= strlen(filename
);
2416 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2417 filename_size
= strlen(filename
);
2420 strncat(filename
, "_", PATH_MAX
- filename_size
);
2421 filename_size
= strlen(filename
);
2423 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2424 filename_size
= strlen(filename
);
2427 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2428 filename_size
= strlen(filename
);
2431 fd
= fopen(filename
, "w");
2433 printf("Error opening file %s for writing : %s\n",
2434 filename
, strerror(errno
));
2439 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2440 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2441 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2443 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
2445 fprintf(fd
,"/**** facility handle ****/\n\n");
2446 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2447 fac
->name
, fac
->checksum
);
2448 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2450 strncpy(basename
, fac
->name
, PATH_MAX
);
2451 basename_len
= strlen(basename
);
2452 strncat(basename
, "_", PATH_MAX
- basename_len
);
2455 fprintf(fd
,"/**** event index ****/\n\n");
2456 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2458 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2459 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2460 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2462 fprintf(fd
, "event_%s,\n", basename
);
2465 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2466 fprintf(fd
, "};\n");
2470 fprintf(fd
, "#endif //CONFIG_LTT\n");
2471 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2473 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2474 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2475 fprintf(fd
, "#ifdef LTT_TRACE\n");
2477 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n\n");
2479 fprintf(fd
,"/**** facility handle ****/\n\n");
2480 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2481 fac
->name
, fac
->checksum
);
2482 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2484 strncpy(basename
, fac
->name
, PATH_MAX
);
2485 basename_len
= strlen(basename
);
2486 strncat(basename
, "_", PATH_MAX
- basename_len
);
2489 fprintf(fd
,"/**** event index ****/\n\n");
2490 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2492 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2493 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2494 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2496 fprintf(fd
, "event_%s,\n", basename
);
2499 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2500 fprintf(fd
, "};\n");
2504 fprintf(fd
, "#endif //LTT_TRACE\n");
2505 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2515 /* ltt-facility-loader-name.h : facility specific loader info.
2517 int print_loader_header(facility_t
*fac
)
2519 char filename
[PATH_MAX
];
2520 unsigned int filename_size
= 0;
2522 dprintf("%s\n", fac
->name
);
2524 strcpy(filename
, "ltt-facility-loader-");
2525 filename_size
= strlen(filename
);
2527 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2528 filename_size
= strlen(filename
);
2531 strncat(filename
, "_", PATH_MAX
- filename_size
);
2532 filename_size
= strlen(filename
);
2534 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2535 filename_size
= strlen(filename
);
2538 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2539 filename_size
= strlen(filename
);
2542 fd
= fopen(filename
, "w");
2544 printf("Error opening file %s for writing : %s\n",
2545 filename
, strerror(errno
));
2549 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2550 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2551 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
2552 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
2554 fprintf(fd
,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2557 fprintf(fd
,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n",
2560 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2561 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2562 fac
->name
, fac
->checksum
);
2564 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\tltt_facility_%s\n",
2566 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\tltt_facility_%s_%X\n",
2567 fac
->name
, fac
->checksum
);
2568 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t0x%X\n", fac
->checksum
);
2569 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\"%s\"\n", fac
->name
);
2570 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\tfacility_%s_num_events\n\n",
2572 fprintf(fd
,"#define LTT_FACILITY_ALIGNMENT\t\t%u\n\n",
2574 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
2575 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2582 int print_loader_header_user(facility_t
*fac
)
2584 char filename
[PATH_MAX
];
2585 unsigned int filename_size
= 0;
2587 dprintf("%s\n", fac
->name
);
2589 strcpy(filename
, "ltt-facility-loader-");
2590 filename_size
= strlen(filename
);
2592 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2593 filename_size
= strlen(filename
);
2596 strncat(filename
, "_", PATH_MAX
- filename_size
);
2597 filename_size
= strlen(filename
);
2599 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2600 filename_size
= strlen(filename
);
2603 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2604 filename_size
= strlen(filename
);
2607 fd
= fopen(filename
, "w");
2609 printf("Error opening file %s for writing : %s\n",
2610 filename
, strerror(errno
));
2614 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2615 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2616 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n");
2618 fprintf(fd
,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2621 fprintf(fd
,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n",
2624 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2625 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2626 fac
->name
, fac
->checksum
);
2628 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
2630 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
2631 fac
->name
, fac
->checksum
);
2632 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
2633 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
2634 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
2636 fprintf(fd
,"#define LTT_FACILITY_ALIGNMENT\t\t\t\t\t%u\n\n",
2638 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2647 /* ltt-facility-loader-name.c : generic facility loader
2649 int print_loader_c(facility_t
*fac
)
2651 char filename
[PATH_MAX
];
2652 unsigned int filename_size
= 0;
2654 dprintf("%s\n", fac
->name
);
2656 strcpy(filename
, "ltt-facility-loader-");
2657 filename_size
= strlen(filename
);
2659 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2660 filename_size
= strlen(filename
);
2663 strncat(filename
, "_", PATH_MAX
- filename_size
);
2664 filename_size
= strlen(filename
);
2666 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2667 filename_size
= strlen(filename
);
2670 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2671 filename_size
= strlen(filename
);
2674 fd
= fopen(filename
, "w");
2676 printf("Error opening file %s for writing : %s\n",
2677 filename
, strerror(errno
));
2681 fprintf(fd
, "/*\n");
2683 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2685 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2686 fprintf(fd
, " *\n");
2687 fprintf(fd
, " * (C) Copyright 2005 - \n");
2688 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2689 fprintf(fd
, " *\n");
2690 fprintf(fd
, " * Contains the LTT facility loader.\n");
2691 fprintf(fd
, " *\n");
2692 fprintf(fd
, " */\n");
2695 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
2696 fprintf(fd
, "#include <linux/module.h>\n");
2697 fprintf(fd
, "#include <linux/init.h>\n");
2699 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2701 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2702 fac
->name
, fac
->arch
);
2705 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2707 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
2708 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
2710 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
2712 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
2714 fprintf(fd
, "static struct ltt_facility facility = {\n");
2715 fprintf(fd
, "\t.name = ltt_facility_name,\n");
2716 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2717 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2718 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
2719 fprintf(fd
, "\t.alignment = LTT_FACILITY_ALIGNMENT,\n");
2720 fprintf(fd
, "};\n");
2722 fprintf(fd
, "static int __init facility_init(void)\n");
2724 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
2726 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n");
2727 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2728 fprintf(fd
, "\t\n");
2729 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
2732 fprintf(fd
, "#ifndef MODULE\n");
2733 fprintf(fd
, "__initcall(facility_init);\n");
2734 fprintf(fd
, "#else\n");
2735 fprintf(fd
, "module_init(facility_init);\n");
2736 fprintf(fd
, "static void __exit facility_exit(void)\n");
2738 fprintf(fd
, "\tint err;\n");
2740 fprintf(fd
, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n");
2741 fprintf(fd
, "\tif (err != 0)\n");
2742 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
2745 fprintf(fd
, "module_exit(facility_exit)\n");
2747 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
2748 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
2749 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
2751 fprintf(fd
, "#endif //MODULE\n");
2753 fprintf(fd
, "#endif //CONFIG_LTT\n");
2760 int print_loader_c_user(facility_t
*fac
)
2762 char filename
[PATH_MAX
];
2763 unsigned int filename_size
= 0;
2765 dprintf("%s\n", fac
->name
);
2767 strcpy(filename
, "ltt-facility-loader-");
2768 filename_size
= strlen(filename
);
2770 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2771 filename_size
= strlen(filename
);
2774 strncat(filename
, "_", PATH_MAX
- filename_size
);
2775 filename_size
= strlen(filename
);
2777 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2778 filename_size
= strlen(filename
);
2781 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2782 filename_size
= strlen(filename
);
2785 fd
= fopen(filename
, "w");
2787 printf("Error opening file %s for writing : %s\n",
2788 filename
, strerror(errno
));
2792 fprintf(fd
, "/*\n");
2794 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2796 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2797 fprintf(fd
, " *\n");
2798 fprintf(fd
, " * (C) Copyright 2005 - \n");
2799 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2800 fprintf(fd
, " *\n");
2801 fprintf(fd
, " * Contains the LTT user space facility loader.\n");
2802 fprintf(fd
, " *\n");
2803 fprintf(fd
, " */\n");
2806 fprintf(fd
, "#define LTT_TRACE\n");
2807 fprintf(fd
, "#include <error.h>\n");
2808 fprintf(fd
, "#include <stdio.h>\n");
2809 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2811 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2813 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2814 fac
->name
, fac
->arch
);
2816 fprintf(fd
, "static struct user_facility_info facility = {\n");
2817 fprintf(fd
, "\t.name = LTT_FACILITY_NAME,\n");
2818 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2819 fprintf(fd
, "#ifndef LTT_PACK\n");
2820 fprintf(fd
, "\t.alignment = LTT_FACILITY_ALIGNMENT?sizeof(void*):0,\n");
2821 fprintf(fd
, "#else\n");
2822 fprintf(fd
, "\t.alignment = 0,\n");
2823 fprintf(fd
, "#endif //LTT_PACK\n");
2824 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2825 fprintf(fd
, "\t.int_size = sizeof(int),\n");
2826 fprintf(fd
, "\t.long_size = sizeof(long),\n");
2827 fprintf(fd
, "\t.pointer_size = sizeof(void*),\n");
2828 fprintf(fd
, "\t.size_t_size = sizeof(size_t)\n");
2829 fprintf(fd
, "};\n");
2831 fprintf(fd
, "static void __attribute__((constructor)) __ltt_user_init(void)\n");
2833 fprintf(fd
, "\tint err;\n");
2834 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2835 fprintf(fd
, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac
->name
);
2836 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2838 fprintf(fd
, "\terr = ltt_register_generic(<T_FACILITY_SYMBOL, &facility);\n");
2839 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2840 fprintf(fd
, "\t\n");
2841 fprintf(fd
, "\tif (err) {\n");
2842 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2843 fprintf(fd
, "\t\tperror(\"Error in ltt_register_generic\");\n");
2844 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2845 fprintf(fd
, "\t}\n");
2857 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
2859 /*****************************************************************************
2861 * ltt_facility_open : open facilities
2863 * pathname : the path name of the facility
2865 * Open the facility corresponding to the right checksum.
2867 *returns the facility on success, NULL on error.
2868 ****************************************************************************/
2869 facility_t
*ltt_facility_open(char * pathname
)
2874 facility_t
* fac
= NULL
;
2875 char buffer
[BUFFER_SIZE
];
2876 int generated
= FALSE
;
2878 in
.buffer
= &(buffer
[0]);
2880 in
.error
= error_callback
;
2884 in
.fp
= fopen(in
.name
, "r");
2891 token
= getToken(&in
);
2892 if(in
.type
== ENDFILE
) break;
2895 printf("More than one facility in the file. Only using the first one.\n");
2899 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2900 token
= getName(&in
);
2901 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2902 token
= getName(&in
);
2903 if(strcmp(token
, "xml")) in
.error(&in
,"not a facility file");
2904 token
= getName(&in
);
2905 if(strcmp(token
, "version")) in
.error(&in
,"not a facility file");
2906 token
= getName(&in
);
2907 if(strcmp(token
, "=")) in
.error(&in
,"not a facility file");
2908 token
= getQuotedString(&in
);
2909 if(strcmp(token
, "1.0")) in
.error(&in
,"not a facility file");
2910 token
= getName(&in
);
2911 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2912 token
= getToken(&in
);
2913 if(strcmp(token
, ">")) in
.error(&in
,"not a facility file");
2915 token
= getName(&in
);
2916 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2917 token
= getName(&in
);
2918 if(strcmp("facility",token
) == 0) {
2919 fac
= malloc(sizeof(facility_t
));
2921 fac
->description
= NULL
;
2922 sequence_init(&(fac
->events
));
2923 table_init(&(fac
->named_types
));
2924 sequence_init(&(fac
->unnamed_types
));
2926 parseFacility(&in
, fac
);
2928 //check if any namedType is not defined
2929 checkNamedTypesImplemented(&fac
->named_types
);
2931 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2936 printf("facility token was expected in file %s\n", in
.name
);
2947 printf("Cannot find facility %s\n", pathname
);
2954 /* Close the facility */
2955 void ltt_facility_close(facility_t
*fac
)
2959 free(fac
->description
);
2960 freeEvents(&fac
->events
);
2961 sequence_dispose(&fac
->events
);
2962 freeNamedType(&fac
->named_types
);
2963 table_dispose(&fac
->named_types
);
2964 freeTypes(&fac
->unnamed_types
);
2965 sequence_dispose(&fac
->unnamed_types
);
2971 void show_help(int argc
, char ** argv
)
2973 printf("Genevent help : \n");
2975 printf("Use %s name.xml\n", argv
[0]);
2976 printf("to create :\n");
2977 printf("ltt-facility-name.h\n");
2978 printf("ltt-facility-id-name.h\n");
2979 printf("ltt-facility-loader-name.h\n");
2980 printf("ltt-facility-loader-name.c\n");
2981 printf("In the current directory.\n");
2985 /* Parse program arguments */
2987 * 0 : continue program
2988 * -1 : stop program, return 0
2989 * > 0 : stop program, return value as exit.
2991 int check_args(int argc
, char **argv
)
2994 printf("Not enough arguments\n");
2995 show_help(argc
, argv
);
2999 if(strcmp(argv
[1], "-h") == 0) {
3000 show_help(argc
, argv
);
3007 int main(int argc
, char **argv
)
3012 err
= check_args(argc
, argv
);
3013 if(err
> 0) return err
;
3014 else if(err
< 0) return 0;
3016 /* open the facility */
3017 fac
= ltt_facility_open(argv
[1]);
3019 printf("Error opening file %s for reading : %s\n",
3020 argv
[1], strerror(errno
));
3024 /* generate the output C files */
3027 /* ltt-facility-name.h : main logging header.
3029 err
= print_log_header(fac
);
3032 /* ltt-facility-id-name.h : facility id.
3034 err
= print_id_header(fac
);
3037 /* ltt-facility-loader-name.h : facility specific loader info.
3040 err
= print_loader_header(fac
);
3042 err
= print_loader_header_user(fac
);
3045 /* ltt-facility-loader-name.c : generic faciilty loader
3048 err
= print_loader_c(fac
);
3050 err
= print_loader_c_user(fac
);
3053 /* close the facility */
3054 ltt_facility_close(fac
);
This page took 0.257836 seconds and 4 git commands to generate.