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