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");
1579 fprintf(fd
, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac
->name
);
1581 fprintf(fd
, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
1584 fprintf(fd
, "#include <linux/ltt-core.h>\n");
1590 int print_log_header_types(facility_t
*fac
, FILE *fd
)
1592 sequence_t
*types
= &fac
->named_types
.values
;
1593 fprintf(fd
, "/* Named types */\n");
1596 for(unsigned int i
= 0; i
< types
->position
; i
++) {
1597 /* For each named type, print the definition */
1598 if(print_type_declaration(types
->array
[i
], fd
,
1599 0, "", "")) return 1;
1600 /* Print also the align function */
1601 if(print_type_alignment_fct(types
->array
[i
], fd
,
1602 0, "", "")) return 1;
1603 /* Print also the write function */
1604 if(print_type_write_fct(types
->array
[i
], fd
,
1605 0, "", "")) return 1;
1610 int print_log_header_events(facility_t
*fac
, FILE *fd
)
1612 sequence_t
*events
= &fac
->events
;
1613 char basename
[PATH_MAX
];
1614 unsigned int facname_len
;
1616 strncpy(basename
, fac
->name
, PATH_MAX
);
1617 facname_len
= strlen(basename
);
1618 strncat(basename
, "_", PATH_MAX
-facname_len
);
1619 facname_len
= strlen(basename
);
1621 for(unsigned int i
= 0; i
< events
->position
; i
++) {
1622 event_t
*event
= (event_t
*)events
->array
[i
];
1623 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
1625 /* For each event, print structure, and then logging function */
1626 fprintf(fd
, "/* Event %s structures */\n",
1628 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
1629 /* For each unnamed type, print the definition */
1630 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
1631 type_descriptor_t
*t
= f
->type
;
1632 if(t
->type_name
== NULL
) {
1633 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
1634 /* Print also the align function */
1635 if((print_type_alignment_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1636 /* Print also the write function */
1637 if((print_type_write_fct(t
, fd
, 0, basename
, f
->name
))) return 1;
1643 fprintf(fd
, "/* Event %s logging function */\n",
1646 if(print_event_logging_function(basename
, fac
, event
, fd
)) return 1;
1655 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
1657 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
1660 int print_log_header(facility_t
*fac
)
1662 char filename
[PATH_MAX
];
1663 unsigned int filename_size
= 0;
1665 dprintf("%s\n", fac
->name
);
1667 strcpy(filename
, "ltt-facility-");
1668 filename_size
= strlen(filename
);
1670 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1671 filename_size
= strlen(filename
);
1674 strncat(filename
, "_", PATH_MAX
- filename_size
);
1675 filename_size
= strlen(filename
);
1677 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
1678 filename_size
= strlen(filename
);
1681 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1682 filename_size
= strlen(filename
);
1685 fd
= fopen(filename
, "w");
1687 printf("Error opening file %s for writing : %s\n",
1688 filename
, strerror(errno
));
1692 /* Print file head */
1693 print_log_header_head(fac
, fd
);
1695 /* print named types in declaration order */
1696 if(print_log_header_types(fac
, fd
)) return 1;
1699 if(print_log_header_events(fac
, fd
)) return 1;
1701 /* Print file tail */
1702 print_log_header_tail(fac
, fd
);
1711 /* ltt-facility-id-name.h : facility id.
1713 int print_id_header(facility_t
*fac
)
1715 char filename
[PATH_MAX
];
1716 unsigned int filename_size
= 0;
1718 char basename
[PATH_MAX
];
1719 char basename_len
= 0;
1721 dprintf("%s\n", fac
->name
);
1723 strcpy(filename
, "ltt-facility-id-");
1724 filename_size
= strlen(filename
);
1726 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1727 filename_size
= strlen(filename
);
1730 strncat(filename
, "_", PATH_MAX
- filename_size
);
1731 filename_size
= strlen(filename
);
1733 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
1734 filename_size
= strlen(filename
);
1737 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1738 filename_size
= strlen(filename
);
1741 fd
= fopen(filename
, "w");
1743 printf("Error opening file %s for writing : %s\n",
1744 filename
, strerror(errno
));
1748 fprintf(fd
, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1749 fprintf(fd
, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac
->capname
);
1750 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1752 fprintf(fd
,"#include <linux/ltt-facilities.h>\n\n");
1754 fprintf(fd
,"/**** facility handle ****/\n\n");
1755 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s_%X;\n",
1756 fac
->name
, fac
->checksum
);
1757 fprintf(fd
,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac
->name
);
1759 strncpy(basename
, fac
->name
, PATH_MAX
);
1760 basename_len
= strlen(basename
);
1761 strncat(basename
, "_", PATH_MAX
- basename_len
);
1764 fprintf(fd
,"/**** event index ****/\n\n");
1765 fprintf(fd
,"enum %s_event {\n",fac
->name
);
1767 for(unsigned int i
= 0; i
< fac
->events
.position
; i
++) {
1768 event_t
*event
= (event_t
*)fac
->events
.array
[i
];
1769 strncpy(basename
+basename_len
, event
->name
, PATH_MAX
-basename_len
);
1771 fprintf(fd
, "event_%s,\n", basename
);
1774 fprintf(fd
, "facility_%s_num_events\n", fac
->name
);
1775 fprintf(fd
, "};\n");
1779 fprintf(fd
, "#endif //CONFIG_LTT\n");
1780 fprintf(fd
, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac
->capname
);
1789 /* ltt-facility-loader-name.h : facility specific loader info.
1791 int print_loader_header(facility_t
*fac
)
1793 char filename
[PATH_MAX
];
1794 unsigned int filename_size
= 0;
1796 dprintf("%s\n", fac
->name
);
1798 strcpy(filename
, "ltt-facility-loader-");
1799 filename_size
= strlen(filename
);
1801 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1802 filename_size
= strlen(filename
);
1805 strncat(filename
, "_", PATH_MAX
- filename_size
);
1806 filename_size
= strlen(filename
);
1808 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
1809 filename_size
= strlen(filename
);
1812 strncat(filename
, ".h", PATH_MAX
- filename_size
);
1813 filename_size
= strlen(filename
);
1816 fd
= fopen(filename
, "w");
1818 printf("Error opening file %s for writing : %s\n",
1819 filename
, strerror(errno
));
1823 fprintf(fd
, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1824 fprintf(fd
, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac
->capname
);
1825 fprintf(fd
, "#ifdef CONFIG_LTT\n\n");
1826 fprintf(fd
,"#include <linux/ltt-facilities.h>\n");
1828 fprintf(fd
,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
1831 fprintf(fd
,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
1834 fprintf(fd
,"ltt_facility_t\tltt_facility_%s;\n", fac
->name
);
1835 fprintf(fd
,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
1836 fac
->name
, fac
->checksum
);
1838 fprintf(fd
,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
1840 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
1841 fac
->name
, fac
->checksum
);
1842 fprintf(fd
,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac
->checksum
);
1843 fprintf(fd
,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac
->name
);
1844 fprintf(fd
,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
1846 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
1847 fprintf(fd
, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac
->capname
);
1854 /* ltt-facility-loader-name.c : generic faciilty loader
1856 int print_loader_c(facility_t
*fac
)
1858 char filename
[PATH_MAX
];
1859 unsigned int filename_size
= 0;
1861 dprintf("%s\n", fac
->name
);
1863 strcpy(filename
, "ltt-facility-loader-");
1864 filename_size
= strlen(filename
);
1866 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
1867 filename_size
= strlen(filename
);
1870 strncat(filename
, "_", PATH_MAX
- filename_size
);
1871 filename_size
= strlen(filename
);
1873 strncat(filename
, fac
->arch
, PATH_MAX
- filename_size
);
1874 filename_size
= strlen(filename
);
1877 strncat(filename
, ".c", PATH_MAX
- filename_size
);
1878 filename_size
= strlen(filename
);
1881 fd
= fopen(filename
, "w");
1883 printf("Error opening file %s for writing : %s\n",
1884 filename
, strerror(errno
));
1888 fprintf(fd
, "/*\n");
1890 fprintf(fd
, " * ltt-facility-loader-%s.c\n", fac
->name
);
1892 fprintf(fd
, " * ltt-facility-loader-%s_%s.c\n", fac
->name
, fac
->arch
);
1893 fprintf(fd
, " *\n");
1894 fprintf(fd
, " * (C) Copyright 2005 - \n");
1895 fprintf(fd
, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
1896 fprintf(fd
, " *\n");
1897 fprintf(fd
, " * Contains the LTT facility loader.\n");
1898 fprintf(fd
, " *\n");
1899 fprintf(fd
, " */\n");
1902 fprintf(fd
, "#include <linux/ltt-facilities.h>\n");
1903 fprintf(fd
, "#include <linux/module.h>\n");
1904 fprintf(fd
, "#include <linux/init.h>\n");
1905 fprintf(fd
, "#include <linux/config.h>\n");
1907 fprintf(fd
, "#include \"ltt-facility-loader-%s.h\"\n", fac
->name
);
1909 fprintf(fd
, "#include \"ltt-facility-loader-%s_%s.h\"\n",
1910 fac
->name
, fac
->arch
);
1913 fprintf(fd
, "#ifdef CONFIG_LTT\n");
1915 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
1916 fprintf(fd
, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
1918 fprintf(fd
, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
1920 fprintf(fd
, "#define SYMBOL_STRING(sym) #sym\n");
1922 fprintf(fd
, "static struct ltt_facility facility = {\n");
1923 fprintf(fd
, "\t.name = ltt_facility_name,\n");
1924 fprintf(fd
, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
1925 fprintf(fd
, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
1926 fprintf(fd
, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
1927 fprintf(fd
, "};\n");
1929 fprintf(fd
, "#ifndef MODULE\n");
1931 fprintf(fd
, "/* Built-in facility. */\n");
1933 fprintf(fd
, "static int __init facility_init(void)\n");
1935 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac
->name
);
1937 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
1938 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1939 fprintf(fd
, "\t\n");
1940 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1942 fprintf(fd
, "__initcall(facility_init);\n");
1946 fprintf(fd
, "#else \n");
1948 fprintf(fd
, "/* Dynamic facility. */\n");
1950 fprintf(fd
, "static int __init facility_init(void)\n");
1952 fprintf(fd
, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac
->name
);
1954 fprintf(fd
, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
1955 fprintf(fd
, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1957 fprintf(fd
, "\treturn LTT_FACILITY_SYMBOL;\n");
1960 fprintf(fd
, "static void __exit facility_exit(void)\n");
1962 fprintf(fd
, "\tint err;\n");
1964 fprintf(fd
, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
1965 fprintf(fd
, "\tif(err != 0)\n");
1966 fprintf(fd
, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
1970 fprintf(fd
, "module_init(facility_init)\n");
1971 fprintf(fd
, "module_exit(facility_exit)\n");
1974 fprintf(fd
, "MODULE_LICENSE(\"GPL\");\n");
1975 fprintf(fd
, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
1976 fprintf(fd
, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
1978 fprintf(fd
, "#endif //MODULE\n");
1980 fprintf(fd
, "#endif //CONFIG_LTT\n");
1990 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1992 /*****************************************************************************
1994 * ltt_facility_open : open facilities
1996 * pathname : the path name of the facility
1998 * Open the facility corresponding to the right checksum.
2000 *returns the facility on success, NULL on error.
2001 ****************************************************************************/
2002 facility_t
*ltt_facility_open(char * pathname
)
2007 facility_t
* fac
= NULL
;
2008 char buffer
[BUFFER_SIZE
];
2009 int generated
= FALSE
;
2011 in
.buffer
= &(buffer
[0]);
2013 in
.error
= error_callback
;
2017 in
.fp
= fopen(in
.name
, "r");
2024 token
= getToken(&in
);
2025 if(in
.type
== ENDFILE
) break;
2028 printf("More than one facility in the file. Only using the first one.\n");
2032 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
2033 token
= getName(&in
);
2035 if(strcmp("facility",token
) == 0) {
2036 fac
= malloc(sizeof(facility_t
));
2038 fac
->description
= NULL
;
2039 sequence_init(&(fac
->events
));
2040 table_init(&(fac
->named_types
));
2041 sequence_init(&(fac
->unnamed_types
));
2043 parseFacility(&in
, fac
);
2045 //check if any namedType is not defined
2046 checkNamedTypesImplemented(&fac
->named_types
);
2048 generateChecksum(fac
->name
, &fac
->checksum
, &fac
->events
);
2053 printf("facility token was expected in file %s\n", in
.name
);
2064 printf("Cannot find facility %s\n", pathname
);
2071 /* Close the facility */
2072 void ltt_facility_close(facility_t
*fac
)
2076 free(fac
->description
);
2077 freeEvents(&fac
->events
);
2078 sequence_dispose(&fac
->events
);
2079 freeNamedType(&fac
->named_types
);
2080 table_dispose(&fac
->named_types
);
2081 freeTypes(&fac
->unnamed_types
);
2082 sequence_dispose(&fac
->unnamed_types
);
2088 void show_help(int argc
, char ** argv
)
2090 printf("Genevent help : \n");
2092 printf("Use %s name.xml\n", argv
[0]);
2093 printf("to create :\n");
2094 printf("ltt-facility-name.h\n");
2095 printf("ltt-facility-id-name.h\n");
2096 printf("ltt-facility-loader-name.h\n");
2097 printf("ltt-facility-loader-name.c\n");
2098 printf("In the current directory.\n");
2102 /* Parse program arguments */
2104 * 0 : continue program
2105 * -1 : stop program, return 0
2106 * > 0 : stop program, return value as exit.
2108 int check_args(int argc
, char **argv
)
2111 printf("Not enough arguments\n");
2112 show_help(argc
, argv
);
2116 if(strcmp(argv
[1], "-h") == 0) {
2117 show_help(argc
, argv
);
2124 int main(int argc
, char **argv
)
2129 err
= check_args(argc
, argv
);
2130 if(err
> 0) return err
;
2131 else if(err
< 0) return 0;
2133 /* open the facility */
2134 fac
= ltt_facility_open(argv
[1]);
2136 printf("Error opening file %s for reading : %s\n",
2137 argv
[1], strerror(errno
));
2141 /* generate the output C files */
2144 /* ltt-facility-name.h : main logging header.
2146 err
= print_log_header(fac
);
2149 /* ltt-facility-id-name.h : facility id.
2151 err
= print_id_header(fac
);
2154 /* ltt-facility-loader-name.h : facility specific loader info.
2156 err
= print_loader_header(fac
);
2159 /* ltt-facility-loader-name.c : generic faciilty loader
2161 err
= print_loader_c(fac
);
2164 /* close the facility */
2165 ltt_facility_close(fac
);
This page took 0.112292 seconds and 4 git commands to generate.