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