Skip to content

Commit 4d9b514

Browse files
captain5050acmel
authored andcommitted
perf symbol: Move demangling code out of symbol-elf.c
symbol-elf.c is used when building with libelf, symbol-minimal is used otherwise. There is no reason the demangling code with no dependencies on libelf is part of symbol-elf.c so move to symbol.c. This allows demangling tests to pass with NO_LIBELF=1. Structurally, while moving the functions rename demangle_sym() to dso__demangle_sym() which is already a function exposed in symbol.h and the only purpose of which in symbol-elf.c was to call demangle_sym(). Change the calls to demangle_sym() in symbol-elf.c to calls to dso__demangle_sym(). 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: Andi Kleen <ak@linux.intel.com> Cc: Andreas Hindborg <a.hindborg@kernel.org> Cc: Benno Lossin <benno.lossin@proton.me> Cc: Björn Roy Baron <bjorn3_gh@protonmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Danilo Krummrich <dakr@kernel.org> Cc: Dmitriy Vyukov <dvyukov@google.com> 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: Kan Liang <kan.liang@linux.intel.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephen Brennan <stephen.s.brennan@oracle.com> Cc: Trevor Gross <tmgross@umich.edu> Cc: Weilin Wang <weilin.wang@intel.com> Link: https://lore.kernel.org/r/20250528210858.499898-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent c7a48ea commit 4d9b514

File tree

4 files changed

+87
-96
lines changed

4 files changed

+87
-96
lines changed

tools/perf/util/demangle-cxx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef __PERF_DEMANGLE_CXX
33
#define __PERF_DEMANGLE_CXX 1
44

5+
#include <stdbool.h>
6+
57
#ifdef __cplusplus
68
extern "C" {
79
#endif

tools/perf/util/symbol-elf.c

Lines changed: 3 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@
1313
#include "maps.h"
1414
#include "symbol.h"
1515
#include "symsrc.h"
16-
#include "demangle-cxx.h"
17-
#include "demangle-ocaml.h"
18-
#include "demangle-java.h"
19-
#include "demangle-rust-v0.h"
2016
#include "machine.h"
2117
#include "vdso.h"
2218
#include "debug.h"
2319
#include "util/copyfile.h"
2420
#include <linux/ctype.h>
2521
#include <linux/kernel.h>
26-
#include <linux/log2.h>
2722
#include <linux/zalloc.h>
2823
#include <linux/string.h>
2924
#include <symbol/kallsyms.h>
@@ -280,82 +275,6 @@ static int elf_read_program_header(Elf *elf, u64 vaddr, GElf_Phdr *phdr)
280275
return -1;
281276
}
282277

283-
static bool want_demangle(bool is_kernel_sym)
284-
{
285-
return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
286-
}
287-
288-
/*
289-
* Demangle C++ function signature, typically replaced by demangle-cxx.cpp
290-
* version.
291-
*/
292-
#ifndef HAVE_CXA_DEMANGLE_SUPPORT
293-
char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
294-
bool modifiers __maybe_unused)
295-
{
296-
#ifdef HAVE_LIBBFD_SUPPORT
297-
int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
298-
299-
return bfd_demangle(NULL, str, flags);
300-
#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
301-
int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
302-
303-
return cplus_demangle(str, flags);
304-
#else
305-
return NULL;
306-
#endif
307-
}
308-
#endif /* !HAVE_CXA_DEMANGLE_SUPPORT */
309-
310-
static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
311-
{
312-
struct demangle rust_demangle = {
313-
.style = DemangleStyleUnknown,
314-
};
315-
char *demangled = NULL;
316-
317-
/*
318-
* We need to figure out if the object was created from C++ sources
319-
* DWARF DW_compile_unit has this, but we don't always have access
320-
* to it...
321-
*/
322-
if (!want_demangle((dso && dso__kernel(dso)) || kmodule))
323-
return demangled;
324-
325-
rust_demangle_demangle(elf_name, &rust_demangle);
326-
if (rust_demangle_is_known(&rust_demangle)) {
327-
/* A rust mangled name. */
328-
if (rust_demangle.mangled_len == 0)
329-
return demangled;
330-
331-
for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2);
332-
buf_len < 1024 * 1024; buf_len += 32) {
333-
char *tmp = realloc(demangled, buf_len);
334-
335-
if (!tmp) {
336-
/* Failure to grow output buffer, return what is there. */
337-
return demangled;
338-
}
339-
demangled = tmp;
340-
if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len,
341-
/*alternate=*/true) == OverflowOk)
342-
return demangled;
343-
}
344-
/* Buffer exceeded sensible bounds, return what is there. */
345-
return demangled;
346-
}
347-
348-
demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0);
349-
if (demangled)
350-
return demangled;
351-
352-
demangled = ocaml_demangle_sym(elf_name);
353-
if (demangled)
354-
return demangled;
355-
356-
return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
357-
}
358-
359278
struct rel_info {
360279
u32 nr_entries;
361280
u32 *sorted;
@@ -641,7 +560,7 @@ static bool get_plt_got_name(GElf_Shdr *shdr, size_t i,
641560
/* Get the associated symbol */
642561
gelf_getsym(di->dynsym_data, vr->sym_idx, &sym);
643562
sym_name = elf_sym__name(&sym, di->dynstr_data);
644-
demangled = demangle_sym(di->dso, 0, sym_name);
563+
demangled = dso__demangle_sym(di->dso, /*kmodule=*/0, sym_name);
645564
if (demangled != NULL)
646565
sym_name = demangled;
647566

@@ -839,7 +758,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
839758
gelf_getsym(syms, get_rel_symidx(&ri, idx), &sym);
840759

841760
elf_name = elf_sym__name(&sym, symstrs);
842-
demangled = demangle_sym(dso, 0, elf_name);
761+
demangled = dso__demangle_sym(dso, /*kmodule=*/0, elf_name);
843762
if (demangled)
844763
elf_name = demangled;
845764
if (*elf_name)
@@ -868,11 +787,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss)
868787
return 0;
869788
}
870789

