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 llvm_unreachable("Fully covered switch above!"); 358 } 359 360 //---------------------------------------------------------------------- 361 // Generate the demangled name on demand using this accessor. Code in this 362 // class will need to use this accessor if it wishes to decode the demangled 363 // name. The result is cached and will be kept until a new string value is 364 // supplied to this object, or until the end of the object's lifetime. 365 //---------------------------------------------------------------------- 366 const ConstString & 367 Mangled::GetDemangledName(lldb::LanguageType language) const { 368 // Check to make sure we have a valid mangled name and that we haven't 369 // already decoded our mangled name. 370 if (m_mangled && m_demangled.IsNull()) { 371 // We need to generate and cache the demangled name. 372 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 373 Timer scoped_timer(func_cat, "Mangled::GetDemangledName (m_mangled = %s)", 374 m_mangled.GetCString()); 375 376 // Don't bother running anything that isn't mangled 377 const char *mangled_name = m_mangled.GetCString(); 378 ManglingScheme mangling_scheme{cstring_mangling_scheme(mangled_name)}; 379 if (mangling_scheme != eManglingSchemeNone && 380 !m_mangled.GetMangledCounterpart(m_demangled)) { 381 // We didn't already mangle this name, demangle it and if all goes well 382 // add it to our map. 383 char *demangled_name = nullptr; 384 switch (mangling_scheme) { 385 case eManglingSchemeMSVC: 386 demangled_name = GetMSVCDemangledStr(mangled_name); 387 break; 388 case eManglingSchemeItanium: { 389 demangled_name = GetItaniumDemangledStr(mangled_name); 390 break; 391 } 392 case eManglingSchemeNone: 393 llvm_unreachable("eManglingSchemeNone was handled already"); 394 } 395 if (demangled_name) { 396 m_demangled.SetStringWithMangledCounterpart( 397 llvm::StringRef(demangled_name), m_mangled); 398 free(demangled_name); 399 } 400 } 401 if (m_demangled.IsNull()) { 402 // Set the demangled string to the empty string to indicate we tried to 403 // parse it once and failed. 404 m_demangled.SetCString(""); 405 } 406 } 407 408 return m_demangled; 409 } 410 411 ConstString 412 Mangled::GetDisplayDemangledName(lldb::LanguageType language) const { 413 return GetDemangledName(language); 414 } 415 416 bool Mangled::NameMatches(const RegularExpression ®ex, 417 lldb::LanguageType language) const { 418 if (m_mangled && regex.Execute(m_mangled.AsCString())) 419 return true; 420 421 ConstString demangled = GetDemangledName(language); 422 if (demangled && regex.Execute(demangled.AsCString())) 423 return true; 424 return false; 425 } 426 427 //---------------------------------------------------------------------- 428 // Get the demangled name if there is one, else return the mangled name. 429 //---------------------------------------------------------------------- 430 ConstString Mangled::GetName(lldb::LanguageType language, 431 Mangled::NamePreference preference) const { 432 if (preference == ePreferMangled && m_mangled) 433 return m_mangled; 434 435 ConstString demangled = GetDemangledName(language); 436 437 if (preference == ePreferDemangledWithoutArguments) { 438 return get_demangled_name_without_arguments(m_mangled, demangled); 439 } 440 if (preference == ePreferDemangled) { 441 // Call the accessor to make sure we get a demangled name in case it hasn't 442 // been demangled yet... 443 if (demangled) 444 return demangled; 445 return m_mangled; 446 } 447 return demangled; 448 } 449 450 //---------------------------------------------------------------------- 451 // Dump a Mangled object to stream "s". We don't force our demangled name to be 452 // computed currently (we don't use the accessor). 453 //---------------------------------------------------------------------- 454 void Mangled::Dump(Stream *s) const { 455 if (m_mangled) { 456 *s << ", mangled = " << m_mangled; 457 } 458 if (m_demangled) { 459 const char *demangled = m_demangled.AsCString(); 460 s->Printf(", demangled = %s", demangled[0] ? demangled : "<error>"); 461 } 462 } 463 464 //---------------------------------------------------------------------- 465 // Dumps a debug version of this string with extra object and state information 466 // to stream "s". 467 //---------------------------------------------------------------------- 468 void Mangled::DumpDebug(Stream *s) const { 469 s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2), 470 static_cast<const void *>(this)); 471 m_mangled.DumpDebug(s); 472 s->Printf(", demangled = "); 473 m_demangled.DumpDebug(s); 474 } 475 476 //---------------------------------------------------------------------- 477 // Return the size in byte that this object takes in memory. The size includes 478 // the size of the objects it owns, and not the strings that it references 479 // because they are shared strings. 480 //---------------------------------------------------------------------- 481 size_t Mangled::MemorySize() const { 482 return m_mangled.MemorySize() + m_demangled.MemorySize(); 483 } 484 485 //---------------------------------------------------------------------- 486 // We "guess" the language because we can't determine a symbol's language from 487 // it's name. For example, a Pascal symbol can be mangled using the C++ 488 // Itanium scheme, and defined in a compilation unit within the same module as 489 // other C++ units. In addition, different targets could have different ways 490 // of mangling names from a given language, likewise the compilation units 491 // within those targets. 492 //---------------------------------------------------------------------- 493 lldb::LanguageType Mangled::GuessLanguage() const { 494 ConstString mangled = GetMangledName(); 495 if (mangled) { 496 if (GetDemangledName(lldb::eLanguageTypeUnknown)) { 497 const char *mangled_name = mangled.GetCString(); 498 if (CPlusPlusLanguage::IsCPPMangledName(mangled_name)) 499 return lldb::eLanguageTypeC_plus_plus; 500 else if (ObjCLanguage::IsPossibleObjCMethodName(mangled_name)) 501 return lldb::eLanguageTypeObjC; 502 } 503 } else { 504 // ObjC names aren't really mangled, so they won't necessarily be in the 505 // mangled name slot. 506 ConstString demangled_name = GetDemangledName(lldb::eLanguageTypeUnknown); 507 if (demangled_name 508 && ObjCLanguage::IsPossibleObjCMethodName(demangled_name.GetCString())) 509 return lldb::eLanguageTypeObjC; 510 511 } 512 return lldb::eLanguageTypeUnknown; 513 } 514 515 //---------------------------------------------------------------------- 516 // Dump OBJ to the supplied stream S. 517 //---------------------------------------------------------------------- 518 Stream &operator<<(Stream &s, const Mangled &obj) { 519 if (obj.GetMangledName()) 520 s << "mangled = '" << obj.GetMangledName() << "'"; 521 522 const ConstString &demangled = 523 obj.GetDemangledName(lldb::eLanguageTypeUnknown); 524 if (demangled) 525 s << ", demangled = '" << demangled << '\''; 526 else 527 s << ", demangled = <error>"; 528 return s; 529 } 530