a489165e91db5dbacd24e1911ad8cc8c1e9cc5b8
[lttv.git] / genevent-new / genevent.c
1 /******************************************************************************
2 * Genevent
3 *
4 * Event generator. XML to logging C code converter.
5 *
6 * Program parameters :
7 * ./genevent name.xml
8 *
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.
12 *
13 * Supports :
14 * - C Alignment
15 * - C types : struct, union, enum, basic types.
16 * - Architectures : LP32, ILP32, ILP64, LLP64, LP64.
17 *
18 * Additionnal structures supported :
19 * - embedded variable size strings
20 * - embedded variable size arrays
21 * - embedded variable size sequences
22 *
23 * Notes :
24 * (1)
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.
29 *
30 * (2)
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.
34 *
35 * Note that, from
36 * http://www.usenix.org/publications/login/standards/10.data.html
37 * (Andrew Josey <a.josey@opengroup.org>) :
38 *
39 * Data Type LP32 ILP32 ILP64 LLP64 LP64
40 * char 8 8 8 8 8
41 * short 16 16 16 16 16
42 * int32 32
43 * int 16 32 64 32 32
44 * long 32 32 64 32 64
45 * long long (int64) 64
46 * pointer 32 32 64 64 64
47 *
48 * With these constraints :
49 * sizeof(char) <= sizeof(short) <= sizeof(int)
50 * <= sizeof(long) = sizeof(size_t)
51 *
52 * and therefore sizeof(long) <= sizeof(pointer) <= sizeof(size_t)
53 *
54 * Which means we only have to remember which is the biggest type in a structure
55 * to know the structure's alignment.
56 */
57
58 #define _GNU_SOURCE
59 #include <limits.h>
60 #include <stdlib.h>
61 #include <errno.h>
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <fcntl.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <unistd.h>
68 #include <assert.h>
69
70 #include "genevent.h"
71 #include "parser.h"
72
73
74 #define TRUE 1
75 #define FALSE (!TRUE)
76
77 /* Debugging printf */
78 #ifdef DEBUG
79 #define dprintf(...) \
80 do {\
81 printf(__FILE__ ",%u,%s: ",\
82 __LINE__, __func__);\
83 printf(__VA_ARGS__);\
84 } while(0)
85 #else
86 #define dprintf(...)
87 #endif
88
89 /* Code printing */
90
91 void print_tabs(unsigned int tabs, FILE *fd)
92 {
93 for(unsigned int i = 0; i<tabs;i++)
94 fprintf(fd, "\t");
95 }
96
97 /* print type.
98 *
99 * Copied from construct_types_and_fields in LTTV facility.c */
100
101 int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs,
102 char *nest_name, char *field_name)
103 {
104 char basename[PATH_MAX];
105 unsigned int basename_len = 0;
106
107 strcpy(basename, nest_name);
108 basename_len = strlen(basename);
109
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);
114 } else {
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);
121 }
122 strncat(basename, field_name, PATH_MAX - basename_len);
123 }
124
125 switch(td->type) {
126 case INT_FIXED:
127 fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]);
128 break;
129 case UINT_FIXED:
130 fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]);
131 break;
132 case CHAR:
133 fprintf(fd, "signed char");
134 break;
135 case UCHAR:
136 fprintf(fd, "unsigned char");
137 break;
138 case SHORT:
139 fprintf(fd, "short");
140 break;
141 case USHORT:
142 fprintf(fd, "unsigned short");
143 break;
144 case INT:
145 fprintf(fd, "int");
146 break;
147 case UINT:
148 fprintf(fd, "unsigned int");
149 break;
150 case FLOAT:
151 fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]);
152 break;
153 case POINTER:
154 fprintf(fd, "const void *");
155 break;
156 case LONG:
157 fprintf(fd, "long");
158 break;
159 case ULONG:
160 fprintf(fd, "unsigned long");
161 break;
162 case SIZE_T:
163 fprintf(fd, "size_t");
164 break;
165 case SSIZE_T:
166 fprintf(fd, "ssize_t");
167 break;
168 case OFF_T:
169 fprintf(fd, "off_t");
170 break;
171 case STRING:
172 fprintf(fd, "const char *");
173 break;
174 case ENUM:
175 fprintf(fd, "enum lttng_%s", basename);
176 break;
177 case ARRAY:
178 fprintf(fd, "lttng_array_%s", basename);
179 break;
180 case SEQUENCE:
181 fprintf(fd, "lttng_sequence_%s", basename);
182 break;
183 case STRUCT:
184 fprintf(fd, "struct lttng_%s", basename);
185 break;
186 case UNION:
187 fprintf(fd, "union lttng_%s", basename);
188 break;
189 default:
190 printf("print_type : unknown type\n");
191 return 1;
192 }
193
194 return 0;
195 }
196
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)
200 {
201 char basename[PATH_MAX];
202 unsigned int basename_len = 0;
203
204 strcpy(basename, nest_name);
205 basename_len = strlen(basename);
206
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);
211 } else {
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);
218 }
219 strncat(basename, field_name, PATH_MAX - basename_len);
220 }
221
222 print_tabs(tabs, fd);
223
224 switch(td->type) {
225 case INT_FIXED:
226 fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]);
227 fprintf(fd, " %s", field_name);
228 break;
229 case UINT_FIXED:
230 fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]);
231 fprintf(fd, " %s", field_name);
232 break;
233 case CHAR:
234 fprintf(fd, "signed char");
235 fprintf(fd, " %s", field_name);
236 break;
237 case UCHAR:
238 fprintf(fd, "unsigned char");
239 fprintf(fd, " %s", field_name);
240 break;
241 case SHORT:
242 fprintf(fd, "short");
243 fprintf(fd, " %s", field_name);
244 break;
245 case USHORT:
246 fprintf(fd, "unsigned short");
247 fprintf(fd, " %s", field_name);
248 break;
249 case INT:
250 fprintf(fd, "int");
251 fprintf(fd, " %s", field_name);
252 break;
253 case UINT:
254 fprintf(fd, "unsigned int");
255 fprintf(fd, " %s", field_name);
256 break;
257 case FLOAT:
258 fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]);
259 fprintf(fd, " %s", field_name);
260 break;
261 case POINTER:
262 fprintf(fd, "const void *");
263 fprintf(fd, " %s", field_name);
264 break;
265 case LONG:
266 fprintf(fd, "long");
267 fprintf(fd, " %s", field_name);
268 break;
269 case ULONG:
270 fprintf(fd, "unsigned long");
271 fprintf(fd, " %s", field_name);
272 break;
273 case SIZE_T:
274 fprintf(fd, "size_t");
275 fprintf(fd, " %s", field_name);
276 break;
277 case SSIZE_T:
278 fprintf(fd, "ssize_t");
279 fprintf(fd, " %s", field_name);
280 break;
281 case OFF_T:
282 fprintf(fd, "off_t");
283 fprintf(fd, " %s", field_name);
284 break;
285 case STRING:
286 fprintf(fd, "const char *");
287 fprintf(fd, " %s", field_name);
288 break;
289 case ENUM:
290 fprintf(fd, "enum lttng_%s", basename);
291 fprintf(fd, " %s", field_name);
292 break;
293 case ARRAY:
294 fprintf(fd, "lttng_array_%s", basename);
295 fprintf(fd, " %s", field_name);
296 break;
297 case SEQUENCE:
298 fprintf(fd, "lttng_sequence_%s *", basename);
299 fprintf(fd, " %s", field_name);
300 break;
301 case STRUCT:
302 fprintf(fd, "struct lttng_%s *", basename);
303 fprintf(fd, " %s", field_name);
304 break;
305 case UNION:
306 fprintf(fd, "union lttng_%s *", basename);
307 fprintf(fd, " %s", field_name);
308 break;
309 default:
310 printf("print_type : unknown type\n");
311 return 1;
312 }
313
314 return 0;
315 }
316
317
318 /* Does the type has a fixed size ? (as know from the compiler)
319 *
320 * 1 : fixed size
321 * 0 : variable length
322 */
323 int has_type_fixed_size(type_descriptor_t *td)
324 {
325 switch(td->type) {
326 case INT_FIXED:
327 case UINT_FIXED:
328 case CHAR:
329 case UCHAR:
330 case SHORT:
331 case USHORT:
332 case INT:
333 case UINT:
334 case FLOAT:
335 case POINTER:
336 case LONG:
337 case ULONG:
338 case SIZE_T:
339 case SSIZE_T:
340 case OFF_T:
341 case ENUM:
342 case UNION: /* The union must have fixed size children. Must be checked by
343 the parser */
344 return 1;
345 break;
346 case STRING:
347 case SEQUENCE:
348 return 0;
349 break;
350 case STRUCT:
351 {
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;
356
357 has_type_fixed = has_type_fixed_size(type);
358 if(!has_type_fixed) return 0;
359 }
360 return 1;
361 }
362 break;
363 case ARRAY:
364 assert(td->size >= 0);
365 return has_type_fixed_size(((field_t*)td->fields.array[0])->type);
366 break;
367 case NONE:
368 printf("There is a type defined to NONE : bad.\n");
369 assert(0);
370 break;
371 }
372 return 0; //make gcc happy.
373 }
374
375
376
377
378
379 /* print type declaration.
380 *
381 * Copied from construct_types_and_fields in LTTV facility.c */
382
383 int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
384 char *nest_name, char *field_name)
385 {
386 char basename[PATH_MAX];
387 unsigned int basename_len = 0;
388
389 strncpy(basename, nest_name, PATH_MAX);
390 basename_len = strlen(basename);
391
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);
396 } else {
397 /* For a unnamed type, there must be a field name, except for
398 * the array. */
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);
404 }
405 strncat(basename, field_name, PATH_MAX - basename_len);
406 }
407
408 switch(td->type) {
409 case ENUM:
410 fprintf(fd, "enum lttng_%s", basename);
411 fprintf(fd, " {\n");
412 for(unsigned int i=0;i<td->labels.position;i++){
413 print_tabs(1, fd);
414 fprintf(fd, "LTTNG_%s", ((char*)(td->labels.array[i])));
415 fprintf(fd, ",\n");
416 }
417 fprintf(fd, "};\n");
418 fprintf(fd, "\n");
419 break;
420
421 case ARRAY:
422 dprintf("%s\n", basename);
423 assert(td->size >= 0);
424 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
425 /* Not a named nested type : we must print its declaration first */
426 if(print_type_declaration(((field_t*)td->fields.array[0])->type,
427 fd, 0, basename, "")) return 1;
428 }
429 fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename,
430 td->size);
431 fprintf(fd, "typedef ");
432 if(print_type(((field_t*)td->fields.array[0])->type,
433 fd, tabs, basename, "")) return 1;
434 fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename,
435 basename);
436 fprintf(fd, "\n");
437 break;
438 case SEQUENCE:
439 /* We assume that the sequence length type does not need to be declared.
440 */
441 if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
442 /* Not a named nested type : we must print its declaration first */
443 if(print_type_declaration(((field_t*)td->fields.array[1])->type,
444 fd, 0, basename, "")) return 1;
445 }
446 fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
447 basename,
448 basename);
449 fprintf(fd, "struct lttng_sequence_%s", basename);
450 fprintf(fd, " {\n");
451 print_tabs(1, fd);
452 if(print_type(((field_t*)td->fields.array[0])->type,
453 fd, tabs, basename, "")) return 1;
454 fprintf(fd, " len;\n");
455 print_tabs(1, fd);
456 if(print_type(((field_t*)td->fields.array[1])->type,
457 fd, tabs, basename, "")) return 1;
458 fprintf(fd, " *array;\n");
459 fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy
460 it to the buffer directly. */
461 fprintf(fd, "\n");
462 break;
463
464 case STRUCT:
465 for(unsigned int i=0;i<td->fields.position;i++){
466 field_t *field = (field_t*)(td->fields.array[i]);
467 type_descriptor_t *type = field->type;
468 if(type->type_name == NULL) {
469 /* Not a named nested type : we must print its declaration first */
470 if(print_type_declaration(type,
471 fd, 0, basename, field->name)) return 1;
472 }
473 }
474 fprintf(fd, "struct lttng_%s", basename);
475 fprintf(fd, " {\n");
476 for(unsigned int i=0;i<td->fields.position;i++){
477 field_t *field = (field_t*)(td->fields.array[i]);
478 type_descriptor_t *type = field->type;
479 print_tabs(1, fd);
480 if(print_type(type, fd, tabs, basename, field->name)) return 1;
481 fprintf(fd, " ");
482 fprintf(fd, "%s", field->name);
483 fprintf(fd, ";\n");
484 }
485 fprintf(fd, "} LTT_ALIGN;\n");
486 fprintf(fd, "\n");
487 break;
488 case UNION:
489 for(unsigned int i=0;i<td->fields.position;i++){
490 field_t *field = (field_t*)(td->fields.array[i]);
491 type_descriptor_t *type = field->type;
492 if(type->type_name == NULL) {
493 /* Not a named nested type : we must print its declaration first */
494 if(print_type_declaration(type,
495 fd, 0, basename, field->name)) return 1;
496 }
497 }
498 fprintf(fd, "union lttng_%s", basename);
499 fprintf(fd, " {\n");
500 for(unsigned i=0;i<td->fields.position;i++){
501 field_t *field = (field_t*)(td->fields.array[i]);
502 type_descriptor_t *type = field->type;
503 print_tabs(1, fd);
504 if(print_type(type, fd, tabs, basename, field->name)) return 1;
505 fprintf(fd, " ");
506 fprintf(fd, "%s", field->name);
507 fprintf(fd, ";\n");
508 }
509 fprintf(fd, "} LTT_ALIGN;\n");
510 fprintf(fd, "\n");
511 break;
512 default:
513 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
514 break;
515 }
516
517 return 0;
518 }
519
520
521 /* print type alignment.
522 *
523 * Copied from construct_types_and_fields in LTTV facility.c
524 *
525 * basename is the name which identifies the type (along with a prefix
526 * (possibly)). */
527
528 int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
529 char *nest_name, char *field_name, char *obj_prefix)
530 {
531 char basename[PATH_MAX];
532 unsigned int basename_len = 0;
533
534 strncpy(basename, nest_name, PATH_MAX);
535 basename_len = strlen(basename);
536
537 /* For a named type, we use the type_name directly */
538 if(td->type_name != NULL) {
539 strncpy(basename, td->type_name, PATH_MAX);
540 basename_len = strlen(basename);
541 } else {
542 /* For a unnamed type, there must be a field name, except for
543 * the array. */
544 if((basename_len != 0)
545 && (basename[basename_len-1] != '_'
546 && field_name != NULL
547 && (field_name[0] != '\0'))) {
548 strncat(basename, "_", PATH_MAX - basename_len);
549 basename_len = strlen(basename);
550 }
551 if(field_name != NULL)
552 strncat(basename, field_name, PATH_MAX - basename_len);
553 }
554
555 if(field_name[0] == '\0') {
556 /* We are in a write function : it's the obj that we must align. */
557 switch(td->type) {
558 case SEQUENCE:
559 fprintf(fd, "lttng_get_alignment_sequence_%s(%s)", basename,
560 obj_prefix);
561 break;
562 case STRUCT:
563 fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename,
564 obj_prefix);
565 break;
566 case UNION:
567 fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename,
568 obj_prefix);
569 break;
570 case ARRAY:
571 fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename,
572 obj_prefix);
573 case STRING:
574 fprintf(fd, "sizeof(char)");
575 break;
576 default:
577 printf("error : type unexpected\n");
578 return 1;
579 break;
580 }
581 } else {
582
583 switch(td->type) {
584 case INT_FIXED:
585 case UINT_FIXED:
586 case CHAR:
587 case UCHAR:
588 case SHORT:
589 case USHORT:
590 case INT:
591 case UINT:
592 case FLOAT:
593 case POINTER:
594 case LONG:
595 case ULONG:
596 case SIZE_T:
597 case SSIZE_T:
598 case OFF_T:
599 case ENUM:
600 fprintf(fd, "sizeof(");
601 if(print_type(td, fd, 0, basename, "")) return 1;
602 fprintf(fd, ")");
603 break;
604 case STRING:
605 fprintf(fd, "sizeof(char)");
606 break;
607 case SEQUENCE:
608 fprintf(fd, "lttng_get_alignment_sequence_%s(&%s%s)", basename,
609 obj_prefix, field_name);
610 break;
611 case STRUCT:
612 fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename,
613 obj_prefix, field_name);
614 break;
615 case UNION:
616 fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename,
617 obj_prefix, field_name);
618 break;
619 case ARRAY:
620 fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename,
621 obj_prefix, field_name);
622 break;
623 case NONE:
624 printf("error : type NONE unexpected\n");
625 return 1;
626 break;
627 }
628 }
629
630 return 0;
631 }
632
633 /* print type write.
634 *
635 * Copied from construct_types_and_fields in LTTV facility.c
636 *
637 * basename is the name which identifies the type (along with a prefix
638 * (possibly)). */
639
640 int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
641 char *nest_name, char *field_name, char *obj_prefix)
642 {
643 char basename[PATH_MAX];
644 unsigned int basename_len = 0;
645
646 strncpy(basename, nest_name, PATH_MAX);
647 basename_len = strlen(basename);
648
649 /* For a named type, we use the type_name directly */
650 if(td->type_name != NULL) {
651 strncpy(basename, td->type_name, PATH_MAX);
652 basename_len = strlen(basename);
653 } else {
654 /* For a unnamed type, there must be a field name, except for
655 * the array. */
656 if((basename_len != 0)
657 && (basename[basename_len-1] != '_'
658 && (field_name[0] != '\0'))) {
659 strncat(basename, "_", PATH_MAX - basename_len);
660 basename_len = strlen(basename);
661 }
662 strncat(basename, field_name, PATH_MAX - basename_len);
663 }
664
665 switch(td->type) {
666 case INT_FIXED:
667 case UINT_FIXED:
668 case CHAR:
669 case UCHAR:
670 case SHORT:
671 case USHORT:
672 case INT:
673 case UINT:
674 case FLOAT:
675 case POINTER:
676 case LONG:
677 case ULONG:
678 case SIZE_T:
679 case SSIZE_T:
680 case OFF_T:
681 case ENUM:
682 print_tabs(tabs, fd);
683 fprintf(fd, "size = ");
684 fprintf(fd, "sizeof(");
685 if(print_type(td, fd, 0, basename, "")) return 1;
686 fprintf(fd, ");\n");
687 print_tabs(tabs, fd);
688 fprintf(fd, "size += ltt_align(*to+*len, size) + size;\n");
689 print_tabs(tabs, fd);
690 fprintf(fd, "*len += size;");
691 break;
692 case STRING:
693 print_tabs(tabs, fd);
694 fprintf(fd,
695 "lttng_write_string_%s(buffer, to_base, to, from, len, %s%s);\n",
696 basename, obj_prefix, field_name);
697 break;
698 case SEQUENCE:
699 print_tabs(tabs, fd);
700 fprintf(fd,
701 "lttng_write_sequence_%s(buffer, to_base, to, from, len, &%s%s);",
702 basename, obj_prefix, field_name);
703 break;
704 case STRUCT:
705 print_tabs(tabs, fd);
706 fprintf(fd,
707 "lttng_write_struct_%s(buffer, to_base, to, from, len, &%s%s);",
708 basename, obj_prefix, field_name);
709 break;
710 case UNION:
711 print_tabs(tabs, fd);
712 fprintf(fd,
713 "lttng_write_union_%s(buffer, to_base, to, from, len, &%s%s);",
714 basename, obj_prefix, field_name);
715 break;
716 case ARRAY:
717 print_tabs(tabs, fd);
718 fprintf(fd,
719 "lttng_write_array_%s(buffer, to_base, to, from, len, %s%s);",
720 basename, obj_prefix, field_name);
721 break;
722 case NONE:
723 printf("Error : type NONE unexpected\n");
724 return 1;
725 break;
726 }
727
728 return 0;
729 }
730
731
732
733 /* print type alignment function.
734 *
735 * Copied from construct_types_and_fields in LTTV facility.c
736 *
737 * basename is the name which identifies the type (along with a prefix
738 * (possibly)). */
739
740 int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
741 unsigned int tabs,
742 char *nest_name, char *field_name)
743 {
744 char basename[PATH_MAX];
745 unsigned int basename_len = 0;
746
747 strncpy(basename, nest_name, PATH_MAX);
748 basename_len = strlen(basename);
749
750 /* For a named type, we use the type_name directly */
751 if(td->type_name != NULL) {
752 strncpy(basename, td->type_name, PATH_MAX);
753 basename_len = strlen(basename);
754 } else {
755 /* For a unnamed type, there must be a field name, except for
756 * the array. */
757 if((basename_len != 0)
758 && (basename[basename_len-1] != '_'
759 && (field_name[0] != '\0'))) {
760 strncat(basename, "_", PATH_MAX - basename_len);
761 basename_len = strlen(basename);
762 }
763 strncat(basename, field_name, PATH_MAX - basename_len);
764 }
765
766 switch(td->type) {
767 case SEQUENCE:
768 /* Function header */
769 fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
770 basename);
771 print_tabs(2, fd);
772 if(print_type(td, fd, 0, basename, "")) return 1;
773 fprintf(fd, " *obj)\n");
774 fprintf(fd, "{\n");
775 print_tabs(1, fd);
776 fprintf(fd, "size_t align=0, localign;");
777 fprintf(fd, "\n");
778 print_tabs(1, fd);
779 fprintf(fd, "localign = ");
780 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
781 fd, 0, basename, "len", "obj->")) return 1;
782 fprintf(fd, ";\n");
783 print_tabs(1, fd);
784 fprintf(fd, "align = max(align, localign);\n");
785 fprintf(fd, "\n");
786 print_tabs(1, fd);
787 fprintf(fd, "localign = ");
788 if(print_type_alignment(((field_t*)td->fields.array[1])->type,
789 fd, 0, basename, "array[0]", "obj->")) return 1;
790 fprintf(fd, ";\n");
791 print_tabs(1, fd);
792 fprintf(fd, "align = max(align, localign);\n");
793 fprintf(fd, "\n");
794 print_tabs(1, fd);
795 fprintf(fd, "return align;\n");
796 break;
797 case STRUCT:
798 /* Function header */
799 fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
800 basename);
801 print_tabs(2, fd);
802 if(print_type(td, fd, 0, basename, "")) return 1;
803 fprintf(fd, " *obj)\n");
804 fprintf(fd, "{\n");
805 print_tabs(1, fd);
806 fprintf(fd, "size_t align=0, localign;");
807 fprintf(fd, "\n");
808 for(unsigned int i=0;i<td->fields.position;i++){
809 field_t *field = (field_t*)(td->fields.array[i]);
810 type_descriptor_t *type = field->type;
811 print_tabs(1, fd);
812 fprintf(fd, "localign = ");
813 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
814 return 1;
815 fprintf(fd, ";\n");
816 print_tabs(1, fd);
817 fprintf(fd, "align = max(align, localign);\n");
818 fprintf(fd, "\n");
819 }
820 print_tabs(1, fd);
821 fprintf(fd, "return align;\n");
822
823 break;
824 case UNION:
825 /* Function header */
826 fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
827 basename);
828 print_tabs(2, fd);
829 if(print_type(td, fd, 0, basename, "")) return 1;
830 fprintf(fd, " *obj)\n");
831 fprintf(fd, "{\n");
832 print_tabs(1, fd);
833 fprintf(fd, "size_t align=0, localign;");
834 fprintf(fd, "\n");
835 for(unsigned int i=0;i<td->fields.position;i++){
836 field_t *field = (field_t*)(td->fields.array[i]);
837 type_descriptor_t *type = field->type;
838 print_tabs(1, fd);
839 fprintf(fd, "localign = ");
840 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
841 return 1;
842 fprintf(fd, ";\n");
843 print_tabs(1, fd);
844 fprintf(fd, "align = max(align, localign);\n");
845 fprintf(fd, "\n");
846 }
847 print_tabs(1, fd);
848 fprintf(fd, "return align;\n");
849
850 break;
851 case ARRAY:
852 /* Function header */
853 fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
854 basename);
855 print_tabs(2, fd);
856 if(print_type(td, fd, 0, basename, "")) return 1;
857 fprintf(fd, " obj)\n");
858 fprintf(fd, "{\n");
859 print_tabs(1, fd);
860 fprintf(fd, "return \n");
861 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
862 fd, 0, basename, "", "obj[0]"))
863 return 1;
864 fprintf(fd, ";\n");
865 break;
866 default:
867 dprintf("print_type_alignment_fct : type has no alignment function.\n");
868 return 0;
869 break;
870 }
871
872
873 /* Function footer */
874 fprintf(fd, "}\n");
875 fprintf(fd, "\n");
876
877 return 0;
878 }
879
880 /* print type write function.
881 *
882 * Copied from construct_types_and_fields in LTTV facility.c
883 *
884 * basename is the name which identifies the type (along with a prefix
885 * (possibly)). */
886
887 int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
888 char *nest_name, char *field_name)
889 {
890 char basename[PATH_MAX];
891 unsigned int basename_len = 0;
892
893 strncpy(basename, nest_name, PATH_MAX);
894 basename_len = strlen(basename);
895
896 /* For a named type, we use the type_name directly */
897 if(td->type_name != NULL) {
898 strncpy(basename, td->type_name, PATH_MAX);
899 basename_len = strlen(basename);
900 } else {
901 /* For a unnamed type, there must be a field name, except for
902 * the array. */
903 if((basename_len != 0)
904 && (basename[basename_len-1] != '_'
905 && (field_name[0] != '\0'))) {
906 strncat(basename, "_", PATH_MAX - basename_len);
907 basename_len = strlen(basename);
908 }
909 strncat(basename, field_name, PATH_MAX - basename_len);
910 }
911
912 switch(td->type) {
913 case SEQUENCE:
914 case STRUCT:
915 case UNION:
916 case ARRAY:
917 case STRING:
918 break;
919 default:
920 dprintf("print_type_write_fct : type has no write function.\n");
921 return 0;
922 break;
923 }
924
925 /* Print header */
926 switch(td->type) {
927 case SEQUENCE:
928 fprintf(fd, "static inline void lttng_write_sequence_%s(\n",
929 basename);
930 break;
931 case STRUCT:
932 fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename);
933 break;
934 case UNION:
935 fprintf(fd, "static inline void lttng_write_union_%s(\n", basename);
936 break;
937 case ARRAY:
938 fprintf(fd, "static inline void lttng_write_array_%s(\n", basename);
939 break;
940 case STRING:
941 fprintf(fd, "static inline void lttng_write_string_%s(\n", basename);
942 break;
943 default:
944 printf("print_type_write_fct : type has no write function.\n");
945 break;
946 }
947
948 print_tabs(2, fd);
949 fprintf(fd, "void *buffer,\n");
950 print_tabs(2, fd);
951 fprintf(fd, "size_t *to_base,\n");
952 print_tabs(2, fd);
953 fprintf(fd, "size_t *to,\n");
954 print_tabs(2, fd);
955 fprintf(fd, "void **from,\n");
956 print_tabs(2, fd);
957 fprintf(fd, "size_t *len,\n");
958 print_tabs(2, fd);
959 if(print_type(td, fd, 0, basename, "")) return 1;
960
961 switch(td->type) {
962 case SEQUENCE:
963 fprintf(fd, " *obj)\n");
964 break;
965 case STRUCT:
966 fprintf(fd, " *obj)\n");
967 break;
968 case UNION:
969 fprintf(fd, " *obj)\n");
970 break;
971 case ARRAY:
972 fprintf(fd, " obj)\n");
973 break;
974 case STRING:
975 fprintf(fd, " obj)\n");
976 break;
977 default:
978 printf("print_type_write_fct : type has no write function.\n");
979 break;
980 }
981
982 fprintf(fd, "{\n");
983 print_tabs(1, fd);
984 fprintf(fd, "size_t align, size;\n");
985 fprintf(fd, "\n");
986
987 switch(td->type) {
988 case SEQUENCE:
989 case STRING:
990 print_tabs(1, fd);
991 fprintf(fd, "/* Flush pending memcpy */\n");
992 print_tabs(1, fd);
993 fprintf(fd, "if(*len != 0) {\n");
994 print_tabs(2, fd);
995 fprintf(fd, "if(buffer != NULL)\n");
996 print_tabs(3, fd);
997 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
998 print_tabs(1, fd);
999 fprintf(fd, "}\n");
1000 print_tabs(1, fd);
1001 fprintf(fd, "*to += *len;\n");
1002 print_tabs(1, fd);
1003 fprintf(fd, "*len = 0;\n");
1004 fprintf(fd, "\n");
1005 break;
1006 case STRUCT:
1007 case UNION:
1008 case ARRAY:
1009 break;
1010 default:
1011 printf("print_type_write_fct : type has no write function.\n");
1012 break;
1013 }
1014
1015 print_tabs(1, fd);
1016 fprintf(fd, "align = ");
1017 if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
1018 fprintf(fd, ";\n");
1019 fprintf(fd, "\n");
1020 print_tabs(1, fd);
1021 fprintf(fd, "if(*len == 0) {\n");
1022 print_tabs(2, fd);
1023 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
1024 print_tabs(1, fd);
1025 fprintf(fd, "} else {\n");
1026 print_tabs(2, fd);
1027 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1028 print_tabs(1, fd);
1029 fprintf(fd, "}\n");
1030 fprintf(fd, "\n");
1031
1032 /* First, check if the type has a fixed size. If it is the case, then the size
1033 * to write is know by the compiler : simply use a sizeof() */
1034 if(has_type_fixed_size(td)) {
1035 print_tabs(1, fd);
1036 fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1037 fprintf(fd, "\n");
1038 print_tabs(1, fd);
1039 fprintf(fd, "size = sizeof(");
1040 if(print_type(td, fd, 0, basename, field_name)) return 1;
1041 fprintf(fd, ");\n");
1042 print_tabs(1, fd);
1043 fprintf(fd, "*len += size;\n");
1044 } else {
1045 /* The type contains nested variable size subtypes :
1046 * we must write field by field. */
1047 print_tabs(1, fd);
1048 fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n");
1049 fprintf(fd, "\n");
1050
1051 switch(td->type) {
1052 case SEQUENCE:
1053 print_tabs(1, fd);
1054 fprintf(fd, "/* Copy members */\n");
1055 // print_tabs(1, fd);
1056 // fprintf(fd, "size = sizeof(\n");
1057 if(print_type_write(((field_t*)td->fields.array[0])->type,
1058 fd, 1, basename, "len", "obj->")) return 1;
1059 fprintf(fd, "\n");
1060 // fprintf(fd, ");\n");
1061 // print_tabs(1, fd);
1062 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1063 print_tabs(1, fd);
1064 fprintf(fd, "if(buffer != NULL)\n");
1065 print_tabs(2, fd);
1066 fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, size);\n");
1067 print_tabs(1, fd);
1068 fprintf(fd, "*to += size;\n");
1069 fprintf(fd, "\n");
1070
1071 /* Write the child : varlen child or not ? */
1072 if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) {
1073 /* Fixed size len child : use a multiplication of its size */
1074 // print_tabs(1, fd);
1075 // fprintf(fd, "size = sizeof(\n");
1076 if(print_type_write(((field_t*)td->fields.array[1])->type,
1077 fd, 1, basename, "array[0]", "obj->")) return 1;
1078 // fprintf(fd, ");\n");
1079 // print_tabs(1, fd);
1080 fprintf(fd, "*to += ltt_align(*to, size);\n");
1081 print_tabs(1, fd);
1082 fprintf(fd, "size = obj->len * size;\n");
1083 print_tabs(1, fd);
1084 fprintf(fd, "if(buffer != NULL)\n");
1085 print_tabs(2, fd);
1086 fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, size);\n");
1087 print_tabs(1, fd);
1088 fprintf(fd, "*to += size;\n");
1089 fprintf(fd, "\n");
1090 } else {
1091 print_tabs(1, fd);
1092 fprintf(fd, "/* Variable length child : iter. */\n");
1093 print_tabs(1, fd);
1094 fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n");
1095 if(print_type_write(((field_t*)td->fields.array[1])->type,
1096 fd, 2, basename, "array[i]", "obj->")) return 1;
1097 print_tabs(1, fd);
1098 fprintf(fd, "}\n");
1099 }
1100 fprintf(fd, "\n");
1101 print_tabs(1, fd);
1102 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1103 print_tabs(1, fd);
1104 fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n");
1105 print_tabs(1, fd);
1106 fprintf(fd, "*to_base = *to_base+*to;\n");
1107 print_tabs(1, fd);
1108 fprintf(fd, "*to = 0;\n");
1109 fprintf(fd, "\n");
1110 print_tabs(1, fd);
1111 fprintf(fd, "/* Put source *from just after the C sequence */\n");
1112 print_tabs(1, fd);
1113 fprintf(fd, "*from = obj+1;\n");
1114 break;
1115 case STRING:
1116 print_tabs(1, fd);
1117 fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1118 print_tabs(1, fd);
1119 fprintf(fd, "if(buffer != NULL)\n");
1120 print_tabs(2, fd);
1121 fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n");
1122 print_tabs(1, fd);
1123 fprintf(fd, "*to += size;\n");
1124 fprintf(fd, "\n");
1125 print_tabs(1, fd);
1126 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1127 print_tabs(1, fd);
1128 fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n");
1129 print_tabs(1, fd);
1130 fprintf(fd, "*to_base = *to_base+*to;\n");
1131 print_tabs(1, fd);
1132 fprintf(fd, "*to = 0;\n");
1133 fprintf(fd, "\n");
1134 print_tabs(1, fd);
1135 fprintf(fd, "/* Put source *from just after the C string */\n");
1136 print_tabs(1, fd);
1137 fprintf(fd, "*from += size;\n");
1138 break;
1139 case STRUCT:
1140 for(unsigned int i=0;i<td->fields.position;i++){
1141 field_t *field = (field_t*)(td->fields.array[i]);
1142 type_descriptor_t *type = field->type;
1143 if(print_type_write(type,
1144 fd, 1, basename, field->name, "obj->")) return 1;
1145 fprintf(fd, "\n");
1146 }
1147 break;
1148 case UNION:
1149 printf("ERROR : A union CANNOT contain a variable size child.\n");
1150 return 1;
1151 break;
1152 case ARRAY:
1153 /* Write the child : varlen child or not ? */
1154 if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) {
1155 /* Error : if an array has a variable size, then its child must also
1156 * have a variable size. */
1157 assert(0);
1158 } else {
1159 print_tabs(1, fd);
1160 fprintf(fd, "/* Variable length child : iter. */\n");
1161 print_tabs(1, fd);
1162 fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename);
1163 if(print_type_write(((field_t*)td->fields.array[0])->type,
1164 fd, 2, basename, "", "obj->array[i]")) return 1;
1165 print_tabs(1, fd);
1166 fprintf(fd, "}\n");
1167 }
1168 break;
1169 default:
1170 printf("print_type_write_fct : type has no write function.\n");
1171 break;
1172 }
1173
1174
1175 }
1176
1177
1178 /* Function footer */
1179 fprintf(fd, "}\n");
1180 fprintf(fd, "\n");
1181 return 0;
1182 }
1183
1184
1185
1186 /* Print the logging function of an event. This is the core of genevent */
1187 int print_event_logging_function(char *basename, facility_t *fac,
1188 event_t *event, FILE *fd)
1189 {
1190 fprintf(fd, "static inline void trace_%s(\n", basename);
1191 int has_argument = 0;
1192
1193 /* Does it support per trace tracing ? */
1194 if(event->per_trace) {
1195 print_tabs(2, fd);
1196 fprintf(fd, "struct ltt_trace_struct *dest_trace");
1197 has_argument = 1;
1198 }
1199
1200 /* Does it support per tracefile tracing ? */
1201 if(event->per_tracefile) {
1202 if(has_argument) {
1203 fprintf(fd, ",");
1204 fprintf(fd, "\n");
1205 }
1206 fprintf(fd, "unsigned int tracefile_index");
1207 has_argument = 1;
1208 }
1209
1210 for(unsigned int j = 0; j < event->fields.position; j++) {
1211 /* For each field, print the function argument */
1212 field_t *f = (field_t*)event->fields.array[j];
1213 type_descriptor_t *t = f->type;
1214 if(has_argument) {
1215 fprintf(fd, ",");
1216 fprintf(fd, "\n");
1217 }
1218 if(print_arg(t, fd, 2, basename, f->name)) return 1;
1219 has_argument = 1;
1220 }
1221 if(!has_argument) {
1222 print_tabs(2, fd);
1223 fprintf(fd, "void");
1224 }
1225 fprintf(fd,")\n");
1226 fprintf(fd,
1227 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1228 fac->capname);
1229 fprintf(fd, "{\n");
1230 fprintf(fd, "}\n");
1231 fprintf(fd,"#else\n");
1232 fprintf(fd, "{\n");
1233 /* Print the function variables */
1234 print_tabs(1, fd);
1235 fprintf(fd, "unsigned int index;\n");
1236 print_tabs(1, fd);
1237 fprintf(fd, "struct ltt_channel_struct *channel;\n");
1238 print_tabs(1, fd);
1239 fprintf(fd, "struct ltt_trace_struct *trace;\n");
1240 print_tabs(1, fd);
1241 fprintf(fd, "struct rchan_buf *relayfs_buf;\n");
1242 print_tabs(1, fd);
1243 fprintf(fd, "void *buffer = NULL;\n");
1244 print_tabs(1, fd);
1245 fprintf(fd, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1246 print_tabs(1, fd);
1247 fprintf(fd, "size_t to = 0;\n");
1248 print_tabs(1, fd);
1249 fprintf(fd, "void *from;");
1250 print_tabs(1, fd);
1251 fprintf(fd, "size_t len = 0;\n");
1252 print_tabs(1, fd);
1253 fprintf(fd, "size_t reserve_size;\n");
1254 print_tabs(1, fd);
1255 fprintf(fd, "size_t slot_size;\n");
1256 print_tabs(1, fd);
1257 fprintf(fd, "cycles_t tsc;\n");
1258 print_tabs(1, fd);
1259 fprintf(fd, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
1260 fprintf(fd, "\n");
1261
1262 print_tabs(1, fd);
1263 fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n");
1264 fprintf(fd, "\n");
1265
1266 /* Calculate event variable len + event data alignment offset.
1267 * Assume that the padding for alignment starts at a void*
1268 * address.
1269 * This excludes the header size and alignment. */
1270
1271 print_tabs(1, fd);
1272 fprintf(fd, "/* For each field, calculate the field size. */\n");
1273 print_tabs(1, fd);
1274 fprintf(fd, "/* size = to_base + to + len */\n");
1275 print_tabs(1, fd);
1276 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
1277 print_tabs(1, fd);
1278 fprintf(fd, " * sizeof(void *) address. */\n");
1279 fprintf(fd, "\n");
1280
1281 for(unsigned int i=0;i<event->fields.position;i++){
1282 field_t *field = (field_t*)(event->fields.array[i]);
1283 type_descriptor_t *type = field->type;
1284 if(print_type_write(type,
1285 fd, 1, basename, field->name, "")) return 1;
1286 fprintf(fd, "\n");
1287 }
1288 print_tabs(1, fd);
1289 fprintf(fd, "reserve_size = to_base + to + len;\n");
1290 print_tabs(1, fd);
1291 fprintf(fd, "to_base = to = len = 0;\n");
1292 fprintf(fd, "\n");
1293
1294 /* Take locks : make sure the trace does not vanish while we write on
1295 * it. A simple preemption disabling is enough (using rcu traces). */
1296 print_tabs(1, fd);
1297 fprintf(fd, "preempt_disable();\n");
1298 print_tabs(1, fd);
1299 fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n");
1300
1301 /* Get facility index */
1302
1303 if(event->per_tracefile) {
1304 print_tabs(1, fd);
1305 fprintf(fd, "index = tracefile_index;\n");
1306 } else {
1307 print_tabs(1, fd);
1308 fprintf(fd,
1309 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1310 "\t\t\t\t\t\tevent_%s);\n",
1311 fac->name, fac->checksum, event->name);
1312 }
1313 fprintf(fd,"\n");
1314
1315
1316 /* For each trace */
1317 print_tabs(1, fd);
1318 fprintf(fd, "list_for_each_entry_rcu(trace, &ltt_traces.head, list) {\n");
1319 print_tabs(2, fd);
1320 fprintf(fd, "if(!trace->active) continue;\n\n");
1321
1322 if(event->per_trace) {
1323 print_tabs(2, fd);
1324 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
1325 }
1326
1327 print_tabs(2, fd);
1328 fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
1329 print_tabs(2, fd);
1330 fprintf(fd, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1331 fprintf(fd, "\n");
1332
1333
1334 /* Relay reserve */
1335 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1336 * return */
1337 print_tabs(2, fd);
1338 fprintf(fd, "slot_size = 0;\n");
1339 print_tabs(2, fd);
1340 fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1341 print_tabs(3, fd);
1342 fprintf(fd, "reserve_size, &slot_size, &tsc,\n");
1343 print_tabs(3, fd);
1344 fprintf(fd, "&before_hdr_pad, &after_hdr_pad);\n");
1345 /* If error, return */
1346 print_tabs(2, fd);
1347 fprintf(fd, "if(!buffer) return;\n\n");
1348
1349 /* write data : assume stack alignment is the same as struct alignment. */
1350
1351 for(unsigned int i=0;i<event->fields.position;i++){
1352 field_t *field = (field_t*)(event->fields.array[i]);
1353 type_descriptor_t *type = field->type;
1354
1355 /* Set from */
1356 print_tabs(2, fd);
1357 switch(type->type) {
1358 case SEQUENCE:
1359 case UNION:
1360 case ARRAY:
1361 case STRUCT:
1362 case STRING:
1363 fprintf(fd, "from = %s;\n", field->name);
1364 break;
1365 default:
1366 fprintf(fd, "from = &%s;\n", field->name);
1367 break;
1368 }
1369
1370
1371 if(print_type_write(type,
1372 fd, 2, basename, field->name, "")) return 1;
1373 fprintf(fd, "\n");
1374
1375 /* Don't forget to flush pending memcpy */
1376 print_tabs(2, fd);
1377 fprintf(fd, "/* Flush pending memcpy */\n");
1378 print_tabs(2, fd);
1379 fprintf(fd, "if(len != 0) {\n");
1380 print_tabs(3, fd);
1381 fprintf(fd, "memcpy(buffer+to_base+to, from, len);\n");
1382 print_tabs(3, fd);
1383 fprintf(fd, "to += len;\n");
1384 //print_tabs(3, fd);
1385 //fprintf(fd, "from += len;\n");
1386 print_tabs(3, fd);
1387 fprintf(fd, "len = 0;\n");
1388 print_tabs(2, fd);
1389 fprintf(fd, "}\n");
1390 fprintf(fd, "\n");
1391 }
1392
1393
1394 /* commit */
1395 print_tabs(2, fd);
1396 fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
1397
1398 print_tabs(1, fd);
1399 fprintf(fd, "}\n\n");
1400
1401 /* Release locks */
1402 print_tabs(1, fd);
1403 fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n");
1404 print_tabs(1, fd);
1405 fprintf(fd, "preempt_enable_no_resched();\n");
1406
1407 fprintf(fd, "}\n");
1408 fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1409 fac->capname);
1410 return 0;
1411 }
1412
1413
1414 /* ltt-facility-name.h : main logging header.
1415 * log_header */
1416
1417 void print_log_header_head(facility_t *fac, FILE *fd)
1418 {
1419 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
1420 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
1421 fprintf(fd, "#include <linux/types.h>\n");
1422 fprintf(fd, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac->name);
1423 fprintf(fd, "#include <linux/ltt-core.h>\n");
1424 fprintf(fd, "\n");
1425 }
1426
1427
1428
1429 int print_log_header_types(facility_t *fac, FILE *fd)
1430 {
1431 sequence_t *types = &fac->named_types.values;
1432 fprintf(fd, "/* Named types */\n");
1433 fprintf(fd, "\n");
1434
1435 for(unsigned int i = 0; i < types->position; i++) {
1436 /* For each named type, print the definition */
1437 if((print_type_declaration(types->array[i], fd,
1438 0, "", ""))) return 1;
1439 /* Print also the align function */
1440 if((print_type_alignment_fct(types->array[i], fd,
1441 0, "", ""))) return 1;
1442 /* Print also the write function */
1443 if((print_type_write_fct(types->array[i], fd,
1444 0, "", ""))) return 1;
1445 }
1446 return 0;
1447 }
1448
1449 int print_log_header_events(facility_t *fac, FILE *fd)
1450 {
1451 sequence_t *events = &fac->events;
1452 char basename[PATH_MAX];
1453 unsigned int facname_len;
1454
1455 strncpy(basename, fac->name, PATH_MAX);
1456 facname_len = strlen(basename);
1457 strncat(basename, "_", PATH_MAX-facname_len);
1458 facname_len = strlen(basename);
1459
1460 for(unsigned int i = 0; i < events->position; i++) {
1461 event_t *event = (event_t*)events->array[i];
1462 strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len);
1463
1464 /* For each event, print structure, and then logging function */
1465 fprintf(fd, "/* Event %s structures */\n",
1466 event->name);
1467 for(unsigned int j = 0; j < event->fields.position; j++) {
1468 /* For each unnamed type, print the definition */
1469 field_t *f = (field_t*)event->fields.array[j];
1470 type_descriptor_t *t = f->type;
1471 if(t->type_name == NULL) {
1472 if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
1473 /* Print also the align function */
1474 if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1;
1475 /* Print also the write function */
1476 if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1;
1477 }
1478 }
1479
1480 fprintf(fd, "\n");
1481
1482 fprintf(fd, "/* Event %s logging function */\n",
1483 event->name);
1484
1485 if(print_event_logging_function(basename, fac, event, fd)) return 1;
1486
1487 fprintf(fd, "\n");
1488 }
1489
1490 return 0;
1491 }
1492
1493
1494 void print_log_header_tail(facility_t *fac, FILE *fd)
1495 {
1496 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
1497 }
1498
1499 int print_log_header(facility_t *fac)
1500 {
1501 char filename[PATH_MAX];
1502 unsigned int filename_size = 0;
1503 FILE *fd;
1504 dprintf("%s\n", fac->name);
1505
1506 strcpy(filename, "ltt-facility-");
1507 filename_size = strlen(filename);
1508
1509 strncat(filename, fac->name, PATH_MAX - filename_size);
1510 filename_size = strlen(filename);
1511
1512 strncat(filename, ".h", PATH_MAX - filename_size);
1513 filename_size = strlen(filename);
1514
1515
1516 fd = fopen(filename, "w");
1517 if(fd == NULL) {
1518 printf("Error opening file %s for writing : %s\n",
1519 filename, strerror(errno));
1520 return errno;
1521 }
1522
1523 /* Print file head */
1524 print_log_header_head(fac, fd);
1525
1526 /* print named types in declaration order */
1527 if(print_log_header_types(fac, fd)) return 1;
1528
1529 /* Print events */
1530 if(print_log_header_events(fac, fd)) return 1;
1531
1532 /* Print file tail */
1533 print_log_header_tail(fac, fd);
1534
1535
1536 fclose(fd);
1537
1538 return 0;
1539 }
1540
1541
1542 /* ltt-facility-id-name.h : facility id.
1543 * log_id_header */
1544 int print_id_header(facility_t *fac)
1545 {
1546 char filename[PATH_MAX];
1547 unsigned int filename_size = 0;
1548 FILE *fd;
1549 char basename[PATH_MAX];
1550 char basename_len = 0;
1551
1552 dprintf("%s\n", fac->name);
1553
1554 strcpy(filename, "ltt-facility-id-");
1555 filename_size = strlen(filename);
1556
1557 strncat(filename, fac->name, PATH_MAX - filename_size);
1558 filename_size = strlen(filename);
1559
1560 strncat(filename, ".h", PATH_MAX - filename_size);
1561 filename_size = strlen(filename);
1562
1563
1564 fd = fopen(filename, "w");
1565 if(fd == NULL) {
1566 printf("Error opening file %s for writing : %s\n",
1567 filename, strerror(errno));
1568 return errno;
1569 }
1570
1571 fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
1572 fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
1573 fprintf(fd, "#ifdef CONFIG_LTT\n");
1574
1575 fprintf(fd,"#include <linux/ltt-facilities.h>\n\n");
1576
1577 fprintf(fd,"/**** facility handle ****/\n\n");
1578 fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n",
1579 fac->name, fac->checksum);
1580 fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name);
1581
1582 strncpy(basename, fac->name, PATH_MAX);
1583 basename_len = strlen(basename);
1584 strncat(basename, "_", PATH_MAX - basename_len);
1585 basename_len++;
1586
1587 fprintf(fd,"/**** event index ****/\n\n");
1588 fprintf(fd,"enum %s_event {\n",fac->name);
1589
1590 for(unsigned int i = 0; i < fac->events.position; i++) {
1591 event_t *event = (event_t*)fac->events.array[i];
1592 strncpy(basename+basename_len, event->name, PATH_MAX-basename_len);
1593 print_tabs(1, fd);
1594 fprintf(fd, "event_%s,\n", basename);
1595 }
1596 print_tabs(1, fd);
1597 fprintf(fd, "facility_%s_num_events\n", fac->name);
1598 fprintf(fd, "};\n");
1599 fprintf(fd, "\n");
1600
1601
1602 fprintf(fd, "#endif //CONFIG_LTT\n");
1603 fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
1604
1605
1606 fclose(fd);
1607
1608 return 0;
1609 }
1610
1611
1612 /* ltt-facility-loader-name.h : facility specific loader info.
1613 * loader_header */
1614 int print_loader_header(facility_t *fac)
1615 {
1616 char filename[PATH_MAX];
1617 unsigned int filename_size = 0;
1618 FILE *fd;
1619 dprintf("%s\n", fac->name);
1620
1621 strcpy(filename, "ltt-facility-loader-");
1622 filename_size = strlen(filename);
1623
1624 strncat(filename, fac->name, PATH_MAX - filename_size);
1625 filename_size = strlen(filename);
1626
1627 strncat(filename, ".h", PATH_MAX - filename_size);
1628 filename_size = strlen(filename);
1629
1630
1631 fd = fopen(filename, "w");
1632 if(fd == NULL) {
1633 printf("Error opening file %s for writing : %s\n",
1634 filename, strerror(errno));
1635 return errno;
1636 }
1637
1638 fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
1639 fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname);
1640 fprintf(fd, "#ifdef CONFIG_LTT\n\n");
1641 fprintf(fd,"#include <linux/ltt-facilities.h>\n");
1642 fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
1643 fac->name);
1644 fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name);
1645 fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
1646 fac->name, fac->checksum);
1647
1648 fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
1649 fac->name);
1650 fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
1651 fac->name, fac->checksum);
1652 fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum);
1653 fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name);
1654 fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
1655 fac->name);
1656 fprintf(fd, "#endif //CONFIG_LTT\n\n");
1657 fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
1658
1659 fclose(fd);
1660
1661 return 0;
1662 }
1663
1664 /* ltt-facility-loader-name.c : generic faciilty loader
1665 * loader_c */
1666 int print_loader_c(facility_t *fac)
1667 {
1668 char filename[PATH_MAX];
1669 unsigned int filename_size = 0;
1670 FILE *fd;
1671 dprintf("%s\n", fac->name);
1672
1673 strcpy(filename, "ltt-facility-loader-");
1674 filename_size = strlen(filename);
1675
1676 strncat(filename, fac->name, PATH_MAX - filename_size);
1677 filename_size = strlen(filename);
1678
1679 strncat(filename, ".c", PATH_MAX - filename_size);
1680 filename_size = strlen(filename);
1681
1682
1683 fd = fopen(filename, "w");
1684 if(fd == NULL) {
1685 printf("Error opening file %s for writing : %s\n",
1686 filename, strerror(errno));
1687 return errno;
1688 }
1689
1690 fprintf(fd, "/*\n");
1691 fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name);
1692 fprintf(fd, " *\n");
1693 fprintf(fd, " * (C) Copyright 2005 - \n");
1694 fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
1695 fprintf(fd, " *\n");
1696 fprintf(fd, " * Contains the LTT facility loader.\n");
1697 fprintf(fd, " *\n");
1698 fprintf(fd, " */\n");
1699 fprintf(fd, "\n");
1700 fprintf(fd, "\n");
1701 fprintf(fd, "#include <linux/ltt-facilities.h>\n");
1702 fprintf(fd, "#include <linux/module.h>\n");
1703 fprintf(fd, "#include <linux/init.h>\n");
1704 fprintf(fd, "#include <linux/config.h>\n");
1705 fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name);
1706 fprintf(fd, "\n");
1707 fprintf(fd, "\n");
1708 fprintf(fd, "#ifdef CONFIG_LTT\n");
1709 fprintf(fd, "\n");
1710 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
1711 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
1712 fprintf(fd, "\n");
1713 fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
1714 fprintf(fd, "\n");
1715 fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n");
1716 fprintf(fd, "\n");
1717 fprintf(fd, "static struct ltt_facility facility = {\n");
1718 fprintf(fd, "\t.name = ltt_facility_name,\n");
1719 fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
1720 fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
1721 fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
1722 fprintf(fd, "};\n");
1723 fprintf(fd, "\n");
1724 fprintf(fd, "#ifndef MODULE\n");
1725 fprintf(fd, "\n");
1726 fprintf(fd, "/* Built-in facility. */\n");
1727 fprintf(fd, "\n");
1728 fprintf(fd, "static int __init facility_init(void)\n");
1729 fprintf(fd, "{\n");
1730 fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name);
1731 fprintf(fd, "\n");
1732 fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
1733 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1734 fprintf(fd, "\t\n");
1735 fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
1736 fprintf(fd, "}\n");
1737 fprintf(fd, "__initcall(facility_init);\n");
1738 fprintf(fd, "\n");
1739 fprintf(fd, "\n");
1740 fprintf(fd, "\n");
1741 fprintf(fd, "#else \n");
1742 fprintf(fd, "\n");
1743 fprintf(fd, "/* Dynamic facility. */\n");
1744 fprintf(fd, "\n");
1745 fprintf(fd, "static int __init facility_init(void)\n");
1746 fprintf(fd, "{\n");
1747 fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac->name);
1748 fprintf(fd, "\n");
1749 fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
1750 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1751 fprintf(fd, "\n");
1752 fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
1753 fprintf(fd, "}\n");
1754 fprintf(fd, "\n");
1755 fprintf(fd, "static void __exit facility_exit(void)\n");
1756 fprintf(fd, "{\n");
1757 fprintf(fd, "\tint err;\n");
1758 fprintf(fd, "\n");
1759 fprintf(fd, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
1760 fprintf(fd, "\tif(err != 0)\n");
1761 fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
1762 fprintf(fd, "\n");
1763 fprintf(fd, "}\n");
1764 fprintf(fd, "\n");
1765 fprintf(fd, "module_init(facility_init)\n");
1766 fprintf(fd, "module_exit(facility_exit)\n");
1767 fprintf(fd, "\n");
1768 fprintf(fd, "\n");
1769 fprintf(fd, "MODULE_LICENSE(\"GPL\");\n");
1770 fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
1771 fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
1772 fprintf(fd, "\n");
1773 fprintf(fd, "#endif //MODULE\n");
1774 fprintf(fd, "\n");
1775 fprintf(fd, "#endif //CONFIG_LTT\n");
1776
1777 fclose(fd);
1778
1779 return 0;
1780 }
1781
1782
1783
1784 /* open facility */
1785 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
1786
1787 /*****************************************************************************
1788 *Function name
1789 * ltt_facility_open : open facilities
1790 *Input params
1791 * pathname : the path name of the facility
1792 *
1793 * Open the facility corresponding to the right checksum.
1794 *
1795 *returns the facility on success, NULL on error.
1796 ****************************************************************************/
1797 facility_t *ltt_facility_open(char * pathname)
1798 {
1799 int ret = 0;
1800 char *token;
1801 parse_file_t in;
1802 facility_t * fac = NULL;
1803 char buffer[BUFFER_SIZE];
1804 int generated = FALSE;
1805
1806 in.buffer = &(buffer[0]);
1807 in.lineno = 0;
1808 in.error = error_callback;
1809 in.name = pathname;
1810 in.unget = 0;
1811
1812 in.fp = fopen(in.name, "r");
1813 if(in.fp == NULL) {
1814 ret = 1;
1815 goto open_error;
1816 }
1817
1818 while(1){
1819 token = getToken(&in);
1820 if(in.type == ENDFILE) break;
1821
1822 if(generated) {
1823 printf("More than one facility in the file. Only using the first one.\n");
1824 break;
1825 }
1826
1827 if(strcmp(token, "<")) in.error(&in,"not a facility file");
1828 token = getName(&in);
1829
1830 if(strcmp("facility",token) == 0) {
1831 fac = malloc(sizeof(facility_t));
1832 fac->name = NULL;
1833 fac->description = NULL;
1834 sequence_init(&(fac->events));
1835 table_init(&(fac->named_types));
1836 sequence_init(&(fac->unnamed_types));
1837
1838 parseFacility(&in, fac);
1839
1840 //check if any namedType is not defined
1841 checkNamedTypesImplemented(&fac->named_types);
1842
1843 generateChecksum(fac->name, &fac->checksum, &fac->events);
1844
1845 generated = TRUE;
1846 }
1847 else {
1848 printf("facility token was expected in file %s\n", in.name);
1849 ret = 1;
1850 goto parse_error;
1851 }
1852 }
1853
1854 parse_error:
1855 fclose(in.fp);
1856 open_error:
1857
1858 if(!generated) {
1859 printf("Cannot find facility %s\n", pathname);
1860 fac = NULL;
1861 }
1862
1863 return fac;
1864 }
1865
1866 /* Close the facility */
1867 void ltt_facility_close(facility_t *fac)
1868 {
1869 free(fac->name);
1870 free(fac->capname);
1871 free(fac->description);
1872 freeEvents(&fac->events);
1873 sequence_dispose(&fac->events);
1874 freeNamedType(&fac->named_types);
1875 table_dispose(&fac->named_types);
1876 freeTypes(&fac->unnamed_types);
1877 sequence_dispose(&fac->unnamed_types);
1878 free(fac);
1879 }
1880
1881
1882 /* Show help */
1883 void show_help(int argc, char ** argv)
1884 {
1885 printf("Genevent help : \n");
1886 printf("\n");
1887 printf("Use %s name.xml\n", argv[0]);
1888 printf("to create :\n");
1889 printf("ltt-facility-name.h\n");
1890 printf("ltt-facility-id-name.h\n");
1891 printf("ltt-facility-loader-name.h\n");
1892 printf("ltt-facility-loader-name.c\n");
1893 printf("In the current directory.\n");
1894 printf("\n");
1895 }
1896
1897 /* Parse program arguments */
1898 /* Return values :
1899 * 0 : continue program
1900 * -1 : stop program, return 0
1901 * > 0 : stop program, return value as exit.
1902 */
1903 int check_args(int argc, char **argv)
1904 {
1905 if(argc < 2) {
1906 printf("Not enough arguments\n");
1907 show_help(argc, argv);
1908 return EINVAL;
1909 }
1910
1911 if(strcmp(argv[1], "-h") == 0) {
1912 show_help(argc, argv);
1913 return -1;
1914 }
1915
1916 return 0;
1917 }
1918
1919 int main(int argc, char **argv)
1920 {
1921 int err = 0;
1922 facility_t *fac;
1923
1924 err = check_args(argc, argv);
1925 if(err > 0) return err;
1926 else if(err < 0) return 0;
1927
1928 /* open the facility */
1929 fac = ltt_facility_open(argv[1]);
1930 if(fac == NULL) {
1931 printf("Error opening file %s for reading : %s\n",
1932 argv[1], strerror(errno));
1933 return errno;
1934 }
1935
1936 /* generate the output C files */
1937
1938
1939 /* ltt-facility-name.h : main logging header.
1940 * log_header */
1941 err = print_log_header(fac);
1942 if(err) return err;
1943
1944 /* ltt-facility-id-name.h : facility id.
1945 * log_id_header */
1946 err = print_id_header(fac);
1947 if(err) return err;
1948
1949 /* ltt-facility-loader-name.h : facility specific loader info.
1950 * loader_header */
1951 err = print_loader_header(fac);
1952 if(err) return err;
1953
1954 /* ltt-facility-loader-name.c : generic faciilty loader
1955 * loader_c */
1956 err = print_loader_c(fac);
1957 if(err) return err;
1958
1959 /* close the facility */
1960 ltt_facility_close(fac);
1961
1962 return 0;
1963 }
1964
1965
This page took 0.126547 seconds and 4 git commands to generate.