Message ID | 20230311065753.3012826-10-irogers@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp157801wrd; Fri, 10 Mar 2023 23:03:19 -0800 (PST) X-Google-Smtp-Source: AK7set9LnWY0kyFGKC+y4Am5Qsm36KtbRl/mttad4AYp5DzMdnGgduigVDpUKAYKy/NSxRKpN1ov X-Received: by 2002:a17:902:c40d:b0:19e:61c8:e374 with SMTP id k13-20020a170902c40d00b0019e61c8e374mr35330260plk.29.1678518198830; Fri, 10 Mar 2023 23:03:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678518198; cv=none; d=google.com; s=arc-20160816; b=LCPhzLsPChVxyxglHWM6Pyog4f/JmLRSqf8yvrLzwEbH5QEvxjOqGF3rR2ZaaHrK85 XGvwVSBA2S0VE9+JGStb8EeMtr/s+h4GJ0qLDJlo47CFxrzQhmhigccekmt9jvPUYcl9 /2Wt629INaNteaZegofm7rHdklDGerT0CD22+Jm6FfTNQFHujAVDcJopdsPIo9GgJi7u G855KPqWrXA4UXI2+8JwkW3sFqslNANu44TN1Uar5R4O4c0nDvk1EKuSJEFSYW6NeJzn dFvDlHQTRXmuJPVP1Ik9yUSl8qLSMp0tKpR45iR91cDASrgp/iNiGOIWgnCUucQ53miX LkqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=TM6MVHcCvuJekzTOoSVMhLDs6+EBUa5GfqRq4fZc+Ec=; b=nxumdu5yw3lb+ndxYzCES3iXwBzR6OAuqfuQFAsSSbRKQjopCwuF1UW8ZJYQz0sl/C v0HTz8V+IeM5uVNDbu363oOmXGYcVIog6SfK6Z8Mad+VuSNpjEDKuoi+fzaKhFIHg4s/ PQJ1FtlPqm2tNV1BfPyUuIXBI5kveKDIrJf3Yg7HpSs6/EubkR96J1nSL4sLXQUXVfTu K4+LUtDG8AgWLAkt+FQp6jzR7uQ8PjlBFsGVhYgvhK+3HId09RxQukzE443vjAvGpt1s rFbTnOQcXEO54BQYL7ZKm01utj4GmzrVpGuSjp/sFmLX1eFb63w3ZQF/ZnFZsHTJidOF V8lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HdhM8KBj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kc7-20020a17090333c700b0019608f59f8bsi1743650plb.64.2023.03.10.23.03.04; Fri, 10 Mar 2023 23:03:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=HdhM8KBj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230119AbjCKG7z (ORCPT <rfc822;carlos.wei.hk@gmail.com> + 99 others); Sat, 11 Mar 2023 01:59:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230063AbjCKG7q (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 11 Mar 2023 01:59:46 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBBB5144845 for <linux-kernel@vger.kernel.org>; Fri, 10 Mar 2023 22:59:14 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id h3-20020a25d003000000b00a1a201a745aso8194141ybg.22 for <linux-kernel@vger.kernel.org>; Fri, 10 Mar 2023 22:59:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678517952; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=TM6MVHcCvuJekzTOoSVMhLDs6+EBUa5GfqRq4fZc+Ec=; b=HdhM8KBj3naBHJmHM7igItQ5pBTYYwK7qACdj0h9KiPaEWKZLsUQBg7f1POy+kvwR0 XAIB8LuFlxWvu5TJMnOKluFvecDnp4+Xw8uQugf394yiK26eDGIZZqr7zTQUjPBdFLul 1ff2oQ0gCAQDA25UdC3vYZNsEI3KK1fWX0PwAZRays8oDpkiKTfFz5wYtByAr9wryMG4 a9W8QMUejxImdACl7uh8gRxo2ihfKbxsJysnghExmev5iG4I7yWGlAqOThA+lLRGYcvb tceKy1NLHq1aJo1vvHH9YBqDy9NL/LVKMRu4baJXtD8QlnkxKOs5DQDOn5p5mPfW2kc4 wNvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678517952; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TM6MVHcCvuJekzTOoSVMhLDs6+EBUa5GfqRq4fZc+Ec=; b=7FcWiXggURg0v1ygcdzfak3alPxZv45+YxxByfKzabQSzpUhsAJ6qv368NDrLDf3P0 pp9bt9YWP8AkXcdGY670VYekVW3GnKKvWNOw3EuujBZAZ8iPbXMhwnKFy1vfFObuzbR3 GRl/2qcfLt9DXKQwpNRW74pSMzE7Rf9dvzpEwi5emHDIdnfr27zljOdvT2xgvLNSbjZd hamfU6V1ZG1EOSWYQOW4SltZyDjKUKjO9vjdjl75MRRXIE8zbIIY74oeBkLBV4yKg9V2 dXTJJnlOtP4JAJYQ23AxaDpeAbAm+/ZIyBtlg8svoKc58dCyk+HLKcBq8sYzvWIfUIdd MWtQ== X-Gm-Message-State: AO0yUKUF8TxexQMzaqd7jMQpR1YmxRQ+IeN5eDafWf2pSueK3wt2Q7YZ ZvVP9cPGOfglab8IsUTNqoipS2RVPQBJ X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:a11d:a763:a328:f2d6]) (user=irogers job=sendgmr) by 2002:a5b:83:0:b0:a17:c04e:bf7c with SMTP id b3-20020a5b0083000000b00a17c04ebf7cmr17133991ybp.11.1678517952523; Fri, 10 Mar 2023 22:59:12 -0800 (PST) Date: Fri, 10 Mar 2023 22:57:49 -0800 In-Reply-To: <20230311065753.3012826-1-irogers@google.com> Message-Id: <20230311065753.3012826-10-irogers@google.com> Mime-Version: 1.0 References: <20230311065753.3012826-1-irogers@google.com> X-Mailer: git-send-email 2.40.0.rc1.284.g88254d51c5-goog Subject: [PATCH v1 09/13] perf symbol: Add abi::__cxa_demangle C++ demangling support 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>, " =?utf-8?q?Martin_Li=C5=A1ka?= " <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> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760053898884363637?= X-GMAIL-MSGID: =?utf-8?q?1760053898884363637?= |
Series |
Perf tool build improvements
|
|
Commit Message
Ian Rogers
March 11, 2023, 6:57 a.m. UTC
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
Comments
On 11/03/2023 06:57, Ian Rogers wrote: > 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++ Hi Ian, I think cross compilation for arm on x86 isn't working after this change (at least on Ubuntu). Even with all of the arm64 libstdc++ stuff installed, you can only link to it using g++, but the perf build tries to link to it using gcc. Not sure it's some quirk with the search paths on Ubuntu or something else: $ aarch64-linux-gnu-gcc -lstdc++ /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux- gnu/bin/ld: cannot find -lstdc++: No such file or directory collect2: error: ld returned 1 exit status g++ gets further: $ aarch64-linux-gnu-g++ -lstdc++ ... (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status At the end of the perf build it looks like something similar is happening (with all the non interesting bits deleted): $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf NO_BPF_SKEL=1 V=1 aarch64-linux-gnu-gcc ... -o perf /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: cannot find -lstdc++: No such file or directory If I build with NO_DEMANGLE=1 then the build works, but I think it would at be best to autodetect rather than have to do this. Or maybe even link at the end with g++ if we're going to use libstdc++? Thanks James > + 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) {
On Thu, Mar 30, 2023 at 7:08 AM James Clark <james.clark@arm.com> wrote: > > > > On 11/03/2023 06:57, Ian Rogers wrote: > > 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++ > > Hi Ian, > > I think cross compilation for arm on x86 isn't working after this change > (at least on Ubuntu). > > Even with all of the arm64 libstdc++ stuff installed, you can only link > to it using g++, but the perf build tries to link to it using gcc. Not > sure it's some quirk with the search paths on Ubuntu or something else: > > $ aarch64-linux-gnu-gcc -lstdc++ > > /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux- > gnu/bin/ld: cannot find -lstdc++: No such file or directory > collect2: error: ld returned 1 exit status > > g++ gets further: > > $ aarch64-linux-gnu-g++ -lstdc++ > > ... > (.text+0x20): undefined reference to `main' > collect2: error: ld returned 1 exit status > > At the end of the perf build it looks like something similar is > happening (with all the non interesting bits deleted): > > $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf > NO_BPF_SKEL=1 V=1 > > aarch64-linux-gnu-gcc ... -o perf > > /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: > cannot find -lstdc++: No such file or directory > > If I build with NO_DEMANGLE=1 then the build works, but I think it would > at be best to autodetect rather than have to do this. Or maybe even link > at the end with g++ if we're going to use libstdc++? Hi James, sorry for the problems you are having, I'll see if I can get a repo. I did add a feature test with this change in the same set: https://lore.kernel.org/lkml/20230311065753.3012826-9-irogers@google.com/ So it should be feature testing and only enabling when HAVE_CXA_DEMANGLE_SUPPORT is present. Obviously something is up, so I'll have a think about it. Thanks, Ian > Thanks > James > > > + 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) {
On Thu, Mar 30, 2023 at 9:50 AM Ian Rogers <irogers@google.com> wrote: > > On Thu, Mar 30, 2023 at 7:08 AM James Clark <james.clark@arm.com> wrote: > > > > > > > > On 11/03/2023 06:57, Ian Rogers wrote: > > > 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++ > > > > Hi Ian, > > > > I think cross compilation for arm on x86 isn't working after this change > > (at least on Ubuntu). > > > > Even with all of the arm64 libstdc++ stuff installed, you can only link > > to it using g++, but the perf build tries to link to it using gcc. Not > > sure it's some quirk with the search paths on Ubuntu or something else: > > > > $ aarch64-linux-gnu-gcc -lstdc++ > > > > /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux- > > gnu/bin/ld: cannot find -lstdc++: No such file or directory > > collect2: error: ld returned 1 exit status > > > > g++ gets further: > > > > $ aarch64-linux-gnu-g++ -lstdc++ > > > > ... > > (.text+0x20): undefined reference to `main' > > collect2: error: ld returned 1 exit status > > > > At the end of the perf build it looks like something similar is > > happening (with all the non interesting bits deleted): > > > > $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf > > NO_BPF_SKEL=1 V=1 > > > > aarch64-linux-gnu-gcc ... -o perf > > > > /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: > > cannot find -lstdc++: No such file or directory > > > > If I build with NO_DEMANGLE=1 then the build works, but I think it would > > at be best to autodetect rather than have to do this. Or maybe even link > > at the end with g++ if we're going to use libstdc++? > > Hi James, > > sorry for the problems you are having, I'll see if I can get a repo. I > did add a feature test with this change in the same set: > https://lore.kernel.org/lkml/20230311065753.3012826-9-irogers@google.com/ > So it should be feature testing and only enabling when > HAVE_CXA_DEMANGLE_SUPPORT is present. Obviously something is up, so > I'll have a think about it. > > Thanks, > Ian Sorry to say I couldn't repro on Debian: $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf NO_LIBELF=1 NO_LIBTRACEEVENT=1 make: Entering directory '/home/irogers/kernel.org/tools/perf' BUILD: Doing 'make -j16' parallel build Warning: Kernel ABI header at 'tools/include/uapi/linux/in.h' differs from latest version at 'include/uapi/linux/in.h' diff -u tools/include/uapi/linux/in.h include/uapi/linux/in.h Warning: Kernel ABI header at 'tools/arch/x86/include/asm/cpufeatures.h' differs from latest version at 'arch/x86/include/asm/cpufeatures.h' diff -u tools/arch/x86/include/asm/cpufeatures.h arch/x86/include/asm/cpufeatures.h Warning: Kernel ABI header at 'tools/arch/arm64/include/uapi/asm/perf_regs.h' differs from latest version at 'arch/arm64/include/uapi/asm/perf_regs.h' diff -u tools/arch/arm64/include/uapi/asm/perf_regs.h arch/arm64/include/uapi/asm/perf_regs.h Warning: Kernel ABI header at 'tools/include/linux/coresight-pmu.h' differs from latest version at 'include/linux/coresight-pmu.h' diff -u tools/include/linux/coresight-pmu.h include/linux/coresight-pmu.h Makefile.config:588: No sys/sdt.h found, no SDT events are defined, please install systemtap-sdt-devel or systemtap-sdt-dev Makefile.config:679: Disabling post unwind, no support found. Makefile.config:747: No libcrypto.h found, disables jitted code injection, please install openssl-devel or libssl-dev Makefile.config:759: slang not found, disables TUI support. Please install slang-devel, libslang-devor libslang2-dev Makefile.config:806: Missing perl devel files. Disabling perl scripting support, please install perl-ExtUtils-Embed/libperl-dev Makefile.config:846: No 'Python.h' was found: disables Python support - please install python-devel/python-dev Makefile.config:951: No liblzma found, disables xz kernel module decompression, please install xz-devel/liblzma-dev Makefile.config:964: No libzstd found, disables trace compression, please install libzstd-dev[el] and/or set LIBZSTD_DIR Makefile.config:975: No libcap found, disables capability support, please install libcap-devel/libcap-dev Makefile.config:988: No numa.h found, disables 'perf bench numa mem' benchmark, please install numactl-devel/libnuma-devel/libnuma-dev Makefile.config:1047: No libbabeltrace found, disables 'perf data' CTF format support, please install libbabeltrace-dev[el]/libbabeltrace-ctf-dev Makefile.config:1138: libpfm4 not found, disables libpfm4 support. Please install libpfm4-dev Auto-detecting system features: ... dwarf: [ OFF ] ... dwarf_getlocations: [ OFF ] ... glibc: [ on ] ... libbfd: [ OFF ] ... libbfd-buildid: [ OFF ] ... libcap: [ OFF ] ... libelf: [ OFF ] ... libnuma: [ OFF ] ... numa_num_possible_cpus: [ OFF ] ... libperl: [ OFF ] ... libpython: [ OFF ] ... libcrypto: [ OFF ] ... libunwind: [ OFF ] ... libdw-dwarf-unwind: [ OFF ] ... zlib: [ OFF ] ... lzma: [ OFF ] ... get_cpuid: [ OFF ] ... bpf: [ on ] ... libaio: [ on ] ... libzstd: [ OFF ] INSTALL libsubcmd_headers INSTALL libsymbol_headers INSTALL libapi_headers INSTALL libperf_headers FLEX util/parse-events-flex.c FLEX util/pmu-flex.c CC util/pmu-bison.o FLEX util/expr-flex.c CC util/expr-bison.o CXX util/demangle-cxx.o CC util/demangle-ocaml.o CC util/demangle-java.o CC util/demangle-rust.o CC util/perf-hooks.o CC util/parse-events.o CC util/parse-events-flex.o CC util/pmu.o CC util/pmu-flex.o CC util/expr-flex.o CC util/expr.o LD util/perf-in.o LD perf-in.o LINK perf make: Leaving directory '.../tools/perf' Thanks, Ian > > Thanks > > James > > > > > + 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) {
On 30/03/2023 20:03, Ian Rogers wrote: > On Thu, Mar 30, 2023 at 9:50 AM Ian Rogers <irogers@google.com> wrote: >> >> On Thu, Mar 30, 2023 at 7:08 AM James Clark <james.clark@arm.com> wrote: >>> >>> >>> >>> On 11/03/2023 06:57, Ian Rogers wrote: >>>> 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++ >>> >>> Hi Ian, >>> >>> I think cross compilation for arm on x86 isn't working after this change >>> (at least on Ubuntu). >>> >>> Even with all of the arm64 libstdc++ stuff installed, you can only link >>> to it using g++, but the perf build tries to link to it using gcc. Not >>> sure it's some quirk with the search paths on Ubuntu or something else: >>> >>> $ aarch64-linux-gnu-gcc -lstdc++ >>> >>> /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux- >>> gnu/bin/ld: cannot find -lstdc++: No such file or directory >>> collect2: error: ld returned 1 exit status >>> >>> g++ gets further: >>> >>> $ aarch64-linux-gnu-g++ -lstdc++ >>> >>> ... >>> (.text+0x20): undefined reference to `main' >>> collect2: error: ld returned 1 exit status >>> >>> At the end of the perf build it looks like something similar is >>> happening (with all the non interesting bits deleted): >>> >>> $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf >>> NO_BPF_SKEL=1 V=1 >>> >>> aarch64-linux-gnu-gcc ... -o perf >>> >>> /usr/lib/gcc-cross/aarch64-linux-gnu/12/../../../../aarch64-linux-gnu/bin/ld: >>> cannot find -lstdc++: No such file or directory >>> >>> If I build with NO_DEMANGLE=1 then the build works, but I think it would >>> at be best to autodetect rather than have to do this. Or maybe even link >>> at the end with g++ if we're going to use libstdc++? >> >> Hi James, >> >> sorry for the problems you are having, I'll see if I can get a repo. I >> did add a feature test with this change in the same set: >> https://lore.kernel.org/lkml/20230311065753.3012826-9-irogers@google.com/ >> So it should be feature testing and only enabling when >> HAVE_CXA_DEMANGLE_SUPPORT is present. Obviously something is up, so >> I'll have a think about it. >> >> Thanks, >> Ian > > Sorry to say I couldn't repro on Debian: > > $ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -C tools/perf > NO_LIBELF=1 NO_LIBTRACEEVENT=1 If you have NO_LIBELF=1 it sets NO_DEMANGLE=1 automatically so it skips the c++ linking step. If you run with V=1 you can see that it doesn't have -lstdc++ on the last link line. But, having said that, I went to make a full reproducer in docker but it actually worked. Turns out that the issue was on my end. I'd used update-alternatives for a different compiler version and looks like I had half of one gcc and half of another g++. Completely my fault, sorry for the noise! James
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) {