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 | |
922b1092 |
89 | |
90 | enum user_fct_types { USER_FCT_PROTO, USER_FCT_DECLARATION } ; |
91 | |
92d82357 |
92 | /* Code printing */ |
93 | |
2d2d14a7 |
94 | void print_tabs(unsigned int tabs, FILE *fd) |
95 | { |
96 | for(unsigned int i = 0; i<tabs;i++) |
97 | fprintf(fd, "\t"); |
98 | } |
99 | |
2d2d14a7 |
100 | /* print type. |
101 | * |
102 | * Copied from construct_types_and_fields in LTTV facility.c */ |
103 | |
104 | int print_type(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
105 | char *nest_name, char *field_name) |
106 | { |
107 | char basename[PATH_MAX]; |
108 | unsigned int basename_len = 0; |
109 | |
110 | strcpy(basename, nest_name); |
111 | basename_len = strlen(basename); |
112 | |
113 | /* For a named type, we use the type_name directly */ |
114 | if(td->type_name != NULL) { |
115 | strncpy(basename, td->type_name, PATH_MAX); |
116 | basename_len = strlen(basename); |
117 | } else { |
118 | /* For a unnamed type, there must be a field name */ |
7b175edc |
119 | if((basename_len != 0) |
120 | && (basename[basename_len-1] != '_') |
121 | && (field_name[0] != '\0')) { |
2d2d14a7 |
122 | strncat(basename, "_", PATH_MAX - basename_len); |
123 | basename_len = strlen(basename); |
124 | } |
125 | strncat(basename, field_name, PATH_MAX - basename_len); |
126 | } |
127 | |
128 | switch(td->type) { |
129 | case INT_FIXED: |
130 | fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]); |
131 | break; |
132 | case UINT_FIXED: |
133 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
134 | break; |
135 | case CHAR: |
136 | fprintf(fd, "signed char"); |
137 | break; |
138 | case UCHAR: |
139 | fprintf(fd, "unsigned char"); |
140 | break; |
141 | case SHORT: |
142 | fprintf(fd, "short"); |
143 | break; |
144 | case USHORT: |
145 | fprintf(fd, "unsigned short"); |
146 | break; |
147 | case INT: |
148 | fprintf(fd, "int"); |
149 | break; |
150 | case UINT: |
151 | fprintf(fd, "unsigned int"); |
152 | break; |
153 | case FLOAT: |
154 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
155 | break; |
156 | case POINTER: |
458989d8 |
157 | fprintf(fd, "const void *"); |
2d2d14a7 |
158 | break; |
159 | case LONG: |
160 | fprintf(fd, "long"); |
161 | break; |
162 | case ULONG: |
163 | fprintf(fd, "unsigned long"); |
164 | break; |
165 | case SIZE_T: |
166 | fprintf(fd, "size_t"); |
167 | break; |
168 | case SSIZE_T: |
169 | fprintf(fd, "ssize_t"); |
170 | break; |
171 | case OFF_T: |
172 | fprintf(fd, "off_t"); |
173 | break; |
174 | case STRING: |
3d9b9b0c |
175 | fprintf(fd, "const char *"); |
2d2d14a7 |
176 | break; |
177 | case ENUM: |
178 | fprintf(fd, "enum lttng_%s", basename); |
179 | break; |
180 | case ARRAY: |
181 | fprintf(fd, "lttng_array_%s", basename); |
182 | break; |
183 | case SEQUENCE: |
184 | fprintf(fd, "lttng_sequence_%s", basename); |
185 | break; |
186 | case STRUCT: |
187 | fprintf(fd, "struct lttng_%s", basename); |
188 | break; |
189 | case UNION: |
190 | fprintf(fd, "union lttng_%s", basename); |
191 | break; |
192 | default: |
193 | printf("print_type : unknown type\n"); |
194 | return 1; |
195 | } |
196 | |
197 | return 0; |
198 | } |
199 | |
7e97b039 |
200 | /* Print logging function argument */ |
201 | int print_arg(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
202 | char *nest_name, char *field_name) |
203 | { |
204 | char basename[PATH_MAX]; |
205 | unsigned int basename_len = 0; |
206 | |
207 | strcpy(basename, nest_name); |
208 | basename_len = strlen(basename); |
209 | |
210 | /* For a named type, we use the type_name directly */ |
211 | if(td->type_name != NULL) { |
212 | strncpy(basename, td->type_name, PATH_MAX); |
213 | basename_len = strlen(basename); |
214 | } else { |
215 | /* For a unnamed type, there must be a field name */ |
216 | if((basename_len != 0) |
217 | && (basename[basename_len-1] != '_') |
218 | && (field_name[0] != '\0')) { |
219 | strncat(basename, "_", PATH_MAX - basename_len); |
220 | basename_len = strlen(basename); |
221 | } |
222 | strncat(basename, field_name, PATH_MAX - basename_len); |
223 | } |
224 | |
225 | print_tabs(tabs, fd); |
226 | |
227 | switch(td->type) { |
228 | case INT_FIXED: |
229 | fprintf(fd, "%s", intOutputTypes[getSizeindex(td->size)]); |
1f42154b |
230 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
231 | break; |
232 | case UINT_FIXED: |
233 | fprintf(fd, "%s", uintOutputTypes[getSizeindex(td->size)]); |
1f42154b |
234 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
235 | break; |
236 | case CHAR: |
237 | fprintf(fd, "signed char"); |
3ace7bc4 |
238 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
239 | break; |
240 | case UCHAR: |
241 | fprintf(fd, "unsigned char"); |
3ace7bc4 |
242 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
243 | break; |
244 | case SHORT: |
245 | fprintf(fd, "short"); |
3ace7bc4 |
246 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
247 | break; |
248 | case USHORT: |
249 | fprintf(fd, "unsigned short"); |
3ace7bc4 |
250 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
251 | break; |
252 | case INT: |
253 | fprintf(fd, "int"); |
3ace7bc4 |
254 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
255 | break; |
256 | case UINT: |
257 | fprintf(fd, "unsigned int"); |
3ace7bc4 |
258 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
259 | break; |
260 | case FLOAT: |
261 | fprintf(fd, "%s", floatOutputTypes[getSizeindex(td->size)]); |
3ace7bc4 |
262 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
263 | break; |
264 | case POINTER: |
458989d8 |
265 | fprintf(fd, "const void *"); |
3ace7bc4 |
266 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
267 | break; |
268 | case LONG: |
269 | fprintf(fd, "long"); |
3ace7bc4 |
270 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
271 | break; |
272 | case ULONG: |
273 | fprintf(fd, "unsigned long"); |
3ace7bc4 |
274 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
275 | break; |
276 | case SIZE_T: |
277 | fprintf(fd, "size_t"); |
3ace7bc4 |
278 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
279 | break; |
280 | case SSIZE_T: |
281 | fprintf(fd, "ssize_t"); |
3ace7bc4 |
282 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
283 | break; |
284 | case OFF_T: |
285 | fprintf(fd, "off_t"); |
3ace7bc4 |
286 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
287 | break; |
288 | case STRING: |
3d9b9b0c |
289 | fprintf(fd, "const char *"); |
3ace7bc4 |
290 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
291 | break; |
292 | case ENUM: |
293 | fprintf(fd, "enum lttng_%s", basename); |
3ace7bc4 |
294 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
295 | break; |
296 | case ARRAY: |
297 | fprintf(fd, "lttng_array_%s", basename); |
3ace7bc4 |
298 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
299 | break; |
300 | case SEQUENCE: |
301 | fprintf(fd, "lttng_sequence_%s *", basename); |
3ace7bc4 |
302 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
303 | break; |
304 | case STRUCT: |
305 | fprintf(fd, "struct lttng_%s *", basename); |
3ace7bc4 |
306 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
307 | break; |
308 | case UNION: |
309 | fprintf(fd, "union lttng_%s *", basename); |
3ace7bc4 |
310 | fprintf(fd, " lttng_param_%s", field_name); |
7e97b039 |
311 | break; |
312 | default: |
313 | printf("print_type : unknown type\n"); |
314 | return 1; |
315 | } |
316 | |
317 | return 0; |
318 | } |
319 | |
320 | |
a3e6ce64 |
321 | /* Does the type has a fixed size ? (as know from the compiler) |
322 | * |
323 | * 1 : fixed size |
324 | * 0 : variable length |
325 | */ |
326 | int has_type_fixed_size(type_descriptor_t *td) |
327 | { |
328 | switch(td->type) { |
329 | case INT_FIXED: |
330 | case UINT_FIXED: |
331 | case CHAR: |
332 | case UCHAR: |
333 | case SHORT: |
334 | case USHORT: |
335 | case INT: |
336 | case UINT: |
337 | case FLOAT: |
338 | case POINTER: |
339 | case LONG: |
340 | case ULONG: |
341 | case SIZE_T: |
342 | case SSIZE_T: |
343 | case OFF_T: |
344 | case ENUM: |
345 | case UNION: /* The union must have fixed size children. Must be checked by |
346 | the parser */ |
347 | return 1; |
348 | break; |
349 | case STRING: |
350 | case SEQUENCE: |
351 | return 0; |
352 | break; |
353 | case STRUCT: |
354 | { |
355 | int has_type_fixed = 0; |
356 | for(unsigned int i=0;i<td->fields.position;i++){ |
357 | field_t *field = (field_t*)(td->fields.array[i]); |
358 | type_descriptor_t *type = field->type; |
359 | |
360 | has_type_fixed = has_type_fixed_size(type); |
361 | if(!has_type_fixed) return 0; |
362 | } |
363 | return 1; |
364 | } |
365 | break; |
366 | case ARRAY: |
367 | assert(td->size >= 0); |
2e415130 |
368 | return has_type_fixed_size(((field_t*)td->fields.array[0])->type); |
369 | break; |
370 | case NONE: |
371 | printf("There is a type defined to NONE : bad.\n"); |
372 | assert(0); |
a3e6ce64 |
373 | break; |
374 | } |
2e415130 |
375 | return 0; //make gcc happy. |
a3e6ce64 |
376 | } |
377 | |
378 | |
379 | |
380 | |
381 | |
2d2d14a7 |
382 | /* print type declaration. |
383 | * |
384 | * Copied from construct_types_and_fields in LTTV facility.c */ |
385 | |
386 | int print_type_declaration(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
387 | char *nest_name, char *field_name) |
388 | { |
389 | char basename[PATH_MAX]; |
390 | unsigned int basename_len = 0; |
391 | |
64a6ab10 |
392 | if(td->custom_write) return 0; /* Does print custom type */ |
393 | |
7b175edc |
394 | strncpy(basename, nest_name, PATH_MAX); |
2d2d14a7 |
395 | basename_len = strlen(basename); |
396 | |
397 | /* For a named type, we use the type_name directly */ |
398 | if(td->type_name != NULL) { |
399 | strncpy(basename, td->type_name, PATH_MAX); |
400 | basename_len = strlen(basename); |
401 | } else { |
7b175edc |
402 | /* For a unnamed type, there must be a field name, except for |
403 | * the array. */ |
404 | if((basename_len != 0) |
405 | && (basename[basename_len-1] != '_' |
406 | && (field_name[0] != '\0'))) { |
2d2d14a7 |
407 | strncat(basename, "_", PATH_MAX - basename_len); |
408 | basename_len = strlen(basename); |
409 | } |
410 | strncat(basename, field_name, PATH_MAX - basename_len); |
2d2d14a7 |
411 | } |
412 | |
413 | switch(td->type) { |
414 | case ENUM: |
415 | fprintf(fd, "enum lttng_%s", basename); |
416 | fprintf(fd, " {\n"); |
417 | for(unsigned int i=0;i<td->labels.position;i++){ |
418 | print_tabs(1, fd); |
70f46ac3 |
419 | fprintf(fd, "LTTNG_%s = %d", ((char*)td->labels.array[i]), |
420 | (*(int*)td->labels_values.array[i])); |
2d2d14a7 |
421 | fprintf(fd, ",\n"); |
422 | } |
423 | fprintf(fd, "};\n"); |
424 | fprintf(fd, "\n"); |
425 | break; |
426 | |
427 | case ARRAY: |
7b175edc |
428 | dprintf("%s\n", basename); |
2d2d14a7 |
429 | assert(td->size >= 0); |
a67cd958 |
430 | if(((field_t*)td->fields.array[0])->type->type_name == NULL) { |
2d2d14a7 |
431 | /* Not a named nested type : we must print its declaration first */ |
a67cd958 |
432 | if(print_type_declaration(((field_t*)td->fields.array[0])->type, |
2d2d14a7 |
433 | fd, 0, basename, "")) return 1; |
434 | } |
2e415130 |
435 | fprintf(fd, "#define LTTNG_ARRAY_SIZE_%s %zu\n", basename, |
2d2d14a7 |
436 | td->size); |
7e97b039 |
437 | fprintf(fd, "typedef "); |
a67cd958 |
438 | if(print_type(((field_t*)td->fields.array[0])->type, |
439 | fd, tabs, basename, "")) return 1; |
2d2d14a7 |
440 | fprintf(fd, " lttng_array_%s[LTTNG_ARRAY_SIZE_%s];\n", basename, |
441 | basename); |
442 | fprintf(fd, "\n"); |
443 | break; |
444 | case SEQUENCE: |
a67cd958 |
445 | /* We assume that the sequence length type does not need to be declared. |
446 | */ |
447 | if(((field_t*)td->fields.array[1])->type->type_name == NULL) { |
2d2d14a7 |
448 | /* Not a named nested type : we must print its declaration first */ |
a67cd958 |
449 | if(print_type_declaration(((field_t*)td->fields.array[1])->type, |
2d2d14a7 |
450 | fd, 0, basename, "")) return 1; |
451 | } |
452 | fprintf(fd, "typedef struct lttng_sequence_%s lttng_sequence_%s;\n", |
453 | basename, |
454 | basename); |
455 | fprintf(fd, "struct lttng_sequence_%s", basename); |
456 | fprintf(fd, " {\n"); |
457 | print_tabs(1, fd); |
a3e6ce64 |
458 | if(print_type(((field_t*)td->fields.array[0])->type, |
459 | fd, tabs, basename, "")) return 1; |
40c18b13 |
460 | fprintf(fd, " len;\n"); |
2d2d14a7 |
461 | print_tabs(1, fd); |
8c9e9076 |
462 | fprintf(fd, "const "); |
a67cd958 |
463 | if(print_type(((field_t*)td->fields.array[1])->type, |
464 | fd, tabs, basename, "")) return 1; |
2d2d14a7 |
465 | fprintf(fd, " *array;\n"); |
d428224c |
466 | fprintf(fd, "};\n"); /* We do not LTT_ALIGN, because we never copy |
467 | it to the buffer directly. */ |
2d2d14a7 |
468 | fprintf(fd, "\n"); |
469 | break; |
470 | |
471 | case STRUCT: |
472 | for(unsigned int i=0;i<td->fields.position;i++){ |
473 | field_t *field = (field_t*)(td->fields.array[i]); |
474 | type_descriptor_t *type = field->type; |
475 | if(type->type_name == NULL) { |
476 | /* Not a named nested type : we must print its declaration first */ |
477 | if(print_type_declaration(type, |
478 | fd, 0, basename, field->name)) return 1; |
479 | } |
480 | } |
481 | fprintf(fd, "struct lttng_%s", basename); |
482 | fprintf(fd, " {\n"); |
483 | for(unsigned int i=0;i<td->fields.position;i++){ |
484 | field_t *field = (field_t*)(td->fields.array[i]); |
485 | type_descriptor_t *type = field->type; |
486 | print_tabs(1, fd); |
47299663 |
487 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
2d2d14a7 |
488 | fprintf(fd, " "); |
489 | fprintf(fd, "%s", field->name); |
490 | fprintf(fd, ";\n"); |
491 | } |
d428224c |
492 | fprintf(fd, "} LTT_ALIGN;\n"); |
2d2d14a7 |
493 | fprintf(fd, "\n"); |
494 | break; |
495 | case UNION: |
2d2d14a7 |
496 | for(unsigned int i=0;i<td->fields.position;i++){ |
497 | field_t *field = (field_t*)(td->fields.array[i]); |
498 | type_descriptor_t *type = field->type; |
499 | if(type->type_name == NULL) { |
500 | /* Not a named nested type : we must print its declaration first */ |
47299663 |
501 | if(print_type_declaration(type, |
502 | fd, 0, basename, field->name)) return 1; |
2d2d14a7 |
503 | } |
504 | } |
505 | fprintf(fd, "union lttng_%s", basename); |
506 | fprintf(fd, " {\n"); |
507 | for(unsigned i=0;i<td->fields.position;i++){ |
508 | field_t *field = (field_t*)(td->fields.array[i]); |
509 | type_descriptor_t *type = field->type; |
510 | print_tabs(1, fd); |
47299663 |
511 | if(print_type(type, fd, tabs, basename, field->name)) return 1; |
2d2d14a7 |
512 | fprintf(fd, " "); |
513 | fprintf(fd, "%s", field->name); |
514 | fprintf(fd, ";\n"); |
515 | } |
d428224c |
516 | fprintf(fd, "} LTT_ALIGN;\n"); |
2d2d14a7 |
517 | fprintf(fd, "\n"); |
518 | break; |
519 | default: |
520 | dprintf("print_type_declaration : unknown type or nothing to declare.\n"); |
521 | break; |
522 | } |
523 | |
524 | return 0; |
525 | } |
526 | |
a67cd958 |
527 | |
a3e6ce64 |
528 | /* print type alignment. |
529 | * |
530 | * Copied from construct_types_and_fields in LTTV facility.c |
531 | * |
532 | * basename is the name which identifies the type (along with a prefix |
533 | * (possibly)). */ |
a67cd958 |
534 | |
a3e6ce64 |
535 | int print_type_alignment(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
3261899d |
536 | char *nest_name, char *field_name, char *obj_prefix) |
a67cd958 |
537 | { |
a3e6ce64 |
538 | char basename[PATH_MAX]; |
539 | unsigned int basename_len = 0; |
540 | |
541 | strncpy(basename, nest_name, PATH_MAX); |
542 | basename_len = strlen(basename); |
543 | |
544 | /* For a named type, we use the type_name directly */ |
545 | if(td->type_name != NULL) { |
546 | strncpy(basename, td->type_name, PATH_MAX); |
547 | basename_len = strlen(basename); |
a67cd958 |
548 | } else { |
a3e6ce64 |
549 | /* For a unnamed type, there must be a field name, except for |
550 | * the array. */ |
551 | if((basename_len != 0) |
552 | && (basename[basename_len-1] != '_' |
34c1d1b5 |
553 | && field_name != NULL |
a3e6ce64 |
554 | && (field_name[0] != '\0'))) { |
555 | strncat(basename, "_", PATH_MAX - basename_len); |
556 | basename_len = strlen(basename); |
a67cd958 |
557 | } |
34c1d1b5 |
558 | if(field_name != NULL) |
559 | strncat(basename, field_name, PATH_MAX - basename_len); |
a67cd958 |
560 | } |
a3e6ce64 |
561 | |
3d9b9b0c |
562 | if(field_name[0] == '\0') { |
563 | /* We are in a write function : it's the obj that we must align. */ |
564 | switch(td->type) { |
565 | case SEQUENCE: |
3261899d |
566 | fprintf(fd, "lttng_get_alignment_sequence_%s(%s)", basename, |
567 | obj_prefix); |
3d9b9b0c |
568 | break; |
569 | case STRUCT: |
3261899d |
570 | fprintf(fd, "lttng_get_alignment_struct_%s(%s)", basename, |
571 | obj_prefix); |
3d9b9b0c |
572 | break; |
573 | case UNION: |
3261899d |
574 | fprintf(fd, "lttng_get_alignment_union_%s(%s)", basename, |
575 | obj_prefix); |
3d9b9b0c |
576 | break; |
577 | case ARRAY: |
3261899d |
578 | fprintf(fd, "lttng_get_alignment_array_%s(%s)", basename, |
579 | obj_prefix); |
a2ff13ed |
580 | case STRING: |
581 | fprintf(fd, "sizeof(char)"); |
3d9b9b0c |
582 | break; |
8f78c30f |
583 | case INT_FIXED: |
584 | case UINT_FIXED: |
585 | case CHAR: |
586 | case UCHAR: |
587 | case SHORT: |
588 | case USHORT: |
589 | case INT: |
590 | case UINT: |
591 | case FLOAT: |
592 | case POINTER: |
593 | case LONG: |
594 | case ULONG: |
595 | case SIZE_T: |
596 | case SSIZE_T: |
597 | case OFF_T: |
598 | case ENUM: |
599 | fprintf(fd, "sizeof("); |
600 | if(print_type(td, fd, 0, basename, "")) return 1; |
601 | fprintf(fd, ")"); |
602 | break; |
603 | |
3d9b9b0c |
604 | default: |
605 | printf("error : type unexpected\n"); |
606 | return 1; |
607 | break; |
608 | } |
609 | } else { |
610 | |
611 | switch(td->type) { |
612 | case INT_FIXED: |
613 | case UINT_FIXED: |
614 | case CHAR: |
615 | case UCHAR: |
616 | case SHORT: |
617 | case USHORT: |
618 | case INT: |
619 | case UINT: |
620 | case FLOAT: |
621 | case POINTER: |
622 | case LONG: |
623 | case ULONG: |
624 | case SIZE_T: |
625 | case SSIZE_T: |
626 | case OFF_T: |
627 | case ENUM: |
628 | fprintf(fd, "sizeof("); |
629 | if(print_type(td, fd, 0, basename, "")) return 1; |
630 | fprintf(fd, ")"); |
631 | break; |
632 | case STRING: |
633 | fprintf(fd, "sizeof(char)"); |
634 | break; |
635 | case SEQUENCE: |
3261899d |
636 | fprintf(fd, "lttng_get_alignment_sequence_%s(&%s%s)", basename, |
637 | obj_prefix, field_name); |
3d9b9b0c |
638 | break; |
639 | case STRUCT: |
3261899d |
640 | fprintf(fd, "lttng_get_alignment_struct_%s(&%s%s)", basename, |
641 | obj_prefix, field_name); |
3d9b9b0c |
642 | break; |
643 | case UNION: |
3261899d |
644 | fprintf(fd, "lttng_get_alignment_union_%s(&%s%s)", basename, |
645 | obj_prefix, field_name); |
3d9b9b0c |
646 | break; |
647 | case ARRAY: |
3261899d |
648 | fprintf(fd, "lttng_get_alignment_array_%s(%s%s)", basename, |
649 | obj_prefix, field_name); |
3d9b9b0c |
650 | break; |
651 | case NONE: |
652 | printf("error : type NONE unexpected\n"); |
653 | return 1; |
654 | break; |
655 | } |
a3e6ce64 |
656 | } |
3d9b9b0c |
657 | |
a3e6ce64 |
658 | return 0; |
a67cd958 |
659 | } |
660 | |
a3e6ce64 |
661 | /* print type write. |
662 | * |
663 | * Copied from construct_types_and_fields in LTTV facility.c |
664 | * |
665 | * basename is the name which identifies the type (along with a prefix |
666 | * (possibly)). */ |
a67cd958 |
667 | |
a3e6ce64 |
668 | int print_type_write(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
458989d8 |
669 | char *nest_name, char *field_name, char *obj_prefix, int get_ptr) |
a67cd958 |
670 | { |
a67cd958 |
671 | char basename[PATH_MAX]; |
672 | unsigned int basename_len = 0; |
458989d8 |
673 | char get_ptr_char[2] = ""; |
64a6ab10 |
674 | char custom[PATH_MAX] = ""; |
a3e6ce64 |
675 | |
676 | strncpy(basename, nest_name, PATH_MAX); |
a67cd958 |
677 | basename_len = strlen(basename); |
678 | |
679 | /* For a named type, we use the type_name directly */ |
680 | if(td->type_name != NULL) { |
681 | strncpy(basename, td->type_name, PATH_MAX); |
682 | basename_len = strlen(basename); |
683 | } else { |
a3e6ce64 |
684 | /* For a unnamed type, there must be a field name, except for |
685 | * the array. */ |
a67cd958 |
686 | if((basename_len != 0) |
a3e6ce64 |
687 | && (basename[basename_len-1] != '_' |
688 | && (field_name[0] != '\0'))) { |
a67cd958 |
689 | strncat(basename, "_", PATH_MAX - basename_len); |
690 | basename_len = strlen(basename); |
691 | } |
692 | strncat(basename, field_name, PATH_MAX - basename_len); |
693 | } |
694 | |
458989d8 |
695 | if(get_ptr) { |
696 | strcpy(get_ptr_char, "&"); |
697 | } |
698 | |
64a6ab10 |
699 | if(td->custom_write) { |
700 | strcpy(custom, "_custom"); |
701 | } |
702 | |
a67cd958 |
703 | switch(td->type) { |
704 | case INT_FIXED: |
705 | case UINT_FIXED: |
706 | case CHAR: |
707 | case UCHAR: |
708 | case SHORT: |
709 | case USHORT: |
710 | case INT: |
711 | case UINT: |
712 | case FLOAT: |
713 | case POINTER: |
714 | case LONG: |
715 | case ULONG: |
716 | case SIZE_T: |
717 | case SSIZE_T: |
718 | case OFF_T: |
719 | case ENUM: |
a67cd958 |
720 | print_tabs(tabs, fd); |
8f78c30f |
721 | fprintf(fd, "align = "); |
722 | if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1; |
723 | fprintf(fd, ";\n"); |
724 | fprintf(fd, "\n"); |
725 | print_tabs(tabs, fd); |
726 | fprintf(fd, "if(*len == 0) {\n"); |
727 | print_tabs(tabs+1, fd); |
728 | fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n"); |
729 | print_tabs(tabs, fd); |
730 | fprintf(fd, "} else {\n"); |
731 | print_tabs(tabs+1, fd); |
732 | fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n"); |
733 | print_tabs(tabs, fd); |
734 | fprintf(fd, "}\n"); |
735 | fprintf(fd, "\n"); |
736 | |
737 | print_tabs(tabs, fd); |
738 | fprintf(fd, "*len += "); |
a3e6ce64 |
739 | fprintf(fd, "sizeof("); |
2e415130 |
740 | if(print_type(td, fd, 0, basename, "")) return 1; |
a67cd958 |
741 | fprintf(fd, ");\n"); |
8f78c30f |
742 | |
a67cd958 |
743 | break; |
744 | case STRING: |
a67cd958 |
745 | print_tabs(tabs, fd); |
3261899d |
746 | fprintf(fd, |
64a6ab10 |
747 | "lttng_write%s_string_%s(buffer, to_base, to, from, len, %s%s);\n", |
748 | custom, basename, obj_prefix, field_name); |
a3e6ce64 |
749 | break; |
750 | case SEQUENCE: |
751 | print_tabs(tabs, fd); |
3261899d |
752 | fprintf(fd, |
64a6ab10 |
753 | "lttng_write%s_sequence_%s(buffer, to_base, to, from, len, %s%s%s);", |
754 | custom, basename, get_ptr_char, obj_prefix, field_name); |
a3e6ce64 |
755 | break; |
756 | case STRUCT: |
757 | print_tabs(tabs, fd); |
3261899d |
758 | fprintf(fd, |
64a6ab10 |
759 | "lttng_write%s_struct_%s(buffer, to_base, to, from, len, %s%s%s);", |
760 | custom, basename, get_ptr_char, obj_prefix, field_name); |
a3e6ce64 |
761 | break; |
762 | case UNION: |
763 | print_tabs(tabs, fd); |
3261899d |
764 | fprintf(fd, |
64a6ab10 |
765 | "lttng_write%s_union_%s(buffer, to_base, to, from, len, %s%s%s);", |
766 | custom, basename, get_ptr_char, obj_prefix, field_name); |
a67cd958 |
767 | break; |
768 | case ARRAY: |
a67cd958 |
769 | print_tabs(tabs, fd); |
3261899d |
770 | fprintf(fd, |
64a6ab10 |
771 | "lttng_write%s_array_%s(buffer, to_base, to, from, len, %s%s);", |
772 | custom, basename, obj_prefix, field_name); |
a3e6ce64 |
773 | break; |
2e415130 |
774 | case NONE: |
775 | printf("Error : type NONE unexpected\n"); |
776 | return 1; |
777 | break; |
a3e6ce64 |
778 | } |
a67cd958 |
779 | |
a3e6ce64 |
780 | return 0; |
781 | } |
a67cd958 |
782 | |
63c831c5 |
783 | /* print need local vars ?. |
784 | * |
785 | * Copied from print_type_write |
786 | * |
787 | * Does the type_write call needs local size and from variables ? |
788 | * return value : 1 yes, 0 no. |
789 | */ |
790 | |
791 | int has_type_local(type_descriptor_t * td) |
792 | { |
793 | switch(td->type) { |
794 | case INT_FIXED: |
795 | case UINT_FIXED: |
796 | case CHAR: |
797 | case UCHAR: |
798 | case SHORT: |
799 | case USHORT: |
800 | case INT: |
801 | case UINT: |
802 | case FLOAT: |
803 | case POINTER: |
804 | case LONG: |
805 | case ULONG: |
806 | case SIZE_T: |
807 | case SSIZE_T: |
808 | case OFF_T: |
809 | case ENUM: |
810 | return 1; |
811 | break; |
812 | case STRING: |
813 | case SEQUENCE: |
814 | case STRUCT: |
815 | case UNION: |
816 | case ARRAY: |
817 | return 0; |
818 | break; |
819 | case NONE: |
820 | printf("Error : type NONE unexpected\n"); |
821 | return 1; |
822 | break; |
823 | } |
824 | |
825 | return 0; |
826 | } |
827 | |
a67cd958 |
828 | |
a67cd958 |
829 | |
a3e6ce64 |
830 | /* print type alignment function. |
831 | * |
832 | * Copied from construct_types_and_fields in LTTV facility.c |
833 | * |
834 | * basename is the name which identifies the type (along with a prefix |
835 | * (possibly)). */ |
836 | |
837 | int print_type_alignment_fct(type_descriptor_t * td, FILE *fd, |
838 | unsigned int tabs, |
839 | char *nest_name, char *field_name) |
840 | { |
841 | char basename[PATH_MAX]; |
842 | unsigned int basename_len = 0; |
843 | |
64a6ab10 |
844 | if(td->custom_write) return 0; /* Does print custom type */ |
845 | |
a3e6ce64 |
846 | strncpy(basename, nest_name, PATH_MAX); |
847 | basename_len = strlen(basename); |
848 | |
849 | /* For a named type, we use the type_name directly */ |
850 | if(td->type_name != NULL) { |
851 | strncpy(basename, td->type_name, PATH_MAX); |
852 | basename_len = strlen(basename); |
853 | } else { |
854 | /* For a unnamed type, there must be a field name, except for |
855 | * the array. */ |
856 | if((basename_len != 0) |
857 | && (basename[basename_len-1] != '_' |
858 | && (field_name[0] != '\0'))) { |
859 | strncat(basename, "_", PATH_MAX - basename_len); |
860 | basename_len = strlen(basename); |
861 | } |
862 | strncat(basename, field_name, PATH_MAX - basename_len); |
863 | } |
864 | |
865 | switch(td->type) { |
866 | case SEQUENCE: |
ba899d3d |
867 | if(((field_t*)td->fields.array[1])->type->type_name == NULL) { |
868 | /* Not a named nested type : we must print its align fct */ |
869 | if(print_type_alignment_fct(((field_t*)td->fields.array[1])->type, fd, |
870 | 0, basename, "")) return 1; |
871 | } |
a3e6ce64 |
872 | /* Function header */ |
873 | fprintf(fd, "static inline size_t lttng_get_alignment_sequence_%s(\n", |
874 | basename); |
875 | print_tabs(2, fd); |
2e415130 |
876 | if(print_type(td, fd, 0, basename, "")) return 1; |
a3e6ce64 |
877 | fprintf(fd, " *obj)\n"); |
878 | fprintf(fd, "{\n"); |
879 | print_tabs(1, fd); |
880 | fprintf(fd, "size_t align=0, localign;"); |
881 | fprintf(fd, "\n"); |
882 | print_tabs(1, fd); |
883 | fprintf(fd, "localign = "); |
884 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
3261899d |
885 | fd, 0, basename, "len", "obj->")) return 1; |
a3e6ce64 |
886 | fprintf(fd, ";\n"); |
887 | print_tabs(1, fd); |
888 | fprintf(fd, "align = max(align, localign);\n"); |
889 | fprintf(fd, "\n"); |
890 | print_tabs(1, fd); |
891 | fprintf(fd, "localign = "); |
892 | if(print_type_alignment(((field_t*)td->fields.array[1])->type, |
3261899d |
893 | fd, 0, basename, "array[0]", "obj->")) return 1; |
a3e6ce64 |
894 | fprintf(fd, ";\n"); |
895 | print_tabs(1, fd); |
896 | fprintf(fd, "align = max(align, localign);\n"); |
897 | fprintf(fd, "\n"); |
898 | print_tabs(1, fd); |
899 | fprintf(fd, "return align;\n"); |
900 | break; |
901 | case STRUCT: |
ba899d3d |
902 | for(unsigned int i=0;i<td->fields.position;i++){ |
903 | field_t *field = (field_t*)(td->fields.array[i]); |
904 | type_descriptor_t *type = field->type; |
905 | if(type->type_name == NULL) { |
906 | /* Not a named nested type : we must print its align fct */ |
907 | if(print_type_alignment_fct(type, fd, |
908 | 0, basename, field->name)) return 1; |
909 | } |
910 | } |
a3e6ce64 |
911 | /* Function header */ |
912 | fprintf(fd, "static inline size_t lttng_get_alignment_struct_%s(\n", |
913 | basename); |
914 | print_tabs(2, fd); |
2e415130 |
915 | if(print_type(td, fd, 0, basename, "")) return 1; |
a3e6ce64 |
916 | fprintf(fd, " *obj)\n"); |
917 | fprintf(fd, "{\n"); |
918 | print_tabs(1, fd); |
919 | fprintf(fd, "size_t align=0, localign;"); |
920 | fprintf(fd, "\n"); |
921 | for(unsigned int i=0;i<td->fields.position;i++){ |
922 | field_t *field = (field_t*)(td->fields.array[i]); |
923 | type_descriptor_t *type = field->type; |
924 | print_tabs(1, fd); |
925 | fprintf(fd, "localign = "); |
3261899d |
926 | if(print_type_alignment(type, fd, 0, basename, field->name, "obj->")) |
927 | return 1; |
a3e6ce64 |
928 | fprintf(fd, ";\n"); |
929 | print_tabs(1, fd); |
930 | fprintf(fd, "align = max(align, localign);\n"); |
931 | fprintf(fd, "\n"); |
932 | } |
933 | print_tabs(1, fd); |
934 | fprintf(fd, "return align;\n"); |
935 | |
a67cd958 |
936 | break; |
a3e6ce64 |
937 | case UNION: |
ba899d3d |
938 | for(unsigned int i=0;i<td->fields.position;i++){ |
939 | field_t *field = (field_t*)(td->fields.array[i]); |
940 | type_descriptor_t *type = field->type; |
941 | if(type->type_name == NULL) { |
942 | /* Not a named nested type : we must print its align fct */ |
943 | if(print_type_alignment_fct(type, fd, |
944 | 0, basename, field->name)) return 1; |
945 | } |
946 | } |
a3e6ce64 |
947 | /* Function header */ |
948 | fprintf(fd, "static inline size_t lttng_get_alignment_union_%s(\n", |
949 | basename); |
950 | print_tabs(2, fd); |
2e415130 |
951 | if(print_type(td, fd, 0, basename, "")) return 1; |
a3e6ce64 |
952 | fprintf(fd, " *obj)\n"); |
953 | fprintf(fd, "{\n"); |
954 | print_tabs(1, fd); |
955 | fprintf(fd, "size_t align=0, localign;"); |
956 | fprintf(fd, "\n"); |
957 | for(unsigned int i=0;i<td->fields.position;i++){ |
958 | field_t *field = (field_t*)(td->fields.array[i]); |
959 | type_descriptor_t *type = field->type; |
960 | print_tabs(1, fd); |
961 | fprintf(fd, "localign = "); |
3261899d |
962 | if(print_type_alignment(type, fd, 0, basename, field->name, "obj->")) |
963 | return 1; |
a3e6ce64 |
964 | fprintf(fd, ";\n"); |
965 | print_tabs(1, fd); |
966 | fprintf(fd, "align = max(align, localign);\n"); |
967 | fprintf(fd, "\n"); |
968 | } |
969 | print_tabs(1, fd); |
970 | fprintf(fd, "return align;\n"); |
971 | |
972 | break; |
973 | case ARRAY: |
ba899d3d |
974 | if(((field_t*)td->fields.array[0])->type->type_name == NULL) { |
975 | /* Not a named nested type : we must print its align fct */ |
976 | if(print_type_alignment_fct(((field_t*)td->fields.array[0])->type, fd, |
977 | 0, basename, "")) return 1; |
978 | } |
a3e6ce64 |
979 | /* Function header */ |
980 | fprintf(fd, "static inline size_t lttng_get_alignment_array_%s(\n", |
981 | basename); |
982 | print_tabs(2, fd); |
2e415130 |
983 | if(print_type(td, fd, 0, basename, "")) return 1; |
a3e6ce64 |
984 | fprintf(fd, " obj)\n"); |
985 | fprintf(fd, "{\n"); |
986 | print_tabs(1, fd); |
987 | fprintf(fd, "return \n"); |
988 | if(print_type_alignment(((field_t*)td->fields.array[0])->type, |
3261899d |
989 | fd, 0, basename, "", "obj[0]")) |
990 | return 1; |
a3e6ce64 |
991 | fprintf(fd, ";\n"); |
992 | break; |
993 | default: |
71e09db9 |
994 | dprintf("print_type_alignment_fct : type has no alignment function.\n"); |
34c1d1b5 |
995 | return 0; |
a3e6ce64 |
996 | break; |
997 | } |
998 | |
999 | |
1000 | /* Function footer */ |
1001 | fprintf(fd, "}\n"); |
1002 | fprintf(fd, "\n"); |
1003 | |
2e415130 |
1004 | return 0; |
a3e6ce64 |
1005 | } |
1006 | |
1007 | /* print type write function. |
1008 | * |
1009 | * Copied from construct_types_and_fields in LTTV facility.c |
1010 | * |
1011 | * basename is the name which identifies the type (along with a prefix |
1012 | * (possibly)). */ |
1013 | |
1014 | int print_type_write_fct(type_descriptor_t * td, FILE *fd, unsigned int tabs, |
1015 | char *nest_name, char *field_name) |
1016 | { |
1017 | char basename[PATH_MAX]; |
1018 | unsigned int basename_len = 0; |
1019 | |
64a6ab10 |
1020 | if(td->custom_write) return 0; /* Does print custom type */ |
1021 | |
a3e6ce64 |
1022 | strncpy(basename, nest_name, PATH_MAX); |
1023 | basename_len = strlen(basename); |
1024 | |
1025 | /* For a named type, we use the type_name directly */ |
1026 | if(td->type_name != NULL) { |
1027 | strncpy(basename, td->type_name, PATH_MAX); |
1028 | basename_len = strlen(basename); |
1029 | } else { |
1030 | /* For a unnamed type, there must be a field name, except for |
1031 | * the array. */ |
1032 | if((basename_len != 0) |
1033 | && (basename[basename_len-1] != '_' |
1034 | && (field_name[0] != '\0'))) { |
1035 | strncat(basename, "_", PATH_MAX - basename_len); |
1036 | basename_len = strlen(basename); |
1037 | } |
1038 | strncat(basename, field_name, PATH_MAX - basename_len); |
1039 | } |
1040 | |
34c1d1b5 |
1041 | switch(td->type) { |
1042 | case SEQUENCE: |
ba899d3d |
1043 | if(((field_t*)td->fields.array[1])->type->type_name == NULL) { |
1044 | /* Not a named nested type : we must print its write fct */ |
1045 | if(print_type_write_fct(((field_t*)td->fields.array[1])->type, fd, |
1046 | 0, basename, "")) return 1; |
1047 | } |
1048 | break; |
34c1d1b5 |
1049 | case STRUCT: |
ba899d3d |
1050 | for(unsigned int i=0;i<td->fields.position;i++){ |
1051 | field_t *field = (field_t*)(td->fields.array[i]); |
1052 | type_descriptor_t *type = field->type; |
1053 | if(type->type_name == NULL) { |
1054 | /* Not a named nested type : we must print its write fct */ |
1055 | if(print_type_write_fct(type, fd, |
1056 | 0, basename, field->name)) return 1; |
1057 | } |
1058 | } |
1059 | break; |
34c1d1b5 |
1060 | case UNION: |
ba899d3d |
1061 | for(unsigned int i=0;i<td->fields.position;i++){ |
1062 | field_t *field = (field_t*)(td->fields.array[i]); |
1063 | type_descriptor_t *type = field->type; |
1064 | if(type->type_name == NULL) { |
1065 | /* Not a named nested type : we must print its write fct */ |
1066 | if(print_type_write_fct(type, fd, |
1067 | 0, basename, field->name)) return 1; |
1068 | } |
1069 | } |
1070 | break; |
34c1d1b5 |
1071 | case ARRAY: |
ba899d3d |
1072 | if(((field_t*)td->fields.array[0])->type->type_name == NULL) { |
1073 | /* Not a named nested type : we must print its write fct */ |
1074 | if(print_type_write_fct(((field_t*)td->fields.array[0])->type, fd, |
1075 | 0, basename, "")) return 1; |
1076 | } |
1077 | break; |
a2ff13ed |
1078 | case STRING: |
34c1d1b5 |
1079 | break; |
1080 | default: |
71e09db9 |
1081 | dprintf("print_type_write_fct : type has no write function.\n"); |
34c1d1b5 |
1082 | return 0; |
1083 | break; |
1084 | } |
1085 | |
a3e6ce64 |
1086 | /* Print header */ |
1087 | switch(td->type) { |
a67cd958 |
1088 | case SEQUENCE: |
34c1d1b5 |
1089 | fprintf(fd, "static inline void lttng_write_sequence_%s(\n", |
a3e6ce64 |
1090 | basename); |
a67cd958 |
1091 | break; |
a3e6ce64 |
1092 | case STRUCT: |
6e27ba88 |
1093 | fprintf(fd, "static inline void lttng_write_struct_%s(\n", basename); |
a67cd958 |
1094 | break; |
a3e6ce64 |
1095 | case UNION: |
6e27ba88 |
1096 | fprintf(fd, "static inline void lttng_write_union_%s(\n", basename); |
a3e6ce64 |
1097 | break; |
1098 | case ARRAY: |
6e27ba88 |
1099 | fprintf(fd, "static inline void lttng_write_array_%s(\n", basename); |
a3e6ce64 |
1100 | break; |
a2ff13ed |
1101 | case STRING: |
1102 | fprintf(fd, "static inline void lttng_write_string_%s(\n", basename); |
1103 | break; |
a3e6ce64 |
1104 | default: |
1105 | printf("print_type_write_fct : type has no write function.\n"); |
a67cd958 |
1106 | break; |
a67cd958 |
1107 | } |
1108 | |
a3e6ce64 |
1109 | print_tabs(2, fd); |
59a957df |
1110 | fprintf(fd, "char *buffer,\n"); |
a3e6ce64 |
1111 | print_tabs(2, fd); |
1112 | fprintf(fd, "size_t *to_base,\n"); |
1113 | print_tabs(2, fd); |
1114 | fprintf(fd, "size_t *to,\n"); |
1115 | print_tabs(2, fd); |
59a957df |
1116 | fprintf(fd, "const char **from,\n"); |
a3e6ce64 |
1117 | print_tabs(2, fd); |
1118 | fprintf(fd, "size_t *len,\n"); |
1119 | print_tabs(2, fd); |
2e415130 |
1120 | if(print_type(td, fd, 0, basename, "")) return 1; |
a3e6ce64 |
1121 | |
1122 | switch(td->type) { |
1123 | case SEQUENCE: |
1124 | fprintf(fd, " *obj)\n"); |
1125 | break; |
1126 | case STRUCT: |
1127 | fprintf(fd, " *obj)\n"); |
1128 | break; |
1129 | case UNION: |
1130 | fprintf(fd, " *obj)\n"); |
1131 | break; |
1132 | case ARRAY: |
1133 | fprintf(fd, " obj)\n"); |
1134 | break; |
a2ff13ed |
1135 | case STRING: |
1136 | fprintf(fd, " obj)\n"); |
1137 | break; |
a3e6ce64 |
1138 | default: |
1139 | printf("print_type_write_fct : type has no write function.\n"); |
1140 | break; |
1141 | } |
1142 | |
1143 | fprintf(fd, "{\n"); |
3ace7bc4 |
1144 | |
f5f2fde4 |
1145 | switch(td->type) { |
1146 | case STRING: |
1147 | print_tabs(1, fd); |
1148 | fprintf(fd, "size_t size;\n"); |
1149 | break; |
1150 | default: |
1151 | break; |
1152 | } |
1153 | |
a3e6ce64 |
1154 | print_tabs(1, fd); |
3ace7bc4 |
1155 | fprintf(fd, "size_t align;\n"); |
a3e6ce64 |
1156 | fprintf(fd, "\n"); |
1157 | |
1158 | switch(td->type) { |
1159 | case SEQUENCE: |
1160 | case STRING: |
1161 | print_tabs(1, fd); |
1162 | fprintf(fd, "/* Flush pending memcpy */\n"); |
1163 | print_tabs(1, fd); |
1164 | fprintf(fd, "if(*len != 0) {\n"); |
1165 | print_tabs(2, fd); |
1166 | fprintf(fd, "if(buffer != NULL)\n"); |
1167 | print_tabs(3, fd); |
1168 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
1169 | print_tabs(1, fd); |
1170 | fprintf(fd, "}\n"); |
1171 | print_tabs(1, fd); |
1172 | fprintf(fd, "*to += *len;\n"); |
1173 | print_tabs(1, fd); |
1174 | fprintf(fd, "*len = 0;\n"); |
1175 | fprintf(fd, "\n"); |
1176 | break; |
1177 | case STRUCT: |
1178 | case UNION: |
1179 | case ARRAY: |
1180 | break; |
1181 | default: |
1182 | printf("print_type_write_fct : type has no write function.\n"); |
1183 | break; |
1184 | } |
1185 | |
1186 | print_tabs(1, fd); |
34c1d1b5 |
1187 | fprintf(fd, "align = "); |
0231ef76 |
1188 | if(print_type_alignment(td, fd, 0, basename, "", "obj")) return 1; |
a3e6ce64 |
1189 | fprintf(fd, ";\n"); |
1190 | fprintf(fd, "\n"); |
1191 | print_tabs(1, fd); |
1192 | fprintf(fd, "if(*len == 0) {\n"); |
1193 | print_tabs(2, fd); |
1194 | fprintf(fd, "*to += ltt_align(*to, align); /* align output */\n"); |
1195 | print_tabs(1, fd); |
1196 | fprintf(fd, "} else {\n"); |
1197 | print_tabs(2, fd); |
1198 | fprintf(fd, "*len += ltt_align(*to+*len, align); /* alignment, ok to do a memcpy of it */\n"); |
1199 | print_tabs(1, fd); |
1200 | fprintf(fd, "}\n"); |
1201 | fprintf(fd, "\n"); |
1202 | |
1203 | /* First, check if the type has a fixed size. If it is the case, then the size |
1204 | * to write is know by the compiler : simply use a sizeof() */ |
1205 | if(has_type_fixed_size(td)) { |
1206 | print_tabs(1, fd); |
1207 | fprintf(fd, "/* Contains only fixed size fields : use compiler sizeof() */\n"); |
1208 | fprintf(fd, "\n"); |
1209 | print_tabs(1, fd); |
f5f2fde4 |
1210 | fprintf(fd, "*len += sizeof("); |
2e415130 |
1211 | if(print_type(td, fd, 0, basename, field_name)) return 1; |
a3e6ce64 |
1212 | fprintf(fd, ");\n"); |
1213 | } else { |
1214 | /* The type contains nested variable size subtypes : |
1215 | * we must write field by field. */ |
1216 | print_tabs(1, fd); |
1217 | fprintf(fd, "/* Contains variable sized fields : must explode the structure */\n"); |
1218 | fprintf(fd, "\n"); |
1219 | |
1220 | switch(td->type) { |
1221 | case SEQUENCE: |
1222 | print_tabs(1, fd); |
1223 | fprintf(fd, "/* Copy members */\n"); |
3261899d |
1224 | // print_tabs(1, fd); |
1225 | // fprintf(fd, "size = sizeof(\n"); |
a3e6ce64 |
1226 | if(print_type_write(((field_t*)td->fields.array[0])->type, |
458989d8 |
1227 | fd, 1, basename, "len", "obj->", 1)) return 1; |
3261899d |
1228 | fprintf(fd, "\n"); |
1229 | // fprintf(fd, ");\n"); |
1230 | // print_tabs(1, fd); |
1231 | // fprintf(fd, "*to += ltt_align(*to, size);\n"); |
a3e6ce64 |
1232 | print_tabs(1, fd); |
1233 | fprintf(fd, "if(buffer != NULL)\n"); |
1234 | print_tabs(2, fd); |
8f78c30f |
1235 | fprintf(fd, "memcpy(buffer+*to_base+*to, &obj->len, *len);\n"); |
a3e6ce64 |
1236 | print_tabs(1, fd); |
8f78c30f |
1237 | fprintf(fd, "*to += *len;\n"); |
1238 | print_tabs(1, fd); |
1239 | fprintf(fd, "*len = 0;\n"); |
a3e6ce64 |
1240 | fprintf(fd, "\n"); |
8f78c30f |
1241 | |
a3e6ce64 |
1242 | /* Write the child : varlen child or not ? */ |
1243 | if(has_type_fixed_size(((field_t*)td->fields.array[1])->type)) { |
1244 | /* Fixed size len child : use a multiplication of its size */ |
3261899d |
1245 | // print_tabs(1, fd); |
1246 | // fprintf(fd, "size = sizeof(\n"); |
8f78c30f |
1247 | |
1248 | //print_tabs(1, fd); |
1249 | /* We know that *len does not contain alignment because of the |
1250 | * previous align output. len is always 0 here. */ |
a3e6ce64 |
1251 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
8f78c30f |
1252 | fd, 1, basename, "array[0]", "obj->", 1)) |
1253 | return 1; |
3261899d |
1254 | // fprintf(fd, ");\n"); |
8f78c30f |
1255 | fprintf(fd, "\n"); |
a3e6ce64 |
1256 | print_tabs(1, fd); |
8f78c30f |
1257 | fprintf(fd, "*len = obj->len * (*len);\n"); |
a3e6ce64 |
1258 | print_tabs(1, fd); |
1259 | fprintf(fd, "if(buffer != NULL)\n"); |
1260 | print_tabs(2, fd); |
8f78c30f |
1261 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj->array, *len);\n"); |
1262 | print_tabs(1, fd); |
1263 | fprintf(fd, "*to += *len;\n"); |
a3e6ce64 |
1264 | print_tabs(1, fd); |
8f78c30f |
1265 | fprintf(fd, "*len = 0;\n"); |
a3e6ce64 |
1266 | fprintf(fd, "\n"); |
1267 | } else { |
1268 | print_tabs(1, fd); |
1269 | fprintf(fd, "/* Variable length child : iter. */\n"); |
1270 | print_tabs(1, fd); |
1271 | fprintf(fd, "for(unsigned int i=0; i<obj->len; i++) {\n"); |
1272 | if(print_type_write(((field_t*)td->fields.array[1])->type, |
458989d8 |
1273 | fd, 2, basename, "array[i]", "obj->", 1)) return 1; |
a3e6ce64 |
1274 | print_tabs(1, fd); |
1275 | fprintf(fd, "}\n"); |
1276 | } |
1277 | fprintf(fd, "\n"); |
1278 | print_tabs(1, fd); |
1279 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
1280 | print_tabs(1, fd); |
2a5eec7d |
1281 | fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n"); |
a3e6ce64 |
1282 | print_tabs(1, fd); |
1283 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
1284 | print_tabs(1, fd); |
1285 | fprintf(fd, "*to = 0;\n"); |
1286 | fprintf(fd, "\n"); |
a2ff13ed |
1287 | print_tabs(1, fd); |
a3e6ce64 |
1288 | fprintf(fd, "/* Put source *from just after the C sequence */\n"); |
1289 | print_tabs(1, fd); |
c07c0891 |
1290 | fprintf(fd, "*from = (const char*)(obj+1);\n"); |
a3e6ce64 |
1291 | break; |
1292 | case STRING: |
a2ff13ed |
1293 | print_tabs(1, fd); |
caabcb43 |
1294 | fprintf(fd, "size = strlen(obj) + 1; /* Include final NULL char. */\n"); |
a3e6ce64 |
1295 | print_tabs(1, fd); |
1296 | fprintf(fd, "if(buffer != NULL)\n"); |
1297 | print_tabs(2, fd); |
1298 | fprintf(fd, "memcpy(buffer+*to_base+*to, obj, size);\n"); |
1299 | print_tabs(1, fd); |
1300 | fprintf(fd, "*to += size;\n"); |
1301 | fprintf(fd, "\n"); |
1302 | print_tabs(1, fd); |
1303 | fprintf(fd, "/* Realign the *to_base on arch size, set *to to 0 */\n"); |
1304 | print_tabs(1, fd); |
2a5eec7d |
1305 | fprintf(fd, "*to += ltt_align(*to, sizeof(void *));\n"); |
a3e6ce64 |
1306 | print_tabs(1, fd); |
1307 | fprintf(fd, "*to_base = *to_base+*to;\n"); |
1308 | print_tabs(1, fd); |
1309 | fprintf(fd, "*to = 0;\n"); |
1310 | fprintf(fd, "\n"); |
a3e6ce64 |
1311 | print_tabs(1, fd); |
a2ff13ed |
1312 | fprintf(fd, "/* Put source *from just after the C string */\n"); |
1313 | print_tabs(1, fd); |
1314 | fprintf(fd, "*from += size;\n"); |
a3e6ce64 |
1315 | break; |
1316 | case STRUCT: |
1317 | for(unsigned int i=0;i<td->fields.position;i++){ |
1318 | field_t *field = (field_t*)(td->fields.array[i]); |
1319 | type_descriptor_t *type = field->type; |
1320 | if(print_type_write(type, |
458989d8 |
1321 | fd, 1, basename, field->name, "obj->", 1)) return 1; |
a3e6ce64 |
1322 | fprintf(fd, "\n"); |
1323 | } |
1324 | break; |
1325 | case UNION: |
1326 | printf("ERROR : A union CANNOT contain a variable size child.\n"); |
1327 | return 1; |
1328 | break; |
1329 | case ARRAY: |
1330 | /* Write the child : varlen child or not ? */ |
1331 | if(has_type_fixed_size(((field_t*)td->fields.array[0])->type)) { |
1332 | /* Error : if an array has a variable size, then its child must also |
1333 | * have a variable size. */ |
1334 | assert(0); |
1335 | } else { |
1336 | print_tabs(1, fd); |
1337 | fprintf(fd, "/* Variable length child : iter. */\n"); |
1338 | print_tabs(1, fd); |
1339 | fprintf(fd, "for(unsigned int i=0; i<LTTNG_ARRAY_SIZE_%s; i++) {\n", basename); |
3261899d |
1340 | if(print_type_write(((field_t*)td->fields.array[0])->type, |
458989d8 |
1341 | fd, 2, basename, "", "obj->array[i]", 1)) return 1; |
a3e6ce64 |
1342 | print_tabs(1, fd); |
1343 | fprintf(fd, "}\n"); |
1344 | } |
1345 | break; |
1346 | default: |
1347 | printf("print_type_write_fct : type has no write function.\n"); |
1348 | break; |
1349 | } |
1350 | |
1351 | |
1352 | } |
1353 | |
1354 | |
1355 | /* Function footer */ |
1356 | fprintf(fd, "}\n"); |
1357 | fprintf(fd, "\n"); |
a67cd958 |
1358 | return 0; |
1359 | } |
1360 | |
1361 | |
1362 | |
47299663 |
1363 | /* Print the logging function of an event. This is the core of genevent */ |
a3e6ce64 |
1364 | int print_event_logging_function(char *basename, facility_t *fac, |
1365 | event_t *event, FILE *fd) |
47299663 |
1366 | { |
1367 | fprintf(fd, "static inline void trace_%s(\n", basename); |
3d9b9b0c |
1368 | int has_argument = 0; |
63c831c5 |
1369 | int has_type_fixed = 0; |
3d9b9b0c |
1370 | |
1371 | /* Does it support per trace tracing ? */ |
1372 | if(event->per_trace) { |
1373 | print_tabs(2, fd); |
1374 | fprintf(fd, "struct ltt_trace_struct *dest_trace"); |
1375 | has_argument = 1; |
1376 | } |
1377 | |
1378 | /* Does it support per tracefile tracing ? */ |
1379 | if(event->per_tracefile) { |
1380 | if(has_argument) { |
1381 | fprintf(fd, ","); |
1382 | fprintf(fd, "\n"); |
1383 | } |
1384 | fprintf(fd, "unsigned int tracefile_index"); |
1385 | has_argument = 1; |
1386 | } |
1387 | |
47299663 |
1388 | for(unsigned int j = 0; j < event->fields.position; j++) { |
1389 | /* For each field, print the function argument */ |
1390 | field_t *f = (field_t*)event->fields.array[j]; |
1391 | type_descriptor_t *t = f->type; |
3d9b9b0c |
1392 | if(has_argument) { |
47299663 |
1393 | fprintf(fd, ","); |
1394 | fprintf(fd, "\n"); |
1395 | } |
3d9b9b0c |
1396 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
1397 | has_argument = 1; |
47299663 |
1398 | } |
3d9b9b0c |
1399 | if(!has_argument) { |
47299663 |
1400 | print_tabs(2, fd); |
1401 | fprintf(fd, "void"); |
1402 | } |
1403 | fprintf(fd,")\n"); |
c3fff9a4 |
1404 | #if 0 |
71e09db9 |
1405 | fprintf(fd, |
c3fff9a4 |
1406 | "#if (!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n", |
1407 | fac->capname, fac->capname); |
47299663 |
1408 | fprintf(fd, "{\n"); |
1409 | fprintf(fd, "}\n"); |
1410 | fprintf(fd,"#else\n"); |
c3fff9a4 |
1411 | #endif //0 |
47299663 |
1412 | fprintf(fd, "{\n"); |
7e97b039 |
1413 | /* Print the function variables */ |
a67cd958 |
1414 | print_tabs(1, fd); |
a3e6ce64 |
1415 | fprintf(fd, "unsigned int index;\n"); |
1416 | print_tabs(1, fd); |
1417 | fprintf(fd, "struct ltt_channel_struct *channel;\n"); |
1418 | print_tabs(1, fd); |
1419 | fprintf(fd, "struct ltt_trace_struct *trace;\n"); |
1420 | print_tabs(1, fd); |
db6c25a6 |
1421 | fprintf(fd, "void *transport_data;\n"); |
a3e6ce64 |
1422 | print_tabs(1, fd); |
59a957df |
1423 | fprintf(fd, "char *buffer = NULL;\n"); |
a3e6ce64 |
1424 | print_tabs(1, fd); |
458989d8 |
1425 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
1426 | print_tabs(1, fd); |
1427 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
a3e6ce64 |
1428 | print_tabs(1, fd); |
458989d8 |
1429 | fprintf(fd, "size_t real_to = 0;\n"); |
a3e6ce64 |
1430 | print_tabs(1, fd); |
458989d8 |
1431 | fprintf(fd, "size_t *to = &real_to;\n"); |
a3e6ce64 |
1432 | print_tabs(1, fd); |
458989d8 |
1433 | fprintf(fd, "size_t real_len = 0;\n"); |
1434 | print_tabs(1, fd); |
1435 | fprintf(fd, "size_t *len = &real_len;\n"); |
30d72138 |
1436 | print_tabs(1, fd); |
6e27ba88 |
1437 | fprintf(fd, "size_t reserve_size;\n"); |
1438 | print_tabs(1, fd); |
a3e6ce64 |
1439 | fprintf(fd, "size_t slot_size;\n"); |
30d72138 |
1440 | print_tabs(1, fd); |
63c831c5 |
1441 | |
ac963fe3 |
1442 | if(event->fields.position > 0) { |
63c831c5 |
1443 | for(unsigned int i=0;i<event->fields.position;i++){ |
1444 | /* Search for at least one child with fixed size. It means |
1445 | * we need local variables.*/ |
1446 | field_t *field = (field_t*)(event->fields.array[i]); |
1447 | type_descriptor_t *type = field->type; |
1448 | has_type_fixed = has_type_local(type); |
1449 | if(has_type_fixed) break; |
1450 | } |
1451 | |
1452 | if(has_type_fixed) { |
8f78c30f |
1453 | fprintf(fd, "size_t align;\n"); |
63c831c5 |
1454 | print_tabs(1, fd); |
1455 | } |
1456 | |
59a957df |
1457 | fprintf(fd, "const char *real_from;\n"); |
ac963fe3 |
1458 | print_tabs(1, fd); |
59a957df |
1459 | fprintf(fd, "const char **from = &real_from;\n"); |
ac963fe3 |
1460 | print_tabs(1, fd); |
1461 | } |
8e290e8b |
1462 | fprintf(fd, "u64 tsc;\n"); |
30d72138 |
1463 | print_tabs(1, fd); |
458989d8 |
1464 | fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n"); |
a3e6ce64 |
1465 | fprintf(fd, "\n"); |
7e97b039 |
1466 | |
a3e6ce64 |
1467 | print_tabs(1, fd); |
1468 | fprintf(fd, "if(ltt_traces.num_active_traces == 0) return;\n"); |
1469 | fprintf(fd, "\n"); |
1470 | |
a67cd958 |
1471 | /* Calculate event variable len + event data alignment offset. |
7e97b039 |
1472 | * Assume that the padding for alignment starts at a void* |
a67cd958 |
1473 | * address. |
1474 | * This excludes the header size and alignment. */ |
1475 | |
a3e6ce64 |
1476 | print_tabs(1, fd); |
1477 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
1478 | print_tabs(1, fd); |
458989d8 |
1479 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
a3e6ce64 |
1480 | print_tabs(1, fd); |
30d72138 |
1481 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
a3e6ce64 |
1482 | print_tabs(1, fd); |
30d72138 |
1483 | fprintf(fd, " * sizeof(void *) address. */\n"); |
a3e6ce64 |
1484 | fprintf(fd, "\n"); |
a67cd958 |
1485 | |
a3e6ce64 |
1486 | for(unsigned int i=0;i<event->fields.position;i++){ |
2e415130 |
1487 | field_t *field = (field_t*)(event->fields.array[i]); |
a3e6ce64 |
1488 | type_descriptor_t *type = field->type; |
458989d8 |
1489 | /* Set from */ |
1490 | print_tabs(1, fd); |
1491 | switch(type->type) { |
1492 | case SEQUENCE: |
1493 | case UNION: |
1494 | case ARRAY: |
1495 | case STRUCT: |
1496 | case STRING: |
922b1092 |
1497 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
458989d8 |
1498 | break; |
1499 | default: |
922b1092 |
1500 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
458989d8 |
1501 | break; |
1502 | } |
1503 | |
a3e6ce64 |
1504 | if(print_type_write(type, |
3ace7bc4 |
1505 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
a3e6ce64 |
1506 | fprintf(fd, "\n"); |
a67cd958 |
1507 | } |
6e27ba88 |
1508 | print_tabs(1, fd); |
458989d8 |
1509 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
47299663 |
1510 | |
7e97b039 |
1511 | /* Take locks : make sure the trace does not vanish while we write on |
1512 | * it. A simple preemption disabling is enough (using rcu traces). */ |
a3e6ce64 |
1513 | print_tabs(1, fd); |
1514 | fprintf(fd, "preempt_disable();\n"); |
1515 | print_tabs(1, fd); |
358f8354 |
1516 | fprintf(fd, "ltt_nesting[smp_processor_id()]++;\n"); |
a3e6ce64 |
1517 | |
1518 | /* Get facility index */ |
1519 | |
1520 | if(event->per_tracefile) { |
1521 | print_tabs(1, fd); |
1522 | fprintf(fd, "index = tracefile_index;\n"); |
1523 | } else { |
1524 | print_tabs(1, fd); |
1525 | fprintf(fd, |
aaa18042 |
1526 | "index = ltt_get_index_from_facility_%s(" \ |
458989d8 |
1527 | "\t\t\t\t\t\tevent_%s_%s);\n", |
aaa18042 |
1528 | fac->name, fac->name, event->name); |
a3e6ce64 |
1529 | } |
1530 | fprintf(fd,"\n"); |
1531 | |
7e97b039 |
1532 | |
1533 | /* For each trace */ |
a3e6ce64 |
1534 | print_tabs(1, fd); |
1535 | fprintf(fd, "list_for_each_entry_rcu(trace, <t_traces.head, list) {\n"); |
1536 | print_tabs(2, fd); |
1537 | fprintf(fd, "if(!trace->active) continue;\n\n"); |
1538 | |
1539 | if(event->per_trace) { |
1540 | print_tabs(2, fd); |
1541 | fprintf(fd, "if(dest_trace != trace) continue;\n\n"); |
1542 | } |
1543 | |
1544 | print_tabs(2, fd); |
2e415130 |
1545 | fprintf(fd, "channel = ltt_get_channel_from_index(trace, index);\n"); |
a3e6ce64 |
1546 | fprintf(fd, "\n"); |
1547 | |
7e97b039 |
1548 | |
1549 | /* Relay reserve */ |
a3e6ce64 |
1550 | /* If error, increment event lost counter (done by ltt_reserve_slot) and |
1551 | * return */ |
1552 | print_tabs(2, fd); |
1553 | fprintf(fd, "slot_size = 0;\n"); |
1554 | print_tabs(2, fd); |
db6c25a6 |
1555 | fprintf(fd, "buffer = ltt_reserve_slot(trace, channel, &transport_data,\n"); |
30d72138 |
1556 | print_tabs(3, fd); |
6e27ba88 |
1557 | fprintf(fd, "reserve_size, &slot_size, &tsc,\n"); |
30d72138 |
1558 | print_tabs(3, fd); |
458989d8 |
1559 | fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n"); |
a3e6ce64 |
1560 | /* If error, return */ |
1561 | print_tabs(2, fd); |
43f7f14f |
1562 | fprintf(fd, "if(!buffer) continue; /* buffer full */\n\n"); |
8f78c30f |
1563 | //print_tabs(2, fd); |
1564 | // for DEBUG only |
1565 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
1566 | print_tabs(2, fd); |
1567 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
1568 | fprintf(fd, "\n"); |
1569 | |
458989d8 |
1570 | /* Write event header */ |
1571 | print_tabs(2, fd); |
1572 | fprintf(fd, "ltt_write_event_header(trace, channel, buffer,\n"); |
1573 | print_tabs(3, fd); |
1574 | fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum, |
1575 | fac->name, event->name); |
1576 | print_tabs(3, fd); |
1577 | fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n"); |
1578 | print_tabs(2, fd); |
1579 | fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n"); |
2a5eec7d |
1580 | fprintf(fd, "\n"); |
7e97b039 |
1581 | |
8f78c30f |
1582 | /* write data. */ |
a3e6ce64 |
1583 | |
1584 | for(unsigned int i=0;i<event->fields.position;i++){ |
2e415130 |
1585 | field_t *field = (field_t*)(event->fields.array[i]); |
a3e6ce64 |
1586 | type_descriptor_t *type = field->type; |
8f78c30f |
1587 | |
a3e6ce64 |
1588 | /* Set from */ |
30d72138 |
1589 | print_tabs(2, fd); |
1590 | switch(type->type) { |
1591 | case SEQUENCE: |
1592 | case UNION: |
1593 | case ARRAY: |
1594 | case STRUCT: |
1595 | case STRING: |
922b1092 |
1596 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
30d72138 |
1597 | break; |
1598 | default: |
922b1092 |
1599 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
30d72138 |
1600 | break; |
1601 | } |
1602 | |
a3e6ce64 |
1603 | |
1604 | if(print_type_write(type, |
3ace7bc4 |
1605 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
a3e6ce64 |
1606 | fprintf(fd, "\n"); |
7e97b039 |
1607 | |
a3e6ce64 |
1608 | /* Don't forget to flush pending memcpy */ |
1609 | print_tabs(2, fd); |
1610 | fprintf(fd, "/* Flush pending memcpy */\n"); |
1611 | print_tabs(2, fd); |
8f78c30f |
1612 | fprintf(fd, "if(*len != 0) {\n"); |
a3e6ce64 |
1613 | print_tabs(3, fd); |
458989d8 |
1614 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
a3e6ce64 |
1615 | print_tabs(3, fd); |
458989d8 |
1616 | fprintf(fd, "*to += *len;\n"); |
a3e6ce64 |
1617 | //print_tabs(3, fd); |
1618 | //fprintf(fd, "from += len;\n"); |
1619 | print_tabs(3, fd); |
458989d8 |
1620 | fprintf(fd, "*len = 0;\n"); |
a3e6ce64 |
1621 | print_tabs(2, fd); |
1622 | fprintf(fd, "}\n"); |
1623 | fprintf(fd, "\n"); |
1624 | } |
1625 | |
1626 | |
7e97b039 |
1627 | /* commit */ |
8f78c30f |
1628 | // for DEBUG only. |
1629 | //fprintf(fd, "commit:\n"); /* DEBUG! */ |
a3e6ce64 |
1630 | print_tabs(2, fd); |
db6c25a6 |
1631 | fprintf(fd, "ltt_commit_slot(channel, &transport_data, buffer, slot_size);\n\n"); |
7e97b039 |
1632 | |
a3e6ce64 |
1633 | print_tabs(1, fd); |
1634 | fprintf(fd, "}\n\n"); |
1635 | |
7e97b039 |
1636 | /* Release locks */ |
a3e6ce64 |
1637 | print_tabs(1, fd); |
358f8354 |
1638 | fprintf(fd, "ltt_nesting[smp_processor_id()]--;\n"); |
a3e6ce64 |
1639 | print_tabs(1, fd); |
1640 | fprintf(fd, "preempt_enable_no_resched();\n"); |
2d2d14a7 |
1641 | |
47299663 |
1642 | fprintf(fd, "}\n"); |
c3fff9a4 |
1643 | #if 0 |
1644 | fprintf(fd, "#endif //(!defined(CONFIG_LTT) || (!defined(CONFIG_LTT_FACILITY_%s) && !defined(CONFIG_LTT_FACILITY_%s_MODULE)))\n\n", |
1645 | fac->capname, fac->capname); |
1646 | #endif //0 |
47299663 |
1647 | return 0; |
1648 | } |
2d2d14a7 |
1649 | |
922b1092 |
1650 | int print_event_logging_function_header_user_generic(char *basename, facility_t *fac, |
1651 | event_t *event, FILE *fd, enum user_fct_types fct_type) |
bd7b8ca6 |
1652 | { |
4a6829e2 |
1653 | char *attrib; |
8a9103df |
1654 | |
922b1092 |
1655 | if(event->no_instrument_function && fct_type == USER_FCT_PROTO) { |
4a6829e2 |
1656 | attrib = "__attribute__((no_instrument_function)) "; |
1657 | } else { |
1658 | attrib = ""; |
1659 | } |
1e08067e |
1660 | if(event->param_buffer) { |
4a6829e2 |
1661 | fprintf(fd, "static inline %sint trace_%s_param_buffer(\n", attrib, basename); |
1e08067e |
1662 | } else { |
4a6829e2 |
1663 | fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename); |
1e08067e |
1664 | } |
bd7b8ca6 |
1665 | int has_argument = 0; |
bd7b8ca6 |
1666 | |
1e08067e |
1667 | if(event->param_buffer) { |
bd7b8ca6 |
1668 | if(has_argument) { |
1e08067e |
1669 | fprintf(fd, ","); |
1670 | fprintf(fd, "\n"); |
bd7b8ca6 |
1671 | } |
1e08067e |
1672 | print_tabs(2, fd); |
59a957df |
1673 | fprintf(fd, "char *buffer"); |
bd7b8ca6 |
1674 | has_argument = 1; |
1e08067e |
1675 | fprintf(fd, ","); |
1676 | fprintf(fd, "\n"); |
1677 | print_tabs(2, fd); |
1678 | fprintf(fd, "size_t reserve_size"); |
1679 | } else { |
1680 | for(unsigned int j = 0; j < event->fields.position; j++) { |
1681 | /* For each field, print the function argument */ |
1682 | field_t *f = (field_t*)event->fields.array[j]; |
1683 | type_descriptor_t *t = f->type; |
1684 | if(has_argument) { |
1685 | fprintf(fd, ","); |
1686 | fprintf(fd, "\n"); |
1687 | } |
1688 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
1689 | has_argument = 1; |
1690 | } |
bd7b8ca6 |
1691 | } |
1692 | if(!has_argument) { |
1693 | print_tabs(2, fd); |
1694 | fprintf(fd, "void"); |
1695 | } |
922b1092 |
1696 | fprintf(fd,")"); |
1697 | return 0; |
1698 | } |
1699 | |
1700 | |
1701 | /* print_event_logging_function_user_generic |
1702 | * Print the logging function of an event for userspace tracing. This is the |
1703 | * core of genevent */ |
1704 | int print_event_logging_function_user_generic(char *basename, facility_t *fac, |
1705 | event_t *event, FILE *fd) |
1706 | { |
1707 | int has_type_fixed = 0; |
1708 | |
1709 | if(print_event_logging_function_header_user_generic(basename, fac, event, fd, USER_FCT_PROTO)) return 1; |
1710 | fprintf(fd,";\n"); |
1711 | fprintf(fd,"\n"); |
1712 | fprintf(fd, "#ifndef LTT_TRACE_FAST\n"); |
1713 | if(print_event_logging_function_header_user_generic(basename, fac, event, fd, USER_FCT_DECLARATION)) return 1; |
1714 | fprintf(fd,"\n"); |
bd7b8ca6 |
1715 | fprintf(fd, |
1716 | "#ifndef LTT_TRACE\n"); |
1717 | fprintf(fd, "{\n"); |
1718 | fprintf(fd, "}\n"); |
1719 | fprintf(fd,"#else\n"); |
1720 | fprintf(fd, "{\n"); |
1721 | /* Print the function variables */ |
1722 | print_tabs(1, fd); |
1e08067e |
1723 | fprintf(fd, "int ret = 0;\n"); |
1724 | if(event->param_buffer) { |
0bdf8e98 |
1725 | //FIX print_tabs(1, fd); |
1726 | //fprintf(fd, "reserve_size = ltt_align(reserve_size, sizeof(void *));\n"); |
1e08067e |
1727 | print_tabs(1, fd); |
1728 | fprintf(fd, "{\n"); |
1729 | goto do_syscall; |
1730 | } |
1731 | print_tabs(1, fd); |
59a957df |
1732 | fprintf(fd, "char *buffer = NULL;\n"); |
bd7b8ca6 |
1733 | print_tabs(1, fd); |
1734 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
1735 | print_tabs(1, fd); |
1736 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
1737 | print_tabs(1, fd); |
1738 | fprintf(fd, "size_t real_to = 0;\n"); |
1739 | print_tabs(1, fd); |
1740 | fprintf(fd, "size_t *to = &real_to;\n"); |
1741 | print_tabs(1, fd); |
1742 | fprintf(fd, "size_t real_len = 0;\n"); |
1743 | print_tabs(1, fd); |
1744 | fprintf(fd, "size_t *len = &real_len;\n"); |
1745 | print_tabs(1, fd); |
1746 | fprintf(fd, "size_t reserve_size;\n"); |
1747 | print_tabs(1, fd); |
1748 | fprintf(fd, "size_t slot_size;\n"); |
1749 | print_tabs(1, fd); |
bd7b8ca6 |
1750 | |
1751 | if(event->fields.position > 0) { |
1752 | for(unsigned int i=0;i<event->fields.position;i++){ |
1753 | /* Search for at least one child with fixed size. It means |
1754 | * we need local variables.*/ |
1755 | field_t *field = (field_t*)(event->fields.array[i]); |
1756 | type_descriptor_t *type = field->type; |
1757 | has_type_fixed = has_type_local(type); |
1758 | if(has_type_fixed) break; |
1759 | } |
1760 | |
1761 | if(has_type_fixed) { |
1762 | fprintf(fd, "size_t align;\n"); |
1763 | print_tabs(1, fd); |
1764 | } |
1765 | |
59a957df |
1766 | fprintf(fd, "const char *real_from;\n"); |
bd7b8ca6 |
1767 | print_tabs(1, fd); |
59a957df |
1768 | fprintf(fd, "const char **from = &real_from;\n"); |
bd7b8ca6 |
1769 | print_tabs(1, fd); |
1770 | } |
1771 | |
1772 | /* Calculate event variable len + event data alignment offset. |
1773 | * Assume that the padding for alignment starts at a void* |
1774 | * address. |
1775 | * This excludes the header size and alignment. */ |
1776 | |
1777 | print_tabs(1, fd); |
1778 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
1779 | print_tabs(1, fd); |
1780 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
1781 | print_tabs(1, fd); |
1782 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
1783 | print_tabs(1, fd); |
1784 | fprintf(fd, " * sizeof(void *) address. */\n"); |
1785 | fprintf(fd, "\n"); |
1786 | |
1787 | for(unsigned int i=0;i<event->fields.position;i++){ |
1788 | field_t *field = (field_t*)(event->fields.array[i]); |
1789 | type_descriptor_t *type = field->type; |
1790 | /* Set from */ |
1791 | print_tabs(1, fd); |
1792 | switch(type->type) { |
1793 | case SEQUENCE: |
1794 | case UNION: |
1795 | case ARRAY: |
1796 | case STRUCT: |
1797 | case STRING: |
922b1092 |
1798 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
bd7b8ca6 |
1799 | break; |
1800 | default: |
922b1092 |
1801 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
bd7b8ca6 |
1802 | break; |
1803 | } |
1804 | |
1805 | if(print_type_write(type, |
1806 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
1807 | fprintf(fd, "\n"); |
1808 | } |
1809 | print_tabs(1, fd); |
1810 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
1811 | |
1812 | print_tabs(1, fd); |
1813 | fprintf(fd, "{\n"); |
1814 | print_tabs(2, fd); |
1815 | fprintf(fd, "char stack_buffer[reserve_size];\n"); |
1816 | print_tabs(2, fd); |
1817 | fprintf(fd, "buffer = stack_buffer;\n"); |
1818 | fprintf(fd, "\n"); |
1819 | |
1820 | |
1821 | //print_tabs(2, fd); |
1822 | // for DEBUG only |
1823 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
1824 | print_tabs(2, fd); |
1825 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
1826 | fprintf(fd, "\n"); |
1827 | |
1828 | /* write data. */ |
1829 | |
1830 | for(unsigned int i=0;i<event->fields.position;i++){ |
1831 | field_t *field = (field_t*)(event->fields.array[i]); |
1832 | type_descriptor_t *type = field->type; |
1833 | |
1834 | /* Set from */ |
1835 | print_tabs(2, fd); |
1836 | switch(type->type) { |
1837 | case SEQUENCE: |
1838 | case UNION: |
1839 | case ARRAY: |
1840 | case STRUCT: |
1841 | case STRING: |
922b1092 |
1842 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
bd7b8ca6 |
1843 | break; |
1844 | default: |
922b1092 |
1845 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
bd7b8ca6 |
1846 | break; |
1847 | } |
1848 | |
1849 | |
1850 | if(print_type_write(type, |
1851 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
1852 | fprintf(fd, "\n"); |
1853 | |
1854 | /* Don't forget to flush pending memcpy */ |
1855 | print_tabs(2, fd); |
1856 | fprintf(fd, "/* Flush pending memcpy */\n"); |
1857 | print_tabs(2, fd); |
1858 | fprintf(fd, "if(*len != 0) {\n"); |
1859 | print_tabs(3, fd); |
1860 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
1861 | print_tabs(3, fd); |
1862 | fprintf(fd, "*to += *len;\n"); |
1863 | //print_tabs(3, fd); |
1864 | //fprintf(fd, "from += len;\n"); |
1865 | print_tabs(3, fd); |
1866 | fprintf(fd, "*len = 0;\n"); |
1867 | print_tabs(2, fd); |
1868 | fprintf(fd, "}\n"); |
1869 | fprintf(fd, "\n"); |
1870 | } |
1871 | |
1e08067e |
1872 | do_syscall: |
bd7b8ca6 |
1873 | print_tabs(2, fd); |
c3fff9a4 |
1874 | fprintf(fd, "ret = ltt_trace_generic(ltt_facility_%s_%X, event_%s_%s, buffer, reserve_size, LTT_BLOCKING, %u);\n", fac->name, fac->checksum, fac->name, event->name, event->high_priority); |
bd7b8ca6 |
1875 | |
1876 | print_tabs(1, fd); |
1877 | fprintf(fd, "}\n\n"); |
1878 | |
1879 | print_tabs(1, fd); |
1880 | fprintf(fd, "return ret;\n\n"); |
1881 | |
1882 | fprintf(fd, "}\n"); |
1883 | fprintf(fd, |
1884 | "#endif //LTT_TRACE\n"); |
8a9103df |
1885 | fprintf(fd, "#endif //!LTT_TRACE_FAST\n\n"); |
bd7b8ca6 |
1886 | |
1887 | return 0; |
1888 | } |
2d2d14a7 |
1889 | |
8a9103df |
1890 | /* print_event_logging_function_user_fast |
1891 | * Print the logging function of an event for userspace tracing. This is the |
1892 | * core of genevent */ |
1893 | int print_event_logging_function_user_fast(char *basename, facility_t *fac, |
1894 | event_t *event, FILE *fd) |
1895 | { |
1896 | char *attrib; |
1897 | |
1898 | fprintf(fd, "#ifdef LTT_TRACE_FAST\n"); |
1899 | |
1900 | if(event->no_instrument_function) { |
1901 | attrib = "__attribute__((no_instrument_function)) "; |
1902 | } else { |
1903 | attrib = ""; |
1904 | } |
1905 | fprintf(fd, "static inline %sint trace_%s(\n",attrib, basename); |
1906 | |
1907 | int has_argument = 0; |
1908 | int has_type_fixed = 0; |
1909 | |
1910 | for(unsigned int j = 0; j < event->fields.position; j++) { |
1911 | /* For each field, print the function argument */ |
1912 | field_t *f = (field_t*)event->fields.array[j]; |
1913 | type_descriptor_t *t = f->type; |
1914 | if(has_argument) { |
1915 | fprintf(fd, ","); |
1916 | fprintf(fd, "\n"); |
1917 | } |
1918 | if(print_arg(t, fd, 2, basename, f->name)) return 1; |
1919 | has_argument = 1; |
1920 | } |
1921 | if(!has_argument) { |
1922 | print_tabs(2, fd); |
1923 | fprintf(fd, "void"); |
1924 | } |
1925 | fprintf(fd,")\n"); |
1926 | fprintf(fd, |
1927 | "#ifndef LTT_TRACE\n"); |
1928 | fprintf(fd, "{\n"); |
1929 | fprintf(fd, "}\n"); |
1930 | fprintf(fd,"#else\n"); |
1931 | fprintf(fd, "{\n"); |
1932 | /* Print the function variables */ |
1933 | print_tabs(1, fd); |
1934 | fprintf(fd, "unsigned int index;\n"); |
1935 | print_tabs(1, fd); |
1936 | fprintf(fd, "struct ltt_trace_info *trace = thread_trace_info;\n"); |
1937 | print_tabs(1, fd); |
1938 | fprintf(fd, "struct ltt_buf *ltt_buf;\n"); |
1939 | print_tabs(1, fd); |
59a957df |
1940 | fprintf(fd, "char *buffer = NULL;\n"); |
8a9103df |
1941 | print_tabs(1, fd); |
1942 | fprintf(fd, "size_t real_to_base = 0; /* The buffer is allocated on arch_size alignment */\n"); |
1943 | print_tabs(1, fd); |
1944 | fprintf(fd, "size_t *to_base = &real_to_base;\n"); |
1945 | print_tabs(1, fd); |
1946 | fprintf(fd, "size_t real_to = 0;\n"); |
1947 | print_tabs(1, fd); |
1948 | fprintf(fd, "size_t *to = &real_to;\n"); |
1949 | print_tabs(1, fd); |
1950 | fprintf(fd, "size_t real_len = 0;\n"); |
1951 | print_tabs(1, fd); |
1952 | fprintf(fd, "size_t *len = &real_len;\n"); |
1953 | print_tabs(1, fd); |
1954 | fprintf(fd, "size_t reserve_size;\n"); |
1955 | print_tabs(1, fd); |
1956 | fprintf(fd, "size_t slot_size;\n"); |
1957 | print_tabs(1, fd); |
1958 | |
1959 | if(event->fields.position > 0) { |
1960 | for(unsigned int i=0;i<event->fields.position;i++){ |
1961 | /* Search for at least one child with fixed size. It means |
1962 | * we need local variables.*/ |
1963 | field_t *field = (field_t*)(event->fields.array[i]); |
1964 | type_descriptor_t *type = field->type; |
1965 | has_type_fixed = has_type_local(type); |
1966 | if(has_type_fixed) break; |
1967 | } |
1968 | |
1969 | if(has_type_fixed) { |
1970 | fprintf(fd, "size_t align;\n"); |
1971 | print_tabs(1, fd); |
1972 | } |
1973 | |
59a957df |
1974 | fprintf(fd, "const char *real_from;\n"); |
8a9103df |
1975 | print_tabs(1, fd); |
59a957df |
1976 | fprintf(fd, "const char **from = &real_from;\n"); |
8a9103df |
1977 | print_tabs(1, fd); |
1978 | } |
1979 | fprintf(fd, "uint64_t tsc;\n"); |
1980 | print_tabs(1, fd); |
1981 | fprintf(fd, "size_t before_hdr_pad, after_hdr_pad, header_size;\n"); |
1982 | fprintf(fd, "\n"); |
1983 | |
1984 | print_tabs(1, fd); |
ba017bc3 |
1985 | fprintf(fd, "if(!trace) {\n"); |
1986 | print_tabs(2, fd); |
1987 | fprintf(fd, "ltt_thread_init();\n"); |
1988 | print_tabs(2, fd); |
1989 | fprintf(fd, "trace = thread_trace_info;\n"); |
1990 | print_tabs(1, fd); |
1991 | fprintf(fd, "}\n\n"); |
8a9103df |
1992 | fprintf(fd, "\n"); |
1993 | |
1994 | /* Calculate event variable len + event data alignment offset. |
1995 | * Assume that the padding for alignment starts at a void* |
1996 | * address. |
1997 | * This excludes the header size and alignment. */ |
1998 | |
1999 | print_tabs(1, fd); |
2000 | fprintf(fd, "/* For each field, calculate the field size. */\n"); |
2001 | print_tabs(1, fd); |
2002 | fprintf(fd, "/* size = *to_base + *to + *len */\n"); |
2003 | print_tabs(1, fd); |
2004 | fprintf(fd, "/* Assume that the padding for alignment starts at a\n"); |
2005 | print_tabs(1, fd); |
2006 | fprintf(fd, " * sizeof(void *) address. */\n"); |
2007 | fprintf(fd, "\n"); |
2008 | |
2009 | for(unsigned int i=0;i<event->fields.position;i++){ |
2010 | field_t *field = (field_t*)(event->fields.array[i]); |
2011 | type_descriptor_t *type = field->type; |
2012 | /* Set from */ |
2013 | print_tabs(1, fd); |
2014 | switch(type->type) { |
2015 | case SEQUENCE: |
2016 | case UNION: |
2017 | case ARRAY: |
2018 | case STRUCT: |
2019 | case STRING: |
922b1092 |
2020 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
8a9103df |
2021 | break; |
2022 | default: |
922b1092 |
2023 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
8a9103df |
2024 | break; |
2025 | } |
2026 | |
2027 | if(print_type_write(type, |
2028 | fd, 1, basename, field->name, "lttng_param_", 0)) return 1; |
2029 | fprintf(fd, "\n"); |
2030 | } |
2031 | print_tabs(1, fd); |
2032 | fprintf(fd, "reserve_size = *to_base + *to + *len;\n"); |
2033 | |
2034 | print_tabs(1, fd); |
2035 | fprintf(fd, "trace->nesting++;\n"); |
2036 | |
2037 | /* Get facility index */ |
2038 | |
2039 | print_tabs(1, fd); |
2040 | fprintf(fd, |
2041 | "index = ltt_get_index_from_facility(ltt_facility_%s_%X,\n"\ |
2042 | "\t\t\t\t\t\tevent_%s_%s);\n", |
2043 | fac->name, fac->checksum, fac->name, event->name); |
2044 | fprintf(fd,"\n"); |
2045 | |
2046 | |
2047 | print_tabs(1, fd); |
2048 | fprintf(fd, "{\n"); |
2049 | |
2050 | if(event->per_trace) { |
2051 | print_tabs(2, fd); |
2052 | fprintf(fd, "if(dest_trace != trace) continue;\n\n"); |
2053 | } |
2054 | |
2055 | print_tabs(2, fd); |
2056 | fprintf(fd, "ltt_buf = ltt_get_channel_from_index(trace, index);\n"); |
2057 | print_tabs(2, fd); |
2058 | |
2059 | |
2060 | /* Relay reserve */ |
2061 | /* If error, increment event lost counter (done by ltt_reserve_slot) and |
2062 | * return */ |
2063 | print_tabs(2, fd); |
2064 | fprintf(fd, "slot_size = 0;\n"); |
2065 | print_tabs(2, fd); |
922b1092 |
2066 | fprintf(fd, "buffer = ltt_reserve_slot(trace, ltt_buf,\n"); |
8a9103df |
2067 | print_tabs(3, fd); |
2068 | fprintf(fd, "reserve_size, &slot_size, &tsc,\n"); |
2069 | print_tabs(3, fd); |
2070 | fprintf(fd, "&before_hdr_pad, &after_hdr_pad, &header_size);\n"); |
2071 | /* If error, return */ |
2072 | print_tabs(2, fd); |
2073 | fprintf(fd, "if(!buffer) goto end; /* buffer full */\n\n"); |
2074 | //print_tabs(2, fd); |
2075 | // for DEBUG only |
2076 | // fprintf(fd, "goto commit; /* DEBUG : never actually write. */\n\n"); |
2077 | print_tabs(2, fd); |
2078 | fprintf(fd, "*to_base = *to = *len = 0;\n"); |
2079 | fprintf(fd, "\n"); |
2080 | |
2081 | /* Write event header */ |
2082 | print_tabs(2, fd); |
2083 | fprintf(fd, "ltt_write_event_header(trace, ltt_buf, buffer,\n"); |
2084 | print_tabs(3, fd); |
2085 | fprintf(fd, "ltt_facility_%s_%X, event_%s_%s,\n", fac->name, fac->checksum, |
2086 | fac->name, event->name); |
2087 | print_tabs(3, fd); |
2088 | fprintf(fd, "reserve_size, before_hdr_pad, tsc);\n"); |
2089 | print_tabs(2, fd); |
2090 | fprintf(fd, "*to_base += before_hdr_pad + after_hdr_pad + header_size;\n"); |
2091 | fprintf(fd, "\n"); |
2092 | |
2093 | /* write data. */ |
2094 | |
2095 | for(unsigned int i=0;i<event->fields.position;i++){ |
2096 | field_t *field = (field_t*)(event->fields.array[i]); |
2097 | type_descriptor_t *type = field->type; |
2098 | |
2099 | /* Set from */ |
2100 | print_tabs(2, fd); |
2101 | switch(type->type) { |
2102 | case SEQUENCE: |
2103 | case UNION: |
2104 | case ARRAY: |
2105 | case STRUCT: |
2106 | case STRING: |
922b1092 |
2107 | fprintf(fd, "*from = (const char*)lttng_param_%s;\n", field->name); |
8a9103df |
2108 | break; |
2109 | default: |
922b1092 |
2110 | fprintf(fd, "*from = (const char*)<tng_param_%s;\n", field->name); |
8a9103df |
2111 | break; |
2112 | } |
2113 | |
2114 | |
2115 | if(print_type_write(type, |
2116 | fd, 2, basename, field->name, "lttng_param_", 0)) return 1; |
2117 | fprintf(fd, "\n"); |
2118 | |
2119 | /* Don't forget to flush pending memcpy */ |
2120 | print_tabs(2, fd); |
2121 | fprintf(fd, "/* Flush pending memcpy */\n"); |
2122 | print_tabs(2, fd); |
2123 | fprintf(fd, "if(*len != 0) {\n"); |
2124 | print_tabs(3, fd); |
2125 | fprintf(fd, "memcpy(buffer+*to_base+*to, *from, *len);\n"); |
2126 | print_tabs(3, fd); |
2127 | fprintf(fd, "*to += *len;\n"); |
2128 | //print_tabs(3, fd); |
2129 | //fprintf(fd, "from += len;\n"); |
2130 | print_tabs(3, fd); |
2131 | fprintf(fd, "*len = 0;\n"); |
2132 | print_tabs(2, fd); |
2133 | fprintf(fd, "}\n"); |
2134 | fprintf(fd, "\n"); |
2135 | } |
2136 | |
2137 | |
2138 | /* commit */ |
2139 | // for DEBUG only. |
2140 | //fprintf(fd, "commit:\n"); /* DEBUG! */ |
2141 | print_tabs(2, fd); |
922b1092 |
2142 | fprintf(fd, "ltt_commit_slot(ltt_buf, buffer, slot_size);\n\n"); |
8a9103df |
2143 | |
2144 | fprintf(fd, "}\n\n"); |
2145 | |
2146 | fprintf(fd, "end:\n"); |
2147 | /* Release locks */ |
2148 | print_tabs(1, fd); |
2149 | fprintf(fd, "trace->nesting--;\n"); |
2150 | |
2151 | |
2152 | fprintf(fd, "}\n"); |
2153 | fprintf(fd, |
2154 | "#endif //LTT_TRACE\n"); |
2155 | fprintf(fd, "#endif //LTT_TRACE_FAST\n"); |
2156 | |
2157 | return 0; |
2158 | } |
2159 | |
2160 | |
2161 | |
2162 | |
2163 | |
2164 | |
2d2d14a7 |
2165 | /* ltt-facility-name.h : main logging header. |
2166 | * log_header */ |
2167 | |
2168 | void print_log_header_head(facility_t *fac, FILE *fd) |
2169 | { |
2170 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
2171 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
922b1092 |
2172 | fprintf(fd, "#include <linux/types.h>\n"); |
ffaf5031 |
2173 | if(!fac->arch) |
c3fff9a4 |
2174 | fprintf(fd, "#include <ltt/ltt-facility-id-%s.h>\n", fac->name); |
ffaf5031 |
2175 | else |
c3fff9a4 |
2176 | fprintf(fd, "#include <ltt/ltt-facility-id-%s_%s.h>\n", |
ffaf5031 |
2177 | fac->name, |
2178 | fac->arch); |
c3fff9a4 |
2179 | fprintf(fd, "#include <ltt/ltt-tracer.h>\n"); |
47299663 |
2180 | fprintf(fd, "\n"); |
2d2d14a7 |
2181 | } |
2182 | |
bd7b8ca6 |
2183 | /* ltt-facility-name.h : main logging header. |
2184 | * log_header */ |
2185 | |
2186 | void print_log_header_head_user(facility_t *fac, FILE *fd) |
2187 | { |
2188 | fprintf(fd, "#ifndef _LTT_FACILITY_%s_H_\n", fac->capname); |
2189 | fprintf(fd, "#define _LTT_FACILITY_%s_H_\n\n", fac->capname); |
922b1092 |
2190 | fprintf(fd, "#include <sys/types.h>\n"); |
bd7b8ca6 |
2191 | if(!fac->arch) |
922b1092 |
2192 | fprintf(fd, "#include <ltt/ltt-facility-id-%s.h>\n", fac->name); |
bd7b8ca6 |
2193 | else |
922b1092 |
2194 | fprintf(fd, "#include <asm/ltt/ltt-facility-id-%s_%s.h>\n", |
bd7b8ca6 |
2195 | fac->name, |
2196 | fac->arch); |
922b1092 |
2197 | fprintf(fd, "#include <ltt/ltt-usertrace.h>\n"); |
2198 | fprintf(fd, "\n"); |
2199 | fprintf(fd, "#ifdef __cplusplus\n"); |
2200 | fprintf(fd, "extern \"C\" {\n"); |
2201 | fprintf(fd, "#endif\n"); |
bd7b8ca6 |
2202 | fprintf(fd, "\n"); |
2203 | } |
2d2d14a7 |
2204 | |
2205 | |
2d2d14a7 |
2206 | int print_log_header_types(facility_t *fac, FILE *fd) |
2207 | { |
2208 | sequence_t *types = &fac->named_types.values; |
2209 | fprintf(fd, "/* Named types */\n"); |
2210 | fprintf(fd, "\n"); |
2211 | |
2212 | for(unsigned int i = 0; i < types->position; i++) { |
2213 | /* For each named type, print the definition */ |
458989d8 |
2214 | if(print_type_declaration(types->array[i], fd, |
2215 | 0, "", "")) return 1; |
34c1d1b5 |
2216 | /* Print also the align function */ |
458989d8 |
2217 | if(print_type_alignment_fct(types->array[i], fd, |
2218 | 0, "", "")) return 1; |
34c1d1b5 |
2219 | /* Print also the write function */ |
458989d8 |
2220 | if(print_type_write_fct(types->array[i], fd, |
2221 | 0, "", "")) return 1; |
2d2d14a7 |
2222 | } |
2223 | return 0; |
2224 | } |
2225 | |
2226 | int print_log_header_events(facility_t *fac, FILE *fd) |
2227 | { |
47299663 |
2228 | sequence_t *events = &fac->events; |
2229 | char basename[PATH_MAX]; |
2230 | unsigned int facname_len; |
2231 | |
2232 | strncpy(basename, fac->name, PATH_MAX); |
2233 | facname_len = strlen(basename); |
2234 | strncat(basename, "_", PATH_MAX-facname_len); |
2235 | facname_len = strlen(basename); |
2236 | |
2237 | for(unsigned int i = 0; i < events->position; i++) { |
2238 | event_t *event = (event_t*)events->array[i]; |
2239 | strncpy(&basename[facname_len], event->name, PATH_MAX-facname_len); |
2240 | |
2241 | /* For each event, print structure, and then logging function */ |
2242 | fprintf(fd, "/* Event %s structures */\n", |
2243 | event->name); |
2244 | for(unsigned int j = 0; j < event->fields.position; j++) { |
2245 | /* For each unnamed type, print the definition */ |
2246 | field_t *f = (field_t*)event->fields.array[j]; |
2247 | type_descriptor_t *t = f->type; |
34c1d1b5 |
2248 | if(t->type_name == NULL) { |
47299663 |
2249 | if((print_type_declaration(t, fd, 0, basename, f->name))) return 1; |
34c1d1b5 |
2250 | /* Print also the align function */ |
2251 | if((print_type_alignment_fct(t, fd, 0, basename, f->name))) return 1; |
2252 | /* Print also the write function */ |
2253 | if((print_type_write_fct(t, fd, 0, basename, f->name))) return 1; |
2254 | } |
47299663 |
2255 | } |
34c1d1b5 |
2256 | |
47299663 |
2257 | fprintf(fd, "\n"); |
2d2d14a7 |
2258 | |
47299663 |
2259 | fprintf(fd, "/* Event %s logging function */\n", |
2260 | event->name); |
bd7b8ca6 |
2261 | |
2262 | if(!fac->user) { |
2263 | if(print_event_logging_function(basename, fac, event, fd)) return 1; |
2264 | } else { |
8a9103df |
2265 | if(print_event_logging_function_user_generic(basename, fac, event, fd)) |
2266 | return 1; |
2267 | if(print_event_logging_function_user_fast(basename, fac, event, fd)) |
2268 | return 1; |
bd7b8ca6 |
2269 | } |
47299663 |
2270 | |
2271 | fprintf(fd, "\n"); |
2272 | } |
2273 | |
2d2d14a7 |
2274 | return 0; |
2275 | } |
2276 | |
2277 | |
2278 | void print_log_header_tail(facility_t *fac, FILE *fd) |
2279 | { |
2280 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
2281 | } |
bd7b8ca6 |
2282 | |
2283 | void print_log_header_tail_user(facility_t *fac, FILE *fd) |
2284 | { |
922b1092 |
2285 | fprintf(fd, "#ifdef __cplusplus\n"); |
2286 | fprintf(fd, "} /* end of extern \"C\" */\n"); |
2287 | fprintf(fd, "#endif\n"); |
2288 | fprintf(fd, "\n"); |
bd7b8ca6 |
2289 | fprintf(fd, "#endif //_LTT_FACILITY_%s_H_\n",fac->capname); |
2290 | } |
2d2d14a7 |
2291 | |
2292 | int print_log_header(facility_t *fac) |
2293 | { |
2294 | char filename[PATH_MAX]; |
2295 | unsigned int filename_size = 0; |
2296 | FILE *fd; |
2297 | dprintf("%s\n", fac->name); |
2298 | |
2299 | strcpy(filename, "ltt-facility-"); |
2300 | filename_size = strlen(filename); |
2301 | |
2302 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2303 | filename_size = strlen(filename); |
2304 | |
ffaf5031 |
2305 | if(fac->arch) { |
2306 | strncat(filename, "_", PATH_MAX - filename_size); |
2307 | filename_size = strlen(filename); |
2308 | |
2309 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2310 | filename_size = strlen(filename); |
2311 | } |
2312 | |
2d2d14a7 |
2313 | strncat(filename, ".h", PATH_MAX - filename_size); |
2314 | filename_size = strlen(filename); |
2315 | |
2316 | |
2317 | fd = fopen(filename, "w"); |
2318 | if(fd == NULL) { |
2319 | printf("Error opening file %s for writing : %s\n", |
2320 | filename, strerror(errno)); |
2321 | return errno; |
2322 | } |
2323 | |
2324 | /* Print file head */ |
bd7b8ca6 |
2325 | if(!fac->user) |
2326 | print_log_header_head(fac, fd); |
2327 | else |
2328 | print_log_header_head_user(fac, fd); |
2d2d14a7 |
2329 | |
2330 | /* print named types in declaration order */ |
2331 | if(print_log_header_types(fac, fd)) return 1; |
2332 | |
2333 | /* Print events */ |
2334 | if(print_log_header_events(fac, fd)) return 1; |
2335 | |
2336 | /* Print file tail */ |
bd7b8ca6 |
2337 | if(!fac->user) |
2338 | print_log_header_tail(fac, fd); |
2339 | else |
2340 | print_log_header_tail_user(fac, fd); |
2341 | |
2d2d14a7 |
2342 | |
2343 | |
2344 | fclose(fd); |
2345 | |
2346 | return 0; |
2347 | } |
2348 | |
2349 | |
2350 | /* ltt-facility-id-name.h : facility id. |
2351 | * log_id_header */ |
2352 | int print_id_header(facility_t *fac) |
2353 | { |
2354 | char filename[PATH_MAX]; |
2355 | unsigned int filename_size = 0; |
2356 | FILE *fd; |
71e09db9 |
2357 | char basename[PATH_MAX]; |
2358 | char basename_len = 0; |
2359 | |
2d2d14a7 |
2360 | dprintf("%s\n", fac->name); |
2361 | |
2362 | strcpy(filename, "ltt-facility-id-"); |
2363 | filename_size = strlen(filename); |
2364 | |
2365 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2366 | filename_size = strlen(filename); |
2367 | |
ffaf5031 |
2368 | if(fac->arch) { |
2369 | strncat(filename, "_", PATH_MAX - filename_size); |
2370 | filename_size = strlen(filename); |
2371 | |
2372 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2373 | filename_size = strlen(filename); |
2374 | } |
2375 | |
2d2d14a7 |
2376 | strncat(filename, ".h", PATH_MAX - filename_size); |
2377 | filename_size = strlen(filename); |
2378 | |
2379 | |
2380 | fd = fopen(filename, "w"); |
2381 | if(fd == NULL) { |
2382 | printf("Error opening file %s for writing : %s\n", |
2383 | filename, strerror(errno)); |
2384 | return errno; |
2385 | } |
2386 | |
bd7b8ca6 |
2387 | if(!fac->user) { |
2388 | fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname); |
2389 | fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname); |
2390 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
71e09db9 |
2391 | |
bd7b8ca6 |
2392 | fprintf(fd,"#include <linux/ltt-facilities.h>\n\n"); |
71e09db9 |
2393 | |
bd7b8ca6 |
2394 | fprintf(fd,"/**** facility handle ****/\n\n"); |
2395 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n", |
2396 | fac->name, fac->checksum); |
2397 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name); |
71e09db9 |
2398 | |
bd7b8ca6 |
2399 | strncpy(basename, fac->name, PATH_MAX); |
2400 | basename_len = strlen(basename); |
2401 | strncat(basename, "_", PATH_MAX - basename_len); |
2402 | basename_len++; |
2403 | |
2404 | fprintf(fd,"/**** event index ****/\n\n"); |
2405 | fprintf(fd,"enum %s_event {\n",fac->name); |
2406 | |
2407 | for(unsigned int i = 0; i < fac->events.position; i++) { |
2408 | event_t *event = (event_t*)fac->events.array[i]; |
2409 | strncpy(basename+basename_len, event->name, PATH_MAX-basename_len); |
2410 | print_tabs(1, fd); |
2411 | fprintf(fd, "event_%s,\n", basename); |
2412 | } |
71e09db9 |
2413 | print_tabs(1, fd); |
bd7b8ca6 |
2414 | fprintf(fd, "facility_%s_num_events\n", fac->name); |
2415 | fprintf(fd, "};\n"); |
2416 | fprintf(fd, "\n"); |
71e09db9 |
2417 | |
2418 | |
bd7b8ca6 |
2419 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
2420 | fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname); |
2421 | } else { |
2422 | fprintf(fd, "#ifndef _LTT_FACILITY_ID_%s_H_\n",fac->capname); |
2423 | fprintf(fd, "#define _LTT_FACILITY_ID_%s_H_\n\n",fac->capname); |
2424 | fprintf(fd, "#ifdef LTT_TRACE\n"); |
2425 | |
f389e596 |
2426 | fprintf(fd,"#include <ltt/ltt-usertrace.h>\n\n"); |
bd7b8ca6 |
2427 | |
2428 | fprintf(fd,"/**** facility handle ****/\n\n"); |
2429 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s_%X;\n", |
2430 | fac->name, fac->checksum); |
2431 | fprintf(fd,"extern ltt_facility_t ltt_facility_%s;\n\n\n",fac->name); |
2432 | |
2433 | strncpy(basename, fac->name, PATH_MAX); |
2434 | basename_len = strlen(basename); |
2435 | strncat(basename, "_", PATH_MAX - basename_len); |
2436 | basename_len++; |
2437 | |
2438 | fprintf(fd,"/**** event index ****/\n\n"); |
2439 | fprintf(fd,"enum %s_event {\n",fac->name); |
2440 | |
2441 | for(unsigned int i = 0; i < fac->events.position; i++) { |
2442 | event_t *event = (event_t*)fac->events.array[i]; |
2443 | strncpy(basename+basename_len, event->name, PATH_MAX-basename_len); |
2444 | print_tabs(1, fd); |
2445 | fprintf(fd, "event_%s,\n", basename); |
2446 | } |
2447 | print_tabs(1, fd); |
2448 | fprintf(fd, "facility_%s_num_events\n", fac->name); |
2449 | fprintf(fd, "};\n"); |
2450 | fprintf(fd, "\n"); |
2451 | |
2452 | |
2453 | fprintf(fd, "#endif //LTT_TRACE\n"); |
2454 | fprintf(fd, "#endif //_LTT_FACILITY_ID_%s_H_\n",fac->capname); |
2455 | } |
71e09db9 |
2456 | |
2457 | |
2d2d14a7 |
2458 | fclose(fd); |
2459 | |
2460 | return 0; |
2461 | } |
2462 | |
2463 | |
2464 | /* ltt-facility-loader-name.h : facility specific loader info. |
2465 | * loader_header */ |
2466 | int print_loader_header(facility_t *fac) |
2467 | { |
2468 | char filename[PATH_MAX]; |
2469 | unsigned int filename_size = 0; |
2470 | FILE *fd; |
2471 | dprintf("%s\n", fac->name); |
2472 | |
2473 | strcpy(filename, "ltt-facility-loader-"); |
2474 | filename_size = strlen(filename); |
2475 | |
2476 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2477 | filename_size = strlen(filename); |
2478 | |
ffaf5031 |
2479 | if(fac->arch) { |
2480 | strncat(filename, "_", PATH_MAX - filename_size); |
2481 | filename_size = strlen(filename); |
2482 | |
2483 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2484 | filename_size = strlen(filename); |
2485 | } |
2486 | |
2d2d14a7 |
2487 | strncat(filename, ".h", PATH_MAX - filename_size); |
2488 | filename_size = strlen(filename); |
2489 | |
2490 | |
2491 | fd = fopen(filename, "w"); |
2492 | if(fd == NULL) { |
2493 | printf("Error opening file %s for writing : %s\n", |
2494 | filename, strerror(errno)); |
2495 | return errno; |
2496 | } |
2497 | |
71e09db9 |
2498 | fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
2499 | fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname); |
2500 | fprintf(fd, "#ifdef CONFIG_LTT\n\n"); |
2501 | fprintf(fd,"#include <linux/ltt-facilities.h>\n"); |
ffaf5031 |
2502 | if(!fac->arch) |
c3fff9a4 |
2503 | fprintf(fd,"#include <ltt/ltt-facility-id-%s.h>\n\n", |
ffaf5031 |
2504 | fac->name); |
2505 | else |
c3fff9a4 |
2506 | fprintf(fd,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n", |
ffaf5031 |
2507 | fac->name, |
2508 | fac->arch); |
71e09db9 |
2509 | fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name); |
2510 | fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n", |
2511 | fac->name, fac->checksum); |
2512 | |
0043cb00 |
2513 | fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\tltt_facility_%s\n", |
71e09db9 |
2514 | fac->name); |
0043cb00 |
2515 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\tltt_facility_%s_%X\n", |
71e09db9 |
2516 | fac->name, fac->checksum); |
0043cb00 |
2517 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t0x%X\n", fac->checksum); |
2518 | fprintf(fd,"#define LTT_FACILITY_NAME\t\t\"%s\"\n", fac->name); |
2519 | fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\tfacility_%s_num_events\n\n", |
71e09db9 |
2520 | fac->name); |
2521 | fprintf(fd, "#endif //CONFIG_LTT\n\n"); |
2522 | fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
2523 | |
2d2d14a7 |
2524 | fclose(fd); |
2525 | |
2526 | return 0; |
2527 | } |
2528 | |
bd7b8ca6 |
2529 | int print_loader_header_user(facility_t *fac) |
2530 | { |
2531 | char filename[PATH_MAX]; |
2532 | unsigned int filename_size = 0; |
2533 | FILE *fd; |
2534 | dprintf("%s\n", fac->name); |
2535 | |
2536 | strcpy(filename, "ltt-facility-loader-"); |
2537 | filename_size = strlen(filename); |
2538 | |
2539 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2540 | filename_size = strlen(filename); |
2541 | |
2542 | if(fac->arch) { |
2543 | strncat(filename, "_", PATH_MAX - filename_size); |
2544 | filename_size = strlen(filename); |
2545 | |
2546 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2547 | filename_size = strlen(filename); |
2548 | } |
2549 | |
2550 | strncat(filename, ".h", PATH_MAX - filename_size); |
2551 | filename_size = strlen(filename); |
2552 | |
2553 | |
2554 | fd = fopen(filename, "w"); |
2555 | if(fd == NULL) { |
2556 | printf("Error opening file %s for writing : %s\n", |
2557 | filename, strerror(errno)); |
2558 | return errno; |
2559 | } |
2560 | |
2561 | fprintf(fd, "#ifndef _LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
2562 | fprintf(fd, "#define _LTT_FACILITY_LOADER_%s_H_\n\n", fac->capname); |
f389e596 |
2563 | fprintf(fd,"#include <ltt/ltt-usertrace.h>\n"); |
bd7b8ca6 |
2564 | if(!fac->arch) |
2565 | fprintf(fd,"#include <ltt/ltt-facility-id-%s.h>\n\n", |
2566 | fac->name); |
2567 | else |
c3fff9a4 |
2568 | fprintf(fd,"#include <ltt/ltt-facility-id-%s_%s.h>\n\n", |
bd7b8ca6 |
2569 | fac->name, |
2570 | fac->arch); |
2571 | fprintf(fd,"ltt_facility_t\tltt_facility_%s;\n", fac->name); |
2572 | fprintf(fd,"ltt_facility_t\tltt_facility_%s_%X;\n\n", |
2573 | fac->name, fac->checksum); |
2574 | |
2575 | fprintf(fd,"#define LTT_FACILITY_SYMBOL\t\t\t\t\t\t\tltt_facility_%s\n", |
2576 | fac->name); |
2577 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM_SYMBOL\t\tltt_facility_%s_%X\n", |
2578 | fac->name, fac->checksum); |
2579 | fprintf(fd,"#define LTT_FACILITY_CHECKSUM\t\t\t\t\t\t0x%X\n", fac->checksum); |
2580 | fprintf(fd,"#define LTT_FACILITY_NAME\t\t\t\t\t\t\t\t\"%s\"\n", fac->name); |
2581 | fprintf(fd,"#define LTT_FACILITY_NUM_EVENTS\t\t\t\t\tfacility_%s_num_events\n\n", |
2582 | fac->name); |
2583 | fprintf(fd, "#endif //_LTT_FACILITY_LOADER_%s_H_\n", fac->capname); |
2584 | |
2585 | fclose(fd); |
2586 | |
2587 | return 0; |
2588 | } |
2589 | |
2590 | |
2591 | |
2592 | /* ltt-facility-loader-name.c : generic facility loader |
2d2d14a7 |
2593 | * loader_c */ |
2594 | int print_loader_c(facility_t *fac) |
2595 | { |
2596 | char filename[PATH_MAX]; |
2597 | unsigned int filename_size = 0; |
2598 | FILE *fd; |
2599 | dprintf("%s\n", fac->name); |
2600 | |
2601 | strcpy(filename, "ltt-facility-loader-"); |
2602 | filename_size = strlen(filename); |
2603 | |
2604 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2605 | filename_size = strlen(filename); |
2606 | |
ffaf5031 |
2607 | if(fac->arch) { |
2608 | strncat(filename, "_", PATH_MAX - filename_size); |
2609 | filename_size = strlen(filename); |
2610 | |
2611 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2612 | filename_size = strlen(filename); |
2613 | } |
2614 | |
2d2d14a7 |
2615 | strncat(filename, ".c", PATH_MAX - filename_size); |
2616 | filename_size = strlen(filename); |
2617 | |
2618 | |
2619 | fd = fopen(filename, "w"); |
2620 | if(fd == NULL) { |
2621 | printf("Error opening file %s for writing : %s\n", |
2622 | filename, strerror(errno)); |
2623 | return errno; |
2624 | } |
2625 | |
71e09db9 |
2626 | fprintf(fd, "/*\n"); |
ffaf5031 |
2627 | if(!fac->arch) |
2628 | fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name); |
2629 | else |
2630 | fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch); |
71e09db9 |
2631 | fprintf(fd, " *\n"); |
2632 | fprintf(fd, " * (C) Copyright 2005 - \n"); |
2633 | fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n"); |
2634 | fprintf(fd, " *\n"); |
2635 | fprintf(fd, " * Contains the LTT facility loader.\n"); |
2636 | fprintf(fd, " *\n"); |
2637 | fprintf(fd, " */\n"); |
2638 | fprintf(fd, "\n"); |
2639 | fprintf(fd, "\n"); |
2640 | fprintf(fd, "#include <linux/ltt-facilities.h>\n"); |
2641 | fprintf(fd, "#include <linux/module.h>\n"); |
2642 | fprintf(fd, "#include <linux/init.h>\n"); |
2643 | fprintf(fd, "#include <linux/config.h>\n"); |
ffaf5031 |
2644 | if(!fac->arch) |
2645 | fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name); |
2646 | else |
2647 | fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n", |
2648 | fac->name, fac->arch); |
71e09db9 |
2649 | fprintf(fd, "\n"); |
2650 | fprintf(fd, "\n"); |
2651 | fprintf(fd, "#ifdef CONFIG_LTT\n"); |
2652 | fprintf(fd, "\n"); |
2653 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_SYMBOL);\n"); |
2654 | fprintf(fd, "EXPORT_SYMBOL(LTT_FACILITY_CHECKSUM_SYMBOL);\n"); |
2655 | fprintf(fd, "\n"); |
2656 | fprintf(fd, "static const char ltt_facility_name[] = LTT_FACILITY_NAME;\n"); |
2657 | fprintf(fd, "\n"); |
2658 | fprintf(fd, "#define SYMBOL_STRING(sym) #sym\n"); |
2659 | fprintf(fd, "\n"); |
2660 | fprintf(fd, "static struct ltt_facility facility = {\n"); |
2661 | fprintf(fd, "\t.name = ltt_facility_name,\n"); |
2662 | fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n"); |
2663 | fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n"); |
2664 | fprintf(fd, "\t.symbol = SYMBOL_STRING(LTT_FACILITY_SYMBOL),\n"); |
2665 | fprintf(fd, "};\n"); |
2666 | fprintf(fd, "\n"); |
71e09db9 |
2667 | fprintf(fd, "static int __init facility_init(void)\n"); |
2668 | fprintf(fd, "{\n"); |
2669 | fprintf(fd, "\tprintk(KERN_INFO \"LTT : ltt-facility-%s init in kernel\\n\");\n", fac->name); |
2670 | fprintf(fd, "\n"); |
bd7b8ca6 |
2671 | fprintf(fd, "\tLTT_FACILITY_SYMBOL = ltt_facility_kernel_register(&facility);\n"); |
71e09db9 |
2672 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
2673 | fprintf(fd, "\t\n"); |
2674 | fprintf(fd, "\treturn LTT_FACILITY_SYMBOL;\n"); |
2675 | fprintf(fd, "}\n"); |
71e09db9 |
2676 | fprintf(fd, "\n"); |
bd7b8ca6 |
2677 | fprintf(fd, "#ifndef MODULE\n"); |
ba899d3d |
2678 | fprintf(fd, "__initcall(facility_init);\n"); |
2679 | fprintf(fd, "#else\n"); |
2680 | fprintf(fd, "module_init(facility_init);\n"); |
71e09db9 |
2681 | fprintf(fd, "static void __exit facility_exit(void)\n"); |
2682 | fprintf(fd, "{\n"); |
2683 | fprintf(fd, "\tint err;\n"); |
2684 | fprintf(fd, "\n"); |
bd7b8ca6 |
2685 | fprintf(fd, "\terr = ltt_facility_unregister(LTT_FACILITY_SYMBOL);\n"); |
71e09db9 |
2686 | fprintf(fd, "\tif(err != 0)\n"); |
2687 | fprintf(fd, "\t\tprintk(KERN_ERR \"LTT : Error in unregistering facility.\\n\");\n"); |
2688 | fprintf(fd, "\n"); |
2689 | fprintf(fd, "}\n"); |
71e09db9 |
2690 | fprintf(fd, "module_exit(facility_exit)\n"); |
2691 | fprintf(fd, "\n"); |
71e09db9 |
2692 | fprintf(fd, "MODULE_LICENSE(\"GPL\");\n"); |
2693 | fprintf(fd, "MODULE_AUTHOR(\"Mathieu Desnoyers\");\n"); |
2694 | fprintf(fd, "MODULE_DESCRIPTION(\"Linux Trace Toolkit Facility\");\n"); |
2695 | fprintf(fd, "\n"); |
2696 | fprintf(fd, "#endif //MODULE\n"); |
2697 | fprintf(fd, "\n"); |
2698 | fprintf(fd, "#endif //CONFIG_LTT\n"); |
2d2d14a7 |
2699 | |
2700 | fclose(fd); |
2701 | |
2702 | return 0; |
2703 | } |
2704 | |
bd7b8ca6 |
2705 | int print_loader_c_user(facility_t *fac) |
2706 | { |
2707 | char filename[PATH_MAX]; |
2708 | unsigned int filename_size = 0; |
2709 | FILE *fd; |
2710 | dprintf("%s\n", fac->name); |
2711 | |
2712 | strcpy(filename, "ltt-facility-loader-"); |
2713 | filename_size = strlen(filename); |
2714 | |
2715 | strncat(filename, fac->name, PATH_MAX - filename_size); |
2716 | filename_size = strlen(filename); |
2717 | |
2718 | if(fac->arch) { |
2719 | strncat(filename, "_", PATH_MAX - filename_size); |
2720 | filename_size = strlen(filename); |
2721 | |
2722 | strncat(filename, fac->arch, PATH_MAX - filename_size); |
2723 | filename_size = strlen(filename); |
2724 | } |
2725 | |
2726 | strncat(filename, ".c", PATH_MAX - filename_size); |
2727 | filename_size = strlen(filename); |
2728 | |
2729 | |
2730 | fd = fopen(filename, "w"); |
2731 | if(fd == NULL) { |
2732 | printf("Error opening file %s for writing : %s\n", |
2733 | filename, strerror(errno)); |
2734 | return errno; |
2735 | } |
2736 | |
2737 | fprintf(fd, "/*\n"); |
2738 | if(!fac->arch) |
2739 | fprintf(fd, " * ltt-facility-loader-%s.c\n", fac->name); |
2740 | else |
2741 | fprintf(fd, " * ltt-facility-loader-%s_%s.c\n", fac->name, fac->arch); |
2742 | fprintf(fd, " *\n"); |
2743 | fprintf(fd, " * (C) Copyright 2005 - \n"); |
2744 | fprintf(fd, " * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)\n"); |
2745 | fprintf(fd, " *\n"); |
2746 | fprintf(fd, " * Contains the LTT user space facility loader.\n"); |
2747 | fprintf(fd, " *\n"); |
2748 | fprintf(fd, " */\n"); |
2749 | fprintf(fd, "\n"); |
2750 | fprintf(fd, "\n"); |
2751 | fprintf(fd, "#define LTT_TRACE\n"); |
2752 | fprintf(fd, "#include <error.h>\n"); |
2753 | fprintf(fd, "#include <stdio.h>\n"); |
f389e596 |
2754 | fprintf(fd, "#include <ltt/ltt-usertrace.h>\n"); |
bd7b8ca6 |
2755 | if(!fac->arch) |
2756 | fprintf(fd, "#include \"ltt-facility-loader-%s.h\"\n", fac->name); |
2757 | else |
2758 | fprintf(fd, "#include \"ltt-facility-loader-%s_%s.h\"\n", |
2759 | fac->name, fac->arch); |
2760 | fprintf(fd, "\n"); |
2761 | fprintf(fd, "static struct user_facility_info facility = {\n"); |
2762 | fprintf(fd, "\t.name = LTT_FACILITY_NAME,\n"); |
2763 | fprintf(fd, "\t.num_events = LTT_FACILITY_NUM_EVENTS,\n"); |
2764 | fprintf(fd, "#ifndef LTT_PACK\n"); |
2765 | fprintf(fd, "\t.alignment = sizeof(void*),\n"); |
2766 | fprintf(fd, "#else\n"); |
2767 | fprintf(fd, "\t.alignment = 0,\n"); |
2768 | fprintf(fd, "#endif //LTT_PACK\n"); |
2769 | fprintf(fd, "\t.checksum = LTT_FACILITY_CHECKSUM,\n"); |
2770 | fprintf(fd, "\t.int_size = sizeof(int),\n"); |
2771 | fprintf(fd, "\t.long_size = sizeof(long),\n"); |
2772 | fprintf(fd, "\t.pointer_size = sizeof(void*),\n"); |
2773 | fprintf(fd, "\t.size_t_size = sizeof(size_t)\n"); |
2774 | fprintf(fd, "};\n"); |
2775 | fprintf(fd, "\n"); |
2776 | fprintf(fd, "static void __attribute__((constructor)) __ltt_user_init(void)\n"); |
2777 | fprintf(fd, "{\n"); |
2778 | fprintf(fd, "\tint err;\n"); |
2d6c6b76 |
2779 | fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n"); |
bd7b8ca6 |
2780 | fprintf(fd, "\tprintf(\"LTT : ltt-facility-%s init in userspace\\n\");\n", fac->name); |
2d6c6b76 |
2781 | fprintf(fd, "#endif //LTT_SHOW_DEBUG\n"); |
bd7b8ca6 |
2782 | fprintf(fd, "\n"); |
2783 | fprintf(fd, "\terr = ltt_register_generic(<T_FACILITY_SYMBOL, &facility);\n"); |
2784 | fprintf(fd, "\tLTT_FACILITY_CHECKSUM_SYMBOL = LTT_FACILITY_SYMBOL;\n"); |
2785 | fprintf(fd, "\t\n"); |
2786 | fprintf(fd, "\tif(err) {\n"); |
2d6c6b76 |
2787 | fprintf(fd, "#ifdef LTT_SHOW_DEBUG\n"); |
bd7b8ca6 |
2788 | fprintf(fd, "\t\tperror(\"Error in ltt_register_generic\");\n"); |
2d6c6b76 |
2789 | fprintf(fd, "#endif //LTT_SHOW_DEBUG\n"); |
bd7b8ca6 |
2790 | fprintf(fd, "\t}\n"); |
2791 | fprintf(fd, "}\n"); |
2792 | fprintf(fd, "\n"); |
2793 | |
2794 | fclose(fd); |
2795 | |
2796 | return 0; |
2797 | } |
2798 | |
2d2d14a7 |
2799 | |
2800 | |
92d82357 |
2801 | /* open facility */ |
2802 | /* code taken from ltt_facility_open in ltt/facility.c in lttv */ |
2803 | |
2804 | /***************************************************************************** |
2805 | *Function name |
2806 | * ltt_facility_open : open facilities |
2807 | *Input params |
2808 | * pathname : the path name of the facility |
2809 | * |
2810 | * Open the facility corresponding to the right checksum. |
2811 | * |
2812 | *returns the facility on success, NULL on error. |
2813 | ****************************************************************************/ |
2814 | facility_t *ltt_facility_open(char * pathname) |
2815 | { |
2816 | int ret = 0; |
2817 | char *token; |
2818 | parse_file_t in; |
2819 | facility_t * fac = NULL; |
92d82357 |
2820 | char buffer[BUFFER_SIZE]; |
2821 | int generated = FALSE; |
2822 | |
2823 | in.buffer = &(buffer[0]); |
2824 | in.lineno = 0; |
2825 | in.error = error_callback; |
2826 | in.name = pathname; |
2827 | in.unget = 0; |
2828 | |
2829 | in.fp = fopen(in.name, "r"); |
2830 | if(in.fp == NULL) { |
2831 | ret = 1; |
2832 | goto open_error; |
2833 | } |
2834 | |
2835 | while(1){ |
2836 | token = getToken(&in); |
2837 | if(in.type == ENDFILE) break; |
2838 | |
2839 | if(generated) { |
2840 | printf("More than one facility in the file. Only using the first one.\n"); |
2841 | break; |
2842 | } |
2843 | |
2844 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
2845 | token = getName(&in); |
f389e596 |
2846 | if(strcmp(token, "?")) in.error(&in,"not a facility file"); |
2847 | token = getName(&in); |
2848 | if(strcmp(token, "xml")) in.error(&in,"not a facility file"); |
2849 | token = getName(&in); |
2850 | if(strcmp(token, "version")) in.error(&in,"not a facility file"); |
2851 | token = getName(&in); |
2852 | if(strcmp(token, "=")) in.error(&in,"not a facility file"); |
2853 | token = getQuotedString(&in); |
2854 | if(strcmp(token, "1.0")) in.error(&in,"not a facility file"); |
2855 | token = getName(&in); |
2856 | if(strcmp(token, "?")) in.error(&in,"not a facility file"); |
2857 | token = getToken(&in); |
2858 | if(strcmp(token, ">")) in.error(&in,"not a facility file"); |
92d82357 |
2859 | |
f389e596 |
2860 | token = getName(&in); |
2861 | if(strcmp(token, "<")) in.error(&in,"not a facility file"); |
2862 | token = getName(&in); |
92d82357 |
2863 | if(strcmp("facility",token) == 0) { |
2864 | fac = malloc(sizeof(facility_t)); |
2865 | fac->name = NULL; |
2866 | fac->description = NULL; |
2867 | sequence_init(&(fac->events)); |
2868 | table_init(&(fac->named_types)); |
2869 | sequence_init(&(fac->unnamed_types)); |
2870 | |
2871 | parseFacility(&in, fac); |
2872 | |
2873 | //check if any namedType is not defined |
2874 | checkNamedTypesImplemented(&fac->named_types); |
2875 | |
2e415130 |
2876 | generateChecksum(fac->name, &fac->checksum, &fac->events); |
a67cd958 |
2877 | |
92d82357 |
2878 | generated = TRUE; |
2879 | } |
2880 | else { |
2881 | printf("facility token was expected in file %s\n", in.name); |
2882 | ret = 1; |
2883 | goto parse_error; |
2884 | } |
2885 | } |
2886 | |
2887 | parse_error: |
2888 | fclose(in.fp); |
2889 | open_error: |
2890 | |
2891 | if(!generated) { |
2892 | printf("Cannot find facility %s\n", pathname); |
2893 | fac = NULL; |
2894 | } |
2895 | |
2896 | return fac; |
2897 | } |
2898 | |
2899 | /* Close the facility */ |
2900 | void ltt_facility_close(facility_t *fac) |
2901 | { |
2902 | free(fac->name); |
2903 | free(fac->capname); |
2904 | free(fac->description); |
2905 | freeEvents(&fac->events); |
2906 | sequence_dispose(&fac->events); |
2907 | freeNamedType(&fac->named_types); |
2908 | table_dispose(&fac->named_types); |
2909 | freeTypes(&fac->unnamed_types); |
2910 | sequence_dispose(&fac->unnamed_types); |
2911 | free(fac); |
2912 | } |
2913 | |
2914 | |
2915 | /* Show help */ |
2916 | void show_help(int argc, char ** argv) |
2917 | { |
2918 | printf("Genevent help : \n"); |
2919 | printf("\n"); |
2920 | printf("Use %s name.xml\n", argv[0]); |
2921 | printf("to create :\n"); |
2922 | printf("ltt-facility-name.h\n"); |
2923 | printf("ltt-facility-id-name.h\n"); |
2924 | printf("ltt-facility-loader-name.h\n"); |
2925 | printf("ltt-facility-loader-name.c\n"); |
2926 | printf("In the current directory.\n"); |
2927 | printf("\n"); |
2928 | } |
2929 | |
2930 | /* Parse program arguments */ |
2931 | /* Return values : |
2932 | * 0 : continue program |
2933 | * -1 : stop program, return 0 |
2934 | * > 0 : stop program, return value as exit. |
2935 | */ |
2936 | int check_args(int argc, char **argv) |
2937 | { |
2938 | if(argc < 2) { |
2939 | printf("Not enough arguments\n"); |
2940 | show_help(argc, argv); |
2941 | return EINVAL; |
2942 | } |
2943 | |
2944 | if(strcmp(argv[1], "-h") == 0) { |
2945 | show_help(argc, argv); |
2946 | return -1; |
2947 | } |
2948 | |
2949 | return 0; |
2950 | } |
2951 | |
2952 | int main(int argc, char **argv) |
2953 | { |
2954 | int err = 0; |
2955 | facility_t *fac; |
2956 | |
2957 | err = check_args(argc, argv); |
2958 | if(err > 0) return err; |
2959 | else if(err < 0) return 0; |
2960 | |
2961 | /* open the facility */ |
2962 | fac = ltt_facility_open(argv[1]); |
2963 | if(fac == NULL) { |
2964 | printf("Error opening file %s for reading : %s\n", |
2965 | argv[1], strerror(errno)); |
2966 | return errno; |
2967 | } |
2968 | |
2969 | /* generate the output C files */ |
2970 | |
2971 | |
2d2d14a7 |
2972 | /* ltt-facility-name.h : main logging header. |
2973 | * log_header */ |
2974 | err = print_log_header(fac); |
2975 | if(err) return err; |
92d82357 |
2976 | |
2d2d14a7 |
2977 | /* ltt-facility-id-name.h : facility id. |
2978 | * log_id_header */ |
2979 | err = print_id_header(fac); |
2980 | if(err) return err; |
92d82357 |
2981 | |
2d2d14a7 |
2982 | /* ltt-facility-loader-name.h : facility specific loader info. |
2983 | * loader_header */ |
bd7b8ca6 |
2984 | if(!fac->user) |
2985 | err = print_loader_header(fac); |
2986 | else |
2987 | err = print_loader_header_user(fac); |
2d2d14a7 |
2988 | if(err) return err; |
2989 | |
2990 | /* ltt-facility-loader-name.c : generic faciilty loader |
2991 | * loader_c */ |
bd7b8ca6 |
2992 | if(!fac->user) |
2993 | err = print_loader_c(fac); |
2994 | else |
2995 | err = print_loader_c_user(fac); |
2d2d14a7 |
2996 | if(err) return err; |
92d82357 |
2997 | |
2998 | /* close the facility */ |
2999 | ltt_facility_close(fac); |
3000 | |
3001 | return 0; |
3002 | } |
3003 | |
3004 | |