1 //===-- Mangled.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/Mangled.h"
10 
11 #include "lldb/Core/RichManglingContext.h"
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/Log.h"
14 #include "lldb/Utility/Logging.h"
15 #include "lldb/Utility/RegularExpression.h"
16 #include "lldb/Utility/Stream.h"
17 #include "lldb/lldb-enumerations.h"
18 
19 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
20 #include "Plugins/Language/ObjC/ObjCLanguage.h"
21 
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Demangle/Demangle.h"
24 #include "llvm/Support/Compiler.h"
25 
26 #include <mutex>
27 #include <string>
28 #include <utility>
29 
30 #include <cstdlib>
31 #include <cstring>
32 using namespace lldb_private;
33 
34 static inline bool cstring_is_mangled(llvm::StringRef s) {
35   return Mangled::GetManglingScheme(s) != Mangled::eManglingSchemeNone;
36 }
37 
38 static ConstString GetDemangledNameWithoutArguments(ConstString mangled,
39                                                     ConstString demangled) {
40   const char *mangled_name_cstr = mangled.GetCString();
41 
42   if (demangled && mangled_name_cstr && mangled_name_cstr[0]) {
43     if (mangled_name_cstr[0] == '_' && mangled_name_cstr[1] == 'Z' &&
44         (mangled_name_cstr[2] != 'T' && // avoid virtual table, VTT structure,
45                                         // typeinfo structure, and typeinfo
46                                         // mangled_name
47          mangled_name_cstr[2] != 'G' && // avoid guard variables
48          mangled_name_cstr[2] != 'Z')) // named local entities (if we eventually
49                                        // handle eSymbolTypeData, we will want
50                                        // this back)
51     {
52       CPlusPlusLanguage::MethodName cxx_method(demangled);
53       if (!cxx_method.GetBasename().empty()) {
54         std::string shortname;
55         if (!cxx_method.GetContext().empty())
56           shortname = cxx_method.GetContext().str() + "::";
57         shortname += cxx_method.GetBasename().str();
58         return ConstString(shortname);
59       }
60     }
61   }
62   if (demangled)
63     return demangled;
64   return mangled;
65 }
66 
67 #pragma mark Mangled
68 
69 Mangled::ManglingScheme Mangled::GetManglingScheme(llvm::StringRef const name) {
70   if (name.empty())
71     return Mangled::eManglingSchemeNone;
72 
73   if (name.startswith("?"))
74     return Mangled::eManglingSchemeMSVC;
75 
76   if (name.startswith("_Z"))
77     return Mangled::eManglingSchemeItanium;
78 
79   // ___Z is a clang extension of block invocations
80   if (name.startswith("___Z"))
81     return Mangled::eManglingSchemeItanium;
82 
83   return Mangled::eManglingSchemeNone;
84 }
85 
86 Mangled::Mangled(ConstString s) : m_mangled(), m_demangled() {
87   if (s)
88     SetValue(s);
89 }
90 
91 Mangled::Mangled(llvm::StringRef name) {
92   if (!name.empty())
93     SetValue(ConstString(name));
94 }
95 
96 // Convert to pointer operator. This allows code to check any Mangled objects
97 // to see if they contain anything valid using code such as:
98 //
99 //  Mangled mangled(...);
100 //  if (mangled)
101 //  { ...
102 Mangled::operator void *() const {
103   return (m_mangled) ? const_cast<Mangled *>(this) : nullptr;
104 }
105 
106 // Logical NOT operator. This allows code to check any Mangled objects to see
107 // if they are invalid using code such as:
108 //
109 //  Mangled mangled(...);
110 //  if (!file_spec)
111 //  { ...
112 bool Mangled::operator!() const { return !m_mangled; }
113 
114 // Clear the mangled and demangled values.
115 void Mangled::Clear() {
116   m_mangled.Clear();
117   m_demangled.Clear();
118 }
119 
120 // Compare the string values.
121 int Mangled::Compare(const Mangled &a, const Mangled &b) {
122   return ConstString::Compare(a.GetName(ePreferMangled),
123                               b.GetName(ePreferMangled));
124 }
125 
126 // Set the string value in this objects. If "mangled" is true, then the mangled
127 // named is set with the new value in "s", else the demangled name is set.
128 void Mangled::SetValue(ConstString s, bool mangled) {
129   if (s) {
130     if (mangled) {
131       m_demangled.Clear();
132       m_mangled = s;
133     } else {
134       m_demangled = s;
135       m_mangled.Clear();
136     }
137   } else {
138     m_demangled.Clear();
139     m_mangled.Clear();
140   }
141 }
142 
143 void Mangled::SetValue(ConstString name) {
144   if (name) {
145     if (cstring_is_mangled(name.GetStringRef())) {
146       m_demangled.Clear();
147       m_mangled = name;
148     } else {
149       m_demangled = name;
150       m_mangled.Clear();
151     }
152   } else {
153     m_demangled.Clear();
154     m_mangled.Clear();
155   }
156 }
157 
158 // Local helpers for different demangling implementations.
159 static char *GetMSVCDemangledStr(const char *M) {
160   char *demangled_cstr = llvm::microsoftDemangle(
161       M, nullptr, nullptr, nullptr, nullptr,
162       llvm::MSDemangleFlags(llvm::MSDF_NoAccessSpecifier |
163                             llvm::MSDF_NoCallingConvention |
164                             llvm::MSDF_NoMemberType));
165 
166   if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
167     if (demangled_cstr && demangled_cstr[0])
168       LLDB_LOGF(log, "demangled msvc: %s -> \"%s\"", M, demangled_cstr);
169     else
170       LLDB_LOGF(log, "demangled msvc: %s -> error", M);
171   }
172 
173   return demangled_cstr;
174 }
175 
176 static char *GetItaniumDemangledStr(const char *M) {
177   char *demangled_cstr = nullptr;
178 
179   llvm::ItaniumPartialDemangler ipd;
180   bool err = ipd.partialDemangle(M);
181   if (!err) {
182     // Default buffer and size (will realloc in case it's too small).
183     size_t demangled_size = 80;
184     demangled_cstr = static_cast<char *>(std::malloc(demangled_size));
185     demangled_cstr = ipd.finishDemangle(demangled_cstr, &demangled_size);
186 
187     assert(demangled_cstr &&
188            "finishDemangle must always succeed if partialDemangle did");
189     assert(demangled_cstr[demangled_size - 1] == '\0' &&
190            "Expected demangled_size to return length including trailing null");
191   }
192 
193   if (Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DEMANGLE)) {
194     if (demangled_cstr)
195       LLDB_LOGF(log, "demangled itanium: %s -> \"%s\"", M, demangled_cstr);
196     else
197       LLDB_LOGF(log, "demangled itanium: %s -> error: failed to demangle", M);
198   }
199 
200   return demangled_cstr;
201 }
202 
203 // Explicit demangling for scheduled requests during batch processing. This
204 // makes use of ItaniumPartialDemangler's rich demangle info
205 bool Mangled::DemangleWithRichManglingInfo(
206     RichManglingContext &context, SkipMangledNameFn *skip_mangled_name) {
207   // Others are not meant to arrive here. ObjC names or C's main() for example
208   // have their names stored in m_demangled, while m_mangled is empty.
209   assert(m_mangled);
210 
211   // Check whether or not we are interested in this name at all.
212   ManglingScheme scheme = GetManglingScheme(m_mangled.GetStringRef());
213   if (skip_mangled_name && skip_mangled_name(m_mangled.GetStringRef(), scheme))
214     return false;
215 
216   switch (scheme) {
217   case eManglingSchemeNone:
218     // The current mangled_name_filter would allow llvm_unreachable here.
219     return false;
220 
221   case eManglingSchemeItanium:
222     // We want the rich mangling info here, so we don't care whether or not
223     // there is a demangled string in the pool already.
224     if (context.FromItaniumName(m_mangled)) {
225       // If we got an info, we have a name. Copy to string pool and connect the
226       // counterparts to accelerate later access in GetDemangledName().
227       context.ParseFullName();
228       m_demangled.SetStringWithMangledCounterpart(context.GetBufferRef(),
229                                                   m_mangled);
230       return true;
231     } else {
232       m_demangled.SetCString("");
233       return false;
234     }
235 
236   case eManglingSchemeMSVC: {
237     // We have no rich mangling for MSVC-mangled names yet, so first try to
238     // demangle it if necessary.
239     if (!m_demangled && !m_mangled.GetMangledCounterpart(m_demangled)) {
240       if (char *d = GetMSVCDemangledStr(m_mangled.GetCString())) {
241         // If we got an info, we have a name. Copy to string pool and connect
242         // the counterparts to accelerate later access in GetDemangledName().
243         m_demangled.SetStringWithMangledCounterpart(llvm::StringRef(d),
244                                                     m_mangled);
245         ::free(d);
246       } else {
247         m_demangled.SetCString("");
248       }
249     }
250 
251     if (m_demangled.IsEmpty()) {
252       // Cannot demangle it, so don't try parsing.
253       return false;
254     } else {
255       // Demangled successfully, we can try and parse it with
256       // CPlusPlusLanguage::MethodName.
257       return context.FromCxxMethodName(m_demangled);
258     }
259   }
260   }
261   llvm_unreachable("Fully covered switch above!");
262 }
263 
264 // Generate the demangled name on demand using this accessor. Code in this
265 // class will need to use this accessor if it wishes to decode the demangled
266 // name. The result is cached and will be kept until a new string value is
267 // supplied to this object, or until the end of the object's lifetime.
268 ConstString Mangled::GetDemangledName() const {
269   // Check to make sure we have a valid mangled name and that we haven't
270   // already decoded our mangled name.
271   if (m_mangled && m_demangled.IsNull()) {
272     // Don't bother running anything that isn't mangled
273     const char *mangled_name = m_mangled.GetCString();
274     ManglingScheme mangling_scheme = GetManglingScheme(m_mangled.GetStringRef());
275     if (mangling_scheme != eManglingSchemeNone &&
276         !m_mangled.GetMangledCounterpart(m_demangled)) {
277       // We didn't already mangle this name, demangle it and if all goes well
278       // add it to our map.
279       char *demangled_name = nullptr;
280       switch (mangling_scheme) {
281       case eManglingSchemeMSVC:
282         demangled_name = GetMSVCDemangledStr(mangled_name);
283         break;
284       case eManglingSchemeItanium: {
285         demangled_name = GetItaniumDemangledStr(mangled_name);
286         break;
287       }
288       case eManglingSchemeNone:
289         llvm_unreachable("eManglingSchemeNone was handled already");
290       }
291       if (demangled_name) {
292         m_demangled.SetStringWithMangledCounterpart(
293             llvm::StringRef(demangled_name), m_mangled);
294         free(demangled_name);
295       }
296     }
297     if (m_demangled.IsNull()) {
298       // Set the demangled string to the empty string to indicate we tried to
299       // parse it once and failed.
300       m_demangled.SetCString("");
301     }
302   }
303 
304   return m_demangled;
305 }
306 
307 ConstString
308 Mangled::GetDisplayDemangledName() const {
309   return GetDemangledName();
310 }
311 
312 bool Mangled::NameMatches(const RegularExpression &regex) const {
313   if (m_mangled && regex.Execute(m_mangled.GetStringRef()))
314     return true;
315 
316   ConstString demangled = GetDemangledName();
317   return demangled && regex.Execute(demangled.GetStringRef());
318 }
319 
320 // Get the demangled name if there is one, else return the mangled name.
321 ConstString Mangled::GetName(Mangled::NamePreference preference) const {
322   if (preference == ePreferMangled && m_mangled)
323     return m_mangled;
324 
325   ConstString demangled = GetDemangledName();
326 
327   if (preference == ePreferDemangledWithoutArguments) {
328     return GetDemangledNameWithoutArguments(m_mangled, demangled);
329   }
330   if (preference == ePreferDemangled) {
331     // Call the accessor to make sure we get a demangled name in case it hasn't
332     // been demangled yet...
333     if (demangled)
334       return demangled;
335     return m_mangled;
336   }
337   return demangled;
338 }
339 
340 // Dump a Mangled object to stream "s". We don't force our demangled name to be
341 // computed currently (we don't use the accessor).
342 void Mangled::Dump(Stream *s) const {
343   if (m_mangled) {
344     *s << ", mangled = " << m_mangled;
345   }
346   if (m_demangled) {
347     const char *demangled = m_demangled.AsCString();
348     s->Printf(", demangled = %s", demangled[0] ? demangled : "<error>");
349   }
350 }
351 
352 // Dumps a debug version of this string with extra object and state information
353 // to stream "s".
354 void Mangled::DumpDebug(Stream *s) const {
355   s->Printf("%*p: Mangled mangled = ", static_cast<int>(sizeof(void *) * 2),
356             static_cast<const void *>(this));
357   m_mangled.DumpDebug(s);
358   s->Printf(", demangled = ");
359   m_demangled.DumpDebug(s);
360 }
361 
362 // Return the size in byte that this object takes in memory. The size includes
363 // the size of the objects it owns, and not the strings that it references
364 // because they are shared strings.
365 size_t Mangled::MemorySize() const {
366   return m_mangled.MemorySize() + m_demangled.MemorySize();
367 }
368 
369 // We "guess" the language because we can't determine a symbol's language from
370 // it's name.  For example, a Pascal symbol can be mangled using the C++
371 // Itanium scheme, and defined in a compilation unit within the same module as
372 // other C++ units.  In addition, different targets could have different ways
373 // of mangling names from a given language, likewise the compilation units
374 // within those targets.
375 lldb::LanguageType Mangled::GuessLanguage() const {
376   ConstString mangled = GetMangledName();
377 
378   if (mangled) {
379     const char *mangled_name = mangled.GetCString();
380     if (CPlusPlusLanguage::IsCPPMangledName(mangled_name))
381       return lldb::eLanguageTypeC_plus_plus;
382   } else {
383     // ObjC names aren't really mangled, so they won't necessarily be in the
384     // mangled name slot.
385     ConstString demangled_name = GetDemangledName();
386     if (demangled_name
387         && ObjCLanguage::IsPossibleObjCMethodName(demangled_name.GetCString()))
388       return lldb::eLanguageTypeObjC;
389 
390   }
391   return lldb::eLanguageTypeUnknown;
392 }
393 
394 // Dump OBJ to the supplied stream S.
395 Stream &operator<<(Stream &s, const Mangled &obj) {
396   if (obj.GetMangledName())
397     s << "mangled = '" << obj.GetMangledName() << "'";
398 
399   ConstString demangled = obj.GetDemangledName();
400   if (demangled)
401     s << ", demangled = '" << demangled << '\'';
402   else
403     s << ", demangled = <error>";
404   return s;
405 }
406