From nobody Sun Dec 14 19:35:46 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CFC523A993 for ; Wed, 21 May 2025 22:27:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866456; cv=none; b=TgGOdGx5XdwEe4KfrjK6L1DYF219I4wadtUTX8ikR/rqJcctAGDuzF/AsT38yIYB0IYAWulsUI6defodnIUPS201dfmlWJDAE/5k++d3PPRURuHpK7HVIyC11TDY1d17UtEoimRvTxpPL9GS7gxQoypX4JBDW3ljz4zmT9B+E6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866456; c=relaxed/simple; bh=aMZ4AYJ3M/DhRPmbWLQpDVxaBCdOgCFI+6QJcEMCLII=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CWOppszWJ8lQCeoOojtowefai3ExtWZdbga6cIXZAOURYLD9tYXN5AX4KWkpzAkwjD91Ip31+PLUJWvW9hvk/Cq3+voFtsVtwf6TAPaBU6vvMLkjfaKOreVlgml4dLZQplGOar4Sne/No2qw6R4n6GQwAASNrpMJzT4jD83+6RQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=KkqgLQAb; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="KkqgLQAb" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7377139d8b1so6277169b3a.0 for ; Wed, 21 May 2025 15:27:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747866454; x=1748471254; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QFzAdIN3ADd6NVAF4nynmA4pWoTI5Or9NT8etnM6Hdc=; b=KkqgLQAbtYA8wqzYtflF+sNQNJRgL1Cd/RY9BQBYxTSOoeXfawLQmCh++poGJvO/+8 ZeNPftrtXZQ31eGXAsyalrPzL7vydUChzWb8YEaXZBAKf/G0hocWP/Op2cjea3vh/wak 3onAKDkRaAizSuhoQ/0zkWvNWa+MQVO0l7EDmHXQGkR4+PHLP5OboSMYr00Eo6UDzq6n PyqO7cRB/LL9VP6Xq4Rbgb0DIRUXEymVNxtGXVBQDfbRnYPUXuulPeCyXoSHVZnxC8kh LuLSSSp8e6TgG7dEDzRDyWz9KRbsUgRmdj2U8YmbtZhMhUzT5kq1pgozaniGbj/IgC1n uf+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747866454; x=1748471254; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QFzAdIN3ADd6NVAF4nynmA4pWoTI5Or9NT8etnM6Hdc=; b=ZPGsqzUorRA7joPs5GclgRfDVCPzZN0W0vCLmWdY+62qg8Wrcn4R9GxkS1Ip6kp9G4 KP8l/C7RX7mbLBq+P4lJ8ejOgx/0UTcia8HxyoO2syvd9NWhKenq2WOI9Qve6rO33l3G cLbvGqvN77GPchf8uj6QcdKPhe7mk0ODSChNa185aKtogPfjoGPWdCbBtJRJX1Y0O46U PRDBiTUHAmIWMogi5bHdnhK/j6/JUSxerv0+Jyiqen5yWKn5vnxfaKx/EmiPpKVskLhq 454/uHI7Tlm9f407RegiAdJpfkZIs3XYUtBC7CeIeEAXNEwL5R+/vDsF1bmu53JoDEFC 0Apw== X-Forwarded-Encrypted: i=1; AJvYcCX8zCEY0zxFcoCZ52XnBm0SEgWJ9twqCwfeetb1QIHAH8PAnj/jt2ya2uFimblAwklY85A1xYhlqPbZDPU=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6NiFe806mThgLvp8UJD8O5mgy5Xbup9suwM0rgAZcBffS/kfq 7GjyRw1LoZ6YSwAMbgNvUNyI/nlbnCAah7mSHVBRqgX9fkl+tfxp9i+JrayTyHxONAh19pghkrL IzjXl+sZGc/G9UElEyMOnOg== X-Google-Smtp-Source: AGHT+IHlq2IKbk0B/4Exi52CVMnW2wIsd2uu9LhDEluot7rft40H1p6F/fVChZv0umGaaYkzUtyGx+eovn4ubOGg X-Received: from pfxa34.prod.google.com ([2002:a05:6a00:1d22:b0:73e:1cf2:cd5c]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:432a:b0:215:eac9:1ab2 with SMTP id adf61e73a8af0-216219f04e0mr36376303637.28.1747866454479; Wed, 21 May 2025 15:27:34 -0700 (PDT) Date: Wed, 21 May 2025 15:27:23 -0700 In-Reply-To: <20250521222725.3895192-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250521222725.3895192-1-blakejones@google.com> X-Mailer: git-send-email 2.49.0.1143.g0be31eac6b-goog Message-ID: <20250521222725.3895192-2-blakejones@google.com> Subject: [PATCH 1/3] perf: add support for printing BTF character arrays as strings From: Blake Jones To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , Kan Liang Cc: Chun-Tse Shao , Zhongqiu Han , James Clark , Charlie Jenkins , Andi Kleen , Dmitry Vyukov , Leo Yan , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The BTF dumper code currently displays arrays of characters as just that - arrays, with each character formatted individually. Sometimes this is what makes sense, but it's nice to be able to treat that array as a string. This change adds a special case to the btf_dump functionality to allow arrays of single-byte integer values to be printed as character strings. Characters for which isprint() returns false are printed as hex-escaped values. This is enabled when the new ".print_strings" is set to 1 in the btf_dump_type_data_opts structure. As an example, here's what it looks like to dump the string "hello" using a few different field values for btf_dump_type_data_opts (.compact =3D 1): - .print_strings =3D 0, .skip_names =3D 0: (char[6])['h','e','l','l','o',] - .print_strings =3D 0, .skip_names =3D 1: ['h','e','l','l','o',] - .print_strings =3D 1, .skip_names =3D 0: (char[6])"hello" - .print_strings =3D 1, .skip_names =3D 1: "hello" Here's the string "h\xff", dumped with .compact =3D 1 and .skip_names =3D 1: - .print_strings =3D 0: ['h',-1,] - .print_strings =3D 1: "h\xff" Signed-off-by: Blake Jones --- tools/lib/bpf/btf.h | 3 ++- tools/lib/bpf/btf_dump.c | 51 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 4392451d634b..be8e8e26d245 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -326,9 +326,10 @@ struct btf_dump_type_data_opts { bool compact; /* no newlines/indentation */ bool skip_names; /* skip member/type names */ bool emit_zeroes; /* show 0-valued fields */ + bool print_strings; /* print char arrays as strings */ size_t :0; }; -#define btf_dump_type_data_opts__last_field emit_zeroes +#define btf_dump_type_data_opts__last_field print_strings =20 LIBBPF_API int btf_dump__dump_type_data(struct btf_dump *d, __u32 id, diff --git a/tools/lib/bpf/btf_dump.c b/tools/lib/bpf/btf_dump.c index 460c3e57fadb..a07dd5accdd8 100644 --- a/tools/lib/bpf/btf_dump.c +++ b/tools/lib/bpf/btf_dump.c @@ -75,6 +75,7 @@ struct btf_dump_data { bool is_array_member; bool is_array_terminated; bool is_array_char; + bool print_strings; }; =20 struct btf_dump { @@ -2028,6 +2029,50 @@ static int btf_dump_var_data(struct btf_dump *d, return btf_dump_dump_type_data(d, NULL, t, type_id, data, 0, 0); } =20 +static int btf_dump_string_data(struct btf_dump *d, + const struct btf_type *t, + __u32 id, + const void *data) +{ + const struct btf_array *array =3D btf_array(t); + __u32 i; + + if (!btf_is_int(skip_mods_and_typedefs(d->btf, array->type, NULL)) || + btf__resolve_size(d->btf, array->type) !=3D 1 || + !d->typed_dump->print_strings) { + pr_warn("unexpected %s() call for array type %u\n", + __func__, array->type); + return -EINVAL; + } + + btf_dump_data_pfx(d); + btf_dump_printf(d, "\""); + + for (i =3D 0; i < array->nelems; i++, data++) { + char c; + + if (data >=3D d->typed_dump->data_end) + return -E2BIG; + + c =3D *(char *)data; + if (c =3D=3D '\0') { + /* When printing character arrays as strings, NUL bytes + * are always treated as string terminators; they are + * never printed. + */ + break; + } + if (isprint(c)) + btf_dump_printf(d, "%c", c); + else + btf_dump_printf(d, "\\x%02x", *(__u8 *)data); + } + + btf_dump_printf(d, "\""); + + return 0; +} + static int btf_dump_array_data(struct btf_dump *d, const struct btf_type *t, __u32 id, @@ -2055,8 +2100,11 @@ static int btf_dump_array_data(struct btf_dump *d, * char arrays, so if size is 1 and element is * printable as a char, we'll do that. */ - if (elem_size =3D=3D 1) + if (elem_size =3D=3D 1) { + if (d->typed_dump->print_strings) + return btf_dump_string_data(d, t, id, data); d->typed_dump->is_array_char =3D true; + } } =20 /* note that we increment depth before calling btf_dump_print() below; @@ -2544,6 +2592,7 @@ int btf_dump__dump_type_data(struct btf_dump *d, __u3= 2 id, d->typed_dump->compact =3D OPTS_GET(opts, compact, false); d->typed_dump->skip_names =3D OPTS_GET(opts, skip_names, false); d->typed_dump->emit_zeroes =3D OPTS_GET(opts, emit_zeroes, false); + d->typed_dump->print_strings =3D OPTS_GET(opts, print_strings, false); =20 ret =3D btf_dump_dump_type_data(d, NULL, t, id, data, 0, 0); =20 --=20 2.49.0.1143.g0be31eac6b-goog From nobody Sun Dec 14 19:35:46 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2924F23BCF3 for ; Wed, 21 May 2025 22:27:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866459; cv=none; b=Le/TUqvVPs4QGyMwRYTT5tjZDoiKYcWtemp6lHiEszwjhrReppKv3q/HFzlq/1Kf+2M5rdAtJihdRgH5o2OQLV6CMOjh6DgTP5yTMxPt+39+uojIEIo2jCuiaFzqQ1ND+/raYUME+XdOgtMJ3wC3RYY9xcjvRxCznDFu0tJL1e0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866459; c=relaxed/simple; bh=YYB7ay2XZ2UC9vhXiivQ9Qog0KQ0BcG5GWpUEKVELGs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gwagA668UkxNuIhoezsdbb0fUHr/Ns6AjBsIwzmzv/dTJyt6n0sHZUVM76RxeC7q9dqcGtjE6BDtjWPm6GHD3ow0e+jcEdIgUDMe3FQRLRrQwZ/VD+SPDB1D/ptYi4JeAT8TZcEz4UdI4DFzZJlHXVpJqrZmCSbFK1ljFn4eTrk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n/rwSt7R; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n/rwSt7R" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-740adfc7babso6042570b3a.0 for ; Wed, 21 May 2025 15:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747866456; x=1748471256; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=kcDBUrG9Rt/ZHj/u5g5h7BamQXJVoaPyall+VOpEmVg=; b=n/rwSt7RQ/Nj8KkXyYh7w/ky0clPrK0UnrZBvbqk/SPWVkjIv9K6tztNqMxRlWlLAe mZvu2vC26syTy19PIU4STRZV7F/SUURN2XB3VZ2CDtpc8lLfLXcjmSgECPMJs9SiRpF6 N5lx/OtLz7Ii1dhd9xCPGAl6bJdcEjT1mcwmPm7oe4uqtkd3p6afV9NCYgrJc28vR4KT yFFhF53cXYjnybHeSTjCUTtvispqsfrzSythBTRECSYogFefUM9aTSbbUnjLFmVsmv79 OVbcQcCdRZjvioQFQ4Mbf1HG/j8DzFCwUHqqT2ZchHSxym5VLn8+bQg9tWKy2WkdoLyL S1YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747866456; x=1748471256; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=kcDBUrG9Rt/ZHj/u5g5h7BamQXJVoaPyall+VOpEmVg=; b=U/6C+l1Q0l1vZlsru1eJ3DWHOIZqZ8Z6C+6W1H8zTSZ5bqkqc23a0oNxGtQLV30VXv kYPAp8uqjjIOi1MbuFGghek2r6mNB2TUbloXWF53/KfQ9T50l6v9Choul8IEcNuGdCkZ u3SJbaVOFy7squeY0puTTb81SrsiiTlSfjsNDUUJh826Q6P/SMi/sDZjIgxNKSpgoKuj ggQXQgcyOTy1tK+MiNQEFRk8jZzd0IWjrzfWybtICsjqbvfBEE6CY72+ix2a1YxWXm56 3EGXbJrVYeifKIY+qyqpzsMdGIL1VJqu664Z0yBUdRGvFVWgLMz+xXAVZNX2U/lLCsZi /9Xg== X-Forwarded-Encrypted: i=1; AJvYcCXQB10ijYCOVoO+Lvl3JREQM5sfEO3b6UBfGpGOl3eisklj7n6gki9OF3s3tcfcseogrtQd+dfOGNV4Xx8=@vger.kernel.org X-Gm-Message-State: AOJu0YxRuLtJZ6HSXIpPaJxoeHqTzMWqcSHLjjuyhIPmpKGqL9XR0hDV X5Q5Ma9QcXhCTs1Q5QE/yzDxSzLFhxgdwr2FSGXEYQZcK9OxaIXwhBM1GWWgBuYSuGAvifBoIOQ bwW36Gp3zILho23yqhzA4Xw== X-Google-Smtp-Source: AGHT+IFf0gEcrt2DaqwzhkUNz7mVUXeJdUFtOelSLdDPz/TcjQIS84dfzKUQ6LOF4dcmBbsGH8K+O4CUlfY9WzAQ X-Received: from pfwo11.prod.google.com ([2002:a05:6a00:1bcb:b0:736:3cd5:ba3f]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2304:b0:739:50c0:b3fe with SMTP id d2e1a72fcca58-742a97ab076mr35274944b3a.8.1747866456246; Wed, 21 May 2025 15:27:36 -0700 (PDT) Date: Wed, 21 May 2025 15:27:24 -0700 In-Reply-To: <20250521222725.3895192-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250521222725.3895192-1-blakejones@google.com> X-Mailer: git-send-email 2.49.0.1143.g0be31eac6b-goog Message-ID: <20250521222725.3895192-3-blakejones@google.com> Subject: [PATCH 2/3] perf: collect BPF metadata from existing BPF programs From: Blake Jones To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , Kan Liang Cc: Chun-Tse Shao , Zhongqiu Han , James Clark , Charlie Jenkins , Andi Kleen , Dmitry Vyukov , Leo Yan , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Look for .rodata maps, find ones with 'bpf_metadata_' variables, extract their values as strings, and create a new PERF_RECORD_BPF_METADATA synthetic event using that data. The code gets invoked from the existing routine perf_event__synthesize_one_bpf_prog(). Signed-off-by: Blake Jones Reviewed-by: Ian Rogers --- tools/lib/perf/include/perf/event.h | 18 ++ tools/perf/util/bpf-event.c | 310 ++++++++++++++++++++++++++++ tools/perf/util/bpf-event.h | 13 ++ 3 files changed, 341 insertions(+) diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/p= erf/event.h index 09b7c643ddac..6608f1e3701b 100644 --- a/tools/lib/perf/include/perf/event.h +++ b/tools/lib/perf/include/perf/event.h @@ -467,6 +467,22 @@ struct perf_record_compressed2 { char data[]; }; =20 +#define BPF_METADATA_KEY_LEN 64 +#define BPF_METADATA_VALUE_LEN 256 +#define BPF_PROG_NAME_LEN KSYM_NAME_LEN + +struct perf_record_bpf_metadata_entry { + char key[BPF_METADATA_KEY_LEN]; + char value[BPF_METADATA_VALUE_LEN]; +}; + +struct perf_record_bpf_metadata { + struct perf_event_header header; + char prog_name[BPF_PROG_NAME_LEN]; + __u64 nr_entries; + struct perf_record_bpf_metadata_entry entries[]; +}; + enum perf_user_event_type { /* above any possible kernel type */ PERF_RECORD_USER_TYPE_START =3D 64, PERF_RECORD_HEADER_ATTR =3D 64, @@ -489,6 +505,7 @@ enum perf_user_event_type { /* above any possible kerne= l type */ PERF_RECORD_COMPRESSED =3D 81, PERF_RECORD_FINISHED_INIT =3D 82, PERF_RECORD_COMPRESSED2 =3D 83, + PERF_RECORD_BPF_METADATA =3D 84, PERF_RECORD_HEADER_MAX }; =20 @@ -530,6 +547,7 @@ union perf_event { struct perf_record_header_feature feat; struct perf_record_compressed pack; struct perf_record_compressed2 pack2; + struct perf_record_bpf_metadata bpf_metadata; }; =20 #endif /* __LIBPERF_EVENT_H */ diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index c81444059ad0..36d5676f025e 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -1,13 +1,20 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include +#include +#include #include +#include #include #include #include +#include #include #include +#include #include #include +#include #include #include "bpf-event.h" #include "bpf-utils.h" @@ -151,6 +158,298 @@ static int synthesize_bpf_prog_name(char *buf, int si= ze, return name_len; } =20 +#define BPF_METADATA_PREFIX "bpf_metadata_" +#define BPF_METADATA_PREFIX_LEN (sizeof(BPF_METADATA_PREFIX) - 1) + +static bool name_has_bpf_metadata_prefix(const char **s) +{ + if (strncmp(*s, BPF_METADATA_PREFIX, BPF_METADATA_PREFIX_LEN) !=3D 0) + return false; + *s +=3D BPF_METADATA_PREFIX_LEN; + return true; +} + +struct bpf_metadata_map { + struct btf *btf; + const struct btf_type *datasec; + void *rodata; + size_t rodata_size; + unsigned int num_vars; +}; + +static int bpf_metadata_read_map_data(__u32 map_id, struct bpf_metadata_ma= p *map) +{ + int map_fd; + struct bpf_map_info map_info; + __u32 map_info_len; + int key; + struct btf *btf; + const struct btf_type *datasec; + struct btf_var_secinfo *vsi; + unsigned int vlen, vars; + void *rodata; + + map_fd =3D bpf_map_get_fd_by_id(map_id); + if (map_fd < 0) + return -1; + + memset(&map_info, 0, sizeof(map_info)); + map_info_len =3D sizeof(map_info); + if (bpf_obj_get_info_by_fd(map_fd, &map_info, &map_info_len) < 0) + goto out_close; + + /* If it's not an .rodata map, don't bother. */ + if (map_info.type !=3D BPF_MAP_TYPE_ARRAY || + map_info.key_size !=3D sizeof(int) || + map_info.max_entries !=3D 1 || + !map_info.btf_value_type_id || + !strstr(map_info.name, ".rodata")) { + goto out_close; + } + + btf =3D btf__load_from_kernel_by_id(map_info.btf_id); + if (!btf) + goto out_close; + datasec =3D btf__type_by_id(btf, map_info.btf_value_type_id); + if (!btf_is_datasec(datasec)) + goto out_free_btf; + + /* If there aren't any variables with the "bpf_metadata_" prefix, + * don't bother. + */ + vlen =3D btf_vlen(datasec); + vsi =3D btf_var_secinfos(datasec); + vars =3D 0; + for (unsigned int i =3D 0; i < vlen; i++, vsi++) { + const struct btf_type *t_var =3D btf__type_by_id(btf, vsi->type); + const char *name =3D btf__name_by_offset(btf, t_var->name_off); + + if (name_has_bpf_metadata_prefix(&name)) + vars++; + } + if (vars =3D=3D 0) + goto out_free_btf; + + rodata =3D calloc(1, map_info.value_size); + if (!rodata) + goto out_free_btf; + key =3D 0; + if (bpf_map_lookup_elem(map_fd, &key, rodata)) { + free(rodata); + goto out_free_btf; + } + close(map_fd); + + map->btf =3D btf; + map->datasec =3D datasec; + map->rodata =3D rodata; + map->rodata_size =3D map_info.value_size; + map->num_vars =3D vars; + return 0; + +out_free_btf: + btf__free(btf); +out_close: + close(map_fd); + return -1; +} + +struct format_btf_ctx { + char *buf; + size_t buf_size; + size_t buf_idx; +}; + +static void format_btf_cb(void *arg, const char *fmt, va_list ap) +{ + int n; + struct format_btf_ctx *ctx =3D (struct format_btf_ctx *)arg; + + n =3D vsnprintf(ctx->buf + ctx->buf_idx, ctx->buf_size - ctx->buf_idx, + fmt, ap); + ctx->buf_idx +=3D n; + if (ctx->buf_idx >=3D ctx->buf_size) + ctx->buf_idx =3D ctx->buf_size; +} + +static void format_btf_variable(struct btf *btf, char *buf, size_t buf_siz= e, + const struct btf_type *t, const void *btf_data) +{ + struct format_btf_ctx ctx =3D { + .buf =3D buf, + .buf_idx =3D 0, + .buf_size =3D buf_size, + }; + const struct btf_dump_type_data_opts opts =3D { + .sz =3D sizeof(struct btf_dump_type_data_opts), + .skip_names =3D 1, + .compact =3D 1, + .print_strings =3D 1, + }; + struct btf_dump *d; + size_t btf_size; + + d =3D btf_dump__new(btf, format_btf_cb, &ctx, NULL); + btf_size =3D btf__resolve_size(btf, t->type); + btf_dump__dump_type_data(d, t->type, btf_data, btf_size, &opts); + btf_dump__free(d); +} + +static void bpf_metadata_fill_event(struct bpf_metadata_map *map, + struct perf_record_bpf_metadata *bpf_metadata_event) +{ + struct btf_var_secinfo *vsi; + unsigned int i, vlen; + + memset(bpf_metadata_event->prog_name, 0, BPF_PROG_NAME_LEN); + vlen =3D btf_vlen(map->datasec); + vsi =3D btf_var_secinfos(map->datasec); + + for (i =3D 0; i < vlen; i++, vsi++) { + const struct btf_type *t_var =3D btf__type_by_id(map->btf, + vsi->type); + const char *name =3D btf__name_by_offset(map->btf, + t_var->name_off); + const __u64 nr_entries =3D bpf_metadata_event->nr_entries; + struct perf_record_bpf_metadata_entry *entry; + + if (!name_has_bpf_metadata_prefix(&name)) + continue; + + if (nr_entries >=3D (__u64)map->num_vars) + break; + + entry =3D &bpf_metadata_event->entries[nr_entries]; + memset(entry, 0, sizeof(*entry)); + snprintf(entry->key, BPF_METADATA_KEY_LEN, "%s", name); + format_btf_variable(map->btf, entry->value, + BPF_METADATA_VALUE_LEN, t_var, + map->rodata + vsi->offset); + bpf_metadata_event->nr_entries++; + } +} + +static void bpf_metadata_free_map_data(struct bpf_metadata_map *map) +{ + btf__free(map->btf); + free(map->rodata); +} + +void bpf_metadata_free(struct bpf_metadata *metadata) +{ + if (metadata =3D=3D NULL) + return; + for (__u32 index =3D 0; index < metadata->nr_prog_names; index++) + free(metadata->prog_names[index]); + if (metadata->prog_names !=3D NULL) + free(metadata->prog_names); + if (metadata->event !=3D NULL) + free(metadata->event); + free(metadata); +} + +static struct bpf_metadata *bpf_metadata_alloc(__u32 nr_prog_tags, + __u32 nr_variables) +{ + struct bpf_metadata *metadata; + + metadata =3D calloc(1, sizeof(struct bpf_metadata)); + if (!metadata) + return NULL; + + metadata->prog_names =3D calloc(nr_prog_tags, sizeof(char *)); + if (!metadata->prog_names) { + bpf_metadata_free(metadata); + return NULL; + } + for (__u32 prog_index =3D 0; prog_index < nr_prog_tags; prog_index++) { + metadata->prog_names[prog_index] =3D calloc(BPF_PROG_NAME_LEN, + sizeof(char)); + if (!metadata->prog_names[prog_index]) { + bpf_metadata_free(metadata); + return NULL; + } + metadata->nr_prog_names++; + } + + metadata->event_size =3D sizeof(metadata->event->bpf_metadata) + + nr_variables * sizeof(metadata->event->bpf_metadata.entries[0]); + metadata->event =3D calloc(1, metadata->event_size); + if (!metadata->event) { + bpf_metadata_free(metadata); + return NULL; + } + + return metadata; +} + +static struct bpf_metadata *bpf_metadata_create(struct bpf_prog_info *info) +{ + struct bpf_metadata *metadata; + const __u32 *map_ids =3D (__u32 *)(uintptr_t)info->map_ids; + + for (__u32 map_index =3D 0; map_index < info->nr_map_ids; map_index++) { + struct perf_record_bpf_metadata *bpf_metadata_event; + struct bpf_metadata_map map; + + if (bpf_metadata_read_map_data(map_ids[map_index], &map) !=3D 0) + continue; + + metadata =3D bpf_metadata_alloc(info->nr_prog_tags, map.num_vars); + if (!metadata) + continue; + + bpf_metadata_event =3D &metadata->event->bpf_metadata; + *bpf_metadata_event =3D (struct perf_record_bpf_metadata) { + .header =3D { + .type =3D PERF_RECORD_BPF_METADATA, + .size =3D metadata->event_size, + }, + .nr_entries =3D 0, + }; + bpf_metadata_fill_event(&map, bpf_metadata_event); + + for (__u32 index =3D 0; index < info->nr_prog_tags; index++) { + synthesize_bpf_prog_name(metadata->prog_names[index], + BPF_PROG_NAME_LEN, info, + map.btf, index); + } + + bpf_metadata_free_map_data(&map); + + return metadata; + } + + return NULL; +} + +static int synthesize_perf_record_bpf_metadata(const struct bpf_metadata *= metadata, + const struct perf_tool *tool, + perf_event__handler_t process, + struct machine *machine) +{ + union perf_event *event; + int err =3D 0; + + event =3D calloc(1, metadata->event_size + machine->id_hdr_size); + if (!event) + return -1; + memcpy(event, metadata->event, metadata->event_size); + memset((void *)event + event->header.size, 0, machine->id_hdr_size); + event->header.size +=3D machine->id_hdr_size; + for (__u32 index =3D 0; index < metadata->nr_prog_names; index++) { + memcpy(event->bpf_metadata.prog_name, + metadata->prog_names[index], BPF_PROG_NAME_LEN); + err =3D perf_tool__process_synth_event(tool, event, machine, + process); + if (err !=3D 0) + break; + } + + free(event); + return err; +} + /* * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf * program. One PERF_RECORD_BPF_EVENT is generated for the program. And @@ -173,6 +472,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, const struct perf_tool *tool =3D session->tool; struct bpf_prog_info_node *info_node; struct perf_bpil *info_linear; + struct bpf_metadata *metadata; struct bpf_prog_info *info; struct btf *btf =3D NULL; struct perf_env *env; @@ -193,6 +493,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, arrays |=3D 1UL << PERF_BPIL_JITED_INSNS; arrays |=3D 1UL << PERF_BPIL_LINE_INFO; arrays |=3D 1UL << PERF_BPIL_JITED_LINE_INFO; + arrays |=3D 1UL << PERF_BPIL_MAP_IDS; =20 info_linear =3D get_bpf_prog_info_linear(fd, arrays); if (IS_ERR_OR_NULL(info_linear)) { @@ -301,6 +602,15 @@ static int perf_event__synthesize_one_bpf_prog(struct = perf_session *session, */ err =3D perf_tool__process_synth_event(tool, event, machine, process); + + /* Synthesize PERF_RECORD_BPF_METADATA */ + metadata =3D bpf_metadata_create(info); + if (metadata !=3D NULL) { + err =3D synthesize_perf_record_bpf_metadata(metadata, + tool, process, + machine); + bpf_metadata_free(metadata); + } } =20 out: diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index e2f0420905f5..007f0b4d21cb 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -17,6 +17,13 @@ struct record_opts; struct evlist; struct target; =20 +struct bpf_metadata { + union perf_event *event; + size_t event_size; + char **prog_names; + __u64 nr_prog_names; +}; + struct bpf_prog_info_node { struct perf_bpil *info_linear; struct rb_node rb_node; @@ -36,6 +43,7 @@ int evlist__add_bpf_sb_event(struct evlist *evlist, struc= t perf_env *env); void __bpf_event__print_bpf_prog_info(struct bpf_prog_info *info, struct perf_env *env, FILE *fp); +void bpf_metadata_free(struct bpf_metadata *metadata); #else static inline int machine__process_bpf(struct machine *machine __maybe_unu= sed, union perf_event *event __maybe_unused, @@ -55,6 +63,11 @@ static inline void __bpf_event__print_bpf_prog_info(stru= ct bpf_prog_info *info _ FILE *fp __maybe_unused) { =20 +} + +static inline void bpf_metadata_free(struct bpf_metadata *metadata) +{ + } #endif // HAVE_LIBBPF_SUPPORT #endif --=20 2.49.0.1143.g0be31eac6b-goog From nobody Sun Dec 14 19:35:46 2025 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7CC7823A9A5 for ; Wed, 21 May 2025 22:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866460; cv=none; b=gsFlfFZvgiNcGizConA9UB079lvQNAF4NAZ/ivC9/JXhtjLPrWCzqvwJHty9GPVOnyTehzuRJ99FyOAQppfnMo6ip6kW6IuI30mXGUa3pFO/UASvi0DHTFXo4xP+g1jD62WjSQTC85artwN3jKCoUeSUypKavKy8FlZtJVB5QOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747866460; c=relaxed/simple; bh=5ndQlhz9FB0GfSKHERXhMy+k18dPFRMneg+C7rlhXrU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cwZc3BG3gPzmQvlFByYdUiMMp11ja0rnjFwq2qnRcPgfRFf+vTM1pJhxkVgN/nWyGq5SqX6liD7F9hpqsW+ExDLmmryysLK27AAtMIZPuJA11tjDKxlBMae5brWvkMCp3Y/KcA5eRs4aaohXykvUMjhuWXbaEQo6ZJFrFVWvxKs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4fiOf15n; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--blakejones.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4fiOf15n" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b26e33ae9d5so7608644a12.1 for ; Wed, 21 May 2025 15:27:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747866458; x=1748471258; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=wDcyyMeU1YQ93lXrfp13UEs17WcGmsLfdj2k2DTJvn0=; b=4fiOf15nj2eUvQ3wGvBM+9Zg41ywyP2b/WcvRC5EByR3Lg3WN4iOkBFB5QCjqMIIln FFTTlELJWL0Yxky3ylHbPensmussOri6LKEd95t8SSlc/iJnyc9Pai1283hZVCrFLogl O+/3FWfyQAExVYeUaLnevU7WqKIdX0PXi8Nb2TtXNC58WxdricgYzqkf/ZsK/2N1ln1F Zt32Uq8x+0ZNTZ251AHu07vj87hSmSRheFyUfHsXzybIk4rJ7xTOhpDhKUrGWYaiUaBA SiRJJYy7V9AhDOeBo8yLMuxERwuRFFQ7/QuRh5KwT4Vg/5A5ujCQf/darlzbL46dtMt/ r61g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747866458; x=1748471258; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wDcyyMeU1YQ93lXrfp13UEs17WcGmsLfdj2k2DTJvn0=; b=K9P8Sa1z1GdKm4iRlUM0qYQjpvN2mGZ559ZvCR6IVZG6mgy1sHDTAv5zLO/mPqSSSt 1zMgQ/Ztre6rQwI3deaUOherHyur9EXotyXeI0//pfh/fAjTXxlLnoA5JILP/WpYzpcA Bz95raT88BVnBXaTDg7wgOO8oPRVNVW+bO9vAvTfmQD3U5j18gIPRx802RH54TwWWgrY WQcDdCwJvRC0TV6F31mp46jU95e2hC+1HoIfnleetFU4FEJHosh6oB8iaO0dhA90rvtT +C5pwwS0OIWP3OeItOwiH1ePucLldMvTYXfeN9MXHpGCbzz/1qHlhp6nrtF4CoLaKc3d UIhw== X-Forwarded-Encrypted: i=1; AJvYcCV+M06cUFWU7n2+4Ai6FJmh5NjOe1gGkweOnkD9PCRJ+idc5wPDVj8h4SKtnZ7HUWOkiJT9/OJzt2LbCdA=@vger.kernel.org X-Gm-Message-State: AOJu0YyizACxkw9txAHE8berAcLQpWsYDztDxejob4UoP1VhCacauOwn 44tPNW/ysJoTaLF6Hm5TZ6CfeoA5nAPOXD6ZGO2X8s7nP0ftrMQWWJWAeAtC+Yw7o35/dUC6kwb 59I+XNZtvKBjTBSpfAjBZRA== X-Google-Smtp-Source: AGHT+IFGTXu7UDkEpHZnS+Hgm7cF9sD9eNKr/GvnOl9svURNm8Pz7ShENK06WLkLAzzYV8lkiM3fDq1O64bf0hvW X-Received: from pgal137.prod.google.com ([2002:a63:3e8f:0:b0:af2:54b0:c8d5]) (user=blakejones job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:cfa2:b0:204:4573:d856 with SMTP id adf61e73a8af0-216fba27c2cmr32432733637.4.1747866458060; Wed, 21 May 2025 15:27:38 -0700 (PDT) Date: Wed, 21 May 2025 15:27:25 -0700 In-Reply-To: <20250521222725.3895192-1-blakejones@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250521222725.3895192-1-blakejones@google.com> X-Mailer: git-send-email 2.49.0.1143.g0be31eac6b-goog Message-ID: <20250521222725.3895192-4-blakejones@google.com> Subject: [PATCH 3/3] perf: collect BPF metadata from new programs, and display the new event From: Blake Jones To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Ian Rogers , Adrian Hunter , Kan Liang Cc: Chun-Tse Shao , Zhongqiu Han , James Clark , Charlie Jenkins , Andi Kleen , Dmitry Vyukov , Leo Yan , Yujie Liu , Graham Woodward , Yicong Yang , Ben Gainey , linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-perf-users@vger.kernel.org, Blake Jones Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This collects metadata for any BPF programs that were loaded during a "perf record" run, and emits it at the end of the run. It also adds support for displaying the new PERF_RECORD_BPF_METADATA type. Here's some example "perf script -D" output for the new event type. The ": unhandled!" message is from tool.c, analogous to other behavior there. I've elided some rows with all NUL characters for brevity, and I wrapped one of the >75-column lines to fit in the commit guidelines. 0x50fc8@perf.data [0x260]: event: 84 =2E . ... raw event: size 608 bytes . 0000: 54 00 00 00 00 00 60 02 62 70 66 5f 70 72 6f 67 T.....`.bpf_prog . 0010: 5f 31 65 30 61 32 65 33 36 36 65 35 36 66 31 61 _1e0a2e366e56f1a . 0020: 32 5f 70 65 72 66 5f 73 61 6d 70 6c 65 5f 66 69 2_perf_sample_fi . 0030: 6c 74 65 72 00 00 00 00 00 00 00 00 00 00 00 00 lter............ . 0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] . 0110: 74 65 73 74 5f 76 61 6c 75 65 00 00 00 00 00 00 test_value...... . 0120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] . 0150: 34 32 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42.............. . 0160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [...] 0 0x50fc8 [0x260]: PERF_RECORD_BPF_METADATA \ prog bpf_prog_1e0a2e366e56f1a2_perf_sample_filter entry 0: test_value =3D 42 : unhandled! Signed-off-by: Blake Jones --- tools/perf/builtin-inject.c | 1 + tools/perf/builtin-record.c | 8 +++ tools/perf/builtin-script.c | 15 ++++- tools/perf/tests/shell/test_bpf_metadata.sh | 67 ++++++++++++++++++++ tools/perf/util/bpf-event.c | 46 ++++++++++++++ tools/perf/util/bpf-event.h | 1 + tools/perf/util/bpf_skel/sample_filter.bpf.c | 4 ++ tools/perf/util/env.c | 19 +++++- tools/perf/util/env.h | 4 ++ tools/perf/util/event.c | 21 ++++++ tools/perf/util/event.h | 1 + tools/perf/util/header.c | 1 + tools/perf/util/session.c | 4 ++ tools/perf/util/synthetic-events.h | 2 + tools/perf/util/tool.c | 14 ++++ tools/perf/util/tool.h | 3 +- 16 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 tools/perf/tests/shell/test_bpf_metadata.sh diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 11e49cafa3af..b15eac0716f7 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -2530,6 +2530,7 @@ int cmd_inject(int argc, const char **argv) inject.tool.finished_init =3D perf_event__repipe_op2_synth; inject.tool.compressed =3D perf_event__repipe_op4_synth; inject.tool.auxtrace =3D perf_event__repipe_auxtrace; + inject.tool.bpf_metadata =3D perf_event__repipe_op2_synth; inject.tool.dont_split_sample_group =3D true; inject.session =3D __perf_session__new(&data, &inject.tool, /*trace_event_repipe=3D*/inject.output.is_pipe); diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c index 136c0172799a..067e203f57c2 100644 --- a/tools/perf/builtin-record.c +++ b/tools/perf/builtin-record.c @@ -2161,6 +2161,12 @@ static int record__synthesize(struct record *rec, bo= ol tail) return err; } =20 +static void record__synthesize_final_bpf_metadata(struct record *rec) +{ + perf_event__synthesize_final_bpf_metadata(rec->session, + process_synthesized_event); +} + static int record__process_signal_event(union perf_event *event __maybe_un= used, void *data) { struct record *rec =3D data; @@ -2806,6 +2812,8 @@ static int __cmd_record(struct record *rec, int argc,= const char **argv) trigger_off(&auxtrace_snapshot_trigger); trigger_off(&switch_output_trigger); =20 + record__synthesize_final_bpf_metadata(rec); + if (opts->auxtrace_snapshot_on_exit) record__auxtrace_snapshot_exit(rec); =20 diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 6c3bf74dd78c..4001e621b6cb 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -38,6 +38,7 @@ #include "print_insn.h" #include "archinsn.h" #include +#include #include #include #include @@ -50,6 +51,7 @@ #include #include #include +#include #include #include #include @@ -2755,6 +2757,14 @@ process_bpf_events(const struct perf_tool *tool __ma= ybe_unused, sample->tid); } =20 +static int +process_bpf_metadata_event(struct perf_session *session __maybe_unused, + union perf_event *event) +{ + perf_event__fprintf(event, NULL, stdout); + return 0; +} + static int process_text_poke_events(const struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, @@ -2877,8 +2887,9 @@ static int __cmd_script(struct perf_script *script) script->tool.finished_round =3D process_finished_round_event; } if (script->show_bpf_events) { - script->tool.ksymbol =3D process_bpf_events; - script->tool.bpf =3D process_bpf_events; + script->tool.ksymbol =3D process_bpf_events; + script->tool.bpf =3D process_bpf_events; + script->tool.bpf_metadata =3D process_bpf_metadata_event; } if (script->show_text_poke_events) { script->tool.ksymbol =3D process_bpf_events; diff --git a/tools/perf/tests/shell/test_bpf_metadata.sh b/tools/perf/tests= /shell/test_bpf_metadata.sh new file mode 100644 index 000000000000..ede31d5a3c36 --- /dev/null +++ b/tools/perf/tests/shell/test_bpf_metadata.sh @@ -0,0 +1,67 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# BPF metadata collection test. + +set -e + +err=3D0 +perfdata=3D$(mktemp /tmp/__perf_test.perf.data.XXXXX) + +cleanup() { + rm -f "${perfdata}" + rm -f "${perfdata}".old + trap - EXIT TERM INT +} + +trap_cleanup() { + cleanup + exit 1 +} +trap trap_cleanup EXIT TERM INT + +test_bpf_metadata() { + echo "Checking BPF metadata collection" + + # This is a basic invocation of perf record + # that invokes the perf_sample_filter BPF program. + if ! perf record -e task-clock --filter 'ip > 0' \ + -o "${perfdata}" sleep 1 2> /dev/null + then + echo "Basic BPF metadata test [Failed record]" + err=3D1 + return + fi + + # The perf_sample_filter BPF program has the following variable in it: + # + # volatile const int bpf_metadata_test_value SEC(".rodata") =3D 42; + # + # This invocation looks for a PERF_RECORD_BPF_METADATA event, + # and checks that its content includes something for the above variable. + if ! perf script --show-bpf-events -i "${perfdata}" | awk ' + /PERF_RECORD_BPF_METADATA.*perf_sample_filter/ { + header =3D 1; + } + /^ *entry/ { + if (header) { header =3D 0; entry =3D 1; } + } + $0 !~ /^ *entry/ { + entry =3D 0; + } + /test_value/ { + if (entry) print $NF; + } + ' | grep 42 > /dev/null + then + echo "Basic BPF metadata test [Failed invalid output]" + err=3D1 + return + fi + echo "Basic BPF metadata test [Success]" +} + +test_bpf_metadata + +cleanup +exit $err diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c index 36d5676f025e..019832ec4802 100644 --- a/tools/perf/util/bpf-event.c +++ b/tools/perf/util/bpf-event.c @@ -450,6 +450,49 @@ static int synthesize_perf_record_bpf_metadata(const s= truct bpf_metadata *metada return err; } =20 +struct bpf_metadata_final_ctx { + const struct perf_tool *tool; + perf_event__handler_t process; + struct machine *machine; +}; + +static void synthesize_final_bpf_metadata_cb(struct bpf_prog_info_node *no= de, + void *data) +{ + struct bpf_metadata_final_ctx *ctx =3D (struct bpf_metadata_final_ctx *)d= ata; + struct bpf_metadata *metadata =3D node->metadata; + int err; + + if (metadata =3D=3D NULL) + return; + err =3D synthesize_perf_record_bpf_metadata(metadata, ctx->tool, + ctx->process, ctx->machine); + if (err !=3D 0) { + const char *prog_name =3D metadata->prog_names[0]; + + if (prog_name !=3D NULL) + pr_warning("Couldn't synthesize final BPF metadata for %s.\n", prog_nam= e); + else + pr_warning("Couldn't synthesize final BPF metadata.\n"); + } + bpf_metadata_free(metadata); + node->metadata =3D NULL; +} + +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process) +{ + struct perf_env *env =3D &session->header.env; + struct bpf_metadata_final_ctx ctx =3D { + .tool =3D session->tool, + .process =3D process, + .machine =3D &session->machines.host, + }; + + perf_env__iterate_bpf_prog_info(env, synthesize_final_bpf_metadata_cb, + &ctx); +} + /* * Synthesize PERF_RECORD_KSYMBOL and PERF_RECORD_BPF_EVENT for one bpf * program. One PERF_RECORD_BPF_EVENT is generated for the program. And @@ -590,6 +633,7 @@ static int perf_event__synthesize_one_bpf_prog(struct p= erf_session *session, } =20 info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); @@ -781,6 +825,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) arrays |=3D 1UL << PERF_BPIL_JITED_INSNS; arrays |=3D 1UL << PERF_BPIL_LINE_INFO; arrays |=3D 1UL << PERF_BPIL_JITED_LINE_INFO; + arrays |=3D 1UL << PERF_BPIL_MAP_IDS; =20 info_linear =3D get_bpf_prog_info_linear(fd, arrays); if (IS_ERR_OR_NULL(info_linear)) { @@ -793,6 +838,7 @@ static void perf_env__add_bpf_info(struct perf_env *env= , u32 id) info_node =3D malloc(sizeof(struct bpf_prog_info_node)); if (info_node) { info_node->info_linear =3D info_linear; + info_node->metadata =3D bpf_metadata_create(&info_linear->info); if (!perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index 007f0b4d21cb..49a42c15bcc6 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -26,6 +26,7 @@ struct bpf_metadata { =20 struct bpf_prog_info_node { struct perf_bpil *info_linear; + struct bpf_metadata *metadata; struct rb_node rb_node; }; =20 diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util= /bpf_skel/sample_filter.bpf.c index b195e6efeb8b..b0265f000325 100644 --- a/tools/perf/util/bpf_skel/sample_filter.bpf.c +++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c @@ -43,6 +43,10 @@ struct lost_count { __uint(max_entries, 1); } dropped SEC(".maps"); =20 +// This is used by tests/shell/record_bpf_metadata.sh +// to verify that BPF metadata generation works. +const int bpf_metadata_test_value SEC(".rodata") =3D 42; + volatile const int use_idx_hash; =20 void *bpf_cast_to_kern_ctx(void *) __ksym; diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 36411749e007..05a4f2657d72 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -3,8 +3,10 @@ #include "debug.h" #include "env.h" #include "util/header.h" -#include "linux/compiler.h" +#include "util/rwsem.h" +#include #include +#include #include #include #include "cgroup.h" @@ -89,6 +91,20 @@ struct bpf_prog_info_node *perf_env__find_bpf_prog_info(= struct perf_env *env, return node; } =20 +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data) +{ + struct rb_node *first; + + down_read(&env->bpf_progs.lock); + first =3D rb_first(&env->bpf_progs.infos); + for (struct rb_node *node =3D first; node !=3D NULL; node =3D rb_next(nod= e)) + (*cb)(rb_entry(node, struct bpf_prog_info_node, rb_node), data); + up_read(&env->bpf_progs.lock); +} + bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node) { bool ret; @@ -174,6 +190,7 @@ static void perf_env__purge_bpf(struct perf_env *env) next =3D rb_next(&node->rb_node); rb_erase(&node->rb_node, root); zfree(&node->info_linear); + bpf_metadata_free(node->metadata); free(node); } =20 diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index d90e343cf1fa..6819cb9b99ff 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -180,6 +180,10 @@ bool perf_env__insert_bpf_prog_info(struct perf_env *e= nv, struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *e= nv, __u32 prog_id); +void perf_env__iterate_bpf_prog_info(struct perf_env *env, + void (*cb)(struct bpf_prog_info_node *node, + void *data), + void *data); bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node); bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_nod= e); struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id); diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c index 80c9ea682413..e81c2d87d76a 100644 --- a/tools/perf/util/event.c +++ b/tools/perf/util/event.c @@ -1,9 +1,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -78,6 +81,7 @@ static const char *perf_event__names[] =3D { [PERF_RECORD_COMPRESSED] =3D "COMPRESSED", [PERF_RECORD_FINISHED_INIT] =3D "FINISHED_INIT", [PERF_RECORD_COMPRESSED2] =3D "COMPRESSED2", + [PERF_RECORD_BPF_METADATA] =3D "BPF_METADATA", }; =20 const char *perf_event__name(unsigned int id) @@ -504,6 +508,20 @@ size_t perf_event__fprintf_bpf(union perf_event *event= , FILE *fp) event->bpf.type, event->bpf.flags, event->bpf.id); } =20 +size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp) +{ + struct perf_record_bpf_metadata *metadata =3D &event->bpf_metadata; + size_t ret; + + ret =3D fprintf(fp, " prog %s\n", metadata->prog_name); + for (__u32 i =3D 0; i < metadata->nr_entries; i++) { + ret +=3D fprintf(fp, " entry %d: %20s =3D %s\n", i, + metadata->entries[i].key, + metadata->entries[i].value); + } + return ret; +} + static int text_poke_printer(enum binary_printer_ops op, unsigned int val, void *extra, FILE *fp) { @@ -601,6 +619,9 @@ size_t perf_event__fprintf(union perf_event *event, str= uct machine *machine, FIL case PERF_RECORD_AUX_OUTPUT_HW_ID: ret +=3D perf_event__fprintf_aux_output_hw_id(event, fp); break; + case PERF_RECORD_BPF_METADATA: + ret +=3D perf_event__fprintf_bpf_metadata(event, fp); + break; default: ret +=3D fprintf(fp, "\n"); } diff --git a/tools/perf/util/event.h b/tools/perf/util/event.h index 664bf39567ce..67ad4a2014bc 100644 --- a/tools/perf/util/event.h +++ b/tools/perf/util/event.h @@ -370,6 +370,7 @@ size_t perf_event__fprintf_namespaces(union perf_event = *event, FILE *fp); size_t perf_event__fprintf_cgroup(union perf_event *event, FILE *fp); size_t perf_event__fprintf_ksymbol(union perf_event *event, FILE *fp); size_t perf_event__fprintf_bpf(union perf_event *event, FILE *fp); +size_t perf_event__fprintf_bpf_metadata(union perf_event *event, FILE *fp); size_t perf_event__fprintf_text_poke(union perf_event *event, struct machi= ne *machine,FILE *fp); size_t perf_event__fprintf(union perf_event *event, struct machine *machin= e, FILE *fp); =20 diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index e3cdc3b7b4ab..7c477e2a93b3 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -3161,6 +3161,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, = void *data __maybe_unused) /* after reading from file, translate offset to address */ bpil_offs_to_addr(info_linear); info_node->info_linear =3D info_linear; + info_node->metadata =3D NULL; if (!__perf_env__insert_bpf_prog_info(env, info_node)) { free(info_linear); free(info_node); diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index a320672c264e..38075059086c 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -12,6 +12,7 @@ #include #include #include +#include =20 #include "map_symbol.h" #include "branch.h" @@ -1491,6 +1492,9 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, case PERF_RECORD_FINISHED_INIT: err =3D tool->finished_init(session, event); break; + case PERF_RECORD_BPF_METADATA: + err =3D tool->bpf_metadata(session, event); + break; default: err =3D -EINVAL; break; diff --git a/tools/perf/util/synthetic-events.h b/tools/perf/util/synthetic= -events.h index b9c936b5cfeb..ee29615d68e5 100644 --- a/tools/perf/util/synthetic-events.h +++ b/tools/perf/util/synthetic-events.h @@ -92,6 +92,8 @@ int perf_event__synthesize_threads(const struct perf_tool= *tool, perf_event__han int perf_event__synthesize_tracing_data(const struct perf_tool *tool, int = fd, struct evlist *evlist, perf_event__handler_t process); int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, con= st struct perf_tool *tool, perf_event__handler_t process, struct machine *m= achine); pid_t perf_event__synthesize_comm(const struct perf_tool *tool, union perf= _event *event, pid_t pid, perf_event__handler_t process, struct machine *ma= chine); +void perf_event__synthesize_final_bpf_metadata(struct perf_session *sessio= n, + perf_event__handler_t process); =20 int perf_tool__process_synth_event(const struct perf_tool *tool, union per= f_event *event, struct machine *machine, perf_event__handler_t process); =20 diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c index 37bd8ac63b01..204ec03071bc 100644 --- a/tools/perf/util/tool.c +++ b/tools/perf/util/tool.c @@ -1,12 +1,15 @@ // SPDX-License-Identifier: GPL-2.0 #include "data.h" #include "debug.h" +#include "event.h" #include "header.h" #include "session.h" #include "stat.h" #include "tool.h" #include "tsc.h" +#include #include +#include #include =20 #ifdef HAVE_ZSTD_SUPPORT @@ -237,6 +240,16 @@ static int perf_session__process_compressed_event_stub= (struct perf_session *sess return 0; } =20 +static int perf_event__process_bpf_metadata_stub(struct perf_session *perf= _session __maybe_unused, + union perf_event *event) +{ + if (dump_trace) + perf_event__fprintf_bpf_metadata(event, stdout); + + dump_printf(": unhandled!\n"); + return 0; +} + void perf_tool__init(struct perf_tool *tool, bool ordered_events) { tool->ordered_events =3D ordered_events; @@ -293,6 +306,7 @@ void perf_tool__init(struct perf_tool *tool, bool order= ed_events) tool->compressed =3D perf_session__process_compressed_event_stub; #endif tool->finished_init =3D process_event_op2_stub; + tool->bpf_metadata =3D perf_event__process_bpf_metadata_stub; } =20 bool perf_tool__compressed_is_stub(const struct perf_tool *tool) diff --git a/tools/perf/util/tool.h b/tools/perf/util/tool.h index db1c7642b0d1..18b76ff0f26a 100644 --- a/tools/perf/util/tool.h +++ b/tools/perf/util/tool.h @@ -77,7 +77,8 @@ struct perf_tool { stat, stat_round, feature, - finished_init; + finished_init, + bpf_metadata; event_op4 compressed; event_op3 auxtrace; bool ordered_events; --=20 2.49.0.1143.g0be31eac6b-goog