blob: ab3827d31a833e1d6e81a4de0f50121190fdff76 [file] [log] [blame]
Willy Tarreau7de211c2012-05-25 23:53:16 +02001/*
2 * Function call tracing for gcc >= 2.95
3 *
4 * Copyright 2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * gcc is able to call a specific function when entering and leaving any
12 * function when compiled with -finstrument-functions. This code must not
13 * be built with this argument. The performance impact is huge, so this
14 * feature should only be used when debugging.
15 *
16 * The entry and exits of all functions will be dumped into a file designated
17 * by the HAPROXY_TRACE environment variable, or by default "trace.out". If the
18 * trace file name is empty or "/dev/null", then traces are disabled. If
19 * opening the trace file fails, then stderr is used. If HAPROXY_TRACE_FAST is
20 * used, then the time is taken from the global <now> variable. Last, if
21 * HAPROXY_TRACE_TSC is used, then the machine's TSC is used instead of the
22 * real time (almost twice as fast).
23 *
24 * The output format is :
25 *
26 * <sec.usec> <level> <caller_ptr> <dir> <callee_ptr>
27 * or :
28 * <tsc> <level> <caller_ptr> <dir> <callee_ptr>
29 *
30 * where <dir> is '>' when entering a function and '<' when leaving.
31 *
32 * The article below is a nice explanation of how this works :
33 * http://balau82.wordpress.com/2010/10/06/trace-and-profile-function-calls-with-gcc/
34 */
35
36#include <sys/time.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <common/compiler.h>
41#include <common/time.h>
42
43static FILE *log;
44static int level;
45static int disabled;
46static int fast_time;
47static int use_tsc;
48static struct timeval trace_now;
49static struct timeval *now_ptr;
50static char line[128]; /* more than enough for a message (9+1+6+1+3+1+18+1+1+18+1+1) */
51
52static int open_trace()
53{
54 const char *output = getenv("HAPROXY_TRACE");
55
56 if (!output)
57 output = "trace.out";
58
59 if (!*output || strcmp(output, "/dev/null") == 0) {
60 disabled = 1;
61 return 0;
62 }
63
64 log = fopen(output, "w");
65 if (!log)
66 log = stderr;
67
68 now_ptr = &now;
69 if (getenv("HAPROXY_TRACE_FAST") != NULL) {
70 fast_time = 1;
71 now_ptr = &trace_now;
72 }
73 if (getenv("HAPROXY_TRACE_TSC") != NULL) {
74 fast_time = 1;
75 use_tsc = 1;
76 }
77 return 1;
78}
79
80/* This function first divides the number by 100M then iteratively multiplies it
81 * by 100 (using adds and shifts). The trick is that dividing by 100M is equivalent
82 * to multiplying by 1/100M, which approximates to 1441151881/2^57. All local
83 * variables fit in registers on x86. This version outputs two digits per round.
84 * <min_pairs> indicates the minimum number of pairs of digits that have to be
85 * emitted, which might be left-padded with zeroes.
86 * It returns the pointer to the ending '\0'.
87 */
88static char *ultoad2(unsigned int x, char *out, int min_pairs)
89{
90 unsigned int q;
91 char *p = out;
92 int pos = 4;
93 unsigned long long y;
94
95 static const unsigned short bcd[100] = {
96 0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
97 0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
98 0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
99 0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
100 0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
101 0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
102 0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
103 0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
104 0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
105 0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939 };
106
107 y = x * 1441151881ULL; /* y>>57 will be the integer part of x/100M */
108 while (1) {
109 q = y >> 57;
110 /* Q is composed of the first digit in the lower byte and the second
111 * digit in the higher byte.
112 */
113 if (p != out || q > 9 || pos < min_pairs) {
114#if defined(__i386__) || defined(__x86_64__)
115 /* unaligned accesses are fast on x86 */
116 *(unsigned short *)p = bcd[q];
117 p += 2;
118#else
119 *(p++) = bcd[q];
120 *(p++) = bcd[q] >> 8;
121#endif
122 }
123 else if (q || !pos) {
124 /* only at most one digit */
125 *(p++) = bcd[q] >> 8;
126 }
127 if (--pos < 0)
128 break;
129
130 y &= 0x1FFFFFFFFFFFFFFULL; // remainder
131
132 if (sizeof(long) >= sizeof(long long)) {
133 /* shifting is preferred on 64-bit archs, while mult is faster on 32-bit.
134 * We multiply by 100 by doing *5, *5 and *4, all of which are trivial.
135 */
136 y += (y << 2);
137 y += (y << 2);
138 y <<= 2;
139 }
140 else
141 y *= 100;
142 }
143
144 *p = '\0';
145 return p;
146}
147
148/* Send <h> as hex into <out>. Returns the pointer to the ending '\0'. */
149static char *emit_hex(unsigned long h, char *out)
150{
151 static unsigned char hextab[16] = "0123456789abcdef";
152 int shift = sizeof(h) * 8 - 4;
153 unsigned int idx;
154
155 do {
156 idx = (h >> shift);
157 if (idx || !shift)
158 *out++ = hextab[idx & 15];
159 shift -= 4;
160 } while (shift >= 0);
161 *out = '\0';
162 return out;
163}
164
165#if defined(__i386__) || defined(__x86_64__)
166static inline unsigned long long rdtsc()
167{
168 unsigned int a, d;
169 asm volatile("rdtsc" : "=a" (a), "=d" (d));
170 return a + ((unsigned long long)d << 32);
171}
172#else
173static inline unsigned long long rdtsc()
174{
175 struct timeval tv;
176 gettimeofday(&tv, NULL);
177 return tv.tv_sec * 1000000 + tv.tv_usec;
178}
179#endif
180
181static void make_line(void *from, void *to, int level, char dir)
182{
183 char *p = line;
184
185 if (unlikely(!log) && !open_trace())
186 return;
187
188 if (unlikely(!fast_time))
189 gettimeofday(now_ptr, NULL);
190
191#ifdef USE_SLOW_FPRINTF
192 if (!use_tsc)
193 fprintf(log, "%u.%06u %d %p %c %p\n",
194 (unsigned int)now_ptr->tv_sec,
195 (unsigned int)now_ptr->tv_usec,
196 level, from, dir, to);
197 else
198 fprintf(log, "%llx %d %p %c %p\n",
199 rdtsc(), level, from, dir, to);
200 return;
201#endif
202
203 if (unlikely(!use_tsc)) {
204 /* "%u.06u", tv_sec, tv_usec */
205 p = ultoad2(now_ptr->tv_sec, p, 0);
206 *p++ = '.';
207 p = ultoad2(now_ptr->tv_usec, p, 3);
208 } else {
209 /* "%08x%08x", high, low */
210 unsigned long long t = rdtsc();
211 if (sizeof(long) < sizeof(long long))
212 p = emit_hex((unsigned long)(t >> 32U), p);
213 p = emit_hex((unsigned long)(t), p);
214 }
215
216 /* " %u", level */
217 *p++ = ' ';
218 p = ultoad2(level, p, 0);
219
220 /* " %p", from */
221 *p++ = ' '; *p++ = '0'; *p++ = 'x';
222 p = emit_hex((unsigned long)from, p);
223
224 /* " %c", dir */
225 *p++ = ' '; *p++ = dir;
226
227 /* " %p", to */
228 *p++ = ' '; *p++ = '0'; *p++ = 'x';
229 p = emit_hex((unsigned long)to, p);
230
231 *p++ = '\n';
232
233 fwrite(line, p - line, 1, log);
234}
235
236/* These are the functions GCC calls */
237void __cyg_profile_func_enter(void *to, void *from)
238{
239 if (!disabled)
240 return make_line(from, to, ++level, '>');
241}
242
243void __cyg_profile_func_exit(void *to, void *from)
244{
245 if (!disabled)
246 return make_line(from, to, level--, '<');
247}