Re: [PATCH v1 3/3] perf probe: Generate hash event for long symbol

From: Google
Date: Thu Oct 10 2024 - 11:35:19 EST


On Mon, 7 Oct 2024 15:11:16 +0100
Leo Yan <leo.yan@xxxxxxx> wrote:

> 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 = 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_abcdefghijklmnopqrstuvwxyzR5Point
> 0000000000000b14 T _Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyzi
>
> # perf probe -x test_cpp_mangle --add \
> "_Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyzi"
> snprintf() failed: -7; the event name nbase='_Z62this_is_a_very_very_long_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='_Z62this_is_a_very_very_long_print_data_abcdefghijklmnopqrstuvwxyzi' is too long (>= 64 bytes).
> Generate hashed event name='_Z62this_is_a_very_very_long_print_data_abcdef_91f40679'
>
> Added new event:
> probe_test_cpp_mangle: _Z62this_is_a_very_very_long_print_data_abcdef_91f40679
> (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_print_data_abcdef_91f40679 -aR sleep 1

OK, personally, I recommend you to specify event name instead of generating
long event name in this case. But I understand sometimes this kind of feature
is good for someone.

BTW, I would like to confirm. Can't we demangle the symbol name and parse it?

Thank you,

>
> Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
> ---
> 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_trace_event *tev)
> /* Defined in kernel/trace/trace.h */
> #define MAX_EVENT_NAME_LEN 64
>
> +static char *probe_trace_event__hash_event(const char *event)
> +{
> + char *str = NULL;
> + size_t hash;
> +
> + str = malloc(MAX_EVENT_NAME_LEN);
> + if (!str)
> + return NULL;
> +
> + hash = 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_trace_event *tev,
> bool allow_suffix)
> {
> const char *event, *group;
> - char *buf;
> + char *buf, *hash_event = NULL;
> int ret;
>
> buf = malloc(MAX_EVENT_NAME_LEN);
> @@ -2864,6 +2890,19 @@ static int probe_trace_event__set_name(struct probe_trace_event *tev,
> event = pev->point.function;
> else
> event = tev->point.realname;
> +
> + if (strlen(event) >= MAX_EVENT_NAME_LEN) {
> + pr_warning("Probe event='%s' is too long (>= %d bytes).\n",
> + event, MAX_EVENT_NAME_LEN);
> +
> + hash_event = probe_trace_event__hash_event(event);
> + if (!hash_event) {
> + ret = -ENOMEM;
> + goto out;
> + }
> + pr_warning("Generate hashed event name='%s'\n", hash_event);
> + event = hash_event;
> + }
> }
> if (pev->group && !pev->sdt)
> group = pev->group;
> @@ -2903,6 +2942,7 @@ static int probe_trace_event__set_name(struct probe_trace_event *tev,
> strlist__add(namelist, event);
>
> out:
> + free(hash_event);
> free(buf);
> return ret < 0 ? ret : 0;
> }
> --
> 2.34.1
>
>


--
Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>