Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2407300rwl; Thu, 30 Mar 2023 09:52:23 -0700 (PDT) X-Google-Smtp-Source: AKy350bMwDaq6d3gZyTHmZgs8wVtJbFBNc1lw4t8z4xgsEzsjRq+BqJvHE/iGm0fHCrVsgvpg8Ei X-Received: by 2002:a05:6402:1d48:b0:4bb:afe3:e0a with SMTP id dz8-20020a0564021d4800b004bbafe30e0amr3804133edb.3.1680195142810; Thu, 30 Mar 2023 09:52:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680195142; cv=none; d=google.com; s=arc-20160816; b=rSI0hv3C34kcAHH/gGEKJr0oaIN0S4/eLyeEithPpTKn6KUIm4AouKc6nLNblolO2S ER98+FNHxbhg1a5rhChQK75gkLZuuL7AoFYRUczGYUdEZ6E8Wa8jAChi4QwgcJmONzsl Sx1lEt1jdlIxmLEbwW79UiZeaLeijaXyuuAf7a9fKNLxNhq8dBnktCKNogaHZmWC4bEi KAqggl/S8QVasRfx8ESt/qbrkZ6N266ktBYpHtv4xReKucjCKU+hbapUpLlsVgdjPaLk R5y3adYqmKXJEmbCb2IlhSuKpJ17XZwW+pMMe8hhsEHmEXvsYOYKzsXqLVwJ8pFTrHFJ hi6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=p/FCVT8pjnQlaEXCyw0RIWbqk31GFiyTZmtv3Hci8Xw=; b=SMwT2rs4Mi3t45AkBFfU8KgqNmPm3FerD+k8MMlMwHDBJ4ii1tolebeoOqYCVKhqM0 Fe5RYS1xUK7Xe4G09kA6cHVtmzRwgHxZ6BUiaz4W/1eQPBP9ggQgJvYFt/9y0V/6Ofzp 3p0l0I6Tm2GZz2h1ptCdVlSivHFtUWg9qAY15+RDgjPbF+6dUAEigYw/Va9CCJX2nmBc t0M0F/X4JYLcJ2ukh6bmYtKmc1UHZVMrIysSRfgxT4jY6r61aMI7qSwlm/j7998eMZOb eAQRh5pHwIaCUQ+AvMTZkE4nb7uVK78q4/FygIt0PKztfqKoBR6WFy5EASbiSRo0yaQF 6cQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=mdd3A89x; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c9-20020aa7d609000000b004fc24481138si165390edr.340.2023.03.30.09.51.56; Thu, 30 Mar 2023 09:52:22 -0700 (PDT) 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=mdd3A89x; 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 S231771AbjC3QvS (ORCPT + 99 others); Thu, 30 Mar 2023 12:51:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232135AbjC3Quy (ORCPT ); Thu, 30 Mar 2023 12:50:54 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1467FCDE1 for ; Thu, 30 Mar 2023 09:50:22 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id d75a77b69052e-3e390e23f83so529571cf.1 for ; Thu, 30 Mar 2023 09:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680195021; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=p/FCVT8pjnQlaEXCyw0RIWbqk31GFiyTZmtv3Hci8Xw=; b=mdd3A89xFS/UXD4CEvKqBUpBNPb3hEulmratv1DXHQuP2vWGlwzaSih3DwD4MxsYS2 t0nUfVFsxPOpt63YihPpB+KmM5Excd9HxuHNvGwVFXmEzQWIAgcbQwa1YUals8RBvFMQ +y8aDEioiEVrw1+OPSVovqC26tGXE6EfhYF3vJXeJ6ZB8fL8VT3ZZldxhtlyFa2JtJVA jde4IM8125i1twncWTjmcXv9hppjqRgCe/D+KWSuZM0qDeZ/NCZfR0SF8xUxRGnD0Mqg C2bsmPgZDBZVkf0JhnhNmgmS33OQr/wxzwFJMMT+V9km3t8P/lvNYSZiMtrMaqs4DMg2 JKLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680195021; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p/FCVT8pjnQlaEXCyw0RIWbqk31GFiyTZmtv3Hci8Xw=; b=aFcVcnko4xSE0HvsR6keMrLNXzZW2QzCcaub0U+bE4odG6sO8t8QzxamIQXESFIkRQ lL3RblkHN6FR7WQ5heZ9kMhl6zl33RJTQ3VjxrlXZX5PO9NrMdOOZek4+IrSPcyzXmik ECvp1XobgtxVle7UCAj0mxHxqK+F64d5liWTcCnHr7dC/tdsV2w4/uGTMZ8CLougbmT9 i49qc6lPyvwyI0De8XN1q+c49ySoYF2k1v5zUty5g9q3+trV5ugB8rIDclH47QCjxhq9 lFYtMuJ721NMP9R5Ip6fVMZ/vqT04LYiHpQXsM21PuuOgxdbyfMYdbF2Ucxmtu9heEPE v/4w== X-Gm-Message-State: AAQBX9cSEj6qUtUI+anl0Cv4udaXv4gO1uXbEEnoUuCL87LTeemzTfgT tYvCx+W1ibCsMsU7D6EEOQ/GuHI7PX+Tv/BSXm+k1g== X-Received: by 2002:a05:622a:1a86:b0:3bd:17d6:e04c with SMTP id s6-20020a05622a1a8600b003bd17d6e04cmr400268qtc.2.1680195020817; Thu, 30 Mar 2023 09:50:20 -0700 (PDT) MIME-Version: 1.0 References: <20230311065753.3012826-1-irogers@google.com> <20230311065753.3012826-10-irogers@google.com> In-Reply-To: From: Ian Rogers Date: Thu, 30 Mar 2023 09:50:06 -0700 Message-ID: Subject: Re: [PATCH v1 09/13] perf symbol: Add abi::__cxa_demangle C++ demangling support To: James Clark Cc: Arnaldo Carvalho de Melo , Stephane Eranian , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Nathan Chancellor , Nick Desaulniers , Tom Rix , Roberto Sassu , Quentin Monnet , Andres Freund , Tiezhu Yang , Pavithra Gurushankar , Yang Jihong , Adrian Hunter , Leo Yan , =?UTF-8?Q?Martin_Li=C5=A1ka?= , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, Peter Zijlstra , Ingo Molnar Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable 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: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Mar 30, 2023 at 7:08=E2=80=AFAM James Clark w= rote: > > > > 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 > > --- > > 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 +=3D -DHAVE_LIBBFD_SUPPORT > > + CXXFLAGS +=3D -DHAVE_LIBBFD_SUPPORT > > ifeq ($(feature-libbfd-buildid), 1) > > CFLAGS +=3D -DHAVE_LIBBFD_BUILDID_SUPPORT > > else > > @@ -913,26 +914,24 @@ ifdef BUILD_NONDISTRO > > endif > > endif > > > > -ifdef NO_DEMANGLE > > - CFLAGS +=3D -DNO_DEMANGLE > > -else > > +ifndef NO_DEMANGLE > > + $(call feature_check,cxa-demangle) > > + ifeq ($(feature-cxa-demangle), 1) > > + EXTLIBS +=3D -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=3Darm64 CROSS_COMPILE=3Daarch64-linux-gnu- -C tools/perf > NO_BPF_SKEL=3D1 V=3D1 > > 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=3D1 then the build works, but I think it woul= d > 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 +=3D -DHAVE_CXA_DEMANGLE_SUPPORT > > + CXXFLAGS +=3D -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 +=3D -liberty > > - else > > - $(call feature_check,cplus-demangle) > > - ifeq ($(feature-cplus-demangle), 1) > > - EXTLIBS +=3D -liberty > > - endif > > endif > > endif > > - endif > > - > > - ifneq ($(filter -liberty,$(EXTLIBS)),) > > - CFLAGS +=3D -DHAVE_CPLUS_DEMANGLE_SUPPORT > > - else > > - CFLAGS +=3D -DNO_DEMANGLE > > + ifneq ($(filter -liberty,$(EXTLIBS)),) > > + CFLAGS +=3D -DHAVE_CPLUS_DEMANGLE_SUPPORT > > + CXXFLAGS +=3D -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) +=3D zstd.o > > > > perf-$(CONFIG_LIBCAP) +=3D cap.o > > > > +perf-y +=3D demangle-cxx.o > > perf-y +=3D demangle-ocaml.o > > perf-y +=3D demangle-java.o > > perf-y +=3D demangle-rust.o > > diff --git a/tools/perf/util/demangle-cxx.cpp b/tools/perf/util/demangl= e-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 > > +#include > > +#include > > + > > +#ifdef HAVE_LIBBFD_SUPPORT > > +#define PACKAGE 'perf' > > +#include > > +#endif > > + > > +#ifdef HAVE_CXA_DEMANGLE_SUPPORT > > +#include > > +#endif > > + > > +#if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPOR= T) > > +#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 =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_A= NSI : 0); > > + > > + return bfd_demangle(NULL, str, flags); > > +#elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT) > > + int flags =3D (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_A= NSI : 0); > > + > > + return cplus_demangle(str, flags); > > +#elif defined(HAVE_CXA_DEMANGLE_SUPPORT) > > + size_t len =3D strlen(str); > > + char *output =3D (char*)malloc(len); > > + int status; > > + > > + output =3D 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 > > #include > > > > +#ifdef HAVE_LIBBFD_SUPPORT > > +#define PACKAGE 'perf' > > +#include > > +#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, et= c */ > > -#endif > > - > > -#ifdef HAVE_LIBBFD_SUPPORT > > -#define PACKAGE 'perf' > > -#include > > -#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 *el= f_name) > > { > > - int demangle_flags =3D verbose > 0 ? (DMGL_PARAMS | DMGL_ANSI) : = DMGL_NO_OPTS; > > char *demangled =3D NULL; > > > > /* > > @@ -306,7 +283,7 @@ static char *demangle_sym(struct dso *dso, int kmod= ule, const char *elf_name) > > if (!want_demangle(dso->kernel || kmodule)) > > return demangled; > > > > - demangled =3D bfd_demangle(NULL, elf_name, demangle_flags); > > + demangled =3D cxx_demangle_sym(elf_name, verbose > 0, verbose > 0= ); > > if (demangled =3D=3D NULL) { > > demangled =3D ocaml_demangle_sym(elf_name); > > if (demangled =3D=3D NULL) {