linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: "Peter Zijlstra" <peterz@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Arnaldo Carvalho de Melo" <acme@kernel.org>,
	"Mark Rutland" <mark.rutland@arm.com>,
	"Alexander Shishkin" <alexander.shishkin@linux.intel.com>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Namhyung Kim" <namhyung@kernel.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nick Desaulniers" <ndesaulniers@google.com>,
	"Tom Rix" <trix@redhat.com>,
	"Roberto Sassu" <roberto.sassu@huawei.com>,
	"Quentin Monnet" <quentin@isovalent.com>,
	"Andres Freund" <andres@anarazel.de>,
	"Tiezhu Yang" <yangtiezhu@loongson.cn>,
	"Pavithra Gurushankar" <gpavithrasha@gmail.com>,
	"Yang Jihong" <yangjihong1@huawei.com>,
	"Adrian Hunter" <adrian.hunter@intel.com>,
	"Leo Yan" <leo.yan@linaro.org>, "Martin Liška" <mliska@suse.cz>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	llvm@lists.linux.dev
Cc: Stephane Eranian <eranian@google.com>, Ian Rogers <irogers@google.com>
Subject: [PATCH v1 09/13] perf symbol: Add abi::__cxa_demangle C++ demangling support
Date: Fri, 10 Mar 2023 22:57:49 -0800	[thread overview]
Message-ID: <20230311065753.3012826-10-irogers@google.com> (raw)
In-Reply-To: <20230311065753.3012826-1-irogers@google.com>

Refactor C++ demangling out of symbol-elf into its own files similar
to other languages. Add abi::__cxa_demangle support. As the other
demanglers are not shippable with distributions, this brings back C++
demangling in a common case. It isn't perfect as the support for
optionally demangling arguments and modifiers isn't present.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/Makefile.config       | 29 +++++++++---------
 tools/perf/util/Build            |  1 +
 tools/perf/util/demangle-cxx.cpp | 50 ++++++++++++++++++++++++++++++++
 tools/perf/util/demangle-cxx.h   | 16 ++++++++++
 tools/perf/util/symbol-elf.c     | 37 +++++------------------
 5 files changed, 88 insertions(+), 45 deletions(-)
 create mode 100644 tools/perf/util/demangle-cxx.cpp
 create mode 100644 tools/perf/util/demangle-cxx.h

diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
index 5756498248e0..fdeca45cf15f 100644
--- a/tools/perf/Makefile.config
+++ b/tools/perf/Makefile.config
@@ -906,6 +906,7 @@ ifdef BUILD_NONDISTRO
   endif
 
   CFLAGS += -DHAVE_LIBBFD_SUPPORT
+  CXXFLAGS += -DHAVE_LIBBFD_SUPPORT
   ifeq ($(feature-libbfd-buildid), 1)
     CFLAGS += -DHAVE_LIBBFD_BUILDID_SUPPORT
   else
@@ -913,26 +914,24 @@ ifdef BUILD_NONDISTRO
   endif
 endif
 
-ifdef NO_DEMANGLE
-  CFLAGS += -DNO_DEMANGLE
-else
+ifndef NO_DEMANGLE
+  $(call feature_check,cxa-demangle)
+  ifeq ($(feature-cxa-demangle), 1)
+    EXTLIBS += -lstdc++
+    CFLAGS += -DHAVE_CXA_DEMANGLE_SUPPORT
+    CXXFLAGS += -DHAVE_CXA_DEMANGLE_SUPPORT
+  endif
   ifdef BUILD_NONDISTRO
     ifeq ($(filter -liberty,$(EXTLIBS)),)
-      ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
+      $(call feature_check,cplus-demangle)
+      ifeq ($(feature-cplus-demangle), 1)
         EXTLIBS += -liberty
-      else
-        $(call feature_check,cplus-demangle)
-        ifeq ($(feature-cplus-demangle), 1)
-          EXTLIBS += -liberty
-        endif
       endif
     endif
-  endif
-
-  ifneq ($(filter -liberty,$(EXTLIBS)),)
-    CFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
-  else
-    CFLAGS += -DNO_DEMANGLE
+    ifneq ($(filter -liberty,$(EXTLIBS)),)
+      CFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
+      CXXFLAGS += -DHAVE_CPLUS_DEMANGLE_SUPPORT
+    endif
   endif
 endif
 
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 918b501f9bd8..8607575183a9 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -211,6 +211,7 @@ perf-$(CONFIG_ZSTD) += zstd.o
 
 perf-$(CONFIG_LIBCAP) += cap.o
 
