xref: /aosp_15_r20/external/abseil-cpp/absl/debugging/internal/demangle.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
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 
15 #ifndef ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
16 #define ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
17 
18 #include <string>
19 #include "absl/base/config.h"
20 
21 namespace absl {
22 ABSL_NAMESPACE_BEGIN
23 namespace debugging_internal {
24 
25 // Demangle `mangled`.  On success, return true and write the
26 // demangled symbol name to `out`.  Otherwise, return false.
27 // `out` is modified even if demangling is unsuccessful.
28 //
29 // This function provides an alternative to libstdc++'s abi::__cxa_demangle,
30 // which is not async signal safe (it uses malloc internally).  It's intended to
31 // be used in async signal handlers to symbolize stack traces.
32 //
33 // Note that this demangler doesn't support full demangling.  More
34 // specifically, it doesn't print types of function parameters and
35 // types of template arguments.  It just skips them.  However, it's
36 // still very useful to extract basic information such as class,
37 // function, constructor, destructor, and operator names.
38 //
39 // See the implementation note in demangle.cc if you are interested.
40 //
41 // Example:
42 //
43 // | Mangled Name  | Demangle    | DemangleString
44 // |---------------|-------------|-----------------------
45 // | _Z1fv         | f()         | f()
46 // | _Z1fi         | f()         | f(int)
47 // | _Z3foo3bar    | foo()       | foo(bar)
48 // | _Z1fIiEvi     | f<>()       | void f<int>(int)
49 // | _ZN1N1fE      | N::f        | N::f
50 // | _ZN3Foo3BarEv | Foo::Bar()  | Foo::Bar()
51 // | _Zrm1XS_"     | operator%() | operator%(X, X)
52 // | _ZN3FooC1Ev   | Foo::Foo()  | Foo::Foo()
53 // | _Z1fSs        | f()         | f(std::basic_string<char,
54 // |               |             |   std::char_traits<char>,
55 // |               |             |   std::allocator<char> >)
56 //
57 // See the unit test for more examples.
58 //
59 // Demangle also recognizes Rust mangled names by delegating the parsing of
60 // anything that starts with _R to DemangleRustSymbolEncoding (demangle_rust.h).
61 //
62 // Note: we might want to write demanglers for ABIs other than Itanium
63 // C++ ABI in the future.
64 bool Demangle(const char* mangled, char* out, size_t out_size);
65 
66 // A wrapper around `abi::__cxa_demangle()`.  On success, returns the demangled
67 // name.  On failure, returns the input mangled name.
68 //
69 // This function is not async-signal-safe.
70 std::string DemangleString(const char* mangled);
71 
72 }  // namespace debugging_internal
73 ABSL_NAMESPACE_END
74 }  // namespace absl
75 
76 #endif  // ABSL_DEBUGGING_INTERNAL_DEMANGLE_H_
77