[PATCH 2/2] perf trace: Quote and escape augmented buffer contents
From: Marek Pazur
Date: Wed Dec 02 2026 - 07:16:45 EST
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@xxxxxxxxxx>
---
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