blob: 4bbbc302abe2a48cc3b648fd216b86623d37f2af [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
Willy Tarreau7de211c2012-05-25 23:53:16 +0200165static void make_line(void *from, void *to, int level, char dir)
166{
167 char *p = line;
168
169 if (unlikely(!log) && !open_trace())
170 return;
171
172 if (unlikely(!fast_time))
173 gettimeofday(now_ptr, NULL);
174
175#ifdef USE_SLOW_FPRINTF
176 if (!use_tsc)
177 fprintf(log, "%u.%06u %d %p %c %p\n",
178 (unsigned int)now_ptr->tv_sec,
179 (unsigned int)now_ptr->tv_usec,
180 level, from, dir, to);
181 else
182 fprintf(log, "%llx %d %p %c %p\n",
183 rdtsc(), level, from, dir, to);
184 return;
185#endif
186
187 if (unlikely(!use_tsc)) {
188 /* "%u.06u", tv_sec, tv_usec */
189 p = ultoad2(now_ptr->tv_sec, p, 0);
190 *p++ = '.';
191 p = ultoad2(now_ptr->tv_usec, p, 3);
192 } else {
193 /* "%08x%08x", high, low */
194 unsigned long long t = rdtsc();
195 if (sizeof(long) < sizeof(long long))
196 p = emit_hex((unsigned long)(t >> 32U), p);
197 p = emit_hex((unsigned long)(t), p);
198 }
199
200 /* " %u", level */
201 *p++ = ' ';
202 p = ultoad2(level, p, 0);
203
204 /* " %p", from */
205 *p++ = ' '; *p++ = '0'; *p++ = 'x';
206 p = emit_hex((unsigned long)from, p);
207
208 /* " %c", dir */
209 *p++ = ' '; *p++ = dir;
210
211 /* " %p", to */
212 *p++ = ' '; *p++ = '0'; *p++ = 'x';
213 p = emit_hex((unsigned long)to, p);
214
215 *p++ = '\n';
216
217 fwrite(line, p - line, 1, log);
218}
219
220/* These are the functions GCC calls */
221void __cyg_profile_func_enter(void *to, void *from)
222{
223 if (!disabled)
224 return make_line(from, to, ++level, '>');
225}
226
227void __cyg_profile_func_exit(void *to, void *from)
228{
229 if (!disabled)
230 return make_line(from, to, level--, '<');
231}