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