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