blob: 4e771f86ac289ada2f5b97eb4013ef313fbb3cbf [file] [log] [blame]
Karsten Blees5991a552014-06-11 09:56:49 +02001#ifndef TRACE_H
2#define TRACE_H
3
4#include "git-compat-util.h"
5#include "strbuf.h"
6
Heba Walyf1ecbe02019-11-17 21:04:56 +00007/**
8 * The trace API can be used to print debug messages to stderr or a file. Trace
9 * code is inactive unless explicitly enabled by setting `GIT_TRACE*` environment
10 * variables.
11 *
12 * The trace implementation automatically adds `timestamp file:line ... \n` to
13 * all trace messages. E.g.:
14 *
15 * ------------
16 * 23:59:59.123456 git.c:312 trace: built-in: git 'foo'
17 * 00:00:00.000001 builtin/foo.c:99 foo: some message
18 * ------------
19 *
20 * Bugs & Caveats
21 * --------------
22 *
23 * GIT_TRACE_* environment variables can be used to tell Git to show
24 * trace output to its standard error stream. Git can often spawn a pager
25 * internally to run its subcommand and send its standard output and
26 * standard error to it.
27 *
28 * Because GIT_TRACE_PERFORMANCE trace is generated only at the very end
29 * of the program with atexit(), which happens after the pager exits, it
30 * would not work well if you send its log to the standard error output
31 * and let Git spawn the pager at the same time.
32 *
33 * As a work around, you can for example use '--no-pager', or set
34 * GIT_TRACE_PERFORMANCE to another file descriptor which is redirected
35 * to stderr, or set GIT_TRACE_PERFORMANCE to a file specified by its
36 * absolute path.
37 *
38 * For example instead of the following command which by default may not
39 * print any performance information:
40 *
41 * ------------
42 * GIT_TRACE_PERFORMANCE=2 git log -1
43 * ------------
44 *
45 * you may want to use:
46 *
47 * ------------
48 * GIT_TRACE_PERFORMANCE=2 git --no-pager log -1
49 * ------------
50 *
51 * or:
52 *
53 * ------------
54 * GIT_TRACE_PERFORMANCE=3 3>&2 git log -1
55 * ------------
56 *
57 * or:
58 *
59 * ------------
60 * GIT_TRACE_PERFORMANCE=/path/to/log/file git log -1
61 * ------------
62 *
63 */
64
65/**
66 * Defines a trace key (or category). The default (for API functions that
67 * don't take a key) is `GIT_TRACE`.
68 *
69 * E.g. to define a trace key controlled by environment variable `GIT_TRACE_FOO`:
70 *
71 * ------------
72 * static struct trace_key trace_foo = TRACE_KEY_INIT(FOO);
73 *
74 * static void trace_print_foo(const char *message)
75 * {
76 * trace_printf_key(&trace_foo, "%s", message);
77 * }
78 * ------------
79 *
80 * Note: don't use `const` as the trace implementation stores internal state in
81 * the `trace_key` structure.
82 */
Karsten Blees6aa30852014-07-12 02:00:06 +020083struct trace_key {
84 const char * const key;
85 int fd;
86 unsigned int initialized : 1;
87 unsigned int need_close : 1;
88};
89
Gennady Kupava406102a2017-11-26 20:11:18 +000090extern struct trace_key trace_default_key;
91
Ævar Arnfjörð Bjarmasonf69a6e42021-09-27 14:54:27 +020092#define TRACE_KEY_INIT(name) { .key = "GIT_TRACE_" #name }
Gennady Kupava8eeb25c2017-11-26 20:11:19 +000093extern struct trace_key trace_perf_key;
Nguyễn Thái Ngọc Duycb507612018-03-30 14:34:59 -040094extern struct trace_key trace_setup_key;
Karsten Blees6aa30852014-07-12 02:00:06 +020095
Denton Liu55454422019-04-29 04:28:14 -040096void trace_repo_setup(const char *prefix);
Heba Walyf1ecbe02019-11-17 21:04:56 +000097
98/**
99 * Checks whether the trace key is enabled. Used to prevent expensive
100 * string formatting before calling one of the printing APIs.
101 */
Denton Liu55454422019-04-29 04:28:14 -0400102int trace_want(struct trace_key *key);
Heba Walyf1ecbe02019-11-17 21:04:56 +0000103
104/**
Jonathan Tan7167a622020-05-11 10:43:10 -0700105 * Enables or disables tracing for the specified key, as if the environment
106 * variable was set to the given value.
107 */
108void trace_override_envvar(struct trace_key *key, const char *value);
109
110/**
Heba Walyf1ecbe02019-11-17 21:04:56 +0000111 * Disables tracing for the specified key, even if the environment variable
112 * was set.
113 */
Denton Liu55454422019-04-29 04:28:14 -0400114void trace_disable(struct trace_key *key);
Heba Walyf1ecbe02019-11-17 21:04:56 +0000115
116/**
117 * Returns nanoseconds since the epoch (01/01/1970), typically used
118 * for performance measurements.
119 * Currently there are high precision timer implementations for Linux (using
120 * `clock_gettime(CLOCK_MONOTONIC)`) and Windows (`QueryPerformanceCounter`).
121 * Other platforms use `gettimeofday` as time source.
122 */
Denton Liu55454422019-04-29 04:28:14 -0400123uint64_t getnanotime(void);
Heba Walyf1ecbe02019-11-17 21:04:56 +0000124
Denton Liu55454422019-04-29 04:28:14 -0400125void trace_command_performance(const char **argv);
126void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200127uint64_t trace_performance_enter(void);
Karsten Blees66f66c52014-07-12 02:04:29 +0200128
Karsten Bleese05bed92014-07-12 02:05:03 +0200129/*
130 * TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
131 * default is __FILE__, as it is consistent with assert(), and static function
132 * names are not necessarily unique.
133 *
134 * __FILE__ ":" __FUNCTION__ doesn't work with GNUC, as __FILE__ is supplied
135 * by the preprocessor as a string literal, and __FUNCTION__ is filled in by
136 * the compiler as a string constant.
137 */
138#ifndef TRACE_CONTEXT
139# define TRACE_CONTEXT __FILE__
140#endif
141
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100142/**
143 * Macros to add the file:line of the calling code, instead of that of
144 * the trace function itself.
145 *
Karsten Bleese05bed92014-07-12 02:05:03 +0200146 * Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
147 * parameter ('format' in this case). Otherwise, a call without variable
148 * arguments will have a surplus ','. E.g.:
149 *
150 * #define foo(format, ...) bar(format, __VA_ARGS__)
151 * foo("test");
152 *
153 * will expand to
154 *
155 * bar("test",);
156 *
157 * which is invalid (note the ',)'). With GNUC, '##__VA_ARGS__' drops the
158 * comma, but this is non-standard.
159 */
160
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100161/**
162 * trace_printf(), accepts "const char *format, ...".
163 *
164 * Prints a formatted message, similar to printf.
165 */
166#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
167
168/**
169 * trace_printf_key(), accepts "struct trace_key *key, const char *format, ...".
170 */
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000171#define trace_printf_key(key, ...) \
172 do { \
173 if (trace_pass_fl(key)) \
174 trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
175 __VA_ARGS__); \
176 } while (0)
Karsten Bleese05bed92014-07-12 02:05:03 +0200177
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100178/**
179 * trace_argv_printf(), accepts "struct trace_key *key, const char *format, ...)".
180 *
181 * Prints a formatted message, followed by a quoted list of arguments.
182 */
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000183#define trace_argv_printf(argv, ...) \
184 do { \
185 if (trace_pass_fl(&trace_default_key)) \
186 trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
187 argv, __VA_ARGS__); \
188 } while (0)
Karsten Bleese05bed92014-07-12 02:05:03 +0200189
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100190/**
191 * trace_strbuf(), accepts "struct trace_key *key, const struct strbuf *data".
192 *
193 * Prints the strbuf, without additional formatting (i.e. doesn't
194 * choke on `%` or even `\0`).
195 */
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000196#define trace_strbuf(key, data) \
197 do { \
198 if (trace_pass_fl(key)) \
199 trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
200 } while (0)
Karsten Bleese05bed92014-07-12 02:05:03 +0200201
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100202/**
203 * trace_performance(), accepts "uint64_t nanos, const char *format, ...".
204 *
205 * Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled.
206 *
207 * Example:
208 * ------------
209 * uint64_t t = 0;
210 * for (;;) {
211 * // ignore
212 * t -= getnanotime();
213 * // code section to measure
214 * t += getnanotime();
215 * // ignore
216 * }
217 * trace_performance(t, "frotz");
218 * ------------
219 */
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000220#define trace_performance(nanos, ...) \
221 do { \
222 if (trace_pass_fl(&trace_perf_key)) \
223 trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
224 __VA_ARGS__); \
225 } while (0)
Karsten Blees09b2c1c2014-07-12 02:06:28 +0200226
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100227/**
228 * trace_performance_since(), accepts "uint64_t start, const char *format, ...".
229 *
230 * Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled.
231 *
232 * Example:
233 * ------------
234 * uint64_t start = getnanotime();
235 * // code section to measure
236 * trace_performance_since(start, "foobar");
237 * ------------
238 */
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000239#define trace_performance_since(start, ...) \
240 do { \
241 if (trace_pass_fl(&trace_perf_key)) \
242 trace_performance_fl(TRACE_CONTEXT, __LINE__, \
243 getnanotime() - (start), \
244 __VA_ARGS__); \
245 } while (0)
Karsten Blees09b2c1c2014-07-12 02:06:28 +0200246
Ævar Arnfjörð Bjarmason56a29d22022-02-21 17:05:27 +0100247/**
248 * trace_performance_leave(), accepts "const char *format, ...".
249 */
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200250#define trace_performance_leave(...) \
251 do { \
252 if (trace_pass_fl(&trace_perf_key)) \
253 trace_performance_leave_fl(TRACE_CONTEXT, __LINE__, \
254 getnanotime(), \
255 __VA_ARGS__); \
256 } while (0)
257
Karsten Bleese05bed92014-07-12 02:05:03 +0200258/* backend functions, use non-*fl macros instead */
259__attribute__((format (printf, 4, 5)))
Denton Liub199d712019-04-29 04:28:20 -0400260void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
Denton Liuad6dad02019-04-29 04:28:23 -0400261 const char *format, ...);
Karsten Bleese05bed92014-07-12 02:05:03 +0200262__attribute__((format (printf, 4, 5)))
Denton Liub199d712019-04-29 04:28:20 -0400263void trace_argv_printf_fl(const char *file, int line, const char **argv,
Denton Liuad6dad02019-04-29 04:28:23 -0400264 const char *format, ...);
Denton Liu55454422019-04-29 04:28:14 -0400265void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
Denton Liuad6dad02019-04-29 04:28:23 -0400266 const struct strbuf *data);
Karsten Blees09b2c1c2014-07-12 02:06:28 +0200267__attribute__((format (printf, 4, 5)))
Denton Liub199d712019-04-29 04:28:20 -0400268void trace_performance_fl(const char *file, int line,
Denton Liuad6dad02019-04-29 04:28:23 -0400269 uint64_t nanos, const char *fmt, ...);
Nguyễn Thái Ngọc Duyc46c4062018-08-18 16:41:22 +0200270__attribute__((format (printf, 4, 5)))
Denton Liub199d712019-04-29 04:28:20 -0400271void trace_performance_leave_fl(const char *file, int line,
Denton Liuad6dad02019-04-29 04:28:23 -0400272 uint64_t nanos, const char *fmt, ...);
Gennady Kupava8eeb25c2017-11-26 20:11:19 +0000273static inline int trace_pass_fl(struct trace_key *key)
274{
275 return key->fd || !key->initialized;
276}
Karsten Bleese05bed92014-07-12 02:05:03 +0200277
Karsten Blees5991a552014-06-11 09:56:49 +0200278#endif /* TRACE_H */