demangle_symbol.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015 Klemens Morgenstern
  2. //
  3. // This file provides a demangling for function names, i.e. entry points of a dll.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. #ifndef BOOST_DLL_DEMANGLE_SYMBOL_HPP_
  8. #define BOOST_DLL_DEMANGLE_SYMBOL_HPP_
  9. #include <boost/config.hpp>
  10. #include <string>
  11. #include <algorithm>
  12. #if defined(BOOST_MSVC) || defined(BOOST_MSVC_FULL_VER)
  13. namespace boost
  14. {
  15. namespace dll
  16. {
  17. namespace detail
  18. {
  19. typedef void * (__cdecl * allocation_function)(std::size_t);
  20. typedef void (__cdecl * free_function)(void *);
  21. extern "C" char* __unDName( char* outputString,
  22. const char* name,
  23. int maxStringLength, // Note, COMMA is leading following optional arguments
  24. allocation_function pAlloc,
  25. free_function pFree,
  26. unsigned short disableFlags
  27. );
  28. inline std::string demangle_symbol(const char *mangled_name)
  29. {
  30. allocation_function alloc = [](std::size_t size){return static_cast<void*>(new char[size]);};
  31. free_function free_f = [](void* p){delete [] static_cast<char*>(p);};
  32. std::unique_ptr<char> name { __unDName(
  33. nullptr,
  34. mangled_name,
  35. 0,
  36. alloc,
  37. free_f,
  38. static_cast<unsigned short>(0))};
  39. return std::string(name.get());
  40. }
  41. inline std::string demangle_symbol(const std::string& mangled_name)
  42. {
  43. return demangle_symbol(mangled_name.c_str());
  44. }
  45. }}}
  46. #else
  47. #include <boost/core/demangle.hpp>
  48. namespace boost
  49. {
  50. namespace dll
  51. {
  52. namespace detail
  53. {
  54. inline std::string demangle_symbol(const char *mangled_name)
  55. {
  56. if (*mangled_name == '_')
  57. {
  58. //because it start's with an underline _
  59. auto dm = boost::core::demangle(mangled_name);
  60. if (!dm.empty())
  61. return dm;
  62. else
  63. return (mangled_name);
  64. }
  65. //could not demangled
  66. return "";
  67. }
  68. //for my personal convinience
  69. inline std::string demangle_symbol(const std::string& mangled_name)
  70. {
  71. return demangle_symbol(mangled_name.c_str());
  72. }
  73. }
  74. namespace experimental
  75. {
  76. using ::boost::dll::detail::demangle_symbol;
  77. }
  78. }}
  79. #endif
  80. #endif /* BOOST_DEMANGLE_HPP_ */