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 = %d", ((char*)td
->labels
.array
[i
]),
415 (*(int*)td
->labels_values
.array
[i
]));
423 dprintf("%s\n", basename
);
424 assert(td
->size
>= 0);
425 if(((field_t
*)td
->fields
.array
[0])->type
->type_name
== NULL
) {
426 /* Not a named nested type : we must print its declaration first */
427 if(print_type_declaration(((field_t
*)td
->fields
.array
[0])->type
,
428 fd
, 0, basename
, "")) return 1;
430 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename
,
432 fprintf(fd
, "typedef ");
433 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
434 fd
, tabs
, basename
, "")) return 1;
435 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
440 /* We assume that the sequence length type does not need to be declared.
442 if(((field_t
*)td
->fields
.array
[1])->type
->type_name
== NULL
) {
443 /* Not a named nested type : we must print its declaration first */
444 if(print_type_declaration(((field_t
*)td
->fields
.array
[1])->type
,
445 fd
, 0, basename
, "")) return 1;
447 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
450 fprintf(fd
, "struct lttng_sequence_%s", basename
);
453 if(print_type(((field_t
*)td
->fields
.array
[0])->type
,
454 fd
, tabs
, basename
, "")) return 1;
455 fprintf(fd
, " len;\n");
457 fprintf(fd
, "const ");
458 if(print_type(((field_t
*)td
->fields
.array
[1])->type
,
459 fd
, tabs
, basename
, "")) return 1;
460 fprintf(fd
, " *array;\n");
461 fprintf(fd
, "};\n"); /* We do not LTT_ALIGN, because we never copy
462 it to the buffer directly. */
467 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
468 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
469 type_descriptor_t
*type
= field
->type
;
470 if(type
->type_name
== NULL
) {
471 /* Not a named nested type : we must print its declaration first */
472 if(print_type_declaration(type
,
473 fd
, 0, basename
, field
->name
)) return 1;
476 fprintf(fd
, "struct lttng_%s", basename
);
478 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
479 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
480 type_descriptor_t
*type
= field
->type
;
482 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
484 fprintf(fd
, "%s", field
->name
);
487 fprintf(fd
, "} LTT_ALIGN;\n");
491 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
492 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
493 type_descriptor_t
*type
= field
->type
;
494 if(type
->type_name
== NULL
) {
495 /* Not a named nested type : we must print its declaration first */
496 if(print_type_declaration(type
,
497 fd
, 0, basename
, field
->name
)) return 1;
500 fprintf(fd
, "union lttng_%s", basename
);
502 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
503 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
504 type_descriptor_t
*type
= field
->type
;
506 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
508 fprintf(fd
, "%s", field
->name
);
511 fprintf(fd
, "} LTT_ALIGN;\n");
515 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
523 /* print type alignment.
525 * Copied from construct_types_and_fields in LTTV facility.c
527 * basename is the name which identifies the type (along with a prefix
530 int print_type_alignment(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
531 char *nest_name
, char *field_name
, char *obj_prefix
)
533 char basename
[PATH_MAX
];
534 unsigned int basename_len
= 0;
536 strncpy(basename
, nest_name
, PATH_MAX
);
537 basename_len
= strlen(basename
);
539 /* For a named type, we use the type_name directly */
540 if(td
->type_name
!= NULL
) {
541 strncpy(basename
, td
->type_name
, PATH_MAX
);
542 basename_len
= strlen(basename
);
544 /* For a unnamed type, there must be a field name, except for
546 if((basename_len
!= 0)
547 && (basename
[basename_len
-1] != '_'
548 && field_name
!= NULL
549 && (field_name
[0] != '\0'))) {
550 strncat(basename
, "_", PATH_MAX
- basename_len
);
551 basename_len
= strlen(basename
);
553 if(field_name
!= NULL
)
554 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
557 if(field_name
[0] == '\0') {
558 /* We are in a write function : it's the obj that we must align. */
561 fprintf(fd
, "lttng_get_alignment_sequence_%s(%s)", basename
,
565 fprintf(fd
, "lttng_get_alignment_struct_%s(%s)", basename
,
569 fprintf(fd
, "lttng_get_alignment_union_%s(%s)", basename
,
573 fprintf(fd
, "lttng_get_alignment_array_%s(%s)", basename
,
576 fprintf(fd
, "sizeof(char)");
594 fprintf(fd
, "sizeof(");
595 if(print_type(td
, fd
, 0, basename
, "")) return 1;
600 printf("error : type unexpected\n");
623 fprintf(fd
, "sizeof(");
624 if(print_type(td
, fd
, 0, basename
, "")) return 1;
628 fprintf(fd
, "sizeof(char)");
631 fprintf(fd
, "lttng_get_alignment_sequence_%s(&%s%s)", basename
,
632 obj_prefix
, field_name
);
635 fprintf(fd
, "lttng_get_alignment_struct_%s(&%s%s)", basename
,
636 obj_prefix
, field_name
);
639 fprintf(fd
, "lttng_get_alignment_union_%s(&%s%s)", basename
,
640 obj_prefix
, field_name
);
643 fprintf(fd
, "lttng_get_alignment_array_%s(%s%s)", basename
,
644 obj_prefix
, field_name
);
647 printf("error : type NONE unexpected\n");
658 * Copied from construct_types_and_fields in LTTV facility.c
660 * basename is the name which identifies the type (along with a prefix
663 int print_type_write(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
664 char *nest_name
, char *field_name
, char *obj_prefix
, int get_ptr
)
666 char basename
[PATH_MAX
];
667 unsigned int basename_len
= 0;
668 char get_ptr_char
[2] = "";
670 strncpy(basename
, nest_name
, PATH_MAX
);
671 basename_len
= strlen(basename
);
673 /* For a named type, we use the type_name directly */
674 if(td
->type_name
!= NULL
) {
675 strncpy(basename
, td
->type_name
, PATH_MAX
);
676 basename_len
= strlen(basename
);
678 /* For a unnamed type, there must be a field name, except for
680 if((basename_len
!= 0)
681 && (basename
[basename_len
-1] != '_'
682 && (field_name
[0] != '\0'))) {
683 strncat(basename
, "_", PATH_MAX
- basename_len
);
684 basename_len
= strlen(basename
);
686 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
690 strcpy(get_ptr_char
, "&");
710 print_tabs(tabs
, fd
);
711 fprintf(fd
, "align = ");
712 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
715 print_tabs(tabs
, fd
);
716 fprintf(fd
, "if(*len == 0) {\n");
717 print_tabs(tabs
+1, fd
);
718 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
719 print_tabs(tabs
, fd
);
720 fprintf(fd
, "} else {\n");
721 print_tabs(tabs
+1, fd
);
722 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
723 print_tabs(tabs
, fd
);
727 print_tabs(tabs
, fd
);
728 fprintf(fd
, "*len += ");
729 fprintf(fd
, "sizeof(");
730 if(print_type(td
, fd
, 0, basename
, "")) return 1;
735 print_tabs(tabs
, fd
);
737 "lttng_write_string_%s(buffer, to_base, to, from, len, %s%s);\n",
738 basename
, obj_prefix
, field_name
);
741 print_tabs(tabs
, fd
);
743 "lttng_write_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
744 basename
, get_ptr_char
, obj_prefix
, field_name
);
747 print_tabs(tabs
, fd
);
749 "lttng_write_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
750 basename
, get_ptr_char
, obj_prefix
, field_name
);
753 print_tabs(tabs
, fd
);
755 "lttng_write_union_%s(buffer, to_base, to, from, len, %s%s%s);",
756 basename
, get_ptr_char
, obj_prefix
, field_name
);
759 print_tabs(tabs
, fd
);
761 "lttng_write_array_%s(buffer, to_base, to, from, len, %s%s);",
762 basename
, obj_prefix
, field_name
);
765 printf("Error : type NONE unexpected\n");
773 /* print need local vars ?.
775 * Copied from print_type_write
777 * Does the type_write call needs local size and from variables ?
778 * return value : 1 yes, 0 no.
781 int has_type_local(type_descriptor_t
* td
)
810 printf("Error : type NONE unexpected\n");
820 /* print type alignment function.
822 * Copied from construct_types_and_fields in LTTV facility.c
824 * basename is the name which identifies the type (along with a prefix
827 int print_type_alignment_fct(type_descriptor_t
* td
, FILE *fd
,
829 char *nest_name
, char *field_name
)
831 char basename
[PATH_MAX
];
832 unsigned int basename_len
= 0;
834 strncpy(basename
, nest_name
, PATH_MAX
);
835 basename_len
= strlen(basename
);
837 /* For a named type, we use the type_name directly */
838 if(td
->type_name
!= NULL
) {
839 strncpy(basename
, td
->type_name
, PATH_MAX
);
840 basename_len
= strlen(basename
);
842 /* For a unnamed type, there must be a field name, except for
844 if((basename_len
!= 0)
845 && (basename
[basename_len
-1] != '_'
846 && (field_name
[0] != '\0'))) {
847 strncat(basename
, "_", PATH_MAX
- basename_len
);
848 basename_len
= strlen(basename
);
850 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
855 /* Function header */
856 fprintf(fd
, "static inline size_t lttng_get_alignment_sequence_%s(\n",
859 if(print_type(td
, fd
, 0, basename
, "")) return 1;
860 fprintf(fd
, " *obj)\n");
863 fprintf(fd
, "size_t align=0, localign;");
866 fprintf(fd
, "localign = ");
867 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
868 fd
, 0, basename
, "len", "obj->")) return 1;
871 fprintf(fd
, "align = max(align, localign);\n");
874 fprintf(fd
, "localign = ");
875 if(print_type_alignment(((field_t
*)td
->fields
.array
[1])->type
,
876 fd
, 0, basename
, "array[0]", "obj->")) return 1;
879 fprintf(fd
, "align = max(align, localign);\n");
882 fprintf(fd
, "return align;\n");
885 /* Function header */
886 fprintf(fd
, "static inline size_t lttng_get_alignment_struct_%s(\n",
889 if(print_type(td
, fd
, 0, basename
, "")) return 1;
890 fprintf(fd
, " *obj)\n");
893 fprintf(fd
, "size_t align=0, localign;");
895 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
896 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
897 type_descriptor_t
*type
= field
->type
;
899 fprintf(fd
, "localign = ");
900 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
904 fprintf(fd
, "align = max(align, localign);\n");
908 fprintf(fd
, "return align;\n");
912 /* Function header */
913 fprintf(fd
, "static inline size_t lttng_get_alignment_union_%s(\n",
916 if(print_type(td
, fd
, 0, basename
, "")) return 1;
917 fprintf(fd
, " *obj)\n");
920 fprintf(fd
, "size_t align=0, localign;");
922 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
923 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
924 type_descriptor_t
*type
= field
->type
;
926 fprintf(fd
, "localign = ");
927 if(print_type_alignment(type
, fd
, 0, basename
, field
->name
, "obj->"))
931 fprintf(fd
, "align = max(align, localign);\n");
935 fprintf(fd
, "return align;\n");
939 /* Function header */
940 fprintf(fd
, "static inline size_t lttng_get_alignment_array_%s(\n",
943 if(print_type(td
, fd
, 0, basename
, "")) return 1;
944 fprintf(fd
, " obj)\n");
947 fprintf(fd
, "return \n");
948 if(print_type_alignment(((field_t
*)td
->fields
.array
[0])->type
,
949 fd
, 0, basename
, "", "obj[0]"))
954 dprintf("print_type_alignment_fct : type has no alignment function.\n");
960 /* Function footer */
967 /* print type write function.
969 * Copied from construct_types_and_fields in LTTV facility.c
971 * basename is the name which identifies the type (along with a prefix
974 int print_type_write_fct(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
975 char *nest_name
, char *field_name
)
977 char basename
[PATH_MAX
];
978 unsigned int basename_len
= 0;
980 strncpy(basename
, nest_name
, PATH_MAX
);
981 basename_len
= strlen(basename
);
983 /* For a named type, we use the type_name directly */
984 if(td
->type_name
!= NULL
) {
985 strncpy(basename
, td
->type_name
, PATH_MAX
);
986 basename_len
= strlen(basename
);
988 /* For a unnamed type, there must be a field name, except for
990 if((basename_len
!= 0)
991 && (basename
[basename_len
-1] != '_'
992 && (field_name
[0] != '\0'))) {
993 strncat(basename
, "_", PATH_MAX
- basename_len
);
994 basename_len
= strlen(basename
);
996 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
1007 dprintf("print_type_write_fct : type has no write function.\n");
1015 fprintf(fd
, "static inline void lttng_write_sequence_%s(\n",
1019 fprintf(fd
, "static inline void lttng_write_struct_%s(\n", basename
);
1022 fprintf(fd
, "static inline void lttng_write_union_%s(\n", basename
);
1025 fprintf(fd
, "static inline void lttng_write_array_%s(\n", basename
);
1028 fprintf(fd
, "static inline void lttng_write_string_%s(\n", basename
);
1031 printf("print_type_write_fct : type has no write function.\n");
1036 fprintf(fd
, "void *buffer,\n");
1038 fprintf(fd
, "size_t *to_base,\n");
1040 fprintf(fd
, "size_t *to,\n");
1042 fprintf(fd
, "const void **from,\n");
1044 fprintf(fd
, "size_t *len,\n");
1046 if(print_type(td
, fd
, 0, basename
, "")) return 1;
1050 fprintf(fd
, " *obj)\n");
1053 fprintf(fd
, " *obj)\n");
1056 fprintf(fd
, " *obj)\n");
1059 fprintf(fd
, " obj)\n");
1062 fprintf(fd
, " obj)\n");
1065 printf("print_type_write_fct : type has no write function.\n");
1072 fprintf(fd
, "size_t size;\n");
1074 fprintf(fd
, "size_t align;\n");
1081 fprintf(fd
, "/* Flush pending memcpy */\n");
1083 fprintf(fd
, "if(*len != 0) {\n");
1085 fprintf(fd
, "if(buffer != NULL)\n");
1087 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1091 fprintf(fd
, "*to += *len;\n");
1093 fprintf(fd
, "*len = 0;\n");
1101 printf("print_type_write_fct : type has no write function.\n");
1106 fprintf(fd
, "align = ");
1107 if(print_type_alignment(td
, fd
, 0, basename
, "", "obj")) return 1;
1111 fprintf(fd
, "if(*len == 0) {\n");
1113 fprintf(fd
, "*to += ltt_align(*to, align); /* align output */\n");
1115 fprintf(fd
, "} else {\n");
1117 fprintf(fd
, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1122 /* First, check if the type has a fixed size. If it is the case, then the size
1123 * to write is know by the compiler : simply use a sizeof() */
1124 if(has_type_fixed_size(td
)) {
1126 fprintf(fd
, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1129 fprintf(fd
, "size = sizeof(");
1130 if(print_type(td
, fd
, 0, basename
, field_name
)) return 1;
1131 fprintf(fd
, ");\n");
1133 fprintf(fd
, "*len += size;\n");
1135 /* The type contains nested variable size subtypes :
1136 * we must write field by field. */
1138 fprintf(fd
, "/* Contains variable sized fields : must explode the structure */\n");
1144 fprintf(fd
, "/* Copy members */\n");
1145 // print_tabs(1, fd);
1146 // fprintf(fd, "size = sizeof(\n");
1147 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1148 fd
, 1, basename
, "len", "obj->", 1)) return 1;
1150 // fprintf(fd, ");\n");
1151 // print_tabs(1, fd);
1152 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1154 fprintf(fd
, "if(buffer != NULL)\n");
1156 fprintf(fd
, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1158 fprintf(fd
, "*to += *len;\n");
1160 fprintf(fd
, "*len = 0;\n");
1163 /* Write the child : varlen child or not ? */
1164 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[1])->type
)) {
1165 /* Fixed size len child : use a multiplication of its size */
1166 // print_tabs(1, fd);
1167 // fprintf(fd, "size = sizeof(\n");
1169 //print_tabs(1, fd);
1170 /* We know that *len does not contain alignment because of the
1171 * previous align output. len is always 0 here. */
1172 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1173 fd
, 1, basename
, "array[0]", "obj->", 1))
1175 // fprintf(fd, ");\n");
1178 fprintf(fd
, "*len = obj->len * (*len);\n");
1180 fprintf(fd
, "if(buffer != NULL)\n");
1182 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1184 fprintf(fd
, "*to += *len;\n");
1186 fprintf(fd
, "*len = 0;\n");
1190 fprintf(fd
, "/* Variable length child : iter. */\n");
1192 fprintf(fd
, "for(unsigned int i=0; i<obj->len; i++) {\n");
1193 if(print_type_write(((field_t
*)td
->fields
.array
[1])->type
,
1194 fd
, 2, basename
, "array[i]", "obj->", 1)) return 1;
1200 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1202 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1204 fprintf(fd
, "*to_base = *to_base+*to;\n");
1206 fprintf(fd
, "*to = 0;\n");
1209 fprintf(fd
, "/* Put source *from just after the C sequence */\n");
1211 fprintf(fd
, "*from = obj+1;\n");
1215 fprintf(fd
, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1217 fprintf(fd
, "if(buffer != NULL)\n");
1219 fprintf(fd
, "memcpy(buffer+*to_base+*to, obj, size);\n");
1221 fprintf(fd
, "*to += size;\n");
1224 fprintf(fd
, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1226 fprintf(fd
, "*to += ltt_align(*to, sizeof(void *));\n");
1228 fprintf(fd
, "*to_base = *to_base+*to;\n");
1230 fprintf(fd
, "*to = 0;\n");
1233 fprintf(fd
, "/* Put source *from just after the C string */\n");
1235 fprintf(fd
, "*from += size;\n");
1238 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
1239 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
1240 type_descriptor_t
*type
= field
->type
;
1241 if(print_type_write(type
,
1242 fd
, 1, basename
, field
->name
, "obj->", 1)) return 1;
1247 printf("ERROR : A union CANNOT contain a variable size child.\n");
1251 /* Write the child : varlen child or not ? */
1252 if(has_type_fixed_size(((field_t
*)td
->fields
.array
[0])->type
)) {
1253 /* Error : if an array has a variable size, then its child must also
1254 * have a variable size. */
1258 fprintf(fd
, "/* Variable length child : iter. */\n");
1260 fprintf(fd
, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename
);
1261 if(print_type_write(((field_t
*)td
->fields
.array
[0])->type
,
1262 fd
, 2, basename
, "", "obj->array[i]", 1)) return 1;
1268 printf("print_type_write_fct : type has no write function.\n");
1276 /* Function footer */
1284 /* Print the logging function of an event. This is the core of genevent */
1285 int print_event_logging_function(char *basename
, facility_t
*fac
,
1286 event_t
*event
, FILE *fd
)
1288 fprintf(fd
, "static inline void trace_%s(\n", basename
);
1289 int has_argument
= 0;
1290 int has_type_fixed
= 0;
1292 /* Does it support per trace tracing ? */
1293 if(event
->per_trace
) {
1295 fprintf(fd
, "struct ltt_trace_struct *dest_trace");
1299 /* Does it support per tracefile tracing ? */
1300 if(event
->per_tracefile
) {
1305 fprintf(fd
, "unsigned int tracefile_index");
1309 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1310 /* For each field, print the function argument */
1311 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1312 type_descriptor_t
*t
= f
->type
;
1317 if(print_arg(t
, fd
, 2, basename
, f
->name
)) return 1;
1322 fprintf(fd
, "void");
1326 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1330 fprintf(fd
,"#else\n");
1332 /* Print the function variables */
1334 fprintf(fd
, "unsigned int index;\n");
1336 fprintf(fd
, "struct ltt_channel_struct *channel;\n");
1338 fprintf(fd
, "struct ltt_trace_struct *trace;\n");
1340 fprintf(fd
, "struct rchan_buf *relayfs_buf;\n");
1342 fprintf(fd
, "void *buffer = NULL;\n");
1344 fprintf(fd
, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1346 fprintf(fd
, "size_t *to_base = &real_to_base;\n");
1348 fprintf(fd
, "size_t real_to = 0;\n");
1350 fprintf(fd
, "size_t *to = &real_to;\n");
1352 fprintf(fd
, "size_t real_len = 0;\n");
1354 fprintf(fd
, "size_t *len = &real_len;\n");
1356 fprintf(fd
, "size_t reserve_size;\n");
1358 fprintf(fd
, "size_t slot_size;\n");
1361 if(event
->fields
.position
> 0) {
1362 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1363 /* Search for at least one child with fixed size. It means
1364 * we need local variables.*/
1365 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1366 type_descriptor_t
*type
= field
->type
;
1367 has_type_fixed
= has_type_local(type
);
1368 if(has_type_fixed
) break;
1371 if(has_type_fixed
) {
1372 fprintf(fd
, "size_t align;\n");
1376 fprintf(fd
, "const void *real_from;\n");
1378 fprintf(fd
, "const void **from = &real_from;\n");
1381 fprintf(fd
, "cycles_t tsc;\n");
1383 fprintf(fd
, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1387 fprintf(fd
, "if(ltt_traces.num_active_traces == 0) return;\n");
1390 /* Calculate event variable len + event data alignment offset.
1391 * Assume that the padding for alignment starts at a void*
1393 * This excludes the header size and alignment. */
1396 fprintf(fd
, "/* For each field, calculate the field size. */\n");
1398 fprintf(fd
, "/* size = *to_base + *to + *len */\n");
1400 fprintf(fd
, "/* Assume that the padding for alignment starts at a\n");
1402 fprintf(fd
, " * sizeof(void *) address. */\n");
1405 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1406 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1407 type_descriptor_t
*type
= field
->type
;
1410 switch(type
->type
) {
1416 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1419 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1423 if(print_type_write(type
,
1424 fd
, 1, basename
, field
->name
, "lttng_param_", 0)) return 1;
1428 fprintf(fd
, "reserve_size = *to_base + *to + *len;\n");
1430 /* Take locks : make sure the trace does not vanish while we write on
1431 * it. A simple preemption disabling is enough (using rcu traces). */
1433 fprintf(fd
, "preempt_disable();\n");
1435 fprintf(fd
, "ltt_nesting[smp_processor_id()]++;\n");
1437 /* Get facility index */
1439 if(event
->per_tracefile
) {
1441 fprintf(fd
, "index = tracefile_index;\n");
1445 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1446 "\t\t\t\t\t\tevent_%s_%s);\n",
1447 fac
->name
, fac
->checksum
, fac
->name
, event
->name
);
1452 /* For each trace */
1454 fprintf(fd
, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n");
1456 fprintf(fd
, "if(!trace->active) continue;\n\n");
1458 if(event
->per_trace
) {
1460 fprintf(fd
, "if(dest_trace != trace) continue;\n\n");
1464 fprintf(fd
, "channel = ltt_get_channel_from_index(trace, index);\n");
1466 fprintf(fd
, "relayfs_buf = channel->rchan->buf[smp_processor_id()];\n");
1471 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1474 fprintf(fd
, "slot_size = 0;\n");
1476 fprintf(fd
, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1478 fprintf(fd
, "reserve_size, &slot_size, &tsc,\n");
1480 fprintf(fd
, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1481 /* If error, return */
1483 fprintf(fd
, "if(!buffer) continue; /* buffer full */\n\n");
1484 //print_tabs(2, fd);
1486 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1488 fprintf(fd
, "*to_base = *to = *len = 0;\n");
1491 /* Write event header */
1493 fprintf(fd
, "ltt_write_event_header(trace, channel, buffer,\n");
1495 fprintf(fd
, "ltt_facility_%s_%X, event_%s_%s,\n", fac
->name
, fac
->checksum
,
1496 fac
->name
, event
->name
);
1498 fprintf(fd
, "reserve_size, before_hdr_pad, tsc);\n");
1500 fprintf(fd
, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1505 for(unsigned int i
=0;i
<event
->fields
.position
;i
++){
1506 field_t
*field
= (field_t
*)(event
->fields
.array
[i
]);
1507 type_descriptor_t
*type
= field
->type
;
1511 switch(type
->type
) {
1517 fprintf(fd
, "*from = lttng_param_%s;\n", field
->name
);
1520 fprintf(fd
, "*from = <tng_param_%s;\n", field
->name
);
1525 if(print_type_write(type
,
1526 fd
, 2, basename
, field
->name
, "lttng_param_", 0)) return 1;
1529 /* Don't forget to flush pending memcpy */
1531 fprintf(fd
, "/* Flush pending memcpy */\n");
1533 fprintf(fd
, "if(*len != 0) {\n");
1535 fprintf(fd
, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1537 fprintf(fd
, "*to += *len;\n");
1538 //print_tabs(3, fd);
1539 //fprintf(fd, "from += len;\n");
1541 fprintf(fd
, "*len = 0;\n");
1550 //fprintf(fd, "commit:\n"); /* DEBUG! */
1552 fprintf(fd
, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1555 fprintf(fd
, "}\n\n");
1559 fprintf(fd
, "ltt_nesting[smp_processor_id()]--;\n");
1561 fprintf(fd
, "preempt_enable_no_resched();\n");
1564 fprintf(fd
, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1570 /* ltt-facility-name.h : main logging header.
1573 void print_log_header_head(facility_t
*fac
, FILE *fd
)
1575 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
1576 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
1577 fprintf(fd
, "#include <linux/types.h>\n");
1578 fprintf(fd
, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac
->name
);
1579 fprintf(fd
, "#include <linux/ltt-core.h>\n");
1585 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1587 sequence_t
*types
= &fac
->named_types
.values
;
1588 fprintf(fd
, "/* Named types */\n");
1591 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1592 /* For each named type, print the definition */
1593 if(print_type_declaration(types
->array
[i
], fd
,
1594 0, "", "")) return 1;
1595 /* Print also the align function */
1596 if(print_type_alignment_fct(types
->array
[i
], fd
,
1597 0, "", "")) return 1;
1598 /* Print also the write function */
1599 if(print_type_write_fct(types
->array
[i
], fd
,
1600 0, "", "")) return 1;
1605 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1607 sequence_t
*events
= &fac
->events
;
1608 char basename
[PATH_MAX
];
1609 unsigned int facname_len
;
1611 strncpy(basename
, fac
->name
, PATH_MAX
);
1612 facname_len
= strlen(basename
);
1613 strncat(basename
, "_", PATH_MAX
-facname_len
);
1614 facname_len
= strlen(basename
);
1616 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1617 event_t
*event
= (event_t
*)events
->array
[i
];
1618 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1620 /* For each event, print structure, and then logging function */
1621 fprintf(fd
, "/* Event %s structures */\n",
1623 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1624 /* For each unnamed type, print the definition */
1625 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1626 type_descriptor_t
*t
= f
->type
;
1627 if(t
->type_name
== NULL
) {
1628 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1629 /* Print also the align function */
1630 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1631 /* Print also the write function */
1632 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1638 fprintf(fd
, "/* Event %s logging function */\n",
1641 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1650 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1652 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1655 int print_log_header(facility_t
*fac
)
1657 char filename
[PATH_MAX
];
1658 unsigned int filename_size
= 0;
1660 dprintf("%s\n", fac
->name
);
1662 strcpy(filename
, "ltt-facility-");
1663 filename_size
= strlen(filename
);
1665 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1666 filename_size
= strlen(filename
);
1668 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1669 filename_size
= strlen(filename
);
1672 fd
= fopen(filename
, "w");
1674 printf("Error opening file %s for writing : %s\n",
1675 filename
, strerror(errno
));
1679 /* Print file head */
1680 print_log_header_head(fac
, fd
);
1682 /* print named types in declaration order */
1683 if(print_log_header_types(fac
, fd
)) return 1;
1686 if(print_log_header_events(fac
, fd
)) return 1;
1688 /* Print file tail */
1689 print_log_header_tail(fac
, fd
);
1698 /* ltt-facility-id-name.h : facility id.
1700 int print_id_header(facility_t
*fac
)
1702 char filename
[PATH_MAX
];
1703 unsigned int filename_size
= 0;
1705 char basename
[PATH_MAX
];
1706 char basename_len
= 0;
1708 dprintf("%s\n", fac
->name
);
1710 strcpy(filename
, "ltt-facility-id-");
1711 filename_size
= strlen(filename
);
1713 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1714 filename_size
= strlen(filename
);
1716 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1717 filename_size
= strlen(filename
);
1720 fd
= fopen(filename
, "w");
1722 printf("Error opening file %s for writing : %s\n",
1723 filename
, strerror(errno
));
1727 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1728 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
1729 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1731 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
1733 fprintf(fd
,"/**** facility handle ****/\n\n");
1734 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
1735 fac
->name
, fac
->checksum
);
1736 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
1738 strncpy(basename
, fac
->name
, PATH_MAX
);
1739 basename_len
= strlen(basename
);
1740 strncat(basename
, "_", PATH_MAX
- basename_len
);
1743 fprintf(fd
,"/**** event index ****/\n\n");
1744 fprintf(fd
,"enum %s_event {\n",fac
->name
);
1746 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
1747 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
1748 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
1750 fprintf(fd
, "event_%s,\n", basename
);
1753 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
1754 fprintf(fd
, "};\n");
1758 fprintf(fd
, "#endif //CONFIG_LTT\n");
1759 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1768 /* ltt-facility-loader-name.h : facility specific loader info.
1770 int print_loader_header(facility_t
*fac
)
1772 char filename
[PATH_MAX
];
1773 unsigned int filename_size
= 0;
1775 dprintf("%s\n", fac
->name
);
1777 strcpy(filename
, "ltt-facility-loader-");
1778 filename_size
= strlen(filename
);
1780 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1781 filename_size
= strlen(filename
);
1783 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1784 filename_size
= strlen(filename
);
1787 fd
= fopen(filename
, "w");
1789 printf("Error opening file %s for writing : %s\n",
1790 filename
, strerror(errno
));
1794 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1795 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
1796 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
1797 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
1798 fprintf(fd
,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
1800 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
1801 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
1802 fac
->name
, fac
->checksum
);
1804 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
1806 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
1807 fac
->name
, fac
->checksum
);
1808 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
1809 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
1810 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
1812 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1813 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1820 /* ltt-facility-loader-name.c : generic faciilty loader
1822 int print_loader_c(facility_t
*fac
)
1824 char filename
[PATH_MAX
];
1825 unsigned int filename_size
= 0;
1827 dprintf("%s\n", fac
->name
);
1829 strcpy(filename
, "ltt-facility-loader-");
1830 filename_size
= strlen(filename
);
1832 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1833 filename_size
= strlen(filename
);
1835 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1836 filename_size
= strlen(filename
);
1839 fd
= fopen(filename
, "w");
1841 printf("Error opening file %s for writing : %s\n",
1842 filename
, strerror(errno
));
1846 fprintf(fd
, "/*\n");
1847 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
1848 fprintf(fd
, " *\n");
1849 fprintf(fd
, " * (C) Copyright 2005 - \n");
1850 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
1851 fprintf(fd
, " *\n");
1852 fprintf(fd
, " * Contains the LTT facility loader.\n");
1853 fprintf(fd
, " *\n");
1854 fprintf(fd
, " */\n");
1857 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
1858 fprintf(fd
, "#include <linux/module.h>\n");
1859 fprintf(fd
, "#include <linux/init.h>\n");
1860 fprintf(fd
, "#include <linux/config.h>\n");
1861 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
1864 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1866 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
1867 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
1869 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
1871 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
1873 fprintf(fd
, "static struct ltt_facility facility = {\n");
1874 fprintf(fd
, "\t.name = ltt_facility_name,\n");
1875 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
1876 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
1877 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
1878 fprintf(fd
, "};\n");
1880 fprintf(fd
, "#ifndef MODULE\n");
1882 fprintf(fd
, "/* Built-in facility. */\n");
1884 fprintf(fd
, "static int __init facility_init(void)\n");
1886 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
1888 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
1889 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1890 fprintf(fd
, "\t\n");
1891 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1893 fprintf(fd
, "__initcall(facility_init);\n");
1897 fprintf(fd
, "#else \n");
1899 fprintf(fd
, "/* Dynamic facility. */\n");
1901 fprintf(fd
, "static int __init facility_init(void)\n");
1903 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac
->name
);
1905 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
1906 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1908 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1911 fprintf(fd
, "static void __exit facility_exit(void)\n");
1913 fprintf(fd
, "\tint err;\n");
1915 fprintf(fd
, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
1916 fprintf(fd
, "\tif(err != 0)\n");
1917 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
1921 fprintf(fd
, "module_init(facility_init)\n");
1922 fprintf(fd
, "module_exit(facility_exit)\n");
1925 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
1926 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
1927 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
1929 fprintf(fd
, "#endif //MODULE\n");
1931 fprintf(fd
, "#endif //CONFIG_LTT\n");
1941 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1943 /*****************************************************************************
1945 * ltt_facility_open : open facilities
1947 * pathname : the path name of the facility
1949 * Open the facility corresponding to the right checksum.
1951 *returns the facility on success, NULL on error.
1952 ****************************************************************************/
1953 facility_t
*ltt_facility_open(char * pathname
)
1958 facility_t
* fac
= NULL
;
1959 char buffer
[BUFFER_SIZE
];
1960 int generated
= FALSE
;
1962 in
.buffer
= &(buffer
[0]);
1964 in
.error
= error_callback
;
1968 in
.fp
= fopen(in
.name
, "r");
1975 token
= getToken(&in
);
1976 if(in
.type
== ENDFILE
) break;
1979 printf("More than one facility in the file. Only using the first one.\n");
1983 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
1984 token
= getName(&in
);
1986 if(strcmp("facility",token
) == 0) {
1987 fac
= malloc(sizeof(facility_t
));
1989 fac
->description
= NULL
;
1990 sequence_init(&(fac
->events
));
1991 table_init(&(fac
->named_types
));
1992 sequence_init(&(fac
->unnamed_types
));
1994 parseFacility(&in
, fac
);
1996 //check if any namedType is not defined
1997 checkNamedTypesImplemented(&fac
->named_types
);
1999 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2004 printf("facility token was expected in file %s\n", in
.name
);
2015 printf("Cannot find facility %s\n", pathname
);
2022 /* Close the facility */
2023 void ltt_facility_close(facility_t
*fac
)
2027 free(fac
->description
);
2028 freeEvents(&fac
->events
);
2029 sequence_dispose(&fac
->events
);
2030 freeNamedType(&fac
->named_types
);
2031 table_dispose(&fac
->named_types
);
2032 freeTypes(&fac
->unnamed_types
);
2033 sequence_dispose(&fac
->unnamed_types
);
2039 void show_help(int argc
, char ** argv
)
2041 printf("Genevent help : \n");
2043 printf("Use %s name.xml\n", argv
[0]);
2044 printf("to create :\n");
2045 printf("ltt-facility-name.h\n");
2046 printf("ltt-facility-id-name.h\n");
2047 printf("ltt-facility-loader-name.h\n");
2048 printf("ltt-facility-loader-name.c\n");
2049 printf("In the current directory.\n");
2053 /* Parse program arguments */
2055 * 0 : continue program
2056 * -1 : stop program, return 0
2057 * > 0 : stop program, return value as exit.
2059 int check_args(int argc
, char **argv
)
2062 printf("Not enough arguments\n");
2063 show_help(argc
, argv
);
2067 if(strcmp(argv
[1], "-h") == 0) {
2068 show_help(argc
, argv
);
2075 int main(int argc
, char **argv
)
2080 err
= check_args(argc
, argv
);
2081 if(err
> 0) return err
;
2082 else if(err
< 0) return 0;
2084 /* open the facility */
2085 fac
= ltt_facility_open(argv
[1]);
2087 printf("Error opening file %s for reading : %s\n",
2088 argv
[1], strerror(errno
));
2092 /* generate the output C files */
2095 /* ltt-facility-name.h : main logging header.
2097 err
= print_log_header(fac
);
2100 /* ltt-facility-id-name.h : facility id.
2102 err
= print_id_header(fac
);
2105 /* ltt-facility-loader-name.h : facility specific loader info.
2107 err
= print_loader_header(fac
);
2110 /* ltt-facility-loader-name.c : generic faciilty loader
2112 err
= print_loader_c(fac
);
2115 /* close the facility */
2116 ltt_facility_close(fac
);
This page took 0.204757 seconds and 4 git commands to generate.