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
);
492 fprintf(fd
, "} LTT_ALIGN;\n");
496 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
497 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
498 type_descriptor_t
*type
= field
->type
;
499 if(type
->type_name
== NULL
) {
500 /* Not a named nested type : we must print its declaration first */
501 if(print_type_declaration(type
,
502 fd
, 0, basename
, field
->name
)) return 1;
505 fprintf(fd
, "union lttng_%s", basename
);
507 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
508 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
509 type_descriptor_t
*type
= field
->type
;
511 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
513 fprintf(fd
, "%s", field
->name
);
516 fprintf(fd
, "} LTT_ALIGN;\n");
520 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
528 /* print type alignment.
530 * Copied from construct_types_and_fields in LTTV facility.c
532 * basename is the name which identifies the type (along with a prefix
535 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
536 char *nest_name
, char *field_name
, char *obj_prefix
)
538 char basename
[PATH_MAX
];
539 unsigned int basename_len
= 0;
541 strncpy(basename
, nest_name
, PATH_MAX
);
542 basename_len
= strlen(basename
);
544 /* For a named type, we use the type_name directly */
545 if(td
->type_name
!= NULL
) {
546 strncpy(basename
, td
->type_name
, PATH_MAX
);
547 basename_len
= strlen(basename
);
549 /* For a unnamed type, there must be a field name, except for
551 if((basename_len
!= 0)
552 && (basename
[basename_len
-1] != '_'
553 && field_name
!= NULL
554 && (field_name
[0] != '\0'))) {
555 strncat(basename
, "_", PATH_MAX
- basename_len
);
556 basename_len
= strlen(basename
);
558 if(field_name
!= NULL
)
559 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
562 if(field_name
[0] == '\0') {
563 /* We are in a write function : it's the obj that we must align. */
566 fprintf(fd
, "lttng_get_alignment_sequence_%s(%s)", basename
,
570 fprintf(fd
, "lttng_get_alignment_struct_%s(%s)", basename
,
574 fprintf(fd
, "lttng_get_alignment_union_%s(%s)", basename
,
578 fprintf(fd
, "lttng_get_alignment_array_%s(%s)", basename
,
581 fprintf(fd
, "sizeof(char)");
599 fprintf(fd
, "sizeof(");
600 if(print_type(td
, fd
, 0, basename
, "")) return 1;
605 printf("error : type unexpected\n");
628 fprintf(fd
, "sizeof(");
629 if(print_type(td
, fd
, 0, basename
, "")) return 1;
633 fprintf(fd
, "sizeof(char)");
636 fprintf(fd
, "lttng_get_alignment_sequence_%s(&%s%s)", basename
,
637 obj_prefix
, field_name
);
640 fprintf(fd
, "lttng_get_alignment_struct_%s(&%s%s)", basename
,
641 obj_prefix
, field_name
);
644 fprintf(fd
, "lttng_get_alignment_union_%s(&%s%s)", basename
,
645 obj_prefix
, field_name
);
648 fprintf(fd
, "lttng_get_alignment_array_%s(%s%s)", basename
,
649 obj_prefix
, field_name
);
652 printf("error : type NONE unexpected\n");
663 * Copied from construct_types_and_fields in LTTV facility.c
665 * basename is the name which identifies the type (along with a prefix
668 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
669 char *nest_name
, char *field_name
, char *obj_prefix
, int get_ptr
)
671 char basename
[PATH_MAX
];
672 unsigned int basename_len
= 0;
673 char get_ptr_char
[2] = "";
674 char custom
[PATH_MAX
] = "";
676 strncpy(basename
, nest_name
, PATH_MAX
);
677 basename_len
= strlen(basename
);
679 /* For a named type, we use the type_name directly */
680 if(td
->type_name
!= NULL
) {
681 strncpy(basename
, td
->type_name
, PATH_MAX
);
682 basename_len
= strlen(basename
);
684 /* For a unnamed type, there must be a field name, except for
686 if((basename_len
!= 0)
687 && (basename
[basename_len
-1] != '_'
688 && (field_name
[0] != '\0'))) {
689 strncat(basename
, "_", PATH_MAX
- basename_len
);
690 basename_len
= strlen(basename
);
692 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
696 strcpy(get_ptr_char
, "&");
699 if(td
->custom_write
) {
700 strcpy(custom
, "_custom");
720 print_tabs(tabs
, fd
);
721 fprintf(fd
, "align = ");
722 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
725 print_tabs(tabs
, fd
);
726 fprintf(fd
, "if(*len == 0) {\n");
727 print_tabs(tabs
+1, fd
);
728 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
729 print_tabs(tabs
, fd
);
730 fprintf(fd
, "} else {\n");
731 print_tabs(tabs
+1, fd
);
732 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
733 print_tabs(tabs
, fd
);
737 print_tabs(tabs
, fd
);
738 fprintf(fd
, "*len += ");
739 fprintf(fd
, "sizeof(");
740 if(print_type(td
, fd
, 0, basename
, "")) return 1;
745 print_tabs(tabs
, fd
);
747 "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n",
748 custom
, basename
, obj_prefix
, field_name
);
751 print_tabs(tabs
, fd
);
753 "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
754 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
757 print_tabs(tabs
, fd
);
759 "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
760 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
763 print_tabs(tabs
, fd
);
765 "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);",
766 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
769 print_tabs(tabs
, fd
);
771 "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);",
772 custom
, basename
, obj_prefix
, field_name
);
775 printf("Error : type NONE unexpected\n");
783 /* print need local vars ?.
785 * Copied from print_type_write
787 * Does the type_write call needs local size and from variables ?
788 * return value : 1 yes, 0 no.
791 int has_type_local(type_descriptor_t
* td
)
820 printf("Error : type NONE unexpected\n");
830 /* print type alignment function.
832 * Copied from construct_types_and_fields in LTTV facility.c
834 * basename is the name which identifies the type (along with a prefix
837 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
839 char *nest_name
, char *field_name
)
841 char basename
[PATH_MAX
];
842 unsigned int basename_len
= 0;
844 if(td
->custom_write
) return 0; /* Does print custom type */
846 strncpy(basename
, nest_name
, PATH_MAX
);
847 basename_len
= strlen(basename
);
849 /* For a named type, we use the type_name directly */
850 if(td
->type_name
!= NULL
) {
851 strncpy(basename
, td
->type_name
, PATH_MAX
);
852 basename_len
= strlen(basename
);
854 /* For a unnamed type, there must be a field name, except for
856 if((basename_len
!= 0)
857 && (basename
[basename_len
-1] != '_'
858 && (field_name
[0] != '\0'))) {
859 strncat(basename
, "_", PATH_MAX
- basename_len
);
860 basename_len
= strlen(basename
);
862 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
867 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
868 /* Not a named nested type : we must print its align fct */
869 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
870 0, basename
, "")) return 1;
872 /* Function header */
873 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
876 if(print_type(td
, fd
, 0, basename
, "")) return 1;
877 fprintf(fd
, " *obj)\n");
880 fprintf(fd
, "size_t align=0, localign;");
883 fprintf(fd
, "localign = ");
884 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
885 fd
, 0, basename
, "len", "obj->")) return 1;
888 fprintf(fd
, "align = max(align, localign);\n");
891 fprintf(fd
, "localign = ");
892 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
893 fd
, 0, basename
, "array[0]", "obj->")) return 1;
896 fprintf(fd
, "align = max(align, localign);\n");
899 fprintf(fd
, "return align;\n");
902 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
903 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
904 type_descriptor_t
*type
= field
->type
;
905 if(type
->type_name
== NULL
) {
906 /* Not a named nested type : we must print its align fct */
907 if(print_type_alignment_fct(type
, fd
,
908 0, basename
, field
->name
)) return 1;
911 /* Function header */
912 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
915 if(print_type(td
, fd
, 0, basename
, "")) return 1;
916 fprintf(fd
, " *obj)\n");
919 fprintf(fd
, "size_t align=0, localign;");
921 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
922 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
923 type_descriptor_t
*type
= field
->type
;
925 fprintf(fd
, "localign = ");
926 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
930 fprintf(fd
, "align = max(align, localign);\n");
934 fprintf(fd
, "return align;\n");
938 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
939 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
940 type_descriptor_t
*type
= field
->type
;
941 if(type
->type_name
== NULL
) {
942 /* Not a named nested type : we must print its align fct */
943 if(print_type_alignment_fct(type
, fd
,
944 0, basename
, field
->name
)) return 1;
947 /* Function header */
948 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
951 if(print_type(td
, fd
, 0, basename
, "")) return 1;
952 fprintf(fd
, " *obj)\n");
955 fprintf(fd
, "size_t align=0, localign;");
957 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
958 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
959 type_descriptor_t
*type
= field
->type
;
961 fprintf(fd
, "localign = ");
962 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
966 fprintf(fd
, "align = max(align, localign);\n");
970 fprintf(fd
, "return align;\n");
974 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
975 /* Not a named nested type : we must print its align fct */
976 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
977 0, basename
, "")) return 1;
979 /* Function header */
980 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
983 if(print_type(td
, fd
, 0, basename
, "")) return 1;
984 fprintf(fd
, " obj)\n");
987 fprintf(fd
, "return \n");
988 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
989 fd
, 0, basename
, "", "obj[0]"))
994 dprintf("print_type_alignment_fct : type has no alignment function.\n");
1000 /* Function footer */
1007 /* print type write function.
1009 * Copied from construct_types_and_fields in LTTV facility.c
1011 * basename is the name which identifies the type (along with a prefix
1014 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
1015 char *nest_name
, char *field_name
)
1017 char basename
[PATH_MAX
];
1018 unsigned int basename_len
= 0;
1020 if(td
->custom_write
) return 0; /* Does print custom type */
1022 strncpy(basename
, nest_name
, PATH_MAX
);
1023 basename_len
= strlen(basename
);
1025 /* For a named type, we use the type_name directly */
1026 if(td
->type_name
!= NULL
) {
1027 strncpy(basename
, td
->type_name
, PATH_MAX
);
1028 basename_len
= strlen(basename
);
1030 /* For a unnamed type, there must be a field name, except for
1032 if((basename_len
!= 0)
1033 && (basename
[basename_len
-1] != '_'
1034 && (field_name
[0] != '\0'))) {
1035 strncat(basename
, "_", PATH_MAX
- basename_len
);
1036 basename_len
= strlen(basename
);
1038 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
1043 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
1044 /* Not a named nested type : we must print its write fct */
1045 if(print_type_write_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
1046 0, basename
, "")) return 1;
1050 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1051 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1052 type_descriptor_t
*type
= field
->type
;
1053 if(type
->type_name
== NULL
) {
1054 /* Not a named nested type : we must print its write fct */
1055 if(print_type_write_fct(type
, fd
,
1056 0, basename
, field
->name
)) return 1;
1061 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1062 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1063 type_descriptor_t
*type
= field
->type
;
1064 if(type
->type_name
== NULL
) {
1065 /* Not a named nested type : we must print its write fct */
1066 if(print_type_write_fct(type
, fd
,
1067 0, basename
, field
->name
)) return 1;
1072 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
1073 /* Not a named nested type : we must print its write fct */
1074 if(print_type_write_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
1075 0, basename
, "")) return 1;
1081 dprintf("print_type_write_fct : type has no write function.\n");
1089 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
1093 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
1096 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
1099 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
1102 fprintf(fd
, "static inline void lttng_write_string_%s(\n", basename
);
1105 printf("print_type_write_fct : type has no write function.\n");
1110 fprintf(fd
, "char *buffer,\n");
1112 fprintf(fd
, "size_t *to_base,\n");
1114 fprintf(fd
, "size_t *to,\n");
1116 fprintf(fd
, "const char **from,\n");
1118 fprintf(fd
, "size_t *len,\n");
1120 if(print_type(td
, fd
, 0, basename
, "")) return 1;
1124 fprintf(fd
, " *obj)\n");
1127 fprintf(fd
, " *obj)\n");
1130 fprintf(fd
, " *obj)\n");
1133 fprintf(fd
, " obj)\n");
1136 fprintf(fd
, " obj)\n");
1139 printf("print_type_write_fct : type has no write function.\n");
1148 fprintf(fd
, "size_t size;\n");
1155 fprintf(fd
, "size_t align;\n");
1162 fprintf(fd
, "/* Flush pending memcpy */\n");
1164 fprintf(fd
, "if(*len != 0) {\n");
1166 fprintf(fd
, "if(buffer != NULL)\n");
1168 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1172 fprintf(fd
, "*to += *len;\n");
1174 fprintf(fd
, "*len = 0;\n");
1182 printf("print_type_write_fct : type has no write function.\n");
1187 fprintf(fd
, "align = ");
1188 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
1192 fprintf(fd
, "if(*len == 0) {\n");
1194 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
1196 fprintf(fd
, "} else {\n");
1198 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1203 /* First, check if the type has a fixed size. If it is the case, then the size
1204 * to write is know by the compiler : simply use a sizeof() */
1205 if(has_type_fixed_size(td
)) {
1207 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1210 fprintf(fd
, "*len += sizeof(");
1211 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
1212 fprintf(fd
, ");\n");
1214 /* The type contains nested variable size subtypes :
1215 * we must write field by field. */
1217 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1223 fprintf(fd
, "/* Copy members */\n");
1224 // print_tabs(1, fd);
1225 // fprintf(fd, "size = sizeof(\n");
1226 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1227 fd
, 1, basename
, "len", "obj->", 1)) return 1;
1229 // fprintf(fd, ");\n");
1230 // print_tabs(1, fd);
1231 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1233 fprintf(fd
, "if(buffer != NULL)\n");
1235 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1237 fprintf(fd
, "*to += *len;\n");
1239 fprintf(fd
, "*len = 0;\n");
1242 /* Write the child : varlen child or not ? */
1243 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1244 /* Fixed size len child : use a multiplication of its size */
1245 // print_tabs(1, fd);
1246 // fprintf(fd, "size = sizeof(\n");
1248 //print_tabs(1, fd);
1249 /* We know that *len does not contain alignment because of the
1250 * previous align output. len is always 0 here. */
1251 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1252 fd
, 1, basename
, "array[0]", "obj->", 1))
1254 // fprintf(fd, ");\n");
1257 fprintf(fd
, "*len = obj->len * (*len);\n");
1259 fprintf(fd
, "if(buffer != NULL)\n");
1261 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1263 fprintf(fd
, "*to += *len;\n");
1265 fprintf(fd
, "*len = 0;\n");
1269 fprintf(fd
, "/* Variable length child : iter. */\n");
1271 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1272 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1273 fd
, 2, basename
, "array[i]", "obj->", 1)) return 1;
1279 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1281 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1283 fprintf(fd
, "*to_base = *to_base+*to;\n");
1285 fprintf(fd
, "*to = 0;\n");
1288 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1290 fprintf(fd
, "*from = (const char*)(obj+1);\n");
1294 fprintf(fd
, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1296 fprintf(fd
, "if(buffer != NULL)\n");
1298 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1300 fprintf(fd
, "*to += size;\n");
1303 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1305 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1307 fprintf(fd
, "*to_base = *to_base+*to;\n");
1309 fprintf(fd
, "*to = 0;\n");
1312 fprintf(fd
, "/* Put source *from just after the C string */\n");
1314 fprintf(fd
, "*from += size;\n");
1317 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1318 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1319 type_descriptor_t
*type
= field
->type
;
1320 if(print_type_write(type
,
1321 fd
, 1, basename
, field
->name
, "obj->", 1)) return 1;
1326 printf("ERROR : A union CANNOT contain a variable size child.\n");
1330 /* Write the child : varlen child or not ? */
1331 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1332 /* Error : if an array has a variable size, then its child must also
1333 * have a variable size. */
1337 fprintf(fd
, "/* Variable length child : iter. */\n");
1339 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1340 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1341 fd
, 2, basename
, "", "obj->array[i]", 1)) return 1;
1347 printf("print_type_write_fct : type has no write function.\n");
1355 /* Function footer */
1363 /* Print the logging function of an event. This is the core of genevent */
1364 int print_event_logging_function(char *basename
, facility_t
*fac
,
1365 event_t
*event
, FILE *fd
)
1367 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1368 int has_argument
= 0;
1369 int has_type_fixed
= 0;
1371 /* Does it support per trace tracing ? */
1372 if(event
->per_trace
) {
1374 fprintf(fd
, "struct ltt_trace_struct *dest_trace");
1378 /* Does it support per tracefile tracing ? */
1379 if(event
->per_tracefile
) {
1384 fprintf(fd
, "unsigned int tracefile_index");
1388 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1389 /* For each field, print the function argument */
1390 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1391 type_descriptor_t
*t
= f
->type
;
1396 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1401 fprintf(fd
, "void");
1406 "#if (!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n",
1407 fac
->capname
, fac
->capname
);
1410 fprintf(fd
,"#else\n");
1413 /* Print the function variables */
1415 fprintf(fd
, "unsigned int index;\n");
1417 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1419 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1421 fprintf(fd
, "void *transport_data;\n");
1423 fprintf(fd
, "char *buffer = NULL;\n");
1425 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1427 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1429 fprintf(fd
, "size_t real_to = 0;\n");
1431 fprintf(fd
, "size_t *to = &real_to;\n");
1433 fprintf(fd
, "size_t real_len = 0;\n");
1435 fprintf(fd
, "size_t *len = &real_len;\n");
1437 fprintf(fd
, "size_t reserve_size;\n");
1439 fprintf(fd
, "size_t slot_size;\n");
1442 if(event
->fields
.position
> 0) {
1443 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1444 /* Search for at least one child with fixed size. It means
1445 * we need local variables.*/
1446 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1447 type_descriptor_t
*type
= field
->type
;
1448 has_type_fixed
= has_type_local(type
);
1449 if(has_type_fixed
) break;
1452 if(has_type_fixed
) {
1453 fprintf(fd
, "size_t align;\n");
1457 fprintf(fd
, "const char *real_from;\n");
1459 fprintf(fd
, "const char **from = &real_from;\n");
1462 fprintf(fd
, "u64 tsc;\n");
1464 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1468 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1471 /* Calculate event variable len + event data alignment offset.
1472 * Assume that the padding for alignment starts at a void*
1474 * This excludes the header size and alignment. */
1477 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1479 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1481 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1483 fprintf(fd
, " * sizeof(void *) address. */\n");
1486 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1487 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1488 type_descriptor_t
*type
= field
->type
;
1491 switch(type
->type
) {
1497 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1500 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1504 if(print_type_write(type
,
1505 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1509 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1511 /* Take locks : make sure the trace does not vanish while we write on
1512 * it. A simple preemption disabling is enough (using rcu traces). */
1514 fprintf(fd
, "preempt_disable();\n");
1516 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1518 /* Get facility index */
1520 if(event
->per_tracefile
) {
1522 fprintf(fd
, "index = tracefile_index;\n");
1526 "index = ltt_get_index_from_facility_%s(" \
1527 "\t\t\t\t\t\tevent_%s_%s);\n",
1528 fac
->name
, fac
->name
, event
->name
);
1533 /* For each trace */
1535 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1537 fprintf(fd
, "if(!trace->active) continue;\n\n");
1539 if(event
->per_trace
) {
1541 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1545 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1550 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1553 fprintf(fd
, "slot_size = 0;\n");
1555 fprintf(fd
, "buffer = ltt_reserve_slot(trace, channel, &transport_data,\n");
1557 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1559 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1560 /* If error, return */
1562 fprintf(fd
, "if(!buffer) continue; /* buffer full */\n\n");
1563 //print_tabs(2, fd);
1565 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1567 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1570 /* Write event header */
1572 fprintf(fd
, "ltt_write_event_header(trace, channel, buffer,\n");
1574 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1575 fac
->name
, event
->name
);
1577 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
1579 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1584 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1585 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1586 type_descriptor_t
*type
= field
->type
;
1590 switch(type
->type
) {
1596 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1599 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1604 if(print_type_write(type
,
1605 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1608 /* Don't forget to flush pending memcpy */
1610 fprintf(fd
, "/* Flush pending memcpy */\n");
1612 fprintf(fd
, "if(*len != 0) {\n");
1614 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1616 fprintf(fd
, "*to += *len;\n");
1617 //print_tabs(3, fd);
1618 //fprintf(fd, "from += len;\n");
1620 fprintf(fd
, "*len = 0;\n");
1629 //fprintf(fd, "commit:\n"); /* DEBUG! */
1631 fprintf(fd
, "ltt_commit_slot(channel, &transport_data, buffer, slot_size);\n\n");
1634 fprintf(fd
, "}\n\n");
1638 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1640 fprintf(fd
, "preempt_enable_no_resched();\n");
1644 fprintf(fd
, "#endif //(!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n\n",
1645 fac
->capname
, fac
->capname
);
1650 int print_event_logging_function_header_user_generic(char *basename
, facility_t
*fac
,
1651 event_t
*event
, FILE *fd
, enum user_fct_types fct_type
)
1655 if(event
->no_instrument_function
&& fct_type
== USER_FCT_PROTO
) {
1656 attrib
= "__attribute__((no_instrument_function)) ";
1660 if(event
->param_buffer
) {
1661 fprintf(fd
, "static inline %sint trace_%s_param_buffer(\n", attrib
, basename
);
1663 fprintf(fd
, "static inline %sint trace_%s(\n",attrib
, basename
);
1665 int has_argument
= 0;
1667 if(event
->param_buffer
) {
1673 fprintf(fd
, "char *buffer");
1678 fprintf(fd
, "size_t reserve_size");
1680 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1681 /* For each field, print the function argument */
1682 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1683 type_descriptor_t
*t
= f
->type
;
1688 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1694 fprintf(fd
, "void");
1701 /* print_event_logging_function_user_generic
1702 * Print the logging function of an event for userspace tracing. This is the
1703 * core of genevent */
1704 int print_event_logging_function_user_generic(char *basename
, facility_t
*fac
,
1705 event_t
*event
, FILE *fd
)
1707 int has_type_fixed
= 0;
1709 if(print_event_logging_function_header_user_generic(basename
, fac
, event
, fd
, USER_FCT_PROTO
)) return 1;
1712 fprintf(fd
, "#ifndef LTT_TRACE_FAST\n");
1713 if(print_event_logging_function_header_user_generic(basename
, fac
, event
, fd
, USER_FCT_DECLARATION
)) return 1;
1716 "#ifndef LTT_TRACE\n");
1719 fprintf(fd
,"#else\n");
1721 /* Print the function variables */
1723 fprintf(fd
, "int ret = 0;\n");
1724 if(event
->param_buffer
) {
1725 //FIX print_tabs(1, fd);
1726 //fprintf(fd, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n");
1732 fprintf(fd
, "char *buffer = NULL;\n");
1734 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1736 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1738 fprintf(fd
, "size_t real_to = 0;\n");
1740 fprintf(fd
, "size_t *to = &real_to;\n");
1742 fprintf(fd
, "size_t real_len = 0;\n");
1744 fprintf(fd
, "size_t *len = &real_len;\n");
1746 fprintf(fd
, "size_t reserve_size;\n");
1748 fprintf(fd
, "size_t slot_size;\n");
1751 if(event
->fields
.position
> 0) {
1752 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1753 /* Search for at least one child with fixed size. It means
1754 * we need local variables.*/
1755 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1756 type_descriptor_t
*type
= field
->type
;
1757 has_type_fixed
= has_type_local(type
);
1758 if(has_type_fixed
) break;
1761 if(has_type_fixed
) {
1762 fprintf(fd
, "size_t align;\n");
1766 fprintf(fd
, "const char *real_from;\n");
1768 fprintf(fd
, "const char **from = &real_from;\n");
1772 /* Calculate event variable len + event data alignment offset.
1773 * Assume that the padding for alignment starts at a void*
1775 * This excludes the header size and alignment. */
1778 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1780 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1782 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1784 fprintf(fd
, " * sizeof(void *) address. */\n");
1787 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1788 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1789 type_descriptor_t
*type
= field
->type
;
1792 switch(type
->type
) {
1798 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1801 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1805 if(print_type_write(type
,
1806 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1810 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1815 fprintf(fd
, "char stack_buffer[reserve_size];\n");
1817 fprintf(fd
, "buffer = stack_buffer;\n");
1821 //print_tabs(2, fd);
1823 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1825 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1830 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1831 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1832 type_descriptor_t
*type
= field
->type
;
1836 switch(type
->type
) {
1842 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
1845 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
1850 if(print_type_write(type
,
1851 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1854 /* Don't forget to flush pending memcpy */
1856 fprintf(fd
, "/* Flush pending memcpy */\n");
1858 fprintf(fd
, "if(*len != 0) {\n");
1860 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1862 fprintf(fd
, "*to += *len;\n");
1863 //print_tabs(3, fd);
1864 //fprintf(fd, "from += len;\n");
1866 fprintf(fd
, "*len = 0;\n");
1874 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
);
1877 fprintf(fd
, "}\n\n");
1880 fprintf(fd
, "return ret;\n\n");
1884 "#endif //LTT_TRACE\n");
1885 fprintf(fd
, "#endif //!LTT_TRACE_FAST\n\n");
1890 /* print_event_logging_function_user_fast
1891 * Print the logging function of an event for userspace tracing. This is the
1892 * core of genevent */
1893 int print_event_logging_function_user_fast(char *basename
, facility_t
*fac
,
1894 event_t
*event
, FILE *fd
)
1898 fprintf(fd
, "#ifdef LTT_TRACE_FAST\n");
1900 if(event
->no_instrument_function
) {
1901 attrib
= "__attribute__((no_instrument_function)) ";
1905 fprintf(fd
, "static inline %sint trace_%s(\n",attrib
, basename
);
1907 int has_argument
= 0;
1908 int has_type_fixed
= 0;
1910 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1911 /* For each field, print the function argument */
1912 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1913 type_descriptor_t
*t
= f
->type
;
1918 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1923 fprintf(fd
, "void");
1927 "#ifndef LTT_TRACE\n");
1930 fprintf(fd
,"#else\n");
1932 /* Print the function variables */
1934 fprintf(fd
, "unsigned int index;\n");
1936 fprintf(fd
, "struct ltt_trace_info *trace = thread_trace_info;\n");
1938 fprintf(fd
, "struct ltt_buf *ltt_buf;\n");
1940 fprintf(fd
, "char *buffer = NULL;\n");
1942 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1944 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1946 fprintf(fd
, "size_t real_to = 0;\n");
1948 fprintf(fd
, "size_t *to = &real_to;\n");
1950 fprintf(fd
, "size_t real_len = 0;\n");
1952 fprintf(fd
, "size_t *len = &real_len;\n");
1954 fprintf(fd
, "size_t reserve_size;\n");
1956 fprintf(fd
, "size_t slot_size;\n");
1959 if(event
->fields
.position
> 0) {
1960 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1961 /* Search for at least one child with fixed size. It means
1962 * we need local variables.*/
1963 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1964 type_descriptor_t
*type
= field
->type
;
1965 has_type_fixed
= has_type_local(type
);
1966 if(has_type_fixed
) break;
1969 if(has_type_fixed
) {
1970 fprintf(fd
, "size_t align;\n");
1974 fprintf(fd
, "const char *real_from;\n");
1976 fprintf(fd
, "const char **from = &real_from;\n");
1979 fprintf(fd
, "uint64_t tsc;\n");
1981 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1985 fprintf(fd
, "if(!trace) {\n");
1987 fprintf(fd
, "ltt_thread_init();\n");
1989 fprintf(fd
, "trace = thread_trace_info;\n");
1991 fprintf(fd
, "}\n\n");
1994 /* Calculate event variable len + event data alignment offset.
1995 * Assume that the padding for alignment starts at a void*
1997 * This excludes the header size and alignment. */
2000 fprintf(fd
, "/* For each field, calculate the field size. */\n");
2002 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
2004 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
2006 fprintf(fd
, " * sizeof(void *) address. */\n");
2009 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2010 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2011 type_descriptor_t
*type
= field
->type
;
2014 switch(type
->type
) {
2020 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
2023 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
2027 if(print_type_write(type
,
2028 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
2032 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
2035 fprintf(fd
, "trace->nesting++;\n");
2037 /* Get facility index */
2041 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
2042 "\t\t\t\t\t\tevent_%s_%s);\n",
2043 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
2050 if(event
->per_trace
) {
2052 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
2056 fprintf(fd
, "ltt_buf = ltt_get_channel_from_index(trace, index);\n");
2061 /* If error, increment event lost counter (done by ltt_reserve_slot) and
2064 fprintf(fd
, "slot_size = 0;\n");
2066 fprintf(fd
, "buffer = ltt_reserve_slot(trace, ltt_buf,\n");
2068 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
2070 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
2071 /* If error, return */
2073 fprintf(fd
, "if(!buffer) goto end; /* buffer full */\n\n");
2074 //print_tabs(2, fd);
2076 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
2078 fprintf(fd
, "*to_base = *to = *len = 0;\n");
2081 /* Write event header */
2083 fprintf(fd
, "ltt_write_event_header(trace, ltt_buf, buffer,\n");
2085 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
2086 fac
->name
, event
->name
);
2088 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
2090 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
2095 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2096 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2097 type_descriptor_t
*type
= field
->type
;
2101 switch(type
->type
) {
2107 fprintf(fd
, "*from = (const char*)lttng_param_%s;\n", field
->name
);
2110 fprintf(fd
, "*from = (const char*)<tng_param_%s;\n", field
->name
);
2115 if(print_type_write(type
,
2116 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
2119 /* Don't forget to flush pending memcpy */
2121 fprintf(fd
, "/* Flush pending memcpy */\n");
2123 fprintf(fd
, "if(*len != 0) {\n");
2125 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
2127 fprintf(fd
, "*to += *len;\n");
2128 //print_tabs(3, fd);
2129 //fprintf(fd, "from += len;\n");
2131 fprintf(fd
, "*len = 0;\n");
2140 //fprintf(fd, "commit:\n"); /* DEBUG! */
2142 fprintf(fd
, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n");
2144 fprintf(fd
, "}\n\n");
2146 fprintf(fd
, "end:\n");
2149 fprintf(fd
, "trace->nesting--;\n");
2154 "#endif //LTT_TRACE\n");
2155 fprintf(fd
, "#endif //LTT_TRACE_FAST\n");
2165 /* ltt-facility-name.h : main logging header.
2168 void print_log_header_head(facility_t
*fac
, FILE *fd
)
2170 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2171 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2172 fprintf(fd
, "#include <linux/types.h>\n");
2174 fprintf(fd
, "#include <ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2176 fprintf(fd
, "#include <ltt/ltt-facility-id-%s_%s.h>\n",
2179 fprintf(fd
, "#include <ltt/ltt-tracer.h>\n");
2183 /* ltt-facility-name.h : main logging header.
2186 void print_log_header_head_user(facility_t
*fac
, FILE *fd
)
2188 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2189 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2190 fprintf(fd
, "#include <sys/types.h>\n");
2192 fprintf(fd
, "#include <ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2194 fprintf(fd
, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2197 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2199 fprintf(fd
, "#ifdef __cplusplus\n");
2200 fprintf(fd
, "extern \"C\" {\n");
2201 fprintf(fd
, "#endif\n");
2206 int print_log_header_types(facility_t
*fac
, FILE *fd
)
2208 sequence_t
*types
= &fac
->named_types
.values
;
2209 fprintf(fd
, "/* Named types */\n");
2212 for(unsigned int i
= 0; i
< types
->position
; i
++) {
2213 /* For each named type, print the definition */
2214 if(print_type_declaration(types
->array
[i
], fd
,
2215 0, "", "")) return 1;
2216 /* Print also the align function */
2217 if(print_type_alignment_fct(types
->array
[i
], fd
,
2218 0, "", "")) return 1;
2219 /* Print also the write function */
2220 if(print_type_write_fct(types
->array
[i
], fd
,
2221 0, "", "")) return 1;
2226 int print_log_header_events(facility_t
*fac
, FILE *fd
)
2228 sequence_t
*events
= &fac
->events
;
2229 char basename
[PATH_MAX
];
2230 unsigned int facname_len
;
2232 strncpy(basename
, fac
->name
, PATH_MAX
);
2233 facname_len
= strlen(basename
);
2234 strncat(basename
, "_", PATH_MAX
-facname_len
);
2235 facname_len
= strlen(basename
);
2237 for(unsigned int i
= 0; i
< events
->position
; i
++) {
2238 event_t
*event
= (event_t
*)events
->array
[i
];
2239 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
2241 /* For each event, print structure, and then logging function */
2242 fprintf(fd
, "/* Event %s structures */\n",
2244 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
2245 /* For each unnamed type, print the definition */
2246 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
2247 type_descriptor_t
*t
= f
->type
;
2248 if(t
->type_name
== NULL
) {
2249 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
2250 /* Print also the align function */
2251 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2252 /* Print also the write function */
2253 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2259 fprintf(fd
, "/* Event %s logging function */\n",
2263 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
2265 if(print_event_logging_function_user_generic(basename
, fac
, event
, fd
))
2267 if(print_event_logging_function_user_fast(basename
, fac
, event
, fd
))
2278 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
2280 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2283 void print_log_header_tail_user(facility_t
*fac
, FILE *fd
)
2285 fprintf(fd
, "#ifdef __cplusplus\n");
2286 fprintf(fd
, "} /* end of extern \"C\" */\n");
2287 fprintf(fd
, "#endif\n");
2289 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2292 int print_log_header(facility_t
*fac
)
2294 char filename
[PATH_MAX
];
2295 unsigned int filename_size
= 0;
2297 dprintf("%s\n", fac
->name
);
2299 strcpy(filename
, "ltt-facility-");
2300 filename_size
= strlen(filename
);
2302 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2303 filename_size
= strlen(filename
);
2306 strncat(filename
, "_", PATH_MAX
- filename_size
);
2307 filename_size
= strlen(filename
);
2309 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2310 filename_size
= strlen(filename
);
2313 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2314 filename_size
= strlen(filename
);
2317 fd
= fopen(filename
, "w");
2319 printf("Error opening file %s for writing : %s\n",
2320 filename
, strerror(errno
));
2324 /* Print file head */
2326 print_log_header_head(fac
, fd
);
2328 print_log_header_head_user(fac
, fd
);
2330 /* print named types in declaration order */
2331 if(print_log_header_types(fac
, fd
)) return 1;
2334 if(print_log_header_events(fac
, fd
)) return 1;
2336 /* Print file tail */
2338 print_log_header_tail(fac
, fd
);
2340 print_log_header_tail_user(fac
, fd
);
2350 /* ltt-facility-id-name.h : facility id.
2352 int print_id_header(facility_t
*fac
)
2354 char filename
[PATH_MAX
];
2355 unsigned int filename_size
= 0;
2357 char basename
[PATH_MAX
];
2358 char basename_len
= 0;
2360 dprintf("%s\n", fac
->name
);
2362 strcpy(filename
, "ltt-facility-id-");
2363 filename_size
= strlen(filename
);
2365 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2366 filename_size
= strlen(filename
);
2369 strncat(filename
, "_", PATH_MAX
- filename_size
);
2370 filename_size
= strlen(filename
);
2372 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2373 filename_size
= strlen(filename
);
2376 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2377 filename_size
= strlen(filename
);
2380 fd
= fopen(filename
, "w");
2382 printf("Error opening file %s for writing : %s\n",
2383 filename
, strerror(errno
));
2388 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2389 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2390 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2392 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
2394 fprintf(fd
,"/**** facility handle ****/\n\n");
2395 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2396 fac
->name
, fac
->checksum
);
2397 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2399 strncpy(basename
, fac
->name
, PATH_MAX
);
2400 basename_len
= strlen(basename
);
2401 strncat(basename
, "_", PATH_MAX
- basename_len
);
2404 fprintf(fd
,"/**** event index ****/\n\n");
2405 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2407 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2408 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2409 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2411 fprintf(fd
, "event_%s,\n", basename
);
2414 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2415 fprintf(fd
, "};\n");
2419 fprintf(fd
, "#endif //CONFIG_LTT\n");
2420 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2422 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2423 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2424 fprintf(fd
, "#ifdef LTT_TRACE\n");
2426 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n\n");
2428 fprintf(fd
,"/**** facility handle ****/\n\n");
2429 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2430 fac
->name
, fac
->checksum
);
2431 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2433 strncpy(basename
, fac
->name
, PATH_MAX
);
2434 basename_len
= strlen(basename
);
2435 strncat(basename
, "_", PATH_MAX
- basename_len
);
2438 fprintf(fd
,"/**** event index ****/\n\n");
2439 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2441 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2442 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2443 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2445 fprintf(fd
, "event_%s,\n", basename
);
2448 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2449 fprintf(fd
, "};\n");
2453 fprintf(fd
, "#endif //LTT_TRACE\n");
2454 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2464 /* ltt-facility-loader-name.h : facility specific loader info.
2466 int print_loader_header(facility_t
*fac
)
2468 char filename
[PATH_MAX
];
2469 unsigned int filename_size
= 0;
2471 dprintf("%s\n", fac
->name
);
2473 strcpy(filename
, "ltt-facility-loader-");
2474 filename_size
= strlen(filename
);
2476 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2477 filename_size
= strlen(filename
);
2480 strncat(filename
, "_", PATH_MAX
- filename_size
);
2481 filename_size
= strlen(filename
);
2483 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2484 filename_size
= strlen(filename
);
2487 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2488 filename_size
= strlen(filename
);
2491 fd
= fopen(filename
, "w");
2493 printf("Error opening file %s for writing : %s\n",
2494 filename
, strerror(errno
));
2498 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2499 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2500 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
2501 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
2503 fprintf(fd
,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2506 fprintf(fd
,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n",
2509 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2510 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2511 fac
->name
, fac
->checksum
);
2513 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\tltt_facility_%s\n",
2515 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\tltt_facility_%s_%X\n",
2516 fac
->name
, fac
->checksum
);
2517 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t0x%X\n", fac
->checksum
);
2518 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\"%s\"\n", fac
->name
);
2519 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\tfacility_%s_num_events\n\n",
2521 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
2522 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2529 int print_loader_header_user(facility_t
*fac
)
2531 char filename
[PATH_MAX
];
2532 unsigned int filename_size
= 0;
2534 dprintf("%s\n", fac
->name
);
2536 strcpy(filename
, "ltt-facility-loader-");
2537 filename_size
= strlen(filename
);
2539 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2540 filename_size
= strlen(filename
);
2543 strncat(filename
, "_", PATH_MAX
- filename_size
);
2544 filename_size
= strlen(filename
);
2546 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2547 filename_size
= strlen(filename
);
2550 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2551 filename_size
= strlen(filename
);
2554 fd
= fopen(filename
, "w");
2556 printf("Error opening file %s for writing : %s\n",
2557 filename
, strerror(errno
));
2561 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2562 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2563 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n");
2565 fprintf(fd
,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2568 fprintf(fd
,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n",
2571 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2572 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2573 fac
->name
, fac
->checksum
);
2575 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
2577 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
2578 fac
->name
, fac
->checksum
);
2579 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
2580 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
2581 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
2583 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2592 /* ltt-facility-loader-name.c : generic facility loader
2594 int print_loader_c(facility_t
*fac
)
2596 char filename
[PATH_MAX
];
2597 unsigned int filename_size
= 0;
2599 dprintf("%s\n", fac
->name
);
2601 strcpy(filename
, "ltt-facility-loader-");
2602 filename_size
= strlen(filename
);
2604 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2605 filename_size
= strlen(filename
);
2608 strncat(filename
, "_", PATH_MAX
- filename_size
);
2609 filename_size
= strlen(filename
);
2611 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2612 filename_size
= strlen(filename
);
2615 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2616 filename_size
= strlen(filename
);
2619 fd
= fopen(filename
, "w");
2621 printf("Error opening file %s for writing : %s\n",
2622 filename
, strerror(errno
));
2626 fprintf(fd
, "/*\n");
2628 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2630 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2631 fprintf(fd
, " *\n");
2632 fprintf(fd
, " * (C) Copyright 2005 - \n");
2633 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2634 fprintf(fd
, " *\n");
2635 fprintf(fd
, " * Contains the LTT facility loader.\n");
2636 fprintf(fd
, " *\n");
2637 fprintf(fd
, " */\n");
2640 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
2641 fprintf(fd
, "#include <linux/module.h>\n");
2642 fprintf(fd
, "#include <linux/init.h>\n");
2643 fprintf(fd
, "#include <linux/config.h>\n");
2645 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2647 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2648 fac
->name
, fac
->arch
);
2651 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2653 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
2654 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
2656 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
2658 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
2660 fprintf(fd
, "static struct ltt_facility facility = {\n");
2661 fprintf(fd
, "\t.name = ltt_facility_name,\n");
2662 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2663 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2664 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
2665 fprintf(fd
, "};\n");
2667 fprintf(fd
, "static int __init facility_init(void)\n");
2669 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
2671 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n");
2672 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2673 fprintf(fd
, "\t\n");
2674 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
2677 fprintf(fd
, "#ifndef MODULE\n");
2678 fprintf(fd
, "__initcall(facility_init);\n");
2679 fprintf(fd
, "#else\n");
2680 fprintf(fd
, "module_init(facility_init);\n");
2681 fprintf(fd
, "static void __exit facility_exit(void)\n");
2683 fprintf(fd
, "\tint err;\n");
2685 fprintf(fd
, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n");
2686 fprintf(fd
, "\tif(err != 0)\n");
2687 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
2690 fprintf(fd
, "module_exit(facility_exit)\n");
2692 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
2693 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
2694 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
2696 fprintf(fd
, "#endif //MODULE\n");
2698 fprintf(fd
, "#endif //CONFIG_LTT\n");
2705 int print_loader_c_user(facility_t
*fac
)
2707 char filename
[PATH_MAX
];
2708 unsigned int filename_size
= 0;
2710 dprintf("%s\n", fac
->name
);
2712 strcpy(filename
, "ltt-facility-loader-");
2713 filename_size
= strlen(filename
);
2715 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2716 filename_size
= strlen(filename
);
2719 strncat(filename
, "_", PATH_MAX
- filename_size
);
2720 filename_size
= strlen(filename
);
2722 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2723 filename_size
= strlen(filename
);
2726 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2727 filename_size
= strlen(filename
);
2730 fd
= fopen(filename
, "w");
2732 printf("Error opening file %s for writing : %s\n",
2733 filename
, strerror(errno
));
2737 fprintf(fd
, "/*\n");
2739 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2741 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2742 fprintf(fd
, " *\n");
2743 fprintf(fd
, " * (C) Copyright 2005 - \n");
2744 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2745 fprintf(fd
, " *\n");
2746 fprintf(fd
, " * Contains the LTT user space facility loader.\n");
2747 fprintf(fd
, " *\n");
2748 fprintf(fd
, " */\n");
2751 fprintf(fd
, "#define LTT_TRACE\n");
2752 fprintf(fd
, "#include <error.h>\n");
2753 fprintf(fd
, "#include <stdio.h>\n");
2754 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2756 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2758 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2759 fac
->name
, fac
->arch
);
2761 fprintf(fd
, "static struct user_facility_info facility = {\n");
2762 fprintf(fd
, "\t.name = LTT_FACILITY_NAME,\n");
2763 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2764 fprintf(fd
, "#ifndef LTT_PACK\n");
2765 fprintf(fd
, "\t.alignment = sizeof(void*),\n");
2766 fprintf(fd
, "#else\n");
2767 fprintf(fd
, "\t.alignment = 0,\n");
2768 fprintf(fd
, "#endif //LTT_PACK\n");
2769 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2770 fprintf(fd
, "\t.int_size = sizeof(int),\n");
2771 fprintf(fd
, "\t.long_size = sizeof(long),\n");
2772 fprintf(fd
, "\t.pointer_size = sizeof(void*),\n");
2773 fprintf(fd
, "\t.size_t_size = sizeof(size_t)\n");
2774 fprintf(fd
, "};\n");
2776 fprintf(fd
, "static void __attribute__((constructor)) __ltt_user_init(void)\n");
2778 fprintf(fd
, "\tint err;\n");
2779 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2780 fprintf(fd
, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac
->name
);
2781 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2783 fprintf(fd
, "\terr = ltt_register_generic(<T_FACILITY_SYMBOL, &facility);\n");
2784 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2785 fprintf(fd
, "\t\n");
2786 fprintf(fd
, "\tif(err) {\n");
2787 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2788 fprintf(fd
, "\t\tperror(\"Error in ltt_register_generic\");\n");
2789 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2790 fprintf(fd
, "\t}\n");
2802 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
2804 /*****************************************************************************
2806 * ltt_facility_open : open facilities
2808 * pathname : the path name of the facility
2810 * Open the facility corresponding to the right checksum.
2812 *returns the facility on success, NULL on error.
2813 ****************************************************************************/
2814 facility_t
*ltt_facility_open(char * pathname
)
2819 facility_t
* fac
= NULL
;
2820 char buffer
[BUFFER_SIZE
];
2821 int generated
= FALSE
;
2823 in
.buffer
= &(buffer
[0]);
2825 in
.error
= error_callback
;
2829 in
.fp
= fopen(in
.name
, "r");
2836 token
= getToken(&in
);
2837 if(in
.type
== ENDFILE
) break;
2840 printf("More than one facility in the file. Only using the first one.\n");
2844 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2845 token
= getName(&in
);
2846 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2847 token
= getName(&in
);
2848 if(strcmp(token
, "xml")) in
.error(&in
,"not a facility file");
2849 token
= getName(&in
);
2850 if(strcmp(token
, "version")) in
.error(&in
,"not a facility file");
2851 token
= getName(&in
);
2852 if(strcmp(token
, "=")) in
.error(&in
,"not a facility file");
2853 token
= getQuotedString(&in
);
2854 if(strcmp(token
, "1.0")) in
.error(&in
,"not a facility file");
2855 token
= getName(&in
);
2856 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2857 token
= getToken(&in
);
2858 if(strcmp(token
, ">")) in
.error(&in
,"not a facility file");
2860 token
= getName(&in
);
2861 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2862 token
= getName(&in
);
2863 if(strcmp("facility",token
) == 0) {
2864 fac
= malloc(sizeof(facility_t
));
2866 fac
->description
= NULL
;
2867 sequence_init(&(fac
->events
));
2868 table_init(&(fac
->named_types
));
2869 sequence_init(&(fac
->unnamed_types
));
2871 parseFacility(&in
, fac
);
2873 //check if any namedType is not defined
2874 checkNamedTypesImplemented(&fac
->named_types
);
2876 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2881 printf("facility token was expected in file %s\n", in
.name
);
2892 printf("Cannot find facility %s\n", pathname
);
2899 /* Close the facility */
2900 void ltt_facility_close(facility_t
*fac
)
2904 free(fac
->description
);
2905 freeEvents(&fac
->events
);
2906 sequence_dispose(&fac
->events
);
2907 freeNamedType(&fac
->named_types
);
2908 table_dispose(&fac
->named_types
);
2909 freeTypes(&fac
->unnamed_types
);
2910 sequence_dispose(&fac
->unnamed_types
);
2916 void show_help(int argc
, char ** argv
)
2918 printf("Genevent help : \n");
2920 printf("Use %s name.xml\n", argv
[0]);
2921 printf("to create :\n");
2922 printf("ltt-facility-name.h\n");
2923 printf("ltt-facility-id-name.h\n");
2924 printf("ltt-facility-loader-name.h\n");
2925 printf("ltt-facility-loader-name.c\n");
2926 printf("In the current directory.\n");
2930 /* Parse program arguments */
2932 * 0 : continue program
2933 * -1 : stop program, return 0
2934 * > 0 : stop program, return value as exit.
2936 int check_args(int argc
, char **argv
)
2939 printf("Not enough arguments\n");
2940 show_help(argc
, argv
);
2944 if(strcmp(argv
[1], "-h") == 0) {
2945 show_help(argc
, argv
);
2952 int main(int argc
, char **argv
)
2957 err
= check_args(argc
, argv
);
2958 if(err
> 0) return err
;
2959 else if(err
< 0) return 0;
2961 /* open the facility */
2962 fac
= ltt_facility_open(argv
[1]);
2964 printf("Error opening file %s for reading : %s\n",
2965 argv
[1], strerror(errno
));
2969 /* generate the output C files */
2972 /* ltt-facility-name.h : main logging header.
2974 err
= print_log_header(fac
);
2977 /* ltt-facility-id-name.h : facility id.
2979 err
= print_id_header(fac
);
2982 /* ltt-facility-loader-name.h : facility specific loader info.
2985 err
= print_loader_header(fac
);
2987 err
= print_loader_header_user(fac
);
2990 /* ltt-facility-loader-name.c : generic faciilty loader
2993 err
= print_loader_c(fac
);
2995 err
= print_loader_c_user(fac
);
2998 /* close the facility */
2999 ltt_facility_close(fac
);
This page took 0.094121 seconds and 4 git commands to generate.