demangle-cxx.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "demangle-cxx.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <linux/compiler.h>
  6. #ifdef HAVE_LIBBFD_SUPPORT
  7. #define PACKAGE 'perf'
  8. #include <bfd.h>
  9. #endif
  10. #ifdef HAVE_CXA_DEMANGLE_SUPPORT
  11. #include <cxxabi.h>
  12. #endif
  13. #if defined(HAVE_LIBBFD_SUPPORT) || defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
  14. #ifndef DMGL_PARAMS
  15. #define DMGL_PARAMS (1 << 0) /* Include function args */
  16. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  17. #endif
  18. #endif
  19. /*
  20. * Demangle C++ function signature
  21. *
  22. * Note: caller is responsible for freeing demangled string
  23. */
  24. extern "C"
  25. char *cxx_demangle_sym(const char *str, bool params __maybe_unused,
  26. bool modifiers __maybe_unused)
  27. {
  28. #ifdef HAVE_LIBBFD_SUPPORT
  29. int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
  30. return bfd_demangle(NULL, str, flags);
  31. #elif defined(HAVE_CPLUS_DEMANGLE_SUPPORT)
  32. int flags = (params ? DMGL_PARAMS : 0) | (modifiers ? DMGL_ANSI : 0);
  33. return cplus_demangle(str, flags);
  34. #elif defined(HAVE_CXA_DEMANGLE_SUPPORT)
  35. char *output;
  36. int status;
  37. output = abi::__cxa_demangle(str, /*output_buffer=*/NULL, /*length=*/NULL, &status);
  38. return output;
  39. #else
  40. return NULL;
  41. #endif
  42. }