From: Marek Pazur <mpazur@redhat.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Howard Chu <howardchu95@gmail.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] perf trace: Quote and escape augmented buffer contents
Date: Wed, 2 Dec 2026 13:16:22 +0100 [thread overview]
Message-ID: <20261202121622.30071-3-mpazur@redhat.com> (raw)
In-Reply-To: <20261202121622.30071-1-mpazur@redhat.com>
When using perf trace, syscall_arg__scnprintf_buf() pretty-prints
augmented buf arguments without delimiters. Since the payload can
contain arbitrary characters (such as commas, quotes or brackets),
the resulting trace output is ambiguous to parse reliably in automated
tests. Additionally, literal backslashes and escape sequences are not
differentiated.
Before:
write(fd: 1, buf: 123, fake_argument: 456, count: 23)
write(fd: 1, buf: 98171 write(fd: 13, buf: write(f, count: 33)
write(fd: 1, buf: \0, count: 1) // buf = {0}
write(fd: 1, buf: \0, count: 2) // buf = {'\\', '0'}
write(fd: 1, buf: "Hello world!", count: 14)
The quotes were present in the original submission (v2 used "\"%s\""),
but got removed during the review process [1].
Enclose the buf contents in double quotes and backslash-escape '"'
and '\', matching strace's formatting style:
write(fd: 1, buf: "123, fake_argument: 456", count: 23)
write(fd: 1, buf: "98171 write(fd: 13, buf: write(f", count: 33)
write(fd: 1, buf: "\0", count: 1) // buf = {0}
write(fd: 1, buf: "\\0", count: 2) // buf = {'\\', '0'}
write(fd: 1, buf: "\"Hello world!\"", count: 14)
Update the buffer augmentation shell tests to account for the new format.
[1] https://lore.kernel.org/all/ZsiaBnd5KrkGbZ9w@x1/
Signed-off-by: Marek Pazur <mpazur@redhat.com>
---
tools/perf/builtin-trace.c | 14 ++++++++++++--
tools/perf/tests/shell/trace_btf_general.sh | 2 +-
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index c08f2c3a8128..3b61bc895aa3 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -1964,26 +1964,36 @@ static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_a
struct augmented_arg *augmented_arg = arg->augmented.args;
size_t printed = 0;
unsigned char *orig;
int consumed;
if (!syscall_arg__augmented_args_valid(arg, 0))
return 0;
orig = (unsigned char *)augmented_arg->value;
+ printed += scnprintf(bf + printed, size - printed, "\"");
+
for (int j = 0; j < augmented_arg->size; ++j) {
bool control_char = orig[j] <= MAX_CONTROL_CHAR || orig[j] >= MAX_ASCII;
- /* print control characters (0~31 and 127), and non-ascii characters in \(digits) */
- printed += scnprintf(bf + printed, size - printed, control_char ? "\\%d" : "%c", (int)orig[j]);
+ bool escaped = orig[j] == '"' || orig[j] == '\\';
+ /*
+ * Print control characters (0~31 and 127), and non-ascii
+ * characters in \(digits). Escape '"' and '\'.
+ */
+ printed += scnprintf(bf + printed, size - printed,
+ control_char ? "\\%d" : escaped ? "\\%c" : "%c",
+ (int)orig[j]);
}
+ printed += scnprintf(bf + printed, size - printed, "\"");
+
consumed = sizeof(*augmented_arg) + augmented_arg->size;
arg->augmented.args = ((void *)arg->augmented.args) + consumed;
arg->augmented.size -= consumed;
return printed;
}
static bool trace__filter_duration(struct trace *trace, double t)
{
return t < (trace->duration_filter * NSEC_PER_MSEC);
diff --git a/tools/perf/tests/shell/trace_btf_general.sh b/tools/perf/tests/shell/trace_btf_general.sh
index 0941459a7137..7784e93645d1 100755
--- a/tools/perf/tests/shell/trace_btf_general.sh
+++ b/tools/perf/tests/shell/trace_btf_general.sh
@@ -31,21 +31,21 @@ trace_test_string() {
then
printf "String augmentation test failed, output:\n%s\n" "$output"
err=1
fi
}
trace_test_buffer() {
echo "Testing perf trace's buffer augmentation"
# echo will insert a newline (\10) at the end of the buffer
output="$(perf trace --sort-events -e write -- echo "${buffer}" 2>&1)"
- if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, ${buffer}.*, [0-9]+\) += +[0-9]+$"
+ if ! echo "$output" | grep -q -E "^echo/[0-9]+ write\([0-9]+, \"${buffer}.*\", [0-9]+\) += +[0-9]+$"
then
printf "Buffer augmentation test failed, output:\n%s\n" "$output"
err=1
fi
}
trace_test_struct_btf() {
echo "Testing perf trace's struct augmentation"
output="$(perf trace --sort-events -e clock_nanosleep --force-btf -- sleep 1 2>&1)"
if ! echo "$output" | grep -q -E "^sleep/[0-9]+ clock_nanosleep\(0, 0, \{1,.*\}, 0x[0-9a-f]+\) += +[0-9]+$"
--
2.55.0
next prev parent reply other threads:[~2026-12-02 12:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 12:16 [PATCH 0/2] perf trace: Improve augmented buf output formatting, fix test output Marek Pazur
2026-12-02 12:16 ` [PATCH 1/2] perf test trace_btf_general: Use %s format specifier when printing " Marek Pazur
2026-12-02 12:16 ` Marek Pazur [this message]
2026-12-02 12:19 ` [PATCH 0/2] perf trace: Improve augmented buf output formatting, fix " sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202121622.30071-3-mpazur@redhat.com \
--to=mpazur@redhat.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=howardchu95@gmail.com \
--cc=irogers@google.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=namhyung@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox