1 //===-- Mangled.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/Mangled.h" 11 12 #if defined(_WIN32) 13 #include "lldb/Host/windows/windows.h" 14 15 #include <dbghelp.h> 16 #pragma comment(lib, "dbghelp.lib") 17 #endif 18 19 #include "lldb/Core/RichManglingContext.h" 20 #include "lldb/Utility/ConstString.h" 21 #include "lldb/Utility/Log.h" 22 #include "lldb/Utility/Logging.h" 23 #include "lldb/Utility/RegularExpression.h" 24 #include "lldb/Utility/Stream.h" 25 #include "lldb/Utility/Timer.h" 26 #include "lldb/lldb-enumerations.h" 27 28 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 29 #include "Plugins/Language/ObjC/ObjCLanguage.h" 30 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/Demangle/Demangle.h" 33 #include "llvm/Support/Compiler.h" 34 35 #include <mutex> 36 #include <string> 37 #include <utility> 38 39 #include <stdlib.h> 40 #include <string.h> 41 using namespace lldb_private; 42 43 #if defined(_MSC_VER) 44 static DWORD safeUndecorateName(const char *Mangled, char *Demangled, 45 DWORD DemangledLength) { 46 static std::mutex M; 47 std::lock_guard<std::mutex> Lock(M); 48 return ::UnDecorateSymbolName( 49 Mangled, Demangled, DemangledLength, 50 UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected 51 // keywords 52 UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, 53 // etc keywords 54 UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications 55 UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc 56 // specifiers 57 UNDNAME_NO_MS_KEYWORDS // Strip all MS extension keywords 58 ); 59 } 60 #endif 61 62 static inline Mangled::ManglingScheme cstring_mangling_scheme(const char *s) { 63 if (s) { 64 if (s[0] == '?') 65 return Mangled::eManglingSchemeMSVC; 66 if (s[0] == '_' && s[1] == 'Z') 67 return Mangled::eManglingSchemeItanium; 68 } 69 return Mangled::eManglingSchemeNone; 70 } 71 72 static inline bool cstring_is_mangled(const char *s) { 73 return cstring_mangling_scheme(s) != Mangled::eManglingSchemeNone; 74 } 75 76 static const ConstString & 77 get_demangled_name_without_arguments(ConstString mangled, 78 ConstString demangled) { 79 // This pair is <mangled name, demangled name without function arguments> 80 static std::pair<ConstString, ConstString> 81 g_most_recent_mangled_to_name_sans_args; 82 83 // Need to have the mangled & demangled names we're currently examining as 84 // statics so we can return a const ref to them at the end of the func if we 85 // don't have anything better. 86 static ConstString g_last_mangled; 87 static ConstString g_last_demangled; 88 89 if (mangled && g_most_recent_mangled_to_name_sans_args.first == mangled) { 90 return g_most_recent_mangled_to_name_sans_args.second; 91 } 92 93 g_last_demangled = demangled; 94 g_last_mangled = mangled; 95 96 const char *mangled_name_cstr = mangled.GetCString(); 97 98 if (demangled && mangled_name_cstr && mangled_name_cstr[0]) { 99 if (mangled_name_cstr[0] == '_' && mangled_name_cstr[1] == 'Z' && 100 (mangled_name_cstr[2] != 'T' && // avoid virtual table, VTT structure, 101 // typeinfo structure, and typeinfo 102 // mangled_name 103 mangled_name_cstr[2] != 'G' && // avoid guard variables 104 mangled_name_cstr[2] != 'Z')) // named local entities (if we eventually 105 // handle eSymbolTypeData, we will want 106 // this back) 107 { 108 CPlusPlusLanguage::MethodName cxx_method(demangled); 109 if (!cxx_method.GetBasename().empty()) { 110 std::string shortname; 111 if (!cxx_method.GetContext().empty()) 112 shortname = cxx_method.GetContext().str() + "::"; 113 shortname += cxx_method.GetBasename().str(); 114 ConstString result(shortname.c_str()); 115 g_most_recent_mangled_to_name_sans_args.first = mangled; 116 g_most_recent_mangled_to_name_sans_args.second = result; 117 return g_most_recent_mangled_to_name_sans_args.second; 118 } 119 } 120 } 121 122 if (demangled) 123 return g_last_demangled; 124 return g_last_mangled; 125 } 126 127 #pragma mark Mangled 128 //---------------------------------------------------------------------- 129 // Default constructor 130 //---------------------------------------------------------------------- 131 Mangled::Mangled() : m_mangled(), m_demangled() {} 132 133 //---------------------------------------------------------------------- 134 // Constructor with an optional string and a boolean indicating if it is the 135 // mangled version. 136 //---------------------------------------------------------------------- 137 Mangled::Mangled(const ConstString &s, bool mangled) 138 : m_mangled(), m_demangled() { 139 if (s) 140 SetValue(s, mangled); 141 } 142 143 Mangled::Mangled(llvm::StringRef name, bool is_mangled) { 144 if (!name.empty()) 145 SetValue(ConstString(name), is_mangled); 146 } 147 148 Mangled::Mangled(const ConstString &s) : m_mangled(), m_demangled() { 149 if (s) 150 SetValue(s); 151 } 152 153 Mangled::Mangled(llvm::StringRef name) { 154 if (!name.empty()) 155 SetValue(ConstString(name)); 156 } 157 158 //---------------------------------------------------------------------- 159 // Destructor 160 //---------------------------------------------------------------------- 161 Mangled::~Mangled() {} 162 163 //---------------------------------------------------------------------- 164 // Convert to pointer operator. This allows code to check any Mangled objects 165 // to see if they contain anything valid using code such as: 166 // 167 // Mangled mangled(...); 168 // if (mangled) 169 // { ... 170 //---------------------------------------------------------------------- 171 Mangled::operator void *() const { 172 return (m_mangled) ? const_cast<Mangled *>(this) : NULL; 173 } 174 175 //---------------------------------------------------------------------- 176 // Logical NOT operator. This allows code to check any Mangled objects to see 177 // if they are invalid using code such as: 178 // 179 // Mangled mangled(...); 180 // if (!file_spec) 181 // { ... 182 //---------------------------------------------------------------------- 183 bool Mangled::operator!() const { return !m_mangled; } 184 185 //---------------------------------------------------------------------- 186 // Clear the mangled and demangled values. 187 //---------------------------------------------------------------------- 188 void Mangled::Clear() { 189 m_mangled.Clear(); 190 m_demangled.Clear(); 191 } 192 193 //---------------------------------------------------------------------- 194 // Compare the string values. 195 //---------------------------------------------------------------------- 196 int Mangled::Compare(const Mangled &a, const Mangled &b) { 197 return ConstString::Compare( 198 a.GetName(lldb::eLanguageTypeUnknown, ePreferMangled), 199 b.GetName(lldb::eLanguageTypeUnknown, ePreferMangled)); 200 } 201 202 //---------------------------------------------------------------------- 203 // Set the string value in this objects. If "mangled" is true, then the mangled 204 // named is set with the new value in "s", else the demangled name is set. 205 //---------------------------------------------------------------------- 206 void Mangled::SetValue(const ConstString &s, bool mangled) { 207 if (s) { 208 if (mangled) { 209 m_demangled.Clear(); 210 m_mangled = s; 211 } else { 212 m_demangled = s; 213 m_mangled.Clear(); 214 } 215 } else { 216 m_demangled.Clear(); 217 m_mangled.Clear(); 218 } 219 } 220 221 void Mangled::SetValue(const ConstString &name) { 222 if (name) { 223 if (cstring_is_mangled(name.GetCString())) { 224 m_demangled.Clear(); 225 m_mangled = name; 226 } else { 227 m_demangled = name; 228 m_mangled.Clear(); 229 } 230 } else { 231 m_demangled.Clear(); 232 m_mangled.Clear(); 233 } 234 } 235 236 //---------------------------------------------------------------------- 237 // Local helpers for different demangling implementations. 238 //---------------------------------------------------------------------- 239 static char *GetMSVCDemangledStr(const char *M) { 240 #if defined(_MSC_VER) 241 const size_t demangled_length = 2048; 242 char *demangled_cstr = static_cast<char *>(::malloc(demangled_length)); 243 ::ZeroMemory(demangled_cstr, demangled_length); 244 DWORD result = safeUndecorateName(M, demangled_cstr, demangled_length); 245 246 if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) { 247 if (demangled_cstr && demangled_cstr[0]) 248 log->Printf("demangled msvc: %s -> \"%s\"", M, demangled_cstr); 249 else 250 log->Printf("demangled msvc: %s -> error: 0x%lu", M, result); 251 } 252 253 if (result != 0) { 254 return demangled_cstr; 255 } else { 256 ::free(demangled_cstr); 257 return nullptr; 258 } 259 #else 260 return nullptr; 261 #endif 262 } 263 264 static char *GetItaniumDemangledStr(const char *M) { 265 char *demangled_cstr = nullptr; 266 267 llvm::ItaniumPartialDemangler ipd; 268 bool err = ipd.partialDemangle(M); 269 if (!err) { 270 // Default buffer and size (will realloc in case it's too small). 271 size_t demangled_size = 80; 272 demangled_cstr = static_cast<char *>(std::malloc(demangled_size)); 273 demangled_cstr = ipd.finishDemangle(demangled_cstr, &demangled_size); 274 275 assert(demangled_cstr && 276 "finishDemangle must always succeed if partialDemangle did"); 277 assert(demangled_cstr[demangled_size - 1] == '\0' && 278 "Expected demangled_size to return length including trailing null"); 279 } 280 281 if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) { 282 if (demangled_cstr) 283 log->Printf("demangled itanium: %s -> \"%s\"", M, demangled_cstr); 284 else 285 log->Printf("demangled itanium: %s -> error: failed to demangle", M); 286 } 287 288 return demangled_cstr; 289 } 290 291 //---------------------------------------------------------------------- 292 // Explicit demangling for scheduled requests during batch processing. This 293 // makes use of ItaniumPartialDemangler's rich demangle info 294 //---------------------------------------------------------------------- 295 bool Mangled::DemangleWithRichManglingInfo( 296 RichManglingContext &context, SkipMangledNameFn *skip_mangled_name) { 297 // We need to generate and cache the demangled name. 298 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 299 Timer scoped_timer(func_cat, 300 "Mangled::DemangleWithRichNameIndexInfo (m_mangled = %s)", 301 m_mangled.GetCString()); 302 303 // Others are not meant to arrive here. ObjC names or C's main() for example 304 // have their names stored in m_demangled, while m_mangled is empty. 305 assert(m_mangled); 306 307 // Check whether or not we are interested in this name at all. 308 ManglingScheme scheme = cstring_mangling_scheme(m_mangled.GetCString()); 309 if (skip_mangled_name && skip_mangled_name(m_mangled.GetStringRef(), scheme)) 310 return false; 311 312 switch (scheme) { 313 case eManglingSchemeNone: 314 // The current mangled_name_filter would allow llvm_unreachable here. 315 return false; 316 317 case eManglingSchemeItanium: 318 // We want the rich mangling info here, so we don't care whether or not 319 // there is a demangled string in the pool already. 320 if (context.FromItaniumName(m_mangled)) { 321 // If we got an info, we have a name. Copy to string pool and connect the 322 // counterparts to accelerate later access in GetDemangledName(). 323 context.ParseFullName(); 324 m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(), 325 m_mangled); 326 return true; 327 } else { 328 m_demangled.SetCString(""); 329 return false; 330 } 331 332 case eManglingSchemeMSVC: { 333 // We have no rich mangling for MSVC-mangled names yet, so first try to 334 // demangle it if necessary. 335 if (!m_demangled && !m_mangled.GetMangledCounterpart(m_demangled)) { 336 if (char *d = GetMSVCDemangledStr(m_mangled.GetCString())) { 337 // If we got an info, we have a name. Copy to string pool and connect 338 // the counterparts to accelerate later access in GetDemangledName(). 339 m_demangled.SetStringWithMangledCounterpart(llvm::StringRef(d), 340 m_mangled); 341 ::free(d); 342 } else { 343 m_demangled.SetCString(""); 344 } 345 } 346 347 if (m_demangled.IsEmpty()) { 348 // Cannot demangle it, so don't try parsing. 349 return false; 350 } else { 351 // Demangled successfully, we can try and parse it with 352 // CPlusPlusLanguage::MethodName. 353 return context.FromCxxMethodName(m_demangled); 354 } 355 } 356 } 357 } 358 359 //---------------------------------------------------------------------- 360 // Generate the demangled name on demand using this accessor. Code in this 361 // class will need to use this accessor if it wishes to decode the demangled 362 // name. The result is cached and will be kept until a new string value is 363 // supplied to this object, or until the end of the object's lifetime. 364 //---------------------------------------------------------------------- 365 const ConstString & 366 Mangled::GetDemangledName(lldb::LanguageType language) const { 367 // Check to make sure we have a valid mangled name and that we haven't 368 // already decoded our mangled name. 369 if (m_mangled && m_demangled.IsNull()) { 370 // We need to generate and cache the demangled name. 371 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 372 Timer scoped_timer(func_cat, "Mangled::GetDemangledName (m_mangled = %s)", 373 m_mangled.GetCString()); 374 375 // Don't bother running anything that isn't mangled 376 const char *mangled_name = m_mangled.GetCString(); 377 ManglingScheme mangling_scheme{cstring_mangling_scheme(mangled_name)}; 378 if (mangling_scheme != eManglingSchemeNone && 379 !m_mangled.GetMangledCounterpart(m_demangled)) { 380 // We didn't already mangle this name, demangle it and if all goes well 381 // add it to our map. 382 char *demangled_name = nullptr; 383 switch (mangling_scheme) { 384 case eManglingSchemeMSVC: 385 demangled_name = GetMSVCDemangledStr(mangled_name); 386 break; 387 case eManglingSchemeItanium: { 388 demangled_name = GetItaniumDemangledStr(mangled_name); 389 break; 390 } 391 case eManglingSchemeNone: 392 llvm_unreachable("eManglingSchemeNone was handled already"); 393 } 394 if (demangled_name) { 395 m_demangled.SetStringWithMangledCounterpart( 396 llvm::StringRef(demangled_name), m_mangled); 397 free(demangled_name); 398 } 399 } 400 if (m_demangled.IsNull()) { 401 // Set the demangled string to the empty string to indicate we tried to 402 // parse it once and failed. 403 m_demangled.SetCString(""); 404 } 405 } 406 407 return m_demangled; 408 } 409 410 ConstString 411 Mangled::GetDisplayDemangledName(lldb::LanguageType language) const { 412 return GetDemangledName(language); 413 } 414 415 bool Mangled::NameMatches(const RegularExpression ®ex, 416 lldb::LanguageType language) const { 417 if (m_mangled && regex.Execute(m_mangled.AsCString())) 418 return true; 419 420 ConstString demangled = GetDemangledName(language); 421 if (demangled && regex.Execute(demangled.AsCString())) 422 return true; 423 return false; 424 } 425 426 //---------------------------------------------------------------------- 427 // Get the demangled name if there is one, else return the mangled name. 428 //---------------------------------------------------------------------- 429 ConstString Mangled::GetName(lldb::LanguageType language, 430 Mangled::NamePreference preference) const { 431 if (preference == ePreferMangled && m_mangled) 432 return m_mangled; 433 434 ConstString demangled = GetDemangledName(language); 435 436 if (preference == ePreferDemangledWithoutArguments) { 437 return get_demangled_name_without_arguments(m_mangled, demangled); 438 } 439 if (preference == ePreferDemangled) { 440 // Call the accessor to make sure we get a demangled name in case it hasn't 441 // been demangled yet... 442 if (demangled) 443 return demangled; 444 return m_mangled; 445 } 446 return demangled; 447 } 448 449 //---------------------------------------------------------------------- 450 // Dump a Mangled object to stream "s". We don't force our demangled name to be 451 // computed currently (we don't use the accessor). 452 //---------------------------------------------------------------------- 453 void Mangled::Dump(Stream *s) const { 454 if (m_mangled) { 455 *s << ", mangled = " << m_mangled; 456 } 457 if (m_demangled) { 458 const char *demangled = m_demangled.AsCString(); 459 s->Printf(", demangled = %s", demangled[0] ? demangled : "<error>"); 460 } 461 } 462 463 //---------------------------------------------------------------------- 464 // Dumps a debug version of this string with extra object and state information 465 // to stream "s". 466 //---------------------------------------------------------------------- 467 void Mangled::DumpDebug(Stream *s) const { 468 s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2), 469 static_cast<const void *>(this)); 470 m_mangled.DumpDebug(s); 471 s->Printf(", demangled = "); 472 m_demangled.DumpDebug(s); 473 } 474 475 //---------------------------------------------------------------------- 476 // Return the size in byte that this object takes in memory. The size includes 477 // the size of the objects it owns, and not the strings that it references 478 // because they are shared strings. 479 //---------------------------------------------------------------------- 480 size_t Mangled::MemorySize() const { 481 return m_mangled.MemorySize() + m_demangled.MemorySize(); 482 } 483 484 //---------------------------------------------------------------------- 485 // We "guess" the language because we can't determine a symbol's language from 486 // it's name. For example, a Pascal symbol can be mangled using the C++ 487 // Itanium scheme, and defined in a compilation unit within the same module as 488 // other C++ units. In addition, different targets could have different ways 489 // of mangling names from a given language, likewise the compilation units 490 // within those targets. 491 //---------------------------------------------------------------------- 492 lldb::LanguageType Mangled::GuessLanguage() const { 493 ConstString mangled = GetMangledName(); 494 if (mangled) { 495 if (GetDemangledName(lldb::eLanguageTypeUnknown)) { 496 const char *mangled_name = mangled.GetCString(); 497 if (CPlusPlusLanguage::IsCPPMangledName(mangled_name)) 498 return lldb::eLanguageTypeC_plus_plus; 499 else if (ObjCLanguage::IsPossibleObjCMethodName(mangled_name)) 500 return lldb::eLanguageTypeObjC; 501 } 502 } else { 503 // ObjC names aren't really mangled, so they won't necessarily be in the 504 // mangled name slot. 505 ConstString demangled_name = GetDemangledName(lldb::eLanguageTypeUnknown); 506 if (demangled_name 507 && ObjCLanguage::IsPossibleObjCMethodName(demangled_name.GetCString())) 508 return lldb::eLanguageTypeObjC; 509 510 } 511 return lldb::eLanguageTypeUnknown; 512 } 513 514 //---------------------------------------------------------------------- 515 // Dump OBJ to the supplied stream S. 516 //---------------------------------------------------------------------- 517 Stream &operator<<(Stream &s, const Mangled &obj) { 518 if (obj.GetMangledName()) 519 s << "mangled = '" << obj.GetMangledName() << "'"; 520 521 const ConstString &demangled = 522 obj.GetDemangledName(lldb::eLanguageTypeUnknown); 523 if (demangled) 524 s << ", demangled = '" << demangled << '\''; 525 else 526 s << ", demangled = <error>"; 527 return s; 528 } 529