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