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