Commit | Line | Data |
---|---|---|
8d938dbd PMF |
1 | /* |
2 | * LTTng serializing code. | |
3 | * | |
4 | * Copyright Mathieu Desnoyers, March 2007. | |
5 | * | |
22cb38fb PMF |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | * | |
8d938dbd PMF |
20 | * |
21 | * See this discussion about weirdness about passing va_list and then va_list to | |
22 | * functions. (related to array argument passing). va_list seems to be | |
23 | * implemented as an array on x86_64, but not on i386... This is why we pass a | |
24 | * va_list * to ltt_vtrace. | |
25 | */ | |
26 | ||
27 | #include <stdarg.h> | |
8d938dbd PMF |
28 | #include <string.h> |
29 | #include <stdint.h> | |
b4041302 | 30 | #include <stdio.h> |
769d0157 | 31 | |
fbca6b62 | 32 | #include <ust/kernelcompat.h> |
769d0157 | 33 | #define _LGPL_SOURCE |
b7ea1a1c PMF |
34 | #include <urcu-bp.h> |
35 | #include <urcu/rculist.h> | |
769d0157 | 36 | |
b5b073e2 | 37 | #include "buffers.h" |
c93858f1 | 38 | #include "tracer.h" |
769d0157 | 39 | //#include "list.h" |
ba6459ba | 40 | #include "usterr.h" |
8d938dbd PMF |
41 | |
42 | enum ltt_type { | |
43 | LTT_TYPE_SIGNED_INT, | |
44 | LTT_TYPE_UNSIGNED_INT, | |
45 | LTT_TYPE_STRING, | |
46 | LTT_TYPE_NONE, | |
47 | }; | |
48 | ||
49 | #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1) | |
50 | ||
51 | /* | |
52 | * Inspired from vsnprintf | |
53 | * | |
54 | * The serialization format string supports the basic printf format strings. | |
55 | * In addition, it defines new formats that can be used to serialize more | |
56 | * complex/non portable data structures. | |
57 | * | |
58 | * Typical use: | |
59 | * | |
60 | * field_name %ctype | |
61 | * field_name #tracetype %ctype | |
62 | * field_name #tracetype %ctype1 %ctype2 ... | |
63 | * | |
64 | * A conversion is performed between format string types supported by GCC and | |
65 | * the trace type requested. GCC type is used to perform type checking on format | |
66 | * strings. Trace type is used to specify the exact binary representation | |
67 | * in the trace. A mapping is done between one or more GCC types to one trace | |
68 | * type. Sign extension, if required by the conversion, is performed following | |
69 | * the trace type. | |
70 | * | |
71 | * If a gcc format is not declared with a trace format, the gcc format is | |
72 | * also used as binary representation in the trace. | |
73 | * | |
74 | * Strings are supported with %s. | |
75 | * A single tracetype (sequence) can take multiple c types as parameter. | |
76 | * | |
77 | * c types: | |
78 | * | |
79 | * see printf(3). | |
80 | * | |
81 | * Note: to write a uint32_t in a trace, the following expression is recommended | |
82 | * si it can be portable: | |
83 | * | |
84 | * ("#4u%lu", (unsigned long)var) | |
85 | * | |
86 | * trace types: | |
87 | * | |
88 | * Serialization specific formats : | |
89 | * | |
90 | * Fixed size integers | |
91 | * #1u writes uint8_t | |
92 | * #2u writes uint16_t | |
93 | * #4u writes uint32_t | |
94 | * #8u writes uint64_t | |
95 | * #1d writes int8_t | |
96 | * #2d writes int16_t | |
97 | * #4d writes int32_t | |
98 | * #8d writes int64_t | |
99 | * i.e.: | |
100 | * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu | |
101 | * | |
102 | * * Attributes: | |
103 | * | |
104 | * n: (for network byte order) | |
105 | * #ntracetype%ctype | |
106 | * is written in the trace in network byte order. | |
107 | * | |
108 | * i.e.: #bn4u%lu, #n%lu, #b%u | |
109 | * | |
110 | * TODO (eventually) | |
111 | * Variable length sequence | |
112 | * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems | |
113 | * In the trace: | |
114 | * #a specifies that this is a sequence | |
115 | * #tracetype1 is the type of elements in the sequence | |
116 | * #tracetype2 is the type of the element count | |
117 | * GCC input: | |
118 | * array_ptr is a pointer to an array that contains members of size | |
119 | * elem_size. | |
120 | * num_elems is the number of elements in the array. | |
121 | * i.e.: #a #lu #lu %p %lu %u | |
122 | * | |
123 | * Callback | |
124 | * #k callback (taken from the probe data) | |
125 | * The following % arguments are exepected by the callback | |
126 | * | |
127 | * i.e.: #a #lu #lu #k %p | |
128 | * | |
129 | * Note: No conversion is done from floats to integers, nor from integers to | |
130 | * floats between c types and trace types. float conversion from double to float | |
131 | * or from float to double is also not supported. | |
132 | * | |
133 | * REMOVE | |
134 | * %*b expects sizeof(data), data | |
135 | * where sizeof(data) is 1, 2, 4 or 8 | |
136 | * | |
137 | * Fixed length struct, union or array. | |
138 | * FIXME: unable to extract those sizes statically. | |
139 | * %*r expects sizeof(*ptr), ptr | |
140 | * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr | |
141 | * struct and unions removed. | |
142 | * Fixed length array: | |
143 | * [%p]#a[len #tracetype] | |
144 | * i.e.: [%p]#a[12 #lu] | |
145 | * | |
146 | * Variable length sequence | |
147 | * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr | |
148 | * where elem_num is the number of elements in the sequence | |
149 | */ | |
150 | static inline const char *parse_trace_type(const char *fmt, | |
151 | char *trace_size, enum ltt_type *trace_type, | |
152 | unsigned long *attributes) | |
153 | { | |
154 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ | |
155 | /* 'z' support added 23/7/1999 S.H. */ | |
156 | /* 'z' changed to 'Z' --davidm 1/25/99 */ | |
157 | /* 't' added for ptrdiff_t */ | |
158 | ||
159 | /* parse attributes. */ | |
160 | repeat: | |
161 | switch (*fmt) { | |
162 | case 'n': | |
163 | *attributes |= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER; | |
164 | ++fmt; | |
165 | goto repeat; | |
166 | } | |
167 | ||
168 | /* get the conversion qualifier */ | |
169 | qualifier = -1; | |
170 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | |
171 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't' || | |
172 | *fmt == 'S' || *fmt == '1' || *fmt == '2' || | |
173 | *fmt == '4' || *fmt == 8) { | |
174 | qualifier = *fmt; | |
175 | ++fmt; | |
176 | if (qualifier == 'l' && *fmt == 'l') { | |
177 | qualifier = 'L'; | |
178 | ++fmt; | |
179 | } | |
180 | } | |
181 | ||
182 | switch (*fmt) { | |
183 | case 'c': | |
184 | *trace_type = LTT_TYPE_UNSIGNED_INT; | |
185 | *trace_size = sizeof(unsigned char); | |
186 | goto parse_end; | |
187 | case 's': | |
188 | *trace_type = LTT_TYPE_STRING; | |
189 | goto parse_end; | |
190 | case 'p': | |
191 | *trace_type = LTT_TYPE_UNSIGNED_INT; | |
192 | *trace_size = sizeof(void *); | |
193 | goto parse_end; | |
194 | case 'd': | |
195 | case 'i': | |
196 | *trace_type = LTT_TYPE_SIGNED_INT; | |
197 | break; | |
198 | case 'o': | |
199 | case 'u': | |
200 | case 'x': | |
201 | case 'X': | |
202 | *trace_type = LTT_TYPE_UNSIGNED_INT; | |
203 | break; | |
204 | default: | |
205 | if (!*fmt) | |
206 | --fmt; | |
207 | goto parse_end; | |
208 | } | |
209 | switch (qualifier) { | |
210 | case 'L': | |
211 | *trace_size = sizeof(long long); | |
212 | break; | |
213 | case 'l': | |
214 | *trace_size = sizeof(long); | |
215 | break; | |
216 | case 'Z': | |
217 | case 'z': | |
218 | *trace_size = sizeof(size_t); | |
219 | break; | |
220 | //ust// case 't': | |
221 | //ust// *trace_size = sizeof(ptrdiff_t); | |
222 | //ust// break; | |
223 | case 'h': | |
224 | *trace_size = sizeof(short); | |
225 | break; | |
226 | case '1': | |
227 | *trace_size = sizeof(uint8_t); | |
228 | break; | |
229 | case '2': | |
230 | *trace_size = sizeof(uint16_t); | |
231 | break; | |
232 | case '4': | |
233 | *trace_size = sizeof(uint32_t); | |
234 | break; | |
235 | case '8': | |
236 | *trace_size = sizeof(uint64_t); | |
237 | break; | |
238 | default: | |
239 | *trace_size = sizeof(int); | |
240 | } | |
241 | ||
242 | parse_end: | |
243 | return fmt; | |
244 | } | |
245 | ||
246 | /* | |
247 | * Restrictions: | |
248 | * Field width and precision are *not* supported. | |
249 | * %n not supported. | |
250 | */ | |
251 | static inline const char *parse_c_type(const char *fmt, | |
252 | char *c_size, enum ltt_type *c_type) | |
253 | { | |
254 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ | |
255 | /* 'z' support added 23/7/1999 S.H. */ | |
256 | /* 'z' changed to 'Z' --davidm 1/25/99 */ | |
257 | /* 't' added for ptrdiff_t */ | |
258 | ||
259 | /* process flags : ignore standard print formats for now. */ | |
260 | repeat: | |
261 | switch (*fmt) { | |
262 | case '-': | |
263 | case '+': | |
264 | case ' ': | |
265 | case '#': | |
266 | case '0': | |
267 | ++fmt; | |
268 | goto repeat; | |
269 | } | |
270 | ||
271 | /* get the conversion qualifier */ | |
272 | qualifier = -1; | |
273 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || | |
274 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't' || | |
275 | *fmt == 'S') { | |
276 | qualifier = *fmt; | |
277 | ++fmt; | |
278 | if (qualifier == 'l' && *fmt == 'l') { | |
279 | qualifier = 'L'; | |
280 | ++fmt; | |
281 | } | |
282 | } | |
283 | ||
284 | switch (*fmt) { | |
285 | case 'c': | |
286 | *c_type = LTT_TYPE_UNSIGNED_INT; | |
287 | *c_size = sizeof(unsigned char); | |
288 | goto parse_end; | |
289 | case 's': | |
290 | *c_type = LTT_TYPE_STRING; | |
291 | goto parse_end; | |
292 | case 'p': | |
293 | *c_type = LTT_TYPE_UNSIGNED_INT; | |
294 | *c_size = sizeof(void *); | |
295 | goto parse_end; | |
296 | case 'd': | |
297 | case 'i': | |
298 | *c_type = LTT_TYPE_SIGNED_INT; | |
299 | break; | |
300 | case 'o': | |
301 | case 'u': | |
302 | case 'x': | |
303 | case 'X': | |
304 | *c_type = LTT_TYPE_UNSIGNED_INT; | |
305 | break; | |
306 | default: | |
307 | if (!*fmt) | |
308 | --fmt; | |
309 | goto parse_end; | |
310 | } | |
311 | switch (qualifier) { | |
312 | case 'L': | |
313 | *c_size = sizeof(long long); | |
314 | break; | |
315 | case 'l': | |
316 | *c_size = sizeof(long); | |
317 | break; | |
318 | case 'Z': | |
319 | case 'z': | |
320 | *c_size = sizeof(size_t); | |
321 | break; | |
322 | //ust// case 't': | |
323 | //ust// *c_size = sizeof(ptrdiff_t); | |
324 | //ust// break; | |
325 | case 'h': | |
326 | *c_size = sizeof(short); | |
327 | break; | |
328 | default: | |
329 | *c_size = sizeof(int); | |
330 | } | |
331 | ||
332 | parse_end: | |
333 | return fmt; | |
334 | } | |
335 | ||
b5b073e2 | 336 | static inline size_t serialize_trace_data(struct ust_buffer *buf, |
8d938dbd PMF |
337 | size_t buf_offset, |
338 | char trace_size, enum ltt_type trace_type, | |
339 | char c_size, enum ltt_type c_type, | |
340 | int *largest_align, va_list *args) | |
341 | { | |
342 | union { | |
343 | unsigned long v_ulong; | |
344 | uint64_t v_uint64; | |
345 | struct { | |
346 | const char *s; | |
347 | size_t len; | |
348 | } v_string; | |
349 | } tmp; | |
350 | ||
351 | /* | |
352 | * Be careful about sign extension here. | |
353 | * Sign extension is done with the destination (trace) type. | |
354 | */ | |
355 | switch (trace_type) { | |
356 | case LTT_TYPE_SIGNED_INT: | |
357 | switch (c_size) { | |
358 | case 1: | |
359 | tmp.v_ulong = (long)(int8_t)va_arg(*args, int); | |
360 | break; | |
361 | case 2: | |
362 | tmp.v_ulong = (long)(int16_t)va_arg(*args, int); | |
363 | break; | |
364 | case 4: | |
365 | tmp.v_ulong = (long)(int32_t)va_arg(*args, int); | |
366 | break; | |
367 | case 8: | |
368 | tmp.v_uint64 = va_arg(*args, int64_t); | |
369 | break; | |
370 | default: | |
371 | BUG(); | |
372 | } | |
373 | break; | |
374 | case LTT_TYPE_UNSIGNED_INT: | |
375 | switch (c_size) { | |
376 | case 1: | |
377 | tmp.v_ulong = (unsigned long)(uint8_t) | |
378 | va_arg(*args, unsigned int); | |
379 | break; | |
380 | case 2: | |
381 | tmp.v_ulong = (unsigned long)(uint16_t) | |
382 | va_arg(*args, unsigned int); | |
383 | break; | |
384 | case 4: | |
385 | tmp.v_ulong = (unsigned long)(uint32_t) | |
386 | va_arg(*args, unsigned int); | |
387 | break; | |
388 | case 8: | |
389 | tmp.v_uint64 = va_arg(*args, uint64_t); | |
390 | break; | |
391 | default: | |
392 | BUG(); | |
393 | } | |
394 | break; | |
395 | case LTT_TYPE_STRING: | |
396 | tmp.v_string.s = va_arg(*args, const char *); | |
397 | if ((unsigned long)tmp.v_string.s < PAGE_SIZE) | |
398 | tmp.v_string.s = "<NULL>"; | |
399 | tmp.v_string.len = strlen(tmp.v_string.s)+1; | |
400 | if (buf) | |
b5b073e2 | 401 | ust_buffers_write(buf, buf_offset, tmp.v_string.s, |
8d938dbd PMF |
402 | tmp.v_string.len); |
403 | buf_offset += tmp.v_string.len; | |
404 | goto copydone; | |
405 | default: | |
406 | BUG(); | |
407 | } | |
408 | ||
409 | /* | |
410 | * If trace_size is lower or equal to 4 bytes, there is no sign | |
411 | * extension to do because we are already encoded in a long. Therefore, | |
412 | * we can combine signed and unsigned ops. 4 bytes float also works | |
413 | * with this, because we do a simple copy of 4 bytes into 4 bytes | |
414 | * without manipulation (and we do not support conversion from integers | |
415 | * to floats). | |
416 | * It is also the case if c_size is 8 bytes, which is the largest | |
417 | * possible integer. | |
418 | */ | |
419 | if (ltt_get_alignment()) { | |
420 | buf_offset += ltt_align(buf_offset, trace_size); | |
421 | if (largest_align) | |
422 | *largest_align = max_t(int, *largest_align, trace_size); | |
423 | } | |
424 | if (trace_size <= 4 || c_size == 8) { | |
425 | if (buf) { | |
426 | switch (trace_size) { | |
427 | case 1: | |
428 | if (c_size == 8) | |
b5b073e2 | 429 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
430 | (uint8_t[]){ (uint8_t)tmp.v_uint64 }, |
431 | sizeof(uint8_t)); | |
432 | else | |
b5b073e2 | 433 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
434 | (uint8_t[]){ (uint8_t)tmp.v_ulong }, |
435 | sizeof(uint8_t)); | |
436 | break; | |
437 | case 2: | |
438 | if (c_size == 8) | |
b5b073e2 | 439 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
440 | (uint16_t[]){ (uint16_t)tmp.v_uint64 }, |
441 | sizeof(uint16_t)); | |
442 | else | |
b5b073e2 | 443 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
444 | (uint16_t[]){ (uint16_t)tmp.v_ulong }, |
445 | sizeof(uint16_t)); | |
446 | break; | |
447 | case 4: | |
448 | if (c_size == 8) | |
b5b073e2 | 449 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
450 | (uint32_t[]){ (uint32_t)tmp.v_uint64 }, |
451 | sizeof(uint32_t)); | |
452 | else | |
b5b073e2 | 453 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
454 | (uint32_t[]){ (uint32_t)tmp.v_ulong }, |
455 | sizeof(uint32_t)); | |
456 | break; | |
457 | case 8: | |
458 | /* | |
459 | * c_size cannot be other than 8 here because | |
460 | * trace_size > 4. | |
461 | */ | |
b5b073e2 | 462 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
463 | (uint64_t[]){ (uint64_t)tmp.v_uint64 }, |
464 | sizeof(uint64_t)); | |
465 | break; | |
466 | default: | |
467 | BUG(); | |
468 | } | |
469 | } | |
470 | buf_offset += trace_size; | |
471 | goto copydone; | |
472 | } else { | |
473 | /* | |
474 | * Perform sign extension. | |
475 | */ | |
476 | if (buf) { | |
477 | switch (trace_type) { | |
478 | case LTT_TYPE_SIGNED_INT: | |
b5b073e2 | 479 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
480 | (int64_t[]){ (int64_t)tmp.v_ulong }, |
481 | sizeof(int64_t)); | |
482 | break; | |
483 | case LTT_TYPE_UNSIGNED_INT: | |
b5b073e2 | 484 | ust_buffers_write(buf, buf_offset, |
8d938dbd PMF |
485 | (uint64_t[]){ (uint64_t)tmp.v_ulong }, |
486 | sizeof(uint64_t)); | |
487 | break; | |
488 | default: | |
489 | BUG(); | |
490 | } | |
491 | } | |
492 | buf_offset += trace_size; | |
493 | goto copydone; | |
494 | } | |
495 | ||
496 | copydone: | |
497 | return buf_offset; | |
498 | } | |
499 | ||
b4041302 PMF |
500 | static notrace void skip_space(const char **ps) |
501 | { | |
502 | while(**ps == ' ') | |
503 | (*ps)++; | |
504 | } | |
505 | ||
506 | static notrace void copy_token(char **out, const char **in) | |
507 | { | |
508 | while(**in != ' ' && **in != '\0') { | |
509 | **out = **in; | |
510 | (*out)++; | |
511 | (*in)++; | |
512 | } | |
513 | } | |
514 | ||
515 | /* serialize_to_text | |
516 | * | |
517 | * Given a format string and a va_list of arguments, convert them to a | |
518 | * human-readable string. | |
519 | * | |
520 | * @outbuf: the buffer to output the string to | |
521 | * @bufsize: the max size that can be used in outbuf | |
522 | * @fmt: the marker format string | |
523 | * @ap: a va_list that contains the arguments corresponding to fmt | |
524 | * | |
525 | * Return value: the number of chars that have been put in outbuf, excluding | |
526 | * the final \0, or, if the buffer was too small, the number of chars that | |
527 | * would have been written in outbuf if it had been large enough. | |
528 | * | |
529 | * outbuf may be NULL. The return value may then be used be allocate an | |
530 | * appropriate outbuf. | |
531 | * | |
532 | */ | |
533 | ||
534 | notrace | |
535 | int serialize_to_text(char *outbuf, int bufsize, const char *fmt, va_list ap) | |
536 | { | |
537 | int fmt_len = strlen(fmt); | |
538 | char *new_fmt = alloca(fmt_len + 1); | |
539 | const char *orig_fmt_p = fmt; | |
540 | char *new_fmt_p = new_fmt; | |
541 | char false_buf; | |
542 | int result; | |
543 | enum { none, cfmt, tracefmt, argname } prev_token = none; | |
544 | ||
545 | while(*orig_fmt_p != '\0') { | |
546 | if(*orig_fmt_p == '%') { | |
547 | prev_token = cfmt; | |
548 | copy_token(&new_fmt_p, &orig_fmt_p); | |
549 | } | |
550 | else if(*orig_fmt_p == '#') { | |
551 | prev_token = tracefmt; | |
552 | do { | |
553 | orig_fmt_p++; | |
554 | } while(*orig_fmt_p != ' ' && *orig_fmt_p != '\0'); | |
555 | } | |
556 | else if(*orig_fmt_p == ' ') { | |
557 | if(prev_token == argname) { | |
558 | *new_fmt_p = '='; | |
559 | new_fmt_p++; | |
560 | } | |
561 | else if(prev_token == cfmt) { | |
562 | *new_fmt_p = ' '; | |
563 | new_fmt_p++; | |
564 | } | |
565 | ||
566 | skip_space(&orig_fmt_p); | |
567 | } | |
568 | else { | |
569 | prev_token = argname; | |
570 | copy_token(&new_fmt_p, &orig_fmt_p); | |
571 | } | |
572 | } | |
573 | ||
574 | *new_fmt_p = '\0'; | |
575 | ||
576 | if(outbuf == NULL) { | |
577 | /* use this false_buffer for compatibility with pre-C99 */ | |
578 | outbuf = &false_buf; | |
579 | bufsize = 1; | |
580 | } | |
581 | result = vsnprintf(outbuf, bufsize, new_fmt, ap); | |
582 | ||
583 | return result; | |
584 | } | |
585 | ||
b5b073e2 | 586 | notrace size_t ltt_serialize_data(struct ust_buffer *buf, size_t buf_offset, |
8d938dbd PMF |
587 | struct ltt_serialize_closure *closure, |
588 | void *serialize_private, int *largest_align, | |
589 | const char *fmt, va_list *args) | |
590 | { | |
591 | char trace_size = 0, c_size = 0; /* | |
592 | * 0 (unset), 1, 2, 4, 8 bytes. | |
593 | */ | |
594 | enum ltt_type trace_type = LTT_TYPE_NONE, c_type = LTT_TYPE_NONE; | |
595 | unsigned long attributes = 0; | |
596 | ||
597 | for (; *fmt ; ++fmt) { | |
598 | switch (*fmt) { | |
599 | case '#': | |
600 | /* tracetypes (#) */ | |
601 | ++fmt; /* skip first '#' */ | |
602 | if (*fmt == '#') /* Escaped ## */ | |
603 | break; | |
604 | attributes = 0; | |
605 | fmt = parse_trace_type(fmt, &trace_size, &trace_type, | |
606 | &attributes); | |
607 | break; | |
608 | case '%': | |
609 | /* c types (%) */ | |
610 | ++fmt; /* skip first '%' */ | |
611 | if (*fmt == '%') /* Escaped %% */ | |
612 | break; | |
613 | fmt = parse_c_type(fmt, &c_size, &c_type); | |
614 | /* | |
615 | * Output c types if no trace types has been | |
616 | * specified. | |
617 | */ | |
618 | if (!trace_size) | |
619 | trace_size = c_size; | |
620 | if (trace_type == LTT_TYPE_NONE) | |
621 | trace_type = c_type; | |
622 | if (c_type == LTT_TYPE_STRING) | |
623 | trace_type = LTT_TYPE_STRING; | |
624 | /* perform trace write */ | |
625 | buf_offset = serialize_trace_data(buf, | |
626 | buf_offset, trace_size, | |
627 | trace_type, c_size, c_type, | |
628 | largest_align, args); | |
629 | trace_size = 0; | |
630 | c_size = 0; | |
631 | trace_type = LTT_TYPE_NONE; | |
632 | c_size = LTT_TYPE_NONE; | |
633 | attributes = 0; | |
634 | break; | |
635 | /* default is to skip the text, doing nothing */ | |
636 | } | |
637 | } | |
638 | return buf_offset; | |
639 | } | |
8d938dbd PMF |
640 | |
641 | /* | |
642 | * Calculate data size | |
643 | * Assume that the padding for alignment starts at a sizeof(void *) address. | |
644 | */ | |
645 | static notrace size_t ltt_get_data_size(struct ltt_serialize_closure *closure, | |
646 | void *serialize_private, int *largest_align, | |
647 | const char *fmt, va_list *args) | |
648 | { | |
649 | ltt_serialize_cb cb = closure->callbacks[0]; | |
650 | closure->cb_idx = 0; | |
651 | return (size_t)cb(NULL, 0, closure, serialize_private, | |
652 | largest_align, fmt, args); | |
653 | } | |
654 | ||
655 | static notrace | |
b5b073e2 | 656 | void ltt_write_event_data(struct ust_buffer *buf, size_t buf_offset, |
8d938dbd PMF |
657 | struct ltt_serialize_closure *closure, |
658 | void *serialize_private, int largest_align, | |
659 | const char *fmt, va_list *args) | |
660 | { | |
661 | ltt_serialize_cb cb = closure->callbacks[0]; | |
662 | closure->cb_idx = 0; | |
663 | buf_offset += ltt_align(buf_offset, largest_align); | |
664 | cb(buf, buf_offset, closure, serialize_private, NULL, fmt, args); | |
665 | } | |
666 | ||
667 | ||
668 | notrace void ltt_vtrace(const struct marker *mdata, void *probe_data, | |
75667d04 PMF |
669 | struct registers *regs, void *call_data, |
670 | const char *fmt, va_list *args) | |
8d938dbd PMF |
671 | { |
672 | int largest_align, ret; | |
673 | struct ltt_active_marker *pdata; | |
674 | uint16_t eID; | |
675 | size_t data_size, slot_size; | |
676 | unsigned int chan_index; | |
b5b073e2 | 677 | struct ust_channel *channel; |
8d938dbd | 678 | struct ltt_trace_struct *trace, *dest_trace = NULL; |
b5b073e2 | 679 | struct ust_buffer *buf; |
8d938dbd | 680 | void *transport_data; |
1ae7f074 | 681 | u64 tsc; |
8d938dbd PMF |
682 | long buf_offset; |
683 | va_list args_copy; | |
684 | struct ltt_serialize_closure closure; | |
685 | struct ltt_probe_private_data *private_data = call_data; | |
686 | void *serialize_private = NULL; | |
4268fdcd | 687 | //ust// int cpu; |
8d938dbd PMF |
688 | unsigned int rflags; |
689 | ||
690 | /* | |
691 | * This test is useful for quickly exiting static tracing when no trace | |
692 | * is active. We expect to have an active trace when we get here. | |
693 | */ | |
694 | if (unlikely(ltt_traces.num_active_traces == 0)) | |
695 | return; | |
696 | ||
6cb88bc0 | 697 | rcu_read_lock(); //ust// rcu_read_lock_sched_notrace(); |
4268fdcd | 698 | //ust// cpu = smp_processor_id(); |
8d938dbd PMF |
699 | //ust// __get_cpu_var(ltt_nesting)++; |
700 | ltt_nesting++; | |
701 | ||
702 | pdata = (struct ltt_active_marker *)probe_data; | |
703 | eID = mdata->event_id; | |
704 | chan_index = mdata->channel_id; | |
705 | closure.callbacks = pdata->probe->callbacks; | |
706 | ||
707 | if (unlikely(private_data)) { | |
708 | dest_trace = private_data->trace; | |
709 | if (private_data->serializer) | |
710 | closure.callbacks = &private_data->serializer; | |
711 | serialize_private = private_data->serialize_private; | |
712 | } | |
713 | ||
714 | va_copy(args_copy, *args); | |
715 | /* | |
716 | * Assumes event payload to start on largest_align alignment. | |
717 | */ | |
718 | largest_align = 1; /* must be non-zero for ltt_align */ | |
719 | data_size = ltt_get_data_size(&closure, serialize_private, | |
720 | &largest_align, fmt, &args_copy); | |
721 | largest_align = min_t(int, largest_align, sizeof(void *)); | |
722 | va_end(args_copy); | |
723 | ||
724 | /* Iterate on each trace */ | |
725 | list_for_each_entry_rcu(trace, <t_traces.head, list) { | |
726 | /* | |
727 | * Expect the filter to filter out events. If we get here, | |
728 | * we went through tracepoint activation as a first step. | |
729 | */ | |
730 | if (unlikely(dest_trace && trace != dest_trace)) | |
731 | continue; | |
732 | if (unlikely(!trace->active)) | |
733 | continue; | |
734 | if (unlikely(!ltt_run_filter(trace, eID))) | |
735 | continue; | |
736 | #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE | |
737 | rflags = LTT_RFLAG_ID_SIZE; | |
738 | #else | |
739 | if (unlikely(eID >= LTT_FREE_EVENTS)) | |
740 | rflags = LTT_RFLAG_ID; | |
741 | else | |
742 | rflags = 0; | |
743 | #endif | |
744 | /* | |
745 | * Skip channels added after trace creation. | |
746 | */ | |
747 | if (unlikely(chan_index >= trace->nr_channels)) | |
748 | continue; | |
749 | channel = &trace->channels[chan_index]; | |
750 | if (!channel->active) | |
751 | continue; | |
752 | ||
753 | /* reserve space : header and data */ | |
754 | ret = ltt_reserve_slot(trace, channel, &transport_data, | |
755 | data_size, &slot_size, &buf_offset, | |
756 | &tsc, &rflags, | |
c1dea0b3 | 757 | largest_align); |
8d938dbd PMF |
758 | if (unlikely(ret < 0)) |
759 | continue; /* buffer full */ | |
760 | ||
761 | va_copy(args_copy, *args); | |
762 | /* FIXME : could probably encapsulate transport better. */ | |
763 | //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu]; | |
b5b073e2 | 764 | buf = channel->buf; |
8d938dbd PMF |
765 | /* Out-of-order write : header and data */ |
766 | buf_offset = ltt_write_event_header(trace, | |
b5b073e2 | 767 | buf, buf_offset, |
8d938dbd PMF |
768 | eID, data_size, tsc, rflags); |
769 | ltt_write_event_data(buf, buf_offset, &closure, | |
770 | serialize_private, | |
771 | largest_align, fmt, &args_copy); | |
772 | va_end(args_copy); | |
773 | /* Out-of-order commit */ | |
774 | ltt_commit_slot(channel, &transport_data, buf_offset, | |
8431032f | 775 | data_size, slot_size); |
08230db7 | 776 | DBG("just commited event at offset %ld and size %zd", buf_offset, slot_size); |
8d938dbd PMF |
777 | } |
778 | //ust// __get_cpu_var(ltt_nesting)--; | |
779 | ltt_nesting--; | |
6cb88bc0 | 780 | rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace(); |
8d938dbd | 781 | } |
8d938dbd PMF |
782 | |
783 | notrace void ltt_trace(const struct marker *mdata, void *probe_data, | |
75667d04 PMF |
784 | struct registers *regs, void *call_data, |
785 | const char *fmt, ...) | |
8d938dbd PMF |
786 | { |
787 | va_list args; | |
788 | ||
789 | va_start(args, fmt); | |
75667d04 | 790 | ltt_vtrace(mdata, probe_data, regs, call_data, fmt, &args); |
8d938dbd PMF |
791 | va_end(args); |
792 | } | |
8d938dbd PMF |
793 | |
794 | //ust// MODULE_LICENSE("GPL"); | |
795 | //ust// MODULE_AUTHOR("Mathieu Desnoyers"); | |
796 | //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer"); |