1 /******************************************************************************
4 * Event generator. XML to logging C code converter.
9 * Will generate ltt-facility-name.h, ltt-facility-id-name.h
10 * ltt-facility-loader-name.c, ltt-facility-loader-name.h
11 * in the current directory.
15 * - C types : struct, union, enum, basic types.
16 * - Architectures : LP32, ILP32, ILP64, LLP64, LP64.
18 * Additionnal structures supported :
19 * - embedded variable size strings
20 * - embedded variable size arrays
21 * - embedded variable size sequences
25 * enums are limited to integer type, as this is what is used in C. Note,
26 * however, that ISO/IEC 9899:TC2 specify that the type of enum can be char,
27 * unsigned int or int. This is implementation defined (compiler). That's why we
28 * add a check for sizeof enum.
31 * Because of archtecture defined type sizes, we need to ask for ltt_align
32 * (which gives the alignment) by passing basic types, not their actual sizes.
33 * It's up to ltt_align to determine sizes of types.
36 * http://www.usenix.org/publications/login/standards/10.data.html
37 * (Andrew Josey <a.josey@opengroup.org>) :
39 * Data Type LP32 ILP32 ILP64 LLP64 LP64
41 * short 16 16 16 16 16
45 * long long (int64) 64
46 * pointer 32 32 64 64 64
48 * With these constraints :
49 * sizeof(char) <= sizeof(short) <= sizeof(int)
50 * <= sizeof(long) = sizeof(size_t)
52 * and therefore sizeof(long) <= sizeof(pointer) <= sizeof(size_t)
54 * Which means we only have to remember which is the biggest type in a structure
55 * to know the structure's alignment.
62 #include <sys/types.h>
77 /* Debugging printf */
79 #define dprintf(...) \
81 printf(__FILE__ ",%u,%s: ",\
91 void print_tabs(unsigned int tabs
, FILE *fd
)
93 for(unsigned int i
= 0; i
<tabs
;i
++)
99 * Copied from construct_types_and_fields in LTTV facility.c */
101 int print_type(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
102 char *nest_name
, char *field_name
)
104 char basename
[PATH_MAX
];
105 unsigned int basename_len
= 0;
107 strcpy(basename
, nest_name
);
108 basename_len
= strlen(basename
);
110 /* For a named type, we use the type_name directly */
111 if(td
->type_name
!= NULL
) {
112 strncpy(basename
, td
->type_name
, PATH_MAX
);
113 basename_len
= strlen(basename
);
115 /* For a unnamed type, there must be a field name */
116 if((basename_len
!= 0)
117 && (basename
[basename_len
-1] != '_')
118 && (field_name
[0] != '\0')) {
119 strncat(basename
, "_", PATH_MAX
- basename_len
);
120 basename_len
= strlen(basename
);
122 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
127 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
130 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
133 fprintf(fd
, "signed char");
136 fprintf(fd
, "unsigned char");
139 fprintf(fd
, "short");
142 fprintf(fd
, "unsigned short");
148 fprintf(fd
, "unsigned int");
151 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
154 fprintf(fd
, "const void *");
160 fprintf(fd
, "unsigned long");
163 fprintf(fd
, "size_t");
166 fprintf(fd
, "ssize_t");
169 fprintf(fd
, "off_t");
172 fprintf(fd
, "const char *");
175 fprintf(fd
, "enum lttng_%s", basename
);
178 fprintf(fd
, "lttng_array_%s", basename
);
181 fprintf(fd
, "lttng_sequence_%s", basename
);
184 fprintf(fd
, "struct lttng_%s", basename
);
187 fprintf(fd
, "union lttng_%s", basename
);
190 printf("print_type : unknown type\n");
197 /* Print logging function argument */
198 int print_arg(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
199 char *nest_name
, char *field_name
)
201 char basename
[PATH_MAX
];
202 unsigned int basename_len
= 0;
204 strcpy(basename
, nest_name
);
205 basename_len
= strlen(basename
);
207 /* For a named type, we use the type_name directly */
208 if(td
->type_name
!= NULL
) {
209 strncpy(basename
, td
->type_name
, PATH_MAX
);
210 basename_len
= strlen(basename
);
212 /* For a unnamed type, there must be a field name */
213 if((basename_len
!= 0)
214 && (basename
[basename_len
-1] != '_')
215 && (field_name
[0] != '\0')) {
216 strncat(basename
, "_", PATH_MAX
- basename_len
);
217 basename_len
= strlen(basename
);
219 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
222 print_tabs(tabs
, fd
);
226 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
227 fprintf(fd
, "lttng_param_%s", field_name
);
230 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
231 fprintf(fd
, "lttng_param_%s", field_name
);
234 fprintf(fd
, "signed char");
235 fprintf(fd
, " lttng_param_%s", field_name
);
238 fprintf(fd
, "unsigned char");
239 fprintf(fd
, " lttng_param_%s", field_name
);
242 fprintf(fd
, "short");
243 fprintf(fd
, " lttng_param_%s", field_name
);
246 fprintf(fd
, "unsigned short");
247 fprintf(fd
, " lttng_param_%s", field_name
);
251 fprintf(fd
, " lttng_param_%s", field_name
);
254 fprintf(fd
, "unsigned int");
255 fprintf(fd
, " lttng_param_%s", field_name
);
258 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
259 fprintf(fd
, " lttng_param_%s", field_name
);
262 fprintf(fd
, "const void *");
263 fprintf(fd
, " lttng_param_%s", field_name
);
267 fprintf(fd
, " lttng_param_%s", field_name
);
270 fprintf(fd
, "unsigned long");
271 fprintf(fd
, " lttng_param_%s", field_name
);
274 fprintf(fd
, "size_t");
275 fprintf(fd
, " lttng_param_%s", field_name
);
278 fprintf(fd
, "ssize_t");
279 fprintf(fd
, " lttng_param_%s", field_name
);
282 fprintf(fd
, "off_t");
283 fprintf(fd
, " lttng_param_%s", field_name
);
286 fprintf(fd
, "const char *");
287 fprintf(fd
, " lttng_param_%s", field_name
);
290 fprintf(fd
, "enum lttng_%s", basename
);
291 fprintf(fd
, " lttng_param_%s", field_name
);
294 fprintf(fd
, "lttng_array_%s", basename
);
295 fprintf(fd
, " lttng_param_%s", field_name
);
298 fprintf(fd
, "lttng_sequence_%s *", basename
);
299 fprintf(fd
, " lttng_param_%s", field_name
);
302 fprintf(fd
, "struct lttng_%s *", basename
);
303 fprintf(fd
, " lttng_param_%s", field_name
);
306 fprintf(fd
, "union lttng_%s *", basename
);
307 fprintf(fd
, " lttng_param_%s", field_name
);
310 printf("print_type : unknown type\n");
318 /* Does the type has a fixed size ? (as know from the compiler)
321 * 0 : variable length
323 int has_type_fixed_size(type_descriptor_t
*td
)
342 case UNION
: /* The union must have fixed size children. Must be checked by
352 int has_type_fixed
= 0;
353 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
354 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
355 type_descriptor_t
*type
= field
->type
;
357 has_type_fixed
= has_type_fixed_size(type
);
358 if(!has_type_fixed
) return 0;
364 assert(td
->size
>= 0);
365 return has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
);
368 printf("There is a type defined to NONE : bad.\n");
372 return 0; //make gcc happy.
379 /* print type declaration.
381 * Copied from construct_types_and_fields in LTTV facility.c */
383 int print_type_declaration(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
384 char *nest_name
, char *field_name
)
386 char basename
[PATH_MAX
];
387 unsigned int basename_len
= 0;
389 if(td
->custom_write
) return 0; /* Does print custom type */
391 strncpy(basename
, nest_name
, PATH_MAX
);
392 basename_len
= strlen(basename
);
394 /* For a named type, we use the type_name directly */
395 if(td
->type_name
!= NULL
) {
396 strncpy(basename
, td
->type_name
, PATH_MAX
);
397 basename_len
= strlen(basename
);
399 /* For a unnamed type, there must be a field name, except for
401 if((basename_len
!= 0)
402 && (basename
[basename_len
-1] != '_'
403 && (field_name
[0] != '\0'))) {
404 strncat(basename
, "_", PATH_MAX
- basename_len
);
405 basename_len
= strlen(basename
);
407 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
412 fprintf(fd
, "enum lttng_%s", basename
);
414 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
416 fprintf(fd
, "LTTNG_%s = %d", ((char*)td
->labels
.array
[i
]),
417 (*(int*)td
->labels_values
.array
[i
]));
425 dprintf("%s\n", basename
);
426 assert(td
->size
>= 0);
427 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
428 /* Not a named nested type : we must print its declaration first */
429 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
430 fd
, 0, basename
, "")) return 1;
432 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename
,
434 fprintf(fd
, "typedef ");
435 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
436 fd
, tabs
, basename
, "")) return 1;
437 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
442 /* We assume that the sequence length type does not need to be declared.
444 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
445 /* Not a named nested type : we must print its declaration first */
446 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
447 fd
, 0, basename
, "")) return 1;
449 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
452 fprintf(fd
, "struct lttng_sequence_%s", basename
);
455 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
456 fd
, tabs
, basename
, "")) return 1;
457 fprintf(fd
, " len;\n");
459 fprintf(fd
, "const ");
460 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
461 fd
, tabs
, basename
, "")) return 1;
462 fprintf(fd
, " *array;\n");
463 fprintf(fd
, "};\n"); /* We do not LTT_ALIGN, because we never copy
464 it to the buffer directly. */
469 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
470 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
471 type_descriptor_t
*type
= field
->type
;
472 if(type
->type_name
== NULL
) {
473 /* Not a named nested type : we must print its declaration first */
474 if(print_type_declaration(type
,
475 fd
, 0, basename
, field
->name
)) return 1;
478 fprintf(fd
, "struct lttng_%s", basename
);
480 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
481 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
482 type_descriptor_t
*type
= field
->type
;
484 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
486 fprintf(fd
, "%s", field
->name
);
489 fprintf(fd
, "} LTT_ALIGN;\n");
493 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
494 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
495 type_descriptor_t
*type
= field
->type
;
496 if(type
->type_name
== NULL
) {
497 /* Not a named nested type : we must print its declaration first */
498 if(print_type_declaration(type
,
499 fd
, 0, basename
, field
->name
)) return 1;
502 fprintf(fd
, "union lttng_%s", basename
);
504 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
505 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
506 type_descriptor_t
*type
= field
->type
;
508 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
510 fprintf(fd
, "%s", field
->name
);
513 fprintf(fd
, "} LTT_ALIGN;\n");
517 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
525 /* print type alignment.
527 * Copied from construct_types_and_fields in LTTV facility.c
529 * basename is the name which identifies the type (along with a prefix
532 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
533 char *nest_name
, char *field_name
, char *obj_prefix
)
535 char basename
[PATH_MAX
];
536 unsigned int basename_len
= 0;
538 strncpy(basename
, nest_name
, PATH_MAX
);
539 basename_len
= strlen(basename
);
541 /* For a named type, we use the type_name directly */
542 if(td
->type_name
!= NULL
) {
543 strncpy(basename
, td
->type_name
, PATH_MAX
);
544 basename_len
= strlen(basename
);
546 /* For a unnamed type, there must be a field name, except for
548 if((basename_len
!= 0)
549 && (basename
[basename_len
-1] != '_'
550 && field_name
!= NULL
551 && (field_name
[0] != '\0'))) {
552 strncat(basename
, "_", PATH_MAX
- basename_len
);
553 basename_len
= strlen(basename
);
555 if(field_name
!= NULL
)
556 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
559 if(field_name
[0] == '\0') {
560 /* We are in a write function : it's the obj that we must align. */
563 fprintf(fd
, "lttng_get_alignment_sequence_%s(%s)", basename
,
567 fprintf(fd
, "lttng_get_alignment_struct_%s(%s)", basename
,
571 fprintf(fd
, "lttng_get_alignment_union_%s(%s)", basename
,
575 fprintf(fd
, "lttng_get_alignment_array_%s(%s)", basename
,
578 fprintf(fd
, "sizeof(char)");
596 fprintf(fd
, "sizeof(");
597 if(print_type(td
, fd
, 0, basename
, "")) return 1;
602 printf("error : type unexpected\n");
625 fprintf(fd
, "sizeof(");
626 if(print_type(td
, fd
, 0, basename
, "")) return 1;
630 fprintf(fd
, "sizeof(char)");
633 fprintf(fd
, "lttng_get_alignment_sequence_%s(&%s%s)", basename
,
634 obj_prefix
, field_name
);
637 fprintf(fd
, "lttng_get_alignment_struct_%s(&%s%s)", basename
,
638 obj_prefix
, field_name
);
641 fprintf(fd
, "lttng_get_alignment_union_%s(&%s%s)", basename
,
642 obj_prefix
, field_name
);
645 fprintf(fd
, "lttng_get_alignment_array_%s(%s%s)", basename
,
646 obj_prefix
, field_name
);
649 printf("error : type NONE unexpected\n");
660 * Copied from construct_types_and_fields in LTTV facility.c
662 * basename is the name which identifies the type (along with a prefix
665 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
666 char *nest_name
, char *field_name
, char *obj_prefix
, int get_ptr
)
668 char basename
[PATH_MAX
];
669 unsigned int basename_len
= 0;
670 char get_ptr_char
[2] = "";
671 char custom
[PATH_MAX
] = "";
673 strncpy(basename
, nest_name
, PATH_MAX
);
674 basename_len
= strlen(basename
);
676 /* For a named type, we use the type_name directly */
677 if(td
->type_name
!= NULL
) {
678 strncpy(basename
, td
->type_name
, PATH_MAX
);
679 basename_len
= strlen(basename
);
681 /* For a unnamed type, there must be a field name, except for
683 if((basename_len
!= 0)
684 && (basename
[basename_len
-1] != '_'
685 && (field_name
[0] != '\0'))) {
686 strncat(basename
, "_", PATH_MAX
- basename_len
);
687 basename_len
= strlen(basename
);
689 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
693 strcpy(get_ptr_char
, "&");
696 if(td
->custom_write
) {
697 strcpy(custom
, "_custom");
717 print_tabs(tabs
, fd
);
718 fprintf(fd
, "align = ");
719 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
722 print_tabs(tabs
, fd
);
723 fprintf(fd
, "if(*len == 0) {\n");
724 print_tabs(tabs
+1, fd
);
725 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
726 print_tabs(tabs
, fd
);
727 fprintf(fd
, "} else {\n");
728 print_tabs(tabs
+1, fd
);
729 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
730 print_tabs(tabs
, fd
);
734 print_tabs(tabs
, fd
);
735 fprintf(fd
, "*len += ");
736 fprintf(fd
, "sizeof(");
737 if(print_type(td
, fd
, 0, basename
, "")) return 1;
742 print_tabs(tabs
, fd
);
744 "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n",
745 custom
, basename
, obj_prefix
, field_name
);
748 print_tabs(tabs
, fd
);
750 "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
751 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
754 print_tabs(tabs
, fd
);
756 "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
757 custom
, basename
, get_ptr_char
, obj_prefix
, field_name
);
760 print_tabs(tabs
, fd
);
762 "lttng_write%s_union_%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_array_%s(buffer, to_base, to, from, len, %s%s);",
769 custom
, basename
, obj_prefix
, field_name
);
772 printf("Error : type NONE unexpected\n");
780 /* print need local vars ?.
782 * Copied from print_type_write
784 * Does the type_write call needs local size and from variables ?
785 * return value : 1 yes, 0 no.
788 int has_type_local(type_descriptor_t
* td
)
817 printf("Error : type NONE unexpected\n");
827 /* print type alignment function.
829 * Copied from construct_types_and_fields in LTTV facility.c
831 * basename is the name which identifies the type (along with a prefix
834 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
836 char *nest_name
, char *field_name
)
838 char basename
[PATH_MAX
];
839 unsigned int basename_len
= 0;
841 if(td
->custom_write
) return 0; /* Does print custom type */
843 strncpy(basename
, nest_name
, PATH_MAX
);
844 basename_len
= strlen(basename
);
846 /* For a named type, we use the type_name directly */
847 if(td
->type_name
!= NULL
) {
848 strncpy(basename
, td
->type_name
, PATH_MAX
);
849 basename_len
= strlen(basename
);
851 /* For a unnamed type, there must be a field name, except for
853 if((basename_len
!= 0)
854 && (basename
[basename_len
-1] != '_'
855 && (field_name
[0] != '\0'))) {
856 strncat(basename
, "_", PATH_MAX
- basename_len
);
857 basename_len
= strlen(basename
);
859 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
864 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
865 /* Not a named nested type : we must print its align fct */
866 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
867 0, basename
, "")) return 1;
869 /* Function header */
870 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
873 if(print_type(td
, fd
, 0, basename
, "")) return 1;
874 fprintf(fd
, " *obj)\n");
877 fprintf(fd
, "size_t align=0, localign;");
880 fprintf(fd
, "localign = ");
881 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
882 fd
, 0, basename
, "len", "obj->")) return 1;
885 fprintf(fd
, "align = max(align, localign);\n");
888 fprintf(fd
, "localign = ");
889 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
890 fd
, 0, basename
, "array[0]", "obj->")) return 1;
893 fprintf(fd
, "align = max(align, localign);\n");
896 fprintf(fd
, "return align;\n");
899 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
900 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
901 type_descriptor_t
*type
= field
->type
;
902 if(type
->type_name
== NULL
) {
903 /* Not a named nested type : we must print its align fct */
904 if(print_type_alignment_fct(type
, fd
,
905 0, basename
, field
->name
)) return 1;
908 /* Function header */
909 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
912 if(print_type(td
, fd
, 0, basename
, "")) return 1;
913 fprintf(fd
, " *obj)\n");
916 fprintf(fd
, "size_t align=0, localign;");
918 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
919 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
920 type_descriptor_t
*type
= field
->type
;
922 fprintf(fd
, "localign = ");
923 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
927 fprintf(fd
, "align = max(align, localign);\n");
931 fprintf(fd
, "return align;\n");
935 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
936 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
937 type_descriptor_t
*type
= field
->type
;
938 if(type
->type_name
== NULL
) {
939 /* Not a named nested type : we must print its align fct */
940 if(print_type_alignment_fct(type
, fd
,
941 0, basename
, field
->name
)) return 1;
944 /* Function header */
945 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
948 if(print_type(td
, fd
, 0, basename
, "")) return 1;
949 fprintf(fd
, " *obj)\n");
952 fprintf(fd
, "size_t align=0, localign;");
954 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
955 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
956 type_descriptor_t
*type
= field
->type
;
958 fprintf(fd
, "localign = ");
959 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
963 fprintf(fd
, "align = max(align, localign);\n");
967 fprintf(fd
, "return align;\n");
971 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
972 /* Not a named nested type : we must print its align fct */
973 if(print_type_alignment_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
974 0, basename
, "")) return 1;
976 /* Function header */
977 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
980 if(print_type(td
, fd
, 0, basename
, "")) return 1;
981 fprintf(fd
, " obj)\n");
984 fprintf(fd
, "return \n");
985 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
986 fd
, 0, basename
, "", "obj[0]"))
991 dprintf("print_type_alignment_fct : type has no alignment function.\n");
997 /* Function footer */
1004 /* print type write function.
1006 * Copied from construct_types_and_fields in LTTV facility.c
1008 * basename is the name which identifies the type (along with a prefix
1011 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
1012 char *nest_name
, char *field_name
)
1014 char basename
[PATH_MAX
];
1015 unsigned int basename_len
= 0;
1017 if(td
->custom_write
) return 0; /* Does print custom type */
1019 strncpy(basename
, nest_name
, PATH_MAX
);
1020 basename_len
= strlen(basename
);
1022 /* For a named type, we use the type_name directly */
1023 if(td
->type_name
!= NULL
) {
1024 strncpy(basename
, td
->type_name
, PATH_MAX
);
1025 basename_len
= strlen(basename
);
1027 /* For a unnamed type, there must be a field name, except for
1029 if((basename_len
!= 0)
1030 && (basename
[basename_len
-1] != '_'
1031 && (field_name
[0] != '\0'))) {
1032 strncat(basename
, "_", PATH_MAX
- basename_len
);
1033 basename_len
= strlen(basename
);
1035 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
1040 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
1041 /* Not a named nested type : we must print its write fct */
1042 if(print_type_write_fct(((field_t
*)td
->fields
.array
[1])->type
, fd
,
1043 0, basename
, "")) return 1;
1047 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1048 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1049 type_descriptor_t
*type
= field
->type
;
1050 if(type
->type_name
== NULL
) {
1051 /* Not a named nested type : we must print its write fct */
1052 if(print_type_write_fct(type
, fd
,
1053 0, basename
, field
->name
)) return 1;
1058 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1059 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1060 type_descriptor_t
*type
= field
->type
;
1061 if(type
->type_name
== NULL
) {
1062 /* Not a named nested type : we must print its write fct */
1063 if(print_type_write_fct(type
, fd
,
1064 0, basename
, field
->name
)) return 1;
1069 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
1070 /* Not a named nested type : we must print its write fct */
1071 if(print_type_write_fct(((field_t
*)td
->fields
.array
[0])->type
, fd
,
1072 0, basename
, "")) return 1;
1078 dprintf("print_type_write_fct : type has no write function.\n");
1086 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
1090 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
1093 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
1096 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
1099 fprintf(fd
, "static inline void lttng_write_string_%s(\n", basename
);
1102 printf("print_type_write_fct : type has no write function.\n");
1107 fprintf(fd
, "void *buffer,\n");
1109 fprintf(fd
, "size_t *to_base,\n");
1111 fprintf(fd
, "size_t *to,\n");
1113 fprintf(fd
, "const void **from,\n");
1115 fprintf(fd
, "size_t *len,\n");
1117 if(print_type(td
, fd
, 0, basename
, "")) return 1;
1121 fprintf(fd
, " *obj)\n");
1124 fprintf(fd
, " *obj)\n");
1127 fprintf(fd
, " *obj)\n");
1130 fprintf(fd
, " obj)\n");
1133 fprintf(fd
, " obj)\n");
1136 printf("print_type_write_fct : type has no write function.\n");
1145 fprintf(fd
, "size_t size;\n");
1152 fprintf(fd
, "size_t align;\n");
1159 fprintf(fd
, "/* Flush pending memcpy */\n");
1161 fprintf(fd
, "if(*len != 0) {\n");
1163 fprintf(fd
, "if(buffer != NULL)\n");
1165 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1169 fprintf(fd
, "*to += *len;\n");
1171 fprintf(fd
, "*len = 0;\n");
1179 printf("print_type_write_fct : type has no write function.\n");
1184 fprintf(fd
, "align = ");
1185 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
1189 fprintf(fd
, "if(*len == 0) {\n");
1191 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
1193 fprintf(fd
, "} else {\n");
1195 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1200 /* First, check if the type has a fixed size. If it is the case, then the size
1201 * to write is know by the compiler : simply use a sizeof() */
1202 if(has_type_fixed_size(td
)) {
1204 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1207 fprintf(fd
, "*len += sizeof(");
1208 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
1209 fprintf(fd
, ");\n");
1211 /* The type contains nested variable size subtypes :
1212 * we must write field by field. */
1214 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1220 fprintf(fd
, "/* Copy members */\n");
1221 // print_tabs(1, fd);
1222 // fprintf(fd, "size = sizeof(\n");
1223 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1224 fd
, 1, basename
, "len", "obj->", 1)) return 1;
1226 // fprintf(fd, ");\n");
1227 // print_tabs(1, fd);
1228 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1230 fprintf(fd
, "if(buffer != NULL)\n");
1232 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1234 fprintf(fd
, "*to += *len;\n");
1236 fprintf(fd
, "*len = 0;\n");
1239 /* Write the child : varlen child or not ? */
1240 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1241 /* Fixed size len child : use a multiplication of its size */
1242 // print_tabs(1, fd);
1243 // fprintf(fd, "size = sizeof(\n");
1245 //print_tabs(1, fd);
1246 /* We know that *len does not contain alignment because of the
1247 * previous align output. len is always 0 here. */
1248 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1249 fd
, 1, basename
, "array[0]", "obj->", 1))
1251 // fprintf(fd, ");\n");
1254 fprintf(fd
, "*len = obj->len * (*len);\n");
1256 fprintf(fd
, "if(buffer != NULL)\n");
1258 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1260 fprintf(fd
, "*to += *len;\n");
1262 fprintf(fd
, "*len = 0;\n");
1266 fprintf(fd
, "/* Variable length child : iter. */\n");
1268 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1269 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1270 fd
, 2, basename
, "array[i]", "obj->", 1)) return 1;
1276 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1278 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1280 fprintf(fd
, "*to_base = *to_base+*to;\n");
1282 fprintf(fd
, "*to = 0;\n");
1285 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1287 fprintf(fd
, "*from = obj+1;\n");
1291 fprintf(fd
, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1293 fprintf(fd
, "if(buffer != NULL)\n");
1295 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1297 fprintf(fd
, "*to += size;\n");
1300 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1302 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1304 fprintf(fd
, "*to_base = *to_base+*to;\n");
1306 fprintf(fd
, "*to = 0;\n");
1309 fprintf(fd
, "/* Put source *from just after the C string */\n");
1311 fprintf(fd
, "*from += size;\n");
1314 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1315 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1316 type_descriptor_t
*type
= field
->type
;
1317 if(print_type_write(type
,
1318 fd
, 1, basename
, field
->name
, "obj->", 1)) return 1;
1323 printf("ERROR : A union CANNOT contain a variable size child.\n");
1327 /* Write the child : varlen child or not ? */
1328 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1329 /* Error : if an array has a variable size, then its child must also
1330 * have a variable size. */
1334 fprintf(fd
, "/* Variable length child : iter. */\n");
1336 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1337 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1338 fd
, 2, basename
, "", "obj->array[i]", 1)) return 1;
1344 printf("print_type_write_fct : type has no write function.\n");
1352 /* Function footer */
1360 /* Print the logging function of an event. This is the core of genevent */
1361 int print_event_logging_function(char *basename
, facility_t
*fac
,
1362 event_t
*event
, FILE *fd
)
1364 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1365 int has_argument
= 0;
1366 int has_type_fixed
= 0;
1368 /* Does it support per trace tracing ? */
1369 if(event
->per_trace
) {
1371 fprintf(fd
, "struct ltt_trace_struct *dest_trace");
1375 /* Does it support per tracefile tracing ? */
1376 if(event
->per_tracefile
) {
1381 fprintf(fd
, "unsigned int tracefile_index");
1385 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1386 /* For each field, print the function argument */
1387 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1388 type_descriptor_t
*t
= f
->type
;
1393 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1398 fprintf(fd
, "void");
1402 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1406 fprintf(fd
,"#else\n");
1408 /* Print the function variables */
1410 fprintf(fd
, "unsigned int index;\n");
1412 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1414 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1416 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1418 fprintf(fd
, "void *buffer = NULL;\n");
1420 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1422 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1424 fprintf(fd
, "size_t real_to = 0;\n");
1426 fprintf(fd
, "size_t *to = &real_to;\n");
1428 fprintf(fd
, "size_t real_len = 0;\n");
1430 fprintf(fd
, "size_t *len = &real_len;\n");
1432 fprintf(fd
, "size_t reserve_size;\n");
1434 fprintf(fd
, "size_t slot_size;\n");
1437 if(event
->fields
.position
> 0) {
1438 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1439 /* Search for at least one child with fixed size. It means
1440 * we need local variables.*/
1441 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1442 type_descriptor_t
*type
= field
->type
;
1443 has_type_fixed
= has_type_local(type
);
1444 if(has_type_fixed
) break;
1447 if(has_type_fixed
) {
1448 fprintf(fd
, "size_t align;\n");
1452 fprintf(fd
, "const void *real_from;\n");
1454 fprintf(fd
, "const void **from = &real_from;\n");
1457 fprintf(fd
, "u64 tsc;\n");
1459 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1463 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1466 /* Calculate event variable len + event data alignment offset.
1467 * Assume that the padding for alignment starts at a void*
1469 * This excludes the header size and alignment. */
1472 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1474 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1476 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1478 fprintf(fd
, " * sizeof(void *) address. */\n");
1481 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1482 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1483 type_descriptor_t
*type
= field
->type
;
1486 switch(type
->type
) {
1492 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1495 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1499 if(print_type_write(type
,
1500 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1504 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1506 /* Take locks : make sure the trace does not vanish while we write on
1507 * it. A simple preemption disabling is enough (using rcu traces). */
1509 fprintf(fd
, "preempt_disable();\n");
1511 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1513 /* Get facility index */
1515 if(event
->per_tracefile
) {
1517 fprintf(fd
, "index = tracefile_index;\n");
1521 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1522 "\t\t\t\t\t\tevent_%s_%s);\n",
1523 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
1528 /* For each trace */
1530 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1532 fprintf(fd
, "if(!trace->active) continue;\n\n");
1534 if(event
->per_trace
) {
1536 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1540 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1542 fprintf(fd
, "relayfs_buf = channel->rchan->buf[smp_processor_id()];\n");
1547 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1550 fprintf(fd
, "slot_size = 0;\n");
1552 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1554 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1556 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1557 /* If error, return */
1559 fprintf(fd
, "if(!buffer) continue; /* buffer full */\n\n");
1560 //print_tabs(2, fd);
1562 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1564 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1567 /* Write event header */
1569 fprintf(fd
, "ltt_write_event_header(trace, channel, buffer,\n");
1571 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1572 fac
->name
, event
->name
);
1574 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
1576 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1581 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1582 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1583 type_descriptor_t
*type
= field
->type
;
1587 switch(type
->type
) {
1593 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1596 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1601 if(print_type_write(type
,
1602 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1605 /* Don't forget to flush pending memcpy */
1607 fprintf(fd
, "/* Flush pending memcpy */\n");
1609 fprintf(fd
, "if(*len != 0) {\n");
1611 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1613 fprintf(fd
, "*to += *len;\n");
1614 //print_tabs(3, fd);
1615 //fprintf(fd, "from += len;\n");
1617 fprintf(fd
, "*len = 0;\n");
1626 //fprintf(fd, "commit:\n"); /* DEBUG! */
1628 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1631 fprintf(fd
, "}\n\n");
1635 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1637 fprintf(fd
, "preempt_enable_no_resched();\n");
1640 fprintf(fd
, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1645 /* print_event_logging_function_user_generic
1646 * Print the logging function of an event for userspace tracing. This is the
1647 * core of genevent */
1648 int print_event_logging_function_user_generic(char *basename
, facility_t
*fac
,
1649 event_t
*event
, FILE *fd
)
1653 fprintf(fd
, "#ifndef LTT_TRACE_FAST\n");
1655 if(event
->no_instrument_function
) {
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;
1666 int has_type_fixed
= 0;
1668 if(event
->param_buffer
) {
1674 fprintf(fd
, "void *buffer");
1679 fprintf(fd
, "size_t reserve_size");
1681 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1682 /* For each field, print the function argument */
1683 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1684 type_descriptor_t
*t
= f
->type
;
1689 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1695 fprintf(fd
, "void");
1699 "#ifndef LTT_TRACE\n");
1702 fprintf(fd
,"#else\n");
1704 /* Print the function variables */
1706 fprintf(fd
, "int ret = 0;\n");
1707 if(event
->param_buffer
) {
1709 fprintf(fd
, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n");
1715 fprintf(fd
, "void *buffer = NULL;\n");
1717 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1719 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1721 fprintf(fd
, "size_t real_to = 0;\n");
1723 fprintf(fd
, "size_t *to = &real_to;\n");
1725 fprintf(fd
, "size_t real_len = 0;\n");
1727 fprintf(fd
, "size_t *len = &real_len;\n");
1729 fprintf(fd
, "size_t reserve_size;\n");
1731 fprintf(fd
, "size_t slot_size;\n");
1734 if(event
->fields
.position
> 0) {
1735 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1736 /* Search for at least one child with fixed size. It means
1737 * we need local variables.*/
1738 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1739 type_descriptor_t
*type
= field
->type
;
1740 has_type_fixed
= has_type_local(type
);
1741 if(has_type_fixed
) break;
1744 if(has_type_fixed
) {
1745 fprintf(fd
, "size_t align;\n");
1749 fprintf(fd
, "const void *real_from;\n");
1751 fprintf(fd
, "const void **from = &real_from;\n");
1755 /* Calculate event variable len + event data alignment offset.
1756 * Assume that the padding for alignment starts at a void*
1758 * This excludes the header size and alignment. */
1761 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1763 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1765 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1767 fprintf(fd
, " * sizeof(void *) address. */\n");
1770 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1771 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1772 type_descriptor_t
*type
= field
->type
;
1775 switch(type
->type
) {
1781 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1784 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1788 if(print_type_write(type
,
1789 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1793 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1798 fprintf(fd
, "char stack_buffer[reserve_size];\n");
1800 fprintf(fd
, "buffer = stack_buffer;\n");
1804 //print_tabs(2, fd);
1806 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1808 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1813 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1814 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1815 type_descriptor_t
*type
= field
->type
;
1819 switch(type
->type
) {
1825 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1828 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1833 if(print_type_write(type
,
1834 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1837 /* Don't forget to flush pending memcpy */
1839 fprintf(fd
, "/* Flush pending memcpy */\n");
1841 fprintf(fd
, "if(*len != 0) {\n");
1843 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1845 fprintf(fd
, "*to += *len;\n");
1846 //print_tabs(3, fd);
1847 //fprintf(fd, "from += len;\n");
1849 fprintf(fd
, "*len = 0;\n");
1857 fprintf(fd
, "ret = ltt_trace_generic(ltt_facility_%s_%X, event_%s_%s, buffer, reserve_size, LTT_BLOCKING);\n", fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
1860 fprintf(fd
, "}\n\n");
1863 fprintf(fd
, "return ret;\n\n");
1867 "#endif //LTT_TRACE\n");
1868 fprintf(fd
, "#endif //!LTT_TRACE_FAST\n\n");
1873 /* print_event_logging_function_user_fast
1874 * Print the logging function of an event for userspace tracing. This is the
1875 * core of genevent */
1876 int print_event_logging_function_user_fast(char *basename
, facility_t
*fac
,
1877 event_t
*event
, FILE *fd
)
1881 fprintf(fd
, "#ifdef LTT_TRACE_FAST\n");
1883 if(event
->no_instrument_function
) {
1884 attrib
= "__attribute__((no_instrument_function)) ";
1888 fprintf(fd
, "static inline %sint trace_%s(\n",attrib
, basename
);
1890 int has_argument
= 0;
1891 int has_type_fixed
= 0;
1893 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1894 /* For each field, print the function argument */
1895 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1896 type_descriptor_t
*t
= f
->type
;
1901 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1906 fprintf(fd
, "void");
1910 "#ifndef LTT_TRACE\n");
1913 fprintf(fd
,"#else\n");
1915 /* Print the function variables */
1917 fprintf(fd
, "unsigned int index;\n");
1919 fprintf(fd
, "struct ltt_trace_info *trace = thread_trace_info;\n");
1921 fprintf(fd
, "struct ltt_buf *ltt_buf;\n");
1923 fprintf(fd
, "void *buffer = NULL;\n");
1925 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1927 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1929 fprintf(fd
, "size_t real_to = 0;\n");
1931 fprintf(fd
, "size_t *to = &real_to;\n");
1933 fprintf(fd
, "size_t real_len = 0;\n");
1935 fprintf(fd
, "size_t *len = &real_len;\n");
1937 fprintf(fd
, "size_t reserve_size;\n");
1939 fprintf(fd
, "size_t slot_size;\n");
1942 if(event
->fields
.position
> 0) {
1943 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1944 /* Search for at least one child with fixed size. It means
1945 * we need local variables.*/
1946 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1947 type_descriptor_t
*type
= field
->type
;
1948 has_type_fixed
= has_type_local(type
);
1949 if(has_type_fixed
) break;
1952 if(has_type_fixed
) {
1953 fprintf(fd
, "size_t align;\n");
1957 fprintf(fd
, "const void *real_from;\n");
1959 fprintf(fd
, "const void **from = &real_from;\n");
1962 fprintf(fd
, "uint64_t tsc;\n");
1964 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1968 fprintf(fd
, "if(!trace) {\n");
1970 fprintf(fd
, "ltt_thread_init();\n");
1972 fprintf(fd
, "trace = thread_trace_info;\n");
1974 fprintf(fd
, "}\n\n");
1977 /* Calculate event variable len + event data alignment offset.
1978 * Assume that the padding for alignment starts at a void*
1980 * This excludes the header size and alignment. */
1983 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1985 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1987 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1989 fprintf(fd
, " * sizeof(void *) address. */\n");
1992 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1993 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1994 type_descriptor_t
*type
= field
->type
;
1997 switch(type
->type
) {
2003 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
2006 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
2010 if(print_type_write(type
,
2011 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
2015 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
2018 fprintf(fd
, "trace->nesting++;\n");
2020 /* Get facility index */
2024 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
2025 "\t\t\t\t\t\tevent_%s_%s);\n",
2026 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
2033 if(event
->per_trace
) {
2035 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
2039 fprintf(fd
, "ltt_buf = ltt_get_channel_from_index(trace, index);\n");
2044 /* If error, increment event lost counter (done by ltt_reserve_slot) and
2047 fprintf(fd
, "slot_size = 0;\n");
2049 fprintf(fd
, "buffer = ltt_reserve_slot(trace, ltt_buf,\n");
2051 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
2053 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
2054 /* If error, return */
2056 fprintf(fd
, "if(!buffer) goto end; /* buffer full */\n\n");
2057 //print_tabs(2, fd);
2059 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
2061 fprintf(fd
, "*to_base = *to = *len = 0;\n");
2064 /* Write event header */
2066 fprintf(fd
, "ltt_write_event_header(trace, ltt_buf, buffer,\n");
2068 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
2069 fac
->name
, event
->name
);
2071 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
2073 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
2078 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
2079 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
2080 type_descriptor_t
*type
= field
->type
;
2084 switch(type
->type
) {
2090 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
2093 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
2098 if(print_type_write(type
,
2099 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
2102 /* Don't forget to flush pending memcpy */
2104 fprintf(fd
, "/* Flush pending memcpy */\n");
2106 fprintf(fd
, "if(*len != 0) {\n");
2108 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
2110 fprintf(fd
, "*to += *len;\n");
2111 //print_tabs(3, fd);
2112 //fprintf(fd, "from += len;\n");
2114 fprintf(fd
, "*len = 0;\n");
2123 //fprintf(fd, "commit:\n"); /* DEBUG! */
2125 fprintf(fd
, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n");
2127 fprintf(fd
, "}\n\n");
2129 fprintf(fd
, "end:\n");
2132 fprintf(fd
, "trace->nesting--;\n");
2137 "#endif //LTT_TRACE\n");
2138 fprintf(fd
, "#endif //LTT_TRACE_FAST\n");
2148 /* ltt-facility-name.h : main logging header.
2151 void print_log_header_head(facility_t
*fac
, FILE *fd
)
2153 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2154 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2155 fprintf(fd
, "#include <linux/types.h>\n");
2157 fprintf(fd
, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2159 fprintf(fd
, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2162 fprintf(fd
, "#include <linux/ltt-core.h>\n");
2166 /* ltt-facility-name.h : main logging header.
2169 void print_log_header_head_user(facility_t
*fac
, FILE *fd
)
2171 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
2172 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
2173 fprintf(fd
, "#include <sys/types.h>\n");
2175 fprintf(fd
, "#include <ltt/ltt-facility-id-%s.h>\n", fac
->name
);
2177 fprintf(fd
, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2180 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2185 int print_log_header_types(facility_t
*fac
, FILE *fd
)
2187 sequence_t
*types
= &fac
->named_types
.values
;
2188 fprintf(fd
, "/* Named types */\n");
2191 for(unsigned int i
= 0; i
< types
->position
; i
++) {
2192 /* For each named type, print the definition */
2193 if(print_type_declaration(types
->array
[i
], fd
,
2194 0, "", "")) return 1;
2195 /* Print also the align function */
2196 if(print_type_alignment_fct(types
->array
[i
], fd
,
2197 0, "", "")) return 1;
2198 /* Print also the write function */
2199 if(print_type_write_fct(types
->array
[i
], fd
,
2200 0, "", "")) return 1;
2205 int print_log_header_events(facility_t
*fac
, FILE *fd
)
2207 sequence_t
*events
= &fac
->events
;
2208 char basename
[PATH_MAX
];
2209 unsigned int facname_len
;
2211 strncpy(basename
, fac
->name
, PATH_MAX
);
2212 facname_len
= strlen(basename
);
2213 strncat(basename
, "_", PATH_MAX
-facname_len
);
2214 facname_len
= strlen(basename
);
2216 for(unsigned int i
= 0; i
< events
->position
; i
++) {
2217 event_t
*event
= (event_t
*)events
->array
[i
];
2218 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
2220 /* For each event, print structure, and then logging function */
2221 fprintf(fd
, "/* Event %s structures */\n",
2223 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
2224 /* For each unnamed type, print the definition */
2225 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
2226 type_descriptor_t
*t
= f
->type
;
2227 if(t
->type_name
== NULL
) {
2228 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
2229 /* Print also the align function */
2230 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2231 /* Print also the write function */
2232 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
2238 fprintf(fd
, "/* Event %s logging function */\n",
2242 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
2244 if(print_event_logging_function_user_generic(basename
, fac
, event
, fd
))
2246 if(print_event_logging_function_user_fast(basename
, fac
, event
, fd
))
2257 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
2259 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2262 void print_log_header_tail_user(facility_t
*fac
, FILE *fd
)
2264 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
2267 int print_log_header(facility_t
*fac
)
2269 char filename
[PATH_MAX
];
2270 unsigned int filename_size
= 0;
2272 dprintf("%s\n", fac
->name
);
2274 strcpy(filename
, "ltt-facility-");
2275 filename_size
= strlen(filename
);
2277 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2278 filename_size
= strlen(filename
);
2281 strncat(filename
, "_", PATH_MAX
- filename_size
);
2282 filename_size
= strlen(filename
);
2284 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2285 filename_size
= strlen(filename
);
2288 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2289 filename_size
= strlen(filename
);
2292 fd
= fopen(filename
, "w");
2294 printf("Error opening file %s for writing : %s\n",
2295 filename
, strerror(errno
));
2299 /* Print file head */
2301 print_log_header_head(fac
, fd
);
2303 print_log_header_head_user(fac
, fd
);
2305 /* print named types in declaration order */
2306 if(print_log_header_types(fac
, fd
)) return 1;
2309 if(print_log_header_events(fac
, fd
)) return 1;
2311 /* Print file tail */
2313 print_log_header_tail(fac
, fd
);
2315 print_log_header_tail_user(fac
, fd
);
2325 /* ltt-facility-id-name.h : facility id.
2327 int print_id_header(facility_t
*fac
)
2329 char filename
[PATH_MAX
];
2330 unsigned int filename_size
= 0;
2332 char basename
[PATH_MAX
];
2333 char basename_len
= 0;
2335 dprintf("%s\n", fac
->name
);
2337 strcpy(filename
, "ltt-facility-id-");
2338 filename_size
= strlen(filename
);
2340 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2341 filename_size
= strlen(filename
);
2344 strncat(filename
, "_", PATH_MAX
- filename_size
);
2345 filename_size
= strlen(filename
);
2347 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2348 filename_size
= strlen(filename
);
2351 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2352 filename_size
= strlen(filename
);
2355 fd
= fopen(filename
, "w");
2357 printf("Error opening file %s for writing : %s\n",
2358 filename
, strerror(errno
));
2363 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2364 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2365 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2367 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
2369 fprintf(fd
,"/**** facility handle ****/\n\n");
2370 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2371 fac
->name
, fac
->checksum
);
2372 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2374 strncpy(basename
, fac
->name
, PATH_MAX
);
2375 basename_len
= strlen(basename
);
2376 strncat(basename
, "_", PATH_MAX
- basename_len
);
2379 fprintf(fd
,"/**** event index ****/\n\n");
2380 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2382 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2383 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2384 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2386 fprintf(fd
, "event_%s,\n", basename
);
2389 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2390 fprintf(fd
, "};\n");
2394 fprintf(fd
, "#endif //CONFIG_LTT\n");
2395 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2397 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2398 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
2399 fprintf(fd
, "#ifdef LTT_TRACE\n");
2401 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n\n");
2403 fprintf(fd
,"/**** facility handle ****/\n\n");
2404 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2405 fac
->name
, fac
->checksum
);
2406 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
2408 strncpy(basename
, fac
->name
, PATH_MAX
);
2409 basename_len
= strlen(basename
);
2410 strncat(basename
, "_", PATH_MAX
- basename_len
);
2413 fprintf(fd
,"/**** event index ****/\n\n");
2414 fprintf(fd
,"enum %s_event {\n",fac
->name
);
2416 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
2417 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
2418 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
2420 fprintf(fd
, "event_%s,\n", basename
);
2423 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
2424 fprintf(fd
, "};\n");
2428 fprintf(fd
, "#endif //LTT_TRACE\n");
2429 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
2439 /* ltt-facility-loader-name.h : facility specific loader info.
2441 int print_loader_header(facility_t
*fac
)
2443 char filename
[PATH_MAX
];
2444 unsigned int filename_size
= 0;
2446 dprintf("%s\n", fac
->name
);
2448 strcpy(filename
, "ltt-facility-loader-");
2449 filename_size
= strlen(filename
);
2451 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2452 filename_size
= strlen(filename
);
2455 strncat(filename
, "_", PATH_MAX
- filename_size
);
2456 filename_size
= strlen(filename
);
2458 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2459 filename_size
= strlen(filename
);
2462 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2463 filename_size
= strlen(filename
);
2466 fd
= fopen(filename
, "w");
2468 printf("Error opening file %s for writing : %s\n",
2469 filename
, strerror(errno
));
2473 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2474 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2475 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
2476 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
2478 fprintf(fd
,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
2481 fprintf(fd
,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
2484 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2485 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2486 fac
->name
, fac
->checksum
);
2488 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\tltt_facility_%s\n",
2490 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\tltt_facility_%s_%X\n",
2491 fac
->name
, fac
->checksum
);
2492 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t0x%X\n", fac
->checksum
);
2493 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\"%s\"\n", fac
->name
);
2494 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\tfacility_%s_num_events\n\n",
2496 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
2497 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2504 int print_loader_header_user(facility_t
*fac
)
2506 char filename
[PATH_MAX
];
2507 unsigned int filename_size
= 0;
2509 dprintf("%s\n", fac
->name
);
2511 strcpy(filename
, "ltt-facility-loader-");
2512 filename_size
= strlen(filename
);
2514 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2515 filename_size
= strlen(filename
);
2518 strncat(filename
, "_", PATH_MAX
- filename_size
);
2519 filename_size
= strlen(filename
);
2521 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2522 filename_size
= strlen(filename
);
2525 strncat(filename
, ".h", PATH_MAX
- filename_size
);
2526 filename_size
= strlen(filename
);
2529 fd
= fopen(filename
, "w");
2531 printf("Error opening file %s for writing : %s\n",
2532 filename
, strerror(errno
));
2536 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2537 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
2538 fprintf(fd
,"#include <ltt/ltt-usertrace.h>\n");
2540 fprintf(fd
,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2543 fprintf(fd
,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
2546 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
2547 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2548 fac
->name
, fac
->checksum
);
2550 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
2552 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
2553 fac
->name
, fac
->checksum
);
2554 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
2555 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
2556 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
2558 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
2567 /* ltt-facility-loader-name.c : generic facility loader
2569 int print_loader_c(facility_t
*fac
)
2571 char filename
[PATH_MAX
];
2572 unsigned int filename_size
= 0;
2574 dprintf("%s\n", fac
->name
);
2576 strcpy(filename
, "ltt-facility-loader-");
2577 filename_size
= strlen(filename
);
2579 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2580 filename_size
= strlen(filename
);
2583 strncat(filename
, "_", PATH_MAX
- filename_size
);
2584 filename_size
= strlen(filename
);
2586 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2587 filename_size
= strlen(filename
);
2590 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2591 filename_size
= strlen(filename
);
2594 fd
= fopen(filename
, "w");
2596 printf("Error opening file %s for writing : %s\n",
2597 filename
, strerror(errno
));
2601 fprintf(fd
, "/*\n");
2603 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2605 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2606 fprintf(fd
, " *\n");
2607 fprintf(fd
, " * (C) Copyright 2005 - \n");
2608 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2609 fprintf(fd
, " *\n");
2610 fprintf(fd
, " * Contains the LTT facility loader.\n");
2611 fprintf(fd
, " *\n");
2612 fprintf(fd
, " */\n");
2615 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
2616 fprintf(fd
, "#include <linux/module.h>\n");
2617 fprintf(fd
, "#include <linux/init.h>\n");
2618 fprintf(fd
, "#include <linux/config.h>\n");
2620 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2622 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2623 fac
->name
, fac
->arch
);
2626 fprintf(fd
, "#ifdef CONFIG_LTT\n");
2628 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
2629 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
2631 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
2633 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
2635 fprintf(fd
, "static struct ltt_facility facility = {\n");
2636 fprintf(fd
, "\t.name = ltt_facility_name,\n");
2637 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2638 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2639 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
2640 fprintf(fd
, "};\n");
2642 fprintf(fd
, "static int __init facility_init(void)\n");
2644 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
2646 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n");
2647 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2648 fprintf(fd
, "\t\n");
2649 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
2652 fprintf(fd
, "#ifndef MODULE\n");
2653 fprintf(fd
, "__initcall(facility_init);\n");
2654 fprintf(fd
, "#else\n");
2655 fprintf(fd
, "module_init(facility_init);\n");
2656 fprintf(fd
, "static void __exit facility_exit(void)\n");
2658 fprintf(fd
, "\tint err;\n");
2660 fprintf(fd
, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n");
2661 fprintf(fd
, "\tif(err != 0)\n");
2662 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
2665 fprintf(fd
, "module_exit(facility_exit)\n");
2667 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
2668 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
2669 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
2671 fprintf(fd
, "#endif //MODULE\n");
2673 fprintf(fd
, "#endif //CONFIG_LTT\n");
2680 int print_loader_c_user(facility_t
*fac
)
2682 char filename
[PATH_MAX
];
2683 unsigned int filename_size
= 0;
2685 dprintf("%s\n", fac
->name
);
2687 strcpy(filename
, "ltt-facility-loader-");
2688 filename_size
= strlen(filename
);
2690 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
2691 filename_size
= strlen(filename
);
2694 strncat(filename
, "_", PATH_MAX
- filename_size
);
2695 filename_size
= strlen(filename
);
2697 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
2698 filename_size
= strlen(filename
);
2701 strncat(filename
, ".c", PATH_MAX
- filename_size
);
2702 filename_size
= strlen(filename
);
2705 fd
= fopen(filename
, "w");
2707 printf("Error opening file %s for writing : %s\n",
2708 filename
, strerror(errno
));
2712 fprintf(fd
, "/*\n");
2714 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
2716 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
2717 fprintf(fd
, " *\n");
2718 fprintf(fd
, " * (C) Copyright 2005 - \n");
2719 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2720 fprintf(fd
, " *\n");
2721 fprintf(fd
, " * Contains the LTT user space facility loader.\n");
2722 fprintf(fd
, " *\n");
2723 fprintf(fd
, " */\n");
2726 fprintf(fd
, "#define LTT_TRACE\n");
2727 fprintf(fd
, "#include <error.h>\n");
2728 fprintf(fd
, "#include <stdio.h>\n");
2729 fprintf(fd
, "#include <ltt/ltt-usertrace.h>\n");
2731 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
2733 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2734 fac
->name
, fac
->arch
);
2736 fprintf(fd
, "static struct user_facility_info facility = {\n");
2737 fprintf(fd
, "\t.name = LTT_FACILITY_NAME,\n");
2738 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2739 fprintf(fd
, "#ifndef LTT_PACK\n");
2740 fprintf(fd
, "\t.alignment = sizeof(void*),\n");
2741 fprintf(fd
, "#else\n");
2742 fprintf(fd
, "\t.alignment = 0,\n");
2743 fprintf(fd
, "#endif //LTT_PACK\n");
2744 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2745 fprintf(fd
, "\t.int_size = sizeof(int),\n");
2746 fprintf(fd
, "\t.long_size = sizeof(long),\n");
2747 fprintf(fd
, "\t.pointer_size = sizeof(void*),\n");
2748 fprintf(fd
, "\t.size_t_size = sizeof(size_t)\n");
2749 fprintf(fd
, "};\n");
2751 fprintf(fd
, "static void __attribute__((constructor)) __ltt_user_init(void)\n");
2753 fprintf(fd
, "\tint err;\n");
2754 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2755 fprintf(fd
, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac
->name
);
2756 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2758 fprintf(fd
, "\terr = ltt_register_generic(<T_FACILITY_SYMBOL, &facility);\n");
2759 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2760 fprintf(fd
, "\t\n");
2761 fprintf(fd
, "\tif(err) {\n");
2762 fprintf(fd
, "#ifdef LTT_SHOW_DEBUG\n");
2763 fprintf(fd
, "\t\tperror(\"Error in ltt_register_generic\");\n");
2764 fprintf(fd
, "#endif //LTT_SHOW_DEBUG\n");
2765 fprintf(fd
, "\t}\n");
2777 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
2779 /*****************************************************************************
2781 * ltt_facility_open : open facilities
2783 * pathname : the path name of the facility
2785 * Open the facility corresponding to the right checksum.
2787 *returns the facility on success, NULL on error.
2788 ****************************************************************************/
2789 facility_t
*ltt_facility_open(char * pathname
)
2794 facility_t
* fac
= NULL
;
2795 char buffer
[BUFFER_SIZE
];
2796 int generated
= FALSE
;
2798 in
.buffer
= &(buffer
[0]);
2800 in
.error
= error_callback
;
2804 in
.fp
= fopen(in
.name
, "r");
2811 token
= getToken(&in
);
2812 if(in
.type
== ENDFILE
) break;
2815 printf("More than one facility in the file. Only using the first one.\n");
2819 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2820 token
= getName(&in
);
2821 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2822 token
= getName(&in
);
2823 if(strcmp(token
, "xml")) in
.error(&in
,"not a facility file");
2824 token
= getName(&in
);
2825 if(strcmp(token
, "version")) in
.error(&in
,"not a facility file");
2826 token
= getName(&in
);
2827 if(strcmp(token
, "=")) in
.error(&in
,"not a facility file");
2828 token
= getQuotedString(&in
);
2829 if(strcmp(token
, "1.0")) in
.error(&in
,"not a facility file");
2830 token
= getName(&in
);
2831 if(strcmp(token
, "?")) in
.error(&in
,"not a facility file");
2832 token
= getToken(&in
);
2833 if(strcmp(token
, ">")) in
.error(&in
,"not a facility file");
2835 token
= getName(&in
);
2836 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2837 token
= getName(&in
);
2838 if(strcmp("facility",token
) == 0) {
2839 fac
= malloc(sizeof(facility_t
));
2841 fac
->description
= NULL
;
2842 sequence_init(&(fac
->events
));
2843 table_init(&(fac
->named_types
));
2844 sequence_init(&(fac
->unnamed_types
));
2846 parseFacility(&in
, fac
);
2848 //check if any namedType is not defined
2849 checkNamedTypesImplemented(&fac
->named_types
);
2851 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2856 printf("facility token was expected in file %s\n", in
.name
);
2867 printf("Cannot find facility %s\n", pathname
);
2874 /* Close the facility */
2875 void ltt_facility_close(facility_t
*fac
)
2879 free(fac
->description
);
2880 freeEvents(&fac
->events
);
2881 sequence_dispose(&fac
->events
);
2882 freeNamedType(&fac
->named_types
);
2883 table_dispose(&fac
->named_types
);
2884 freeTypes(&fac
->unnamed_types
);
2885 sequence_dispose(&fac
->unnamed_types
);
2891 void show_help(int argc
, char ** argv
)
2893 printf("Genevent help : \n");
2895 printf("Use %s name.xml\n", argv
[0]);
2896 printf("to create :\n");
2897 printf("ltt-facility-name.h\n");
2898 printf("ltt-facility-id-name.h\n");
2899 printf("ltt-facility-loader-name.h\n");
2900 printf("ltt-facility-loader-name.c\n");
2901 printf("In the current directory.\n");
2905 /* Parse program arguments */
2907 * 0 : continue program
2908 * -1 : stop program, return 0
2909 * > 0 : stop program, return value as exit.
2911 int check_args(int argc
, char **argv
)
2914 printf("Not enough arguments\n");
2915 show_help(argc
, argv
);
2919 if(strcmp(argv
[1], "-h") == 0) {
2920 show_help(argc
, argv
);
2927 int main(int argc
, char **argv
)
2932 err
= check_args(argc
, argv
);
2933 if(err
> 0) return err
;
2934 else if(err
< 0) return 0;
2936 /* open the facility */
2937 fac
= ltt_facility_open(argv
[1]);
2939 printf("Error opening file %s for reading : %s\n",
2940 argv
[1], strerror(errno
));
2944 /* generate the output C files */
2947 /* ltt-facility-name.h : main logging header.
2949 err
= print_log_header(fac
);
2952 /* ltt-facility-id-name.h : facility id.
2954 err
= print_id_header(fac
);
2957 /* ltt-facility-loader-name.h : facility specific loader info.
2960 err
= print_loader_header(fac
);
2962 err
= print_loader_header_user(fac
);
2965 /* ltt-facility-loader-name.c : generic faciilty loader
2968 err
= print_loader_c(fac
);
2970 err
= print_loader_c_user(fac
);
2973 /* close the facility */
2974 ltt_facility_close(fac
);
This page took 0.095173 seconds and 4 git commands to generate.