blob: 1b206de36d6e1cb7799fce728c2a37e310479223 [file] [log] [blame]
Linus Torvalds0fcfd162005-04-18 13:04:43 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
Junio C Hamano4050c0d2005-12-05 11:54:29 -08006#include "git-compat-util.h"
Clemens Buchacher3bc41812011-07-27 23:32:34 +02007#include "cache.h"
Linus Torvalds0fcfd162005-04-18 13:04:43 -07008
Johannes Sixtebaa79f2010-03-06 16:40:39 +01009void vreportf(const char *prefix, const char *err, va_list params)
Linus Torvalds0fcfd162005-04-18 13:04:43 -070010{
Jeff Kingb5a9e432017-01-11 09:02:03 -050011 char msg[4096];
Johannes Schindelin116d1fa2019-10-30 10:44:36 +000012 char *p, *pend = msg + sizeof(msg);
13 size_t prefix_len = strlen(prefix);
Jeff Kingf4c3edc2015-08-11 14:13:59 -040014
Johannes Schindelin116d1fa2019-10-30 10:44:36 +000015 if (sizeof(msg) <= prefix_len) {
16 fprintf(stderr, "BUG!!! too long a prefix '%s'\n", prefix);
17 abort();
18 }
19 memcpy(msg, prefix, prefix_len);
20 p = msg + prefix_len;
21 if (vsnprintf(p, pend - p, err, params) < 0)
22 *p = '\0'; /* vsnprintf() failed, clip at prefix */
23
24 for (; p != pend - 1 && *p; p++) {
Jeff Kingf2900892017-01-11 09:02:23 -050025 if (iscntrl(*p) && *p != '\t' && *p != '\n')
26 *p = '?';
Jeff Kingf4c3edc2015-08-11 14:13:59 -040027 }
Johannes Schindelin116d1fa2019-10-30 10:44:36 +000028
29 *(p++) = '\n'; /* we no longer need a NUL */
30 fflush(stderr);
31 write_in_full(2, msg, p - msg);
Clemens Buchacher3bc41812011-07-27 23:32:34 +020032}
33
Jonathan Nieder64b1cb72009-11-09 09:05:02 -060034static NORETURN void usage_builtin(const char *err, va_list params)
Linus Torvalds0fcfd162005-04-18 13:04:43 -070035{
Johannes Sixtebaa79f2010-03-06 16:40:39 +010036 vreportf("usage: ", err, params);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080037
38 /*
39 * When we detect a usage error *before* the command dispatch in
40 * cmd_main(), we don't know what verb to report. Force it to this
41 * to facilitate post-processing.
42 */
43 trace2_cmd_name("_usage_");
44
45 /*
46 * Currently, the (err, params) are usually just the static usage
47 * string which isn't very useful here. Usually, the call site
48 * manually calls fprintf(stderr,...) with the actual detailed
49 * syntax error before calling usage().
50 *
51 * TODO It would be nice to update the call sites to pass both
52 * the static usage string and the detailed error message.
53 */
54
Linus Torvalds5d1a5c02005-10-01 13:24:27 -070055 exit(129);
Linus Torvalds0fcfd162005-04-18 13:04:43 -070056}
57
Junio C Hamanoce88ac52006-06-23 22:44:33 -070058static NORETURN void die_builtin(const char *err, va_list params)
Petr Baudis39a3f5e2006-06-24 04:34:38 +020059{
Jeff Hostetleree4512e2019-02-22 14:25:01 -080060 /*
61 * We call this trace2 function first and expect it to va_copy 'params'
62 * before using it (because an 'ap' can only be walked once).
63 */
64 trace2_cmd_error_va(err, params);
65
Johannes Sixtebaa79f2010-03-06 16:40:39 +010066 vreportf("fatal: ", err, params);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080067
Petr Baudis39a3f5e2006-06-24 04:34:38 +020068 exit(128);
69}
70
Junio C Hamanoce88ac52006-06-23 22:44:33 -070071static void error_builtin(const char *err, va_list params)
Petr Baudis39a3f5e2006-06-24 04:34:38 +020072{
Jeff Hostetleree4512e2019-02-22 14:25:01 -080073 /*
74 * We call this trace2 function first and expect it to va_copy 'params'
75 * before using it (because an 'ap' can only be walked once).
76 */
77 trace2_cmd_error_va(err, params);
78
Johannes Sixtebaa79f2010-03-06 16:40:39 +010079 vreportf("error: ", err, params);
Petr Baudis39a3f5e2006-06-24 04:34:38 +020080}
81
Shawn O. Pearcefa39b6b2006-12-21 19:48:32 -050082static void warn_builtin(const char *warn, va_list params)
83{
Jonathan Tan0ee10fd2020-11-23 12:45:22 -080084 /*
85 * We call this trace2 function first and expect it to va_copy 'params'
86 * before using it (because an 'ap' can only be walked once).
87 */
88 trace2_cmd_error_va(warn, params);
89
Johannes Sixtebaa79f2010-03-06 16:40:39 +010090 vreportf("warning: ", warn, params);
Shawn O. Pearcefa39b6b2006-12-21 19:48:32 -050091}
Petr Baudis39a3f5e2006-06-24 04:34:38 +020092
Jeff Kingc19a4902013-04-16 15:46:22 -040093static int die_is_recursing_builtin(void)
94{
95 static int dying;
Ævar Arnfjörð Bjarmason2d3c02f2017-06-21 20:47:42 +000096 /*
97 * Just an arbitrary number X where "a < x < b" where "a" is
98 * "maximum number of pthreads we'll ever plausibly spawn" and
99 * "b" is "something less than Inf", since the point is to
100 * prevent infinite recursion.
101 */
102 static const int recursion_limit = 1024;
103
104 dying++;
105 if (dying > recursion_limit) {
106 return 1;
107 } else if (dying == 2) {
108 warning("die() called many times. Recursion error or racy threaded death!");
109 return 0;
110 } else {
111 return 0;
112 }
Jeff Kingc19a4902013-04-16 15:46:22 -0400113}
114
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200115/* If we are in a dlopen()ed .so write to a global variable would segfault
116 * (ugh), so keep things static. */
Jeff King5710dcc2020-10-15 15:30:04 -0400117static NORETURN_PTR report_fn usage_routine = usage_builtin;
118static NORETURN_PTR report_fn die_routine = die_builtin;
119static report_fn error_routine = error_builtin;
120static report_fn warn_routine = warn_builtin;
Jeff Kingc19a4902013-04-16 15:46:22 -0400121static int (*die_is_recursing)(void) = die_is_recursing_builtin;
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200122
Jeff King5710dcc2020-10-15 15:30:04 -0400123void set_die_routine(NORETURN_PTR report_fn routine)
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200124{
125 die_routine = routine;
126}
127
Jeff King5710dcc2020-10-15 15:30:04 -0400128void set_error_routine(report_fn routine)
Clemens Buchacher3bc41812011-07-27 23:32:34 +0200129{
130 error_routine = routine;
131}
132
Jeff King5710dcc2020-10-15 15:30:04 -0400133report_fn get_error_routine(void)
Christian Couder725149b2016-09-04 22:18:28 +0200134{
135 return error_routine;
136}
137
Jeff King5710dcc2020-10-15 15:30:04 -0400138void set_warn_routine(report_fn routine)
Christian Couderb83f1082016-09-04 22:18:27 +0200139{
140 warn_routine = routine;
141}
142
Jeff King5710dcc2020-10-15 15:30:04 -0400143report_fn get_warn_routine(void)
Christian Couder725149b2016-09-04 22:18:28 +0200144{
145 return warn_routine;
146}
147
Jeff Kingc19a4902013-04-16 15:46:22 -0400148void set_die_is_recursing_routine(int (*routine)(void))
149{
150 die_is_recursing = routine;
151}
152
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700153void NORETURN usagef(const char *err, ...)
Jonathan Nieder64b1cb72009-11-09 09:05:02 -0600154{
155 va_list params;
156
157 va_start(params, err);
158 usage_routine(err, params);
159 va_end(params);
160}
161
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700162void NORETURN usage(const char *err)
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200163{
Jonathan Nieder64b1cb72009-11-09 09:05:02 -0600164 usagef("%s", err);
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200165}
166
Stephen Boydc2e86ad2011-03-22 00:51:05 -0700167void NORETURN die(const char *err, ...)
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700168{
169 va_list params;
170
Jeff Kingc19a4902013-04-16 15:46:22 -0400171 if (die_is_recursing()) {
Brandon Caseycd163d42012-11-14 17:45:52 -0800172 fputs("fatal: recursion detected in die handler\n", stderr);
173 exit(128);
174 }
Brandon Caseycd163d42012-11-14 17:45:52 -0800175
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700176 va_start(params, err);
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200177 die_routine(err, params);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700178 va_end(params);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700179}
180
Nguyễn Thái Ngọc Duy58e4e512016-05-08 16:47:21 +0700181static const char *fmt_with_err(char *buf, int n, const char *fmt)
Thomas Rastb8750362009-06-27 17:58:44 +0200182{
Junio C Hamanof8b5a8e2009-06-27 17:58:45 +0200183 char str_error[256], *err;
184 int i, j;
Thomas Rastb8750362009-06-27 17:58:44 +0200185
Junio C Hamanof8b5a8e2009-06-27 17:58:45 +0200186 err = strerror(errno);
187 for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
188 if ((str_error[j++] = err[i++]) != '%')
189 continue;
190 if (j < sizeof(str_error) - 1) {
191 str_error[j++] = '%';
192 } else {
193 /* No room to double the '%', so we overwrite it with
194 * '\0' below */
195 j--;
196 break;
197 }
198 }
199 str_error[j] = 0;
Jeff Kingac4896f2018-05-18 18:58:44 -0700200 /* Truncation is acceptable here */
Nguyễn Thái Ngọc Duy58e4e512016-05-08 16:47:21 +0700201 snprintf(buf, n, "%s: %s", fmt, str_error);
202 return buf;
203}
204
205void NORETURN die_errno(const char *fmt, ...)
206{
207 char buf[1024];
208 va_list params;
209
210 if (die_is_recursing()) {
211 fputs("fatal: recursion detected in die_errno handler\n",
212 stderr);
213 exit(128);
214 }
Thomas Rastb8750362009-06-27 17:58:44 +0200215
216 va_start(params, fmt);
Nguyễn Thái Ngọc Duy58e4e512016-05-08 16:47:21 +0700217 die_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
Thomas Rastb8750362009-06-27 17:58:44 +0200218 va_end(params);
219}
220
Jeff King4df5e912016-08-30 23:41:22 -0400221#undef error_errno
Nguyễn Thái Ngọc Duyfd1d6722016-05-08 16:47:22 +0700222int error_errno(const char *fmt, ...)
223{
224 char buf[1024];
225 va_list params;
226
227 va_start(params, fmt);
228 error_routine(fmt_with_err(buf, sizeof(buf), fmt), params);
229 va_end(params);
230 return -1;
231}
232
Jeff Kinge208f9c2012-12-15 12:37:36 -0500233#undef error
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700234int error(const char *err, ...)
235{
236 va_list params;
237
238 va_start(params, err);
Petr Baudis39a3f5e2006-06-24 04:34:38 +0200239 error_routine(err, params);
Linus Torvalds0fcfd162005-04-18 13:04:43 -0700240 va_end(params);
241 return -1;
242}
Shawn O. Pearcefa39b6b2006-12-21 19:48:32 -0500243
Nguyễn Thái Ngọc Duyfd1d6722016-05-08 16:47:22 +0700244void warning_errno(const char *warn, ...)
245{
246 char buf[1024];
247 va_list params;
248
249 va_start(params, warn);
250 warn_routine(fmt_with_err(buf, sizeof(buf), warn), params);
251 va_end(params);
252}
253
Theodore Ts'o46efd2d2007-03-30 19:07:05 -0400254void warning(const char *warn, ...)
Shawn O. Pearcefa39b6b2006-12-21 19:48:32 -0500255{
256 va_list params;
257
258 va_start(params, warn);
259 warn_routine(warn, params);
260 va_end(params);
261}
Jeff Kingd8193742017-05-12 23:28:50 -0400262
Johannes Schindelina86303c2018-05-02 11:38:28 +0200263/* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
264int BUG_exit_code;
265
Jeff Kingd8193742017-05-12 23:28:50 -0400266static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
267{
268 char prefix[256];
Jonathan Tan0a9dde42021-02-05 12:09:08 -0800269 va_list params_copy;
270 static int in_bug;
271
272 va_copy(params_copy, params);
Jeff Kingd8193742017-05-12 23:28:50 -0400273
274 /* truncation via snprintf is OK here */
275 if (file)
276 snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
277 else
278 snprintf(prefix, sizeof(prefix), "BUG: ");
279
280 vreportf(prefix, fmt, params);
Jonathan Tan0a9dde42021-02-05 12:09:08 -0800281
282 if (in_bug)
283 abort();
284 in_bug = 1;
285
286 trace2_cmd_error_va(fmt, params_copy);
287
Johannes Schindelina86303c2018-05-02 11:38:28 +0200288 if (BUG_exit_code)
289 exit(BUG_exit_code);
Jeff Kingd8193742017-05-12 23:28:50 -0400290 abort();
291}
292
293#ifdef HAVE_VARIADIC_MACROS
Ramsay Jones3d7dd2d2017-05-21 23:25:39 +0100294NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
Jeff Kingd8193742017-05-12 23:28:50 -0400295{
296 va_list ap;
297 va_start(ap, fmt);
298 BUG_vfl(file, line, fmt, ap);
299 va_end(ap);
300}
301#else
Ramsay Jones3d7dd2d2017-05-21 23:25:39 +0100302NORETURN void BUG(const char *fmt, ...)
Jeff Kingd8193742017-05-12 23:28:50 -0400303{
304 va_list ap;
305 va_start(ap, fmt);
306 BUG_vfl(NULL, 0, fmt, ap);
307 va_end(ap);
308}
309#endif
Jeff King0e5bba52017-09-08 02:38:41 -0400310
311#ifdef SUPPRESS_ANNOTATED_LEAKS
312void unleak_memory(const void *ptr, size_t len)
313{
314 static struct suppressed_leak_root {
315 struct suppressed_leak_root *next;
316 char data[FLEX_ARRAY];
317 } *suppressed_leaks;
318 struct suppressed_leak_root *root;
319
320 FLEX_ALLOC_MEM(root, data, ptr, len);
321 root->next = suppressed_leaks;
322 suppressed_leaks = root;
323}
324#endif