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