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