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