e36efdc5 |
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> |
d5970cd7 |
6 | #include <ltt/ltt-usertrace.h> |
e36efdc5 |
7 | #include <stdio.h> |
8 | #include <stdlib.h> |
9 | #include <stdarg.h> |
10 | |
895ad115 |
11 | #ifdef __cplusplus |
12 | extern "C" { |
13 | #endif |
14 | |
3a4541a5 |
15 | static int trace_user_generic_slow_printf( |
e36efdc5 |
16 | const char *fmt, ...) |
17 | #ifndef LTT_TRACE |
18 | { |
19 | } |
20 | #else |
21 | { |
22 | /* Guess we need no more than 100 bytes. */ |
0bdf8e98 |
23 | int n, size = 104; |
e36efdc5 |
24 | char *p, *np; |
25 | va_list ap; |
26 | int ret; |
27 | |
0bdf8e98 |
28 | size += ltt_align(size, sizeof(void*)); |
e36efdc5 |
29 | if ((p = malloc (size)) == NULL) |
30 | return -1; |
31 | |
32 | while (1) { |
33 | /* Try to print in the allocated space. */ |
34 | va_start(ap, fmt); |
35 | n = vsnprintf (p, size, fmt, ap); |
36 | va_end(ap); |
37 | /* If that worked, trace the string. */ |
38 | if (n > -1 && n < size) { |
0bdf8e98 |
39 | ret = trace_user_generic_slow_printf_param_buffer(p, n+1+ltt_align(n+1, sizeof(void*))); |
e36efdc5 |
40 | free(p); |
41 | return ret; |
42 | } |
43 | /* Else try again with more space. */ |
44 | if (n > -1) /* glibc 2.1 */ |
45 | size = n+1; /* precisely what is needed */ |
46 | else /* glibc 2.0 */ |
47 | size *= 2; /* twice the old size */ |
0bdf8e98 |
48 | size += ltt_align(size, sizeof(void*)); |
e36efdc5 |
49 | if ((np = realloc (p, size)) == NULL) { |
50 | free(p); |
51 | return -1; |
52 | } else { |
53 | p = np; |
54 | } |
55 | } |
56 | } |
57 | #endif //LTT_TRACE |
58 | |
895ad115 |
59 | #ifdef __cplusplus |
60 | } /* end of extern "C" */ |
61 | #endif |
62 | |
e36efdc5 |
63 | #endif //_LTT_FACILITY_CUSTOM_USER_GENERIC_H_ |