tls static init
[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:
458989d8 154 fprintf(fd, "const void *");
2d2d14a7 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:
3d9b9b0c 172 fprintf(fd, "const char *");
2d2d14a7 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)]);
3ace7bc4 227 fprintf(fd, "lttng_param_%s", field_name);
7e97b039 228 break;
229 case UINT_FIXED:
230 fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]);
3ace7bc4 231 fprintf(fd, "lttng_param_%s", field_name);
7e97b039 232 break;
233 case CHAR:
234 fprintf(fd, "signed char");
3ace7bc4 235 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 236 break;
237 case UCHAR:
238 fprintf(fd, "unsigned char");
3ace7bc4 239 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 240 break;
241 case SHORT:
242 fprintf(fd, "short");
3ace7bc4 243 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 244 break;
245 case USHORT:
246 fprintf(fd, "unsigned short");
3ace7bc4 247 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 248 break;
249 case INT:
250 fprintf(fd, "int");
3ace7bc4 251 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 252 break;
253 case UINT:
254 fprintf(fd, "unsigned int");
3ace7bc4 255 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 256 break;
257 case FLOAT:
258 fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]);
3ace7bc4 259 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 260 break;
261 case POINTER:
458989d8 262 fprintf(fd, "const void *");
3ace7bc4 263 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 264 break;
265 case LONG:
266 fprintf(fd, "long");
3ace7bc4 267 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 268 break;
269 case ULONG:
270 fprintf(fd, "unsigned long");
3ace7bc4 271 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 272 break;
273 case SIZE_T:
274 fprintf(fd, "size_t");
3ace7bc4 275 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 276 break;
277 case SSIZE_T:
278 fprintf(fd, "ssize_t");
3ace7bc4 279 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 280 break;
281 case OFF_T:
282 fprintf(fd, "off_t");
3ace7bc4 283 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 284 break;
285 case STRING:
3d9b9b0c 286 fprintf(fd, "const char *");
3ace7bc4 287 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 288 break;
289 case ENUM:
290 fprintf(fd, "enum lttng_%s", basename);
3ace7bc4 291 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 292 break;
293 case ARRAY:
294 fprintf(fd, "lttng_array_%s", basename);
3ace7bc4 295 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 296 break;
297 case SEQUENCE:
298 fprintf(fd, "lttng_sequence_%s *", basename);
3ace7bc4 299 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 300 break;
301 case STRUCT:
302 fprintf(fd, "struct lttng_%s *", basename);
3ace7bc4 303 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 304 break;
305 case UNION:
306 fprintf(fd, "union lttng_%s *", basename);
3ace7bc4 307 fprintf(fd, " lttng_param_%s", field_name);
7e97b039 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);
70f46ac3 414 fprintf(fd, "LTTNG_%s = %d", ((char*)td->labels.array[i]),
415 (*(int*)td->labels_values.array[i]));
2d2d14a7 416 fprintf(fd, ",\n");
417 }
418 fprintf(fd, "};\n");
419 fprintf(fd, "\n");
420 break;
421
422 case ARRAY:
7b175edc 423 dprintf("%s\n", basename);
2d2d14a7 424 assert(td->size >= 0);
a67cd958 425 if(((field_t*)td->fields.array[0])->type->type_name == NULL) {
2d2d14a7 426 /* Not a named nested type : we must print its declaration first */
a67cd958 427 if(print_type_declaration(((field_t*)td->fields.array[0])->type,
2d2d14a7 428 fd, 0, basename, "")) return 1;
429 }
2e415130 430 fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename,
2d2d14a7 431 td->size);
7e97b039 432 fprintf(fd, "typedef ");
a67cd958 433 if(print_type(((field_t*)td->fields.array[0])->type,
434 fd, tabs, basename, "")) return 1;
2d2d14a7 435 fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename,
436 basename);
437 fprintf(fd, "\n");
438 break;
439 case SEQUENCE:
a67cd958 440 /* We assume that the sequence length type does not need to be declared.
441 */
442 if(((field_t*)td->fields.array[1])->type->type_name == NULL) {
2d2d14a7 443 /* Not a named nested type : we must print its declaration first */
a67cd958 444 if(print_type_declaration(((field_t*)td->fields.array[1])->type,
2d2d14a7 445 fd, 0, basename, "")) return 1;
446 }
447 fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n",
448 basename,
449 basename);
450 fprintf(fd, "struct lttng_sequence_%s", basename);
451 fprintf(fd, " {\n");
452 print_tabs(1, fd);
a3e6ce64 453 if(print_type(((field_t*)td->fields.array[0])->type,
454 fd, tabs, basename, "")) return 1;
40c18b13 455 fprintf(fd, " len;\n");
2d2d14a7 456 print_tabs(1, fd);
8c9e9076 457 fprintf(fd, "const ");
a67cd958 458 if(print_type(((field_t*)td->fields.array[1])->type,
459 fd, tabs, basename, "")) return 1;
2d2d14a7 460 fprintf(fd, " *array;\n");
d428224c 461 fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy
462 it to the buffer directly. */
2d2d14a7 463 fprintf(fd, "\n");
464 break;
465
466 case STRUCT:
467 for(unsigned int i=0;i<td->fields.position;i++){
468 field_t *field = (field_t*)(td->fields.array[i]);
469 type_descriptor_t *type = field->type;
470 if(type->type_name == NULL) {
471 /* Not a named nested type : we must print its declaration first */
472 if(print_type_declaration(type,
473 fd, 0, basename, field->name)) return 1;
474 }
475 }
476 fprintf(fd, "struct lttng_%s", basename);
477 fprintf(fd, " {\n");
478 for(unsigned int i=0;i<td->fields.position;i++){
479 field_t *field = (field_t*)(td->fields.array[i]);
480 type_descriptor_t *type = field->type;
481 print_tabs(1, fd);
47299663 482 if(print_type(type, fd, tabs, basename, field->name)) return 1;
2d2d14a7 483 fprintf(fd, " ");
484 fprintf(fd, "%s", field->name);
485 fprintf(fd, ";\n");
486 }
d428224c 487 fprintf(fd, "} LTT_ALIGN;\n");
2d2d14a7 488 fprintf(fd, "\n");
489 break;
490 case UNION:
2d2d14a7 491 for(unsigned int i=0;i<td->fields.position;i++){
492 field_t *field = (field_t*)(td->fields.array[i]);
493 type_descriptor_t *type = field->type;
494 if(type->type_name == NULL) {
495 /* Not a named nested type : we must print its declaration first */
47299663 496 if(print_type_declaration(type,
497 fd, 0, basename, field->name)) return 1;
2d2d14a7 498 }
499 }
500 fprintf(fd, "union lttng_%s", basename);
501 fprintf(fd, " {\n");
502 for(unsigned i=0;i<td->fields.position;i++){
503 field_t *field = (field_t*)(td->fields.array[i]);
504 type_descriptor_t *type = field->type;
505 print_tabs(1, fd);
47299663 506 if(print_type(type, fd, tabs, basename, field->name)) return 1;
2d2d14a7 507 fprintf(fd, " ");
508 fprintf(fd, "%s", field->name);
509 fprintf(fd, ";\n");
510 }
d428224c 511 fprintf(fd, "} LTT_ALIGN;\n");
2d2d14a7 512 fprintf(fd, "\n");
513 break;
514 default:
515 dprintf("print_type_declaration : unknown type or nothing to declare.\n");
516 break;
517 }
518
519 return 0;
520}
521
a67cd958 522
a3e6ce64 523/* print type alignment.
524 *
525 * Copied from construct_types_and_fields in LTTV facility.c
526 *
527 * basename is the name which identifies the type (along with a prefix
528 * (possibly)). */
a67cd958 529
a3e6ce64 530int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs,
3261899d 531 char *nest_name, char *field_name, char *obj_prefix)
a67cd958 532{
a3e6ce64 533 char basename[PATH_MAX];
534 unsigned int basename_len = 0;
535
536 strncpy(basename, nest_name, PATH_MAX);
537 basename_len = strlen(basename);
538
539 /* For a named type, we use the type_name directly */
540 if(td->type_name != NULL) {
541 strncpy(basename, td->type_name, PATH_MAX);
542 basename_len = strlen(basename);
a67cd958 543 } else {
a3e6ce64 544 /* For a unnamed type, there must be a field name, except for
545 * the array. */
546 if((basename_len != 0)
547 && (basename[basename_len-1] != '_'
34c1d1b5 548 && field_name != NULL
a3e6ce64 549 && (field_name[0] != '\0'))) {
550 strncat(basename, "_", PATH_MAX - basename_len);
551 basename_len = strlen(basename);
a67cd958 552 }
34c1d1b5 553 if(field_name != NULL)
554 strncat(basename, field_name, PATH_MAX - basename_len);
a67cd958 555 }
a3e6ce64 556
3d9b9b0c 557 if(field_name[0] == '\0') {
558 /* We are in a write function : it's the obj that we must align. */
559 switch(td->type) {
560 case SEQUENCE:
3261899d 561 fprintf(fd, "lttng_get_alignment_sequence_%s(%s)", basename,
562 obj_prefix);
3d9b9b0c 563 break;
564 case STRUCT:
3261899d 565 fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename,
566 obj_prefix);
3d9b9b0c 567 break;
568 case UNION:
3261899d 569 fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename,
570 obj_prefix);
3d9b9b0c 571 break;
572 case ARRAY:
3261899d 573 fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename,
574 obj_prefix);
a2ff13ed 575 case STRING:
576 fprintf(fd, "sizeof(char)");
3d9b9b0c 577 break;
8f78c30f 578 case INT_FIXED:
579 case UINT_FIXED:
580 case CHAR:
581 case UCHAR:
582 case SHORT:
583 case USHORT:
584 case INT:
585 case UINT:
586 case FLOAT:
587 case POINTER:
588 case LONG:
589 case ULONG:
590 case SIZE_T:
591 case SSIZE_T:
592 case OFF_T:
593 case ENUM:
594 fprintf(fd, "sizeof(");
595 if(print_type(td, fd, 0, basename, "")) return 1;
596 fprintf(fd, ")");
597 break;
598
3d9b9b0c 599 default:
600 printf("error : type unexpected\n");
601 return 1;
602 break;
603 }
604 } else {
605
606 switch(td->type) {
607 case INT_FIXED:
608 case UINT_FIXED:
609 case CHAR:
610 case UCHAR:
611 case SHORT:
612 case USHORT:
613 case INT:
614 case UINT:
615 case FLOAT:
616 case POINTER:
617 case LONG:
618 case ULONG:
619 case SIZE_T:
620 case SSIZE_T:
621 case OFF_T:
622 case ENUM:
623 fprintf(fd, "sizeof(");
624 if(print_type(td, fd, 0, basename, "")) return 1;
625 fprintf(fd, ")");
626 break;
627 case STRING:
628 fprintf(fd, "sizeof(char)");
629 break;
630 case SEQUENCE:
3261899d 631 fprintf(fd, "lttng_get_alignment_sequence_%s(&%s%s)", basename,
632 obj_prefix, field_name);
3d9b9b0c 633 break;
634 case STRUCT:
3261899d 635 fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename,
636 obj_prefix, field_name);
3d9b9b0c 637 break;
638 case UNION:
3261899d 639 fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename,
640 obj_prefix, field_name);
3d9b9b0c 641 break;
642 case ARRAY:
3261899d 643 fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename,
644 obj_prefix, field_name);
3d9b9b0c 645 break;
646 case NONE:
647 printf("error : type NONE unexpected\n");
648 return 1;
649 break;
650 }
a3e6ce64 651 }
3d9b9b0c 652
a3e6ce64 653 return 0;
a67cd958 654}
655
a3e6ce64 656/* print type write.
657 *
658 * Copied from construct_types_and_fields in LTTV facility.c
659 *
660 * basename is the name which identifies the type (along with a prefix
661 * (possibly)). */
a67cd958 662
a3e6ce64 663int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs,
458989d8 664 char *nest_name, char *field_name, char *obj_prefix, int get_ptr)
a67cd958 665{
a67cd958 666 char basename[PATH_MAX];
667 unsigned int basename_len = 0;
458989d8 668 char get_ptr_char[2] = "";
a3e6ce64 669
670 strncpy(basename, nest_name, PATH_MAX);
a67cd958 671 basename_len = strlen(basename);
672
673 /* For a named type, we use the type_name directly */
674 if(td->type_name != NULL) {
675 strncpy(basename, td->type_name, PATH_MAX);
676 basename_len = strlen(basename);
677 } else {
a3e6ce64 678 /* For a unnamed type, there must be a field name, except for
679 * the array. */
a67cd958 680 if((basename_len != 0)
a3e6ce64 681 && (basename[basename_len-1] != '_'
682 && (field_name[0] != '\0'))) {
a67cd958 683 strncat(basename, "_", PATH_MAX - basename_len);
684 basename_len = strlen(basename);
685 }
686 strncat(basename, field_name, PATH_MAX - basename_len);
687 }
688
458989d8 689 if(get_ptr) {
690 strcpy(get_ptr_char, "&");
691 }
692
a67cd958 693 switch(td->type) {
694 case INT_FIXED:
695 case UINT_FIXED:
696 case CHAR:
697 case UCHAR:
698 case SHORT:
699 case USHORT:
700 case INT:
701 case UINT:
702 case FLOAT:
703 case POINTER:
704 case LONG:
705 case ULONG:
706 case SIZE_T:
707 case SSIZE_T:
708 case OFF_T:
709 case ENUM:
a67cd958 710 print_tabs(tabs, fd);
8f78c30f 711 fprintf(fd, "align = ");
712 if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
713 fprintf(fd, ";\n");
714 fprintf(fd, "\n");
715 print_tabs(tabs, fd);
716 fprintf(fd, "if(*len == 0) {\n");
717 print_tabs(tabs+1, fd);
718 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
719 print_tabs(tabs, fd);
720 fprintf(fd, "} else {\n");
721 print_tabs(tabs+1, fd);
722 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
723 print_tabs(tabs, fd);
724 fprintf(fd, "}\n");
725 fprintf(fd, "\n");
726
727 print_tabs(tabs, fd);
728 fprintf(fd, "*len += ");
a3e6ce64 729 fprintf(fd, "sizeof(");
2e415130 730 if(print_type(td, fd, 0, basename, "")) return 1;
a67cd958 731 fprintf(fd, ");\n");
8f78c30f 732
a67cd958 733 break;
734 case STRING:
a67cd958 735 print_tabs(tabs, fd);
3261899d 736 fprintf(fd,
737 "lttng_write_string_%s(buffer, to_base, to, from, len, %s%s);\n",
738 basename, obj_prefix, field_name);
a3e6ce64 739 break;
740 case SEQUENCE:
741 print_tabs(tabs, fd);
3261899d 742 fprintf(fd,
458989d8 743 "lttng_write_sequence_%s(buffer, to_base, to, from, len, %s%s%s);",
744 basename, get_ptr_char, obj_prefix, field_name);
a3e6ce64 745 break;
746 case STRUCT:
747 print_tabs(tabs, fd);
3261899d 748 fprintf(fd,
458989d8 749 "lttng_write_struct_%s(buffer, to_base, to, from, len, %s%s%s);",
750 basename, get_ptr_char, obj_prefix, field_name);
a3e6ce64 751 break;
752 case UNION:
753 print_tabs(tabs, fd);
3261899d 754 fprintf(fd,
458989d8 755 "lttng_write_union_%s(buffer, to_base, to, from, len, %s%s%s);",
756 basename, get_ptr_char, obj_prefix, field_name);
a67cd958 757 break;
758 case ARRAY:
a67cd958 759 print_tabs(tabs, fd);
3261899d 760 fprintf(fd,
a2ff13ed 761 "lttng_write_array_%s(buffer, to_base, to, from, len, %s%s);",
3261899d 762 basename, obj_prefix, field_name);
a3e6ce64 763 break;
2e415130 764 case NONE:
765 printf("Error : type NONE unexpected\n");
766 return 1;
767 break;
a3e6ce64 768 }
a67cd958 769
a3e6ce64 770 return 0;
771}
a67cd958 772
63c831c5 773/* print need local vars ?.
774 *
775 * Copied from print_type_write
776 *
777 * Does the type_write call needs local size and from variables ?
778 * return value : 1 yes, 0 no.
779 */
780
781int has_type_local(type_descriptor_t * td)
782{
783 switch(td->type) {
784 case INT_FIXED:
785 case UINT_FIXED:
786 case CHAR:
787 case UCHAR:
788 case SHORT:
789 case USHORT:
790 case INT:
791 case UINT:
792 case FLOAT:
793 case POINTER:
794 case LONG:
795 case ULONG:
796 case SIZE_T:
797 case SSIZE_T:
798 case OFF_T:
799 case ENUM:
800 return 1;
801 break;
802 case STRING:
803 case SEQUENCE:
804 case STRUCT:
805 case UNION:
806 case ARRAY:
807 return 0;
808 break;
809 case NONE:
810 printf("Error : type NONE unexpected\n");
811 return 1;
812 break;
813 }
814
815 return 0;
816}
817
a67cd958 818
a67cd958 819
a3e6ce64 820/* print type alignment function.
821 *
822 * Copied from construct_types_and_fields in LTTV facility.c
823 *
824 * basename is the name which identifies the type (along with a prefix
825 * (possibly)). */
826
827int print_type_alignment_fct(type_descriptor_t * td, FILE *fd,
828 unsigned int tabs,
829 char *nest_name, char *field_name)
830{
831 char basename[PATH_MAX];
832 unsigned int basename_len = 0;
833
834 strncpy(basename, nest_name, PATH_MAX);
835 basename_len = strlen(basename);
836
837 /* For a named type, we use the type_name directly */
838 if(td->type_name != NULL) {
839 strncpy(basename, td->type_name, PATH_MAX);
840 basename_len = strlen(basename);
841 } else {
842 /* For a unnamed type, there must be a field name, except for
843 * the array. */
844 if((basename_len != 0)
845 && (basename[basename_len-1] != '_'
846 && (field_name[0] != '\0'))) {
847 strncat(basename, "_", PATH_MAX - basename_len);
848 basename_len = strlen(basename);
849 }
850 strncat(basename, field_name, PATH_MAX - basename_len);
851 }
852
853 switch(td->type) {
854 case SEQUENCE:
855 /* Function header */
856 fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n",
857 basename);
858 print_tabs(2, fd);
2e415130 859 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 860 fprintf(fd, " *obj)\n");
861 fprintf(fd, "{\n");
862 print_tabs(1, fd);
863 fprintf(fd, "size_t align=0, localign;");
864 fprintf(fd, "\n");
865 print_tabs(1, fd);
866 fprintf(fd, "localign = ");
867 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
3261899d 868 fd, 0, basename, "len", "obj->")) return 1;
a3e6ce64 869 fprintf(fd, ";\n");
870 print_tabs(1, fd);
871 fprintf(fd, "align = max(align, localign);\n");
872 fprintf(fd, "\n");
873 print_tabs(1, fd);
874 fprintf(fd, "localign = ");
875 if(print_type_alignment(((field_t*)td->fields.array[1])->type,
3261899d 876 fd, 0, basename, "array[0]", "obj->")) return 1;
a3e6ce64 877 fprintf(fd, ";\n");
878 print_tabs(1, fd);
879 fprintf(fd, "align = max(align, localign);\n");
880 fprintf(fd, "\n");
881 print_tabs(1, fd);
882 fprintf(fd, "return align;\n");
883 break;
884 case STRUCT:
885 /* Function header */
886 fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n",
887 basename);
888 print_tabs(2, fd);
2e415130 889 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 890 fprintf(fd, " *obj)\n");
891 fprintf(fd, "{\n");
892 print_tabs(1, fd);
893 fprintf(fd, "size_t align=0, localign;");
894 fprintf(fd, "\n");
895 for(unsigned int i=0;i<td->fields.position;i++){
896 field_t *field = (field_t*)(td->fields.array[i]);
897 type_descriptor_t *type = field->type;
898 print_tabs(1, fd);
899 fprintf(fd, "localign = ");
3261899d 900 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
901 return 1;
a3e6ce64 902 fprintf(fd, ";\n");
903 print_tabs(1, fd);
904 fprintf(fd, "align = max(align, localign);\n");
905 fprintf(fd, "\n");
906 }
907 print_tabs(1, fd);
908 fprintf(fd, "return align;\n");
909
a67cd958 910 break;
a3e6ce64 911 case UNION:
912 /* Function header */
913 fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n",
914 basename);
915 print_tabs(2, fd);
2e415130 916 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 917 fprintf(fd, " *obj)\n");
918 fprintf(fd, "{\n");
919 print_tabs(1, fd);
920 fprintf(fd, "size_t align=0, localign;");
921 fprintf(fd, "\n");
922 for(unsigned int i=0;i<td->fields.position;i++){
923 field_t *field = (field_t*)(td->fields.array[i]);
924 type_descriptor_t *type = field->type;
925 print_tabs(1, fd);
926 fprintf(fd, "localign = ");
3261899d 927 if(print_type_alignment(type, fd, 0, basename, field->name, "obj->"))
928 return 1;
a3e6ce64 929 fprintf(fd, ";\n");
930 print_tabs(1, fd);
931 fprintf(fd, "align = max(align, localign);\n");
932 fprintf(fd, "\n");
933 }
934 print_tabs(1, fd);
935 fprintf(fd, "return align;\n");
936
937 break;
938 case ARRAY:
939 /* Function header */
940 fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n",
941 basename);
942 print_tabs(2, fd);
2e415130 943 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 944 fprintf(fd, " obj)\n");
945 fprintf(fd, "{\n");
946 print_tabs(1, fd);
947 fprintf(fd, "return \n");
948 if(print_type_alignment(((field_t*)td->fields.array[0])->type,
3261899d 949 fd, 0, basename, "", "obj[0]"))
950 return 1;
a3e6ce64 951 fprintf(fd, ";\n");
952 break;
953 default:
71e09db9 954 dprintf("print_type_alignment_fct : type has no alignment function.\n");
34c1d1b5 955 return 0;
a3e6ce64 956 break;
957 }
958
959
960 /* Function footer */
961 fprintf(fd, "}\n");
962 fprintf(fd, "\n");
963
2e415130 964 return 0;
a3e6ce64 965}
966
967/* print type write function.
968 *
969 * Copied from construct_types_and_fields in LTTV facility.c
970 *
971 * basename is the name which identifies the type (along with a prefix
972 * (possibly)). */
973
974int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs,
975 char *nest_name, char *field_name)
976{
977 char basename[PATH_MAX];
978 unsigned int basename_len = 0;
979
980 strncpy(basename, nest_name, PATH_MAX);
981 basename_len = strlen(basename);
982
983 /* For a named type, we use the type_name directly */
984 if(td->type_name != NULL) {
985 strncpy(basename, td->type_name, PATH_MAX);
986 basename_len = strlen(basename);
987 } else {
988 /* For a unnamed type, there must be a field name, except for
989 * the array. */
990 if((basename_len != 0)
991 && (basename[basename_len-1] != '_'
992 && (field_name[0] != '\0'))) {
993 strncat(basename, "_", PATH_MAX - basename_len);
994 basename_len = strlen(basename);
995 }
996 strncat(basename, field_name, PATH_MAX - basename_len);
997 }
998
34c1d1b5 999 switch(td->type) {
1000 case SEQUENCE:
1001 case STRUCT:
1002 case UNION:
1003 case ARRAY:
a2ff13ed 1004 case STRING:
34c1d1b5 1005 break;
1006 default:
71e09db9 1007 dprintf("print_type_write_fct : type has no write function.\n");
34c1d1b5 1008 return 0;
1009 break;
1010 }
1011
a3e6ce64 1012 /* Print header */
1013 switch(td->type) {
a67cd958 1014 case SEQUENCE:
34c1d1b5 1015 fprintf(fd, "static inline void lttng_write_sequence_%s(\n",
a3e6ce64 1016 basename);
a67cd958 1017 break;
a3e6ce64 1018 case STRUCT:
6e27ba88 1019 fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename);
a67cd958 1020 break;
a3e6ce64 1021 case UNION:
6e27ba88 1022 fprintf(fd, "static inline void lttng_write_union_%s(\n", basename);
a3e6ce64 1023 break;
1024 case ARRAY:
6e27ba88 1025 fprintf(fd, "static inline void lttng_write_array_%s(\n", basename);
a3e6ce64 1026 break;
a2ff13ed 1027 case STRING:
1028 fprintf(fd, "static inline void lttng_write_string_%s(\n", basename);
1029 break;
a3e6ce64 1030 default:
1031 printf("print_type_write_fct : type has no write function.\n");
a67cd958 1032 break;
a67cd958 1033 }
1034
a3e6ce64 1035 print_tabs(2, fd);
1036 fprintf(fd, "void *buffer,\n");
1037 print_tabs(2, fd);
1038 fprintf(fd, "size_t *to_base,\n");
1039 print_tabs(2, fd);
1040 fprintf(fd, "size_t *to,\n");
1041 print_tabs(2, fd);
458989d8 1042 fprintf(fd, "const void **from,\n");
a3e6ce64 1043 print_tabs(2, fd);
1044 fprintf(fd, "size_t *len,\n");
1045 print_tabs(2, fd);
2e415130 1046 if(print_type(td, fd, 0, basename, "")) return 1;
a3e6ce64 1047
1048 switch(td->type) {
1049 case SEQUENCE:
1050 fprintf(fd, " *obj)\n");
1051 break;
1052 case STRUCT:
1053 fprintf(fd, " *obj)\n");
1054 break;
1055 case UNION:
1056 fprintf(fd, " *obj)\n");
1057 break;
1058 case ARRAY:
1059 fprintf(fd, " obj)\n");
1060 break;
a2ff13ed 1061 case STRING:
1062 fprintf(fd, " obj)\n");
1063 break;
a3e6ce64 1064 default:
1065 printf("print_type_write_fct : type has no write function.\n");
1066 break;
1067 }
1068
1069 fprintf(fd, "{\n");
3ace7bc4 1070
f5f2fde4 1071 switch(td->type) {
1072 case STRING:
1073 print_tabs(1, fd);
1074 fprintf(fd, "size_t size;\n");
1075 break;
1076 default:
1077 break;
1078 }
1079
a3e6ce64 1080 print_tabs(1, fd);
3ace7bc4 1081 fprintf(fd, "size_t align;\n");
a3e6ce64 1082 fprintf(fd, "\n");
1083
1084 switch(td->type) {
1085 case SEQUENCE:
1086 case STRING:
1087 print_tabs(1, fd);
1088 fprintf(fd, "/* Flush pending memcpy */\n");
1089 print_tabs(1, fd);
1090 fprintf(fd, "if(*len != 0) {\n");
1091 print_tabs(2, fd);
1092 fprintf(fd, "if(buffer != NULL)\n");
1093 print_tabs(3, fd);
1094 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
1095 print_tabs(1, fd);
1096 fprintf(fd, "}\n");
1097 print_tabs(1, fd);
1098 fprintf(fd, "*to += *len;\n");
1099 print_tabs(1, fd);
1100 fprintf(fd, "*len = 0;\n");
1101 fprintf(fd, "\n");
1102 break;
1103 case STRUCT:
1104 case UNION:
1105 case ARRAY:
1106 break;
1107 default:
1108 printf("print_type_write_fct : type has no write function.\n");
1109 break;
1110 }
1111
1112 print_tabs(1, fd);
34c1d1b5 1113 fprintf(fd, "align = ");
0231ef76 1114 if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1;
a3e6ce64 1115 fprintf(fd, ";\n");
1116 fprintf(fd, "\n");
1117 print_tabs(1, fd);
1118 fprintf(fd, "if(*len == 0) {\n");
1119 print_tabs(2, fd);
1120 fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n");
1121 print_tabs(1, fd);
1122 fprintf(fd, "} else {\n");
1123 print_tabs(2, fd);
1124 fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n");
1125 print_tabs(1, fd);
1126 fprintf(fd, "}\n");
1127 fprintf(fd, "\n");
1128
1129 /* First, check if the type has a fixed size. If it is the case, then the size
1130 * to write is know by the compiler : simply use a sizeof() */
1131 if(has_type_fixed_size(td)) {
1132 print_tabs(1, fd);
1133 fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n");
1134 fprintf(fd, "\n");
1135 print_tabs(1, fd);
f5f2fde4 1136 fprintf(fd, "*len += sizeof(");
2e415130 1137 if(print_type(td, fd, 0, basename, field_name)) return 1;
a3e6ce64 1138 fprintf(fd, ");\n");
1139 } else {
1140 /* The type contains nested variable size subtypes :
1141 * we must write field by field. */
1142 print_tabs(1, fd);
1143 fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n");
1144 fprintf(fd, "\n");
1145
1146 switch(td->type) {
1147 case SEQUENCE:
1148 print_tabs(1, fd);
1149 fprintf(fd, "/* Copy members */\n");
3261899d 1150// print_tabs(1, fd);
1151// fprintf(fd, "size = sizeof(\n");
a3e6ce64 1152 if(print_type_write(((field_t*)td->fields.array[0])->type,
458989d8 1153 fd, 1, basename, "len", "obj->", 1)) return 1;
3261899d 1154 fprintf(fd, "\n");
1155// fprintf(fd, ");\n");
1156// print_tabs(1, fd);
1157// fprintf(fd, "*to += ltt_align(*to, size);\n");
a3e6ce64 1158 print_tabs(1, fd);
1159 fprintf(fd, "if(buffer != NULL)\n");
1160 print_tabs(2, fd);
8f78c30f 1161 fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n");
a3e6ce64 1162 print_tabs(1, fd);
8f78c30f 1163 fprintf(fd, "*to += *len;\n");
1164 print_tabs(1, fd);
1165 fprintf(fd, "*len = 0;\n");
a3e6ce64 1166 fprintf(fd, "\n");
8f78c30f 1167
a3e6ce64 1168 /* Write the child : varlen child or not ? */
1169 if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) {
1170 /* Fixed size len child : use a multiplication of its size */
3261899d 1171// print_tabs(1, fd);
1172// fprintf(fd, "size = sizeof(\n");
8f78c30f 1173
1174 //print_tabs(1, fd);
1175 /* We know that *len does not contain alignment because of the
1176 * previous align output. len is always 0 here. */
a3e6ce64 1177 if(print_type_write(((field_t*)td->fields.array[1])->type,
8f78c30f 1178 fd, 1, basename, "array[0]", "obj->", 1))
1179 return 1;
3261899d 1180// fprintf(fd, ");\n");
8f78c30f 1181 fprintf(fd, "\n");
a3e6ce64 1182 print_tabs(1, fd);
8f78c30f 1183 fprintf(fd, "*len = obj->len * (*len);\n");
a3e6ce64 1184 print_tabs(1, fd);
1185 fprintf(fd, "if(buffer != NULL)\n");
1186 print_tabs(2, fd);
8f78c30f 1187 fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, *len);\n");
1188 print_tabs(1, fd);
1189 fprintf(fd, "*to += *len;\n");
a3e6ce64 1190 print_tabs(1, fd);
8f78c30f 1191 fprintf(fd, "*len = 0;\n");
a3e6ce64 1192 fprintf(fd, "\n");
1193 } else {
1194 print_tabs(1, fd);
1195 fprintf(fd, "/* Variable length child : iter. */\n");
1196 print_tabs(1, fd);
1197 fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n");
1198 if(print_type_write(((field_t*)td->fields.array[1])->type,
458989d8 1199 fd, 2, basename, "array[i]", "obj->", 1)) return 1;
a3e6ce64 1200 print_tabs(1, fd);
1201 fprintf(fd, "}\n");
1202 }
1203 fprintf(fd, "\n");
1204 print_tabs(1, fd);
1205 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1206 print_tabs(1, fd);
2a5eec7d 1207 fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
a3e6ce64 1208 print_tabs(1, fd);
1209 fprintf(fd, "*to_base = *to_base+*to;\n");
1210 print_tabs(1, fd);
1211 fprintf(fd, "*to = 0;\n");
1212 fprintf(fd, "\n");
a2ff13ed 1213 print_tabs(1, fd);
a3e6ce64 1214 fprintf(fd, "/* Put source *from just after the C sequence */\n");
1215 print_tabs(1, fd);
1216 fprintf(fd, "*from = obj+1;\n");
1217 break;
1218 case STRING:
a2ff13ed 1219 print_tabs(1, fd);
caabcb43 1220 fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n");
a3e6ce64 1221 print_tabs(1, fd);
1222 fprintf(fd, "if(buffer != NULL)\n");
1223 print_tabs(2, fd);
1224 fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n");
1225 print_tabs(1, fd);
1226 fprintf(fd, "*to += size;\n");
1227 fprintf(fd, "\n");
1228 print_tabs(1, fd);
1229 fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n");
1230 print_tabs(1, fd);
2a5eec7d 1231 fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n");
a3e6ce64 1232 print_tabs(1, fd);
1233 fprintf(fd, "*to_base = *to_base+*to;\n");
1234 print_tabs(1, fd);
1235 fprintf(fd, "*to = 0;\n");
1236 fprintf(fd, "\n");
a3e6ce64 1237 print_tabs(1, fd);
a2ff13ed 1238 fprintf(fd, "/* Put source *from just after the C string */\n");
1239 print_tabs(1, fd);
1240 fprintf(fd, "*from += size;\n");
a3e6ce64 1241 break;
1242 case STRUCT:
1243 for(unsigned int i=0;i<td->fields.position;i++){
1244 field_t *field = (field_t*)(td->fields.array[i]);
1245 type_descriptor_t *type = field->type;
1246 if(print_type_write(type,
458989d8 1247 fd, 1, basename, field->name, "obj->", 1)) return 1;
a3e6ce64 1248 fprintf(fd, "\n");
1249 }
1250 break;
1251 case UNION:
1252 printf("ERROR : A union CANNOT contain a variable size child.\n");
1253 return 1;
1254 break;
1255 case ARRAY:
1256 /* Write the child : varlen child or not ? */
1257 if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) {
1258 /* Error : if an array has a variable size, then its child must also
1259 * have a variable size. */
1260 assert(0);
1261 } else {
1262 print_tabs(1, fd);
1263 fprintf(fd, "/* Variable length child : iter. */\n");
1264 print_tabs(1, fd);
1265 fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename);
3261899d 1266 if(print_type_write(((field_t*)td->fields.array[0])->type,
458989d8 1267 fd, 2, basename, "", "obj->array[i]", 1)) return 1;
a3e6ce64 1268 print_tabs(1, fd);
1269 fprintf(fd, "}\n");
1270 }
1271 break;
1272 default:
1273 printf("print_type_write_fct : type has no write function.\n");
1274 break;
1275 }
1276
1277
1278 }
1279
1280
1281 /* Function footer */
1282 fprintf(fd, "}\n");
1283 fprintf(fd, "\n");
a67cd958 1284 return 0;
1285}
1286
1287
1288
47299663 1289/* Print the logging function of an event. This is the core of genevent */
a3e6ce64 1290int print_event_logging_function(char *basename, facility_t *fac,
1291 event_t *event, FILE *fd)
47299663 1292{
1293 fprintf(fd, "static inline void trace_%s(\n", basename);
3d9b9b0c 1294 int has_argument = 0;
63c831c5 1295 int has_type_fixed = 0;
3d9b9b0c 1296
1297 /* Does it support per trace tracing ? */
1298 if(event->per_trace) {
1299 print_tabs(2, fd);
1300 fprintf(fd, "struct ltt_trace_struct *dest_trace");
1301 has_argument = 1;
1302 }
1303
1304 /* Does it support per tracefile tracing ? */
1305 if(event->per_tracefile) {
1306 if(has_argument) {
1307 fprintf(fd, ",");
1308 fprintf(fd, "\n");
1309 }
1310 fprintf(fd, "unsigned int tracefile_index");
1311 has_argument = 1;
1312 }
1313
47299663 1314 for(unsigned int j = 0; j < event->fields.position; j++) {
1315 /* For each field, print the function argument */
1316 field_t *f = (field_t*)event->fields.array[j];
1317 type_descriptor_t *t = f->type;
3d9b9b0c 1318 if(has_argument) {
47299663 1319 fprintf(fd, ",");
1320 fprintf(fd, "\n");
1321 }
3d9b9b0c 1322 if(print_arg(t, fd, 2, basename, f->name)) return 1;
1323 has_argument = 1;
47299663 1324 }
3d9b9b0c 1325 if(!has_argument) {
47299663 1326 print_tabs(2, fd);
1327 fprintf(fd, "void");
1328 }
1329 fprintf(fd,")\n");
71e09db9 1330 fprintf(fd,
1331 "#if (!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n",
1332 fac->capname);
47299663 1333 fprintf(fd, "{\n");
1334 fprintf(fd, "}\n");
1335 fprintf(fd,"#else\n");
1336 fprintf(fd, "{\n");
7e97b039 1337 /* Print the function variables */
a67cd958 1338 print_tabs(1, fd);
a3e6ce64 1339 fprintf(fd, "unsigned int index;\n");
1340 print_tabs(1, fd);
1341 fprintf(fd, "struct ltt_channel_struct *channel;\n");
1342 print_tabs(1, fd);
1343 fprintf(fd, "struct ltt_trace_struct *trace;\n");
1344 print_tabs(1, fd);
1345 fprintf(fd, "struct rchan_buf *relayfs_buf;\n");
1346 print_tabs(1, fd);
1347 fprintf(fd, "void *buffer = NULL;\n");
1348 print_tabs(1, fd);
458989d8 1349 fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n");
1350 print_tabs(1, fd);
1351 fprintf(fd, "size_t *to_base = &real_to_base;\n");
a3e6ce64 1352 print_tabs(1, fd);
458989d8 1353 fprintf(fd, "size_t real_to = 0;\n");
a3e6ce64 1354 print_tabs(1, fd);
458989d8 1355 fprintf(fd, "size_t *to = &real_to;\n");
a3e6ce64 1356 print_tabs(1, fd);
458989d8 1357 fprintf(fd, "size_t real_len = 0;\n");
1358 print_tabs(1, fd);
1359 fprintf(fd, "size_t *len = &real_len;\n");
30d72138 1360 print_tabs(1, fd);
6e27ba88 1361 fprintf(fd, "size_t reserve_size;\n");
1362 print_tabs(1, fd);
a3e6ce64 1363 fprintf(fd, "size_t slot_size;\n");
30d72138 1364 print_tabs(1, fd);
63c831c5 1365
ac963fe3 1366 if(event->fields.position > 0) {
63c831c5 1367 for(unsigned int i=0;i<event->fields.position;i++){
1368 /* Search for at least one child with fixed size. It means
1369 * we need local variables.*/
1370 field_t *field = (field_t*)(event->fields.array[i]);
1371 type_descriptor_t *type = field->type;
1372 has_type_fixed = has_type_local(type);
1373 if(has_type_fixed) break;
1374 }
1375
1376 if(has_type_fixed) {
8f78c30f 1377 fprintf(fd, "size_t align;\n");
63c831c5 1378 print_tabs(1, fd);
1379 }
1380
ac963fe3 1381 fprintf(fd, "const void *real_from;\n");
1382 print_tabs(1, fd);
1383 fprintf(fd, "const void **from = &real_from;\n");
1384 print_tabs(1, fd);
1385 }
a3e6ce64 1386 fprintf(fd, "cycles_t tsc;\n");
30d72138 1387 print_tabs(1, fd);
458989d8 1388 fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n");
a3e6ce64 1389 fprintf(fd, "\n");
7e97b039 1390
a3e6ce64 1391 print_tabs(1, fd);
1392 fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n");
1393 fprintf(fd, "\n");
1394
a67cd958 1395 /* Calculate event variable len + event data alignment offset.
7e97b039 1396 * Assume that the padding for alignment starts at a void*
a67cd958 1397 * address.
1398 * This excludes the header size and alignment. */
1399
a3e6ce64 1400 print_tabs(1, fd);
1401 fprintf(fd, "/* For each field, calculate the field size. */\n");
1402 print_tabs(1, fd);
458989d8 1403 fprintf(fd, "/* size = *to_base + *to + *len */\n");
a3e6ce64 1404 print_tabs(1, fd);
30d72138 1405 fprintf(fd, "/* Assume that the padding for alignment starts at a\n");
a3e6ce64 1406 print_tabs(1, fd);
30d72138 1407 fprintf(fd, " * sizeof(void *) address. */\n");
a3e6ce64 1408 fprintf(fd, "\n");
a67cd958 1409
a3e6ce64 1410 for(unsigned int i=0;i<event->fields.position;i++){
2e415130 1411 field_t *field = (field_t*)(event->fields.array[i]);
a3e6ce64 1412 type_descriptor_t *type = field->type;
458989d8 1413 /* Set from */
1414 print_tabs(1, fd);
1415 switch(type->type) {
1416 case SEQUENCE:
1417 case UNION:
1418 case ARRAY:
1419 case STRUCT:
1420 case STRING:
3ace7bc4 1421 fprintf(fd, "*from = lttng_param_%s;\n", field->name);
458989d8 1422 break;
1423 default:
3ace7bc4 1424 fprintf(fd, "*from = &lttng_param_%s;\n", field->name);
458989d8 1425 break;
1426 }
1427
a3e6ce64 1428 if(print_type_write(type,
3ace7bc4 1429 fd, 1, basename, field->name, "lttng_param_", 0)) return 1;
a3e6ce64 1430 fprintf(fd, "\n");
a67cd958 1431 }
6e27ba88 1432 print_tabs(1, fd);
458989d8 1433 fprintf(fd, "reserve_size = *to_base + *to + *len;\n");
47299663 1434
7e97b039 1435 /* Take locks : make sure the trace does not vanish while we write on
1436 * it. A simple preemption disabling is enough (using rcu traces). */
a3e6ce64 1437 print_tabs(1, fd);
1438 fprintf(fd, "preempt_disable();\n");
1439 print_tabs(1, fd);
1440 fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n");
1441
1442 /* Get facility index */
1443
1444 if(event->per_tracefile) {
1445 print_tabs(1, fd);
1446 fprintf(fd, "index = tracefile_index;\n");
1447 } else {
1448 print_tabs(1, fd);
1449 fprintf(fd,
1450 "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\
458989d8 1451 "\t\t\t\t\t\tevent_%s_%s);\n",
1452 fac->name, fac->checksum, fac->name, event->name);
a3e6ce64 1453 }
1454 fprintf(fd,"\n");
1455
7e97b039 1456
1457 /* For each trace */
a3e6ce64 1458 print_tabs(1, fd);
1459 fprintf(fd, "list_for_each_entry_rcu(trace, &ltt_traces.head, list) {\n");
1460 print_tabs(2, fd);
1461 fprintf(fd, "if(!trace->active) continue;\n\n");
1462
1463 if(event->per_trace) {
1464 print_tabs(2, fd);
1465 fprintf(fd, "if(dest_trace != trace) continue;\n\n");
1466 }
1467
1468 print_tabs(2, fd);
2e415130 1469 fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n");
a3e6ce64 1470 print_tabs(2, fd);
458989d8 1471 fprintf(fd, "relayfs_buf = channel->rchan->buf[smp_processor_id()];\n");
a3e6ce64 1472 fprintf(fd, "\n");
1473
7e97b039 1474
1475 /* Relay reserve */
a3e6ce64 1476 /* If error, increment event lost counter (done by ltt_reserve_slot) and
1477 * return */
1478 print_tabs(2, fd);
1479 fprintf(fd, "slot_size = 0;\n");
1480 print_tabs(2, fd);
30d72138 1481 fprintf(fd, "buffer = ltt_reserve_slot(trace, relayfs_buf,\n");
1482 print_tabs(3, fd);
6e27ba88 1483 fprintf(fd, "reserve_size, &slot_size, &tsc,\n");
30d72138 1484 print_tabs(3, fd);
458989d8 1485 fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n");
a3e6ce64 1486 /* If error, return */
1487 print_tabs(2, fd);
43f7f14f 1488 fprintf(fd, "if(!buffer) continue; /* buffer full */\n\n");
8f78c30f 1489 //print_tabs(2, fd);
1490 // for DEBUG only
1491 // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n");
1492 print_tabs(2, fd);
1493 fprintf(fd, "*to_base = *to = *len = 0;\n");
1494 fprintf(fd, "\n");
1495
458989d8 1496 /* Write event header */
1497 print_tabs(2, fd);
1498 fprintf(fd, "ltt_write_event_header(trace, channel, buffer,\n");
1499 print_tabs(3, fd);
1500 fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum,
1501 fac->name, event->name);
1502 print_tabs(3, fd);
1503 fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n");
1504 print_tabs(2, fd);
1505 fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n");
2a5eec7d 1506 fprintf(fd, "\n");
7e97b039 1507
8f78c30f 1508 /* write data. */
a3e6ce64 1509
1510 for(unsigned int i=0;i<event->fields.position;i++){
2e415130 1511 field_t *field = (field_t*)(event->fields.array[i]);
a3e6ce64 1512 type_descriptor_t *type = field->type;
8f78c30f 1513
a3e6ce64 1514 /* Set from */
30d72138 1515 print_tabs(2, fd);
1516 switch(type->type) {
1517 case SEQUENCE:
1518 case UNION:
1519 case ARRAY:
1520 case STRUCT:
1521 case STRING:
3ace7bc4 1522 fprintf(fd, "*from = lttng_param_%s;\n", field->name);
30d72138 1523 break;
1524 default:
3ace7bc4 1525 fprintf(fd, "*from = &lttng_param_%s;\n", field->name);
30d72138 1526 break;
1527 }
1528
a3e6ce64 1529
1530 if(print_type_write(type,
3ace7bc4 1531 fd, 2, basename, field->name, "lttng_param_", 0)) return 1;
a3e6ce64 1532 fprintf(fd, "\n");
7e97b039 1533
a3e6ce64 1534 /* Don't forget to flush pending memcpy */
1535 print_tabs(2, fd);
1536 fprintf(fd, "/* Flush pending memcpy */\n");
1537 print_tabs(2, fd);
8f78c30f 1538 fprintf(fd, "if(*len != 0) {\n");
a3e6ce64 1539 print_tabs(3, fd);
458989d8 1540 fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n");
a3e6ce64 1541 print_tabs(3, fd);
458989d8 1542 fprintf(fd, "*to += *len;\n");
a3e6ce64 1543 //print_tabs(3, fd);
1544 //fprintf(fd, "from += len;\n");
1545 print_tabs(3, fd);
458989d8 1546 fprintf(fd, "*len = 0;\n");
a3e6ce64 1547 print_tabs(2, fd);
1548 fprintf(fd, "}\n");
1549 fprintf(fd, "\n");
1550 }
1551
1552
7e97b039 1553 /* commit */
8f78c30f 1554 // for DEBUG only.
1555 //fprintf(fd, "commit:\n"); /* DEBUG! */
a3e6ce64 1556 print_tabs(2, fd);
1557 fprintf(fd, "ltt_commit_slot(relayfs_buf, buffer, slot_size);\n\n");
7e97b039 1558
a3e6ce64 1559 print_tabs(1, fd);
1560 fprintf(fd, "}\n\n");
1561
7e97b039 1562 /* Release locks */
a3e6ce64 1563 print_tabs(1, fd);
1564 fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n");
1565 print_tabs(1, fd);
1566 fprintf(fd, "preempt_enable_no_resched();\n");
2d2d14a7 1567
47299663 1568 fprintf(fd, "}\n");
71e09db9 1569 fprintf(fd, "#endif //(!defined(CONFIG_LTT) || !defined(CONFIG_LTT_FACILITY_%s))\n\n",
1570 fac->capname);
47299663 1571 return 0;
1572}
2d2d14a7 1573
1574
1575/* ltt-facility-name.h : main logging header.
1576 * log_header */
1577
1578void print_log_header_head(facility_t *fac, FILE *fd)
1579{
1580 fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname);
1581 fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname);
3d9b9b0c 1582 fprintf(fd, "#include <linux/types.h>\n");
ffaf5031 1583 if(!fac->arch)
1584 fprintf(fd, "#include <linux/ltt/ltt-facility-id-%s.h>\n", fac->name);
1585 else
1586 fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n",
1587 fac->name,
1588 fac->arch);
3d9b9b0c 1589 fprintf(fd, "#include <linux/ltt-core.h>\n");
47299663 1590 fprintf(fd, "\n");
2d2d14a7 1591}
1592
1593
1594
2d2d14a7 1595int print_log_header_types(facility_t *fac, FILE *fd)
1596{
1597 sequence_t *types = &fac->named_types.values;
1598 fprintf(fd, "/* Named types */\n");
1599 fprintf(fd, "\n");
1600
1601 for(unsigned int i = 0; i < types->position; i++) {
1602 /* For each named type, print the definition */
458989d8 1603 if(print_type_declaration(types->array[i], fd,
1604 0, "", "")) return 1;
34c1d1b5 1605 /* Print also the align function */
458989d8 1606 if(print_type_alignment_fct(types->array[i], fd,
1607 0, "", "")) return 1;
34c1d1b5 1608 /* Print also the write function */
458989d8 1609 if(print_type_write_fct(types->array[i], fd,
1610 0, "", "")) return 1;
2d2d14a7 1611 }
1612 return 0;
1613}
1614
1615int print_log_header_events(facility_t *fac, FILE *fd)
1616{
47299663 1617 sequence_t *events = &fac->events;
1618 char basename[PATH_MAX];
1619 unsigned int facname_len;
1620
1621 strncpy(basename, fac->name, PATH_MAX);
1622 facname_len = strlen(basename);
1623 strncat(basename, "_", PATH_MAX-facname_len);
1624 facname_len = strlen(basename);
1625
1626 for(unsigned int i = 0; i < events->position; i++) {
1627 event_t *event = (event_t*)events->array[i];
1628 strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len);
1629
1630 /* For each event, print structure, and then logging function */
1631 fprintf(fd, "/* Event %s structures */\n",
1632 event->name);
1633 for(unsigned int j = 0; j < event->fields.position; j++) {
1634 /* For each unnamed type, print the definition */
1635 field_t *f = (field_t*)event->fields.array[j];
1636 type_descriptor_t *t = f->type;
34c1d1b5 1637 if(t->type_name == NULL) {
47299663 1638 if((print_type_declaration(t, fd, 0, basename, f->name))) return 1;
34c1d1b5 1639 /* Print also the align function */
1640 if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1;
1641 /* Print also the write function */
1642 if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1;
1643 }
47299663 1644 }
34c1d1b5 1645
47299663 1646 fprintf(fd, "\n");
2d2d14a7 1647
47299663 1648 fprintf(fd, "/* Event %s logging function */\n",
1649 event->name);
1650
a3e6ce64 1651 if(print_event_logging_function(basename, fac, event, fd)) return 1;
47299663 1652
1653 fprintf(fd, "\n");
1654 }
1655
2d2d14a7 1656 return 0;
1657}
1658
1659
1660void print_log_header_tail(facility_t *fac, FILE *fd)
1661{
1662 fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname);
1663}
1664
1665int print_log_header(facility_t *fac)
1666{
1667 char filename[PATH_MAX];
1668 unsigned int filename_size = 0;
1669 FILE *fd;
1670 dprintf("%s\n", fac->name);
1671
1672 strcpy(filename, "ltt-facility-");
1673 filename_size = strlen(filename);
1674
1675 strncat(filename, fac->name, PATH_MAX - filename_size);
1676 filename_size = strlen(filename);
1677
ffaf5031 1678 if(fac->arch) {
1679 strncat(filename, "_", PATH_MAX - filename_size);
1680 filename_size = strlen(filename);
1681
1682 strncat(filename, fac->arch, PATH_MAX - filename_size);
1683 filename_size = strlen(filename);
1684 }
1685
2d2d14a7 1686 strncat(filename, ".h", PATH_MAX - filename_size);
1687 filename_size = strlen(filename);
1688
1689
1690 fd = fopen(filename, "w");
1691 if(fd == NULL) {
1692 printf("Error opening file %s for writing : %s\n",
1693 filename, strerror(errno));
1694 return errno;
1695 }
1696
1697 /* Print file head */
1698 print_log_header_head(fac, fd);
1699
1700 /* print named types in declaration order */
1701 if(print_log_header_types(fac, fd)) return 1;
1702
1703 /* Print events */
1704 if(print_log_header_events(fac, fd)) return 1;
1705
1706 /* Print file tail */
1707 print_log_header_tail(fac, fd);
1708
1709
1710 fclose(fd);
1711
1712 return 0;
1713}
1714
1715
1716/* ltt-facility-id-name.h : facility id.
1717 * log_id_header */
1718int print_id_header(facility_t *fac)
1719{
1720 char filename[PATH_MAX];
1721 unsigned int filename_size = 0;
1722 FILE *fd;
71e09db9 1723 char basename[PATH_MAX];
1724 char basename_len = 0;
1725
2d2d14a7 1726 dprintf("%s\n", fac->name);
1727
1728 strcpy(filename, "ltt-facility-id-");
1729 filename_size = strlen(filename);
1730
1731 strncat(filename, fac->name, PATH_MAX - filename_size);
1732 filename_size = strlen(filename);
1733
ffaf5031 1734 if(fac->arch) {
1735 strncat(filename, "_", PATH_MAX - filename_size);
1736 filename_size = strlen(filename);
1737
1738 strncat(filename, fac->arch, PATH_MAX - filename_size);
1739 filename_size = strlen(filename);
1740 }
1741
2d2d14a7 1742 strncat(filename, ".h", PATH_MAX - filename_size);
1743 filename_size = strlen(filename);
1744
1745
1746 fd = fopen(filename, "w");
1747 if(fd == NULL) {
1748 printf("Error opening file %s for writing : %s\n",
1749 filename, strerror(errno));
1750 return errno;
1751 }
1752
71e09db9 1753 fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname);
1754 fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname);
1755 fprintf(fd, "#ifdef CONFIG_LTT\n");
1756
1757 fprintf(fd,"#include <linux/ltt-facilities.h>\n\n");
1758
1759 fprintf(fd,"/**** facility handle ****/\n\n");
1760 fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n",
1761 fac->name, fac->checksum);
1762 fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name);
1763
1764 strncpy(basename, fac->name, PATH_MAX);
1765 basename_len = strlen(basename);
1766 strncat(basename, "_", PATH_MAX - basename_len);
1767 basename_len++;
1768
1769 fprintf(fd,"/**** event index ****/\n\n");
1770 fprintf(fd,"enum %s_event {\n",fac->name);
1771
1772 for(unsigned int i = 0; i < fac->events.position; i++) {
1773 event_t *event = (event_t*)fac->events.array[i];
1774 strncpy(basename+basename_len, event->name, PATH_MAX-basename_len);
1775 print_tabs(1, fd);
1776 fprintf(fd, "event_%s,\n", basename);
1777 }
1778 print_tabs(1, fd);
1779 fprintf(fd, "facility_%s_num_events\n", fac->name);
1780 fprintf(fd, "};\n");
1781 fprintf(fd, "\n");
1782
1783
1784 fprintf(fd, "#endif //CONFIG_LTT\n");
1785 fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname);
1786
1787
2d2d14a7 1788 fclose(fd);
1789
1790 return 0;
1791}
1792
1793
1794/* ltt-facility-loader-name.h : facility specific loader info.
1795 * loader_header */
1796int print_loader_header(facility_t *fac)
1797{
1798 char filename[PATH_MAX];
1799 unsigned int filename_size = 0;
1800 FILE *fd;
1801 dprintf("%s\n", fac->name);
1802
1803 strcpy(filename, "ltt-facility-loader-");
1804 filename_size = strlen(filename);
1805
1806 strncat(filename, fac->name, PATH_MAX - filename_size);
1807 filename_size = strlen(filename);
1808
ffaf5031 1809 if(fac->arch) {
1810 strncat(filename, "_", PATH_MAX - filename_size);
1811 filename_size = strlen(filename);
1812
1813 strncat(filename, fac->arch, PATH_MAX - filename_size);
1814 filename_size = strlen(filename);
1815 }
1816
2d2d14a7 1817 strncat(filename, ".h", PATH_MAX - filename_size);
1818 filename_size = strlen(filename);
1819
1820
1821 fd = fopen(filename, "w");
1822 if(fd == NULL) {
1823 printf("Error opening file %s for writing : %s\n",
1824 filename, strerror(errno));
1825 return errno;
1826 }
1827
71e09db9 1828 fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
1829 fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname);
1830 fprintf(fd, "#ifdef CONFIG_LTT\n\n");
1831 fprintf(fd,"#include <linux/ltt-facilities.h>\n");
ffaf5031 1832 if(!fac->arch)
1833 fprintf(fd,"#include <linux/ltt/ltt-facility-id-%s.h>\n\n",
1834 fac->name);
1835 else
1836 fprintf(fd,"#include <asm/ltt/ltt-facility-id-%s_%s.h>\n\n",
1837 fac->name,
1838 fac->arch);
71e09db9 1839 fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name);
1840 fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n",
1841 fac->name, fac->checksum);
1842
1843 fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n",
1844 fac->name);
1845 fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n",
1846 fac->name, fac->checksum);
1847 fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum);
1848 fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name);
1849 fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n",
1850 fac->name);
1851 fprintf(fd, "#endif //CONFIG_LTT\n\n");
1852 fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname);
1853
2d2d14a7 1854 fclose(fd);
1855
1856 return 0;
1857}
1858
1859/* ltt-facility-loader-name.c : generic faciilty loader
1860 * loader_c */
1861int print_loader_c(facility_t *fac)
1862{
1863 char filename[PATH_MAX];
1864 unsigned int filename_size = 0;
1865 FILE *fd;
1866 dprintf("%s\n", fac->name);
1867
1868 strcpy(filename, "ltt-facility-loader-");
1869 filename_size = strlen(filename);
1870
1871 strncat(filename, fac->name, PATH_MAX - filename_size);
1872 filename_size = strlen(filename);
1873
ffaf5031 1874 if(fac->arch) {
1875 strncat(filename, "_", PATH_MAX - filename_size);
1876 filename_size = strlen(filename);
1877
1878 strncat(filename, fac->arch, PATH_MAX - filename_size);
1879 filename_size = strlen(filename);
1880 }
1881
2d2d14a7 1882 strncat(filename, ".c", PATH_MAX - filename_size);
1883 filename_size = strlen(filename);
1884
1885
1886 fd = fopen(filename, "w");
1887 if(fd == NULL) {
1888 printf("Error opening file %s for writing : %s\n",
1889 filename, strerror(errno));
1890 return errno;
1891 }
1892
71e09db9 1893 fprintf(fd, "/*\n");
ffaf5031 1894 if(!fac->arch)
1895 fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name);
1896 else
1897 fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch);
71e09db9 1898 fprintf(fd, " *\n");
1899 fprintf(fd, " * (C) Copyright 2005 - \n");
1900 fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n");
1901 fprintf(fd, " *\n");
1902 fprintf(fd, " * Contains the LTT facility loader.\n");
1903 fprintf(fd, " *\n");
1904 fprintf(fd, " */\n");
1905 fprintf(fd, "\n");
1906 fprintf(fd, "\n");
1907 fprintf(fd, "#include <linux/ltt-facilities.h>\n");
1908 fprintf(fd, "#include <linux/module.h>\n");
1909 fprintf(fd, "#include <linux/init.h>\n");
1910 fprintf(fd, "#include <linux/config.h>\n");
ffaf5031 1911 if(!fac->arch)
1912 fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name);
1913 else
1914 fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n",
1915 fac->name, fac->arch);
71e09db9 1916 fprintf(fd, "\n");
1917 fprintf(fd, "\n");
1918 fprintf(fd, "#ifdef CONFIG_LTT\n");
1919 fprintf(fd, "\n");
1920 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n");
1921 fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n");
1922 fprintf(fd, "\n");
1923 fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n");
1924 fprintf(fd, "\n");
1925 fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n");
1926 fprintf(fd, "\n");
1927 fprintf(fd, "static struct ltt_facility facility = {\n");
1928 fprintf(fd, "\t.name = ltt_facility_name,\n");
1929 fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n");
1930 fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n");
1931 fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n");
1932 fprintf(fd, "};\n");
1933 fprintf(fd, "\n");
1934 fprintf(fd, "#ifndef MODULE\n");
1935 fprintf(fd, "\n");
1936 fprintf(fd, "/* Built-in facility. */\n");
1937 fprintf(fd, "\n");
1938 fprintf(fd, "static int __init facility_init(void)\n");
1939 fprintf(fd, "{\n");
1940 fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name);
1941 fprintf(fd, "\n");
1942 fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_builtin_register(&facility);\n");
1943 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1944 fprintf(fd, "\t\n");
1945 fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
1946 fprintf(fd, "}\n");
1947 fprintf(fd, "__initcall(facility_init);\n");
1948 fprintf(fd, "\n");
1949 fprintf(fd, "\n");
1950 fprintf(fd, "\n");
1951 fprintf(fd, "#else \n");
1952 fprintf(fd, "\n");
1953 fprintf(fd, "/* Dynamic facility. */\n");
1954 fprintf(fd, "\n");
1955 fprintf(fd, "static int __init facility_init(void)\n");
1956 fprintf(fd, "{\n");
1957 fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init dynamic\\n\");\n", fac->name);
1958 fprintf(fd, "\n");
1959 fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_dynamic_register(&facility);\n");
1960 fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n");
1961 fprintf(fd, "\n");
1962 fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n");
1963 fprintf(fd, "}\n");
1964 fprintf(fd, "\n");
1965 fprintf(fd, "static void __exit facility_exit(void)\n");
1966 fprintf(fd, "{\n");
1967 fprintf(fd, "\tint err;\n");
1968 fprintf(fd, "\n");
1969 fprintf(fd, "\terr = ltt_facility_dynamic_unregister(LTT_FACILITY_SYMBOL);\n");
1970 fprintf(fd, "\tif(err != 0)\n");
1971 fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n");
1972 fprintf(fd, "\n");
1973 fprintf(fd, "}\n");
1974 fprintf(fd, "\n");
1975 fprintf(fd, "module_init(facility_init)\n");
1976 fprintf(fd, "module_exit(facility_exit)\n");
1977 fprintf(fd, "\n");
1978 fprintf(fd, "\n");
1979 fprintf(fd, "MODULE_LICENSE(\"GPL\");\n");
1980 fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n");
1981 fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n");
1982 fprintf(fd, "\n");
1983 fprintf(fd, "#endif //MODULE\n");
1984 fprintf(fd, "\n");
1985 fprintf(fd, "#endif //CONFIG_LTT\n");
2d2d14a7 1986
1987 fclose(fd);
1988
1989 return 0;
1990}
1991
1992
1993
92d82357 1994/* open facility */
1995/* code taken from ltt_facility_open in ltt/facility.c in lttv */
1996
1997/*****************************************************************************
1998 *Function name
1999 * ltt_facility_open : open facilities
2000 *Input params
2001 * pathname : the path name of the facility
2002 *
2003 * Open the facility corresponding to the right checksum.
2004 *
2005 *returns the facility on success, NULL on error.
2006 ****************************************************************************/
2007facility_t *ltt_facility_open(char * pathname)
2008{
2009 int ret = 0;
2010 char *token;
2011 parse_file_t in;
2012 facility_t * fac = NULL;
92d82357 2013 char buffer[BUFFER_SIZE];
2014 int generated = FALSE;
2015
2016 in.buffer = &(buffer[0]);
2017 in.lineno = 0;
2018 in.error = error_callback;
2019 in.name = pathname;
2020 in.unget = 0;
2021
2022 in.fp = fopen(in.name, "r");
2023 if(in.fp == NULL) {
2024 ret = 1;
2025 goto open_error;
2026 }
2027
2028 while(1){
2029 token = getToken(&in);
2030 if(in.type == ENDFILE) break;
2031
2032 if(generated) {
2033 printf("More than one facility in the file. Only using the first one.\n");
2034 break;
2035 }
2036
2037 if(strcmp(token, "<")) in.error(&in,"not a facility file");
2038 token = getName(&in);
2039
2040 if(strcmp("facility",token) == 0) {
2041 fac = malloc(sizeof(facility_t));
2042 fac->name = NULL;
2043 fac->description = NULL;
2044 sequence_init(&(fac->events));
2045 table_init(&(fac->named_types));
2046 sequence_init(&(fac->unnamed_types));
2047
2048 parseFacility(&in, fac);
2049
2050 //check if any namedType is not defined
2051 checkNamedTypesImplemented(&fac->named_types);
2052
2e415130 2053 generateChecksum(fac->name, &fac->checksum, &fac->events);
a67cd958 2054
92d82357 2055 generated = TRUE;
2056 }
2057 else {
2058 printf("facility token was expected in file %s\n", in.name);
2059 ret = 1;
2060 goto parse_error;
2061 }
2062 }
2063
2064 parse_error:
2065 fclose(in.fp);
2066open_error:
2067
2068 if(!generated) {
2069 printf("Cannot find facility %s\n", pathname);
2070 fac = NULL;
2071 }
2072
2073 return fac;
2074}
2075
2076/* Close the facility */
2077void ltt_facility_close(facility_t *fac)
2078{
2079 free(fac->name);
2080 free(fac->capname);
2081 free(fac->description);
2082 freeEvents(&fac->events);
2083 sequence_dispose(&fac->events);
2084 freeNamedType(&fac->named_types);
2085 table_dispose(&fac->named_types);
2086 freeTypes(&fac->unnamed_types);
2087 sequence_dispose(&fac->unnamed_types);
2088 free(fac);
2089}
2090
2091
2092/* Show help */
2093void show_help(int argc, char ** argv)
2094{
2095 printf("Genevent help : \n");
2096 printf("\n");
2097 printf("Use %s name.xml\n", argv[0]);
2098 printf("to create :\n");
2099 printf("ltt-facility-name.h\n");
2100 printf("ltt-facility-id-name.h\n");
2101 printf("ltt-facility-loader-name.h\n");
2102 printf("ltt-facility-loader-name.c\n");
2103 printf("In the current directory.\n");
2104 printf("\n");
2105}
2106
2107/* Parse program arguments */
2108/* Return values :
2109 * 0 : continue program
2110 * -1 : stop program, return 0
2111 * > 0 : stop program, return value as exit.
2112 */
2113int check_args(int argc, char **argv)
2114{
2115 if(argc < 2) {
2116 printf("Not enough arguments\n");
2117 show_help(argc, argv);
2118 return EINVAL;
2119 }
2120
2121 if(strcmp(argv[1], "-h") == 0) {
2122 show_help(argc, argv);
2123 return -1;
2124 }
2125
2126 return 0;
2127}
2128
2129int main(int argc, char **argv)
2130{
2131 int err = 0;
2132 facility_t *fac;
2133
2134 err = check_args(argc, argv);
2135 if(err > 0) return err;
2136 else if(err < 0) return 0;
2137
2138 /* open the facility */
2139 fac = ltt_facility_open(argv[1]);
2140 if(fac == NULL) {
2141 printf("Error opening file %s for reading : %s\n",
2142 argv[1], strerror(errno));
2143 return errno;
2144 }
2145
2146 /* generate the output C files */
2147
2148
2d2d14a7 2149 /* ltt-facility-name.h : main logging header.
2150 * log_header */
2151 err = print_log_header(fac);
2152 if(err) return err;
92d82357 2153
2d2d14a7 2154 /* ltt-facility-id-name.h : facility id.
2155 * log_id_header */
2156 err = print_id_header(fac);
2157 if(err) return err;
92d82357 2158
2d2d14a7 2159 /* ltt-facility-loader-name.h : facility specific loader info.
2160 * loader_header */
2161 err = print_loader_header(fac);
2162 if(err) return err;
2163
2164 /* ltt-facility-loader-name.c : generic faciilty loader
2165 * loader_c */
2166 err = print_loader_c(fac);
2167 if(err) return err;
92d82357 2168
2169 /* close the facility */
2170 ltt_facility_close(fac);
2171
2172 return 0;
2173}
2174
2175
This page took 0.126955 seconds and 4 git commands to generate.