demangle.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
  15. #define ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
  16. #include <string>
  17. #include "absl/base/config.h"
  18. namespace absl {
  19. ABSL_NAMESPACE_BEGIN
  20. namespace debugging_internal {
  21. // Demangle `mangled`. On success, return true and write the
  22. // demangled symbol name to `out`. Otherwise, return false.
  23. // `out` is modified even if demangling is unsuccessful.
  24. //
  25. // This function provides an alternative to libstdc++'s abi::__cxa_demangle,
  26. // which is not async signal safe (it uses malloc internally). It's intended to
  27. // be used in async signal handlers to symbolize stack traces.
  28. //
  29. // Note that this demangler doesn't support full demangling. More
  30. // specifically, it doesn't print types of function parameters and
  31. // types of template arguments. It just skips them. However, it's
  32. // still very useful to extract basic information such as class,
  33. // function, constructor, destructor, and operator names.
  34. //
  35. // See the implementation note in demangle.cc if you are interested.
  36. //
  37. // Example:
  38. //
  39. // | Mangled Name | Demangle | DemangleString
  40. // |---------------|-------------|-----------------------
  41. // | _Z1fv | f() | f()
  42. // | _Z1fi | f() | f(int)
  43. // | _Z3foo3bar | foo() | foo(bar)
  44. // | _Z1fIiEvi | f<>() | void f<int>(int)
  45. // | _ZN1N1fE | N::f | N::f
  46. // | _ZN3Foo3BarEv | Foo::Bar() | Foo::Bar()
  47. // | _Zrm1XS_" | operator%() | operator%(X, X)
  48. // | _ZN3FooC1Ev | Foo::Foo() | Foo::Foo()
  49. // | _Z1fSs | f() | f(std::basic_string<char,
  50. // | | | std::char_traits<char>,
  51. // | | | std::allocator<char> >)
  52. //
  53. // See the unit test for more examples.
  54. //
  55. // Demangle also recognizes Rust mangled names by delegating the parsing of
  56. // anything that starts with _R to DemangleRustSymbolEncoding (demangle_rust.h).
  57. //
  58. // Note: we might want to write demanglers for ABIs other than Itanium
  59. // C++ ABI in the future.
  60. bool Demangle(const char* mangled, char* out, size_t out_size);
  61. // A wrapper around `abi::__cxa_demangle()`. On success, returns the demangled
  62. // name. On failure, returns the input mangled name.
  63. //
  64. // This function is not async-signal-safe.
  65. std::string DemangleString(const char* mangled);
  66. } // namespace debugging_internal
  67. ABSL_NAMESPACE_END
  68. } // namespace absl
  69. #endif // ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_