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