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 *p
= '\0'; /* vsnprintf() failed, clip at prefix */
25 for (; p
!= pend
- 1 && *p
; p
++) {
26 if (iscntrl(*p
) && *p
!= '\t' && *p
!= '\n')
30 *(p
++) = '\n'; /* we no longer need a NUL */
32 write_in_full(2, msg
, p
- msg
);
35 static NORETURN
void usage_builtin(const char *err
, va_list params
)
37 vreportf(_("usage: "), err
, params
);
40 * When we detect a usage error *before* the command dispatch in
41 * cmd_main(), we don't know what verb to report. Force it to this
42 * to facilitate post-processing.
44 trace2_cmd_name("_usage_");
47 * Currently, the (err, params) are usually just the static usage
48 * string which isn't very useful here. Usually, the call site
49 * manually calls fprintf(stderr,...) with the actual detailed
50 * syntax error before calling usage().
52 * TODO It would be nice to update the call sites to pass both
53 * the static usage string and the detailed error message.
59 static void die_message_builtin(const char *err
, va_list params
)
61 trace2_cmd_error_va(err
, params
);
62 vreportf(_("fatal: "), err
, params
);
66 * We call trace2_cmd_error_va() in the below functions first and
67 * expect it to va_copy 'params' before using it (because an 'ap' can
68 * only be walked once).
70 static NORETURN
void die_builtin(const char *err
, va_list params
)
72 report_fn die_message_fn
= get_die_message_routine();
74 die_message_fn(err
, params
);
78 static void error_builtin(const char *err
, va_list params
)
80 trace2_cmd_error_va(err
, params
);
82 vreportf(_("error: "), err
, params
);
85 static void warn_builtin(const char *warn
, va_list params
)
87 trace2_cmd_error_va(warn
, params
);
89 vreportf(_("warning: "), warn
, params
);
92 static int die_is_recursing_builtin(void)
96 * Just an arbitrary number X where "a < x < b" where "a" is
97 * "maximum number of pthreads we'll ever plausibly spawn" and
98 * "b" is "something less than Inf", since the point is to
99 * prevent infinite recursion.
101 static const int recursion_limit
= 1024;
104 if (dying
> recursion_limit
) {
106 } else if (dying
== 2) {
107 warning("die() called many times. Recursion error or racy threaded death!");
114 /* If we are in a dlopen()ed .so write to a global variable would segfault
115 * (ugh), so keep things static. */
116 static NORETURN_PTR report_fn usage_routine
= usage_builtin
;
117 static NORETURN_PTR report_fn die_routine
= die_builtin
;
118 static report_fn die_message_routine
= die_message_builtin
;
119 static report_fn error_routine
= error_builtin
;
120 static report_fn warn_routine
= warn_builtin
;
121 static int (*die_is_recursing
)(void) = die_is_recursing_builtin
;
123 void set_die_routine(NORETURN_PTR report_fn routine
)
125 die_routine
= routine
;
128 report_fn
get_die_message_routine(void)
130 return die_message_routine
;
133 void set_error_routine(report_fn routine
)
135 error_routine
= routine
;
138 report_fn
get_error_routine(void)
140 return error_routine
;
143 void set_warn_routine(report_fn routine
)
145 warn_routine
= routine
;
148 report_fn
get_warn_routine(void)
153 void set_die_is_recursing_routine(int (*routine
)(void))
155 die_is_recursing
= routine
;
158 void NORETURN
usagef(const char *err
, ...)
162 va_start(params
, err
);
163 usage_routine(err
, params
);
167 void NORETURN
usage(const char *err
)
172 void NORETURN
die(const char *err
, ...)
176 if (die_is_recursing()) {
177 fputs("fatal: recursion detected in die handler\n", stderr
);
181 va_start(params
, err
);
182 die_routine(err
, params
);
186 static const char *fmt_with_err(char *buf
, int n
, const char *fmt
)
188 char str_error
[256], *err
;
191 err
= strerror(errno
);
192 for (i
= j
= 0; err
[i
] && j
< sizeof(str_error
) - 1; ) {
193 if ((str_error
[j
++] = err
[i
++]) != '%')
195 if (j
< sizeof(str_error
) - 1) {
196 str_error
[j
++] = '%';
198 /* No room to double the '%', so we overwrite it with
205 /* Truncation is acceptable here */
206 snprintf(buf
, n
, "%s: %s", fmt
, str_error
);
210 void NORETURN
die_errno(const char *fmt
, ...)
215 if (die_is_recursing()) {
216 fputs("fatal: recursion detected in die_errno handler\n",
221 va_start(params
, fmt
);
222 die_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
227 int die_message(const char *err
, ...)
231 va_start(params
, err
);
232 die_message_routine(err
, params
);
237 #undef die_message_errno
238 int die_message_errno(const char *fmt
, ...)
243 va_start(params
, fmt
);
244 die_message_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
250 int error_errno(const char *fmt
, ...)
255 va_start(params
, fmt
);
256 error_routine(fmt_with_err(buf
, sizeof(buf
), fmt
), params
);
262 int error(const char *err
, ...)
266 va_start(params
, err
);
267 error_routine(err
, params
);
272 void warning_errno(const char *warn
, ...)
277 va_start(params
, warn
);
278 warn_routine(fmt_with_err(buf
, sizeof(buf
), warn
), params
);
282 void warning(const char *warn
, ...)
286 va_start(params
, warn
);
287 warn_routine(warn
, params
);
291 /* Only set this, ever, from t/helper/, when verifying that bugs are caught. */
294 static void BUG_vfl_common(const char *file
, int line
, const char *fmt
,
299 /* truncation via snprintf is OK here */
300 snprintf(prefix
, sizeof(prefix
), "BUG: %s:%d: ", file
, line
);
302 vreportf(prefix
, fmt
, params
);
305 static NORETURN
void BUG_vfl(const char *file
, int line
, const char *fmt
, va_list params
)
310 va_copy(params_copy
, params
);
311 BUG_vfl_common(file
, line
, fmt
, params
);
317 trace2_cmd_error_va(fmt
, params_copy
);
324 NORETURN
void BUG_fl(const char *file
, int line
, const char *fmt
, ...)
328 bug_called_must_BUG
= 0;
331 BUG_vfl(file
, line
, fmt
, ap
);
335 int bug_called_must_BUG
;
336 void bug_fl(const char *file
, int line
, const char *fmt
, ...)
340 bug_called_must_BUG
= 1;
343 BUG_vfl_common(file
, line
, fmt
, ap
);
347 trace2_cmd_error_va(fmt
, ap
);
351 #ifdef SUPPRESS_ANNOTATED_LEAKS
352 void unleak_memory(const void *ptr
, size_t len
)
354 static struct suppressed_leak_root
{
355 struct suppressed_leak_root
*next
;
356 char data
[FLEX_ARRAY
];
358 struct suppressed_leak_root
*root
;
360 FLEX_ALLOC_MEM(root
, data
, ptr
, len
);
361 root
->next
= suppressed_leaks
;
362 suppressed_leaks
= root
;