fix usertrace and genevent for printf alignment
[lttv.git] / genevent / 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
90 enum user_fct_types { USER_FCT_PROTO, USER_FCT_DECLARATION } ;
91
92 /* Code printing */
93
94 void print_tabs(unsigned int tabs, FILE *fd)
95 {
96 for(unsigned int i = 0; i<tabs;i++)
97 fprintf(fd, "\t");
98 }
99
100 /* print type.
101 *
102 * Copied from construct_types_and_fields in LTTV facility.c */
103
104 int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs,
105 char *nest_name, char *field_name)
106 {
107 char basename[PATH_MAX];
108 unsigned int basename_len = 0;
109
110 strcpy(basename, nest_name);
111 basename_len = strlen(basename);
112
113 /* For a named type, we use the type_name directly */
114 if(td->type_name != NULL) {
115 strncpy(basename, td->type_name, PATH_MAX);
116 basename_len = strlen(basename);
117 } else {
118 /* For a unnamed type, there must be a field name */
119 if((basename_len != 0)
120 && (basename[basename_len-1] != '_')
121 && (field_name[0] != '\0')) {
122 strncat(basename, "_", PATH_MAX - basename_len);
123 basename_len = strlen(basename);
124 }
125 strncat(basename, field_name, PATH_MAX - basename_len);
126 }
127
128 switch(td->type) {
129 case INT_FIXED:
130 fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]);
131 break;
132 case UINT_FIXED:
133 fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]);
134 break;
135 case CHAR:
136 fprintf(fd, "signed char");
137 break;
138 case UCHAR:
139 fprintf(fd, "unsigned char");
140 break;
141 case SHORT:
142 fprintf(fd, "short");
143 break;
144 case USHORT:
145 fprintf(fd, "unsigned short");
146 break;
147 case INT:
148 fprintf(fd, "int");
149 break;
150 case UINT:
151 fprintf(fd, "unsigned int");
152 break;
153 case FLOAT:
154 fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]);
155 break;
156 case POINTER:
157 fprintf(fd, "const void *");
158 break;
159 case LONG:
160 fprintf(fd, "long");
161 break;
162 case ULONG:
163 fprintf(fd, "unsigned long");
164 break;
165 case SIZE_T:
166 fprintf(fd, "size_t");
167 break;
168 case SSIZE_T:
169 fprintf(fd, "ssize_t");
170 break;
171 case OFF_T:
172 fprintf(fd, "off_t");
173 break;
174 case STRING:
175 fprintf(fd, "const char *");
176 break;
177 case ENUM:
178 fprintf(fd, "enum lttng_%s", basename);
179 break;
180 case ARRAY:
181 fprintf(fd, "lttng_array_%s", basename);
182 break;
183 case SEQUENCE:
184 fprintf(fd, "lttng_sequence_%s", basename);
185 break;
186 case STRUCT:
187 fprintf(fd, "struct lttng_%s", basename);
188 break;
189 case UNION:
190 fprintf(fd, "union lttng_%s", basename);
191 break;
192 default:
193 printf("print_type : unknown type\n");
194 return 1;
195 }
196
197 return 0;
198 }
199
200 /* Print logging function argument */
201 int print_arg(type_descriptor_t * td, FILE *fd, unsigned int tabs,
202 char *nest_name, char *field_name)
203 {
204 char basename[PATH_MAX];
205 unsigned int basename_len = 0;
206
207 strcpy(basename, nest_name);
208 basename_len = strlen(basename);
209
210 /* For a named type, we use the type_name directly */
211 if(td->type_name != NULL) {
212 strncpy(basename, td->type_name, PATH_MAX);
213 basename_len = strlen(basename);
214 } else {
215 /* For a unnamed type, there must be a field name */
216 if((basename_len != 0)
217 && (basename[basename_len-1] != '_')
218 && (field_name[0] != '\0')) {
219 strncat(basename, "_", PATH_MAX - basename_len);
220 basename_len = strlen(basename);
221 }
222 strncat(basename, field_name, PATH_MAX - basename_len);
223 }
224
225 print_tabs(tabs, fd);
226
227 switch(td->type) {
228 case INT_FIXED:
229 fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]);
230 fprintf(fd, " lttng_param_%s", field_name);
231 break;
232 case UINT_FIXED:
233 fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]);
234 fprintf(fd, " lttng_param_%s", field_name);
235 break;
236 case CHAR:
237 fprintf(fd, "signed char");
238 fprintf(fd, " lttng_param_%s", field_name);
239 break;
240 case UCHAR:
241 fprintf(fd, "unsigned char");
242 fprintf(fd, " lttng_param_%s", field_name);
243 break;
244 case SHORT:
245 fprintf(fd, "short");
246 fprintf(fd, " lttng_param_%s", field_name);
247 break;
248 case USHORT:
249 fprintf(fd, "unsigned short");
250 fprintf(fd, " lttng_param_%s", field_name);
251 break;
252 case INT:
253 fprintf(fd, "int");
254 fprintf(fd, " lttng_param_%s", field_name);
255 break;
256 case UINT:
257 fprintf(fd, "unsigned int");
258 fprintf(fd, " lttng_param_%s", field_name);
259 break;
260 case FLOAT:
261 fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]);
262 fprintf(fd, " lttng_param_%s", field_name);
263 break;
264 case POINTER:
265 fprintf(fd, "const void *");
266 fprintf(fd, " lttng_param_%s", field_name);
267 break;
268 case LONG:
269 fprintf(fd, "long");
270 fprintf(fd, " lttng_param_%s", field_name);
271 break;
272 case ULONG:
273 fprintf(fd, "unsigned long");
274 fprintf(fd, " lttng_param_%s", field_name);
275 break;
276 case SIZE_T:
277 fprintf(fd, "size_t");
278 fprintf(fd, " lttng_param_%s", field_name);
279 break;
280 case SSIZE_T:
281 fprintf(fd, "ssize_t");
282 fprintf(fd, " lttng_param_%s", field_name);
283 break;
284 case OFF_T:
285 fprintf(fd, "off_t");
286 fprintf(fd, " lttng_param_%s", field_name);
287 break;
288 case STRING:
289 fprintf(fd, "const char *");
290 fprintf(fd, " lttng_param_%s", field_name);
291 break;
292 case ENUM:
293 fprintf(fd, "enum lttng_%s", basename);
294 fprintf(fd, " lttng_param_%s", field_name);
295 break;
296 case ARRAY:
297 fprintf(fd, "lttng_array_%s", basename);
298 fprintf(fd, " lttng_param_%s", field_name);
299 break;
300 case SEQUENCE:
301 fprintf(fd, "lttng_sequence_%s *", basename);
302 fprintf(fd, " lttng_param_%s", field_name);
303 break;
304 case STRUCT:
305 fprintf(fd, "struct lttng_%s *", basename);
306 fprintf(fd, " lttng_param_%s", field_name);
307 break;
308 case UNION:
309 fprintf(fd, "union lttng_%s *", basename);
310 fprintf(fd, " lttng_param_%s", field_name);
311 break;
312 default:
313 printf("print_type : unknown type\n");
314 return 1;
315 }
316
317 return 0;
318 }
319
320
321 /* Does the type has a fixed size ? (as know from the compiler)
322 *
323 * 1 : fixed size
324 * 0 : variable length
325 */
326 int has_type_fixed_size(type_descriptor_t *td)
327 {
328 switch(td->type) {
329 case INT_FIXED:
330 case UINT_FIXED:
331 case CHAR:
332 case UCHAR:
333 case SHORT:
334 case USHORT:
335 case INT:
336 case UINT:
337 case FLOAT:
338 case POINTER:
339 case LONG:
340 case ULONG:
341 case SIZE_T:
342 case SSIZE_T:
343 case OFF_T:
344 case ENUM:
345 case UNION: /* The union must have fixed size children. Must be checked by
346 the parser */
347 return 1;
348 break;
349 case STRING:
350 case SEQUENCE:
351 return 0;
352 break;
353 case STRUCT:
354 {
355 int has_type_fixed = 0;
356 for(unsigned int i=0;i<td->fields.position;i++){
357 field_t *field = (field_t*)(td->fields.array[i]);
358 type_descriptor_t *type = field->type;
359
360 has_type_fixed = has_type_fixed_size(type);
361 if(!has_type_fixed) return 0;
362 }
363 return 1;
364 }
365 break;
366 case ARRAY:
367 assert(td->size >= 0);
368 return has_type_fixed_size(((field_t*)td->fields.array[0])->type);
369 break;
370 case NONE:
371 printf("There is a type defined to NONE : bad.\n");
372 assert(0);
373 break;
374 }
375 return 0; //make gcc happy.
376 }
377
378
379
380
381
382 /* print type declaration.
383 *
384 * Copied from construct_types_and_fields in LTTV facility.c */
385
386 int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs,
387 char *nest_name, char *field_name)
388 {
389 char basename[PATH_MAX];
390 unsigned int basename_len = 0;
391
392 if(td->custom_write) return 0; /* Does print custom type */
393
394 strncpy(basename, nest_name, PATH_MAX);
395 basename_len = strlen(basename);
396
397 /* For a named type, we use the type_name directly */
398 if(td->type_name != NULL) {
399 strncpy(basename, td->type_name, PATH_MAX);
400 basename_len = strlen(basename);
401 } else {
402 /* For a unnamed type, there must be a field name, except for
403 * the array. */
404 if((basename_len != 0)
405 && (basename[basename_len-1] != '_'
406 && (field_name[0] != '\0'))) {
407 strncat(basename, "_", PATH_MAX - basename_len);
408 basename_len = strlen(basename);
409 }
410 strncat(basename, field_name, PATH_MAX - basename_len);
411 }
412
413 switch(td->type) {
414 case ENUM:
415 fprintf(fd, "enum lttng_%s", basename);
416 fprintf(fd, " {\n");
417 for(unsigned int i=0;i<td->labels.position;i++){
418 print_tabs(1, fd);
419 fprintf(fd, "LTTNG_%s = %d", ((char*)td->labels.array[i]),
420 (*(int*)td->labels_values.array[i]));
421 fprintf(fd, ",\n");
422 }
423 fprintf(fd, "};\n");
424 fprintf(fd, "\n");
425 break;
426
427 case ARRAY:
428 dprintf("%s\n", basename);
429 assert(td->size >= 0);
430 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
431 /* Not a named nested type : we must print its declaration first */
432 if(print_type_declaration(((field_t*)td->fields.array[0])->type,
433 fd, 0, basename, "")) return 1;
434 }
435 fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename,
436 td->size);
437 fprintf(fd, "typedef ");
438 if(print_type(((field_t*)td->fields.array[0])->type,
439 fd, tabs, basename, "")) return 1;
440 fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename,
441 basename);
442 fprintf(fd, "\n");
443 break;
444 case SEQUENCE:
445 /* We assume that the sequence length type does not need to be declared.
446 */
447 if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
448 /* Not a named nested type : we must print its declaration first */
449 if(print_type_declaration(((field_t*)td->fields.array[1])->type,
450 fd, 0, basename, "")) return 1;
451 }
452 fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
453 basename,
454 basename);
455 fprintf(fd, "struct lttng_sequence_%s", basename);
456 fprintf(fd, " {\n");
457 print_tabs(1, fd);
458 if(print_type(((field_t*)td->fields.array[0])->type,
459 fd, tabs, basename, "")) return 1;
460 fprintf(fd, " len;\n");
461 print_tabs(1, fd);
462 fprintf(fd, "const ");
463 if(print_type(((field_t*)td->fields.array[1])->type,
464 fd, tabs, basename, "")) return 1;
465 fprintf(fd, " *array;\n");
466 fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy
467 it to the buffer directly. */
468 fprintf(fd, "\n");
469 break;
470
471 case STRUCT:
472 for(unsigned int i=0;i<td->fields.position;i++){
473 field_t *field = (field_t*)(td->fields.array[i]);
474 type_descriptor_t *type = field->type;
475 if(type->type_name == NULL) {
476 /* Not a named nested type : we must print its declaration first */
477 if(print_type_declaration(type,
478 fd, 0, basename, field->name)) return 1;
479 }
480 }
481 fprintf(fd, "struct lttng_%s", basename);
482 fprintf(fd, " {\n");
483 for(unsigned int i=0;i<td->fields.position;i++){
484 field_t *field = (field_t*)(td->fields.array[i]);
485 type_descriptor_t *type = field->type;
486 print_tabs(1, fd);
487 if(print_type(type, fd, tabs, basename, field->name)) return 1;
488 fprintf(fd, " ");
489 fprintf(fd, "%s", field->name);
490 fprintf(fd, ";\n");
491 }
492 fprintf(fd, "} LTT_ALIGN;\n");
493 fprintf(fd, "\n");
494 break;
495 case UNION:
496 for(unsigned int i=0;i<td->fields.position;i++){
497 field_t *field = (field_t*)(td->fields.array[i]);
498 type_descriptor_t *type = field->type;
499 if(type->type_name == NULL) {
500 /* Not a named nested type : we must print its declaration first */
501 if(print_type_declaration(type,
502 fd, 0, basename, field->name)) return 1;
503 }
504 }
505 fprintf(fd, "union lttng_%s", basename);
506 fprintf(fd, " {\n");
507 for(unsigned i=0;i<td->fields.position;i++){
508 field_t *field = (field_t*)(td->fields.array[i]);
509 type_descriptor_t *type = field->type;
510 print_tabs(1, fd);
511 if(print_type(type, fd, tabs, basename, field->name)) return 1;
512 fprintf(fd, " ");
513 fprintf(fd, "%s", field->name);
514 fprintf(fd, ";\n");
515 }
516 fprintf(fd, "} LTT_ALIGN;\n");
517 fprintf(fd, "\n");
518 break;
519 default:
520 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
521 break;
522 }
523
524 return 0;
525 }
526
527
528 /* print type alignment.
529 *
530 * Copied from construct_types_and_fields in LTTV facility.c
531 *
532 * basename is the name which identifies the type (along with a prefix
533 * (possibly)). */
534
535 int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
536 char *nest_name, char *field_name, char *obj_prefix)
537 {
538 char basename[PATH_MAX];
539 unsigned int basename_len = 0;
540
541 strncpy(basename, nest_name, PATH_MAX);
542 basename_len = strlen(basename);
543
544 /* For a named type, we use the type_name directly */
545 if(td->type_name != NULL) {
546 strncpy(basename, td->type_name, PATH_MAX);
547 basename_len = strlen(basename);
548 } else {
549 /* For a unnamed type, there must be a field name, except for
550 * the array. */
551 if((basename_len != 0)
552 && (basename[basename_len-1] != '_'
553 && field_name != NULL
554 && (field_name[0] != '\0'))) {
555 strncat(basename, "_", PATH_MAX - basename_len);
556 basename_len = strlen(basename);
557 }
558 if(field_name != NULL)
559 strncat(basename, field_name, PATH_MAX - basename_len);
560 }
561
562 if(field_name[0] == '\0') {
563 /* We are in a write function : it's the obj that we must align. */
564 switch(td->type) {
565 case SEQUENCE:
566 fprintf(fd, "lttng_get_alignment_sequence_%s(%s)", basename,
567 obj_prefix);
568 break;
569 case STRUCT:
570 fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename,
571 obj_prefix);
572 break;
573 case UNION:
574 fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename,
575 obj_prefix);
576 break;
577 case ARRAY:
578 fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename,
579 obj_prefix);
580 case STRING:
581 fprintf(fd, "sizeof(char)");
582 break;
583 case INT_FIXED:
584 case UINT_FIXED:
585 case CHAR:
586 case UCHAR:
587 case SHORT:
588 case USHORT:
589 case INT:
590 case UINT:
591 case FLOAT:
592 case POINTER:
593 case LONG:
594 case ULONG:
595 case SIZE_T:
596 case SSIZE_T:
597 case OFF_T:
598 case ENUM:
599 fprintf(fd, "sizeof(");
600 if(print_type(td, fd, 0, basename, "")) return 1;
601 fprintf(fd, ")");
602 break;
603
604 default:
605 printf("error : type unexpected\n");
606 return 1;
607 break;
608 }
609 } else {
610
611 switch(td->type) {
612 case INT_FIXED:
613 case UINT_FIXED:
614 case CHAR:
615 case UCHAR:
616 case SHORT:
617 case USHORT:
618 case INT:
619 case UINT:
620 case FLOAT:
621 case POINTER:
622 case LONG:
623 case ULONG:
624 case SIZE_T:
625 case SSIZE_T:
626 case OFF_T:
627 case ENUM:
628 fprintf(fd, "sizeof(");
629 if(print_type(td, fd, 0, basename, "")) return 1;
630 fprintf(fd, ")");
631 break;
632 case STRING:
633 fprintf(fd, "sizeof(char)");
634 break;
635 case SEQUENCE:
636 fprintf(fd, "lttng_get_alignment_sequence_%s(&%s%s)", basename,
637 obj_prefix, field_name);
638 break;
639 case STRUCT:
640 fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename,
641 obj_prefix, field_name);
642 break;
643 case UNION:
644 fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename,
645 obj_prefix, field_name);
646 break;
647 case ARRAY:
648 fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename,
649 obj_prefix, field_name);
650 break;
651 case NONE:
652 printf("error : type NONE unexpected\n");
653 return 1;
654 break;
655 }
656 }
657
658 return 0;
659 }
660
661 /* print type write.
662 *
663 * Copied from construct_types_and_fields in LTTV facility.c
664 *
665 * basename is the name which identifies the type (along with a prefix
666 * (possibly)). */
667
668 int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
669 char *nest_name, char *field_name, char *obj_prefix, int get_ptr)
670 {
671 char basename[PATH_MAX];
672 unsigned int basename_len = 0;
673 char get_ptr_char[2] = "";
674 char custom[PATH_MAX] = "";
675
676 strncpy(basename, nest_name, PATH_MAX);
677 basename_len = strlen(basename);
678
679 /* For a named type, we use the type_name directly */
680 if(td->type_name != NULL) {
681 strncpy(basename, td->type_name, PATH_MAX);
682 basename_len = strlen(basename);
683 } else {
684 /* For a unnamed type, there must be a field name, except for
685 * the array. */
686 if((basename_len != 0)
687 && (basename[basename_len-1] != '_'
688 && (field_name[0] != '\0'))) {
689 strncat(basename, "_", PATH_MAX - basename_len);
690 basename_len = strlen(basename);
691 }
692 strncat(basename, field_name, PATH_MAX - basename_len);
693 }
694
695 if(get_ptr) {
696 strcpy(get_ptr_char, "&");
697 }
698
699 if(td->custom_write) {
700 strcpy(custom, "_custom");
701 }
702
703 switch(td->type) {
704 case INT_FIXED:
705 case UINT_FIXED:
706 case CHAR:
707 case UCHAR:
708 case SHORT:
709 case USHORT:
710 case INT:
711 case UINT:
712 case FLOAT:
713 case POINTER:
714 case LONG:
715 case ULONG:
716 case SIZE_T:
717 case SSIZE_T:
718 case OFF_T:
719 case ENUM:
720 print_tabs(tabs, fd);
721 fprintf(fd, "align = ");
722 if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
723 fprintf(fd, ";\n");
724 fprintf(fd, "\n");
725 print_tabs(tabs, fd);
726 fprintf(fd, "if(*len == 0) {\n");
727 print_tabs(tabs+1, fd);
728 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
729 print_tabs(tabs, fd);
730 fprintf(fd, "} else {\n");
731 print_tabs(tabs+1, fd);
732 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
733 print_tabs(tabs, fd);
734 fprintf(fd, "}\n");
735 fprintf(fd, "\n");
736
737 print_tabs(tabs, fd);
738 fprintf(fd, "*len += ");
739 fprintf(fd, "sizeof(");
740 if(print_type(td, fd, 0, basename, "")) return 1;
741 fprintf(fd, ");\n");
742
743 break;
744 case STRING:
745 print_tabs(tabs, fd);
746 fprintf(fd,
747 "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n",
748 custom, basename, obj_prefix, field_name);
749 break;
750 case SEQUENCE:
751 print_tabs(tabs, fd);
752 fprintf(fd,
753 "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
754 custom, basename, get_ptr_char, obj_prefix, field_name);
755 break;
756 case STRUCT:
757 print_tabs(tabs, fd);
758 fprintf(fd,
759 "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
760 custom, basename, get_ptr_char, obj_prefix, field_name);
761 break;
762 case UNION:
763 print_tabs(tabs, fd);
764 fprintf(fd,
765 "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);",
766 custom, basename, get_ptr_char, obj_prefix, field_name);
767 break;
768 case ARRAY:
769 print_tabs(tabs, fd);
770 fprintf(fd,
771 "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);",
772 custom, basename, obj_prefix, field_name);
773 break;
774 case NONE:
775 printf("Error : type NONE unexpected\n");
776 return 1;
777 break;
778 }
779
780 return 0;
781 }
782
783 /* print need local vars ?.
784 *
785 * Copied from print_type_write
786 *
787 * Does the type_write call needs local size and from variables ?
788 * return value : 1 yes, 0 no.
789 */
790
791 int has_type_local(type_descriptor_t * td)
792 {
793 switch(td->type) {
794 case INT_FIXED:
795 case UINT_FIXED:
796 case CHAR:
797 case UCHAR:
798 case SHORT:
799 case USHORT:
800 case INT:
801 case UINT:
802 case FLOAT:
803 case POINTER:
804 case LONG:
805 case ULONG:
806 case SIZE_T:
807 case SSIZE_T:
808 case OFF_T:
809 case ENUM:
810 return 1;
811 break;
812 case STRING:
813 case SEQUENCE:
814 case STRUCT:
815 case UNION:
816 case ARRAY:
817 return 0;
818 break;
819 case NONE:
820 printf("Error : type NONE unexpected\n");
821 return 1;
822 break;
823 }
824
825 return 0;
826 }
827
828
829
830 /* print type alignment function.
831 *
832 * Copied from construct_types_and_fields in LTTV facility.c
833 *
834 * basename is the name which identifies the type (along with a prefix
835 * (possibly)). */
836
837 int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
838 unsigned int tabs,
839 char *nest_name, char *field_name)
840 {
841 char basename[PATH_MAX];
842 unsigned int basename_len = 0;
843
844 if(td->custom_write) return 0; /* Does print custom type */
845
846 strncpy(basename, nest_name, PATH_MAX);
847 basename_len = strlen(basename);
848
849 /* For a named type, we use the type_name directly */
850 if(td->type_name != NULL) {
851 strncpy(basename, td->type_name, PATH_MAX);
852 basename_len = strlen(basename);
853 } else {
854 /* For a unnamed type, there must be a field name, except for
855 * the array. */
856 if((basename_len != 0)
857 && (basename[basename_len-1] != '_'
858 && (field_name[0] != '\0'))) {
859 strncat(basename, "_", PATH_MAX - basename_len);
860 basename_len = strlen(basename);
861 }
862 strncat(basename, field_name, PATH_MAX - basename_len);
863 }
864
865 switch(td->type) {
866 case SEQUENCE:
867 if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
868 /* Not a named nested type : we must print its align fct */
869 if(print_type_alignment_fct(((field_t*)td->fields.array[1])->type, fd,
870 0, basename, "")) return 1;
871 }
872 /* Function header */
873 fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
874 basename);
875 print_tabs(2, fd);
876 if(print_type(td, fd, 0, basename, "")) return 1;
877 fprintf(fd, " *obj)\n");
878 fprintf(fd, "{\n");
879 print_tabs(1, fd);
880 fprintf(fd, "size_t align=0, localign;");
881 fprintf(fd, "\n");
882 print_tabs(1, fd);
883 fprintf(fd, "localign = ");
884 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
885 fd, 0, basename, "len", "obj->")) return 1;
886 fprintf(fd, ";\n");
887 print_tabs(1, fd);
888 fprintf(fd, "align = max(align, localign);\n");
889 fprintf(fd, "\n");
890 print_tabs(1, fd);
891 fprintf(fd, "localign = ");
892 if(print_type_alignment(((field_t*)td->fields.array[1])->type,
893 fd, 0, basename, "array[0]", "obj->")) return 1;
894 fprintf(fd, ";\n");
895 print_tabs(1, fd);
896 fprintf(fd, "align = max(align, localign);\n");
897 fprintf(fd, "\n");
898 print_tabs(1, fd);
899 fprintf(fd, "return align;\n");
900 break;
901 case STRUCT:
902 for(unsigned int i=0;i<td->fields.position;i++){
903 field_t *field = (field_t*)(td->fields.array[i]);
904 type_descriptor_t *type = field->type;
905 if(type->type_name == NULL) {
906 /* Not a named nested type : we must print its align fct */
907 if(print_type_alignment_fct(type, fd,
908 0, basename, field->name)) return 1;
909 }
910 }
911 /* Function header */
912 fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
913 basename);
914 print_tabs(2, fd);
915 if(print_type(td, fd, 0, basename, "")) return 1;
916 fprintf(fd, " *obj)\n");
917 fprintf(fd, "{\n");
918 print_tabs(1, fd);
919 fprintf(fd, "size_t align=0, localign;");
920 fprintf(fd, "\n");
921 for(unsigned int i=0;i<td->fields.position;i++){
922 field_t *field = (field_t*)(td->fields.array[i]);
923 type_descriptor_t *type = field->type;
924 print_tabs(1, fd);
925 fprintf(fd, "localign = ");
926 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
927 return 1;
928 fprintf(fd, ";\n");
929 print_tabs(1, fd);
930 fprintf(fd, "align = max(align, localign);\n");
931 fprintf(fd, "\n");
932 }
933 print_tabs(1, fd);
934 fprintf(fd, "return align;\n");
935
936 break;
937 case UNION:
938 for(unsigned int i=0;i<td->fields.position;i++){
939 field_t *field = (field_t*)(td->fields.array[i]);
940 type_descriptor_t *type = field->type;
941 if(type->type_name == NULL) {
942 /* Not a named nested type : we must print its align fct */
943 if(print_type_alignment_fct(type, fd,
944 0, basename, field->name)) return 1;
945 }
946 }
947 /* Function header */
948 fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
949 basename);
950 print_tabs(2, fd);
951 if(print_type(td, fd, 0, basename, "")) return 1;
952 fprintf(fd, " *obj)\n");
953 fprintf(fd, "{\n");
954 print_tabs(1, fd);
955 fprintf(fd, "size_t align=0, localign;");
956 fprintf(fd, "\n");
957 for(unsigned int i=0;i<td->fields.position;i++){
958 field_t *field = (field_t*)(td->fields.array[i]);
959 type_descriptor_t *type = field->type;
960 print_tabs(1, fd);
961 fprintf(fd, "localign = ");
962 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
963 return 1;
964 fprintf(fd, ";\n");
965 print_tabs(1, fd);
966 fprintf(fd, "align = max(align, localign);\n");
967 fprintf(fd, "\n");
968 }
969 print_tabs(1, fd);
970 fprintf(fd, "return align;\n");
971
972 break;
973 case ARRAY:
974 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
975 /* Not a named nested type : we must print its align fct */
976 if(print_type_alignment_fct(((field_t*)td->fields.array[0])->type, fd,
977 0, basename, "")) return 1;
978 }
979 /* Function header */
980 fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
981 basename);
982 print_tabs(2, fd);
983 if(print_type(td, fd, 0, basename, "")) return 1;
984 fprintf(fd, " obj)\n");
985 fprintf(fd, "{\n");
986 print_tabs(1, fd);
987 fprintf(fd, "return \n");
988 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
989 fd, 0, basename, "", "obj[0]"))
990 return 1;
991 fprintf(fd, ";\n");
992 break;
993 default:
994 dprintf("print_type_alignment_fct : type has no alignment function.\n");
995 return 0;
996 break;
997 }
998
999
1000 /* Function footer */
1001 fprintf(fd, "}\n");
1002 fprintf(fd, "\n");
1003
1004 return 0;
1005 }
1006
1007 /* print type write function.
1008 *
1009 * Copied from construct_types_and_fields in LTTV facility.c
1010 *
1011 * basename is the name which identifies the type (along with a prefix
1012 * (possibly)). */
1013
1014 int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
1015 char *nest_name, char *field_name)
1016 {
1017 char basename[PATH_MAX];
1018 unsigned int basename_len = 0;
1019
1020 if(td->custom_write) return 0; /* Does print custom type */
1021
1022 strncpy(basename, nest_name, PATH_MAX);
1023 basename_len = strlen(basename);
1024
1025 /* For a named type, we use the type_name directly */
1026 if(td->type_name != NULL) {
1027 strncpy(basename, td->type_name, PATH_MAX);
1028 basename_len = strlen(basename);
1029 } else {
1030 /* For a unnamed type, there must be a field name, except for
1031 * the array. */
1032 if((basename_len != 0)
1033 && (basename[basename_len-1] != '_'
1034 && (field_name[0] != '\0'))) {
1035 strncat(basename, "_", PATH_MAX - basename_len);
1036 basename_len = strlen(basename);
1037 }
1038 strncat(basename, field_name, PATH_MAX - basename_len);
1039 }
1040
1041 switch(td->type) {
1042 case SEQUENCE:
1043 if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
1044 /* Not a named nested type : we must print its write fct */
1045 if(print_type_write_fct(((field_t*)td->fields.array[1])->type, fd,
1046 0, basename, "")) return 1;
1047 }
1048 break;
1049 case STRUCT:
1050 for(unsigned int i=0;i<td->fields.position;i++){
1051 field_t *field = (field_t*)(td->fields.array[i]);
1052 type_descriptor_t *type = field->type;
1053 if(type->type_name == NULL) {
1054 /* Not a named nested type : we must print its write fct */
1055 if(print_type_write_fct(type, fd,
1056 0, basename, field->name)) return 1;
1057 }
1058 }
1059 break;
1060 case UNION:
1061 for(unsigned int i=0;i<td->fields.position;i++){
1062 field_t *field = (field_t*)(td->fields.array[i]);
1063 type_descriptor_t *type = field->type;
1064 if(type->type_name == NULL) {
1065 /* Not a named nested type : we must print its write fct */
1066 if(print_type_write_fct(type, fd,
1067 0, basename, field->name)) return 1;
1068 }
1069 }
1070 break;
1071 case ARRAY:
1072 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
1073 /* Not a named nested type : we must print its write fct */
1074 if(print_type_write_fct(((field_t*)td->fields.array[0])->type, fd,
1075 0, basename, "")) return 1;
1076 }
1077 break;
1078 case STRING:
1079 break;
1080 default:
1081 dprintf("print_type_write_fct : type has no write function.\n");
1082 return 0;
1083 break;
1084 }
1085
1086 /* Print header */
1087 switch(td->type) {
1088 case SEQUENCE:
1089 fprintf(fd, "static inline void lttng_write_sequence_%s(\n",
1090 basename);
1091 break;
1092 case STRUCT:
1093 fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename);
1094 break;
1095 case UNION:
1096 fprintf(fd, "static inline void lttng_write_union_%s(\n", basename);
1097 break;
1098 case ARRAY:
1099 fprintf(fd, "static inline void lttng_write_array_%s(\n", basename);
1100 break;
1101 case STRING:
1102 fprintf(fd, "static inline void lttng_write_string_%s(\n", basename);
1103 break;
1104 default:
1105 printf("print_type_write_fct : type has no write function.\n");
1106 break;
1107 }
1108
1109 print_tabs(2, fd);
1110 fprintf(fd, "char *buffer,\n");
1111 print_tabs(2, fd);
1112 fprintf(fd, "size_t *to_base,\n");
1113 print_tabs(2, fd);
1114 fprintf(fd, "size_t *to,\n");
1115 print_tabs(2, fd);
1116 fprintf(fd, "const char **from,\n");
1117 print_tabs(2, fd);
1118 fprintf(fd, "size_t *len,\n");
1119 print_tabs(2, fd);
1120 if(print_type(td, fd, 0, basename, "")) return 1;
1121
1122 switch(td->type) {
1123 case SEQUENCE:
1124 fprintf(fd, " *obj)\n");
1125 break;
1126 case STRUCT:
1127 fprintf(fd, " *obj)\n");
1128 break;
1129 case UNION:
1130 fprintf(fd, " *obj)\n");
1131 break;
1132 case ARRAY:
1133 fprintf(fd, " obj)\n");
1134 break;
1135 case STRING:
1136 fprintf(fd, " obj)\n");
1137 break;
1138 default:
1139 printf("print_type_write_fct : type has no write function.\n");
1140 break;
1141 }
1142
1143 fprintf(fd, "{\n");
1144
1145 switch(td->type) {
1146 case STRING:
1147 print_tabs(1, fd);
1148 fprintf(fd, "size_t size;\n");
1149 break;
1150 default:
1151 break;
1152 }
1153
1154 print_tabs(1, fd);
1155 fprintf(fd, "size_t align;\n");
1156 fprintf(fd, "\n");
1157
1158 switch(td->type) {
1159 case SEQUENCE:
1160 case STRING:
1161 print_tabs(1, fd);
1162 fprintf(fd, "/* Flush pending memcpy */\n");
1163 print_tabs(1, fd);
1164 fprintf(fd, "if(*len != 0) {\n");
1165 print_tabs(2, fd);
1166 fprintf(fd, "if(buffer != NULL)\n");
1167 print_tabs(3, fd);
1168 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1169 print_tabs(1, fd);
1170 fprintf(fd, "}\n");
1171 print_tabs(1, fd);
1172 fprintf(fd, "*to += *len;\n");
1173 print_tabs(1, fd);
1174 fprintf(fd, "*len = 0;\n");
1175 fprintf(fd, "\n");
1176 break;
1177 case STRUCT:
1178 case UNION:
1179 case ARRAY:
1180 break;
1181 default:
1182 printf("print_type_write_fct : type has no write function.\n");
1183 break;
1184 }
1185
1186 print_tabs(1, fd);
1187 fprintf(fd, "align = ");
1188 if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
1189 fprintf(fd, ";\n");
1190 fprintf(fd, "\n");
1191 print_tabs(1, fd);
1192 fprintf(fd, "if(*len == 0) {\n");
1193 print_tabs(2, fd);
1194 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
1195 print_tabs(1, fd);
1196 fprintf(fd, "} else {\n");
1197 print_tabs(2, fd);
1198 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1199 print_tabs(1, fd);
1200 fprintf(fd, "}\n");
1201 fprintf(fd, "\n");
1202
1203 /* First, check if the type has a fixed size. If it is the case, then the size
1204 * to write is know by the compiler : simply use a sizeof() */
1205 if(has_type_fixed_size(td)) {
1206 print_tabs(1, fd);
1207 fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1208 fprintf(fd, "\n");
1209 print_tabs(1, fd);
1210 fprintf(fd, "*len += sizeof(");
1211 if(print_type(td, fd, 0, basename, field_name)) return 1;
1212 fprintf(fd, ");\n");
1213 } else {
1214 /* The type contains nested variable size subtypes :
1215 * we must write field by field. */
1216 print_tabs(1, fd);
1217 fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n");
1218 fprintf(fd, "\n");
1219
1220 switch(td->type) {
1221 case SEQUENCE:
1222 print_tabs(1, fd);
1223 fprintf(fd, "/* Copy members */\n");
1224 // print_tabs(1, fd);
1225 // fprintf(fd, "size = sizeof(\n");
1226 if(print_type_write(((field_t*)td->fields.array[0])->type,
1227 fd, 1, basename, "len", "obj->", 1)) return 1;
1228 fprintf(fd, "\n");
1229 // fprintf(fd, ");\n");
1230 // print_tabs(1, fd);
1231 // fprintf(fd, "*to += ltt_align(*to, size);\n");
1232 print_tabs(1, fd);
1233 fprintf(fd, "if(buffer != NULL)\n");
1234 print_tabs(2, fd);
1235 fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
1236 print_tabs(1, fd);
1237 fprintf(fd, "*to += *len;\n");
1238 print_tabs(1, fd);
1239 fprintf(fd, "*len = 0;\n");
1240 fprintf(fd, "\n");
1241
1242 /* Write the child : varlen child or not ? */
1243 if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) {
1244 /* Fixed size len child : use a multiplication of its size */
1245 // print_tabs(1, fd);
1246 // fprintf(fd, "size = sizeof(\n");
1247
1248 //print_tabs(1, fd);
1249 /* We know that *len does not contain alignment because of the
1250 * previous align output. len is always 0 here. */
1251 if(print_type_write(((field_t*)td->fields.array[1])->type,
1252 fd, 1, basename, "array[0]", "obj->", 1))
1253 return 1;
1254 // fprintf(fd, ");\n");
1255 fprintf(fd, "\n");
1256 print_tabs(1, fd);
1257 fprintf(fd, "*len = obj->len * (*len);\n");
1258 print_tabs(1, fd);
1259 fprintf(fd, "if(buffer != NULL)\n");
1260 print_tabs(2, fd);
1261 fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1262 print_tabs(1, fd);
1263 fprintf(fd, "*to += *len;\n");
1264 print_tabs(1, fd);
1265 fprintf(fd, "*len = 0;\n");
1266 fprintf(fd, "\n");
1267 } else {
1268 print_tabs(1, fd);
1269 fprintf(fd, "/* Variable length child : iter. */\n");
1270 print_tabs(1, fd);
1271 fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n");
1272 if(print_type_write(((field_t*)td->fields.array[1])->type,
1273 fd, 2, basename, "array[i]", "obj->", 1)) return 1;
1274 print_tabs(1, fd);
1275 fprintf(fd, "}\n");
1276 }
1277 fprintf(fd, "\n");
1278 print_tabs(1, fd);
1279 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1280 print_tabs(1, fd);
1281 fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
1282 print_tabs(1, fd);
1283 fprintf(fd, "*to_base = *to_base+*to;\n");
1284 print_tabs(1, fd);
1285 fprintf(fd, "*to = 0;\n");
1286 fprintf(fd, "\n");
1287 print_tabs(1, fd);
1288 fprintf(fd, "/* Put source *from just after the C sequence */\n");
1289 print_tabs(1, fd);
1290 fprintf(fd, "*from = (const char*)(obj+1);\n");
1291 break;
1292 case STRING:
1293 print_tabs(1, fd);
1294 fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
1295 print_tabs(1, fd);
1296 fprintf(fd, "if(buffer != NULL)\n");
1297 print_tabs(2, fd);
1298 fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n");
1299 print_tabs(1, fd);
1300 fprintf(fd, "*to += size;\n");
1301 fprintf(fd, "\n");
1302 print_tabs(1, fd);
1303 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1304 print_tabs(1, fd);
1305 fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
1306 print_tabs(1, fd);
1307 fprintf(fd, "*to_base = *to_base+*to;\n");
1308 print_tabs(1, fd);
1309 fprintf(fd, "*to = 0;\n");
1310 fprintf(fd, "\n");
1311 print_tabs(1, fd);
1312 fprintf(fd, "/* Put source *from just after the C string */\n");
1313 print_tabs(1, fd);
1314 fprintf(fd, "*from += size;\n");
1315 break;
1316 case STRUCT:
1317 for(unsigned int i=0;i<td->fields.position;i++){
1318 field_t *field = (field_t*)(td->fields.array[i]);
1319 type_descriptor_t *type = field->type;
1320 if(print_type_write(type,
1321 fd, 1, basename, field->name, "obj->", 1)) return 1;
1322 fprintf(fd, "\n");
1323 }
1324 break;
1325 case UNION:
1326 printf("ERROR : A union CANNOT contain a variable size child.\n");
1327 return 1;
1328 break;
1329 case ARRAY:
1330 /* Write the child : varlen child or not ? */
1331 if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) {
1332 /* Error : if an array has a variable size, then its child must also
1333 * have a variable size. */
1334 assert(0);
1335 } else {
1336 print_tabs(1, fd);
1337 fprintf(fd, "/* Variable length child : iter. */\n");
1338 print_tabs(1, fd);
1339 fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename);
1340 if(print_type_write(((field_t*)td->fields.array[0])->type,
1341 fd, 2, basename, "", "obj->array[i]", 1)) return 1;
1342 print_tabs(1, fd);
1343 fprintf(fd, "}\n");
1344 }
1345 break;
1346 default:
1347 printf("print_type_write_fct : type has no write function.\n");
1348 break;
1349 }
1350
1351
1352 }
1353
1354
1355 /* Function footer */
1356 fprintf(fd, "}\n");
1357 fprintf(fd, "\n");
1358 return 0;
1359 }
1360
1361
1362
1363 /* Print the logging function of an event. This is the core of genevent */
1364 int print_event_logging_function(char *basename, facility_t *fac,
1365 event_t *event, FILE *fd)
1366 {
1367 fprintf(fd, "static inline void trace_%s(\n", basename);
1368 int has_argument = 0;
1369 int has_type_fixed = 0;
1370
1371 /* Does it support per trace tracing ? */
1372 if(event->per_trace) {
1373 print_tabs(2, fd);
1374 fprintf(fd, "struct ltt_trace_struct *dest_trace");
1375 has_argument = 1;
1376 }
1377
1378 /* Does it support per tracefile tracing ? */
1379 if(event->per_tracefile) {
1380 if(has_argument) {
1381 fprintf(fd, ",");
1382 fprintf(fd, "\n");
1383 }
1384 fprintf(fd, "unsigned int tracefile_index");
1385 has_argument = 1;
1386 }
1387
1388 for(unsigned int j = 0; j < event->fields.position; j++) {
1389 /* For each field, print the function argument */
1390 field_t *f = (field_t*)event->fields.array[j];
1391 type_descriptor_t *t = f->type;
1392 if(has_argument) {
1393 fprintf(fd, ",");
1394 fprintf(fd, "\n");
1395 }
1396 if(print_arg(t, fd, 2, basename, f->name)) return 1;
1397 has_argument = 1;
1398 }
1399 if(!has_argument) {
1400 print_tabs(2, fd);
1401 fprintf(fd, "void");
1402 }
1403 fprintf(fd,")\n");
1404 fprintf(fd,
1405 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1406 fac->capname);
1407 fprintf(fd, "{\n");
1408 fprintf(fd, "}\n");
1409 fprintf(fd,"#else\n");
1410 fprintf(fd, "{\n");
1411 /* Print the function variables */
1412 print_tabs(1, fd);
1413 fprintf(fd, "unsigned int index;\n");
1414 print_tabs(1, fd);
1415 fprintf(fd, "struct ltt_channel_struct *channel;\n");
1416 print_tabs(1, fd);
1417 fprintf(fd, "struct ltt_trace_struct *trace;\n");
1418 print_tabs(1, fd);
1419 fprintf(fd, "void *transport_data;\n");
1420 print_tabs(1, fd);
1421 fprintf(fd, "char *buffer = NULL;\n");
1422 print_tabs(1, fd);
1423 fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1424 print_tabs(1, fd);
1425 fprintf(fd, "size_t *to_base = &real_to_base;\n");
1426 print_tabs(1, fd);
1427 fprintf(fd, "size_t real_to = 0;\n");
1428 print_tabs(1, fd);
1429 fprintf(fd, "size_t *to = &real_to;\n");
1430 print_tabs(1, fd);
1431 fprintf(fd, "size_t real_len = 0;\n");
1432 print_tabs(1, fd);
1433 fprintf(fd, "size_t *len = &real_len;\n");
1434 print_tabs(1, fd);
1435 fprintf(fd, "size_t reserve_size;\n");
1436 print_tabs(1, fd);
1437 fprintf(fd, "size_t slot_size;\n");
1438 print_tabs(1, fd);
1439
1440 if(event->fields.position > 0) {
1441 for(unsigned int i=0;i<event->fields.position;i++){
1442 /* Search for at least one child with fixed size. It means
1443 * we need local variables.*/
1444 field_t *field = (field_t*)(event->fields.array[i]);
1445 type_descriptor_t *type = field->type;
1446 has_type_fixed = has_type_local(type);
1447 if(has_type_fixed) break;
1448 }
1449
1450 if(has_type_fixed) {
1451 fprintf(fd, "size_t align;\n");
1452 print_tabs(1, fd);
1453 }
1454
1455 fprintf(fd, "const char *real_from;\n");
1456 print_tabs(1, fd);
1457 fprintf(fd, "const char **from = &real_from;\n");
1458 print_tabs(1, fd);
1459 }
1460 fprintf(fd, "u64 tsc;\n");
1461 print_tabs(1, fd);
1462 fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1463 fprintf(fd, "\n");
1464
1465 print_tabs(1, fd);
1466 fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n");
1467 fprintf(fd, "\n");
1468
1469 /* Calculate event variable len + event data alignment offset.
1470 * Assume that the padding for alignment starts at a void*
1471 * address.
1472 * This excludes the header size and alignment. */
1473
1474 print_tabs(1, fd);
1475 fprintf(fd, "/* For each field, calculate the field size. */\n");
1476 print_tabs(1, fd);
1477 fprintf(fd, "/* size = *to_base + *to + *len */\n");
1478 print_tabs(1, fd);
1479 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
1480 print_tabs(1, fd);
1481 fprintf(fd, " * sizeof(void *) address. */\n");
1482 fprintf(fd, "\n");
1483
1484 for(unsigned int i=0;i<event->fields.position;i++){
1485 field_t *field = (field_t*)(event->fields.array[i]);
1486 type_descriptor_t *type = field->type;
1487 /* Set from */
1488 print_tabs(1, fd);
1489 switch(type->type) {
1490 case SEQUENCE:
1491 case UNION:
1492 case ARRAY:
1493 case STRUCT:
1494 case STRING:
1495 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
1496 break;
1497 default:
1498 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
1499 break;
1500 }
1501
1502 if(print_type_write(type,
1503 fd, 1, basename, field->name, "lttng_param_", 0)) return 1;
1504 fprintf(fd, "\n");
1505 }
1506 print_tabs(1, fd);
1507 fprintf(fd, "reserve_size = *to_base + *to + *len;\n");
1508
1509 /* Take locks : make sure the trace does not vanish while we write on
1510 * it. A simple preemption disabling is enough (using rcu traces). */
1511 print_tabs(1, fd);
1512 fprintf(fd, "preempt_disable();\n");
1513 print_tabs(1, fd);
1514 fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n");
1515
1516 /* Get facility index */
1517
1518 if(event->per_tracefile) {
1519 print_tabs(1, fd);
1520 fprintf(fd, "index = tracefile_index;\n");
1521 } else {
1522 print_tabs(1, fd);
1523 fprintf(fd,
1524 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1525 "\t\t\t\t\t\tevent_%s_%s);\n",
1526 fac->name, fac->checksum, fac->name, event->name);
1527 }
1528 fprintf(fd,"\n");
1529
1530
1531 /* For each trace */
1532 print_tabs(1, fd);
1533 fprintf(fd, "list_for_each_entry_rcu(trace, &ltt_traces.head, list) {\n");
1534 print_tabs(2, fd);
1535 fprintf(fd, "if(!trace->active) continue;\n\n");
1536
1537 if(event->per_trace) {
1538 print_tabs(2, fd);
1539 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
1540 }
1541
1542 print_tabs(2, fd);
1543 fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
1544 fprintf(fd, "\n");
1545
1546
1547 /* Relay reserve */
1548 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1549 * return */
1550 print_tabs(2, fd);
1551 fprintf(fd, "slot_size = 0;\n");
1552 print_tabs(2, fd);
1553 fprintf(fd, "buffer = ltt_reserve_slot(trace, channel, &transport_data,\n");
1554 print_tabs(3, fd);
1555 fprintf(fd, "reserve_size, &slot_size, &tsc,\n");
1556 print_tabs(3, fd);
1557 fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
1558 /* If error, return */
1559 print_tabs(2, fd);
1560 fprintf(fd, "if(!buffer) continue; /* buffer full */\n\n");
1561 //print_tabs(2, fd);
1562 // for DEBUG only
1563 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1564 print_tabs(2, fd);
1565 fprintf(fd, "*to_base = *to = *len = 0;\n");
1566 fprintf(fd, "\n");
1567
1568 /* Write event header */
1569 print_tabs(2, fd);
1570 fprintf(fd, "ltt_write_event_header(trace, channel, buffer,\n");
1571 print_tabs(3, fd);
1572 fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum,
1573 fac->name, event->name);
1574 print_tabs(3, fd);
1575 fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n");
1576 print_tabs(2, fd);
1577 fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
1578 fprintf(fd, "\n");
1579
1580 /* write data. */
1581
1582 for(unsigned int i=0;i<event->fields.position;i++){
1583 field_t *field = (field_t*)(event->fields.array[i]);
1584 type_descriptor_t *type = field->type;
1585
1586 /* Set from */
1587 print_tabs(2, fd);
1588 switch(type->type) {
1589 case SEQUENCE:
1590 case UNION:
1591 case ARRAY:
1592 case STRUCT:
1593 case STRING:
1594 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
1595 break;
1596 default:
1597 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
1598 break;
1599 }
1600
1601
1602 if(print_type_write(type,
1603 fd, 2, basename, field->name, "lttng_param_", 0)) return 1;
1604 fprintf(fd, "\n");
1605
1606 /* Don't forget to flush pending memcpy */
1607 print_tabs(2, fd);
1608 fprintf(fd, "/* Flush pending memcpy */\n");
1609 print_tabs(2, fd);
1610 fprintf(fd, "if(*len != 0) {\n");
1611 print_tabs(3, fd);
1612 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1613 print_tabs(3, fd);
1614 fprintf(fd, "*to += *len;\n");
1615 //print_tabs(3, fd);
1616 //fprintf(fd, "from += len;\n");
1617 print_tabs(3, fd);
1618 fprintf(fd, "*len = 0;\n");
1619 print_tabs(2, fd);
1620 fprintf(fd, "}\n");
1621 fprintf(fd, "\n");
1622 }
1623
1624
1625 /* commit */
1626 // for DEBUG only.
1627 //fprintf(fd, "commit:\n"); /* DEBUG! */
1628 print_tabs(2, fd);
1629 fprintf(fd, "ltt_commit_slot(channel, &transport_data, buffer, slot_size);\n\n");
1630
1631 print_tabs(1, fd);
1632 fprintf(fd, "}\n\n");
1633
1634 /* Release locks */
1635 print_tabs(1, fd);
1636 fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n");
1637 print_tabs(1, fd);
1638 fprintf(fd, "preempt_enable_no_resched();\n");
1639
1640 fprintf(fd, "}\n");
1641 fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1642 fac->capname);
1643 return 0;
1644 }
1645
1646 int print_event_logging_function_header_user_generic(char *basename, facility_t *fac,
1647 event_t *event, FILE *fd, enum user_fct_types fct_type)
1648 {
1649 char *attrib;
1650
1651 if(event->no_instrument_function && fct_type == USER_FCT_PROTO) {
1652 attrib = "__attribute__((no_instrument_function)) ";
1653 } else {
1654 attrib = "";
1655 }
1656 if(event->param_buffer) {
1657 fprintf(fd, "static inline %sint trace_%s_param_buffer(\n", attrib, basename);
1658 } else {
1659 fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename);
1660 }
1661 int has_argument = 0;
1662
1663 if(event->param_buffer) {
1664 if(has_argument) {
1665 fprintf(fd, ",");
1666 fprintf(fd, "\n");
1667 }
1668 print_tabs(2, fd);
1669 fprintf(fd, "char *buffer");
1670 has_argument = 1;
1671 fprintf(fd, ",");
1672 fprintf(fd, "\n");
1673 print_tabs(2, fd);
1674 fprintf(fd, "size_t reserve_size");
1675 } else {
1676 for(unsigned int j = 0; j < event->fields.position; j++) {
1677 /* For each field, print the function argument */
1678 field_t *f = (field_t*)event->fields.array[j];
1679 type_descriptor_t *t = f->type;
1680 if(has_argument) {
1681 fprintf(fd, ",");
1682 fprintf(fd, "\n");
1683 }
1684 if(print_arg(t, fd, 2, basename, f->name)) return 1;
1685 has_argument = 1;
1686 }
1687 }
1688 if(!has_argument) {
1689 print_tabs(2, fd);
1690 fprintf(fd, "void");
1691 }
1692 fprintf(fd,")");
1693 return 0;
1694 }
1695
1696
1697 /* print_event_logging_function_user_generic
1698 * Print the logging function of an event for userspace tracing. This is the
1699 * core of genevent */
1700 int print_event_logging_function_user_generic(char *basename, facility_t *fac,
1701 event_t *event, FILE *fd)
1702 {
1703 int has_type_fixed = 0;
1704
1705 if(print_event_logging_function_header_user_generic(basename, fac, event, fd, USER_FCT_PROTO)) return 1;
1706 fprintf(fd,";\n");
1707 fprintf(fd,"\n");
1708 fprintf(fd, "#ifndef LTT_TRACE_FAST\n");
1709 if(print_event_logging_function_header_user_generic(basename, fac, event, fd, USER_FCT_DECLARATION)) return 1;
1710 fprintf(fd,"\n");
1711 fprintf(fd,
1712 "#ifndef LTT_TRACE\n");
1713 fprintf(fd, "{\n");
1714 fprintf(fd, "}\n");
1715 fprintf(fd,"#else\n");
1716 fprintf(fd, "{\n");
1717 /* Print the function variables */
1718 print_tabs(1, fd);
1719 fprintf(fd, "int ret = 0;\n");
1720 if(event->param_buffer) {
1721 //FIX print_tabs(1, fd);
1722 //fprintf(fd, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n");
1723 print_tabs(1, fd);
1724 fprintf(fd, "{\n");
1725 goto do_syscall;
1726 }
1727 print_tabs(1, fd);
1728 fprintf(fd, "char *buffer = NULL;\n");
1729 print_tabs(1, fd);
1730 fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1731 print_tabs(1, fd);
1732 fprintf(fd, "size_t *to_base = &real_to_base;\n");
1733 print_tabs(1, fd);
1734 fprintf(fd, "size_t real_to = 0;\n");
1735 print_tabs(1, fd);
1736 fprintf(fd, "size_t *to = &real_to;\n");
1737 print_tabs(1, fd);
1738 fprintf(fd, "size_t real_len = 0;\n");
1739 print_tabs(1, fd);
1740 fprintf(fd, "size_t *len = &real_len;\n");
1741 print_tabs(1, fd);
1742 fprintf(fd, "size_t reserve_size;\n");
1743 print_tabs(1, fd);
1744 fprintf(fd, "size_t slot_size;\n");
1745 print_tabs(1, fd);
1746
1747 if(event->fields.position > 0) {
1748 for(unsigned int i=0;i<event->fields.position;i++){
1749 /* Search for at least one child with fixed size. It means
1750 * we need local variables.*/
1751 field_t *field = (field_t*)(event->fields.array[i]);
1752 type_descriptor_t *type = field->type;
1753 has_type_fixed = has_type_local(type);
1754 if(has_type_fixed) break;
1755 }
1756
1757 if(has_type_fixed) {
1758 fprintf(fd, "size_t align;\n");
1759 print_tabs(1, fd);
1760 }
1761
1762 fprintf(fd, "const char *real_from;\n");
1763 print_tabs(1, fd);
1764 fprintf(fd, "const char **from = &real_from;\n");
1765 print_tabs(1, fd);
1766 }
1767
1768 /* Calculate event variable len + event data alignment offset.
1769 * Assume that the padding for alignment starts at a void*
1770 * address.
1771 * This excludes the header size and alignment. */
1772
1773 print_tabs(1, fd);
1774 fprintf(fd, "/* For each field, calculate the field size. */\n");
1775 print_tabs(1, fd);
1776 fprintf(fd, "/* size = *to_base + *to + *len */\n");
1777 print_tabs(1, fd);
1778 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
1779 print_tabs(1, fd);
1780 fprintf(fd, " * sizeof(void *) address. */\n");
1781 fprintf(fd, "\n");
1782
1783 for(unsigned int i=0;i<event->fields.position;i++){
1784 field_t *field = (field_t*)(event->fields.array[i]);
1785 type_descriptor_t *type = field->type;
1786 /* Set from */
1787 print_tabs(1, fd);
1788 switch(type->type) {
1789 case SEQUENCE:
1790 case UNION:
1791 case ARRAY:
1792 case STRUCT:
1793 case STRING:
1794 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
1795 break;
1796 default:
1797 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
1798 break;
1799 }
1800
1801 if(print_type_write(type,
1802 fd, 1, basename, field->name, "lttng_param_", 0)) return 1;
1803 fprintf(fd, "\n");
1804 }
1805 print_tabs(1, fd);
1806 fprintf(fd, "reserve_size = *to_base + *to + *len;\n");
1807
1808 print_tabs(1, fd);
1809 fprintf(fd, "{\n");
1810 print_tabs(2, fd);
1811 fprintf(fd, "char stack_buffer[reserve_size];\n");
1812 print_tabs(2, fd);
1813 fprintf(fd, "buffer = stack_buffer;\n");
1814 fprintf(fd, "\n");
1815
1816
1817 //print_tabs(2, fd);
1818 // for DEBUG only
1819 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1820 print_tabs(2, fd);
1821 fprintf(fd, "*to_base = *to = *len = 0;\n");
1822 fprintf(fd, "\n");
1823
1824 /* write data. */
1825
1826 for(unsigned int i=0;i<event->fields.position;i++){
1827 field_t *field = (field_t*)(event->fields.array[i]);
1828 type_descriptor_t *type = field->type;
1829
1830 /* Set from */
1831 print_tabs(2, fd);
1832 switch(type->type) {
1833 case SEQUENCE:
1834 case UNION:
1835 case ARRAY:
1836 case STRUCT:
1837 case STRING:
1838 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
1839 break;
1840 default:
1841 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
1842 break;
1843 }
1844
1845
1846 if(print_type_write(type,
1847 fd, 2, basename, field->name, "lttng_param_", 0)) return 1;
1848 fprintf(fd, "\n");
1849
1850 /* Don't forget to flush pending memcpy */
1851 print_tabs(2, fd);
1852 fprintf(fd, "/* Flush pending memcpy */\n");
1853 print_tabs(2, fd);
1854 fprintf(fd, "if(*len != 0) {\n");
1855 print_tabs(3, fd);
1856 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1857 print_tabs(3, fd);
1858 fprintf(fd, "*to += *len;\n");
1859 //print_tabs(3, fd);
1860 //fprintf(fd, "from += len;\n");
1861 print_tabs(3, fd);
1862 fprintf(fd, "*len = 0;\n");
1863 print_tabs(2, fd);
1864 fprintf(fd, "}\n");
1865 fprintf(fd, "\n");
1866 }
1867
1868 do_syscall:
1869 print_tabs(2, fd);
1870 fprintf(fd, "ret = ltt_trace_generic(ltt_facility_%s_%X, event_%s_%s, buffer, reserve_size, LTT_BLOCKING);\n", fac->name, fac->checksum, fac->name, event->name);
1871
1872 print_tabs(1, fd);
1873 fprintf(fd, "}\n\n");
1874
1875 print_tabs(1, fd);
1876 fprintf(fd, "return ret;\n\n");
1877
1878 fprintf(fd, "}\n");
1879 fprintf(fd,
1880 "#endif //LTT_TRACE\n");
1881 fprintf(fd, "#endif //!LTT_TRACE_FAST\n\n");
1882
1883 return 0;
1884 }
1885
1886 /* print_event_logging_function_user_fast
1887 * Print the logging function of an event for userspace tracing. This is the
1888 * core of genevent */
1889 int print_event_logging_function_user_fast(char *basename, facility_t *fac,
1890 event_t *event, FILE *fd)
1891 {
1892 char *attrib;
1893
1894 fprintf(fd, "#ifdef LTT_TRACE_FAST\n");
1895
1896 if(event->no_instrument_function) {
1897 attrib = "__attribute__((no_instrument_function)) ";
1898 } else {
1899 attrib = "";
1900 }
1901 fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename);
1902
1903 int has_argument = 0;
1904 int has_type_fixed = 0;
1905
1906 for(unsigned int j = 0; j < event->fields.position; j++) {
1907 /* For each field, print the function argument */
1908 field_t *f = (field_t*)event->fields.array[j];
1909 type_descriptor_t *t = f->type;
1910 if(has_argument) {
1911 fprintf(fd, ",");
1912 fprintf(fd, "\n");
1913 }
1914 if(print_arg(t, fd, 2, basename, f->name)) return 1;
1915 has_argument = 1;
1916 }
1917 if(!has_argument) {
1918 print_tabs(2, fd);
1919 fprintf(fd, "void");
1920 }
1921 fprintf(fd,")\n");
1922 fprintf(fd,
1923 "#ifndef LTT_TRACE\n");
1924 fprintf(fd, "{\n");
1925 fprintf(fd, "}\n");
1926 fprintf(fd,"#else\n");
1927 fprintf(fd, "{\n");
1928 /* Print the function variables */
1929 print_tabs(1, fd);
1930 fprintf(fd, "unsigned int index;\n");
1931 print_tabs(1, fd);
1932 fprintf(fd, "struct ltt_trace_info *trace = thread_trace_info;\n");
1933 print_tabs(1, fd);
1934 fprintf(fd, "struct ltt_buf *ltt_buf;\n");
1935 print_tabs(1, fd);
1936 fprintf(fd, "char *buffer = NULL;\n");
1937 print_tabs(1, fd);
1938 fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1939 print_tabs(1, fd);
1940 fprintf(fd, "size_t *to_base = &real_to_base;\n");
1941 print_tabs(1, fd);
1942 fprintf(fd, "size_t real_to = 0;\n");
1943 print_tabs(1, fd);
1944 fprintf(fd, "size_t *to = &real_to;\n");
1945 print_tabs(1, fd);
1946 fprintf(fd, "size_t real_len = 0;\n");
1947 print_tabs(1, fd);
1948 fprintf(fd, "size_t *len = &real_len;\n");
1949 print_tabs(1, fd);
1950 fprintf(fd, "size_t reserve_size;\n");
1951 print_tabs(1, fd);
1952 fprintf(fd, "size_t slot_size;\n");
1953 print_tabs(1, fd);
1954
1955 if(event->fields.position > 0) {
1956 for(unsigned int i=0;i<event->fields.position;i++){
1957 /* Search for at least one child with fixed size. It means
1958 * we need local variables.*/
1959 field_t *field = (field_t*)(event->fields.array[i]);
1960 type_descriptor_t *type = field->type;
1961 has_type_fixed = has_type_local(type);
1962 if(has_type_fixed) break;
1963 }
1964
1965 if(has_type_fixed) {
1966 fprintf(fd, "size_t align;\n");
1967 print_tabs(1, fd);
1968 }
1969
1970 fprintf(fd, "const char *real_from;\n");
1971 print_tabs(1, fd);
1972 fprintf(fd, "const char **from = &real_from;\n");
1973 print_tabs(1, fd);
1974 }
1975 fprintf(fd, "uint64_t tsc;\n");
1976 print_tabs(1, fd);
1977 fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
1978 fprintf(fd, "\n");
1979
1980 print_tabs(1, fd);
1981 fprintf(fd, "if(!trace) {\n");
1982 print_tabs(2, fd);
1983 fprintf(fd, "ltt_thread_init();\n");
1984 print_tabs(2, fd);
1985 fprintf(fd, "trace = thread_trace_info;\n");
1986 print_tabs(1, fd);
1987 fprintf(fd, "}\n\n");
1988 fprintf(fd, "\n");
1989
1990 /* Calculate event variable len + event data alignment offset.
1991 * Assume that the padding for alignment starts at a void*
1992 * address.
1993 * This excludes the header size and alignment. */
1994
1995 print_tabs(1, fd);
1996 fprintf(fd, "/* For each field, calculate the field size. */\n");
1997 print_tabs(1, fd);
1998 fprintf(fd, "/* size = *to_base + *to + *len */\n");
1999 print_tabs(1, fd);
2000 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
2001 print_tabs(1, fd);
2002 fprintf(fd, " * sizeof(void *) address. */\n");
2003 fprintf(fd, "\n");
2004
2005 for(unsigned int i=0;i<event->fields.position;i++){
2006 field_t *field = (field_t*)(event->fields.array[i]);
2007 type_descriptor_t *type = field->type;
2008 /* Set from */
2009 print_tabs(1, fd);
2010 switch(type->type) {
2011 case SEQUENCE:
2012 case UNION:
2013 case ARRAY:
2014 case STRUCT:
2015 case STRING:
2016 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
2017 break;
2018 default:
2019 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
2020 break;
2021 }
2022
2023 if(print_type_write(type,
2024 fd, 1, basename, field->name, "lttng_param_", 0)) return 1;
2025 fprintf(fd, "\n");
2026 }
2027 print_tabs(1, fd);
2028 fprintf(fd, "reserve_size = *to_base + *to + *len;\n");
2029
2030 print_tabs(1, fd);
2031 fprintf(fd, "trace->nesting++;\n");
2032
2033 /* Get facility index */
2034
2035 print_tabs(1, fd);
2036 fprintf(fd,
2037 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
2038 "\t\t\t\t\t\tevent_%s_%s);\n",
2039 fac->name, fac->checksum, fac->name, event->name);
2040 fprintf(fd,"\n");
2041
2042
2043 print_tabs(1, fd);
2044 fprintf(fd, "{\n");
2045
2046 if(event->per_trace) {
2047 print_tabs(2, fd);
2048 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
2049 }
2050
2051 print_tabs(2, fd);
2052 fprintf(fd, "ltt_buf = ltt_get_channel_from_index(trace, index);\n");
2053 print_tabs(2, fd);
2054
2055
2056 /* Relay reserve */
2057 /* If error, increment event lost counter (done by ltt_reserve_slot) and
2058 * return */
2059 print_tabs(2, fd);
2060 fprintf(fd, "slot_size = 0;\n");
2061 print_tabs(2, fd);
2062 fprintf(fd, "buffer = ltt_reserve_slot(trace, ltt_buf,\n");
2063 print_tabs(3, fd);
2064 fprintf(fd, "reserve_size, &slot_size, &tsc,\n");
2065 print_tabs(3, fd);
2066 fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
2067 /* If error, return */
2068 print_tabs(2, fd);
2069 fprintf(fd, "if(!buffer) goto end; /* buffer full */\n\n");
2070 //print_tabs(2, fd);
2071 // for DEBUG only
2072 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
2073 print_tabs(2, fd);
2074 fprintf(fd, "*to_base = *to = *len = 0;\n");
2075 fprintf(fd, "\n");
2076
2077 /* Write event header */
2078 print_tabs(2, fd);
2079 fprintf(fd, "ltt_write_event_header(trace, ltt_buf, buffer,\n");
2080 print_tabs(3, fd);
2081 fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum,
2082 fac->name, event->name);
2083 print_tabs(3, fd);
2084 fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n");
2085 print_tabs(2, fd);
2086 fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
2087 fprintf(fd, "\n");
2088
2089 /* write data. */
2090
2091 for(unsigned int i=0;i<event->fields.position;i++){
2092 field_t *field = (field_t*)(event->fields.array[i]);
2093 type_descriptor_t *type = field->type;
2094
2095 /* Set from */
2096 print_tabs(2, fd);
2097 switch(type->type) {
2098 case SEQUENCE:
2099 case UNION:
2100 case ARRAY:
2101 case STRUCT:
2102 case STRING:
2103 fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name);
2104 break;
2105 default:
2106 fprintf(fd, "*from = (const char*)&lttng_param_%s;\n", field->name);
2107 break;
2108 }
2109
2110
2111 if(print_type_write(type,
2112 fd, 2, basename, field->name, "lttng_param_", 0)) return 1;
2113 fprintf(fd, "\n");
2114
2115 /* Don't forget to flush pending memcpy */
2116 print_tabs(2, fd);
2117 fprintf(fd, "/* Flush pending memcpy */\n");
2118 print_tabs(2, fd);
2119 fprintf(fd, "if(*len != 0) {\n");
2120 print_tabs(3, fd);
2121 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
2122 print_tabs(3, fd);
2123 fprintf(fd, "*to += *len;\n");
2124 //print_tabs(3, fd);
2125 //fprintf(fd, "from += len;\n");
2126 print_tabs(3, fd);
2127 fprintf(fd, "*len = 0;\n");
2128 print_tabs(2, fd);
2129 fprintf(fd, "}\n");
2130 fprintf(fd, "\n");
2131 }
2132
2133
2134 /* commit */
2135 // for DEBUG only.
2136 //fprintf(fd, "commit:\n"); /* DEBUG! */
2137 print_tabs(2, fd);
2138 fprintf(fd, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n");
2139
2140 fprintf(fd, "}\n\n");
2141
2142 fprintf(fd, "end:\n");
2143 /* Release locks */
2144 print_tabs(1, fd);
2145 fprintf(fd, "trace->nesting--;\n");
2146
2147
2148 fprintf(fd, "}\n");
2149 fprintf(fd,
2150 "#endif //LTT_TRACE\n");
2151 fprintf(fd, "#endif //LTT_TRACE_FAST\n");
2152
2153 return 0;
2154 }
2155
2156
2157
2158
2159
2160
2161 /* ltt-facility-name.h : main logging header.
2162 * log_header */
2163
2164 void print_log_header_head(facility_t *fac, FILE *fd)
2165 {
2166 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
2167 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
2168 fprintf(fd, "#include <linux/types.h>\n");
2169 if(!fac->arch)
2170 fprintf(fd, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac->name);
2171 else
2172 fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2173 fac->name,
2174 fac->arch);
2175 fprintf(fd, "#include <linux/ltt-core.h>\n");
2176 fprintf(fd, "\n");
2177 }
2178
2179 /* ltt-facility-name.h : main logging header.
2180 * log_header */
2181
2182 void print_log_header_head_user(facility_t *fac, FILE *fd)
2183 {
2184 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
2185 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
2186 fprintf(fd, "#include <sys/types.h>\n");
2187 if(!fac->arch)
2188 fprintf(fd, "#include <ltt/ltt-facility-id-%s.h>\n", fac->name);
2189 else
2190 fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
2191 fac->name,
2192 fac->arch);
2193 fprintf(fd, "#include <ltt/ltt-usertrace.h>\n");
2194 fprintf(fd, "\n");
2195 fprintf(fd, "#ifdef __cplusplus\n");
2196 fprintf(fd, "extern \"C\" {\n");
2197 fprintf(fd, "#endif\n");
2198 fprintf(fd, "\n");
2199 }
2200
2201
2202 int print_log_header_types(facility_t *fac, FILE *fd)
2203 {
2204 sequence_t *types = &fac->named_types.values;
2205 fprintf(fd, "/* Named types */\n");
2206 fprintf(fd, "\n");
2207
2208 for(unsigned int i = 0; i < types->position; i++) {
2209 /* For each named type, print the definition */
2210 if(print_type_declaration(types->array[i], fd,
2211 0, "", "")) return 1;
2212 /* Print also the align function */
2213 if(print_type_alignment_fct(types->array[i], fd,
2214 0, "", "")) return 1;
2215 /* Print also the write function */
2216 if(print_type_write_fct(types->array[i], fd,
2217 0, "", "")) return 1;
2218 }
2219 return 0;
2220 }
2221
2222 int print_log_header_events(facility_t *fac, FILE *fd)
2223 {
2224 sequence_t *events = &fac->events;
2225 char basename[PATH_MAX];
2226 unsigned int facname_len;
2227
2228 strncpy(basename, fac->name, PATH_MAX);
2229 facname_len = strlen(basename);
2230 strncat(basename, "_", PATH_MAX-facname_len);
2231 facname_len = strlen(basename);
2232
2233 for(unsigned int i = 0; i < events->position; i++) {
2234 event_t *event = (event_t*)events->array[i];
2235 strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len);
2236
2237 /* For each event, print structure, and then logging function */
2238 fprintf(fd, "/* Event %s structures */\n",
2239 event->name);
2240 for(unsigned int j = 0; j < event->fields.position; j++) {
2241 /* For each unnamed type, print the definition */
2242 field_t *f = (field_t*)event->fields.array[j];
2243 type_descriptor_t *t = f->type;
2244 if(t->type_name == NULL) {
2245 if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
2246 /* Print also the align function */
2247 if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1;
2248 /* Print also the write function */
2249 if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1;
2250 }
2251 }
2252
2253 fprintf(fd, "\n");
2254
2255 fprintf(fd, "/* Event %s logging function */\n",
2256 event->name);
2257
2258 if(!fac->user) {
2259 if(print_event_logging_function(basename, fac, event, fd)) return 1;
2260 } else {
2261 if(print_event_logging_function_user_generic(basename, fac, event, fd))
2262 return 1;
2263 if(print_event_logging_function_user_fast(basename, fac, event, fd))
2264 return 1;
2265 }
2266
2267 fprintf(fd, "\n");
2268 }
2269
2270 return 0;
2271 }
2272
2273
2274 void print_log_header_tail(facility_t *fac, FILE *fd)
2275 {
2276 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
2277 }
2278
2279 void print_log_header_tail_user(facility_t *fac, FILE *fd)
2280 {
2281 fprintf(fd, "#ifdef __cplusplus\n");
2282 fprintf(fd, "} /* end of extern \"C\" */\n");
2283 fprintf(fd, "#endif\n");
2284 fprintf(fd, "\n");
2285 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
2286 }
2287
2288 int print_log_header(facility_t *fac)
2289 {
2290 char filename[PATH_MAX];
2291 unsigned int filename_size = 0;
2292 FILE *fd;
2293 dprintf("%s\n", fac->name);
2294
2295 strcpy(filename, "ltt-facility-");
2296 filename_size = strlen(filename);
2297
2298 strncat(filename, fac->name, PATH_MAX - filename_size);
2299 filename_size = strlen(filename);
2300
2301 if(fac->arch) {
2302 strncat(filename, "_", PATH_MAX - filename_size);
2303 filename_size = strlen(filename);
2304
2305 strncat(filename, fac->arch, PATH_MAX - filename_size);
2306 filename_size = strlen(filename);
2307 }
2308
2309 strncat(filename, ".h", PATH_MAX - filename_size);
2310 filename_size = strlen(filename);
2311
2312
2313 fd = fopen(filename, "w");
2314 if(fd == NULL) {
2315 printf("Error opening file %s for writing : %s\n",
2316 filename, strerror(errno));
2317 return errno;
2318 }
2319
2320 /* Print file head */
2321 if(!fac->user)
2322 print_log_header_head(fac, fd);
2323 else
2324 print_log_header_head_user(fac, fd);
2325
2326 /* print named types in declaration order */
2327 if(print_log_header_types(fac, fd)) return 1;
2328
2329 /* Print events */
2330 if(print_log_header_events(fac, fd)) return 1;
2331
2332 /* Print file tail */
2333 if(!fac->user)
2334 print_log_header_tail(fac, fd);
2335 else
2336 print_log_header_tail_user(fac, fd);
2337
2338
2339
2340 fclose(fd);
2341
2342 return 0;
2343 }
2344
2345
2346 /* ltt-facility-id-name.h : facility id.
2347 * log_id_header */
2348 int print_id_header(facility_t *fac)
2349 {
2350 char filename[PATH_MAX];
2351 unsigned int filename_size = 0;
2352 FILE *fd;
2353 char basename[PATH_MAX];
2354 char basename_len = 0;
2355
2356 dprintf("%s\n", fac->name);
2357
2358 strcpy(filename, "ltt-facility-id-");
2359 filename_size = strlen(filename);
2360
2361 strncat(filename, fac->name, PATH_MAX - filename_size);
2362 filename_size = strlen(filename);
2363
2364 if(fac->arch) {
2365 strncat(filename, "_", PATH_MAX - filename_size);
2366 filename_size = strlen(filename);
2367
2368 strncat(filename, fac->arch, PATH_MAX - filename_size);
2369 filename_size = strlen(filename);
2370 }
2371
2372 strncat(filename, ".h", PATH_MAX - filename_size);
2373 filename_size = strlen(filename);
2374
2375
2376 fd = fopen(filename, "w");
2377 if(fd == NULL) {
2378 printf("Error opening file %s for writing : %s\n",
2379 filename, strerror(errno));
2380 return errno;
2381 }
2382
2383 if(!fac->user) {
2384 fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
2385 fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
2386 fprintf(fd, "#ifdef CONFIG_LTT\n");
2387
2388 fprintf(fd,"#include <linux/ltt-facilities.h>\n\n");
2389
2390 fprintf(fd,"/**** facility handle ****/\n\n");
2391 fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2392 fac->name, fac->checksum);
2393 fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name);
2394
2395 strncpy(basename, fac->name, PATH_MAX);
2396 basename_len = strlen(basename);
2397 strncat(basename, "_", PATH_MAX - basename_len);
2398 basename_len++;
2399
2400 fprintf(fd,"/**** event index ****/\n\n");
2401 fprintf(fd,"enum %s_event {\n",fac->name);
2402
2403 for(unsigned int i = 0; i < fac->events.position; i++) {
2404 event_t *event = (event_t*)fac->events.array[i];
2405 strncpy(basename+basename_len, event->name, PATH_MAX-basename_len);
2406 print_tabs(1, fd);
2407 fprintf(fd, "event_%s,\n", basename);
2408 }
2409 print_tabs(1, fd);
2410 fprintf(fd, "facility_%s_num_events\n", fac->name);
2411 fprintf(fd, "};\n");
2412 fprintf(fd, "\n");
2413
2414
2415 fprintf(fd, "#endif //CONFIG_LTT\n");
2416 fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
2417 } else {
2418 fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
2419 fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
2420 fprintf(fd, "#ifdef LTT_TRACE\n");
2421
2422 fprintf(fd,"#include <ltt/ltt-usertrace.h>\n\n");
2423
2424 fprintf(fd,"/**** facility handle ****/\n\n");
2425 fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n",
2426 fac->name, fac->checksum);
2427 fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name);
2428
2429 strncpy(basename, fac->name, PATH_MAX);
2430 basename_len = strlen(basename);
2431 strncat(basename, "_", PATH_MAX - basename_len);
2432 basename_len++;
2433
2434 fprintf(fd,"/**** event index ****/\n\n");
2435 fprintf(fd,"enum %s_event {\n",fac->name);
2436
2437 for(unsigned int i = 0; i < fac->events.position; i++) {
2438 event_t *event = (event_t*)fac->events.array[i];
2439 strncpy(basename+basename_len, event->name, PATH_MAX-basename_len);
2440 print_tabs(1, fd);
2441 fprintf(fd, "event_%s,\n", basename);
2442 }
2443 print_tabs(1, fd);
2444 fprintf(fd, "facility_%s_num_events\n", fac->name);
2445 fprintf(fd, "};\n");
2446 fprintf(fd, "\n");
2447
2448
2449 fprintf(fd, "#endif //LTT_TRACE\n");
2450 fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
2451 }
2452
2453
2454 fclose(fd);
2455
2456 return 0;
2457 }
2458
2459
2460 /* ltt-facility-loader-name.h : facility specific loader info.
2461 * loader_header */
2462 int print_loader_header(facility_t *fac)
2463 {
2464 char filename[PATH_MAX];
2465 unsigned int filename_size = 0;
2466 FILE *fd;
2467 dprintf("%s\n", fac->name);
2468
2469 strcpy(filename, "ltt-facility-loader-");
2470 filename_size = strlen(filename);
2471
2472 strncat(filename, fac->name, PATH_MAX - filename_size);
2473 filename_size = strlen(filename);
2474
2475 if(fac->arch) {
2476 strncat(filename, "_", PATH_MAX - filename_size);
2477 filename_size = strlen(filename);
2478
2479 strncat(filename, fac->arch, PATH_MAX - filename_size);
2480 filename_size = strlen(filename);
2481 }
2482
2483 strncat(filename, ".h", PATH_MAX - filename_size);
2484 filename_size = strlen(filename);
2485
2486
2487 fd = fopen(filename, "w");
2488 if(fd == NULL) {
2489 printf("Error opening file %s for writing : %s\n",
2490 filename, strerror(errno));
2491 return errno;
2492 }
2493
2494 fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
2495 fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname);
2496 fprintf(fd, "#ifdef CONFIG_LTT\n\n");
2497 fprintf(fd,"#include <linux/ltt-facilities.h>\n");
2498 if(!fac->arch)
2499 fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
2500 fac->name);
2501 else
2502 fprintf(fd,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
2503 fac->name,
2504 fac->arch);
2505 fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name);
2506 fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2507 fac->name, fac->checksum);
2508
2509 fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\tltt_facility_%s\n",
2510 fac->name);
2511 fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\tltt_facility_%s_%X\n",
2512 fac->name, fac->checksum);
2513 fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t0x%X\n", fac->checksum);
2514 fprintf(fd,"#define LTT_FACILITY_NAME\t\t\"%s\"\n", fac->name);
2515 fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\tfacility_%s_num_events\n\n",
2516 fac->name);
2517 fprintf(fd, "#endif //CONFIG_LTT\n\n");
2518 fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
2519
2520 fclose(fd);
2521
2522 return 0;
2523 }
2524
2525 int print_loader_header_user(facility_t *fac)
2526 {
2527 char filename[PATH_MAX];
2528 unsigned int filename_size = 0;
2529 FILE *fd;
2530 dprintf("%s\n", fac->name);
2531
2532 strcpy(filename, "ltt-facility-loader-");
2533 filename_size = strlen(filename);
2534
2535 strncat(filename, fac->name, PATH_MAX - filename_size);
2536 filename_size = strlen(filename);
2537
2538 if(fac->arch) {
2539 strncat(filename, "_", PATH_MAX - filename_size);
2540 filename_size = strlen(filename);
2541
2542 strncat(filename, fac->arch, PATH_MAX - filename_size);
2543 filename_size = strlen(filename);
2544 }
2545
2546 strncat(filename, ".h", PATH_MAX - filename_size);
2547 filename_size = strlen(filename);
2548
2549
2550 fd = fopen(filename, "w");
2551 if(fd == NULL) {
2552 printf("Error opening file %s for writing : %s\n",
2553 filename, strerror(errno));
2554 return errno;
2555 }
2556
2557 fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
2558 fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname);
2559 fprintf(fd,"#include <ltt/ltt-usertrace.h>\n");
2560 if(!fac->arch)
2561 fprintf(fd,"#include <ltt/ltt-facility-id-%s.h>\n\n",
2562 fac->name);
2563 else
2564 fprintf(fd,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
2565 fac->name,
2566 fac->arch);
2567 fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name);
2568 fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
2569 fac->name, fac->checksum);
2570
2571 fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
2572 fac->name);
2573 fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
2574 fac->name, fac->checksum);
2575 fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum);
2576 fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name);
2577 fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
2578 fac->name);
2579 fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
2580
2581 fclose(fd);
2582
2583 return 0;
2584 }
2585
2586
2587
2588 /* ltt-facility-loader-name.c : generic facility loader
2589 * loader_c */
2590 int print_loader_c(facility_t *fac)
2591 {
2592 char filename[PATH_MAX];
2593 unsigned int filename_size = 0;
2594 FILE *fd;
2595 dprintf("%s\n", fac->name);
2596
2597 strcpy(filename, "ltt-facility-loader-");
2598 filename_size = strlen(filename);
2599
2600 strncat(filename, fac->name, PATH_MAX - filename_size);
2601 filename_size = strlen(filename);
2602
2603 if(fac->arch) {
2604 strncat(filename, "_", PATH_MAX - filename_size);
2605 filename_size = strlen(filename);
2606
2607 strncat(filename, fac->arch, PATH_MAX - filename_size);
2608 filename_size = strlen(filename);
2609 }
2610
2611 strncat(filename, ".c", PATH_MAX - filename_size);
2612 filename_size = strlen(filename);
2613
2614
2615 fd = fopen(filename, "w");
2616 if(fd == NULL) {
2617 printf("Error opening file %s for writing : %s\n",
2618 filename, strerror(errno));
2619 return errno;
2620 }
2621
2622 fprintf(fd, "/*\n");
2623 if(!fac->arch)
2624 fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name);
2625 else
2626 fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch);
2627 fprintf(fd, " *\n");
2628 fprintf(fd, " * (C) Copyright 2005 - \n");
2629 fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2630 fprintf(fd, " *\n");
2631 fprintf(fd, " * Contains the LTT facility loader.\n");
2632 fprintf(fd, " *\n");
2633 fprintf(fd, " */\n");
2634 fprintf(fd, "\n");
2635 fprintf(fd, "\n");
2636 fprintf(fd, "#include <linux/ltt-facilities.h>\n");
2637 fprintf(fd, "#include <linux/module.h>\n");
2638 fprintf(fd, "#include <linux/init.h>\n");
2639 fprintf(fd, "#include <linux/config.h>\n");
2640 if(!fac->arch)
2641 fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name);
2642 else
2643 fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2644 fac->name, fac->arch);
2645 fprintf(fd, "\n");
2646 fprintf(fd, "\n");
2647 fprintf(fd, "#ifdef CONFIG_LTT\n");
2648 fprintf(fd, "\n");
2649 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
2650 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
2651 fprintf(fd, "\n");
2652 fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
2653 fprintf(fd, "\n");
2654 fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n");
2655 fprintf(fd, "\n");
2656 fprintf(fd, "static struct ltt_facility facility = {\n");
2657 fprintf(fd, "\t.name = ltt_facility_name,\n");
2658 fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2659 fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2660 fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
2661 fprintf(fd, "};\n");
2662 fprintf(fd, "\n");
2663 fprintf(fd, "static int __init facility_init(void)\n");
2664 fprintf(fd, "{\n");
2665 fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name);
2666 fprintf(fd, "\n");
2667 fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n");
2668 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2669 fprintf(fd, "\t\n");
2670 fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
2671 fprintf(fd, "}\n");
2672 fprintf(fd, "\n");
2673 fprintf(fd, "#ifndef MODULE\n");
2674 fprintf(fd, "__initcall(facility_init);\n");
2675 fprintf(fd, "#else\n");
2676 fprintf(fd, "module_init(facility_init);\n");
2677 fprintf(fd, "static void __exit facility_exit(void)\n");
2678 fprintf(fd, "{\n");
2679 fprintf(fd, "\tint err;\n");
2680 fprintf(fd, "\n");
2681 fprintf(fd, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n");
2682 fprintf(fd, "\tif(err != 0)\n");
2683 fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
2684 fprintf(fd, "\n");
2685 fprintf(fd, "}\n");
2686 fprintf(fd, "module_exit(facility_exit)\n");
2687 fprintf(fd, "\n");
2688 fprintf(fd, "MODULE_LICENSE(\"GPL\");\n");
2689 fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
2690 fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
2691 fprintf(fd, "\n");
2692 fprintf(fd, "#endif //MODULE\n");
2693 fprintf(fd, "\n");
2694 fprintf(fd, "#endif //CONFIG_LTT\n");
2695
2696 fclose(fd);
2697
2698 return 0;
2699 }
2700
2701 int print_loader_c_user(facility_t *fac)
2702 {
2703 char filename[PATH_MAX];
2704 unsigned int filename_size = 0;
2705 FILE *fd;
2706 dprintf("%s\n", fac->name);
2707
2708 strcpy(filename, "ltt-facility-loader-");
2709 filename_size = strlen(filename);
2710
2711 strncat(filename, fac->name, PATH_MAX - filename_size);
2712 filename_size = strlen(filename);
2713
2714 if(fac->arch) {
2715 strncat(filename, "_", PATH_MAX - filename_size);
2716 filename_size = strlen(filename);
2717
2718 strncat(filename, fac->arch, PATH_MAX - filename_size);
2719 filename_size = strlen(filename);
2720 }
2721
2722 strncat(filename, ".c", PATH_MAX - filename_size);
2723 filename_size = strlen(filename);
2724
2725
2726 fd = fopen(filename, "w");
2727 if(fd == NULL) {
2728 printf("Error opening file %s for writing : %s\n",
2729 filename, strerror(errno));
2730 return errno;
2731 }
2732
2733 fprintf(fd, "/*\n");
2734 if(!fac->arch)
2735 fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name);
2736 else
2737 fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch);
2738 fprintf(fd, " *\n");
2739 fprintf(fd, " * (C) Copyright 2005 - \n");
2740 fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
2741 fprintf(fd, " *\n");
2742 fprintf(fd, " * Contains the LTT user space facility loader.\n");
2743 fprintf(fd, " *\n");
2744 fprintf(fd, " */\n");
2745 fprintf(fd, "\n");
2746 fprintf(fd, "\n");
2747 fprintf(fd, "#define LTT_TRACE\n");
2748 fprintf(fd, "#include <error.h>\n");
2749 fprintf(fd, "#include <stdio.h>\n");
2750 fprintf(fd, "#include <ltt/ltt-usertrace.h>\n");
2751 if(!fac->arch)
2752 fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name);
2753 else
2754 fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n",
2755 fac->name, fac->arch);
2756 fprintf(fd, "\n");
2757 fprintf(fd, "static struct user_facility_info facility = {\n");
2758 fprintf(fd, "\t.name = LTT_FACILITY_NAME,\n");
2759 fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
2760 fprintf(fd, "#ifndef LTT_PACK\n");
2761 fprintf(fd, "\t.alignment = sizeof(void*),\n");
2762 fprintf(fd, "#else\n");
2763 fprintf(fd, "\t.alignment = 0,\n");
2764 fprintf(fd, "#endif //LTT_PACK\n");
2765 fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
2766 fprintf(fd, "\t.int_size = sizeof(int),\n");
2767 fprintf(fd, "\t.long_size = sizeof(long),\n");
2768 fprintf(fd, "\t.pointer_size = sizeof(void*),\n");
2769 fprintf(fd, "\t.size_t_size = sizeof(size_t)\n");
2770 fprintf(fd, "};\n");
2771 fprintf(fd, "\n");
2772 fprintf(fd, "static void __attribute__((constructor)) __ltt_user_init(void)\n");
2773 fprintf(fd, "{\n");
2774 fprintf(fd, "\tint err;\n");
2775 fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n");
2776 fprintf(fd, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac->name);
2777 fprintf(fd, "#endif //LTT_SHOW_DEBUG\n");
2778 fprintf(fd, "\n");
2779 fprintf(fd, "\terr = ltt_register_generic(&LTT_FACILITY_SYMBOL, &facility);\n");
2780 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
2781 fprintf(fd, "\t\n");
2782 fprintf(fd, "\tif(err) {\n");
2783 fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n");
2784 fprintf(fd, "\t\tperror(\"Error in ltt_register_generic\");\n");
2785 fprintf(fd, "#endif //LTT_SHOW_DEBUG\n");
2786 fprintf(fd, "\t}\n");
2787 fprintf(fd, "}\n");
2788 fprintf(fd, "\n");
2789
2790 fclose(fd);
2791
2792 return 0;
2793 }
2794
2795
2796
2797 /* open facility */
2798 /* code taken from ltt_facility_open in ltt/facility.c in lttv */
2799
2800 /*****************************************************************************
2801 *Function name
2802 * ltt_facility_open : open facilities
2803 *Input params
2804 * pathname : the path name of the facility
2805 *
2806 * Open the facility corresponding to the right checksum.
2807 *
2808 *returns the facility on success, NULL on error.
2809 ****************************************************************************/
2810 facility_t *ltt_facility_open(char * pathname)
2811 {
2812 int ret = 0;
2813 char *token;
2814 parse_file_t in;
2815 facility_t * fac = NULL;
2816 char buffer[BUFFER_SIZE];
2817 int generated = FALSE;
2818
2819 in.buffer = &(buffer[0]);
2820 in.lineno = 0;
2821 in.error = error_callback;
2822 in.name = pathname;
2823 in.unget = 0;
2824
2825 in.fp = fopen(in.name, "r");
2826 if(in.fp == NULL) {
2827 ret = 1;
2828 goto open_error;
2829 }
2830
2831 while(1){
2832 token = getToken(&in);
2833 if(in.type == ENDFILE) break;
2834
2835 if(generated) {
2836 printf("More than one facility in the file. Only using the first one.\n");
2837 break;
2838 }
2839
2840 if(strcmp(token, "<")) in.error(&in,"not a facility file");
2841 token = getName(&in);
2842 if(strcmp(token, "?")) in.error(&in,"not a facility file");
2843 token = getName(&in);
2844 if(strcmp(token, "xml")) in.error(&in,"not a facility file");
2845 token = getName(&in);
2846 if(strcmp(token, "version")) in.error(&in,"not a facility file");
2847 token = getName(&in);
2848 if(strcmp(token, "=")) in.error(&in,"not a facility file");
2849 token = getQuotedString(&in);
2850 if(strcmp(token, "1.0")) in.error(&in,"not a facility file");
2851 token = getName(&in);
2852 if(strcmp(token, "?")) in.error(&in,"not a facility file");
2853 token = getToken(&in);
2854 if(strcmp(token, ">")) in.error(&in,"not a facility file");
2855
2856 token = getName(&in);
2857 if(strcmp(token, "<")) in.error(&in,"not a facility file");
2858 token = getName(&in);
2859 if(strcmp("facility",token) == 0) {
2860 fac = malloc(sizeof(facility_t));
2861 fac->name = NULL;
2862 fac->description = NULL;
2863 sequence_init(&(fac->events));
2864 table_init(&(fac->named_types));
2865 sequence_init(&(fac->unnamed_types));
2866
2867 parseFacility(&in, fac);
2868
2869 //check if any namedType is not defined
2870 checkNamedTypesImplemented(&fac->named_types);
2871
2872 generateChecksum(fac->name, &fac->checksum, &fac->events);
2873
2874 generated = TRUE;
2875 }
2876 else {
2877 printf("facility token was expected in file %s\n", in.name);
2878 ret = 1;
2879 goto parse_error;
2880 }
2881 }
2882
2883 parse_error:
2884 fclose(in.fp);
2885 open_error:
2886
2887 if(!generated) {
2888 printf("Cannot find facility %s\n", pathname);
2889 fac = NULL;
2890 }
2891
2892 return fac;
2893 }
2894
2895 /* Close the facility */
2896 void ltt_facility_close(facility_t *fac)
2897 {
2898 free(fac->name);
2899 free(fac->capname);
2900 free(fac->description);
2901 freeEvents(&fac->events);
2902 sequence_dispose(&fac->events);
2903 freeNamedType(&fac->named_types);
2904 table_dispose(&fac->named_types);
2905 freeTypes(&fac->unnamed_types);
2906 sequence_dispose(&fac->unnamed_types);
2907 free(fac);
2908 }
2909
2910
2911 /* Show help */
2912 void show_help(int argc, char ** argv)
2913 {
2914 printf("Genevent help : \n");
2915 printf("\n");
2916 printf("Use %s name.xml\n", argv[0]);
2917 printf("to create :\n");
2918 printf("ltt-facility-name.h\n");
2919 printf("ltt-facility-id-name.h\n");
2920 printf("ltt-facility-loader-name.h\n");
2921 printf("ltt-facility-loader-name.c\n");
2922 printf("In the current directory.\n");
2923 printf("\n");
2924 }
2925
2926 /* Parse program arguments */
2927 /* Return values :
2928 * 0 : continue program
2929 * -1 : stop program, return 0
2930 * > 0 : stop program, return value as exit.
2931 */
2932 int check_args(int argc, char **argv)
2933 {
2934 if(argc < 2) {
2935 printf("Not enough arguments\n");
2936 show_help(argc, argv);
2937 return EINVAL;
2938 }
2939
2940 if(strcmp(argv[1], "-h") == 0) {
2941 show_help(argc, argv);
2942 return -1;
2943 }
2944
2945 return 0;
2946 }
2947
2948 int main(int argc, char **argv)
2949 {
2950 int err = 0;
2951 facility_t *fac;
2952
2953 err = check_args(argc, argv);
2954 if(err > 0) return err;
2955 else if(err < 0) return 0;
2956
2957 /* open the facility */
2958 fac = ltt_facility_open(argv[1]);
2959 if(fac == NULL) {
2960 printf("Error opening file %s for reading : %s\n",
2961 argv[1], strerror(errno));
2962 return errno;
2963 }
2964
2965 /* generate the output C files */
2966
2967
2968 /* ltt-facility-name.h : main logging header.
2969 * log_header */
2970 err = print_log_header(fac);
2971 if(err) return err;
2972
2973 /* ltt-facility-id-name.h : facility id.
2974 * log_id_header */
2975 err = print_id_header(fac);
2976 if(err) return err;
2977
2978 /* ltt-facility-loader-name.h : facility specific loader info.
2979 * loader_header */
2980 if(!fac->user)
2981 err = print_loader_header(fac);
2982 else
2983 err = print_loader_header_user(fac);
2984 if(err) return err;
2985
2986 /* ltt-facility-loader-name.c : generic faciilty loader
2987 * loader_c */
2988 if(!fac->user)
2989 err = print_loader_c(fac);
2990 else
2991 err = print_loader_c_user(fac);
2992 if(err) return err;
2993
2994 /* close the facility */
2995 ltt_facility_close(fac);
2996
2997 return 0;
2998 }
2999
3000
This page took 0.12024 seconds and 4 git commands to generate.