| 1 | #ifndef _LTT_FACILITY_CUSTOM_USER_GENERIC_H_ |
| 2 | #define _LTT_FACILITY_CUSTOM_USER_GENERIC_H_ |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <ltt/ltt-facility-id-user_generic.h> |
| 6 | #include <ltt/ltt-generic.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | static inline int trace_user_generic_slow_printf( |
| 12 | const char *fmt, ...) |
| 13 | #ifndef LTT_TRACE |
| 14 | { |
| 15 | } |
| 16 | #else |
| 17 | { |
| 18 | /* Guess we need no more than 100 bytes. */ |
| 19 | int n, size = 100; |
| 20 | char *p, *np; |
| 21 | va_list ap; |
| 22 | int ret; |
| 23 | |
| 24 | if ((p = malloc (size)) == NULL) |
| 25 | return -1; |
| 26 | |
| 27 | while (1) { |
| 28 | /* Try to print in the allocated space. */ |
| 29 | va_start(ap, fmt); |
| 30 | n = vsnprintf (p, size, fmt, ap); |
| 31 | va_end(ap); |
| 32 | /* If that worked, trace the string. */ |
| 33 | if (n > -1 && n < size) { |
| 34 | ret = trace_user_generic_slow_printf_param_buffer(p, n+1); |
| 35 | free(p); |
| 36 | return ret; |
| 37 | } |
| 38 | /* Else try again with more space. */ |
| 39 | if (n > -1) /* glibc 2.1 */ |
| 40 | size = n+1; /* precisely what is needed */ |
| 41 | else /* glibc 2.0 */ |
| 42 | size *= 2; /* twice the old size */ |
| 43 | if ((np = realloc (p, size)) == NULL) { |
| 44 | free(p); |
| 45 | return -1; |
| 46 | } else { |
| 47 | p = np; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | #endif //LTT_TRACE |
| 52 | |
| 53 | #endif //_LTT_FACILITY_CUSTOM_USER_GENERIC_H_ |