linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Howard Chu <howardchu95@gmail.com>
To: acme@kernel.org
Cc: adrian.hunter@intel.com, irogers@google.com, jolsa@kernel.org,
	kan.liang@linux.intel.com, namhyung@kernel.org,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2 07/10] perf trace: Pretty print buffer data
Date: Thu, 15 Aug 2024 09:36:23 +0800	[thread overview]
Message-ID: <20240815013626.935097-8-howardchu95@gmail.com> (raw)
In-Reply-To: <20240815013626.935097-1-howardchu95@gmail.com>

Define TRACE_AUG_MAX_BUF in trace_augment.h data, which is the maximum
buffer size we can augment. BPF will include this header too.

Print buffer in a way that's different than just printing a string, we
print all the control characters in \digits (such as \0 for null, and
\10 for newline, LF).

For character that has a bigger value than 127, we print the digits
instead of the character itself as well.

Signed-off-by: Howard Chu <howardchu95@gmail.com>
---
 tools/perf/builtin-trace.c      | 48 +++++++++++++++++++++++++++++++++
 tools/perf/util/trace_augment.h |  6 +++++
 2 files changed, 54 insertions(+)
 create mode 100644 tools/perf/util/trace_augment.h

diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
index e7421128f589..593b0b8724d0 100644
--- a/tools/perf/builtin-trace.c
+++ b/tools/perf/builtin-trace.c
@@ -65,6 +65,7 @@
 #include "syscalltbl.h"
 #include "rb_resort.h"
 #include "../perf.h"
+#include "trace_augment.h"
 
 #include <errno.h>
 #include <inttypes.h>
@@ -852,6 +853,10 @@ static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
 
 #define SCA_FILENAME syscall_arg__scnprintf_filename
 
