2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
9 static void vreportf(const char *prefix
, const char *err
, va_list params
)
12 char *p
, *pend
= msg
+ sizeof(msg
);
13 size_t prefix_len
= strlen(prefix
);
15 if (sizeof(msg
) <= prefix_len
) {
16 fprintf(stderr
, "BUG!!! too long a prefix '%s'\n", prefix
);
19 memcpy(msg
, prefix
, prefix_len
);
21 if (vsnprintf(p
, pend
- p
, err
, params
) < 0)
22 *p
= '\0'; /* vsnprintf() failed, clip at prefix */
24 for (; p
!= pend
- 1 && *p
; p
++) {
25 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
29 *(p
++) = '\n'; /* we no longer need a NUL */
31 write_in_full(2, msg
, p
- msg
);
34 static NORETURN
void usage_builtin(const char *err
, va_list params
)
36 vreportf("usage: ", err
, params
);
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.
43 trace2_cmd_name("_usage_");
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().
51 * TODO It would be nice to update the call sites to pass both
52 * the static usage string and the detailed error message.
58 static void die_message_builtin(const char *err
, va_list params
)
60 trace2_cmd_error_va(err
, params
);
61 vreportf("fatal: ", err
, params
);
65 * We call trace2_cmd_error_va() in the below functions first and
66 * expect it to va_copy 'params' before using it (because an 'ap' can
67 * only be walked once).
69 static NORETURN
void die_builtin(const char *err
, va_list params
)
71 report_fn die_message_fn
= get_die_message_routine();
73 die_message_fn(err
, params
);
77 static void error_builtin(const char *err
, va_list params
)
79 trace2_cmd_error_va(err
, params
);
81 vreportf("error: ", err
, params
);
84 static void warn_builtin(const char *warn
, va_list params
)
86 trace2_cmd_error_va(warn
, params
);
88 vreportf("warning: ", warn
, params
);
91 static int die_is_recursing_builtin(void)
95 * Just an arbitrary number X where "a < x < b" where "a" is
96 * "maximum number of pthreads we'll ever plausibly spawn" and
97 * "b" is "something less than Inf", since the point is to
98 * prevent infinite recursion.
100 static const int recursion_limit
= 1024;
103 if (dying
> recursion_limit
) {
105 } else if (dying
== 2) {
106 warning("die() called many times. Recursion error or racy threaded death!");
113 /* If we are in a dlopen()ed .so write to a global variable would segfault
114 * (ugh), so keep things static. */
115 static NORETURN_PTR report_fn usage_routine
= usage_builtin
;
116 static NORETURN_PTR report_fn die_routine
= die_builtin
;
117 static report_fn die_message_routine
= die_message_builtin
;
118 static report_fn error_routine
= error_builtin
;
119 static report_fn warn_routine
= warn_builtin
;
120 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
122 void set_die_routine(NORETURN_PTR report_fn routine
)
124 die_routine
= routine
;
127 report_fn
get_die_message_routine(void)
129 return die_message_routine
;
132 void set_error_routine(report_fn routine
)
134 error_routine
= routine
;
137 report_fn
get_error_routine(void)
139 return error_routine
;
142 void set_warn_routine(report_fn routine
)
144 warn_routine
= routine
;
147 report_fn
get_warn_routine(void)
152 void set_die_is_recursing_routine(int (*routine
)(void))
154 die_is_recursing
= routine
;
157 void NORETURN
usagef(const char *err
, ...)
161 va_start(params
, err
);
162 usage_routine(err
, params
);
166 void NORETURN
usage(const char *err
)
171 void NORETURN
die(const char *err
, ...)
175 if (die_is_recursing()) {
176 fputs("fatal: recursion detected in die handler\n", stderr
);
180 va_start(params
, err
);
181 die_routine(err
, params
);
185 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
187 char str_error
[256], *err
;
190 err
= strerror(errno
);
191 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
192 if ((str_error
[j
++] = err
[i
++]) != '%')
194 if (j
< sizeof(str_error
) - 1) {
195 str_error
[j
++] = '%';
197 /* No room to double the '%', so we overwrite it with
204 /* Truncation is acceptable here */
205 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
209 void NORETURN
die_errno(const char *fmt
, ...)
214 if (die_is_recursing()) {
215 fputs("fatal: recursion detected in die_errno handler\n",
220 va_start(params
, fmt
);
221 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
226 int die_message(const char *err
, ...)
230 va_start(params
, err
);
231 die_message_routine(err
, params
);
236 #undef die_message_errno
237 int die_message_errno(const char *fmt
, ...)
242 va_start(params
, fmt
);
243 die_message_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
249 int error_errno(const char *fmt
, ...)
254 va_start(params
, fmt
);
255 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
261 int error(const char *err
, ...)
265 va_start(params
, err
);
266 error_routine(err
, params
);
271 void warning_errno(const char *warn
, ...)
276 va_start(params
, warn
);
277 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
281 void warning(const char *warn
, ...)
285 va_start(params
, warn
);
286 warn_routine(warn
, params
);
290 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
293 static void BUG_vfl_common(const char *file
, int line
, const char *fmt
,
298 /* truncation via snprintf is OK here */
299 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
301 vreportf(prefix
, fmt
, params
);
304 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
309 va_copy(params_copy
, params
);
310 BUG_vfl_common(file
, line
, fmt
, params
);
316 trace2_cmd_error_va(fmt
, params_copy
);
323 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
327 bug_called_must_BUG
= 0;
330 BUG_vfl(file
, line
, fmt
, ap
);
334 int bug_called_must_BUG
;
335 void bug_fl(const char *file
, int line
, const char *fmt
, ...)
339 bug_called_must_BUG
= 1;
342 BUG_vfl_common(file
, line
, fmt
, ap
);
346 trace2_cmd_error_va(fmt
, ap
);
350 #ifdef SUPPRESS_ANNOTATED_LEAKS
351 void unleak_memory(const void *ptr
, size_t len
)
353 static struct suppressed_leak_root
{
354 struct suppressed_leak_root
*next
;
355 char data
[FLEX_ARRAY
];
357 struct suppressed_leak_root
*root
;
359 FLEX_ALLOC_MEM(root
, data
, ptr
, len
);
360 root
->next
= suppressed_leaks
;
361 suppressed_leaks
= root
;