All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Howard Chu <howardchu95@gmail.com>
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,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: Re: [PATCH v3 4/8] perf trace: Pretty print struct data
Date: Wed, 28 Aug 2024 18:04:57 -0300	[thread overview]
Message-ID: <Zs-Q-ZyZKd_NzsIv@x1> (raw)
In-Reply-To: <20240824163322.60796-5-howardchu95@gmail.com>

On Sun, Aug 25, 2024 at 12:33:18AM +0800, Howard Chu wrote:
> Change the arg->augmented.args to arg->augmented.args->value to skip the
> header for customized pretty printers, since we collect data in BPF
> using the general augment_sys_enter(), which always adds the header.
> 
> Use btf_dump API to pretty print augmented struct pointer.
> 
> Prefer existed pretty-printer than btf general pretty-printer.
> 
> set compact = true and skip_names = true, so that no newline character
> and argument name are printed.
> 
> Committer notes:
> 
> Simplified the btf_dump_snprintf callback to avoid using multiple
> buffers, as discussed in the thread accessible via the Link tag below.
> 
> Signed-off-by: Howard Chu <howardchu95@gmail.com>
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Kan Liang <kan.liang@linux.intel.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Link: https://lore.kernel.org/r/20240815013626.935097-7-howardchu95@gmail.com
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/builtin-trace.c                | 65 +++++++++++++++++++++--
>  tools/perf/trace/beauty/perf_event_open.c |  2 +-
>  tools/perf/trace/beauty/sockaddr.c        |  2 +-
>  tools/perf/trace/beauty/timespec.c        |  2 +-
>  4 files changed, 63 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c
> index 43b1f63415b4..048bcb92624c 100644
> --- a/tools/perf/builtin-trace.c
> +++ b/tools/perf/builtin-trace.c
> @@ -990,6 +990,54 @@ static size_t btf_enum_scnprintf(const struct btf_type *type, struct btf *btf, c
>  	return 0;
>  }
>  
> +struct trace_btf_dump_snprintf_ctx {
> +	char   *bf;
> +	size_t printed, size;
> +};
> +
> +static void trace__btf_dump_snprintf(void *vctx, const char *fmt, va_list args)
> +{
> +	struct trace_btf_dump_snprintf_ctx *ctx = vctx;
> +
> +	ctx->printed += vscnprintf(ctx->bf + ctx->printed, ctx->size - ctx->printed, fmt, args);
> +}
> +
> +static size_t btf_struct_scnprintf(const struct btf_type *type, struct btf *btf, char *bf, size_t size, struct syscall_arg *arg)
> +{
> +	struct trace_btf_dump_snprintf_ctx ctx = {
> +		.bf   = bf,
> +		.size = size,
> +	};
> +	struct augmented_arg *augmented_arg = arg->augmented.args;
> +	int type_id = arg->fmt->type_id, consumed;
> +	struct btf_dump *btf_dump;
> +
> +	LIBBPF_OPTS(btf_dump_opts, dump_opts);
> +	LIBBPF_OPTS(btf_dump_type_data_opts, dump_data_opts);
> +
> +	if (arg == NULL || arg->augmented.args == NULL)
> +		return 0;
> +
> +	dump_data_opts.compact     = true;
> +	dump_data_opts.skip_names  = true;
> +
> +	btf_dump = btf_dump__new(btf, trace__btf_dump_snprintf, &ctx, &dump_opts);
> +	if (btf_dump == NULL)
> +		return 0;
> +
> +	/* pretty print the struct data here */
> +	if (btf_dump__dump_type_data(btf_dump, type_id, arg->augmented.args->value, type->size, &dump_data_opts) == 0)
> +		return 0;
> +
> +	consumed = sizeof(*augmented_arg) + augmented_arg->size;
> +	arg->augmented.args = ((void *)arg->augmented.args) + consumed;
> +	arg->augmented.size -= consumed;
> +
> +	btf_dump__free(btf_dump);
> +
> +	return ctx.printed;
> +}
> +
>  static size_t trace__btf_scnprintf(struct trace *trace, struct syscall_arg *arg, char *bf,
>  				   size_t size, int val, char *type)
>  {
> @@ -1009,6 +1057,8 @@ static size_t trace__btf_scnprintf(struct trace *trace, struct syscall_arg *arg,
>  
>  	if (btf_is_enum(arg_fmt->type))
>  		return btf_enum_scnprintf(arg_fmt->type, trace->btf, bf, size, val);
> +	else if (btf_is_struct(arg_fmt->type))
> +		return btf_struct_scnprintf(arg_fmt->type, trace->btf, bf, size, arg);
>  
>  	return 0;
>  }
> @@ -2222,6 +2272,7 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
>  		.show_string_prefix = trace->show_string_prefix,
>  	};
>  	struct thread_trace *ttrace = thread__priv(thread);
> +	void *default_scnprintf;
>  
>  	/*
>  	 * Things like fcntl will set this in its 'cmd' formatter to pick the
> @@ -2263,11 +2314,15 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
>  			if (trace->show_arg_names)
>  				printed += scnprintf(bf + printed, size - printed, "%s: ", field->name);
>  
> -			btf_printed = trace__btf_scnprintf(trace, &arg, bf + printed,
> -							   size - printed, val, field->type);
> -			if (btf_printed) {
> -				printed += btf_printed;
> -				continue;
> +			default_scnprintf = sc->arg_fmt[arg.idx].scnprintf;
> +
> +			if (default_scnprintf == NULL || default_scnprintf == SCA_PTR) {
> +				btf_printed = trace__btf_scnprintf(trace, &arg, bf + printed,
> +								   size - printed, val, field->type);
> +				if (btf_printed) {
> +					printed += btf_printed;
> +					continue;
> +				}

Ok, we agreed on this one, and you noted that that in this cset comment,
good.

Next time make a note after the cset commit log message and before the
actual patch, something like:

vN: prefer pre-existing userspace scnprintf if explicitely specified,
only fallbacking to the generic BTF one when none is specified.

>  			}
>  
>  			printed += syscall_arg_fmt__scnprintf_val(&sc->arg_fmt[arg.idx],
> diff --git a/tools/perf/trace/beauty/perf_event_open.c b/tools/perf/trace/beauty/perf_event_open.c
> index 01ee15fe9d0c..632237128640 100644
> --- a/tools/perf/trace/beauty/perf_event_open.c
> +++ b/tools/perf/trace/beauty/perf_event_open.c
> @@ -76,7 +76,7 @@ static size_t perf_event_attr___scnprintf(struct perf_event_attr *attr, char *bf

But this part will work if we use the old collectors in the BPF skel?

I.e. isn't this a change in the protocol of the BPF colector with the
userpace augmented snprintf routines?

If I remember we discussed that first you make this change in the
protocol, test it with the pre-existing BPF collector, it works. Ok, now
we have a new protocol and we then use it in the generic BTF-based BPF
collector. This way that option of using the BTF-based collector or the
preexisting BPF collector works.

I'll try to do this.

- Arnaldo
  
>  static size_t syscall_arg__scnprintf_augmented_perf_event_attr(struct syscall_arg *arg, char *bf, size_t size)
>  {
> -	return perf_event_attr___scnprintf((void *)arg->augmented.args, bf, size, arg->trace->show_zeros);
> +	return perf_event_attr___scnprintf((void *)arg->augmented.args->value, bf, size, arg->trace->show_zeros);
>  }
>  
>  static size_t syscall_arg__scnprintf_perf_event_attr(char *bf, size_t size, struct syscall_arg *arg)
> diff --git a/tools/perf/trace/beauty/sockaddr.c b/tools/perf/trace/beauty/sockaddr.c
> index 2e0e867c0c1b..6ecebf776899 100644
> --- a/tools/perf/trace/beauty/sockaddr.c
> +++ b/tools/perf/trace/beauty/sockaddr.c
> @@ -47,7 +47,7 @@ static size_t (*af_scnprintfs[])(struct sockaddr *sa, char *bf, size_t size) = {
>  
>  static size_t syscall_arg__scnprintf_augmented_sockaddr(struct syscall_arg *arg, char *bf, size_t size)
>  {
> -	struct sockaddr *sa = (struct sockaddr *)arg->augmented.args;
> +	struct sockaddr *sa = (struct sockaddr *)arg->augmented.args->value;
>  	char family[32];
>  	size_t printed;
>  
> diff --git a/tools/perf/trace/beauty/timespec.c b/tools/perf/trace/beauty/timespec.c
> index e1a61f092aad..b14ab72a2738 100644
> --- a/tools/perf/trace/beauty/timespec.c
> +++ b/tools/perf/trace/beauty/timespec.c
> @@ -7,7 +7,7 @@
>  
>  static size_t syscall_arg__scnprintf_augmented_timespec(struct syscall_arg *arg, char *bf, size_t size)
>  {
> -	struct timespec *ts = (struct timespec *)arg->augmented.args;
> +	struct timespec *ts = (struct timespec *)arg->augmented.args->value;
>  
>  	return scnprintf(bf, size, "{ .tv_sec: %" PRIu64 ", .tv_nsec: %" PRIu64 " }", ts->tv_sec, ts->tv_nsec);
>  }
> -- 
> 2.45.2

  reply	other threads:[~2024-08-28 21:05 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-24 16:33 [PATCH v3 0/8] perf trace: Enhanced augmentation for pointer arguments Howard Chu
2024-08-24 16:33 ` [PATCH v3 1/8] perf trace: Fix perf trace -p <PID> Howard Chu
2024-08-24 16:33 ` [PATCH v3 2/8] perf trace: Add trace__bpf_sys_enter_beauty_map() to prepare for fetching data in BPF Howard Chu
2024-09-09 19:45   ` Arnaldo Carvalho de Melo
2024-09-09 20:14     ` Arnaldo Carvalho de Melo
2024-09-10  4:59       ` Howard Chu
2024-08-24 16:33 ` [PATCH v3 3/8] perf trace: Pass the richer 'struct syscall_arg' pointer to trace__btf_scnprintf() Howard Chu
2024-08-24 16:33 ` [PATCH v3 4/8] perf trace: Pretty print struct data Howard Chu
2024-08-28 21:04   ` Arnaldo Carvalho de Melo [this message]
2024-08-30  0:16     ` Howard Chu
2024-09-09 14:40       ` Arnaldo Carvalho de Melo
2024-09-09 14:46         ` Arnaldo Carvalho de Melo
2024-08-24 16:33 ` [PATCH v3 5/8] perf trace: Pretty print buffer data Howard Chu
2024-09-09 16:33   ` Arnaldo Carvalho de Melo
2024-09-09 16:45     ` Arnaldo Carvalho de Melo
2024-09-09 16:50       ` Arnaldo Carvalho de Melo
2024-09-09 17:17       ` Howard Chu
2024-09-09 19:19         ` Arnaldo Carvalho de Melo
2024-09-09 17:14     ` Howard Chu
2024-08-24 16:33 ` [PATCH v3 6/8] perf trace: Collect augmented data using BPF Howard Chu
2024-09-04 19:52   ` Arnaldo Carvalho de Melo
2024-09-04 21:11     ` Howard Chu
2024-09-11 14:23       ` Arnaldo Carvalho de Melo
2024-08-24 16:33 ` [PATCH v3 7/8] perf trace: Add --force-btf for debugging Howard Chu
2024-08-24 16:33 ` [PATCH v3 8/8] perf trace: Add general tests for augmented syscalls Howard Chu
2024-09-09 22:11   ` Arnaldo Carvalho de Melo
2024-09-10  5:00     ` Howard Chu

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=Zs-Q-ZyZKd_NzsIv@x1 \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=adrian.hunter@intel.com \
    --cc=howardchu95@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.