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