+perf-y += demangle-cxx.o
 perf-y += demangle-ocaml.o
 perf-y += demangle-java.o
 perf-y += demangle-rust.o
diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangle-cxx.cpp
new file mode 100644
index 000000000000..8708bcafd370
--- /dev/null
+++ b/tools/perf/util/demangle-cxx.cpp
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "demangle-cxx.h"
+#include <stdlib.h>
+#include <string.h>
+#include <linux/compiler.h>
+
+#ifdef HAVE_LIBBFD_SUPPORT
+#define PACKAGE 'perf'
+#include <bfd.h>
+#endif
+
+#ifdef HAVE_CXA_DEMANGLE_SUPPORT
+#include <cxxabi.h>
+#endif
+
+#if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
+#ifndef DMGL_PARAMS
+#define DMGL_PARAMS     (1 << 0)  /* Include function args */
+#define DMGL_ANSI       (1 << 1)  /* Include const, volatile, etc */
+#endif
+#endif
+
+/*
+ * Demangle C++ function signature
+ *
+ * Note: caller is responsible for freeing demangled string
+ */
+extern "C"
+char *cxx_demangle_sym(const char *str, bool params __maybe_unused,
+                       bool modifiers __maybe_unused)
+{
+#ifdef HAVE_LIBBFD_SUPPORT
+        int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
+
+        return bfd_demangle(NULL, str, flags);
+#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
+        int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
+
+        return cplus_demangle(str, flags);
+#elif defined(HAVE_CXA_DEMANGLE_SUPPORT)
+        size_t len = strlen(str);
+        char *output = (char*)malloc(len);
+        int status;
+
+        output = abi::__cxa_demangle(str, output, &len, &status);
+        return output;
+#else
+        return NULL;
+#endif
+}
diff --git a/tools/perf/util/demangle-cxx.h b/tools/perf/util/demangle-cxx.h
new file mode 100644
index 000000000000..26b5b66c0b4e
--- /dev/null
+++ b/tools/perf/util/demangle-cxx.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __PERF_DEMANGLE_CXX
+#define __PERF_DEMANGLE_CXX 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *cxx_demangle_sym(const char *str, bool params, bool modifiers);
+
+#ifdef __cplusplus
+}
+#endif
+
+
+#endif /* __PERF_DEMANGLE_CXX */
diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c
index 41882ae8452e..c0a2de42c51b 100644
--- a/tools/perf/util/symbol-elf.c
+++ b/tools/perf/util/symbol-elf.c
@@ -12,6 +12,7 @@
 #include "maps.h"
 #include "symbol.h"
 #include "symsrc.h"
+#include "demangle-cxx.h"
 #include "demangle-ocaml.h"
 #include "demangle-java.h"
 #include "demangle-rust.h"
@@ -25,6 +26,11 @@
 #include <symbol/kallsyms.h>
 #include <internal/lib.h>
 
+#ifdef HAVE_LIBBFD_SUPPORT
+#define PACKAGE 'perf'
+#include <bfd.h>
+#endif
+
 #ifndef EM_AARCH64
 #define EM_AARCH64	183  /* ARM 64 bit */
 #endif
@@ -45,34 +51,6 @@
 
 typedef Elf64_Nhdr GElf_Nhdr;
 
-#ifndef DMGL_PARAMS
-#define DMGL_NO_OPTS     0              /* For readability... */
-#define DMGL_PARAMS      (1 << 0)       /* Include function args */
-#define DMGL_ANSI        (1 << 1)       /* Include const, volatile, etc */
-#endif
-
-#ifdef HAVE_LIBBFD_SUPPORT
-#define PACKAGE 'perf'
-#include <bfd.h>
-#else
-#ifdef HAVE_CPLUS_DEMANGLE_SUPPORT
-extern char *cplus_demangle(const char *, int);
-
-static inline char *bfd_demangle(void __maybe_unused *v, const char *c, int i)
-{
-	return cplus_demangle(c, i);
-}
-#else
-#ifdef NO_DEMANGLE
-static inline char *bfd_demangle(void __maybe_unused *v,
-				 const char __maybe_unused *c,
-				 int __maybe_unused i)
-{
-	return NULL;
-}
-#endif
-#endif
-#endif
 
 #ifndef HAVE_ELF_GETPHDRNUM_SUPPORT
 static int elf_getphdrnum(Elf *elf, size_t *dst)
