From nobody Wed Nov 27 21:50:57 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 811EE1D6182; Mon, 7 Oct 2024 14:11:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310298; cv=none; b=Dwnutfc6f1sTqCQgkTR+/P+CBgUMFpPe3nJcqggmSc7ev5c1XJ49B+OA3t4xyXpz/a6RZaw6JQQfwAbWCUoTRynnlDfGy+RDb+rn+nsp32ROsntFiumFqtGMuNDatdKTm8aydtKp0I5muKdlgVe5lt+ukFG+/Oy8+z+znViGe60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310298; c=relaxed/simple; bh=HvUNDL8dAP+7s0rvV3MlShaHb15zvas9haBDEv5Ov9c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KBsQRpa4BFQBcNoyCf62M6n2pZLggu6aURmbsjqxThiYsDo9IrkPxlssL3nmlE9Kjj1PJ3/H5JB/XjfRRs89qIibVeHzTBstIQhwIOd+dIpOsua4D2VntNhnLOIxp40DaXU92+LC1Ord2kz8GuuOLbWOGFmXmhHVvPKENNrE6Mc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 706C2FEC; Mon, 7 Oct 2024 07:12:05 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 03DAD3F64C; Mon, 7 Oct 2024 07:11:33 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Dima Kogan , james.clark@linaro.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 1/3] perf: Dynamically allocate buffer for event string Date: Mon, 7 Oct 2024 15:11:14 +0100 Message-Id: <20241007141116.882450-2-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141116.882450-1-leo.yan@arm.com> References: <20241007141116.882450-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Dynamically allocate and free buffer to support event name. Signed-off-by: Leo Yan --- tools/perf/util/probe-event.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index a17c9b8a7a79..cad11d95af4f 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -2834,6 +2834,9 @@ static void warn_uprobe_event_compat(struct probe_tra= ce_event *tev) free(buf); } =20 +/* Defined in kernel/trace/trace.h */ +#define MAX_EVENT_NAME_LEN 64 + /* Set new name from original perf_probe_event and namelist */ static int probe_trace_event__set_name(struct probe_trace_event *tev, struct perf_probe_event *pev, @@ -2841,9 +2844,13 @@ static int probe_trace_event__set_name(struct probe_= trace_event *tev, bool allow_suffix) { const char *event, *group; - char buf[64]; + char *buf; int ret; =20 + buf =3D malloc(MAX_EVENT_NAME_LEN); + if (!buf) + return -ENOMEM; + /* If probe_event or trace_event already have the name, reuse it */ if (pev->event && !pev->sdt) event =3D pev->event; @@ -2866,17 +2873,19 @@ static int probe_trace_event__set_name(struct probe= _trace_event *tev, group =3D PERFPROBE_GROUP; =20 /* Get an unused new event name */ - ret =3D get_new_event_name(buf, sizeof(buf), event, namelist, + ret =3D get_new_event_name(buf, MAX_EVENT_NAME_LEN, event, namelist, tev->point.retprobe, allow_suffix); if (ret < 0) - return ret; + goto out; =20 event =3D buf; =20 tev->event =3D strdup(event); tev->group =3D strdup(group); - if (tev->event =3D=3D NULL || tev->group =3D=3D NULL) - return -ENOMEM; + if (tev->event =3D=3D NULL || tev->group =3D=3D NULL) { + ret =3D -ENOMEM; + goto out; + } =20 /* * Add new event name to namelist if multiprobe event is NOT @@ -2885,7 +2894,10 @@ static int probe_trace_event__set_name(struct probe_= trace_event *tev, */ if (!multiprobe_event_is_supported()) strlist__add(namelist, event); - return 0; + +out: + free(buf); + return ret < 0 ? ret : 0; } =20 static int __open_probe_file_and_namelist(bool uprobe, --=20 2.34.1 From nobody Wed Nov 27 21:50:57 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA8E71D88A4; Mon, 7 Oct 2024 14:11:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310300; cv=none; b=s840vbLusr3QMDsNQ+KMEFamdz4mn8BKq2jOCtU57eZsa0BCE+I111+SHgZqPgxc8XETjDdnx3trzIZ4vFjKLFw98huD+0MdZDDM9f7tCFYDgaqgyYO3210RnO/XK4EndTs0dUzUFIOTl49RIbGXeDTKCmMhx5LscnA46josWLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310300; c=relaxed/simple; bh=ephwfqqSwMeRkEkj9idjCl7uv+fLynMsOvLznB9wqsM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CJbvSKeCLQRLowCZzXt/R4CjhoeRR10TYGkF9rDoWeTSe2atAEmJ7E6642SrjNhnrBFKi7Zp8Isjj+XsYcz7Ci8aNyvlI8tzi1lYPWeaughwsEQU1aJvVbWG9Oq5Cmalr97hr4lTZjxK8e8QxAtKK2VsXI/+8wKI2K1gQ3qfOkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id A09E4DA7; Mon, 7 Oct 2024 07:12:07 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 2AA233F64C; Mon, 7 Oct 2024 07:11:36 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Dima Kogan , james.clark@linaro.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 2/3] perf probe: Check group string length Date: Mon, 7 Oct 2024 15:11:15 +0100 Message-Id: <20241007141116.882450-3-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141116.882450-1-leo.yan@arm.com> References: <20241007141116.882450-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In the kernel, the probe group string length is limited up to MAX_EVENT_NAME_LEN (including the NULL terminator). Check for this limitation and report an error if it is exceeded. Signed-off-by: Leo Yan Acked-by: Masami Hiramatsu (Google) --- tools/perf/util/probe-event.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index cad11d95af4f..71acea07cb46 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -2872,6 +2872,13 @@ static int probe_trace_event__set_name(struct probe_= trace_event *tev, else group =3D PERFPROBE_GROUP; =20 + if (strlen(group) >=3D MAX_EVENT_NAME_LEN) { + pr_err("Probe group string=3D'%s' is too long (>=3D %d bytes)\n", + group, MAX_EVENT_NAME_LEN); + ret =3D -ENOMEM; + goto out; + } + /* Get an unused new event name */ ret =3D get_new_event_name(buf, MAX_EVENT_NAME_LEN, event, namelist, tev->point.retprobe, allow_suffix); --=20 2.34.1 From nobody Wed Nov 27 21:50:57 2024 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B9F191D88A2; Mon, 7 Oct 2024 14:11:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310302; cv=none; b=uVWt4nk3IqukWUS0IW8CJyjPv5VvFwhADn8nZTU1xSmKiNVihhgMcqAZ1ZmgfjMatHJPgYRkIhWhQkbZ0h7eTsBQ982mY2J+R5wCIK2f2W+U0FXeV3/nZDI0mSJPi0pzO5rnRa0CybMoVlXlobrL/KMOfIoxR54BsaWgA4SAouQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728310302; c=relaxed/simple; bh=bTqJlxMu8UAh36R1oSXfPxrxzREjS1ZfQMC89zv8lVs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bw9Jkxke2WLTCdLAT9U1Y0BRLunjAzkFbv0xYdAiJRqzDGV8fjv3X+zE9wZfsGfWUALds69z51tKAFdK31CvvP5XOudzoD3tZplHYTfLthQi8QYxQ97aNxLhRULjKrQ2bYV4ylMhHezTrZvOTiCwEudfPU9J3/2+PssuVDkKa18= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C68C1FEC; Mon, 7 Oct 2024 07:12:09 -0700 (PDT) Received: from e132581.cambridge.arm.com (e132581.arm.com [10.2.76.71]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 5A0543F64C; Mon, 7 Oct 2024 07:11:38 -0700 (PDT) From: Leo Yan To: Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , Dima Kogan , james.clark@linaro.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Leo Yan Subject: [PATCH v1 3/3] perf probe: Generate hash event for long symbol Date: Mon, 7 Oct 2024 15:11:16 +0100 Message-Id: <20241007141116.882450-4-leo.yan@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241007141116.882450-1-leo.yan@arm.com> References: <20241007141116.882450-1-leo.yan@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a symbol name is longer than the maximum event length (64 bytes), generate an new event name with below combination: TruncatedSymbol + '_' + HashString + '__return' + '\0' `> 46B + 1B + 8B + 8B + 1B =3D 64 Bytes. With this change, a probe can be injected for long symbol. Before: # nm test_cpp_mangle | grep -E "print_data|Point" 0000000000000cac t _GLOBAL__sub_I__Z62this_is_a_very_very_long_print_data= _abcdefghijklmnopqrstuvwxyzi 0000000000000b50 T _Z62this_is_a_very_very_long_print_data_abcdefghijklmn= opqrstuvwxyzR5Point 0000000000000b14 T _Z62this_is_a_very_very_long_print_data_abcdefghijklmn= opqrstuvwxyzi # perf probe -x test_cpp_mangle --add \ "_Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyz= i" snprintf() failed: -7; the event name nbase=3D'_Z62this_is_a_very_very_lo= ng_print_data_abcdefghijklmnopqrstuvwxyzi' is too long Error: Failed to add events. After: # perf probe -x test_cpp_mangle --add \ "_Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyzi" Probe event=3D'_Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqr= stuvwxyzi' is too long (>=3D 64 bytes). Generate hashed event name=3D'_Z62this_is_a_very_very_long_print_data_abc= def_91f40679' Added new event: probe_test_cpp_mangle: _Z62this_is_a_very_very_long_print_data_abcdef_9= 1f40679 (on _Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyzi= in /mnt/test_cpp_mangle) You can now use it in all perf tools, such as: perf record -e probe_test_cpp_mangle: _Z62this_is_a_very_very_long_pr= int_data_abcdef_91f40679 -aR sleep 1 Signed-off-by: Leo Yan --- tools/perf/util/probe-event.c | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c index 71acea07cb46..bacd29b95c75 100644 --- a/tools/perf/util/probe-event.c +++ b/tools/perf/util/probe-event.c @@ -2837,6 +2837,32 @@ static void warn_uprobe_event_compat(struct probe_tr= ace_event *tev) /* Defined in kernel/trace/trace.h */ #define MAX_EVENT_NAME_LEN 64 =20 +static char *probe_trace_event__hash_event(const char *event) +{ + char *str =3D NULL; + size_t hash; + + str =3D malloc(MAX_EVENT_NAME_LEN); + if (!str) + return NULL; + + hash =3D str_hash(event); + + /* + * Reserve characters for the "__return" suffix for the return probe. + * Thus the string buffer (64 bytes) are used for: + * Truncated event: 46 bytes + * '_' : 1 byte + * hash string : 8 bytes + * reserved : 8 bytes (for suffix "__return") + * '\0' : 1 byte + */ + strncpy(str, event, 46); + /* '_' + hash string + '\0' */ + snprintf(str + 46, 10, "_%lx", hash); + return str; +} + /* Set new name from original perf_probe_event and namelist */ static int probe_trace_event__set_name(struct probe_trace_event *tev, struct perf_probe_event *pev, @@ -2844,7 +2870,7 @@ static int probe_trace_event__set_name(struct probe_t= race_event *tev, bool allow_suffix) { const char *event, *group; - char *buf; + char *buf, *hash_event =3D NULL; int ret; =20 buf =3D malloc(MAX_EVENT_NAME_LEN); @@ -2864,6 +2890,19 @@ static int probe_trace_event__set_name(struct probe_= trace_event *tev, event =3D pev->point.function; else event =3D tev->point.realname; + + if (strlen(event) >=3D MAX_EVENT_NAME_LEN) { + pr_warning("Probe event=3D'%s' is too long (>=3D %d bytes).\n", + event, MAX_EVENT_NAME_LEN); + + hash_event =3D probe_trace_event__hash_event(event); + if (!hash_event) { + ret =3D -ENOMEM; + goto out; + } + pr_warning("Generate hashed event name=3D'%s'\n", hash_event); + event =3D hash_event; + } } if (pev->group && !pev->sdt) group =3D pev->group; @@ -2903,6 +2942,7 @@ static int probe_trace_event__set_name(struct probe_t= race_event *tev, strlist__add(namelist, event); =20 out: + free(hash_event); free(buf); return ret < 0 ? ret : 0; } --=20 2.34.1