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 print_type(td
->nested_type
, fd
, tabs
, basename
, "");
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 print_type(td
->nested_type
, fd
, tabs
, basename
, "");
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 print_type(type
, fd
, tabs
, basename
, field
->name
);
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 print_type(type
, fd
, tabs
, basename
, field
->name
);
326 fprintf(fd
, "%s", field
->name
);
333 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
343 /* ltt-facility-name.h : main logging header.
346 void print_log_header_head(facility_t
*fac
, FILE *fd
)
348 fprintf(fd
, "#ifndef _LTT_FACILITY_%s_H_\n", fac
->capname
);
349 fprintf(fd
, "#define _LTT_FACILITY_%s_H_\n\n", fac
->capname
);
355 int print_log_header_types(facility_t
*fac
, FILE *fd
)
357 sequence_t
*types
= &fac
->named_types
.values
;
358 fprintf(fd
, "/* Named types */\n");
361 for(unsigned int i
= 0; i
< types
->position
; i
++) {
362 /* For each named type, print the definition */
363 if((print_type_declaration(types
->array
[i
], fd
,
364 0, "", ""))) return 1;
369 int print_log_header_events(facility_t
*fac
, FILE *fd
)
376 void print_log_header_tail(facility_t
*fac
, FILE *fd
)
378 fprintf(fd
, "#endif //_LTT_FACILITY_%s_H_\n",fac
->capname
);
381 int print_log_header(facility_t
*fac
)
383 char filename
[PATH_MAX
];
384 unsigned int filename_size
= 0;
386 dprintf("%s\n", fac
->name
);
388 strcpy(filename
, "ltt-facility-");
389 filename_size
= strlen(filename
);
391 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
392 filename_size
= strlen(filename
);
394 strncat(filename
, ".h", PATH_MAX
- filename_size
);
395 filename_size
= strlen(filename
);
398 fd
= fopen(filename
, "w");
400 printf("Error opening file %s for writing : %s\n",
401 filename
, strerror(errno
));
405 /* Print file head */
406 print_log_header_head(fac
, fd
);
408 /* print named types in declaration order */
409 if(print_log_header_types(fac
, fd
)) return 1;
412 if(print_log_header_events(fac
, fd
)) return 1;
414 /* Print file tail */
415 print_log_header_tail(fac
, fd
);
424 /* ltt-facility-id-name.h : facility id.
426 int print_id_header(facility_t
*fac
)
428 char filename
[PATH_MAX
];
429 unsigned int filename_size
= 0;
431 dprintf("%s\n", fac
->name
);
433 strcpy(filename
, "ltt-facility-id-");
434 filename_size
= strlen(filename
);
436 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
437 filename_size
= strlen(filename
);
439 strncat(filename
, ".h", PATH_MAX
- filename_size
);
440 filename_size
= strlen(filename
);
443 fd
= fopen(filename
, "w");
445 printf("Error opening file %s for writing : %s\n",
446 filename
, strerror(errno
));
456 /* ltt-facility-loader-name.h : facility specific loader info.
458 int print_loader_header(facility_t
*fac
)
460 char filename
[PATH_MAX
];
461 unsigned int filename_size
= 0;
463 dprintf("%s\n", fac
->name
);
465 strcpy(filename
, "ltt-facility-loader-");
466 filename_size
= strlen(filename
);
468 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
469 filename_size
= strlen(filename
);
471 strncat(filename
, ".h", PATH_MAX
- filename_size
);
472 filename_size
= strlen(filename
);
475 fd
= fopen(filename
, "w");
477 printf("Error opening file %s for writing : %s\n",
478 filename
, strerror(errno
));
487 /* ltt-facility-loader-name.c : generic faciilty loader
489 int print_loader_c(facility_t
*fac
)
491 char filename
[PATH_MAX
];
492 unsigned int filename_size
= 0;
494 dprintf("%s\n", fac
->name
);
496 strcpy(filename
, "ltt-facility-loader-");
497 filename_size
= strlen(filename
);
499 strncat(filename
, fac
->name
, PATH_MAX
- filename_size
);
500 filename_size
= strlen(filename
);
502 strncat(filename
, ".c", PATH_MAX
- filename_size
);
503 filename_size
= strlen(filename
);
506 fd
= fopen(filename
, "w");
508 printf("Error opening file %s for writing : %s\n",
509 filename
, strerror(errno
));
522 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
524 /*****************************************************************************
526 * ltt_facility_open : open facilities
528 * pathname : the path name of the facility
530 * Open the facility corresponding to the right checksum.
532 *returns the facility on success, NULL on error.
533 ****************************************************************************/
534 facility_t
*ltt_facility_open(char * pathname
)
539 facility_t
* fac
= NULL
;
540 unsigned long checksum
;
541 char buffer
[BUFFER_SIZE
];
542 int generated
= FALSE
;
544 in
.buffer
= &(buffer
[0]);
546 in
.error
= error_callback
;
550 in
.fp
= fopen(in
.name
, "r");
557 token
= getToken(&in
);
558 if(in
.type
== ENDFILE
) break;
561 printf("More than one facility in the file. Only using the first one.\n");
565 if(strcmp(token
, "<")) in
.error(&in
,"not a facility file");
566 token
= getName(&in
);
568 if(strcmp("facility",token
) == 0) {
569 fac
= malloc(sizeof(facility_t
));
571 fac
->description
= NULL
;
572 sequence_init(&(fac
->events
));
573 table_init(&(fac
->named_types
));
574 sequence_init(&(fac
->unnamed_types
));
576 parseFacility(&in
, fac
);
578 //check if any namedType is not defined
579 checkNamedTypesImplemented(&fac
->named_types
);
581 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
586 printf("facility token was expected in file %s\n", in
.name
);
597 printf("Cannot find facility %s\n", pathname
);
604 /* Close the facility */
605 void ltt_facility_close(facility_t
*fac
)
609 free(fac
->description
);
610 freeEvents(&fac
->events
);
611 sequence_dispose(&fac
->events
);
612 freeNamedType(&fac
->named_types
);
613 table_dispose(&fac
->named_types
);
614 freeTypes(&fac
->unnamed_types
);
615 sequence_dispose(&fac
->unnamed_types
);
621 void show_help(int argc
, char ** argv
)
623 printf("Genevent help : \n");
625 printf("Use %s name.xml\n", argv
[0]);
626 printf("to create :\n");
627 printf("ltt-facility-name.h\n");
628 printf("ltt-facility-id-name.h\n");
629 printf("ltt-facility-loader-name.h\n");
630 printf("ltt-facility-loader-name.c\n");
631 printf("In the current directory.\n");
635 /* Parse program arguments */
637 * 0 : continue program
638 * -1 : stop program, return 0
639 * > 0 : stop program, return value as exit.
641 int check_args(int argc
, char **argv
)
644 printf("Not enough arguments\n");
645 show_help(argc
, argv
);
649 if(strcmp(argv
[1], "-h") == 0) {
650 show_help(argc
, argv
);
657 int main(int argc
, char **argv
)
662 err
= check_args(argc
, argv
);
663 if(err
> 0) return err
;
664 else if(err
< 0) return 0;
666 /* open the facility */
667 fac
= ltt_facility_open(argv
[1]);
669 printf("Error opening file %s for reading : %s\n",
670 argv
[1], strerror(errno
));
674 /* generate the output C files */
677 /* ltt-facility-name.h : main logging header.
679 err
= print_log_header(fac
);
682 /* ltt-facility-id-name.h : facility id.
684 err
= print_id_header(fac
);
687 /* ltt-facility-loader-name.h : facility specific loader info.
689 err
= print_loader_header(fac
);
692 /* ltt-facility-loader-name.c : generic faciilty loader
694 err
= print_loader_c(fac
);
697 /* close the facility */
698 ltt_facility_close(fac
);
This page took 0.0600039999999999 seconds and 4 git commands to generate.