enable code for alignment and write
[lttv.git] / genevent-new / genevent.c
CommitLineData
92d82357 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
2d2d14a7 58#define _GNU_SOURCE
59#include <limits.h>
60#include <stdlib.h>
92d82357 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>
2d2d14a7 68#include <assert.h>
92d82357 69
70#include "genevent.h"
71#include "parser.h"
72
73
74#define TRUE 1
75#define FALSE (!TRUE)
76
2d2d14a7 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
92d82357 89/* Code printing */
90
2d2d14a7 91void print_tabs(unsigned int tabs, FILE *fd)
92{
93 for(unsigned int i = 0; i<tabs;i++)
94 fprintf(fd, "\t");
95}
96
2d2d14a7 97/* print type.
98 *
99 * Copied from construct_types_and_fields in LTTV facility.c */
100
101int 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 */
7b175edc 116 if((basename_len != 0)
117 && (basename[basename_len-1] != '_')
118 && (field_name[0] != '\0')) {
2d2d14a7 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, "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, "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
7e97b039 197/* Print logging function argument */
198int 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, "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, "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
a3e6ce64 318/* Does the type has a fixed size ? (as know from the compiler)
319 *
320 * 1 : fixed size
321 * 0 : variable length
322 */
323int 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);
2e415130 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);
a3e6ce64 370 break;
371 }
2e415130 372 return 0; //make gcc happy.
a3e6ce64 373}
374
375
376
377
378
2d2d14a7 379/* print type declaration.
380 *
381 * Copied from construct_types_and_fields in LTTV facility.c */
382
383int 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
7b175edc 389 strncpy(basename, nest_name, PATH_MAX);
2d2d14a7 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 {
7b175edc 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'))) {
2d2d14a7 402 strncat(basename, "_", PATH_MAX - basename_len);
403 basename_len = strlen(basename);
404 }
405 strncat(basename, field_name, PATH_MAX - basename_len);
2d2d14a7 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:
7b175edc 422 dprintf("%s\n", basename);
2d2d14a7 423 assert(td->size >= 0);
a67cd958 424 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
2d2d14a7 425 /* Not a named nested type : we must print its declaration first */
a67cd958 426 if(print_type_declaration(((field_t*)td->fields.array[0])->type,
2d2d14a7 427 fd, 0, basename, "")) return 1;
428 }
2e415130 429 fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename,
2d2d14a7 430 td->size);
7e97b039 431 fprintf(fd, "typedef ");
a67cd958 432 if(print_type(((field_t*)td->fields.array[0])->type,
433 fd, tabs, basename, "")) return 1;
2d2d14a7 434 fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename,
435 basename);
436 fprintf(fd, "\n");
437 break;
438 case SEQUENCE:
a67cd958 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) {
2d2d14a7 442 /* Not a named nested type : we must print its declaration first */
a67cd958 443 if(print_type_declaration(((field_t*)td->fields.array[1])->type,
2d2d14a7 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);
a3e6ce64 452 if(print_type(((field_t*)td->fields.array[0])->type,
453 fd, tabs, basename, "")) return 1;
454 fprintf(fd, "len;\n");
2d2d14a7 455 print_tabs(1, fd);
a67cd958 456 if(print_type(((field_t*)td->fields.array[1])->type,
457 fd, tabs, basename, "")) return 1;
2d2d14a7 458 fprintf(fd, " *array;\n");
459 fprintf(fd, "};\n");
460 fprintf(fd, "\n");
461 break;
462
463 case STRUCT:
464 for(unsigned int i=0;i<td->fields.position;i++){
465 field_t *field = (field_t*)(td->fields.array[i]);
466 type_descriptor_t *type = field->type;
467 if(type->type_name == NULL) {
468 /* Not a named nested type : we must print its declaration first */
469 if(print_type_declaration(type,
470 fd, 0, basename, field->name)) return 1;
471 }
472 }
473 fprintf(fd, "struct lttng_%s", basename);
474 fprintf(fd, " {\n");
475 for(unsigned int i=0;i<td->fields.position;i++){
476 field_t *field = (field_t*)(td->fields.array[i]);
477 type_descriptor_t *type = field->type;
478 print_tabs(1, fd);
47299663 479 if(print_type(type, fd, tabs, basename, field->name)) return 1;
2d2d14a7 480 fprintf(fd, " ");
481 fprintf(fd, "%s", field->name);
482 fprintf(fd, ";\n");
483 }
484 fprintf(fd, "};\n");
485 fprintf(fd, "\n");
486 break;
487 case UNION:
488 /* TODO : Do not allow variable length fields in a 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 */
47299663 494 if(print_type_declaration(type,
495 fd, 0, basename, field->name)) return 1;
2d2d14a7 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);
47299663 504 if(print_type(type, fd, tabs, basename, field->name)) return 1;
2d2d14a7 505 fprintf(fd, " ");
506 fprintf(fd, "%s", field->name);
507 fprintf(fd, ";\n");
508 }
509 fprintf(fd, "};\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
a67cd958 520
a3e6ce64 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)). */
a67cd958 527
a3e6ce64 528int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
529 char *nest_name, char *field_name)
a67cd958 530{
a3e6ce64 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);
a67cd958 541 } else {
a3e6ce64 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] != '_'
34c1d1b5 546 && field_name != NULL
a3e6ce64 547 && (field_name[0] != '\0'))) {
548 strncat(basename, "_", PATH_MAX - basename_len);
549 basename_len = strlen(basename);
a67cd958 550 }
34c1d1b5 551 if(field_name != NULL)
552 strncat(basename, field_name, PATH_MAX - basename_len);
a67cd958 553 }
a3e6ce64 554
555 switch(td->type) {
a67cd958 556 case INT_FIXED:
557 case UINT_FIXED:
a67cd958 558 case CHAR:
559 case UCHAR:
560 case SHORT:
561 case USHORT:
a3e6ce64 562 case INT:
563 case UINT:
564 case FLOAT:
565 case POINTER:
566 case LONG:
567 case ULONG:
568 case SIZE_T:
569 case SSIZE_T:
570 case OFF_T:
571 case ENUM:
572 fprintf(fd, "sizeof(");
2e415130 573 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 574 fprintf(fd, ")");
575 break;
576 case STRING:
577 fprintf(fd, "sizeof(char)");
578 break;
579 case SEQUENCE:
580 fprintf(fd, "lttng_get_alignment_sequence_%s(&obj->%s)", basename,
581 field_name);
582 break;
583 case STRUCT:
584 fprintf(fd, "lttng_get_alignment_struct_%s(&obj->%s)", basename,
585 field_name);
586 break;
587 case UNION:
588 fprintf(fd, "lttng_get_alignment_union_%s(&obj->%s)", basename,
589 field_name);
590 break;
591 case ARRAY:
592 fprintf(fd, "lttng_get_alignment_array_%s(obj->%s)", basename,
593 field_name);
594 break;
2e415130 595 case NONE:
596 printf("error : type NONE unexpected\n");
597 return 1;
598 break;
a3e6ce64 599 }
a67cd958 600
a3e6ce64 601 return 0;
a67cd958 602}
603
a3e6ce64 604/* print type write.
605 *
606 * Copied from construct_types_and_fields in LTTV facility.c
607 *
608 * basename is the name which identifies the type (along with a prefix
609 * (possibly)). */
a67cd958 610
a3e6ce64 611int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
612 char *nest_name, char *field_name)
a67cd958 613{
a67cd958 614 char basename[PATH_MAX];
615 unsigned int basename_len = 0;
a3e6ce64 616
617 strncpy(basename, nest_name, PATH_MAX);
a67cd958 618 basename_len = strlen(basename);
619
620 /* For a named type, we use the type_name directly */
621 if(td->type_name != NULL) {
622 strncpy(basename, td->type_name, PATH_MAX);
623 basename_len = strlen(basename);
624 } else {
a3e6ce64 625 /* For a unnamed type, there must be a field name, except for
626 * the array. */
a67cd958 627 if((basename_len != 0)
a3e6ce64 628 && (basename[basename_len-1] != '_'
629 && (field_name[0] != '\0'))) {
a67cd958 630 strncat(basename, "_", PATH_MAX - basename_len);
631 basename_len = strlen(basename);
632 }
633 strncat(basename, field_name, PATH_MAX - basename_len);
634 }
635
a67cd958 636 switch(td->type) {
637 case INT_FIXED:
638 case UINT_FIXED:
639 case CHAR:
640 case UCHAR:
641 case SHORT:
642 case USHORT:
643 case INT:
644 case UINT:
645 case FLOAT:
646 case POINTER:
647 case LONG:
648 case ULONG:
649 case SIZE_T:
650 case SSIZE_T:
651 case OFF_T:
652 case ENUM:
a67cd958 653 print_tabs(tabs, fd);
a3e6ce64 654 fprintf(fd, "size = ");
655 fprintf(fd, "sizeof(");
2e415130 656 if(print_type(td, fd, 0, basename, "")) return 1;
a67cd958 657 fprintf(fd, ");\n");
a67cd958 658 print_tabs(tabs, fd);
30d72138 659 fprintf(fd, "size += ltt_align(*to+*len, size) + size;\n");
a3e6ce64 660 print_tabs(tabs, fd);
661 fprintf(fd, "*len += size;");
a67cd958 662 break;
663 case STRING:
a67cd958 664 print_tabs(tabs, fd);
2e415130 665 fprintf(fd, "lttng_write_string_%s(buffer, to_base, to, from, len, obj->%s);\n", basename, field_name);
a3e6ce64 666 break;
667 case SEQUENCE:
668 print_tabs(tabs, fd);
34c1d1b5 669 fprintf(fd, "lttng_write_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
a3e6ce64 670 field_name);
671 break;
672 case STRUCT:
673 print_tabs(tabs, fd);
34c1d1b5 674 fprintf(fd, "lttng_write_struct_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
a3e6ce64 675 field_name);
676 break;
677 case UNION:
678 print_tabs(tabs, fd);
34c1d1b5 679 fprintf(fd, "lttng_write_union_%s(buffer, to_base, to, from, len, &obj->%s)", basename,
a3e6ce64 680 field_name);
a67cd958 681 break;
682 case ARRAY:
a67cd958 683 print_tabs(tabs, fd);
a3e6ce64 684 fprintf(fd, "lttng_write_array_%s(buffer, to_base, to, from, len, obj->%s)", basename,
685 field_name);
686 break;
2e415130 687 case NONE:
688 printf("Error : type NONE unexpected\n");
689 return 1;
690 break;
a3e6ce64 691 }
a67cd958 692
a3e6ce64 693 return 0;
694}
a67cd958 695
a67cd958 696
a67cd958 697
a3e6ce64 698/* print type alignment function.
699 *
700 * Copied from construct_types_and_fields in LTTV facility.c
701 *
702 * basename is the name which identifies the type (along with a prefix
703 * (possibly)). */
704
705int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
706 unsigned int tabs,
707 char *nest_name, char *field_name)
708{
709 char basename[PATH_MAX];
710 unsigned int basename_len = 0;
711
712 strncpy(basename, nest_name, PATH_MAX);
713 basename_len = strlen(basename);
714
715 /* For a named type, we use the type_name directly */
716 if(td->type_name != NULL) {
717 strncpy(basename, td->type_name, PATH_MAX);
718 basename_len = strlen(basename);
719 } else {
720 /* For a unnamed type, there must be a field name, except for
721 * the array. */
722 if((basename_len != 0)
723 && (basename[basename_len-1] != '_'
724 && (field_name[0] != '\0'))) {
725 strncat(basename, "_", PATH_MAX - basename_len);
726 basename_len = strlen(basename);
727 }
728 strncat(basename, field_name, PATH_MAX - basename_len);
729 }
730
731 switch(td->type) {
732 case SEQUENCE:
733 /* Function header */
734 fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
735 basename);
736 print_tabs(2, fd);
2e415130 737 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 738 fprintf(fd, " *obj)\n");
739 fprintf(fd, "{\n");
740 print_tabs(1, fd);
741 fprintf(fd, "size_t align=0, localign;");
742 fprintf(fd, "\n");
743 print_tabs(1, fd);
744 fprintf(fd, "localign = ");
745 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
2e415130 746 fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1;
a3e6ce64 747 fprintf(fd, ";\n");
748 print_tabs(1, fd);
749 fprintf(fd, "align = max(align, localign);\n");
750 fprintf(fd, "\n");
751 print_tabs(1, fd);
752 fprintf(fd, "localign = ");
753 if(print_type_alignment(((field_t*)td->fields.array[1])->type,
2e415130 754 fd, 0, basename, ((field_t*)td->fields.array[1])->name)) return 1;
a3e6ce64 755 fprintf(fd, ";\n");
756 print_tabs(1, fd);
757 fprintf(fd, "align = max(align, localign);\n");
758 fprintf(fd, "\n");
759 print_tabs(1, fd);
760 fprintf(fd, "return align;\n");
761 break;
762 case STRUCT:
763 /* Function header */
764 fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
765 basename);
766 print_tabs(2, fd);
2e415130 767 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 768 fprintf(fd, " *obj)\n");
769 fprintf(fd, "{\n");
770 print_tabs(1, fd);
771 fprintf(fd, "size_t align=0, localign;");
772 fprintf(fd, "\n");
773 for(unsigned int i=0;i<td->fields.position;i++){
774 field_t *field = (field_t*)(td->fields.array[i]);
775 type_descriptor_t *type = field->type;
776 print_tabs(1, fd);
777 fprintf(fd, "localign = ");
778 if(print_type_alignment(type, fd, 0, basename, field->name)) return 1;
779 fprintf(fd, ";\n");
780 print_tabs(1, fd);
781 fprintf(fd, "align = max(align, localign);\n");
782 fprintf(fd, "\n");
783 }
784 print_tabs(1, fd);
785 fprintf(fd, "return align;\n");
786
a67cd958 787 break;
a3e6ce64 788 case UNION:
789 /* Function header */
790 fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
791 basename);
792 print_tabs(2, fd);
2e415130 793 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 794 fprintf(fd, " *obj)\n");
795 fprintf(fd, "{\n");
796 print_tabs(1, fd);
797 fprintf(fd, "size_t align=0, localign;");
798 fprintf(fd, "\n");
799 for(unsigned int i=0;i<td->fields.position;i++){
800 field_t *field = (field_t*)(td->fields.array[i]);
801 type_descriptor_t *type = field->type;
802 print_tabs(1, fd);
803 fprintf(fd, "localign = ");
804 if(print_type_alignment(type, fd, 0, basename, field->name)) return 1;
805 fprintf(fd, ";\n");
806 print_tabs(1, fd);
807 fprintf(fd, "align = max(align, localign);\n");
808 fprintf(fd, "\n");
809 }
810 print_tabs(1, fd);
811 fprintf(fd, "return align;\n");
812
813 break;
814 case ARRAY:
815 /* Function header */
816 fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
817 basename);
818 print_tabs(2, fd);
2e415130 819 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 820 fprintf(fd, " obj)\n");
821 fprintf(fd, "{\n");
822 print_tabs(1, fd);
823 fprintf(fd, "return \n");
824 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
2e415130 825 fd, 0, basename, ((field_t*)td->fields.array[0])->name)) return 1;
a3e6ce64 826 fprintf(fd, ";\n");
827 break;
828 default:
829 printf("print_type_alignment_fct : type has no alignment function.\n");
34c1d1b5 830 return 0;
a3e6ce64 831 break;
832 }
833
834
835 /* Function footer */
836 fprintf(fd, "}\n");
837 fprintf(fd, "\n");
838
2e415130 839 return 0;
a3e6ce64 840}
841
842/* print type write function.
843 *
844 * Copied from construct_types_and_fields in LTTV facility.c
845 *
846 * basename is the name which identifies the type (along with a prefix
847 * (possibly)). */
848
849int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
850 char *nest_name, char *field_name)
851{
852 char basename[PATH_MAX];
853 unsigned int basename_len = 0;
854
855 strncpy(basename, nest_name, PATH_MAX);
856 basename_len = strlen(basename);
857
858 /* For a named type, we use the type_name directly */
859 if(td->type_name != NULL) {
860 strncpy(basename, td->type_name, PATH_MAX);
861 basename_len = strlen(basename);
862 } else {
863 /* For a unnamed type, there must be a field name, except for
864 * the array. */
865 if((basename_len != 0)
866 && (basename[basename_len-1] != '_'
867 && (field_name[0] != '\0'))) {
868 strncat(basename, "_", PATH_MAX - basename_len);
869 basename_len = strlen(basename);
870 }
871 strncat(basename, field_name, PATH_MAX - basename_len);
872 }
873
34c1d1b5 874 switch(td->type) {
875 case SEQUENCE:
876 case STRUCT:
877 case UNION:
878 case ARRAY:
879 break;
880 default:
881 printf("print_type_write_fct : type has no write function.\n");
882 return 0;
883 break;
884 }
885
a3e6ce64 886 /* Print header */
887 switch(td->type) {
a67cd958 888 case SEQUENCE:
34c1d1b5 889 fprintf(fd, "static inline void lttng_write_sequence_%s(\n",
a3e6ce64 890 basename);
a67cd958 891 break;
a3e6ce64 892 case STRUCT:
34c1d1b5 893 fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename,
a3e6ce64 894 field_name);
a67cd958 895 break;
a3e6ce64 896 case UNION:
34c1d1b5 897 fprintf(fd, "static inline void lttng_write_union_%s(\n", basename,
a3e6ce64 898 field_name);
899 break;
900 case ARRAY:
34c1d1b5 901 fprintf(fd, "static inline void lttng_write_array_%s(\n", basename,
a3e6ce64 902 field_name);
903 break;
904 default:
905 printf("print_type_write_fct : type has no write function.\n");
a67cd958 906 break;
a67cd958 907 }
908
a3e6ce64 909 print_tabs(2, fd);
910 fprintf(fd, "void *buffer,\n");
911 print_tabs(2, fd);
912 fprintf(fd, "size_t *to_base,\n");
913 print_tabs(2, fd);
914 fprintf(fd, "size_t *to,\n");
915 print_tabs(2, fd);
916 fprintf(fd, "void **from,\n");
917 print_tabs(2, fd);
918 fprintf(fd, "size_t *len,\n");
919 print_tabs(2, fd);
2e415130 920 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 921
922 switch(td->type) {
923 case SEQUENCE:
924 fprintf(fd, " *obj)\n");
925 break;
926 case STRUCT:
927 fprintf(fd, " *obj)\n");
928 break;
929 case UNION:
930 fprintf(fd, " *obj)\n");
931 break;
932 case ARRAY:
933 fprintf(fd, " obj)\n");
934 break;
935 default:
936 printf("print_type_write_fct : type has no write function.\n");
937 break;
938 }
939
940 fprintf(fd, "{\n");
941 print_tabs(1, fd);
942 fprintf(fd, "size_t align, size;\n");
943 fprintf(fd, "\n");
944
945 switch(td->type) {
946 case SEQUENCE:
947 case STRING:
948 print_tabs(1, fd);
949 fprintf(fd, "/* Flush pending memcpy */\n");
950 print_tabs(1, fd);
951 fprintf(fd, "if(*len != 0) {\n");
952 print_tabs(2, fd);
953 fprintf(fd, "if(buffer != NULL)\n");
954 print_tabs(3, fd);
955 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
956 print_tabs(1, fd);
957 fprintf(fd, "}\n");
958 print_tabs(1, fd);
959 fprintf(fd, "*to += *len;\n");
960 print_tabs(1, fd);
961 fprintf(fd, "*len = 0;\n");
962 fprintf(fd, "\n");
963 break;
964 case STRUCT:
965 case UNION:
966 case ARRAY:
967 break;
968 default:
969 printf("print_type_write_fct : type has no write function.\n");
970 break;
971 }
972
973 print_tabs(1, fd);
34c1d1b5 974 fprintf(fd, "align = ");
2e415130 975 if(print_type_alignment(td, fd, 0, basename, field_name)) return 1;
a3e6ce64 976 fprintf(fd, ";\n");
977 fprintf(fd, "\n");
978 print_tabs(1, fd);
979 fprintf(fd, "if(*len == 0) {\n");
980 print_tabs(2, fd);
981 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
982 print_tabs(1, fd);
983 fprintf(fd, "} else {\n");
984 print_tabs(2, fd);
985 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
986 print_tabs(1, fd);
987 fprintf(fd, "}\n");
988 fprintf(fd, "\n");
989
990 /* First, check if the type has a fixed size. If it is the case, then the size
991 * to write is know by the compiler : simply use a sizeof() */
992 if(has_type_fixed_size(td)) {
993 print_tabs(1, fd);
994 fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n");
995 fprintf(fd, "\n");
996 print_tabs(1, fd);
997 fprintf(fd, "*len += sizeof(");
2e415130 998 if(print_type(td, fd, 0, basename, field_name)) return 1;
a3e6ce64 999 fprintf(fd, ");\n");
1000 } else {
1001 /* The type contains nested variable size subtypes :
1002 * we must write field by field. */
1003 print_tabs(1, fd);
1004 fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n");
1005 fprintf(fd, "\n");
1006
1007 switch(td->type) {
1008 case SEQUENCE:
1009 print_tabs(1, fd);
1010 fprintf(fd, "/* Copy members */\n");
1011 print_tabs(1, fd);
1012 fprintf(fd, "size = sizeof(\n");
1013 if(print_type_write(((field_t*)td->fields.array[0])->type,
2e415130 1014 fd, 1, basename, ((field_t*)td->fields.array[0])->name)) return 1;
a3e6ce64 1015 fprintf(fd, ");\n");
1016 print_tabs(1, fd);
1017 fprintf(fd, "*to += ltt_align(*to, size);\n");
1018 print_tabs(1, fd);
1019 fprintf(fd, "if(buffer != NULL)\n");
1020 print_tabs(2, fd);
1021 fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, size);\n");
1022 print_tabs(1, fd);
1023 fprintf(fd, "*to += size;\n");
1024 fprintf(fd, "\n");
1025
1026 /* Write the child : varlen child or not ? */
1027 if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) {
1028 /* Fixed size len child : use a multiplication of its size */
1029 print_tabs(1, fd);
1030 fprintf(fd, "size = sizeof(\n");
1031 if(print_type_write(((field_t*)td->fields.array[1])->type,
2e415130 1032 fd, 1, basename, ((field_t*)td->fields.array[1])->name)) return 1;
a3e6ce64 1033 fprintf(fd, ");\n");
1034 print_tabs(1, fd);
1035 fprintf(fd, "*to += ltt_align(*to, size);\n");
1036 print_tabs(1, fd);
1037 fprintf(fd, "size = obj->len * size;\n");
1038 print_tabs(1, fd);
1039 fprintf(fd, "if(buffer != NULL)\n");
1040 print_tabs(2, fd);
1041 fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, size);\n");
1042 print_tabs(1, fd);
1043 fprintf(fd, "*to += size;\n");
1044 fprintf(fd, "\n");
1045 } else {
1046 print_tabs(1, fd);
1047 fprintf(fd, "/* Variable length child : iter. */\n");
1048 print_tabs(1, fd);
1049 fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n");
1050 if(print_type_write(((field_t*)td->fields.array[1])->type,
2e415130 1051 fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1;
a3e6ce64 1052 print_tabs(1, fd);
1053 fprintf(fd, "}\n");
1054 }
1055 fprintf(fd, "\n");
1056 print_tabs(1, fd);
1057 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1058 print_tabs(1, fd);
1059 fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n");
1060 print_tabs(1, fd);
1061 fprintf(fd, "*to_base = *to_base+*to;\n");
1062 print_tabs(1, fd);
1063 fprintf(fd, "*to = 0;\n");
1064 fprintf(fd, "\n");
1065 fprintf(fd, "/* Put source *from just after the C sequence */\n");
1066 print_tabs(1, fd);
1067 fprintf(fd, "*from = obj+1;\n");
1068 break;
1069 case STRING:
1070 fprintf(fd, "size = strlen(obj);\n");
1071 print_tabs(1, fd);
1072 fprintf(fd, "if(buffer != NULL)\n");
1073 print_tabs(2, fd);
1074 fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n");
1075 print_tabs(1, fd);
1076 fprintf(fd, "*to += size;\n");
1077 fprintf(fd, "\n");
1078 print_tabs(1, fd);
1079 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1080 print_tabs(1, fd);
1081 fprintf(fd, "*to = ltt_align(*to, sizeof(void *));\n");
1082 print_tabs(1, fd);
1083 fprintf(fd, "*to_base = *to_base+*to;\n");
1084 print_tabs(1, fd);
1085 fprintf(fd, "*to = 0;\n");
1086 fprintf(fd, "\n");
1087 fprintf(fd, "/* Put source *from just after the C sequence */\n");
1088 print_tabs(1, fd);
1089 fprintf(fd, "*from = obj+1;\n");
1090 break;
1091 case STRUCT:
1092 for(unsigned int i=0;i<td->fields.position;i++){
1093 field_t *field = (field_t*)(td->fields.array[i]);
1094 type_descriptor_t *type = field->type;
1095 if(print_type_write(type,
1096 fd, 1, basename, field->name)) return 1;
1097 fprintf(fd, "\n");
1098 }
1099 break;
1100 case UNION:
1101 printf("ERROR : A union CANNOT contain a variable size child.\n");
1102 return 1;
1103 break;
1104 case ARRAY:
1105 /* Write the child : varlen child or not ? */
1106 if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) {
1107 /* Error : if an array has a variable size, then its child must also
1108 * have a variable size. */
1109 assert(0);
1110 } else {
1111 print_tabs(1, fd);
1112 fprintf(fd, "/* Variable length child : iter. */\n");
1113 print_tabs(1, fd);
1114 fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename);
1115 if(print_type_write(((field_t*)td->fields.array[1])->type,
2e415130 1116 fd, 2, basename, ((field_t*)td->fields.array[1])->name)) return 1;
a3e6ce64 1117 print_tabs(1, fd);
1118 fprintf(fd, "}\n");
1119 }
1120 break;
1121 default:
1122 printf("print_type_write_fct : type has no write function.\n");
1123 break;
1124 }
1125
1126
1127 }
1128
1129
1130 /* Function footer */
1131 fprintf(fd, "}\n");
1132 fprintf(fd, "\n");
a67cd958 1133 return 0;
1134}
1135
1136
1137
47299663 1138/* Print the logging function of an event. This is the core of genevent */
a3e6ce64 1139int print_event_logging_function(char *basename, facility_t *fac,
1140 event_t *event, FILE *fd)
47299663 1141{
1142 fprintf(fd, "static inline void trace_%s(\n", basename);
1143 for(unsigned int j = 0; j < event->fields.position; j++) {
1144 /* For each field, print the function argument */
1145 field_t *f = (field_t*)event->fields.array[j];
1146 type_descriptor_t *t = f->type;
7e97b039 1147 if(print_arg(t, fd, 2, basename, f->name)) return 1;
47299663 1148 if(j < event->fields.position-1) {
1149 fprintf(fd, ",");
1150 fprintf(fd, "\n");
1151 }
1152 }
1153 if(event->fields.position == 0) {
1154 print_tabs(2, fd);
1155 fprintf(fd, "void");
1156 }
1157 fprintf(fd,")\n");
1158 fprintf(fd, "#ifndef CONFIG_LTT\n");
1159 fprintf(fd, "{\n");
1160 fprintf(fd, "}\n");
1161 fprintf(fd,"#else\n");
1162 fprintf(fd, "{\n");
7e97b039 1163 /* Print the function variables */
a67cd958 1164 print_tabs(1, fd);
a3e6ce64 1165 fprintf(fd, "unsigned int index;\n");
1166 print_tabs(1, fd);
1167 fprintf(fd, "struct ltt_channel_struct *channel;\n");
1168 print_tabs(1, fd);
1169 fprintf(fd, "struct ltt_trace_struct *trace;\n");
1170 print_tabs(1, fd);
1171 fprintf(fd, "struct rchan_buf *relayfs_buf;\n");
1172 print_tabs(1, fd);
1173 fprintf(fd, "void *buffer = NULL;\n");
1174 print_tabs(1, fd);
1175 fprintf(fd, "size_t to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1176 print_tabs(1, fd);
1177 fprintf(fd, "size_t to = 0;\n");
1178 print_tabs(1, fd);
1179 fprintf(fd, "void *from;");
1180 print_tabs(1, fd);
1181 fprintf(fd, "size_t len = 0;\n");
30d72138 1182 print_tabs(1, fd);
a3e6ce64 1183 fprintf(fd, "size_t slot_size;\n");
30d72138 1184 print_tabs(1, fd);
a3e6ce64 1185 fprintf(fd, "cycles_t tsc;\n");
30d72138 1186 print_tabs(1, fd);
a3e6ce64 1187 fprintf(fd, "size_t before_hdr_pad, size_t after_hdr_pad;\n");
1188 fprintf(fd, "\n");
7e97b039 1189
a3e6ce64 1190 print_tabs(1, fd);
1191 fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n");
1192 fprintf(fd, "\n");
1193
a67cd958 1194 /* Calculate event variable len + event data alignment offset.
7e97b039 1195 * Assume that the padding for alignment starts at a void*
a67cd958 1196 * address.
1197 * This excludes the header size and alignment. */
1198
a3e6ce64 1199 print_tabs(1, fd);
1200 fprintf(fd, "/* For each field, calculate the field size. */\n");
1201 print_tabs(1, fd);
1202 fprintf(fd, "/* size = to_base + to + len */\n");
1203 print_tabs(1, fd);
30d72138 1204 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
a3e6ce64 1205 print_tabs(1, fd);
30d72138 1206 fprintf(fd, " * sizeof(void *) address. */\n");
a3e6ce64 1207 fprintf(fd, "\n");
a67cd958 1208
a3e6ce64 1209 for(unsigned int i=0;i<event->fields.position;i++){
2e415130 1210 field_t *field = (field_t*)(event->fields.array[i]);
a3e6ce64 1211 type_descriptor_t *type = field->type;
1212 if(print_type_write(type,
1213 fd, 1, basename, field->name)) return 1;
1214 fprintf(fd, "\n");
a67cd958 1215 }
30d72138 1216 fprintf(fd, "\n");
47299663 1217
7e97b039 1218 /* Take locks : make sure the trace does not vanish while we write on
1219 * it. A simple preemption disabling is enough (using rcu traces). */
a3e6ce64 1220 print_tabs(1, fd);
1221 fprintf(fd, "preempt_disable();\n");
1222 print_tabs(1, fd);
1223 fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n");
1224
1225 /* Get facility index */
1226
1227 if(event->per_tracefile) {
1228 print_tabs(1, fd);
1229 fprintf(fd, "index = tracefile_index;\n");
1230 } else {
1231 print_tabs(1, fd);
1232 fprintf(fd,
1233 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
30d72138 1234 "\t\t\t\t\t\tevent_%s);\n",
a3e6ce64 1235 fac->name, fac->checksum, event->name);
1236 }
1237 fprintf(fd,"\n");
1238
7e97b039 1239
1240 /* For each trace */
a3e6ce64 1241 print_tabs(1, fd);
1242 fprintf(fd, "list_for_each_entry_rcu(trace, &ltt_traces.head, list) {\n");
1243 print_tabs(2, fd);
1244 fprintf(fd, "if(!trace->active) continue;\n\n");
1245
1246 if(event->per_trace) {
1247 print_tabs(2, fd);
1248 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
1249 }
1250
1251 print_tabs(2, fd);
2e415130 1252 fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
a3e6ce64 1253 print_tabs(2, fd);
1254 fprintf(fd, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1255 fprintf(fd, "\n");
1256
7e97b039 1257
1258 /* Relay reserve */
a3e6ce64 1259 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1260 * return */
1261 print_tabs(2, fd);
1262 fprintf(fd, "slot_size = 0;\n");
1263 print_tabs(2, fd);
30d72138 1264 fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1265 print_tabs(3, fd);
1266 fprintf(fd, "to_base + to + len, &slot_size, &tsc,\n");
1267 print_tabs(3, fd);
1268 fprintf(fd, "&before_hdr_pad, &after_hdr_pad);\n");
a3e6ce64 1269 /* If error, return */
1270 print_tabs(2, fd);
1271 fprintf(fd, "if(!buffer) return;\n\n");
7e97b039 1272
a3e6ce64 1273 /* write data : assume stack alignment is the same as struct alignment. */
1274
1275 for(unsigned int i=0;i<event->fields.position;i++){
2e415130 1276 field_t *field = (field_t*)(event->fields.array[i]);
a3e6ce64 1277 type_descriptor_t *type = field->type;
1278
1279 /* Set from */
30d72138 1280 print_tabs(2, fd);
1281 switch(type->type) {
1282 case SEQUENCE:
1283 case UNION:
1284 case ARRAY:
1285 case STRUCT:
1286 case STRING:
1287 fprintf(fd, "from = %s;\n", field->name);
1288 break;
1289 default:
1290 fprintf(fd, "from = &%s;\n", field->name);
1291 break;
1292 }
1293
a3e6ce64 1294
1295 if(print_type_write(type,
1296 fd, 2, basename, field->name)) return 1;
1297 fprintf(fd, "\n");
7e97b039 1298
a3e6ce64 1299 /* Don't forget to flush pending memcpy */
1300 print_tabs(2, fd);
1301 fprintf(fd, "/* Flush pending memcpy */\n");
1302 print_tabs(2, fd);
1303 fprintf(fd, "if(len != 0) {\n");
1304 print_tabs(3, fd);
1305 fprintf(fd, "memcpy(buffer+to_base+to, from, len);\n");
1306 print_tabs(3, fd);
1307 fprintf(fd, "to += len;\n");
1308 //print_tabs(3, fd);
1309 //fprintf(fd, "from += len;\n");
1310 print_tabs(3, fd);
1311 fprintf(fd, "len = 0;\n");
1312 print_tabs(2, fd);
1313 fprintf(fd, "}\n");
1314 fprintf(fd, "\n");
1315 }
1316
1317
7e97b039 1318 /* commit */
a3e6ce64 1319 print_tabs(2, fd);
1320 fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
7e97b039 1321
a3e6ce64 1322 print_tabs(1, fd);
1323 fprintf(fd, "}\n\n");
1324
7e97b039 1325 /* Release locks */
a3e6ce64 1326 print_tabs(1, fd);
1327 fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n");
1328 print_tabs(1, fd);
1329 fprintf(fd, "preempt_enable_no_resched();\n");
2d2d14a7 1330
47299663 1331 fprintf(fd, "}\n");
1332 fprintf(fd, "#endif //CONFIG_LTT\n\n");
1333 return 0;
1334}
2d2d14a7 1335
1336
1337/* ltt-facility-name.h : main logging header.
1338 * log_header */
1339
1340void print_log_header_head(facility_t *fac, FILE *fd)
1341{
1342 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
1343 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
47299663 1344 fprintf(fd, "\n");
1345 fprintf(fd, "/* Facility activation at compile time. */\n");
1346 fprintf(fd, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
2d2d14a7 1347}
1348
1349
1350
2d2d14a7 1351int print_log_header_types(facility_t *fac, FILE *fd)
1352{
1353 sequence_t *types = &fac->named_types.values;
1354 fprintf(fd, "/* Named types */\n");
1355 fprintf(fd, "\n");
1356
1357 for(unsigned int i = 0; i < types->position; i++) {
1358 /* For each named type, print the definition */
1359 if((print_type_declaration(types->array[i], fd,
1360 0, "", ""))) return 1;
34c1d1b5 1361 /* Print also the align function */
1362 if((print_type_alignment_fct(types->array[i], fd,
1363 0, "", ""))) return 1;
1364 /* Print also the write function */
1365 if((print_type_write_fct(types->array[i], fd,
1366 0, "", ""))) return 1;
2d2d14a7 1367 }
1368 return 0;
1369}
1370
1371int print_log_header_events(facility_t *fac, FILE *fd)
1372{
47299663 1373 sequence_t *events = &fac->events;
1374 char basename[PATH_MAX];
1375 unsigned int facname_len;
1376
1377 strncpy(basename, fac->name, PATH_MAX);
1378 facname_len = strlen(basename);
1379 strncat(basename, "_", PATH_MAX-facname_len);
1380 facname_len = strlen(basename);
1381
1382 for(unsigned int i = 0; i < events->position; i++) {
1383 event_t *event = (event_t*)events->array[i];
1384 strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len);
1385
1386 /* For each event, print structure, and then logging function */
1387 fprintf(fd, "/* Event %s structures */\n",
1388 event->name);
1389 for(unsigned int j = 0; j < event->fields.position; j++) {
1390 /* For each unnamed type, print the definition */
1391 field_t *f = (field_t*)event->fields.array[j];
1392 type_descriptor_t *t = f->type;
34c1d1b5 1393 if(t->type_name == NULL) {
47299663 1394 if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
34c1d1b5 1395 /* Print also the align function */
1396 if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1;
1397 /* Print also the write function */
1398 if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1;
1399 }
47299663 1400 }
34c1d1b5 1401
47299663 1402 fprintf(fd, "\n");
2d2d14a7 1403
47299663 1404 fprintf(fd, "/* Event %s logging function */\n",
1405 event->name);
1406
a3e6ce64 1407 if(print_event_logging_function(basename, fac, event, fd)) return 1;
47299663 1408
1409 fprintf(fd, "\n");
1410 }
1411
2d2d14a7 1412 return 0;
1413}
1414
1415
1416void print_log_header_tail(facility_t *fac, FILE *fd)
1417{
47299663 1418 fprintf(fd, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
2d2d14a7 1419 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
1420}
1421
1422int print_log_header(facility_t *fac)
1423{
1424 char filename[PATH_MAX];
1425 unsigned int filename_size = 0;
1426 FILE *fd;
1427 dprintf("%s\n", fac->name);
1428
1429 strcpy(filename, "ltt-facility-");
1430 filename_size = strlen(filename);
1431
1432 strncat(filename, fac->name, PATH_MAX - filename_size);
1433 filename_size = strlen(filename);
1434
1435 strncat(filename, ".h", PATH_MAX - filename_size);
1436 filename_size = strlen(filename);
1437
1438
1439 fd = fopen(filename, "w");
1440 if(fd == NULL) {
1441 printf("Error opening file %s for writing : %s\n",
1442 filename, strerror(errno));
1443 return errno;
1444 }
1445
1446 /* Print file head */
1447 print_log_header_head(fac, fd);
1448
1449 /* print named types in declaration order */
1450 if(print_log_header_types(fac, fd)) return 1;
1451
1452 /* Print events */
1453 if(print_log_header_events(fac, fd)) return 1;
1454
1455 /* Print file tail */
1456 print_log_header_tail(fac, fd);
1457
1458
1459 fclose(fd);
1460
1461 return 0;
1462}
1463
1464
1465/* ltt-facility-id-name.h : facility id.
1466 * log_id_header */
1467int print_id_header(facility_t *fac)
1468{
1469 char filename[PATH_MAX];
1470 unsigned int filename_size = 0;
1471 FILE *fd;
1472 dprintf("%s\n", fac->name);
1473
1474 strcpy(filename, "ltt-facility-id-");
1475 filename_size = strlen(filename);
1476
1477 strncat(filename, fac->name, PATH_MAX - filename_size);
1478 filename_size = strlen(filename);
1479
1480 strncat(filename, ".h", PATH_MAX - filename_size);
1481 filename_size = strlen(filename);
1482
1483
1484 fd = fopen(filename, "w");
1485 if(fd == NULL) {
1486 printf("Error opening file %s for writing : %s\n",
1487 filename, strerror(errno));
1488 return errno;
1489 }
1490
1491 fclose(fd);
1492
1493 return 0;
1494}
1495
1496
1497/* ltt-facility-loader-name.h : facility specific loader info.
1498 * loader_header */
1499int print_loader_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-loader-");
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 fclose(fd);
1524
1525 return 0;
1526}
1527
1528/* ltt-facility-loader-name.c : generic faciilty loader
1529 * loader_c */
1530int print_loader_c(facility_t *fac)
1531{
1532 char filename[PATH_MAX];
1533 unsigned int filename_size = 0;
1534 FILE *fd;
1535 dprintf("%s\n", fac->name);
1536
1537 strcpy(filename, "ltt-facility-loader-");
1538 filename_size = strlen(filename);
1539
1540 strncat(filename, fac->name, PATH_MAX - filename_size);
1541 filename_size = strlen(filename);
1542
1543 strncat(filename, ".c", PATH_MAX - filename_size);
1544 filename_size = strlen(filename);
1545
1546
1547 fd = fopen(filename, "w");
1548 if(fd == NULL) {
1549 printf("Error opening file %s for writing : %s\n",
1550 filename, strerror(errno));
1551 return errno;
1552 }
1553
1554
1555 fclose(fd);
1556
1557 return 0;
1558}
1559
1560
1561
92d82357 1562/* open facility */
1563/* code taken from ltt_facility_open in ltt/facility.c in lttv */
1564
1565/*****************************************************************************
1566 *Function name
1567 * ltt_facility_open : open facilities
1568 *Input params
1569 * pathname : the path name of the facility
1570 *
1571 * Open the facility corresponding to the right checksum.
1572 *
1573 *returns the facility on success, NULL on error.
1574 ****************************************************************************/
1575facility_t *ltt_facility_open(char * pathname)
1576{
1577 int ret = 0;
1578 char *token;
1579 parse_file_t in;
1580 facility_t * fac = NULL;
92d82357 1581 char buffer[BUFFER_SIZE];
1582 int generated = FALSE;
1583
1584 in.buffer = &(buffer[0]);
1585 in.lineno = 0;
1586 in.error = error_callback;
1587 in.name = pathname;
1588 in.unget = 0;
1589
1590 in.fp = fopen(in.name, "r");
1591 if(in.fp == NULL) {
1592 ret = 1;
1593 goto open_error;
1594 }
1595
1596 while(1){
1597 token = getToken(&in);
1598 if(in.type == ENDFILE) break;
1599
1600 if(generated) {
1601 printf("More than one facility in the file. Only using the first one.\n");
1602 break;
1603 }
1604
1605 if(strcmp(token, "<")) in.error(&in,"not a facility file");
1606 token = getName(&in);
1607
1608 if(strcmp("facility",token) == 0) {
1609 fac = malloc(sizeof(facility_t));
1610 fac->name = NULL;
1611 fac->description = NULL;
1612 sequence_init(&(fac->events));
1613 table_init(&(fac->named_types));
1614 sequence_init(&(fac->unnamed_types));
1615
1616 parseFacility(&in, fac);
1617
1618 //check if any namedType is not defined
1619 checkNamedTypesImplemented(&fac->named_types);
1620
2e415130 1621 generateChecksum(fac->name, &fac->checksum, &fac->events);
a67cd958 1622
92d82357 1623 generated = TRUE;
1624 }
1625 else {
1626 printf("facility token was expected in file %s\n", in.name);
1627 ret = 1;
1628 goto parse_error;
1629 }
1630 }
1631
1632 parse_error:
1633 fclose(in.fp);
1634open_error:
1635
1636 if(!generated) {
1637 printf("Cannot find facility %s\n", pathname);
1638 fac = NULL;
1639 }
1640
1641 return fac;
1642}
1643
1644/* Close the facility */
1645void ltt_facility_close(facility_t *fac)
1646{
1647 free(fac->name);
1648 free(fac->capname);
1649 free(fac->description);
1650 freeEvents(&fac->events);
1651 sequence_dispose(&fac->events);
1652 freeNamedType(&fac->named_types);
1653 table_dispose(&fac->named_types);
1654 freeTypes(&fac->unnamed_types);
1655 sequence_dispose(&fac->unnamed_types);
1656 free(fac);
1657}
1658
1659
1660/* Show help */
1661void show_help(int argc, char ** argv)
1662{
1663 printf("Genevent help : \n");
1664 printf("\n");
1665 printf("Use %s name.xml\n", argv[0]);
1666 printf("to create :\n");
1667 printf("ltt-facility-name.h\n");
1668 printf("ltt-facility-id-name.h\n");
1669 printf("ltt-facility-loader-name.h\n");
1670 printf("ltt-facility-loader-name.c\n");
1671 printf("In the current directory.\n");
1672 printf("\n");
1673}
1674
1675/* Parse program arguments */
1676/* Return values :
1677 * 0 : continue program
1678 * -1 : stop program, return 0
1679 * > 0 : stop program, return value as exit.
1680 */
1681int check_args(int argc, char **argv)
1682{
1683 if(argc < 2) {
1684 printf("Not enough arguments\n");
1685 show_help(argc, argv);
1686 return EINVAL;
1687 }
1688
1689 if(strcmp(argv[1], "-h") == 0) {
1690 show_help(argc, argv);
1691 return -1;
1692 }
1693
1694 return 0;
1695}
1696
1697int main(int argc, char **argv)
1698{
1699 int err = 0;
1700 facility_t *fac;
1701
1702 err = check_args(argc, argv);
1703 if(err > 0) return err;
1704 else if(err < 0) return 0;
1705
1706 /* open the facility */
1707 fac = ltt_facility_open(argv[1]);
1708 if(fac == NULL) {
1709 printf("Error opening file %s for reading : %s\n",
1710 argv[1], strerror(errno));
1711 return errno;
1712 }
1713
1714 /* generate the output C files */
1715
1716
2d2d14a7 1717 /* ltt-facility-name.h : main logging header.
1718 * log_header */
1719 err = print_log_header(fac);
1720 if(err) return err;
92d82357 1721
2d2d14a7 1722 /* ltt-facility-id-name.h : facility id.
1723 * log_id_header */
1724 err = print_id_header(fac);
1725 if(err) return err;
92d82357 1726
2d2d14a7 1727 /* ltt-facility-loader-name.h : facility specific loader info.
1728 * loader_header */
1729 err = print_loader_header(fac);
1730 if(err) return err;
1731
1732 /* ltt-facility-loader-name.c : generic faciilty loader
1733 * loader_c */
1734 err = print_loader_c(fac);
1735 if(err) return err;
92d82357 1736
1737 /* close the facility */
1738 ltt_facility_close(fac);
1739
1740 return 0;
1741}
1742
1743
This page took 0.098389 seconds and 4 git commands to generate.