2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
10 static void vreportf(const char *prefix
, const char *err
, va_list params
)
13 char *p
, *pend
= msg
+ sizeof(msg
);
14 size_t prefix_len
= strlen(prefix
);
16 if (sizeof(msg
) <= prefix_len
) {
17 fprintf(stderr
, "BUG!!! too long a prefix '%s'\n", prefix
);
20 memcpy(msg
, prefix
, prefix_len
);
22 if (vsnprintf(p
, pend
- p
, err
, params
) < 0) {
23 fprintf(stderr
, _("error: unable to format message: %s\n"),
25 *p
= '\0'; /* vsnprintf() failed, clip at prefix */
28 for (; p
!= pend
- 1 && *p
; p
++) {
29 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
33 *(p
++) = '\n'; /* we no longer need a NUL */
35 write_in_full(2, msg
, p
- msg
);
38 static NORETURN
void usage_builtin(const char *err
, va_list params
)
40 vreportf(_("usage: "), err
, params
);
43 * When we detect a usage error *before* the command dispatch in
44 * cmd_main(), we don't know what verb to report. Force it to this
45 * to facilitate post-processing.
47 trace2_cmd_name("_usage_");
50 * Currently, the (err, params) are usually just the static usage
51 * string which isn't very useful here. Usually, the call site
52 * manually calls fprintf(stderr,...) with the actual detailed
53 * syntax error before calling usage().
55 * TODO It would be nice to update the call sites to pass both
56 * the static usage string and the detailed error message.
62 static void die_message_builtin(const char *err
, va_list params
)
64 trace2_cmd_error_va(err
, params
);
65 vreportf(_("fatal: "), err
, params
);
69 * We call trace2_cmd_error_va() in the below functions first and
70 * expect it to va_copy 'params' before using it (because an 'ap' can
71 * only be walked once).
73 static NORETURN
void die_builtin(const char *err
, va_list params
)
75 report_fn die_message_fn
= get_die_message_routine();
77 die_message_fn(err
, params
);
81 static void error_builtin(const char *err
, va_list params
)
83 trace2_cmd_error_va(err
, params
);
85 vreportf(_("error: "), err
, params
);
88 static void warn_builtin(const char *warn
, va_list params
)
90 trace2_cmd_error_va(warn
, params
);
92 vreportf(_("warning: "), warn
, params
);
95 static int die_is_recursing_builtin(void)
99 * Just an arbitrary number X where "a < x < b" where "a" is
100 * "maximum number of pthreads we'll ever plausibly spawn" and
101 * "b" is "something less than Inf", since the point is to
102 * prevent infinite recursion.
104 static const int recursion_limit
= 1024;
107 if (dying
> recursion_limit
) {
109 } else if (dying
== 2) {
110 warning("die() called many times. Recursion error or racy threaded death!");
117 /* If we are in a dlopen()ed .so write to a global variable would segfault
118 * (ugh), so keep things static. */
119 static NORETURN_PTR report_fn usage_routine
= usage_builtin
;
120 static NORETURN_PTR report_fn die_routine
= die_builtin
;
121 static report_fn die_message_routine
= die_message_builtin
;
122 static report_fn error_routine
= error_builtin
;
123 static report_fn warn_routine
= warn_builtin
;
124 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
126 void set_die_routine(NORETURN_PTR report_fn routine
)
128 die_routine
= routine
;
131 report_fn
get_die_message_routine(void)
133 return die_message_routine
;
136 void set_error_routine(report_fn routine
)
138 error_routine
= routine
;
141 report_fn
get_error_routine(void)
143 return error_routine
;
146 void set_warn_routine(report_fn routine
)
148 warn_routine
= routine
;
151 report_fn
get_warn_routine(void)
156 void set_die_is_recursing_routine(int (*routine
)(void))
158 die_is_recursing
= routine
;
161 void NORETURN
usagef(const char *err
, ...)
165 va_start(params
, err
);
166 usage_routine(err
, params
);
170 void NORETURN
usage(const char *err
)
175 void NORETURN
die(const char *err
, ...)
179 if (die_is_recursing()) {
180 fputs("fatal: recursion detected in die handler\n", stderr
);
184 va_start(params
, err
);
185 die_routine(err
, params
);
189 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
191 char str_error
[256], *err
;
194 err
= strerror(errno
);
195 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
196 if ((str_error
[j
++] = err
[i
++]) != '%')
198 if (j
< sizeof(str_error
) - 1) {
199 str_error
[j
++] = '%';
201 /* No room to double the '%', so we overwrite it with
208 /* Truncation is acceptable here */
209 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
213 void NORETURN
die_errno(const char *fmt
, ...)
218 if (die_is_recursing()) {
219 fputs("fatal: recursion detected in die_errno handler\n",
224 va_start(params
, fmt
);
225 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
230 int die_message(const char *err
, ...)
234 va_start(params
, err
);
235 die_message_routine(err
, params
);
240 #undef die_message_errno
241 int die_message_errno(const char *fmt
, ...)
246 va_start(params
, fmt
);
247 die_message_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
253 int error_errno(const char *fmt
, ...)
258 va_start(params
, fmt
);
259 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
265 int error(const char *err
, ...)
269 va_start(params
, err
);
270 error_routine(err
, params
);
275 void warning_errno(const char *warn
, ...)
280 va_start(params
, warn
);
281 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
285 void warning(const char *warn
, ...)
289 va_start(params
, warn
);
290 warn_routine(warn
, params
);
294 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
297 static void BUG_vfl_common(const char *file
, int line
, const char *fmt
,
302 /* truncation via snprintf is OK here */
303 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
305 vreportf(prefix
, fmt
, params
);
308 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
313 va_copy(params_copy
, params
);
314 BUG_vfl_common(file
, line
, fmt
, params
);
320 trace2_cmd_error_va(fmt
, params_copy
);
327 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
331 bug_called_must_BUG
= 0;
334 BUG_vfl(file
, line
, fmt
, ap
);
338 int bug_called_must_BUG
;
339 void bug_fl(const char *file
, int line
, const char *fmt
, ...)
343 bug_called_must_BUG
= 1;
346 BUG_vfl_common(file
, line
, fmt
, ap
);
350 trace2_cmd_error_va(fmt
, ap
);
354 #ifdef SUPPRESS_ANNOTATED_LEAKS
355 void unleak_memory(const void *ptr
, size_t len
)
357 static struct suppressed_leak_root
{
358 struct suppressed_leak_root
*next
;
359 char data
[FLEX_ARRAY
];
361 struct suppressed_leak_root
*root
;
363 FLEX_ALLOC_MEM(root
, data
, ptr
, len
);
364 root
->next
= suppressed_leaks
;
365 suppressed_leaks
= root
;