perf test demangle-rust: Add Rust demangling test
authorIan Rogers <irogers@google.com>
Wed, 30 Apr 2025 00:41:26 +0000 (17:41 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 9 May 2025 20:01:57 +0000 (17:01 -0300)
The test cases are listed examples in:

https://doc.rust-lang.org/rustc/symbol-mangling/v0.html

This test was previously part of a different Rust v0 demangler:

https://lore.kernel.org/lkml/20250129193037.573431-1-irogers@google.com/

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alex Gaynor <alex.gaynor@gmail.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alice Ryhl <aliceryhl@google.com>
Cc: Andreas Hindborg <a.hindborg@kernel.org>
Cc: Ariel Ben-Yehuda <ariel.byd@gmail.com>
Cc: Benno Lossin <benno.lossin@proton.me>
Cc: Bill Wendling <morbo@google.com>
Cc: Björn Roy Baron <bjorn3_gh@protonmail.com>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Daniel Xu <dxu@dxuuu.xyz>
Cc: Danilo Krummrich <dakr@kernel.org>
Cc: Gary Guo <gary@garyguo.net>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiapeng Chong <jiapeng.chong@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <nick.desaulniers+lkml@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Stephen Brennan <stephen.s.brennan@oracle.com>
Cc: Trevor Gross <tmgross@umich.edu>
Link: https://lore.kernel.org/r/20250430004128.474388-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/Build
tools/perf/tests/builtin-test.c
tools/perf/tests/demangle-rust-v0-test.c [new file with mode: 0644]
tools/perf/tests/tests.h
tools/perf/util/symbol-elf.c

index 934f320905531ce4dbdb630053da03fc7e873df9..2181f5a92148b0b90a430d6d9b38919dea105850 100644 (file)
@@ -56,6 +56,7 @@ perf-test-y += genelf.o
 perf-test-y += api-io.o
 perf-test-y += demangle-java-test.o
 perf-test-y += demangle-ocaml-test.o
+perf-test-y += demangle-rust-v0-test.o
 perf-test-y += pfm.o
 perf-test-y += parse-metric.o
 perf-test-y += pe-file-parsing.o
index 14d30a5053be59e3c6673fda8d93ec21d3bbae3b..45d3d8b3317a7c0a9a6add9c8dae99f3e10a8278 100644 (file)
@@ -126,6 +126,7 @@ static struct test_suite *generic_tests[] = {
        &suite__maps__merge_in,
        &suite__demangle_java,
        &suite__demangle_ocaml,
+       &suite__demangle_rust,
        &suite__parse_metric,
        &suite__pe_file_parsing,
        &suite__expand_cgroup_events,
diff --git a/tools/perf/tests/demangle-rust-v0-test.c b/tools/perf/tests/demangle-rust-v0-test.c
new file mode 100644 (file)
index 0000000..904f966
--- /dev/null
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: Apache-2.0 OR MIT
+#include "tests.h"
+#include "debug.h"
+#include "symbol.h"
+#include <linux/kernel.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int test__demangle_rust(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+{
+       int ret = TEST_OK;
+       char *buf = NULL;
+       size_t i;
+
+       struct {
+               const char *mangled, *demangled;
+       } test_cases[] = {
+               { "_RNvMsr_NtCs3ssYzQotkvD_3std4pathNtB5_7PathBuf3newCs15kBYyAo9fc_7mycrate",
+                 "<std::path::PathBuf>::new" },
+               { "_RNvCs15kBYyAo9fc_7mycrate7example",
+                 "mycrate::example" },
+               { "_RNvMs_Cs4Cv8Wi1oAIB_7mycrateNtB4_7Example3foo",
+                 "<mycrate::Example>::foo" },
+               { "_RNvXCs15kBYyAo9fc_7mycrateNtB2_7ExampleNtB2_5Trait3foo",
+                 "<mycrate::Example as mycrate::Trait>::foo" },
+               { "_RNvMCs7qp2U7fqm6G_7mycrateNtB2_7Example3foo",
+                 "<mycrate::Example>::foo" },
+               { "_RNvMs_Cs7qp2U7fqm6G_7mycrateNtB4_7Example3bar",
+                 "<mycrate::Example>::bar" },
+               { "_RNvYNtCs15kBYyAo9fc_7mycrate7ExampleNtB4_5Trait7exampleB4_",
+                 "<mycrate::Example as mycrate::Trait>::example" },
+               { "_RNCNvCsgStHSCytQ6I_7mycrate4main0B3_",
+                 "mycrate::main::{closure#0}" },
+               { "_RNCNvCsgStHSCytQ6I_7mycrate4mains_0B3_",
+                 "mycrate::main::{closure#1}" },
+               { "_RINvCsgStHSCytQ6I_7mycrate7examplelKj1_EB2_",
+                 "mycrate::example::<i32, 1>" },
+               { "_RINvCs7qp2U7fqm6G_7mycrate7exampleFG0_RL1_hRL0_tEuEB2_",
+                 "mycrate::example::<for<'a, 'b> fn(&'a u8, &'b u16)>",
+               },
+               { "_RINvCs7qp2U7fqm6G_7mycrate7exampleKy12345678_EB2_",
+                 "mycrate::example::<305419896>" },
+               { "_RNvNvMCsd9PVOYlP1UU_7mycrateINtB4_7ExamplepKpE3foo14EXAMPLE_STATIC",
+                 "<mycrate::Example<_, _>>::foo::EXAMPLE_STATIC",
+               },
+               { "_RINvCs7qp2U7fqm6G_7mycrate7exampleAtj8_EB2_",
+                 "mycrate::example::<[u16; 8]>" },
+               { "_RINvCs7qp2U7fqm6G_7mycrate7exampleNtB2_7ExampleBw_EB2_",
+                 "mycrate::example::<mycrate::Example, mycrate::Example>" },
+               { "_RINvMsY_NtCseXNvpPnDBDp_3std4pathNtB6_4Path3neweECs7qp2U7fqm6G_7mycrate",
+                 "<std::path::Path>::new::<str>" },
+               { "_RNvNvNvCs7qp2U7fqm6G_7mycrate7EXAMPLE7___getit5___KEY",
+                 "mycrate::EXAMPLE::__getit::__KEY" },
+       };
+
+       for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
+               buf = dso__demangle_sym(/*dso=*/NULL, /*kmodule=*/0, test_cases[i].mangled);
+               if (!buf) {
+                       pr_debug("FAILED to demangle: \"%s\"\n \"%s\"\n", test_cases[i].mangled,
+                                test_cases[i].demangled);
+                       continue;
+               }
+               if (strcmp(buf, test_cases[i].demangled)) {
+                       pr_debug("FAILED: %s: %s != %s\n", test_cases[i].mangled,
+                                buf, test_cases[i].demangled);
+                       ret = TEST_FAIL;
+               }
+               free(buf);
+       }
+
+       return ret;
+}
+
+DEFINE_SUITE("Demangle Rust", demangle_rust);
index 8aea344536b8ab7d4db94bd7ac702c1e162840c0..bb7951c6197144f59cca6fa4755b1fc497fe4cd5 100644 (file)
@@ -157,6 +157,7 @@ DECLARE_SUITE(jit_write_elf);
 DECLARE_SUITE(api_io);
 DECLARE_SUITE(demangle_java);
 DECLARE_SUITE(demangle_ocaml);
+DECLARE_SUITE(demangle_rust);
 DECLARE_SUITE(pfm);
 DECLARE_SUITE(parse_metric);
 DECLARE_SUITE(pe_file_parsing);
index 3fc87309746feb1b8329f21a7421b525084551f9..8734e8b6cf8479e3a6af7cc25a3299c37cd9b368 100644 (file)
@@ -319,7 +319,7 @@ static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
         * DWARF DW_compile_unit has this, but we don't always have access
         * to it...
         */
-       if (!want_demangle(dso__kernel(dso) || kmodule))
+       if (!want_demangle((dso && dso__kernel(dso)) || kmodule))
                return demangled;
 
        rust_demangle_demangle(elf_name, &rust_demangle);