1 //===-- Mangled.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Mangled.h"
13 #include "lldb/Host/windows/windows.h"
16 #pragma comment(lib, "dbghelp.lib")
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Logging.h"
22 #include "lldb/Utility/RegularExpression.h"
23 #include "lldb/Utility/Stream.h"
24 #include "lldb/Utility/Timer.h"
25 #include "lldb/lldb-enumerations.h"
27 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
28 #include "Plugins/Language/ObjC/ObjCLanguage.h"
30 #include "llvm/ADT/StringRef.h"
31 #include "llvm/Demangle/Demangle.h"
32 #include "llvm/Support/Compiler.h"
40 using namespace lldb_private;
43 static DWORD safeUndecorateName(const char *Mangled, char *Demangled,
44 DWORD DemangledLength) {
46 std::lock_guard<std::mutex> Lock(M);
47 return ::UnDecorateSymbolName(
48 Mangled, Demangled, DemangledLength,
49 UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected
51 UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall,
53 UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications
54 UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc
56 UNDNAME_NO_MS_KEYWORDS // Strip all MS extension keywords
61 static inline Mangled::ManglingScheme cstring_mangling_scheme(const char *s) {
64 return Mangled::eManglingSchemeMSVC;
65 if (s[0] == '_' && s[1] == 'Z')
66 return Mangled::eManglingSchemeItanium;
68 return Mangled::eManglingSchemeNone;
71 static inline bool cstring_is_mangled(const char *s) {
72 return cstring_mangling_scheme(s) != Mangled::eManglingSchemeNone;
75 static const ConstString &
76 get_demangled_name_without_arguments(ConstString mangled,
77 ConstString demangled) {
78 // This pair is <mangled name, demangled name without function arguments>
79 static std::pair<ConstString, ConstString>
80 g_most_recent_mangled_to_name_sans_args;
82 // Need to have the mangled & demangled names we're currently examining as
83 // statics so we can return a const ref to them at the end of the func if we
84 // don't have anything better.
85 static ConstString g_last_mangled;
86 static ConstString g_last_demangled;
88 if (mangled && g_most_recent_mangled_to_name_sans_args.first == mangled) {
89 return g_most_recent_mangled_to_name_sans_args.second;
92 g_last_demangled = demangled;
93 g_last_mangled = mangled;
95 const char *mangled_name_cstr = mangled.GetCString();
97 if (demangled && mangled_name_cstr && mangled_name_cstr[0]) {
98 if (mangled_name_cstr[0] == '_' && mangled_name_cstr[1] == 'Z' &&
99 (mangled_name_cstr[2] != 'T' && // avoid virtual table, VTT structure,
100 // typeinfo structure, and typeinfo
102 mangled_name_cstr[2] != 'G' && // avoid guard variables
103 mangled_name_cstr[2] != 'Z')) // named local entities (if we eventually
104 // handle eSymbolTypeData, we will want
107 CPlusPlusLanguage::MethodName cxx_method(demangled);
108 if (!cxx_method.GetBasename().empty()) {
109 std::string shortname;
110 if (!cxx_method.GetContext().empty())
111 shortname = cxx_method.GetContext().str() + "::";
112 shortname += cxx_method.GetBasename().str();
113 ConstString result(shortname.c_str());
114 g_most_recent_mangled_to_name_sans_args.first = mangled;
115 g_most_recent_mangled_to_name_sans_args.second = result;
116 return g_most_recent_mangled_to_name_sans_args.second;
122 return g_last_demangled;
123 return g_last_mangled;
127 //----------------------------------------------------------------------
128 // Default constructor
129 //----------------------------------------------------------------------
130 Mangled::Mangled() : m_mangled(), m_demangled() {}
132 //----------------------------------------------------------------------
133 // Constructor with an optional string and a boolean indicating if it is the
135 //----------------------------------------------------------------------
136 Mangled::Mangled(const ConstString &s, bool mangled)
137 : m_mangled(), m_demangled() {
139 SetValue(s, mangled);
142 Mangled::Mangled(llvm::StringRef name, bool is_mangled) {
144 SetValue(ConstString(name), is_mangled);
147 Mangled::Mangled(const ConstString &s) : m_mangled(), m_demangled() {
152 Mangled::Mangled(llvm::StringRef name) {
154 SetValue(ConstString(name));
157 //----------------------------------------------------------------------
159 //----------------------------------------------------------------------
160 Mangled::~Mangled() {}
162 //----------------------------------------------------------------------
163 // Convert to pointer operator. This allows code to check any Mangled objects
164 // to see if they contain anything valid using code such as:
166 // Mangled mangled(...);
169 //----------------------------------------------------------------------
170 Mangled::operator void *() const {
171 return (m_mangled) ? const_cast<Mangled *>(this) : NULL;
174 //----------------------------------------------------------------------
175 // Logical NOT operator. This allows code to check any Mangled objects to see
176 // if they are invalid using code such as:
178 // Mangled mangled(...);
181 //----------------------------------------------------------------------
182 bool Mangled::operator!() const { return !m_mangled; }
184 //----------------------------------------------------------------------
185 // Clear the mangled and demangled values.
186 //----------------------------------------------------------------------
187 void Mangled::Clear() {
192 //----------------------------------------------------------------------
193 // Compare the string values.
194 //----------------------------------------------------------------------
195 int Mangled::Compare(const Mangled &a, const Mangled &b) {
196 return ConstString::Compare(
197 a.GetName(lldb::eLanguageTypeUnknown, ePreferMangled),
198 a.GetName(lldb::eLanguageTypeUnknown, ePreferMangled));
201 //----------------------------------------------------------------------
202 // Set the string value in this objects. If "mangled" is true, then the mangled
203 // named is set with the new value in "s", else the demangled name is set.
204 //----------------------------------------------------------------------
205 void Mangled::SetValue(const ConstString &s, bool mangled) {
220 void Mangled::SetValue(const ConstString &name) {
222 if (cstring_is_mangled(name.GetCString())) {
235 //----------------------------------------------------------------------
236 // Generate the demangled name on demand using this accessor. Code in this
237 // class will need to use this accessor if it wishes to decode the demangled
238 // name. The result is cached and will be kept until a new string value is
239 // supplied to this object, or until the end of the object's lifetime.
240 //----------------------------------------------------------------------
242 Mangled::GetDemangledName(lldb::LanguageType language) const {
243 // Check to make sure we have a valid mangled name and that we haven't
244 // already decoded our mangled name.
245 if (m_mangled && !m_demangled) {
246 // We need to generate and cache the demangled name.
247 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
248 Timer scoped_timer(func_cat, "Mangled::GetDemangledName (m_mangled = %s)",
249 m_mangled.GetCString());
251 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE);
253 // Don't bother running anything that isn't mangled
254 const char *mangled_name = m_mangled.GetCString();
255 ManglingScheme mangling_scheme{cstring_mangling_scheme(mangled_name)};
256 if (mangling_scheme != eManglingSchemeNone &&
257 !m_mangled.GetMangledCounterpart(m_demangled)) {
258 // We didn't already mangle this name, demangle it and if all goes well
259 // add it to our map.
260 char *demangled_name = nullptr;
261 switch (mangling_scheme) {
262 case eManglingSchemeMSVC: {
263 #if defined(_MSC_VER)
265 log->Printf("demangle msvc: %s", mangled_name);
266 const size_t demangled_length = 2048;
267 demangled_name = static_cast<char *>(::malloc(demangled_length));
268 ::ZeroMemory(demangled_name, demangled_length);
270 safeUndecorateName(mangled_name, demangled_name, demangled_length);
272 if (demangled_name && demangled_name[0])
273 log->Printf("demangled msvc: %s -> \"%s\"", mangled_name,
276 log->Printf("demangled msvc: %s -> error: 0x%lu", mangled_name,
281 free(demangled_name);
282 demangled_name = nullptr;
287 case eManglingSchemeItanium: {
288 llvm::ItaniumPartialDemangler IPD;
289 bool demangle_err = IPD.partialDemangle(mangled_name);
291 // Default buffer and size (realloc is used in case it's too small).
292 size_t demangled_size = 80;
293 demangled_name = static_cast<char *>(::malloc(demangled_size));
294 demangled_name = IPD.finishDemangle(demangled_name, &demangled_size);
299 log->Printf("demangled itanium: %s -> \"%s\"", mangled_name,
302 log->Printf("demangled itanium: %s -> error: failed to demangle",
307 case eManglingSchemeNone:
310 if (demangled_name) {
311 m_demangled.SetCStringWithMangledCounterpart(demangled_name, m_mangled);
312 free(demangled_name);
316 // Set the demangled string to the empty string to indicate we tried to
317 // parse it once and failed.
318 m_demangled.SetCString("");
326 Mangled::GetDisplayDemangledName(lldb::LanguageType language) const {
327 return GetDemangledName(language);
330 bool Mangled::NameMatches(const RegularExpression ®ex,
331 lldb::LanguageType language) const {
332 if (m_mangled && regex.Execute(m_mangled.AsCString()))
335 ConstString demangled = GetDemangledName(language);
336 if (demangled && regex.Execute(demangled.AsCString()))
341 //----------------------------------------------------------------------
342 // Get the demangled name if there is one, else return the mangled name.
343 //----------------------------------------------------------------------
344 ConstString Mangled::GetName(lldb::LanguageType language,
345 Mangled::NamePreference preference) const {
346 if (preference == ePreferMangled && m_mangled)
349 ConstString demangled = GetDemangledName(language);
351 if (preference == ePreferDemangledWithoutArguments) {
352 return get_demangled_name_without_arguments(m_mangled, demangled);
354 if (preference == ePreferDemangled) {
355 // Call the accessor to make sure we get a demangled name in case it hasn't
356 // been demangled yet...
364 //----------------------------------------------------------------------
365 // Dump a Mangled object to stream "s". We don't force our demangled name to be
366 // computed currently (we don't use the accessor).
367 //----------------------------------------------------------------------
368 void Mangled::Dump(Stream *s) const {
370 *s << ", mangled = " << m_mangled;
373 const char *demangled = m_demangled.AsCString();
374 s->Printf(", demangled = %s", demangled[0] ? demangled : "<error>");
378 //----------------------------------------------------------------------
379 // Dumps a debug version of this string with extra object and state information
381 //----------------------------------------------------------------------
382 void Mangled::DumpDebug(Stream *s) const {
383 s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2),
384 static_cast<const void *>(this));
385 m_mangled.DumpDebug(s);
386 s->Printf(", demangled = ");
387 m_demangled.DumpDebug(s);
390 //----------------------------------------------------------------------
391 // Return the size in byte that this object takes in memory. The size includes
392 // the size of the objects it owns, and not the strings that it references
393 // because they are shared strings.
394 //----------------------------------------------------------------------
395 size_t Mangled::MemorySize() const {
396 return m_mangled.MemorySize() + m_demangled.MemorySize();
399 //----------------------------------------------------------------------
400 // We "guess" the language because we can't determine a symbol's language from
401 // it's name. For example, a Pascal symbol can be mangled using the C++
402 // Itanium scheme, and defined in a compilation unit within the same module as
403 // other C++ units. In addition, different targets could have different ways
404 // of mangling names from a given language, likewise the compilation units
405 // within those targets.
406 //----------------------------------------------------------------------
407 lldb::LanguageType Mangled::GuessLanguage() const {
408 ConstString mangled = GetMangledName();
410 if (GetDemangledName(lldb::eLanguageTypeUnknown)) {
411 const char *mangled_name = mangled.GetCString();
412 if (CPlusPlusLanguage::IsCPPMangledName(mangled_name))
413 return lldb::eLanguageTypeC_plus_plus;
414 else if (ObjCLanguage::IsPossibleObjCMethodName(mangled_name))
415 return lldb::eLanguageTypeObjC;
418 // ObjC names aren't really mangled, so they won't necessarily be in the
419 // mangled name slot.
420 ConstString demangled_name = GetDemangledName(lldb::eLanguageTypeUnknown);
422 && ObjCLanguage::IsPossibleObjCMethodName(demangled_name.GetCString()))
423 return lldb::eLanguageTypeObjC;
426 return lldb::eLanguageTypeUnknown;
429 //----------------------------------------------------------------------
430 // Dump OBJ to the supplied stream S.
431 //----------------------------------------------------------------------
432 Stream &operator<<(Stream &s, const Mangled &obj) {
433 if (obj.GetMangledName())
434 s << "mangled = '" << obj.GetMangledName() << "'";
436 const ConstString &demangled =
437 obj.GetDemangledName(lldb::eLanguageTypeUnknown);
439 s << ", demangled = '" << demangled << '\'';
441 s << ", demangled = <error>";