88cecb69c5d5c821ac971bafee618926c328cf78
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 strncpy(basename
, nest_name
, PATH_MAX
);
390 basename_len
= strlen(basename
);
392 /* For a named type, we use the type_name directly */
393 if(td
->type_name
!= NULL
) {
394 strncpy(basename
, td
->type_name
, PATH_MAX
);
395 basename_len
= strlen(basename
);
397 /* For a unnamed type, there must be a field name, except for
399 if((basename_len
!= 0)
400 && (basename
[basename_len
-1] != '_'
401 && (field_name
[0] != '\0'))) {
402 strncat(basename
, "_", PATH_MAX
- basename_len
);
403 basename_len
= strlen(basename
);
405 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
410 fprintf(fd
, "enum lttng_%s", basename
);
412 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
414 fprintf(fd
, "LTTNG_%s", ((char*)(td
->labels
.array
[i
])));
422 dprintf("%s\n", basename
);
423 assert(td
->size
>= 0);
424 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
425 /* Not a named nested type : we must print its declaration first */
426 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
427 fd
, 0, basename
, "")) return 1;
429 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename
,
431 fprintf(fd
, "typedef ");
432 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
433 fd
, tabs
, basename
, "")) return 1;
434 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
439 /* We assume that the sequence length type does not need to be declared.
441 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
442 /* Not a named nested type : we must print its declaration first */
443 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
444 fd
, 0, basename
, "")) return 1;
446 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
449 fprintf(fd
, "struct lttng_sequence_%s", basename
);
452 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
453 fd
, tabs
, basename
, "")) return 1;
454 fprintf(fd
, " len;\n");
456 fprintf(fd
, "const ");
457 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
458 fd
, tabs
, basename
, "")) return 1;
459 fprintf(fd
, " *array;\n");
460 fprintf(fd
, "};\n"); /* We do not LTT_ALIGN, because we never copy
461 it to the buffer directly. */
466 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
467 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
468 type_descriptor_t
*type
= field
->type
;
469 if(type
->type_name
== NULL
) {
470 /* Not a named nested type : we must print its declaration first */
471 if(print_type_declaration(type
,
472 fd
, 0, basename
, field
->name
)) return 1;
475 fprintf(fd
, "struct lttng_%s", basename
);
477 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
478 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
479 type_descriptor_t
*type
= field
->type
;
481 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
483 fprintf(fd
, "%s", field
->name
);
486 fprintf(fd
, "} LTT_ALIGN;\n");
490 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
491 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
492 type_descriptor_t
*type
= field
->type
;
493 if(type
->type_name
== NULL
) {
494 /* Not a named nested type : we must print its declaration first */
495 if(print_type_declaration(type
,
496 fd
, 0, basename
, field
->name
)) return 1;
499 fprintf(fd
, "union lttng_%s", basename
);
501 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
502 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
503 type_descriptor_t
*type
= field
->type
;
505 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
507 fprintf(fd
, "%s", field
->name
);
510 fprintf(fd
, "} LTT_ALIGN;\n");
514 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
522 /* print type alignment.
524 * Copied from construct_types_and_fields in LTTV facility.c
526 * basename is the name which identifies the type (along with a prefix
529 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
530 char *nest_name
, char *field_name
, char *obj_prefix
)
532 char basename
[PATH_MAX
];
533 unsigned int basename_len
= 0;
535 strncpy(basename
, nest_name
, PATH_MAX
);
536 basename_len
= strlen(basename
);
538 /* For a named type, we use the type_name directly */
539 if(td
->type_name
!= NULL
) {
540 strncpy(basename
, td
->type_name
, PATH_MAX
);
541 basename_len
= strlen(basename
);
543 /* For a unnamed type, there must be a field name, except for
545 if((basename_len
!= 0)
546 && (basename
[basename_len
-1] != '_'
547 && field_name
!= NULL
548 && (field_name
[0] != '\0'))) {
549 strncat(basename
, "_", PATH_MAX
- basename_len
);
550 basename_len
= strlen(basename
);
552 if(field_name
!= NULL
)
553 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
556 if(field_name
[0] == '\0') {
557 /* We are in a write function : it's the obj that we must align. */
560 fprintf(fd
, "lttng_get_alignment_sequence_%s(%s)", basename
,
564 fprintf(fd
, "lttng_get_alignment_struct_%s(%s)", basename
,
568 fprintf(fd
, "lttng_get_alignment_union_%s(%s)", basename
,
572 fprintf(fd
, "lttng_get_alignment_array_%s(%s)", basename
,
575 fprintf(fd
, "sizeof(char)");
593 fprintf(fd
, "sizeof(");
594 if(print_type(td
, fd
, 0, basename
, "")) return 1;
599 printf("error : type unexpected\n");
622 fprintf(fd
, "sizeof(");
623 if(print_type(td
, fd
, 0, basename
, "")) return 1;
627 fprintf(fd
, "sizeof(char)");
630 fprintf(fd
, "lttng_get_alignment_sequence_%s(&%s%s)", basename
,
631 obj_prefix
, field_name
);
634 fprintf(fd
, "lttng_get_alignment_struct_%s(&%s%s)", basename
,
635 obj_prefix
, field_name
);
638 fprintf(fd
, "lttng_get_alignment_union_%s(&%s%s)", basename
,
639 obj_prefix
, field_name
);
642 fprintf(fd
, "lttng_get_alignment_array_%s(%s%s)", basename
,
643 obj_prefix
, field_name
);
646 printf("error : type NONE unexpected\n");
657 * Copied from construct_types_and_fields in LTTV facility.c
659 * basename is the name which identifies the type (along with a prefix
662 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
663 char *nest_name
, char *field_name
, char *obj_prefix
, int get_ptr
)
665 char basename
[PATH_MAX
];
666 unsigned int basename_len
= 0;
667 char get_ptr_char
[2] = "";
669 strncpy(basename
, nest_name
, PATH_MAX
);
670 basename_len
= strlen(basename
);
672 /* For a named type, we use the type_name directly */
673 if(td
->type_name
!= NULL
) {
674 strncpy(basename
, td
->type_name
, PATH_MAX
);
675 basename_len
= strlen(basename
);
677 /* For a unnamed type, there must be a field name, except for
679 if((basename_len
!= 0)
680 && (basename
[basename_len
-1] != '_'
681 && (field_name
[0] != '\0'))) {
682 strncat(basename
, "_", PATH_MAX
- basename_len
);
683 basename_len
= strlen(basename
);
685 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
689 strcpy(get_ptr_char
, "&");
709 print_tabs(tabs
, fd
);
710 fprintf(fd
, "align = ");
711 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
714 print_tabs(tabs
, fd
);
715 fprintf(fd
, "if(*len == 0) {\n");
716 print_tabs(tabs
+1, fd
);
717 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
718 print_tabs(tabs
, fd
);
719 fprintf(fd
, "} else {\n");
720 print_tabs(tabs
+1, fd
);
721 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
722 print_tabs(tabs
, fd
);
726 print_tabs(tabs
, fd
);
727 fprintf(fd
, "*len += ");
728 fprintf(fd
, "sizeof(");
729 if(print_type(td
, fd
, 0, basename
, "")) return 1;
734 print_tabs(tabs
, fd
);
736 "lttng_write_string_%s(buffer, to_base, to, from, len, %s%s);\n",
737 basename
, obj_prefix
, field_name
);
740 print_tabs(tabs
, fd
);
742 "lttng_write_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
743 basename
, get_ptr_char
, obj_prefix
, field_name
);
746 print_tabs(tabs
, fd
);
748 "lttng_write_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
749 basename
, get_ptr_char
, obj_prefix
, field_name
);
752 print_tabs(tabs
, fd
);
754 "lttng_write_union_%s(buffer, to_base, to, from, len, %s%s%s);",
755 basename
, get_ptr_char
, obj_prefix
, field_name
);
758 print_tabs(tabs
, fd
);
760 "lttng_write_array_%s(buffer, to_base, to, from, len, %s%s);",
761 basename
, obj_prefix
, field_name
);
764 printf("Error : type NONE unexpected\n");
772 /* print need local vars ?.
774 * Copied from print_type_write
776 * Does the type_write call needs local size and from variables ?
777 * return value : 1 yes, 0 no.
780 int has_type_local(type_descriptor_t
* td
)
809 printf("Error : type NONE unexpected\n");
819 /* print type alignment function.
821 * Copied from construct_types_and_fields in LTTV facility.c
823 * basename is the name which identifies the type (along with a prefix
826 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
828 char *nest_name
, char *field_name
)
830 char basename
[PATH_MAX
];
831 unsigned int basename_len
= 0;
833 strncpy(basename
, nest_name
, PATH_MAX
);
834 basename_len
= strlen(basename
);
836 /* For a named type, we use the type_name directly */
837 if(td
->type_name
!= NULL
) {
838 strncpy(basename
, td
->type_name
, PATH_MAX
);
839 basename_len
= strlen(basename
);
841 /* For a unnamed type, there must be a field name, except for
843 if((basename_len
!= 0)
844 && (basename
[basename_len
-1] != '_'
845 && (field_name
[0] != '\0'))) {
846 strncat(basename
, "_", PATH_MAX
- basename_len
);
847 basename_len
= strlen(basename
);
849 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
854 /* Function header */
855 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
858 if(print_type(td
, fd
, 0, basename
, "")) return 1;
859 fprintf(fd
, " *obj)\n");
862 fprintf(fd
, "size_t align=0, localign;");
865 fprintf(fd
, "localign = ");
866 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
867 fd
, 0, basename
, "len", "obj->")) return 1;
870 fprintf(fd
, "align = max(align, localign);\n");
873 fprintf(fd
, "localign = ");
874 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
875 fd
, 0, basename
, "array[0]", "obj->")) return 1;
878 fprintf(fd
, "align = max(align, localign);\n");
881 fprintf(fd
, "return align;\n");
884 /* Function header */
885 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
888 if(print_type(td
, fd
, 0, basename
, "")) return 1;
889 fprintf(fd
, " *obj)\n");
892 fprintf(fd
, "size_t align=0, localign;");
894 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
895 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
896 type_descriptor_t
*type
= field
->type
;
898 fprintf(fd
, "localign = ");
899 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
903 fprintf(fd
, "align = max(align, localign);\n");
907 fprintf(fd
, "return align;\n");
911 /* Function header */
912 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%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 /* Function header */
939 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
942 if(print_type(td
, fd
, 0, basename
, "")) return 1;
943 fprintf(fd
, " obj)\n");
946 fprintf(fd
, "return \n");
947 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
948 fd
, 0, basename
, "", "obj[0]"))
953 dprintf("print_type_alignment_fct : type has no alignment function.\n");
959 /* Function footer */
966 /* print type write function.
968 * Copied from construct_types_and_fields in LTTV facility.c
970 * basename is the name which identifies the type (along with a prefix
973 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
974 char *nest_name
, char *field_name
)
976 char basename
[PATH_MAX
];
977 unsigned int basename_len
= 0;
979 strncpy(basename
, nest_name
, PATH_MAX
);
980 basename_len
= strlen(basename
);
982 /* For a named type, we use the type_name directly */
983 if(td
->type_name
!= NULL
) {
984 strncpy(basename
, td
->type_name
, PATH_MAX
);
985 basename_len
= strlen(basename
);
987 /* For a unnamed type, there must be a field name, except for
989 if((basename_len
!= 0)
990 && (basename
[basename_len
-1] != '_'
991 && (field_name
[0] != '\0'))) {
992 strncat(basename
, "_", PATH_MAX
- basename_len
);
993 basename_len
= strlen(basename
);
995 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
1006 dprintf("print_type_write_fct : type has no write function.\n");
1014 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
1018 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
1021 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
1024 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
1027 fprintf(fd
, "static inline void lttng_write_string_%s(\n", basename
);
1030 printf("print_type_write_fct : type has no write function.\n");
1035 fprintf(fd
, "void *buffer,\n");
1037 fprintf(fd
, "size_t *to_base,\n");
1039 fprintf(fd
, "size_t *to,\n");
1041 fprintf(fd
, "const void **from,\n");
1043 fprintf(fd
, "size_t *len,\n");
1045 if(print_type(td
, fd
, 0, basename
, "")) return 1;
1049 fprintf(fd
, " *obj)\n");
1052 fprintf(fd
, " *obj)\n");
1055 fprintf(fd
, " *obj)\n");
1058 fprintf(fd
, " obj)\n");
1061 fprintf(fd
, " obj)\n");
1064 printf("print_type_write_fct : type has no write function.\n");
1071 fprintf(fd
, "size_t size;\n");
1073 fprintf(fd
, "size_t align;\n");
1080 fprintf(fd
, "/* Flush pending memcpy */\n");
1082 fprintf(fd
, "if(*len != 0) {\n");
1084 fprintf(fd
, "if(buffer != NULL)\n");
1086 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1090 fprintf(fd
, "*to += *len;\n");
1092 fprintf(fd
, "*len = 0;\n");
1100 printf("print_type_write_fct : type has no write function.\n");
1105 fprintf(fd
, "align = ");
1106 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
1110 fprintf(fd
, "if(*len == 0) {\n");
1112 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
1114 fprintf(fd
, "} else {\n");
1116 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1121 /* First, check if the type has a fixed size. If it is the case, then the size
1122 * to write is know by the compiler : simply use a sizeof() */
1123 if(has_type_fixed_size(td
)) {
1125 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1128 fprintf(fd
, "size = sizeof(");
1129 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
1130 fprintf(fd
, ");\n");
1132 fprintf(fd
, "*len += size;\n");
1134 /* The type contains nested variable size subtypes :
1135 * we must write field by field. */
1137 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1143 fprintf(fd
, "/* Copy members */\n");
1144 // print_tabs(1, fd);
1145 // fprintf(fd, "size = sizeof(\n");
1146 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1147 fd
, 1, basename
, "len", "obj->", 1)) return 1;
1149 // fprintf(fd, ");\n");
1150 // print_tabs(1, fd);
1151 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1153 fprintf(fd
, "if(buffer != NULL)\n");
1155 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1157 fprintf(fd
, "*to += *len;\n");
1159 fprintf(fd
, "*len = 0;\n");
1162 /* Write the child : varlen child or not ? */
1163 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1164 /* Fixed size len child : use a multiplication of its size */
1165 // print_tabs(1, fd);
1166 // fprintf(fd, "size = sizeof(\n");
1168 //print_tabs(1, fd);
1169 /* We know that *len does not contain alignment because of the
1170 * previous align output. len is always 0 here. */
1171 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1172 fd
, 1, basename
, "array[0]", "obj->", 1))
1174 // fprintf(fd, ");\n");
1177 fprintf(fd
, "*len = obj->len * (*len);\n");
1179 fprintf(fd
, "if(buffer != NULL)\n");
1181 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1183 fprintf(fd
, "*to += *len;\n");
1185 fprintf(fd
, "*len = 0;\n");
1189 fprintf(fd
, "/* Variable length child : iter. */\n");
1191 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1192 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1193 fd
, 2, basename
, "array[i]", "obj->", 1)) return 1;
1199 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1201 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1203 fprintf(fd
, "*to_base = *to_base+*to;\n");
1205 fprintf(fd
, "*to = 0;\n");
1208 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1210 fprintf(fd
, "*from = obj+1;\n");
1214 fprintf(fd
, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1216 fprintf(fd
, "if(buffer != NULL)\n");
1218 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1220 fprintf(fd
, "*to += size;\n");
1223 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1225 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1227 fprintf(fd
, "*to_base = *to_base+*to;\n");
1229 fprintf(fd
, "*to = 0;\n");
1232 fprintf(fd
, "/* Put source *from just after the C string */\n");
1234 fprintf(fd
, "*from += size;\n");
1237 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1238 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1239 type_descriptor_t
*type
= field
->type
;
1240 if(print_type_write(type
,
1241 fd
, 1, basename
, field
->name
, "obj->", 1)) return 1;
1246 printf("ERROR : A union CANNOT contain a variable size child.\n");
1250 /* Write the child : varlen child or not ? */
1251 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1252 /* Error : if an array has a variable size, then its child must also
1253 * have a variable size. */
1257 fprintf(fd
, "/* Variable length child : iter. */\n");
1259 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1260 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1261 fd
, 2, basename
, "", "obj->array[i]", 1)) return 1;
1267 printf("print_type_write_fct : type has no write function.\n");
1275 /* Function footer */
1283 /* Print the logging function of an event. This is the core of genevent */
1284 int print_event_logging_function(char *basename
, facility_t
*fac
,
1285 event_t
*event
, FILE *fd
)
1287 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1288 int has_argument
= 0;
1289 int has_type_fixed
= 0;
1291 /* Does it support per trace tracing ? */
1292 if(event
->per_trace
) {
1294 fprintf(fd
, "struct ltt_trace_struct *dest_trace");
1298 /* Does it support per tracefile tracing ? */
1299 if(event
->per_tracefile
) {
1304 fprintf(fd
, "unsigned int tracefile_index");
1308 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1309 /* For each field, print the function argument */
1310 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1311 type_descriptor_t
*t
= f
->type
;
1316 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1321 fprintf(fd
, "void");
1325 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1329 fprintf(fd
,"#else\n");
1331 /* Print the function variables */
1333 fprintf(fd
, "unsigned int index;\n");
1335 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1337 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1339 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1341 fprintf(fd
, "void *buffer = NULL;\n");
1343 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1345 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1347 fprintf(fd
, "size_t real_to = 0;\n");
1349 fprintf(fd
, "size_t *to = &real_to;\n");
1351 fprintf(fd
, "size_t real_len = 0;\n");
1353 fprintf(fd
, "size_t *len = &real_len;\n");
1355 fprintf(fd
, "size_t reserve_size;\n");
1357 fprintf(fd
, "size_t slot_size;\n");
1360 if(event
->fields
.position
> 0) {
1361 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1362 /* Search for at least one child with fixed size. It means
1363 * we need local variables.*/
1364 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1365 type_descriptor_t
*type
= field
->type
;
1366 has_type_fixed
= has_type_local(type
);
1367 if(has_type_fixed
) break;
1370 if(has_type_fixed
) {
1371 fprintf(fd
, "size_t align;\n");
1375 fprintf(fd
, "const void *real_from;\n");
1377 fprintf(fd
, "const void **from = &real_from;\n");
1380 fprintf(fd
, "cycles_t tsc;\n");
1382 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1386 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1389 /* Calculate event variable len + event data alignment offset.
1390 * Assume that the padding for alignment starts at a void*
1392 * This excludes the header size and alignment. */
1395 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1397 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1399 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1401 fprintf(fd
, " * sizeof(void *) address. */\n");
1404 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1405 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1406 type_descriptor_t
*type
= field
->type
;
1409 switch(type
->type
) {
1415 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1418 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1422 if(print_type_write(type
,
1423 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1427 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1429 /* Take locks : make sure the trace does not vanish while we write on
1430 * it. A simple preemption disabling is enough (using rcu traces). */
1432 fprintf(fd
, "preempt_disable();\n");
1434 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1436 /* Get facility index */
1438 if(event
->per_tracefile
) {
1440 fprintf(fd
, "index = tracefile_index;\n");
1444 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1445 "\t\t\t\t\t\tevent_%s_%s);\n",
1446 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
1451 /* For each trace */
1453 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1455 fprintf(fd
, "if(!trace->active) continue;\n\n");
1457 if(event
->per_trace
) {
1459 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1463 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1465 fprintf(fd
, "relayfs_buf = channel->rchan->buf[smp_processor_id()];\n");
1470 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1473 fprintf(fd
, "slot_size = 0;\n");
1475 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1477 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1479 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1480 /* If error, return */
1482 fprintf(fd
, "if(!buffer) continue; /* buffer full */\n\n");
1483 //print_tabs(2, fd);
1485 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1487 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1490 /* Write event header */
1492 fprintf(fd
, "ltt_write_event_header(trace, channel, buffer,\n");
1494 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1495 fac
->name
, event
->name
);
1497 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
1499 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1504 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1505 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1506 type_descriptor_t
*type
= field
->type
;
1510 switch(type
->type
) {
1516 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1519 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1524 if(print_type_write(type
,
1525 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1528 /* Don't forget to flush pending memcpy */
1530 fprintf(fd
, "/* Flush pending memcpy */\n");
1532 fprintf(fd
, "if(*len != 0) {\n");
1534 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1536 fprintf(fd
, "*to += *len;\n");
1537 //print_tabs(3, fd);
1538 //fprintf(fd, "from += len;\n");
1540 fprintf(fd
, "*len = 0;\n");
1549 //fprintf(fd, "commit:\n"); /* DEBUG! */
1551 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1554 fprintf(fd
, "}\n\n");
1558 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1560 fprintf(fd
, "preempt_enable_no_resched();\n");
1563 fprintf(fd
, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1569 /* ltt-facility-name.h : main logging header.
1572 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1574 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1575 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1576 fprintf(fd
, "#include <linux/types.h>\n");
1577 fprintf(fd
, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac
->name
);
1578 fprintf(fd
, "#include <linux/ltt-core.h>\n");
1584 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1586 sequence_t
*types
= &fac
->named_types
.values
;
1587 fprintf(fd
, "/* Named types */\n");
1590 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1591 /* For each named type, print the definition */
1592 if(print_type_declaration(types
->array
[i
], fd
,
1593 0, "", "")) return 1;
1594 /* Print also the align function */
1595 if(print_type_alignment_fct(types
->array
[i
], fd
,
1596 0, "", "")) return 1;
1597 /* Print also the write function */
1598 if(print_type_write_fct(types
->array
[i
], fd
,
1599 0, "", "")) return 1;
1604 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1606 sequence_t
*events
= &fac
->events
;
1607 char basename
[PATH_MAX
];
1608 unsigned int facname_len
;
1610 strncpy(basename
, fac
->name
, PATH_MAX
);
1611 facname_len
= strlen(basename
);
1612 strncat(basename
, "_", PATH_MAX
-facname_len
);
1613 facname_len
= strlen(basename
);
1615 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1616 event_t
*event
= (event_t
*)events
->array
[i
];
1617 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1619 /* For each event, print structure, and then logging function */
1620 fprintf(fd
, "/* Event %s structures */\n",
1622 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1623 /* For each unnamed type, print the definition */
1624 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1625 type_descriptor_t
*t
= f
->type
;
1626 if(t
->type_name
== NULL
) {
1627 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1628 /* Print also the align function */
1629 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1630 /* Print also the write function */
1631 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1637 fprintf(fd
, "/* Event %s logging function */\n",
1640 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1649 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1651 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1654 int print_log_header(facility_t
*fac
)
1656 char filename
[PATH_MAX
];
1657 unsigned int filename_size
= 0;
1659 dprintf("%s\n", fac
->name
);
1661 strcpy(filename
, "ltt-facility-");
1662 filename_size
= strlen(filename
);
1664 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1665 filename_size
= strlen(filename
);
1667 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1668 filename_size
= strlen(filename
);
1671 fd
= fopen(filename
, "w");
1673 printf("Error opening file %s for writing : %s\n",
1674 filename
, strerror(errno
));
1678 /* Print file head */
1679 print_log_header_head(fac
, fd
);
1681 /* print named types in declaration order */
1682 if(print_log_header_types(fac
, fd
)) return 1;
1685 if(print_log_header_events(fac
, fd
)) return 1;
1687 /* Print file tail */
1688 print_log_header_tail(fac
, fd
);
1697 /* ltt-facility-id-name.h : facility id.
1699 int print_id_header(facility_t
*fac
)
1701 char filename
[PATH_MAX
];
1702 unsigned int filename_size
= 0;
1704 char basename
[PATH_MAX
];
1705 char basename_len
= 0;
1707 dprintf("%s\n", fac
->name
);
1709 strcpy(filename
, "ltt-facility-id-");
1710 filename_size
= strlen(filename
);
1712 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1713 filename_size
= strlen(filename
);
1715 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1716 filename_size
= strlen(filename
);
1719 fd
= fopen(filename
, "w");
1721 printf("Error opening file %s for writing : %s\n",
1722 filename
, strerror(errno
));
1726 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1727 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
1728 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1730 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
1732 fprintf(fd
,"/**** facility handle ****/\n\n");
1733 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
1734 fac
->name
, fac
->checksum
);
1735 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
1737 strncpy(basename
, fac
->name
, PATH_MAX
);
1738 basename_len
= strlen(basename
);
1739 strncat(basename
, "_", PATH_MAX
- basename_len
);
1742 fprintf(fd
,"/**** event index ****/\n\n");
1743 fprintf(fd
,"enum %s_event {\n",fac
->name
);
1745 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
1746 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
1747 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
1749 fprintf(fd
, "event_%s,\n", basename
);
1752 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
1753 fprintf(fd
, "};\n");
1757 fprintf(fd
, "#endif //CONFIG_LTT\n");
1758 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1767 /* ltt-facility-loader-name.h : facility specific loader info.
1769 int print_loader_header(facility_t
*fac
)
1771 char filename
[PATH_MAX
];
1772 unsigned int filename_size
= 0;
1774 dprintf("%s\n", fac
->name
);
1776 strcpy(filename
, "ltt-facility-loader-");
1777 filename_size
= strlen(filename
);
1779 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1780 filename_size
= strlen(filename
);
1782 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1783 filename_size
= strlen(filename
);
1786 fd
= fopen(filename
, "w");
1788 printf("Error opening file %s for writing : %s\n",
1789 filename
, strerror(errno
));
1793 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1794 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
1795 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
1796 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
1797 fprintf(fd
,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
1799 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
1800 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
1801 fac
->name
, fac
->checksum
);
1803 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
1805 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
1806 fac
->name
, fac
->checksum
);
1807 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
1808 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
1809 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
1811 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1812 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1819 /* ltt-facility-loader-name.c : generic faciilty loader
1821 int print_loader_c(facility_t
*fac
)
1823 char filename
[PATH_MAX
];
1824 unsigned int filename_size
= 0;
1826 dprintf("%s\n", fac
->name
);
1828 strcpy(filename
, "ltt-facility-loader-");
1829 filename_size
= strlen(filename
);
1831 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1832 filename_size
= strlen(filename
);
1834 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1835 filename_size
= strlen(filename
);
1838 fd
= fopen(filename
, "w");
1840 printf("Error opening file %s for writing : %s\n",
1841 filename
, strerror(errno
));
1845 fprintf(fd
, "/*\n");
1846 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
1847 fprintf(fd
, " *\n");
1848 fprintf(fd
, " * (C) Copyright 2005 - \n");
1849 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
1850 fprintf(fd
, " *\n");
1851 fprintf(fd
, " * Contains the LTT facility loader.\n");
1852 fprintf(fd
, " *\n");
1853 fprintf(fd
, " */\n");
1856 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
1857 fprintf(fd
, "#include <linux/module.h>\n");
1858 fprintf(fd
, "#include <linux/init.h>\n");
1859 fprintf(fd
, "#include <linux/config.h>\n");
1860 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
1863 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1865 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
1866 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
1868 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
1870 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
1872 fprintf(fd
, "static struct ltt_facility facility = {\n");
1873 fprintf(fd
, "\t.name = ltt_facility_name,\n");
1874 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
1875 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
1876 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
1877 fprintf(fd
, "};\n");
1879 fprintf(fd
, "#ifndef MODULE\n");
1881 fprintf(fd
, "/* Built-in facility. */\n");
1883 fprintf(fd
, "static int __init facility_init(void)\n");
1885 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
1887 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
1888 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1889 fprintf(fd
, "\t\n");
1890 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1892 fprintf(fd
, "__initcall(facility_init);\n");
1896 fprintf(fd
, "#else \n");
1898 fprintf(fd
, "/* Dynamic facility. */\n");
1900 fprintf(fd
, "static int __init facility_init(void)\n");
1902 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac
->name
);
1904 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
1905 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1907 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1910 fprintf(fd
, "static void __exit facility_exit(void)\n");
1912 fprintf(fd
, "\tint err;\n");
1914 fprintf(fd
, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
1915 fprintf(fd
, "\tif(err != 0)\n");
1916 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
1920 fprintf(fd
, "module_init(facility_init)\n");
1921 fprintf(fd
, "module_exit(facility_exit)\n");
1924 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
1925 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
1926 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
1928 fprintf(fd
, "#endif //MODULE\n");
1930 fprintf(fd
, "#endif //CONFIG_LTT\n");
1940 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1942 /*****************************************************************************
1944 * ltt_facility_open : open facilities
1946 * pathname : the path name of the facility
1948 * Open the facility corresponding to the right checksum.
1950 *returns the facility on success, NULL on error.
1951 ****************************************************************************/
1952 facility_t
*ltt_facility_open(char * pathname
)
1957 facility_t
* fac
= NULL
;
1958 char buffer
[BUFFER_SIZE
];
1959 int generated
= FALSE
;
1961 in
.buffer
= &(buffer
[0]);
1963 in
.error
= error_callback
;
1967 in
.fp
= fopen(in
.name
, "r");
1974 token
= getToken(&in
);
1975 if(in
.type
== ENDFILE
) break;
1978 printf("More than one facility in the file. Only using the first one.\n");
1982 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1983 token
= getName(&in
);
1985 if(strcmp("facility",token
) == 0) {
1986 fac
= malloc(sizeof(facility_t
));
1988 fac
->description
= NULL
;
1989 sequence_init(&(fac
->events
));
1990 table_init(&(fac
->named_types
));
1991 sequence_init(&(fac
->unnamed_types
));
1993 parseFacility(&in
, fac
);
1995 //check if any namedType is not defined
1996 checkNamedTypesImplemented(&fac
->named_types
);
1998 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2003 printf("facility token was expected in file %s\n", in
.name
);
2014 printf("Cannot find facility %s\n", pathname
);
2021 /* Close the facility */
2022 void ltt_facility_close(facility_t
*fac
)
2026 free(fac
->description
);
2027 freeEvents(&fac
->events
);
2028 sequence_dispose(&fac
->events
);
2029 freeNamedType(&fac
->named_types
);
2030 table_dispose(&fac
->named_types
);
2031 freeTypes(&fac
->unnamed_types
);
2032 sequence_dispose(&fac
->unnamed_types
);
2038 void show_help(int argc
, char ** argv
)
2040 printf("Genevent help : \n");
2042 printf("Use %s name.xml\n", argv
[0]);
2043 printf("to create :\n");
2044 printf("ltt-facility-name.h\n");
2045 printf("ltt-facility-id-name.h\n");
2046 printf("ltt-facility-loader-name.h\n");
2047 printf("ltt-facility-loader-name.c\n");
2048 printf("In the current directory.\n");
2052 /* Parse program arguments */
2054 * 0 : continue program
2055 * -1 : stop program, return 0
2056 * > 0 : stop program, return value as exit.
2058 int check_args(int argc
, char **argv
)
2061 printf("Not enough arguments\n");
2062 show_help(argc
, argv
);
2066 if(strcmp(argv
[1], "-h") == 0) {
2067 show_help(argc
, argv
);
2074 int main(int argc
, char **argv
)
2079 err
= check_args(argc
, argv
);
2080 if(err
> 0) return err
;
2081 else if(err
< 0) return 0;
2083 /* open the facility */
2084 fac
= ltt_facility_open(argv
[1]);
2086 printf("Error opening file %s for reading : %s\n",
2087 argv
[1], strerror(errno
));
2091 /* generate the output C files */
2094 /* ltt-facility-name.h : main logging header.
2096 err
= print_log_header(fac
);
2099 /* ltt-facility-id-name.h : facility id.
2101 err
= print_id_header(fac
);
2104 /* ltt-facility-loader-name.h : facility specific loader info.
2106 err
= print_loader_header(fac
);
2109 /* ltt-facility-loader-name.c : generic faciilty loader
2111 err
= print_loader_c(fac
);
2114 /* close the facility */
2115 ltt_facility_close(fac
);
This page took 0.142548 seconds and 4 git commands to generate.