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