118298ce7bf58fe9552c91ab40f03678425f9fd2
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: ",\
92 void print_tabs(unsigned int tabs
, FILE *fd
)
94 for(unsigned int i
= 0; i
<tabs
;i
++)
98 /* Type size checking */
99 /* Uses #error in the generated code to signal error and stop the compiler */
100 int print_check(int fd
);
104 int print_types(int fd
);
108 int print_events(int fd
);
113 * Copied from construct_types_and_fields in LTTV facility.c */
115 int print_type(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
116 char *nest_name
, char *field_name
)
118 char basename
[PATH_MAX
];
119 unsigned int basename_len
= 0;
121 strcpy(basename
, nest_name
);
122 basename_len
= strlen(basename
);
124 /* For a named type, we use the type_name directly */
125 if(td
->type_name
!= NULL
) {
126 strncpy(basename
, td
->type_name
, PATH_MAX
);
127 basename_len
= strlen(basename
);
129 /* For a unnamed type, there must be a field name */
130 if(basename_len
!= 0) {
131 strncat(basename
, "_", PATH_MAX
- basename_len
);
132 basename_len
= strlen(basename
);
134 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
139 fprintf(fd
, "%s", intOutputTypes
[getSizeindex(td
->size
)]);
142 fprintf(fd
, "%s", uintOutputTypes
[getSizeindex(td
->size
)]);
145 fprintf(fd
, "signed char");
148 fprintf(fd
, "unsigned char");
151 fprintf(fd
, "short");
154 fprintf(fd
, "unsigned short");
160 fprintf(fd
, "unsigned int");
163 fprintf(fd
, "%s", floatOutputTypes
[getSizeindex(td
->size
)]);
166 fprintf(fd
, "void *");
172 fprintf(fd
, "unsigned long");
175 fprintf(fd
, "size_t");
178 fprintf(fd
, "ssize_t");
181 fprintf(fd
, "off_t");
184 fprintf(fd
, "char *");
187 fprintf(fd
, "enum lttng_%s", basename
);
190 fprintf(fd
, "lttng_array_%s", basename
);
193 fprintf(fd
, "lttng_sequence_%s", basename
);
196 fprintf(fd
, "struct lttng_%s", basename
);
199 fprintf(fd
, "union lttng_%s", basename
);
202 printf("print_type : unknown type\n");
209 /* print type declaration.
211 * Copied from construct_types_and_fields in LTTV facility.c */
213 int print_type_declaration(type_descriptor_t
* td
, FILE *fd
, unsigned int tabs
,
214 char *nest_name
, char *field_name
)
216 char basename
[PATH_MAX
];
217 unsigned int basename_len
= 0;
219 strcpy(basename
, nest_name
);
220 basename_len
= strlen(basename
);
222 /* For a named type, we use the type_name directly */
223 if(td
->type_name
!= NULL
) {
224 strncpy(basename
, td
->type_name
, PATH_MAX
);
225 basename_len
= strlen(basename
);
227 /* For a unnamed type, there must be a field name */
228 if(basename_len
!= 0) {
229 strncat(basename
, "_", PATH_MAX
- basename_len
);
230 basename_len
= strlen(basename
);
232 strncat(basename
, field_name
, PATH_MAX
- basename_len
);
233 dprintf("%s\n", field_name
);
238 fprintf(fd
, "enum lttng_%s", basename
);
240 for(unsigned int i
=0;i
<td
->labels
.position
;i
++){
242 fprintf(fd
, "LTTNG_%s", ((char*)(td
->labels
.array
[i
])));
250 assert(td
->size
>= 0);
251 if(td
->nested_type
->type_name
== NULL
) {
252 /* Not a named nested type : we must print its declaration first */
253 if(print_type_declaration(td
->nested_type
,
254 fd
, 0, basename
, "")) return 1;
256 fprintf(fd
, "#define LTTNG_ARRAY_SIZE_%s %llu\n", basename
,
258 if(print_type(td
->nested_type
, fd
, tabs
, basename
, "")) return 1;
259 fprintf(fd
, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename
,
264 if(td
->nested_type
->type_name
== NULL
) {
265 /* Not a named nested type : we must print its declaration first */
266 if(print_type_declaration(td
->nested_type
,
267 fd
, 0, basename
, "")) return 1;
269 fprintf(fd
, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
272 fprintf(fd
, "struct lttng_sequence_%s", basename
);
275 fprintf(fd
, "unsigned int len;\n");
277 if(print_type(td
->nested_type
, fd
, tabs
, basename
, "")) return 1;
278 fprintf(fd
, " *array;\n");
284 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
285 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
286 type_descriptor_t
*type
= field
->type
;
287 if(type
->type_name
== NULL
) {
288 /* Not a named nested type : we must print its declaration first */
289 if(print_type_declaration(type
,
290 fd
, 0, basename
, field
->name
)) return 1;
293 fprintf(fd
, "struct lttng_%s", basename
);
295 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
296 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
297 type_descriptor_t
*type
= field
->type
;
299 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
301 fprintf(fd
, "%s", field
->name
);
308 /* TODO : Do not allow variable length fields in a union */
309 for(unsigned int i
=0;i
<td
->fields
.position
;i
++){
310 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
311 type_descriptor_t
*type
= field
->type
;
312 if(type
->type_name
== NULL
) {
313 /* Not a named nested type : we must print its declaration first */
314 if(print_type_declaration(type
,
315 fd
, 0, basename
, field
->name
)) return 1;
318 fprintf(fd
, "union lttng_%s", basename
);
320 for(unsigned i
=0;i
<td
->fields
.position
;i
++){
321 field_t
*field
= (field_t
*)(td
->fields
.array
[i
]);
322 type_descriptor_t
*type
= field
->type
;
324 if(print_type(type
, fd
, tabs
, basename
, field
->name
)) return 1;
326 fprintf(fd
, "%s", field
->name
);
333 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
340 /* Print the logging function of an event. This is the core of genevent */
341 int print_event_logging_function(char *basename
, event_t
*event
, FILE *fd
)
343 fprintf(fd
, "static inline void trace_%s(\n", basename
);
344 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
345 /* For each field, print the function argument */
346 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
347 type_descriptor_t
*t
= f
->type
;
349 if(print_type(t
, fd
, 0, basename
, f
->name
)) return 1;
350 fprintf(fd
, " %s", f
->name
);
351 if(j
< event
->fields
.position
-1) {
356 if(event
->fields
.position
== 0) {
361 fprintf(fd
, "#ifndef CONFIG_LTT\n");
364 fprintf(fd
,"#else\n");
369 fprintf(fd
, "#endif //CONFIG_LTT\n\n");
374 /* ltt-facility-name.h : main logging header.
377 void print_log_header_head(facility_t
*fac
, FILE *fd
)
379 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
380 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
382 fprintf(fd
, "/* Facility activation at compile time. */\n");
383 fprintf(fd
, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
388 int print_log_header_types(facility_t
*fac
, FILE *fd
)
390 sequence_t
*types
= &fac
->named_types
.values
;
391 fprintf(fd
, "/* Named types */\n");
394 for(unsigned int i
= 0; i
< types
->position
; i
++) {
395 /* For each named type, print the definition */
396 if((print_type_declaration(types
->array
[i
], fd
,
397 0, "", ""))) return 1;
402 int print_log_header_events(facility_t
*fac
, FILE *fd
)
404 sequence_t
*events
= &fac
->events
;
405 char basename
[PATH_MAX
];
406 unsigned int facname_len
;
408 strncpy(basename
, fac
->name
, PATH_MAX
);
409 facname_len
= strlen(basename
);
410 strncat(basename
, "_", PATH_MAX
-facname_len
);
411 facname_len
= strlen(basename
);
413 for(unsigned int i
= 0; i
< events
->position
; i
++) {
414 event_t
*event
= (event_t
*)events
->array
[i
];
415 strncpy(&basename
[facname_len
], event
->name
, PATH_MAX
-facname_len
);
417 /* For each event, print structure, and then logging function */
418 fprintf(fd
, "/* Event %s structures */\n",
420 for(unsigned int j
= 0; j
< event
->fields
.position
; j
++) {
421 /* For each unnamed type, print the definition */
422 field_t
*f
= (field_t
*)event
->fields
.array
[j
];
423 type_descriptor_t
*t
= f
->type
;
424 if(t
->type_name
== NULL
)
425 if((print_type_declaration(t
, fd
, 0, basename
, f
->name
))) return 1;
429 fprintf(fd
, "/* Event %s logging function */\n",
432 if(print_event_logging_function(basename
, event
, fd
)) return 1;
441 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
443 fprintf(fd
, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac
->capname
);
444 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
447 int print_log_header(facility_t
*fac
)
449 char filename
[PATH_MAX
];
450 unsigned int filename_size
= 0;
452 dprintf("%s\n", fac
->name
);
454 strcpy(filename
, "ltt-facility-");
455 filename_size
= strlen(filename
);
457 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
458 filename_size
= strlen(filename
);
460 strncat(filename
, ".h", PATH_MAX
- filename_size
);
461 filename_size
= strlen(filename
);
464 fd
= fopen(filename
, "w");
466 printf("Error opening file %s for writing : %s\n",
467 filename
, strerror(errno
));
471 /* Print file head */
472 print_log_header_head(fac
, fd
);
474 /* print named types in declaration order */
475 if(print_log_header_types(fac
, fd
)) return 1;
478 if(print_log_header_events(fac
, fd
)) return 1;
480 /* Print file tail */
481 print_log_header_tail(fac
, fd
);
490 /* ltt-facility-id-name.h : facility id.
492 int print_id_header(facility_t
*fac
)
494 char filename
[PATH_MAX
];
495 unsigned int filename_size
= 0;
497 dprintf("%s\n", fac
->name
);
499 strcpy(filename
, "ltt-facility-id-");
500 filename_size
= strlen(filename
);
502 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
503 filename_size
= strlen(filename
);
505 strncat(filename
, ".h", PATH_MAX
- filename_size
);
506 filename_size
= strlen(filename
);
509 fd
= fopen(filename
, "w");
511 printf("Error opening file %s for writing : %s\n",
512 filename
, strerror(errno
));
522 /* ltt-facility-loader-name.h : facility specific loader info.
524 int print_loader_header(facility_t
*fac
)
526 char filename
[PATH_MAX
];
527 unsigned int filename_size
= 0;
529 dprintf("%s\n", fac
->name
);
531 strcpy(filename
, "ltt-facility-loader-");
532 filename_size
= strlen(filename
);
534 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
535 filename_size
= strlen(filename
);
537 strncat(filename
, ".h", PATH_MAX
- filename_size
);
538 filename_size
= strlen(filename
);
541 fd
= fopen(filename
, "w");
543 printf("Error opening file %s for writing : %s\n",
544 filename
, strerror(errno
));
553 /* ltt-facility-loader-name.c : generic faciilty loader
555 int print_loader_c(facility_t
*fac
)
557 char filename
[PATH_MAX
];
558 unsigned int filename_size
= 0;
560 dprintf("%s\n", fac
->name
);
562 strcpy(filename
, "ltt-facility-loader-");
563 filename_size
= strlen(filename
);
565 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
566 filename_size
= strlen(filename
);
568 strncat(filename
, ".c", PATH_MAX
- filename_size
);
569 filename_size
= strlen(filename
);
572 fd
= fopen(filename
, "w");
574 printf("Error opening file %s for writing : %s\n",
575 filename
, strerror(errno
));
588 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
590 /*****************************************************************************
592 * ltt_facility_open : open facilities
594 * pathname : the path name of the facility
596 * Open the facility corresponding to the right checksum.
598 *returns the facility on success, NULL on error.
599 ****************************************************************************/
600 facility_t
*ltt_facility_open(char * pathname
)
605 facility_t
* fac
= NULL
;
606 unsigned long checksum
;
607 char buffer
[BUFFER_SIZE
];
608 int generated
= FALSE
;
610 in
.buffer
= &(buffer
[0]);
612 in
.error
= error_callback
;
616 in
.fp
= fopen(in
.name
, "r");
623 token
= getToken(&in
);
624 if(in
.type
== ENDFILE
) break;
627 printf("More than one facility in the file. Only using the first one.\n");
631 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
632 token
= getName(&in
);
634 if(strcmp("facility",token
) == 0) {
635 fac
= malloc(sizeof(facility_t
));
637 fac
->description
= NULL
;
638 sequence_init(&(fac
->events
));
639 table_init(&(fac
->named_types
));
640 sequence_init(&(fac
->unnamed_types
));
642 parseFacility(&in
, fac
);
644 //check if any namedType is not defined
645 checkNamedTypesImplemented(&fac
->named_types
);
647 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
652 printf("facility token was expected in file %s\n", in
.name
);
663 printf("Cannot find facility %s\n", pathname
);
670 /* Close the facility */
671 void ltt_facility_close(facility_t
*fac
)
675 free(fac
->description
);
676 freeEvents(&fac
->events
);
677 sequence_dispose(&fac
->events
);
678 freeNamedType(&fac
->named_types
);
679 table_dispose(&fac
->named_types
);
680 freeTypes(&fac
->unnamed_types
);
681 sequence_dispose(&fac
->unnamed_types
);
687 void show_help(int argc
, char ** argv
)
689 printf("Genevent help : \n");
691 printf("Use %s name.xml\n", argv
[0]);
692 printf("to create :\n");
693 printf("ltt-facility-name.h\n");
694 printf("ltt-facility-id-name.h\n");
695 printf("ltt-facility-loader-name.h\n");
696 printf("ltt-facility-loader-name.c\n");
697 printf("In the current directory.\n");
701 /* Parse program arguments */
703 * 0 : continue program
704 * -1 : stop program, return 0
705 * > 0 : stop program, return value as exit.
707 int check_args(int argc
, char **argv
)
710 printf("Not enough arguments\n");
711 show_help(argc
, argv
);
715 if(strcmp(argv
[1], "-h") == 0) {
716 show_help(argc
, argv
);
723 int main(int argc
, char **argv
)
728 err
= check_args(argc
, argv
);
729 if(err
> 0) return err
;
730 else if(err
< 0) return 0;
732 /* open the facility */
733 fac
= ltt_facility_open(argv
[1]);
735 printf("Error opening file %s for reading : %s\n",
736 argv
[1], strerror(errno
));
740 /* generate the output C files */
743 /* ltt-facility-name.h : main logging header.
745 err
= print_log_header(fac
);
748 /* ltt-facility-id-name.h : facility id.
750 err
= print_id_header(fac
);
753 /* ltt-facility-loader-name.h : facility specific loader info.
755 err
= print_loader_header(fac
);
758 /* ltt-facility-loader-name.c : generic faciilty loader
760 err
= print_loader_c(fac
);
763 /* close the facility */
764 ltt_facility_close(fac
);
This page took 0.046331 seconds and 4 git commands to generate.