+static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg);
+
+#define SCA_BUF syscall_arg__scnprintf_buf
+
 static size_t syscall_arg__scnprintf_pipe_flags(char *bf, size_t size,
 						struct syscall_arg *arg)
 {
@@ -1760,6 +1765,47 @@ static size_t syscall_arg__scnprintf_filename(char *bf, size_t size,
 	return 0;
 }
 
+#define MAX_CONTROL_CHAR 31
+#define MAX_ASCII 127
+
+static size_t syscall_arg__scnprintf_buf(char *bf, size_t size, struct syscall_arg *arg)
+{
+	char result[TRACE_AUG_MAX_BUF * 4], tens[4];
+	struct augmented_arg *augmented_arg = arg->augmented.args;
+	unsigned char *orig;
+	int i = 0, n, consumed, digits;
+
+	if (augmented_arg == NULL)
+		return 0;
+
+	orig = (unsigned char *)augmented_arg->value;
+	n = augmented_arg->size;
+
+	memset(result, 0, sizeof(result));
+
+	for (int j = 0; j < n && i < (int)sizeof(result) - 1; ++j) {
+		/* print control characters (0~31 and 127), and non-ascii characters in \(digits) */
+		if (orig[j] <= MAX_CONTROL_CHAR || orig[j] >= MAX_ASCII) {
+			result[i++] = '\\';
+
+			/* convert to digits */
+			digits = scnprintf(tens, sizeof(result) - i, "%d", (int)orig[j]);
+			if (digits + i <= (int)sizeof(result) - 1) {
+				strncpy(result + i, tens, digits);
+				i += digits;
+			}
+		} else  {
+			result[i++] = orig[j];
+		}
+	}
+
+	consumed = sizeof(*augmented_arg) + augmented_arg->size;
+	arg->augmented.args = ((void *)arg->augmented.args) + consumed;
+	arg->augmented.size -= consumed;
+
+	return scnprintf(bf, size, "\"%s\"", result);
+}
+
 static bool trace__filter_duration(struct trace *trace, double t)
 {
 	return t < (trace->duration_filter * NSEC_PER_MSEC);
@@ -1977,6 +2023,8 @@ syscall_arg_fmt__init_array(struct syscall_arg_fmt *arg, struct tep_format_field
 		     strstr(field->name, "type") ||
 		     strstr(field->name, "description")))
 			arg->scnprintf = SCA_FILENAME;
+		else if (strstr(field->type, "char *") && strstr(field->name, "buf"))
+			arg->scnprintf = SCA_BUF;
 		else if ((field->flags & TEP_FIELD_IS_POINTER) || strstr(field->name, "addr"))
 			arg->scnprintf = SCA_PTR;
 		else if (strcmp(field->type, "pid_t") == 0)
diff --git a/tools/perf/util/trace_augment.h b/tools/perf/util/trace_augment.h
new file mode 100644
index 000000000000..57a3e5045937
--- /dev/null
+++ b/tools/perf/util/trace_augment.h
@@ -0,0 +1,6 @@
+#ifndef TRACE_AUGMENT_H
+#define TRACE_AUGMENT_H
+
+#define TRACE_AUG_MAX_BUF 32 /* for buffer augmentation in perf trace */
+
+#endif
-- 
2.45.2


  parent reply	other threads:[~2024-08-15  1:37 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-15  1:36 [PATCH v2 00/10] perf trace: Enhanced augmentation for pointer arguments Howard Chu
2024-08-15  1:36 ` [PATCH v2 01/10] perf trace: Fix perf trace -p <PID> Howard Chu
2024-08-15 18:28   ` Ian Rogers
2024-08-16 14:52     ` Arnaldo Carvalho de Melo
2024-08-16 17:25       ` Howard Chu
2024-08-15  1:36 ` [PATCH v2 02/10] perf trace: Change some comments Howard Chu
2024-08-16 14:58   ` Arnaldo Carvalho de Melo
2024-08-15  1:36 ` [PATCH v2 03/10] perf trace: Add trace__bpf_sys_enter_beauty_map() to prepare for fetching data in BPF Howard Chu
2024-08-22 17:49   ` Arnaldo Carvalho de Melo
2024-08-22 17:53     ` Arnaldo Carvalho de Melo
2024-08-22 21:09       ` Arnaldo Carvalho de Melo
2024-08-23  4:09         ` Howard Chu
2024-08-15  1:36 ` [PATCH v2 04/10] perf trace: Add some string arguments' name in syscall_arg_fmt__init_array() Howard Chu
2024-08-22 22:14   ` Arnaldo Carvalho de Melo
2024-08-23  4:37     ` Howard Chu
2024-08-23 13:17       ` Arnaldo Carvalho de Melo
2024-08-15  1:36 ` [PATCH v2 05/10] perf trace: Add a new argument to trace__btf_scnprintf() Howard Chu
2024-08-22 18:00   ` Arnaldo Carvalho de Melo
2024-08-22 18:13     ` Arnaldo Carvalho de Melo
2024-08-23  4:05       ` Howard Chu
2024-08-15  1:36 ` [PATCH v2 06/10] perf trace: Pretty print struct data Howard Chu
2024-08-23 12:41   ` Arnaldo Carvalho de Melo
2024-08-23 13:15     ` Arnaldo Carvalho de Melo
2024-08-15  1:36 ` Howard Chu [this message]
2024-08-23 14:17   ` [PATCH v2 07/10] perf trace: Pretty print buffer data Arnaldo Carvalho de Melo
2024-08-15  1:36 ` [PATCH v2 08/10] perf trace: Add pids_allowed and rename pids_filtered Howard Chu
2024-08-15  1:36 ` [PATCH v2 09/10] perf trace: Collect augmented data using BPF Howard Chu
2024-08-23 13:24   ` Arnaldo Carvalho de Melo
2024-08-23 13:38     ` Arnaldo Carvalho de Melo
2024-08-23 13:42       ` Arnaldo Carvalho de Melo
2024-08-23 14:23         ` Arnaldo Carvalho de Melo
2024-08-15  1:36 ` [PATCH v2 10/10] perf trace: Add general tests for augmented syscalls Howard Chu
2024-08-16  3:15   ` Ian Rogers

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=20240815013626.935097-8-howardchu95@gmail.com \
    --to=howardchu95@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --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;
as well as URLs for NNTP newsgroup(s).