@@ -295,7 +273,6 @@ static bool want_demangle(bool is_kernel_sym)
 
 static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
 {
-	int demangle_flags = verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : DMGL_NO_OPTS;
 	char *demangled = NULL;
 
 	/*
@@ -306,7 +283,7 @@ static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name)
 	if (!want_demangle(dso->kernel || kmodule))
 	    return demangled;
 
-	demangled = bfd_demangle(NULL, elf_name, demangle_flags);
+	demangled = cxx_demangle_sym(elf_name, verbose > 0, verbose > 0);
 	if (demangled == NULL) {
 		demangled = ocaml_demangle_sym(elf_name);
 		if (demangled == NULL) {
-- 
2.40.0.rc1.284.g88254d51c5-goog


  parent reply	other threads:[~2023-03-11  6:59 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-11  6:57 [PATCH v1 00/13] Perf tool build improvements Ian Rogers
2023-03-11  6:57 ` [PATCH v1 01/13] perf build: Support python/perf.so testing Ian Rogers
2023-03-11  6:57 ` [PATCH v1 02/13] perf build: Make BUILD_BPF_SKEL default, rename to NO_BPF_SKEL Ian Rogers
2023-03-13 20:19   ` Arnaldo Carvalho de Melo
2023-03-13 20:27     ` Ian Rogers
2023-03-13 20:34       ` Arnaldo Carvalho de Melo
2023-03-13 20:59         ` Arnaldo Carvalho de Melo
2023-03-13 21:05           ` Arnaldo Carvalho de Melo
2023-03-11  6:57 ` [PATCH v1 03/13] perf build: Remove unused HAVE_GLIBC_SUPPORT Ian Rogers
2023-03-11  6:57 ` [PATCH v1 04/13] perf build: Error if no libelf and NO_LIBELF isn't set Ian Rogers
2023-03-13 19:45   ` Arnaldo Carvalho de Melo
2023-03-13 20:18     ` Ian Rogers
2023-03-11  6:57 ` [PATCH v1 05/13] perf util: Remove weak sched_getcpu Ian Rogers
2023-03-11  6:57 ` [PATCH v1 06/13] perf build: Error if jevents won't work and NO_JEVENTS=1 isn't set Ian Rogers
2023-03-11  6:57 ` [PATCH v1 07/13] perf build: Make binutil libraries opt in Ian Rogers
2023-03-13 19:37   ` Arnaldo Carvalho de Melo
2023-03-11  6:57 ` [PATCH v1 08/13] tools build: Add feature test for abi::__cxa_demangle Ian Rogers
2023-03-11  6:57 ` Ian Rogers [this message]
2023-03-30 14:08   ` [PATCH v1 09/13] perf symbol: Add abi::__cxa_demangle C++ demangling support James Clark
2023-03-30 16:50     ` Ian Rogers
2023-03-30 19:03       ` Ian Rogers
2023-03-31  9:27         ` James Clark
2023-03-11  6:57 ` [PATCH v1 10/13] perf build: Switch libpfm4 to opt-out rather than opt-in Ian Rogers
2023-03-11  6:57 ` [PATCH v1 11/13] perf build: If libtraceevent isn't present error the build Ian Rogers
2023-03-11  6:57 ` [PATCH v1 12/13] tools headers: Make the difference output easier to read Ian Rogers
2023-03-13 19:35   ` Arnaldo Carvalho de Melo
2023-03-13 20:14     ` Ian Rogers
2023-03-11  6:57 ` [PATCH v1 13/13] perf build: Remove redundant NO_NEWT build option Ian Rogers
2023-03-14 12:11 ` [PATCH v1 00/13] Perf tool build improvements Arnaldo Carvalho de Melo
2023-03-14 12:12   ` Arnaldo Carvalho de Melo
2023-03-14 12:27     ` Arnaldo Carvalho de Melo
2023-03-14 12:29       ` Arnaldo Carvalho de Melo
2023-03-14 12:21   ` Adrian Hunter
2023-03-14 12:28     ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230311065753.3012826-10-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andres@anarazel.de \
    --cc=eranian@google.com \
    --cc=gpavithrasha@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=leo.yan@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=mliska@suse.cz \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=quentin@isovalent.com \
    --cc=roberto.sassu@huawei.com \
    --cc=trix@redhat.com \
    --cc=yangjihong1@huawei.com \
    --cc=yangtiezhu@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).