[PATCH] trace-cmd report: keep a separator before long timestamps
CaoRuichuang <[email protected]> Mon, 6 Apr 2026 15:03:44 +0800
| Newsgroups | org.kernel.vger.linux-trace-devel |
|---|---|
| Message-ID | <[email protected]> |
trace-cmd report formats normal nanosecond timestamps with "%12d:". That relies on the field width to provide the separator before the timestamp. With --raw-ts and a large --ts-offset, the timestamp can grow beyond 12 digits. Once that happens, the padding disappears and the output runs into the raw flags field, producing lines such as: [003] dNh2.749686305812356: Make the normal timestamp format include an explicit leading space so the separator remains present regardless of the timestamp width. Add a unit test that exercises report --raw-ts with a large timestamp offset and checks both the fixed and the broken output forms. Link: https://bugzilla.kernel.org/show_bug.cgi?id=221303 Signed-off-by: CaoRuichuang <[email protected]> --- tracecmd/trace-read.c | 2 +- utest/tracecmd-utest.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tracecmd/trace-read.c b/tracecmd/trace-read.c index 355ee0f4..aef9b4c5 100644 --- a/tracecmd/trace-read.c +++ b/tracecmd/trace-read.c @@ -170,7 +170,7 @@ static const char *time_format(struct tracecmd_input *handle, enum time_fmt tf) else return tf == TIME_FMT_NORMAL ? " %6.1000d:" : "%6.1000d"; } else - return tf == TIME_FMT_NORMAL ? "%12d:" : "%12d"; + return tf == TIME_FMT_NORMAL ? " %12d:" : "%12d"; } } diff --git a/utest/tracecmd-utest.c b/utest/tracecmd-utest.c index c18fb2ea..cc487d13 100644 --- a/utest/tracecmd-utest.c +++ b/utest/tracecmd-utest.c @@ -445,6 +445,28 @@ static void test_trace_convert6(void) CU_TEST(ret == 0); } +static void test_trace_report_long_timestamp_separator(void) +{ + struct stat st; + int ret; + + /* If the trace data is already created, just use it, otherwise make it again */ + if (stat(TRACECMD_FILE, &st) < 0) { + ret = run_trace("record", TRACECMD_OUT, "-e", "sched", "sleep", "1", NULL); + CU_TEST(ret == 0); + } + + ret = grep_match("\\[[0-9][0-9][0-9]\\] [^ ]*\\. [0-9]\\{12,\\}:", + "report", TRACECMD_IN, "--raw-ts", + "--ts-offset", "100000000000000", NULL); + CU_TEST(ret == 0); + + ret = grep_match("\\[[0-9][0-9][0-9]\\] [^ ]*\\.[0-9]\\{12,\\}:", + "report", TRACECMD_IN, "--raw-ts", + "--ts-offset", "100000000000000", NULL); + CU_TEST(ret == 1); +} + struct callback_data { long counter; struct trace_seq seq; @@ -798,6 +820,8 @@ void test_tracecmd_lib(void) test_trace_sqlhist_hist); CU_add_test(suite, "Test convert from v7 to v6", test_trace_convert6); + CU_add_test(suite, "Keep separator before long timestamps", + test_trace_report_long_timestamp_separator); CU_add_test(suite, "Use libraries to read file", test_trace_library_read); CU_add_test(suite, "Use libraries to read file incremental", -- 2.39.5 (Apple Git-154)