usage: trace2 BUG() invocations
die() messages are traced in trace2, but BUG() messages are not. Anyone tracking die() messages would have even more reason to track BUG(). Therefore, write to trace2 when BUG() is invoked. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Tan committed
Feb 5, 2021 at 12:09 UTC
0a9dde4a04c1228025d292f44113cb8f9cebbfba
3 files changed
+39
t/helper/test-trace2.c
+9
@@ -198,6 +198,14 @@ static int ut_006data(int argc, const char **argv)
198
return 0;
199
}
200
201
+static int ut_007bug(int argc, const char **argv)
202
+{
203
+ /*
204
+ * Exercise BUG() to ensure that the message is printed to trace2.
205
+ */
206
+ BUG("the bug message");
207
+}
208
+
209
/*
210
* Usage:
211
* test-tool trace2 <ut_name_1> <ut_usage_1>
@@ -214,6 +222,7 @@ static struct unit_test ut_table[] = {
222
{ ut_004child, "004child", "[<child_command_line>]" },
223
{ ut_005exec, "005exec", "<git_command_args>" },
224
{ ut_006data, "006data", "[<category> <key> <value>]+" },
225
+ { ut_007bug, "007bug", "" },
226
};
227
/* clang-format on */
228
t/t0210-trace2-normal.sh
+19
@@ -147,6 +147,25 @@ test_expect_success 'normal stream, error event' '
147
test_cmp expect actual
148
'
149
150
+# Verb 007bug
151
+#
152
+# Check that BUG writes to trace2
153
+
154
+test_expect_success 'BUG messages are written to trace2' '
155
+ test_when_finished "rm trace.normal actual expect" &&
156
+ test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 007bug &&
157
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
158
+ cat >expect <<-EOF &&
159
+ version $V
160
+ start _EXE_ trace2 007bug
161
+ cmd_name trace2 (trace2)
162
+ error the bug message
163
+ exit elapsed:_TIME_ code:99
164
+ atexit elapsed:_TIME_ code:99
165
+ EOF
166
+ test_cmp expect actual
167
+'
168
+
169
sane_unset GIT_TRACE2_BRIEF
170
171
# Now test without environment variables and get all Trace2 settings
usage.c
+11
@@ -266,6 +266,10 @@ int BUG_exit_code;
266
static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
267
{
268
char prefix[256];
269
+ va_list params_copy;
270
+ static int in_bug;
271
+
272
+ va_copy(params_copy, params);
273
274
/* truncation via snprintf is OK here */
275
if (file)
@@ -274,6 +278,13 @@ static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_lis
278
snprintf(prefix, sizeof(prefix), "BUG: ");
279
280
vreportf(prefix, fmt, params);
281
+
282
+ if (in_bug)
283
+ abort();
284
+ in_bug = 1;
285
+
286
+ trace2_cmd_error_va(fmt, params_copy);
287
+
288
if (BUG_exit_code)
289
exit(BUG_exit_code);
290
abort();