Skip to content

Commit 0a9dde4

Browse files
jonathantanmygitster
authored andcommitted
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 <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 773e25a commit 0a9dde4

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

t/helper/test-trace2.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ static int ut_006data(int argc, const char **argv)
198198
return 0;
199199
}
200200

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+
201209
/*
202210
* Usage:
203211
* test-tool trace2 <ut_name_1> <ut_usage_1>
@@ -214,6 +222,7 @@ static struct unit_test ut_table[] = {
214222
{ ut_004child, "004child", "[<child_command_line>]" },
215223
{ ut_005exec, "005exec", "<git_command_args>" },
216224
{ ut_006data, "006data", "[<category> <key> <value>]+" },
225+
{ ut_007bug, "007bug", "" },
217226
};
218227
/* clang-format on */
219228

t/t0210-trace2-normal.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,25 @@ test_expect_success 'normal stream, error event' '
147147
test_cmp expect actual
148148
'
149149

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+
150169
sane_unset GIT_TRACE2_BRIEF
151170

152171
# Now test without environment variables and get all Trace2 settings

usage.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ int BUG_exit_code;
266266
static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
267267
{
268268
char prefix[256];
269+
va_list params_copy;
270+
static int in_bug;
271+
272+
va_copy(params_copy, params);
269273

270274
/* truncation via snprintf is OK here */
271275
if (file)
@@ -274,6 +278,13 @@ static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_lis
274278
snprintf(prefix, sizeof(prefix), "BUG: ");
275279

276280
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+
277288
if (BUG_exit_code)
278289
exit(BUG_exit_code);
279290
abort();

0 commit comments

Comments
 (0)