]> git.ipfire.org Git - thirdparty/git.git/commitdiff
usage: trace2 BUG() invocations
authorJonathan Tan <jonathantanmy@google.com>
Fri, 5 Feb 2021 20:09:08 +0000 (12:09 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 9 Feb 2021 22:14:34 +0000 (14:14 -0800)
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>
t/helper/test-trace2.c
t/t0210-trace2-normal.sh
usage.c

index 823f33ceff4aadac07128786f01c8a38a223cdcf..f93633f895a1926aa6ea4ca712305cccb5826a2d 100644 (file)
@@ -198,6 +198,14 @@ static int ut_006data(int argc, const char **argv)
        return 0;
 }
 
+static int ut_007bug(int argc, const char **argv)
+{
+       /*
+        * Exercise BUG() to ensure that the message is printed to trace2.
+        */
+       BUG("the bug message");
+}
+
 /*
  * Usage:
  *     test-tool trace2 <ut_name_1> <ut_usage_1>
@@ -214,6 +222,7 @@ static struct unit_test ut_table[] = {
        { ut_004child,    "004child",  "[<child_command_line>]" },
        { ut_005exec,     "005exec",   "<git_command_args>" },
        { ut_006data,     "006data",   "[<category> <key> <value>]+" },
+       { ut_007bug,      "007bug",    "" },
 };
 /* clang-format on */
 
index ce7574edb1e772fca30044ceb8a7cf3ac2e19470..0cf3a63b75b7c1e5c121787b5d612cd49a89ef71 100755 (executable)
@@ -147,6 +147,25 @@ test_expect_success 'normal stream, error event' '
        test_cmp expect actual
 '
 
+# Verb 007bug
+#
+# Check that BUG writes to trace2
+
+test_expect_success 'BUG messages are written to trace2' '
+       test_when_finished "rm trace.normal actual expect" &&
+       test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 007bug &&
+       perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+       cat >expect <<-EOF &&
+               version $V
+               start _EXE_ trace2 007bug
+               cmd_name trace2 (trace2)
+               error the bug message
+               exit elapsed:_TIME_ code:99
+               atexit elapsed:_TIME_ code:99
+       EOF
+       test_cmp expect actual
+'
+
 sane_unset GIT_TRACE2_BRIEF
 
 # Now test without environment variables and get all Trace2 settings
diff --git a/usage.c b/usage.c
index 1868a24f7a5148b1714dc4a9730f343339e61dce..1b206de36d6e1cb7799fce728c2a37e310479223 100644 (file)
--- a/usage.c
+++ b/usage.c
@@ -266,6 +266,10 @@ int BUG_exit_code;
 static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
 {
        char prefix[256];
+       va_list params_copy;
+       static int in_bug;
+
+       va_copy(params_copy, params);
 
        /* truncation via snprintf is OK here */
        if (file)
@@ -274,6 +278,13 @@ static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_lis
                snprintf(prefix, sizeof(prefix), "BUG: ");
 
        vreportf(prefix, fmt, params);
+
+       if (in_bug)
+               abort();
+       in_bug = 1;
+
+       trace2_cmd_error_va(fmt, params_copy);
+
        if (BUG_exit_code)
                exit(BUG_exit_code);
        abort();