compile and runtime fixes
[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);
a3e6ce64 657 fprintf(fd, "size += ltt_align(*to+*len, size) + size;");
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");
1170 fprintf(fd, "\n");
1171 fprintf(fd, "size_t slot_size;\n");
1172 fprintf(fd, "\n");
1173 fprintf(fd, "cycles_t tsc;\n");
1174 fprintf(fd, "\n");
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);
1192 fprintf(fd, "/* Assume that the padding for alignment starts at a void*\n");
1193 print_tabs(1, fd);
1194 fprintf(fd, "/* address. */\n");
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 }
47299663 1204
7e97b039 1205 /* Take locks : make sure the trace does not vanish while we write on
1206 * it. A simple preemption disabling is enough (using rcu traces). */
a3e6ce64 1207 print_tabs(1, fd);
1208 fprintf(fd, "preempt_disable();\n");
1209 print_tabs(1, fd);
1210 fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n");
1211
1212 /* Get facility index */
1213
1214 if(event->per_tracefile) {
1215 print_tabs(1, fd);
1216 fprintf(fd, "index = tracefile_index;\n");
1217 } else {
1218 print_tabs(1, fd);
1219 fprintf(fd,
1220 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
1221 "\t\t\t\tevent_%s);\n",
1222 fac->name, fac->checksum, event->name);
1223 }
1224 fprintf(fd,"\n");
1225
7e97b039 1226
1227 /* For each trace */
a3e6ce64 1228 print_tabs(1, fd);
1229 fprintf(fd, "list_for_each_entry_rcu(trace, &ltt_traces.head, list) {\n");
1230 print_tabs(2, fd);
1231 fprintf(fd, "if(!trace->active) continue;\n\n");
1232
1233 if(event->per_trace) {
1234 print_tabs(2, fd);
1235 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
1236 }
1237
1238 print_tabs(2, fd);
2e415130 1239 fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
a3e6ce64 1240 print_tabs(2, fd);
1241 fprintf(fd, "relayfs_buf = channel->rchan->buf[channel->rchan->buf->cpu];\n");
1242 fprintf(fd, "\n");
1243
7e97b039 1244
1245 /* Relay reserve */
a3e6ce64 1246 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1247 * return */
1248 print_tabs(2, fd);
1249 fprintf(fd, "slot_size = 0;\n");
1250 print_tabs(2, fd);
1251 fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf, to_base + to + len, &slot_size, &tsc,\n"
1252 "\t\t&before_hdr_pad, &after_hdr_pad);\n");
1253 /* If error, return */
1254 print_tabs(2, fd);
1255 fprintf(fd, "if(!buffer) return;\n\n");
7e97b039 1256
a3e6ce64 1257 /* write data : assume stack alignment is the same as struct alignment. */
1258
1259 for(unsigned int i=0;i<event->fields.position;i++){
2e415130 1260 field_t *field = (field_t*)(event->fields.array[i]);
a3e6ce64 1261 type_descriptor_t *type = field->type;
1262
1263 /* Set from */
1264 print_tabs(3, fd);
2e415130 1265 fprintf(fd, "from = %s;\n", field->name);
a3e6ce64 1266
1267 if(print_type_write(type,
1268 fd, 2, basename, field->name)) return 1;
1269 fprintf(fd, "\n");
7e97b039 1270
a3e6ce64 1271 /* Don't forget to flush pending memcpy */
1272 print_tabs(2, fd);
1273 fprintf(fd, "/* Flush pending memcpy */\n");
1274 print_tabs(2, fd);
1275 fprintf(fd, "if(len != 0) {\n");
1276 print_tabs(3, fd);
1277 fprintf(fd, "memcpy(buffer+to_base+to, from, len);\n");
1278 print_tabs(3, fd);
1279 fprintf(fd, "to += len;\n");
1280 //print_tabs(3, fd);
1281 //fprintf(fd, "from += len;\n");
1282 print_tabs(3, fd);
1283 fprintf(fd, "len = 0;\n");
1284 print_tabs(2, fd);
1285 fprintf(fd, "}\n");
1286 fprintf(fd, "\n");
1287 }
1288
1289
7e97b039 1290 /* commit */
a3e6ce64 1291 print_tabs(2, fd);
1292 fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
7e97b039 1293
a3e6ce64 1294 print_tabs(1, fd);
1295 fprintf(fd, "}\n\n");
1296
7e97b039 1297 /* Release locks */
a3e6ce64 1298 print_tabs(1, fd);
1299 fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n");
1300 print_tabs(1, fd);
1301 fprintf(fd, "preempt_enable_no_resched();\n");
2d2d14a7 1302
47299663 1303 fprintf(fd, "}\n");
1304 fprintf(fd, "#endif //CONFIG_LTT\n\n");
1305 return 0;
1306}
2d2d14a7 1307
1308
1309/* ltt-facility-name.h : main logging header.
1310 * log_header */
1311
1312void print_log_header_head(facility_t *fac, FILE *fd)
1313{
1314 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
1315 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
47299663 1316 fprintf(fd, "\n");
1317 fprintf(fd, "/* Facility activation at compile time. */\n");
1318 fprintf(fd, "#ifdef CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
2d2d14a7 1319}
1320
1321
1322
2d2d14a7 1323int print_log_header_types(facility_t *fac, FILE *fd)
1324{
1325 sequence_t *types = &fac->named_types.values;
1326 fprintf(fd, "/* Named types */\n");
1327 fprintf(fd, "\n");
1328
1329 for(unsigned int i = 0; i < types->position; i++) {
1330 /* For each named type, print the definition */
1331 if((print_type_declaration(types->array[i], fd,
1332 0, "", ""))) return 1;
1333 }
1334 return 0;
1335}
1336
1337int print_log_header_events(facility_t *fac, FILE *fd)
1338{
47299663 1339 sequence_t *events = &fac->events;
1340 char basename[PATH_MAX];
1341 unsigned int facname_len;
1342
1343 strncpy(basename, fac->name, PATH_MAX);
1344 facname_len = strlen(basename);
1345 strncat(basename, "_", PATH_MAX-facname_len);
1346 facname_len = strlen(basename);
1347
1348 for(unsigned int i = 0; i < events->position; i++) {
1349 event_t *event = (event_t*)events->array[i];
1350 strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len);
1351
1352 /* For each event, print structure, and then logging function */
1353 fprintf(fd, "/* Event %s structures */\n",
1354 event->name);
1355 for(unsigned int j = 0; j < event->fields.position; j++) {
1356 /* For each unnamed type, print the definition */
1357 field_t *f = (field_t*)event->fields.array[j];
1358 type_descriptor_t *t = f->type;
1359 if(t->type_name == NULL)
1360 if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
1361 }
1362 fprintf(fd, "\n");
2d2d14a7 1363
47299663 1364 fprintf(fd, "/* Event %s logging function */\n",
1365 event->name);
1366
a3e6ce64 1367 if(print_event_logging_function(basename, fac, event, fd)) return 1;
47299663 1368
1369 fprintf(fd, "\n");
1370 }
1371
2d2d14a7 1372 return 0;
1373}
1374
1375
1376void print_log_header_tail(facility_t *fac, FILE *fd)
1377{
47299663 1378 fprintf(fd, "#endif //CONFIG_LTT_FACILITY_%s\n\n", fac->capname);
2d2d14a7 1379 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
1380}
1381
1382int print_log_header(facility_t *fac)
1383{
1384 char filename[PATH_MAX];
1385 unsigned int filename_size = 0;
1386 FILE *fd;
1387 dprintf("%s\n", fac->name);
1388
1389 strcpy(filename, "ltt-facility-");
1390 filename_size = strlen(filename);
1391
1392 strncat(filename, fac->name, PATH_MAX - filename_size);
1393 filename_size = strlen(filename);
1394
1395 strncat(filename, ".h", PATH_MAX - filename_size);
1396 filename_size = strlen(filename);
1397
1398
1399 fd = fopen(filename, "w");
1400 if(fd == NULL) {
1401 printf("Error opening file %s for writing : %s\n",
1402 filename, strerror(errno));
1403 return errno;
1404 }
1405
1406 /* Print file head */
1407 print_log_header_head(fac, fd);
1408
1409 /* print named types in declaration order */
1410 if(print_log_header_types(fac, fd)) return 1;
1411
1412 /* Print events */
1413 if(print_log_header_events(fac, fd)) return 1;
1414
1415 /* Print file tail */
1416 print_log_header_tail(fac, fd);
1417
1418
1419 fclose(fd);
1420
1421 return 0;
1422}
1423
1424
1425/* ltt-facility-id-name.h : facility id.
1426 * log_id_header */
1427int print_id_header(facility_t *fac)
1428{
1429 char filename[PATH_MAX];
1430 unsigned int filename_size = 0;
1431 FILE *fd;
1432 dprintf("%s\n", fac->name);
1433
1434 strcpy(filename, "ltt-facility-id-");
1435 filename_size = strlen(filename);
1436
1437 strncat(filename, fac->name, PATH_MAX - filename_size);
1438 filename_size = strlen(filename);
1439
1440 strncat(filename, ".h", PATH_MAX - filename_size);
1441 filename_size = strlen(filename);
1442
1443
1444 fd = fopen(filename, "w");
1445 if(fd == NULL) {
1446 printf("Error opening file %s for writing : %s\n",
1447 filename, strerror(errno));
1448 return errno;
1449 }
1450
1451 fclose(fd);
1452
1453 return 0;
1454}
1455
1456
1457/* ltt-facility-loader-name.h : facility specific loader info.
1458 * loader_header */
1459int print_loader_header(facility_t *fac)
1460{
1461 char filename[PATH_MAX];
1462 unsigned int filename_size = 0;
1463 FILE *fd;
1464 dprintf("%s\n", fac->name);
1465
1466 strcpy(filename, "ltt-facility-loader-");
1467 filename_size = strlen(filename);
1468
1469 strncat(filename, fac->name, PATH_MAX - filename_size);
1470 filename_size = strlen(filename);
1471
1472 strncat(filename, ".h", PATH_MAX - filename_size);
1473 filename_size = strlen(filename);
1474
1475
1476 fd = fopen(filename, "w");
1477 if(fd == NULL) {
1478 printf("Error opening file %s for writing : %s\n",
1479 filename, strerror(errno));
1480 return errno;
1481 }
1482
1483 fclose(fd);
1484
1485 return 0;
1486}
1487
1488/* ltt-facility-loader-name.c : generic faciilty loader
1489 * loader_c */
1490int print_loader_c(facility_t *fac)
1491{
1492 char filename[PATH_MAX];
1493 unsigned int filename_size = 0;
1494 FILE *fd;
1495 dprintf("%s\n", fac->name);
1496
1497 strcpy(filename, "ltt-facility-loader-");
1498 filename_size = strlen(filename);
1499
1500 strncat(filename, fac->name, PATH_MAX - filename_size);
1501 filename_size = strlen(filename);
1502
1503 strncat(filename, ".c", PATH_MAX - filename_size);
1504 filename_size = strlen(filename);
1505
1506
1507 fd = fopen(filename, "w");
1508 if(fd == NULL) {
1509 printf("Error opening file %s for writing : %s\n",
1510 filename, strerror(errno));
1511 return errno;
1512 }
1513
1514
1515 fclose(fd);
1516
1517 return 0;
1518}
1519
1520
1521
92d82357 1522/* open facility */
1523/* code taken from ltt_facility_open in ltt/facility.c in lttv */
1524
1525/*****************************************************************************
1526 *Function name
1527 * ltt_facility_open : open facilities
1528 *Input params
1529 * pathname : the path name of the facility
1530 *
1531 * Open the facility corresponding to the right checksum.
1532 *
1533 *returns the facility on success, NULL on error.
1534 ****************************************************************************/
1535facility_t *ltt_facility_open(char * pathname)
1536{
1537 int ret = 0;
1538 char *token;
1539 parse_file_t in;
1540 facility_t * fac = NULL;
92d82357 1541 char buffer[BUFFER_SIZE];
1542 int generated = FALSE;
1543
1544 in.buffer = &(buffer[0]);
1545 in.lineno = 0;
1546 in.error = error_callback;
1547 in.name = pathname;
1548 in.unget = 0;
1549
1550 in.fp = fopen(in.name, "r");
1551 if(in.fp == NULL) {
1552 ret = 1;
1553 goto open_error;
1554 }
1555
1556 while(1){
1557 token = getToken(&in);
1558 if(in.type == ENDFILE) break;
1559
1560 if(generated) {
1561 printf("More than one facility in the file. Only using the first one.\n");
1562 break;
1563 }
1564
1565 if(strcmp(token, "<")) in.error(&in,"not a facility file");
1566 token = getName(&in);
1567
1568 if(strcmp("facility",token) == 0) {
1569 fac = malloc(sizeof(facility_t));
1570 fac->name = NULL;
1571 fac->description = NULL;
1572 sequence_init(&(fac->events));
1573 table_init(&(fac->named_types));
1574 sequence_init(&(fac->unnamed_types));
1575
1576 parseFacility(&in, fac);
1577
1578 //check if any namedType is not defined
1579 checkNamedTypesImplemented(&fac->named_types);
1580
2e415130 1581 generateChecksum(fac->name, &fac->checksum, &fac->events);
a67cd958 1582
92d82357 1583 generated = TRUE;
1584 }
1585 else {
1586 printf("facility token was expected in file %s\n", in.name);
1587 ret = 1;
1588 goto parse_error;
1589 }
1590 }
1591
1592 parse_error:
1593 fclose(in.fp);
1594open_error:
1595
1596 if(!generated) {
1597 printf("Cannot find facility %s\n", pathname);
1598 fac = NULL;
1599 }
1600
1601 return fac;
1602}
1603
1604/* Close the facility */
1605void ltt_facility_close(facility_t *fac)
1606{
1607 free(fac->name);
1608 free(fac->capname);
1609 free(fac->description);
1610 freeEvents(&fac->events);
1611 sequence_dispose(&fac->events);
1612 freeNamedType(&fac->named_types);
1613 table_dispose(&fac->named_types);
1614 freeTypes(&fac->unnamed_types);
1615 sequence_dispose(&fac->unnamed_types);
1616 free(fac);
1617}
1618
1619
1620/* Show help */
1621void show_help(int argc, char ** argv)
1622{
1623 printf("Genevent help : \n");
1624 printf("\n");
1625 printf("Use %s name.xml\n", argv[0]);
1626 printf("to create :\n");
1627 printf("ltt-facility-name.h\n");
1628 printf("ltt-facility-id-name.h\n");
1629 printf("ltt-facility-loader-name.h\n");
1630 printf("ltt-facility-loader-name.c\n");
1631 printf("In the current directory.\n");
1632 printf("\n");
1633}
1634
1635/* Parse program arguments */
1636/* Return values :
1637 * 0 : continue program
1638 * -1 : stop program, return 0
1639 * > 0 : stop program, return value as exit.
1640 */
1641int check_args(int argc, char **argv)
1642{
1643 if(argc < 2) {
1644 printf("Not enough arguments\n");
1645 show_help(argc, argv);
1646 return EINVAL;
1647 }
1648
1649 if(strcmp(argv[1], "-h") == 0) {
1650 show_help(argc, argv);
1651 return -1;
1652 }
1653
1654 return 0;
1655}
1656
1657int main(int argc, char **argv)
1658{
1659 int err = 0;
1660 facility_t *fac;
1661
1662 err = check_args(argc, argv);
1663 if(err > 0) return err;
1664 else if(err < 0) return 0;
1665
1666 /* open the facility */
1667 fac = ltt_facility_open(argv[1]);
1668 if(fac == NULL) {
1669 printf("Error opening file %s for reading : %s\n",
1670 argv[1], strerror(errno));
1671 return errno;
1672 }
1673
1674 /* generate the output C files */
1675
1676
2d2d14a7 1677 /* ltt-facility-name.h : main logging header.
1678 * log_header */
1679 err = print_log_header(fac);
1680 if(err) return err;
92d82357 1681
2d2d14a7 1682 /* ltt-facility-id-name.h : facility id.
1683 * log_id_header */
1684 err = print_id_header(fac);
1685 if(err) return err;
92d82357 1686
2d2d14a7 1687 /* ltt-facility-loader-name.h : facility specific loader info.
1688 * loader_header */
1689 err = print_loader_header(fac);
1690 if(err) return err;
1691
1692 /* ltt-facility-loader-name.c : generic faciilty loader
1693 * loader_c */
1694 err = print_loader_c(fac);
1695 if(err) return err;
92d82357 1696
1697 /* close the facility */
1698 ltt_facility_close(fac);
1699
1700 return 0;
1701}
1702
1703
This page took 0.104646 seconds and 4 git commands to generate.