871-
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
872-
{
873-
return demangle_sym(dso, kmodule, elf_name);
874-
}
875-
876790
/*
877791
* Align offset to 4 bytes as needed for note name and descriptor data.
878792
*/
@@ -1861,7 +1775,7 @@ dso__load_sym_internal(struct dso *dso, struct map *map, struct symsrc *syms_ss,
18611775
}
18621776
}
18631777

1864-
demangled = demangle_sym(dso, kmodule, elf_name);
1778+
demangled = dso__demangle_sym(dso, kmodule, elf_name);
18651779
if (demangled != NULL)
18661780
elf_name = demangled;
18671781

tools/perf/util/symbol-minimal.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,13 +355,6 @@ void symbol__elf_init(void)
355355
{
356356
}
357357

358-
char *dso__demangle_sym(struct dso *dso __maybe_unused,
359-
int kmodule __maybe_unused,
360-
const char *elf_name __maybe_unused)
361-
{
362-
return NULL;
363-
}
364-
365358
bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused)
366359
{
367360
return false;

tools/perf/util/symbol.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
#include "build-id.h"
2020
#include "cap.h"
2121
#include "cpumap.h"
22+
#include "debug.h"
23+
#include "demangle-cxx.h"
24+
#include "demangle-java.h"
25+
#include "demangle-ocaml.h"
26+
#include "demangle-rust-v0.h"
2227
#include "dso.h"
2328
#include "util.h" // lsdir()
2429
#include "debug.h"
@@ -36,6 +41,7 @@
3641
#include "header.h"
3742
#include "path.h"
3843
#include <linux/ctype.h>
44+
#include <linux/log2.h>
3945
#include <linux/zalloc.h>
4046

4147
#include <elf.h>
@@ -2648,3 +2654,79 @@ int symbol__validate_sym_arguments(void)
26482654
}
26492655
return 0;
26502656
}
2657+
2658+
static bool want_demangle(bool is_kernel_sym)
2659+
{
2660+
return is_kernel_sym ? symbol_conf.demangle_kernel : symbol_conf.demangle;
2661+
}
2662+
2663+
/*
2664+
* Demangle C++ function signature, typically replaced by demangle-cxx.cpp
2665+
* version.
2666+
*/
2667+
#ifndef HAVE_CXA_DEMANGLE_SUPPORT
2668+
char *cxx_demangle_sym(const char *str __maybe_unused, bool params __maybe_unused,
2669+
bool modifiers __maybe_unused)
2670+
{
2671+
#ifdef HAVE_LIBBFD_SUPPORT
2672+
int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
2673+
2674+
return bfd_demangle(NULL, str, flags);
2675+
#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
2676+
int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
2677+
2678+
return cplus_demangle(str, flags);
2679+
#else
2680+
return NULL;
2681+
#endif
2682+
}
2683+
#endif /* !HAVE_CXA_DEMANGLE_SUPPORT */
2684+
2685+
char *dso__demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
2686+
{
2687+
struct demangle rust_demangle = {
2688+
.style = DemangleStyleUnknown,
2689+
};
2690+
char *demangled = NULL;
2691+
2692+
/*
2693+
* We need to figure out if the object was created from C++ sources
2694+
* DWARF DW_compile_unit has this, but we don't always have access
2695+
* to it...
2696+
*/
2697+
if (!want_demangle((dso && dso__kernel(dso)) || kmodule))
2698+
return demangled;
2699+
2700+
rust_demangle_demangle(elf_name, &rust_demangle);
2701+
if (rust_demangle_is_known(&rust_demangle)) {
2702+
/* A rust mangled name. */
2703+
if (rust_demangle.mangled_len == 0)
2704+
return demangled;
2705+
2706+
for (size_t buf_len = roundup_pow_of_two(rust_demangle.mangled_len * 2);
2707+
buf_len < 1024 * 1024; buf_len += 32) {
2708+
char *tmp = realloc(demangled, buf_len);
2709+
2710+
if (!tmp) {
2711+
/* Failure to grow output buffer, return what is there. */
2712+
return demangled;
2713+
}
2714+
demangled = tmp;
2715+
if (rust_demangle_display_demangle(&rust_demangle, demangled, buf_len,
2716+
/*alternate=*/true) == OverflowOk)
2717+
return demangled;
2718+
}
2719+
/* Buffer exceeded sensible bounds, return what is there. */
2720+
return demangled;
2721+
}
2722+
2723+
demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0);
2724+
if (demangled)
2725+
return demangled;
2726+
2727+
demangled = ocaml_demangle_sym(elf_name);
2728+
if (demangled)
2729+
return demangled;
2730+
2731+
return java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
2732+
}

0 commit comments

Comments
 (0)