2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
6 #include "git-compat-util.h"
11 static void vreportf(const char *prefix
, const char *err
, va_list params
)
14 char *p
, *pend
= msg
+ sizeof(msg
);
15 size_t prefix_len
= strlen(prefix
);
17 if (sizeof(msg
) <= prefix_len
) {
18 fprintf(stderr
, "BUG!!! too long a prefix '%s'\n", prefix
);
21 memcpy(msg
, prefix
, prefix_len
);
23 if (vsnprintf(p
, pend
- p
, err
, params
) < 0)
24 *p
= '\0'; /* vsnprintf() failed, clip at prefix */
26 for (; p
!= pend
- 1 && *p
; p
++) {
27 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
31 *(p
++) = '\n'; /* we no longer need a NUL */
33 write_in_full(2, msg
, p
- msg
);
36 static NORETURN
void usage_builtin(const char *err
, va_list params
)
38 vreportf(_("usage: "), err
, params
);
41 * When we detect a usage error *before* the command dispatch in
42 * cmd_main(), we don't know what verb to report. Force it to this
43 * to facilitate post-processing.
45 trace2_cmd_name("_usage_");
48 * Currently, the (err, params) are usually just the static usage
49 * string which isn't very useful here. Usually, the call site
50 * manually calls fprintf(stderr,...) with the actual detailed
51 * syntax error before calling usage().
53 * TODO It would be nice to update the call sites to pass both
54 * the static usage string and the detailed error message.
60 static void die_message_builtin(const char *err
, va_list params
)
62 trace2_cmd_error_va(err
, params
);
63 vreportf(_("fatal: "), err
, params
);
67 * We call trace2_cmd_error_va() in the below functions first and
68 * expect it to va_copy 'params' before using it (because an 'ap' can
69 * only be walked once).
71 static NORETURN
void die_builtin(const char *err
, va_list params
)
73 report_fn die_message_fn
= get_die_message_routine();
75 die_message_fn(err
, params
);
79 static void error_builtin(const char *err
, va_list params
)
81 trace2_cmd_error_va(err
, params
);
83 vreportf(_("error: "), err
, params
);
86 static void warn_builtin(const char *warn
, va_list params
)
88 trace2_cmd_error_va(warn
, params
);
90 vreportf(_("warning: "), warn
, params
);
93 static int die_is_recursing_builtin(void)
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.
102 static const int recursion_limit
= 1024;
105 if (dying
> recursion_limit
) {
107 } else if (dying
== 2) {
108 warning("die() called many times. Recursion error or racy threaded death!");
115 /* If we are in a dlopen()ed .so write to a global variable would segfault
116 * (ugh), so keep things static. */
117 static NORETURN_PTR report_fn usage_routine
= usage_builtin
;
118 static NORETURN_PTR report_fn die_routine
= die_builtin
;
119 static report_fn die_message_routine
= die_message_builtin
;
120 static report_fn error_routine
= error_builtin
;
121 static report_fn warn_routine
= warn_builtin
;
122 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
124 void set_die_routine(NORETURN_PTR report_fn routine
)
126 die_routine
= routine
;
129 report_fn
get_die_message_routine(void)
131 return die_message_routine
;
134 void set_error_routine(report_fn routine
)
136 error_routine
= routine
;
139 report_fn
get_error_routine(void)
141 return error_routine
;
144 void set_warn_routine(report_fn routine
)
146 warn_routine
= routine
;
149 report_fn
get_warn_routine(void)
154 void set_die_is_recursing_routine(int (*routine
)(void))
156 die_is_recursing
= routine
;
159 void NORETURN
usagef(const char *err
, ...)
163 va_start(params
, err
);
164 usage_routine(err
, params
);
168 void NORETURN
usage(const char *err
)
173 void NORETURN
die(const char *err
, ...)
177 if (die_is_recursing()) {
178 fputs("fatal: recursion detected in die handler\n", stderr
);
182 va_start(params
, err
);
183 die_routine(err
, params
);
187 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
189 char str_error
[256], *err
;
192 err
= strerror(errno
);
193 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
194 if ((str_error
[j
++] = err
[i
++]) != '%')
196 if (j
< sizeof(str_error
) - 1) {
197 str_error
[j
++] = '%';
199 /* No room to double the '%', so we overwrite it with
206 /* Truncation is acceptable here */
207 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
211 void NORETURN
die_errno(const char *fmt
, ...)
216 if (die_is_recursing()) {
217 fputs("fatal: recursion detected in die_errno handler\n",
222 va_start(params
, fmt
);
223 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
228 int die_message(const char *err
, ...)
232 va_start(params
, err
);
233 die_message_routine(err
, params
);
238 #undef die_message_errno
239 int die_message_errno(const char *fmt
, ...)
244 va_start(params
, fmt
);
245 die_message_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
251 int error_errno(const char *fmt
, ...)
256 va_start(params
, fmt
);
257 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
263 int error(const char *err
, ...)
267 va_start(params
, err
);
268 error_routine(err
, params
);
273 void warning_errno(const char *warn
, ...)
278 va_start(params
, warn
);
279 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
283 void warning(const char *warn
, ...)
287 va_start(params
, warn
);
288 warn_routine(warn
, params
);
292 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
295 static void BUG_vfl_common(const char *file
, int line
, const char *fmt
,
300 /* truncation via snprintf is OK here */
301 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
303 vreportf(prefix
, fmt
, params
);
306 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
311 va_copy(params_copy
, params
);
312 BUG_vfl_common(file
, line
, fmt
, params
);
318 trace2_cmd_error_va(fmt
, params_copy
);
325 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
329 bug_called_must_BUG
= 0;
332 BUG_vfl(file
, line
, fmt
, ap
);
336 int bug_called_must_BUG
;
337 void bug_fl(const char *file
, int line
, const char *fmt
, ...)
341 bug_called_must_BUG
= 1;
344 BUG_vfl_common(file
, line
, fmt
, ap
);
348 trace2_cmd_error_va(fmt
, ap
);
352 #ifdef SUPPRESS_ANNOTATED_LEAKS
353 void unleak_memory(const void *ptr
, size_t len
)
355 static struct suppressed_leak_root
{
356 struct suppressed_leak_root
*next
;
357 char data
[FLEX_ARRAY
];
359 struct suppressed_leak_root
*root
;
361 FLEX_ALLOC_MEM(root
, data
, ptr
, len
);
362 root
->next
= suppressed_leaks
;
363 suppressed_leaks
= root
;